Add Claude streamed tool parser

0cab91e0f8c0074d4bfdf33c3971b3dde1029412

Cohee <18619528+Cohee1207@users.noreply.github.com>

1 files changed, +20 -3Showing whitespace changes
public/scripts/tool-calling.js+20 -3
@@ -256,9 +256,7 @@ export class ToolManager {
256256 * @returns {void}
257257 */
258258 static parseToolCalls(toolCalls, parsed) {
259259 if (!Array.isArray(parsed?.choices)) {
260- return;
261- }
262260 for (const choice of parsed.choices) {
263261 const choiceIndex = (typeof choice.index === 'number') ? choice.index : null;
264262 const choiceDelta = choice.delta;
@@ -294,6 +292,25 @@ export class ToolManager {
294292 }
295293 }
296294 }
295+ if (typeof parsed?.content_block === 'object') {
296+ const choiceIndex = 0;
297+
298+ if (parsed?.content_block?.type === 'tool_use') {
299+ if (!Array.isArray(toolCalls[choiceIndex])) {
300+ toolCalls[choiceIndex] = [];
301+ }
302+
303+ const toolCallIndex = toolCalls[choiceIndex].length;
304+
305+ if (toolCalls[choiceIndex][toolCallIndex] === undefined) {
306+ toolCalls[choiceIndex][toolCallIndex] = {};
307+ }
308+
309+ const targetToolCall = toolCalls[choiceIndex][toolCallIndex];
310+ Object.assign(targetToolCall, parsed.content_block);
311+ }
312+ }
313+ }
297314
298315 static #applyToolCallDelta(target, delta) {
299316 for (const key in delta) {