The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import * as Cause from "effect/Cause"; |
| 2 | import * as CliError from "effect/cli/CliError"; |
| 3 | import * as Console from "effect/Console"; |
| 4 | import * as Data from "effect/Data"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as Runtime from "effect/Runtime"; |
| 7 | import * as Schema from "effect/Schema"; |
| 8 | import { isUserFacing, UserFacingError } from "../../UserFacingError.ts"; |
| 9 | import { |
| 10 | ANSI_DIM, |
| 11 | ANSI_RESET, |
| 12 | ansiFg, |
| 13 | colorsEnabled, |
| 14 | unicodeEnabled, |
| 15 | } from "../../Util/Terminal.ts"; |
| 16 | import { glyphsFor, theme } from "../../Util/Theme.ts"; |
| 17 | |
| 18 | const isTerminalCancelled = Schema.is( |
| 19 | Schema.Struct({ _tag: Schema.Literals(["TerminalCancelled"]) }), |
| 20 | ); |
| 21 | const isAuthErrorLike = Schema.is( |
| 22 | Schema.Struct({ |
| 23 | _tag: Schema.Literals(["AuthError"]), |
| 24 | cause: Schema.optional(Schema.Unknown), |
| 25 | }), |
| 26 | ); |
| 27 | |
| 28 | export const isPromptCancellation = (error: unknown): boolean => { |
| 29 | for (let current = error, depth = 0; current != null && depth < 16; depth++) { |
| 30 | if (isTerminalCancelled(current)) return true; |
| 31 | if (!isAuthErrorLike(current)) return false; |
| 32 | current = current.cause; |
| 33 | } |
| 34 | return false; |
| 35 | }; |
| 36 | |
| 37 | export const EXIT_CANCELLED = 130; |
| 38 | |
| 39 | export const setExitCode = (code: number) => |
| 40 | Effect.sync(() => { |
| 41 | process.exitCode = code; |
| 42 | }); |
| 43 | |
| 44 | export const exitDeclined = setExitCode(1); |
| 45 | |
| 46 | let interruptMessagesSuppressed = false; |
| 47 | export const suppressInterruptMessages = Effect.sync(() => { |
| 48 | interruptMessagesSuppressed = true; |
| 49 | }); |
| 50 | |
| 51 | const SHUTDOWN_FEEDBACK_DELAY_MS = 500; |
| 52 | |
| 53 | /** |
| 54 | * Keep quick shutdowns quiet, but acknowledge cleanup that takes long enough |
| 55 | * to be noticeable. The Effect runtime continues to own signal handling and |
| 56 | * teardown; this listener only writes delayed feedback. |
| 57 | */ |
| 58 | export const installShutdownFeedback = Effect.sync(() => { |
| 59 | let scheduled = false; |
| 60 | const onSignal = () => { |
| 61 | if (scheduled || interruptMessagesSuppressed) return; |
| 62 | scheduled = true; |
| 63 | const timer = setTimeout(() => { |
| 64 | process.stderr.write( |
| 65 | colorsEnabled() ? `\n${ANSI_DIM}Shutting down${ANSI_RESET}\n` : "\nShutting down\n", |
| 66 | ); |
| 67 | }, SHUTDOWN_FEEDBACK_DELAY_MS); |
| 68 | timer.unref(); |
| 69 | }; |
| 70 | process.on("SIGINT", onSignal); |
| 71 | process.on("SIGTERM", onSignal); |
| 72 | }); |
| 73 | |
| 74 | export const handleCancellation = <A, E, R>(self: Effect.Effect<A, E, R>) => |
| 75 | self.pipe( |
| 76 | Effect.catchCause((cause) => { |
| 77 | const cancelled = cause.reasons.some((reason) => { |
| 78 | if (Cause.isFailReason(reason)) return isPromptCancellation(reason.error); |
| 79 | if (Cause.isDieReason(reason)) return isPromptCancellation(reason.defect); |
| 80 | return false; |
| 81 | }); |
| 82 | return cancelled |
| 83 | ? Console.log( |
| 84 | colorsEnabled() ? `\n${ANSI_DIM}Cancelled.${ANSI_RESET}` : "\nCancelled.", |
| 85 | ).pipe(Effect.andThen(setExitCode(EXIT_CANCELLED))) |
| 86 | : (Effect.failCause(cause) as Effect.Effect<never, E, never>); |
| 87 | }), |
| 88 | Effect.onInterrupt(() => |
| 89 | interruptMessagesSuppressed |
| 90 | ? Effect.void |
| 91 | : Console.log(colorsEnabled() ? `\n${ANSI_DIM}Exited.${ANSI_RESET}` : "\nExited."), |
| 92 | ), |
| 93 | ); |
| 94 | |
| 95 | class ReportedCliError { |
| 96 | readonly [Runtime.errorReported] = false; |
| 97 | constructor(readonly cause: unknown) {} |
| 98 | } |
| 99 | |
| 100 | // `ConfigError` is effect's own class and cannot carry the `UserFacingError` |
| 101 | // marker. Everything else is matched by the marker alone — new user-facing |
| 102 | // errors opt in by assigning the symbol, not by growing this list. |
| 103 | const isUnmarkedUserFacingError = Schema.is( |
| 104 | Schema.Struct({ |
| 105 | _tag: Schema.Literals(["ConfigError"]), |
| 106 | message: Schema.String, |
| 107 | }), |
| 108 | ); |
| 109 | |
| 110 | const isUserFacingError = (error: unknown): error is { readonly message: string } => |
| 111 | isUserFacing(error) || isUnmarkedUserFacingError(error); |
| 112 | |
| 113 | export class UserInputError extends Data.TaggedError("UserInputError")<{ |
| 114 | readonly message: string; |
| 115 | }> { |
| 116 | readonly [UserFacingError] = true; |
| 117 | } |
| 118 | |
| 119 | export const handleUserErrors = <A, E, R>(self: Effect.Effect<A, E, R>) => |
| 120 | self.pipe( |
| 121 | Effect.catchCause((cause) => { |
| 122 | for (const reason of cause.reasons) { |
| 123 | const error = Cause.isFailReason(reason) |
| 124 | ? reason.error |
| 125 | : Cause.isDieReason(reason) |
| 126 | ? reason.defect |
| 127 | : undefined; |
| 128 | if (isUserFacingError(error)) { |
| 129 | const glyphs = glyphsFor(unicodeEnabled()); |
| 130 | return Console.error( |
| 131 | `${colorsEnabled() ? `${ansiFg(theme.color.danger)}${glyphs.error} error:${ANSI_RESET}` : "error:"} ${error.message}`, |
| 132 | ).pipe(Effect.flatMap(() => Effect.fail(new ReportedCliError(cause)))) as Effect.Effect< |
| 133 | never, |
| 134 | E | ReportedCliError, |
| 135 | never |
| 136 | >; |
| 137 | } |
| 138 | } |
| 139 | return Effect.failCause(cause) as Effect.Effect<never, E, never>; |
| 140 | }), |
| 141 | ); |
| 142 | |
| 143 | export const handleCliErrors = <A, E, R>(self: Effect.Effect<A, E, R>) => |
| 144 | self.pipe(handleCancellation, handleUserErrors); |
| 145 | |
| 146 | export const failWithHelp = (commandPath: ReadonlyArray<string>) => |
| 147 | setExitCode(1).pipe( |
| 148 | Effect.andThen( |
| 149 | Effect.fail(CliError.ShowHelp.make({ commandPath: [...commandPath], errors: [] })), |
| 150 | ), |
| 151 | ); |