@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 130 lines 5.98 KB
Raw
1 import { telemetry, withCommandTelemetry } from 'src/analytics/command-input';
2 import { Argument, Command, Flag } from 'effect/cli';
3 import { CLI_DEBUG_FLAG_NAMES } from 'src/services/runtime-flags';
4 import { runScript } from './run.handler';
5
6 const RUN_FLAG_NAMES = {
7 file: 'file',
8 dryRun: 'dry-run',
9 debug: 'debug',
10 skipConnectionCheck: 'skip-connection-check',
11 skipToolParamsCheck: 'skip-tool-params-check',
12 skipChecks: 'skip-checks',
13 } as const;
14
15 const file = Flag.String(RUN_FLAG_NAMES.file).pipe(
16 Flag.withAlias('f'),
17 Flag.withDescription('Run a TS/JS file instead of inline code'),
18 Flag.optional
19 );
20
21 const dryRun = Flag.Boolean(RUN_FLAG_NAMES.dryRun).pipe(
22 Flag.withDescription('Preview execute() calls without running them'),
23 Flag.withDefault(false)
24 );
25 const debug = Flag.Boolean(RUN_FLAG_NAMES.debug).pipe(
26 Flag.withDescription('Log helper steps while the script runs'),
27 Flag.withDefault(false)
28 );
29 const skipConnectionCheck = Flag.Boolean(RUN_FLAG_NAMES.skipConnectionCheck).pipe(
30 Flag.withDescription('Skip the connected-account check'),
31 Flag.withDefault(false)
32 );
33 const skipToolParamsCheck = Flag.Boolean(RUN_FLAG_NAMES.skipToolParamsCheck).pipe(
34 Flag.withDescription('Skip input validation against cached schema'),
35 Flag.withDefault(false)
36 );
37 const skipChecks = Flag.Boolean(RUN_FLAG_NAMES.skipChecks).pipe(
38 Flag.withDescription('Skip both connection and input validation checks'),
39 Flag.withDefault(false)
40 );
41
42 const runFlags = {
43 file,
44 dryRun,
45 debug,
46 skipConnectionCheck,
47 skipToolParamsCheck,
48 skipChecks,
49 };
50
51 /** Share names with the passthrough adapter; the framework owns parsing the flag values. */
52 export const RUN_FILE_FLAGS = new Set([`--${RUN_FLAG_NAMES.file}`, '-f']);
53 export const RUN_KNOWN_VALUE_FLAGS = new Set([...RUN_FILE_FLAGS, '--log-level']);
54 export const RUN_KNOWN_BOOLEAN_FLAGS = new Set([
55 ...Object.values(RUN_FLAG_NAMES)
56 .filter(name => name !== RUN_FLAG_NAMES.file)
57 .map(name => `--${name}`),
58 ...CLI_DEBUG_FLAG_NAMES.flatMap(name => [`--${name}`, `--no-${name}`]),
59 '--help',
60 '-h',
61 '--version',
62 '-v',
63 ]);
64
65 const args = Argument.String('arg').pipe(
66 Argument.variadic(),
67 Argument.withDescription('Inline code followed by arguments, or just arguments when using --file')
68 );
69
70 const runCmdBase = Command.make(
71 'run',
72 {
73 ...runFlags,
74 args,
75 },
76 runScript
77 ).pipe(
78 Command.withDescription(
79 'Run inline TS/JS code or a file with injected Composio helpers that behave like their CLI counterparts.\n\nInjected helpers (behave like their CLI counterparts):\n execute(slug, data?) Same as `composio execute` — returns parsed JSON\n search(query, options?) Same as `composio search` — returns matching tools\n result.prompt() Prompt-safe serialization of a helper result\n const f = await proxy(toolkit) Same as `composio proxy` — returns a fetch function\n Example: const f = await proxy("gmail")\n const me = await f("https://gmail.googleapis.com/gmail/v1/users/me/profile")\n z Injected global from `zod` for defining and validating schemas\n\nAll helpers reuse your CLI auth state and connected accounts.\n\nUse composio search "<query>" to discover tools and composio execute <slug> --get-schema to inspect inputs.'
80 ),
81 Command.withShortDescription(
82 'Run inline TS/JS code or a file with injected Composio helpers that behave like their CLI counterparts.'
83 ),
84 Command.withExamples([
85 {
86 command:
87 'composio run \'\n // execute(slug, data?) — run a tool, returns parsed JSON\n const me = await execute("GITHUB_GET_THE_AUTHENTICATED_USER");\n console.log(me);\n\'',
88 },
89 {
90 command:
91 'composio run \'\n // search(query, opts?) — find tools by use case\n const tools = await search("send email");\n console.log(tools);\n\'',
92 },
93 {
94 command:
95 'composio run \'\n const issue = await execute("GITHUB_CREATE_ISSUE", { owner: "acme", repo: "app", title: "Deploy v2" });\n await execute("SLACK_SEND_MESSAGE", { channel: "eng", markdown_text: "Created: " + issue.data.html_url });\n\'',
96 description: 'Sequential: chain tool outputs across services',
97 },
98 {
99 command:
100 'composio run \'\n const [emails, issues, events] = await Promise.all([\n execute("GMAIL_FETCH_EMAILS", { max_results: 5 }),\n execute("GITHUB_LIST_REPOSITORY_ISSUES", { owner: "composiohq", repo: "composio", state: "open" }),\n execute("GOOGLECALENDAR_FIND_EVENT", { calendar_id: "primary" }),\n ]);\n console.log({ emails: emails.data, issues: issues.data, events: events.data });\n\'',
101 description: 'Parallel: fetch from multiple services at once with Promise.all',
102 },
103 {
104 command:
105 'composio run \'\n const issues = [101, 102, 103, 104];\n await Promise.all(issues.map(n =>\n execute("GITHUB_ADD_LABELS_TO_ISSUE", { owner: "acme", repo: "app", issue_number: n, labels: ["priority"] })\n ));\n\'',
106 description: 'Bulk: fan out with Promise.all + .map()',
107 },
108 {
109 command:
110 'composio run \'\n const f = await proxy("gmail");\n console.log(await f("https://gmail.googleapis.com/gmail/v1/users/me/profile"));\n\'',
111 description: 'proxy(toolkit) — returns a fetch() bound to your connected account',
112 },
113 {
114 command: 'composio run --file ./workflow.ts -- --repo acme/app',
115 description: 'Run from a file',
116 },
117 ])
118 );
119
120 export const runCmd = withCommandTelemetry(runCmdBase, input =>
121 telemetry.fields({
122 debug: telemetry.flag('--debug', input.debug),
123 dry_run: telemetry.flag('--dry-run', input.dryRun),
124 skip_connection_check: telemetry.flag('--skip-connection-check', input.skipConnectionCheck),
125 skip_tool_params_check: telemetry.flag('--skip-tool-params-check', input.skipToolParamsCheck),
126 skip_checks: telemetry.flag('--skip-checks', input.skipChecks),
127 file_mode: telemetry.flag('--file', telemetry.present(input.file)),
128 arg_count: telemetry.value(input.args?.length ?? 0),
129 })
130 );