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

main
ts 26 lines 1.03 KB
Raw
1 import type { RuntimeContext } from "alchemy/RuntimeContext";
2 import * as Effect from "effect/Effect";
3 import { encodeRecords, encodeWithSchema } from "./K2Codec.ts";
4 import type { EncodedRecord, ProduceError, Record as K2Record, RecordSchema } from "./K2Types.ts";
5 import type { WriteStreamClient } from "./WriteStream.ts";
6
7 /**
8 * Build a {@link WriteStreamClient} over a transport that appends encoded
9 * records. Shared by the binding, HTTP, and local implementations — they
10 * differ only in `sendEncoded`. Internal — not exported from the K2 barrel.
11 */
12 export const makeWriteStreamClient = (
13 schema: RecordSchema<any> | undefined,
14 sendEncoded: (
15 records: ReadonlyArray<EncodedRecord>,
16 ) => Effect.Effect<void, ProduceError, RuntimeContext>,
17 ): WriteStreamClient<any> => {
18 const encode = schema
19 ? encodeWithSchema(schema)
20 : (records: ReadonlyArray<K2Record>) => Effect.sync(() => encodeRecords(records));
21 return {
22 encode,
23 sendEncoded,
24 send: (records) => encode(records).pipe(Effect.flatMap(sendEncoded)),
25 };
26 };