Add Claude streamed tool parser

This commit is contained in:
Cohee 2024-10-04 13:39:08 +03:00
parent c3c10a629e
commit 0cab91e0f8
1 changed files with 43 additions and 26 deletions

View File

@ -256,9 +256,7 @@ export class ToolManager {
* @returns {void}
*/
static parseToolCalls(toolCalls, parsed) {
if (!Array.isArray(parsed?.choices)) {
return;
}
if (Array.isArray(parsed?.choices)) {
for (const choice of parsed.choices) {
const choiceIndex = (typeof choice.index === 'number') ? choice.index : null;
const choiceDelta = choice.delta;
@ -294,6 +292,25 @@ export class ToolManager {
}
}
}
if (typeof parsed?.content_block === 'object') {
const choiceIndex = 0;
if (parsed?.content_block?.type === 'tool_use') {
if (!Array.isArray(toolCalls[choiceIndex])) {
toolCalls[choiceIndex] = [];
}
const toolCallIndex = toolCalls[choiceIndex].length;
if (toolCalls[choiceIndex][toolCallIndex] === undefined) {
toolCalls[choiceIndex][toolCallIndex] = {};
}
const targetToolCall = toolCalls[choiceIndex][toolCallIndex];
Object.assign(targetToolCall, parsed.content_block);
}
}
}
static #applyToolCallDelta(target, delta) {
for (const key in delta) {