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

main
ts 95 lines 3.66 KB
Raw
1 import * as Output from "alchemy/Output";
2 import type { RuntimeContext } from "alchemy/RuntimeContext";
3 import { sha256 } from "alchemy/Util/sha256";
4 import * as Effect from "effect/Effect";
5 import * as Redacted from "effect/Redacted";
6
7 /**
8 * A database connection-string source, in every shape alchemy hands them
9 * out:
10 *
11 * - a literal / `Redacted` string
12 * - a resource Output (Neon `project.connectionUri`, PlanetScale
13 * `role.connectionUrl`, Prisma `connection.databaseUrl`, ...) — bound
14 * into the host environment at deploy and read back at runtime
15 * - a runtime-only Effect (Cloudflare Hyperdrive's per-invocation
16 * `connectionString`)
17 */
18 export type ConnectionSource =
19 | string
20 | Redacted.Redacted<string>
21 | Output.Output<string>
22 | Output.Output<Redacted.Redacted<string>>
23 | Effect.Effect<Redacted.Redacted<string>, never, RuntimeContext>;
24
25 /**
26 * The deploy-resolvable subset of {@link ConnectionSource}: literals and
27 * resource Outputs. Runtime-only Effects (Hyperdrive) are excluded —
28 * deploy-time consumers (migrations, seeds) need a value that resolves
29 * during stack evaluation.
30 */
31 export type StaticConnectionSource = Exclude<
32 ConnectionSource,
33 Effect.Effect<Redacted.Redacted<string>, never, RuntimeContext>
34 >;
35
36 const toRedacted = (value: string | Redacted.Redacted<string>): Redacted.Redacted<string> =>
37 typeof value === "string" ? Redacted.make(value) : value;
38
39 /**
40 * Resolve a {@link ConnectionSource} to its runtime accessor effect.
41 *
42 * Outputs are yielded NOW (binding into the host environment during a
43 * host init, or recording an Action capture during an Action init) and
44 * the returned accessor reads the resolved value back later; Effects pass
45 * through untouched; literals wrap.
46 */
47 export const resolveConnectionSource = (
48 source: ConnectionSource,
49 ): Effect.Effect<Effect.Effect<Redacted.Redacted<string>>> =>
50 Effect.gen(function* () {
51 if (Output.isOutput(source)) {
52 const accessor = yield* source;
53 return Effect.map(accessor as Effect.Effect<string | Redacted.Redacted<string>>, toRedacted);
54 }
55 if (Effect.isEffect(source)) {
56 // oxlint-disable-next-line effecttsgo/unsafe-effect-type-assertion -- RuntimeContext is satisfied by the host at runtime
57 return source as Effect.Effect<Redacted.Redacted<string>>;
58 }
59 return Effect.succeed(toRedacted(source));
60 }) as Effect.Effect<Effect.Effect<Redacted.Redacted<string>>>;
61
62 /**
63 * Pick the deploy-resolvable source for deploy-time work (migrations,
64 * seeds): an explicit override wins, `false` disables, and the primary
65 * source is a valid default only when it is itself deploy-resolvable.
66 */
67 export const staticConnectionSource = (
68 source: ConnectionSource,
69 override: StaticConnectionSource | false | undefined,
70 ): StaticConnectionSource | undefined => {
71 if (override === false) {
72 return undefined;
73 }
74 if (override !== undefined) {
75 return override;
76 }
77 return Effect.isEffect(source) && !Output.isOutput(source)
78 ? undefined
79 : (source as StaticConnectionSource);
80 };
81
82 /**
83 * Non-secret digest of a connection source — a sha256 Output suitable for
84 * persisted identity inputs (e.g. a migration Action's diff key) where the
85 * connection string itself must never be stored.
86 */
87 export const connectionSourceDigest = (source: StaticConnectionSource): Output.Output<string> => {
88 const digest = (value: string | Redacted.Redacted<string>) =>
89 sha256(typeof value === "string" ? value : Redacted.value(value));
90 return Output.isOutput(source)
91 ? (Output.mapEffect(digest)(
92 source as Output.Output<string | Redacted.Redacted<string>>,
93 ) as Output.Output<string>)
94 : (Output.fromEffect(digest(source)) as Output.Output<string>);
95 };