The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as pipelines from "@distilled.cloud/cloudflare/pipelines"; |
| 2 | import type { RuntimeContext } from "alchemy/RuntimeContext"; |
| 3 | import { Self } from "alchemy/Self"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import type * as HttpClient from "effect/http/HttpClient"; |
| 6 | import { AccountApiToken } from "../ApiToken/AccountApiToken.ts"; |
| 7 | import { CloudflareEnvironment } from "../CloudflareEnvironment.ts"; |
| 8 | import type { Credentials } from "../Credentials.ts"; |
| 9 | import { requireLiveResource } from "../HttpBindingDev.ts"; |
| 10 | import { authorizeWith } from "../HttpClientUtils.ts"; |
| 11 | import { isLegacyPipeline, type LegacyPipeline } from "./LegacyPipeline.ts"; |
| 12 | import type { Stream } from "./Stream.ts"; |
| 13 | import { makeStreamClient, recordSchemaOf, toStreamSendError } from "./StreamCodec.ts"; |
| 14 | import { StreamSendError, type WriteStreamClient } from "./WriteStream.ts"; |
| 15 | |
| 16 | /** |
| 17 | * Shared scaffolding for the HTTP-ingest Pipelines clients |
| 18 | * (`WriteStreamHttp`, `WriteStreamLocal` and the matching StreamSink |
| 19 | * layers). Internal — not exported from the Pipelines barrel. |
| 20 | */ |
| 21 | |
| 22 | /** |
| 23 | * Injectable auth: runs a raw distilled op (which needs |
| 24 | * `Credentials | HttpClient`) and discharges those requirements down to |
| 25 | * {@link RuntimeContext}. The HTTP variant provides a minted token; the |
| 26 | * Local variant provides the ambient current-credentials context. |
| 27 | */ |
| 28 | export interface StreamAuth { |
| 29 | authorize: <A, E>( |
| 30 | eff: Effect.Effect<A, E, Credentials | HttpClient.HttpClient>, |
| 31 | ) => Effect.Effect<A, E, RuntimeContext>; |
| 32 | } |
| 33 | |
| 34 | /** Legacy pipelines have their own ingest endpoint; only streams are supported. */ |
| 35 | export const legacyPipelineUnsupported = (layer: string) => |
| 36 | Effect.die( |
| 37 | new StreamSendError({ |
| 38 | message: `${layer} supports Pipelines streams only; bind a LegacyPipeline with WriteStreamBinding.`, |
| 39 | reason: "SendFailed", |
| 40 | }), |
| 41 | ); |
| 42 | |
| 43 | /** |
| 44 | * Build the producer client over the stream's HTTP ingest endpoint |
| 45 | * (`POST https://{stream_id}.ingest.cloudflare.com`, at most 5 MB per |
| 46 | * request). The stream needs `http` enabled. |
| 47 | */ |
| 48 | export const makeWriteStreamHttpClient = ( |
| 49 | auth: StreamAuth, |
| 50 | stream: Stream<any>, |
| 51 | streamId: Effect.Effect<string>, |
| 52 | ): WriteStreamClient<any> => |
| 53 | makeStreamClient({ |
| 54 | schema: recordSchemaOf(stream), |
| 55 | raw: Effect.die( |
| 56 | new StreamSendError({ |
| 57 | message: "The Pipelines HTTP client has no native binding; use send/sendEncoded.", |
| 58 | reason: "SendFailed", |
| 59 | }), |
| 60 | ), |
| 61 | sendEncoded: (records) => |
| 62 | streamId.pipe( |
| 63 | Effect.flatMap((id) => |
| 64 | auth.authorize(pipelines.sendStreamRecords({ streamId: id, records: [...records] })), |
| 65 | ), |
| 66 | Effect.mapError(toStreamSendError), |
| 67 | Effect.asVoid, |
| 68 | ), |
| 69 | }); |
| 70 | |
| 71 | /** |
| 72 | * Mint (or reuse) the host's scoped {@link AccountApiToken}, grant it |
| 73 | * `Pipelines Send`, bind its value into the host at deploy time, and |
| 74 | * build the client with it. |
| 75 | */ |
| 76 | export const makeHttpStreamBinding = <Client>(options: { |
| 77 | layer: string; |
| 78 | makeClient: (client: WriteStreamClient<any>) => Client; |
| 79 | }) => |
| 80 | Effect.gen(function* () { |
| 81 | const Token = yield* AccountApiToken; |
| 82 | const self = yield* Self; |
| 83 | |
| 84 | return Effect.fn(function* (stream: Stream<any> | LegacyPipeline) { |
| 85 | if (isLegacyPipeline(stream)) return yield* legacyPipelineUnsupported(options.layer); |
| 86 | if (!globalThis.__ALCHEMY_RUNTIME__) yield* requireLiveResource(stream); |
| 87 | const token = yield* Token(`${self.LogicalId}Token`); |
| 88 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 89 | const { accountId } = yield* CloudflareEnvironment.current; |
| 90 | yield* token.bind`${stream.LogicalId}`({ |
| 91 | policies: [ |
| 92 | { |
| 93 | effect: "allow", |
| 94 | permissionGroups: ["Pipelines Send"], |
| 95 | resources: { |
| 96 | [`com.cloudflare.api.account.${accountId}`]: "*", |
| 97 | }, |
| 98 | }, |
| 99 | ], |
| 100 | }); |
| 101 | } |
| 102 | const value = yield* token.value; |
| 103 | const streamId = yield* stream.streamId; |
| 104 | return options.makeClient( |
| 105 | makeWriteStreamHttpClient({ authorize: authorizeWith({ value }) }, stream, streamId), |
| 106 | ); |
| 107 | }); |
| 108 | }); |
| 109 | |
| 110 | /** |
| 111 | * Build the client with the **current credentials** captured at layer |
| 112 | * build (no Worker host, no minted token). |
| 113 | */ |
| 114 | export const makeLocalStreamBinding = <Client>(options: { |
| 115 | layer: string; |
| 116 | makeClient: (client: WriteStreamClient<any>) => Client; |
| 117 | }) => |
| 118 | Effect.gen(function* () { |
| 119 | const context = yield* Effect.context<Credentials | HttpClient.HttpClient>(); |
| 120 | |
| 121 | return Effect.fn(function* (stream: Stream<any> | LegacyPipeline) { |
| 122 | if (isLegacyPipeline(stream)) return yield* legacyPipelineUnsupported(options.layer); |
| 123 | // Deferred accessor — resolves the stream id at apply time, so a |
| 124 | // stream created in the same deploy works. No `host.bind`. |
| 125 | const streamId = yield* stream.streamId; |
| 126 | return options.makeClient( |
| 127 | makeWriteStreamHttpClient( |
| 128 | { authorize: (eff) => eff.pipe(Effect.provideContext(context)) }, |
| 129 | stream, |
| 130 | streamId, |
| 131 | ), |
| 132 | ); |
| 133 | }); |
| 134 | }); |