@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, 06:00 UTC

main
ts 96 lines 3.43 KB
Raw
1 import * as ConfigProvider from "effect/ConfigProvider";
2 import * as Effect from "effect/Effect";
3 import type * as FileSystem from "effect/FileSystem";
4 import type * as HttpClient from "effect/http/HttpClient";
5 import type * as Layer from "effect/Layer";
6 import type * as Path from "effect/Path";
7 import type * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
8 import { AlchemyContext } from "../AlchemyContext.ts";
9 import type { AuthProviders } from "../Auth/AuthProvider.ts";
10 import type { ProfileStore } from "../Auth/Profile.ts";
11 import type { Stack } from "../Stack.ts";
12 import { StackContext } from "../StackContext.ts";
13 import { Stage } from "../Stage.ts";
14
15 /** What a secrets provider may depend on. */
16 export type SecretsServices =
17 | Stage
18 | Stack
19 | AlchemyContext
20 | FileSystem.FileSystem
21 | Path.Path
22 | HttpClient.HttpClient
23 | ChildProcessSpawner.ChildProcessSpawner
24 | AuthProviders
25 | ProfileStore;
26
27 /** What a `secrets` callback is given to decide its value. */
28 export interface SecretsContext {
29 readonly stage: string;
30 readonly stack: Stack["Service"];
31 readonly alchemyContext: AlchemyContext["Service"];
32 }
33
34 /**
35 * A value, or a callback that picks it from the {@link SecretsContext}.
36 * Both the stack's `secrets` list and every provider's options accept this.
37 */
38 export type SecretsOption<T> = T | ((context: SecretsContext) => T);
39
40 /** @internal */
41 export const resolveSecretsOption = <T>(
42 option: SecretsOption<T>,
43 ): Effect.Effect<T, never, Stage | Stack | AlchemyContext> =>
44 typeof option !== "function"
45 ? Effect.succeed(option)
46 : Effect.map(
47 Effect.all({
48 stage: Stage,
49 stack: StackContext,
50 alchemyContext: AlchemyContext,
51 }),
52 option as (context: SecretsContext) => T,
53 );
54
55 /**
56 * The process environment, and only that, for a secrets provider's own
57 * credentials and platform detection: `DOPPLER_TOKEN`,
58 * `INFISICAL_IDENTITY_ID`, `CI`, the OIDC variables. Nothing loaded by an
59 * earlier entry can stand in for them, and the stack's precedence is
60 * untouched, so a runner's exported variables reach the provider whatever
61 * the list says. `ALCHEMY_PROFILE` alone keeps coming from the pinned view
62 * the stack built the provider on, so `--profile` still selects the profile.
63 *
64 * @internal
65 */
66 export const CredentialsConfig = ConfigProvider.layer(
67 Effect.map(ConfigProvider.ConfigProvider, (pinned) => {
68 const shell = ConfigProvider.fromEnv({ preserveEmptyStrings: true });
69 return ConfigProvider.make((path) =>
70 (path[0] === "ALCHEMY_PROFILE" ? pinned : shell).load(path),
71 );
72 }),
73 );
74
75 /** The layer shape every secrets provider builds: it reads the configuration assembled so far and provides the merged one. */
76 export type SecretsLayer = Layer.Layer<never, unknown, SecretsServices>;
77
78 /**
79 * What a `secrets` entry must satisfy. Built-in providers are
80 * `Data.TaggedClass` instances, so a `_tag` identifies the kind of provider
81 * (the stack looks for the process environment this way) and `layer` reads
82 * the configuration assembled so far and provides the merged one.
83 *
84 * ```ts
85 * class VaultProvider extends Data.TaggedClass("my-app/SecretProvider::Vault")<{
86 * readonly layer: Secrets.SecretsLayer;
87 * }> {}
88 * ```
89 */
90 export interface Provider {
91 readonly _tag: string;
92 readonly layer: SecretsLayer;
93 }
94
95 /** A `secrets` entry: a provider, or any layer that provides a ConfigProvider. */
96 export type SecretsEntry = Provider | SecretsLayer;