provider-google-vertex.test.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. import { describe, expect, mock } from "bun:test"
  2. import { Effect } from "effect"
  3. import { Catalog } from "@opencode-ai/core/catalog"
  4. import { ModelV2 } from "@opencode-ai/core/model"
  5. import { PluginV2 } from "@opencode-ai/core/plugin"
  6. import { PluginHost } from "@opencode-ai/core/plugin/host"
  7. import { GoogleVertexPlugin } from "@opencode-ai/core/plugin/provider/google-vertex"
  8. import { ProviderV2 } from "@opencode-ai/core/provider"
  9. import type { LanguageModelV3 } from "@ai-sdk/provider"
  10. import { testEffect } from "../lib/effect"
  11. import { PluginTestLayer } from "./fixture"
  12. const vertexOptions: Record<string, any>[] = []
  13. const googleAuthOptions: Record<string, any>[] = []
  14. const it = testEffect(PluginTestLayer)
  15. const addPlugin = Effect.fn(function* () {
  16. const plugin = yield* PluginV2.Service
  17. const host = yield* PluginHost.make()
  18. yield* plugin.add({ id: GoogleVertexPlugin.id, effect: GoogleVertexPlugin.effect(host) })
  19. })
  20. function required<T>(value: T | undefined): T {
  21. if (value === undefined) throw new Error("Expected value")
  22. return value
  23. }
  24. function withEnv<A, E, R>(vars: Record<string, string | undefined>, effect: () => Effect.Effect<A, E, R>) {
  25. return Effect.acquireUseRelease(
  26. Effect.sync(() => {
  27. const previous = Object.fromEntries(Object.keys(vars).map((key) => [key, process.env[key]]))
  28. Object.entries(vars).forEach(([key, value]) => {
  29. if (value === undefined) delete process.env[key]
  30. else process.env[key] = value
  31. })
  32. return previous
  33. }),
  34. effect,
  35. (previous) =>
  36. Effect.sync(() =>
  37. Object.entries(previous).forEach(([key, value]) => {
  38. if (value === undefined) delete process.env[key]
  39. else process.env[key] = value
  40. }),
  41. ),
  42. )
  43. }
  44. function fakeSelectorSdk(calls: string[]) {
  45. const make = (method: string) => (id: string) => {
  46. calls.push(`${method}:${id}`)
  47. return { modelId: id, provider: method, specificationVersion: "v3" } as unknown as LanguageModelV3
  48. }
  49. return {
  50. responses: make("responses"),
  51. messages: make("messages"),
  52. chat: make("chat"),
  53. languageModel: make("languageModel"),
  54. }
  55. }
  56. void mock.module("@ai-sdk/google-vertex", () => ({
  57. createVertex: (options: Record<string, any>) => {
  58. vertexOptions.push(options)
  59. return {
  60. languageModel: (modelID: string) => ({ modelID, provider: "google-vertex", specificationVersion: "v3" }),
  61. }
  62. },
  63. }))
  64. void mock.module("google-auth-library", () => ({
  65. GoogleAuth: class {
  66. constructor(options: Record<string, any>) {
  67. googleAuthOptions.push(options)
  68. }
  69. async getClient() {
  70. return {
  71. async getAccessToken() {
  72. return { token: "vertex-token" }
  73. },
  74. }
  75. }
  76. },
  77. }))
  78. describe("GoogleVertexPlugin", () => {
  79. it.effect("ignores OpenAI-compatible providers that are not Google Vertex", () =>
  80. Effect.gen(function* () {
  81. const catalog = yield* Catalog.Service
  82. yield* catalog.transform((catalog) =>
  83. catalog.provider.update(ProviderV2.ID.opencode, (provider) => {
  84. provider.api = {
  85. type: "aisdk",
  86. package: "@ai-sdk/openai-compatible",
  87. url: "https://opencode.ai/zen/v1",
  88. }
  89. }),
  90. )
  91. yield* addPlugin()
  92. const provider = required(yield* catalog.provider.get(ProviderV2.ID.opencode))
  93. expect(provider.request.body).toEqual({})
  94. }),
  95. )
  96. it.effect("resolves project and location from env using legacy precedence", () =>
  97. withEnv(
  98. {
  99. GOOGLE_CLOUD_PROJECT: "google-cloud-project",
  100. GCP_PROJECT: "gcp-project",
  101. GCLOUD_PROJECT: "gcloud-project",
  102. GOOGLE_VERTEX_LOCATION: "google-vertex-location",
  103. GOOGLE_CLOUD_LOCATION: "google-cloud-location",
  104. VERTEX_LOCATION: "vertex-location",
  105. },
  106. () =>
  107. Effect.gen(function* () {
  108. const catalog = yield* Catalog.Service
  109. yield* catalog.transform((catalog) =>
  110. catalog.provider.update(ProviderV2.ID.make("google-vertex"), (provider) => {
  111. provider.api = {
  112. type: "aisdk",
  113. package: "@ai-sdk/openai-compatible",
  114. url: "https://${GOOGLE_VERTEX_ENDPOINT}/v1/projects/${GOOGLE_VERTEX_PROJECT}/locations/${GOOGLE_VERTEX_LOCATION}",
  115. }
  116. }),
  117. )
  118. yield* addPlugin()
  119. const provider = required(yield* catalog.provider.get(ProviderV2.ID.make("google-vertex")))
  120. expect(provider.request.body.project).toBe("google-cloud-project")
  121. expect(provider.request.body.location).toBe("google-vertex-location")
  122. expect(provider.api).toEqual({
  123. type: "aisdk",
  124. package: "@ai-sdk/openai-compatible",
  125. url: "https://google-vertex-location-aiplatform.googleapis.com/v1/projects/google-cloud-project/locations/google-vertex-location",
  126. })
  127. }),
  128. ),
  129. )
  130. it.effect("resolves the advertised GOOGLE_VERTEX_PROJECT env for provider updates and SDKs", () =>
  131. withEnv(
  132. {
  133. GOOGLE_VERTEX_PROJECT: "vertex-project",
  134. GOOGLE_CLOUD_PROJECT: undefined,
  135. GCP_PROJECT: undefined,
  136. GCLOUD_PROJECT: undefined,
  137. GOOGLE_VERTEX_LOCATION: "europe-west4",
  138. GOOGLE_CLOUD_LOCATION: undefined,
  139. VERTEX_LOCATION: undefined,
  140. },
  141. () =>
  142. Effect.gen(function* () {
  143. vertexOptions.length = 0
  144. const plugin = yield* PluginV2.Service
  145. const catalog = yield* Catalog.Service
  146. yield* catalog.transform((catalog) =>
  147. catalog.provider.update(ProviderV2.ID.make("google-vertex"), (provider) => {
  148. provider.api = {
  149. type: "aisdk",
  150. package: "@ai-sdk/openai-compatible",
  151. url: "https://${GOOGLE_VERTEX_ENDPOINT}/v1/projects/${GOOGLE_VERTEX_PROJECT}/locations/${GOOGLE_VERTEX_LOCATION}",
  152. }
  153. }),
  154. )
  155. yield* addPlugin()
  156. const provider = required(yield* catalog.provider.get(ProviderV2.ID.make("google-vertex")))
  157. yield* plugin.trigger(
  158. "aisdk.sdk",
  159. {
  160. model: new ModelV2.Info({
  161. ...ModelV2.Info.empty(ProviderV2.ID.make("google-vertex"), ModelV2.ID.make("gemini")),
  162. api: {
  163. id: ModelV2.ID.make("gemini"),
  164. type: "aisdk",
  165. package: "@ai-sdk/google-vertex",
  166. },
  167. }),
  168. package: "@ai-sdk/google-vertex",
  169. options: { name: "google-vertex" },
  170. },
  171. {},
  172. )
  173. expect(provider.request.body.project).toBe("vertex-project")
  174. expect(provider.api).toEqual({
  175. type: "aisdk",
  176. package: "@ai-sdk/openai-compatible",
  177. url: "https://europe-west4-aiplatform.googleapis.com/v1/projects/vertex-project/locations/europe-west4",
  178. })
  179. expect(vertexOptions[0].project).toBe("vertex-project")
  180. expect(vertexOptions[0].location).toBe("europe-west4")
  181. }),
  182. ),
  183. )
  184. it.effect("keeps configured project and location over env and uses global endpoint", () =>
  185. withEnv(
  186. {
  187. GOOGLE_CLOUD_PROJECT: "env-project",
  188. GCP_PROJECT: "env-gcp-project",
  189. GCLOUD_PROJECT: "env-gcloud-project",
  190. GOOGLE_VERTEX_LOCATION: "env-location",
  191. GOOGLE_CLOUD_LOCATION: "env-google-cloud-location",
  192. VERTEX_LOCATION: "env-vertex-location",
  193. },
  194. () =>
  195. Effect.gen(function* () {
  196. const catalog = yield* Catalog.Service
  197. yield* catalog.transform((catalog) =>
  198. catalog.provider.update(ProviderV2.ID.make("google-vertex"), (provider) => {
  199. provider.api = {
  200. type: "aisdk",
  201. package: "@ai-sdk/openai-compatible",
  202. url: "https://${GOOGLE_VERTEX_ENDPOINT}/v1/projects/${GOOGLE_VERTEX_PROJECT}/locations/${GOOGLE_VERTEX_LOCATION}",
  203. }
  204. provider.request.body.project = "config-project"
  205. provider.request.body.location = "global"
  206. }),
  207. )
  208. yield* addPlugin()
  209. const provider = required(yield* catalog.provider.get(ProviderV2.ID.make("google-vertex")))
  210. expect(provider.request.body.project).toBe("config-project")
  211. expect(provider.request.body.location).toBe("global")
  212. expect(provider.api).toEqual({
  213. type: "aisdk",
  214. package: "@ai-sdk/openai-compatible",
  215. url: "https://aiplatform.googleapis.com/v1/projects/config-project/locations/global",
  216. })
  217. }),
  218. ),
  219. )
  220. it.effect("keeps OpenAI-compatible Vertex endpoint templates regional for eu", () =>
  221. Effect.gen(function* () {
  222. const catalog = yield* Catalog.Service
  223. yield* catalog.transform((catalog) =>
  224. catalog.provider.update(ProviderV2.ID.make("google-vertex"), (provider) => {
  225. provider.api = {
  226. type: "aisdk",
  227. package: "@ai-sdk/openai-compatible",
  228. url: "https://${GOOGLE_VERTEX_ENDPOINT}/v1/projects/${GOOGLE_VERTEX_PROJECT}/locations/${GOOGLE_VERTEX_LOCATION}",
  229. }
  230. provider.request.body.project = "config-project"
  231. provider.request.body.location = "eu"
  232. }),
  233. )
  234. yield* addPlugin()
  235. const provider = required(yield* catalog.provider.get(ProviderV2.ID.make("google-vertex")))
  236. expect(provider.api).toEqual({
  237. type: "aisdk",
  238. package: "@ai-sdk/openai-compatible",
  239. url: "https://eu-aiplatform.googleapis.com/v1/projects/config-project/locations/eu",
  240. })
  241. }),
  242. )
  243. it.effect("defaults location to us-central1 when only project is configured", () =>
  244. withEnv(
  245. {
  246. GOOGLE_CLOUD_PROJECT: undefined,
  247. GCP_PROJECT: undefined,
  248. GCLOUD_PROJECT: undefined,
  249. GOOGLE_VERTEX_LOCATION: undefined,
  250. GOOGLE_CLOUD_LOCATION: undefined,
  251. VERTEX_LOCATION: undefined,
  252. },
  253. () =>
  254. Effect.gen(function* () {
  255. const catalog = yield* Catalog.Service
  256. yield* catalog.transform((catalog) =>
  257. catalog.provider.update(ProviderV2.ID.make("google-vertex"), (provider) => {
  258. provider.api = { type: "aisdk", package: "@ai-sdk/google-vertex" }
  259. provider.request.body.project = "config-project"
  260. }),
  261. )
  262. yield* addPlugin()
  263. const provider = required(yield* catalog.provider.get(ProviderV2.ID.make("google-vertex")))
  264. expect(provider.request.body.project).toBe("config-project")
  265. expect(provider.request.body.location).toBe("us-central1")
  266. }),
  267. ),
  268. )
  269. it.effect("does not pass Google auth fetch to the native Vertex SDK", () =>
  270. withEnv(
  271. {
  272. GOOGLE_CLOUD_PROJECT: "env-project",
  273. GOOGLE_VERTEX_LOCATION: "env-location",
  274. },
  275. () =>
  276. Effect.gen(function* () {
  277. vertexOptions.length = 0
  278. const plugin = yield* PluginV2.Service
  279. yield* addPlugin()
  280. yield* plugin.trigger(
  281. "aisdk.sdk",
  282. {
  283. model: new ModelV2.Info({
  284. ...ModelV2.Info.empty(ProviderV2.ID.make("google-vertex"), ModelV2.ID.make("gemini")),
  285. api: {
  286. id: ModelV2.ID.make("gemini"),
  287. type: "aisdk",
  288. package: "@ai-sdk/google-vertex",
  289. },
  290. }),
  291. package: "@ai-sdk/google-vertex",
  292. options: { name: "google-vertex" },
  293. },
  294. {},
  295. )
  296. expect(vertexOptions).toHaveLength(1)
  297. expect(vertexOptions[0].project).toBe("env-project")
  298. expect(vertexOptions[0].location).toBe("env-location")
  299. expect(vertexOptions[0].fetch).toBeUndefined()
  300. }),
  301. ),
  302. )
  303. it.effect("keeps Google auth fetch for OpenAI-compatible Vertex endpoints", () =>
  304. Effect.gen(function* () {
  305. googleAuthOptions.length = 0
  306. const fetchCalls: { input: Parameters<typeof fetch>[0]; init?: RequestInit }[] = []
  307. const plugin = yield* PluginV2.Service
  308. yield* addPlugin()
  309. yield* plugin.hook("aisdk.sdk", (evt) =>
  310. Effect.promise(async () => {
  311. if (evt.model.providerID !== "google-vertex") return
  312. if (evt.package !== "@ai-sdk/openai-compatible") return
  313. expect(typeof evt.options.fetch).toBe("function")
  314. await evt.options.fetch("https://vertex.example", {
  315. headers: { "x-test": "1" },
  316. })
  317. }),
  318. )
  319. const originalFetch = fetch
  320. ;(globalThis as typeof globalThis & { fetch: typeof fetch }).fetch = (async (
  321. input: Parameters<typeof fetch>[0],
  322. init?: RequestInit,
  323. ) => {
  324. fetchCalls.push({ input, init })
  325. return new Response("ok")
  326. }) as typeof fetch
  327. yield* Effect.acquireUseRelease(
  328. Effect.void,
  329. () =>
  330. plugin.trigger(
  331. "aisdk.sdk",
  332. {
  333. model: new ModelV2.Info({
  334. ...ModelV2.Info.empty(ProviderV2.ID.make("google-vertex"), ModelV2.ID.make("gemini")),
  335. api: {
  336. id: ModelV2.ID.make("gemini"),
  337. type: "aisdk",
  338. package: "@ai-sdk/openai-compatible",
  339. },
  340. }),
  341. package: "@ai-sdk/openai-compatible",
  342. options: { name: "google-vertex" },
  343. },
  344. {},
  345. ),
  346. () =>
  347. Effect.sync(() => {
  348. ;(globalThis as typeof globalThis & { fetch: typeof fetch }).fetch = originalFetch
  349. }),
  350. )
  351. expect(fetchCalls).toHaveLength(1)
  352. expect(googleAuthOptions).toEqual([{ scopes: ["https://www.googleapis.com/auth/cloud-platform"] }])
  353. expect(fetchCalls[0].input).toBe("https://vertex.example")
  354. expect(new Headers(fetchCalls[0].init?.headers).get("authorization")).toBe("Bearer vertex-token")
  355. expect(new Headers(fetchCalls[0].init?.headers).get("x-test")).toBe("1")
  356. }),
  357. )
  358. it.effect("trims model IDs before selecting language models", () =>
  359. Effect.gen(function* () {
  360. const plugin = yield* PluginV2.Service
  361. const calls: string[] = []
  362. yield* addPlugin()
  363. yield* plugin.trigger(
  364. "aisdk.language",
  365. {
  366. model: new ModelV2.Info({
  367. ...ModelV2.Info.empty(ProviderV2.ID.make("google-vertex"), ModelV2.ID.make(" gemini-2.5-pro ")),
  368. api: { id: ModelV2.ID.make(" gemini-2.5-pro "), type: "aisdk", package: "test-provider" },
  369. }),
  370. sdk: { languageModel: fakeSelectorSdk(calls).languageModel },
  371. options: {},
  372. },
  373. {},
  374. )
  375. expect(calls).toEqual(["languageModel:gemini-2.5-pro"])
  376. }),
  377. )
  378. })