@devries-cameron20 / composio Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 04:45 UTC

next
ts 110 lines 4.04 KB
Raw
1 import { Duration, Effect, Option, Result } from 'effect';
2 import { resolveExplicitTextInput } from 'src/effects/resolve-optional-text-input';
3 import { Stdin } from 'src/services/stdin';
4 import {
5 findFileUploadablePaths,
6 schemaHasFileUploadable,
7 setArgumentAt,
8 } from 'src/services/tool-file-uploads';
9 import { isPlainRecord, parseJsonRecord } from 'src/utils/parse-json';
10 import { invalidArguments, ToolExecutionError } from './tools.execute.errors';
11
12 /**
13 * How long `composio execute` waits for piped JSON to start arriving when it got no `-d`. An
14 * agent harness can hand the CLI a pipe it never writes to or closes; waiting for its end would
15 * hang the command with no output.
16 */
17 export const PIPED_STDIN_SILENCE_TIMEOUT = Duration.seconds(1);
18
19 /**
20 * The tool arguments: `-d` as `resolveExplicitTextInput` reads it, else JSON piped on stdin, else
21 * `{}` when stdin is a terminal (e.g. tools with no required args).
22 */
23 export const resolveInput = (input: Option.Option<string>) =>
24 Effect.gen(function* () {
25 if (Option.isSome(input)) {
26 return (yield* resolveExplicitTextInput(input)) ?? '{}';
27 }
28 const stdin = yield* Stdin;
29 if (stdin.isTTY()) {
30 return '{}';
31 }
32 const piped = yield* stdin.readAllUnlessSilent(PIPED_STDIN_SILENCE_TIMEOUT);
33 if (Option.isNone(piped)) {
34 // Running the tool with `{}` could act without the arguments still on their way.
35 return yield* Effect.fail(
36 invalidArguments(
37 "No input arrived on stdin within 1 second. Pass the arguments with -d '{...}', or -d - to wait for stdin.",
38 '--data'
39 )
40 );
41 }
42 return piped.value;
43 });
44
45 export const parseArguments = (raw: string) =>
46 Effect.fromResult(
47 parseJsonRecord(raw).pipe(
48 Result.mapError(error =>
49 invalidArguments(
50 error.reason === 'not-a-record'
51 ? 'Expected a JSON object for tool arguments, e.g. -d \'{ "key": "value" }\''
52 : 'Invalid JSON input. Provide JSON or a JS-style object literal, e.g. -d \'{ "key": "value" }\''
53 )
54 )
55 )
56 );
57
58 const hasNestedKey = (
59 record: Record<string, unknown>,
60 pathParts: ReadonlyArray<string | number>
61 ): boolean => {
62 let current: unknown = record;
63 for (const key of pathParts) {
64 if (Array.isArray(current) && typeof key === 'number' && key < current.length) {
65 current = current[key];
66 } else if (isPlainRecord(current) && typeof key === 'string' && Object.hasOwn(current, key)) {
67 current = current[key];
68 } else return false;
69 }
70 return true;
71 };
72
73 export const injectSingleFileArgument = (params: {
74 readonly slug: string;
75 readonly args: Record<string, unknown>;
76 readonly filePath: string;
77 readonly schema: Record<string, unknown>;
78 }) =>
79 Effect.gen(function* () {
80 const uploadablePaths = findFileUploadablePaths(params.schema, [], params.args);
81
82 if (uploadablePaths.length === 0) {
83 return yield* new ToolExecutionError({
84 reason: 'file_input',
85 toolSlug: params.slug,
86 message: schemaHasFileUploadable(params.schema)
87 ? `Tool "${params.slug}" has no matching file_uploadable input in the supplied arguments. Supply the target array item with -d, or remove --file.`
88 : `Tool "${params.slug}" has no file_uploadable input. Remove --file or pass JSON via -d.`,
89 });
90 }
91
92 if (uploadablePaths.length > 1) {
93 return yield* new ToolExecutionError({
94 reason: 'file_input',
95 toolSlug: params.slug,
96 message: `Tool "${params.slug}" has multiple file_uploadable inputs (${uploadablePaths.map(parts => parts.join('.')).join(', ')}). Pass the target field explicitly with -d instead of --file.`,
97 });
98 }
99
100 const targetPath = uploadablePaths[0] ?? [];
101 if (hasNestedKey(params.args, targetPath)) {
102 return yield* new ToolExecutionError({
103 reason: 'file_input',
104 toolSlug: params.slug,
105 message: `Cannot use --file because "${targetPath.join('.')}" is already set in -d. Remove that field or omit --file.`,
106 });
107 }
108
109 return setArgumentAt(params.args, targetPath, params.filePath);
110 });