Skip to content

Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.

2.5 KB
Raw Blame
/**
 * Turns a failed local-inference IPC result into an Error the UI can render.
 *
 * The main process reports a machine-readable `code` and `details`; the
 * translation keys live here, on the renderer side, so both surfaces that show
 * these failures — the notes toast and the dictation cleanup toast — get the
 * same wording for free.
 *
 * Before #2142 the raw llama.cpp 400 body was shown to the user verbatim.
 */

import { TRUNCATED_OUTPUT_MESSAGE_KEY } from "../services/ai/chatRequestBody";

export interface LocalInferenceFailure {
  error?: string;
  code?: string;
  details?: Record<string, unknown> | null;
}

export interface LocalInferenceError extends Error {
  code?: string;
  messageKey?: string;
  messageParams?: Record<string, string | number>;
}

const asPositiveInteger = (value: unknown): number | null =>
  typeof value === "number" && Number.isFinite(value) && value > 0 ? Math.round(value) : null;

function contextTooLarge(
  details: Record<string, unknown>
): Pick<LocalInferenceError, "messageKey" | "messageParams"> {
  const model = typeof details.modelName === "string" ? details.modelName : "";
  const needed = asPositiveInteger(details.neededTokens);
  const max = asPositiveInteger(details.maxContextTokens);

  if (needed === null || max === null) {
    return {
      messageKey: "models.errors.contextTooLargeGeneric",
      messageParams: { model },
    };
  }
  return {
    messageKey: "models.errors.contextTooLarge",
    messageParams: { model, needed, max },
  };
}

export function buildLocalInferenceError(result: LocalInferenceFailure): LocalInferenceError {
  const error: LocalInferenceError = new Error(result.error || "Local inference failed");
  if (result.code) error.code = result.code;

  const details = result.details ?? {};
  if (result.code === "CONTEXT_TOO_LARGE") {
    Object.assign(error, contextTooLarge(details));
  } else if (result.code === "LOCAL_SERVER_UNAVAILABLE") {
    // The llama.cpp startup dump stays in the main-process log; only the model
    // name crosses, so there is nothing here that could reach the user raw.
    error.messageKey = "models.errors.localServerUnavailable";
    error.messageParams = { model: typeof details.modelName === "string" ? details.modelName : "" };
  } else if (result.code === "LOCAL_MODEL_BUSY") {
    error.messageKey = "models.errors.localModelBusy";
  } else if (result.code === "OUTPUT_TRUNCATED") {
    // Same toast the cloud providers raise when a cleanup reply hits the token cap (#2091).
    error.messageKey = TRUNCATED_OUTPUT_MESSAGE_KEY;
  }

  return error;
}