fix: tool_call_id error when switching (#6854)

This commit is contained in:
Cristina Poncela Cubeiro
2026-07-20 13:51:16 +02:00
committed by GitHub
parent 9e7fce70a3
commit d9f7f81473
+14 -2
View File
@@ -34,6 +34,7 @@ import type {
} from "../types.ts";
import { formatProviderError, normalizeProviderError } from "../utils/error-body.ts";
import { AssistantMessageEventStream } from "../utils/event-stream.ts";
import { shortHash } from "../utils/hash.ts";
import { headersToRecord } from "../utils/headers.ts";
import { parseStreamingJson } from "../utils/json-parse.ts";
import { getProviderEnvValue } from "../utils/provider-env.ts";
@@ -895,10 +896,21 @@ export function convertMessages(
// Format: {call_id}|{id} where {id} can be 400+ chars with special chars (+, /, =)
// These come from providers like github-copilot, openai-codex, opencode
// Extract just the call_id part and normalize it
// Multiple tool calls in the same turn can share call_id but differ by item_id.
// Preserve item-level uniqueness when replaying into Chat Completions, which
// requires distinct tool call ids.
if (id.includes("|")) {
const [callId] = id.split("|");
// Sanitize to allowed chars and truncate to 40 chars (OpenAI limit)
return callId.replace(/[^a-zA-Z0-9_-]/g, "_").slice(0, 40);
const separatorIndex = id.indexOf("|");
const callId = id.slice(0, separatorIndex).replace(/[^a-zA-Z0-9_-]/g, "_");
const itemId = id.slice(separatorIndex + 1).replace(/[^a-zA-Z0-9_-]/g, "_");
const combinedId = itemId.length > 0 ? `${callId}_${itemId}` : callId;
if (combinedId.length <= 40) {
return combinedId;
}
const hash = shortHash(id).slice(0, 8);
const prefix = callId.slice(0, Math.max(1, 40 - hash.length - 1));
return `${prefix}_${hash}`;
}
if (model.provider === "openai") return id.length > 40 ? id.slice(0, 40) : id;