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
4418 }4418 }
44194419
4420 if (canPerformToolCalls && Array.isArray(streamingProcessor.toolCalls) && streamingProcessor.toolCalls.length) {4420 if (canPerformToolCalls && Array.isArray(streamingProcessor.toolCalls) && streamingProcessor.toolCalls.length) {
4421 const invocationResult = await ToolManager.invokeFunctionTools(streamingProcessor.toolCalls);
4422 if (invocationResult.hadToolCalls) {
4423 const lastMessage = chat[chat.length - 1];4421 const lastMessage = chat[chat.length - 1];
4422 const hasToolCalls = ToolManager.hasToolCalls(streamingProcessor.toolCalls);
4424 const shouldDeleteMessage = ['', '...'].includes(lastMessage?.mes) && ['', '...'].includes(streamingProcessor?.result);4423 const shouldDeleteMessage = ['', '...'].includes(lastMessage?.mes) && ['', '...'].includes(streamingProcessor?.result);
4425 shouldDeleteMessage && await deleteLastMessage();4424 hasToolCalls && shouldDeleteMessage && await deleteLastMessage();
4425 const invocationResult = await ToolManager.invokeFunctionTools(streamingProcessor.toolCalls);
4426 if (hasToolCalls) {
4426 if (!invocationResult.invocations.length && shouldDeleteMessage) {4427 if (!invocationResult.invocations.length && shouldDeleteMessage) {
4427 ToolManager.showToolCallError(invocationResult.errors);4428 ToolManager.showToolCallError(invocationResult.errors);
4428 unblockGeneration(type);4429 unblockGeneration(type);
@@ -4512,10 +4513,11 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro
4512 }4513 }
45134514
4514 if (canPerformToolCalls) {4515 if (canPerformToolCalls) {
4515 const invocationResult = await ToolManager.invokeFunctionTools(data);4516 const hasToolCalls = ToolManager.hasToolCalls(data);
4516 if (invocationResult.hadToolCalls) {
4517 const shouldDeleteMessage = ['', '...'].includes(getMessage);4517 const shouldDeleteMessage = ['', '...'].includes(getMessage);
4518 shouldDeleteMessage && await deleteLastMessage();4518 hasToolCalls && shouldDeleteMessage && await deleteLastMessage();
4519 const invocationResult = await ToolManager.invokeFunctionTools(data);
4520 if (hasToolCalls) {
4519 if (!invocationResult.invocations.length && shouldDeleteMessage) {4521 if (!invocationResult.invocations.length && shouldDeleteMessage) {
4520 ToolManager.showToolCallError(invocationResult.errors);4522 ToolManager.showToolCallError(invocationResult.errors);
4521 unblockGeneration(type);4523 unblockGeneration(type);
public/scripts/tool-calling.js+9 -3
@@ -14,7 +14,6 @@ import { Popup } from './popup.js';
14/**14/**
15 * @typedef {object} ToolInvocationResult15 * @typedef {object} ToolInvocationResult
16 * @property {ToolInvocation[]} invocations Successful tool invocations16 * @property {ToolInvocation[]} invocations Successful tool invocations
17 * @property {boolean} hadToolCalls Whether any tool calls were found
18 * @property {Error[]} errors Errors that occurred during tool invocation17 * @property {Error[]} errors Errors that occurred during tool invocation
19 */18 */
2019
@@ -455,6 +454,15 @@ export class ToolManager {
455 }454 }
456455
457 /**456 /**
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 /**
458 * Check for function tool calls in the response data and invoke them.466 * Check for function tool calls in the response data and invoke them.
459 * @param {any} data Reply data467 * @param {any} data Reply data
460 * @returns {Promise<ToolInvocationResult>} Successful tool invocations468 * @returns {Promise<ToolInvocationResult>} Successful tool invocations
@@ -463,7 +471,6 @@ export class ToolManager {
463 /** @type {ToolInvocationResult} */471 /** @type {ToolInvocationResult} */
464 const result = {472 const result = {
465 invocations: [],473 invocations: [],
466 hadToolCalls: false,
467 errors: [],474 errors: [],
468 };475 };
469 const toolCalls = ToolManager.#getToolCallsFromData(data);476 const toolCalls = ToolManager.#getToolCallsFromData(data);
@@ -482,7 +489,6 @@ export class ToolManager {
482 const parameters = toolCall.function.arguments;489 const parameters = toolCall.function.arguments;
483 const name = toolCall.function.name;490 const name = toolCall.function.name;
484 const displayName = ToolManager.getDisplayName(name);491 const displayName = ToolManager.getDisplayName(name);
485 result.hadToolCalls = true;
486492
487 const message = ToolManager.formatToolCallMessage(name, parameters);493 const message = ToolManager.formatToolCallMessage(name, parameters);
488 const toast = message && toastr.info(message, 'Tool Calling', { timeOut: 0 });494 const toast = message && toastr.info(message, 'Tool Calling', { timeOut: 0 });