New context methods, added type parameters to message events

This commit is contained in:
bmen25124
2025-03-12 21:21:23 +03:00
parent 1b02426df1
commit ddb77732f2
5 changed files with 29 additions and 24 deletions

View File

@ -3373,8 +3373,8 @@ class StreamingProcessor {
} }
if (this.type !== 'impersonate') { if (this.type !== 'impersonate') {
await eventSource.emit(event_types.MESSAGE_RECEIVED, this.messageId); await eventSource.emit(event_types.MESSAGE_RECEIVED, this.messageId, this.type);
await eventSource.emit(event_types.CHARACTER_MESSAGE_RENDERED, this.messageId); await eventSource.emit(event_types.CHARACTER_MESSAGE_RENDERED, this.messageId, this.type);
} else { } else {
await eventSource.emit(event_types.IMPERSONATE_READY, text); await eventSource.emit(event_types.IMPERSONATE_READY, text);
} }
@ -3403,8 +3403,8 @@ class StreamingProcessor {
const noEmitTypes = ['swipe', 'impersonate', 'continue']; const noEmitTypes = ['swipe', 'impersonate', 'continue'];
if (!noEmitTypes.includes(this.type)) { if (!noEmitTypes.includes(this.type)) {
eventSource.emit(event_types.MESSAGE_RECEIVED, this.messageId); eventSource.emit(event_types.MESSAGE_RECEIVED, this.messageId, this.type);
eventSource.emit(event_types.CHARACTER_MESSAGE_RENDERED, this.messageId); eventSource.emit(event_types.CHARACTER_MESSAGE_RENDERED, this.messageId, this.type);
} }
} }
@ -6015,9 +6015,9 @@ export async function saveReply(type, getMessage, fromStreaming, title, swipes,
chat[chat.length - 1]['extra']['token_count'] = await getTokenCountAsync(tokenCountText, 0); chat[chat.length - 1]['extra']['token_count'] = await getTokenCountAsync(tokenCountText, 0);
} }
const chat_id = (chat.length - 1); const chat_id = (chat.length - 1);
await eventSource.emit(event_types.MESSAGE_RECEIVED, chat_id); await eventSource.emit(event_types.MESSAGE_RECEIVED, chat_id, type);
addOneMessage(chat[chat_id], { type: 'swipe' }); addOneMessage(chat[chat_id], { type: 'swipe' });
await eventSource.emit(event_types.CHARACTER_MESSAGE_RENDERED, chat_id); await eventSource.emit(event_types.CHARACTER_MESSAGE_RENDERED, chat_id, type);
} else { } else {
chat[chat.length - 1]['mes'] = getMessage; chat[chat.length - 1]['mes'] = getMessage;
} }
@ -6038,9 +6038,9 @@ export async function saveReply(type, getMessage, fromStreaming, title, swipes,
chat[chat.length - 1]['extra']['token_count'] = await getTokenCountAsync(tokenCountText, 0); chat[chat.length - 1]['extra']['token_count'] = await getTokenCountAsync(tokenCountText, 0);
} }
const chat_id = (chat.length - 1); const chat_id = (chat.length - 1);
await eventSource.emit(event_types.MESSAGE_RECEIVED, chat_id); await eventSource.emit(event_types.MESSAGE_RECEIVED, chat_id, type);
addOneMessage(chat[chat_id], { type: 'swipe' }); addOneMessage(chat[chat_id], { type: 'swipe' });
await eventSource.emit(event_types.CHARACTER_MESSAGE_RENDERED, chat_id); await eventSource.emit(event_types.CHARACTER_MESSAGE_RENDERED, chat_id, type);
} else if (type === 'appendFinal') { } else if (type === 'appendFinal') {
oldMessage = chat[chat.length - 1]['mes']; oldMessage = chat[chat.length - 1]['mes'];
console.debug('Trying to appendFinal.'); console.debug('Trying to appendFinal.');
@ -6058,9 +6058,9 @@ export async function saveReply(type, getMessage, fromStreaming, title, swipes,
chat[chat.length - 1]['extra']['token_count'] = await getTokenCountAsync(tokenCountText, 0); chat[chat.length - 1]['extra']['token_count'] = await getTokenCountAsync(tokenCountText, 0);
} }
const chat_id = (chat.length - 1); const chat_id = (chat.length - 1);
await eventSource.emit(event_types.MESSAGE_RECEIVED, chat_id); await eventSource.emit(event_types.MESSAGE_RECEIVED, chat_id, type);
addOneMessage(chat[chat_id], { type: 'swipe' }); addOneMessage(chat[chat_id], { type: 'swipe' });
await eventSource.emit(event_types.CHARACTER_MESSAGE_RENDERED, chat_id); await eventSource.emit(event_types.CHARACTER_MESSAGE_RENDERED, chat_id, type);
} else { } else {
console.debug('entering chat update routine for non-swipe post'); console.debug('entering chat update routine for non-swipe post');
@ -6100,9 +6100,9 @@ export async function saveReply(type, getMessage, fromStreaming, title, swipes,
saveImageToMessage(img, chat[chat.length - 1]); saveImageToMessage(img, chat[chat.length - 1]);
const chat_id = (chat.length - 1); const chat_id = (chat.length - 1);
!fromStreaming && await eventSource.emit(event_types.MESSAGE_RECEIVED, chat_id); !fromStreaming && await eventSource.emit(event_types.MESSAGE_RECEIVED, chat_id, type);
addOneMessage(chat[chat_id]); addOneMessage(chat[chat_id]);
!fromStreaming && await eventSource.emit(event_types.CHARACTER_MESSAGE_RENDERED, chat_id); !fromStreaming && await eventSource.emit(event_types.CHARACTER_MESSAGE_RENDERED, chat_id, type);
} }
const item = chat[chat.length - 1]; const item = chat[chat.length - 1];
@ -6850,8 +6850,8 @@ async function getChatResult() {
if (chat.length === 1) { if (chat.length === 1) {
const chat_id = (chat.length - 1); const chat_id = (chat.length - 1);
await eventSource.emit(event_types.MESSAGE_RECEIVED, chat_id); await eventSource.emit(event_types.MESSAGE_RECEIVED, chat_id, 'first_message');
await eventSource.emit(event_types.CHARACTER_MESSAGE_RENDERED, chat_id); await eventSource.emit(event_types.CHARACTER_MESSAGE_RENDERED, chat_id, 'first_message');
} }
} }
@ -8729,10 +8729,10 @@ async function createOrEditCharacter(e) {
if (shouldRegenerateMessage) { if (shouldRegenerateMessage) {
chat.splice(0, chat.length, message); chat.splice(0, chat.length, message);
const messageId = (chat.length - 1); const messageId = (chat.length - 1);
await eventSource.emit(event_types.MESSAGE_RECEIVED, messageId); await eventSource.emit(event_types.MESSAGE_RECEIVED, messageId, 'first_message');
await clearChat(); await clearChat();
await printMessages(); await printMessages();
await eventSource.emit(event_types.CHARACTER_MESSAGE_RENDERED, messageId); await eventSource.emit(event_types.CHARACTER_MESSAGE_RENDERED, messageId, 'first_message');
await saveChatConditional(); await saveChatConditional();
} }
} catch (error) { } catch (error) {

View File

@ -3739,9 +3739,9 @@ async function sendMessage(prompt, image, generationType, additionalNegativePref
}; };
context.chat.push(message); context.chat.push(message);
const messageId = context.chat.length - 1; const messageId = context.chat.length - 1;
await eventSource.emit(event_types.MESSAGE_RECEIVED, messageId); await eventSource.emit(event_types.MESSAGE_RECEIVED, messageId, 'stable-diffusion');
context.addOneMessage(message); context.addOneMessage(message);
await eventSource.emit(event_types.CHARACTER_MESSAGE_RENDERED, messageId); await eventSource.emit(event_types.CHARACTER_MESSAGE_RENDERED, messageId, 'stable-diffusion');
await context.saveChat(); await context.saveChat();
} }

