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

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