Don't call tools on stopped streams. Emit events

47e3cf82e09279efc498325abdab7d42db8b0bd5

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

2 files changed, +13 -6Ignore whitespace
public/script.js+8 -4
@@ -467,6 +467,8 @@ export const event_types = {
467467 ONLINE_STATUS_CHANGED: 'online_status_changed',
468468 IMAGE_SWIPED: 'image_swiped',
469469 CONNECTION_PROFILE_LOADED: 'connection_profile_loaded',
470+ TOOL_CALLS_PERFORMED: 'tool_calls_performed',
471+ TOOL_CALLS_RENDERED: 'tool_calls_rendered',
470472};
471473
472474export const eventSource = new EventEmitter();
@@ -4421,7 +4423,9 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro
44214423 getMessage = continue_mag + getMessage;
44224424 }
44234425
44244426 ifconst (canPerformToolCallsisStreamFinished = streamingProcessor && Array.isArray(!streamingProcessor.toolCalls)isStopped && streamingProcessor.toolCalls.length) {isFinished;
4427+ const isStreamWithToolCalls = streamingProcessor && Array.isArray(streamingProcessor.toolCalls) && streamingProcessor.toolCalls.length;
4428+ if (canPerformToolCalls && isStreamFinished && isStreamWithToolCalls) {
44254429 const lastMessage = chat[chat.length - 1];
44264430 const hasToolCalls = ToolManager.hasToolCalls(streamingProcessor.toolCalls);
44274431 const shouldDeleteMessage = ['', '...'].includes(lastMessage?.mes) && ['', '...'].includes(streamingProcessor?.result);
@@ -4437,12 +4441,12 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro
44374441 }
44384442
44394443 streamingProcessor = null;
44404444 await ToolManager.saveFunctionToolInvocations(invocationResult.invocations);
44414445 return Generate(type, { automatic_trigger, force_name2, quiet_prompt, quietToLoud, skipWIAN, force_chid, signal, quietImage, quietName }, dryRun);
44424446 }
44434447 }
44444448
4445- if (streamingProcessor && !streamingProcessor.isStopped && streamingProcessor.isFinished) {
4449+ if (isStreamFinished) {
44464450 await streamingProcessor.onFinishStreaming(streamingProcessor.messageId, getMessage);
44474451 streamingProcessor = null;
44484452 triggerAutoContinue(messageChunk, isImpersonate);
@@ -4529,7 +4533,7 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro
45294533 return;
45304534 }
45314535
45324536 await ToolManager.saveFunctionToolInvocations(invocationResult.invocations);
45334537 return Generate(type, { automatic_trigger, force_name2, quiet_prompt, quietToLoud, skipWIAN, force_chid, signal, quietImage, quietName }, dryRun);
45344538 }
45354539 }
public/scripts/tool-calling.js+5 -2
@@ -1,4 +1,4 @@
11import { addOneMessage, chat, event_types, eventSource, main_api, saveChatConditional, system_avatar, systemUserName } from '../script.js';
22import { chat_completion_sources, oai_settings } from './openai.js';
33import { Popup } from './popup.js';
44
@@ -542,7 +542,7 @@ export class ToolManager {
542542 * Saves function tool invocations to the last user chat message extra metadata.
543543 * @param {ToolInvocation[]} invocations Successful tool invocations
544544 */
545545 static async saveFunctionToolInvocations(invocations) {
546546 if (!Array.isArray(invocations) || invocations.length === 0) {
547547 return;
548548 }
@@ -558,7 +558,10 @@ export class ToolManager {
558558 },
559559 };
560560 chat.push(message);
561+ await eventSource.emit(event_types.TOOL_CALLS_PERFORMED, invocations);
561562 addOneMessage(message);
563+ await eventSource.emit(event_types.TOOL_CALLS_RENDERED, invocations);
564+ await saveChatConditional();
562565 }
563566
564567 /**