| 732 | 732 | /** @type {import('./tool-calling.js').ToolInvocation[]} */ |
| 733 | 733 | const invocations = chatPrompt.invocations; |
| 734 | 734 | const toolCallMessage = new Message(chatMessage.role, undefined, 'toolCall-' + chatMessage.identifier); |
| 735 | + const toolResultMessages = invocations.slice().reverse().map((invocation) => new Message('tool', invocation.result || '[No content]', invocation.id)); |
| 735 | 736 | toolCallMessage.setToolCalls(invocations); |
| 736 | 737 | if (chatCompletion.canAffordcanAffordAll([toolCallMessage, ...toolResultMessages])) { |
| 737 | | - chatCompletion.reserveBudget(toolCallMessage); |
| 738 | + for (const resultMessage of toolResultMessages) { |
| 738 | | - for (const invocation of invocations.slice().reverse()) { |
| 739 | + chatCompletion.insertAtStart(resultMessage, 'chatHistory'); |
| 739 | | - const toolResultMessage = new Message('tool', invocation.result || '[No content]', invocation.id); |
| 740 | | - const canAfford = chatCompletion.canAfford(toolResultMessage); |
| 741 | | - if (!canAfford) { |
| 742 | | - break; |
| 743 | | - } |
| 744 | | - chatCompletion.insertAtStart(toolResultMessage, 'chatHistory'); |
| 745 | 740 | } |
| 746 | | - chatCompletion.freeBudget(toolCallMessage); |
| 747 | 741 | chatCompletion.insertAtStart(toolCallMessage, 'chatHistory'); |
| 742 | + } else { |
| 743 | + break; |
| 748 | 744 | } |
| 749 | 745 | |
| 750 | 746 | continue; |