The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 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 Effect from "effect/Effect"; |
| 6 | import { MinimumLogLevel } from "effect/References"; |
| 7 | import * as RpcClient from "effect/rpc/RpcClient"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import * as Stream from "effect/Stream"; |
| 10 | import ChatPersistenceRpcWorker from "./fixtures/ChatPersistenceRpcWorker.ts"; |
| 11 | import { ChatRpcs } from "./fixtures/ChatRpcs.ts"; |
| 12 | import { Gateway } from "./fixtures/Gateway.ts"; |
| 13 | |
| 14 | const { test, beforeAll, afterAll, deploy, destroy } = Test.make({ |
| 15 | providers: Cloudflare.providers(), |
| 16 | }); |
| 17 | |
| 18 | // `Test.rpcClientLayer` speaks `RpcSerialization.ndjson` (streaming |
| 19 | // procedures require newline framing on the wire, matching the worker) and |
| 20 | // guards the transport against edge-generated HTML bodies (workers.dev |
| 21 | // placeholder, error pages) that the RPC protocol would otherwise surface as |
| 22 | // an opaque `RpcClientDefect`; see Test/Http.ts. |
| 23 | const clientLayer = Test.rpcClientLayer; |
| 24 | |
| 25 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 26 | |
| 27 | const Stack = Alchemy.Stack( |
| 28 | "AiGatewayChatPersistenceRpcStack", |
| 29 | { |
| 30 | providers: Cloudflare.providers(), |
| 31 | state: Cloudflare.state(), |
| 32 | }, |
| 33 | Effect.gen(function* () { |
| 34 | const worker = yield* ChatPersistenceRpcWorker; |
| 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 | afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack)); |
| 45 | |
| 46 | // Cap exponential backoff at 3s so retries stay bounded when the CF edge is |
| 47 | // slow (otherwise the geometric blow-up dominates wall time). |
| 48 | const readinessSchedule = Schedule.min([ |
| 49 | Schedule.exponential("500 millis"), |
| 50 | Schedule.spaced("3 seconds"), |
| 51 | ]); |
| 52 | |
| 53 | // A freshly deployed worker isn't live on every Cloudflare edge yet, so the |
| 54 | // first RPC calls fail with `Handler does not export a fetch() function.` — |
| 55 | // which arrives at the client as a DEFECT, and `Effect.retry` does not retry |
| 56 | // defects. Promote defects to failures so the readiness retry absorbs the |
| 57 | // transient cold-start error (a genuine bug just keeps failing until the |
| 58 | // retry budget is exhausted). |
| 59 | const retryReady = <A, E, R>(eff: Effect.Effect<A, E, R>) => |
| 60 | eff.pipe( |
| 61 | Effect.catchDefect((defect) => Effect.fail(defect)), |
| 62 | Effect.retry({ schedule: readinessSchedule, times: 15 }), |
| 63 | ); |
| 64 | |
| 65 | test( |
| 66 | "send round-trips text and turns through the RpcWorker → DO", |
| 67 | Effect.gen(function* () { |
| 68 | const { url } = yield* stack; |
| 69 | const id = `rpc-send-${Date.now()}`; |
| 70 | |
| 71 | yield* Effect.gen(function* () { |
| 72 | const client = yield* RpcClient.make(ChatRpcs); |
| 73 | const result = yield* client |
| 74 | .send({ id, prompt: "Say the single word 'pong'." }) |
| 75 | .pipe(retryReady); |
| 76 | |
| 77 | expect(typeof result.text).toBe("string"); |
| 78 | expect(result.text.length).toBeGreaterThan(0); |
| 79 | // One turn appends the user prompt and the assistant reply. |
| 80 | expect(result.turns).toBe(2); |
| 81 | }).pipe(Effect.scoped, Effect.provide(clientLayer(url))); |
| 82 | }).pipe(logLevel), |
| 83 | { |
| 84 | tags: ["provider:cloudflare", "provider:cloudflare:ai", "live"], |
| 85 | timeout: 180_000, |
| 86 | }, |
| 87 | ); |
| 88 | |
| 89 | test( |
| 90 | "streamMessage yields effect/ai Response parts that decode to real classes", |
| 91 | Effect.gen(function* () { |
| 92 | const { url } = yield* stack; |
| 93 | const id = `rpc-stream-${Date.now()}`; |
| 94 | |
| 95 | yield* Effect.gen(function* () { |
| 96 | const client = yield* RpcClient.make(ChatRpcs); |
| 97 | |
| 98 | const parts = yield* client |
| 99 | .streamMessage({ |
| 100 | id, |
| 101 | prompt: "Write a short sentence about Effect TS.", |
| 102 | }) |
| 103 | .pipe(Stream.runCollect, retryReady); |
| 104 | |
| 105 | // The stream parts arrived as decoded `effect/ai` instances (a |
| 106 | // discriminated union by `type`), not opaque JSON. |
| 107 | expect(parts.length).toBeGreaterThan(0); |
| 108 | |
| 109 | const text = parts |
| 110 | .filter((p) => p.type === "text-delta") |
| 111 | .map((p) => p.delta) |
| 112 | .join(""); |
| 113 | expect(text.length).toBeGreaterThan(0); |
| 114 | |
| 115 | const finish = parts.find((p) => p.type === "finish"); |
| 116 | expect(finish).toBeDefined(); |
| 117 | }).pipe(Effect.scoped, Effect.provide(clientLayer(url))); |
| 118 | }).pipe(logLevel), |
| 119 | { |
| 120 | tags: ["provider:cloudflare", "provider:cloudflare:ai", "live"], |
| 121 | timeout: 180_000, |
| 122 | }, |
| 123 | ); |
| 124 | |
| 125 | test( |
| 126 | "streamed turn is persisted: a follow-up send recalls it", |
| 127 | Effect.gen(function* () { |
| 128 | const { url } = yield* stack; |
| 129 | const id = `rpc-memory-${Date.now()}`; |
| 130 | |
| 131 | yield* Effect.gen(function* () { |
| 132 | const client = yield* RpcClient.make(ChatRpcs); |
| 133 | |
| 134 | // First turn over the streaming procedure — persistence saves the |
| 135 | // appended turn when the stream finalizes. |
| 136 | yield* client |
| 137 | .streamMessage({ id, prompt: "My name is Sam. Remember it." }) |
| 138 | .pipe(Stream.runDrain, retryReady); |
| 139 | |
| 140 | // Second turn hits the same DO instance; history is reloaded from |
| 141 | // `state.storage`, so the model can recall the name and the turn |
| 142 | // count reflects the restored history (2 from the stream + 2). |
| 143 | const result = yield* client |
| 144 | .send({ id, prompt: "What is my name? Answer with just the name." }) |
| 145 | .pipe(retryReady); |
| 146 | |
| 147 | expect(result.text.toLowerCase()).toContain("sam"); |
| 148 | expect(result.turns).toBe(4); |
| 149 | }).pipe(Effect.scoped, Effect.provide(clientLayer(url))); |
| 150 | }).pipe(logLevel), |
| 151 | { |
| 152 | tags: ["provider:cloudflare", "provider:cloudflare:ai", "live"], |
| 153 | timeout: 240_000, |
| 154 | }, |
| 155 | ); |