Skip to content

Commit 797c689

Browse files
fix(opencode): pass OAuth scopes to GoogleAuth for Vertex AI (anomalyco#15110)
Co-authored-by: Aiden Cline <aidenpcline@gmail.com>
1 parent acca886 commit 797c689

3 files changed

Lines changed: 16 additions & 11 deletions

File tree

packages/core/src/plugin/provider/google-vertex.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ function authFetch(fetchWithRuntimeOptions?: unknown) {
4343
// do not, so inject a Google access token into their fetch path.
4444
return async (input: Parameters<typeof fetch>[0], init?: RequestInit) => {
4545
const { GoogleAuth } = await import("google-auth-library")
46-
const auth = new GoogleAuth()
47-
const client = await auth.getApplicationDefault()
48-
const token = await client.credential.getAccessToken()
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()
4949
const headers = new Headers(init?.headers)
5050
headers.set("Authorization", `Bearer ${token.token}`)
5151
return typeof fetchWithRuntimeOptions === "function"

packages/core/test/plugin/provider-google-vertex.test.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { ProviderV2 } from "@opencode-ai/core/provider"
77
import { fakeSelectorSdk, it, model, withEnv } from "./provider-helper"
88

99
const vertexOptions: Record<string, any>[] = []
10+
const googleAuthOptions: Record<string, any>[] = []
1011

1112
void mock.module("@ai-sdk/google-vertex", () => ({
1213
createVertex: (options: Record<string, any>) => {
@@ -19,12 +20,14 @@ void mock.module("@ai-sdk/google-vertex", () => ({
1920

2021
void mock.module("google-auth-library", () => ({
2122
GoogleAuth: class {
22-
async getApplicationDefault() {
23+
constructor(options: Record<string, any>) {
24+
googleAuthOptions.push(options)
25+
}
26+
27+
async getClient() {
2328
return {
24-
credential: {
25-
async getAccessToken() {
26-
return { token: "vertex-token" }
27-
},
29+
async getAccessToken() {
30+
return { token: "vertex-token" }
2831
},
2932
}
3033
}
@@ -247,6 +250,7 @@ describe("GoogleVertexPlugin", () => {
247250

248251
it.effect("keeps Google auth fetch for OpenAI-compatible Vertex endpoints", () =>
249252
Effect.gen(function* () {
253+
googleAuthOptions.length = 0
250254
const fetchCalls: { input: Parameters<typeof fetch>[0]; init?: RequestInit }[] = []
251255
const plugin = yield* PluginV2.Service
252256
yield* plugin.add(GoogleVertexPlugin)
@@ -292,6 +296,7 @@ describe("GoogleVertexPlugin", () => {
292296
}),
293297
)
294298
expect(fetchCalls).toHaveLength(1)
299+
expect(googleAuthOptions).toEqual([{ scopes: ["https://www.googleapis.com/auth/cloud-platform"] }])
295300
expect(fetchCalls[0].input).toBe("https://vertex.example")
296301
expect(new Headers(fetchCalls[0].init?.headers).get("authorization")).toBe("Bearer vertex-token")
297302
expect(new Headers(fetchCalls[0].init?.headers).get("x-test")).toBe("1")

packages/opencode/src/provider/provider.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -502,9 +502,9 @@ function custom(dep: CustomDep): Record<string, CustomLoader> {
502502
location,
503503
fetch: async (input: RequestInfo | URL, init?: RequestInit) => {
504504
const { GoogleAuth } = await import("google-auth-library")
505-
const auth = new GoogleAuth()
506-
const client = await auth.getApplicationDefault()
507-
const token = await client.credential.getAccessToken()
505+
const auth = new GoogleAuth({ scopes: ["https://www.googleapis.com/auth/cloud-platform"] })
506+
const client = await auth.getClient()
507+
const token = await client.getAccessToken()
508508

509509
const headers = new Headers(init?.headers)
510510
headers.set("Authorization", `Bearer ${token.token}`)

0 commit comments

Comments
 (0)