@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 488 lines 19 KB
Raw
1 import type * as cf from "@cloudflare/workers-types";
2 import * as NodeChildProcessSpawner from "@effect/platform-node/NodeChildProcessSpawner";
3 import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
4 import * as NodeFileSystem from "@effect/platform-node/NodeFileSystem";
5 import * as NodePath from "@effect/platform-node/NodePath";
6 import * as NodeStdio from "@effect/platform-node/NodeStdio";
7 import * as NodeTerminal from "@effect/platform-node/NodeTerminal";
8 import { makeEntrypointLayer, reifyBoundConfigProvider } from "alchemy/Runtime";
9 import { RuntimeContext } from "alchemy/RuntimeContext";
10 import { Self } from "alchemy/Self";
11 import { StackContext } from "alchemy/StackContext";
12 import { Stage } from "alchemy/Stage";
13 import { buildEventTelemetry } from "alchemy/TelemetryRuntime";
14 import type { DurableObject, WorkerEntrypoint } from "cloudflare:workers";
15 import * as Cause from "effect/Cause";
16 import * as ConfigProvider from "effect/ConfigProvider";
17 import * as Context from "effect/Context";
18 import * as Effect from "effect/Effect";
19 import * as Exit from "effect/Exit";
20 import * as FetchHttpClient from "effect/http/FetchHttpClient";
21 import * as EffectHttp from "effect/http/HttpEffect";
22 import * as Layer from "effect/Layer";
23 import * as Logger from "effect/Logger";
24 import { MinimumLogLevel } from "effect/References";
25 import * as Scope from "effect/Scope";
26 import * as Stream from "effect/Stream";
27 import { CloudflareEnvironment } from "../CloudflareEnvironmentService.ts";
28 import cloudflare_workers from "./cloudflare_workers.ts";
29 import { isScopeEjected } from "./HttpServer.ts";
30 import {
31 ErrorTag,
32 type RpcErrorEnvelope,
33 type RpcStreamEnvelope,
34 encodeRpcError,
35 toRpcStream,
36 } from "./Rpc.ts";
37 import type { Worker } from "./Worker.ts";
38 import {
39 ExportedHandlerMethods,
40 WorkerEnvironment,
41 WorkerExecutionContext,
42 fromExecutionContext,
43 } from "./WorkerRuntime.ts";
44 import type { WorkerRuntimeContext } from "./WorkerRuntimeContext.ts";
45
46 /**
47 * The isolate-lifetime artifacts produced by a single layer build: the built
48 * service Context, the resolved export for this entrypoint, the user's
49 * RPC shape (a thunk — the shape is only populated once `serve` has run),
50 * and the telemetry Layer override registered during init (a thunk for the
51 * same reason).
52 */
53 export interface WorkerBuild<Export = any> {
54 readonly context: Context.Context<any>;
55 readonly runtimeContext: WorkerRuntimeContext;
56 readonly export: Export;
57 readonly shape: () => Record<string, any>;
58 readonly telemetry: () => Layer.Layer<never, any, any> | undefined;
59 }
60
61 /**
62 * Makes the WorkerEntrypoint class and bridges to Effect fetch and RPC calls.
63 */
64 export const makeWorkerBridge = (
65 Base: typeof WorkerEntrypoint | typeof DurableObject,
66 { stack, entrypoint }: { stack: { name: string; stage: string }; entrypoint: any },
67 ) => {
68 const { build } = getWorkerExport({ entrypoint, stack, exportName: "default" });
69
70 const processEvent = <T>(
71 makeEffect: (
72 build: WorkerBuild,
73 ) => readonly [Effect.Effect<any, any, any>, Context.Context<never>],
74 ctx: cf.ExecutionContext,
75 env: Record<string, unknown> | undefined,
76 onExit: (exit: Exit.Exit<any, any>, scope: Scope.Closeable) => T | Promise<T>,
77 ): Promise<T> => {
78 const scope = Scope.makeUnsafe();
79 return build((promise) => ctx.waitUntil(promise as Promise<any>))
80 .then(
81 (built) => {
82 const [eff, services] = makeEffect(built);
83 return eff.pipe(
84 // Per-event services take precedence over the captured services
85 // and the built isolate context: the real per-event
86 // WorkerExecutionContext (init only ever sees the reference's
87 // deferred default), and the fresh request `Scope` so
88 // `Effect.addFinalizer` in a handler attaches to the request
89 // scope (closed into `ctx.waitUntil` below).
90 Effect.provide(
91 Layer.mergeAll(
92 Layer.succeed(WorkerExecutionContext, fromExecutionContext(ctx, env)),
93 Layer.succeed(Scope.Scope, scope),
94 Layer.succeed(RuntimeContext, built.runtimeContext),
95 // The configured telemetry exporters. Constructed as part
96 // of this per-event layer, but `buildEventTelemetry`
97 // attaches their batching fibers and flush finalizers to
98 // the request `scope` (not this build's transient scope),
99 // so buffered telemetry flushes when the scope closes into
100 // `ctx.waitUntil` below — never on workerd's ephemeral
101 // isolate scope.
102 Layer.effectContext(buildEventTelemetry(built.context, scope, built.telemetry())),
103 ).pipe(
104 Layer.provideMerge(Layer.succeedContext(services)),
105 Layer.provideMerge(Layer.succeedContext(built.context)),
106 ),
107 ),
108 Effect.runPromiseExit,
109 );
110 },
111 // A failed isolate build reaches callers as a defect exit so the RPC
112 // path can envelope-encode it like any other handler defect.
113 (error) => Exit.die(error),
114 )
115 .then((exit) => onExit(exit, scope))
116 .finally(() =>
117 isScopeEjected(scope)
118 ? undefined
119 : ctx.waitUntil(
120 // The HttpMiddleware tracer ends the request's root span in a
121 // dispatcher task scheduled after the handler effect resolves.
122 // Yield one macrotask before closing the scope so that span
123 // reaches the telemetry exporter's buffer before the scope's
124 // flush finalizer runs.
125 new Promise((resolve) => setTimeout(resolve, 0)).then(() =>
126 Effect.runPromise(Scope.close(scope, Exit.void)),
127 ),
128 ),
129 );
130 };
131 class WorkerBridge extends Base {
132 constructor(
133 public readonly ctx: any,
134 public readonly env: any,
135 ) {
136 super(ctx, env);
137
138 for (const methodName of ExportedHandlerMethods) {
139 (this as any)[methodName] = async (input: any) =>
140 processEvent(
141 (built) =>
142 built.export[methodName](input, this.env, this.ctx) as [
143 Effect.Effect<any>,
144 Context.Context<never>,
145 ],
146 this.ctx,
147 this.env,
148 (exit) =>
149 exit._tag === "Success"
150 ? Promise.resolve(exit.value)
151 : Promise.reject(Cause.squash(exit.cause)),
152 );
153 }
154
155 return new Proxy(this, {
156 get: (target, prop) => {
157 if (typeof prop !== "string") return (target as any)[prop];
158 if (prop in target) return (target as any)[prop];
159 return (...args: unknown[]) =>
160 processEvent(
161 (built) => {
162 const dispatcher = built.shape()?.[prop];
163 if (typeof dispatcher !== "function") {
164 return [
165 Effect.die(
166 new Error(
167 `Method "${prop}" not found on worker. ` +
168 `Make sure it's returned from the worker's default export.`,
169 ),
170 ),
171 Context.empty(),
172 ] as const;
173 }
174 const result = dispatcher(...args);
175 // Effects (including nested-RPC values built by
176 // `asEffectOrStream`, which are Effects *branded* as Streams)
177 // must be run as effects — their resolved value may itself be
178 // a `Stream`, which `handleRpcExit` then encodes. Only a
179 // *genuine* `Stream` (not an Effect) is lifted into the
180 // success channel so `handleRpcExit` encodes it directly.
181 return [
182 Effect.isEffect(result)
183 ? // oxlint-disable-next-line effecttsgo/unsafe-effect-type-assertion -- untyped RPC dispatcher result; failures are encoded by handleRpcExit
184 (result as Effect.Effect<any>)
185 : Stream.isStream(result)
186 ? Effect.succeed(result)
187 : (result as Effect.Effect<any>),
188 Context.empty(),
189 ] as const;
190 },
191 this.ctx,
192 this.env,
193 handleRpcExit,
194 );
195 },
196 });
197 }
198 }
199
200 // Stub prototype methods so Cloudflare's script-validate detects the
201 // standard handler set; per-instance overrides above are what actually
202 // run.
203 for (const method of ExportedHandlerMethods) {
204 Object.defineProperty(WorkerBridge.prototype, method, {
205 value: function () {
206 throw new Error(`Bridge method '${method}' was called before instance setup`);
207 },
208 writable: true,
209 configurable: true,
210 });
211 }
212
213 return WorkerBridge;
214 };
215
216 /**
217 * One isolate-lifetime layer build per entrypoint module. The generated
218 * entry passes the same `meta.entrypoint` object to `makeWorkerBridge`,
219 * `makeDurableObjectBridge`, and `makeWorkflowBridge`, so keying on it
220 * shares a single build (one run of the user's init closure) across the
221 * default worker and every Durable Object / Workflow class in the isolate.
222 */
223 const sharedBuilds = new WeakMap<
224 object,
225 (pin: (promise: Promise<unknown>) => unknown) => Promise<Context.Context<any>>
226 >();
227
228 const getSharedBuild = (entrypoint: any, stack: { name: string; stage: string }) => {
229 let shared = sharedBuilds.get(entrypoint);
230 if (shared !== undefined) {
231 return shared;
232 }
233
234 const tag = Self as any as Context.Service<
235 never,
236 Worker & { RuntimeContext: WorkerRuntimeContext }
237 >;
238
239 const layer = makeEntrypointLayer(tag, entrypoint);
240
241 // `NodeServices.layer`, except `NodeTerminal` is only built when
242 // `process.stdin` is usable: it calls `process.stdin.once` while it builds,
243 // and workerd's `process` has no `stdin` on compatibility dates before
244 // Node.js process v2.
245 const terminal = Layer.unwrap(
246 Effect.sync(() =>
247 typeof globalThis.process?.stdin?.once === "function" ? NodeTerminal.layer : Layer.empty,
248 ),
249 );
250 const platformServices = Layer.provideMerge(
251 NodeChildProcessSpawner.layer,
252 Layer.mergeAll(
253 NodeFileSystem.layer,
254 NodeCrypto.layer,
255 NodePath.layer,
256 NodeStdio.layer,
257 terminal,
258 ),
259 );
260
261 const platform = Layer.mergeAll(
262 platformServices,
263 FetchHttpClient.layer,
264 // TODO(sam): wire this up to telemetry more directly
265 Logger.layer([Logger.consolePretty()]),
266 );
267
268 // Private scope for the isolate-lifetime layer build. Never closed —
269 // workerd has no isolate-teardown hook, so finalizers attached here can
270 // never run. It exists only because `Layer.buildWithMemoMap` requires a
271 // scope argument (`Layer.scoped`-style layers attach their finalizers to
272 // it). It is deliberately NOT provided as the ambient `Scope.Scope` of the
273 // init context: request-coupled resources are acquired inside handlers
274 // against the per-event scope that `processEvent` provides.
275 const instanceScope = Scope.makeUnsafe();
276 const memoMap = Layer.makeMemoMapUnsafe();
277
278 const globalContext = Layer.unwrap(
279 cloudflare_workers.pipe(
280 Effect.map(({ env }) =>
281 layer.pipe(
282 Layer.provideMerge(
283 Layer.succeed(StackContext, {
284 name: stack.name,
285 stage: stack.stage,
286 bindings: {},
287 resources: {},
288 actions: {},
289 }),
290 ),
291 Layer.provideMerge(Layer.succeed(Stage, stack.stage)),
292 Layer.provideMerge(platform),
293 Layer.provideMerge(
294 Layer.succeed(
295 ConfigProvider.ConfigProvider,
296 ConfigProvider.orElse(
297 ConfigProvider.fromUnknown({ ALCHEMY_PHASE: "runtime" }),
298 // Auto-bound `Config` values arrive in `env` as
299 // `{"_tag":"Redacted","value":...}` markers; reify them so a
300 // `Config` re-read inside a request handler decodes the raw
301 // source value instead of the marker JSON.
302 reifyBoundConfigProvider(
303 ConfigProvider.fromUnknown(env),
304 env as Record<string, unknown>,
305 ),
306 ),
307 ),
308 ),
309 Layer.provideMerge(Layer.succeed(WorkerEnvironment, env)),
310 Layer.provideMerge(
311 Layer.succeed(
312 CloudflareEnvironment,
313 // TODO(sam): fix this with maybe a CloudflareAccountId Effect service
314 // @ts-expect-error - this is hacky, but we only need and have this property
315 Effect.succeed({ account: (env as any).ALCHEMY_CLOUDFLARE_ACCOUNT_ID }),
316 ),
317 ),
318 Layer.provideMerge(Layer.succeed(MinimumLogLevel, (env as any).DEBUG ? "Debug" : "Info")),
319 ),
320 ),
321 ),
322 );
323
324 let built: Promise<Context.Context<any>> | undefined;
325
326 /**
327 * Build the isolate-lifetime layer stack exactly once; every subsequent
328 * event (and every export sharing this entrypoint) reuses the memoized
329 * Context.
330 *
331 * `pin` registers the in-flight build promise with the calling event
332 * (`ctx.waitUntil` / `state.waitUntil`). Every awaiting event must pin:
333 * workerd schedules a promise's continuations back into its origin request
334 * context and *drops* them if that context has ended
335 * (`handle_cross_request_promise_resolution`), so the origin event must be
336 * kept alive until the build settles or concurrent cold-start requests
337 * would hang.
338 *
339 * Only success is memoized — a transient init failure (e.g. a flaky
340 * `Config` read in user init) resets the memo and heals on the next event.
341 */
342 shared = (pin) => {
343 const promise = (built ??= Effect.runPromise(
344 Layer.buildWithMemoMap(globalContext, memoMap, instanceScope).pipe(
345 // Strip the build's memo map from the exposed context so a Layer the
346 // user `Effect.provide`s *inside a handler* builds per event instead
347 // of sharing one instance (pinned to the first request's IoContext)
348 // across concurrent events.
349 Effect.map(Context.omit(Layer.CurrentMemoMap)),
350 ),
351 ).catch((error) => {
352 built = undefined;
353 throw error;
354 }));
355 pin(promise.catch(() => {}));
356 return promise;
357 };
358 sharedBuilds.set(entrypoint, shared);
359 return shared;
360 };
361
362 export const getWorkerExport = <Export = any>({
363 entrypoint,
364 stack,
365 exportName,
366 }: {
367 entrypoint: any;
368 stack: { name: string; stage: string };
369 exportName: string;
370 }) => {
371 const tag = Self as any as Context.Service<
372 never,
373 Worker & { RuntimeContext: WorkerRuntimeContext }
374 >;
375
376 const runtimeContext = tag.pipe(Effect.map((func) => func.RuntimeContext));
377 // oxlint-disable-next-line effecttsgo/unsafe-effect-type-assertion -- export resolution requirements are provided by the bridge at runtime
378 const exported = runtimeContext.pipe(
379 Effect.flatMap((context) => context.exports),
380 Effect.flatMap((exports) =>
381 Effect.isEffect(exports[exportName])
382 ? exports[exportName]
383 : Effect.succeed(exports[exportName]),
384 ),
385 ) as Effect.Effect<Export>;
386
387 const sharedBuild = getSharedBuild(entrypoint, stack);
388
389 let built: Promise<WorkerBuild<Export>> | undefined;
390
391 /**
392 * Resolve this export against the shared isolate build; memoized so
393 * listener assembly and the captured services context resolve once per
394 * export. Same success-only memoization contract as the shared build.
395 */
396 const build = (pin: (promise: Promise<unknown>) => unknown): Promise<WorkerBuild<Export>> => {
397 const promise = (built ??= sharedBuild(pin)
398 .then((context) =>
399 Effect.runPromise(
400 Effect.all([exported, runtimeContext]).pipe(
401 Effect.map(([exp, rc]): WorkerBuild<Export> => ({
402 context,
403 runtimeContext: rc,
404 export: exp,
405 shape: rc.shape,
406 telemetry: () => rc.telemetry,
407 })),
408 Effect.provideContext(context),
409 ),
410 ),
411 )
412 .catch((error) => {
413 built = undefined;
414 throw error;
415 }));
416 pin(promise.catch(() => {}));
417 return promise;
418 };
419
420 return { build };
421 };
422
423 export const makeRpcProxy = (
424 self: any,
425 userShape: Effect.Effect<any>,
426 processEvent: (eff: Effect.Effect<[Effect.Effect<any>, Context.Context<never>]>) => Promise<any>,
427 ) =>
428 new Proxy(self, {
429 get: (target, prop) => {
430 if (typeof prop !== "string") return (target as any)[prop];
431 if (prop in target) return (target as any)[prop];
432 return (...args: unknown[]) =>
433 userShape
434 .pipe(
435 Effect.map((shape) => shape[prop]),
436 Effect.flatMap((dispatcher) => {
437 if (typeof dispatcher !== "function") {
438 return Effect.die(
439 new Error(
440 `Method "${prop}" not found on worker. ` +
441 `Make sure it's returned from the worker's default export.`,
442 ),
443 );
444 }
445 const result = dispatcher(...args);
446 // Effects (including nested-RPC values built by
447 // `asEffectOrStream`, which are Effects *branded* as Streams)
448 // must be run as effects — their resolved value may itself be a
449 // `Stream`, which `handleRpcExit` then encodes. Only a *genuine*
450 // `Stream` (not an Effect) is lifted into the success channel so
451 // `handleRpcExit` encodes it directly.
452 return Effect.isEffect(result)
453 ? // oxlint-disable-next-line effecttsgo/unsafe-effect-type-assertion -- untyped RPC dispatcher result; failures are encoded by handleRpcExit
454 (result as Effect.Effect<any>)
455 : Stream.isStream(result)
456 ? Effect.succeed(result)
457 : (result as Effect.Effect<any>);
458 }),
459 processEvent,
460 )
461 .then((exit) => handleRpcExit(exit));
462 },
463 });
464
465 export const handleRpcExit = async (exit: Exit.Exit<any, any>, scope?: Scope.Closeable) => {
466 if (exit._tag === "Success") {
467 if (Stream.isStream(exit.value)) {
468 let stream = exit.value as Stream.Stream<any, any, any>;
469 if (scope !== undefined && !isScopeEjected(scope)) {
470 // The RPC transport drains the encoded ReadableStream *after* this
471 // function returns, so the request scope must outlive the handler:
472 // eject it from the bridge's close-on-return path and close it when
473 // the stream settles instead — mirroring `scopeTransferToStream` on
474 // the fetch path.
475 EffectHttp.scopeDisableClose(scope);
476 stream = stream.pipe(Stream.onExit((streamExit) => Scope.close(scope, streamExit)));
477 }
478 return await Effect.runPromise(toRpcStream(stream) as Effect.Effect<RpcStreamEnvelope>);
479 }
480 return exit.value;
481 }
482 const failReason = exit.cause.reasons.find(Cause.isFailReason);
483 if (failReason) {
484 return { _tag: ErrorTag, error: encodeRpcError(failReason.error) } satisfies RpcErrorEnvelope;
485 }
486 const dieReason = exit.cause.reasons.find(Cause.isDieReason);
487 throw dieReason?.defect ?? new Error("RPC method failed with an unexpected cause");
488 };