Don't split tool calls and tool results

591f61d35459ec2817ff251aa2392b4f7f7cdeaf

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

1 files changed, +15 -10Showing whitespace changes
public/scripts/openai.js+15 -10
@@ -732,19 +732,15 @@ async function populateChatHistory(messages, prompts, chatCompletion, type = nul
732 /** @type {import('./tool-calling.js').ToolInvocation[]} */732 /** @type {import('./tool-calling.js').ToolInvocation[]} */
733 const invocations = chatPrompt.invocations;733 const invocations = chatPrompt.invocations;
734 const toolCallMessage = new Message(chatMessage.role, undefined, 'toolCall-' + chatMessage.identifier);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 toolCallMessage.setToolCalls(invocations);736 toolCallMessage.setToolCalls(invocations);
736 if (chatCompletion.canAfford(toolCallMessage)) {737 if (chatCompletion.canAffordAll([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 chatCompletion.insertAtStart(toolCallMessage, 'chatHistory');741 chatCompletion.insertAtStart(toolCallMessage, 'chatHistory');
742 } else {
743 break;
748 }744 }
749745
750 continue;746 continue;
@@ -2653,6 +2649,15 @@ export class ChatCompletion {
2653 }2649 }
26542650
2655 /**2651 /**
2652 * Checks if the token budget can afford the tokens of all the specified messages.
2653 * @param {Message[]} messages - The messages to check for affordability.
2654 * @returns {boolean} True if the budget can afford all the messages, false otherwise.
2655 */
2656 canAffordAll(messages) {
2657 return 0 <= this.tokenBudget - messages.reduce((total, message) => total + message.getTokens(), 0);
2658 }
2659
2660 /**
2656 * Checks if a message with the specified identifier exists in the collection.2661 * Checks if a message with the specified identifier exists in the collection.
2657 *2662 *
2658 * @param {string} identifier - The identifier to check for existence.2663 * @param {string} identifier - The identifier to check for existence.