The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { NonEmptyReadonlyArray } from "effect/Array"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Layer from "effect/Layer"; |
| 4 | import * as Schedule from "effect/Schedule"; |
| 5 | import * as Sink from "effect/Sink"; |
| 6 | import { wireSize } from "./K2Codec.ts"; |
| 7 | import type { EncodedRecord } from "./K2Types.ts"; |
| 8 | import type { Stream } from "./Stream.ts"; |
| 9 | import { StreamSink, type StreamSinkClient } from "./StreamSink.ts"; |
| 10 | import { WriteStream, type WriteStreamClient } from "./WriteStream.ts"; |
| 11 | |
| 12 | /** |
| 13 | * Batch packing for {@link StreamSink}. Internal — not exported from the K2 |
| 14 | * barrel. |
| 15 | */ |
| 16 | |
| 17 | /** K2's limit on one produce request (1 MB = 1,000,000 bytes). */ |
| 18 | export const MAX_REQUEST_BYTES = 5_000_000; |
| 19 | |
| 20 | /** |
| 21 | * Greedily pack records into requests of at most {@link MAX_REQUEST_BYTES} |
| 22 | * (wire size, base64 included), preserving order. A record larger than the |
| 23 | * limit ships alone so K2's rejection surfaces instead of the record being |
| 24 | * dropped. |
| 25 | */ |
| 26 | export const packStreamSinkBatches = ( |
| 27 | records: ReadonlyArray<EncodedRecord>, |
| 28 | maxBytes: number = MAX_REQUEST_BYTES, |
| 29 | ): ReadonlyArray<ReadonlyArray<EncodedRecord>> => { |
| 30 | const batches: EncodedRecord[][] = []; |
| 31 | let current: EncodedRecord[] = []; |
| 32 | let currentBytes = 0; |
| 33 | for (const record of records) { |
| 34 | const size = wireSize(record); |
| 35 | if (current.length > 0 && currentBytes + size > maxBytes) { |
| 36 | batches.push(current); |
| 37 | current = []; |
| 38 | currentBytes = 0; |
| 39 | } |
| 40 | current.push(record); |
| 41 | currentBytes += size; |
| 42 | } |
| 43 | if (current.length > 0) { |
| 44 | batches.push(current); |
| 45 | } |
| 46 | return batches; |
| 47 | }; |
| 48 | |
| 49 | /** Bounded retry for a batch K2 reported as not stored (`K2Unavailable`). */ |
| 50 | const unavailableRetry = Schedule.max([Schedule.exponential("200 millis"), Schedule.recurs(5)]); |
| 51 | |
| 52 | /** Build the sink over a `WriteStream` client: one `sendEncoded` per packed batch. */ |
| 53 | export const makeStreamSink = <A>(client: WriteStreamClient<A>): StreamSinkClient<A> => |
| 54 | Sink.forEachArray((chunk: NonEmptyReadonlyArray<A>) => |
| 55 | client.encode(chunk).pipe( |
| 56 | Effect.flatMap((encoded) => |
| 57 | Effect.forEach( |
| 58 | packStreamSinkBatches(encoded), |
| 59 | (batch) => |
| 60 | client.sendEncoded(batch).pipe( |
| 61 | Effect.retry({ |
| 62 | while: (e) => e._tag === "K2Unavailable", |
| 63 | schedule: unavailableRetry, |
| 64 | }), |
| 65 | ), |
| 66 | { discard: true }, |
| 67 | ), |
| 68 | ), |
| 69 | ), |
| 70 | ); |
| 71 | |
| 72 | /** |
| 73 | * {@link StreamSink} over whichever {@link WriteStream} implementation is |
| 74 | * provided. Composed with each `WriteStream` layer in `StreamSinkBinding`, |
| 75 | * `StreamSinkHttp` and `StreamSinkLocal`. |
| 76 | */ |
| 77 | export const StreamSinkFromWriteStream = Layer.effect( |
| 78 | StreamSink, |
| 79 | Effect.gen(function* () { |
| 80 | const writeStream = yield* WriteStream; |
| 81 | return Effect.fn(function* (stream: Stream<any>) { |
| 82 | const client = yield* writeStream(stream); |
| 83 | return makeStreamSink(client); |
| 84 | }); |
| 85 | }), |
| 86 | ); |