(fetchWithRuntimeOptions?: unknown)
| 39 | } |
| 40 | |
| 41 | function authFetch(fetchWithRuntimeOptions?: unknown) { |
| 42 | // Native Vertex SDKs handle ADC internally. OpenAI-compatible Vertex endpoints |
| 43 | // do not, so inject a Google access token into their fetch path. |
| 44 | return async (input: Parameters<typeof fetch>[0], init?: RequestInit) => { |
| 45 | const { GoogleAuth } = await import("google-auth-library") |
| 46 | const auth = new GoogleAuth({ scopes: ["https://www.googleapis.com/auth/cloud-platform"] }) |
| 47 | const client = await auth.getClient() |
| 48 | const token = await client.getAccessToken() |
| 49 | const headers = new Headers(init?.headers) |
| 50 | headers.set("Authorization", `Bearer ${token.token}`) |
| 51 | return typeof fetchWithRuntimeOptions === "function" |
| 52 | ? fetchWithRuntimeOptions(input, { ...init, headers }) |
| 53 | : fetch(input, { ...init, headers }) |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | export const GoogleVertexPlugin = define({ |
| 58 | id: "google-vertex", |
no test coverage detected