@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 250 lines 7.94 KB
Raw
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 * as HttpClient from "effect/http/HttpClient";
7 import { MinimumLogLevel } from "effect/References";
8 import * as Schedule from "effect/Schedule";
9 import { AiAsyncWorker } from "./fixtures/AiAsyncWorker.ts";
10 import AiBindingTestWorker from "./fixtures/AiBindingWorker.ts";
11
12 // Fresh `workers.dev` URLs return non-200 (404 / 500 "Script not found") for
13 // a few seconds while the edge propagates. Each test uses
14 // `HttpClient.filterStatusOk` so `Effect.retry` rides through these by
15 // converting the bad-status response into a retryable Effect failure.
16
17 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
18 providers: Cloudflare.providers(),
19 });
20
21 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
22
23 const Stack = Alchemy.Stack(
24 "AiBindingStack",
25 {
26 providers: Cloudflare.providers(),
27 state: Cloudflare.state(),
28 },
29 Effect.gen(function* () {
30 const worker = yield* AiBindingTestWorker;
31 const asyncWorker = yield* AiAsyncWorker;
32 return {
33 url: worker.url.as<string>(),
34 asyncUrl: asyncWorker.url.as<string>(),
35 };
36 }),
37 );
38
39 const stack = beforeAll(deploy(Stack));
40 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack));
41
42 type StreamPart = {
43 type: string;
44 delta?: string;
45 reason?: string;
46 };
47
48 const parseSse = (sse: string): ReadonlyArray<StreamPart> =>
49 sse
50 .split("\n\n")
51 .map((frame) => frame.replace(/^data:\s*/, "").trim())
52 .filter((line) => line.length > 0)
53 .map((line) => JSON.parse(line) as StreamPart);
54
55 test(
56 "deployed worker runs a Workers AI model via the plain AI binding",
57 Effect.gen(function* () {
58 const out = yield* stack;
59 const client = HttpClient.filterStatusOk(yield* HttpClient.HttpClient);
60
61 const res = yield* client.get(`${out.url}/run?prompt=${encodeURIComponent("Say pong.")}`).pipe(
62 Effect.retry({
63 schedule: Schedule.exponential("500 millis"),
64 times: 10,
65 }),
66 );
67 expect(res.status).toBe(200);
68
69 // Workers AI answers either the native shape (`{ response }`) or the
70 // OpenAI shape (`{ choices: [{ message: { content } }] }`).
71 const body = (yield* res.json) as {
72 response?: string;
73 choices?: Array<{ message?: { content?: string } }>;
74 };
75 const text = body.response ?? body.choices?.[0]?.message?.content;
76 expect(typeof text).toBe("string");
77 expect((text as string).length).toBeGreaterThan(0);
78 }).pipe(logLevel),
79 {
80 tags: ["provider:cloudflare", "provider:cloudflare:ai", "provider:cloudflare:worker", "live"],
81 timeout: 180_000,
82 },
83 );
84
85 test(
86 "deployed worker lists Workers AI models via the binding",
87 Effect.gen(function* () {
88 const out = yield* stack;
89 const client = HttpClient.filterStatusOk(yield* HttpClient.HttpClient);
90
91 const res = yield* client.get(`${out.url}/models`).pipe(
92 Effect.retry({
93 schedule: Schedule.exponential("500 millis"),
94 times: 10,
95 }),
96 );
97 expect(res.status).toBe(200);
98
99 const body = (yield* res.json) as { count: number; names: string[] };
100 expect(body.count).toBeGreaterThan(0);
101 expect(body.names.some((name) => name.includes("llama-3.3"))).toBe(true);
102 }).pipe(logLevel),
103 {
104 tags: ["provider:cloudflare", "provider:cloudflare:ai", "provider:cloudflare:worker", "live"],
105 timeout: 180_000,
106 },
107 );
108
109 test(
110 "deployed worker generates text via AI-binding-backed LanguageModel",
111 Effect.gen(function* () {
112 const out = yield* stack;
113 const client = HttpClient.filterStatusOk(yield* HttpClient.HttpClient);
114
115 const res = yield* client
116 .get(`${out.url}/generate?prompt=${encodeURIComponent("Say pong.")}`)
117 .pipe(
118 Effect.retry({
119 schedule: Schedule.exponential("500 millis"),
120 times: 10,
121 }),
122 );
123 expect(res.status).toBe(200);
124
125 const body = (yield* res.json) as {
126 text: string;
127 finishReason: string;
128 usage: {
129 inputTokens: number | undefined;
130 outputTokens: number | undefined;
131 };
132 };
133
134 expect(typeof body.text).toBe("string");
135 expect(body.text.length).toBeGreaterThan(0);
136 // Invariant shared with the gateway-backed adapter: `mapUsage` populates
137 // both counts and a normal completion maps to `stop`.
138 expect(body.usage.inputTokens).toBeGreaterThan(0);
139 expect(body.usage.outputTokens).toBeGreaterThan(0);
140 expect(body.finishReason).toBe("stop");
141 }).pipe(logLevel),
142 {
143 tags: ["provider:cloudflare", "provider:cloudflare:ai", "provider:cloudflare:worker", "live"],
144 timeout: 180_000,
145 },
146 );
147
148 test(
149 "async worker runs a Workers AI model via env AI binding",
150 Effect.gen(function* () {
151 const out = yield* stack;
152 const client = HttpClient.filterStatusOk(yield* HttpClient.HttpClient);
153
154 const res = yield* client
155 .get(`${out.asyncUrl}/run?prompt=${encodeURIComponent("Say pong.")}`)
156 .pipe(
157 Effect.retry({
158 schedule: Schedule.exponential("500 millis"),
159 times: 10,
160 }),
161 );
162 expect(res.status).toBe(200);
163
164 const body = (yield* res.json) as {
165 mode: string;
166 result: {
167 response?: string;
168 choices?: Array<{ message?: { content?: string } }>;
169 };
170 };
171 expect(body.mode).toBe("async");
172 const text = body.result.response ?? body.result.choices?.[0]?.message?.content;
173 expect(typeof text).toBe("string");
174 expect((text as string).length).toBeGreaterThan(0);
175 }).pipe(logLevel),
176 {
177 tags: ["provider:cloudflare", "provider:cloudflare:ai", "provider:cloudflare:worker", "live"],
178 timeout: 180_000,
179 },
180 );
181
182 test(
183 "async worker lists Workers AI models via env AI binding",
184 Effect.gen(function* () {
185 const out = yield* stack;
186 const client = HttpClient.filterStatusOk(yield* HttpClient.HttpClient);
187
188 const res = yield* client.get(`${out.asyncUrl}/models`).pipe(
189 Effect.retry({
190 schedule: Schedule.exponential("500 millis"),
191 times: 10,
192 }),
193 );
194 expect(res.status).toBe(200);
195
196 const body = (yield* res.json) as {
197 mode: string;
198 count: number;
199 names: string[];
200 };
201 expect(body.mode).toBe("async");
202 expect(body.count).toBeGreaterThan(0);
203 expect(body.names.some((name) => name.includes("llama-3.3"))).toBe(true);
204 }).pipe(logLevel),
205 {
206 tags: ["provider:cloudflare", "provider:cloudflare:ai", "provider:cloudflare:worker", "live"],
207 timeout: 180_000,
208 },
209 );
210
211 test(
212 "deployed worker streams text via AI-binding-backed LanguageModel",
213 Effect.gen(function* () {
214 const out = yield* stack;
215 const client = HttpClient.filterStatusOk(yield* HttpClient.HttpClient);
216
217 // A cold or transient call can answer 200 with an empty SSE (no tokens
218 // streamed), which `filterStatusOk` does not catch. Fold the parse into
219 // the retried effect so the same backoff rides out the blip.
220 const parts = yield* client
221 .get(`${out.url}/stream?prompt=${encodeURIComponent("Say pong.")}`)
222 .pipe(
223 Effect.flatMap((res) => res.text),
224 Effect.map(parseSse),
225 Effect.flatMap((parts) =>
226 parts.some((p) => p.type === "text-delta") && parts.some((p) => p.type === "finish")
227 ? Effect.succeed(parts)
228 : Effect.fail(new Error("AI stream not ready: empty/unfinished")),
229 ),
230 Effect.retry({
231 schedule: Schedule.exponential("500 millis"),
232 times: 10,
233 }),
234 );
235
236 const text = parts
237 .filter((p) => p.type === "text-delta")
238 .map((p) => p.delta ?? "")
239 .join("");
240 const finish = parts.find((p) => p.type === "finish");
241
242 expect(parts.length).toBeGreaterThan(0);
243 expect(text.length).toBeGreaterThan(0);
244 expect(finish).toBeDefined();
245 }).pipe(logLevel),
246 {
247 tags: ["provider:cloudflare", "provider:cloudflare:ai", "provider:cloudflare:worker", "live"],
248 timeout: 180_000,
249 },
250 );