Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

1.1 KB
Raw Blame
import { sanitizeErrorMessage } from "./error.ts";

export const MAX_UPSTREAM_ERROR_MESSAGE_LENGTH = 512;

export function withUpstreamErrorDetail(message: string, upstreamDetails: unknown): string {
  const base = sanitizeErrorMessage(message) || "Upstream stream failed before completion.";
  const parts: string[] = [];
  if (upstreamDetails && typeof upstreamDetails === "object" && !Array.isArray(upstreamDetails)) {
    const details = upstreamDetails as Record<string, unknown>;
    const nested =
      details.error && typeof details.error === "object" && !Array.isArray(details.error)
        ? (details.error as Record<string, unknown>)
        : null;
    for (const candidate of [details.detail, nested?.message, details.message]) {
      if (typeof candidate !== "string") continue;
      const safe = sanitizeErrorMessage(candidate);
      if (!safe || base.includes(safe) || parts.includes(safe)) continue;
      parts.push(safe);
    }
  }
  const full = parts.length ? `${base} (${parts.join("; ")})` : base;
  return full.length > MAX_UPSTREAM_ERROR_MESSAGE_LENGTH
    ? `${full.slice(0, MAX_UPSTREAM_ERROR_MESSAGE_LENGTH - 3)}...`
    : full;
}