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
| 1 | import * as Binding from "alchemy/Binding"; |
| 2 | import type { RuntimeContext } from "alchemy/RuntimeContext"; |
| 3 | import type * as Effect from "effect/Effect"; |
| 4 | import type * as Sink from "effect/Sink"; |
| 5 | import type { Queue } from "./Queue.ts"; |
| 6 | import type { SendError } from "./QueueTypes.ts"; |
| 7 | |
| 8 | /** |
| 9 | * Binding service that exposes a {@link Queue} as an Effect `Sink`: run a |
| 10 | * `Stream` of message bodies into it and every element is enqueued. |
| 11 | * |
| 12 | * Each stream chunk becomes one `sendBatch` call. Chunks larger than a |
| 13 | * Cloudflare batch (100 messages / 256 KB) are split into consecutive |
| 14 | * batches, preserving order. Bodies are any JSON-serializable value, the |
| 15 | * same as {@link WriteQueueClient.send}. |
| 16 | * |
| 17 | * Provide {@link QueueSinkBinding} (native Worker binding), |
| 18 | * {@link QueueSinkHttp} (scoped HTTP token) or {@link QueueSinkLocal} |
| 19 | * (current credentials, for Actions). Each one is the sink layered over |
| 20 | * the matching `WriteQueue` implementation and wires the queue into the |
| 21 | * host exactly as that layer does. |
| 22 | * ### Draining a Stream into a Queue |
| 23 | * **Example:** Run a Stream into a Queue |
| 24 | * ```typescript |
| 25 | * export default Cloudflare.Worker( |
| 26 | * "Api", |
| 27 | * { main: import.meta.url }, |
| 28 | * Effect.gen(function* () { |
| 29 | * const sink = yield* Cloudflare.Queues.QueueSink(Clicks); |
| 30 | * |
| 31 | * return { |
| 32 | * fetch: Effect.gen(function* () { |
| 33 | * yield* Stream.fromIterable(events).pipe(Stream.run(sink)); |
| 34 | * return HttpServerResponse.empty({ status: 202 }); |
| 35 | * }), |
| 36 | * }; |
| 37 | * }).pipe(Effect.provide(Cloudflare.Queues.QueueSinkBinding)), |
| 38 | * ); |
| 39 | * ``` |
| 40 | * |
| 41 | * **Example:** Controlling batch size |
| 42 | * ```typescript |
| 43 | * // one sendBatch call per 25 messages |
| 44 | * yield* Stream.fromIterable(events).pipe( |
| 45 | * Stream.rechunk(25), |
| 46 | * Stream.run(sink), |
| 47 | * ); |
| 48 | * ``` |
| 49 | * |
| 50 | * ### Source → Transform → Sink |
| 51 | * **Example:** Forward a consumed Queue into another Queue |
| 52 | * ```typescript |
| 53 | * Effect.gen(function* () { |
| 54 | * const enriched = yield* Cloudflare.Queues.QueueSink(EnrichedClicks); |
| 55 | * |
| 56 | * yield* Cloudflare.Queues.consumeQueueMessages<Click>(Clicks, (messages) => |
| 57 | * messages.pipe( |
| 58 | * Stream.map((message) => message.body), |
| 59 | * Stream.filter((click) => click.button === "buy"), |
| 60 | * Stream.map((click) => ({ ...click, receivedAt: Date.now() })), |
| 61 | * Stream.run(enriched), |
| 62 | * ), |
| 63 | * ); |
| 64 | * }).pipe( |
| 65 | * Effect.provide( |
| 66 | * Layer.mergeAll( |
| 67 | * Cloudflare.Queues.EventSourceLive, |
| 68 | * Cloudflare.Queues.QueueSinkBinding, |
| 69 | * ), |
| 70 | * ), |
| 71 | * ); |
| 72 | * ``` |
| 73 | * |
| 74 | * @binding |
| 75 | * @product Queues |
| 76 | * @category Storage & Databases |
| 77 | */ |
| 78 | export interface QueueSink extends Binding.Service< |
| 79 | QueueSink, |
| 80 | "Cloudflare.Queues.QueueSink", |
| 81 | (queue: Queue) => Effect.Effect<QueueSinkClient> |
| 82 | > {} |
| 83 | |
| 84 | export const QueueSink = Binding.Service<QueueSink>("Cloudflare.Queues.QueueSink"); |
| 85 | |
| 86 | /** |
| 87 | * A `Sink` that enqueues every element it receives as one message body. |
| 88 | * Runtime-only: it can only run inside the deployed Worker (or Action). |
| 89 | */ |
| 90 | export type QueueSinkClient = Sink.Sink<void, unknown, never, SendError, RuntimeContext>; |