1
0

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

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