The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { makeLanguageModel } from "@alchemy.run/cloudflare/AI/LanguageModel"; |
| 2 | /// <reference types="@cloudflare/workers-types" /> |
| 3 | import { describe, expect, test } from "@alchemy.run/test"; |
| 4 | import { RuntimeContext } from "alchemy/RuntimeContext"; |
| 5 | import { LanguageModel, Tool, Toolkit } from "effect/ai"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as Layer from "effect/Layer"; |
| 8 | import * as Schema from "effect/Schema"; |
| 9 | import * as Stream from "effect/Stream"; |
| 10 | |
| 11 | const tags = ["unit", "provider:cloudflare", "provider:cloudflare:ai", "local"]; |
| 12 | |
| 13 | interface WireTool { |
| 14 | type: "function"; |
| 15 | function: { name: string; description?: string; parameters: Record<string, unknown> }; |
| 16 | } |
| 17 | interface WireBody { |
| 18 | messages: Array<{ role: string; content: unknown }>; |
| 19 | tools?: WireTool[]; |
| 20 | tool_choice?: string; |
| 21 | stream?: boolean; |
| 22 | } |
| 23 | |
| 24 | // Capture requests at the public adapter boundary, including the real Effect |
| 25 | // prompt/tool preparation. A fresh response is required for every invocation. |
| 26 | const capture = (onRequest?: (body: WireBody, index: number) => void) => { |
| 27 | const requests: WireBody[] = []; |
| 28 | const ai = { |
| 29 | run: async (_model: string, input: unknown) => { |
| 30 | const body = input as WireBody; |
| 31 | requests.push(structuredClone(body)); |
| 32 | onRequest?.(body, requests.length - 1); |
| 33 | return body.stream |
| 34 | ? new Response('data: {"response":"ok"}\n\ndata: [DONE]\n\n') |
| 35 | : Response.json({ response: "ok" }); |
| 36 | }, |
| 37 | } as unknown as Ai; |
| 38 | return { |
| 39 | requests, |
| 40 | layer: Layer.effect( |
| 41 | LanguageModel.LanguageModel, |
| 42 | makeLanguageModel({ |
| 43 | model: "@cf/meta/llama-3.3-70b-instruct-fp8-fast", |
| 44 | client: { raw: Effect.succeed(ai) }, |
| 45 | }), |
| 46 | ).pipe(Layer.provide(RuntimeContext.phantom)), |
| 47 | }; |
| 48 | }; |
| 49 | |
| 50 | describe("Workers AI request preparation", () => { |
| 51 | for (const native of [true, false]) { |
| 52 | test.effect( |
| 53 | `${native ? "native" : "fallback"} image encoding preserves offsets, all bytes and padding`, |
| 54 | () => { |
| 55 | const { requests, layer } = capture(); |
| 56 | return Effect.gen(function* () { |
| 57 | const storage = new Uint8Array(264).fill(42); |
| 58 | for (let i = 0; i < 256; i++) storage[i + 4] = i; |
| 59 | const inputs = [ |
| 60 | new Uint8Array(), |
| 61 | new Uint8Array([255]), |
| 62 | new Uint8Array([255, 254]), |
| 63 | new Uint8Array([255, 254, 253]), |
| 64 | storage.subarray(4, 260), |
| 65 | ]; |
| 66 | let nativeCalls = 0; |
| 67 | for (const data of inputs) { |
| 68 | // Shadow the capability on this fixture only. Never mutate the shared |
| 69 | // prototype: unrelated tests may encode bytes concurrently. |
| 70 | Object.defineProperty(data, "toBase64", { |
| 71 | value: native |
| 72 | ? function (this: Uint8Array) { |
| 73 | expect(this).toBe(data); |
| 74 | nativeCalls++; |
| 75 | return btoa(String.fromCharCode(...this)); |
| 76 | } |
| 77 | : undefined, |
| 78 | }); |
| 79 | yield* LanguageModel.generateText({ |
| 80 | prompt: [{ role: "user", content: [{ type: "file", mediaType: "image/png", data }] }], |
| 81 | }); |
| 82 | } |
| 83 | expect(requests.map((request) => request.messages[0]?.content)).toEqual( |
| 84 | inputs.map((data) => [ |
| 85 | { |
| 86 | type: "image_url", |
| 87 | image_url: { |
| 88 | // Independent web encoder; neither branch requires node:buffer. |
| 89 | url: `data:image/png;base64,${btoa(String.fromCharCode(...data))}`, |
| 90 | }, |
| 91 | }, |
| 92 | ]), |
| 93 | ); |
| 94 | expect(Array.from(storage.subarray(0, 4))).toEqual([42, 42, 42, 42]); |
| 95 | expect(Array.from(storage.subarray(260))).toEqual([42, 42, 42, 42]); |
| 96 | expect(nativeCalls).toBe(native ? inputs.length : 0); |
| 97 | }).pipe(Effect.provide(layer)); |
| 98 | }, |
| 99 | { tags }, |
| 100 | ); |
| 101 | } |
| 102 | |
| 103 | test.effect( |
| 104 | "schemas preserve Unicode, transforms and all tool choices", |
| 105 | () => { |
| 106 | const primary = Tool.make("primary", { |
| 107 | description: "Météo 東京 🌦️", |
| 108 | parameters: Schema.Struct({ |
| 109 | count: Schema.NumberFromString, |
| 110 | city: Schema.String.annotate({ description: "São Paulo / 東京" }), |
| 111 | }), |
| 112 | }); |
| 113 | const secondary = Tool.make("secondary", { |
| 114 | parameters: Schema.Struct({ yes: Schema.Boolean }), |
| 115 | }); |
| 116 | const toolkit = Toolkit.make(primary, secondary); |
| 117 | const handlers = toolkit.toLayer({ |
| 118 | primary: () => Effect.void, |
| 119 | secondary: () => Effect.void, |
| 120 | }); |
| 121 | const { requests, layer } = capture(); |
| 122 | return Effect.gen(function* () { |
| 123 | for (const toolChoice of [ |
| 124 | "auto", |
| 125 | "none", |
| 126 | "required", |
| 127 | { tool: "primary" }, |
| 128 | { oneOf: ["secondary"], mode: "auto" }, |
| 129 | { oneOf: ["primary"], mode: "required" }, |
| 130 | ] as const) { |
| 131 | yield* LanguageModel.generateText({ prompt: "hi", toolkit, toolChoice }); |
| 132 | } |
| 133 | // Exercise the same cached preparation through the streaming entrypoint. |
| 134 | yield* LanguageModel.streamText({ prompt: "hi", toolkit, toolChoice: "auto" }).pipe( |
| 135 | Stream.runDrain, |
| 136 | ); |
| 137 | expect(requests.map((r) => r.tool_choice)).toEqual([ |
| 138 | "auto", |
| 139 | "none", |
| 140 | "required", |
| 141 | "required", |
| 142 | "auto", |
| 143 | "required", |
| 144 | "auto", |
| 145 | ]); |
| 146 | expect(requests.map((r) => r.tools?.map((t) => t.function.name))).toEqual([ |
| 147 | ["primary", "secondary"], |
| 148 | ["primary", "secondary"], |
| 149 | ["primary", "secondary"], |
| 150 | ["primary"], |
| 151 | ["secondary"], |
| 152 | ["primary"], |
| 153 | ["primary", "secondary"], |
| 154 | ]); |
| 155 | for (const request of requests) { |
| 156 | for (const tool of request.tools ?? []) { |
| 157 | if (tool.function.name === "primary") { |
| 158 | expect(tool.function.description).toBe("Météo 東京 🌦️"); |
| 159 | expect(tool.function.parameters).toMatchObject({ |
| 160 | properties: { |
| 161 | count: { type: "string" }, |
| 162 | city: { type: "string", description: "São Paulo / 東京" }, |
| 163 | }, |
| 164 | }); |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | }).pipe(Effect.provide(handlers), Effect.provide(layer)); |
| 169 | }, |
| 170 | { tags }, |
| 171 | ); |
| 172 | |
| 173 | test.effect( |
| 174 | "schema replacement and metadata updates cannot reuse stale preparation", |
| 175 | () => { |
| 176 | const tool = Tool.make("mutable", { |
| 177 | description: "before", |
| 178 | parameters: Schema.Struct({ old: Schema.String }), |
| 179 | }); |
| 180 | const toolkit = Toolkit.make(tool); |
| 181 | const handlers = toolkit.toLayer({ mutable: () => Effect.void }); |
| 182 | const { requests, layer } = capture(); |
| 183 | return Effect.gen(function* () { |
| 184 | yield* LanguageModel.generateText({ prompt: "hi", toolkit }); |
| 185 | // JS consumers can replace readonly fields despite the TS contract. |
| 186 | Object.assign(tool, { |
| 187 | description: "after", |
| 188 | parametersSchema: Schema.Struct({ next: Schema.Boolean }), |
| 189 | }); |
| 190 | yield* LanguageModel.generateText({ prompt: "hi", toolkit }); |
| 191 | expect(requests[0]?.tools?.[0]?.function).toMatchObject({ |
| 192 | description: "before", |
| 193 | parameters: { properties: { old: { type: "string" } } }, |
| 194 | }); |
| 195 | expect(requests[1]?.tools?.[0]?.function).toMatchObject({ |
| 196 | description: "after", |
| 197 | parameters: { properties: { next: { type: "boolean" } } }, |
| 198 | }); |
| 199 | expect(requests[1]?.tools?.[0]?.function.parameters.properties).not.toHaveProperty("old"); |
| 200 | }).pipe(Effect.provide(handlers), Effect.provide(layer)); |
| 201 | }, |
| 202 | { tags }, |
| 203 | ); |
| 204 | |
| 205 | test.effect( |
| 206 | "public setParameters clones prepare their new schema", |
| 207 | () => { |
| 208 | const first = Tool.make("replace", { parameters: Schema.Struct({ old: Schema.String }) }); |
| 209 | const next = first.setParameters(Schema.Struct({ next: Schema.Boolean })); |
| 210 | const oldToolkit = Toolkit.make(first); |
| 211 | const nextToolkit = Toolkit.make(next); |
| 212 | const { requests, layer } = capture(); |
| 213 | return Effect.gen(function* () { |
| 214 | yield* LanguageModel.generateText({ prompt: "hi", toolkit: oldToolkit }).pipe( |
| 215 | Effect.provide(oldToolkit.toLayer({ replace: () => Effect.void })), |
| 216 | ); |
| 217 | yield* LanguageModel.generateText({ prompt: "hi", toolkit: nextToolkit }).pipe( |
| 218 | Effect.provide(nextToolkit.toLayer({ replace: () => Effect.void })), |
| 219 | ); |
| 220 | expect(requests[0]?.tools?.[0]?.function.parameters).toMatchObject({ |
| 221 | properties: { old: { type: "string" } }, |
| 222 | }); |
| 223 | expect(requests[1]?.tools?.[0]?.function.parameters).toMatchObject({ |
| 224 | properties: { next: { type: "boolean" } }, |
| 225 | }); |
| 226 | }).pipe(Effect.provide(layer)); |
| 227 | }, |
| 228 | { tags }, |
| 229 | ); |
| 230 | |
| 231 | test.effect( |
| 232 | "dynamic caller-owned JSON schemas remain live after nested mutation", |
| 233 | () => { |
| 234 | const parameters = { |
| 235 | type: "object" as const, |
| 236 | properties: { value: { type: "string" as "string" | "number" } }, |
| 237 | }; |
| 238 | const dynamic = Tool.dynamic("dynamic", { parameters }); |
| 239 | const toolkit = Toolkit.make(dynamic); |
| 240 | const { requests, layer } = capture(); |
| 241 | return Effect.gen(function* () { |
| 242 | yield* LanguageModel.generateText({ prompt: "hi", toolkit }); |
| 243 | parameters.properties.value.type = "number"; |
| 244 | yield* LanguageModel.generateText({ prompt: "hi", toolkit }); |
| 245 | expect(requests[0]?.tools?.[0]?.function.parameters).toMatchObject({ |
| 246 | properties: { value: { type: "string" } }, |
| 247 | }); |
| 248 | expect(requests[1]?.tools?.[0]?.function.parameters).toMatchObject({ |
| 249 | properties: { value: { type: "number" } }, |
| 250 | }); |
| 251 | }).pipe( |
| 252 | Effect.provide(toolkit.toLayer({ dynamic: () => Effect.void })), |
| 253 | Effect.provide(layer), |
| 254 | ); |
| 255 | }, |
| 256 | { tags }, |
| 257 | ); |
| 258 | |
| 259 | test.effect( |
| 260 | "a runtime client mutating one compiled request cannot poison later requests", |
| 261 | () => { |
| 262 | const tool = Tool.make("isolated", { parameters: Schema.Struct({ value: Schema.String }) }); |
| 263 | const toolkit = Toolkit.make(tool); |
| 264 | const { requests, layer } = capture((request, index) => { |
| 265 | if (index === 0) { |
| 266 | const parameters = request.tools?.[0]?.function.parameters; |
| 267 | if (parameters) { |
| 268 | parameters.properties = {}; |
| 269 | parameters.description = "poisoned"; |
| 270 | } |
| 271 | } |
| 272 | }); |
| 273 | return Effect.gen(function* () { |
| 274 | yield* LanguageModel.generateText({ prompt: "hi", toolkit }); |
| 275 | yield* LanguageModel.generateText({ prompt: "hi", toolkit }); |
| 276 | expect(requests[0]?.tools?.[0]?.function.parameters).toMatchObject({ |
| 277 | properties: { value: { type: "string" } }, |
| 278 | }); |
| 279 | expect(requests[1]?.tools?.[0]?.function.parameters).toEqual( |
| 280 | requests[0]?.tools?.[0]?.function.parameters, |
| 281 | ); |
| 282 | }).pipe( |
| 283 | Effect.provide(toolkit.toLayer({ isolated: () => Effect.void })), |
| 284 | Effect.provide(layer), |
| 285 | ); |
| 286 | }, |
| 287 | { tags }, |
| 288 | ); |
| 289 | }); |