@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 499 lines 18.1 KB
Raw
1 import { CommandInputError } from 'src/commands/errors';
2 import process from 'node:process';
3 import * as FileSystem from 'effect/FileSystem';
4 import * as Path from 'effect/Path';
5 import { ChildProcess, ChildProcessSpawner } from 'effect/process';
6 import { Duration, Effect, Option, Ref, Result } from 'effect';
7 import { APP_VERSION } from 'src/constants';
8 import { ChildProcessExited, ChildProcessSignaled } from 'src/effects/child-process-exited';
9 import { childExitStatus, type ChildExitStatus } from 'src/effects/child-exit-status';
10 import {
11 STOP_SIGNALS,
12 ShutdownSignal,
13 type ShutdownSignalShape,
14 type StopSignal,
15 type TerminationSignal,
16 } from 'src/services/shutdown-signal';
17 import { loadGenerationRuntime } from 'src/effects/generation-runtime';
18 import { actingUser } from 'src/commands/surface';
19 import { type RunHelperContext } from 'src/services/run-helpers-runtime';
20 import { warmToolInputDefinitions } from 'src/services/tool-input-validation';
21 import { ComposioUserContext } from 'src/services/user-context';
22 import { resolveConsumerExecutionTransport } from 'src/services/dashboard-tool-execution';
23 import {
24 debugFlagsToChildEnv,
25 isPerfDebugEnabled,
26 isTelemetryDebugEnabled,
27 isToolDebugEnabled,
28 } from 'src/services/runtime-flags';
29 import { cliInvocationContext } from 'src/services/runtime-cli-context';
30 import { invocationRunId } from 'src/invocation/command-identity';
31 import {
32 repairMissingInstalledRunCompanionModules,
33 resolveRunCompanionModulePath,
34 isBunVirtualPath,
35 } from 'src/services/run-companion-modules';
36 import {
37 appendCliSessionHistory,
38 resolveCliSessionArtifacts,
39 } from 'src/services/cli-session-artifacts';
40 import { TerminalUI } from 'src/services/terminal-ui';
41 import { NodeOs } from 'src/services/node-os';
42 import { ChildSignalError } from './run.errors';
43
44 const withArgDelimiter = (args: ReadonlyArray<string>) => (args.length > 0 ? ['--', ...args] : []);
45
46 /**
47 * The source rewrites need the TypeScript compiler, which ships in the
48 * `generation-runtime` companion module next to the executable rather than in
49 * the executable itself. Loading it here keeps it off every other command's
50 * startup path. A missing companion goes through the same self-repair as the
51 * `run-*` modules, and its error surfaces exactly as theirs does below.
52 */
53 const loadSourceTransforms = loadGenerationRuntime.pipe(
54 Effect.mapError(error => new Error(error.message))
55 );
56
57 export const inferCliInvocationPrefix = (
58 path: Path.Path,
59 argv: ReadonlyArray<string> = process.argv
60 ) =>
61 Effect.gen(function* () {
62 const entrypoint = argv[1];
63 if (!entrypoint) {
64 return [process.execPath];
65 }
66
67 // Compiled Bun binaries report an internal $bunfs entrypoint which cannot be
68 // re-executed as a real filesystem path. In that case the binary itself is
69 // the CLI entrypoint.
70 if (isBunVirtualPath(entrypoint)) {
71 return [process.execPath];
72 }
73
74 const fs = yield* FileSystem.FileSystem;
75 const resolvedEntrypoint = path.resolve(entrypoint);
76 return (yield* fs.exists(resolvedEntrypoint))
77 ? [process.execPath, resolvedEntrypoint]
78 : [process.execPath];
79 });
80
81 type RunHelperModuleUrls = {
82 readonly helpersRuntimeModuleUrl: string;
83 };
84
85 const resolveRunHelperModuleUrls: Effect.Effect<
86 RunHelperModuleUrls,
87 never,
88 FileSystem.FileSystem | Path.Path
89 > = Effect.gen(function* () {
90 const path = yield* Path.Path;
91 const modulePath = yield* resolveRunCompanionModulePath({
92 callerImportMetaUrl: import.meta.url,
93 execPath: process.execPath,
94 relativeNoExtensionFromCaller: '../../services/run-helpers-runtime',
95 });
96 const moduleUrl = yield* Effect.orDie(path.toFileUrl(modulePath));
97 return { helpersRuntimeModuleUrl: moduleUrl.href };
98 });
99 export const buildRunHelpersSource = (
100 cliPrefix: ReadonlyArray<string>,
101 context: RunHelperContext = {},
102 moduleUrls: RunHelperModuleUrls
103 ): string =>
104 [
105 `import { installRunHelpers } from ${JSON.stringify(moduleUrls.helpersRuntimeModuleUrl)};`,
106 '',
107 `await installRunHelpers(${JSON.stringify({ cliPrefix, helperContext: context })});`,
108 ].join('\n');
109
110 const createRunHelpersPreloadFile = (
111 path: Path.Path,
112 cliPrefix: ReadonlyArray<string>,
113 context: RunHelperContext,
114 moduleUrls: RunHelperModuleUrls
115 ) =>
116 Effect.gen(function* () {
117 const fs = yield* FileSystem.FileSystem;
118 const os = yield* NodeOs;
119 const directory = yield* fs.makeTempDirectoryScoped({
120 directory: os.tmpdir,
121 prefix: 'composio-run-',
122 });
123 const preloadPath = path.join(directory, 'globals.mjs');
124 // The preload directory is scoped and removed when the run ends, but the run log and any
125 // large tool outputs are advertised to the caller on stderr (`RUN_LOG_FILE=`) and must
126 // outlive the process that printed them, so they get their own unscoped directory.
127 const runOutputDir =
128 typeof context.runOutputDir === 'string' && context.runOutputDir.length > 0
129 ? context.runOutputDir
130 : yield* fs.makeTempDirectory({ directory: os.tmpdir, prefix: 'composio-run-artifacts-' });
131 const runLogFilePath = path.join(runOutputDir, 'run.log');
132 yield* fs.makeDirectory(runOutputDir, { recursive: true });
133 yield* fs.writeFileString(runLogFilePath, '');
134 yield* fs.writeFileString(
135 preloadPath,
136 buildRunHelpersSource(cliPrefix, { ...context, runOutputDir, runLogFilePath }, moduleUrls)
137 );
138 return { directory, preloadPath, runOutputDir, runLogFilePath };
139 });
140
141 /**
142 * `composio run` was given neither inline code nor `--file`.
143 *
144 * An ordinary usage mistake rather than a broken invariant, so it is a typed failure with a
145 * one-line message instead of a defect: the caller sees the fix, not a stack trace.
146 */
147 const MISSING_RUN_SOURCE_MESSAGE = [
148 'Provide inline code or use --file to run a script file.',
149 ` composio run 'console.log(1)'`,
150 ' composio run --file ./script.ts',
151 ].join('\n');
152
153 const missingRunSource = () =>
154 new CommandInputError({ command: 'run', message: MISSING_RUN_SOURCE_MESSAGE });
155
156 export const buildRunCommand = ({
157 path,
158 file,
159 args,
160 preloadPath,
161 preloadDirectory,
162 }: {
163 path: Path.Path;
164 file: Option.Option<string>;
165 args: ReadonlyArray<string>;
166 preloadPath: string;
167 preloadDirectory: string;
168 }) =>
169 Effect.gen(function* () {
170 const fs = yield* FileSystem.FileSystem;
171 const { wrapFileSourceForRun, wrapInlineCodeForRun } = yield* loadSourceTransforms;
172 // Use process.execPath directly — the child is spawned with BUN_BE_BUN=1
173 // which makes compiled Bun binaries act as a plain Bun runtime.
174 // Avoid the `run` subcommand entirely since Bun intercepts it as its own
175 // built-in; `bun --preload <file> <script>` works without it.
176 const base = [process.execPath, '--preload', preloadPath];
177 if (Option.isSome(file)) {
178 const filePath = path.resolve(file.value);
179 const wrapperFilePath = path.join(
180 path.dirname(filePath),
181 `.composio-run-${path.basename(preloadDirectory)}${path.extname(filePath) || '.ts'}`
182 );
183 yield* fs.writeFileString(
184 wrapperFilePath,
185 wrapFileSourceForRun(yield* fs.readFileString(filePath, 'utf8'))
186 );
187 return {
188 cmd: [...base, wrapperFilePath, ...withArgDelimiter(args)],
189 cleanupPaths: [wrapperFilePath],
190 };
191 }
192
193 const [inlineCode, ...scriptArgs] = args;
194 if (inlineCode) {
195 const wrappedInlineCode = [
196 '(async () => {',
197 wrapInlineCodeForRun(inlineCode),
198 '})().then((__composioResult) => {',
199 ' if (__composioResult !== undefined) {',
200 ' console.log(__composioResult);',
201 ' }',
202 '});',
203 ].join('\n');
204 return {
205 cmd: [...base, '--eval', wrappedInlineCode, ...withArgDelimiter(scriptArgs)],
206 cleanupPaths: [],
207 };
208 }
209
210 return yield* Effect.fail(missingRunSource());
211 });
212
213 const resolveRunHelperContext = () =>
214 Effect.gen(function* () {
215 const userContext = yield* ComposioUserContext;
216 const apiKey = Option.getOrUndefined(userContext.data.apiKey);
217 const orgId = Option.getOrUndefined(userContext.data.orgId);
218 // A spawned `composio execute` treats a forwarded web URL as an explicit
219 // choice of Dashboard. When this process keeps the backend transport (a
220 // custom backend with no web URL), forward none so the child decides alike.
221 const forwardWebURL = (yield* resolveConsumerExecutionTransport) === 'dashboard';
222 const baseContext = {
223 apiKey,
224 baseURL: userContext.data.baseURL,
225 ...(forwardWebURL ? { webURL: userContext.data.webURL } : {}),
226 orgId,
227 } satisfies RunHelperContext;
228
229 if (!apiKey || !orgId) {
230 return baseContext;
231 }
232
233 // Without a consumer user the script runs with the base context alone.
234 const consumer = yield* actingUser().pipe(Effect.option);
235 if (Option.isNone(consumer)) {
236 return baseContext;
237 }
238 const { userId: consumerUserId, project: consumerProject } = consumer.value;
239
240 const sessionArtifactsDir = Option.getOrUndefined(
241 yield* resolveCliSessionArtifacts({
242 orgId,
243 consumerUserId,
244 }).pipe(Effect.map(Option.map(artifacts => artifacts.directoryPath)))
245 );
246
247 return {
248 ...baseContext,
249 consumerUserId,
250 consumerProjectId: consumerProject.projectId,
251 consumerProjectName: consumerProject.projectName,
252 runOutputDir: sessionArtifactsDir,
253 } satisfies RunHelperContext;
254 });
255
256 /**
257 * How long the script gets to handle a forwarded signal before the CLI sends it SIGTERM, and
258 * again before SIGKILL.
259 */
260 const CHILD_SIGNAL_GRACE_PERIOD = Duration.seconds(2);
261
262 /** The first signal the CLI received, and the signals it then sent the script itself. */
263 interface Escalation {
264 readonly first: StopSignal;
265 readonly sent: ReadonlyArray<TerminationSignal>;
266 }
267
268 /**
269 * Sends `signal` to the child's process group.
270 *
271 * The `effect/process` spawner spawns with `detached: true` on POSIX, so the script leads its
272 * own process group and a negative pid is what reaches it (and anything it spawned). Delivery
273 * fails with ESRCH when the group is already gone, which is a normal race, not a run failure.
274 */
275 const signalChildProcessGroup = (pid: number, signal: TerminationSignal): void => {
276 Result.try({
277 try: () => process.kill(-pid, signal),
278 catch: cause => new ChildSignalError({ pid, signal, cause }),
279 });
280 };
281
282 /**
283 * Hands the CLI's first signal to the script, then makes sure the script ends: SIGTERM after a
284 * grace period, SIGKILL after another. The script decides how the run ends, as it would under a
285 * shell: one that handles SIGINT and exits 0 makes the run succeed.
286 *
287 * `escalation` records the first signal and each signal the CLI sends of its own, so a script
288 * that one of them killed is reported as cancelled by the first signal rather than by SIGTERM or
289 * SIGKILL.
290 */
291 const relayStopToChild = (
292 child: ChildProcessSpawner.ChildProcessHandle,
293 pid: number,
294 shutdown: ShutdownSignalShape,
295 escalation: Ref.Ref<Option.Option<Escalation>>
296 ) =>
297 Effect.gen(function* () {
298 const signal = yield* shutdown.requested;
299 signalChildProcessGroup(pid, signal);
300 const followUps: ReadonlyArray<TerminationSignal> =
301 signal === 'SIGTERM' ? ['SIGKILL'] : ['SIGTERM', 'SIGKILL'];
302 yield* Effect.forEach(
303 followUps,
304 (next, index) =>
305 Effect.sleep(CHILD_SIGNAL_GRACE_PERIOD).pipe(
306 Effect.andThen(child.isRunning),
307 Effect.flatMap(running =>
308 running
309 ? Ref.set(
310 escalation,
311 Option.some({ first: signal, sent: followUps.slice(0, index + 1) })
312 ).pipe(Effect.andThen(Effect.sync(() => signalChildProcessGroup(pid, next))))
313 : Effect.void
314 )
315 ),
316 { discard: true }
317 );
318 });
319
320 /**
321 * Waits for the script and reports how it ended, relaying the CLI's signals to it meanwhile.
322 *
323 * A terminal Ctrl-C only reaches the CLI's process group, so without this the script never
324 * observes SIGINT and its `process.on('SIGINT')` cleanup never runs. The run cooperates on every
325 * signal the runner handles (`ShutdownSignal`): the CLI is not interrupted, it forwards the
326 * signal and mirrors the script's exit. When the CLI's own SIGTERM or SIGKILL ended the script,
327 * the run ends by the first signal instead; a script that dies by any other signal (a SIGABRT in
328 * its cleanup, say) keeps that signal, so its crash stays visible. A second signal kills the
329 * script's process group and re-raises that signal.
330 */
331 const superviseChild = (child: ChildProcessSpawner.ChildProcessHandle) =>
332 Effect.gen(function* () {
333 const os = yield* NodeOs;
334 // Windows has no process groups and the executor does not detach there: a signal interrupts
335 // the run, and the spawner's finalizer ends the script.
336 if (os.platform === 'win32') {
337 return yield* childExitStatus(child);
338 }
339
340 const shutdown = yield* ShutdownSignal;
341 const pid = Number(child.pid);
342 yield* shutdown.cooperate({
343 signals: STOP_SIGNALS,
344 onForce: () => signalChildProcessGroup(pid, 'SIGKILL'),
345 reraiseOnForce: true,
346 });
347 const escalation = yield* Ref.make(Option.none<Escalation>());
348 yield* Effect.forkScoped(relayStopToChild(child, pid, shutdown, escalation));
349 const status = yield* childExitStatus(child);
350 return Option.match(yield* Ref.get(escalation), {
351 onNone: (): ChildExitStatus => status,
352 onSome: ({ first, sent }): ChildExitStatus =>
353 status.signal !== null && sent.includes(status.signal)
354 ? { code: null, signal: first }
355 : status,
356 });
357 });
358
359 /** The flags and arguments of `composio run`. */
360 export type RunParams = {
361 readonly file: Option.Option<string>;
362 readonly dryRun: boolean;
363 readonly debug: boolean;
364 readonly skipConnectionCheck: boolean;
365 readonly skipToolParamsCheck: boolean;
366 readonly skipChecks: boolean;
367 readonly args: ReadonlyArray<string>;
368 };
369
370 /**
371 * Runs inline code or a script file in a child Bun process with the Composio helpers preloaded,
372 * and ends the way the script did.
373 */
374 export const runScript = ({
375 file,
376 dryRun,
377 debug,
378 skipConnectionCheck,
379 skipToolParamsCheck,
380 skipChecks,
381 args,
382 }: RunParams) =>
383 Effect.gen(function* () {
384 // Checked before any setup work so a bare `composio run` neither creates a run-artifacts
385 // directory nor advertises a log file for a script that will never start.
386 if (Option.isNone(file) && !args[0]) {
387 return yield* Effect.fail(missingRunSource());
388 }
389
390 const fs = yield* FileSystem.FileSystem;
391 const path = yield* Path.Path;
392 const invocation = yield* cliInvocationContext;
393 const runId = Option.getOrElse(
394 yield* invocationRunId,
395 () => invocation.parentRunId ?? crypto.randomUUID()
396 );
397 const perfDebug = yield* isPerfDebugEnabled;
398 const toolDebug = yield* isToolDebugEnabled;
399 const telemetryDebug = yield* isTelemetryDebugEnabled;
400 if (Option.isNone(file)) {
401 const [inlineCode] = args;
402 const { extractInlineExecuteToolSlugs } = yield* loadSourceTransforms;
403 const preloadSlugs = extractInlineExecuteToolSlugs(inlineCode ?? '');
404 if (preloadSlugs.length > 0) {
405 yield* warmToolInputDefinitions(preloadSlugs).pipe(
406 Effect.catch(() => Effect.void),
407 Effect.forkDetach
408 );
409 }
410 }
411
412 const helperContext: RunHelperContext = {
413 ...(yield* resolveRunHelperContext()),
414 runId,
415 perfDebug,
416 toolDebug,
417 telemetryDebug,
418 debug,
419 dryRun,
420 skipConnectionCheck,
421 skipToolParamsCheck,
422 skipChecks,
423 };
424 const runHelperModuleUrls = yield* repairMissingInstalledRunCompanionModules({
425 callerImportMetaUrl: import.meta.url,
426 execPath: process.execPath,
427 appVersion: APP_VERSION,
428 }).pipe(
429 Effect.mapError(error => new Error(error.message)),
430 Effect.andThen(resolveRunHelperModuleUrls)
431 );
432 const cliPrefix = yield* inferCliInvocationPrefix(path);
433 const preload = yield* createRunHelpersPreloadFile(
434 path,
435 cliPrefix,
436 helperContext,
437 runHelperModuleUrls
438 );
439 const ui = yield* TerminalUI;
440 yield* appendCliSessionHistory({
441 orgId: helperContext.orgId,
442 consumerUserId: helperContext.consumerUserId,
443 entry: {
444 command: 'run',
445 status: 'start',
446 file: Option.getOrUndefined(file),
447 args,
448 debug,
449 },
450 }).pipe(Effect.catch(() => Effect.void));
451 yield* ui.error(`RUN_LOG_FILE=${preload.runLogFilePath}`);
452 const runCommand = yield* buildRunCommand({
453 path,
454 file,
455 args,
456 preloadPath: preload.preloadPath,
457 preloadDirectory: preload.directory,
458 });
459 const status = yield* Effect.gen(function* () {
460 const [executable, ...commandArgs] = runCommand.cmd;
461 // Spawning the command (instead of running it to completion) yields the handle whose
462 // pid the signal forwarding below needs. `extendEnv` keeps the caller's environment.
463 const child = yield* ChildProcess.make(executable!, commandArgs, {
464 env: {
465 BUN_BE_BUN: '1',
466 COMPOSIO_CLI_PARENT_RUN_ID: runId,
467 ...debugFlagsToChildEnv({ perfDebug, toolDebug, telemetryDebug }),
468 },
469 extendEnv: true,
470 stdin: 'inherit',
471 stdout: 'inherit',
472 stderr: 'inherit',
473 });
474 return yield* superviseChild(child);
475 }).pipe(
476 Effect.scoped,
477 Effect.ensuring(
478 Effect.forEach(
479 runCommand.cleanupPaths,
480 cleanupPath => fs.remove(cleanupPath, { force: true }),
481 { discard: true }
482 ).pipe(
483 // The wrapper file sits next to the user's script, so removal can fail on a
484 // read-only or locked directory. That must not turn an already-successful run
485 // into a failure, which is what the previous `Effect.orDie` did.
486 Effect.ignore
487 )
488 )
489 );
490 // The process ends the way the script did: with its code, or, when a signal ended it,
491 // by the same signal (the runner re-raises it), so wrappers and `set -e` scripts read a
492 // cancelled run as cancelled.
493 if (status.signal !== null) {
494 return yield* new ChildProcessSignaled({ signal: status.signal });
495 }
496 if (status.code !== 0) {
497 return yield* new ChildProcessExited({ code: status.code });
498 }
499 }).pipe(Effect.scoped);