Files
LittleWhiteBox/modules/story-summary/generate/prompt.js

853 lines
40 KiB
JavaScript
Raw Normal View History

2026-01-27 16:04:57 +08:00
// ═══════════════════════════════════════════════════════════════════════════
// Story Summary - Prompt Injection (Final Clean Version)
2026-01-29 01:17:37 +08:00
// - 仅负责“构建注入文本”,不负责写入 extension_prompts
// - 注入发生在 story-summary.jsGENERATION_STARTED 时写入 extension_promptsIN_CHAT + depth
2026-01-27 16:04:57 +08:00
// ═══════════════════════════════════════════════════════════════════════════
2026-01-26 01:16:35 +08:00
import { getContext } from "../../../../../../extensions.js";
import { xbLog } from "../../../core/debug-core.js";
import { getSummaryStore } from "../data/store.js";
import { getVectorConfig, getSummaryPanelConfig, getSettings } from "../data/config.js";
import { recallMemory, buildQueryText } from "../vector/recall.js";
2026-01-27 22:51:44 +08:00
import { getChunksByFloors, getAllChunkVectors, getAllEventVectors, getMeta } from "../vector/chunk-store.js";
2026-01-26 01:16:35 +08:00
const MODULE_ID = "summaryPrompt";
// ─────────────────────────────────────────────────────────────────────────────
2026-01-27 16:04:57 +08:00
// 召回失败提示节流(避免连续生成刷屏)
// ─────────────────────────────────────────────────────────────────────────────
2026-01-27 16:04:57 +08:00
let lastRecallFailAt = 0;
const RECALL_FAIL_COOLDOWN_MS = 10_000;
2026-01-27 16:04:57 +08:00
function canNotifyRecallFail() {
const now = Date.now();
if (now - lastRecallFailAt < RECALL_FAIL_COOLDOWN_MS) return false;
lastRecallFailAt = now;
return true;
}
// ─────────────────────────────────────────────────────────────────────────────
2026-01-27 16:04:57 +08:00
// 预算常量(向量模式使用)
// ─────────────────────────────────────────────────────────────────────────────
2026-01-27 22:51:44 +08:00
const MAIN_BUDGET_MAX = 10000; // 主装配预算(世界/事件/远期/弧光)
const RECENT_ORPHAN_MAX = 5000; // [待整理] 独立预算
const TOTAL_BUDGET_MAX = 15000; // 总预算(用于日志显示)
const L3_MAX = 2000;
const ARCS_MAX = 1500;
// ─────────────────────────────────────────────────────────────────────────────
2026-01-26 01:16:35 +08:00
// 工具函数
// ─────────────────────────────────────────────────────────────────────────────
2026-01-26 01:16:35 +08:00
function estimateTokens(text) {
if (!text) return 0;
const s = String(text);
const zh = (s.match(/[\u4e00-\u9fff]/g) || []).length;
return Math.ceil(zh + (s.length - zh) / 4);
}
function pushWithBudget(lines, text, state) {
const t = estimateTokens(text);
if (state.used + t > state.max) return false;
lines.push(text);
state.used += t;
return true;
}
2026-01-27 16:04:57 +08:00
function cosineSimilarity(a, b) {
if (!a?.length || !b?.length || a.length !== b.length) return 0;
let dot = 0, nA = 0, nB = 0;
for (let i = 0; i < a.length; i++) {
dot += a[i] * b[i];
nA += a[i] * a[i];
nB += b[i] * b[i];
}
return nA && nB ? dot / (Math.sqrt(nA) * Math.sqrt(nB)) : 0;
}
2026-01-26 01:16:35 +08:00
// 从 summary 解析楼层范围:(#321-322) 或 (#321)
function parseFloorRange(summary) {
if (!summary) return null;
const match = String(summary).match(/\(#(\d+)(?:-(\d+))?\)/);
if (!match) return null;
const start = Math.max(0, parseInt(match[1], 10) - 1);
2026-01-27 16:04:57 +08:00
const end = Math.max(0, (match[2] ? parseInt(match[2], 10) : parseInt(match[1], 10)) - 1);
2026-01-26 01:16:35 +08:00
return { start, end };
}
2026-01-27 16:04:57 +08:00
// 去掉 summary 末尾楼层标记(按你要求:事件本体不显示楼层范围)
2026-01-26 01:16:35 +08:00
function cleanSummary(summary) {
return String(summary || "")
.replace(/\s*\(#\d+(?:-\d+)?\)\s*$/, "")
.trim();
}
// ─────────────────────────────────────────────────────────────────────────────
2026-01-27 16:04:57 +08:00
// 系统前导与后缀
// ─────────────────────────────────────────────────────────────────────────────
2026-01-26 01:16:35 +08:00
2026-01-27 16:04:57 +08:00
function buildSystemPreamble() {
return [
"以上内容为因上下文窗口限制保留的可见历史",
"以下【剧情记忆】是对可见与不可见历史的总结:",
"• 【世界约束】记录着已确立的事实",
"• 其余部分是过往经历的回忆碎片",
2026-01-27 16:04:57 +08:00
"",
"请内化这些记忆:",
2026-01-27 16:04:57 +08:00
].join("\n");
2026-01-26 01:16:35 +08:00
}
2026-01-27 16:04:57 +08:00
function buildPostscript() {
return [
"",
"——",
].join("\n");
}
// ─────────────────────────────────────────────────────────────────────────────
2026-01-26 01:16:35 +08:00
// 格式化函数
// ─────────────────────────────────────────────────────────────────────────────
2026-01-26 01:16:35 +08:00
function formatWorldLines(world) {
return [...(world || [])]
.sort((a, b) => (b.floor || 0) - (a.floor || 0))
.map(w => `- ${w.topic}${w.content}`);
}
2026-01-27 16:04:57 +08:00
function formatArcLine(a) {
const moments = (a.moments || [])
.map(m => (typeof m === "string" ? m : m.text))
.filter(Boolean);
2026-01-27 16:04:57 +08:00
if (moments.length) {
return `- ${a.name}${moments.join(" → ")}(当前:${a.trajectory}`;
2026-01-26 01:16:35 +08:00
}
2026-01-27 16:04:57 +08:00
return `- ${a.name}${a.trajectory}`;
}
2026-01-27 16:04:57 +08:00
// 完整 chunk 输出(不截断)
function formatChunkFullLine(c) {
2026-01-29 01:17:37 +08:00
const { name1, name2 } = getContext();
const speaker = c.isUser ? (name1 || "用户") : (name2 || "角色");
2026-01-27 16:04:57 +08:00
return ` #${c.floor + 1} [${speaker}] ${String(c.text || "").trim()}`;
}
2026-01-27 16:04:57 +08:00
// 因果事件格式(仅作为“前因线索”展示,仍保留楼层提示)
function formatCausalEventLine(causalItem, causalById) {
const ev = causalItem?.event || {};
const depth = Math.max(1, Math.min(9, causalItem?._causalDepth || 1));
const indent = " │" + " ".repeat(depth - 1);
const prefix = `${indent}├─ 前因`;
const time = ev.timeLabel ? `${ev.timeLabel}` : "";
const people = (ev.participants || []).join(" / ");
const summary = cleanSummary(ev.summary);
const r = parseFloorRange(ev.summary);
const floorHint = r ? `(#${r.start + 1}${r.end !== r.start ? `-${r.end + 1}` : ""})` : "";
const lines = [];
lines.push(`${prefix}${time}${people ? ` ${people}` : ""}`);
const body = `${summary}${floorHint ? ` ${floorHint}` : ""}`.trim();
lines.push(`${indent} ${body}`);
const evidence = causalItem._evidenceChunk;
if (evidence) {
const speaker = evidence.speaker || "角色";
2026-01-27 16:04:57 +08:00
const preview = String(evidence.text || "");
const clip = preview.length > 60 ? preview.slice(0, 60) + "..." : preview;
lines.push(`${indent} #${evidence.floor + 1} [${speaker}] ${clip}`);
}
return lines.join("\n");
2026-01-26 01:16:35 +08:00
}
2026-01-27 16:04:57 +08:00
// ─────────────────────────────────────────────────────────────────────────────
// 装配日志(开发调试用)
// ─────────────────────────────────────────────────────────────────────────────
2026-01-27 22:51:44 +08:00
function formatInjectionLog(stats, details, recentOrphanStats = null) {
2026-01-27 16:04:57 +08:00
const pct = (n, d) => (d > 0 ? Math.round((n / d) * 100) : 0);
const lines = [
"",
"╔══════════════════════════════════════════════════════════════╗",
"║ Prompt 装配报告 ║",
"╠══════════════════════════════════════════════════════════════╣",
`║ 总预算: ${stats.budget.max} tokens`,
`║ 已使用: ${stats.budget.used} tokens (${pct(stats.budget.used, stats.budget.max)}%)`,
`║ 剩余: ${stats.budget.max - stats.budget.used} tokens`,
"╚══════════════════════════════════════════════════════════════╝",
"",
];
// 世界状态
lines.push("┌─────────────────────────────────────────────────────────────┐");
2026-01-27 22:51:44 +08:00
lines.push("│ [1] 世界约束 (上限 2000) │");
2026-01-27 16:04:57 +08:00
lines.push("└─────────────────────────────────────────────────────────────┘");
lines.push(` 注入: ${stats.world.count} 条 | ${stats.world.tokens} tokens`);
lines.push("");
2026-01-27 22:51:44 +08:00
// 核心经历 + 过往背景
2026-01-27 16:04:57 +08:00
lines.push("┌─────────────────────────────────────────────────────────────┐");
2026-01-27 22:51:44 +08:00
lines.push("│ [2] 核心经历 + 过往背景(含证据) │");
2026-01-27 16:04:57 +08:00
lines.push("└─────────────────────────────────────────────────────────────┘");
lines.push(` 选入: ${stats.events.selected} 条 | 事件本体: ${stats.events.tokens} tokens`);
lines.push(` 挂载证据: ${stats.evidence.attached} 条 | 证据: ${stats.evidence.tokens} tokens`);
2026-01-27 22:51:44 +08:00
lines.push(` 核心: ${details.directCount || 0} | 过往: ${details.similarCount || 0}`);
2026-01-27 16:04:57 +08:00
if (details.eventList?.length) {
lines.push(" ────────────────────────────────────────");
details.eventList.slice(0, 20).forEach((ev, i) => {
2026-01-27 22:51:44 +08:00
const type = ev.isDirect ? "核心" : "过往";
2026-01-27 16:04:57 +08:00
const hasE = ev.hasEvidence ? " +E" : "";
const title = (ev.title || "(无标题)").slice(0, 32);
2026-01-27 22:51:44 +08:00
lines.push(` ${String(i + 1).padStart(2)}. [${type}${hasE}] ${title} (${ev.tokens}tok)`);
2026-01-27 16:04:57 +08:00
});
if (details.eventList.length > 20) lines.push(` ... 还有 ${details.eventList.length - 20}`);
2026-01-26 01:16:35 +08:00
}
2026-01-27 16:04:57 +08:00
lines.push("");
2026-01-26 01:16:35 +08:00
2026-01-27 22:51:44 +08:00
// 远期片段
2026-01-27 16:04:57 +08:00
lines.push("┌─────────────────────────────────────────────────────────────┐");
2026-01-27 22:51:44 +08:00
lines.push("│ [3] 远期片段(已总结范围) │");
2026-01-27 16:04:57 +08:00
lines.push("└─────────────────────────────────────────────────────────────┘");
lines.push(` 注入: ${stats.orphans.injected} 条 | ${stats.orphans.tokens} tokens`);
lines.push("");
2026-01-27 22:51:44 +08:00
// 待整理
2026-01-27 16:04:57 +08:00
lines.push("┌─────────────────────────────────────────────────────────────┐");
2026-01-27 22:51:44 +08:00
lines.push("│ [4] 待整理(未总结范围,独立预算 5000 │");
lines.push("└─────────────────────────────────────────────────────────────┘");
lines.push(` 注入: ${recentOrphanStats?.injected || 0} 条 | ${recentOrphanStats?.tokens || 0} tokens`);
lines.push(` 楼层范围: ${recentOrphanStats?.floorRange || "N/A"}`);
lines.push("");
lines.push("┌─────────────────────────────────────────────────────────────┐");
lines.push("│ [5] 人物弧光(上限 1500 │");
2026-01-27 16:04:57 +08:00
lines.push("└─────────────────────────────────────────────────────────────┘");
lines.push(` 注入: ${stats.arcs.count} 条 | ${stats.arcs.tokens} tokens`);
lines.push("");
2026-01-27 22:51:44 +08:00
// 预算条形图
2026-01-27 16:04:57 +08:00
lines.push("┌─────────────────────────────────────────────────────────────┐");
lines.push("│ 【预算分布】 │");
lines.push("└─────────────────────────────────────────────────────────────┘");
const total = stats.budget.max;
const bar = (tokens, label) => {
const width = Math.round((tokens / total) * 40);
const pctStr = pct(tokens, total) + "%";
return ` ${label.padEnd(6)} ${"█".repeat(width).padEnd(40)} ${String(tokens).padStart(5)} (${pctStr})`;
};
2026-01-27 22:51:44 +08:00
lines.push(bar(stats.world.tokens, "约束"));
lines.push(bar(stats.events.tokens, "经历"));
2026-01-27 16:04:57 +08:00
lines.push(bar(stats.evidence.tokens, "证据"));
2026-01-27 22:51:44 +08:00
lines.push(bar(stats.orphans.tokens, "远期"));
lines.push(bar(recentOrphanStats?.tokens || 0, "待整理"));
2026-01-27 16:04:57 +08:00
lines.push(bar(stats.arcs.tokens, "弧光"));
lines.push(bar(stats.budget.max - stats.budget.used, "剩余"));
lines.push("");
return lines.join("\n");
}
2026-01-29 17:02:51 +08:00
// 重写事件文本里的序号前缀:把 “{idx}. ” 或 “{idx}.【...】” 的 idx 替换
function renumberEventText(text, newIndex) {
const s = String(text || "");
// 匹配行首: "12." 或 "12.【"
return s.replace(/^(\s*)\d+(\.\s*(?:【)?)/, `$1${newIndex}$2`);
}
function getEventSortKey(ev) {
const r = parseFloorRange(ev?.summary);
if (r) return r.start; // 按事件出现楼层排序(最靠谱)
const m = String(ev?.id || "").match(/evt-(\d+)/);
return m ? parseInt(m[1], 10) : Number.MAX_SAFE_INTEGER;
}
2026-01-26 01:16:35 +08:00
// ─────────────────────────────────────────────────────────────────────────────
2026-01-27 16:04:57 +08:00
// 非向量模式:全量总结注入(世界 + 事件 + 弧光)
// 仅在 GENERATION_STARTED 调用
// ─────────────────────────────────────────────────────────────────────────────
2026-01-27 16:04:57 +08:00
function buildNonVectorPrompt(store) {
const data = store.json || {};
const sections = [];
if (data.world?.length) {
const lines = formatWorldLines(data.world);
sections.push(`[世界约束] 已确立的事实\n${lines.join("\n")}`);
}
if (data.events?.length) {
const lines = data.events.map((ev, i) => {
const time = ev.timeLabel || "";
const title = ev.title || "";
const people = (ev.participants || []).join(" / ");
const summary = cleanSummary(ev.summary);
const header = time ? `${i + 1}.【${time}${title || people}` : `${i + 1}. ${title || people}`;
return `${header}\n ${summary}`;
});
sections.push(`[剧情记忆]\n\n${lines.join("\n\n")}`);
}
if (data.arcs?.length) {
const lines = data.arcs.map(formatArcLine);
sections.push(`[人物弧光]\n${lines.join("\n")}`);
}
2026-01-27 16:04:57 +08:00
if (!sections.length) return "";
return (
`${buildSystemPreamble()}\n` +
`<剧情记忆>\n\n${sections.join("\n\n")}\n\n</剧情记忆>\n` +
`${buildPostscript()}`
);
}
2026-01-29 01:17:37 +08:00
export function buildNonVectorPromptText() {
2026-01-27 16:04:57 +08:00
if (!getSettings().storySummary?.enabled) {
2026-01-29 01:17:37 +08:00
return "";
2026-01-27 16:04:57 +08:00
}
const store = getSummaryStore();
if (!store?.json) {
2026-01-29 01:17:37 +08:00
return "";
2026-01-27 16:04:57 +08:00
}
let text = buildNonVectorPrompt(store);
if (!text.trim()) {
2026-01-29 01:17:37 +08:00
return "";
2026-01-27 16:04:57 +08:00
}
// wrapper沿用面板设置
const cfg = getSummaryPanelConfig();
if (cfg.trigger?.wrapperHead) text = cfg.trigger.wrapperHead + "\n" + text;
if (cfg.trigger?.wrapperTail) text = text + "\n" + cfg.trigger.wrapperTail;
2026-01-29 01:17:37 +08:00
return text;
}
2026-01-27 16:04:57 +08:00
// ─────────────────────────────────────────────────────────────
// 向量模式:预算装配(世界 → 事件(带证据) → 碎片 → 弧光)
// ─────────────────────────────────────────────────────────────
2026-01-27 22:51:44 +08:00
async function buildVectorPrompt(store, recallResult, causalById, queryEntities = [], meta = null) {
2026-01-26 01:16:35 +08:00
const data = store.json || {};
2026-01-27 22:51:44 +08:00
const total = { used: 0, max: MAIN_BUDGET_MAX };
// ═══════════════════════════════════════════════════════════════════
// 预装配各层内容(先计算预算,后按顺序拼接)
// ═══════════════════════════════════════════════════════════════════
const assembled = {
world: { lines: [], tokens: 0 },
arcs: { lines: [], tokens: 0 },
events: { direct: [], similar: [] },
orphans: { lines: [], tokens: 0 },
recentOrphans: { lines: [], tokens: 0 },
};
const injectionStats = {
2026-01-27 22:51:44 +08:00
budget: { max: TOTAL_BUDGET_MAX, used: 0 },
world: { count: 0, tokens: 0 },
arcs: { count: 0, tokens: 0 },
2026-01-27 16:04:57 +08:00
events: { selected: 0, tokens: 0 },
evidence: { attached: 0, tokens: 0 },
orphans: { injected: 0, tokens: 0 },
};
2026-01-27 22:51:44 +08:00
const recentOrphanStats = {
injected: 0,
tokens: 0,
floorRange: "N/A",
};
2026-01-27 16:04:57 +08:00
const details = {
eventList: [],
directCount: 0,
similarCount: 0,
};
2026-01-27 22:51:44 +08:00
// ═══════════════════════════════════════════════════════════════════
// [优先级 1] 世界约束 - 最高优先级
// ═══════════════════════════════════════════════════════════════════
2026-01-26 01:16:35 +08:00
const worldLines = formatWorldLines(data.world);
if (worldLines.length) {
2026-01-27 22:51:44 +08:00
const l3Budget = { used: 0, max: Math.min(L3_MAX, total.max - total.used) };
2026-01-26 01:16:35 +08:00
for (const line of worldLines) {
2026-01-27 22:51:44 +08:00
if (!pushWithBudget(assembled.world.lines, line, l3Budget)) break;
2026-01-26 01:16:35 +08:00
}
2026-01-27 22:51:44 +08:00
assembled.world.tokens = l3Budget.used;
total.used += l3Budget.used;
injectionStats.world.count = assembled.world.lines.length;
injectionStats.world.tokens = l3Budget.used;
}
// ═══════════════════════════════════════════════════════════════════
// [优先级 2] 人物弧光 - 预留预算(稍后再拼接到末尾)
// ═══════════════════════════════════════════════════════════════════
if (data.arcs?.length && total.used < total.max) {
const { name1 } = getContext();
const userName = String(name1 || "").trim();
const relevant = new Set(
[userName, ...(queryEntities || [])]
.map(s => String(s || "").trim())
.filter(Boolean)
);
const filtered = (data.arcs || []).filter(a => {
const n = String(a?.name || "").trim();
return n && relevant.has(n);
});
if (filtered.length) {
const arcBudget = { used: 0, max: Math.min(ARCS_MAX, total.max - total.used) };
for (const a of filtered) {
const line = formatArcLine(a);
if (!pushWithBudget(assembled.arcs.lines, line, arcBudget)) break;
}
assembled.arcs.tokens = arcBudget.used;
total.used += arcBudget.used;
injectionStats.arcs.count = assembled.arcs.lines.length;
injectionStats.arcs.tokens = arcBudget.used;
2026-01-26 01:16:35 +08:00
}
}
2026-01-27 22:51:44 +08:00
// ═══════════════════════════════════════════════════════════════════
// [优先级 3] 事件 + 证据
// ═══════════════════════════════════════════════════════════════════
2026-01-27 16:04:57 +08:00
const recalledEvents = (recallResult?.events || []).filter(e => e?.event?.summary);
2026-01-26 01:16:35 +08:00
const chunks = recallResult?.chunks || [];
2026-01-27 16:04:57 +08:00
const usedChunkIds = new Set();
2026-01-27 16:04:57 +08:00
function pickBestChunkForEvent(eventObj) {
const range = parseFloorRange(eventObj?.summary);
if (!range) return null;
2026-01-27 16:04:57 +08:00
let best = null;
for (const c of chunks) {
if (usedChunkIds.has(c.chunkId)) continue;
if (c.floor < range.start || c.floor > range.end) continue;
if (!best || (c.similarity || 0) > (best.similarity || 0)) best = c;
}
2026-01-27 16:04:57 +08:00
return best;
}
2026-01-27 16:04:57 +08:00
function formatEventWithEvidence(e, idx, chunk) {
const ev = e.event || {};
const time = ev.timeLabel || "";
const title = String(ev.title || "").trim();
const people = (ev.participants || []).join(" / ").trim();
const summary = cleanSummary(ev.summary);
2026-01-27 16:04:57 +08:00
const displayTitle = title || people || ev.id || "事件";
const header = time ? `${idx}.【${time}${displayTitle}` : `${idx}. ${displayTitle}`;
2026-01-27 16:04:57 +08:00
const lines = [header];
if (people && displayTitle !== people) lines.push(` ${people}`);
lines.push(` ${summary}`);
2026-01-27 16:04:57 +08:00
for (const cid of ev.causedBy || []) {
const c = causalById?.get(cid);
if (c) lines.push(formatCausalEventLine(c, causalById));
}
2026-01-27 16:04:57 +08:00
if (chunk) {
lines.push(` ${formatChunkFullLine(chunk)}`);
}
2026-01-27 16:04:57 +08:00
return lines.join("\n");
}
2026-01-27 16:04:57 +08:00
// 候选按相似度从高到低(保证高分优先拥有证据)
const candidates = [...recalledEvents].sort((a, b) => (b.similarity || 0) - (a.similarity || 0));
2026-01-29 17:02:51 +08:00
const selectedDirect = []; // { event, text, tokens, chunk, hasEvidence }
const selectedSimilar = []; // { event, text, tokens, chunk, hasEvidence }
2026-01-27 16:04:57 +08:00
for (const e of candidates) {
if (total.used >= total.max) break;
2026-01-27 16:04:57 +08:00
const isDirect = e._recallType === "DIRECT";
2026-01-27 16:04:57 +08:00
const bestChunk = pickBestChunkForEvent(e.event);
2026-01-27 16:04:57 +08:00
// 先尝试“带证据”
2026-01-29 17:02:51 +08:00
// idx 先占位写 0后面统一按时间线重排后再改号
let text = formatEventWithEvidence(e, 0, bestChunk);
2026-01-27 16:04:57 +08:00
let cost = estimateTokens(text);
let hasEvidence = !!bestChunk;
2026-01-29 17:02:51 +08:00
let chosenChunk = bestChunk || null;
2026-01-27 16:04:57 +08:00
// 塞不下就退化成“不带证据”
if (total.used + cost > total.max) {
2026-01-29 17:02:51 +08:00
text = formatEventWithEvidence(e, 0, null);
2026-01-27 16:04:57 +08:00
cost = estimateTokens(text);
hasEvidence = false;
2026-01-29 17:02:51 +08:00
chosenChunk = null;
2026-01-27 16:04:57 +08:00
if (total.used + cost > total.max) {
continue;
}
2026-01-26 01:16:35 +08:00
}
2026-01-27 16:04:57 +08:00
// 写入
if (isDirect) {
2026-01-29 17:02:51 +08:00
selectedDirect.push({ event: e.event, text, tokens: cost, chunk: chosenChunk, hasEvidence });
2026-01-27 16:04:57 +08:00
} else {
2026-01-29 17:02:51 +08:00
selectedSimilar.push({ event: e.event, text, tokens: cost, chunk: chosenChunk, hasEvidence });
2026-01-26 01:16:35 +08:00
}
2026-01-27 16:04:57 +08:00
injectionStats.events.selected++;
total.used += cost;
// tokens 拆分记账(事件本体 vs 证据)
if (hasEvidence && bestChunk) {
const chunkLine = formatChunkFullLine(bestChunk);
const ct = estimateTokens(chunkLine);
injectionStats.evidence.attached++;
injectionStats.evidence.tokens += ct;
usedChunkIds.add(bestChunk.chunkId);
// 事件本体 tokens = cost - ct粗略但够调试
injectionStats.events.tokens += Math.max(0, cost - ct);
} else {
injectionStats.events.tokens += cost;
2026-01-26 01:16:35 +08:00
}
2026-01-27 16:04:57 +08:00
details.eventList.push({
title: e.event?.title || e.event?.id,
isDirect,
hasEvidence,
tokens: cost,
similarity: e.similarity || 0,
});
2026-01-26 01:16:35 +08:00
}
2026-01-29 17:02:51 +08:00
// ═══════════════════════════════════════════════════════════════════
// 重排:恢复时间线顺序(按楼层/evt 序号升序)
// 并统一重编号(不重新 pick chunk不重新格式化结构
// ═══════════════════════════════════════════════════════════════════
selectedDirect.sort((a, b) => getEventSortKey(a.event) - getEventSortKey(b.event));
selectedSimilar.sort((a, b) => getEventSortKey(a.event) - getEventSortKey(b.event));
const selectedDirectTexts = selectedDirect.map((it, i) => renumberEventText(it.text, i + 1));
const selectedSimilarTexts = selectedSimilar.map((it, i) => renumberEventText(it.text, i + 1));
details.directCount = selectedDirect.length;
details.similarCount = selectedSimilar.length;
2026-01-27 22:51:44 +08:00
assembled.events.direct = selectedDirectTexts;
assembled.events.similar = selectedSimilarTexts;
2026-01-27 16:04:57 +08:00
2026-01-27 22:51:44 +08:00
// ═══════════════════════════════════════════════════════════════════
// [优先级 4] 远期片段(已总结范围的 orphan chunks
// ═══════════════════════════════════════════════════════════════════
const lastSummarized = store.lastSummarizedMesId ?? -1;
const lastChunkFloor = meta?.lastChunkFloor ?? -1;
const keepVisible = store.keepVisibleCount ?? 3;
2026-01-27 16:04:57 +08:00
if (chunks.length && total.used < total.max) {
const orphans = chunks
.filter(c => !usedChunkIds.has(c.chunkId))
2026-01-27 22:51:44 +08:00
.filter(c => c.floor <= lastSummarized)
2026-01-27 16:04:57 +08:00
.sort((a, b) => (a.floor - b.floor) || ((a.chunkIdx ?? 0) - (b.chunkIdx ?? 0)));
2026-01-27 22:51:44 +08:00
const l1Budget = { used: 0, max: total.max - total.used };
2026-01-26 01:16:35 +08:00
for (const c of orphans) {
2026-01-27 16:04:57 +08:00
const line = formatChunkFullLine(c);
2026-01-27 22:51:44 +08:00
if (!pushWithBudget(assembled.orphans.lines, line, l1Budget)) break;
injectionStats.orphans.injected++;
2026-01-26 01:16:35 +08:00
}
2026-01-27 22:51:44 +08:00
assembled.orphans.tokens = l1Budget.used;
total.used += l1Budget.used;
injectionStats.orphans.tokens = l1Budget.used;
2026-01-26 01:16:35 +08:00
}
2026-01-27 22:51:44 +08:00
// ═══════════════════════════════════════════════════════════════════
// [独立预算] 待整理(未总结范围,独立 5000
// ═══════════════════════════════════════════════════════════════════
2026-01-27 22:51:44 +08:00
// 近期范围:(lastSummarized, lastChunkFloor - keepVisible]
const recentStart = lastSummarized + 1;
const recentEnd = lastChunkFloor - keepVisible;
2026-01-27 22:51:44 +08:00
if (chunks.length && recentEnd >= recentStart) {
const recentOrphans = chunks
.filter(c => !usedChunkIds.has(c.chunkId))
.filter(c => c.floor >= recentStart && c.floor <= recentEnd)
.sort((a, b) => (a.floor - b.floor) || ((a.chunkIdx ?? 0) - (b.chunkIdx ?? 0)));
2026-01-27 22:51:44 +08:00
const recentBudget = { used: 0, max: RECENT_ORPHAN_MAX };
2026-01-27 22:51:44 +08:00
for (const c of recentOrphans) {
const line = formatChunkFullLine(c);
if (!pushWithBudget(assembled.recentOrphans.lines, line, recentBudget)) break;
recentOrphanStats.injected++;
2026-01-26 01:16:35 +08:00
}
2026-01-27 22:51:44 +08:00
assembled.recentOrphans.tokens = recentBudget.used;
recentOrphanStats.tokens = recentBudget.used;
recentOrphanStats.floorRange = `${recentStart + 1}~${recentEnd + 1}`;
}
// ═══════════════════════════════════════════════════════════════════
// 按注入顺序拼接 sections
// ═══════════════════════════════════════════════════════════════════
const sections = [];
// 1. 世界约束
if (assembled.world.lines.length) {
sections.push(`[世界约束] 已确立的事实\n${assembled.world.lines.join("\n")}`);
2026-01-27 22:51:44 +08:00
}
// 2. 核心经历
if (assembled.events.direct.length) {
sections.push(`[核心经历] 深刻的记忆\n\n${assembled.events.direct.join("\n\n")}`);
}
// 3. 过往背景
if (assembled.events.similar.length) {
sections.push(`[过往背景] 听别人说起或比较模糊的往事\n\n${assembled.events.similar.join("\n\n")}`);
2026-01-26 01:16:35 +08:00
}
2026-01-27 22:51:44 +08:00
// 4. 远期片段
if (assembled.orphans.lines.length) {
sections.push(`[远期片段] 记忆里残留的一些老画面\n${assembled.orphans.lines.join("\n")}`);
}
// 5. 待整理
if (assembled.recentOrphans.lines.length) {
sections.push(`[待整理] 最近发生但尚未梳理的原始记忆\n${assembled.recentOrphans.lines.join("\n")}`);
}
// 6. 人物弧光(最后注入,但预算已在优先级 2 预留)
if (assembled.arcs.lines.length) {
sections.push(`[人物弧光]\n${assembled.arcs.lines.join("\n")}`);
}
// ═══════════════════════════════════════════════════════════════════
// 统计 & 返回
// ═══════════════════════════════════════════════════════════════════
// 总预算 = 主装配 + 待整理
injectionStats.budget.used = total.used + (recentOrphanStats.tokens || 0);
2026-01-27 16:04:57 +08:00
if (!sections.length) {
return { promptText: "", injectionLogText: "", injectionStats };
2026-01-26 01:16:35 +08:00
}
2026-01-27 16:04:57 +08:00
const promptText =
`${buildSystemPreamble()}\n` +
`<剧情记忆>\n\n${sections.join("\n\n")}\n\n</剧情记忆>\n` +
`${buildPostscript()}`;
2026-01-27 22:51:44 +08:00
const injectionLogText = formatInjectionLog(injectionStats, details, recentOrphanStats);
return { promptText, injectionLogText, injectionStats };
2026-01-26 01:16:35 +08:00
}
// ─────────────────────────────────────────────────────────────────────────────
2026-01-27 16:04:57 +08:00
// 因果证据补充(给 causalEvents 挂 evidence chunk
// ─────────────────────────────────────────────────────────────────────────────
2026-01-26 01:16:35 +08:00
2026-01-27 16:04:57 +08:00
async function attachEvidenceToCausalEvents(causalEvents, eventVectorMap, chunkVectorMap, chunksMap) {
for (const c of causalEvents) {
c._evidenceChunk = null;
const ev = c.event;
if (!ev?.id) continue;
const evVec = eventVectorMap.get(ev.id);
if (!evVec?.length) continue;
const range = parseFloorRange(ev.summary);
if (!range) continue;
const candidateChunks = [];
for (const [chunkId, chunk] of chunksMap) {
if (chunk.floor >= range.start && chunk.floor <= range.end) {
const vec = chunkVectorMap.get(chunkId);
if (vec?.length) candidateChunks.push({ chunk, vec });
}
}
if (!candidateChunks.length) continue;
let best = null;
let bestSim = -1;
for (const { chunk, vec } of candidateChunks) {
const sim = cosineSimilarity(evVec, vec);
if (sim > bestSim) {
bestSim = sim;
best = chunk;
}
}
if (best && bestSim > 0.3) {
c._evidenceChunk = {
floor: best.floor,
speaker: best.speaker,
text: best.text,
similarity: bestSim,
};
}
}
2026-01-26 01:16:35 +08:00
}
2026-01-27 16:04:57 +08:00
// ─────────────────────────────────────────────────────────────────────────────
// ✅ 向量模式:召回 + 注入(供 story-summary.js 在 GENERATION_STARTED 调用)
// ─────────────────────────────────────────────────────────────────────────────
2026-01-29 01:17:37 +08:00
export async function buildVectorPromptText(excludeLastAi = false, hooks = {}) {
const { postToFrame = null, echo = null, pendingUserMessage = null } = hooks;
2026-01-26 01:16:35 +08:00
if (!getSettings().storySummary?.enabled) {
2026-01-29 01:17:37 +08:00
return { text: "", logText: "" };
2026-01-26 01:16:35 +08:00
}
const { chat } = getContext();
const store = getSummaryStore();
if (!store?.json) {
2026-01-29 01:17:37 +08:00
return { text: "", logText: "" };
2026-01-26 01:16:35 +08:00
}
const allEvents = store.json.events || [];
const lastIdx = store.lastSummarizedMesId ?? 0;
const length = chat?.length || 0;
if (lastIdx >= length) {
2026-01-29 01:17:37 +08:00
return { text: "", logText: "" };
2026-01-26 01:16:35 +08:00
}
const vectorCfg = getVectorConfig();
2026-01-27 16:04:57 +08:00
if (!vectorCfg?.enabled) {
2026-01-29 01:17:37 +08:00
return { text: "", logText: "" };
2026-01-27 16:04:57 +08:00
}
2026-01-27 22:51:44 +08:00
const { chatId } = getContext();
// meta 用于 lastChunkFloor供 buildVectorPrompt 分桶)
const meta = chatId ? await getMeta(chatId) : null;
2026-01-27 16:04:57 +08:00
let recallResult = null;
let causalById = new Map();
2026-01-26 01:16:35 +08:00
2026-01-27 16:04:57 +08:00
try {
const queryText = buildQueryText(chat, 2, excludeLastAi);
2026-01-29 01:17:37 +08:00
recallResult = await recallMemory(queryText, allEvents, vectorCfg, {
excludeLastAi,
pendingUserMessage,
});
2026-01-27 16:04:57 +08:00
recallResult = {
...recallResult,
events: recallResult?.events || [],
chunks: recallResult?.chunks || [],
causalEvents: recallResult?.causalEvents || [],
queryEntities: recallResult?.queryEntities || [],
logText: recallResult?.logText || "",
};
// 给因果事件挂证据(用于因果行展示)
const causalEvents = recallResult.causalEvents || [];
if (causalEvents.length > 0) {
const { chatId } = getContext();
if (chatId) {
try {
const floors = new Set();
for (const c of causalEvents) {
const r = parseFloorRange(c.event?.summary);
if (!r) continue;
for (let f = r.start; f <= r.end; f++) floors.add(f);
}
2026-01-27 16:04:57 +08:00
const [chunks, chunkVecs, eventVecs] = await Promise.all([
getChunksByFloors(chatId, Array.from(floors)),
getAllChunkVectors(chatId),
getAllEventVectors(chatId),
]);
const chunksMap = new Map(chunks.map(c => [c.chunkId, c]));
const chunkVectorMap = new Map(chunkVecs.map(v => [v.chunkId, v.vector]));
const eventVectorMap = new Map(eventVecs.map(v => [v.eventId, v.vector]));
await attachEvidenceToCausalEvents(causalEvents, eventVectorMap, chunkVectorMap, chunksMap);
} catch (e) {
xbLog.warn(MODULE_ID, "Causal evidence attachment failed", e);
}
}
2026-01-27 16:04:57 +08:00
}
causalById = new Map(
recallResult.causalEvents
.map(c => [c?.event?.id, c])
.filter(x => x[0])
);
} catch (e) {
xbLog.error(MODULE_ID, "向量召回失败", e);
// 显式提示(节流)
if (echo && canNotifyRecallFail()) {
const msg = String(e?.message || "未知错误").replace(/\s+/g, " ").slice(0, 200);
await echo(`/echo severity=warning 向量召回失败:${msg}`);
}
// iframe 日志也写一份
if (postToFrame) {
postToFrame({
type: "RECALL_LOG",
text: `\n[Vector Recall Failed]\n${String(e?.stack || e?.message || e)}\n`,
});
}
2026-01-29 01:17:37 +08:00
return { text: "", logText: `\n[Vector Recall Failed]\n${String(e?.stack || e?.message || e)}\n` };
2026-01-27 16:04:57 +08:00
}
// 成功但结果为空:也提示,并清空注入(不降级)
const hasUseful =
(recallResult?.events?.length || 0) > 0 ||
(recallResult?.chunks?.length || 0) > 0 ||
(recallResult?.causalEvents?.length || 0) > 0;
if (!hasUseful) {
if (echo && canNotifyRecallFail()) {
await echo(
"/echo severity=warning 向量召回失败:没有可用召回结果(请先在面板中生成向量,或检查指纹不匹配)"
);
2026-01-26 01:16:35 +08:00
}
2026-01-27 16:04:57 +08:00
if (postToFrame) {
postToFrame({
type: "RECALL_LOG",
text: "\n[Vector Recall Empty]\nNo recall candidates / vectors not ready.\n",
});
}
2026-01-29 01:17:37 +08:00
return { text: "", logText: "\n[Vector Recall Empty]\nNo recall candidates / vectors not ready.\n" };
2026-01-26 01:16:35 +08:00
}
2026-01-27 16:04:57 +08:00
// 拼装向量 prompt
const { promptText, injectionLogText } = await buildVectorPrompt(
store,
recallResult,
causalById,
2026-01-27 22:51:44 +08:00
recallResult?.queryEntities || [],
meta
);
2026-01-29 01:17:37 +08:00
// wrapper沿用面板设置——必须补回否则语义回退
const cfg = getSummaryPanelConfig();
let finalText = String(promptText || "");
if (cfg.trigger?.wrapperHead) finalText = cfg.trigger.wrapperHead + "\n" + finalText;
if (cfg.trigger?.wrapperTail) finalText = finalText + "\n" + cfg.trigger.wrapperTail;
2026-01-27 16:04:57 +08:00
// 发给涌现窗口:召回报告 + 装配报告
if (postToFrame) {
const recallLog = recallResult.logText || "";
2026-01-27 16:04:57 +08:00
postToFrame({ type: "RECALL_LOG", text: recallLog + (injectionLogText || "") });
2026-01-26 01:16:35 +08:00
}
2026-01-27 16:04:57 +08:00
2026-01-29 01:17:37 +08:00
return { text: finalText, logText: (recallResult.logText || "") + (injectionLogText || "") };
2026-01-26 01:16:35 +08:00
}