provider-cloudflare-workers-ai.test.ts 11 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 { Location } from "@opencode-ai/core/location"
  6. import { EventV2 } from "@opencode-ai/core/event"
  7. import { ModelV2 } from "@opencode-ai/core/model"
  8. import { PluginV2 } from "@opencode-ai/core/plugin"
  9. import { AccountPlugin } from "@opencode-ai/core/plugin/account"
  10. import { CloudflareWorkersAIPlugin } from "@opencode-ai/core/plugin/provider/cloudflare-workers-ai"
  11. import { ProviderV2 } from "@opencode-ai/core/provider"
  12. import { testEffect } from "../lib/effect"
  13. import { fakeSelectorSdk, it, model, npmLayer, withEnv } from "./provider-helper"
  14. const itWithAccount = testEffect(
  15. Catalog.layer.pipe(
  16. Layer.provideMerge(PluginV2.defaultLayer),
  17. Layer.provideMerge(AccountV2.defaultLayer),
  18. Layer.provideMerge(EventV2.defaultLayer),
  19. Layer.provideMerge(Layer.succeed(Location.Service, Location.Service.of({ directory: "test" }))),
  20. Layer.provideMerge(npmLayer),
  21. ),
  22. )
  23. function cloudflareLanguage(sdk: unknown, modelID = "@cf/model") {
  24. return (sdk as { languageModel: (id: string) => { config: CloudflareConfig; provider: string } }).languageModel(
  25. modelID,
  26. )
  27. }
  28. type CloudflareConfig = {
  29. url: (input: { path: string; modelId: string }) => string
  30. headers: () => Record<string, string> | Promise<Record<string, string>>
  31. }
  32. function cloudflareURL(sdk: unknown, modelID = "@cf/model") {
  33. return cloudflareLanguage(sdk, modelID).config.url({ path: "/chat/completions", modelId: modelID })
  34. }
  35. function cloudflareHeaders(sdk: unknown, modelID = "@cf/model") {
  36. return cloudflareLanguage(sdk, modelID).config.headers()
  37. }
  38. describe("CloudflareWorkersAIPlugin", () => {
  39. it.effect("maps account ID to endpoint URL and creates an OpenAI-compatible SDK", () =>
  40. withEnv({ CLOUDFLARE_ACCOUNT_ID: "acct", CLOUDFLARE_API_KEY: "key" }, () =>
  41. Effect.gen(function* () {
  42. const plugin = yield* PluginV2.Service
  43. const catalog = yield* Catalog.Service
  44. yield* plugin.add(CloudflareWorkersAIPlugin)
  45. const load = yield* catalog.loader()
  46. yield* load((catalog) =>
  47. catalog.provider.update(ProviderV2.ID.make("cloudflare-workers-ai"), (provider) => {
  48. provider.endpoint = { type: "aisdk", package: "test-provider" }
  49. }),
  50. )
  51. const provider = yield* catalog.provider.get(ProviderV2.ID.make("cloudflare-workers-ai"))
  52. const sdk = yield* plugin.trigger(
  53. "aisdk.sdk",
  54. {
  55. model: model("cloudflare-workers-ai", "@cf/model", { endpoint: provider.endpoint }),
  56. package: "@ai-sdk/openai-compatible",
  57. options: { name: "cloudflare-workers-ai", headers: { custom: "header" } },
  58. },
  59. {},
  60. )
  61. expect(provider.endpoint).toEqual({
  62. type: "aisdk",
  63. package: "test-provider",
  64. url: "https://api.cloudflare.com/client/v4/accounts/acct/ai/v1",
  65. })
  66. expect(sdk.sdk).toBeDefined()
  67. }),
  68. ),
  69. )
  70. it.effect("preserves a configured endpoint URL instead of deriving one from account ID", () =>
  71. withEnv({ CLOUDFLARE_ACCOUNT_ID: "acct" }, () =>
  72. Effect.gen(function* () {
  73. const plugin = yield* PluginV2.Service
  74. const catalog = yield* Catalog.Service
  75. yield* plugin.add(CloudflareWorkersAIPlugin)
  76. const load = yield* catalog.loader()
  77. yield* load((catalog) =>
  78. catalog.provider.update(ProviderV2.ID.make("cloudflare-workers-ai"), (provider) => {
  79. provider.endpoint = { type: "aisdk", package: "test-provider", url: "https://proxy.example/v1" }
  80. }),
  81. )
  82. expect((yield* catalog.provider.get(ProviderV2.ID.make("cloudflare-workers-ai"))).endpoint).toEqual({
  83. type: "aisdk",
  84. package: "test-provider",
  85. url: "https://proxy.example/v1",
  86. })
  87. }),
  88. ),
  89. )
  90. it.effect("allows a configured baseURL without account ID", () =>
  91. withEnv({ CLOUDFLARE_ACCOUNT_ID: undefined, CLOUDFLARE_API_KEY: "key" }, () =>
  92. Effect.gen(function* () {
  93. const plugin = yield* PluginV2.Service
  94. yield* plugin.add(CloudflareWorkersAIPlugin)
  95. const result = yield* plugin.trigger(
  96. "aisdk.sdk",
  97. {
  98. model: model("cloudflare-workers-ai", "@cf/model", {
  99. endpoint: { type: "aisdk", package: "@ai-sdk/openai-compatible", url: "https://proxy.example/v1" },
  100. }),
  101. package: "@ai-sdk/openai-compatible",
  102. options: { name: "cloudflare-workers-ai", baseURL: "https://proxy.example/v1" },
  103. },
  104. {},
  105. )
  106. expect(cloudflareURL(result.sdk)).toBe("https://proxy.example/v1/chat/completions")
  107. }),
  108. ),
  109. )
  110. itWithAccount.effect("falls back to account metadata when account env is absent", () =>
  111. withEnv(
  112. {
  113. CLOUDFLARE_ACCOUNT_ID: undefined,
  114. CLOUDFLARE_API_KEY: undefined,
  115. },
  116. () =>
  117. Effect.gen(function* () {
  118. const plugin = yield* PluginV2.Service
  119. const accounts = yield* AccountV2.Service
  120. const catalog = yield* Catalog.Service
  121. const events = yield* EventV2.Service
  122. yield* accounts.create({
  123. serviceID: AccountV2.ServiceID.make("cloudflare-workers-ai"),
  124. credential: new AccountV2.ApiKeyCredential({
  125. type: "api",
  126. key: "account-key",
  127. metadata: { accountId: "account-acct" },
  128. }),
  129. })
  130. yield* plugin.add({
  131. ...AccountPlugin,
  132. effect: AccountPlugin.effect.pipe(
  133. Effect.provideService(AccountV2.Service, accounts),
  134. Effect.provideService(Catalog.Service, catalog),
  135. Effect.provideService(EventV2.Service, events),
  136. Effect.provideService(PluginV2.Service, plugin),
  137. ),
  138. })
  139. yield* plugin.add(CloudflareWorkersAIPlugin)
  140. const load = yield* catalog.loader()
  141. yield* load((catalog) =>
  142. catalog.provider.update(ProviderV2.ID.make("cloudflare-workers-ai"), (provider) => {
  143. provider.endpoint = { type: "aisdk", package: "test-provider" }
  144. }),
  145. )
  146. expect((yield* catalog.provider.get(ProviderV2.ID.make("cloudflare-workers-ai"))).endpoint).toEqual({
  147. type: "aisdk",
  148. package: "test-provider",
  149. url: "https://api.cloudflare.com/client/v4/accounts/account-acct/ai/v1",
  150. })
  151. }),
  152. ),
  153. )
  154. it.effect("uses env account ID over configured account ID", () =>
  155. withEnv({ CLOUDFLARE_ACCOUNT_ID: "env-acct" }, () =>
  156. Effect.gen(function* () {
  157. const plugin = yield* PluginV2.Service
  158. const catalog = yield* Catalog.Service
  159. yield* plugin.add(CloudflareWorkersAIPlugin)
  160. const load = yield* catalog.loader()
  161. yield* load((catalog) =>
  162. catalog.provider.update(ProviderV2.ID.make("cloudflare-workers-ai"), (provider) => {
  163. provider.endpoint = { type: "aisdk", package: "test-provider" }
  164. provider.options.aisdk.provider.accountId = "configured-acct"
  165. }),
  166. )
  167. expect((yield* catalog.provider.get(ProviderV2.ID.make("cloudflare-workers-ai"))).endpoint).toEqual({
  168. type: "aisdk",
  169. package: "test-provider",
  170. url: "https://api.cloudflare.com/client/v4/accounts/env-acct/ai/v1",
  171. })
  172. }),
  173. ),
  174. )
  175. it.effect("uses env API key over auth or configured API key and keeps the Cloudflare User-Agent", () =>
  176. withEnv({ CLOUDFLARE_ACCOUNT_ID: "acct", CLOUDFLARE_API_KEY: "env-key" }, () =>
  177. Effect.gen(function* () {
  178. const plugin = yield* PluginV2.Service
  179. yield* plugin.add(CloudflareWorkersAIPlugin)
  180. const result = yield* plugin.trigger(
  181. "aisdk.sdk",
  182. {
  183. model: model("cloudflare-workers-ai", "@cf/model", {
  184. endpoint: { type: "aisdk", package: "@ai-sdk/openai-compatible", url: "https://proxy.example/v1" },
  185. }),
  186. package: "@ai-sdk/openai-compatible",
  187. options: {
  188. name: "cloudflare-workers-ai",
  189. apiKey: "auth-key",
  190. baseURL: "https://proxy.example/v1",
  191. headers: { custom: "header" },
  192. },
  193. },
  194. {},
  195. )
  196. const headers = yield* Effect.promise(() => Promise.resolve(cloudflareHeaders(result.sdk)))
  197. expect(headers.authorization).toBe("Bearer env-key")
  198. expect(headers.custom).toBe("header")
  199. expect(headers["user-agent"]).toMatch(/^opencode\/.* cloudflare-workers-ai \(.+\) ai-sdk\/openai-compatible\//)
  200. }),
  201. ),
  202. )
  203. it.effect("expands account ID vars in endpoint URLs", () =>
  204. withEnv({ CLOUDFLARE_ACCOUNT_ID: "acct", CLOUDFLARE_API_KEY: "key" }, () =>
  205. Effect.gen(function* () {
  206. const plugin = yield* PluginV2.Service
  207. yield* plugin.add(CloudflareWorkersAIPlugin)
  208. const result = yield* plugin.trigger(
  209. "aisdk.sdk",
  210. {
  211. model: model("cloudflare-workers-ai", "@cf/model", {
  212. endpoint: {
  213. type: "aisdk",
  214. package: "@ai-sdk/openai-compatible",
  215. url: "https://api.cloudflare.com/client/v4/accounts/${CLOUDFLARE_ACCOUNT_ID}/ai/v1",
  216. },
  217. }),
  218. package: "@ai-sdk/openai-compatible",
  219. options: {
  220. name: "cloudflare-workers-ai",
  221. baseURL: "https://api.cloudflare.com/client/v4/accounts/${CLOUDFLARE_ACCOUNT_ID}/ai/v1",
  222. },
  223. },
  224. {},
  225. )
  226. expect(cloudflareURL(result.sdk)).toBe(
  227. "https://api.cloudflare.com/client/v4/accounts/acct/ai/v1/chat/completions",
  228. )
  229. }),
  230. ),
  231. )
  232. it.effect("selects languageModel with the API model ID", () =>
  233. Effect.gen(function* () {
  234. const plugin = yield* PluginV2.Service
  235. const calls: string[] = []
  236. yield* plugin.add(CloudflareWorkersAIPlugin)
  237. const result = yield* plugin.trigger(
  238. "aisdk.language",
  239. {
  240. model: model("cloudflare-workers-ai", "alias", { apiID: ModelV2.ID.make("@cf/api-model") }),
  241. sdk: fakeSelectorSdk(calls),
  242. options: {},
  243. },
  244. {},
  245. )
  246. expect(result.language).toBeDefined()
  247. expect(calls).toEqual(["languageModel:@cf/api-model"])
  248. }),
  249. )
  250. it.effect("does not create an SDK for non OpenAI-compatible packages", () =>
  251. withEnv({ CLOUDFLARE_ACCOUNT_ID: "acct", CLOUDFLARE_API_KEY: "key" }, () =>
  252. Effect.gen(function* () {
  253. const plugin = yield* PluginV2.Service
  254. yield* plugin.add(CloudflareWorkersAIPlugin)
  255. const result = yield* plugin.trigger(
  256. "aisdk.sdk",
  257. {
  258. model: model("cloudflare-workers-ai", "@cf/model", {
  259. endpoint: { type: "aisdk", package: "@ai-sdk/anthropic", url: "https://proxy.example/v1" },
  260. }),
  261. package: "@ai-sdk/anthropic",
  262. options: { name: "cloudflare-workers-ai" },
  263. },
  264. {},
  265. )
  266. expect(result.sdk).toBeUndefined()
  267. }),
  268. ),
  269. )
  270. })