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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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.package = ProviderV2.aisdk("@ai-sdk/google-vertex/anthropic")
  63. }),
  64. )
  65. yield* addPlugin(GoogleVertexAnthropicPlugin)
  66. expect((yield* catalog.provider.get(ProviderV2.ID.make("google-vertex-anthropic")))?.settings?.project).toBe(
  67. "cloud-project",
  68. )
  69. expect((yield* catalog.provider.get(ProviderV2.ID.make("google-vertex-anthropic")))?.settings?.location).toBe(
  70. "cloud-location",
  71. )
  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.package = ProviderV2.aisdk("@ai-sdk/google-vertex/anthropic")
  82. provider.settings = { ...provider.settings, project: "configured-project", location: "configured-location" }
  83. }),
  84. )
  85. yield* addPlugin(GoogleVertexAnthropicPlugin)
  86. expect((yield* catalog.provider.get(ProviderV2.ID.make("google-vertex-anthropic")))?.settings?.project).toBe(
  87. "configured-project",
  88. )
  89. expect((yield* catalog.provider.get(ProviderV2.ID.make("google-vertex-anthropic")))?.settings?.location).toBe(
  90. "configured-location",
  91. )
  92. }),
  93. ),
  94. )
  95. it.effect("creates SDKs from legacy env fallback and default location", () =>
  96. withEnv(
  97. {
  98. GOOGLE_CLOUD_PROJECT: undefined,
  99. GCP_PROJECT: "gcp-project",
  100. GCLOUD_PROJECT: "gcloud-project",
  101. GOOGLE_CLOUD_LOCATION: undefined,
  102. VERTEX_LOCATION: undefined,
  103. GOOGLE_VERTEX_LOCATION: "ignored-location",
  104. },
  105. () =>
  106. Effect.gen(function* () {
  107. const plugin = yield* PluginV2.Service
  108. const aisdk = yield* AISDK.Service
  109. yield* addPlugin(GoogleVertexAnthropicPlugin)
  110. const result = yield* aisdk.runSDK({
  111. model: ModelV2.Info.make({
  112. ...ModelV2.Info.empty(
  113. ProviderV2.ID.make("google-vertex-anthropic"),
  114. ModelV2.ID.make("claude-sonnet-4-5"),
  115. ),
  116. modelID: ModelV2.ID.make("claude-sonnet-4-5"),
  117. package: "aisdk: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. modelID: ModelV2.ID.make("claude-sonnet-4-5"),
  143. package: "aisdk:test-provider",
  144. }),
  145. package: "@ai-sdk/google-vertex/anthropic",
  146. options: { name: "google-vertex-anthropic" },
  147. })
  148. expect(result.sdk.languageModel("claude-sonnet-4-5").config.baseURL).toBe(
  149. "https://cloud-location-aiplatform.googleapis.com/v1/projects/project/locations/cloud-location/publishers/anthropic/models",
  150. )
  151. }),
  152. ),
  153. )
  154. it.effect("creates SDKs for google-vertex Anthropic models with multi-region endpoints", () =>
  155. Effect.gen(function* () {
  156. const plugin = yield* PluginV2.Service
  157. const aisdk = yield* AISDK.Service
  158. yield* addPlugin(GoogleVertexAnthropicPlugin)
  159. const result = yield* aisdk.runSDK({
  160. model: ModelV2.Info.make({
  161. ...ModelV2.Info.empty(ProviderV2.ID.make("google-vertex"), ModelV2.ID.make("claude-sonnet-4-5")),
  162. modelID: ModelV2.ID.make("claude-sonnet-4-5"),
  163. package: "aisdk:test-provider",
  164. }),
  165. package: "@ai-sdk/google-vertex/anthropic",
  166. options: { name: "google-vertex", project: "project", location: "eu" },
  167. })
  168. expect(result.sdk.languageModel("claude-sonnet-4-5").config.baseURL).toBe(
  169. "https://aiplatform.eu.rep.googleapis.com/v1/projects/project/locations/eu/publishers/anthropic/models",
  170. )
  171. }),
  172. )
  173. it.effect("keeps configured baseURL for google-vertex Anthropic models", () =>
  174. Effect.gen(function* () {
  175. const plugin = yield* PluginV2.Service
  176. const aisdk = yield* AISDK.Service
  177. yield* addPlugin(GoogleVertexAnthropicPlugin)
  178. const result = yield* aisdk.runSDK({
  179. model: ModelV2.Info.make({
  180. ...ModelV2.Info.empty(ProviderV2.ID.make("google-vertex"), ModelV2.ID.make("claude-sonnet-4-5")),
  181. modelID: ModelV2.ID.make("claude-sonnet-4-5"),
  182. package: "aisdk:test-provider",
  183. }),
  184. package: "@ai-sdk/google-vertex/anthropic",
  185. options: { name: "google-vertex", project: "project", location: "eu", baseURL: "https://proxy.example/v1" },
  186. })
  187. expect(result.sdk.languageModel("claude-sonnet-4-5").config.baseURL).toBe("https://proxy.example/v1")
  188. }),
  189. )
  190. it.effect("selects google-vertex Anthropic language models through V2 plugins", () =>
  191. Effect.gen(function* () {
  192. const plugin = yield* PluginV2.Service
  193. const aisdk = yield* AISDK.Service
  194. yield* addPlugin(GoogleVertexPlugin)
  195. yield* addPlugin(GoogleVertexAnthropicPlugin)
  196. const sdkResult = yield* aisdk.runSDK({
  197. model: ModelV2.Info.make({
  198. ...ModelV2.Info.empty(ProviderV2.ID.make("google-vertex"), ModelV2.ID.make(" claude-sonnet-4-5 ")),
  199. modelID: ModelV2.ID.make(" claude-sonnet-4-5 "),
  200. package: "aisdk:test-provider",
  201. }),
  202. package: "@ai-sdk/google-vertex/anthropic",
  203. options: { name: "google-vertex", project: "project", location: "us" },
  204. })
  205. const languageResult = yield* aisdk.runLanguage({
  206. model: ModelV2.Info.make({
  207. ...ModelV2.Info.empty(ProviderV2.ID.make("google-vertex"), ModelV2.ID.make(" claude-sonnet-4-5 ")),
  208. modelID: ModelV2.ID.make(" claude-sonnet-4-5 "),
  209. package: "aisdk:test-provider",
  210. }),
  211. sdk: sdkResult.sdk,
  212. options: {},
  213. })
  214. const language = languageResult.language as unknown as { config: { baseURL: string }; modelId: string }
  215. expect(language.config.baseURL).toBe(
  216. "https://aiplatform.us.rep.googleapis.com/v1/projects/project/locations/us/publishers/anthropic/models",
  217. )
  218. expect(language.modelId).toBe("claude-sonnet-4-5")
  219. }),
  220. )
  221. it.effect("trims model IDs before selecting language models", () =>
  222. Effect.gen(function* () {
  223. const plugin = yield* PluginV2.Service
  224. const aisdk = yield* AISDK.Service
  225. const calls: string[] = []
  226. yield* addPlugin(GoogleVertexAnthropicPlugin)
  227. yield* aisdk.runLanguage({
  228. model: ModelV2.Info.make({
  229. ...ModelV2.Info.empty(ProviderV2.ID.make("google-vertex-anthropic"), ModelV2.ID.make(" claude-sonnet-4-5 ")),
  230. modelID: ModelV2.ID.make(" claude-sonnet-4-5 "),
  231. package: "aisdk:test-provider",
  232. }),
  233. sdk: { languageModel: selector(calls) },
  234. options: {},
  235. })
  236. expect(calls).toEqual(["languageModel:claude-sonnet-4-5"])
  237. }),
  238. )
  239. it.effect("ignores non Vertex Anthropic providers for language selection", () =>
  240. Effect.gen(function* () {
  241. const plugin = yield* PluginV2.Service
  242. const aisdk = yield* AISDK.Service
  243. const calls: string[] = []
  244. yield* addPlugin(GoogleVertexAnthropicPlugin)
  245. const result = yield* aisdk.runLanguage({
  246. model: ModelV2.Info.make({
  247. ...ModelV2.Info.empty(ProviderV2.ID.make("google-vertex"), ModelV2.ID.make("claude-sonnet-4-5")),
  248. modelID: ModelV2.ID.make("claude-sonnet-4-5"),
  249. package: "aisdk:test-provider",
  250. }),
  251. sdk: { languageModel: selector(calls) },
  252. options: {},
  253. })
  254. expect(calls).toEqual([])
  255. expect(result.language).toBeUndefined()
  256. }),
  257. )
  258. })