Fix ghost messages

01fc5113d74447c734df4e8dd54628abfc2dcbe7

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

2 files changed, +17 -9Showing whitespace changes
public/script.js+8 -6
@@ -4418,11 +4418,12 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro
44184418 }
44194419
44204420 if (canPerformToolCalls && Array.isArray(streamingProcessor.toolCalls) && streamingProcessor.toolCalls.length) {
4421- const invocationResult = await ToolManager.invokeFunctionTools(streamingProcessor.toolCalls);
4422- if (invocationResult.hadToolCalls) {
44234421 const lastMessage = chat[chat.length - 1];
4422+ const hasToolCalls = ToolManager.hasToolCalls(streamingProcessor.toolCalls);
44244423 const shouldDeleteMessage = ['', '...'].includes(lastMessage?.mes) && ['', '...'].includes(streamingProcessor?.result);
44254424 hasToolCalls && shouldDeleteMessage && await deleteLastMessage();
4425+ const invocationResult = await ToolManager.invokeFunctionTools(streamingProcessor.toolCalls);
4426+ if (hasToolCalls) {
44264427 if (!invocationResult.invocations.length && shouldDeleteMessage) {
44274428 ToolManager.showToolCallError(invocationResult.errors);
44284429 unblockGeneration(type);
@@ -4512,10 +4513,11 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro
45124513 }
45134514
45144515 if (canPerformToolCalls) {
45154516 const invocationResulthasToolCalls = await ToolManager.invokeFunctionToolshasToolCalls(data);
4516- if (invocationResult.hadToolCalls) {
45174517 const shouldDeleteMessage = ['', '...'].includes(getMessage);
45184518 hasToolCalls && shouldDeleteMessage && await deleteLastMessage();
4519+ const invocationResult = await ToolManager.invokeFunctionTools(data);
4520+ if (hasToolCalls) {
45194521 if (!invocationResult.invocations.length && shouldDeleteMessage) {
45204522 ToolManager.showToolCallError(invocationResult.errors);
45214523 unblockGeneration(type);
public/scripts/tool-calling.js+9 -3
@@ -14,7 +14,6 @@ import { Popup } from './popup.js';
1414/**
1515 * @typedef {object} ToolInvocationResult
1616 * @property {ToolInvocation[]} invocations Successful tool invocations
17- * @property {boolean} hadToolCalls Whether any tool calls were found
1817 * @property {Error[]} errors Errors that occurred during tool invocation
1918 */
2019
@@ -455,6 +454,15 @@ export class ToolManager {
455454 }
456455
457456 /**
457+ * Checks if the response data contains tool calls.
458+ * @param {object} data Response data
459+ * @returns {boolean} Whether the response data contains tool calls
460+ */
461+ static hasToolCalls(data) {
462+ return Array.isArray(ToolManager.#getToolCallsFromData(data));
463+ }
464+
465+ /**
458466 * Check for function tool calls in the response data and invoke them.
459467 * @param {any} data Reply data
460468 * @returns {Promise<ToolInvocationResult>} Successful tool invocations
@@ -463,7 +471,6 @@ export class ToolManager {
463471 /** @type {ToolInvocationResult} */
464472 const result = {
465473 invocations: [],
466- hadToolCalls: false,
467474 errors: [],
468475 };
469476 const toolCalls = ToolManager.#getToolCallsFromData(data);
@@ -482,7 +489,6 @@ export class ToolManager {
482489 const parameters = toolCall.function.arguments;
483490 const name = toolCall.function.name;
484491 const displayName = ToolManager.getDisplayName(name);
485- result.hadToolCalls = true;
486492
487493 const message = ToolManager.formatToolCallMessage(name, parameters);
488494 const toast = message && toastr.info(message, 'Tool Calling', { timeOut: 0 });