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

main
ts 99 lines 3.34 KB
Raw
1 import * as k2 from "@distilled.cloud/cloudflare/k2";
2 import * as Effect from "effect/Effect";
3 import * as Layer from "effect/Layer";
4 import * as Option from "effect/Option";
5 import { fromConsumedRecords } from "./K2Codec.ts";
6 import { type K2Auth, makeK2HttpAuth } from "./K2Http.ts";
7 import type { Batch } from "./K2Types.ts";
8 import { ReadSubscription, type ReadSubscriptionClient } from "./ReadSubscription.ts";
9 import type { Subscription } from "./Subscription.ts";
10
11 /** Default `maxRecords` per `consume`. */
12 export const DEFAULT_MAX_RECORDS = 100;
13
14 /**
15 * HTTP-backed implementation of the {@link ReadSubscription} service. Mints
16 * a scoped `K2 Consume` {@link AccountApiToken}, binds it into the host, and
17 * leases batches through the stream's data-plane API
18 * (`https://<id>.k2.cloudflarestorage.com/subscriptions/...`).
19 * ### Providing the Layer
20 * **Example:** Pull from a container
21 * ```typescript
22 * Effect.gen(function* () {
23 * const inbox = yield* Cloudflare.K2.ReadSubscription(Analytics);
24 * // ...
25 * }).pipe(Effect.provide(Cloudflare.K2.ReadSubscriptionHttp));
26 * ```
27 *
28 * @layer
29 * @provides Cloudflare.K2.ReadSubscription
30 * @product K2
31 * @category Storage & Databases
32 */
33 export const ReadSubscriptionHttp = Layer.effect(
34 ReadSubscription,
35 Effect.gen(function* () {
36 const auth = yield* makeK2HttpAuth;
37 return Effect.fn(function* (subscription: Subscription) {
38 const subscriptionAuth = yield* auth(subscription, "K2 Consume");
39 return makeReadSubscriptionClient(subscriptionAuth, {
40 streamId: yield* subscription.streamId,
41 subscriptionId: yield* subscription.subscriptionId,
42 });
43 });
44 }),
45 );
46
47 /**
48 * Build the pull client over the K2 subscription HTTP API. Shared by
49 * {@link ReadSubscriptionHttp} and `ReadSubscriptionLocal`.
50 */
51 export const makeReadSubscriptionClient = (
52 auth: K2Auth,
53 ids: {
54 streamId: Effect.Effect<string>;
55 subscriptionId: Effect.Effect<string>;
56 },
57 ): ReadSubscriptionClient => {
58 const target = Effect.all({ streamId: ids.streamId, subscriptionId: ids.subscriptionId });
59 const onBatch = (batch: Batch) =>
60 target.pipe(Effect.map((t) => ({ ...t, batchId: batch.batchId, workerId: batch.workerId })));
61 return {
62 consume: ({ workerId, maxRecords }) =>
63 target.pipe(
64 Effect.flatMap((t) =>
65 auth.authorize(
66 k2.consume({ ...t, workerId, maxRecords: maxRecords ?? DEFAULT_MAX_RECORDS }),
67 ),
68 ),
69 Effect.map((response) =>
70 response.batchId
71 ? Option.some<Batch>({
72 batchId: response.batchId,
73 workerId,
74 leasedUntil: new Date(response.leasedUntilMs ?? 0),
75 records: fromConsumedRecords(response.records),
76 })
77 : Option.none(),
78 ),
79 ),
80 ack: (batch) =>
81 onBatch(batch).pipe(
82 Effect.flatMap((request) => auth.authorize(k2.ackBatch(request))),
83 Effect.asVoid,
84 ),
85 nack: (batch) =>
86 onBatch(batch).pipe(
87 Effect.flatMap((request) => auth.authorize(k2.nackBatch(request))),
88 Effect.asVoid,
89 ),
90 extend: (batch) =>
91 onBatch(batch).pipe(
92 Effect.flatMap((request) => auth.authorize(k2.extendLease(request))),
93 Effect.map((response): Batch => ({
94 ...batch,
95 leasedUntil: new Date(response.leasedUntilMs),
96 })),
97 ),
98 };
99 };