-
Notifications
You must be signed in to change notification settings - Fork 9.6k
feat: add google-vertex-openapi provider with ADC authentication #10303
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
leehack
wants to merge
6
commits into
anomalyco:dev
Choose a base branch
from
leehack:fix/vertex-glm-openapi
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+276
−10
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f8d1fc3
fix(provider): add google-vertex-openapi provider for GLM-4 model sup…
leehack c4e4b33
refactor: use ADC with google-auth-library instead of gcloud subprocess
leehack 5aba6e0
Consolidate the provider into google-vertex
leehack 7b520a0
Apply suggestions from code review
leehack 3d0268d
Merge branch 'dev' into fix/vertex-glm-openapi
leehack 2bd9b14
fix(provider): surgically sanitize Google SDK options to restore Gemi…
leehack File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,203 @@ | ||
| import { test, expect, mock, describe, beforeAll, afterAll } from "bun:test" | ||
| import path from "path" | ||
|
|
||
| // Mock BunProc to return pkg name | ||
| mock.module("../../src/bun/index", () => ({ | ||
| BunProc: { | ||
| install: async (pkg: string, _version?: string) => { | ||
| return pkg | ||
| }, | ||
| run: async () => { throw new Error("BunProc.run should not be called in tests") }, | ||
| which: () => process.execPath, | ||
| InstallFailedError: class extends Error { }, | ||
| }, | ||
| })) | ||
|
|
||
| // Mock auth plugins | ||
| const mockPlugin = () => ({}) | ||
| mock.module("opencode-copilot-auth", () => ({ default: mockPlugin })) | ||
| mock.module("opencode-anthropic-auth", () => ({ default: mockPlugin })) | ||
| mock.module("@gitlab/opencode-gitlab-auth", () => ({ default: mockPlugin })) | ||
|
|
||
| // Mock Google Auth Library (required for official SDK initialization) | ||
| mock.module("google-auth-library", () => ({ | ||
| GoogleAuth: class { | ||
| async getApplicationDefault() { | ||
| return { | ||
| credential: { | ||
| getAccessToken: async () => ({ | ||
| token: "mock-access-token", | ||
| }), | ||
| }, | ||
| } | ||
| } | ||
| }, | ||
| })) | ||
|
|
||
| describe("Google Vertex Provider Merge", () => { | ||
| let Provider: any | ||
| let Instance: any | ||
| let Env: any | ||
| let tmpdir: any | ||
|
|
||
| beforeAll(async () => { | ||
| // Dynamic import to ensure mocks are active before modules load | ||
| const fixture = await import("../fixture/fixture") | ||
| tmpdir = fixture.tmpdir | ||
|
|
||
| const instance = await import("../../src/project/instance") | ||
| Instance = instance.Instance | ||
|
|
||
| const provider = await import("../../src/provider/provider") | ||
| Provider = provider.Provider | ||
|
|
||
| const env = await import("../../src/env") | ||
| Env = env.Env | ||
| }) | ||
|
|
||
| test("loader returns merged options including baseURL and fetch", async () => { | ||
| await using tmp = await tmpdir({ | ||
| init: async (dir: string) => { | ||
| await Bun.write( | ||
| path.join(dir, "opencode.json"), | ||
| JSON.stringify({ | ||
| $schema: "https://opencode.ai/config.json", | ||
| }), | ||
| ) | ||
| }, | ||
| }) | ||
| await Instance.provide({ | ||
| directory: tmp.path, | ||
| init: async () => { | ||
| Env.set("GOOGLE_CLOUD_PROJECT", "test-project") | ||
| Env.set("GOOGLE_CLOUD_LOCATION", "us-central1") | ||
| }, | ||
| fn: async () => { | ||
| const providers = await Provider.list() | ||
| const vertex = providers["google-vertex"] | ||
| expect(vertex).toBeDefined() | ||
| expect(vertex.options.project).toBe("test-project") | ||
| expect(vertex.options.location).toBe("us-central1") | ||
| expect(vertex.options.baseURL).toContain("us-central1-aiplatform.googleapis.com") | ||
| expect(vertex.options.fetch).toBeDefined() | ||
| }, | ||
| }) | ||
| }) | ||
|
|
||
| test("official SDK options STRIP googleapis.com baseURL but RETAIN custom proxy", async () => { | ||
| await using tmp = await tmpdir({ | ||
| init: async (dir: string) => { | ||
| await Bun.write( | ||
| path.join(dir, "opencode.json"), | ||
| JSON.stringify({ | ||
| $schema: "https://opencode.ai/config.json", | ||
| provider: { | ||
| "google-vertex": { | ||
| models: { | ||
| "google-stripped": { api: { npm: "@ai-sdk/google-vertex", id: "gemini-1.5-pro" } }, | ||
| "proxy-preserved": { api: { npm: "@ai-sdk/google-vertex", id: "gemini-1.5-pro" } }, | ||
| "localhost-preserved": { api: { npm: "@ai-sdk/google-vertex", id: "gemini-1.5-pro" } } | ||
| } | ||
| } | ||
| } | ||
| }), | ||
| ) | ||
| }, | ||
| }) | ||
|
|
||
| await Instance.provide({ | ||
| directory: tmp.path, | ||
| init: async () => { | ||
| Env.set("GOOGLE_CLOUD_PROJECT", "test-project") | ||
| }, | ||
| fn: async () => { | ||
| const providers = await Provider.list() | ||
| const vertex = providers["google-vertex"] | ||
|
|
||
| // Case 1: Standard Google URL -> should be stripped | ||
| vertex.options.baseURL = "https://us-central1-aiplatform.googleapis.com/v1" | ||
| const modelGoogle = await Provider.getModel("google-vertex", "google-stripped") | ||
| const sdkGoogle = await Provider.getLanguage(modelGoogle) as any | ||
| expect(sdkGoogle.config.baseURL).not.toBe("https://us-central1-aiplatform.googleapis.com/v1") | ||
|
|
||
| // Case 2: Custom Proxy -> should be RETAINED | ||
| // We MUST use a different model to avoid cache in getLanguage | ||
| vertex.options.baseURL = "https://my-proxy.com/v1" | ||
| const modelProxy = await Provider.getModel("google-vertex", "proxy-preserved") | ||
| const sdkProxy = await Provider.getLanguage(modelProxy) as any | ||
| expect(sdkProxy.config.baseURL).toBe("https://my-proxy.com/v1") | ||
|
|
||
| // Case 3: Localhost -> should be RETAINED | ||
| vertex.options.baseURL = "http://localhost:8080/v1" | ||
| const modelLocal = await Provider.getModel("google-vertex", "localhost-preserved") | ||
| const sdkLocal = await Provider.getLanguage(modelLocal) as any | ||
| expect(sdkLocal.config.baseURL).toBe("http://localhost:8080/v1") | ||
| }, | ||
| }) | ||
| }) | ||
|
|
||
| test("OpenAPI model options RETAIN baseURL and fetch", async () => { | ||
| const server = Bun.serve({ | ||
| port: 0, | ||
| fetch(req) { | ||
| return new Response(JSON.stringify({ id: "test", choices: [] }), { status: 200 }) | ||
| } | ||
| }) | ||
|
|
||
| await using tmp = await tmpdir({ | ||
| init: async (dir: string) => { | ||
| await Bun.write( | ||
| path.join(dir, "opencode.json"), | ||
| JSON.stringify({ | ||
| $schema: "https://opencode.ai/config.json", | ||
| provider: { | ||
| "google-vertex": { | ||
| models: { | ||
| "openapi-model": { | ||
| protocol: "openapi", | ||
| id: "gemini-1.5-pro-alias" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }), | ||
| ) | ||
| }, | ||
| }) | ||
|
|
||
| await Instance.provide({ | ||
| directory: tmp.path, | ||
| init: async () => { | ||
| Env.set("GOOGLE_CLOUD_PROJECT", "test-project") | ||
| Env.set("GOOGLE_CLOUD_LOCATION", "us-central1") | ||
| }, | ||
| fn: async () => { | ||
| // Trigger SDK loading | ||
| const model = await Provider.getModel("google-vertex", "openapi-model") | ||
| expect(model).toBeDefined() | ||
| expect(model.api.npm).toBe("@ai-sdk/openai-compatible") | ||
|
|
||
| // Manually inject baseURL into the provider options for this test session | ||
| const providers = await Provider.list() | ||
| const vertex = providers["google-vertex"] | ||
| vertex.options.baseURL = server.url.origin + "/v1" | ||
| vertex.options.fetch = fetch | ||
|
|
||
| const sdk = await Provider.getLanguage(model) | ||
|
|
||
| try { | ||
| await sdk.doGenerate({ | ||
| inputFormat: "messages", | ||
| mode: { type: "regular" }, | ||
| modelId: "test-model", | ||
| prompt: [{ role: "user", content: [{ type: "text", text: "test" }] }], | ||
| }) | ||
| } catch (e) { | ||
| // Success if we hit the server | ||
| } | ||
| }, | ||
| }) | ||
|
|
||
| server.stop() | ||
| }) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For tests, let's avoid writing mocks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rekram1-node I see this pattern used for provider tests in other files. What would be the recommended pattern?