The best way to control Claude Code, Codex and more from any computer or phone

main
ts 59 lines 1.82 KB
Raw
1 /// <reference types="@cloudflare/workers-types" />
2
3 import * as Effect from "effect/Effect";
4 import * as Layer from "effect/Layer";
5 import { Worker, WorkerEnvironment } from "../Workers/Worker.ts";
6 import type { DispatchNamespace as DispatchNamespaceResource } from "./DispatchNamespace.ts";
7 import { type DispatchNamespaceClient, DispatchNamespaceError, Get } from "./Get.ts";
8
9 /**
10 * Implementation of the {@link Get} binding that uses a native
11 * `dispatch_namespace` Worker binding.
12 */
13 export const GetBinding = Layer.effect(
14 Get,
15 Effect.gen(function* () {
16 const env = yield* WorkerEnvironment;
17 const host = yield* Worker;
18
19 return Effect.fn(function* (namespace: DispatchNamespaceResource) {
20 if (!globalThis.__ALCHEMY_RUNTIME__) {
21 yield* host.bind`${namespace}`({
22 bindings: [
23 {
24 type: "dispatch_namespace",
25 name: namespace.LogicalId,
26 namespace: namespace.name,
27 },
28 ],
29 });
30 }
31
32 const raw = Effect.sync(
33 // Lazy — the WorkerEnvironment binding is not populated until runtime.
34 () => (env as Record<string, DispatchNamespace>)[namespace.LogicalId]!,
35 );
36
37 const self: DispatchNamespaceClient = {
38 raw,
39 get: (name, args, options) =>
40 raw.pipe(
41 Effect.flatMap((ns) =>
42 Effect.try({
43 try: () => ns.get(name, args, options),
44 catch: (error) =>
45 new DispatchNamespaceError({
46 message:
47 error instanceof Error
48 ? error.message
49 : "Unknown dispatch namespace runtime error",
50 cause: error,
51 }),
52 }),
53 ),
54 ),
55 };
56 return self;
57 });
58 }),
59 );