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, 04:45 UTC
| 1 | import type { RuntimeContext } from "alchemy/RuntimeContext"; |
| 2 | import type { Pipeline, PipelineRecord } from "cloudflare:pipelines"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as Schema from "effect/Schema"; |
| 5 | import { isLegacyPipeline, type LegacyPipeline } from "./LegacyPipeline.ts"; |
| 6 | import type { Stream } from "./Stream.ts"; |
| 7 | import { StreamSendError, type WriteStreamClient } from "./WriteStream.ts"; |
| 8 | |
| 9 | /** |
| 10 | * Shared record encoding + client assembly for the WriteStream / StreamSink |
| 11 | * implementations. Internal — not exported from the Pipelines barrel. |
| 12 | */ |
| 13 | |
| 14 | /** |
| 15 | * The Effect Schema a stream was declared with, if any. Legacy pipelines |
| 16 | * have none. |
| 17 | */ |
| 18 | export const recordSchemaOf = (stream: Stream<any> | LegacyPipeline): Schema.Top | undefined => { |
| 19 | if (isLegacyPipeline(stream)) return undefined; |
| 20 | // References (`Stream.ref`) proxy unknown properties to Outputs — only a |
| 21 | // real Effect Schema counts. |
| 22 | const schema: unknown = stream.RecordSchema; |
| 23 | return Schema.isSchema(schema) ? schema : undefined; |
| 24 | }; |
| 25 | |
| 26 | /** |
| 27 | * Build the batch encoder: `Schema.toCodecJson(schema)` per record when |
| 28 | * the stream has an Effect Schema, identity otherwise. |
| 29 | */ |
| 30 | export const makeRecordEncoder = ( |
| 31 | schema: Schema.Top | undefined, |
| 32 | ): (( |
| 33 | records: ReadonlyArray<unknown>, |
| 34 | ) => Effect.Effect<ReadonlyArray<PipelineRecord>, StreamSendError>) => { |
| 35 | if (schema === undefined) { |
| 36 | return (records) => Effect.succeed(records as ReadonlyArray<PipelineRecord>); |
| 37 | } |
| 38 | const encode = Schema.encodeUnknownEffect( |
| 39 | Schema.toCodecJson(schema as unknown as Schema.Codec<unknown, unknown, never, never>), |
| 40 | ); |
| 41 | return (records) => |
| 42 | Effect.forEach(records, (record, index) => |
| 43 | encode(record).pipe( |
| 44 | Effect.map((json) => json as PipelineRecord), |
| 45 | Effect.mapError( |
| 46 | (issue) => |
| 47 | new StreamSendError({ |
| 48 | message: `record ${index} does not match the stream schema: ${issue.message}`, |
| 49 | cause: issue, |
| 50 | reason: "InvalidRecord", |
| 51 | index, |
| 52 | }), |
| 53 | ), |
| 54 | ), |
| 55 | ); |
| 56 | }; |
| 57 | |
| 58 | /** Wrap any transport failure as a `SendFailed` {@link StreamSendError}. */ |
| 59 | export const toStreamSendError = (error: unknown): StreamSendError => |
| 60 | new StreamSendError({ |
| 61 | message: |
| 62 | typeof error === "object" && error !== null && "message" in error |
| 63 | ? String((error as { message: unknown }).message) |
| 64 | : "Unknown pipeline error", |
| 65 | cause: error, |
| 66 | reason: "SendFailed", |
| 67 | }); |
| 68 | |
| 69 | /** Assemble a {@link WriteStreamClient} from an encoder and a transport. */ |
| 70 | export const makeStreamClient = (options: { |
| 71 | schema: Schema.Top | undefined; |
| 72 | raw: Effect.Effect<Pipeline, never, RuntimeContext>; |
| 73 | sendEncoded: ( |
| 74 | records: ReadonlyArray<PipelineRecord>, |
| 75 | ) => Effect.Effect<void, StreamSendError, RuntimeContext>; |
| 76 | }): WriteStreamClient<any> => { |
| 77 | const encode = makeRecordEncoder(options.schema); |
| 78 | return { |
| 79 | raw: options.raw, |
| 80 | encode, |
| 81 | sendEncoded: options.sendEncoded, |
| 82 | send: (records) => encode(records).pipe(Effect.flatMap(options.sendEncoded)), |
| 83 | }; |
| 84 | }; |
| 85 | |
| 86 | const encoder = new TextEncoder(); |
| 87 | |
| 88 | /** |
| 89 | * Approximate wire size of one encoded record in the JSON array body: |
| 90 | * its UTF-8 JSON plus the separating comma. Values JSON cannot encode |
| 91 | * count as 1 byte; the send rejects them and the error surfaces. |
| 92 | */ |
| 93 | export const recordSize = (record: PipelineRecord): number => { |
| 94 | let json: string | undefined; |
| 95 | try { |
| 96 | json = JSON.stringify(record); |
| 97 | } catch { |
| 98 | json = undefined; |
| 99 | } |
| 100 | return (json === undefined ? 0 : encoder.encode(json).length) + 1; |
| 101 | }; |