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

main
ts 100 lines 3.28 KB
Raw
1 import type { NonEmptyReadonlyArray } from "effect/Array";
2 import * as Effect from "effect/Effect";
3 import * as Layer from "effect/Layer";
4 import * as Sink from "effect/Sink";
5 import type { Queue } from "./Queue.ts";
6 import { QueueSink, type QueueSinkClient } from "./QueueSink.ts";
7 import { WriteQueue, type WriteQueueClient } from "./WriteQueue.ts";
8
9 /** Cloudflare's `sendBatch` limit on messages per call. */
10 export const MAX_BATCH_MESSAGES = 100;
11
12 /** Cloudflare's `sendBatch` limit on total batch size (1 KB = 1000 bytes). */
13 export const MAX_BATCH_BYTES = 256_000;
14
15 /** Internal metadata Cloudflare counts against the size limits per message. */
16 export const MESSAGE_OVERHEAD_BYTES = 100;
17
18 const encoder = new TextEncoder();
19
20 const utf8ByteLength = (value: string): number => {
21 // Node-compatible Workers expose Buffer globally. Do not import node:buffer:
22 // older Workers and no_nodejs_compat deployments still need the web fallback.
23 const buffer = globalThis.Buffer;
24 return typeof buffer?.byteLength === "function"
25 ? buffer.byteLength(value, "utf8")
26 : encoder.encode(value).length;
27 };
28
29 /**
30 * Approximate wire size of one message: its UTF-8 JSON encoding plus the
31 * per-message metadata overhead. Values JSON cannot encode count as the
32 * overhead alone; `sendBatch` rejects them and the error surfaces through
33 * the sink.
34 */
35 export const messageSize = (body: unknown): number => {
36 let json: string | undefined;
37 try {
38 json = JSON.stringify(body);
39 } catch {
40 json = undefined;
41 }
42 return (json === undefined ? 0 : utf8ByteLength(json)) + MESSAGE_OVERHEAD_BYTES;
43 };
44
45 /**
46 * Greedily pack bodies into batches of at most {@link MAX_BATCH_MESSAGES}
47 * messages and {@link MAX_BATCH_BYTES} bytes, preserving order. A single
48 * message larger than the byte limit ships alone so Cloudflare's rejection
49 * surfaces instead of the message being dropped.
50 */
51 export const packQueueSinkBatches = <A>(
52 bodies: ReadonlyArray<A>,
53 ): ReadonlyArray<ReadonlyArray<A>> => {
54 const batches: A[][] = [];
55 let current: A[] = [];
56 let currentBytes = 0;
57 for (const body of bodies) {
58 const size = messageSize(body);
59 if (
60 current.length >= MAX_BATCH_MESSAGES ||
61 (current.length > 0 && currentBytes + size > MAX_BATCH_BYTES)
62 ) {
63 batches.push(current);
64 current = [];
65 currentBytes = 0;
66 }
67 current.push(body);
68 currentBytes += size;
69 }
70 if (current.length > 0) {
71 batches.push(current);
72 }
73 return batches;
74 };
75
76 /** Build the sink over a `WriteQueue` client: one `sendBatch` per packed batch. */
77 export const makeQueueSink = (client: WriteQueueClient): QueueSinkClient =>
78 Sink.forEachArray((chunk: NonEmptyReadonlyArray<unknown>) =>
79 Effect.forEach(
80 packQueueSinkBatches(chunk),
81 (batch) => client.sendBatch(batch.map((body) => ({ body }))),
82 { discard: true },
83 ),
84 );
85
86 /**
87 * {@link QueueSink} over whichever {@link WriteQueue} implementation is
88 * provided. Composed with each `WriteQueue` layer in `QueueSinkBinding`,
89 * `QueueSinkHttp` and `QueueSinkLocal`.
90 */
91 export const QueueSinkFromWriteQueue = Layer.effect(
92 QueueSink,
93 Effect.gen(function* () {
94 const writeQueue = yield* WriteQueue;
95 return Effect.fn(function* (queue: Queue) {
96 const client = yield* writeQueue(queue);
97 return makeQueueSink(client);
98 });
99 }),
100 );