Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

6.6 KB
Raw Blame
// How a chat-completions provider says no to a request, and what that means for the turn.
import { ChatProtocolError, object } from "./openai-chat-protocol.ts";

export interface ProviderBody {
  error?: unknown;
  base_resp?: { status_code?: unknown; status_msg?: unknown };
}

/** MiniMax reports auth, balance and parameter failures as `base_resp` with HTTP 200. */
function providerError(json: ProviderBody): string | null {
  if (typeof json.error === "string") return json.error;
  const error = object(json.error);
  if (error) return typeof error.message === "string" ? error.message : "unknown error";
  const code = json.base_resp?.status_code;
  if (typeof code === "number" && code !== 0) {
    const msg = typeof json.base_resp?.status_msg === "string" ? json.base_resp.status_msg : "";
    return `upstream error ${code}${msg ? `: ${msg}` : ""}`;
  }
  return null;
}

const MACHINE_CODE = /^[a-z][a-z0-9_]*$/;

/** A failed response's own error message, then its machine code (trial-credit.ts parses it), else the raw body. */
export function httpErrorDetail(body: string, limit = 500): string {
  let json: ProviderBody | undefined;
  try { json = object(JSON.parse(body)); } catch { json = undefined; }
  const message = json ? providerError(json) : null;
  if (!message || message === "unknown error") return body.slice(0, limit);
  const code = object(json?.error)?.code;
  const suffix = typeof code === "string" && MACHINE_CODE.test(code) ? ` (${code})` : "";
  return message.slice(0, limit) + suffix;
}

export class UnsupportedChatToolsError extends Error {}

export class ChatHttpError extends Error {
  readonly status: number;
  constructor(status: number, message: string) {
    super(message);
    this.status = status;
  }
}

export class ChatCompletionError extends ChatProtocolError {
  readonly status: number;
  constructor(status: number, message: string) {
    super(message);
    this.status = status;
  }
}

/** The provider closed the stream with neither finish_reason nor [DONE]; resending repeats nothing. */
export class StreamTruncatedError extends ChatProtocolError {}

export function rejectsToolsParameter(status: number, body: string): boolean {
  if (status !== 400 && status !== 422) return false;
  let envelope: Record<string, unknown> | undefined;
  try { envelope = object(JSON.parse(body)); } catch { return false; }
  const error = object(envelope?.error);
  if (error?.param === "tools" && error.code === "unsupported_parameter") return true;
  const message = error?.message ?? envelope?.error;
  return typeof message === "string" && (
    /\bdoes not support (?:tools|tool calling|function calling)(?:[.!]?$|[.!]?\s)/i.test(message) ||
    /\b(?:tools|tool calling|function calling) (?:is|are) not supported\b/i.test(message) ||
    /^(?:unsupported|unknown|unrecognized) (?:parameter|field):?\s*['"]?tools['"]?[.!]?$/i.test(message.trim())
  );
}

export class UnsupportedReasoningReplayError extends Error {}

/** DeepSeek-style providers need `reasoning_content` echoed on a tool-call message; Groq rejects it. */
export function rejectsReasoningReplay(status: number, body: string): boolean {
  if (status !== 400 && status !== 422) return false;
  let envelope: Record<string, unknown> | undefined;
  try { envelope = object(JSON.parse(body)); } catch { return false; }
  const message = object(envelope?.error)?.message ?? envelope?.error;
  return typeof message === "string" && /\breasoning_content\b/.test(message) &&
    /\b(?:unsupported|not supported|unknown|unrecognized|unexpected|not permitted|not allowed)\b/i.test(message);
}

/** Groq fails a completion with `tool_use_failed` when a generated tool call is invalid: usually a
 *  tool it was not offered (gpt-oss inventing a "json" tool), sometimes arguments missing the schema.
 *  oMLX ends the stream with `invalid_tool_call` or `incomplete_tool_call` when it cannot parse the
 *  model's tool call. */
export class RejectedToolCallError extends ChatProtocolError {
  readonly refusal: Record<string, unknown>;
  constructor(message: string, refusal: Record<string, unknown>) {
    super(message);
    this.refusal = refusal;
  }
}

const TOOL_CALL_FAILURES = new Set(["tool_use_failed", "invalid_tool_call", "incomplete_tool_call"]);

export const toolCallRefusal = (error: unknown) => {
  const body = object(error);
  return typeof body?.code === "string" && TOOL_CALL_FAILURES.has(body.code) ? body : undefined;
};

export function refusedToolCall(status: number, body: string) {
  if (status !== 400) return undefined;
  try { return toolCallRefusal(object(JSON.parse(body))?.error); } catch { return undefined; }
}

export function refusedToolCallFailure(error: RejectedToolCallError, earlierSteps: boolean): string {
  const words = typeof error.refusal.message === "string" ? error.refusal.message : error.message;
  const tool = /attempted to call tool '([^']+)'/.exec(words)?.[1];
  const what = tool ? `tried to use a tool it was not given ("${tool}")` : "made a tool call the provider rejected";
  const next = earlierSteps
    ? "The steps before it already ran, so ask only for what's left."
    : "Nothing ran. Retry; if it keeps happening, rephrase the request or choose another model.";
  return `The model ${what}. ${next} Provider: ${words.slice(0, 200)}`;
}

export function completionError(json: ProviderBody, label: string): ChatProtocolError | null {
  const message = providerError(json);
  if (!message) return null;
  const text = `provider returned a ${label}: ${message.slice(0, 200)}`;
  const refusal = toolCallRefusal(json.error);
  if (refusal) return new RejectedToolCallError(text, refusal);
  const code = object(json.error)?.code;
  const status = code === "rate_limit_exceeded" ? 429 : code;
  return typeof status === "number" ? new ChatCompletionError(status, text) : new ChatProtocolError(text);
}

/** The data of an SSE `data:` line, or undefined for any other line. llama.cpp before b7200 (seen on b6500) refuses a
 * streamed request with HTTP 200 and an `error:` event holding the bare error: that throws what `failure` makes of it. */
export function sseData(line: string, failure: (status: number, body: string) => Error): string | undefined {
  if (line.startsWith("error:")) {
    const event = line.slice("error:".length).trim();
    let error: Record<string, unknown> | undefined;
    try { error = object(JSON.parse(event)); } catch { error = undefined; }
    throw typeof error?.code === "number" ? failure(error.code, JSON.stringify({ error }))
      : new ChatProtocolError(`provider returned a streaming error: ${event.slice(0, 200)}`);
  }
  return line.startsWith("data:") ? line.slice("data:".length).trim() : undefined;
}