provider-azure.test.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. import { describe, expect } from "bun:test"
  2. import { Effect, Layer } from "effect"
  3. import { AccountV2 } from "@opencode-ai/core/account"
  4. import { Catalog } from "@opencode-ai/core/catalog"
  5. import { EventV2 } from "@opencode-ai/core/event"
  6. import { Location } from "@opencode-ai/core/location"
  7. import { PluginV2 } from "@opencode-ai/core/plugin"
  8. import { AccountPlugin } from "@opencode-ai/core/plugin/account"
  9. import { AzurePlugin } from "@opencode-ai/core/plugin/provider/azure"
  10. import { ProviderV2 } from "@opencode-ai/core/provider"
  11. import { testEffect } from "../lib/effect"
  12. import { fakeSelectorSdk, it, model, npmLayer, provider, withEnv } from "./provider-helper"
  13. const itWithAccount = testEffect(
  14. Catalog.layer.pipe(
  15. Layer.provideMerge(PluginV2.defaultLayer),
  16. Layer.provideMerge(AccountV2.defaultLayer),
  17. Layer.provideMerge(EventV2.defaultLayer),
  18. Layer.provideMerge(Layer.succeed(Location.Service, Location.Service.of({ directory: "test" }))),
  19. Layer.provideMerge(npmLayer),
  20. ),
  21. )
  22. describe("AzurePlugin", () => {
  23. it.effect("resolves resourceName from env", () =>
  24. withEnv({ AZURE_RESOURCE_NAME: "from-env" }, () =>
  25. Effect.gen(function* () {
  26. const plugin = yield* PluginV2.Service
  27. const catalog = yield* Catalog.Service
  28. yield* plugin.add(AzurePlugin)
  29. const load = yield* catalog.loader()
  30. yield* load((catalog) => {
  31. catalog.provider.update(ProviderV2.ID.azure, (item) => {
  32. item.endpoint = { type: "aisdk", package: "@ai-sdk/azure" }
  33. })
  34. })
  35. expect((yield* catalog.provider.get(ProviderV2.ID.azure)).options.aisdk.provider.resourceName).toBe("from-env")
  36. }),
  37. ),
  38. )
  39. it.effect("keeps explicit resourceName over env and ignores other providers", () =>
  40. withEnv({ AZURE_RESOURCE_NAME: "from-env" }, () =>
  41. Effect.gen(function* () {
  42. const plugin = yield* PluginV2.Service
  43. const catalog = yield* Catalog.Service
  44. yield* plugin.add(AzurePlugin)
  45. const load = yield* catalog.loader()
  46. yield* load((catalog) => {
  47. const azure = provider("azure", {
  48. endpoint: { type: "aisdk", package: "@ai-sdk/azure" },
  49. options: { headers: {}, body: {}, aisdk: { provider: { resourceName: "from-config" }, request: {} } },
  50. })
  51. catalog.provider.update(azure.id, (item) => {
  52. item.endpoint = azure.endpoint
  53. item.options = azure.options
  54. })
  55. catalog.provider.update(ProviderV2.ID.openai, () => {})
  56. })
  57. expect((yield* catalog.provider.get(ProviderV2.ID.azure)).options.aisdk.provider.resourceName).toBe(
  58. "from-config",
  59. )
  60. expect((yield* catalog.provider.get(ProviderV2.ID.openai)).options.aisdk.provider.resourceName).toBeUndefined()
  61. }),
  62. ),
  63. )
  64. itWithAccount.effect("prefers account resourceName over env", () =>
  65. withEnv(
  66. {
  67. AZURE_RESOURCE_NAME: "from-env",
  68. },
  69. () =>
  70. Effect.gen(function* () {
  71. const plugin = yield* PluginV2.Service
  72. const accounts = yield* AccountV2.Service
  73. const catalog = yield* Catalog.Service
  74. const events = yield* EventV2.Service
  75. yield* accounts.create({
  76. serviceID: AccountV2.ServiceID.make("azure"),
  77. credential: new AccountV2.ApiKeyCredential({
  78. type: "api",
  79. key: "key",
  80. metadata: { resourceName: "from-account" },
  81. }),
  82. })
  83. yield* plugin.add({
  84. ...AccountPlugin,
  85. effect: AccountPlugin.effect.pipe(
  86. Effect.provideService(AccountV2.Service, accounts),
  87. Effect.provideService(Catalog.Service, catalog),
  88. Effect.provideService(EventV2.Service, events),
  89. Effect.provideService(PluginV2.Service, plugin),
  90. ),
  91. })
  92. yield* plugin.add(AzurePlugin)
  93. const load = yield* catalog.loader()
  94. yield* load((catalog) => {
  95. catalog.provider.update(ProviderV2.ID.azure, (item) => {
  96. item.endpoint = { type: "aisdk", package: "@ai-sdk/azure" }
  97. })
  98. })
  99. expect((yield* catalog.provider.get(ProviderV2.ID.azure)).options.aisdk.provider.resourceName).toBe(
  100. "from-account",
  101. )
  102. }),
  103. ),
  104. )
  105. it.effect("falls back to env when configured resourceName is blank", () =>
  106. withEnv({ AZURE_RESOURCE_NAME: "from-env" }, () =>
  107. Effect.gen(function* () {
  108. const plugin = yield* PluginV2.Service
  109. const catalog = yield* Catalog.Service
  110. yield* plugin.add(AzurePlugin)
  111. const load = yield* catalog.loader()
  112. yield* load((catalog) => {
  113. const azure = provider("azure", {
  114. endpoint: { type: "aisdk", package: "@ai-sdk/azure" },
  115. options: { headers: {}, body: {}, aisdk: { provider: { resourceName: "" }, request: {} } },
  116. })
  117. catalog.provider.update(azure.id, (item) => {
  118. item.endpoint = azure.endpoint
  119. item.options = azure.options
  120. })
  121. })
  122. expect((yield* catalog.provider.get(ProviderV2.ID.azure)).options.aisdk.provider.resourceName).toBe("from-env")
  123. }),
  124. ),
  125. )
  126. it.effect("falls back to env when configured resourceName is whitespace", () =>
  127. withEnv({ AZURE_RESOURCE_NAME: "from-env" }, () =>
  128. Effect.gen(function* () {
  129. const plugin = yield* PluginV2.Service
  130. const catalog = yield* Catalog.Service
  131. yield* plugin.add(AzurePlugin)
  132. const load = yield* catalog.loader()
  133. yield* load((catalog) => {
  134. const azure = provider("azure", {
  135. endpoint: { type: "aisdk", package: "@ai-sdk/azure" },
  136. options: { headers: {}, body: {}, aisdk: { provider: { resourceName: " " }, request: {} } },
  137. })
  138. catalog.provider.update(azure.id, (item) => {
  139. item.endpoint = azure.endpoint
  140. item.options = azure.options
  141. })
  142. })
  143. expect((yield* catalog.provider.get(ProviderV2.ID.azure)).options.aisdk.provider.resourceName).toBe("from-env")
  144. }),
  145. ),
  146. )
  147. it.effect("allows configured baseURL without resourceName", () =>
  148. withEnv({ AZURE_RESOURCE_NAME: undefined }, () =>
  149. Effect.gen(function* () {
  150. const plugin = yield* PluginV2.Service
  151. yield* plugin.add(AzurePlugin)
  152. const result = yield* plugin.trigger(
  153. "aisdk.sdk",
  154. {
  155. model: model("azure", "deployment"),
  156. package: "@ai-sdk/azure",
  157. options: { name: "azure", baseURL: "https://proxy.example.com/openai" },
  158. },
  159. {},
  160. )
  161. expect(result.sdk).toBeDefined()
  162. }),
  163. ),
  164. )
  165. it.effect("rejects missing resourceName when baseURL is not configured", () =>
  166. withEnv({ AZURE_RESOURCE_NAME: undefined }, () =>
  167. Effect.gen(function* () {
  168. const plugin = yield* PluginV2.Service
  169. yield* plugin.add(AzurePlugin)
  170. const exit = yield* plugin
  171. .trigger(
  172. "aisdk.sdk",
  173. { model: model("azure", "deployment"), package: "@ai-sdk/azure", options: { name: "azure" } },
  174. {},
  175. )
  176. .pipe(Effect.exit)
  177. expect(exit._tag).toBe("Failure")
  178. }),
  179. ),
  180. )
  181. it.effect("selects chat only for completion URLs", () =>
  182. Effect.gen(function* () {
  183. const plugin = yield* PluginV2.Service
  184. const calls: string[] = []
  185. yield* plugin.add(AzurePlugin)
  186. yield* plugin.trigger(
  187. "aisdk.language",
  188. { model: model("azure", "deployment"), sdk: fakeSelectorSdk(calls), options: { useCompletionUrls: true } },
  189. {},
  190. )
  191. expect(calls).toEqual(["chat:deployment"])
  192. }),
  193. )
  194. it.effect("selects chat from per-call useCompletionUrls", () =>
  195. Effect.gen(function* () {
  196. const plugin = yield* PluginV2.Service
  197. const calls: string[] = []
  198. yield* plugin.add(AzurePlugin)
  199. yield* plugin.trigger(
  200. "aisdk.language",
  201. { model: model("azure", "deployment"), sdk: fakeSelectorSdk(calls), options: { useCompletionUrls: true } },
  202. {},
  203. )
  204. expect(calls).toEqual(["chat:deployment"])
  205. }),
  206. )
  207. it.effect("ignores model useCompletionUrls when per-call option is unset", () =>
  208. Effect.gen(function* () {
  209. const plugin = yield* PluginV2.Service
  210. const calls: string[] = []
  211. yield* plugin.add(AzurePlugin)
  212. yield* plugin.trigger(
  213. "aisdk.language",
  214. {
  215. model: model("azure", "deployment", {
  216. options: { headers: {}, body: {}, aisdk: { provider: {}, request: { useCompletionUrls: true } } },
  217. }),
  218. sdk: fakeSelectorSdk(calls),
  219. options: {},
  220. },
  221. {},
  222. )
  223. expect(calls).toEqual(["responses:deployment"])
  224. }),
  225. )
  226. it.effect("uses the legacy Azure selector order and provider guard", () =>
  227. Effect.gen(function* () {
  228. const plugin = yield* PluginV2.Service
  229. const calls: string[] = []
  230. yield* plugin.add(AzurePlugin)
  231. yield* plugin.trigger(
  232. "aisdk.language",
  233. { model: model("azure", "deployment"), sdk: fakeSelectorSdk(calls), options: {} },
  234. {},
  235. )
  236. const ignored = yield* plugin.trigger(
  237. "aisdk.language",
  238. { model: model("openai", "deployment"), sdk: fakeSelectorSdk(calls), options: {} },
  239. {},
  240. )
  241. expect(calls).toEqual(["responses:deployment"])
  242. expect(ignored.language).toBeUndefined()
  243. }),
  244. )
  245. it.effect("falls back through the legacy Azure selector order", () =>
  246. Effect.gen(function* () {
  247. const plugin = yield* PluginV2.Service
  248. const calls: string[] = []
  249. const make = (method: string) => (id: string) => {
  250. calls.push(`${method}:${id}`)
  251. return { modelId: id, provider: method, specificationVersion: "v3" }
  252. }
  253. yield* plugin.add(AzurePlugin)
  254. yield* plugin.trigger(
  255. "aisdk.language",
  256. {
  257. model: model("azure", "messages-deployment"),
  258. sdk: { messages: make("messages"), chat: make("chat"), languageModel: make("languageModel") },
  259. options: {},
  260. },
  261. {},
  262. )
  263. yield* plugin.trigger(
  264. "aisdk.language",
  265. { model: model("azure", "language-deployment"), sdk: { languageModel: make("languageModel") }, options: {} },
  266. {},
  267. )
  268. expect(calls).toEqual(["messages:messages-deployment", "languageModel:language-deployment"])
  269. }),
  270. )
  271. })