The best way to control Claude Code, Codex and more from any computer or phone

main
ts 40 lines 1.58 KB
Raw
1 /**
2 * Tail consumer for {@link AsyncWorker}. Listed in the producer's
3 * `tailConsumers`, so every invocation of the producer delivers a trace
4 * batch to `tail()`. Batches are recorded into a KV namespace and exposed
5 * over `GET /events` so the integ test can assert the producer's
6 * `console.log` marker arrived end-to-end.
7 *
8 * NOTE: the default export must be this module's ONLY export — extra named
9 * exports become workerd top-level exports and fail startup validation.
10 */
11 interface TailEventsKV {
12 put(key: string, value: string): Promise<void>;
13 get(key: string): Promise<string | null>;
14 list(options?: { prefix?: string }): Promise<{ keys: { name: string }[] }>;
15 }
16
17 interface TraceItemLite {
18 scriptName?: string | null;
19 outcome?: string;
20 logs?: { message?: unknown[]; level?: string; timestamp?: number }[];
21 }
22
23 export default {
24 async fetch(request: Request, env: { EVENTS: TailEventsKV }): Promise<Response> {
25 const url = new URL(request.url);
26 if (url.pathname === "/events") {
27 const list = await env.EVENTS.list({ prefix: "evt:" });
28 const batches = await Promise.all(list.keys.map((key) => env.EVENTS.get(key.name)));
29 return Response.json({
30 keys: list.keys.map((key) => key.name),
31 batches: batches.filter((batch): batch is string => batch !== null),
32 });
33 }
34 return new Response("tail-consumer-ok");
35 },
36 async tail(events: TraceItemLite[], env: { EVENTS: TailEventsKV }): Promise<void> {
37 const producer = events[0]?.scriptName ?? "unknown";
38 await env.EVENTS.put(`evt:${producer}:${Date.now()}`, JSON.stringify(events));
39 },
40 };