| 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; | 260 | for (const choice of parsed.choices) { |
| 261 | } | 261 | const choiceIndex = (typeof choice.index === 'number') ? choice.index : null; |
| 262 | for (const choice of parsed.choices) { | 262 | const choiceDelta = choice.delta; |
| 263 | const choiceIndex = (typeof choice.index === 'number') ? choice.index : null; | | |
| 264 | const choiceDelta = choice.delta; | | |
| 265 | | 263 | |
| 266 | if (choiceIndex === null || !choiceDelta) { | 264 | if (choiceIndex === null || !choiceDelta) { |
| 267 | continue; | 265 | continue; |
| 268 | } | 266 | } |
| 269 | | 267 | |
| 270 | const toolCallDeltas = choiceDelta?.tool_calls; | 268 | const toolCallDeltas = choiceDelta?.tool_calls; |
| 271 | | 269 | |
| 272 | if (!Array.isArray(toolCallDeltas)) { | 270 | if (!Array.isArray(toolCallDeltas)) { |
| 273 | continue; | 271 | continue; |
| 274 | } | 272 | } |
| 275 | | 273 | |
| 276 | if (!Array.isArray(toolCalls[choiceIndex])) { | 274 | if (!Array.isArray(toolCalls[choiceIndex])) { |
| 277 | toolCalls[choiceIndex] = []; | 275 | toolCalls[choiceIndex] = []; |
| 278 | } | 276 | } |
| 279 | | 277 | |
| 280 | for (const toolCallDelta of toolCallDeltas) { | 278 | for (const toolCallDelta of toolCallDeltas) { |
| 281 | const toolCallIndex = (typeof toolCallDelta?.index === 'number') ? toolCallDelta.index : toolCallDeltas.indexOf(toolCallDelta); | 279 | const toolCallIndex = (typeof toolCallDelta?.index === 'number') ? toolCallDelta.index : toolCallDeltas.indexOf(toolCallDelta); |
| 282 | | 280 | |
| 283 | if (isNaN(toolCallIndex) || toolCallIndex < 0) { | 281 | if (isNaN(toolCallIndex) || toolCallIndex < 0) { |
| 284 | continue; | 282 | continue; |
| | 283 | } |
| | 284 | |
| | 285 | if (toolCalls[choiceIndex][toolCallIndex] === undefined) { |
| | 286 | toolCalls[choiceIndex][toolCallIndex] = {}; |
| | 287 | } |
| | 288 | |
| | 289 | const targetToolCall = toolCalls[choiceIndex][toolCallIndex]; |
| | 290 | |
| | 291 | ToolManager.#applyToolCallDelta(targetToolCall, toolCallDelta); |
| | 292 | } |
| | 293 | } |
| | 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] = []; |
| 285 | } | 301 | } |
| 286 | | 302 | |
| | 303 | const toolCallIndex = toolCalls[choiceIndex].length; |
| | 304 | |
| 287 | if (toolCalls[choiceIndex][toolCallIndex] === undefined) { | 305 | if (toolCalls[choiceIndex][toolCallIndex] === undefined) { |
| 288 | toolCalls[choiceIndex][toolCallIndex] = {}; | 306 | toolCalls[choiceIndex][toolCallIndex] = {}; |
| 289 | } | 307 | } |
| 290 | | 308 | |
| 291 | const targetToolCall = toolCalls[choiceIndex][toolCallIndex]; | 309 | const targetToolCall = toolCalls[choiceIndex][toolCallIndex]; |
| 292 | | 310 | Object.assign(targetToolCall, parsed.content_block); |
| 293 | ToolManager.#applyToolCallDelta(targetToolCall, toolCallDelta); | | |
| 294 | } | 311 | } |
| 295 | } | 312 | } |
| 296 | } | 313 | } |