Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | /** |
| 2 | * Runs one invocation (`src/invocation/invocation.ts`) as the process: over the production |
| 3 | * services (`InvocationLive`), under the CLI's own runner. The invocation takes the full argv; |
| 4 | * `runWithConfig` removes the executable prefix before `Command.runWith` parses it. |
| 5 | * |
| 6 | * Exit codes come from Effect's runtime markers, never `process.exitCode`: a failure that is |
| 7 | * already on screen (ShowHelp, a command's own report, or a message the invocation printed) |
| 8 | * reaches the runner with `[Runtime.errorReported] = false` (`forRunner`), so the runner logs |
| 9 | * nothing, and `Runtime.defaultTeardown` exits with its `[Runtime.errorExitCode]` (1 when unset), |
| 10 | * or 0 on success. Signals are the runner's (`src/run-main.ts`): a command a signal cancelled |
| 11 | * exits 128 + its number. |
| 12 | */ |
| 13 | import { ConfigProvider, Effect } from 'effect'; |
| 14 | import { forRunner, runInvocation } from 'src/invocation/invocation'; |
| 15 | import { InvocationLive } from 'src/invocation/live'; |
| 16 | import { readTelemetryDebugOverride, telemetryDebugModeLayer } from 'src/services/runtime-flags'; |
| 17 | import { normalizeRunScriptArgs } from 'src/commands/argv-compat'; |
| 18 | import { makeShutdownController, ShutdownSignal } from 'src/services/shutdown-signal'; |
| 19 | import { makeCliRunMain } from 'src/run-main'; |
| 20 | |
| 21 | /** |
| 22 | * Values `src/bin.ts` resolved before the Effect runtime existed and hands to it here. |
| 23 | */ |
| 24 | export type CliBootstrapOptions = { |
| 25 | /** The full, unchanged process argv (executable and script included). */ |
| 26 | readonly argv: ReadonlyArray<string>; |
| 27 | }; |
| 28 | |
| 29 | export const runCli = (options: CliBootstrapOptions): void => { |
| 30 | const debug = readTelemetryDebugOverride(normalizeRunScriptArgs(options.argv)); |
| 31 | // One per invocation: the runner reports the process's signals to the command through it. |
| 32 | const shutdown = makeShutdownController(); |
| 33 | runInvocation(options.argv).pipe( |
| 34 | forRunner, |
| 35 | Effect.provide(InvocationLive), |
| 36 | // Built per invocation, never at module scope: `ConfigProvider.fromEnv()` snapshots |
| 37 | // `process.env` when constructed, and the ambient `ConfigProvider` reference caches its |
| 38 | // default snapshot process-wide on first use. A fresh provider here means every config read |
| 39 | // of this invocation observes the environment as it is now (`vi.stubEnv` in tests, any |
| 40 | // in-process mutation before `runCli`). |
| 41 | Effect.provide(ConfigProvider.layer(ConfigProvider.fromEnv())), |
| 42 | Effect.provideService(ShutdownSignal, shutdown.service), |
| 43 | effect => |
| 44 | debug === undefined ? effect : Effect.provide(effect, telemetryDebugModeLayer(debug)), |
| 45 | makeCliRunMain(shutdown)() |
| 46 | ); |
| 47 | }; |