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 { Data, Effect, Runtime } from 'effect'; |
| 2 | import { TEMPORARY_FAILURE_EXIT_CODE } from 'src/effects/retry-advice'; |
| 3 | import type { TerminalUI } from 'src/services/terminal-ui'; |
| 4 | |
| 5 | /** |
| 6 | * A failure already shown to the user. It only makes the command fail: the runner does not |
| 7 | * log it (`[Runtime.errorReported]` is false) and `Runtime.defaultTeardown` exits 1, or |
| 8 | * `TEMPORARY_FAILURE_EXIT_CODE` (75) when it is `retryable`: running the same command again is |
| 9 | * safe and may pass (see `src/effects/retry-advice.ts`). |
| 10 | * |
| 11 | * The markers are getters, like `ExpectedFailure`, so they stay off the instance's own properties. |
| 12 | */ |
| 13 | export class ReportedFailure extends Data.TaggedError('effects/ReportedFailure')<{ |
| 14 | readonly message: string; |
| 15 | readonly retryable?: boolean; |
| 16 | }> { |
| 17 | get [Runtime.errorReported]() { |
| 18 | return false; |
| 19 | } |
| 20 | |
| 21 | get [Runtime.errorExitCode]() { |
| 22 | return this.retryable === true ? TEMPORARY_FAILURE_EXIT_CODE : 1; |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Prints `message`, then `hint` when there is one, on stderr. On a terminal they render as a |
| 28 | * decorated error and step; otherwise as plain lines, since `ui.log.*` decoration is dropped |
| 29 | * when stderr is not a TTY and scripts, CI and agents would see nothing. |
| 30 | * |
| 31 | * Use it directly only when the command prints more before it fails with a `ReportedFailure`; |
| 32 | * otherwise use `reportFailure`. |
| 33 | */ |
| 34 | export const printFailure = (ui: TerminalUI, message: string, hint?: string) => |
| 35 | Effect.gen(function* () { |
| 36 | const { canDecorate } = yield* ui.capabilities; |
| 37 | const [error, step] = canDecorate ? [ui.log.error, ui.log.step] : [ui.error, ui.error]; |
| 38 | yield* error(message); |
| 39 | if (hint !== undefined) { |
| 40 | yield* step(hint); |
| 41 | } |
| 42 | }); |
| 43 | |
| 44 | /** |
| 45 | * Reports a failure the command handles itself: prints it like `printFailure`, then fails with a |
| 46 | * `ReportedFailure`, so the process exits 1 (75 when `retryable`) without printing more. |
| 47 | * |
| 48 | * A usage mistake fails with a `CommandInputError` instead; see `src/commands/errors.ts`. |
| 49 | */ |
| 50 | export const reportFailure = ( |
| 51 | ui: TerminalUI, |
| 52 | message: string, |
| 53 | hint?: string, |
| 54 | options?: { readonly retryable?: boolean } |
| 55 | ): Effect.Effect<never, ReportedFailure> => |
| 56 | printFailure(ui, message, hint).pipe( |
| 57 | Effect.andThen( |
| 58 | Effect.fail( |
| 59 | new ReportedFailure( |
| 60 | options?.retryable === true ? { message, retryable: true } : { message } |
| 61 | ) |
| 62 | ) |
| 63 | ) |
| 64 | ); |