fix(ai): hash foreign responses tool item ids

This commit is contained in:
Mario Zechner
2026-03-22 05:40:26 +01:00
parent 235b247f1f
commit b21b42d032
3 changed files with 79 additions and 3 deletions

View File

@@ -97,14 +97,20 @@ export function convertResponsesMessages<TApi extends Api>(
return normalized.replace(/_+$/, "");
};
const normalizeToolCallId = (id: string): string => {
const buildForeignResponsesItemId = (itemId: string): string => {
const normalized = `fc_${shortHash(itemId)}`;
return normalized.length > 64 ? normalized.slice(0, 64) : normalized;
};
const normalizeToolCallId = (id: string, _targetModel: Model<TApi>, source: AssistantMessage): string => {
if (!allowedToolCallProviders.has(model.provider)) return normalizeIdPart(id);
if (!id.includes("|")) return normalizeIdPart(id);
const [callId, itemId] = id.split("|");
const normalizedCallId = normalizeIdPart(callId);
let normalizedItemId = normalizeIdPart(itemId);
const isForeignToolCall = source.provider !== model.provider || source.api !== model.api;
let normalizedItemId = isForeignToolCall ? buildForeignResponsesItemId(itemId) : normalizeIdPart(itemId);
// OpenAI Responses API requires item id to start with "fc"
if (!normalizedItemId.startsWith("fc")) {
if (!normalizedItemId.startsWith("fc_")) {
normalizedItemId = normalizeIdPart(`fc_${normalizedItemId}`);
}
return `${normalizedCallId}|${normalizedItemId}`;