provider-google-vertex-anthropic.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. import type { LanguageModelV3 } from "@ai-sdk/provider"
  2. import { describe, expect } 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 { GoogleVertexAnthropicPlugin, GoogleVertexPlugin } from "@opencode-ai/core/plugin/provider/google-vertex"
  9. import { ProviderV2 } from "@opencode-ai/core/provider"
  10. import { testEffect } from "../lib/effect"
  11. import { PluginTestLayer } from "./fixture"
  12. const it = testEffect(PluginTestLayer)
  13. const addPlugin = Effect.fn(function* (definition: typeof GoogleVertexAnthropicPlugin | typeof GoogleVertexPlugin) {
  14. const plugin = yield* PluginV2.Service
  15. const host = yield* PluginHost.make()
  16. yield* plugin.add({ id: definition.id, effect: definition.effect(host) })
  17. })
  18. function withEnv<A, E, R>(vars: Record<string, string | undefined>, effect: () => Effect.Effect<A, E, R>) {
  19. return Effect.acquireUseRelease(
  20. Effect.sync(() => {
  21. const previous = Object.fromEntries(Object.keys(vars).map((key) => [key, process.env[key]]))
  22. Object.entries(vars).forEach(([key, value]) => {
  23. if (value === undefined) delete process.env[key]
  24. else process.env[key] = value
  25. })
  26. return previous
  27. }),
  28. effect,
  29. (previous) =>
  30. Effect.sync(() => {
  31. Object.entries(previous).forEach(([key, value]) => {
  32. if (value === undefined) delete process.env[key]
  33. else process.env[key] = value
  34. })
  35. }),
  36. )
  37. }
  38. function selector(calls: string[]) {
  39. return (id: string) => {
  40. calls.push(`languageModel:${id}`)
  41. return { modelId: id, provider: "languageModel", specificationVersion: "v3" } as unknown as LanguageModelV3
  42. }
  43. }
  44. describe("GoogleVertexAnthropicPlugin", () => {
  45. it.effect("resolves legacy project and location env on provider update", () =>
  46. withEnv(
  47. {
  48. GOOGLE_CLOUD_PROJECT: "cloud-project",
  49. GCP_PROJECT: "gcp-project",
  50. GCLOUD_PROJECT: "gcloud-project",
  51. GOOGLE_CLOUD_LOCATION: "cloud-location",
  52. VERTEX_LOCATION: "vertex-location",
  53. GOOGLE_VERTEX_LOCATION: "google-vertex-location",
  54. },
  55. () =>
  56. Effect.gen(function* () {
  57. const catalog = yield* Catalog.Service
  58. yield* catalog.transform((catalog) =>
  59. catalog.provider.update(ProviderV2.ID.make("google-vertex-anthropic"), (provider) => {
  60. provider.api = { type: "aisdk", package: "@ai-sdk/google-vertex/anthropic" }
  61. }),
  62. )
  63. yield* addPlugin(GoogleVertexAnthropicPlugin)
  64. expect(
  65. (yield* catalog.provider.get(ProviderV2.ID.make("google-vertex-anthropic")))?.request.body.project,
  66. ).toBe("cloud-project")
  67. expect(
  68. (yield* catalog.provider.get(ProviderV2.ID.make("google-vertex-anthropic")))?.request.body.location,
  69. ).toBe("cloud-location")
  70. }),
  71. ),
  72. )
  73. it.effect("keeps configured project and location over env fallback", () =>
  74. withEnv({ GOOGLE_CLOUD_PROJECT: "env-project", GOOGLE_CLOUD_LOCATION: "env-location" }, () =>
  75. Effect.gen(function* () {
  76. const catalog = yield* Catalog.Service
  77. yield* catalog.transform((catalog) =>
  78. catalog.provider.update(ProviderV2.ID.make("google-vertex-anthropic"), (provider) => {
  79. provider.api = { type: "aisdk", package: "@ai-sdk/google-vertex/anthropic" }
  80. provider.request.body.project = "configured-project"
  81. provider.request.body.location = "configured-location"
  82. }),
  83. )
  84. yield* addPlugin(GoogleVertexAnthropicPlugin)
  85. expect((yield* catalog.provider.get(ProviderV2.ID.make("google-vertex-anthropic")))?.request.body.project).toBe(
  86. "configured-project",
  87. )
  88. expect(
  89. (yield* catalog.provider.get(ProviderV2.ID.make("google-vertex-anthropic")))?.request.body.location,
  90. ).toBe("configured-location")
  91. }),
  92. ),
  93. )
  94. it.effect("creates SDKs from legacy env fallback and default location", () =>
  95. withEnv(
  96. {
  97. GOOGLE_CLOUD_PROJECT: undefined,
  98. GCP_PROJECT: "gcp-project",
  99. GCLOUD_PROJECT: "gcloud-project",
  100. GOOGLE_CLOUD_LOCATION: undefined,
  101. VERTEX_LOCATION: undefined,
  102. GOOGLE_VERTEX_LOCATION: "ignored-location",
  103. },
  104. () =>
  105. Effect.gen(function* () {
  106. const plugin = yield* PluginV2.Service
  107. yield* addPlugin(GoogleVertexAnthropicPlugin)
  108. const result = yield* plugin.trigger(
  109. "aisdk.sdk",
  110. {
  111. model: new ModelV2.Info({
  112. ...ModelV2.Info.empty(
  113. ProviderV2.ID.make("google-vertex-anthropic"),
  114. ModelV2.ID.make("claude-sonnet-4-5"),
  115. ),
  116. api: { id: ModelV2.ID.make("claude-sonnet-4-5"), type: "aisdk", package: "test-provider" },
  117. }),
  118. package: "@ai-sdk/google-vertex/anthropic",
  119. options: { name: "google-vertex-anthropic" },
  120. },
  121. {},
  122. )
  123. expect(result.sdk.languageModel("claude-sonnet-4-5").config.baseURL).toBe(
  124. "https://aiplatform.googleapis.com/v1/projects/gcp-project/locations/global/publishers/anthropic/models",
  125. )
  126. }),
  127. ),
  128. )
  129. it.effect("uses GOOGLE_CLOUD_LOCATION before VERTEX_LOCATION when creating SDKs", () =>
  130. withEnv(
  131. { GOOGLE_CLOUD_PROJECT: "project", GOOGLE_CLOUD_LOCATION: "cloud-location", VERTEX_LOCATION: "vertex-location" },
  132. () =>
  133. Effect.gen(function* () {
  134. const plugin = yield* PluginV2.Service
  135. yield* addPlugin(GoogleVertexAnthropicPlugin)
  136. const result = yield* plugin.trigger(
  137. "aisdk.sdk",
  138. {
  139. model: new ModelV2.Info({
  140. ...ModelV2.Info.empty(
  141. ProviderV2.ID.make("google-vertex-anthropic"),
  142. ModelV2.ID.make("claude-sonnet-4-5"),
  143. ),
  144. api: { id: ModelV2.ID.make("claude-sonnet-4-5"), type: "aisdk", package: "test-provider" },
  145. }),
  146. package: "@ai-sdk/google-vertex/anthropic",
  147. options: { name: "google-vertex-anthropic" },
  148. },
  149. {},
  150. )
  151. expect(result.sdk.languageModel("claude-sonnet-4-5").config.baseURL).toBe(
  152. "https://cloud-location-aiplatform.googleapis.com/v1/projects/project/locations/cloud-location/publishers/anthropic/models",
  153. )
  154. }),
  155. ),
  156. )
  157. it.effect("creates SDKs for google-vertex Anthropic models with multi-region endpoints", () =>
  158. Effect.gen(function* () {
  159. const plugin = yield* PluginV2.Service
  160. yield* addPlugin(GoogleVertexAnthropicPlugin)
  161. const result = yield* plugin.trigger(
  162. "aisdk.sdk",
  163. {
  164. model: new ModelV2.Info({
  165. ...ModelV2.Info.empty(ProviderV2.ID.make("google-vertex"), ModelV2.ID.make("claude-sonnet-4-5")),
  166. api: { id: ModelV2.ID.make("claude-sonnet-4-5"), type: "aisdk", package: "test-provider" },
  167. }),
  168. package: "@ai-sdk/google-vertex/anthropic",
  169. options: { name: "google-vertex", project: "project", location: "eu" },
  170. },
  171. {},
  172. )
  173. expect(result.sdk.languageModel("claude-sonnet-4-5").config.baseURL).toBe(
  174. "https://aiplatform.eu.rep.googleapis.com/v1/projects/project/locations/eu/publishers/anthropic/models",
  175. )
  176. }),
  177. )
  178. it.effect("keeps configured baseURL for google-vertex Anthropic models", () =>
  179. Effect.gen(function* () {
  180. const plugin = yield* PluginV2.Service
  181. yield* addPlugin(GoogleVertexAnthropicPlugin)
  182. const result = yield* plugin.trigger(
  183. "aisdk.sdk",
  184. {
  185. model: new ModelV2.Info({
  186. ...ModelV2.Info.empty(ProviderV2.ID.make("google-vertex"), ModelV2.ID.make("claude-sonnet-4-5")),
  187. api: { id: ModelV2.ID.make("claude-sonnet-4-5"), type: "aisdk", package: "test-provider" },
  188. }),
  189. package: "@ai-sdk/google-vertex/anthropic",
  190. options: { name: "google-vertex", project: "project", location: "eu", baseURL: "https://proxy.example/v1" },
  191. },
  192. {},
  193. )
  194. expect(result.sdk.languageModel("claude-sonnet-4-5").config.baseURL).toBe("https://proxy.example/v1")
  195. }),
  196. )
  197. it.effect("selects google-vertex Anthropic language models through V2 plugins", () =>
  198. Effect.gen(function* () {
  199. const plugin = yield* PluginV2.Service
  200. yield* addPlugin(GoogleVertexPlugin)
  201. yield* addPlugin(GoogleVertexAnthropicPlugin)
  202. const sdkResult = yield* plugin.trigger(
  203. "aisdk.sdk",
  204. {
  205. model: new ModelV2.Info({
  206. ...ModelV2.Info.empty(ProviderV2.ID.make("google-vertex"), ModelV2.ID.make(" claude-sonnet-4-5 ")),
  207. api: { id: ModelV2.ID.make(" claude-sonnet-4-5 "), type: "aisdk", package: "test-provider" },
  208. }),
  209. package: "@ai-sdk/google-vertex/anthropic",
  210. options: { name: "google-vertex", project: "project", location: "us" },
  211. },
  212. {},
  213. )
  214. const languageResult = yield* plugin.trigger(
  215. "aisdk.language",
  216. {
  217. model: new ModelV2.Info({
  218. ...ModelV2.Info.empty(ProviderV2.ID.make("google-vertex"), ModelV2.ID.make(" claude-sonnet-4-5 ")),
  219. api: { id: ModelV2.ID.make(" claude-sonnet-4-5 "), type: "aisdk", package: "test-provider" },
  220. }),
  221. sdk: sdkResult.sdk,
  222. options: {},
  223. },
  224. {},
  225. )
  226. const language = languageResult.language as unknown as { config: { baseURL: string }; modelId: string }
  227. expect(language.config.baseURL).toBe(
  228. "https://aiplatform.us.rep.googleapis.com/v1/projects/project/locations/us/publishers/anthropic/models",
  229. )
  230. expect(language.modelId).toBe("claude-sonnet-4-5")
  231. }),
  232. )
  233. it.effect("trims model IDs before selecting language models", () =>
  234. Effect.gen(function* () {
  235. const plugin = yield* PluginV2.Service
  236. const calls: string[] = []
  237. yield* addPlugin(GoogleVertexAnthropicPlugin)
  238. yield* plugin.trigger(
  239. "aisdk.language",
  240. {
  241. model: new ModelV2.Info({
  242. ...ModelV2.Info.empty(
  243. ProviderV2.ID.make("google-vertex-anthropic"),
  244. ModelV2.ID.make(" claude-sonnet-4-5 "),
  245. ),
  246. api: { id: ModelV2.ID.make(" claude-sonnet-4-5 "), type: "aisdk", package: "test-provider" },
  247. }),
  248. sdk: { languageModel: selector(calls) },
  249. options: {},
  250. },
  251. {},
  252. )
  253. expect(calls).toEqual(["languageModel:claude-sonnet-4-5"])
  254. }),
  255. )
  256. it.effect("ignores non Vertex Anthropic providers for language selection", () =>
  257. Effect.gen(function* () {
  258. const plugin = yield* PluginV2.Service
  259. const calls: string[] = []
  260. yield* addPlugin(GoogleVertexAnthropicPlugin)
  261. const result = yield* plugin.trigger(
  262. "aisdk.language",
  263. {
  264. model: new ModelV2.Info({
  265. ...ModelV2.Info.empty(ProviderV2.ID.make("google-vertex"), ModelV2.ID.make("claude-sonnet-4-5")),
  266. api: { id: ModelV2.ID.make("claude-sonnet-4-5"), type: "aisdk", package: "test-provider" },
  267. }),
  268. sdk: { languageModel: selector(calls) },
  269. options: {},
  270. },
  271. {},
  272. )
  273. expect(calls).toEqual([])
  274. expect(result.language).toBeUndefined()
  275. }),
  276. )
  277. })