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

main
ts 66 lines 2.43 KB
Raw
1 import * as Binding from "alchemy/Binding";
2 import type { RuntimeContext } from "alchemy/RuntimeContext";
3 import type * as Effect from "effect/Effect";
4 import type * as Sink from "effect/Sink";
5 import type { ProduceError } from "./K2Types.ts";
6 import type { Stream, StreamRecord } from "./Stream.ts";
7
8 /**
9 * Binding service that exposes a K2 {@link Stream} as an Effect `Sink`: run
10 * a `Stream` of records into it and every element is appended.
11 *
12 * Each upstream chunk is packed, in order, into `send` calls of at most
13 * 5 MB; a record over the 1 MB limit ships alone so K2's rejection
14 * surfaces. A batch that failed with `K2Unavailable` (not stored) is
15 * retried a bounded number of times; `K2AppendOutcomeUnknown` is never
16 * retried because the batch may have been stored.
17 *
18 * Provide {@link StreamSinkBinding} (native Worker binding),
19 * {@link StreamSinkHttp} (scoped `K2 Produce` token) or
20 * {@link StreamSinkLocal} (current credentials, for Actions).
21 * ### Draining a Stream into K2
22 * **Example:** Run a Stream into a K2 stream
23 * ```typescript
24 * export default Cloudflare.Worker(
25 * "Api",
26 * { main: import.meta.url },
27 * Effect.gen(function* () {
28 * const sink = yield* Cloudflare.K2.StreamSink(Orders);
29 * return {
30 * fetch: Effect.gen(function* () {
31 * yield* Stream.fromIterable(events).pipe(
32 * Stream.map((event) => ({ content: JSON.stringify(event) })),
33 * Stream.run(sink),
34 * );
35 * return HttpServerResponse.empty({ status: 202 });
36 * }),
37 * };
38 * }).pipe(Effect.provide(Cloudflare.K2.StreamSinkBinding)),
39 * );
40 * ```
41 *
42 * **Example:** Typed sink with a Schema
43 * ```typescript
44 * const sink = yield* Cloudflare.K2.StreamSink(Orders, { schema: Order });
45 * yield* Stream.fromIterable(orders).pipe(Stream.run(sink));
46 * ```
47 *
48 * @binding
49 * @product K2
50 * @category Storage & Databases
51 */
52 export interface StreamSink extends Binding.Service<
53 StreamSink,
54 "Cloudflare.K2.StreamSink",
55 (stream: Stream<any>) => Effect.Effect<StreamSinkClient<any>>
56 > {
57 <A>(stream: Stream<A>): Effect.Effect<StreamSinkClient<StreamRecord<A>>, never, StreamSink>;
58 }
59
60 export const StreamSink = Binding.Service<StreamSink>("Cloudflare.K2.StreamSink");
61
62 /**
63 * A `Sink` that appends every element it receives as one K2 record.
64 * Runtime-only: it can only run inside the deployed host (or an Action).
65 */
66 export type StreamSinkClient<A> = Sink.Sink<void, A, never, ProduceError, RuntimeContext>;