Skip to content

Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.

1.5 KB
Raw Blame
import * as Predicate from "effect/Predicate";
import * as Console from "effect/Console";
import * as Effect from "effect/Effect";
import * as Runtime from "effect/Runtime";

/**
 * Shared handling for the tagged errors a CLI command raises on purpose, as
 * opposed to defects: the message already tells the user what happened and how
 * to fix it, so the command prints only that to stderr and still fails. The
 * error is marked reported so `NodeRuntime.runMain` does not log it again with
 * a stack trace.
 *
 * Only errors whose `_tag` is on the caller's allowlist are handled this way.
 * Everything else — infrastructure failures, defects — falls through untouched
 * so `runMain` still prints it with its cause and stack.
 */
export const reportExpectedCliError =
  (expectedTags: ReadonlyArray<string>) =>
  <A, E, R>(effect: Effect.Effect<A, E, R>) =>
    Effect.tapError(effect, (error) => {
      if (
        !(error instanceof Error) ||
        !Predicate.hasProperty(error, "_tag") ||
        !Predicate.isString(error._tag) ||
        !expectedTags.includes(error._tag)
      ) {
        return Effect.void;
      }

      const lines =
        error.cause === undefined
          ? [error.message]
          : [error.message, `Caused by: ${String(error.cause)}`];

      return Console.error(lines.join("\n")).pipe(
        Effect.andThen(
          Effect.sync(() => {
            Object.assign(error, { [Runtime.errorReported]: false });
          }),
        ),
      );
    });