From 01fc5113d74447c734df4e8dd54628abfc2dcbe7 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Fri, 4 Oct 2024 23:13:56 +0300 Subject: [PATCH] Fix ghost messages --- public/script.js | 16 +++++++++------- public/scripts/tool-calling.js | 14 ++++++++++---- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/public/script.js b/public/script.js index 545cfe020..cd57ed49b 100644 --- a/public/script.js +++ b/public/script.js @@ -4418,11 +4418,12 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro } if (canPerformToolCalls && Array.isArray(streamingProcessor.toolCalls) && streamingProcessor.toolCalls.length) { + const lastMessage = chat[chat.length - 1]; + const hasToolCalls = ToolManager.hasToolCalls(streamingProcessor.toolCalls); + const shouldDeleteMessage = ['', '...'].includes(lastMessage?.mes) && ['', '...'].includes(streamingProcessor?.result); + hasToolCalls && shouldDeleteMessage && await deleteLastMessage(); const invocationResult = await ToolManager.invokeFunctionTools(streamingProcessor.toolCalls); - if (invocationResult.hadToolCalls) { - const lastMessage = chat[chat.length - 1]; - const shouldDeleteMessage = ['', '...'].includes(lastMessage?.mes) && ['', '...'].includes(streamingProcessor?.result); - shouldDeleteMessage && await deleteLastMessage(); + if (hasToolCalls) { if (!invocationResult.invocations.length && shouldDeleteMessage) { ToolManager.showToolCallError(invocationResult.errors); unblockGeneration(type); @@ -4512,10 +4513,11 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro } if (canPerformToolCalls) { + const hasToolCalls = ToolManager.hasToolCalls(data); + const shouldDeleteMessage = ['', '...'].includes(getMessage); + hasToolCalls && shouldDeleteMessage && await deleteLastMessage(); const invocationResult = await ToolManager.invokeFunctionTools(data); - if (invocationResult.hadToolCalls) { - const shouldDeleteMessage = ['', '...'].includes(getMessage); - shouldDeleteMessage && await deleteLastMessage(); + if (hasToolCalls) { if (!invocationResult.invocations.length && shouldDeleteMessage) { ToolManager.showToolCallError(invocationResult.errors); unblockGeneration(type); diff --git a/public/scripts/tool-calling.js b/public/scripts/tool-calling.js index f1ce95e91..bbd3d7d34 100644 --- a/public/scripts/tool-calling.js +++ b/public/scripts/tool-calling.js @@ -14,7 +14,6 @@ import { Popup } from './popup.js'; /** * @typedef {object} ToolInvocationResult * @property {ToolInvocation[]} invocations Successful tool invocations - * @property {boolean} hadToolCalls Whether any tool calls were found * @property {Error[]} errors Errors that occurred during tool invocation */ @@ -322,7 +321,7 @@ export class ToolManager { const choiceIndex = 0; const toolCallIndex = parsed?.index ?? 0; const targetToolCall = toolCalls[choiceIndex]?.[toolCallIndex]; - if (targetToolCall){ + if (targetToolCall) { if (parsed?.delta?.type === 'input_json_delta') { const jsonDelta = parsed?.delta?.partial_json; if (!targetToolCall[this.#INPUT_DELTA_KEY]) { @@ -454,6 +453,15 @@ export class ToolManager { } } + /** + * Checks if the response data contains tool calls. + * @param {object} data Response data + * @returns {boolean} Whether the response data contains tool calls + */ + static hasToolCalls(data) { + return Array.isArray(ToolManager.#getToolCallsFromData(data)); + } + /** * Check for function tool calls in the response data and invoke them. * @param {any} data Reply data @@ -463,7 +471,6 @@ export class ToolManager { /** @type {ToolInvocationResult} */ const result = { invocations: [], - hadToolCalls: false, errors: [], }; const toolCalls = ToolManager.#getToolCallsFromData(data); @@ -482,7 +489,6 @@ export class ToolManager { const parameters = toolCall.function.arguments; const name = toolCall.function.name; const displayName = ToolManager.getDisplayName(name); - result.hadToolCalls = true; const message = ToolManager.formatToolCallMessage(name, parameters); const toast = message && toastr.info(message, 'Tool Calling', { timeOut: 0 });