@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 64 lines 2.49 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import * as Effect from "effect/Effect";
3 import { HttpServerRequest } from "effect/http/HttpServerRequest";
4 import * as HttpServerResponse from "effect/http/HttpServerResponse";
5 import * as Stream from "effect/Stream";
6 import { DedicatedQueue } from "./dedicated-consumer-queue.ts";
7
8 /** Records the bodies the queue handler observed, so the test can poll them. */
9 export class Inbox extends Cloudflare.DurableObject<Inbox>()(
10 "DedicatedInbox",
11 Effect.gen(function* () {
12 return Effect.gen(function* () {
13 const state = yield* Cloudflare.DurableObjectState;
14 const bodies = (yield* state.storage.get<string[]>("bodies")) ?? [];
15 return {
16 record: Effect.fn(function* (body: string) {
17 bodies.push(body);
18 yield* state.storage.put("bodies", bodies);
19 }),
20 snapshot: () => Effect.succeed({ bodies }),
21 };
22 });
23 }),
24 ) {}
25
26 /**
27 * A Worker that ONLY consumes — it never binds the queue as a producer, so
28 * the queue's name reaches it exclusively through the
29 * `DedicatedQueue_queueName` env binding that `consumeQueueMessages` reads
30 * to scope its handler. This is the topology of #1243.
31 *
32 * `GET /binding` returns that binding RAW from `WorkerEnvironment` (not
33 * through the unpacking `ctx.get` accessor) so the test can pin the wire
34 * format Cloudflare actually stores.
35 */
36 export default class ConsumerWorker extends Cloudflare.Worker<ConsumerWorker>()(
37 "dedicated-consumer-worker",
38 { main: import.meta.url },
39 Effect.gen(function* () {
40 const inbox = yield* Inbox;
41 const queue = yield* DedicatedQueue;
42 const env = yield* Cloudflare.WorkerEnvironment;
43
44 yield* Cloudflare.Queues.consumeQueueMessages<{ text: string }>(queue, (stream) =>
45 Stream.runForEach(stream, (msg) => inbox.getByName("default").record(msg.body.text)),
46 );
47
48 return {
49 fetch: Effect.gen(function* () {
50 const request = yield* HttpServerRequest;
51 const url = new URL(request.url, "http://x");
52 if (url.pathname === "/binding") {
53 return yield* HttpServerResponse.json({
54 queueName: (env as Record<string, unknown>).DedicatedQueue_queueName,
55 });
56 }
57 if (url.pathname === "/received") {
58 return yield* HttpServerResponse.json(yield* inbox.getByName("default").snapshot());
59 }
60 return HttpServerResponse.text("Not Found", { status: 404 });
61 }),
62 };
63 }).pipe(Effect.provide(Cloudflare.Queues.EventSourceLive)),
64 ) {}