fix(engine): 增量上下文作废按 sessionId 现查,修复编辑后仍发增量
getSession() 只查 sessions 表,返回对象没有 history 字段,contextIdOf 里 archivedCount(session.history) 恒得 0。编辑/删除/清空四处入口的 invalidateContext 于是全作用在 "…:0" 上,压缩过的会话真实 cid(如 "…:3031")从没被作废,编辑完仍旧发增量,relay 缓存里留着已删除的行,上游随即 tool_call_mismatch。 改为拿不到 history 时按 sessionId 现查 count(type='archived');runLoop 热路径仍走内存计数。另把「模型调用失败」文案落库标为 type='error_note':buildMessages 跳过、微信不外发,避免失败文案被当成模型回复带进后续轮次。
This commit is contained in:
+13
-3
@@ -118,11 +118,19 @@ function archivedCount(history) {
|
|||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 同上,但按 sessionId 现查:编辑/删除入口拿到的是裸 sessions 行(不含 history),
|
||||||
|
// 用 archivedCount(session.history) 会恒得 0,作废的 cid 就打到别的 key 上(压缩过的会话必踩)。
|
||||||
|
function archivedCountOf(sessionId) {
|
||||||
|
const r = db.prepare("SELECT COUNT(*) AS n FROM messages WHERE session_id = ? AND type = 'archived'").get(sessionId);
|
||||||
|
return r ? r.n : 0;
|
||||||
|
}
|
||||||
|
|
||||||
// 增量上下文 id 的拼法(与 llm.js 的 X-Context-Id 一致):<core_id>:<user>:<session_id>:<archived_cnt>。
|
// 增量上下文 id 的拼法(与 llm.js 的 X-Context-Id 一致):<core_id>:<user>:<session_id>:<archived_cnt>。
|
||||||
// 删除消息后要把这个 key 作废,让下一轮走全量重建。
|
// 删除消息后要把这个 key 作废,让下一轮走全量重建。
|
||||||
|
// runLoop 里 session.history 已就绪,走内存计数;其余入口现查库,两条路结果一致。
|
||||||
function contextIdOf(session) {
|
function contextIdOf(session) {
|
||||||
return instanceId(cfg.data_root) + ':' + (session.user || '-') + ':' + session.id +
|
const n = session.history ? archivedCount(session.history) : archivedCountOf(session.id);
|
||||||
':' + archivedCount(session.history);
|
return instanceId(cfg.data_root) + ':' + (session.user || '-') + ':' + session.id + ':' + n;
|
||||||
}
|
}
|
||||||
|
|
||||||
function saveMessage(sessionId, msg) {
|
function saveMessage(sessionId, msg) {
|
||||||
@@ -885,7 +893,9 @@ async function runLoopInner(sessionId, run) {
|
|||||||
if (run.cancelled) break;
|
if (run.cancelled) break;
|
||||||
if (!result) {
|
if (!result) {
|
||||||
const text = '⚠️ 模型调用失败:' + (lastErr && lastErr.message || '未知错误');
|
const text = '⚠️ 模型调用失败:' + (lastErr && lastErr.message || '未知错误');
|
||||||
const errId = saveMessage(sessionId, { role: 'assistant', content: text });
|
// type='error_note':留痕给 UI,但不进模型上下文(buildMessages 跳过),
|
||||||
|
// 否则失败文案会当成模型的真实回复一直带下去。
|
||||||
|
const errId = saveMessage(sessionId, { role: 'assistant', content: text, type: 'error_note' });
|
||||||
bus.emit(sessionId, 'assistant_message', { id: errId, content: text, error: true, created_at: now() });
|
bus.emit(sessionId, 'assistant_message', { id: errId, content: text, error: true, created_at: now() });
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -310,6 +310,8 @@ function buildMessages(session, language, mintUrl, profile, withProfile, trimOn)
|
|||||||
history.forEach((row, i) => {
|
history.forEach((row, i) => {
|
||||||
// archived:UI 可见,但不进入模型上下文(节省 token)
|
// archived:UI 可见,但不进入模型上下文(节省 token)
|
||||||
if (row.type === 'archived') return;
|
if (row.type === 'archived') return;
|
||||||
|
// error_note:失败文案,只给 UI 留痕,不进模型(否则被当成模型的真实回复带下去)
|
||||||
|
if (row.type === 'error_note') return;
|
||||||
// summarize_request(旧版遗留行):仅在未收尾的旧压缩回合内发给模型,其余一律跳过
|
// summarize_request(旧版遗留行):仅在未收尾的旧压缩回合内发给模型,其余一律跳过
|
||||||
if (row.type === 'summarize_request') {
|
if (row.type === 'summarize_request') {
|
||||||
if (i > lastClosingAssistant && (row.content || '').trim()) {
|
if (i > lastClosingAssistant && (row.content || '').trim()) {
|
||||||
|
|||||||
@@ -489,7 +489,7 @@ function getTurn(bid) { return turnState.get(bid) || resetTurn(bid); }
|
|||||||
|
|
||||||
// 投递前的内容归一(语义同旧 deliverNow):errAck / 空文本 → 不可投;富文本 blocks → 占位文案。
|
// 投递前的内容归一(语义同旧 deliverNow):errAck / 空文本 → 不可投;富文本 blocks → 占位文案。
|
||||||
function normalizeContent(m) {
|
function normalizeContent(m) {
|
||||||
if (!m || m.type === 'errAck') return '';
|
if (!m || m.type === 'errAck' || m.type === 'error_note') return '';
|
||||||
let content = String(m.content || '').trim();
|
let content = String(m.content || '').trim();
|
||||||
if (!content) return '';
|
if (!content) return '';
|
||||||
if (content.startsWith('{"__blocks__"')) content = '【富文本消息,请到网页端查看】';
|
if (content.startsWith('{"__blocks__"')) content = '【富文本消息,请到网页端查看】';
|
||||||
|
|||||||
Reference in New Issue
Block a user