Add debug log toggle and silence diagnostic logs

This commit is contained in:
Youzini-afk
2026-04-06 11:26:37 +08:00
parent b6187d46e0
commit 8320e3e7f6
23 changed files with 226 additions and 96 deletions

View File

@@ -1,3 +1,5 @@
import { debugWarn } from "./debug-logging.js";
function getTimerApi(runtime) {
const rawSetTimeout =
typeof runtime?.setTimeout === "function"
@@ -38,14 +40,14 @@ export function registerBeforeCombinePromptsController(runtime, listener) {
export function registerGenerationAfterCommandsController(runtime, listener) {
const makeFirst = runtime.getEventMakeFirst();
const eventName = runtime.eventTypes.GENERATION_AFTER_COMMANDS;
console.warn("[ST-BME:DIAG] Registering GENERATION_AFTER_COMMANDS:", {
debugWarn("[ST-BME:DIAG] Registering GENERATION_AFTER_COMMANDS:", {
eventName,
hasMakeFirst: typeof makeFirst === "function",
hasListener: typeof listener === "function",
});
if (typeof makeFirst === "function") {
const cleanup = makeFirst(eventName, listener);
console.warn("[ST-BME:DIAG] Registered via makeFirst, cleanup:", typeof cleanup);
debugWarn("[ST-BME:DIAG] Registered via makeFirst, cleanup:", typeof cleanup);
return cleanup;
}
@@ -406,9 +408,9 @@ export async function onGenerationAfterCommandsController(
params = {},
dryRun = false,
) {
console.warn("[ST-BME:DIAG] GENERATION_AFTER_COMMANDS fired", { type, dryRun, paramsKeys: Object.keys(params || {}) });
debugWarn("[ST-BME:DIAG] GENERATION_AFTER_COMMANDS fired", { type, dryRun, paramsKeys: Object.keys(params || {}) });
if (dryRun) {
console.warn("[ST-BME:DIAG] EXIT: dryRun=true");
debugWarn("[ST-BME:DIAG] EXIT: dryRun=true");
return;
}
@@ -418,11 +420,11 @@ export async function onGenerationAfterCommandsController(
? runtime.consumeHostGenerationInputSnapshot?.({ preserve: true }) ||
runtime.consumeHostGenerationInputSnapshot?.()
: null;
console.warn("[ST-BME:DIAG] frozenInputSnapshot:", frozenInputSnapshot?.text ? `"${frozenInputSnapshot.text.slice(0,50)}"` : "(empty)", "fresh:", !!frozenInputSnapshot?.at);
debugWarn("[ST-BME:DIAG] frozenInputSnapshot:", frozenInputSnapshot?.text ? `"${frozenInputSnapshot.text.slice(0,50)}"` : "(empty)", "fresh:", !!frozenInputSnapshot?.at);
const context = runtime.getContext();
const chat = context?.chat;
console.warn("[ST-BME:DIAG] chat length:", chat?.length, "last msg:", chat?.length ? { is_user: chat[chat.length-1]?.is_user, mes: (chat[chat.length-1]?.mes||"").slice(0,50) } : "(no chat)");
debugWarn("[ST-BME:DIAG] chat length:", chat?.length, "last msg:", chat?.length ? { is_user: chat[chat.length-1]?.is_user, mes: (chat[chat.length-1]?.mes||"").slice(0,50) } : "(no chat)");
const recallOptions = runtime.buildGenerationAfterCommandsRecallInput(
type,
@@ -433,14 +435,14 @@ export async function onGenerationAfterCommandsController(
chat,
);
if (!recallOptions) {
console.warn("[ST-BME:DIAG] EXIT: buildGenerationAfterCommandsRecallInput returned null");
debugWarn("[ST-BME:DIAG] EXIT: buildGenerationAfterCommandsRecallInput returned null");
return;
}
if (recallOptions?.__trivialSkip) {
console.warn("[ST-BME:DIAG] EXIT: trivial-input-skip");
debugWarn("[ST-BME:DIAG] EXIT: trivial-input-skip");
return;
}
console.warn("[ST-BME:DIAG] recallOptions:", { generationType: recallOptions.generationType, overrideUserMessage: recallOptions.overrideUserMessage?.slice(0,50), overrideSource: recallOptions.overrideSource, targetIdx: recallOptions.targetUserMessageIndex });
debugWarn("[ST-BME:DIAG] recallOptions:", { generationType: recallOptions.generationType, overrideUserMessage: recallOptions.overrideUserMessage?.slice(0,50), overrideSource: recallOptions.overrideSource, targetIdx: recallOptions.targetUserMessageIndex });
const recallContext = runtime.createGenerationRecallContext({
hookName: "GENERATION_AFTER_COMMANDS",
@@ -448,10 +450,10 @@ export async function onGenerationAfterCommandsController(
recallOptions,
});
if (!recallContext.shouldRun && !recallContext.transaction) {
console.warn("[ST-BME:DIAG] EXIT: shouldRun=false, no transaction. guardReason:", recallContext.guardReason);
debugWarn("[ST-BME:DIAG] EXIT: shouldRun=false, no transaction. guardReason:", recallContext.guardReason);
return;
}
console.warn("[ST-BME:DIAG] recallContext:", { shouldRun: recallContext.shouldRun, guardReason: recallContext.guardReason, transactionId: recallContext.transaction?.id });
debugWarn("[ST-BME:DIAG] recallContext:", { shouldRun: recallContext.shouldRun, guardReason: recallContext.guardReason, transactionId: recallContext.transaction?.id });
const runtimeRecallOptions =
recallContext.recallOptions || recallOptions || {};
@@ -464,7 +466,7 @@ export async function onGenerationAfterCommandsController(
let recallResult = runtime.getGenerationRecallTransactionResult?.(
recallContext.transaction,
);
console.warn("[ST-BME:DIAG] deliveryMode:", deliveryMode, "shouldRun:", recallContext.shouldRun);
debugWarn("[ST-BME:DIAG] deliveryMode:", deliveryMode, "shouldRun:", recallContext.shouldRun);
if (recallContext.shouldRun) {
runtime.markGenerationRecallTransactionHookState(
@@ -475,7 +477,7 @@ export async function onGenerationAfterCommandsController(
if (deliveryMode === "deferred") {
runtime.clearLiveRecallInjectionPromptForRewrite?.();
}
console.warn("[ST-BME:DIAG] >>> Starting runRecall...");
debugWarn("[ST-BME:DIAG] >>> Starting runRecall...");
recallResult = await runtime.runRecall({
...runtimeRecallOptions,
deliveryMode,
@@ -483,7 +485,7 @@ export async function onGenerationAfterCommandsController(
hookName: recallContext.hookName,
signal: params?.signal,
});
console.warn("[ST-BME:DIAG] <<< runRecall finished:", { status: recallResult?.status, ok: recallResult?.ok, reason: recallResult?.reason, injectionText: recallResult?.injectionText?.slice(0,80) });
debugWarn("[ST-BME:DIAG] <<< runRecall finished:", { status: recallResult?.status, ok: recallResult?.ok, reason: recallResult?.reason, injectionText: recallResult?.injectionText?.slice(0,80) });
runtime.storeGenerationRecallTransactionResult?.(
recallContext.transaction,
recallResult,
@@ -516,7 +518,7 @@ export async function onGenerationAfterCommandsController(
// 上面的兜底补写会把 fresh recall 绑定回最终 user 楼层。
// 这里再补一次 UI 刷新,避免需要等到消息编辑/历史恢复后才看到 Recall Card。
runtime.refreshPersistedRecallMessageUi?.();
console.warn("[ST-BME:DIAG] DONE: immediate mode, injection via setExtensionPrompt in runRecall");
debugWarn("[ST-BME:DIAG] DONE: immediate mode, injection via setExtensionPrompt in runRecall");
return recallResult;
}
@@ -550,7 +552,7 @@ export async function onBeforeCombinePromptsController(
frozenInputSnapshot,
});
if (normalInput?.__trivialSkip) {
console.warn("[ST-BME:DIAG] EXIT: trivial-input-skip");
debugWarn("[ST-BME:DIAG] EXIT: trivial-input-skip");
return {
skipped: true,
reason: `trivial:${normalInput.trivialReason || ""}`,