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

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