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

main
ts 84 lines 3.01 KB
Raw
1 /// <reference types="@cloudflare/workers-types" />
2
3 import * as Binding from "alchemy/Binding";
4 import type { RuntimeContext } from "alchemy/RuntimeContext";
5 import * as Data from "effect/Data";
6 import * as Effect from "effect/Effect";
7 import type { DispatchNamespace as DispatchNamespaceResource } from "./DispatchNamespace.ts";
8
9 /**
10 * Bind a {@link DispatchNamespace} to a Worker and obtain the Effect-native
11 * dynamic-dispatch client (`get`).
12 *
13 * `Get` is a single identifier that is simultaneously the binding's Context
14 * tag, its type, and the callable —
15 * `yield* Cloudflare.WorkersForPlatforms.Get(namespace)`.
16 *
17 * Provide {@link GetBinding} on the Worker's runtime layer to resolve the
18 * underlying `dispatch_namespace` binding at request time.
19 *
20 *
21 * ### Dispatching to user Workers
22 * **Example:** Dispatch a request to a customer Worker
23 * Bind the namespace during the Worker's init phase, then look up and forward
24 * to a user Worker from a request handler.
25 * ```typescript
26 * const dispatch = yield* Cloudflare.WorkersForPlatforms.Get(namespace);
27 *
28 * return {
29 * fetch: Effect.gen(function* () {
30 * const request = yield* HttpServerRequest;
31 * const userWorker = yield* dispatch.get("customer-a");
32 * return yield* Effect.promise(() => userWorker.fetch(request));
33 * }),
34 * };
35 * ```
36 *
37 * @see https://developers.cloudflare.com/cloudflare-for-platforms/workers-for-platforms/get-started/dynamic-dispatch/
38 *
39 * @binding
40 * @product Workers for Platforms
41 * @category Workers & Compute
42 */
43 export interface Get extends Binding.Service<
44 Get,
45 "Cloudflare.WorkersForPlatforms.Get",
46 (namespace: DispatchNamespaceResource) => Effect.Effect<DispatchNamespaceClient>
47 > {}
48
49 export const Get = Binding.Service<Get>("Cloudflare.WorkersForPlatforms.Get");
50
51 /** Error raised by dynamic-dispatch runtime operations. */
52 export class DispatchNamespaceError extends Data.TaggedError("DispatchNamespaceError")<{
53 /** Human-readable runtime error message. */
54 message: string;
55 /** Original error thrown by the Cloudflare runtime binding. */
56 cause: unknown;
57 }> {}
58
59 /**
60 * Effect-native client for a Workers for Platforms dispatch-namespace Worker
61 * binding.
62 *
63 * Wraps the runtime `DispatchNamespace` binding so the lookup returns an
64 * Effect tagged with {@link DispatchNamespaceError}. Use
65 * `Cloudflare.WorkersForPlatforms.Get(namespace)` inside a Worker's init
66 * phase.
67 */
68 export interface DispatchNamespaceClient {
69 /** Effect resolving to the raw dispatch-namespace runtime binding. */
70 raw: Effect.Effect<DispatchNamespace, never, RuntimeContext>;
71 /**
72 * Look up a user Worker in the dispatch namespace by script name and obtain
73 * a `Fetcher` to send it requests.
74 *
75 * @param name Name of the user Worker script.
76 * @param args Arguments passed to the user Worker script.
77 * @param options Options for the dynamic-dispatch invocation.
78 */
79 get(
80 name: string,
81 args?: { [key: string]: any },
82 options?: DynamicDispatchOptions,
83 ): Effect.Effect<Fetcher, DispatchNamespaceError, RuntimeContext>;
84 }