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 162 lines 5.61 KB
Raw
1 import * as FileSystem from 'effect/FileSystem';
2 import * as Path from 'effect/Path';
3 import type * as PlatformError from 'effect/PlatformError';
4 import { Context, Effect, Layer, Option, Predicate, Schema } from 'effect';
5 import { JsonRecordSchema } from 'src/effects/json';
6 import { setupCacheDir } from 'src/effects/setup-cache-dir';
7 import { getVersion } from 'src/effects/version';
8 import {
9 CliUserConfig,
10 cliUserConfigFromJSON,
11 cliUserConfigToJSON,
12 } from 'src/models/cli-user-config';
13 import { isExperimentalFeatureEnabledByDefault } from 'src/experimental-features';
14 import * as constants from 'src/constants';
15 import type { CliReleaseChannel } from 'src/constants';
16
17 export type CliUserConfigResolved = {
18 readonly channel: CliReleaseChannel;
19 readonly developerModeEnabled: boolean;
20 readonly developerDangerousCommandsEnabled: boolean;
21 readonly experimentalFeatures: Readonly<Record<string, boolean>>;
22 readonly artifactDirectory: string | undefined;
23 /**
24 * Where the CLI stores the Composio API key. See the
25 * `SecurityBackend` type in `src/models/cli-user-config.ts`.
26 * Default: `"auto"` (plaintext `user_data.json`, backwards-compatible
27 * with every prior CLI release).
28 */
29 readonly security: 'auto' | 'json' | 'keychain-subprocess' | 'keychain';
30 };
31
32 const detectReleaseChannel = (version: string): CliReleaseChannel =>
33 /-[0-9A-Za-z.-]+$/.test(version) ? 'beta' : 'stable';
34
35 const DEFAULT_CLI_USER_CONFIG = CliUserConfig.make({
36 developer: {
37 enabled: true,
38 destructiveActions: false,
39 },
40 experimentalFeatures: {},
41 artifactDirectory: Option.none(),
42 security: 'auto',
43 });
44
45 const decodeConfigJson = Schema.decodeUnknownEffect(Schema.fromJsonString(JsonRecordSchema));
46
47 export const resolveCliConfigPath = Effect.gen(function* () {
48 const path = yield* Path.Path;
49 const configDir = yield* setupCacheDir;
50 return path.join(configDir, constants.CLI_CONFIG_FILE_NAME);
51 });
52
53 export class ComposioCliUserConfig extends Context.Service<
54 ComposioCliUserConfig,
55 {
56 readonly data: CliUserConfigResolved;
57 readonly raw: CliUserConfig;
58 readonly channel: CliReleaseChannel;
59 readonly isDevModeEnabled: () => boolean;
60 readonly areDeveloperDangerousCommandsEnabled: () => boolean;
61 readonly isExperimentalFeatureEnabled: (feature: string) => boolean;
62 readonly update: (
63 next: Partial<CliUserConfig>
64 ) => Effect.Effect<void, Schema.SchemaError | PlatformError.PlatformError, never>;
65 }
66 >()('ComposioCliUserConfig') {}
67
68 const resolveConfig = (raw: CliUserConfig, channel: CliReleaseChannel): CliUserConfigResolved => ({
69 channel,
70 developerModeEnabled: raw.developer.enabled,
71 developerDangerousCommandsEnabled: raw.developer.destructiveActions,
72 experimentalFeatures: raw.experimentalFeatures,
73 artifactDirectory: Option.getOrUndefined(raw.artifactDirectory),
74 security: raw.security,
75 });
76
77 export const ComposioCliUserConfigLive = Layer.effect(
78 ComposioCliUserConfig,
79 Effect.gen(function* () {
80 const fs = yield* FileSystem.FileSystem;
81 const version = yield* getVersion;
82 const channel = detectReleaseChannel(version);
83 const jsonConfigPath = yield* resolveCliConfigPath;
84
85 let rawConfig = DEFAULT_CLI_USER_CONFIG;
86
87 const normalizeRawConfigJson = (value: unknown): unknown => {
88 if (!Predicate.isObject(value)) {
89 return value;
90 }
91
92 const record = { ...value };
93 const existingDeveloper = Predicate.isObject(record.developer) ? { ...record.developer } : {};
94
95 if (!('enabled' in existingDeveloper) && 'developer_mode_enabled' in record) {
96 existingDeveloper.enabled = record.developer_mode_enabled;
97 }
98 if (
99 !('destructive_actions' in existingDeveloper) &&
100 'developer_dangerous_commands_enabled' in record
101 ) {
102 existingDeveloper.destructive_actions = record.developer_dangerous_commands_enabled;
103 }
104
105 delete record.developer_mode_enabled;
106 delete record.developer_dangerous_commands_enabled;
107
108 record.developer = existingDeveloper;
109 return record;
110 };
111
112 const persist = (next: CliUserConfig) =>
113 Effect.gen(function* () {
114 const encoded = yield* cliUserConfigToJSON(next);
115 yield* fs.writeFileString(jsonConfigPath, encoded);
116 rawConfig = next;
117 });
118
119 const update = (
120 next: Partial<CliUserConfig>
121 ): Effect.Effect<void, Schema.SchemaError | PlatformError.PlatformError, never> =>
122 persist(
123 CliUserConfig.make({
124 ...rawConfig,
125 ...next,
126 })
127 );
128
129 const load = Effect.gen(function* () {
130 const configJson = yield* fs.readFileString(jsonConfigPath, 'utf8');
131 const parsed = yield* decodeConfigJson(configJson);
132 rawConfig = yield* cliUserConfigFromJSON(JSON.stringify(normalizeRawConfigJson(parsed)));
133 return rawConfig;
134 });
135
136 if (yield* fs.exists(jsonConfigPath)) {
137 yield* load.pipe(Effect.catch(() => persist(DEFAULT_CLI_USER_CONFIG)));
138 } else {
139 yield* persist(rawConfig);
140 }
141
142 const isExperimentalFeatureEnabled = (feature: string) => {
143 const configured = resolveConfig(rawConfig, channel).experimentalFeatures[feature];
144 return configured ?? isExperimentalFeatureEnabledByDefault(feature, channel);
145 };
146
147 return ComposioCliUserConfig.of({
148 get data() {
149 return resolveConfig(rawConfig, channel);
150 },
151 get raw() {
152 return rawConfig;
153 },
154 channel,
155 isDevModeEnabled: () => resolveConfig(rawConfig, channel).developerModeEnabled,
156 areDeveloperDangerousCommandsEnabled: () =>
157 resolveConfig(rawConfig, channel).developerDangerousCommandsEnabled,
158 isExperimentalFeatureEnabled,
159 update,
160 });
161 })
162 );