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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import { describe, expect } from "bun:test"
  2. import { Effect } from "effect"
  3. import { Catalog } from "@opencode-ai/core/catalog"
  4. import { PluginV2 } from "@opencode-ai/core/plugin"
  5. import { GoogleVertexAnthropicPlugin, GoogleVertexPlugin } from "@opencode-ai/core/plugin/provider/google-vertex"
  6. import { ProviderV2 } from "@opencode-ai/core/provider"
  7. import { fakeSelectorSdk, it, model, withEnv } from "./provider-helper"
  8. describe("GoogleVertexAnthropicPlugin", () => {
  9. it.effect("resolves legacy project and location env on provider update", () =>
  10. withEnv(
  11. {
  12. GOOGLE_CLOUD_PROJECT: "cloud-project",
  13. GCP_PROJECT: "gcp-project",
  14. GCLOUD_PROJECT: "gcloud-project",
  15. GOOGLE_CLOUD_LOCATION: "cloud-location",
  16. VERTEX_LOCATION: "vertex-location",
  17. GOOGLE_VERTEX_LOCATION: "google-vertex-location",
  18. },
  19. () =>
  20. Effect.gen(function* () {
  21. const plugin = yield* PluginV2.Service
  22. const catalog = yield* Catalog.Service
  23. yield* plugin.add(GoogleVertexAnthropicPlugin)
  24. const transform = yield* catalog.transform()
  25. yield* transform((catalog) =>
  26. catalog.provider.update(ProviderV2.ID.make("google-vertex-anthropic"), (provider) => {
  27. provider.api = { type: "aisdk", package: "@ai-sdk/google-vertex/anthropic" }
  28. }),
  29. )
  30. const provider = yield* catalog.provider.get(ProviderV2.ID.make("google-vertex-anthropic"))
  31. expect(provider.request.body.project).toBe("cloud-project")
  32. expect(provider.request.body.location).toBe("cloud-location")
  33. }),
  34. ),
  35. )
  36. it.effect("keeps configured project and location over env fallback", () =>
  37. withEnv({ GOOGLE_CLOUD_PROJECT: "env-project", GOOGLE_CLOUD_LOCATION: "env-location" }, () =>
  38. Effect.gen(function* () {
  39. const plugin = yield* PluginV2.Service
  40. const catalog = yield* Catalog.Service
  41. yield* plugin.add(GoogleVertexAnthropicPlugin)
  42. const transform = yield* catalog.transform()
  43. yield* transform((catalog) =>
  44. catalog.provider.update(ProviderV2.ID.make("google-vertex-anthropic"), (provider) => {
  45. provider.api = { type: "aisdk", package: "@ai-sdk/google-vertex/anthropic" }
  46. provider.request.body.project = "configured-project"
  47. provider.request.body.location = "configured-location"
  48. }),
  49. )
  50. const provider = yield* catalog.provider.get(ProviderV2.ID.make("google-vertex-anthropic"))
  51. expect(provider.request.body.project).toBe("configured-project")
  52. expect(provider.request.body.location).toBe("configured-location")
  53. }),
  54. ),
  55. )
  56. it.effect("creates SDKs from legacy env fallback and default location", () =>
  57. withEnv(
  58. {
  59. GOOGLE_CLOUD_PROJECT: undefined,
  60. GCP_PROJECT: "gcp-project",
  61. GCLOUD_PROJECT: "gcloud-project",
  62. GOOGLE_CLOUD_LOCATION: undefined,
  63. VERTEX_LOCATION: undefined,
  64. GOOGLE_VERTEX_LOCATION: "ignored-location",
  65. },
  66. () =>
  67. Effect.gen(function* () {
  68. const plugin = yield* PluginV2.Service
  69. yield* plugin.add(GoogleVertexAnthropicPlugin)
  70. const result = yield* plugin.trigger(
  71. "aisdk.sdk",
  72. {
  73. model: model("google-vertex-anthropic", "claude-sonnet-4-5"),
  74. package: "@ai-sdk/google-vertex/anthropic",
  75. options: { name: "google-vertex-anthropic" },
  76. },
  77. {},
  78. )
  79. expect(result.sdk.languageModel("claude-sonnet-4-5").config.baseURL).toBe(
  80. "https://aiplatform.googleapis.com/v1/projects/gcp-project/locations/global/publishers/anthropic/models",
  81. )
  82. }),
  83. ),
  84. )
  85. it.effect("uses GOOGLE_CLOUD_LOCATION before VERTEX_LOCATION when creating SDKs", () =>
  86. withEnv(
  87. { GOOGLE_CLOUD_PROJECT: "project", GOOGLE_CLOUD_LOCATION: "cloud-location", VERTEX_LOCATION: "vertex-location" },
  88. () =>
  89. Effect.gen(function* () {
  90. const plugin = yield* PluginV2.Service
  91. yield* plugin.add(GoogleVertexAnthropicPlugin)
  92. const result = yield* plugin.trigger(
  93. "aisdk.sdk",
  94. {
  95. model: model("google-vertex-anthropic", "claude-sonnet-4-5"),
  96. package: "@ai-sdk/google-vertex/anthropic",
  97. options: { name: "google-vertex-anthropic" },
  98. },
  99. {},
  100. )
  101. expect(result.sdk.languageModel("claude-sonnet-4-5").config.baseURL).toBe(
  102. "https://cloud-location-aiplatform.googleapis.com/v1/projects/project/locations/cloud-location/publishers/anthropic/models",
  103. )
  104. }),
  105. ),
  106. )
  107. it.effect("creates SDKs for google-vertex Anthropic models with multi-region endpoints", () =>
  108. Effect.gen(function* () {
  109. const plugin = yield* PluginV2.Service
  110. yield* plugin.add(GoogleVertexAnthropicPlugin)
  111. const result = yield* plugin.trigger(
  112. "aisdk.sdk",
  113. {
  114. model: model("google-vertex", "claude-sonnet-4-5"),
  115. package: "@ai-sdk/google-vertex/anthropic",
  116. options: { name: "google-vertex", project: "project", location: "eu" },
  117. },
  118. {},
  119. )
  120. expect(result.sdk.languageModel("claude-sonnet-4-5").config.baseURL).toBe(
  121. "https://aiplatform.eu.rep.googleapis.com/v1/projects/project/locations/eu/publishers/anthropic/models",
  122. )
  123. }),
  124. )
  125. it.effect("keeps configured baseURL for google-vertex Anthropic models", () =>
  126. Effect.gen(function* () {
  127. const plugin = yield* PluginV2.Service
  128. yield* plugin.add(GoogleVertexAnthropicPlugin)
  129. const result = yield* plugin.trigger(
  130. "aisdk.sdk",
  131. {
  132. model: model("google-vertex", "claude-sonnet-4-5"),
  133. package: "@ai-sdk/google-vertex/anthropic",
  134. options: { name: "google-vertex", project: "project", location: "eu", baseURL: "https://proxy.example/v1" },
  135. },
  136. {},
  137. )
  138. expect(result.sdk.languageModel("claude-sonnet-4-5").config.baseURL).toBe("https://proxy.example/v1")
  139. }),
  140. )
  141. it.effect("selects google-vertex Anthropic language models through V2 plugins", () =>
  142. Effect.gen(function* () {
  143. const plugin = yield* PluginV2.Service
  144. yield* plugin.add(GoogleVertexPlugin)
  145. yield* plugin.add(GoogleVertexAnthropicPlugin)
  146. const sdkResult = yield* plugin.trigger(
  147. "aisdk.sdk",
  148. {
  149. model: model("google-vertex", " claude-sonnet-4-5 "),
  150. package: "@ai-sdk/google-vertex/anthropic",
  151. options: { name: "google-vertex", project: "project", location: "us" },
  152. },
  153. {},
  154. )
  155. const languageResult = yield* plugin.trigger(
  156. "aisdk.language",
  157. {
  158. model: model("google-vertex", " claude-sonnet-4-5 "),
  159. sdk: sdkResult.sdk,
  160. options: {},
  161. },
  162. {},
  163. )
  164. const language = languageResult.language as unknown as { config: { baseURL: string }; modelId: string }
  165. expect(language.config.baseURL).toBe(
  166. "https://aiplatform.us.rep.googleapis.com/v1/projects/project/locations/us/publishers/anthropic/models",
  167. )
  168. expect(language.modelId).toBe("claude-sonnet-4-5")
  169. }),
  170. )
  171. it.effect("trims model IDs before selecting language models", () =>
  172. Effect.gen(function* () {
  173. const plugin = yield* PluginV2.Service
  174. const calls: string[] = []
  175. yield* plugin.add(GoogleVertexAnthropicPlugin)
  176. yield* plugin.trigger(
  177. "aisdk.language",
  178. {
  179. model: model("google-vertex-anthropic", " claude-sonnet-4-5 "),
  180. sdk: { languageModel: fakeSelectorSdk(calls).languageModel },
  181. options: {},
  182. },
  183. {},
  184. )
  185. expect(calls).toEqual(["languageModel:claude-sonnet-4-5"])
  186. }),
  187. )
  188. it.effect("ignores non Vertex Anthropic providers for language selection", () =>
  189. Effect.gen(function* () {
  190. const plugin = yield* PluginV2.Service
  191. const calls: string[] = []
  192. yield* plugin.add(GoogleVertexAnthropicPlugin)
  193. const result = yield* plugin.trigger(
  194. "aisdk.language",
  195. {
  196. model: model("google-vertex", "claude-sonnet-4-5"),
  197. sdk: { languageModel: fakeSelectorSdk(calls).languageModel },
  198. options: {},
  199. },
  200. {},
  201. )
  202. expect(calls).toEqual([])
  203. expect(result.language).toBeUndefined()
  204. }),
  205. )
  206. })