feat(llm): 历史重放重负载占位裁剪(write_file 参数 / fetch·read_file 回执)
- buildMessages 组装时对「距尾 ≥N 个 assistant 轮」的重负载行换单行占位: write_file 参数正文(≥1 轮)、fetch/read_file 回执(≥2 轮);DB/UI 不动, 占位保留 path/url 作恢复指针(文件在盘上可重读,页面可重抓) - 阈值可配 agent.trim_write_rounds / trim_result_rounds(0=关,重启生效) - 压缩回合显式关裁剪:摘要需要原料原文 - 短正文(<600 字)不裁;errAck/图片回执等既有专属路径不碰 - test/trim-smoke.js:纯函数冒烟(占位格式/新鲜保留/压缩豁免/开关/缺参不炸)
This commit is contained in:
@@ -53,6 +53,8 @@ agent:
|
||||
max_retries: 2 # 上游失败重试次数
|
||||
language: "Simplified Chinese" # 强制回复语言;留空不限
|
||||
auto_compress_tokens: 60000 # In token 超此值弹「自动压缩」提议;0=关
|
||||
trim_write_rounds: 1 # 历史重放裁剪:write_file 参数正文距尾 ≥N 个 assistant 轮换单行占位(0=关;DB/UI 不动,盘上可重读)
|
||||
trim_result_rounds: 2 # fetch/read_file 回执正文距尾 ≥N 个 assistant 轮换单行占位(0=关;占位留 url/path 作恢复指针)
|
||||
auto_complete_ms: 1800000 # 保持中(held)超此时长自动收口为已完成;0=关(纯状态动作,历史/模型绑定不动)
|
||||
|
||||
# 长期记忆(日记,memory.js):收口时一条几十字第一人称碎片入当日条目;存储默认永久,旧记忆靠回忆下钻
|
||||
|
||||
@@ -86,7 +86,9 @@ const DEFAULTS = {
|
||||
// auto_compress_tokens:本轮 In token 峰值 ≥ 此值时推送「自动压缩」提议(0=关)。
|
||||
// auto_complete_ms:held(保持中)超过此时长自动收口为 completed(0=关)。key 非独占后 held 不占任何资源,
|
||||
// 收口是纯视觉动作:历史/绑定不动,再发言照常由 completed 分支续跑(见 engine.sweepHeldTimeouts)。
|
||||
agent: { temperature: 1.0, max_tool_iterations: 100, max_retries: 2, language: 'Simplified Chinese', auto_compress_tokens: 60000, auto_complete_ms: 1800000 },
|
||||
// trim_write_rounds / trim_result_rounds:历史重放占位裁剪(llm.buildMessages)——write_file 参数正文 /
|
||||
// fetch、read_file 回执正文距尾 ≥N 个 assistant 轮后换单行占位(DB/UI 不动,内容可再生:盘上重读/重抓);0=关。
|
||||
agent: { temperature: 1.0, max_tool_iterations: 100, max_retries: 2, language: 'Simplified Chinese', auto_compress_tokens: 60000, auto_complete_ms: 1800000, trim_write_rounds: 1, trim_result_rounds: 2 },
|
||||
// 大图上传预处理(浏览器端 canvas 压缩,参数经 /api/system/status 下发前端)。
|
||||
upload: { image_compress_threshold_kb: 1536, image_max_dim: 2000, image_quality: 0.85, confirm_size_mb: 10 },
|
||||
// 技能根目录:留空取 workspace_root 同级 skills(/workspace/tasks → /workspace/skills)。
|
||||
|
||||
+73
-4
@@ -151,7 +151,7 @@ function cachedDecodeBlocks(session, row) {
|
||||
// profile:本轮生效的 prompt+tools 组合名(model_profiles,见 engine.resolveProfileName),
|
||||
// 空 = 默认全量组合。
|
||||
// withProfile=false 时跳过画像快照注入(压缩回合专用,见 compressMessages)。
|
||||
function buildMessages(session, language, mintUrl, profile, withProfile) {
|
||||
function buildMessages(session, language, mintUrl, profile, withProfile, trimOn) {
|
||||
const messages = [{ role: 'system', content: systemPromptText(language, profile) }];
|
||||
// 用户画像快照(sessions.profile_snapshot,建会话/清空时冻结,见 user-profile.js):
|
||||
// 作为 system 后首条 user 注入,会话内字节恒定不破坏前缀缓存;
|
||||
@@ -163,6 +163,25 @@ function buildMessages(session, language, mintUrl, profile, withProfile) {
|
||||
const ids = new Set(history.map(r => r.id));
|
||||
for (const k of Array.from(session.blocksCache.keys())) if (!ids.has(k)) session.blocksCache.delete(k);
|
||||
}
|
||||
// 裁剪预扫:① 每行距尾的 assistant 轮数(从尾往头扫);② tool_call id → function
|
||||
// (给 fetch/read_file 占位回填 url/path)。trimOn=false 时整个跳过(压缩回合)。
|
||||
const trimActive = trimOn !== false && (TRIM.writeRounds > 0 || TRIM.resultRounds > 0);
|
||||
const roundsAfter = new Map();
|
||||
const callById = new Map();
|
||||
if (trimActive) {
|
||||
let n = 0;
|
||||
for (let i = history.length - 1; i >= 0; i--) {
|
||||
const r = history[i];
|
||||
roundsAfter.set(r.id, n);
|
||||
if (r.role === 'assistant' && r.type !== 'archived' && r.type !== 'errAck') n++;
|
||||
}
|
||||
if (TRIM.resultRounds > 0) {
|
||||
for (const r of history) {
|
||||
if (r.role !== 'assistant' || !r.tool_calls) continue;
|
||||
try { for (const c of JSON.parse(r.tool_calls) || []) if (c && c.id && c.function) callById.set(c.id, c.function); } catch (_) {}
|
||||
}
|
||||
}
|
||||
}
|
||||
// tool 回执按 tool_call_id 索引:给 errAck 行拼错误提示时取执行时挡下的原因
|
||||
const toolRowById = new Map();
|
||||
for (const row of history) if (row.role === 'tool' && row.tool_call_id) toolRowById.set(row.tool_call_id, row);
|
||||
@@ -225,7 +244,10 @@ function buildMessages(session, language, mintUrl, profile, withProfile) {
|
||||
return;
|
||||
}
|
||||
const m = { role: 'assistant', content: row.content || '' };
|
||||
if (row.tool_calls) m.tool_calls = JSON.parse(row.tool_calls);
|
||||
if (row.tool_calls) {
|
||||
m.tool_calls = JSON.parse(row.tool_calls);
|
||||
if (trimActive && TRIM.writeRounds > 0) trimWriteCalls(m.tool_calls, roundsAfter.get(row.id) || 0);
|
||||
}
|
||||
if (!m.content) delete m.content;
|
||||
messages.push(m);
|
||||
return;
|
||||
@@ -271,6 +293,7 @@ function buildMessages(session, language, mintUrl, profile, withProfile) {
|
||||
}
|
||||
}
|
||||
if (row.role === 'tool') {
|
||||
if (trimActive && typeof content === 'string') content = trimToolContent(content, row, callById, roundsAfter);
|
||||
messages.push({ role: 'tool', tool_call_id: row.tool_call_id, name: row.name, content });
|
||||
} else {
|
||||
messages.push({ role: row.role, content });
|
||||
@@ -292,6 +315,51 @@ function buildMessages(session, language, mintUrl, profile, withProfile) {
|
||||
return messages;
|
||||
}
|
||||
|
||||
// ---------- 重负载占位裁剪(历史重放省 token) ----------
|
||||
// fetch/read_file 回执与 write_file 参数正文是重放里最贵的存量,且都可再生:文件在盘上、
|
||||
// 页面可重抓;write 参数只是当时快照(后续 edit_file 一次就失真,本就不该被当现势引用)。
|
||||
// 组装请求时对「距尾 ≥N 个 assistant 轮」的行换单行占位:纯函数、DB 不动(UI 工具卡照旧
|
||||
// 可展全量)、与压缩归档正交(archived 行本来就不进模型)。距离按现存行数 assistant 轮:
|
||||
// 删中间消息可让已占位行短暂复活全文(一次性断前缀缓存,与图片消费 i<lastAssistant 同一
|
||||
// 语义,接受)。阈值可配(config agent.trim_write_rounds / trim_result_rounds,0=关),
|
||||
// 启动时 setPayloadTrim 注入。
|
||||
let TRIM = { writeRounds: 1, resultRounds: 2 };
|
||||
const TRIM_MIN_CHARS = 600; // 短正文不裁:占位行自身成本接近原文,裁了反而胀
|
||||
function setPayloadTrim(agentCfg) {
|
||||
const a = agentCfg || {};
|
||||
const toN = (v, d) => { const n = Math.floor(Number(v)); return Number.isFinite(n) ? Math.max(0, n) : d; };
|
||||
TRIM = { writeRounds: toN(a.trim_write_rounds, 1), resultRounds: toN(a.trim_result_rounds, 2) };
|
||||
}
|
||||
// write_file 参数占位:只洗 content,path 留着(模型回看自己发言时知道文件在哪);
|
||||
// 改写的是已 parse 的副本,DB 行不动;arguments 重新 stringify 保持 JSON 合法。
|
||||
function trimWriteCalls(calls, rounds) {
|
||||
if (rounds < TRIM.writeRounds) return;
|
||||
for (const c of calls || []) {
|
||||
if (!c || !c.function || c.function.name !== 'write_file') continue;
|
||||
let args;
|
||||
try { args = JSON.parse(c.function.arguments || ''); } catch (_) { continue; }
|
||||
if (!args || typeof args.content !== 'string' || args.content.length < TRIM_MIN_CHARS) continue;
|
||||
args.content = '[正文省略:本调用已写入 ' + String(args.path || '') + ',' + args.content.length + ' 字]';
|
||||
c.function.arguments = JSON.stringify(args);
|
||||
}
|
||||
}
|
||||
// fetch/read_file 回执占位:url/path 从对应 tool_call 参数回填(回执行自身不存调用目标),
|
||||
// 占位行里留目标就是留恢复指针。图片回执(blocks 非空)不碰:已有自己的消费省略逻辑。
|
||||
function trimToolContent(content, row, callById, roundsAfter) {
|
||||
if (TRIM.resultRounds <= 0 || content.length < TRIM_MIN_CHARS) return content;
|
||||
if ((roundsAfter.get(row.id) || 0) < TRIM.resultRounds) return content;
|
||||
const fn = callById.get(row.tool_call_id);
|
||||
const name = row.name || (fn && fn.name) || '';
|
||||
if (name !== 'fetch' && name !== 'read_file') return content;
|
||||
let arg = {};
|
||||
try { arg = JSON.parse((fn && fn.arguments) || '{}') || {}; } catch (_) {}
|
||||
if (name === 'fetch') {
|
||||
const hm = /^HTTP (\d{3})/.exec(content);
|
||||
return '[fetch 省略:' + String(arg.url || '?') + (hm ? '|HTTP ' + hm[1] : '') + '|原 ' + content.length + ' 字]';
|
||||
}
|
||||
return '[read_file 省略:' + String(arg.path || '?') + '|原 ' + content.length + ' 字]';
|
||||
}
|
||||
|
||||
// 拼压缩回合的 messages(参考 titleMessages 的拼法):默认 system prompt + 正常历史
|
||||
//(与 buildMessages 同取舍:archived 不发、旧摘要/工具轮照常),末尾追加一条 user
|
||||
//(compress-prompt.txt 原文,历史就在它前面的 messages 里,不再填占位符)。
|
||||
@@ -300,7 +368,8 @@ function buildMessages(session, language, mintUrl, profile, withProfile) {
|
||||
// 无需烧进摘要;一旦模型把画像转述进摘要,摘要会以 type='summary' 落库,
|
||||
// 再经 titleMessages(只排 archived)混进标题生成的输入里。
|
||||
function compressMessages(session, language, mintUrl, profile) {
|
||||
const messages = buildMessages(session, language, mintUrl, profile, false);
|
||||
// trim 显式关:摘要需要 fetch/read 的原料原文,且压缩是一次性成本,省它得不偿失。
|
||||
const messages = buildMessages(session, language, mintUrl, profile, false, false);
|
||||
messages.push({ role: 'user', content: COMPRESS_PROMPT_TEMPLATE });
|
||||
return messages;
|
||||
}
|
||||
@@ -556,4 +625,4 @@ async function callModel(cfg, modelId, messages, tools, options) {
|
||||
return { content, reasoning, tool_calls: Array.from(toolCalls.values()), finish_reason: finishReason, usage, aborted, sseFrames };
|
||||
}
|
||||
|
||||
module.exports = { callModel, buildMessages, buildSystemPrompt, systemPromptText, setSkillListText, setProfilePrompts, encodeBlocks, decodeBlocks, compressMessages, titleMessages };
|
||||
module.exports = { callModel, buildMessages, buildSystemPrompt, systemPromptText, setSkillListText, setPayloadTrim, setProfilePrompts, encodeBlocks, decodeBlocks, compressMessages, titleMessages };
|
||||
|
||||
@@ -18,6 +18,7 @@ const log = require('./log');
|
||||
|
||||
const cfg = loadConfig(process.env.AGENT_CONFIG);
|
||||
log.setLevel(cfg.log_level); // 分级日志开关(log.js;config.yml log_level,重启生效)
|
||||
llm.setPayloadTrim(cfg.agent); // 历史重放的重负载占位裁剪阈值(llm.buildMessages;config agent.trim_*)
|
||||
const db = initDb(cfg.db_path);
|
||||
const bus = createBus();
|
||||
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
// 临时冒烟:重负载占位裁剪(跑完即删)。
|
||||
const assert = require('assert');
|
||||
const llm = require('../server/llm');
|
||||
|
||||
const big = n => 'x'.repeat(n);
|
||||
const mk = (id, role, extra) => Object.assign({ id, role, type: null, content: '' }, extra);
|
||||
const call = (id, name, args) => ({ id, type: 'function', function: { name, arguments: JSON.stringify(args) } });
|
||||
|
||||
const rows = [
|
||||
mk(1, 'user', { content: '抓X存notes再写summary' }),
|
||||
mk(2, 'assistant', { tool_calls: JSON.stringify([call('c1', 'fetch', { url: 'https://e.com/x' })]) }),
|
||||
mk(3, 'tool', { tool_call_id: 'c1', name: 'fetch', content: 'HTTP 200\n' + big(8000) }),
|
||||
mk(4, 'assistant', { tool_calls: JSON.stringify([call('c2', 'write_file', { path: 'notes.md', content: big(12000) })]) }),
|
||||
mk(5, 'tool', { tool_call_id: 'c2', name: 'write_file', content: '已创建文件 notes.md(12003 字节)' }),
|
||||
mk(6, 'assistant', { tool_calls: JSON.stringify([call('c3', 'write_file', { path: 'summary.md', content: '小内容' })]) }),
|
||||
mk(7, 'tool', { tool_call_id: 'c3', name: 'write_file', content: '已创建文件 summary.md(9 字节)' }),
|
||||
mk(8, 'assistant', { content: '两篇已写好' }),
|
||||
];
|
||||
const session = () => ({ history: rows.slice(), profile_snapshot: '', blocksCache: new Map() });
|
||||
|
||||
// A) 全历史:距尾够远 → fetch 回执与 write_file(大) 参数被占位;write_file(小)/回执/收尾不动
|
||||
let msgs = llm.buildMessages(session(), 'Simplified Chinese', null, null);
|
||||
const a = msgs.filter(m => m.role === 'assistant' && m.tool_calls);
|
||||
const argsNotes = JSON.parse(a.find(m => m.tool_calls[0].id === 'c2').tool_calls[0].function.arguments);
|
||||
assert(/^\[正文省略:本调用已写入 notes\.md,12000 字\]$/.test(argsNotes.content), 'write_file 大参数应占位: ' + argsNotes.content);
|
||||
const argsSum = JSON.parse(a.find(m => m.tool_calls[0].id === 'c3').tool_calls[0].function.arguments);
|
||||
assert.strictEqual(argsSum.content, '小内容', '低于 TRIM_MIN 不裁');
|
||||
const t = msgs.find(m => m.role === 'tool' && m.tool_call_id === 'c1');
|
||||
assert(/^\[fetch 省略:https:\/\/e\.com\/x|HTTP 200|原 8009 字\]$/.test(t.content), 'fetch 回执应占位: ' + t.content);
|
||||
assert(msgs.find(m => m.role === 'tool' && m.tool_call_id === 'c2').content.includes('已创建文件'), 'write 回执不动');
|
||||
|
||||
// B) 截到第 5 行(write 刚落盘的当次调用):都太新鲜,不裁
|
||||
msgs = llm.buildMessages({ history: rows.slice(0, 5), profile_snapshot: '', blocksCache: new Map() }, 'Simplified Chinese', null, null);
|
||||
assert(msgs.find(m => m.role === 'tool' && m.tool_call_id === 'c1').content.length > 7000, 'fetch 距尾 1 轮应保留');
|
||||
assert.strictEqual(JSON.parse(msgs.filter(m => m.tool_calls).pop().tool_calls[0].function.arguments).content.length, 12000, 'write 参数距尾 0 轮应保留');
|
||||
|
||||
// C) 压缩回合不裁
|
||||
const cm = llm.compressMessages(session(), 'Simplified Chinese', null, null);
|
||||
assert(cm.find(m => m.role === 'tool' && m.tool_call_id === 'c1').content.length > 7000, '压缩回合保留原文');
|
||||
|
||||
// D) read_file 回执同款
|
||||
const r3orig = rows[2];
|
||||
rows[2] = mk(3, 'tool', { tool_call_id: 'c1', name: 'read_file', content: 'HTTP 200\n' + big(8000) });
|
||||
rows[1] = mk(2, 'assistant', { tool_calls: JSON.stringify([call('c1', 'read_file', { path: 'a.md' })]) });
|
||||
msgs = llm.buildMessages(session(), 'Simplified Chinese', null, null);
|
||||
assert(/^\[read_file 省略:a\.md|原 8009 字\]$/.test(msgs.find(m => m.tool_call_id === 'c1' && m.role === 'tool').content), 'read_file 应占位');
|
||||
rows[2] = r3orig;
|
||||
rows[1] = mk(2, 'assistant', { tool_calls: JSON.stringify([call('c1', 'fetch', { url: 'https://e.com/x' })]) });
|
||||
|
||||
// E) 开关:0=关
|
||||
llm.setPayloadTrim({ trim_write_rounds: 0, trim_result_rounds: 0 });
|
||||
msgs = llm.buildMessages(session(), 'Simplified Chinese', null, null);
|
||||
assert(msgs.find(m => m.role === 'tool' && m.tool_call_id === 'c1').content.length > 7000, '0=关');
|
||||
llm.setPayloadTrim({}); // 回落默认 1/2
|
||||
|
||||
// F) 非法/缺失参数不炸(arguments 非 JSON、无 url 的 fetch;需距尾够远)
|
||||
rows.push(mk(9, 'assistant', { tool_calls: JSON.stringify([call('c9', 'fetch', {})]) }));
|
||||
rows.push(mk(10, 'tool', { tool_call_id: 'c9', name: 'fetch', content: 'HTTP 404\n' + big(700) }));
|
||||
rows.push(mk(11, 'assistant', { content: '页面不存在,换个 url?' }));
|
||||
rows.push(mk(12, 'assistant', { tool_calls: JSON.stringify([call('c12', 'read_file', { path: 'notes.md' })]) }));
|
||||
msgs = llm.buildMessages(session(), 'Simplified Chinese', null, null);
|
||||
assert(/\[fetch 省略:\?|HTTP 404/.test(msgs.find(m => m.tool_call_id === 'c9').content), '缺 url 仍占位: ' + msgs.find(m => m.tool_call_id === 'c9').content);
|
||||
|
||||
console.log('TRIM-SMOKE-OK');
|
||||
Reference in New Issue
Block a user