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
732732 /** @type {import('./tool-calling.js').ToolInvocation[]} */
733733 const invocations = chatPrompt.invocations;
734734 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));
735736 toolCallMessage.setToolCalls(invocations);
736737 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');
745740 }
746- chatCompletion.freeBudget(toolCallMessage);
747741 chatCompletion.insertAtStart(toolCallMessage, 'chatHistory');
742+ } else {
743+ break;
748744 }
749745
750746 continue;
@@ -2653,6 +2649,15 @@ export class ChatCompletion {
26532649 }
26542650
26552651 /**
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+ /**
26562661 * Checks if a message with the specified identifier exists in the collection.
26572662 *
26582663 * @param {string} identifier - The identifier to check for existence.