@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, 07:15 UTC

main
ts 49 lines 1.47 KB
Raw
1 /**
2 * Tail consumer for the Website dev tail tests: records every `tail()` batch
3 * and every `tailStream()` session into KV and serves them on `/events`.
4 */
5
6 interface EventsKV {
7 put(key: string, value: string): Promise<void>;
8 get(key: string): Promise<string | null>;
9 list(options?: { prefix?: string }): Promise<{ keys: { name: string }[] }>;
10 }
11
12 interface Env {
13 EVENTS: EventsKV;
14 }
15
16 interface StreamEvent {
17 invocationId?: string;
18 event?: { type?: string };
19 }
20
21 const read = async (env: Env, prefix: string) => {
22 const list = await env.EVENTS.list({ prefix });
23 const values = await Promise.all(list.keys.map((key) => env.EVENTS.get(key.name)));
24 return values.filter((value): value is string => value !== null);
25 };
26
27 export default {
28 async fetch(request: Request, env: Env): Promise<Response> {
29 if (new URL(request.url).pathname === "/events") {
30 return Response.json({
31 tail: await read(env, "tail:"),
32 stream: await read(env, "stream:"),
33 });
34 }
35 return new Response("tail-consumer-ok");
36 },
37 async tail(events: unknown[], env: Env): Promise<void> {
38 await env.EVENTS.put(`tail:${crypto.randomUUID()}`, JSON.stringify(events));
39 },
40 tailStream(onset: StreamEvent, env: Env) {
41 const events: StreamEvent[] = [onset];
42 return async (event: StreamEvent) => {
43 events.push(event);
44 if (event.event?.type === "outcome") {
45 await env.EVENTS.put(`stream:${crypto.randomUUID()}`, JSON.stringify(events));
46 }
47 };
48 },
49 };