View File

@ -246,9 +246,9 @@ export async function getGroupChat(groupId, reload = false) {
} }
chat.push(mes); chat.push(mes);
await eventSource.emit(event_types.MESSAGE_RECEIVED, (chat.length - 1)); await eventSource.emit(event_types.MESSAGE_RECEIVED, (chat.length - 1), 'group_first_message');
addOneMessage(mes); addOneMessage(mes);
await eventSource.emit(event_types.CHARACTER_MESSAGE_RENDERED, (chat.length - 1)); await eventSource.emit(event_types.CHARACTER_MESSAGE_RENDERED, (chat.length - 1), 'group_first_message');
} }
} }
await saveGroupChat(groupId, false); await saveGroupChat(groupId, false);

View File

@ -3628,14 +3628,14 @@ export async function sendMessageAs(args, text) {
if (!isNaN(insertAt) && insertAt >= 0 && insertAt <= chat.length) { if (!isNaN(insertAt) && insertAt >= 0 && insertAt <= chat.length) {
chat.splice(insertAt, 0, message); chat.splice(insertAt, 0, message);
await saveChatConditional(); await saveChatConditional();
await eventSource.emit(event_types.MESSAGE_RECEIVED, insertAt); await eventSource.emit(event_types.MESSAGE_RECEIVED, insertAt, 'command');
await reloadCurrentChat(); await reloadCurrentChat();
await eventSource.emit(event_types.CHARACTER_MESSAGE_RENDERED, insertAt); await eventSource.emit(event_types.CHARACTER_MESSAGE_RENDERED, insertAt, 'command');
} else { } else {
chat.push(message); chat.push(message);
await eventSource.emit(event_types.MESSAGE_RECEIVED, (chat.length - 1)); await eventSource.emit(event_types.MESSAGE_RECEIVED, (chat.length - 1), 'command');
addOneMessage(message); addOneMessage(message);
await eventSource.emit(event_types.CHARACTER_MESSAGE_RENDERED, (chat.length - 1)); await eventSource.emit(event_types.CHARACTER_MESSAGE_RENDERED, (chat.length - 1), 'command');
await saveChatConditional(); await saveChatConditional();
} }

View File

@ -48,6 +48,7 @@ import {
printMessages, printMessages,
clearChat, clearChat,
unshallowCharacter, unshallowCharacter,
deleteLastMessage,
} from '../script.js'; } from '../script.js';
import { import {
extension_settings, extension_settings,
@ -106,6 +107,7 @@ export function getContext() {
eventSource, eventSource,
eventTypes: event_types, eventTypes: event_types,
addOneMessage, addOneMessage,
deleteLastMessage,
generate: Generate, generate: Generate,
sendStreamingRequest, sendStreamingRequest,
sendGenerationRequest, sendGenerationRequest,
@ -147,6 +149,9 @@ export function getContext() {
unregisterFunctionTool: ToolManager.unregisterFunctionTool.bind(ToolManager), unregisterFunctionTool: ToolManager.unregisterFunctionTool.bind(ToolManager),
isToolCallingSupported: ToolManager.isToolCallingSupported.bind(ToolManager), isToolCallingSupported: ToolManager.isToolCallingSupported.bind(ToolManager),
canPerformToolCalls: ToolManager.canPerformToolCalls.bind(ToolManager), canPerformToolCalls: ToolManager.canPerformToolCalls.bind(ToolManager),
registerFunctionToolsOpenAI: ToolManager.registerFunctionToolsOpenAI.bind(ToolManager),
invokeFunctionTools: ToolManager.invokeFunctionTools.bind(ToolManager),
saveFunctionToolInvocations: ToolManager.saveFunctionToolInvocations.bind(ToolManager),
registerDebugFunction, registerDebugFunction,
/** @deprecated Use renderExtensionTemplateAsync instead. */ /** @deprecated Use renderExtensionTemplateAsync instead. */
renderExtensionTemplate, renderExtensionTemplate,