Don't call tools on stopped streams. Emit events

47e3cf82e09279efc498325abdab7d42db8b0bd5

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

2 files changed, +13 -6Showing whitespace changes
public/script.js+8 -4
@@ -467,6 +467,8 @@ export const event_types = {
467 ONLINE_STATUS_CHANGED: 'online_status_changed',467 ONLINE_STATUS_CHANGED: 'online_status_changed',
468 IMAGE_SWIPED: 'image_swiped',468 IMAGE_SWIPED: 'image_swiped',
469 CONNECTION_PROFILE_LOADED: 'connection_profile_loaded',469 CONNECTION_PROFILE_LOADED: 'connection_profile_loaded',
470 TOOL_CALLS_PERFORMED: 'tool_calls_performed',
471 TOOL_CALLS_RENDERED: 'tool_calls_rendered',
470};472};
471473
472export const eventSource = new EventEmitter();474export const eventSource = new EventEmitter();
@@ -4421,7 +4423,9 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro
4421 getMessage = continue_mag + getMessage;4423 getMessage = continue_mag + getMessage;
4422 }4424 }
44234425
4424 if (canPerformToolCalls && Array.isArray(streamingProcessor.toolCalls) && streamingProcessor.toolCalls.length) {4426 const isStreamFinished = streamingProcessor && !streamingProcessor.isStopped && streamingProcessor.isFinished;
4427 const isStreamWithToolCalls = streamingProcessor && Array.isArray(streamingProcessor.toolCalls) && streamingProcessor.toolCalls.length;
4428 if (canPerformToolCalls && isStreamFinished && isStreamWithToolCalls) {
4425 const lastMessage = chat[chat.length - 1];4429 const lastMessage = chat[chat.length - 1];
4426 const hasToolCalls = ToolManager.hasToolCalls(streamingProcessor.toolCalls);4430 const hasToolCalls = ToolManager.hasToolCalls(streamingProcessor.toolCalls);
4427 const shouldDeleteMessage = ['', '...'].includes(lastMessage?.mes) && ['', '...'].includes(streamingProcessor?.result);4431 const shouldDeleteMessage = ['', '...'].includes(lastMessage?.mes) && ['', '...'].includes(streamingProcessor?.result);
@@ -4437,12 +4441,12 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro
4437 }4441 }
44384442
4439 streamingProcessor = null;4443 streamingProcessor = null;
4440 ToolManager.saveFunctionToolInvocations(invocationResult.invocations);4444 await ToolManager.saveFunctionToolInvocations(invocationResult.invocations);
4441 return Generate(type, { automatic_trigger, force_name2, quiet_prompt, quietToLoud, skipWIAN, force_chid, signal, quietImage, quietName }, dryRun);4445 return Generate(type, { automatic_trigger, force_name2, quiet_prompt, quietToLoud, skipWIAN, force_chid, signal, quietImage, quietName }, dryRun);
4442 }4446 }
4443 }4447 }
44444448
4445 if (streamingProcessor && !streamingProcessor.isStopped && streamingProcessor.isFinished) {4449 if (isStreamFinished) {
4446 await streamingProcessor.onFinishStreaming(streamingProcessor.messageId, getMessage);4450 await streamingProcessor.onFinishStreaming(streamingProcessor.messageId, getMessage);
4447 streamingProcessor = null;4451 streamingProcessor = null;
4448 triggerAutoContinue(messageChunk, isImpersonate);4452 triggerAutoContinue(messageChunk, isImpersonate);
@@ -4529,7 +4533,7 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro
4529 return;4533 return;
4530 }4534 }
45314535
4532 ToolManager.saveFunctionToolInvocations(invocationResult.invocations);4536 await ToolManager.saveFunctionToolInvocations(invocationResult.invocations);
4533 return Generate(type, { automatic_trigger, force_name2, quiet_prompt, quietToLoud, skipWIAN, force_chid, signal, quietImage, quietName }, dryRun);4537 return Generate(type, { automatic_trigger, force_name2, quiet_prompt, quietToLoud, skipWIAN, force_chid, signal, quietImage, quietName }, dryRun);
4534 }4538 }
4535 }4539 }
public/scripts/tool-calling.js+5 -2
@@ -1,4 +1,4 @@
1import { addOneMessage, chat, main_api, system_avatar, systemUserName } from '../script.js';1import { addOneMessage, chat, event_types, eventSource, main_api, saveChatConditional, system_avatar, systemUserName } from '../script.js';
2import { chat_completion_sources, oai_settings } from './openai.js';2import { chat_completion_sources, oai_settings } from './openai.js';
3import { Popup } from './popup.js';3import { Popup } from './popup.js';
44
@@ -542,7 +542,7 @@ export class ToolManager {
542 * Saves function tool invocations to the last user chat message extra metadata.542 * Saves function tool invocations to the last user chat message extra metadata.
543 * @param {ToolInvocation[]} invocations Successful tool invocations543 * @param {ToolInvocation[]} invocations Successful tool invocations
544 */544 */
545 static saveFunctionToolInvocations(invocations) {545 static async saveFunctionToolInvocations(invocations) {
546 if (!Array.isArray(invocations) || invocations.length === 0) {546 if (!Array.isArray(invocations) || invocations.length === 0) {
547 return;547 return;
548 }548 }
@@ -558,7 +558,10 @@ export class ToolManager {
558 },558 },
559 };559 };
560 chat.push(message);560 chat.push(message);
561 await eventSource.emit(event_types.TOOL_CALLS_PERFORMED, invocations);
561 addOneMessage(message);562 addOneMessage(message);
563 await eventSource.emit(event_types.TOOL_CALLS_RENDERED, invocations);
564 await saveChatConditional();
562 }565 }
563566
564 /**567 /**