The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import * as Binding from "alchemy/Binding"; |
| 2 | import type { RuntimeContext } from "alchemy/RuntimeContext"; |
| 3 | import type { PipelineRecord } from "cloudflare:pipelines"; |
| 4 | import type * as Effect from "effect/Effect"; |
| 5 | import type * as Sink from "effect/Sink"; |
| 6 | import type { Stream, StreamRecord } from "./Stream.ts"; |
| 7 | import type { StreamSendError } from "./WriteStream.ts"; |
| 8 | |
| 9 | /** |
| 10 | * Binding service that exposes a Pipelines {@link Stream} as an Effect |
| 11 | * `Sink`: run a `Stream` of records into it and every element is |
| 12 | * ingested. Also exported as `Cloudflare.Basin.StreamSink`. |
| 13 | * |
| 14 | * Records are typed, validated and encoded by the stream's Effect Schema |
| 15 | * (plain JSON objects when it has none). Each upstream chunk is packed |
| 16 | * greedily into requests of at most 5 MB, preserving order. |
| 17 | * |
| 18 | * Provide `StreamSinkBinding` (native Worker binding), `StreamSinkHttp` |
| 19 | * (scoped `Pipelines Send` token) or `StreamSinkLocal` (current |
| 20 | * credentials, for Actions) — each is the sink layered over the matching |
| 21 | * `WriteStream` implementation. |
| 22 | * ### Draining a Stream into a Pipelines Stream |
| 23 | * **Example:** Run an Effect Stream into a Pipelines stream |
| 24 | * ```typescript |
| 25 | * export default Cloudflare.Worker( |
| 26 | * "Api", |
| 27 | * { main: import.meta.url }, |
| 28 | * Effect.gen(function* () { |
| 29 | * const sink = yield* Cloudflare.Basin.StreamSink(PageViews); |
| 30 | * return { |
| 31 | * fetch: Effect.gen(function* () { |
| 32 | * yield* EffectStream.fromIterable(views).pipe(EffectStream.run(sink)); |
| 33 | * return HttpServerResponse.empty({ status: 202 }); |
| 34 | * }), |
| 35 | * }; |
| 36 | * }).pipe(Effect.provide(Cloudflare.Basin.StreamSinkBinding)), |
| 37 | * ); |
| 38 | * ``` |
| 39 | * |
| 40 | * **Example:** Controlling request size |
| 41 | * ```typescript |
| 42 | * // at most 500 records per send |
| 43 | * yield* events.pipe(EffectStream.rechunk(500), EffectStream.run(sink)); |
| 44 | * ``` |
| 45 | * |
| 46 | * @binding |
| 47 | * @product Pipelines |
| 48 | * @category Storage & Databases |
| 49 | */ |
| 50 | export interface StreamSink extends Binding.Service< |
| 51 | StreamSink, |
| 52 | "Cloudflare.Pipelines.StreamSink", |
| 53 | (stream: Stream<any>) => Effect.Effect<StreamSinkClient<any>> |
| 54 | > { |
| 55 | <A, E = never, Req = never>( |
| 56 | stream: Stream<A> | Effect.Effect<Stream<A>, E, Req>, |
| 57 | ): Effect.Effect<StreamSinkClient<StreamRecord<A>>, E, StreamSink | Req>; |
| 58 | } |
| 59 | |
| 60 | export const StreamSink = Binding.Service<StreamSink>("Cloudflare.Pipelines.StreamSink"); |
| 61 | |
| 62 | /** |
| 63 | * A `Sink` that ingests every element it receives as one record. |
| 64 | * Runtime-only: it can only run inside the deployed Worker (or Action). |
| 65 | */ |
| 66 | export type StreamSinkClient<A = PipelineRecord> = Sink.Sink< |
| 67 | void, |
| 68 | A, |
| 69 | never, |
| 70 | StreamSendError, |
| 71 | RuntimeContext |
| 72 | >; |