fix: reset tool call state between separate tool blocks to ensure unique IDs across stream segments

This commit is contained in:
CJACK.
2026-04-22 20:10:06 +00:00
parent c291d333c4
commit 5cf56e7628
10 changed files with 175 additions and 3 deletions

View File

@@ -18,6 +18,7 @@ const {
normalizePreparedToolNames,
boolDefaultTrue,
filterIncrementalToolCallDeltasByAllowed,
resetStreamToolCallState,
} = require('./toolcall_policy');
const {
estimateTokens,
@@ -115,6 +116,7 @@ module.exports.__test = {
normalizePreparedToolNames,
boolDefaultTrue,
filterIncrementalToolCallDeltasByAllowed,
resetStreamToolCallState,
estimateTokens,
buildUsage,
filterLeakedContentFilterParts,

View File

@@ -98,6 +98,15 @@ function filterIncrementalToolCallDeltasByAllowed(deltas, allowedNames, seenName
return out;
}
function resetStreamToolCallState(idStore, seenNames) {
if (idStore instanceof Map) {
idStore.clear();
}
if (seenNames instanceof Map) {
seenNames.clear();
}
}
function ensureStreamToolCallID(idStore, index) {
const key = Number.isInteger(index) ? index : 0;
const existing = idStore.get(key);
@@ -135,4 +144,5 @@ module.exports = {
boolDefaultTrue,
formatIncrementalToolCallDeltas,
filterIncrementalToolCallDeltasByAllowed,
resetStreamToolCallState,
};

View File

@@ -18,6 +18,7 @@ const {
formatIncrementalToolCallDeltas,
filterIncrementalToolCallDeltasByAllowed,
boolDefaultTrue,
resetStreamToolCallState,
} = require('./toolcall_policy');
const { createChatCompletionEmitter } = require('./stream_emitter');
const {
@@ -161,6 +162,7 @@ async function handleVercelStream(req, res, rawBody, payload) {
if (evt.type === 'tool_calls' && Array.isArray(evt.calls) && evt.calls.length > 0) {
toolCallsEmitted = true;
sendDeltaFrame({ tool_calls: formatOpenAIStreamToolCalls(evt.calls, streamToolCallIDs) });
resetStreamToolCallState(streamToolCallIDs, streamToolNames);
continue;
}
if (evt.text) {
@@ -283,6 +285,7 @@ async function handleVercelStream(req, res, rawBody, payload) {
if (evt.type === 'tool_calls') {
toolCallsEmitted = true;
sendDeltaFrame({ tool_calls: formatOpenAIStreamToolCalls(evt.calls, streamToolCallIDs) });
resetStreamToolCallState(streamToolCallIDs, streamToolNames);
continue;
}
if (evt.text) {