| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380 |
- import { AISDK } from "@opencode-ai/core/aisdk"
- import { describe, expect, mock } from "bun:test"
- import { Effect } from "effect"
- import { Catalog } from "@opencode-ai/core/catalog"
- import { Model } from "@opencode-ai/core/model"
- import { Plugin } from "@opencode-ai/core/plugin"
- import { PluginHost } from "@opencode-ai/core/plugin/host"
- import { GoogleVertexPlugin } from "@opencode-ai/core/plugin/provider/google-vertex"
- import { Provider } from "@opencode-ai/core/provider"
- import type { LanguageModelV3 } from "@ai-sdk/provider"
- import { testEffect } from "../lib/effect"
- import { PluginTestLayer } from "./fixture"
- const vertexOptions: Record<string, any>[] = []
- const googleAuthOptions: Record<string, any>[] = []
- const it = testEffect(PluginTestLayer)
- const addPlugin = Effect.fn(function* () {
- const plugin = yield* Plugin.Service
- const aisdk = yield* AISDK.Service
- const host = yield* PluginHost.make(plugin)
- yield* GoogleVertexPlugin.effect(host)
- })
- function required<T>(value: T | undefined): T {
- if (value === undefined) throw new Error("Expected value")
- return value
- }
- function withEnv<A, E, R>(vars: Record<string, string | undefined>, effect: () => Effect.Effect<A, E, R>) {
- return Effect.acquireUseRelease(
- Effect.sync(() => {
- const previous = Object.fromEntries(Object.keys(vars).map((key) => [key, process.env[key]]))
- Object.entries(vars).forEach(([key, value]) => {
- if (value === undefined) delete process.env[key]
- else process.env[key] = value
- })
- return previous
- }),
- effect,
- (previous) =>
- Effect.sync(() =>
- Object.entries(previous).forEach(([key, value]) => {
- if (value === undefined) delete process.env[key]
- else process.env[key] = value
- }),
- ),
- )
- }
- function fakeSelectorSdk(calls: string[]) {
- const make = (method: string) => (id: string) => {
- calls.push(`${method}:${id}`)
- return { modelId: id, provider: method, specificationVersion: "v3" } as unknown as LanguageModelV3
- }
- return {
- responses: make("responses"),
- messages: make("messages"),
- chat: make("chat"),
- languageModel: make("languageModel"),
- }
- }
- void mock.module("@ai-sdk/google-vertex", () => ({
- createVertex: (options: Record<string, any>) => {
- vertexOptions.push(options)
- return {
- languageModel: (modelID: string) => ({ modelID, provider: "google-vertex", specificationVersion: "v3" }),
- }
- },
- }))
- void mock.module("google-auth-library", () => ({
- GoogleAuth: class {
- constructor(options: Record<string, any>) {
- googleAuthOptions.push(options)
- }
- async getClient() {
- return {
- async getAccessToken() {
- return { token: "vertex-token" }
- },
- }
- }
- },
- }))
- describe("GoogleVertexPlugin", () => {
- it.effect("ignores OpenAI-compatible providers that are not Google Vertex", () =>
- Effect.gen(function* () {
- const catalog = yield* Catalog.Service
- yield* catalog.transform((catalog) =>
- catalog.provider.update(Provider.ID.opencode, (provider) => {
- provider.package = Provider.aisdk("@ai-sdk/openai-compatible")
- provider.settings = { ...provider.settings, baseURL: "https://opencode.ai/zen/v1" }
- }),
- )
- yield* addPlugin()
- const provider = required(yield* catalog.provider.get(Provider.ID.opencode))
- expect(provider.settings).toEqual({ baseURL: "https://opencode.ai/zen/v1" })
- }),
- )
- it.effect("resolves project and location from env using legacy precedence", () =>
- withEnv(
- {
- GOOGLE_CLOUD_PROJECT: "google-cloud-project",
- GCP_PROJECT: "gcp-project",
- GCLOUD_PROJECT: "gcloud-project",
- GOOGLE_VERTEX_LOCATION: "google-vertex-location",
- GOOGLE_CLOUD_LOCATION: "google-cloud-location",
- VERTEX_LOCATION: "vertex-location",
- },
- () =>
- Effect.gen(function* () {
- const catalog = yield* Catalog.Service
- yield* catalog.transform((catalog) =>
- catalog.provider.update(Provider.ID.make("google-vertex"), (provider) => {
- provider.package = Provider.aisdk("@ai-sdk/openai-compatible")
- provider.settings = {
- ...provider.settings,
- baseURL:
- "https://${GOOGLE_VERTEX_ENDPOINT}/v1/projects/${GOOGLE_VERTEX_PROJECT}/locations/${GOOGLE_VERTEX_LOCATION}",
- }
- }),
- )
- yield* addPlugin()
- const provider = required(yield* catalog.provider.get(Provider.ID.make("google-vertex")))
- expect(provider.settings?.project).toBe("google-cloud-project")
- expect(provider.settings?.location).toBe("google-vertex-location")
- expect(provider).toMatchObject({
- package: "aisdk:@ai-sdk/openai-compatible",
- settings: {
- baseURL:
- "https://google-vertex-location-aiplatform.googleapis.com/v1/projects/google-cloud-project/locations/google-vertex-location",
- },
- })
- }),
- ),
- )
- it.effect("resolves the advertised GOOGLE_VERTEX_PROJECT env for provider updates and SDKs", () =>
- withEnv(
- {
- GOOGLE_VERTEX_PROJECT: "vertex-project",
- GOOGLE_CLOUD_PROJECT: undefined,
- GCP_PROJECT: undefined,
- GCLOUD_PROJECT: undefined,
- GOOGLE_VERTEX_LOCATION: "europe-west4",
- GOOGLE_CLOUD_LOCATION: undefined,
- VERTEX_LOCATION: undefined,
- },
- () =>
- Effect.gen(function* () {
- vertexOptions.length = 0
- const plugin = yield* Plugin.Service
- const aisdk = yield* AISDK.Service
- const catalog = yield* Catalog.Service
- yield* catalog.transform((catalog) =>
- catalog.provider.update(Provider.ID.make("google-vertex"), (provider) => {
- provider.package = Provider.aisdk("@ai-sdk/openai-compatible")
- provider.settings = {
- ...provider.settings,
- baseURL:
- "https://${GOOGLE_VERTEX_ENDPOINT}/v1/projects/${GOOGLE_VERTEX_PROJECT}/locations/${GOOGLE_VERTEX_LOCATION}",
- }
- }),
- )
- yield* addPlugin()
- const provider = required(yield* catalog.provider.get(Provider.ID.make("google-vertex")))
- yield* aisdk.runSDK({
- model: Model.Info.make({
- ...Model.Info.default(Provider.ID.make("google-vertex"), Model.ID.make("gemini")),
- modelID: Model.ID.make("gemini"),
- package: "aisdk:@ai-sdk/google-vertex",
- }),
- package: "@ai-sdk/google-vertex",
- options: { name: "google-vertex" },
- })
- expect(provider.settings?.project).toBe("vertex-project")
- expect(provider).toMatchObject({
- package: "aisdk:@ai-sdk/openai-compatible",
- settings: {
- baseURL:
- "https://europe-west4-aiplatform.googleapis.com/v1/projects/vertex-project/locations/europe-west4",
- },
- })
- expect(vertexOptions[0].project).toBe("vertex-project")
- expect(vertexOptions[0].location).toBe("europe-west4")
- }),
- ),
- )
- it.effect("keeps configured project and location over env and uses global endpoint", () =>
- withEnv(
- {
- GOOGLE_CLOUD_PROJECT: "env-project",
- GCP_PROJECT: "env-gcp-project",
- GCLOUD_PROJECT: "env-gcloud-project",
- GOOGLE_VERTEX_LOCATION: "env-location",
- GOOGLE_CLOUD_LOCATION: "env-google-cloud-location",
- VERTEX_LOCATION: "env-vertex-location",
- },
- () =>
- Effect.gen(function* () {
- const catalog = yield* Catalog.Service
- yield* catalog.transform((catalog) =>
- catalog.provider.update(Provider.ID.make("google-vertex"), (provider) => {
- provider.package = Provider.aisdk("@ai-sdk/openai-compatible")
- provider.settings = {
- ...provider.settings,
- baseURL:
- "https://${GOOGLE_VERTEX_ENDPOINT}/v1/projects/${GOOGLE_VERTEX_PROJECT}/locations/${GOOGLE_VERTEX_LOCATION}",
- }
- provider.settings = { ...provider.settings, project: "config-project", location: "global" }
- }),
- )
- yield* addPlugin()
- const provider = required(yield* catalog.provider.get(Provider.ID.make("google-vertex")))
- expect(provider.settings?.project).toBe("config-project")
- expect(provider.settings?.location).toBe("global")
- expect(provider).toMatchObject({
- package: "aisdk:@ai-sdk/openai-compatible",
- settings: { baseURL: "https://aiplatform.googleapis.com/v1/projects/config-project/locations/global" },
- })
- }),
- ),
- )
- it.effect("keeps OpenAI-compatible Vertex endpoint templates regional for eu", () =>
- Effect.gen(function* () {
- const catalog = yield* Catalog.Service
- yield* catalog.transform((catalog) =>
- catalog.provider.update(Provider.ID.make("google-vertex"), (provider) => {
- provider.package = Provider.aisdk("@ai-sdk/openai-compatible")
- provider.settings = {
- ...provider.settings,
- baseURL:
- "https://${GOOGLE_VERTEX_ENDPOINT}/v1/projects/${GOOGLE_VERTEX_PROJECT}/locations/${GOOGLE_VERTEX_LOCATION}",
- }
- provider.settings = { ...provider.settings, project: "config-project", location: "eu" }
- }),
- )
- yield* addPlugin()
- const provider = required(yield* catalog.provider.get(Provider.ID.make("google-vertex")))
- expect(provider).toMatchObject({
- package: "aisdk:@ai-sdk/openai-compatible",
- settings: { baseURL: "https://eu-aiplatform.googleapis.com/v1/projects/config-project/locations/eu" },
- })
- }),
- )
- it.effect("defaults location to us-central1 when only project is configured", () =>
- withEnv(
- {
- GOOGLE_CLOUD_PROJECT: undefined,
- GCP_PROJECT: undefined,
- GCLOUD_PROJECT: undefined,
- GOOGLE_VERTEX_LOCATION: undefined,
- GOOGLE_CLOUD_LOCATION: undefined,
- VERTEX_LOCATION: undefined,
- },
- () =>
- Effect.gen(function* () {
- const catalog = yield* Catalog.Service
- yield* catalog.transform((catalog) =>
- catalog.provider.update(Provider.ID.make("google-vertex"), (provider) => {
- provider.package = Provider.aisdk("@ai-sdk/google-vertex")
- provider.settings = { ...provider.settings, project: "config-project" }
- }),
- )
- yield* addPlugin()
- const provider = required(yield* catalog.provider.get(Provider.ID.make("google-vertex")))
- expect(provider.settings?.project).toBe("config-project")
- expect(provider.settings?.location).toBe("us-central1")
- }),
- ),
- )
- it.effect("does not pass Google auth fetch to the native Vertex SDK", () =>
- withEnv(
- {
- GOOGLE_CLOUD_PROJECT: "env-project",
- GOOGLE_VERTEX_LOCATION: "env-location",
- },
- () =>
- Effect.gen(function* () {
- vertexOptions.length = 0
- const plugin = yield* Plugin.Service
- const aisdk = yield* AISDK.Service
- yield* addPlugin()
- yield* aisdk.runSDK({
- model: Model.Info.make({
- ...Model.Info.default(Provider.ID.make("google-vertex"), Model.ID.make("gemini")),
- modelID: Model.ID.make("gemini"),
- package: "aisdk:@ai-sdk/google-vertex",
- }),
- package: "@ai-sdk/google-vertex",
- options: { name: "google-vertex" },
- })
- expect(vertexOptions).toHaveLength(1)
- expect(vertexOptions[0].project).toBe("env-project")
- expect(vertexOptions[0].location).toBe("env-location")
- expect(vertexOptions[0].fetch).toBeUndefined()
- }),
- ),
- )
- it.effect("keeps Google auth fetch for OpenAI-compatible Vertex endpoints", () =>
- Effect.gen(function* () {
- googleAuthOptions.length = 0
- const fetchCalls: { input: Parameters<typeof fetch>[0]; init?: RequestInit }[] = []
- const plugin = yield* Plugin.Service
- const aisdk = yield* AISDK.Service
- yield* addPlugin()
- yield* aisdk.hook.sdk((evt) =>
- Effect.promise(async () => {
- if (evt.model.providerID !== "google-vertex") return
- if (evt.package !== "@ai-sdk/openai-compatible") return
- expect(typeof evt.options.fetch).toBe("function")
- await evt.options.fetch("https://vertex.example", {
- headers: { "x-test": "1" },
- })
- }),
- )
- const originalFetch = fetch
- ;(globalThis as typeof globalThis & { fetch: typeof fetch }).fetch = (async (
- input: Parameters<typeof fetch>[0],
- init?: RequestInit,
- ) => {
- fetchCalls.push({ input, init })
- return new Response("ok")
- }) as typeof fetch
- yield* Effect.acquireUseRelease(
- Effect.void,
- () =>
- aisdk.runSDK({
- model: Model.Info.make({
- ...Model.Info.default(Provider.ID.make("google-vertex"), Model.ID.make("gemini")),
- modelID: Model.ID.make("gemini"),
- package: "aisdk:@ai-sdk/openai-compatible",
- }),
- package: "@ai-sdk/openai-compatible",
- options: { name: "google-vertex" },
- }),
- () =>
- Effect.sync(() => {
- ;(globalThis as typeof globalThis & { fetch: typeof fetch }).fetch = originalFetch
- }),
- )
- const vertexCalls = fetchCalls.filter((call) => call.input === "https://vertex.example")
- expect(vertexCalls).toHaveLength(1)
- expect(googleAuthOptions).toEqual([{ scopes: ["https://www.googleapis.com/auth/cloud-platform"] }])
- expect(new Headers(vertexCalls[0].init?.headers).get("authorization")).toBe("Bearer vertex-token")
- expect(new Headers(vertexCalls[0].init?.headers).get("x-test")).toBe("1")
- }),
- )
- it.effect("trims model IDs before selecting language models", () =>
- Effect.gen(function* () {
- const plugin = yield* Plugin.Service
- const aisdk = yield* AISDK.Service
- const calls: string[] = []
- yield* addPlugin()
- yield* aisdk.runLanguage({
- model: Model.Info.make({
- ...Model.Info.default(Provider.ID.make("google-vertex"), Model.ID.make(" gemini-2.5-pro ")),
- modelID: Model.ID.make(" gemini-2.5-pro "),
- package: "aisdk:test-provider",
- }),
- sdk: { languageModel: fakeSelectorSdk(calls).languageModel },
- options: {},
- })
- expect(calls).toEqual(["languageModel:gemini-2.5-pro"])
- }),
- )
- })
|