@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 103 lines 3.67 KB
Raw
1 import { Console, Effect, Exit, Layer } from 'effect';
2 import { getTerminalCapabilities, TerminalUI } from 'src/services/terminal-ui';
3
4 /**
5 * Test layer for TerminalUI that routes all output through Effect's Console.
6 * Since tests use MockConsole (which intercepts Console), this ensures
7 * all terminal UI output is captured deterministically — no animations,
8 * no ANSI codes, no spinners.
9 */
10 export const terminalUITestImpl = TerminalUI.of({
11 capabilities: Effect.succeed(
12 getTerminalCapabilities({
13 stdin: { isTTY: false },
14 stdout: { isTTY: false },
15 stderr: { isTTY: false },
16 })
17 ),
18 // `output` is the real stdout *data* channel (`ui.output()`). It uses
19 // `Console.info` rather than `Console.log` deliberately: `src/commands/index.ts`'s
20 // `runWithConfig` provides a Console override for most invocations that
21 // redirects `.log` calls to `.error` (routing the framework's own decoration
22 // rendering to stderr — see its `runWithDecorationOnStderr`). That override
23 // only touches `.log`; using `.info` here keeps `ui.output()`'s test-visible
24 // channel immune to it, matching production, where `ui.output()` never goes
25 // through the Effect `Console` service at all and so is never affected.
26 output: data => Console.info(data),
27 stdoutClosed: Effect.never,
28 error: data => Console.error(data),
29
30 intro: title => Console.log(`-- ${title} --`),
31 outro: message => Console.log(`-- ${message} --`),
32
33 log: {
34 info: message => Console.log(message),
35 success: message => Console.log(message),
36 warn: message => Console.warn(message),
37 error: message => Console.error(message),
38 step: message => Console.log(message),
39 message: message => Console.log(message),
40 },
41
42 note: (message, title) => Console.log(title ? `[${title}] ${message}` : message),
43
44 select: (_message, options) => Effect.succeed(options[0].value),
45
46 confirm: (_message, options) => Effect.succeed(options?.defaultValue ?? true),
47
48 withSpinner: (message, effect, options) =>
49 Effect.gen(function* () {
50 const result = yield* effect;
51 const successMsg =
52 typeof options?.successMessage === 'function'
53 ? options.successMessage(result)
54 : (options?.successMessage ?? message);
55 yield* Console.log(successMsg);
56 return result;
57 }),
58
59 useMakeSpinner: (message, use) =>
60 Effect.gen(function* () {
61 let stopped = false;
62 const handle = {
63 message: (_msg: string) => Effect.void,
64 stop: (msg?: string) =>
65 Effect.gen(function* () {
66 stopped = true;
67 if (msg) yield* Console.log(msg);
68 }),
69 error: (msg?: string) =>
70 Effect.gen(function* () {
71 stopped = true;
72 if (msg) yield* Console.error(msg);
73 }),
74 };
75 const exit = yield* Effect.exit(use(handle));
76 if (Exit.isFailure(exit) && !stopped) {
77 yield* Console.error(message);
78 }
79 return yield* exit;
80 }),
81 });
82
83 export const TerminalUITest = Layer.succeed(TerminalUI, terminalUITestImpl);
84
85 /** An interactive terminal (all streams a TTY) whose confirmation prompt answers `confirmed`. */
86 export const terminalUIWithConfirm = (confirmed: boolean) =>
87 TerminalUI.of({
88 ...terminalUITestImpl,
89 capabilities: Effect.succeed(
90 getTerminalCapabilities({
91 stdin: { isTTY: true },
92 stdout: { isTTY: true },
93 stderr: { isTTY: true },
94 })
95 ),
96 confirm: () => Effect.succeed(confirmed),
97 });
98
99 /** An interactive terminal whose confirmation prompt must never be shown. */
100 export const terminalUIWithoutPrompt = TerminalUI.of({
101 ...terminalUIWithConfirm(false),
102 confirm: () => Effect.die('the confirmation prompt must not be shown'),
103 });