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

main
ts 51 lines 1.53 KB
Raw
1 import type * as runtime from "@cloudflare/workers-types";
2 import * as Effect from "effect/Effect";
3 import * as Layer from "effect/Layer";
4 import * as Redacted from "effect/Redacted";
5 import { Worker, WorkerEnvironment } from "../Workers/Worker.ts";
6 import { ReadSecret, SecretError } from "./ReadSecret.ts";
7 import type { Secret } from "./Secret.ts";
8
9 export const ReadSecretBinding = Layer.effect(
10 ReadSecret,
11 Effect.gen(function* () {
12 const env = yield* WorkerEnvironment;
13 const host = yield* Worker;
14
15 return Effect.fn(function* (secret: Secret) {
16 if (!globalThis.__ALCHEMY_RUNTIME__) {
17 yield* host.bind`${secret}`({
18 bindings: [
19 {
20 type: "secrets_store_secret",
21 name: secret.LogicalId,
22 secretName: secret.secretName,
23 storeId: secret.storeId,
24 },
25 ],
26 });
27 }
28 const raw = Effect.sync(
29 () => (env as Record<string, runtime.SecretsStoreSecret>)[secret.LogicalId],
30 );
31 const tryPromise = <T>(fn: () => Promise<T>): Effect.Effect<T, SecretError> =>
32 Effect.tryPromise({
33 try: fn,
34 catch: (error: any) =>
35 new SecretError({
36 message: error.message ?? "Unknown error",
37 cause: error,
38 }),
39 });
40
41 const getEffect = raw.pipe(
42 Effect.flatMap((raw) => tryPromise(() => raw.get().then(Redacted.make))),
43 );
44
45 return Object.assign(getEffect, {
46 raw,
47 get: () => getEffect,
48 });
49 });
50 }),
51 );