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 92 lines 3.12 KB
Raw
1 // eslint-disable-next-line no-restricted-imports -- This Effect helper is the sole detached-spawn boundary for CLI source: @effect/platform Command processes are scope-bound and are killed when their scope closes, while these children must outlive the CLI process.
2 import { spawn } from 'node:child_process';
3 import process from 'node:process';
4 import * as FileSystem from 'effect/FileSystem';
5 import { Data, Effect } from 'effect';
6
7 export class DetachedProcessSpawnError extends Data.TaggedError(
8 'services/DetachedProcessSpawnError'
9 )<{
10 readonly command: string;
11 readonly cause: unknown;
12 }> {}
13
14 /**
15 * Spawns a process that outlives the CLI: detached from the CLI's process
16 * group so terminal signals never reach it, and unref'd so the CLI event loop
17 * never waits on it. All stdio is ignored unless stderr passthrough is
18 * requested (telemetry debug output).
19 */
20 export const spawnDetached = (
21 command: string,
22 args: ReadonlyArray<string>,
23 options?: { readonly inheritStderr?: boolean }
24 ): Effect.Effect<void, DetachedProcessSpawnError> =>
25 Effect.try({
26 try: () =>
27 spawn(command, [...args], {
28 detached: true,
29 stdio: options?.inheritStderr === true ? ['ignore', 'ignore', 'inherit'] : 'ignore',
30 // Leaving `env` unset makes the child inherit the complete parent environment.
31 }),
32 catch: cause => new DetachedProcessSpawnError({ command, cause }),
33 }).pipe(
34 Effect.flatMap(child =>
35 Effect.callback<void, DetachedProcessSpawnError>(resume => {
36 let settled = false;
37
38 const cleanup = () => {
39 child.removeListener('spawn', onSpawn);
40 child.removeListener('error', onError);
41 };
42
43 const settle = (result: Effect.Effect<void, DetachedProcessSpawnError>) => {
44 if (settled) {
45 return;
46 }
47
48 settled = true;
49 cleanup();
50 resume(result);
51 };
52
53 const onSpawn = () => {
54 child.unref();
55 settle(Effect.void);
56 };
57
58 const onError = (cause: Error) => {
59 settle(Effect.fail(new DetachedProcessSpawnError({ command, cause })));
60 };
61
62 child.once('spawn', onSpawn);
63 child.once('error', onError);
64 })
65 )
66 );
67
68 /**
69 * Resolve the executable/argument shape for a detached worker in both compiled
70 * binaries and source-driven development.
71 */
72 export const getDetachedWorkerSpawnArgs = (workerFlag: string, encodedPayload: string) =>
73 Effect.gen(function* () {
74 const fs = yield* FileSystem.FileSystem;
75 const maybeScriptPath = process.argv[1];
76 const scriptPathExists =
77 typeof maybeScriptPath === 'string' && maybeScriptPath.length > 0
78 ? yield* fs.exists(maybeScriptPath).pipe(Effect.catch(() => Effect.succeed(false)))
79 : false;
80 const scriptPathLooksReal =
81 scriptPathExists && /\.(?:[cm]?[jt]s|mjs|mts|cts)$/u.test(maybeScriptPath ?? '');
82
83 return scriptPathLooksReal
84 ? {
85 command: process.execPath,
86 args: [maybeScriptPath as string, workerFlag, encodedPayload],
87 }
88 : {
89 command: process.execPath,
90 args: [workerFlag, encodedPayload],
91 };
92 });