From d3479bc672573a0e191a6994f331c727ac33a531 Mon Sep 17 00:00:00 2001 From: Youzini-afk <13153778771cx@gmail.com> Date: Sun, 29 Mar 2026 17:09:09 +0800 Subject: [PATCH] refactor: controllerize generation recall event hooks --- event-binding.js | 78 +++++++++++++++++++++++++++++++++++++ index.js | 83 ++++++++++------------------------------ tests/p0-regressions.mjs | 6 +++ 3 files changed, 104 insertions(+), 63 deletions(-) diff --git a/event-binding.js b/event-binding.js index 27091d6..ff5ff0b 100644 --- a/event-binding.js +++ b/event-binding.js @@ -167,3 +167,81 @@ export function onMessageSwipedController(runtime, messageId, meta = null) { runtime.invalidateRecallAfterHistoryMutation("已切换楼层 swipe"); runtime.scheduleHistoryMutationRecheck("message-swiped", messageId, meta); } + +export async function onGenerationAfterCommandsController( + runtime, + type, + params = {}, + dryRun = false, +) { + if (dryRun) return; + + const context = runtime.getContext(); + const chat = context?.chat; + const recallOptions = runtime.buildGenerationAfterCommandsRecallInput( + type, + params, + chat, + ); + if (!recallOptions?.overrideUserMessage) return; + + const recallContext = runtime.createGenerationRecallContext({ + hookName: "GENERATION_AFTER_COMMANDS", + generationType: String(type || "normal").trim() || "normal", + recallOptions, + }); + if (!recallContext.shouldRun) { + return; + } + + runtime.markGenerationRecallTransactionHookState( + recallContext.transaction, + recallContext.hookName, + "running", + ); + const recallResult = await runtime.runRecall({ + ...recallOptions, + recallKey: recallContext.recallKey, + hookName: recallContext.hookName, + signal: params?.signal, + }); + + runtime.markGenerationRecallTransactionHookState( + recallContext.transaction, + recallContext.hookName, + runtime.getGenerationRecallHookStateFromResult(recallResult), + ); +} + +export async function onBeforeCombinePromptsController(runtime) { + const context = runtime.getContext(); + const chat = context?.chat; + const recallOptions = + runtime.buildNormalGenerationRecallInput(chat) || + runtime.buildHistoryGenerationRecallInput(chat) || + {}; + const recallContext = runtime.createGenerationRecallContext({ + hookName: "GENERATE_BEFORE_COMBINE_PROMPTS", + generationType: "normal", + recallOptions, + }); + if (!recallContext.shouldRun) { + return; + } + + runtime.markGenerationRecallTransactionHookState( + recallContext.transaction, + recallContext.hookName, + "running", + ); + const recallResult = await runtime.runRecall({ + ...recallOptions, + recallKey: recallContext.recallKey, + hookName: recallContext.hookName, + }); + runtime.markGenerationRecallTransactionHookState( + recallContext.transaction, + recallContext.hookName, + runtime.getGenerationRecallHookStateFromResult(recallResult), + ); +} diff --git a/index.js b/index.js index 308f187..0c3fa69 100644 --- a/index.js +++ b/index.js @@ -60,6 +60,8 @@ import { installSendIntentHooksController, onChatChangedController, onChatLoadedController, + onBeforeCombinePromptsController, + onGenerationAfterCommandsController, onMessageDeletedController, onMessageEditedController, onMessageSentController, @@ -4409,76 +4411,31 @@ function onMessageSwiped(messageId, meta = null) { } async function onGenerationAfterCommands(type, params = {}, dryRun = false) { - if (dryRun) return; - - const context = getContext(); - const chat = context?.chat; - const recallOptions = buildGenerationAfterCommandsRecallInput( + return await onGenerationAfterCommandsController( + { + buildGenerationAfterCommandsRecallInput, + createGenerationRecallContext, + getContext, + getGenerationRecallHookStateFromResult, + markGenerationRecallTransactionHookState, + runRecall, + }, type, params, - chat, - ); - if (!recallOptions?.overrideUserMessage) return; - - const recallContext = createGenerationRecallContext({ - hookName: "GENERATION_AFTER_COMMANDS", - generationType: String(type || "normal").trim() || "normal", - recallOptions, - }); - if (!recallContext.shouldRun) { - return; - } - - markGenerationRecallTransactionHookState( - recallContext.transaction, - recallContext.hookName, - "running", - ); - const recallResult = await runRecall({ - ...recallOptions, - recallKey: recallContext.recallKey, - hookName: recallContext.hookName, - signal: params?.signal, - }); - - markGenerationRecallTransactionHookState( - recallContext.transaction, - recallContext.hookName, - getGenerationRecallHookStateFromResult(recallResult), + dryRun, ); } async function onBeforeCombinePrompts() { - const context = getContext(); - const chat = context?.chat; - const recallOptions = - buildNormalGenerationRecallInput(chat) || - buildHistoryGenerationRecallInput(chat) || - {}; - const recallContext = createGenerationRecallContext({ - hookName: "GENERATE_BEFORE_COMBINE_PROMPTS", - generationType: "normal", - recallOptions, + return await onBeforeCombinePromptsController({ + buildHistoryGenerationRecallInput, + buildNormalGenerationRecallInput, + createGenerationRecallContext, + getContext, + getGenerationRecallHookStateFromResult, + markGenerationRecallTransactionHookState, + runRecall, }); - if (!recallContext.shouldRun) { - return; - } - - markGenerationRecallTransactionHookState( - recallContext.transaction, - recallContext.hookName, - "running", - ); - const recallResult = await runRecall({ - ...recallOptions, - recallKey: recallContext.recallKey, - hookName: recallContext.hookName, - }); - markGenerationRecallTransactionHookState( - recallContext.transaction, - recallContext.hookName, - getGenerationRecallHookStateFromResult(recallResult), - ); } function onMessageReceived() { diff --git a/tests/p0-regressions.mjs b/tests/p0-regressions.mjs index 5a810f6..174bf22 100644 --- a/tests/p0-regressions.mjs +++ b/tests/p0-regressions.mjs @@ -53,6 +53,10 @@ import { pruneProcessedMessageHashesFromFloor, rollbackAffectedJournals, } from "../chat-history.js"; +import { + onBeforeCombinePromptsController, + onGenerationAfterCommandsController, +} from "../event-binding.js"; const extensionsShimSource = [ "export const extension_settings = globalThis.__p0ExtensionSettings || {};", @@ -303,6 +307,8 @@ function createGenerationRecallHarness() { GRAPH_LOAD_STATES, GRAPH_METADATA_KEY, GRAPH_PERSISTENCE_META_KEY, + onBeforeCombinePromptsController, + onGenerationAfterCommandsController, }; vm.createContext(context); vm.runInContext(