@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 79 lines 3.33 KB
Raw
1 import type * as runtime from "@cloudflare/workers-types";
2 import * as Effect from "effect/Effect";
3 import { Worker, WorkerEnvironment } from "../Workers/Worker.ts";
4 import type { Queue } from "./Queue.ts";
5 import { maybeQueueShim } from "./QueueShim.ts";
6 import { SendError } from "./QueueTypes.ts";
7
8 /**
9 * Shared scaffolding for the Worker-binding implementations of the Queue
10 * services.
11 *
12 * Resolves the {@link WorkerEnvironment} and host {@link Worker}, registers
13 * the `queue` binding at deploy time, then delegates to `makeClient` with the
14 * shared {@link makeQueueHelpers} to build the producer client.
15 */
16 export const makeQueueBinding = <Client>(options: {
17 makeClient: (helpers: ReturnType<typeof makeQueueHelpers>) => Client;
18 }) =>
19 Effect.gen(function* () {
20 const env = yield* WorkerEnvironment;
21 const host = yield* Worker;
22 // Deploy-time ambient context (providers, stack). Captured at layer
23 // init so the shim registration below can run inside the callable —
24 // whose contract requires `R = never` — without leaking engine
25 // requirements into the binding's public type. Only dereferenced under
26 // the `__ALCHEMY_RUNTIME__` guard, where the context is the stack
27 // eval's (at runtime the captured context lacks providers, but the
28 // guarded branch never runs there).
29 const context = yield* Effect.context<never>();
30
31 return Effect.fn(function* (queue: Queue) {
32 if (!globalThis.__ALCHEMY_RUNTIME__) {
33 // A LOCAL host binding a LIVE queue needs the dev-mode
34 // remote-producer shim (see `QueueShim.ts`) — registered here as
35 // ordinary engine-managed resources, like the `AccountApiToken`
36 // the HTTP capability layers mint.
37 // oxlint-disable-next-line effecttsgo/unsafe-effect-type-assertion -- deploy-time only; remaining requirements are ambient where the Worker is evaluated
38 const shim = yield* maybeQueueShim(queue, host).pipe(
39 Effect.provideContext(context),
40 ) as Effect.Effect<Effect.Success<ReturnType<typeof maybeQueueShim>>>;
41 yield* host.bind`${queue}`({
42 bindings: [
43 {
44 type: "queue",
45 name: queue.LogicalId,
46 queueName: queue.queueName,
47 // Alchemy-only mode discriminator for dev (stripped before
48 // upload): a `dev:` id keeps the local broker, a real id
49 // (Alchemy.remote()) routes through the deployed shim.
50 queueId: queue.queueId,
51 ...(shim ? { shim } : {}),
52 },
53 ],
54 });
55 }
56
57 return options.makeClient(makeQueueHelpers(env, queue));
58 });
59 });
60
61 /** Primitives shared by the Worker-binding producer clients. */
62 export const makeQueueHelpers = (env: Record<string, any>, queue: Queue) => {
63 const raw = Effect.sync(() => (env as Record<string, runtime.Queue<unknown>>)[queue.LogicalId]!);
64
65 const tryPromise = <T>(fn: () => Promise<T>): Effect.Effect<T, SendError> =>
66 Effect.tryPromise({
67 try: fn,
68 catch: (error: any) =>
69 new SendError({
70 message: error?.message ?? "Unknown queue error",
71 cause: error,
72 }),
73 });
74
75 const use = <T>(fn: (raw: runtime.Queue<unknown>) => Promise<T>): Effect.Effect<T, SendError> =>
76 raw.pipe(Effect.flatMap((raw) => tryPromise(() => fn(raw))));
77
78 return { raw, use, tryPromise };
79 };