provider-google-vertex.test.ts 14 KB

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