Fix parallel tool use (#400)

This commit is contained in:
Fan
2025-08-26 17:01:09 +08:00
committed by GitHub
parent 1baf5d795f
commit 472df045d3

View File

@@ -1351,7 +1351,9 @@ export class OpenAIContentGenerator implements ContentGenerator {
// Handle text content
if (choice.delta?.content) {
parts.push({ text: choice.delta.content });
if (typeof choice.delta.content === 'string') {
parts.push({ text: choice.delta.content });
}
}
// Handle tool calls - only accumulate during streaming, emit when complete
@@ -1371,10 +1373,36 @@ export class OpenAIContentGenerator implements ContentGenerator {
accumulatedCall.id = toolCall.id;
}
if (toolCall.function?.name) {
// If this is a new function name, reset the arguments
if (accumulatedCall.name !== toolCall.function.name) {
accumulatedCall.arguments = '';
}
accumulatedCall.name = toolCall.function.name;
}
if (toolCall.function?.arguments) {
accumulatedCall.arguments += toolCall.function.arguments;
// Check if we already have a complete JSON object
const currentArgs = accumulatedCall.arguments;
const newArgs = toolCall.function.arguments;
// If current arguments already form a complete JSON and new arguments start a new object,
// this indicates a new tool call with the same name
let shouldReset = false;
if (currentArgs && newArgs.trim().startsWith('{')) {
try {
JSON.parse(currentArgs);
// If we can parse current arguments as complete JSON and new args start with {,
// this is likely a new tool call
shouldReset = true;
} catch {
// Current arguments are not complete JSON, continue accumulating
}
}
if (shouldReset) {
accumulatedCall.arguments = newArgs;
} else {
accumulatedCall.arguments += newArgs;
}
}
}
}
@@ -1562,7 +1590,7 @@ export class OpenAIContentGenerator implements ContentGenerator {
}
}
messageContent = textParts.join('');
messageContent = textParts.join('').trimEnd();
}
const choice: OpenAIChoice = {