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

main
ts 46 lines 1.65 KB
Raw
1 import * as Effect from "effect/Effect";
2 import * as Layer from "effect/Layer";
3 import { makeK2LocalAuth } from "./K2Http.ts";
4 import type { Stream } from "./Stream.ts";
5 import { WriteStream } from "./WriteStream.ts";
6 import { makeWriteStreamHttpClient } from "./WriteStreamHttp.ts";
7
8 /**
9 * Local implementation of the {@link WriteStream} service — appends records
10 * through the stream's HTTP input with the **current credentials** instead
11 * of a native binding or a scoped token. Provide it on an Action (or any
12 * deploy-time Effect) to seed a stream with the same client a Worker uses.
13 *
14 * The stream must have its HTTP input enabled (`http: true`). The stream id
15 * is resolved at apply time, so the stream can be created in the same
16 * deploy.
17 * ### Providing the Layer
18 * **Example:** Seed a stream from an Action
19 * ```typescript
20 * const Seed = Alchemy.Action(
21 * "Seed",
22 * Effect.gen(function* () {
23 * const orders = yield* Cloudflare.K2.WriteStream(Orders);
24 * return Effect.fn(function* () {
25 * yield* orders.send([{ content: "hello" }]);
26 * });
27 * }).pipe(Effect.provide(Cloudflare.K2.WriteStreamLocal)),
28 * );
29 * ```
30 *
31 * @layer
32 * @provides Cloudflare.K2.WriteStream
33 * @product K2
34 * @category Storage & Databases
35 */
36 export const WriteStreamLocal = Layer.effect(
37 WriteStream,
38 Effect.gen(function* () {
39 const auth = yield* makeK2LocalAuth;
40 return Effect.fn(function* (stream: Stream<any>) {
41 // Deferred accessor — resolves the id at apply time. No `host.bind`:
42 // the local variant registers no binding.
43 return makeWriteStreamHttpClient(auth, yield* stream.streamId, stream.RecordSchema);
44 });
45 }),
46 );