From 33afa996aa13873e7b267b583b67f77f6fab3a6e Mon Sep 17 00:00:00 2001 From: Youzini-afk <13153778771cx@gmail.com> Date: Sun, 29 Mar 2026 17:24:57 +0800 Subject: [PATCH] refactor: move message received hook into event binding controller --- event-binding.js | 36 ++++++++++++++++++++++++++ index.js | 50 +++++++++++++++---------------------- tests/graph-persistence.mjs | 1 + 3 files changed, 57 insertions(+), 30 deletions(-) diff --git a/event-binding.js b/event-binding.js index ff5ff0b..8e95f00 100644 --- a/event-binding.js +++ b/event-binding.js @@ -245,3 +245,39 @@ export async function onBeforeCombinePromptsController(runtime) { runtime.getGenerationRecallHookStateFromResult(recallResult), ); } + +export function onMessageReceivedController(runtime) { + if (runtime.getCurrentGraph()) { + if ( + runtime.getGraphPersistenceState()?.pendingPersist && + runtime.isGraphMetadataWriteAllowed() + ) { + runtime.maybeFlushQueuedGraphPersist("message-received-pending-flush"); + } + runtime.maybeCaptureGraphShadowSnapshot("message-received-passive-sync"); + } + + const pendingRecallSendIntent = runtime.getPendingRecallSendIntent(); + if ( + pendingRecallSendIntent?.text && + !runtime.isFreshRecallInputRecord(pendingRecallSendIntent) + ) { + runtime.setPendingRecallSendIntent(runtime.createRecallInputRecord()); + } + + const context = runtime.getContext(); + const chat = context?.chat; + const lastMessage = + Array.isArray(chat) && chat.length > 0 ? chat[chat.length - 1] : null; + + if (runtime.isAssistantChatMessage(lastMessage)) { + runtime.queueMicrotask(() => { + void runtime.runExtraction().catch((error) => { + runtime.console.error("[ST-BME] 异步自动提取失败:", error); + runtime.notifyExtractionIssue( + error?.message || String(error) || "自动提取失败", + ); + }); + }); + } +} diff --git a/index.js b/index.js index 171782b..e24edf6 100644 --- a/index.js +++ b/index.js @@ -64,6 +64,7 @@ import { onGenerationAfterCommandsController, onMessageDeletedController, onMessageEditedController, + onMessageReceivedController, onMessageSentController, onMessageSwipedController, registerBeforeCombinePromptsController, @@ -4456,36 +4457,25 @@ async function onBeforeCombinePrompts() { } function onMessageReceived() { - // 新消息到达,图状态可能需要更新 - if (currentGraph) { - if (graphPersistenceState.pendingPersist && isGraphMetadataWriteAllowed()) { - maybeFlushQueuedGraphPersist("message-received-pending-flush"); - } - maybeCaptureGraphShadowSnapshot("message-received-passive-sync"); - } - - if ( - pendingRecallSendIntent.text && - !isFreshRecallInputRecord(pendingRecallSendIntent) - ) { - pendingRecallSendIntent = createRecallInputRecord(); - } - - const context = getContext(); - const chat = context?.chat; - const lastMessage = - Array.isArray(chat) && chat.length > 0 ? chat[chat.length - 1] : null; - - if (isAssistantChatMessage(lastMessage)) { - queueMicrotask(() => { - void runExtraction().catch((error) => { - console.error("[ST-BME] 异步自动提取失败:", error); - notifyExtractionIssue( - error?.message || String(error) || "自动提取失败", - ); - }); - }); - } + return onMessageReceivedController({ + console, + createRecallInputRecord, + getContext, + getCurrentGraph: () => currentGraph, + getGraphPersistenceState: () => graphPersistenceState, + getPendingRecallSendIntent: () => pendingRecallSendIntent, + isAssistantChatMessage, + isFreshRecallInputRecord, + isGraphMetadataWriteAllowed, + maybeCaptureGraphShadowSnapshot, + maybeFlushQueuedGraphPersist, + notifyExtractionIssue, + queueMicrotask, + runExtraction, + setPendingRecallSendIntent: (record) => { + pendingRecallSendIntent = record; + }, + }); } // ==================== UI 操作 ==================== diff --git a/tests/graph-persistence.mjs b/tests/graph-persistence.mjs index 3324e45..2585831 100644 --- a/tests/graph-persistence.mjs +++ b/tests/graph-persistence.mjs @@ -48,6 +48,7 @@ import { writeChatMetadataPatch, writeGraphShadowSnapshot, } from "../graph-persistence.js"; +import { onMessageReceivedController } from "../event-binding.js"; const moduleDir = path.dirname(fileURLToPath(import.meta.url)); const indexPath = path.resolve(moduleDir, "../index.js");