@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 107 lines 3.14 KB
Raw
1 import process from 'node:process';
2 import { Context, Duration, Effect, Layer, Option } from 'effect';
3
4 const MAX_STDIN_SIZE = 10 * 1024 * 1024; // 10 MB
5
6 export class StdinSizeLimitError extends Error {
7 constructor() {
8 super(`Stdin input exceeds maximum size (${MAX_STDIN_SIZE} bytes)`);
9 this.name = 'StdinSizeLimitError';
10 }
11 }
12
13 export interface Stdin {
14 readonly isTTY: () => boolean;
15 readonly readAll: () => Effect.Effect<string, Error>;
16 /**
17 * Reads stdin to its end, unless stdin stays silent (no data and no end of input) for
18 * `silenceTimeout`: then it stops listening and returns none. Once the first chunk or the end
19 * of input arrives, it waits for the rest without a timeout.
20 */
21 readonly readAllUnlessSilent: (
22 silenceTimeout: Duration.Duration
23 ) => Effect.Effect<Option.Option<string>, Error>;
24 }
25
26 export const Stdin = Context.Service<Stdin>('services/Stdin');
27
28 /**
29 * Reads a stdin stream to its end. Resolves `undefined` when `silenceTimeoutMs` passes before
30 * any data or the end of input arrives.
31 */
32 export const readStdinToEnd = (
33 input: NodeJS.ReadableStream,
34 silenceTimeoutMs?: number
35 ): Promise<string | undefined> =>
36 new Promise((resolve, reject) => {
37 const chunks: string[] = [];
38 let totalLength = 0;
39
40 const onData = (chunk: string) => {
41 clearTimeout(silenceTimer);
42 totalLength += chunk.length;
43 if (totalLength > MAX_STDIN_SIZE) {
44 cleanup();
45 reject(new StdinSizeLimitError());
46 return;
47 }
48 chunks.push(chunk);
49 };
50
51 const onEnd = () => {
52 cleanup();
53 resolve(chunks.join(''));
54 };
55
56 const onError = (error: Error) => {
57 cleanup();
58 reject(error);
59 };
60
61 const onSilence = () => {
62 cleanup();
63 // An agent harness or task runner can hand the CLI a pipe it never writes to or closes;
64 // waiting for its end would hang the command with no output.
65 input.pause();
66 resolve(undefined);
67 };
68
69 const cleanup = () => {
70 clearTimeout(silenceTimer);
71 input.off('data', onData);
72 input.off('end', onEnd);
73 input.off('error', onError);
74 };
75
76 const silenceTimer =
77 silenceTimeoutMs === undefined ? undefined : setTimeout(onSilence, silenceTimeoutMs);
78
79 input.setEncoding('utf8');
80 input.on('data', onData);
81 input.on('end', onEnd);
82 input.on('error', onError);
83 input.resume();
84 });
85
86 const toStdinError = (e: unknown) => (e instanceof Error ? e : new Error(String(e)));
87
88 /** A `Stdin` that reads `input`: `process.stdin` in `StdinLive`, a `PassThrough` in tests. */
89 export const makeStreamStdin = (input: NodeJS.ReadableStream, isTTY: () => boolean): Stdin =>
90 Stdin.of({
91 isTTY,
92 readAll: () =>
93 Effect.tryPromise({
94 try: () => readStdinToEnd(input).then(text => text ?? ''),
95 catch: toStdinError,
96 }),
97 readAllUnlessSilent: silenceTimeout =>
98 Effect.tryPromise({
99 try: () => readStdinToEnd(input, Duration.toMillis(silenceTimeout)),
100 catch: toStdinError,
101 }).pipe(Effect.map(Option.fromUndefinedOr)),
102 });
103
104 export const StdinLive = Layer.succeed(
105 Stdin,
106 makeStreamStdin(process.stdin, () => !!process.stdin.isTTY)
107 );