Fix ghost messages
| @@ -4418,11 +4418,12 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro | |||
| 4418 | } | 4418 | } |
| 4419 | 4419 | ||
| 4420 | if (canPerformToolCalls && Array.isArray(streamingProcessor.toolCalls) && streamingProcessor.toolCalls.length) { | 4420 | if (canPerformToolCalls && Array.isArray(streamingProcessor.toolCalls) && streamingProcessor.toolCalls.length) { |
| 4421 | const lastMessage = chat[chat.length - 1]; | ||
| 4422 | const hasToolCalls = ToolManager.hasToolCalls(streamingProcessor.toolCalls); | ||
| 4423 | const shouldDeleteMessage = ['', '...'].includes(lastMessage?.mes) && ['', '...'].includes(streamingProcessor?.result); | ||
| 4424 | hasToolCalls && shouldDeleteMessage && await deleteLastMessage(); | ||
| 4421 | const invocationResult = await ToolManager.invokeFunctionTools(streamingProcessor.toolCalls); | 4425 | const invocationResult = await ToolManager.invokeFunctionTools(streamingProcessor.toolCalls); |
| 4422 | if (invocationResult.hadToolCalls) { | 4426 | if (hasToolCalls) { |
| 4423 | const lastMessage = chat[chat.length - 1]; | ||
| 4424 | const shouldDeleteMessage = ['', '...'].includes(lastMessage?.mes) && ['', '...'].includes(streamingProcessor?.result); | ||
| 4425 | shouldDeleteMessage && await deleteLastMessage(); | ||
| 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 | } |
| 4513 | 4514 | ||
| 4514 | if (canPerformToolCalls) { | 4515 | if (canPerformToolCalls) { |
| 4516 | const hasToolCalls = ToolManager.hasToolCalls(data); | ||
| 4517 | const shouldDeleteMessage = ['', '...'].includes(getMessage); | ||
| 4518 | hasToolCalls && shouldDeleteMessage && await deleteLastMessage(); | ||
| 4515 | const invocationResult = await ToolManager.invokeFunctionTools(data); | 4519 | const invocationResult = await ToolManager.invokeFunctionTools(data); |
| 4516 | if (invocationResult.hadToolCalls) { | 4520 | if (hasToolCalls) { |
| 4517 | const shouldDeleteMessage = ['', '...'].includes(getMessage); | ||
| 4518 | shouldDeleteMessage && await deleteLastMessage(); | ||
| 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); |
| @@ -14,7 +14,6 @@ import { Popup } from './popup.js'; | |||
| 14 | /** | 14 | /** |
| 15 | * @typedef {object} ToolInvocationResult | 15 | * @typedef {object} ToolInvocationResult |
| 16 | * @property {ToolInvocation[]} invocations Successful tool invocations | 16 | * @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 invocation | 17 | * @property {Error[]} errors Errors that occurred during tool invocation |
| 19 | */ | 18 | */ |
| 20 | 19 | ||
| @@ -322,7 +321,7 @@ export class ToolManager { | |||
| 322 | const choiceIndex = 0; | 321 | const choiceIndex = 0; |
| 323 | const toolCallIndex = parsed?.index ?? 0; | 322 | const toolCallIndex = parsed?.index ?? 0; |
| 324 | const targetToolCall = toolCalls[choiceIndex]?.[toolCallIndex]; | 323 | const targetToolCall = toolCalls[choiceIndex]?.[toolCallIndex]; |
| 325 | if (targetToolCall){ | 324 | if (targetToolCall) { |
| 326 | if (parsed?.delta?.type === 'input_json_delta') { | 325 | if (parsed?.delta?.type === 'input_json_delta') { |
| 327 | const jsonDelta = parsed?.delta?.partial_json; | 326 | const jsonDelta = parsed?.delta?.partial_json; |
| 328 | if (!targetToolCall[this.#INPUT_DELTA_KEY]) { | 327 | if (!targetToolCall[this.#INPUT_DELTA_KEY]) { |
| @@ -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 | * 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 data | 467 | * @param {any} data Reply data |
| 460 | * @returns {Promise<ToolInvocationResult>} Successful tool invocations | 468 | * @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; | ||
| 486 | 492 | ||
| 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 }); |