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 {
256 * @returns {void}256 * @returns {void}
257 */257 */
258 static parseToolCalls(toolCalls, parsed) {258 static parseToolCalls(toolCalls, parsed) {
259 if (!Array.isArray(parsed?.choices)) {259 if (Array.isArray(parsed?.choices)) {
260 return;
261 }
262 for (const choice of parsed.choices) {260 for (const choice of parsed.choices) {
263 const choiceIndex = (typeof choice.index === 'number') ? choice.index : null;261 const choiceIndex = (typeof choice.index === 'number') ? choice.index : null;
264 const choiceDelta = choice.delta;262 const choiceDelta = choice.delta;
@@ -294,6 +292,25 @@ export class ToolManager {
294 }292 }
295 }293 }
296 }294 }
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
298 static #applyToolCallDelta(target, delta) {315 static #applyToolCallDelta(target, delta) {
299 for (const key in delta) {316 for (const key in delta) {