mirror of
https://github.com/CJackHwang/ds2api.git
synced 2026-05-20 16:07:47 +08:00
fix: reset tool call state between separate tool blocks to ensure unique IDs across stream segments
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user