2026-02-08 12:22:45 +08:00
|
|
|
|
// ═══════════════════════════════════════════════════════════════════════════
|
2026-02-11 22:01:02 +08:00
|
|
|
|
// Story Summary - Prompt Injection (v7 - L0 scene-based display)
|
2026-02-09 10:09:16 +08:00
|
|
|
|
//
|
|
|
|
|
|
// 命名规范:
|
|
|
|
|
|
// - 存储层用 L0/L1/L2/L3(StateAtom/Chunk/Event/Fact)
|
|
|
|
|
|
// - 装配层用语义名称:constraint/event/evidence/arc
|
|
|
|
|
|
//
|
2026-02-10 12:43:43 +08:00
|
|
|
|
// 架构变更(v5 → v6):
|
|
|
|
|
|
// - 同楼层多个 L0 共享一对 L1(EvidenceGroup per-floor)
|
2026-02-11 22:01:02 +08:00
|
|
|
|
// - L0 展示文本直接使用 semantic 字段(v7: 场景摘要,纯自然语言)
|
2026-02-05 00:22:02 +08:00
|
|
|
|
// - 仅负责"构建注入文本",不负责写入 extension_prompts
|
2026-02-08 12:22:45 +08:00
|
|
|
|
// - 注入发生在 story-summary.js:GENERATION_STARTED 时写入 extension_prompts
|
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";
|
2026-02-02 21:45:01 +08:00
|
|
|
|
import { getSummaryStore, getFacts, isRelationFact } from "../data/store.js";
|
2026-01-26 01:16:35 +08:00
|
|
|
|
import { getVectorConfig, getSummaryPanelConfig, getSettings } from "../data/config.js";
|
2026-02-09 15:26:43 +08:00
|
|
|
|
import { recallMemory } from "../vector/retrieval/recall.js";
|
2026-02-10 00:18:51 +08:00
|
|
|
|
import { getMeta } from "../vector/storage/chunk-store.js";
|
2026-02-10 12:43:43 +08:00
|
|
|
|
import { getEngineFingerprint } from "../vector/utils/embedder.js";
|
2026-02-15 18:58:51 +08:00
|
|
|
|
import { buildTrustedCharacters } from "../vector/retrieval/entity-lexicon.js";
|
2026-01-26 01:16:35 +08:00
|
|
|
|
|
2026-02-09 10:09:16 +08:00
|
|
|
|
// Metrics
|
2026-02-08 12:22:45 +08:00
|
|
|
|
import { formatMetricsLog, detectIssues } from "../vector/retrieval/metrics.js";
|
|
|
|
|
|
|
2026-01-26 01:16:35 +08:00
|
|
|
|
const MODULE_ID = "summaryPrompt";
|
|
|
|
|
|
|
2026-01-26 23:50:48 +08:00
|
|
|
|
// ─────────────────────────────────────────────────────────────────────────────
|
2026-02-08 12:22:45 +08:00
|
|
|
|
// 召回失败提示节流
|
2026-01-26 23:50:48 +08:00
|
|
|
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
|
|
|
|
|
2026-01-27 16:04:57 +08:00
|
|
|
|
let lastRecallFailAt = 0;
|
|
|
|
|
|
const RECALL_FAIL_COOLDOWN_MS = 10_000;
|
2026-01-26 23:50:48 +08:00
|
|
|
|
|
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-26 23:50:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ─────────────────────────────────────────────────────────────────────────────
|
2026-02-08 12:22:45 +08:00
|
|
|
|
// 预算常量
|
2026-01-26 23:50:48 +08:00
|
|
|
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
|
|
|
|
|
2026-02-12 15:36:07 +08:00
|
|
|
|
const SHARED_POOL_MAX = 10000;
|
2026-02-09 10:09:16 +08:00
|
|
|
|
const CONSTRAINT_MAX = 2000;
|
2026-01-27 22:51:44 +08:00
|
|
|
|
const ARCS_MAX = 1500;
|
2026-02-12 15:36:07 +08:00
|
|
|
|
const EVENT_BUDGET_MAX = 5000;
|
2026-02-15 01:07:16 +08:00
|
|
|
|
const RELATED_EVENT_MAX = 500;
|
2026-02-16 23:26:45 +08:00
|
|
|
|
const SUMMARIZED_EVIDENCE_MAX = 2000;
|
2026-02-14 23:15:00 +08:00
|
|
|
|
const UNSUMMARIZED_EVIDENCE_MAX = 2000;
|
2026-02-08 12:22:45 +08:00
|
|
|
|
const TOP_N_STAR = 5;
|
2026-01-26 23:50:48 +08:00
|
|
|
|
|
2026-02-10 12:43:43 +08:00
|
|
|
|
// L0 显示文本:分号拼接 vs 多行模式的阈值
|
|
|
|
|
|
const L0_JOINED_MAX_LENGTH = 120;
|
2026-02-12 15:36:07 +08:00
|
|
|
|
// 背景证据:无实体匹配时保留的最低相似度(与 recall.js CONFIG.EVENT_ENTITY_BYPASS_SIM 保持一致)
|
2026-02-10 12:43:43 +08:00
|
|
|
|
|
2026-01-26 23:50:48 +08:00
|
|
|
|
// ─────────────────────────────────────────────────────────────────────────────
|
2026-01-26 01:16:35 +08:00
|
|
|
|
// 工具函数
|
2026-01-26 23:50:48 +08:00
|
|
|
|
// ─────────────────────────────────────────────────────────────────────────────
|
2026-01-26 01:16:35 +08:00
|
|
|
|
|
2026-02-09 10:09:16 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 估算文本 token 数量
|
|
|
|
|
|
* @param {string} text - 输入文本
|
|
|
|
|
|
* @returns {number} token 估算值
|
|
|
|
|
|
*/
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-09 10:09:16 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 带预算限制的行追加
|
|
|
|
|
|
* @param {string[]} lines - 行数组
|
|
|
|
|
|
* @param {string} text - 要追加的文本
|
|
|
|
|
|
* @param {object} state - 预算状态 {used, max}
|
|
|
|
|
|
* @returns {boolean} 是否追加成功
|
|
|
|
|
|
*/
|
2026-01-26 01:16:35 +08:00
|
|
|
|
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-02-09 10:09:16 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 解析事件摘要中的楼层范围
|
|
|
|
|
|
* @param {string} summary - 事件摘要
|
|
|
|
|
|
* @returns {{start: number, end: number}|null} 楼层范围
|
|
|
|
|
|
*/
|
2026-01-26 01:16:35 +08:00
|
|
|
|
function parseFloorRange(summary) {
|
|
|
|
|
|
if (!summary) return null;
|
|
|
|
|
|
const match = String(summary).match(/\(#(\d+)(?:-(\d+))?\)/);
|
|
|
|
|
|
if (!match) return null;
|
2026-01-26 23:50:48 +08:00
|
|
|
|
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-02-09 10:09:16 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 清理事件摘要(移除楼层标记)
|
|
|
|
|
|
* @param {string} summary - 事件摘要
|
|
|
|
|
|
* @returns {string} 清理后的摘要
|
|
|
|
|
|
*/
|
2026-01-26 01:16:35 +08:00
|
|
|
|
function cleanSummary(summary) {
|
2026-01-26 23:50:48 +08:00
|
|
|
|
return String(summary || "")
|
|
|
|
|
|
.replace(/\s*\(#\d+(?:-\d+)?\)\s*$/, "")
|
|
|
|
|
|
.trim();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-09 10:09:16 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 标准化字符串
|
|
|
|
|
|
* @param {string} s - 输入字符串
|
|
|
|
|
|
* @returns {string} 标准化后的字符串
|
|
|
|
|
|
*/
|
2026-02-08 12:22:45 +08:00
|
|
|
|
function normalize(s) {
|
|
|
|
|
|
return String(s || '')
|
|
|
|
|
|
.normalize('NFKC')
|
|
|
|
|
|
.replace(/[\u200B-\u200D\uFEFF]/g, '')
|
|
|
|
|
|
.trim()
|
|
|
|
|
|
.toLowerCase();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-12 00:05:19 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 收集 L0 的实体集合(用于背景证据实体过滤)
|
2026-02-13 15:56:22 +08:00
|
|
|
|
* 使用 edges.s/edges.t。
|
2026-02-12 00:05:19 +08:00
|
|
|
|
* @param {object} l0
|
|
|
|
|
|
* @returns {Set<string>}
|
|
|
|
|
|
*/
|
|
|
|
|
|
function collectL0Entities(l0) {
|
2026-02-13 15:56:22 +08:00
|
|
|
|
const atom = l0?.atom || {};
|
2026-02-12 00:05:19 +08:00
|
|
|
|
const set = new Set();
|
|
|
|
|
|
|
|
|
|
|
|
const add = (v) => {
|
|
|
|
|
|
const n = normalize(v);
|
|
|
|
|
|
if (n) set.add(n);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
for (const e of (atom.edges || [])) {
|
|
|
|
|
|
add(e?.s);
|
|
|
|
|
|
add(e?.t);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return set;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 背景证据是否保留(按焦点实体过滤)
|
|
|
|
|
|
* 规则:
|
|
|
|
|
|
* 1) 无焦点实体:保留
|
2026-02-12 15:36:07 +08:00
|
|
|
|
* 2) similarity >= 0.70:保留(旁通)
|
2026-02-13 15:56:22 +08:00
|
|
|
|
* 3) edges 命中焦点实体:保留
|
2026-02-12 00:05:19 +08:00
|
|
|
|
* 否则过滤。
|
|
|
|
|
|
* @param {object} l0
|
|
|
|
|
|
* @param {Set<string>} focusSet
|
|
|
|
|
|
* @returns {boolean}
|
|
|
|
|
|
*/
|
|
|
|
|
|
function shouldKeepEvidenceL0(l0, focusSet) {
|
2026-02-13 18:30:11 +08:00
|
|
|
|
if (!focusSet?.size) return false;
|
2026-02-12 00:05:19 +08:00
|
|
|
|
|
|
|
|
|
|
const entities = collectL0Entities(l0);
|
|
|
|
|
|
for (const f of focusSet) {
|
|
|
|
|
|
if (entities.has(f)) return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-13 18:30:11 +08:00
|
|
|
|
// 兼容旧数据:semantic 文本包含焦点实体
|
|
|
|
|
|
const textNorm = normalize(l0?.atom?.semantic || l0?.text || '');
|
|
|
|
|
|
for (const f of focusSet) {
|
|
|
|
|
|
if (f && textNorm.includes(f)) return true;
|
|
|
|
|
|
}
|
2026-02-12 00:05:19 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-09 10:09:16 +08:00
|
|
|
|
/**
|
2026-02-10 00:18:51 +08:00
|
|
|
|
* 获取事件排序键
|
|
|
|
|
|
* @param {object} event - 事件对象
|
|
|
|
|
|
* @returns {number} 排序键
|
2026-02-09 10:09:16 +08:00
|
|
|
|
*/
|
2026-02-10 00:18:51 +08:00
|
|
|
|
function getEventSortKey(event) {
|
|
|
|
|
|
const r = parseFloorRange(event?.summary);
|
|
|
|
|
|
if (r) return r.start;
|
|
|
|
|
|
const m = String(event?.id || "").match(/evt-(\d+)/);
|
|
|
|
|
|
return m ? parseInt(m[1], 10) : Number.MAX_SAFE_INTEGER;
|
2026-02-05 00:22:02 +08:00
|
|
|
|
}
|
2026-02-08 12:22:45 +08:00
|
|
|
|
|
2026-02-09 10:09:16 +08:00
|
|
|
|
/**
|
2026-02-10 00:18:51 +08:00
|
|
|
|
* 重新编号事件文本
|
|
|
|
|
|
* @param {string} text - 原始文本
|
|
|
|
|
|
* @param {number} newIndex - 新编号
|
|
|
|
|
|
* @returns {string} 重新编号后的文本
|
2026-02-09 10:09:16 +08:00
|
|
|
|
*/
|
2026-02-10 00:18:51 +08:00
|
|
|
|
function renumberEventText(text, newIndex) {
|
|
|
|
|
|
const s = String(text || "");
|
|
|
|
|
|
return s.replace(/^(\s*)\d+(\.\s*(?:【)?)/, `$1${newIndex}$2`);
|
2026-02-05 00:22:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-26 23:50:48 +08:00
|
|
|
|
// ─────────────────────────────────────────────────────────────────────────────
|
2026-01-27 16:04:57 +08:00
|
|
|
|
// 系统前导与后缀
|
2026-01-26 23:50:48 +08:00
|
|
|
|
// ─────────────────────────────────────────────────────────────────────────────
|
2026-01-26 01:16:35 +08:00
|
|
|
|
|
2026-02-09 10:09:16 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 构建系统前导文本
|
|
|
|
|
|
* @returns {string} 前导文本
|
|
|
|
|
|
*/
|
2026-01-27 16:04:57 +08:00
|
|
|
|
function buildSystemPreamble() {
|
|
|
|
|
|
return [
|
2026-02-02 14:02:12 +08:00
|
|
|
|
"以上是还留在眼前的对话",
|
|
|
|
|
|
"以下是脑海里的记忆:",
|
|
|
|
|
|
"• [定了的事] 这些是不会变的",
|
2026-02-13 18:30:11 +08:00
|
|
|
|
"• [其他人的事] 别人的经历,当前角色可能不知晓",
|
2026-01-30 00:55:04 +08:00
|
|
|
|
"• 其余部分是过往经历的回忆碎片",
|
2026-01-27 16:04:57 +08:00
|
|
|
|
"",
|
2026-01-30 00:55:04 +08:00
|
|
|
|
"请内化这些记忆:",
|
2026-01-27 16:04:57 +08:00
|
|
|
|
].join("\n");
|
2026-01-26 01:16:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-09 10:09:16 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 构建后缀文本
|
|
|
|
|
|
* @returns {string} 后缀文本
|
|
|
|
|
|
*/
|
2026-01-27 16:04:57 +08:00
|
|
|
|
function buildPostscript() {
|
|
|
|
|
|
return [
|
|
|
|
|
|
"",
|
2026-02-02 14:02:12 +08:00
|
|
|
|
"这些记忆是真实的,请自然地记住它们。",
|
2026-01-27 16:04:57 +08:00
|
|
|
|
].join("\n");
|
2026-01-26 23:50:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ─────────────────────────────────────────────────────────────────────────────
|
2026-02-09 10:09:16 +08:00
|
|
|
|
// [Constraints] L3 Facts 过滤与格式化
|
2026-01-26 23:50:48 +08:00
|
|
|
|
// ─────────────────────────────────────────────────────────────────────────────
|
2026-01-26 01:16:35 +08:00
|
|
|
|
|
2026-02-09 10:09:16 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 获取已知角色集合
|
|
|
|
|
|
* @param {object} store - 存储对象
|
|
|
|
|
|
* @returns {Set<string>} 角色名称集合(标准化后)
|
|
|
|
|
|
*/
|
2026-02-08 12:22:45 +08:00
|
|
|
|
function getKnownCharacters(store) {
|
|
|
|
|
|
const { name1, name2 } = getContext();
|
2026-02-15 18:58:51 +08:00
|
|
|
|
const names = buildTrustedCharacters(store, { name1, name2 }) || new Set();
|
|
|
|
|
|
// Keep name1 in known-character filtering domain to avoid behavior regression
|
|
|
|
|
|
// for L3 subject filtering (lexicon exclusion and filtering semantics are different concerns).
|
2026-02-08 12:22:45 +08:00
|
|
|
|
if (name1) names.add(normalize(name1));
|
|
|
|
|
|
return names;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-09 10:09:16 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 解析关系谓词中的目标
|
|
|
|
|
|
* @param {string} predicate - 谓词
|
|
|
|
|
|
* @returns {string|null} 目标名称
|
|
|
|
|
|
*/
|
2026-02-08 12:22:45 +08:00
|
|
|
|
function parseRelationTarget(predicate) {
|
|
|
|
|
|
const match = String(predicate || '').match(/^对(.+)的/);
|
|
|
|
|
|
return match ? match[1] : null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-09 10:09:16 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 按相关性过滤 facts
|
|
|
|
|
|
* @param {object[]} facts - 所有 facts
|
2026-02-15 18:58:51 +08:00
|
|
|
|
* @param {string[]} focusCharacters - 焦点人物
|
2026-02-09 10:09:16 +08:00
|
|
|
|
* @param {Set<string>} knownCharacters - 已知角色
|
|
|
|
|
|
* @returns {object[]} 过滤后的 facts
|
|
|
|
|
|
*/
|
2026-02-15 18:58:51 +08:00
|
|
|
|
function filterConstraintsByRelevance(facts, focusCharacters, knownCharacters) {
|
2026-02-08 12:22:45 +08:00
|
|
|
|
if (!facts?.length) return [];
|
|
|
|
|
|
|
2026-02-15 18:58:51 +08:00
|
|
|
|
const focusSet = new Set((focusCharacters || []).map(normalize));
|
2026-02-08 12:22:45 +08:00
|
|
|
|
|
|
|
|
|
|
return facts.filter(f => {
|
|
|
|
|
|
if (f._isState === true) return true;
|
|
|
|
|
|
|
|
|
|
|
|
if (isRelationFact(f)) {
|
|
|
|
|
|
const from = normalize(f.s);
|
|
|
|
|
|
const target = parseRelationTarget(f.p);
|
|
|
|
|
|
const to = target ? normalize(target) : '';
|
|
|
|
|
|
|
|
|
|
|
|
if (focusSet.has(from) || focusSet.has(to)) return true;
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const subjectNorm = normalize(f.s);
|
|
|
|
|
|
if (knownCharacters.has(subjectNorm)) {
|
|
|
|
|
|
return focusSet.has(subjectNorm);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-09 10:09:16 +08:00
|
|
|
|
/**
|
2026-02-14 21:30:57 +08:00
|
|
|
|
* Build people dictionary for constraints display.
|
2026-02-15 18:58:51 +08:00
|
|
|
|
* Primary source: selected event participants; fallback: focus characters.
|
2026-02-14 21:30:57 +08:00
|
|
|
|
*
|
|
|
|
|
|
* @param {object|null} recallResult
|
2026-02-15 18:58:51 +08:00
|
|
|
|
* @param {string[]} focusCharacters
|
2026-02-14 21:30:57 +08:00
|
|
|
|
* @returns {Map<string, string>} normalize(name) -> display name
|
|
|
|
|
|
*/
|
2026-02-15 18:58:51 +08:00
|
|
|
|
function buildConstraintPeopleDict(recallResult, focusCharacters = []) {
|
2026-02-14 21:30:57 +08:00
|
|
|
|
const dict = new Map();
|
|
|
|
|
|
const add = (raw) => {
|
|
|
|
|
|
const display = String(raw || '').trim();
|
|
|
|
|
|
const key = normalize(display);
|
|
|
|
|
|
if (!display || !key) return;
|
|
|
|
|
|
if (!dict.has(key)) dict.set(key, display);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const selectedEvents = recallResult?.events || [];
|
|
|
|
|
|
for (const item of selectedEvents) {
|
|
|
|
|
|
const participants = item?.event?.participants || [];
|
|
|
|
|
|
for (const p of participants) add(p);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (dict.size === 0) {
|
2026-02-15 18:58:51 +08:00
|
|
|
|
for (const f of (focusCharacters || [])) add(f);
|
2026-02-14 21:30:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return dict;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Group filtered constraints into people/world buckets.
|
|
|
|
|
|
* @param {object[]} facts
|
|
|
|
|
|
* @param {Map<string, string>} peopleDict
|
|
|
|
|
|
* @returns {{ people: Map<string, object[]>, world: object[] }}
|
2026-02-09 10:09:16 +08:00
|
|
|
|
*/
|
2026-02-14 21:30:57 +08:00
|
|
|
|
function groupConstraintsForDisplay(facts, peopleDict) {
|
|
|
|
|
|
const people = new Map();
|
|
|
|
|
|
const world = [];
|
|
|
|
|
|
|
|
|
|
|
|
for (const f of (facts || [])) {
|
|
|
|
|
|
const subjectNorm = normalize(f?.s);
|
|
|
|
|
|
const displayName = peopleDict.get(subjectNorm);
|
|
|
|
|
|
if (displayName) {
|
|
|
|
|
|
if (!people.has(displayName)) people.set(displayName, []);
|
|
|
|
|
|
people.get(displayName).push(f);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
world.push(f);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return { people, world };
|
|
|
|
|
|
}
|
2026-02-08 12:22:45 +08:00
|
|
|
|
|
2026-02-14 21:30:57 +08:00
|
|
|
|
function formatConstraintLine(f, includeSubject = false) {
|
|
|
|
|
|
const subject = String(f?.s || '').trim();
|
|
|
|
|
|
const predicate = String(f?.p || '').trim();
|
|
|
|
|
|
const object = String(f?.o || '').trim();
|
|
|
|
|
|
const trendRaw = String(f?.trend || '').trim();
|
|
|
|
|
|
const hasSince = f?.since !== undefined && f?.since !== null;
|
|
|
|
|
|
const since = hasSince ? ` (#${f.since + 1})` : '';
|
|
|
|
|
|
const trend = isRelationFact(f) && trendRaw ? ` [${trendRaw}]` : '';
|
|
|
|
|
|
if (includeSubject) {
|
|
|
|
|
|
return `- ${subject} ${predicate}: ${object}${trend}${since}`;
|
|
|
|
|
|
}
|
|
|
|
|
|
return `- ${predicate}: ${object}${trend}${since}`;
|
|
|
|
|
|
}
|
2026-02-08 12:22:45 +08:00
|
|
|
|
|
2026-02-14 21:30:57 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* Render grouped constraints into structured human-readable lines.
|
|
|
|
|
|
* @param {{ people: Map<string, object[]>, world: object[] }} grouped
|
|
|
|
|
|
* @returns {string[]}
|
|
|
|
|
|
*/
|
2026-02-15 18:58:51 +08:00
|
|
|
|
function formatConstraintsStructured(grouped, order = 'desc') {
|
2026-02-14 21:30:57 +08:00
|
|
|
|
const lines = [];
|
|
|
|
|
|
const people = grouped?.people || new Map();
|
|
|
|
|
|
const world = grouped?.world || [];
|
2026-02-15 18:58:51 +08:00
|
|
|
|
const sorter = order === 'asc'
|
|
|
|
|
|
? ((a, b) => (a.since || 0) - (b.since || 0))
|
|
|
|
|
|
: ((a, b) => (b.since || 0) - (a.since || 0));
|
2026-02-14 21:30:57 +08:00
|
|
|
|
|
|
|
|
|
|
if (people.size > 0) {
|
|
|
|
|
|
lines.push('people:');
|
|
|
|
|
|
for (const [name, facts] of people.entries()) {
|
|
|
|
|
|
lines.push(` ${name}:`);
|
2026-02-15 18:58:51 +08:00
|
|
|
|
const sorted = [...facts].sort(sorter);
|
2026-02-14 21:30:57 +08:00
|
|
|
|
for (const f of sorted) {
|
|
|
|
|
|
lines.push(` ${formatConstraintLine(f, false)}`);
|
2026-02-02 21:45:01 +08:00
|
|
|
|
}
|
2026-02-14 21:30:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (world.length > 0) {
|
|
|
|
|
|
lines.push('world:');
|
2026-02-15 18:58:51 +08:00
|
|
|
|
const sortedWorld = [...world].sort(sorter);
|
2026-02-14 21:30:57 +08:00
|
|
|
|
for (const f of sortedWorld) {
|
|
|
|
|
|
lines.push(` ${formatConstraintLine(f, true)}`);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return lines;
|
2026-01-26 01:16:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-15 18:58:51 +08:00
|
|
|
|
function tryConsumeConstraintLineBudget(line, budgetState) {
|
|
|
|
|
|
const cost = estimateTokens(line);
|
|
|
|
|
|
if (budgetState.used + cost > budgetState.max) return false;
|
|
|
|
|
|
budgetState.used += cost;
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function selectConstraintsByBudgetDesc(grouped, budgetState) {
|
|
|
|
|
|
const selectedPeople = new Map();
|
|
|
|
|
|
const selectedWorld = [];
|
|
|
|
|
|
const people = grouped?.people || new Map();
|
|
|
|
|
|
const world = grouped?.world || [];
|
|
|
|
|
|
|
|
|
|
|
|
if (people.size > 0) {
|
|
|
|
|
|
if (!tryConsumeConstraintLineBudget('people:', budgetState)) {
|
|
|
|
|
|
return { people: selectedPeople, world: selectedWorld };
|
|
|
|
|
|
}
|
|
|
|
|
|
for (const [name, facts] of people.entries()) {
|
|
|
|
|
|
const header = ` ${name}:`;
|
|
|
|
|
|
if (!tryConsumeConstraintLineBudget(header, budgetState)) {
|
|
|
|
|
|
return { people: selectedPeople, world: selectedWorld };
|
|
|
|
|
|
}
|
|
|
|
|
|
const picked = [];
|
|
|
|
|
|
const sorted = [...facts].sort((a, b) => (b.since || 0) - (a.since || 0));
|
|
|
|
|
|
for (const f of sorted) {
|
|
|
|
|
|
const line = ` ${formatConstraintLine(f, false)}`;
|
|
|
|
|
|
if (!tryConsumeConstraintLineBudget(line, budgetState)) {
|
|
|
|
|
|
return { people: selectedPeople, world: selectedWorld };
|
|
|
|
|
|
}
|
|
|
|
|
|
picked.push(f);
|
|
|
|
|
|
}
|
|
|
|
|
|
selectedPeople.set(name, picked);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (world.length > 0) {
|
|
|
|
|
|
if (!tryConsumeConstraintLineBudget('world:', budgetState)) {
|
|
|
|
|
|
return { people: selectedPeople, world: selectedWorld };
|
|
|
|
|
|
}
|
|
|
|
|
|
const sortedWorld = [...world].sort((a, b) => (b.since || 0) - (a.since || 0));
|
|
|
|
|
|
for (const f of sortedWorld) {
|
|
|
|
|
|
const line = ` ${formatConstraintLine(f, true)}`;
|
|
|
|
|
|
if (!tryConsumeConstraintLineBudget(line, budgetState)) {
|
|
|
|
|
|
return { people: selectedPeople, world: selectedWorld };
|
|
|
|
|
|
}
|
|
|
|
|
|
selectedWorld.push(f);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return { people: selectedPeople, world: selectedWorld };
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-08 12:22:45 +08:00
|
|
|
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
|
|
|
|
// 格式化函数
|
|
|
|
|
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
|
|
|
|
|
2026-02-09 10:09:16 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 格式化弧光行
|
|
|
|
|
|
* @param {object} arc - 弧光对象
|
|
|
|
|
|
* @returns {string} 格式化后的行
|
|
|
|
|
|
*/
|
|
|
|
|
|
function formatArcLine(arc) {
|
|
|
|
|
|
const moments = (arc.moments || [])
|
2026-01-27 16:04:57 +08:00
|
|
|
|
.map(m => (typeof m === "string" ? m : m.text))
|
|
|
|
|
|
.filter(Boolean);
|
2026-01-26 23:50:48 +08:00
|
|
|
|
|
2026-01-27 16:04:57 +08:00
|
|
|
|
if (moments.length) {
|
2026-02-09 10:09:16 +08:00
|
|
|
|
return `- ${arc.name}:${moments.join(" → ")}`;
|
2026-01-26 01:16:35 +08:00
|
|
|
|
}
|
2026-02-09 10:09:16 +08:00
|
|
|
|
return `- ${arc.name}:${arc.trajectory}`;
|
2026-01-27 16:04:57 +08:00
|
|
|
|
}
|
2026-01-26 23:50:48 +08:00
|
|
|
|
|
2026-02-09 10:09:16 +08:00
|
|
|
|
/**
|
2026-02-11 22:01:02 +08:00
|
|
|
|
* 从 L0 获取展示文本
|
2026-02-10 12:43:43 +08:00
|
|
|
|
*
|
2026-02-11 22:01:02 +08:00
|
|
|
|
* v7: L0 的 semantic 字段已是纯自然语言场景摘要(60-100字),直接使用。
|
2026-02-10 12:43:43 +08:00
|
|
|
|
*
|
2026-02-11 22:01:02 +08:00
|
|
|
|
* @param {object} l0 - L0 对象
|
|
|
|
|
|
* @returns {string} 场景描述文本
|
2026-02-09 10:09:16 +08:00
|
|
|
|
*/
|
2026-02-10 12:43:43 +08:00
|
|
|
|
function buildL0DisplayText(l0) {
|
2026-02-11 22:01:02 +08:00
|
|
|
|
const atom = l0.atom || {};
|
2026-02-12 15:36:07 +08:00
|
|
|
|
return String(atom.semantic || l0.text || '').trim() || '(未知锚点)';
|
2026-02-10 00:18:51 +08:00
|
|
|
|
}
|
2026-01-31 23:06:03 +08:00
|
|
|
|
|
2026-02-10 00:18:51 +08:00
|
|
|
|
/**
|
2026-02-10 12:43:43 +08:00
|
|
|
|
* 格式化 L1 chunk 行
|
2026-02-10 00:18:51 +08:00
|
|
|
|
* @param {object} chunk - L1 chunk 对象
|
|
|
|
|
|
* @param {boolean} isContext - 是否为上下文(USER 侧)
|
|
|
|
|
|
* @returns {string} 格式化后的行
|
|
|
|
|
|
*/
|
|
|
|
|
|
function formatL1Line(chunk, isContext) {
|
|
|
|
|
|
const { name1, name2 } = getContext();
|
2026-02-09 10:09:16 +08:00
|
|
|
|
const speaker = chunk.isUser ? (name1 || "用户") : (chunk.speaker || name2 || "角色");
|
2026-02-10 00:18:51 +08:00
|
|
|
|
const text = String(chunk.text || "").trim();
|
|
|
|
|
|
const symbol = isContext ? "┌" : "›";
|
|
|
|
|
|
return ` ${symbol} #${chunk.floor + 1} [${speaker}] ${text}`;
|
2026-01-26 23:50:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-09 10:09:16 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 格式化因果事件行
|
|
|
|
|
|
* @param {object} causalItem - 因果事件项
|
|
|
|
|
|
* @returns {string} 格式化后的行
|
|
|
|
|
|
*/
|
2026-02-10 00:18:51 +08:00
|
|
|
|
function formatCausalEventLine(causalItem) {
|
2026-01-26 23:50:48 +08:00
|
|
|
|
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}`);
|
|
|
|
|
|
|
|
|
|
|
|
return lines.join("\n");
|
2026-01-26 01:16:35 +08:00
|
|
|
|
}
|
2026-02-01 16:26:29 +08:00
|
|
|
|
|
2026-02-10 00:18:51 +08:00
|
|
|
|
// ─────────────────────────────────────────────────────────────────────────────
|
2026-02-10 12:43:43 +08:00
|
|
|
|
// L0 按楼层分组
|
2026-02-10 00:18:51 +08:00
|
|
|
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
|
|
|
|
|
2026-02-09 10:09:16 +08:00
|
|
|
|
/**
|
2026-02-10 12:43:43 +08:00
|
|
|
|
* 将 L0 列表按楼层分组
|
|
|
|
|
|
* @param {object[]} l0List - L0 对象列表
|
|
|
|
|
|
* @returns {Map<number, object[]>} floor → L0 数组
|
|
|
|
|
|
*/
|
|
|
|
|
|
function groupL0ByFloor(l0List) {
|
|
|
|
|
|
const map = new Map();
|
|
|
|
|
|
for (const l0 of l0List) {
|
|
|
|
|
|
const floor = l0.floor;
|
|
|
|
|
|
if (!map.has(floor)) {
|
|
|
|
|
|
map.set(floor, []);
|
|
|
|
|
|
}
|
|
|
|
|
|
map.get(floor).push(l0);
|
|
|
|
|
|
}
|
|
|
|
|
|
return map;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
|
|
|
|
// EvidenceGroup(per-floor:N个L0 + 共享一对L1)
|
|
|
|
|
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @typedef {object} EvidenceGroup
|
|
|
|
|
|
* @property {number} floor - 楼层号
|
|
|
|
|
|
* @property {object[]} l0Atoms - 该楼层所有被选中的 L0
|
|
|
|
|
|
* @property {object|null} userL1 - USER 侧 top-1 L1 chunk(仅一份)
|
|
|
|
|
|
* @property {object|null} aiL1 - AI 侧 top-1 L1 chunk(仅一份)
|
|
|
|
|
|
* @property {number} totalTokens - 整组 token 估算
|
2026-02-09 10:09:16 +08:00
|
|
|
|
*/
|
2026-02-10 00:18:51 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
2026-02-10 12:43:43 +08:00
|
|
|
|
* 为一个楼层构建证据组
|
|
|
|
|
|
*
|
|
|
|
|
|
* 同楼层多个 L0 共享一对 L1,避免 L1 重复输出。
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param {number} floor - 楼层号
|
|
|
|
|
|
* @param {object[]} l0AtomsForFloor - 该楼层所有被选中的 L0
|
2026-02-10 00:18:51 +08:00
|
|
|
|
* @param {Map<number, object>} l1ByFloor - 楼层→L1配对映射
|
2026-02-10 12:43:43 +08:00
|
|
|
|
* @returns {EvidenceGroup}
|
2026-02-10 00:18:51 +08:00
|
|
|
|
*/
|
2026-02-10 12:43:43 +08:00
|
|
|
|
function buildEvidenceGroup(floor, l0AtomsForFloor, l1ByFloor) {
|
|
|
|
|
|
const pair = l1ByFloor.get(floor);
|
2026-02-10 00:18:51 +08:00
|
|
|
|
const userL1 = pair?.userTop1 || null;
|
|
|
|
|
|
const aiL1 = pair?.aiTop1 || null;
|
|
|
|
|
|
|
2026-02-10 12:43:43 +08:00
|
|
|
|
// 计算整组 token 开销
|
|
|
|
|
|
let totalTokens = 0;
|
|
|
|
|
|
|
|
|
|
|
|
// 所有 L0 的显示文本
|
|
|
|
|
|
for (const l0 of l0AtomsForFloor) {
|
|
|
|
|
|
totalTokens += estimateTokens(buildL0DisplayText(l0));
|
|
|
|
|
|
}
|
|
|
|
|
|
// 固定开销:楼层前缀、📌 标记、分号等
|
|
|
|
|
|
totalTokens += 10;
|
|
|
|
|
|
|
|
|
|
|
|
// L1 仅算一次
|
2026-02-10 00:18:51 +08:00
|
|
|
|
if (userL1) totalTokens += estimateTokens(formatL1Line(userL1, true));
|
|
|
|
|
|
if (aiL1) totalTokens += estimateTokens(formatL1Line(aiL1, false));
|
|
|
|
|
|
|
2026-02-10 12:43:43 +08:00
|
|
|
|
return { floor, l0Atoms: l0AtomsForFloor, userL1, aiL1, totalTokens };
|
2026-01-29 17:02:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-09 10:09:16 +08:00
|
|
|
|
/**
|
2026-02-10 12:43:43 +08:00
|
|
|
|
* 格式化一个证据组为文本行数组
|
|
|
|
|
|
*
|
|
|
|
|
|
* 短行模式(拼接后 ≤ 120 字):
|
2026-02-12 15:36:07 +08:00
|
|
|
|
* › #500 [📌] 小林整理会议记录;小周补充行动项;两人确认下周安排
|
|
|
|
|
|
* ┌ #499 [小周] ...
|
2026-02-10 12:43:43 +08:00
|
|
|
|
* › #500 [角色] ...
|
|
|
|
|
|
*
|
|
|
|
|
|
* 长行模式(拼接后 > 120 字):
|
2026-02-12 15:36:07 +08:00
|
|
|
|
* › #500 [📌] 小林在图书馆归档旧资料
|
|
|
|
|
|
* │ 小周核对目录并修正编号
|
|
|
|
|
|
* │ 两人讨论借阅规则并更新说明
|
|
|
|
|
|
* ┌ #499 [小周] ...
|
2026-02-10 12:43:43 +08:00
|
|
|
|
* › #500 [角色] ...
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param {EvidenceGroup} group - 证据组
|
2026-02-10 00:18:51 +08:00
|
|
|
|
* @returns {string[]} 文本行数组
|
2026-02-09 10:09:16 +08:00
|
|
|
|
*/
|
2026-02-10 12:43:43 +08:00
|
|
|
|
function formatEvidenceGroup(group) {
|
|
|
|
|
|
const displayTexts = group.l0Atoms.map(l0 => buildL0DisplayText(l0));
|
|
|
|
|
|
|
2026-02-10 00:18:51 +08:00
|
|
|
|
const lines = [];
|
2026-02-10 12:43:43 +08:00
|
|
|
|
|
|
|
|
|
|
// L0 部分
|
|
|
|
|
|
const joined = displayTexts.join(';');
|
|
|
|
|
|
|
|
|
|
|
|
if (joined.length <= L0_JOINED_MAX_LENGTH) {
|
|
|
|
|
|
// 短行:分号拼接为一行
|
|
|
|
|
|
lines.push(` › #${group.floor + 1} [📌] ${joined}`);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 长行:每个 L0 独占一行,首行带楼层号
|
|
|
|
|
|
lines.push(` › #${group.floor + 1} [📌] ${displayTexts[0]}`);
|
|
|
|
|
|
for (let i = 1; i < displayTexts.length; i++) {
|
|
|
|
|
|
lines.push(` │ ${displayTexts[i]}`);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// L1 证据(仅一次)
|
|
|
|
|
|
if (group.userL1) {
|
|
|
|
|
|
lines.push(formatL1Line(group.userL1, true));
|
2026-02-10 00:18:51 +08:00
|
|
|
|
}
|
2026-02-10 12:43:43 +08:00
|
|
|
|
if (group.aiL1) {
|
|
|
|
|
|
lines.push(formatL1Line(group.aiL1, false));
|
2026-02-10 00:18:51 +08:00
|
|
|
|
}
|
2026-02-10 12:43:43 +08:00
|
|
|
|
|
2026-02-10 00:18:51 +08:00
|
|
|
|
return lines;
|
2026-01-29 17:02:51 +08:00
|
|
|
|
}
|
2026-01-26 01:16:35 +08:00
|
|
|
|
|
2026-02-08 18:12:55 +08:00
|
|
|
|
// ─────────────────────────────────────────────────────────────────────────────
|
2026-02-10 12:43:43 +08:00
|
|
|
|
// 事件证据收集(per-floor 分组)
|
2026-02-08 18:12:55 +08:00
|
|
|
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
|
|
|
|
|
2026-02-09 10:09:16 +08:00
|
|
|
|
/**
|
2026-02-10 12:43:43 +08:00
|
|
|
|
* 为事件收集范围内的 EvidenceGroup
|
|
|
|
|
|
*
|
|
|
|
|
|
* 同楼层多个 L0 归入同一组,共享一对 L1。
|
|
|
|
|
|
*
|
2026-02-10 00:18:51 +08:00
|
|
|
|
* @param {object} eventObj - 事件对象
|
|
|
|
|
|
* @param {object[]} l0Selected - 所有选中的 L0
|
|
|
|
|
|
* @param {Map<number, object>} l1ByFloor - 楼层→L1配对映射
|
|
|
|
|
|
* @param {Set<string>} usedL0Ids - 已消费的 L0 ID 集合(会被修改)
|
2026-02-10 12:43:43 +08:00
|
|
|
|
* @returns {EvidenceGroup[]} 该事件的证据组列表(按楼层排序)
|
2026-02-09 10:09:16 +08:00
|
|
|
|
*/
|
2026-02-10 12:43:43 +08:00
|
|
|
|
function collectEvidenceGroupsForEvent(eventObj, l0Selected, l1ByFloor, usedL0Ids) {
|
2026-02-10 00:18:51 +08:00
|
|
|
|
const range = parseFloorRange(eventObj?.summary);
|
|
|
|
|
|
if (!range) return [];
|
2026-02-08 18:12:55 +08:00
|
|
|
|
|
2026-02-10 12:43:43 +08:00
|
|
|
|
// 收集范围内未消费的 L0,按楼层分组
|
|
|
|
|
|
const floorMap = new Map();
|
2026-02-08 18:12:55 +08:00
|
|
|
|
|
2026-02-10 00:18:51 +08:00
|
|
|
|
for (const l0 of l0Selected) {
|
|
|
|
|
|
if (usedL0Ids.has(l0.id)) continue;
|
|
|
|
|
|
if (l0.floor < range.start || l0.floor > range.end) continue;
|
|
|
|
|
|
|
2026-02-10 12:43:43 +08:00
|
|
|
|
if (!floorMap.has(l0.floor)) {
|
|
|
|
|
|
floorMap.set(l0.floor, []);
|
|
|
|
|
|
}
|
|
|
|
|
|
floorMap.get(l0.floor).push(l0);
|
2026-02-10 00:18:51 +08:00
|
|
|
|
usedL0Ids.add(l0.id);
|
2026-02-08 18:12:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-10 12:43:43 +08:00
|
|
|
|
// 构建 groups
|
|
|
|
|
|
const groups = [];
|
|
|
|
|
|
for (const [floor, l0s] of floorMap) {
|
|
|
|
|
|
groups.push(buildEvidenceGroup(floor, l0s, l1ByFloor));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-10 00:18:51 +08:00
|
|
|
|
// 按楼层排序
|
2026-02-10 12:43:43 +08:00
|
|
|
|
groups.sort((a, b) => a.floor - b.floor);
|
2026-02-08 18:12:55 +08:00
|
|
|
|
|
2026-02-10 12:43:43 +08:00
|
|
|
|
return groups;
|
2026-02-10 00:18:51 +08:00
|
|
|
|
}
|
2026-02-08 18:12:55 +08:00
|
|
|
|
|
2026-02-10 00:18:51 +08:00
|
|
|
|
// ─────────────────────────────────────────────────────────────────────────────
|
2026-02-10 12:43:43 +08:00
|
|
|
|
// 事件格式化(L2 → EvidenceGroup 层级)
|
2026-02-10 00:18:51 +08:00
|
|
|
|
// ─────────────────────────────────────────────────────────────────────────────
|
2026-02-08 18:12:55 +08:00
|
|
|
|
|
2026-02-10 00:18:51 +08:00
|
|
|
|
/**
|
2026-02-10 12:43:43 +08:00
|
|
|
|
* 格式化事件(含 EvidenceGroup 证据)
|
2026-02-10 00:18:51 +08:00
|
|
|
|
* @param {object} eventItem - 事件召回项
|
|
|
|
|
|
* @param {number} idx - 编号
|
2026-02-10 12:43:43 +08:00
|
|
|
|
* @param {EvidenceGroup[]} evidenceGroups - 该事件的证据组
|
2026-02-10 00:18:51 +08:00
|
|
|
|
* @param {Map<string, object>} causalById - 因果事件索引
|
|
|
|
|
|
* @returns {string} 格式化后的文本
|
|
|
|
|
|
*/
|
2026-02-10 12:43:43 +08:00
|
|
|
|
function formatEventWithEvidence(eventItem, idx, evidenceGroups, causalById) {
|
2026-02-13 13:39:03 +08:00
|
|
|
|
const ev = eventItem?.event || eventItem || {};
|
2026-02-10 00:18:51 +08:00
|
|
|
|
const time = ev.timeLabel || "";
|
|
|
|
|
|
const title = String(ev.title || "").trim();
|
|
|
|
|
|
const people = (ev.participants || []).join(" / ").trim();
|
|
|
|
|
|
const summary = cleanSummary(ev.summary);
|
2026-02-08 18:12:55 +08:00
|
|
|
|
|
2026-02-10 00:18:51 +08:00
|
|
|
|
const displayTitle = title || people || ev.id || "事件";
|
|
|
|
|
|
const header = time ? `${idx}.【${time}】${displayTitle}` : `${idx}. ${displayTitle}`;
|
|
|
|
|
|
|
|
|
|
|
|
const lines = [header];
|
|
|
|
|
|
if (people && displayTitle !== people) lines.push(` ${people}`);
|
|
|
|
|
|
lines.push(` ${summary}`);
|
|
|
|
|
|
|
|
|
|
|
|
// 因果链
|
|
|
|
|
|
for (const cid of ev.causedBy || []) {
|
|
|
|
|
|
const c = causalById?.get(cid);
|
|
|
|
|
|
if (c) lines.push(formatCausalEventLine(c));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-10 12:43:43 +08:00
|
|
|
|
// EvidenceGroup 证据
|
|
|
|
|
|
for (const group of evidenceGroups) {
|
|
|
|
|
|
lines.push(...formatEvidenceGroup(group));
|
2026-02-08 18:12:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-10 00:18:51 +08:00
|
|
|
|
return lines.join("\n");
|
2026-02-08 18:12:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-26 23:50:48 +08:00
|
|
|
|
// ─────────────────────────────────────────────────────────────────────────────
|
2026-02-08 12:22:45 +08:00
|
|
|
|
// 非向量模式
|
2026-01-26 23:50:48 +08:00
|
|
|
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
|
|
|
|
|
2026-02-09 10:09:16 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 构建非向量模式注入文本
|
|
|
|
|
|
* @param {object} store - 存储对象
|
|
|
|
|
|
* @returns {string} 注入文本
|
|
|
|
|
|
*/
|
2026-01-27 16:04:57 +08:00
|
|
|
|
function buildNonVectorPrompt(store) {
|
2026-01-26 23:50:48 +08:00
|
|
|
|
const data = store.json || {};
|
|
|
|
|
|
const sections = [];
|
|
|
|
|
|
|
2026-02-14 21:30:57 +08:00
|
|
|
|
// [Constraints] L3 Facts (structured: people/world)
|
|
|
|
|
|
const allFacts = getFacts().filter(f => !f.retracted);
|
|
|
|
|
|
const nonVectorPeopleDict = buildConstraintPeopleDict(
|
|
|
|
|
|
{ events: data.events || [] },
|
|
|
|
|
|
[]
|
|
|
|
|
|
);
|
|
|
|
|
|
const nonVectorFocus = nonVectorPeopleDict.size > 0
|
|
|
|
|
|
? [...nonVectorPeopleDict.values()]
|
|
|
|
|
|
: [...getKnownCharacters(store)];
|
|
|
|
|
|
const nonVectorKnownCharacters = getKnownCharacters(store);
|
|
|
|
|
|
const filteredConstraints = filterConstraintsByRelevance(
|
|
|
|
|
|
allFacts,
|
|
|
|
|
|
nonVectorFocus,
|
|
|
|
|
|
nonVectorKnownCharacters
|
|
|
|
|
|
);
|
|
|
|
|
|
const groupedConstraints = groupConstraintsForDisplay(filteredConstraints, nonVectorPeopleDict);
|
2026-02-15 18:58:51 +08:00
|
|
|
|
const constraintLines = formatConstraintsStructured(groupedConstraints, 'asc');
|
2026-02-08 12:22:45 +08:00
|
|
|
|
|
2026-02-09 10:09:16 +08:00
|
|
|
|
if (constraintLines.length) {
|
|
|
|
|
|
sections.push(`[定了的事] 已确立的事实\n${constraintLines.join("\n")}`);
|
2026-01-26 23:50:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-09 10:09:16 +08:00
|
|
|
|
// [Events] L2 Events
|
2026-01-26 23:50:48 +08:00
|
|
|
|
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")}`);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-09 10:09:16 +08:00
|
|
|
|
// [Arcs]
|
2026-01-26 23:50:48 +08:00
|
|
|
|
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-02-09 10:09:16 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 构建非向量模式注入文本(公开接口)
|
|
|
|
|
|
* @returns {string} 注入文本
|
|
|
|
|
|
*/
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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-26 23:50:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-05 00:22:02 +08:00
|
|
|
|
// ─────────────────────────────────────────────────────────────────────────────
|
2026-02-08 12:22:45 +08:00
|
|
|
|
// 向量模式:预算装配
|
2026-02-05 00:22:02 +08:00
|
|
|
|
// ─────────────────────────────────────────────────────────────────────────────
|
2026-01-26 23:50:48 +08:00
|
|
|
|
|
2026-02-09 10:09:16 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 构建向量模式注入文本
|
|
|
|
|
|
* @param {object} store - 存储对象
|
|
|
|
|
|
* @param {object} recallResult - 召回结果
|
2026-02-10 00:18:51 +08:00
|
|
|
|
* @param {Map<string, object>} causalById - 因果事件索引
|
2026-02-15 18:58:51 +08:00
|
|
|
|
* @param {string[]} focusCharacters - 焦点人物
|
2026-02-09 10:09:16 +08:00
|
|
|
|
* @param {object} meta - 元数据
|
|
|
|
|
|
* @param {object} metrics - 指标对象
|
2026-02-10 00:18:51 +08:00
|
|
|
|
* @returns {Promise<{promptText: string, injectionStats: object, metrics: object}>}
|
2026-02-09 10:09:16 +08:00
|
|
|
|
*/
|
2026-02-15 18:58:51 +08:00
|
|
|
|
async function buildVectorPrompt(store, recallResult, causalById, focusCharacters, meta, metrics) {
|
2026-02-08 12:22:45 +08:00
|
|
|
|
const T_Start = performance.now();
|
|
|
|
|
|
|
2026-01-26 01:16:35 +08:00
|
|
|
|
const data = store.json || {};
|
2026-02-12 15:36:07 +08:00
|
|
|
|
const total = { used: 0, max: SHARED_POOL_MAX };
|
2026-01-27 22:51:44 +08:00
|
|
|
|
|
2026-02-10 00:18:51 +08:00
|
|
|
|
// 从 recallResult 解构
|
|
|
|
|
|
const l0Selected = recallResult?.l0Selected || [];
|
|
|
|
|
|
const l1ByFloor = recallResult?.l1ByFloor || new Map();
|
|
|
|
|
|
|
2026-02-09 10:09:16 +08:00
|
|
|
|
// 装配结果
|
2026-01-27 22:51:44 +08:00
|
|
|
|
const assembled = {
|
2026-02-09 10:09:16 +08:00
|
|
|
|
constraints: { lines: [], tokens: 0 },
|
|
|
|
|
|
directEvents: { lines: [], tokens: 0 },
|
|
|
|
|
|
relatedEvents: { lines: [], tokens: 0 },
|
|
|
|
|
|
distantEvidence: { lines: [], tokens: 0 },
|
|
|
|
|
|
recentEvidence: { lines: [], tokens: 0 },
|
2026-01-27 22:51:44 +08:00
|
|
|
|
arcs: { lines: [], tokens: 0 },
|
|
|
|
|
|
};
|
2026-01-26 23:50:48 +08:00
|
|
|
|
|
2026-02-09 10:09:16 +08:00
|
|
|
|
// 注入统计
|
2026-01-26 23:50:48 +08:00
|
|
|
|
const injectionStats = {
|
2026-02-12 15:36:07 +08:00
|
|
|
|
budget: { max: SHARED_POOL_MAX + UNSUMMARIZED_EVIDENCE_MAX, used: 0 },
|
2026-02-09 10:09:16 +08:00
|
|
|
|
constraint: { count: 0, tokens: 0, filtered: 0 },
|
|
|
|
|
|
arc: { count: 0, tokens: 0 },
|
|
|
|
|
|
event: { selected: 0, tokens: 0 },
|
2026-02-10 00:18:51 +08:00
|
|
|
|
evidence: { l0InEvents: 0, l1InEvents: 0, tokens: 0 },
|
|
|
|
|
|
distantEvidence: { units: 0, tokens: 0 },
|
|
|
|
|
|
recentEvidence: { units: 0, tokens: 0 },
|
2026-01-27 22:51:44 +08:00
|
|
|
|
};
|
2026-02-08 12:22:45 +08:00
|
|
|
|
|
2026-02-09 10:09:16 +08:00
|
|
|
|
const eventDetails = {
|
|
|
|
|
|
list: [],
|
2026-01-27 16:04:57 +08:00
|
|
|
|
directCount: 0,
|
2026-02-09 10:09:16 +08:00
|
|
|
|
relatedCount: 0,
|
2026-01-27 16:04:57 +08:00
|
|
|
|
};
|
2026-01-26 23:50:48 +08:00
|
|
|
|
|
2026-02-10 00:18:51 +08:00
|
|
|
|
// 已消费的 L0 ID 集合(事件区域消费后,evidence 区域不再重复)
|
|
|
|
|
|
const usedL0Ids = new Set();
|
|
|
|
|
|
|
2026-02-08 12:22:45 +08:00
|
|
|
|
// ═══════════════════════════════════════════════════════════════════════
|
2026-02-09 10:09:16 +08:00
|
|
|
|
// [Constraints] L3 Facts → 世界约束
|
2026-02-08 12:22:45 +08:00
|
|
|
|
// ═══════════════════════════════════════════════════════════════════════
|
|
|
|
|
|
|
2026-02-09 10:09:16 +08:00
|
|
|
|
const T_Constraint_Start = performance.now();
|
2026-02-08 12:22:45 +08:00
|
|
|
|
|
|
|
|
|
|
const allFacts = getFacts();
|
|
|
|
|
|
const knownCharacters = getKnownCharacters(store);
|
2026-02-15 18:58:51 +08:00
|
|
|
|
const filteredConstraints = filterConstraintsByRelevance(allFacts, focusCharacters, knownCharacters);
|
|
|
|
|
|
const constraintPeopleDict = buildConstraintPeopleDict(recallResult, focusCharacters);
|
2026-02-14 21:30:57 +08:00
|
|
|
|
const groupedConstraints = groupConstraintsForDisplay(filteredConstraints, constraintPeopleDict);
|
2026-02-08 12:22:45 +08:00
|
|
|
|
|
|
|
|
|
|
if (metrics) {
|
2026-02-09 10:09:16 +08:00
|
|
|
|
metrics.constraint.total = allFacts.length;
|
2026-02-14 21:30:57 +08:00
|
|
|
|
metrics.constraint.filtered = allFacts.length - filteredConstraints.length;
|
2026-02-08 12:22:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-15 18:58:51 +08:00
|
|
|
|
const constraintBudget = { used: 0, max: Math.min(CONSTRAINT_MAX, total.max - total.used) };
|
|
|
|
|
|
const groupedSelectedConstraints = selectConstraintsByBudgetDesc(groupedConstraints, constraintBudget);
|
2026-02-15 19:07:11 +08:00
|
|
|
|
const injectedConstraintFacts = (() => {
|
|
|
|
|
|
let count = groupedSelectedConstraints.world.length;
|
|
|
|
|
|
for (const facts of groupedSelectedConstraints.people.values()) {
|
|
|
|
|
|
count += facts.length;
|
|
|
|
|
|
}
|
|
|
|
|
|
return count;
|
|
|
|
|
|
})();
|
2026-02-15 18:58:51 +08:00
|
|
|
|
const constraintLines = formatConstraintsStructured(groupedSelectedConstraints, 'asc');
|
|
|
|
|
|
|
2026-02-09 10:09:16 +08:00
|
|
|
|
if (constraintLines.length) {
|
2026-02-15 18:58:51 +08:00
|
|
|
|
assembled.constraints.lines.push(...constraintLines);
|
2026-02-09 10:09:16 +08:00
|
|
|
|
assembled.constraints.tokens = constraintBudget.used;
|
|
|
|
|
|
total.used += constraintBudget.used;
|
|
|
|
|
|
injectionStats.constraint.count = assembled.constraints.lines.length;
|
|
|
|
|
|
injectionStats.constraint.tokens = constraintBudget.used;
|
2026-02-14 21:30:57 +08:00
|
|
|
|
injectionStats.constraint.filtered = allFacts.length - filteredConstraints.length;
|
2026-02-08 12:22:45 +08:00
|
|
|
|
|
|
|
|
|
|
if (metrics) {
|
2026-02-15 19:07:11 +08:00
|
|
|
|
metrics.constraint.injected = injectedConstraintFacts;
|
2026-02-09 10:09:16 +08:00
|
|
|
|
metrics.constraint.tokens = constraintBudget.used;
|
|
|
|
|
|
metrics.constraint.samples = assembled.constraints.lines.slice(0, 3).map(line =>
|
2026-02-08 12:22:45 +08:00
|
|
|
|
line.length > 60 ? line.slice(0, 60) + '...' : line
|
|
|
|
|
|
);
|
2026-02-09 10:09:16 +08:00
|
|
|
|
metrics.timing.constraintFilter = Math.round(performance.now() - T_Constraint_Start);
|
2026-02-08 12:22:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
} else if (metrics) {
|
2026-02-09 10:09:16 +08:00
|
|
|
|
metrics.timing.constraintFilter = Math.round(performance.now() - T_Constraint_Start);
|
2026-01-27 22:51:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-08 12:22:45 +08:00
|
|
|
|
// ═══════════════════════════════════════════════════════════════════════
|
2026-02-09 10:09:16 +08:00
|
|
|
|
// [Arcs] 人物弧光
|
2026-02-08 12:22:45 +08:00
|
|
|
|
// ═══════════════════════════════════════════════════════════════════════
|
2026-01-27 22:51:44 +08:00
|
|
|
|
|
|
|
|
|
|
if (data.arcs?.length && total.used < total.max) {
|
|
|
|
|
|
const { name1 } = getContext();
|
|
|
|
|
|
const userName = String(name1 || "").trim();
|
|
|
|
|
|
|
|
|
|
|
|
const relevant = new Set(
|
2026-02-15 18:58:51 +08:00
|
|
|
|
[userName, ...(focusCharacters || [])]
|
2026-01-27 22:51:44 +08:00
|
|
|
|
.map(s => String(s || "").trim())
|
|
|
|
|
|
.filter(Boolean)
|
|
|
|
|
|
);
|
|
|
|
|
|
|
2026-02-09 10:09:16 +08:00
|
|
|
|
const filteredArcs = (data.arcs || []).filter(a => {
|
2026-01-27 22:51:44 +08:00
|
|
|
|
const n = String(a?.name || "").trim();
|
|
|
|
|
|
return n && relevant.has(n);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-02-09 10:09:16 +08:00
|
|
|
|
if (filteredArcs.length) {
|
2026-01-27 22:51:44 +08:00
|
|
|
|
const arcBudget = { used: 0, max: Math.min(ARCS_MAX, total.max - total.used) };
|
2026-02-09 10:09:16 +08:00
|
|
|
|
for (const a of filteredArcs) {
|
2026-01-27 22:51:44 +08:00
|
|
|
|
const line = formatArcLine(a);
|
|
|
|
|
|
if (!pushWithBudget(assembled.arcs.lines, line, arcBudget)) break;
|
|
|
|
|
|
}
|
|
|
|
|
|
assembled.arcs.tokens = arcBudget.used;
|
|
|
|
|
|
total.used += arcBudget.used;
|
2026-02-09 10:09:16 +08:00
|
|
|
|
injectionStats.arc.count = assembled.arcs.lines.length;
|
|
|
|
|
|
injectionStats.arc.tokens = arcBudget.used;
|
2026-01-26 01:16:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-01-26 23:50:48 +08:00
|
|
|
|
|
2026-02-08 12:22:45 +08:00
|
|
|
|
// ═══════════════════════════════════════════════════════════════════════
|
2026-02-10 12:43:43 +08:00
|
|
|
|
// [Events] L2 Events → 直接命中 + 相似命中 + 因果链 + EvidenceGroup
|
2026-02-08 12:22:45 +08:00
|
|
|
|
// ═══════════════════════════════════════════════════════════════════════
|
2026-02-09 10:09:16 +08:00
|
|
|
|
const eventHits = (recallResult?.events || []).filter(e => e?.event?.summary);
|
2026-01-26 23:50:48 +08:00
|
|
|
|
|
2026-02-09 10:09:16 +08:00
|
|
|
|
const candidates = [...eventHits].sort((a, b) => (b.similarity || 0) - (a.similarity || 0));
|
2026-02-12 15:36:07 +08:00
|
|
|
|
const eventBudget = { used: 0, max: Math.min(EVENT_BUDGET_MAX, total.max - total.used) };
|
|
|
|
|
|
const relatedBudget = { used: 0, max: RELATED_EVENT_MAX };
|
2026-02-16 23:26:45 +08:00
|
|
|
|
// Once budget becomes tight, keep high-score L2 summaries and stop attaching evidence.
|
|
|
|
|
|
let allowEventEvidence = true;
|
2026-01-26 23:50:48 +08:00
|
|
|
|
|
2026-02-08 12:22:45 +08:00
|
|
|
|
const selectedDirect = [];
|
2026-02-09 10:09:16 +08:00
|
|
|
|
const selectedRelated = [];
|
2026-01-26 23:50:48 +08:00
|
|
|
|
|
2026-01-30 13:47:04 +08:00
|
|
|
|
for (let candidateRank = 0; candidateRank < candidates.length; candidateRank++) {
|
|
|
|
|
|
const e = candidates[candidateRank];
|
|
|
|
|
|
|
2026-01-27 16:04:57 +08:00
|
|
|
|
if (total.used >= total.max) break;
|
2026-02-12 15:36:07 +08:00
|
|
|
|
if (eventBudget.used >= eventBudget.max) break;
|
2026-01-26 23:50:48 +08:00
|
|
|
|
|
2026-01-27 16:04:57 +08:00
|
|
|
|
const isDirect = e._recallType === "DIRECT";
|
2026-02-12 15:36:07 +08:00
|
|
|
|
if (!isDirect && relatedBudget.used >= relatedBudget.max) continue;
|
2026-01-26 23:50:48 +08:00
|
|
|
|
|
2026-02-13 18:54:19 +08:00
|
|
|
|
// 硬规则:RELATED 事件不挂证据(不挂 L0/L1,只保留事件摘要)
|
|
|
|
|
|
// DIRECT 才允许收集事件内证据组。
|
2026-02-16 23:26:45 +08:00
|
|
|
|
const useEvidenceForThisEvent = isDirect && allowEventEvidence;
|
|
|
|
|
|
const evidenceGroups = useEvidenceForThisEvent
|
2026-02-13 18:54:19 +08:00
|
|
|
|
? collectEvidenceGroupsForEvent(e.event, l0Selected, l1ByFloor, usedL0Ids)
|
|
|
|
|
|
: [];
|
2026-01-26 23:50:48 +08:00
|
|
|
|
|
2026-02-10 00:18:51 +08:00
|
|
|
|
// 格式化事件(含证据)
|
2026-02-10 12:43:43 +08:00
|
|
|
|
const text = formatEventWithEvidence(e, 0, evidenceGroups, causalById);
|
2026-02-10 00:18:51 +08:00
|
|
|
|
const cost = estimateTokens(text);
|
2026-02-16 23:26:45 +08:00
|
|
|
|
const fitEventBudget = eventBudget.used + cost <= eventBudget.max;
|
|
|
|
|
|
const fitRelatedBudget = isDirect || (relatedBudget.used + cost <= relatedBudget.max);
|
2026-01-26 23:50:48 +08:00
|
|
|
|
|
2026-02-10 00:18:51 +08:00
|
|
|
|
// 预算检查:整个事件(含证据)作为原子单元
|
2026-02-16 23:26:45 +08:00
|
|
|
|
// 约束:总预算 + 事件预算 + related 子预算(若 applicable)
|
|
|
|
|
|
if (total.used + cost > total.max || !fitEventBudget || !fitRelatedBudget) {
|
2026-02-10 00:18:51 +08:00
|
|
|
|
// 尝试不带证据的版本
|
|
|
|
|
|
const textNoEvidence = formatEventWithEvidence(e, 0, [], causalById);
|
|
|
|
|
|
const costNoEvidence = estimateTokens(textNoEvidence);
|
2026-02-16 23:26:45 +08:00
|
|
|
|
const fitEventBudgetNoEvidence = eventBudget.used + costNoEvidence <= eventBudget.max;
|
|
|
|
|
|
const fitRelatedBudgetNoEvidence = isDirect || (relatedBudget.used + costNoEvidence <= relatedBudget.max);
|
2026-01-26 23:50:48 +08:00
|
|
|
|
|
2026-02-16 23:26:45 +08:00
|
|
|
|
if (total.used + costNoEvidence > total.max || !fitEventBudgetNoEvidence || !fitRelatedBudgetNoEvidence) {
|
2026-02-10 12:43:43 +08:00
|
|
|
|
// 归还 usedL0Ids
|
|
|
|
|
|
for (const group of evidenceGroups) {
|
|
|
|
|
|
for (const l0 of group.l0Atoms) {
|
|
|
|
|
|
usedL0Ids.delete(l0.id);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-02-16 23:26:45 +08:00
|
|
|
|
// Hard cap reached: no-evidence version also cannot fit total/event budget.
|
|
|
|
|
|
// Keep ranking semantics (higher-score events first): stop here.
|
|
|
|
|
|
if (total.used + costNoEvidence > total.max || !fitEventBudgetNoEvidence) {
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
// Related sub-budget overflow: skip this related event and continue.
|
2026-01-27 16:04:57 +08:00
|
|
|
|
continue;
|
2026-01-26 23:50:48 +08:00
|
|
|
|
}
|
2026-02-10 00:18:51 +08:00
|
|
|
|
|
|
|
|
|
|
// 放入不带证据的版本,归还已消费的 L0 ID
|
2026-02-10 12:43:43 +08:00
|
|
|
|
for (const group of evidenceGroups) {
|
|
|
|
|
|
for (const l0 of group.l0Atoms) {
|
|
|
|
|
|
usedL0Ids.delete(l0.id);
|
|
|
|
|
|
}
|
2026-02-10 00:18:51 +08:00
|
|
|
|
}
|
2026-02-16 23:26:45 +08:00
|
|
|
|
// Enter summary-only mode after first budget conflict on evidence.
|
|
|
|
|
|
if (useEvidenceForThisEvent && evidenceGroups.length > 0) {
|
|
|
|
|
|
allowEventEvidence = false;
|
|
|
|
|
|
}
|
2026-02-10 00:18:51 +08:00
|
|
|
|
|
|
|
|
|
|
if (isDirect) {
|
|
|
|
|
|
selectedDirect.push({
|
|
|
|
|
|
event: e.event, text: textNoEvidence, tokens: costNoEvidence,
|
2026-02-10 12:43:43 +08:00
|
|
|
|
evidenceGroups: [], candidateRank,
|
2026-02-10 00:18:51 +08:00
|
|
|
|
});
|
|
|
|
|
|
} else {
|
|
|
|
|
|
selectedRelated.push({
|
|
|
|
|
|
event: e.event, text: textNoEvidence, tokens: costNoEvidence,
|
2026-02-10 12:43:43 +08:00
|
|
|
|
evidenceGroups: [], candidateRank,
|
2026-02-10 00:18:51 +08:00
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
injectionStats.event.selected++;
|
|
|
|
|
|
injectionStats.event.tokens += costNoEvidence;
|
|
|
|
|
|
total.used += costNoEvidence;
|
2026-02-12 15:36:07 +08:00
|
|
|
|
eventBudget.used += costNoEvidence;
|
|
|
|
|
|
if (!isDirect) relatedBudget.used += costNoEvidence;
|
2026-02-10 00:18:51 +08:00
|
|
|
|
|
|
|
|
|
|
eventDetails.list.push({
|
|
|
|
|
|
title: e.event?.title || e.event?.id,
|
|
|
|
|
|
isDirect,
|
|
|
|
|
|
hasEvidence: false,
|
|
|
|
|
|
tokens: costNoEvidence,
|
|
|
|
|
|
similarity: e.similarity || 0,
|
|
|
|
|
|
l0Count: 0,
|
2026-02-10 12:43:43 +08:00
|
|
|
|
l1FloorCount: 0,
|
2026-02-10 00:18:51 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 预算充足,放入完整版本
|
2026-02-10 12:43:43 +08:00
|
|
|
|
let l0Count = 0;
|
|
|
|
|
|
let l1FloorCount = 0;
|
|
|
|
|
|
for (const group of evidenceGroups) {
|
|
|
|
|
|
l0Count += group.l0Atoms.length;
|
|
|
|
|
|
if (group.userL1 || group.aiL1) l1FloorCount++;
|
2026-01-26 01:16:35 +08:00
|
|
|
|
}
|
2026-01-26 23:50:48 +08:00
|
|
|
|
|
2026-01-27 16:04:57 +08:00
|
|
|
|
if (isDirect) {
|
2026-02-10 00:18:51 +08:00
|
|
|
|
selectedDirect.push({
|
|
|
|
|
|
event: e.event, text, tokens: cost,
|
2026-02-10 12:43:43 +08:00
|
|
|
|
evidenceGroups, candidateRank,
|
2026-02-10 00:18:51 +08:00
|
|
|
|
});
|
2026-01-27 16:04:57 +08:00
|
|
|
|
} else {
|
2026-02-10 00:18:51 +08:00
|
|
|
|
selectedRelated.push({
|
|
|
|
|
|
event: e.event, text, tokens: cost,
|
2026-02-10 12:43:43 +08:00
|
|
|
|
evidenceGroups, candidateRank,
|
2026-02-10 00:18:51 +08:00
|
|
|
|
});
|
2026-01-26 01:16:35 +08:00
|
|
|
|
}
|
2026-01-26 23:50:48 +08:00
|
|
|
|
|
2026-02-09 10:09:16 +08:00
|
|
|
|
injectionStats.event.selected++;
|
2026-02-10 00:18:51 +08:00
|
|
|
|
injectionStats.event.tokens += cost;
|
|
|
|
|
|
injectionStats.evidence.l0InEvents += l0Count;
|
2026-02-10 12:43:43 +08:00
|
|
|
|
injectionStats.evidence.l1InEvents += l1FloorCount;
|
2026-01-27 16:04:57 +08:00
|
|
|
|
total.used += cost;
|
2026-02-12 15:36:07 +08:00
|
|
|
|
eventBudget.used += cost;
|
|
|
|
|
|
if (!isDirect) relatedBudget.used += cost;
|
2026-01-27 16:04:57 +08:00
|
|
|
|
|
2026-02-09 10:09:16 +08:00
|
|
|
|
eventDetails.list.push({
|
2026-01-27 16:04:57 +08:00
|
|
|
|
title: e.event?.title || e.event?.id,
|
|
|
|
|
|
isDirect,
|
2026-02-10 00:18:51 +08:00
|
|
|
|
hasEvidence: l0Count > 0,
|
2026-01-27 16:04:57 +08:00
|
|
|
|
tokens: cost,
|
|
|
|
|
|
similarity: e.similarity || 0,
|
2026-02-10 00:18:51 +08:00
|
|
|
|
l0Count,
|
2026-02-10 12:43:43 +08:00
|
|
|
|
l1FloorCount,
|
2026-01-27 16:04:57 +08:00
|
|
|
|
});
|
2026-01-26 01:16:35 +08:00
|
|
|
|
}
|
2026-01-26 23:50:48 +08:00
|
|
|
|
|
2026-02-09 10:09:16 +08:00
|
|
|
|
// 排序
|
2026-01-29 17:02:51 +08:00
|
|
|
|
selectedDirect.sort((a, b) => getEventSortKey(a.event) - getEventSortKey(b.event));
|
2026-02-09 10:09:16 +08:00
|
|
|
|
selectedRelated.sort((a, b) => getEventSortKey(a.event) - getEventSortKey(b.event));
|
2026-01-29 17:02:51 +08:00
|
|
|
|
|
2026-02-12 15:36:07 +08:00
|
|
|
|
// ═══════════════════════════════════════════════════════════════════
|
|
|
|
|
|
// 邻近补挂:未被事件消费的 L0,距最近已选事件 ≤ 2 楼则补挂
|
|
|
|
|
|
// 每个 L0 只挂最近的一个事件,不扩展事件范围,不产生重叠
|
|
|
|
|
|
// ═══════════════════════════════════════════════════════════════════
|
|
|
|
|
|
|
2026-02-09 10:09:16 +08:00
|
|
|
|
// 重新编号 + 星标
|
|
|
|
|
|
const directEventTexts = selectedDirect.map((it, i) => {
|
2026-01-30 13:47:04 +08:00
|
|
|
|
const numbered = renumberEventText(it.text, i + 1);
|
|
|
|
|
|
return it.candidateRank < TOP_N_STAR ? `⭐${numbered}` : numbered;
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-02-09 10:09:16 +08:00
|
|
|
|
const relatedEventTexts = selectedRelated.map((it, i) => {
|
2026-01-30 13:47:04 +08:00
|
|
|
|
const numbered = renumberEventText(it.text, i + 1);
|
2026-02-15 01:02:34 +08:00
|
|
|
|
return numbered;
|
2026-01-30 13:47:04 +08:00
|
|
|
|
});
|
2026-01-29 17:02:51 +08:00
|
|
|
|
|
2026-02-09 10:09:16 +08:00
|
|
|
|
eventDetails.directCount = selectedDirect.length;
|
|
|
|
|
|
eventDetails.relatedCount = selectedRelated.length;
|
|
|
|
|
|
assembled.directEvents.lines = directEventTexts;
|
|
|
|
|
|
assembled.relatedEvents.lines = relatedEventTexts;
|
2026-01-27 16:04:57 +08:00
|
|
|
|
|
2026-02-08 12:22:45 +08:00
|
|
|
|
// ═══════════════════════════════════════════════════════════════════════
|
2026-02-10 12:43:43 +08:00
|
|
|
|
// [Evidence - Distant] 远期证据(已总结范围,未被事件消费的 L0)
|
2026-02-08 12:22:45 +08:00
|
|
|
|
// ═══════════════════════════════════════════════════════════════════════
|
|
|
|
|
|
|
2026-01-27 22:51:44 +08:00
|
|
|
|
const lastSummarized = store.lastSummarizedMesId ?? -1;
|
|
|
|
|
|
const lastChunkFloor = meta?.lastChunkFloor ?? -1;
|
|
|
|
|
|
const keepVisible = store.keepVisibleCount ?? 3;
|
2026-01-26 23:50:48 +08:00
|
|
|
|
|
2026-02-10 00:18:51 +08:00
|
|
|
|
// 收集未被事件消费的 L0,按 rerankScore 降序
|
2026-02-15 18:58:51 +08:00
|
|
|
|
const focusSetForEvidence = new Set((focusCharacters || []).map(normalize).filter(Boolean));
|
2026-02-12 00:05:19 +08:00
|
|
|
|
|
2026-02-10 00:18:51 +08:00
|
|
|
|
const remainingL0 = l0Selected
|
|
|
|
|
|
.filter(l0 => !usedL0Ids.has(l0.id))
|
2026-02-12 00:05:19 +08:00
|
|
|
|
.filter(l0 => shouldKeepEvidenceL0(l0, focusSetForEvidence))
|
2026-02-10 00:18:51 +08:00
|
|
|
|
.sort((a, b) => (b.rerankScore || 0) - (a.rerankScore || 0));
|
2026-02-05 00:22:02 +08:00
|
|
|
|
|
2026-02-10 00:18:51 +08:00
|
|
|
|
// 远期:floor <= lastSummarized
|
|
|
|
|
|
const distantL0 = remainingL0.filter(l0 => l0.floor <= lastSummarized);
|
2026-02-05 00:22:02 +08:00
|
|
|
|
|
2026-02-10 00:18:51 +08:00
|
|
|
|
if (distantL0.length && total.used < total.max) {
|
2026-02-12 15:36:07 +08:00
|
|
|
|
const distantBudget = { used: 0, max: Math.min(SUMMARIZED_EVIDENCE_MAX, total.max - total.used) };
|
2026-02-05 00:22:02 +08:00
|
|
|
|
|
2026-02-16 22:58:36 +08:00
|
|
|
|
// 先按分数挑组(高分优先),再按时间输出(楼层升序)
|
2026-02-10 12:43:43 +08:00
|
|
|
|
const distantFloorMap = groupL0ByFloor(distantL0);
|
2026-02-16 22:58:36 +08:00
|
|
|
|
const distantRanked = [];
|
2026-02-10 12:43:43 +08:00
|
|
|
|
for (const [floor, l0s] of distantFloorMap) {
|
|
|
|
|
|
const group = buildEvidenceGroup(floor, l0s, l1ByFloor);
|
2026-02-16 22:58:36 +08:00
|
|
|
|
const bestScore = Math.max(...l0s.map(l0 => (l0.rerankScore ?? l0.similarity ?? 0)));
|
|
|
|
|
|
distantRanked.push({ group, bestScore });
|
|
|
|
|
|
}
|
|
|
|
|
|
distantRanked.sort((a, b) => (b.bestScore - a.bestScore) || (a.group.floor - b.group.floor));
|
2026-02-10 00:18:51 +08:00
|
|
|
|
|
2026-02-16 22:58:36 +08:00
|
|
|
|
const acceptedDistantGroups = [];
|
|
|
|
|
|
for (const item of distantRanked) {
|
|
|
|
|
|
const group = item.group;
|
2026-02-10 12:43:43 +08:00
|
|
|
|
if (distantBudget.used + group.totalTokens > distantBudget.max) continue;
|
2026-02-16 22:58:36 +08:00
|
|
|
|
distantBudget.used += group.totalTokens;
|
|
|
|
|
|
acceptedDistantGroups.push(group);
|
|
|
|
|
|
for (const l0 of group.l0Atoms) usedL0Ids.add(l0.id);
|
|
|
|
|
|
injectionStats.distantEvidence.units++;
|
|
|
|
|
|
}
|
2026-02-10 00:18:51 +08:00
|
|
|
|
|
2026-02-16 22:58:36 +08:00
|
|
|
|
acceptedDistantGroups.sort((a, b) => a.floor - b.floor);
|
|
|
|
|
|
for (const group of acceptedDistantGroups) {
|
2026-02-10 12:43:43 +08:00
|
|
|
|
const groupLines = formatEvidenceGroup(group);
|
|
|
|
|
|
for (const line of groupLines) {
|
2026-02-10 00:18:51 +08:00
|
|
|
|
assembled.distantEvidence.lines.push(line);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-01-26 23:50:48 +08:00
|
|
|
|
|
2026-02-09 10:09:16 +08:00
|
|
|
|
assembled.distantEvidence.tokens = distantBudget.used;
|
|
|
|
|
|
total.used += distantBudget.used;
|
|
|
|
|
|
injectionStats.distantEvidence.tokens = distantBudget.used;
|
2026-01-26 01:16:35 +08:00
|
|
|
|
}
|
2026-01-26 23:50:48 +08:00
|
|
|
|
|
2026-02-08 12:22:45 +08:00
|
|
|
|
// ═══════════════════════════════════════════════════════════════════════
|
2026-02-10 00:18:51 +08:00
|
|
|
|
// [Evidence - Recent] 近期证据(未总结范围,独立预算)
|
2026-02-08 12:22:45 +08:00
|
|
|
|
// ═══════════════════════════════════════════════════════════════════════
|
2026-01-26 23:50:48 +08:00
|
|
|
|
|
2026-01-27 22:51:44 +08:00
|
|
|
|
const recentStart = lastSummarized + 1;
|
|
|
|
|
|
const recentEnd = lastChunkFloor - keepVisible;
|
2026-01-26 23:50:48 +08:00
|
|
|
|
|
2026-02-10 00:18:51 +08:00
|
|
|
|
if (recentEnd >= recentStart) {
|
|
|
|
|
|
const recentL0 = remainingL0
|
|
|
|
|
|
.filter(l0 => !usedL0Ids.has(l0.id))
|
|
|
|
|
|
.filter(l0 => l0.floor >= recentStart && l0.floor <= recentEnd);
|
2026-02-05 00:22:02 +08:00
|
|
|
|
|
2026-02-10 00:18:51 +08:00
|
|
|
|
if (recentL0.length) {
|
2026-02-12 15:36:07 +08:00
|
|
|
|
const recentBudget = { used: 0, max: UNSUMMARIZED_EVIDENCE_MAX };
|
2026-02-05 00:22:02 +08:00
|
|
|
|
|
2026-02-16 22:58:36 +08:00
|
|
|
|
// 先按分数挑组(高分优先),再按时间输出(楼层升序)
|
2026-02-10 12:43:43 +08:00
|
|
|
|
const recentFloorMap = groupL0ByFloor(recentL0);
|
2026-02-16 22:58:36 +08:00
|
|
|
|
const recentRanked = [];
|
2026-02-10 12:43:43 +08:00
|
|
|
|
for (const [floor, l0s] of recentFloorMap) {
|
|
|
|
|
|
const group = buildEvidenceGroup(floor, l0s, l1ByFloor);
|
2026-02-16 22:58:36 +08:00
|
|
|
|
const bestScore = Math.max(...l0s.map(l0 => (l0.rerankScore ?? l0.similarity ?? 0)));
|
|
|
|
|
|
recentRanked.push({ group, bestScore });
|
|
|
|
|
|
}
|
|
|
|
|
|
recentRanked.sort((a, b) => (b.bestScore - a.bestScore) || (a.group.floor - b.group.floor));
|
2026-02-05 00:22:02 +08:00
|
|
|
|
|
2026-02-16 22:58:36 +08:00
|
|
|
|
const acceptedRecentGroups = [];
|
|
|
|
|
|
for (const item of recentRanked) {
|
|
|
|
|
|
const group = item.group;
|
2026-02-10 12:43:43 +08:00
|
|
|
|
if (recentBudget.used + group.totalTokens > recentBudget.max) continue;
|
2026-02-16 22:58:36 +08:00
|
|
|
|
recentBudget.used += group.totalTokens;
|
|
|
|
|
|
acceptedRecentGroups.push(group);
|
|
|
|
|
|
for (const l0 of group.l0Atoms) usedL0Ids.add(l0.id);
|
|
|
|
|
|
injectionStats.recentEvidence.units++;
|
|
|
|
|
|
}
|
2026-02-05 00:22:02 +08:00
|
|
|
|
|
2026-02-16 22:58:36 +08:00
|
|
|
|
acceptedRecentGroups.sort((a, b) => a.floor - b.floor);
|
|
|
|
|
|
for (const group of acceptedRecentGroups) {
|
2026-02-10 12:43:43 +08:00
|
|
|
|
const groupLines = formatEvidenceGroup(group);
|
|
|
|
|
|
for (const line of groupLines) {
|
2026-02-10 00:18:51 +08:00
|
|
|
|
assembled.recentEvidence.lines.push(line);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-02-05 00:22:02 +08:00
|
|
|
|
|
2026-02-10 00:18:51 +08:00
|
|
|
|
assembled.recentEvidence.tokens = recentBudget.used;
|
|
|
|
|
|
injectionStats.recentEvidence.tokens = recentBudget.used;
|
2026-01-26 01:16:35 +08:00
|
|
|
|
}
|
2026-01-27 22:51:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-08 12:22:45 +08:00
|
|
|
|
// ═══════════════════════════════════════════════════════════════════════
|
2026-02-05 00:22:02 +08:00
|
|
|
|
// 按注入顺序拼接 sections
|
2026-02-08 12:22:45 +08:00
|
|
|
|
// ═══════════════════════════════════════════════════════════════════════
|
|
|
|
|
|
|
2026-02-09 10:09:16 +08:00
|
|
|
|
const T_Format_Start = performance.now();
|
2026-02-08 12:22:45 +08:00
|
|
|
|
|
2026-02-05 00:22:02 +08:00
|
|
|
|
const sections = [];
|
2026-02-08 12:22:45 +08:00
|
|
|
|
|
2026-02-09 10:09:16 +08:00
|
|
|
|
if (assembled.constraints.lines.length) {
|
|
|
|
|
|
sections.push(`[定了的事] 已确立的事实\n${assembled.constraints.lines.join("\n")}`);
|
2026-02-05 00:22:02 +08:00
|
|
|
|
}
|
2026-02-09 10:09:16 +08:00
|
|
|
|
if (assembled.directEvents.lines.length) {
|
|
|
|
|
|
sections.push(`[印象深的事] 记得很清楚\n\n${assembled.directEvents.lines.join("\n\n")}`);
|
2026-02-05 00:22:02 +08:00
|
|
|
|
}
|
2026-02-09 10:09:16 +08:00
|
|
|
|
if (assembled.relatedEvents.lines.length) {
|
2026-02-13 18:30:11 +08:00
|
|
|
|
sections.push(`[其他人的事] 别人经历的类似事\n\n${assembled.relatedEvents.lines.join("\n\n")}`);
|
2026-02-05 00:22:02 +08:00
|
|
|
|
}
|
2026-02-09 10:09:16 +08:00
|
|
|
|
if (assembled.distantEvidence.lines.length) {
|
2026-02-12 15:36:07 +08:00
|
|
|
|
sections.push(`[零散记忆] 没归入事件的片段\n${assembled.distantEvidence.lines.join("\n")}`);
|
2026-02-05 00:22:02 +08:00
|
|
|
|
}
|
2026-02-09 10:09:16 +08:00
|
|
|
|
if (assembled.recentEvidence.lines.length) {
|
2026-02-12 15:36:07 +08:00
|
|
|
|
sections.push(`[新鲜记忆] 还没总结的部分\n${assembled.recentEvidence.lines.join("\n")}`);
|
2026-02-05 00:22:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
if (assembled.arcs.lines.length) {
|
2026-02-05 13:46:28 +08:00
|
|
|
|
sections.push(`[这些人] 他们的弧光\n${assembled.arcs.lines.join("\n")}`);
|
2026-02-05 00:22:02 +08:00
|
|
|
|
}
|
2026-01-27 16:04:57 +08:00
|
|
|
|
|
2026-02-05 00:22:02 +08:00
|
|
|
|
if (!sections.length) {
|
2026-02-08 12:22:45 +08:00
|
|
|
|
if (metrics) {
|
2026-02-09 10:09:16 +08:00
|
|
|
|
metrics.timing.evidenceAssembly = Math.round(performance.now() - T_Start - (metrics.timing.constraintFilter || 0));
|
|
|
|
|
|
metrics.timing.formatting = 0;
|
2026-02-08 12:22:45 +08:00
|
|
|
|
}
|
2026-02-10 00:18:51 +08:00
|
|
|
|
return { promptText: "", injectionStats, metrics };
|
2026-01-26 01:16:35 +08:00
|
|
|
|
}
|
2026-01-26 23:50:48 +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-02-08 12:22:45 +08:00
|
|
|
|
if (metrics) {
|
2026-02-09 10:09:16 +08:00
|
|
|
|
metrics.formatting.sectionsIncluded = [];
|
|
|
|
|
|
if (assembled.constraints.lines.length) metrics.formatting.sectionsIncluded.push('constraints');
|
|
|
|
|
|
if (assembled.directEvents.lines.length) metrics.formatting.sectionsIncluded.push('direct_events');
|
|
|
|
|
|
if (assembled.relatedEvents.lines.length) metrics.formatting.sectionsIncluded.push('related_events');
|
|
|
|
|
|
if (assembled.distantEvidence.lines.length) metrics.formatting.sectionsIncluded.push('distant_evidence');
|
|
|
|
|
|
if (assembled.recentEvidence.lines.length) metrics.formatting.sectionsIncluded.push('recent_evidence');
|
|
|
|
|
|
if (assembled.arcs.lines.length) metrics.formatting.sectionsIncluded.push('arcs');
|
|
|
|
|
|
|
|
|
|
|
|
metrics.formatting.time = Math.round(performance.now() - T_Format_Start);
|
|
|
|
|
|
metrics.timing.formatting = metrics.formatting.time;
|
|
|
|
|
|
|
2026-02-12 15:36:07 +08:00
|
|
|
|
const effectiveTotal = total.used + (assembled.recentEvidence.tokens || 0);
|
|
|
|
|
|
const effectiveLimit = SHARED_POOL_MAX + UNSUMMARIZED_EVIDENCE_MAX;
|
|
|
|
|
|
metrics.budget.total = effectiveTotal;
|
|
|
|
|
|
metrics.budget.limit = effectiveLimit;
|
|
|
|
|
|
metrics.budget.utilization = Math.round(effectiveTotal / effectiveLimit * 100);
|
2026-02-08 12:22:45 +08:00
|
|
|
|
metrics.budget.breakdown = {
|
2026-02-09 10:09:16 +08:00
|
|
|
|
constraints: assembled.constraints.tokens,
|
2026-02-10 00:18:51 +08:00
|
|
|
|
events: injectionStats.event.tokens,
|
2026-02-09 10:09:16 +08:00
|
|
|
|
distantEvidence: injectionStats.distantEvidence.tokens,
|
2026-02-10 00:18:51 +08:00
|
|
|
|
recentEvidence: injectionStats.recentEvidence.tokens,
|
2026-02-08 12:22:45 +08:00
|
|
|
|
arcs: assembled.arcs.tokens,
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-02-10 00:18:51 +08:00
|
|
|
|
metrics.evidence.tokens = injectionStats.distantEvidence.tokens + injectionStats.recentEvidence.tokens;
|
|
|
|
|
|
metrics.evidence.assemblyTime = Math.round(
|
|
|
|
|
|
performance.now() - T_Start - (metrics.timing.constraintFilter || 0) - metrics.formatting.time
|
|
|
|
|
|
);
|
2026-02-09 10:09:16 +08:00
|
|
|
|
metrics.timing.evidenceAssembly = metrics.evidence.assemblyTime;
|
2026-02-08 12:22:45 +08:00
|
|
|
|
|
2026-02-15 19:07:11 +08:00
|
|
|
|
const relevantFacts = Math.max(0, allFacts.length - (metrics.constraint.filtered || 0));
|
|
|
|
|
|
metrics.quality.constraintCoverage = relevantFacts > 0
|
|
|
|
|
|
? Math.round((metrics.constraint.injected || 0) / relevantFacts * 100)
|
2026-02-08 12:22:45 +08:00
|
|
|
|
: 100;
|
2026-02-09 10:09:16 +08:00
|
|
|
|
metrics.quality.eventPrecisionProxy = metrics.event?.similarityDistribution?.mean || 0;
|
2026-02-08 12:22:45 +08:00
|
|
|
|
|
2026-02-10 12:43:43 +08:00
|
|
|
|
// l1AttachRate:有 L1 挂载的唯一楼层占所有 L0 覆盖楼层的比例
|
|
|
|
|
|
const l0Floors = new Set(l0Selected.map(l0 => l0.floor));
|
|
|
|
|
|
const l0FloorsWithL1 = new Set();
|
|
|
|
|
|
for (const floor of l0Floors) {
|
|
|
|
|
|
const pair = l1ByFloor.get(floor);
|
|
|
|
|
|
if (pair?.aiTop1 || pair?.userTop1) {
|
|
|
|
|
|
l0FloorsWithL1.add(floor);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
metrics.quality.l1AttachRate = l0Floors.size > 0
|
|
|
|
|
|
? Math.round(l0FloorsWithL1.size / l0Floors.size * 100)
|
2026-02-09 15:26:43 +08:00
|
|
|
|
: 0;
|
2026-02-10 00:18:51 +08:00
|
|
|
|
|
2026-02-08 12:22:45 +08:00
|
|
|
|
metrics.quality.potentialIssues = detectIssues(metrics);
|
|
|
|
|
|
}
|
2026-01-26 23:50:48 +08:00
|
|
|
|
|
2026-02-10 00:18:51 +08:00
|
|
|
|
return { promptText, injectionStats, metrics };
|
2026-01-26 01:16:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-27 16:04:57 +08:00
|
|
|
|
// ─────────────────────────────────────────────────────────────────────────────
|
2026-02-08 12:22:45 +08:00
|
|
|
|
// 向量模式:召回 + 注入
|
2026-01-27 16:04:57 +08:00
|
|
|
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
|
|
|
|
|
2026-02-09 10:09:16 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 构建向量模式注入文本(公开接口)
|
|
|
|
|
|
* @param {boolean} excludeLastAi - 是否排除最后的 AI 消息
|
|
|
|
|
|
* @param {object} hooks - 钩子函数
|
|
|
|
|
|
* @returns {Promise<{text: string, logText: string}>}
|
|
|
|
|
|
*/
|
2026-01-29 01:17:37 +08:00
|
|
|
|
export async function buildVectorPromptText(excludeLastAi = false, hooks = {}) {
|
|
|
|
|
|
const { postToFrame = null, echo = null, pendingUserMessage = null } = hooks;
|
2026-02-08 12:22:45 +08:00
|
|
|
|
|
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();
|
|
|
|
|
|
const meta = chatId ? await getMeta(chatId) : null;
|
|
|
|
|
|
|
2026-01-27 16:04:57 +08:00
|
|
|
|
let recallResult = null;
|
2026-01-26 23:50:48 +08:00
|
|
|
|
let causalById = new Map();
|
2026-01-26 01:16:35 +08:00
|
|
|
|
|
2026-01-27 16:04:57 +08:00
|
|
|
|
try {
|
2026-02-09 15:26:43 +08:00
|
|
|
|
recallResult = await recallMemory(allEvents, vectorCfg, {
|
2026-01-29 01:17:37 +08:00
|
|
|
|
excludeLastAi,
|
|
|
|
|
|
pendingUserMessage,
|
|
|
|
|
|
});
|
2026-01-27 16:04:57 +08:00
|
|
|
|
|
|
|
|
|
|
recallResult = {
|
|
|
|
|
|
...recallResult,
|
|
|
|
|
|
events: recallResult?.events || [],
|
2026-02-10 00:18:51 +08:00
|
|
|
|
l0Selected: recallResult?.l0Selected || [],
|
|
|
|
|
|
l1ByFloor: recallResult?.l1ByFloor || new Map(),
|
2026-02-09 10:09:16 +08:00
|
|
|
|
causalChain: recallResult?.causalChain || [],
|
2026-02-15 18:58:51 +08:00
|
|
|
|
focusTerms: recallResult?.focusTerms || recallResult?.focusEntities || [],
|
|
|
|
|
|
focusEntities: recallResult?.focusTerms || recallResult?.focusEntities || [], // compat alias
|
|
|
|
|
|
focusCharacters: recallResult?.focusCharacters || [],
|
2026-02-08 12:22:45 +08:00
|
|
|
|
metrics: recallResult?.metrics || null,
|
2026-01-27 16:04:57 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
2026-02-10 00:18:51 +08:00
|
|
|
|
// 构建因果事件索引
|
2026-01-27 16:04:57 +08:00
|
|
|
|
causalById = new Map(
|
2026-02-10 00:18:51 +08:00
|
|
|
|
(recallResult.causalChain || [])
|
2026-01-27 16:04:57 +08:00
|
|
|
|
.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);
|
2026-02-10 12:43:43 +08:00
|
|
|
|
await echo(`/echo severity=warning 嵌入 API 请求失败:${msg}(本次跳过记忆召回)`);
|
2026-01-27 16:04:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (postToFrame) {
|
|
|
|
|
|
postToFrame({
|
|
|
|
|
|
type: "RECALL_LOG",
|
|
|
|
|
|
text: `\n[Vector Recall Failed]\n${String(e?.stack || e?.message || e)}\n`,
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2026-01-26 23:50:48 +08:00
|
|
|
|
|
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 ||
|
2026-02-10 00:18:51 +08:00
|
|
|
|
(recallResult?.l0Selected?.length || 0) > 0 ||
|
2026-02-09 10:09:16 +08:00
|
|
|
|
(recallResult?.causalChain?.length || 0) > 0;
|
2026-01-27 16:04:57 +08:00
|
|
|
|
|
|
|
|
|
|
if (!hasUseful) {
|
2026-02-10 12:43:43 +08:00
|
|
|
|
const noVectorsGenerated = !meta?.fingerprint || (meta?.lastChunkFloor ?? -1) < 0;
|
|
|
|
|
|
const fpMismatch = meta?.fingerprint && meta.fingerprint !== getEngineFingerprint(vectorCfg);
|
|
|
|
|
|
|
|
|
|
|
|
if (fpMismatch) {
|
|
|
|
|
|
if (echo && canNotifyRecallFail()) {
|
|
|
|
|
|
await echo("/echo severity=warning 向量引擎已变更,请重新生成向量");
|
|
|
|
|
|
}
|
|
|
|
|
|
} else if (noVectorsGenerated) {
|
|
|
|
|
|
if (echo && canNotifyRecallFail()) {
|
|
|
|
|
|
await echo("/echo severity=warning 没有可用向量,请在剧情总结面板中生成向量");
|
|
|
|
|
|
}
|
2026-01-26 01:16:35 +08:00
|
|
|
|
}
|
2026-02-10 12:43:43 +08:00
|
|
|
|
// 向量存在但本次未命中 → 静默跳过,不打扰用户
|
|
|
|
|
|
|
|
|
|
|
|
if (postToFrame && (noVectorsGenerated || fpMismatch)) {
|
2026-01-27 16:04:57 +08:00
|
|
|
|
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-02-08 12:22:45 +08:00
|
|
|
|
const { promptText, metrics: promptMetrics } = await buildVectorPrompt(
|
2026-01-26 23:50:48 +08:00
|
|
|
|
store,
|
|
|
|
|
|
recallResult,
|
|
|
|
|
|
causalById,
|
2026-02-15 18:58:51 +08:00
|
|
|
|
recallResult?.focusCharacters || [],
|
2026-02-08 12:22:45 +08:00
|
|
|
|
meta,
|
|
|
|
|
|
recallResult?.metrics || null
|
2026-01-26 23:50:48 +08:00
|
|
|
|
);
|
|
|
|
|
|
|
2026-01-29 01:17:37 +08:00
|
|
|
|
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
|
|
|
|
|
2026-02-08 12:22:45 +08:00
|
|
|
|
const metricsLogText = promptMetrics ? formatMetricsLog(promptMetrics) : '';
|
|
|
|
|
|
|
2026-01-26 23:50:48 +08:00
|
|
|
|
if (postToFrame) {
|
2026-02-08 12:22:45 +08:00
|
|
|
|
postToFrame({ type: "RECALL_LOG", text: metricsLogText });
|
2026-01-26 01:16:35 +08:00
|
|
|
|
}
|
2026-01-27 16:04:57 +08:00
|
|
|
|
|
2026-02-08 12:22:45 +08:00
|
|
|
|
return { text: finalText, logText: metricsLogText };
|
2026-02-02 21:45:01 +08:00
|
|
|
|
}
|