fix(opencode): pass OAuth scopes to GoogleAuth for Vertex AI (#15110)
Co-authored-by: Aiden Cline <aidenpcline@gmail.com>
This commit is contained in:
co-authored by
Aiden Cline
parent
acca8864ba
commit
797c689ec2
@@ -43,9 +43,9 @@ function authFetch(fetchWithRuntimeOptions?: unknown) {
|
|||||||
// do not, so inject a Google access token into their fetch path.
|
// do not, so inject a Google access token into their fetch path.
|
||||||
return async (input: Parameters<typeof fetch>[0], init?: RequestInit) => {
|
return async (input: Parameters<typeof fetch>[0], init?: RequestInit) => {
|
||||||
const { GoogleAuth } = await import("google-auth-library")
|
const { GoogleAuth } = await import("google-auth-library")
|
||||||
const auth = new GoogleAuth()
|
const auth = new GoogleAuth({ scopes: ["https://www.googleapis.com/auth/cloud-platform"] })
|
||||||
const client = await auth.getApplicationDefault()
|
const client = await auth.getClient()
|
||||||
const token = await client.credential.getAccessToken()
|
const token = await client.getAccessToken()
|
||||||
const headers = new Headers(init?.headers)
|
const headers = new Headers(init?.headers)
|
||||||
headers.set("Authorization", `Bearer ${token.token}`)
|
headers.set("Authorization", `Bearer ${token.token}`)
|
||||||
return typeof fetchWithRuntimeOptions === "function"
|
return typeof fetchWithRuntimeOptions === "function"
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import { ProviderV2 } from "@opencode-ai/core/provider"
|
|||||||
import { fakeSelectorSdk, it, model, withEnv } from "./provider-helper"
|
import { fakeSelectorSdk, it, model, withEnv } from "./provider-helper"
|
||||||
|
|
||||||
const vertexOptions: Record<string, any>[] = []
|
const vertexOptions: Record<string, any>[] = []
|
||||||
|
const googleAuthOptions: Record<string, any>[] = []
|
||||||
|
|
||||||
void mock.module("@ai-sdk/google-vertex", () => ({
|
void mock.module("@ai-sdk/google-vertex", () => ({
|
||||||
createVertex: (options: Record<string, any>) => {
|
createVertex: (options: Record<string, any>) => {
|
||||||
@@ -19,12 +20,14 @@ void mock.module("@ai-sdk/google-vertex", () => ({
|
|||||||
|
|
||||||
void mock.module("google-auth-library", () => ({
|
void mock.module("google-auth-library", () => ({
|
||||||
GoogleAuth: class {
|
GoogleAuth: class {
|
||||||
async getApplicationDefault() {
|
constructor(options: Record<string, any>) {
|
||||||
|
googleAuthOptions.push(options)
|
||||||
|
}
|
||||||
|
|
||||||
|
async getClient() {
|
||||||
return {
|
return {
|
||||||
credential: {
|
async getAccessToken() {
|
||||||
async getAccessToken() {
|
return { token: "vertex-token" }
|
||||||
return { token: "vertex-token" }
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -247,6 +250,7 @@ describe("GoogleVertexPlugin", () => {
|
|||||||
|
|
||||||
it.effect("keeps Google auth fetch for OpenAI-compatible Vertex endpoints", () =>
|
it.effect("keeps Google auth fetch for OpenAI-compatible Vertex endpoints", () =>
|
||||||
Effect.gen(function* () {
|
Effect.gen(function* () {
|
||||||
|
googleAuthOptions.length = 0
|
||||||
const fetchCalls: { input: Parameters<typeof fetch>[0]; init?: RequestInit }[] = []
|
const fetchCalls: { input: Parameters<typeof fetch>[0]; init?: RequestInit }[] = []
|
||||||
const plugin = yield* PluginV2.Service
|
const plugin = yield* PluginV2.Service
|
||||||
yield* plugin.add(GoogleVertexPlugin)
|
yield* plugin.add(GoogleVertexPlugin)
|
||||||
@@ -292,6 +296,7 @@ describe("GoogleVertexPlugin", () => {
|
|||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
expect(fetchCalls).toHaveLength(1)
|
expect(fetchCalls).toHaveLength(1)
|
||||||
|
expect(googleAuthOptions).toEqual([{ scopes: ["https://www.googleapis.com/auth/cloud-platform"] }])
|
||||||
expect(fetchCalls[0].input).toBe("https://vertex.example")
|
expect(fetchCalls[0].input).toBe("https://vertex.example")
|
||||||
expect(new Headers(fetchCalls[0].init?.headers).get("authorization")).toBe("Bearer vertex-token")
|
expect(new Headers(fetchCalls[0].init?.headers).get("authorization")).toBe("Bearer vertex-token")
|
||||||
expect(new Headers(fetchCalls[0].init?.headers).get("x-test")).toBe("1")
|
expect(new Headers(fetchCalls[0].init?.headers).get("x-test")).toBe("1")
|
||||||
|
|||||||
@@ -502,9 +502,9 @@ function custom(dep: CustomDep): Record<string, CustomLoader> {
|
|||||||
location,
|
location,
|
||||||
fetch: async (input: RequestInfo | URL, init?: RequestInit) => {
|
fetch: async (input: RequestInfo | URL, init?: RequestInit) => {
|
||||||
const { GoogleAuth } = await import("google-auth-library")
|
const { GoogleAuth } = await import("google-auth-library")
|
||||||
const auth = new GoogleAuth()
|
const auth = new GoogleAuth({ scopes: ["https://www.googleapis.com/auth/cloud-platform"] })
|
||||||
const client = await auth.getApplicationDefault()
|
const client = await auth.getClient()
|
||||||
const token = await client.credential.getAccessToken()
|
const token = await client.getAccessToken()
|
||||||
|
|
||||||
const headers = new Headers(init?.headers)
|
const headers = new Headers(init?.headers)
|
||||||
headers.set("Authorization", `Bearer ${token.token}`)
|
headers.set("Authorization", `Bearer ${token.token}`)
|
||||||
|
|||||||
Reference in New Issue
Block a user