Don't call tools on stopped streams. Emit events
| @@ -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 | }; |
| 471 | 473 | ||
| 472 | export const eventSource = new EventEmitter(); | 474 | export 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 | } |
| 4423 | 4425 | ||
| 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 | } |
| 4438 | 4442 | ||
| 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 | } |
| 4444 | 4448 | ||
| 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 | } |
| 4531 | 4535 | ||
| 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 | } |
| @@ -1,4 +1,4 @@ | |||
| 1 | import { addOneMessage, chat, main_api, system_avatar, systemUserName } from '../script.js'; | 1 | import { addOneMessage, chat, event_types, eventSource, main_api, saveChatConditional, system_avatar, systemUserName } from '../script.js'; |
| 2 | import { chat_completion_sources, oai_settings } from './openai.js'; | 2 | import { chat_completion_sources, oai_settings } from './openai.js'; |
| 3 | import { Popup } from './popup.js'; | 3 | import { Popup } from './popup.js'; |
| 4 | 4 | ||
| @@ -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 invocations | 543 | * @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 | } |
| 563 | 566 | ||
| 564 | /** | 567 | /** |