The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 2 | import { expect } from "@alchemy.run/test"; |
| 3 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 4 | import * as Alchemy from "alchemy"; |
| 5 | import * as ConsoleService from "effect/Console"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as Sse from "effect/encoding/Sse"; |
| 8 | import * as HttpClient from "effect/http/HttpClient"; |
| 9 | import { MinimumLogLevel } from "effect/References"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | import * as Stream from "effect/Stream"; |
| 12 | import { Gateway } from "./fixtures/Gateway.ts"; |
| 13 | import LanguageModelTestWorker from "./fixtures/LanguageModelWorker.ts"; |
| 14 | |
| 15 | // Fresh `workers.dev` URLs return non-200 (404 / 500 "Script not |
| 16 | // found") for a few seconds while the edge propagates. Each test uses |
| 17 | // `HttpClient.filterStatusOk(yield* HttpClient.HttpClient)` so the |
| 18 | // existing `Effect.retry` rides through these by converting the |
| 19 | // bad-status response into a retryable Effect failure. |
| 20 | |
| 21 | const { test, beforeAll, afterAll, deploy, destroy } = Test.make({ |
| 22 | providers: Cloudflare.providers(), |
| 23 | }); |
| 24 | |
| 25 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 26 | |
| 27 | const Stack = Alchemy.Stack( |
| 28 | "AiGatewayLanguageModelStack", |
| 29 | { |
| 30 | providers: Cloudflare.providers(), |
| 31 | state: Cloudflare.state(), |
| 32 | }, |
| 33 | Effect.gen(function* () { |
| 34 | const worker = yield* LanguageModelTestWorker; |
| 35 | const gateway = yield* Gateway; |
| 36 | return { |
| 37 | gatewayId: gateway.gatewayId, |
| 38 | url: worker.url.as<string>(), |
| 39 | }; |
| 40 | }), |
| 41 | ); |
| 42 | |
| 43 | const stack = beforeAll(deploy(Stack)); |
| 44 | |
| 45 | afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack)); |
| 46 | |
| 47 | type StreamPart = { |
| 48 | type: string; |
| 49 | id?: string; |
| 50 | name?: string; |
| 51 | delta?: string; |
| 52 | reason?: string; |
| 53 | params?: { city?: string }; |
| 54 | usage?: { |
| 55 | inputTokens?: { total?: number }; |
| 56 | outputTokens?: { total?: number }; |
| 57 | }; |
| 58 | metadata?: { cloudflare?: { neurons?: number } | null }; |
| 59 | }; |
| 60 | |
| 61 | const parseSse = (sse: string): ReadonlyArray<StreamPart> => |
| 62 | sse |
| 63 | .split("\n\n") |
| 64 | .map((frame) => frame.replace(/^data:\s*/, "").trim()) |
| 65 | .filter((line) => line.length > 0) |
| 66 | .map((line) => JSON.parse(line) as StreamPart); |
| 67 | |
| 68 | test( |
| 69 | "deployed worker generates text via AiGateway-backed LanguageModel", |
| 70 | Effect.gen(function* () { |
| 71 | const out = yield* stack; |
| 72 | const client = HttpClient.filterStatusOk(yield* HttpClient.HttpClient); |
| 73 | |
| 74 | const res = yield* client |
| 75 | .get(`${out.url}/generate?prompt=${encodeURIComponent("Say pong.")}`) |
| 76 | .pipe( |
| 77 | Effect.retry({ |
| 78 | schedule: Schedule.exponential("500 millis"), |
| 79 | times: 10, |
| 80 | }), |
| 81 | ); |
| 82 | expect(res.status).toBe(200); |
| 83 | |
| 84 | const body = (yield* res.json) as { |
| 85 | text: string; |
| 86 | finishReason: string; |
| 87 | usage: { |
| 88 | inputTokens: number | undefined; |
| 89 | outputTokens: number | undefined; |
| 90 | }; |
| 91 | neurons: number | undefined; |
| 92 | }; |
| 93 | |
| 94 | expect(typeof body.text).toBe("string"); |
| 95 | expect(body.text.length).toBeGreaterThan(0); |
| 96 | // Refactor invariant: `mapUsage` must populate both input and output |
| 97 | // tokens from Workers AI's `prompt_tokens` / `completion_tokens` fields. |
| 98 | expect(body.usage.inputTokens).toBeGreaterThan(0); |
| 99 | expect(body.usage.outputTokens).toBeGreaterThan(0); |
| 100 | // Refactor invariant: a normal completion maps to `stop`, not `unknown` / |
| 101 | // `other` / `error`. |
| 102 | expect(body.finishReason).toBe("stop"); |
| 103 | expect(body.neurons).toBeGreaterThan(0); |
| 104 | }).pipe(logLevel), |
| 105 | { |
| 106 | tags: ["provider:cloudflare", "provider:cloudflare:ai", "provider:cloudflare:worker", "live"], |
| 107 | timeout: 180_000, |
| 108 | }, |
| 109 | ); |
| 110 | |
| 111 | test( |
| 112 | "deployed worker streams text via AiGateway-backed LanguageModel", |
| 113 | Effect.gen(function* () { |
| 114 | const out = yield* stack; |
| 115 | const client = HttpClient.filterStatusOk(yield* HttpClient.HttpClient); |
| 116 | |
| 117 | // The AI-Gateway-backed model can answer 200 with an empty SSE on a cold |
| 118 | // or transient call (no tokens streamed), which `filterStatusOk` does not |
| 119 | // catch. Fold the parse into the retried effect and fail on an empty / |
| 120 | // unfinished stream so the same backoff rides out the blip. |
| 121 | const parts = yield* client |
| 122 | .get(`${out.url}/stream?prompt=${encodeURIComponent("Say pong.")}`) |
| 123 | .pipe( |
| 124 | Effect.flatMap((res) => res.text), |
| 125 | Effect.map(parseSse), |
| 126 | Effect.flatMap((parts) => |
| 127 | parts.some((p) => p.type === "text-delta") && parts.some((p) => p.type === "finish") |
| 128 | ? Effect.succeed(parts) |
| 129 | : Effect.fail(new Error("AI stream not ready: empty/unfinished")), |
| 130 | ), |
| 131 | Effect.retry({ |
| 132 | schedule: Schedule.exponential("500 millis"), |
| 133 | times: 10, |
| 134 | }), |
| 135 | ); |
| 136 | |
| 137 | const text = parts |
| 138 | .filter((p) => p.type === "text-delta") |
| 139 | .map((p) => p.delta ?? "") |
| 140 | .join(""); |
| 141 | const finish = parts.find((p) => p.type === "finish"); |
| 142 | |
| 143 | expect(parts.length).toBeGreaterThan(0); |
| 144 | expect(text.length).toBeGreaterThan(0); |
| 145 | expect(finish).toBeDefined(); |
| 146 | expect(finish?.metadata?.cloudflare?.neurons).toBeGreaterThan(0); |
| 147 | }).pipe(logLevel), |
| 148 | { |
| 149 | tags: ["provider:cloudflare", "provider:cloudflare:ai", "provider:cloudflare:worker", "live"], |
| 150 | timeout: 180_000, |
| 151 | }, |
| 152 | ); |
| 153 | |
| 154 | test( |
| 155 | "streamed parts respect ordering: text-start → text-delta+ → text-end → finish", |
| 156 | Effect.gen(function* () { |
| 157 | const out = yield* stack; |
| 158 | const client = HttpClient.filterStatusOk(yield* HttpClient.HttpClient); |
| 159 | |
| 160 | // Same cold/transient empty-SSE hazard as above — retry until the stream |
| 161 | // actually carries the structural parts this test asserts ordering over. |
| 162 | const parts = yield* client |
| 163 | .get(`${out.url}/stream?prompt=${encodeURIComponent("Say pong.")}`) |
| 164 | .pipe( |
| 165 | Effect.flatMap((res) => res.text), |
| 166 | Effect.map(parseSse), |
| 167 | Effect.flatMap((parts) => |
| 168 | parts.some((p) => p.type === "text-delta") && parts.some((p) => p.type === "finish") |
| 169 | ? Effect.succeed(parts) |
| 170 | : Effect.fail(new Error("AI stream not ready: empty/unfinished")), |
| 171 | ), |
| 172 | Effect.retry({ |
| 173 | schedule: Schedule.exponential("500 millis"), |
| 174 | times: 10, |
| 175 | }), |
| 176 | ); |
| 177 | |
| 178 | const indexOfType = (type: string) => parts.findIndex((p) => p.type === type); |
| 179 | const lastIndexOfType = (type: string) => { |
| 180 | for (let i = parts.length - 1; i >= 0; i--) { |
| 181 | if (parts[i]!.type === type) return i; |
| 182 | } |
| 183 | return -1; |
| 184 | }; |
| 185 | |
| 186 | // Refactor invariant: `emitTextDelta` opens a text-start before the first |
| 187 | // text-delta, `finalizeStream` emits text-end before the final finish. |
| 188 | const startIdx = indexOfType("text-start"); |
| 189 | const firstDeltaIdx = indexOfType("text-delta"); |
| 190 | const lastDeltaIdx = lastIndexOfType("text-delta"); |
| 191 | const endIdx = indexOfType("text-end"); |
| 192 | const finishIdx = indexOfType("finish"); |
| 193 | |
| 194 | expect(startIdx).toBeGreaterThanOrEqual(0); |
| 195 | expect(firstDeltaIdx).toBeGreaterThan(startIdx); |
| 196 | expect(endIdx).toBeGreaterThan(lastDeltaIdx); |
| 197 | expect(finishIdx).toBe(parts.length - 1); |
| 198 | expect(finishIdx).toBeGreaterThan(endIdx); |
| 199 | |
| 200 | // Exactly one text segment was opened and closed. |
| 201 | expect(parts.filter((p) => p.type === "text-start")).toHaveLength(1); |
| 202 | expect(parts.filter((p) => p.type === "text-end")).toHaveLength(1); |
| 203 | }).pipe(logLevel), |
| 204 | { |
| 205 | tags: ["provider:cloudflare", "provider:cloudflare:ai", "provider:cloudflare:worker", "live"], |
| 206 | timeout: 180_000, |
| 207 | }, |
| 208 | ); |
| 209 | |
| 210 | test.skipIf(!process.env.DEBUG_RAW_STREAM)( |
| 211 | "DEBUG: dump raw Workers AI SSE stream", |
| 212 | Effect.gen(function* () { |
| 213 | const out = yield* stack; |
| 214 | const client = HttpClient.filterStatusOk(yield* HttpClient.HttpClient); |
| 215 | // Through the Console SERVICE so the runner captures it per-test (raw |
| 216 | // fd writes bypass capture and corrupt the reporter/TUI). |
| 217 | const console = yield* ConsoleService.Console; |
| 218 | const print = (s: string) => console.log(s); |
| 219 | |
| 220 | for (const includeUsage of ["0", "1"] as const) { |
| 221 | const res = yield* client |
| 222 | .get( |
| 223 | `${out.url}/raw-stream?include_usage=${includeUsage}&prompt=${encodeURIComponent("Say pong.")}`, |
| 224 | ) |
| 225 | .pipe( |
| 226 | Effect.retry({ |
| 227 | schedule: Schedule.exponential("500 millis"), |
| 228 | times: 10, |
| 229 | }), |
| 230 | ); |
| 231 | print(`\n=== include_usage=${includeUsage} ===\n`); |
| 232 | print(yield* res.text); |
| 233 | print("\n=== end ===\n"); |
| 234 | } |
| 235 | |
| 236 | const toolRes = yield* client |
| 237 | .get( |
| 238 | `${out.url}/raw-tool-stream?prompt=${encodeURIComponent("What's the weather in Seattle?")}`, |
| 239 | ) |
| 240 | .pipe( |
| 241 | Effect.retry({ |
| 242 | schedule: Schedule.exponential("500 millis"), |
| 243 | times: 10, |
| 244 | }), |
| 245 | ); |
| 246 | print(`\n=== raw tool stream ===\n`); |
| 247 | print(yield* toolRes.text); |
| 248 | print("\n=== end tool stream ===\n"); |
| 249 | }).pipe(logLevel), |
| 250 | { |
| 251 | tags: ["provider:cloudflare", "provider:cloudflare:ai", "provider:cloudflare:worker", "live"], |
| 252 | timeout: 180_000, |
| 253 | }, |
| 254 | ); |
| 255 | |
| 256 | test( |
| 257 | "stream finish part reports real token counts and a terminal reason", |
| 258 | Effect.gen(function* () { |
| 259 | const out = yield* stack; |
| 260 | const client = HttpClient.filterStatusOk(yield* HttpClient.HttpClient); |
| 261 | |
| 262 | const res = yield* client |
| 263 | .get( |
| 264 | `${out.url}/stream?prompt=${encodeURIComponent("Say the word 'pong' and nothing else.")}`, |
| 265 | ) |
| 266 | .pipe( |
| 267 | Effect.retry({ |
| 268 | schedule: Schedule.exponential("500 millis"), |
| 269 | times: 10, |
| 270 | }), |
| 271 | ); |
| 272 | expect(res.status).toBe(200); |
| 273 | |
| 274 | const parts = parseSse(yield* res.text); |
| 275 | const finish = parts.find((p) => p.type === "finish"); |
| 276 | expect(finish).toBeDefined(); |
| 277 | // Workers AI emits the real usage chunk and then a zero-valued |
| 278 | // "terminator" usage chunk; the refactor must keep the real one (see |
| 279 | // `hasNonZeroUsage` in updateChunkMeta). |
| 280 | expect(finish?.usage?.inputTokens?.total).toBeGreaterThan(0); |
| 281 | expect(finish?.usage?.outputTokens?.total).toBeGreaterThan(0); |
| 282 | // A clean `[DONE]` falls back to "stop"; some Workers AI responses now |
| 283 | // report the model's real "length" reason even for this short prompt. |
| 284 | expect(["stop", "length"]).toContain(finish?.reason); |
| 285 | }).pipe(logLevel), |
| 286 | { |
| 287 | tags: ["provider:cloudflare", "provider:cloudflare:ai", "provider:cloudflare:worker", "live"], |
| 288 | timeout: 180_000, |
| 289 | }, |
| 290 | ); |
| 291 | |
| 292 | const normalize = (text: string) => text.toLowerCase().replace(/[^a-z]/g, ""); |
| 293 | |
| 294 | // Workers AI models disagree on where a streamed fragment lives. Text can be |
| 295 | // mirrored in both the native `response` field and |
| 296 | // `choices[0].delta.content` of one chunk (llama, qwen3, mistral), set only |
| 297 | // in the OpenAI delta (kimi, gpt-oss), or set only in `response` (qwq, |
| 298 | // deepseek-r1-distill). Reasoning arrives as `reasoning_content`, |
| 299 | // `reasoning`, both mirrored, or inline in the text inside `<think>` tags. |
| 300 | // Each shape must yield every fragment exactly once, in the right part. |
| 301 | const NON_REASONING_MODELS = [ |
| 302 | "@cf/meta/llama-3.3-70b-instruct-fp8-fast", |
| 303 | "@cf/meta/llama-4-scout-17b-16e-instruct", |
| 304 | "@cf/mistralai/mistral-small-3.1-24b-instruct", |
| 305 | ]; |
| 306 | |
| 307 | const REASONING_MODELS = [ |
| 308 | // inline `<think>…</think>` in the native `response` text |
| 309 | "@cf/deepseek-ai/deepseek-r1-distill-qwen-32b", |
| 310 | // inline, but the chat template pre-opens `<think>`: only `</think>` streams |
| 311 | "@cf/qwen/qwq-32b", |
| 312 | // `reasoning` and `reasoning_content` mirrored |
| 313 | "@cf/qwen/qwen3-30b-a3b-fp8", |
| 314 | "@cf/openai/gpt-oss-120b", |
| 315 | "@cf/openai/gpt-oss-20b", |
| 316 | "@cf/zai-org/glm-4.7-flash", |
| 317 | // `reasoning_content` only |
| 318 | "@cf/moonshotai/kimi-k2.6", |
| 319 | "@cf/moonshotai/kimi-k2.5", |
| 320 | "@cf/zai-org/glm-5.3", |
| 321 | "@cf/deepseek-ai/deepseek-v4-flash-0731", |
| 322 | // `reasoning` only |
| 323 | "@cf/google/gemma-4-26b-a4b-it", |
| 324 | ]; |
| 325 | |
| 326 | const TEXT_MODELS = [...NON_REASONING_MODELS, ...REASONING_MODELS]; |
| 327 | |
| 328 | // Models that reliably make a tool call under `tool_choice: "required"`. |
| 329 | const TOOL_MODELS = [ |
| 330 | "@cf/meta/llama-3.3-70b-instruct-fp8-fast", |
| 331 | "@cf/meta/llama-4-scout-17b-16e-instruct", |
| 332 | "@cf/mistralai/mistral-small-3.1-24b-instruct", |
| 333 | "@cf/qwen/qwen3-30b-a3b-fp8", |
| 334 | "@cf/openai/gpt-oss-120b", |
| 335 | "@cf/openai/gpt-oss-20b", |
| 336 | "@cf/zai-org/glm-4.7-flash", |
| 337 | "@cf/moonshotai/kimi-k2.6", |
| 338 | "@cf/deepseek-ai/deepseek-v4-flash-0731", |
| 339 | "@cf/google/gemma-4-26b-a4b-it", |
| 340 | ]; |
| 341 | |
| 342 | const PONG = "Say the word 'pong' and nothing else."; |
| 343 | |
| 344 | const fetchParts = (path: string) => |
| 345 | Effect.gen(function* () { |
| 346 | const out = yield* stack; |
| 347 | const client = HttpClient.filterStatusOk(yield* HttpClient.HttpClient); |
| 348 | return yield* client.get(`${out.url}${path}`).pipe( |
| 349 | Effect.flatMap((res) => res.text), |
| 350 | Effect.map(parseSse), |
| 351 | Effect.flatMap((parts) => |
| 352 | parts.some((p) => p.type === "finish") |
| 353 | ? Effect.succeed(parts) |
| 354 | : Effect.fail(new Error("AI stream not ready: unfinished")), |
| 355 | ), |
| 356 | Effect.retry({ |
| 357 | schedule: Schedule.exponential("500 millis"), |
| 358 | times: 10, |
| 359 | }), |
| 360 | ); |
| 361 | }); |
| 362 | |
| 363 | const streamedText = (parts: ReadonlyArray<StreamPart>) => |
| 364 | parts |
| 365 | .filter((p) => p.type === "text-delta") |
| 366 | .map((p) => p.delta ?? "") |
| 367 | .join(""); |
| 368 | |
| 369 | const textDeltas = (parts: ReadonlyArray<StreamPart>) => |
| 370 | parts.filter((p) => p.type === "text-delta").map((p) => p.delta ?? ""); |
| 371 | |
| 372 | /** |
| 373 | * Assert a streamed answer to {@link PONG} was emitted exactly once. Checks |
| 374 | * the two duplication signatures rather than the exact wording, so verbose |
| 375 | * models (deepseek-r1-distill) don't flake: a fragment mirrored in two fields |
| 376 | * shows up as each delta repeated back to back, and a buffered reply |
| 377 | * re-emitted on finalize shows up as the whole text repeated. |
| 378 | */ |
| 379 | const expectEmittedOnce = (parts: ReadonlyArray<StreamPart>) => { |
| 380 | const deltas = textDeltas(parts); |
| 381 | const text = normalize(deltas.join("")); |
| 382 | expect(text).toContain("pong"); |
| 383 | const repeats = deltas.filter((d, i) => i > 0 && d === deltas[i - 1]).length; |
| 384 | expect(repeats * 2).toBeLessThan(deltas.length); |
| 385 | expect( |
| 386 | text.length % 2 === 0 && text.slice(0, text.length / 2) === text.slice(text.length / 2), |
| 387 | ).toBe(false); |
| 388 | }; |
| 389 | |
| 390 | /** Assert reasoning was split out of the answer, with no `<think>` tags left over. */ |
| 391 | const expectReasoningSeparated = (reasoning: string | undefined, text: string) => { |
| 392 | expect(reasoning?.trim().length).toBeGreaterThan(0); |
| 393 | expect(reasoning).not.toContain("think>"); |
| 394 | expect(text).not.toContain("think>"); |
| 395 | expect(text).not.toContain(reasoning!.trim().slice(0, 40)); |
| 396 | expect(normalize(text)).toContain("pong"); |
| 397 | }; |
| 398 | |
| 399 | for (const model of TEXT_MODELS) { |
| 400 | test( |
| 401 | `streamed text deltas are emitted exactly once (${model})`, |
| 402 | Effect.gen(function* () { |
| 403 | const parts = yield* fetchParts( |
| 404 | `/stream?model=${encodeURIComponent(model)}&prompt=${encodeURIComponent(PONG)}`, |
| 405 | ); |
| 406 | expectEmittedOnce(parts); |
| 407 | }).pipe(logLevel), |
| 408 | { |
| 409 | tags: ["provider:cloudflare", "provider:cloudflare:ai", "provider:cloudflare:worker", "live"], |
| 410 | timeout: 180_000, |
| 411 | }, |
| 412 | ); |
| 413 | |
| 414 | test( |
| 415 | `streamed text with tools available is emitted exactly once (${model})`, |
| 416 | Effect.gen(function* () { |
| 417 | // With tools passed, the mirrored native `response` text used to be |
| 418 | // buffered as a candidate tool call and re-emitted as a second copy |
| 419 | // of the whole reply on finalize. |
| 420 | const parts = yield* fetchParts( |
| 421 | `/tool-stream?toolChoice=auto&model=${encodeURIComponent(model)}&prompt=${encodeURIComponent( |
| 422 | `Do not call any tools. ${PONG}`, |
| 423 | )}`, |
| 424 | ); |
| 425 | expect(parts.some((p) => p.type === "tool-call")).toBe(false); |
| 426 | expectEmittedOnce(parts); |
| 427 | expect(parts.filter((p) => p.type === "text-start")).toHaveLength(1); |
| 428 | }).pipe(logLevel), |
| 429 | { |
| 430 | tags: ["provider:cloudflare", "provider:cloudflare:ai", "provider:cloudflare:worker", "live"], |
| 431 | timeout: 180_000, |
| 432 | }, |
| 433 | ); |
| 434 | } |
| 435 | |
| 436 | for (const model of TOOL_MODELS) { |
| 437 | test( |
| 438 | `streams a resolvable tool-call part (${model})`, |
| 439 | Effect.gen(function* () { |
| 440 | const parts = yield* fetchParts( |
| 441 | `/tool-stream?model=${encodeURIComponent(model)}&prompt=${encodeURIComponent( |
| 442 | "What's the weather in Paris?", |
| 443 | )}`, |
| 444 | ); |
| 445 | // Effect AI only sees (and resolves) a streamed tool call through a |
| 446 | // `tool-call` part. |
| 447 | const call = parts.find((p) => p.type === "tool-call"); |
| 448 | expect(call?.name).toBe("get_weather"); |
| 449 | expect(call?.params?.city?.toLowerCase()).toContain("paris"); |
| 450 | expect(parts.some((p) => p.type === "tool-result" && p.id === call?.id)).toBe(true); |
| 451 | }).pipe(logLevel), |
| 452 | { |
| 453 | tags: ["provider:cloudflare", "provider:cloudflare:ai", "provider:cloudflare:worker", "live"], |
| 454 | timeout: 180_000, |
| 455 | }, |
| 456 | ); |
| 457 | } |
| 458 | |
| 459 | const reasoningOf = (parts: ReadonlyArray<StreamPart>) => |
| 460 | parts |
| 461 | .filter((p) => p.type === "reasoning-delta") |
| 462 | .map((p) => p.delta ?? "") |
| 463 | .join(""); |
| 464 | |
| 465 | for (const model of REASONING_MODELS) { |
| 466 | test( |
| 467 | `streams reasoning separately from text (${model})`, |
| 468 | Effect.gen(function* () { |
| 469 | const parts = yield* fetchParts( |
| 470 | `/stream?model=${encodeURIComponent(model)}&prompt=${encodeURIComponent(PONG)}`, |
| 471 | ); |
| 472 | expectReasoningSeparated(reasoningOf(parts), streamedText(parts)); |
| 473 | // One reasoning block, closed before the text block opens. |
| 474 | expect(parts.filter((p) => p.type === "reasoning-start")).toHaveLength(1); |
| 475 | expect(parts.filter((p) => p.type === "reasoning-end")).toHaveLength(1); |
| 476 | expect(parts.findIndex((p) => p.type === "reasoning-end")).toBeLessThan( |
| 477 | parts.findIndex((p) => p.type === "text-start"), |
| 478 | ); |
| 479 | }).pipe(logLevel), |
| 480 | { |
| 481 | tags: ["provider:cloudflare", "provider:cloudflare:ai", "provider:cloudflare:worker", "live"], |
| 482 | timeout: 180_000, |
| 483 | }, |
| 484 | ); |
| 485 | |
| 486 | test( |
| 487 | `generates reasoning separately from text (${model})`, |
| 488 | Effect.gen(function* () { |
| 489 | const out = yield* stack; |
| 490 | const client = HttpClient.filterStatusOk(yield* HttpClient.HttpClient); |
| 491 | const res = yield* client |
| 492 | .get( |
| 493 | `${out.url}/generate?model=${encodeURIComponent(model)}&prompt=${encodeURIComponent(PONG)}`, |
| 494 | ) |
| 495 | .pipe( |
| 496 | Effect.retry({ |
| 497 | schedule: Schedule.exponential("500 millis"), |
| 498 | times: 10, |
| 499 | }), |
| 500 | ); |
| 501 | const body = (yield* res.json) as { text: string; reasoningText: string | undefined }; |
| 502 | expectReasoningSeparated(body.reasoningText, body.text); |
| 503 | }).pipe(logLevel), |
| 504 | { |
| 505 | tags: ["provider:cloudflare", "provider:cloudflare:ai", "provider:cloudflare:worker", "live"], |
| 506 | timeout: 180_000, |
| 507 | }, |
| 508 | ); |
| 509 | } |
| 510 | |
| 511 | test( |
| 512 | "stream emits multiple text-delta chunks for a long-form response", |
| 513 | Effect.gen(function* () { |
| 514 | const out = yield* stack; |
| 515 | const client = HttpClient.filterStatusOk(yield* HttpClient.HttpClient); |
| 516 | |
| 517 | const res = yield* client |
| 518 | .get( |
| 519 | `${out.url}/stream?prompt=${encodeURIComponent( |
| 520 | "Write a short paragraph (around 80 words) about why TypeScript developers might enjoy Effect TS.", |
| 521 | )}`, |
| 522 | ) |
| 523 | .pipe( |
| 524 | Effect.retry({ |
| 525 | schedule: Schedule.exponential("500 millis"), |
| 526 | times: 10, |
| 527 | }), |
| 528 | ); |
| 529 | expect(res.status).toBe(200); |
| 530 | |
| 531 | const parts = parseSse(yield* res.text); |
| 532 | const deltas = parts.filter((p) => p.type === "text-delta"); |
| 533 | // Refactor invariant: each model SSE chunk produces a text-delta — so a |
| 534 | // long response should produce many. A single fused delta would mean we |
| 535 | // accidentally buffered the stream. |
| 536 | expect(deltas.length).toBeGreaterThan(3); |
| 537 | const total = deltas.map((p) => p.delta ?? "").join("").length; |
| 538 | expect(total).toBeGreaterThan(20); |
| 539 | }).pipe(logLevel), |
| 540 | { |
| 541 | tags: ["provider:cloudflare", "provider:cloudflare:ai", "provider:cloudflare:worker", "live"], |
| 542 | timeout: 180_000, |
| 543 | }, |
| 544 | ); |
| 545 | |
| 546 | // `persisted chat survives across DO invocations` exercises the |
| 547 | // /chat?id=... route which is wired by `ChatAgent` from the Agent |
| 548 | // slice. The fixture in this slice (`LanguageModelWorker`) does not |
| 549 | // include that route — the test re-activates in the Agent PR. |
| 550 | test.skip( |
| 551 | "persisted chat survives across DO invocations", |
| 552 | Effect.gen(function* () { |
| 553 | const out = yield* stack; |
| 554 | const client = HttpClient.filterStatusOk(yield* HttpClient.HttpClient); |
| 555 | const id = `test-${Date.now()}`; |
| 556 | |
| 557 | const r1 = yield* client |
| 558 | .get(`${out.url}/chat?id=${id}&prompt=${encodeURIComponent("My name is Sam. Remember it.")}`) |
| 559 | .pipe( |
| 560 | Effect.retry({ |
| 561 | schedule: Schedule.exponential("500 millis"), |
| 562 | times: 10, |
| 563 | }), |
| 564 | ); |
| 565 | if (r1.status !== 200) { |
| 566 | console.error("turn1 error body:", yield* r1.text); |
| 567 | } |
| 568 | expect(r1.status).toBe(200); |
| 569 | const b1 = (yield* r1.json) as { text: string; turns: number }; |
| 570 | expect(b1.turns).toBeGreaterThanOrEqual(2); |
| 571 | |
| 572 | const r2 = yield* client |
| 573 | .get( |
| 574 | `${out.url}/chat?id=${id}&prompt=${encodeURIComponent("What is my name? Answer with just the name.")}`, |
| 575 | ) |
| 576 | .pipe( |
| 577 | Effect.retry({ |
| 578 | schedule: Schedule.exponential("500 millis"), |
| 579 | times: 10, |
| 580 | }), |
| 581 | ); |
| 582 | expect(r2.status).toBe(200); |
| 583 | const b2 = (yield* r2.json) as { text: string; turns: number }; |
| 584 | |
| 585 | expect(b2.text.toLowerCase()).toContain("sam"); |
| 586 | expect(b2.turns).toBeGreaterThanOrEqual(4); |
| 587 | }).pipe(logLevel), |
| 588 | { |
| 589 | tags: ["provider:cloudflare", "provider:cloudflare:ai", "provider:cloudflare:worker", "live"], |
| 590 | timeout: 240_000, |
| 591 | }, |
| 592 | ); |
| 593 | |
| 594 | test( |
| 595 | "deployed worker invokes a tool via AiGateway-backed LanguageModel", |
| 596 | Effect.gen(function* () { |
| 597 | const out = yield* stack; |
| 598 | const client = HttpClient.filterStatusOk(yield* HttpClient.HttpClient); |
| 599 | |
| 600 | const res = yield* client |
| 601 | .get(`${out.url}/tool?prompt=${encodeURIComponent("What's the weather in San Francisco?")}`) |
| 602 | .pipe( |
| 603 | Effect.retry({ |
| 604 | schedule: Schedule.exponential("500 millis"), |
| 605 | times: 10, |
| 606 | }), |
| 607 | ); |
| 608 | if (res.status !== 200) { |
| 609 | console.error("tool error body:", yield* res.text); |
| 610 | } |
| 611 | expect(res.status).toBe(200); |
| 612 | |
| 613 | const body = (yield* res.json) as { |
| 614 | text: string; |
| 615 | finishReason: string; |
| 616 | toolCalls: Array<{ |
| 617 | id: string; |
| 618 | name: string; |
| 619 | params: { city: string }; |
| 620 | }>; |
| 621 | toolResults: Array<{ |
| 622 | id: string; |
| 623 | name: string; |
| 624 | result: { city: string; temperatureF: number; condition: string }; |
| 625 | isFailure: boolean; |
| 626 | }>; |
| 627 | }; |
| 628 | |
| 629 | expect(body.toolCalls.length).toBeGreaterThan(0); |
| 630 | const call = body.toolCalls[0]!; |
| 631 | expect(call.name).toBe("get_weather"); |
| 632 | expect(typeof call.params.city).toBe("string"); |
| 633 | expect(call.params.city.toLowerCase()).toContain("san francisco"); |
| 634 | |
| 635 | expect(body.toolResults.length).toBeGreaterThan(0); |
| 636 | const result = body.toolResults[0]!; |
| 637 | expect(result.name).toBe("get_weather"); |
| 638 | expect(result.isFailure).toBe(false); |
| 639 | expect(result.result.temperatureF).toBe(72); |
| 640 | expect(result.result.condition).toBe("sunny"); |
| 641 | }).pipe(logLevel), |
| 642 | { |
| 643 | tags: ["provider:cloudflare", "provider:cloudflare:ai", "provider:cloudflare:worker", "live"], |
| 644 | timeout: 180_000, |
| 645 | }, |
| 646 | ); |
| 647 | |
| 648 | test( |
| 649 | "streams tool-call parts via AiGateway-backed LanguageModel", |
| 650 | Effect.gen(function* () { |
| 651 | const out = yield* stack; |
| 652 | const client = HttpClient.filterStatusOk(yield* HttpClient.HttpClient); |
| 653 | |
| 654 | const res = yield* client |
| 655 | .get(`${out.url}/tool-stream?prompt=${encodeURIComponent("What's the weather in Seattle?")}`) |
| 656 | .pipe( |
| 657 | Effect.retry({ |
| 658 | schedule: Schedule.exponential("500 millis"), |
| 659 | times: 10, |
| 660 | }), |
| 661 | ); |
| 662 | expect(res.status).toBe(200); |
| 663 | |
| 664 | const parts = parseSse(yield* res.text); |
| 665 | const toolParamsStart = parts.filter((p) => p.type === "tool-params-start"); |
| 666 | const toolParamsDeltas = parts.filter((p) => p.type === "tool-params-delta"); |
| 667 | const toolParamsEnd = parts.filter((p) => p.type === "tool-params-end"); |
| 668 | |
| 669 | expect(toolParamsStart.length).toBeGreaterThan(0); |
| 670 | expect(toolParamsStart[0]?.name).toBe("get_weather"); |
| 671 | expect(toolParamsDeltas.length).toBeGreaterThan(0); |
| 672 | |
| 673 | // Refactor invariant: `finalizeStream` closes any tool calls still open |
| 674 | // when the stream ends, emitting one `tool-params-end` per opened call. |
| 675 | expect(toolParamsEnd.length).toBe(toolParamsStart.length); |
| 676 | // Each start is matched to an end by id. |
| 677 | const startIds = new Set(toolParamsStart.map((p) => p.id)); |
| 678 | const endIds = new Set(toolParamsEnd.map((p) => p.id)); |
| 679 | expect(endIds).toEqual(startIds); |
| 680 | |
| 681 | // Ordering: every tool-params-end comes after its corresponding start, |
| 682 | // every delta with the same id falls between start and end, and finish |
| 683 | // is last. |
| 684 | const indexOf = (type: string, id: string | undefined) => |
| 685 | parts.findIndex((p) => p.type === type && p.id === id); |
| 686 | for (const start of toolParamsStart) { |
| 687 | const startIdx = indexOf("tool-params-start", start.id); |
| 688 | const endIdx = indexOf("tool-params-end", start.id); |
| 689 | expect(endIdx).toBeGreaterThan(startIdx); |
| 690 | for (const delta of toolParamsDeltas.filter((p) => p.id === start.id)) { |
| 691 | const dIdx = parts.indexOf(delta); |
| 692 | expect(dIdx).toBeGreaterThan(startIdx); |
| 693 | expect(dIdx).toBeLessThan(endIdx); |
| 694 | } |
| 695 | } |
| 696 | expect(parts[parts.length - 1]?.type).toBe("finish"); |
| 697 | |
| 698 | // Effect AI only sees (and resolves) a streamed tool call through a |
| 699 | // `tool-call` part — `tool-params-*` parts alone are never assembled |
| 700 | // into one (#1907). |
| 701 | const toolCalls = parts.filter((p) => p.type === "tool-call"); |
| 702 | expect(toolCalls.length).toBe(toolParamsStart.length); |
| 703 | for (const call of toolCalls) { |
| 704 | expect(call.name).toBe("get_weather"); |
| 705 | expect(call.params?.city?.toLowerCase()).toContain("seattle"); |
| 706 | expect(parts.indexOf(call)).toBeGreaterThan(indexOf("tool-params-end", call.id)); |
| 707 | } |
| 708 | expect(parts.some((p) => p.type === "tool-result" && p.name === "get_weather")).toBe(true); |
| 709 | }).pipe(logLevel), |
| 710 | { |
| 711 | tags: ["provider:cloudflare", "provider:cloudflare:ai", "provider:cloudflare:worker", "live"], |
| 712 | timeout: 180_000, |
| 713 | }, |
| 714 | ); |
| 715 | |
| 716 | test( |
| 717 | "concatenated tool-params-delta payloads parse back into the requested arguments", |
| 718 | Effect.gen(function* () { |
| 719 | const out = yield* stack; |
| 720 | const client = HttpClient.filterStatusOk(yield* HttpClient.HttpClient); |
| 721 | |
| 722 | const res = yield* client |
| 723 | .get(`${out.url}/tool-stream?prompt=${encodeURIComponent("What's the weather in Portland?")}`) |
| 724 | .pipe( |
| 725 | Effect.retry({ |
| 726 | schedule: Schedule.exponential("500 millis"), |
| 727 | times: 10, |
| 728 | }), |
| 729 | ); |
| 730 | expect(res.status).toBe(200); |
| 731 | |
| 732 | const parts = parseSse(yield* res.text); |
| 733 | const toolParamsStart = parts.filter((p) => p.type === "tool-params-start"); |
| 734 | expect(toolParamsStart.length).toBeGreaterThan(0); |
| 735 | const firstId = toolParamsStart[0]!.id!; |
| 736 | |
| 737 | // Refactor invariant: `handleToolDeltas` emits each `arguments` fragment |
| 738 | // as a `tool-params-delta` in order; concatenating them yields a valid |
| 739 | // JSON document with the originally-requested fields. |
| 740 | const joined = parts |
| 741 | .filter((p) => p.type === "tool-params-delta" && p.id === firstId) |
| 742 | .map((p) => p.delta ?? "") |
| 743 | .join(""); |
| 744 | const args = yield* Effect.try({ |
| 745 | try: () => JSON.parse(joined) as { city?: string }, |
| 746 | catch: (cause) => |
| 747 | new Error(`Invalid concatenated tool arguments ${JSON.stringify(joined)}: ${cause}`), |
| 748 | }); |
| 749 | expect(typeof args.city).toBe("string"); |
| 750 | expect(args.city!.toLowerCase()).toContain("portland"); |
| 751 | }).pipe(logLevel), |
| 752 | { |
| 753 | tags: ["provider:cloudflare", "provider:cloudflare:ai", "provider:cloudflare:worker", "live"], |
| 754 | timeout: 180_000, |
| 755 | }, |
| 756 | ); |
| 757 | |
| 758 | test( |
| 759 | "streams Effect-native parts and prints them live", |
| 760 | Effect.gen(function* () { |
| 761 | const out = yield* stack; |
| 762 | const client = HttpClient.filterStatusOk(yield* HttpClient.HttpClient); |
| 763 | |
| 764 | const res = yield* client |
| 765 | .get(`${out.url}/stream?prompt=${encodeURIComponent("Write a short haiku about Effect TS.")}`) |
| 766 | .pipe( |
| 767 | Effect.retry({ |
| 768 | schedule: Schedule.exponential("500 millis"), |
| 769 | times: 10, |
| 770 | }), |
| 771 | ); |
| 772 | expect(res.status).toBe(200); |
| 773 | |
| 774 | // Through the Console SERVICE: the runner buffers it per-test (the TUI |
| 775 | // detail pane live-tails it), and nothing leaks into the reporter |
| 776 | // output. Raw fd writes (the old fs.writeSync(2, ...)) bypass capture. |
| 777 | const console = yield* ConsoleService.Console; |
| 778 | const print = (s: string) => console.log(s); |
| 779 | |
| 780 | print("--- live stream begin ---"); |
| 781 | let collected = ""; |
| 782 | |
| 783 | yield* res.stream.pipe( |
| 784 | Stream.orDie, |
| 785 | Stream.decodeText(), |
| 786 | Stream.pipeThroughChannel(Sse.decode<never, unknown>()), |
| 787 | Stream.runForEach((event) => |
| 788 | Effect.sync(() => { |
| 789 | const part = JSON.parse(event.data) as { |
| 790 | type: string; |
| 791 | delta?: string; |
| 792 | id?: string; |
| 793 | reason?: string; |
| 794 | }; |
| 795 | switch (part.type) { |
| 796 | case "text-start": |
| 797 | print(`[text-start id=${part.id}]\n`); |
| 798 | break; |
| 799 | case "text-delta": |
| 800 | print(part.delta ?? ""); |
| 801 | collected += part.delta ?? ""; |
| 802 | break; |
| 803 | case "text-end": |
| 804 | print(`\n[text-end id=${part.id}]\n`); |
| 805 | break; |
| 806 | case "finish": |
| 807 | print(`[finish reason=${part.reason}]\n`); |
| 808 | break; |
| 809 | default: |
| 810 | print(`[${part.type}]\n`); |
| 811 | } |
| 812 | }), |
| 813 | ), |
| 814 | ); |
| 815 | |
| 816 | print("--- live stream end ---\n"); |
| 817 | expect(collected.length).toBeGreaterThan(0); |
| 818 | }).pipe(logLevel), |
| 819 | { |
| 820 | tags: ["provider:cloudflare", "provider:cloudflare:ai", "provider:cloudflare:worker", "live"], |
| 821 | timeout: 180_000, |
| 822 | }, |
| 823 | ); |