The best way to control Claude Code, Codex and more from any computer or phone
| 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 Layer from "effect/Layer"; |
| 6 | import * as Stream from "effect/Stream"; |
| 7 | import { |
| 8 | type Click, |
| 9 | type EnrichedClick, |
| 10 | produceRoute, |
| 11 | ResultQueue, |
| 12 | SourceQueue, |
| 13 | } from "./queue-sink-shared.ts"; |
| 14 | |
| 15 | export interface RecorderSnapshot { |
| 16 | /** Distinct indices observed (queues are at-least-once). */ |
| 17 | distinct: number; |
| 18 | /** Every recorded message carried the transformed `doubled` field. */ |
| 19 | transformed: boolean; |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Records the indices that reach the result queue, per run. Persisted in DO |
| 24 | * storage so the snapshot survives hibernation. |
| 25 | */ |
| 26 | export class QueueSinkRecorder extends Cloudflare.DurableObject<QueueSinkRecorder>()( |
| 27 | "QueueSinkRecorder", |
| 28 | Effect.gen(function* () { |
| 29 | return Effect.gen(function* () { |
| 30 | const state = yield* Cloudflare.DurableObjectState; |
| 31 | const seen = new Set((yield* state.storage.get<number[]>("seen")) ?? []); |
| 32 | let transformed = (yield* state.storage.get<boolean>("transformed")) ?? true; |
| 33 | return { |
| 34 | record: Effect.fn(function* (clicks: EnrichedClick[]) { |
| 35 | for (const click of clicks) { |
| 36 | seen.add(click.index); |
| 37 | transformed &&= click.doubled === click.index * 2; |
| 38 | } |
| 39 | yield* state.storage.put("seen", [...seen]); |
| 40 | yield* state.storage.put("transformed", transformed); |
| 41 | }), |
| 42 | snapshot: () => |
| 43 | Effect.succeed({ |
| 44 | distinct: seen.size, |
| 45 | transformed, |
| 46 | } satisfies RecorderSnapshot), |
| 47 | }; |
| 48 | }); |
| 49 | }), |
| 50 | ) {} |
| 51 | |
| 52 | /** |
| 53 | * For up to ~12 seconds after a fresh deploy, Cloudflare can still run the |
| 54 | * precreate placeholder version: queue batches fail with "Handler does not |
| 55 | * export a queue() function", and a newly started recorder DO rejects |
| 56 | * `record` ("The RPC receiver does not implement the method"). With the |
| 57 | * default `retryDelay` of 0 the redeliveries are immediate, so a batch already |
| 58 | * waiting (the Action's seed, the first `/produce`) can exhaust `maxRetries` |
| 59 | * and be dropped before the real version takes over. Spacing the retries |
| 60 | * gives each batch a ~30 second budget. |
| 61 | */ |
| 62 | const consumerSettings = { |
| 63 | batchSize: 100, |
| 64 | maxWaitTime: "1 second", |
| 65 | maxRetries: 10, |
| 66 | retryDelay: "3 seconds", |
| 67 | } as const; |
| 68 | |
| 69 | const groupByRun = (clicks: ReadonlyArray<EnrichedClick>) => { |
| 70 | const runs = new Map<string, EnrichedClick[]>(); |
| 71 | for (const click of clicks) { |
| 72 | const group = runs.get(click.run) ?? []; |
| 73 | group.push(click); |
| 74 | runs.set(click.run, group); |
| 75 | } |
| 76 | return [...runs]; |
| 77 | }; |
| 78 | |
| 79 | /** |
| 80 | * Source → transform → sink in one Worker: |
| 81 | * |
| 82 | * - `POST /produce?run=K&count=N[&padding=B]` drains N clicks into the |
| 83 | * source queue through `QueueSink` (a single stream chunk, so the sink |
| 84 | * must split it into 100-message / 256 KB batches). |
| 85 | * - The source consumer maps each click to an `EnrichedClick` and drains |
| 86 | * the batch into the result queue through a second `QueueSink`. |
| 87 | * - The result consumer records the indices in the run's DO. |
| 88 | * - `GET /count?run=K` reads the DO snapshot. |
| 89 | */ |
| 90 | export default class QueueSinkWorker extends Cloudflare.Worker<QueueSinkWorker>()( |
| 91 | "QueueSinkWorker", |
| 92 | { |
| 93 | main: import.meta.url, |
| 94 | }, |
| 95 | Effect.gen(function* () { |
| 96 | const recorders = yield* QueueSinkRecorder; |
| 97 | const source = yield* SourceQueue; |
| 98 | const results = yield* ResultQueue; |
| 99 | const clicks = yield* Cloudflare.Queues.QueueSink(source); |
| 100 | const enriched = yield* Cloudflare.Queues.QueueSink(results); |
| 101 | |
| 102 | yield* Cloudflare.Queues.consumeQueueMessages<Click>(source, consumerSettings, (messages) => |
| 103 | messages.pipe( |
| 104 | Stream.map((message): EnrichedClick => ({ |
| 105 | ...message.body, |
| 106 | doubled: message.body.index * 2, |
| 107 | })), |
| 108 | Stream.run(enriched), |
| 109 | ), |
| 110 | ); |
| 111 | |
| 112 | yield* Cloudflare.Queues.consumeQueueMessages<EnrichedClick>( |
| 113 | results, |
| 114 | consumerSettings, |
| 115 | (messages) => |
| 116 | messages.pipe( |
| 117 | Stream.map((message) => message.body), |
| 118 | Stream.runCollect, |
| 119 | Effect.flatMap((bodies) => |
| 120 | Effect.forEach( |
| 121 | groupByRun(bodies), |
| 122 | ([run, group]) => |
| 123 | recorders.getByName(run).record( |
| 124 | // Drop the padding before persisting. |
| 125 | group.map(({ padding: _, ...click }) => click), |
| 126 | ), |
| 127 | { discard: true }, |
| 128 | ), |
| 129 | ), |
| 130 | ), |
| 131 | ); |
| 132 | |
| 133 | return { |
| 134 | fetch: Effect.gen(function* () { |
| 135 | const request = yield* HttpServerRequest; |
| 136 | const url = new URL(request.url, "http://x"); |
| 137 | const run = url.searchParams.get("run") ?? "default"; |
| 138 | |
| 139 | if (request.method === "POST" && url.pathname === "/produce") { |
| 140 | return yield* produceRoute(clicks, url); |
| 141 | } |
| 142 | |
| 143 | if (request.method === "GET" && url.pathname === "/count") { |
| 144 | const snapshot = yield* recorders.getByName(run).snapshot(); |
| 145 | return yield* HttpServerResponse.json(snapshot); |
| 146 | } |
| 147 | |
| 148 | return HttpServerResponse.text("Not Found", { status: 404 }); |
| 149 | }), |
| 150 | }; |
| 151 | }).pipe( |
| 152 | Effect.provide( |
| 153 | Layer.mergeAll(Cloudflare.Queues.EventSourceLive, Cloudflare.Queues.QueueSinkBinding), |
| 154 | ), |
| 155 | ), |
| 156 | ) {} |