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
| 1 | import { Deferred, Effect, FiberSet } from 'effect'; |
| 2 | import { reportFailure } from 'src/effects/report-failure'; |
| 3 | import type { TerminalUI } from 'src/services/terminal-ui'; |
| 4 | import { escapeTerminalText } from 'src/ui/escape-terminal-text'; |
| 5 | |
| 6 | /** |
| 7 | * On a normal stop (`--max-events`, `--timeout`), lets the events the listener already accepted |
| 8 | * finish saving or forwarding, for up to 10 seconds, so the summary counts how they ended. A |
| 9 | * `--fail-fast` failure (`failed`) ends the wait early. The listener's scope then interrupts |
| 10 | * whatever is still running. |
| 11 | */ |
| 12 | export const finishAcceptedEvents = (events: FiberSet.FiberSet, failed: Deferred.Deferred<void>) => |
| 13 | Effect.raceFirst(FiberSet.awaitEmpty(events), Deferred.await(failed)).pipe( |
| 14 | Effect.timeoutOption('10 seconds'), |
| 15 | Effect.asVoid |
| 16 | ); |
| 17 | |
| 18 | /** `1 event`, `2 events`. */ |
| 19 | export const eventCount = (count: number) => `${count} event${count === 1 ? '' : 's'}`; |
| 20 | |
| 21 | /** |
| 22 | * Counts the events a listener sink (saved payloads, an output file, a forwarding URL) failed to |
| 23 | * take. One failure never fails the listener on its own: `record` logs a warn, and with |
| 24 | * `--fail-fast` it also runs `stop` and ends the event, so no later sink takes it. `report` |
| 25 | * sums the count up on stderr when the listener stops, as a plain line because piped callers |
| 26 | * see none of the `ui.log.*` decoration; with `--fail-fast` it goes through `reportFailure` and |
| 27 | * the CLI exits 1. |
| 28 | */ |
| 29 | export const makeSinkFailures = (params: { |
| 30 | readonly ui: TerminalUI; |
| 31 | readonly failFast: boolean; |
| 32 | readonly stop: Effect.Effect<void>; |
| 33 | /** The summary line, e.g. `2 events failed to write while listening.` */ |
| 34 | readonly summary: (count: number) => string; |
| 35 | }) => { |
| 36 | let count = 0; |
| 37 | return { |
| 38 | record: (message: string) => |
| 39 | Effect.gen(function* () { |
| 40 | count += 1; |
| 41 | // The message can quote a remote error or an event-derived path. |
| 42 | yield* params.ui.log.warn(escapeTerminalText(message)); |
| 43 | if (params.failFast) { |
| 44 | yield* params.stop; |
| 45 | return yield* Effect.interrupt; |
| 46 | } |
| 47 | }), |
| 48 | report: Effect.suspend(() => { |
| 49 | if (count === 0) { |
| 50 | return Effect.void; |
| 51 | } |
| 52 | const summary = params.summary(count); |
| 53 | return params.failFast ? reportFailure(params.ui, summary) : params.ui.error(summary); |
| 54 | }), |
| 55 | }; |
| 56 | }; |