Guard against Tavern Helper prompt viewer fake generations

This commit is contained in:
Hao19911125
2026-04-11 22:09:28 +08:00
parent 0d4a692bbb
commit 943c398fb9
2 changed files with 98 additions and 1 deletions

View File

@@ -24,6 +24,15 @@ function toSafeFloor(value, fallback = null) {
return Number.isFinite(numeric) ? Math.floor(numeric) : fallback;
}
function isTavernHelperPromptViewerSyntheticGeneration(runtime) {
if (!runtime.isTavernHelperPromptViewerRefreshActive?.()) {
return false;
}
const pendingSendIntent = runtime.getPendingRecallSendIntent?.();
return !runtime.isFreshRecallInputRecord?.(pendingSendIntent);
}
export function registerBeforeCombinePromptsController(runtime, listener) {
const makeFirst = runtime.getEventMakeFirst();
if (typeof makeFirst === "function") {
@@ -333,6 +342,21 @@ export function onGenerationStartedController(
const generationType = String(type || "normal").trim() || "normal";
if (generationType !== "normal") return null;
if (isTavernHelperPromptViewerSyntheticGeneration(runtime)) {
const context = runtime.getContext?.() || {};
runtime.markCurrentGenerationTrivialSkip?.({
reason: "tavern-helper-prompt-viewer",
chatId: context?.chatId || "",
chatLength: Array.isArray(context?.chat) ? context.chat.length : 0,
});
runtime.clearPendingRecallSendIntent?.();
runtime.clearPendingHostGenerationInputSnapshot?.();
console.debug?.(
"[ST-BME] skip: tavern-helper-prompt-viewer hook=GENERATION_STARTED",
);
return null;
}
const pendingSendIntent = runtime.getPendingRecallSendIntent?.();
const pendingIntentText = runtime.isFreshRecallInputRecord?.(
pendingSendIntent,
@@ -445,6 +469,8 @@ export async function onGenerationAfterCommandsController(
return;
}
const generationType = String(type || "normal").trim() || "normal";
if (runtime.isMvuExtraAnalysisGuardActive?.()) {
console.debug?.(
"[ST-BME] skip: mvu-extra-analysis hook=GENERATION_AFTER_COMMANDS",
@@ -452,7 +478,24 @@ export async function onGenerationAfterCommandsController(
return;
}
const generationType = String(type || "normal").trim() || "normal";
if (
generationType === "normal" &&
isTavernHelperPromptViewerSyntheticGeneration(runtime)
) {
const context = runtime.getContext?.() || {};
runtime.markCurrentGenerationTrivialSkip?.({
reason: "tavern-helper-prompt-viewer",
chatId: runtime.getCurrentChatId?.() || context?.chatId || "",
chatLength: Array.isArray(context?.chat) ? context.chat.length : 0,
});
runtime.clearPendingRecallSendIntent?.();
runtime.clearPendingHostGenerationInputSnapshot?.();
console.debug?.(
"[ST-BME] skip: tavern-helper-prompt-viewer hook=GENERATION_AFTER_COMMANDS",
);
return;
}
const frozenInputSnapshot =
generationType === "normal"
? runtime.consumeHostGenerationInputSnapshot?.({ preserve: true }) ||
@@ -595,6 +638,24 @@ export async function onBeforeCombinePromptsController(
};
}
if (isTavernHelperPromptViewerSyntheticGeneration(runtime)) {
const context = runtime.getContext?.() || {};
runtime.markCurrentGenerationTrivialSkip?.({
reason: "tavern-helper-prompt-viewer",
chatId: runtime.getCurrentChatId?.() || context?.chatId || "",
chatLength: Array.isArray(context?.chat) ? context.chat.length : 0,
});
runtime.clearPendingRecallSendIntent?.();
runtime.clearPendingHostGenerationInputSnapshot?.();
console.debug?.(
"[ST-BME] skip: tavern-helper-prompt-viewer hook=GENERATE_BEFORE_COMBINE_PROMPTS",
);
return {
skipped: true,
reason: "tavern-helper-prompt-viewer",
};
}
const frozenInputSnapshot =
runtime.consumeHostGenerationInputSnapshot?.() ||
runtime.getPendingHostGenerationInputSnapshot?.() ||

View File

@@ -6738,6 +6738,27 @@ function isMvuExtraAnalysisGuardActive(now = Date.now()) {
return true;
}
function isTavernHelperPromptViewerRefreshActive() {
try {
const doc = globalThis?.document;
if (!doc?.querySelectorAll) return false;
const dialogs = Array.from(doc.querySelectorAll('[role="dialog"]'));
for (const dialog of dialogs) {
const dialogText = String(dialog?.textContent || "");
if (!/(提示词查看器|prompt\s*viewer)/i.test(dialogText)) {
continue;
}
if (dialog.querySelector(".fa-rotate-right.animate-spin")) {
return true;
}
}
} catch {}
return false;
}
function isGraphEffectivelyEmpty(graph) {
if (!graph || typeof graph !== "object") {
return true;
@@ -12748,6 +12769,7 @@ function onGenerationStarted(type, params = {}, dryRun = false) {
getPendingRecallSendIntent: () => pendingRecallSendIntent,
getSendTextareaValue,
isFreshRecallInputRecord,
isTavernHelperPromptViewerRefreshActive,
isTrivialUserInput,
markDryRunPromptPreview,
markCurrentGenerationTrivialSkip,
@@ -12787,6 +12809,8 @@ async function onGenerationAfterCommands(type, params = {}, dryRun = false) {
{
applyFinalRecallInjectionForGeneration,
buildGenerationAfterCommandsRecallInput,
clearPendingHostGenerationInputSnapshot,
clearPendingRecallSendIntent,
clearLiveRecallInjectionPromptForRewrite,
consumeHostGenerationInputSnapshot,
createGenerationRecallContext,
@@ -12794,7 +12818,12 @@ async function onGenerationAfterCommands(type, params = {}, dryRun = false) {
getContext,
getGenerationRecallHookStateFromResult,
getGenerationRecallTransactionResult,
getCurrentChatId,
getPendingRecallSendIntent: () => pendingRecallSendIntent,
isFreshRecallInputRecord,
isMvuExtraAnalysisGuardActive,
isTavernHelperPromptViewerRefreshActive,
markCurrentGenerationTrivialSkip,
markGenerationRecallTransactionHookState,
resolveGenerationRecallDeliveryMode,
runRecall,
@@ -12812,6 +12841,8 @@ async function onBeforeCombinePrompts(promptData = null) {
applyFinalRecallInjectionForGeneration,
buildHistoryGenerationRecallInput,
buildNormalGenerationRecallInput,
clearPendingHostGenerationInputSnapshot,
clearPendingRecallSendIntent,
clearLiveRecallInjectionPromptForRewrite,
consumeDryRunPromptPreview,
consumeHostGenerationInputSnapshot,
@@ -12819,7 +12850,12 @@ async function onBeforeCombinePrompts(promptData = null) {
getContext,
getGenerationRecallHookStateFromResult,
getGenerationRecallTransactionResult,
getCurrentChatId,
getPendingRecallSendIntent: () => pendingRecallSendIntent,
isFreshRecallInputRecord,
isMvuExtraAnalysisGuardActive,
isTavernHelperPromptViewerRefreshActive,
markCurrentGenerationTrivialSkip,
markGenerationRecallTransactionHookState,
resolveGenerationRecallDeliveryMode,
runRecall,