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 186 lines 6.9 KB
Raw
1 import { withCommandMeta } from 'src/invocation/command-identity';
2 import { Command, Flag } from 'effect/cli';
3 import { Effect, Option } from 'effect';
4 import { CommandInputError } from './errors';
5 import { initCmd } from './init.cmd';
6 import { triggersCmd$Listen } from './triggers/commands/triggers.listen.cmd';
7 import { logsCmd } from './logs/logs.cmd';
8 import { devToolsCmd$Execute } from './tools/execute/tools.execute.cmd';
9 import { toolkitsCmd } from './toolkits/toolkits.cmd';
10 import { authConfigsCmd } from './auth-configs/auth-configs.cmd';
11 import { connectedAccountsCmd } from './connected-accounts/connected-accounts.cmd';
12 import { triggersCmd } from './triggers/triggers.cmd';
13 import { projectsCmd } from './projects/projects.cmd';
14 import { ComposioCliUserConfig, resolveCliConfigPath } from 'src/services/cli-user-config';
15 import { Stdin } from 'src/services/stdin';
16 import { TerminalUI } from 'src/services/terminal-ui';
17 import type { CommandVisibility } from './feature-tags';
18 import { CommandSurface, NO_SHARED_FLAGS, rejectSharedFlags } from './surface';
19 import { withRegisteredSubcommands } from 'src/commands/registration';
20
21 const devMode = Flag.Literals('mode', ['on', 'off'] as const).pipe(
22 Flag.withDescription('Set developer mode on or off.'),
23 Flag.optional
24 );
25
26 /**
27 * Flags every `dev` subcommand accepts, before or after the subcommand name.
28 * A handler reads them through `CommandSurface`; a subcommand must not declare
29 * a flag of the same name, `withSharedFlags` rejects the duplicate.
30 */
31 const userId = Flag.String('user-id').pipe(
32 Flag.optional,
33 Flag.withDescription(
34 'User id to act as or filter by. Without it, commands that act as a user use the test user id of this directory (for its own project), then the global one, and name it on stderr.'
35 )
36 );
37
38 const projectName = Flag.String('project-name').pipe(
39 Flag.optional,
40 Flag.withDescription('Developer project to use instead of the one configured in this directory')
41 );
42
43 export const devSubcommands = [
44 initCmd.pipe(withCommandMeta({ sharedFlags: NO_SHARED_FLAGS })),
45 devToolsCmd$Execute,
46 triggersCmd$Listen.pipe(withCommandMeta({ requiresLogin: true })),
47 logsCmd,
48 toolkitsCmd,
49 authConfigsCmd.pipe(withCommandMeta({ sharedFlags: { userId: false } })),
50 connectedAccountsCmd,
51 triggersCmd,
52 projectsCmd.pipe(withCommandMeta({ sharedFlags: NO_SHARED_FLAGS })),
53 ] as const;
54
55 const describeCurrentMode = (enabled: boolean) =>
56 enabled
57 ? 'Developer mode is on. Developer subcommands are available.'
58 : 'Developer mode is off. Only `composio dev --mode on|off` is available.';
59
60 const applyDeveloperModeChange = (enabled: boolean) =>
61 Effect.gen(function* () {
62 const ui = yield* TerminalUI;
63 const stdin = yield* Stdin;
64 const cliConfig = yield* ComposioCliUserConfig;
65 const current = cliConfig.isDevModeEnabled();
66 if (enabled === current) {
67 yield* ui.log.info(describeCurrentMode(current));
68 return;
69 }
70
71 if (enabled) {
72 if (!stdin.isTTY()) {
73 const configPath = yield* resolveCliConfigPath;
74 return yield* new CommandInputError({
75 command: 'dev',
76 option: '--mode',
77 message: `Enabling developer mode requires an interactive terminal. Set "developer.enabled": true in ${configPath} to enable it outside an interactive session.`,
78 });
79 }
80
81 yield* ui.note(
82 [
83 'Developer mode is for engineers building with the CLI.',
84 '',
85 'It is powerful, but it unlocks more advanced commands like creating or updating auth configs and connected accounts.',
86 ].join('\n'),
87 'Warning'
88 );
89 const confirmed = yield* ui.confirm('Enable developer mode?', { defaultValue: false });
90 if (!confirmed) {
91 yield* ui.outro('Aborted.');
92 return;
93 }
94 }
95
96 yield* cliConfig.update({
97 developer: {
98 ...cliConfig.raw.developer,
99 enabled,
100 },
101 });
102
103 yield* ui.log.success(
104 enabled
105 ? 'Developer mode enabled.'
106 : 'Developer mode disabled. Only `composio dev --mode on|off` remains available.'
107 );
108 const configPath = yield* resolveCliConfigPath;
109 yield* ui.log.step(`Config updated: ${configPath}`);
110 });
111
112 const promptForDeveloperMode = () =>
113 Effect.gen(function* () {
114 const cliConfig = yield* ComposioCliUserConfig;
115 const stdin = yield* Stdin;
116 const ui = yield* TerminalUI;
117 const current = cliConfig.isDevModeEnabled();
118
119 if (!stdin.isTTY()) {
120 yield* ui.log.info(describeCurrentMode(current));
121 yield* ui.log.step('Pass `--mode on` or `--mode off` to change developer mode.');
122 return;
123 }
124
125 const next = yield* ui.select('Developer mode', [
126 {
127 value: 'off' as const,
128 label: current ? 'Turn off' : 'Keep off',
129 hint: 'Hide developer subcommands and block developer workflows.',
130 },
131 {
132 value: 'on' as const,
133 label: current ? 'Keep on' : 'Turn on',
134 hint: 'Show developer subcommands after an interactive warning.',
135 },
136 ]);
137
138 if ((next === 'on') === current) {
139 yield* ui.log.info(describeCurrentMode(current));
140 return;
141 }
142
143 yield* applyDeveloperModeChange(next === 'on');
144 });
145
146 const developerModeHandler = ({ devMode }: { readonly devMode: Option.Option<'on' | 'off'> }) =>
147 Option.match(devMode, {
148 onNone: () => promptForDeveloperMode(),
149 onSome: value => applyDeveloperModeChange(value === 'on'),
150 });
151
152 const makeDevCommand = <E, R>(
153 visibility: CommandVisibility,
154 handler: (input: { readonly devMode: Option.Option<'on' | 'off'> }) => Effect.Effect<void, E, R>
155 ) =>
156 Command.make('dev', { devMode }).pipe(
157 Command.withDescription(
158 visibility.isDevModeEnabled
159 ? 'Developer workflows: init, playground execution, logs, projects, toolkits, accounts, and triggers.'
160 : 'Developer mode controls access to developer-only workflows. When off, only `composio dev --mode on|off` is available.'
161 ),
162 Command.withExamples([
163 { command: 'composio dev --mode on' },
164 { command: 'composio dev --mode off' },
165 ]),
166 Command.withHandler(handler)
167 );
168
169 // The developer-mode toggle changes a global setting, for no user and no project.
170 const toggleRejections = {
171 '--project-name':
172 '`composio dev --mode` changes a global setting, so --project-name does not apply. Remove it.',
173 '--user-id':
174 '`composio dev --mode` changes a global setting, so --user-id does not apply. Remove it.',
175 };
176
177 export const buildDevCommand = (visibility: CommandVisibility) =>
178 visibility.isDevModeEnabled
179 ? makeDevCommand(visibility, input =>
180 rejectSharedFlags('dev', toggleRejections).pipe(Effect.andThen(developerModeHandler(input)))
181 ).pipe(
182 withRegisteredSubcommands([...devSubcommands], { ownAction: true }),
183 Command.withSharedFlags({ userId, projectName }),
184 Command.provide(({ userId, projectName }) => CommandSurface.dev({ userId, projectName }))
185 )
186 : makeDevCommand(visibility, developerModeHandler);