@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 97 lines 3.42 KB
Raw
1 import * as ConfigProvider from "effect/ConfigProvider";
2 import * as Effect from "effect/Effect";
3 import * as Option from "effect/Option";
4 import type { EnvironmentVariable } from "../Auth/AuthProvider.ts";
5 import { getEnv } from "../Auth/Env.ts";
6 import { resolveProfileName } from "../Auth/Resolve.ts";
7 import { loadConfigProvider } from "../Util/ConfigProvider.ts";
8 import { collectAuthProviders } from "./AuthRegistry.ts";
9 import { DEFAULT_ENTRYPOINT, type Target } from "./Entrypoint.ts";
10 import { AlchemistInvalidInput } from "./Errors.ts";
11
12 export interface CheckEnvironmentInput extends Target {
13 /** Providers to check. Omitted means every registered provider. */
14 readonly providers?: ReadonlyArray<string>;
15 }
16
17 export interface EnvironmentCheck {
18 readonly provider: string;
19 readonly status: "satisfied" | "missing" | "no-contract";
20 readonly missing: ReadonlyArray<{
21 readonly alternatives: ReadonlyArray<string>;
22 }>;
23 }
24
25 export interface EnvironmentCheckResult {
26 readonly checks: ReadonlyArray<EnvironmentCheck>;
27 readonly satisfied: boolean;
28 }
29
30 const satisfied = (variable: EnvironmentVariable) =>
31 Effect.gen(function* () {
32 for (const name of [variable.name, ...(variable.alternatives ?? [])]) {
33 const value = yield* getEnv(name);
34 if (value !== undefined && value.length > 0) return true;
35 }
36 return false;
37 });
38
39 /**
40 * Verify the environment variables each registered provider's CI contract
41 * requires are present.
42 */
43 export const checkEnvironment = Effect.fn("Alchemist.provider.checkEnvironment")(function* (
44 input: CheckEnvironmentInput,
45 ) {
46 const profile = yield* resolveProfileName(Option.fromNullishOr(input.envFile), input.profile);
47 const registry = yield* collectAuthProviders({
48 main: input.entrypoint ?? DEFAULT_ENTRYPOINT,
49 envFile: Option.fromNullishOr(input.envFile),
50 profile,
51 });
52 const known = Object.keys(registry).sort();
53 const names = yield* input.providers?.length
54 ? Effect.forEach(input.providers, (requested) => {
55 const name = known.find((candidate) => candidate.toLowerCase() === requested.toLowerCase());
56 return name === undefined
57 ? Effect.fail(
58 new AlchemistInvalidInput({
59 field: "providers",
60 message: `Unknown provider '${requested}'. Registered: ${known.join(", ")}. Install the package that provides it.`,
61 }),
62 )
63 : Effect.succeed(name);
64 })
65 : Effect.succeed(known);
66 const checks = yield* Effect.forEach(names, (name) =>
67 Effect.gen(function* () {
68 const contract = registry[name]!.environment;
69 const missing: Array<{ alternatives: ReadonlyArray<string> }> = [];
70 for (const variable of contract) {
71 if (variable.required && !(yield* satisfied(variable))) {
72 missing.push({
73 alternatives: [variable.name, ...(variable.alternatives ?? [])],
74 });
75 }
76 }
77 return {
78 provider: name,
79 status:
80 contract.length === 0
81 ? ("no-contract" as const)
82 : missing.length === 0
83 ? ("satisfied" as const)
84 : ("missing" as const),
85 missing,
86 } satisfies EnvironmentCheck;
87 }),
88 ).pipe(
89 Effect.provide(
90 ConfigProvider.layer(yield* loadConfigProvider(Option.fromNullishOr(input.envFile))),
91 ),
92 );
93 return {
94 checks,
95 satisfied: checks.every((check) => check.status !== "missing"),
96 } satisfies EnvironmentCheckResult;
97 });