refactor: controllerize generation recall event hooks

This commit is contained in:
Youzini-afk
2026-03-29 17:09:09 +08:00
parent e29e413516
commit d3479bc672
3 changed files with 104 additions and 63 deletions

View File

@@ -167,3 +167,81 @@ export function onMessageSwipedController(runtime, messageId, meta = null) {
runtime.invalidateRecallAfterHistoryMutation("已切换楼层 swipe"); runtime.invalidateRecallAfterHistoryMutation("已切换楼层 swipe");
runtime.scheduleHistoryMutationRecheck("message-swiped", messageId, meta); 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),
);
}

View File

@@ -60,6 +60,8 @@ import {
installSendIntentHooksController, installSendIntentHooksController,
onChatChangedController, onChatChangedController,
onChatLoadedController, onChatLoadedController,
onBeforeCombinePromptsController,
onGenerationAfterCommandsController,
onMessageDeletedController, onMessageDeletedController,
onMessageEditedController, onMessageEditedController,
onMessageSentController, onMessageSentController,
@@ -4409,76 +4411,31 @@ function onMessageSwiped(messageId, meta = null) {
} }
async function onGenerationAfterCommands(type, params = {}, dryRun = false) { async function onGenerationAfterCommands(type, params = {}, dryRun = false) {
if (dryRun) return; return await onGenerationAfterCommandsController(
{
const context = getContext(); buildGenerationAfterCommandsRecallInput,
const chat = context?.chat; createGenerationRecallContext,
const recallOptions = buildGenerationAfterCommandsRecallInput( getContext,
getGenerationRecallHookStateFromResult,
markGenerationRecallTransactionHookState,
runRecall,
},
type, type,
params, params,
chat, dryRun,
);
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),
); );
} }
async function onBeforeCombinePrompts() { async function onBeforeCombinePrompts() {
const context = getContext(); return await onBeforeCombinePromptsController({
const chat = context?.chat; buildHistoryGenerationRecallInput,
const recallOptions = buildNormalGenerationRecallInput,
buildNormalGenerationRecallInput(chat) || createGenerationRecallContext,
buildHistoryGenerationRecallInput(chat) || getContext,
{}; getGenerationRecallHookStateFromResult,
const recallContext = createGenerationRecallContext({ markGenerationRecallTransactionHookState,
hookName: "GENERATE_BEFORE_COMBINE_PROMPTS", runRecall,
generationType: "normal",
recallOptions,
}); });
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() { function onMessageReceived() {

View File

@@ -53,6 +53,10 @@ import {
pruneProcessedMessageHashesFromFloor, pruneProcessedMessageHashesFromFloor,
rollbackAffectedJournals, rollbackAffectedJournals,
} from "../chat-history.js"; } from "../chat-history.js";
import {
onBeforeCombinePromptsController,
onGenerationAfterCommandsController,
} from "../event-binding.js";
const extensionsShimSource = [ const extensionsShimSource = [
"export const extension_settings = globalThis.__p0ExtensionSettings || {};", "export const extension_settings = globalThis.__p0ExtensionSettings || {};",
@@ -303,6 +307,8 @@ function createGenerationRecallHarness() {
GRAPH_LOAD_STATES, GRAPH_LOAD_STATES,
GRAPH_METADATA_KEY, GRAPH_METADATA_KEY,
GRAPH_PERSISTENCE_META_KEY, GRAPH_PERSISTENCE_META_KEY,
onBeforeCombinePromptsController,
onGenerationAfterCommandsController,
}; };
vm.createContext(context); vm.createContext(context);
vm.runInContext( vm.runInContext(