Fix ghost messages

This commit is contained in:
Cohee 2024-10-04 23:13:56 +03:00
parent 8ff9ef3610
commit 01fc5113d7
2 changed files with 19 additions and 11 deletions

View File

@ -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 invocationResult = await ToolManager.invokeFunctionTools(streamingProcessor.toolCalls);
if (invocationResult.hadToolCalls) {
const lastMessage = chat[chat.length - 1];
const hasToolCalls = ToolManager.hasToolCalls(streamingProcessor.toolCalls);
const shouldDeleteMessage = ['', '...'].includes(lastMessage?.mes) && ['', '...'].includes(streamingProcessor?.result);
shouldDeleteMessage && await deleteLastMessage();
hasToolCalls && shouldDeleteMessage && await deleteLastMessage();
const invocationResult = await ToolManager.invokeFunctionTools(streamingProcessor.toolCalls);
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 invocationResult = await ToolManager.invokeFunctionTools(data);
if (invocationResult.hadToolCalls) {
const hasToolCalls = ToolManager.hasToolCalls(data);
const shouldDeleteMessage = ['', '...'].includes(getMessage);
shouldDeleteMessage && await deleteLastMessage();
hasToolCalls && shouldDeleteMessage && await deleteLastMessage();
const invocationResult = await ToolManager.invokeFunctionTools(data);
if (hasToolCalls) {
if (!invocationResult.invocations.length && shouldDeleteMessage) {
ToolManager.showToolCallError(invocationResult.errors);
unblockGeneration(type);

View File

@ -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 });