Don't call tools on stopped streams. Emit events

This commit is contained in:
Cohee
2024-10-05 19:08:57 +03:00
parent 6185974e17
commit 47e3cf82e0
2 changed files with 13 additions and 6 deletions

View File

@ -467,6 +467,8 @@ export const event_types = {
ONLINE_STATUS_CHANGED: 'online_status_changed',
IMAGE_SWIPED: 'image_swiped',
CONNECTION_PROFILE_LOADED: 'connection_profile_loaded',
TOOL_CALLS_PERFORMED: 'tool_calls_performed',
TOOL_CALLS_RENDERED: 'tool_calls_rendered',
};
export const eventSource = new EventEmitter();
@ -4421,7 +4423,9 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro
getMessage = continue_mag + getMessage;
}
if (canPerformToolCalls && Array.isArray(streamingProcessor.toolCalls) && streamingProcessor.toolCalls.length) {
const isStreamFinished = streamingProcessor && !streamingProcessor.isStopped && streamingProcessor.isFinished;
const isStreamWithToolCalls = streamingProcessor && Array.isArray(streamingProcessor.toolCalls) && streamingProcessor.toolCalls.length;
if (canPerformToolCalls && isStreamFinished && isStreamWithToolCalls) {
const lastMessage = chat[chat.length - 1];
const hasToolCalls = ToolManager.hasToolCalls(streamingProcessor.toolCalls);
const shouldDeleteMessage = ['', '...'].includes(lastMessage?.mes) && ['', '...'].includes(streamingProcessor?.result);
@ -4437,12 +4441,12 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro
}
streamingProcessor = null;
ToolManager.saveFunctionToolInvocations(invocationResult.invocations);
await ToolManager.saveFunctionToolInvocations(invocationResult.invocations);
return Generate(type, { automatic_trigger, force_name2, quiet_prompt, quietToLoud, skipWIAN, force_chid, signal, quietImage, quietName }, dryRun);
}
}
if (streamingProcessor && !streamingProcessor.isStopped && streamingProcessor.isFinished) {
if (isStreamFinished) {
await streamingProcessor.onFinishStreaming(streamingProcessor.messageId, getMessage);
streamingProcessor = null;
triggerAutoContinue(messageChunk, isImpersonate);
@ -4529,7 +4533,7 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro
return;
}
ToolManager.saveFunctionToolInvocations(invocationResult.invocations);
await ToolManager.saveFunctionToolInvocations(invocationResult.invocations);
return Generate(type, { automatic_trigger, force_name2, quiet_prompt, quietToLoud, skipWIAN, force_chid, signal, quietImage, quietName }, dryRun);
}
}