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

main
ts 82 lines 3.04 KB
Raw
1 import * as k2 from "@distilled.cloud/cloudflare/k2";
2 import * as Data from "effect/Data";
3 import * as Effect from "effect/Effect";
4 import * as Schedule from "effect/Schedule";
5
6 /** Test-side helpers shared by the K2 suites. */
7
8 class StreamStillExists extends Data.TaggedError("StreamStillExists")<{ streamId: string }> {}
9
10 class SubscriptionStillExists extends Data.TaggedError("SubscriptionStillExists")<{
11 subscriptionId: string;
12 }> {}
13
14 const gone = Schedule.max([Schedule.spaced("1 second"), Schedule.recurs(15)]);
15
16 /** Wait until `getStream` reports `K2StreamNotFound`. */
17 export const waitForStreamGone = (accountId: string, streamId: string) =>
18 k2.getStream({ accountId, streamId }).pipe(
19 Effect.flatMap(() => Effect.fail(new StreamStillExists({ streamId }))),
20 Effect.retry({ while: (e) => e._tag === "StreamStillExists", schedule: gone }),
21 Effect.catchTag("K2StreamNotFound", () => Effect.void),
22 );
23
24 /** Wait until `getSubscription` reports the subscription (or its stream) gone. */
25 export const waitForSubscriptionGone = (streamId: string, subscriptionId: string) =>
26 k2.getSubscription({ streamId, subscriptionId }).pipe(
27 Effect.flatMap(() => Effect.fail(new SubscriptionStillExists({ subscriptionId }))),
28 Effect.retry({ while: (e) => e._tag === "SubscriptionStillExists", schedule: gone }),
29 Effect.catchTag(["K2SubscriptionNotFound", "K2StreamNotFound"], () => Effect.void),
30 );
31
32 const decoder = new TextDecoder();
33
34 export interface VerifiedRecord {
35 text: string;
36 headers: Record<string, string>;
37 }
38
39 class NotEnoughRecords extends Data.TaggedError("NotEnoughRecords")<{
40 expected: number;
41 received: number;
42 }> {}
43
44 /**
45 * Out-of-band verification: drain a subscription with distilled `consume`
46 * + `ackBatch` until `expected` records were read (bounded to ~60 s).
47 */
48 export const drainSubscription = (
49 streamId: string,
50 subscriptionId: string,
51 expected: number,
52 workerId = "alchemy-k2-verifier",
53 ) =>
54 Effect.gen(function* () {
55 const received: VerifiedRecord[] = [];
56 const once = Effect.gen(function* () {
57 const batch = yield* k2.consume({ streamId, subscriptionId, workerId, maxRecords: 1000 });
58 if (batch.batchId) {
59 for (const record of batch.records) {
60 received.push({
61 text: decoder.decode(Uint8Array.from(atob(record.content), (c) => c.charCodeAt(0))),
62 headers: Object.fromEntries(
63 Object.entries(record.headers ?? {}).filter(
64 (entry): entry is [string, string] => entry[1] !== undefined,
65 ),
66 ),
67 });
68 }
69 yield* k2.ackBatch({ streamId, subscriptionId, batchId: batch.batchId, workerId });
70 }
71 if (received.length < expected) {
72 return yield* Effect.fail(new NotEnoughRecords({ expected, received: received.length }));
73 }
74 });
75 yield* once.pipe(
76 Effect.retry({
77 while: (e) => e._tag === "NotEnoughRecords" || e._tag === "K2BatchPending",
78 schedule: Schedule.max([Schedule.spaced("1 second"), Schedule.recurs(60)]),
79 }),
80 );
81 return received;
82 });