Update story summary recall and prompt injection
This commit is contained in:
@@ -8,6 +8,7 @@ import { generateSummary, parseSummaryJson } from "./llm.js";
|
||||
|
||||
const MODULE_ID = 'summaryGenerator';
|
||||
const SUMMARY_SESSION_ID = 'xb9';
|
||||
const MAX_CAUSED_BY = 2;
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════════════
|
||||
// worldUpdate 清洗
|
||||
@@ -45,6 +46,57 @@ function sanitizeWorldUpdate(parsed) {
|
||||
parsed.worldUpdate = ok;
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════════════
|
||||
// causedBy 清洗(事件因果边)
|
||||
// - 允许引用:已存在事件 + 本次新输出事件
|
||||
// - 限制长度:0-2
|
||||
// - 去重、剔除非法ID、剔除自引用
|
||||
// ═══════════════════════════════════════════════════════════════════════════
|
||||
|
||||
function sanitizeEventsCausality(parsed, existingEventIds) {
|
||||
if (!parsed) return;
|
||||
|
||||
const events = Array.isArray(parsed.events) ? parsed.events : [];
|
||||
if (!events.length) return;
|
||||
|
||||
const idRe = /^evt-\d+$/;
|
||||
|
||||
// 本次新输出事件ID集合(允许引用)
|
||||
const newIds = new Set(
|
||||
events
|
||||
.map(e => String(e?.id || '').trim())
|
||||
.filter(id => idRe.test(id))
|
||||
);
|
||||
|
||||
const allowed = new Set([...(existingEventIds || []), ...newIds]);
|
||||
|
||||
for (const e of events) {
|
||||
const selfId = String(e?.id || '').trim();
|
||||
if (!idRe.test(selfId)) {
|
||||
// id 不合格的话,causedBy 直接清空,避免污染
|
||||
e.causedBy = [];
|
||||
continue;
|
||||
}
|
||||
|
||||
const raw = Array.isArray(e.causedBy) ? e.causedBy : [];
|
||||
const out = [];
|
||||
const seen = new Set();
|
||||
|
||||
for (const x of raw) {
|
||||
const cid = String(x || '').trim();
|
||||
if (!idRe.test(cid)) continue;
|
||||
if (cid === selfId) continue;
|
||||
if (!allowed.has(cid)) continue;
|
||||
if (seen.has(cid)) continue;
|
||||
seen.add(cid);
|
||||
out.push(cid);
|
||||
if (out.length >= MAX_CAUSED_BY) break;
|
||||
}
|
||||
|
||||
e.causedBy = out;
|
||||
}
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════════════
|
||||
// 辅助函数
|
||||
// ═══════════════════════════════════════════════════════════════════════════
|
||||
@@ -180,6 +232,8 @@ export async function runSummaryGeneration(mesId, config, callbacks = {}) {
|
||||
}
|
||||
|
||||
sanitizeWorldUpdate(parsed);
|
||||
const existingEventIds = new Set((store?.json?.events || []).map(e => e?.id).filter(Boolean));
|
||||
sanitizeEventsCausality(parsed, existingEventIds);
|
||||
|
||||
const merged = mergeNewData(store?.json || {}, parsed, slice.endMesId);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user