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 { restoreOpenAIToolNames } from "../translator/helpers/toolCallHelper.ts";

type JsonRecord = Record<string, unknown>;

export function normalizeFinalOpenAIStreamChunk(
  parsed: JsonRecord,
  toolNameMap: unknown
): { changed: boolean; hasFinishReason: boolean } {
  let changed = false;
  if (parsed.id != null && typeof parsed.id !== "string") {
    parsed.id = String(parsed.id);
    changed = true;
  }

  if (Array.isArray(parsed.choices)) {
    for (const choice of parsed.choices as JsonRecord[]) {
      const delta = (choice as JsonRecord | null | undefined)?.delta as JsonRecord | undefined;
      if (!Array.isArray(delta?.tool_calls)) continue;
      for (const toolCall of delta.tool_calls as JsonRecord[]) {
        if (toolCall?.id != null && typeof toolCall.id !== "string") {
          toolCall.id = String(toolCall.id);
          changed = true;
        }
      }
    }
  }

  changed = restoreOpenAIToolNames(parsed, toolNameMap) || changed;
  const firstChoice = Array.isArray(parsed.choices)
    ? (parsed.choices[0] as JsonRecord | undefined)
    : undefined;
  return { changed, hasFinishReason: Boolean(firstChoice?.finish_reason) };
}