From 0cab91e0f8c0074d4bfdf33c3971b3dde1029412 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Fri, 4 Oct 2024 13:39:08 +0300 Subject: [PATCH] Add Claude streamed tool parser --- public/scripts/tool-calling.js | 69 +++++++++++++++++++++------------- 1 file changed, 43 insertions(+), 26 deletions(-) diff --git a/public/scripts/tool-calling.js b/public/scripts/tool-calling.js index 15d3db954..57e21b618 100644 --- a/public/scripts/tool-calling.js +++ b/public/scripts/tool-calling.js @@ -256,41 +256,58 @@ export class ToolManager { * @returns {void} */ static parseToolCalls(toolCalls, parsed) { - if (!Array.isArray(parsed?.choices)) { - return; - } - for (const choice of parsed.choices) { - const choiceIndex = (typeof choice.index === 'number') ? choice.index : null; - const choiceDelta = choice.delta; + if (Array.isArray(parsed?.choices)) { + for (const choice of parsed.choices) { + const choiceIndex = (typeof choice.index === 'number') ? choice.index : null; + const choiceDelta = choice.delta; - if (choiceIndex === null || !choiceDelta) { - continue; - } - - const toolCallDeltas = choiceDelta?.tool_calls; - - if (!Array.isArray(toolCallDeltas)) { - continue; - } - - if (!Array.isArray(toolCalls[choiceIndex])) { - toolCalls[choiceIndex] = []; - } - - for (const toolCallDelta of toolCallDeltas) { - const toolCallIndex = (typeof toolCallDelta?.index === 'number') ? toolCallDelta.index : toolCallDeltas.indexOf(toolCallDelta); - - if (isNaN(toolCallIndex) || toolCallIndex < 0) { + if (choiceIndex === null || !choiceDelta) { continue; } + const toolCallDeltas = choiceDelta?.tool_calls; + + if (!Array.isArray(toolCallDeltas)) { + continue; + } + + if (!Array.isArray(toolCalls[choiceIndex])) { + toolCalls[choiceIndex] = []; + } + + for (const toolCallDelta of toolCallDeltas) { + const toolCallIndex = (typeof toolCallDelta?.index === 'number') ? toolCallDelta.index : toolCallDeltas.indexOf(toolCallDelta); + + if (isNaN(toolCallIndex) || toolCallIndex < 0) { + continue; + } + + if (toolCalls[choiceIndex][toolCallIndex] === undefined) { + toolCalls[choiceIndex][toolCallIndex] = {}; + } + + const targetToolCall = toolCalls[choiceIndex][toolCallIndex]; + + ToolManager.#applyToolCallDelta(targetToolCall, toolCallDelta); + } + } + } + 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]; - - ToolManager.#applyToolCallDelta(targetToolCall, toolCallDelta); + Object.assign(targetToolCall, parsed.content_block); } } }