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 60 lines 1.83 KB
Raw
1 import { Context, Effect, Layer, Stream } from 'effect';
2 import type * as PlatformError from 'effect/PlatformError';
3 import type { ChildProcess, ChildProcessSpawner } from 'effect/process';
4
5 export interface CommandResult {
6 readonly exitCode: number;
7 readonly stdout: string;
8 readonly stderr: string;
9 }
10
11 /** Drains a child-process output stream into one string; shared with the run-helpers runtime. */
12 export const collectText = <E>(stream: Stream.Stream<Uint8Array, E>) =>
13 Stream.mkString(Stream.decodeText(stream));
14
15 export interface CommandRunnerShape {
16 readonly run: (
17 command: ChildProcess.Command
18 ) => Effect.Effect<number, PlatformError.PlatformError, ChildProcessSpawner.ChildProcessSpawner>;
19 readonly capture: (
20 command: ChildProcess.Command
21 ) => Effect.Effect<
22 CommandResult,
23 PlatformError.PlatformError,
24 ChildProcessSpawner.ChildProcessSpawner
25 >;
26 }
27
28 const makeCommandRunner = Effect.sync((): CommandRunnerShape => ({
29 run: command =>
30 Effect.scoped(
31 Effect.gen(function* () {
32 const childProcess = yield* command;
33 return Number(yield* childProcess.exitCode);
34 })
35 ),
36 capture: command =>
37 Effect.scoped(
38 Effect.gen(function* () {
39 const childProcess = yield* command;
40 const [exitCode, stdout, stderr] = yield* Effect.all(
41 [
42 childProcess.exitCode,
43 collectText(childProcess.stdout),
44 collectText(childProcess.stderr),
45 ],
46 { concurrency: 'unbounded' }
47 );
48 return { exitCode: Number(exitCode), stdout, stderr } satisfies CommandResult;
49 })
50 ),
51 }));
52
53 export class CommandRunner extends Context.Service<CommandRunner, CommandRunnerShape>()(
54 'services/CommandRunner'
55 ) {
56 static readonly Default: Layer.Layer<CommandRunner> = Layer.effect(
57 CommandRunner,
58 makeCommandRunner
59 );
60 }