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