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

main
ts 91 lines 3.82 KB
Raw
1 /**
2 * Node-side path into the local workerd KV simulator, built on the
3 * runtime's platform proxy (`PlatformProxy.open` — our `getPlatformProxy`).
4 *
5 * For a `dev:` namespace there is no cloud namespace to speak REST to.
6 * Instead, each operation opens a scoped platform proxy hosting the native
7 * KV binding and runs against `proxy.env.KV` — the same
8 * `runtime.KVNamespace` surface the Worker-binding client builders
9 * (`makeReadKVClient` / `makeWriteKVClient`) already consume, so the
10 * `*Local` layers reuse those builders verbatim:
11 *
12 * Node ── proxy.env.KV.get(...) ──▶ kv service ──▶ KVNamespaceObject
13 *
14 * One proxy boot per operation — slow but correct; the `*Local` layers run
15 * in deploy-time Actions, not on a request path. Data lands in the same
16 * `{storage}/kv` directory every local worker binding reads.
17 *
18 * NOT exported from `index.ts` — capability-internal scaffolding.
19 */
20 import { KvNamespace } from "@alchemy.run/cloudflare-runtime/core/bindings";
21 import { open } from "@alchemy.run/cloudflare-runtime/core/platform-proxy";
22 import type * as runtime from "@cloudflare/workers-types";
23 import type * as Context from "effect/Context";
24 import * as Effect from "effect/Effect";
25 import { gatewayName, localGatewayRuntime } from "../LocalGateway.ts";
26 import { isInstantNamespaceLocalId } from "./InstantNamespaceLocal.ts";
27 import type { makeKVNamespaceHelpers } from "./NamespaceBinding.ts";
28 import { NamespaceError } from "./NamespaceTypes.ts";
29
30 const tryPromise = <T>(fn: () => Promise<T>): Effect.Effect<T, NamespaceError> =>
31 Effect.tryPromise({
32 try: fn,
33 catch: (error: any) =>
34 new NamespaceError({
35 message: error?.message ?? "Unknown error",
36 cause: error,
37 }),
38 });
39
40 /**
41 * Binding-client helpers backed by a per-operation platform proxy instead
42 * of a Worker's `env`. `raw` cannot be satisfied — a native namespace only
43 * lives as long as its proxy's scope — so it dies with guidance.
44 */
45 export const makeProxyKVNamespaceHelpers = (
46 namespaceId: string,
47 /**
48 * The FULL ambient stack-eval context: platform services for booting
49 * workerd plus `CloudflareEnvironment`/`AlchemyContext` for the runtime
50 * layer.
51 */
52 ambient: Context.Context<never>,
53 ): ReturnType<typeof makeKVNamespaceHelpers> => {
54 const use = <T>(
55 fn: (raw: runtime.KVNamespace<string>) => Promise<T>,
56 ): Effect.Effect<T, NamespaceError> =>
57 // oxlint-disable-next-line effecttsgo/unsafe-effect-type-assertion -- remaining requirements are satisfied by the captured ambient context
58 Effect.scoped(
59 Effect.gen(function* () {
60 const proxy = yield* open({
61 name: gatewayName("alchemy-kv-gateway", namespaceId),
62 bindings: [
63 KvNamespace.local({
64 binding: "KV",
65 id: namespaceId,
66 mode: isInstantNamespaceLocalId(namespaceId) ? "instant" : undefined,
67 }),
68 ],
69 });
70 const kv = (proxy.env as Record<string, unknown>).KV as runtime.KVNamespace<string>;
71 return yield* tryPromise(() => fn(kv));
72 }),
73 ).pipe(
74 Effect.provide(localGatewayRuntime),
75 // The gateway layer's platform requirements are satisfied by the
76 // ambient stack-eval context; `Context<never>` can't prove that
77 // statically, so erase the leftover R (and the proxy's own infra
78 // error channel) with a cast — mirroring D1's gateway.
79 Effect.provideContext(ambient),
80 ) as Effect.Effect<T, NamespaceError>;
81
82 const raw = Effect.die(
83 new NamespaceError({
84 message:
85 "A locally-emulated KV namespace has no long-lived native binding outside a Worker; use get/put/delete/list/getWithMetadata (each runs through a scoped local gateway).",
86 cause: new Error("unsupported"),
87 }),
88 ) as Effect.Effect<runtime.KVNamespace<string>>;
89
90 return { raw, use, tryPromise };
91 };