Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
ts 83 lines 3.5 KB
Raw
1 import { Config, LogLevel, Option, Schema } from 'effect';
2
3 /**
4 * Readers shared by the configuration catalogs in `src/config`.
5 *
6 * Every catalog names its environment variables in full (`COMPOSIO_USER_API_KEY`, `CI`, ...)
7 * and reads them through the ambient `ConfigProvider`, which defaults to the process
8 * environment. Nothing rewrites lookup paths, so a test configures the CLI by providing
9 * `ConfigProvider.fromEnvRecord({ COMPOSIO_USER_API_KEY: '...' })` and nothing else.
10 */
11
12 /** A string setting; `undefined` when unset or blank. Surrounding whitespace is dropped. */
13 export const optionalString = (name: string): Config.Config<string | undefined> =>
14 Config.option(Config.String(name)).pipe(
15 Config.map(value => {
16 const trimmed = Option.getOrUndefined(value)?.trim();
17 return trimmed ? trimmed : undefined;
18 })
19 );
20
21 /** `self` with `fallback` in place of `undefined`. */
22 export const withFallback = <A>(
23 self: Config.Config<A | undefined>,
24 fallback: A
25 ): Config.Config<A> => self.pipe(Config.map(value => value ?? fallback));
26
27 /** A string setting that falls back to `fallback` when unset or blank. */
28 export const stringWithDefault = (name: string, fallback: string): Config.Config<string> =>
29 withFallback(optionalString(name), fallback);
30
31 const LOG_LEVEL_SPELLINGS = new Map(LogLevel.values.map(level => [level.toLowerCase(), level]));
32
33 /**
34 * A `LogLevel` setting, matched case-insensitively; `undefined` when unset, blank, or not a
35 * level name. A typo in a README-documented variable must not fail the CLI before it starts.
36 */
37 export const optionalLogLevel = (name: string): Config.Config<LogLevel.LogLevel | undefined> =>
38 optionalString(name).pipe(
39 Config.map(value =>
40 value === undefined ? undefined : LOG_LEVEL_SPELLINGS.get(value.toLowerCase())
41 )
42 );
43
44 const FLAG_OFF_SPELLINGS = new Set(['0', 'false', 'no', 'off']);
45
46 /**
47 * Tri-state read of a boolean flag. Flags arrive from the ambient environment, where a blank
48 * (`COMPOSIO_X=`) or unexpected value is routine, so this is deliberately more tolerant than
49 * `Config.Boolean`, which rejects both:
50 *
51 * unset or blank → `undefined`, the caller picks the default
52 * `0` / `false` / `no` / `off`, any casing → `false`
53 * anything else (`1`, `true`, a typo) → `true`
54 */
55 export const optionalFlag = (name: string): Config.Config<boolean | undefined> =>
56 optionalString(name).pipe(
57 Config.map(value =>
58 value === undefined ? undefined : !FLAG_OFF_SPELLINGS.has(value.toLowerCase())
59 )
60 );
61
62 /** A boolean flag with `defaultValue` for the unset and blank cases (see `optionalFlag`). */
63 export const flag = (name: string, defaultValue = false): Config.Config<boolean> =>
64 optionalFlag(name).pipe(Config.map(value => value ?? defaultValue));
65
66 // `ConfigProvider.fromEnv` splits every variable name on `_` to build a trie, so `CODEX_HOME`,
67 // `CODEX_0_X`, and a bare `CODEX` all surface under the root `CODEX`. This permissive schema
68 // accepts that subtree whatever its shape.
69 const environmentTree: Schema.Codec<unknown> = Schema.Union([
70 Schema.String,
71 Schema.Record(
72 Schema.String,
73 Schema.suspend(() => environmentTree)
74 ),
75 Schema.Array(Schema.suspend(() => environmentTree)),
76 ]);
77
78 /**
79 * Whether any variable named `<root>` or `<root>_*` is set. Names are matched as written, so
80 * `codex_home` does not count for the root `CODEX`.
81 */
82 export const hasEnvironmentRoot = (root: string): Config.Config<boolean> =>
83 Config.schema(environmentTree, root).pipe(Config.option, Config.map(Option.isSome));