@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 64 lines 3.16 KB
Raw
1 import * as Effect from "effect/Effect";
2 import type * as HttpClient from "effect/http/HttpClient";
3 import { CloudflareEnvironment } from "../CloudflareEnvironment.ts";
4 import type { Credentials } from "../Credentials.ts";
5 import { dispatchByMode } from "../LocalGateway.ts";
6 import type { InstantNamespace } from "./InstantNamespace.ts";
7 import { makeProxyKVNamespaceHelpers } from "./LocalKVGateway.ts";
8 import type { Namespace } from "./Namespace.ts";
9 import type { makeKVNamespaceHelpers } from "./NamespaceBinding.ts";
10 import type { KVAuth } from "./NamespaceHttp.ts";
11
12 /**
13 * Shared scaffolding for the `*Local` KV services.
14 *
15 * Instead of minting a scoped {@link AccountApiToken} (the `*Http` path) or
16 * resolving a native Worker binding (the `*Binding` path), it captures the
17 * ambient current-credentials context available during stack-eval and
18 * builds the client per resolved namespace id:
19 *
20 * - a REAL id gets the HTTP client (same builders as the `*Http` variant,
21 * authorized with the current credentials — no `host.bind`, no minted
22 * token);
23 * - a `dev:` id (local emulation under `alchemy dev`) gets the native
24 * binding client (same builders as the `*Binding` variant) over a scoped
25 * platform-proxy gateway (see `LocalKVGateway.ts`).
26 *
27 * The id resolves lazily at apply time, so the returned client dispatches
28 * per call.
29 *
30 * NOT exported from `index.ts` — this is internal scaffolding shared by the
31 * three access-level Local layers.
32 */
33 export const makeLocalKVNamespaceBinding = <Client extends object>(options: {
34 makeHttpClient: (auth: KVAuth, namespaceId: Effect.Effect<string>) => Client;
35 makeNativeClient: (helpers: ReturnType<typeof makeKVNamespaceHelpers>) => Client;
36 }) =>
37 Effect.gen(function* () {
38 // Capture the account resolver without authenticating. Only the HTTP
39 // branch evaluates it; native local clients need no cloud credentials.
40 // Credentials are ambient during stack-eval (the stack's
41 // providers layer). Capture the full context so KV HTTP ops can run with
42 // the current credentials — no `host.bind`, no minted token.
43 const environment = yield* CloudflareEnvironment;
44 const context = yield* Effect.context<Credentials | HttpClient.HttpClient>();
45 // The FULL ambient context, for the dev-mode gateway: booting an
46 // ephemeral workerd needs the platform services and the Cloudflare
47 // environment, all present during stack-eval but not statically
48 // enumerable here.
49 const ambient = yield* Effect.context<never>();
50
51 return Effect.fn(function* (namespace: Namespace | InstantNamespace) {
52 // Deferred accessor — resolves the namespaceId against the tracker at
53 // apply time (in an Action, that's the engine's resolve context).
54 const namespaceId = yield* namespace.namespaceId;
55 const auth: KVAuth = {
56 authorize: (eff) => eff.pipe(Effect.provideContext(context)),
57 accountId: Effect.map(environment, (env) => env.accountId),
58 };
59 const httpClient = options.makeHttpClient(auth, namespaceId);
60 return dispatchByMode(namespaceId, httpClient, (id) =>
61 options.makeNativeClient(makeProxyKVNamespaceHelpers(id, ambient)),
62 );
63 });
64 });