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 | import { Effect, Option, type PlatformError } from 'effect'; |
| 2 | import type { ChildProcessSpawner } from 'effect/process'; |
| 3 | import { isTerminationSignal, type TerminationSignal } from 'src/services/shutdown-signal'; |
| 4 | |
| 5 | /** How a child process ended: with an exit code, or killed by a signal. */ |
| 6 | export type ChildExitStatus = |
| 7 | | { readonly code: number; readonly signal: null } |
| 8 | | { readonly code: null; readonly signal: TerminationSignal }; |
| 9 | |
| 10 | /** |
| 11 | * The cause message `@effect/platform-node-shared`'s spawner fails `exitCode` with when a signal |
| 12 | * ended the child. The spawner has Node's `(code, signal)` pair but keeps only this message. |
| 13 | */ |
| 14 | const SIGNAL_DEATH = /^Process interrupted due to receipt of signal: '([A-Z0-9]+)'$/u; |
| 15 | |
| 16 | const signalOf = (error: PlatformError.PlatformError): Option.Option<TerminationSignal> => |
| 17 | error.reason.method === 'exitCode' && error.reason.cause instanceof Error |
| 18 | ? Option.fromNullishOr(SIGNAL_DEATH.exec(error.reason.cause.message)?.[1]).pipe( |
| 19 | Option.filter(isTerminationSignal) |
| 20 | ) |
| 21 | : Option.none(); |
| 22 | |
| 23 | /** |
| 24 | * Waits for `child` and reports its `{ code, signal }`, which `handle.exitCode` drops for a child |
| 25 | * a signal ended (it fails instead). A signal outside `TerminationSignal`, or any other failure, |
| 26 | * stays the spawner's `PlatformError`. |
| 27 | */ |
| 28 | export const childExitStatus = ( |
| 29 | child: ChildProcessSpawner.ChildProcessHandle |
| 30 | ): Effect.Effect<ChildExitStatus, PlatformError.PlatformError> => |
| 31 | child.exitCode.pipe( |
| 32 | Effect.map((code): ChildExitStatus => ({ code: Number(code), signal: null })), |
| 33 | Effect.catchTag('PlatformError', error => |
| 34 | Option.match(signalOf(error), { |
| 35 | onNone: () => Effect.fail(error), |
| 36 | onSome: signal => Effect.succeed<ChildExitStatus>({ code: null, signal }), |
| 37 | }) |
| 38 | ) |
| 39 | ); |