| 256 | 256 | * @returns {void} |
| 257 | 257 | */ |
| 258 | 258 | static parseToolCalls(toolCalls, parsed) { |
| 259 | 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 | 264 | if (choiceIndex === null || !choiceDelta) { |
| 267 | 265 | continue; |
| 268 | 266 | } |
| 269 | 267 | |
| 270 | 268 | const toolCallDeltas = choiceDelta?.tool_calls; |
| 271 | 269 | |
| 272 | 270 | if (!Array.isArray(toolCallDeltas)) { |
| 273 | 271 | continue; |
| 274 | 272 | } |
| 275 | 273 | |
| 276 | 274 | if (!Array.isArray(toolCalls[choiceIndex])) { |
| 277 | 275 | toolCalls[choiceIndex] = []; |
| 278 | 276 | } |
| 279 | 277 | |
| 280 | 278 | for (const toolCallDelta of toolCallDeltas) { |
| 281 | 279 | const toolCallIndex = (typeof toolCallDelta?.index === 'number') ? toolCallDelta.index : toolCallDeltas.indexOf(toolCallDelta); |
| 282 | 280 | |
| 283 | 281 | if (isNaN(toolCallIndex) || toolCallIndex < 0) { |
| 284 | 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 | 305 | if (toolCalls[choiceIndex][toolCallIndex] === undefined) { |
| 288 | 306 | toolCalls[choiceIndex][toolCallIndex] = {}; |
| 289 | 307 | } |
| 290 | 308 | |
| 291 | 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 | } |