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 636 lines 20 KB
Raw
1 import { Context, Effect, Option } from 'effect';
2 import { actingUser, CommandSurface } from 'src/commands/surface';
3 import { toolkitFromToolSlug } from 'src/effects/toolkit-from-tool-slug';
4 import { mapComposioError } from 'src/services/composio-error-overrides';
5 import {
6 getFreshConsumerConnectedToolkitsFromCache,
7 refreshConsumerConnectedToolkitsCache,
8 } from 'src/services/consumer-short-term-cache';
9 import { CLI_DEBUG_FLAG_NAMES } from 'src/services/runtime-flags';
10 import { normalizeFileUploadSchema } from 'src/services/tool-file-uploads';
11 import {
12 getOrFetchToolInputDefinition,
13 validateToolInputArgumentsWithDefinition,
14 } from 'src/services/tool-input-validation';
15 import { escapeTerminalLines, escapeTerminalText } from 'src/ui/escape-terminal-text';
16 import { redact } from 'src/ui/redact';
17 import {
18 resolveExecuteContext,
19 resolveSchemaContext,
20 toolkitProjectScope,
21 type ResolvedExecuteContext,
22 type SharedRunToolsExecuteParams,
23 } from './tools.execute.context';
24 import {
25 invalidArguments,
26 ReportedToolExecutionError,
27 ToolExecutionError,
28 } from './tools.execute.errors';
29 import {
30 ciRedactReplacer,
31 describeStoredOutputSize,
32 executionSuccessSuffix,
33 serializeExecuteOutput,
34 writeExecuteStdout,
35 type DryRunSummary,
36 } from './tools.execute.format';
37 import { prepareExecuteOutput } from './tools.execute.output';
38 import { checkDryRunArguments } from './tools.execute.validation';
39
40 export const ExecuteInvocationArgs = Context.Reference<ReadonlyArray<string>>(
41 'commands/ExecuteInvocationArgs',
42 { defaultValue: () => [] }
43 );
44
45 type ParallelExecuteSpec = {
46 readonly slug: string;
47 readonly data: Option.Option<string>;
48 readonly account: Option.Option<string>;
49 };
50
51 type ParsedParallelExecuteArgs = SharedRunToolsExecuteParams & {
52 readonly userId: Option.Option<string>;
53 readonly specs: ReadonlyArray<ParallelExecuteSpec>;
54 };
55
56 type ParallelExecuteResult =
57 | {
58 readonly slug: string;
59 readonly successful: true;
60 readonly version: string | null;
61 readonly schemaPath: string;
62 readonly inputSchema: Record<string, unknown>;
63 }
64 | {
65 readonly slug: string;
66 readonly successful: false;
67 readonly error: string;
68 };
69
70 export const parseParallelExecuteArgs = (
71 args: ReadonlyArray<string>,
72 config: {
73 readonly surface: 'root' | 'dev';
74 readonly projectMode: 'consumer' | 'developer';
75 readonly allowUserId: boolean;
76 readonly allowProjectName: boolean;
77 }
78 ) =>
79 Effect.gen(function* () {
80 let getSchema = false;
81 let dryRun = false;
82 let skipConnectionCheck = false;
83 let skipToolParamsCheck = false;
84 let skipChecks = false;
85 let account = Option.none<string>();
86 let userId = Option.none<string>();
87 let projectName = Option.none<string>();
88 const specs: ParallelExecuteSpec[] = [];
89 let currentSpec: ParallelExecuteSpec | null = null;
90
91 const pushCurrentSpec = () => {
92 if (!currentSpec) return;
93 specs.push(currentSpec);
94 currentSpec = null;
95 };
96
97 const readValue = (token: string, index: number) => {
98 if (token.includes('=')) {
99 return Effect.succeed({
100 value: token.slice(token.indexOf('=') + 1),
101 nextIndex: index,
102 });
103 }
104
105 const next = args[index + 1];
106 if (!next) {
107 return Effect.fail(invalidArguments(`Missing value for ${token}.`));
108 }
109 return Effect.succeed({ value: next, nextIndex: index + 1 });
110 };
111
112 for (let i = 0; i < args.length; i += 1) {
113 const token = args[i];
114 if (!token) continue;
115
116 const flagName = token.split('=')[0];
117 if (
118 CLI_DEBUG_FLAG_NAMES.some(name => flagName === `--${name}` || flagName === `--no-${name}`)
119 )
120 continue;
121 if (flagName === '--log-level') {
122 if (!token.includes('=')) i += 1;
123 continue;
124 }
125 // The framework supplies these values; this parser only preserves tool/data grouping.
126 if (
127 [
128 'parallel',
129 'get-schema',
130 'dry-run',
131 'skip-connection-check',
132 'skip-tool-params-check',
133 'skip-checks',
134 ].some(name => flagName === `--${name}` || flagName === `--no-${name}`) &&
135 (token !== flagName || flagName.startsWith('--no-'))
136 )
137 continue;
138 if (token === '--parallel' || token === '-p') {
139 continue;
140 }
141 if (token === '--get-schema') {
142 getSchema = true;
143 continue;
144 }
145 if (token === '--dry-run') {
146 dryRun = true;
147 continue;
148 }
149 if (token === '--skip-connection-check') {
150 skipConnectionCheck = true;
151 continue;
152 }
153 if (token === '--skip-tool-params-check') {
154 skipToolParamsCheck = true;
155 continue;
156 }
157 if (token === '--skip-checks') {
158 skipChecks = true;
159 continue;
160 }
161 if (token === '--account' || token.startsWith('--account=')) {
162 const parsed = yield* readValue(token, i);
163 if (currentSpec) {
164 currentSpec = {
165 slug: currentSpec.slug,
166 data: currentSpec.data,
167 account: Option.some(parsed.value),
168 };
169 } else {
170 account = Option.some(parsed.value);
171 }
172 i = parsed.nextIndex;
173 continue;
174 }
175 if (token === '--user-id' || token.startsWith('--user-id=')) {
176 if (!config.allowUserId) {
177 return yield* Effect.fail(
178 invalidArguments(`${token} is not supported for this execute command.`)
179 );
180 }
181 const parsed = yield* readValue(token, i);
182 userId = Option.some(parsed.value);
183 i = parsed.nextIndex;
184 continue;
185 }
186 if (token === '--project-name' || token.startsWith('--project-name=')) {
187 if (!config.allowProjectName) {
188 return yield* Effect.fail(
189 invalidArguments(`${token} is not supported for this execute command.`)
190 );
191 }
192 const parsed = yield* readValue(token, i);
193 projectName = Option.some(parsed.value);
194 i = parsed.nextIndex;
195 continue;
196 }
197 if (
198 token === '--data' ||
199 token === '-d' ||
200 token.startsWith('--data=') ||
201 token.startsWith('-d=')
202 ) {
203 if (!currentSpec) {
204 return yield* Effect.fail(
205 invalidArguments(
206 `Expected a tool slug before ${token}. Use: composio execute --parallel TOOL_SLUG -d '{}' TOOL_SLUG_2 -d '{}'.`
207 )
208 );
209 }
210 const parsed = yield* readValue(token, i);
211 currentSpec = {
212 slug: currentSpec.slug,
213 data: Option.some(parsed.value),
214 account: currentSpec.account,
215 };
216 i = parsed.nextIndex;
217 continue;
218 }
219 if (token.startsWith('-')) {
220 return yield* Effect.fail(
221 invalidArguments(`Unknown option for parallel execute: ${token}`)
222 );
223 }
224
225 pushCurrentSpec();
226 currentSpec = {
227 slug: token,
228 data: Option.none(),
229 account,
230 };
231 }
232
233 pushCurrentSpec();
234
235 if (specs.length === 0) {
236 return yield* Effect.fail(
237 invalidArguments(
238 "No tool slugs were provided. Use: composio execute --parallel TOOL_SLUG -d '{}' TOOL_SLUG_2 -d '{}'."
239 )
240 );
241 }
242
243 return {
244 specs,
245 userId,
246 projectName,
247 surface: config.surface,
248 projectMode: config.projectMode,
249 getSchema,
250 dryRun,
251 skipConnectionCheck,
252 skipToolParamsCheck,
253 skipChecks,
254 account,
255 } satisfies ParsedParallelExecuteArgs;
256 });
257
258 const checkConnectedToolkitOrFail = (params: {
259 readonly slug: string;
260 readonly resolvedProject: ResolvedExecuteContext['resolvedProject'];
261 readonly resolvedUserId: string;
262 readonly skipConnectionCheck: boolean;
263 readonly skipChecks: boolean;
264 }) =>
265 Effect.gen(function* () {
266 if (params.skipConnectionCheck || params.skipChecks) return;
267 if (params.resolvedProject.projectType !== 'CONSUMER') return;
268
269 yield* refreshConsumerConnectedToolkitsCache({
270 orgId: params.resolvedProject.orgId,
271 consumerUserId: params.resolvedUserId,
272 }).pipe(
273 Effect.catch(() => Effect.void),
274 Effect.forkDetach,
275 Effect.asVoid
276 );
277
278 const toolkit = yield* toolkitFromToolSlug(
279 params.slug,
280 toolkitProjectScope(params.resolvedProject)
281 );
282 if (!toolkit) return;
283
284 const cachedToolkits = yield* getFreshConsumerConnectedToolkitsFromCache({
285 orgId: params.resolvedProject.orgId,
286 consumerUserId: params.resolvedUserId,
287 });
288
289 if (Option.isSome(cachedToolkits) && !cachedToolkits.value.includes(toolkit)) {
290 return yield* new ToolExecutionError({
291 reason: 'connection_check',
292 toolSlug: params.slug,
293 message: `Toolkit "${toolkit}" is not connected for this user (cached within the last 5 minutes). If you just connected the account, use --skip-connection-check.`,
294 });
295 }
296 });
297
298 const runParallelSchemaFetchFromParsed = (params: ParsedParallelExecuteArgs) =>
299 Effect.gen(function* () {
300 const contexts = yield* Effect.forEach(
301 params.specs,
302 () =>
303 resolveSchemaContext({
304 account: params.account,
305 projectName: params.projectName,
306 surface: params.surface,
307 projectMode: params.projectMode,
308 getSchema: params.getSchema,
309 dryRun: params.dryRun,
310 skipConnectionCheck: params.skipConnectionCheck,
311 skipToolParamsCheck: params.skipToolParamsCheck,
312 skipChecks: params.skipChecks,
313 }),
314 { concurrency: 'unbounded' }
315 );
316 const entries = contexts.map((context, index) => ({
317 context,
318 spec: params.specs[index]!,
319 }));
320
321 const ui = contexts[0]?.ui;
322 const results = yield* Effect.forEach(
323 entries,
324 ({ context, spec }) =>
325 Effect.gen(function* () {
326 const definition = yield* getOrFetchToolInputDefinition(spec.slug, {
327 orgId: context.resolvedProject.orgId,
328 projectId: context.resolvedProject.projectId,
329 });
330 return {
331 slug: spec.slug,
332 successful: true,
333 version: definition.version,
334 schemaPath: definition.schemaPath,
335 inputSchema: normalizeFileUploadSchema(definition.schema),
336 } satisfies Extract<
337 ParallelExecuteResult,
338 { readonly inputSchema: Record<string, unknown> }
339 >;
340 }).pipe(
341 Effect.catch(error =>
342 toolkitFromToolSlug(spec.slug, toolkitProjectScope(context.resolvedProject)).pipe(
343 Effect.map(toolkit => {
344 const mapped = mapComposioError({
345 error,
346 toolkit,
347 toolSlug: spec.slug,
348 tree: params.surface,
349 });
350 return {
351 slug: spec.slug,
352 successful: false,
353 error: mapped.message,
354 } satisfies Extract<ParallelExecuteResult, { readonly successful: false }>;
355 })
356 )
357 )
358 ),
359 { concurrency: 'unbounded' }
360 );
361
362 if (ui) {
363 for (const result of results) {
364 if (!result.successful) {
365 yield* ui.log.error(`[${result.slug}] ${escapeTerminalText(result.error)}`);
366 continue;
367 }
368
369 yield* ui.log.step(`[${result.slug}] Schema fetched: ${result.schemaPath}`);
370 }
371
372 const successful = results.every(result => result.successful);
373 yield* ui.log.message(
374 `Parallel execute completed: ${results.filter(result => result.successful).length}/${results.length} successful`
375 );
376 yield* writeExecuteStdout(
377 ui,
378 JSON.stringify(
379 {
380 successful,
381 parallel: true,
382 results,
383 },
384 ciRedactReplacer,
385 2
386 )
387 );
388 if (!successful) {
389 return yield* new ReportedToolExecutionError({
390 reason: 'parallel_failed',
391 message: 'One or more parallel tool executions failed.',
392 });
393 }
394 }
395 });
396
397 const runParallelToolsExecuteFromParsed = (params: ParsedParallelExecuteArgs) =>
398 Effect.gen(function* () {
399 if (params.getSchema) {
400 return yield* runParallelSchemaFetchFromParsed(params);
401 }
402
403 // Resolved once, so a test user's notice prints once; each tool still fails on its own
404 // input before it fails on a missing user.
405 const user = Effect.fromResult(yield* Effect.result(actingUser()));
406 const contexts = yield* Effect.forEach(
407 params.specs,
408 spec =>
409 resolveExecuteContext(
410 {
411 slug: spec.slug,
412 data: spec.data,
413 file: Option.none(),
414 account: spec.account,
415 projectName: params.projectName,
416 surface: params.surface,
417 projectMode: params.projectMode,
418 getSchema: params.getSchema,
419 dryRun: params.dryRun,
420 skipConnectionCheck: params.skipConnectionCheck,
421 skipToolParamsCheck: params.skipToolParamsCheck,
422 skipChecks: params.skipChecks,
423 },
424 user
425 ),
426 { concurrency: 'unbounded' }
427 );
428 const entries = contexts.map((context, index) => ({
429 context,
430 spec: params.specs[index]!,
431 }));
432
433 const ui = contexts[0]?.ui;
434 const results = yield* Effect.forEach(
435 entries,
436 ({ context, spec }) =>
437 Effect.gen(function* () {
438 const toolSlug = spec.slug;
439
440 yield* checkConnectedToolkitOrFail({
441 slug: toolSlug,
442 resolvedProject: context.resolvedProject,
443 resolvedUserId: context.resolvedUserId,
444 skipConnectionCheck: params.skipConnectionCheck,
445 skipChecks: params.skipChecks,
446 });
447
448 if (params.dryRun) {
449 const definition = yield* checkDryRunArguments({
450 slug: toolSlug,
451 args: context.args,
452 projectScope: {
453 orgId: context.resolvedProject.orgId,
454 projectId: context.resolvedProject.projectId,
455 },
456 verificationDisabled: params.skipChecks || params.skipToolParamsCheck,
457 });
458 return {
459 successful: true,
460 dryRun: true,
461 slug: toolSlug,
462 arguments: context.args,
463 userId: context.resolvedUserId,
464 schemaPath: definition?.schemaPath,
465 schemaVersion: definition?.version,
466 } satisfies DryRunSummary;
467 }
468
469 if (!params.skipChecks && !params.skipToolParamsCheck) {
470 const definition = yield* getOrFetchToolInputDefinition(toolSlug, {
471 orgId: context.resolvedProject.orgId,
472 projectId: context.resolvedProject.projectId,
473 });
474 yield* validateToolInputArgumentsWithDefinition(toolSlug, context.args, definition);
475 }
476
477 const result = yield* context.executor.execute(toolSlug, context.executeParams);
478 if (!result.successful) {
479 const failed = {
480 slug: toolSlug,
481 successful: false as const,
482 error: result.error ?? 'Execution failed.',
483 logId: result.logId,
484 };
485 if (!result.instantCharge) return failed;
486 return { ...failed, instantCharge: result.instantCharge };
487 }
488
489 const output = yield* prepareExecuteOutput(toolSlug, result, context.executeOutputDir);
490 if (output.kind === 'file') {
491 return {
492 slug: toolSlug,
493 ...output.summary,
494 };
495 }
496
497 return {
498 slug: toolSlug,
499 ...result,
500 };
501 }).pipe(
502 Effect.catch(error =>
503 toolkitFromToolSlug(spec.slug, toolkitProjectScope(context.resolvedProject)).pipe(
504 Effect.map(toolkit => {
505 const mapped = mapComposioError({
506 error,
507 toolkit,
508 toolSlug: spec.slug,
509 tree: params.surface,
510 });
511 return {
512 slug: spec.slug,
513 successful: false,
514 error: mapped.message,
515 } satisfies ParallelExecuteResult;
516 })
517 )
518 )
519 ),
520 { concurrency: 'unbounded' }
521 );
522
523 if (ui) {
524 for (const result of results) {
525 if (!result.successful) {
526 const logId =
527 'logId' in result && typeof result.logId === 'string' && result.logId.length > 0
528 ? ` (logId: ${redact({ value: result.logId, prefix: 'log_' })})`
529 : '';
530 yield* ui.log.error(`[${result.slug}] ${escapeTerminalText(`${result.error}${logId}`)}`);
531 continue;
532 }
533
534 if ('dryRun' in result && result.dryRun) {
535 yield* ui.log.step(`[${result.slug}] Dry run successful`);
536 continue;
537 }
538
539 if ('inputSchema' in result) {
540 yield* ui.log.step(`[${result.slug}] Schema fetched: ${result.schemaPath}`);
541 continue;
542 }
543
544 if ('storedInFile' in result && result.storedInFile) {
545 yield* ui.log.step(
546 `[${result.slug}] Response stored in ${result.outputFilePath} (${describeStoredOutputSize(result)})`
547 );
548 continue;
549 }
550
551 yield* ui.log.step(
552 `[${result.slug}] Execution successful${executionSuccessSuffix(result)}`
553 );
554 if ('data' in result) {
555 yield* ui.note(
556 escapeTerminalLines(serializeExecuteOutput(result)),
557 `Response: ${result.slug}`
558 );
559 }
560 }
561 }
562
563 const successful = results.every(result => result.successful);
564 if (ui) {
565 yield* ui.log.message(
566 `Parallel execute completed: ${results.filter(result => result.successful).length}/${results.length} successful`
567 );
568 yield* writeExecuteStdout(
569 ui,
570 JSON.stringify(
571 {
572 successful,
573 parallel: true,
574 results,
575 },
576 ciRedactReplacer,
577 2
578 )
579 );
580 }
581
582 if (!successful) {
583 return yield* new ReportedToolExecutionError({
584 reason: 'parallel_failed',
585 message: 'One or more parallel tool executions failed.',
586 });
587 }
588 });
589
590 // Flags that can precede the command name and take the next token as their value
591 // (`$default.cmd.ts`, `dev.cmd.ts`), so a value equal to the command name is not the command.
592 const VALUE_FLAGS_BEFORE_COMMAND = new Set([
593 '--log-level',
594 '--mode',
595 '--user-id',
596 '--project-name',
597 ]);
598
599 /** The index of the command name in argv, skipping a flag value that spells the same word. */
600 const commandNameIndex = (argv: ReadonlyArray<string>, name: string) =>
601 argv.findIndex(
602 (token, index) => token === name && !VALUE_FLAGS_BEFORE_COMMAND.has(argv[index - 1] ?? '')
603 );
604
605 export const runParallelFromCommand = (config: {
606 readonly surface: 'root' | 'dev';
607 readonly projectMode: 'consumer' | 'developer';
608 readonly getSchema: boolean;
609 readonly dryRun: boolean;
610 readonly skipConnectionCheck: boolean;
611 readonly skipToolParamsCheck: boolean;
612 readonly skipChecks: boolean;
613 }) =>
614 Effect.gen(function* () {
615 const argv = yield* ExecuteInvocationArgs;
616 const name = config.surface === 'root' ? 'execute' : 'playground-execute';
617 const tail = argv.slice(commandNameIndex(argv, name) + 1);
618 const parsed = yield* parseParallelExecuteArgs(tail, {
619 ...config,
620 allowUserId: config.surface === 'dev',
621 allowProjectName: config.surface === 'dev',
622 });
623 // The tail starts after the command name, so the `dev` group's shared flags given before
624 // it reach the command only through the surface; the tail's own win.
625 const surface = yield* CommandSurface;
626 const userId = Option.orElse(parsed.userId, () => surface.userId);
627 const projectName = Option.orElse(parsed.projectName, () => surface.projectName);
628 return yield* runParallelToolsExecuteFromParsed({
629 ...parsed,
630 ...config,
631 userId,
632 projectName,
633 }).pipe(
634 Effect.provideService(CommandSurface, CommandSurface.of({ ...surface, userId, projectName }))
635 );
636 });