refactor: move chat/message event handlers into event binding controller

This commit is contained in:
Youzini-afk
2026-03-29 17:06:05 +08:00
parent ca2eb35ec5
commit e29e413516
2 changed files with 123 additions and 33 deletions

View File

@@ -107,3 +107,63 @@ export function registerCoreEventHooksController(runtime) {
eventSource.on(eventTypes.MESSAGE_UPDATED, handlers.onMessageEdited);
}
}
export function onChatChangedController(runtime) {
runtime.clearPendingHistoryMutationChecks();
runtime.clearTimeout(runtime.getPendingHistoryRecoveryTimer());
runtime.setPendingHistoryRecoveryTimer(null);
runtime.setPendingHistoryRecoveryTrigger("");
runtime.clearPendingGraphLoadRetry();
runtime.setSkipBeforeCombineRecallUntil(0);
runtime.setLastPreGenerationRecallKey("");
runtime.setLastPreGenerationRecallAt(0);
runtime.clearGenerationRecallTransactionsForChat("", { clearAll: true });
runtime.abortAllRunningStages();
runtime.dismissAllStageNotices();
runtime.syncGraphLoadFromLiveContext({
source: "chat-changed",
force: true,
});
runtime.clearInjectionState();
runtime.clearRecallInputTracking();
runtime.installSendIntentHooks();
}
export function onChatLoadedController(runtime) {
runtime.syncGraphLoadFromLiveContext({
source: "chat-loaded",
});
}
export function onMessageSentController(runtime, messageId) {
const context = runtime.getContext();
const chat = context?.chat;
const message =
Array.isArray(chat) && Number.isFinite(messageId) ? chat[messageId] : null;
if (!message?.is_user) return;
runtime.recordRecallSentUserMessage(messageId, message.mes || "");
}
export function onMessageDeletedController(
runtime,
chatLengthOrMessageId,
meta = null,
) {
runtime.invalidateRecallAfterHistoryMutation("消息已删除");
runtime.scheduleHistoryMutationRecheck(
"message-deleted",
chatLengthOrMessageId,
meta,
);
}
export function onMessageEditedController(runtime, messageId, meta = null) {
runtime.invalidateRecallAfterHistoryMutation("消息已编辑");
runtime.scheduleHistoryMutationRecheck("message-edited", messageId, meta);
}
export function onMessageSwipedController(runtime, messageId, meta = null) {
runtime.invalidateRecallAfterHistoryMutation("已切换楼层 swipe");
runtime.scheduleHistoryMutationRecheck("message-swiped", messageId, meta);
}

View File

@@ -58,6 +58,12 @@ import { getNodeDisplayName } from "./node-labels.js";
import { showManagedBmeNotice } from "./notice.js";
import {
installSendIntentHooksController,
onChatChangedController,
onChatLoadedController,
onMessageDeletedController,
onMessageEditedController,
onMessageSentController,
onMessageSwipedController,
registerBeforeCombinePromptsController,
registerCoreEventHooksController,
registerGenerationAfterCommandsController,
@@ -4323,59 +4329,83 @@ async function runRecall(options = {}) {
// ==================== 事件钩子 ====================
function onChatChanged() {
clearPendingHistoryMutationChecks();
clearTimeout(pendingHistoryRecoveryTimer);
pendingHistoryRecoveryTimer = null;
pendingHistoryRecoveryTrigger = "";
clearPendingGraphLoadRetry();
skipBeforeCombineRecallUntil = 0;
lastPreGenerationRecallKey = "";
lastPreGenerationRecallAt = 0;
clearGenerationRecallTransactionsForChat("", { clearAll: true });
abortAllRunningStages();
dismissAllStageNotices();
syncGraphLoadFromLiveContext({
source: "chat-changed",
force: true,
return onChatChangedController({
abortAllRunningStages,
clearGenerationRecallTransactionsForChat,
clearInjectionState,
clearPendingGraphLoadRetry,
clearPendingHistoryMutationChecks,
clearRecallInputTracking,
clearTimeout,
dismissAllStageNotices,
getPendingHistoryRecoveryTimer: () => pendingHistoryRecoveryTimer,
installSendIntentHooks,
setLastPreGenerationRecallAt: (value) => {
lastPreGenerationRecallAt = value;
},
setLastPreGenerationRecallKey: (value) => {
lastPreGenerationRecallKey = value;
},
setPendingHistoryRecoveryTimer: (value) => {
pendingHistoryRecoveryTimer = value;
},
setPendingHistoryRecoveryTrigger: (value) => {
pendingHistoryRecoveryTrigger = value;
},
setSkipBeforeCombineRecallUntil: (value) => {
skipBeforeCombineRecallUntil = value;
},
syncGraphLoadFromLiveContext,
});
clearInjectionState();
clearRecallInputTracking();
installSendIntentHooks();
}
function onChatLoaded() {
syncGraphLoadFromLiveContext({
source: "chat-loaded",
return onChatLoadedController({
syncGraphLoadFromLiveContext,
});
}
function onMessageSent(messageId) {
const context = getContext();
const chat = context?.chat;
const message =
Array.isArray(chat) && Number.isFinite(messageId) ? chat[messageId] : null;
if (!message?.is_user) return;
recordRecallSentUserMessage(messageId, message.mes || "");
return onMessageSentController(
{
getContext,
recordRecallSentUserMessage,
},
messageId,
);
}
function onMessageDeleted(chatLengthOrMessageId, meta = null) {
invalidateRecallAfterHistoryMutation("消息已删除");
scheduleHistoryMutationRecheck(
"message-deleted",
return onMessageDeletedController(
{
invalidateRecallAfterHistoryMutation,
scheduleHistoryMutationRecheck,
},
chatLengthOrMessageId,
meta,
);
}
function onMessageEdited(messageId, meta = null) {
invalidateRecallAfterHistoryMutation("消息已编辑");
scheduleHistoryMutationRecheck("message-edited", messageId, meta);
return onMessageEditedController(
{
invalidateRecallAfterHistoryMutation,
scheduleHistoryMutationRecheck,
},
messageId,
meta,
);
}
function onMessageSwiped(messageId, meta = null) {
invalidateRecallAfterHistoryMutation("已切换楼层 swipe");
scheduleHistoryMutationRecheck("message-swiped", messageId, meta);
return onMessageSwipedController(
{
invalidateRecallAfterHistoryMutation,
scheduleHistoryMutationRecheck,
},
messageId,
meta,
);
}
async function onGenerationAfterCommands(type, params = {}, dryRun = false) {