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

main
ts 76 lines 2.93 KB
Raw
1 import { SecretsStore } from "@alchemy.run/cloudflare-runtime/core/bindings";
2 import { open } from "@alchemy.run/cloudflare-runtime/core/platform-proxy";
3 /**
4 * Node-side seeding path into the local workerd Secrets Store simulator,
5 * built on the runtime's platform proxy (`PlatformProxy.open` — our
6 * `getPlatformProxy`).
7 *
8 * For a `dev:` store there is no cloud API to speak REST to. The local
9 * `Secret` provider instead writes values through the runtime's
10 * `SecretsStore.admin` hook — the raw KV-namespace view of a store (the
11 * equivalent of Miniflare's `getSecretsStoreSecretAPI` admin surface): a
12 * scoped platform proxy hosts the binding and Node drives it natively.
13 *
14 * Node ── proxy.env.STORE.put(name, value) ──▶ secrets-store service
15 *
16 * Data lands in the same `{storage}/secrets-store` directory every local
17 * worker's `secrets_store_secret` binding reads.
18 *
19 * NOT exported from `index.ts` — provider-internal scaffolding.
20 */
21 import type * as runtime from "@cloudflare/workers-types";
22 import * as Data from "effect/Data";
23 import * as Effect from "effect/Effect";
24 import { gatewayName } from "../LocalGateway.ts";
25
26 export class LocalSecretsStoreError extends Data.TaggedError("LocalSecretsStoreError")<{
27 message: string;
28 cause?: unknown;
29 }> {}
30
31 /**
32 * Boot a scoped platform proxy for `storeId`, hand `use` the store's raw
33 * KV-namespace admin surface, and tear the instance down when `use`
34 * completes. One proxy boot per operation — slow but correct; callers are
35 * provider lifecycle operations, not a request path.
36 */
37 export const withLocalSecretsStore = <A, E, R>(
38 storeId: string,
39 use: (store: runtime.KVNamespace<string>) => Effect.Effect<A, E, R>,
40 ) =>
41 Effect.scoped(
42 Effect.gen(function* () {
43 const proxy = yield* open({
44 name: gatewayName("alchemy-secrets-store-gateway", storeId),
45 bindings: [SecretsStore.admin({ binding: "STORE", storeId })],
46 });
47 const store = (proxy.env as Record<string, unknown>).STORE as runtime.KVNamespace<string>;
48 return yield* use(store);
49 }),
50 );
51
52 /** Write a secret value into the local store (idempotent overwrite). */
53 export const seedLocalSecret = (storeId: string, secretName: string, value: string) =>
54 withLocalSecretsStore(storeId, (store) =>
55 Effect.tryPromise({
56 try: () => store.put(secretName, value),
57 catch: (cause) =>
58 new LocalSecretsStoreError({
59 message: `Failed to seed secret "${secretName}" into the local Secrets Store`,
60 cause,
61 }),
62 }),
63 );
64
65 /** Remove a secret from the local store (idempotent). */
66 export const deleteLocalSecret = (storeId: string, secretName: string) =>
67 withLocalSecretsStore(storeId, (store) =>
68 Effect.tryPromise({
69 try: () => store.delete(secretName),
70 catch: (cause) =>
71 new LocalSecretsStoreError({
72 message: `Failed to delete secret "${secretName}" from the local Secrets Store`,
73 cause,
74 }),
75 }),
76 );