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

main
ts 49 lines 1.8 KB
Raw
1 import * as Effect from "effect/Effect";
2 import * as Layer from "effect/Layer";
3 import { makeK2LocalAuth } from "./K2Http.ts";
4 import { ReadSubscription } from "./ReadSubscription.ts";
5 import { makeReadSubscriptionClient } from "./ReadSubscriptionHttp.ts";
6 import type { Subscription } from "./Subscription.ts";
7
8 /**
9 * Local implementation of the {@link ReadSubscription} service — leases
10 * batches over the K2 subscription HTTP API with the **current
11 * credentials** instead of a scoped token. Provide it on an Action (or any
12 * deploy-time Effect), or on a polling event source run in-process.
13 *
14 * The subscription's ids are resolved at apply time, so the subscription
15 * can be created in the same deploy.
16 * ### Providing the Layer
17 * **Example:** Drain a subscription from an Action
18 * ```typescript
19 * const Drain = Alchemy.Action(
20 * "Drain",
21 * Effect.gen(function* () {
22 * const inbox = yield* Cloudflare.K2.ReadSubscription(Analytics);
23 * return Effect.fn(function* () {
24 * const batch = yield* inbox.consume({ workerId: "drain" });
25 * if (Option.isSome(batch)) yield* inbox.ack(batch.value);
26 * });
27 * }).pipe(Effect.provide(Cloudflare.K2.ReadSubscriptionLocal)),
28 * );
29 * ```
30 *
31 * @layer
32 * @provides Cloudflare.K2.ReadSubscription
33 * @product K2
34 * @category Storage & Databases
35 */
36 export const ReadSubscriptionLocal = Layer.effect(
37 ReadSubscription,
38 Effect.gen(function* () {
39 const auth = yield* makeK2LocalAuth;
40 return Effect.fn(function* (subscription: Subscription) {
41 // Deferred accessors — resolved at apply time. No `host.bind`: the
42 // local variant registers no binding.
43 return makeReadSubscriptionClient(auth, {
44 streamId: yield* subscription.streamId,
45 subscriptionId: yield* subscription.subscriptionId,
46 });
47 });
48 }),
49 );