Don't call tools on stopped streams. Emit events
| @@ -467,6 +467,8 @@ export const event_types = { | ||
| 467 | 467 | ONLINE_STATUS_CHANGED: 'online_status_changed', |
| 468 | 468 | IMAGE_SWIPED: 'image_swiped', |
| 469 | 469 | CONNECTION_PROFILE_LOADED: 'connection_profile_loaded', |
| 470 | + TOOL_CALLS_PERFORMED: 'tool_calls_performed', | |
| 471 | + TOOL_CALLS_RENDERED: 'tool_calls_rendered', | |
| 470 | 472 | }; |
| 471 | 473 | |
| 472 | 474 | export const eventSource = new EventEmitter(); |
| @@ -4421,7 +4423,9 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro | ||
| 4421 | 4423 | getMessage = continue_mag + getMessage; |
| 4422 | 4424 | } |
| 4423 | 4425 | |
| 4424 | 4426 | 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) { | |
| 4425 | 4429 | const lastMessage = chat[chat.length - 1]; |
| 4426 | 4430 | const hasToolCalls = ToolManager.hasToolCalls(streamingProcessor.toolCalls); |
| 4427 | 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 | } |
| 4438 | 4442 | |
| 4439 | 4443 | streamingProcessor = null; |
| 4440 | 4444 | await ToolManager.saveFunctionToolInvocations(invocationResult.invocations); |
| 4441 | 4445 | return Generate(type, { automatic_trigger, force_name2, quiet_prompt, quietToLoud, skipWIAN, force_chid, signal, quietImage, quietName }, dryRun); |
| 4442 | 4446 | } |
| 4443 | 4447 | } |
| 4444 | 4448 | |
| 4445 | - if (streamingProcessor && !streamingProcessor.isStopped && streamingProcessor.isFinished) { | |
| 4449 | + if (isStreamFinished) { | |
| 4446 | 4450 | await streamingProcessor.onFinishStreaming(streamingProcessor.messageId, getMessage); |
| 4447 | 4451 | streamingProcessor = null; |
| 4448 | 4452 | triggerAutoContinue(messageChunk, isImpersonate); |
| @@ -4529,7 +4533,7 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro | ||
| 4529 | 4533 | return; |
| 4530 | 4534 | } |
| 4531 | 4535 | |
| 4532 | 4536 | await ToolManager.saveFunctionToolInvocations(invocationResult.invocations); |
| 4533 | 4537 | return Generate(type, { automatic_trigger, force_name2, quiet_prompt, quietToLoud, skipWIAN, force_chid, signal, quietImage, quietName }, dryRun); |
| 4534 | 4538 | } |
| 4535 | 4539 | } |
| @@ -1,4 +1,4 @@ | ||
| 1 | 1 | import { addOneMessage, chat, event_types, eventSource, main_api, saveChatConditional, system_avatar, systemUserName } from '../script.js'; |
| 2 | 2 | import { chat_completion_sources, oai_settings } from './openai.js'; |
| 3 | 3 | import { Popup } from './popup.js'; |
| 4 | 4 | |
| @@ -542,7 +542,7 @@ export class ToolManager { | ||
| 542 | 542 | * Saves function tool invocations to the last user chat message extra metadata. |
| 543 | 543 | * @param {ToolInvocation[]} invocations Successful tool invocations |
| 544 | 544 | */ |
| 545 | 545 | static async saveFunctionToolInvocations(invocations) { |
| 546 | 546 | if (!Array.isArray(invocations) || invocations.length === 0) { |
| 547 | 547 | return; |
| 548 | 548 | } |
| @@ -558,7 +558,10 @@ export class ToolManager { | ||
| 558 | 558 | }, |
| 559 | 559 | }; |
| 560 | 560 | chat.push(message); |
| 561 | + await eventSource.emit(event_types.TOOL_CALLS_PERFORMED, invocations); | |
| 561 | 562 | addOneMessage(message); |
| 563 | + await eventSource.emit(event_types.TOOL_CALLS_RENDERED, invocations); | |
| 564 | + await saveChatConditional(); | |
| 562 | 565 | } |
| 563 | 566 | |
| 564 | 567 | /** |