provider-cloudflare-workers-ai.test.ts 11 KB

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