占位裁剪新增 turns(真人回合)条件:回合内不裁,跨回合才裁
rounds 数的是 assistant 行,长回合里会把模型还要用的回执裁掉——实测有回合把同一文件 分段重读了 24 次(每次都在 rounds:2 生效的那一轮回来重读)。 - 规则新增 turns 字段:其后又来了几条真人消息(role=user 且 type 为空,注入行不算) - rounds 与 turns 同时满足才裁(AND);两者都写 0 才算规则关闭 - 默认:fetch / read_file / web_search 用 rounds:2 + turns:1(回合内绝不裁), write_file 参数侧保持 rounds:1 + turns:0(只是写盘快照,照旧早裁) - config.example.yml 补了带时间线的例子与「rounds=后面 AI 又说了几句话 / turns=后面又来了几条真人消息」的注释;AGENTS.md 与 tool-design.md 同步 - 启动日志改为打印 轮≥N,回合≥M,便于核对生效规则
This commit is contained in:
+19
-11
@@ -87,15 +87,21 @@ const DEFAULTS = {
|
||||
// auto_complete_ms:held(保持中)超过此时长自动收口为 completed(0=关)。key 非独占后 held 不占任何资源,
|
||||
// 收口是纯视觉动作:历史/绑定不动,再发言照常由 completed 分支续跑(见 engine.sweepHeldTimeouts)。
|
||||
// 历史重放占位裁剪(llm.buildMessages):trim.rules 逐条声明「哪个工具、参数侧还是回执侧、
|
||||
// 距尾多少个 assistant 轮后换单行占位」;只改发给上游的副本,DB/UI 不动。
|
||||
// 距尾多远换单行占位」;只改发给上游的副本,DB/UI 不动。
|
||||
// 两个距离条件**同时满足**才裁:
|
||||
// rounds = 这一行之后又出现了几条 assistant 行(「后面 AI 又说了几句话」)
|
||||
// turns = 这一行之后又来了几条真人消息(role=user 且 type 为空,「后面又来了几条真人消息」)
|
||||
// 所以 turns:1 = 回合内绝不裁、跨过用户回合边界才裁(避免长回合里模型反复重读同一个文件)。
|
||||
// 这里是默认策略的唯一来源:config.yml 写了 trim.rules 就整体替换(写 [] = 全关)。
|
||||
agent: { temperature: 1.0, max_tool_iterations: 100, max_retries: 2, language: 'Simplified Chinese', auto_compress_tokens: 60000, auto_complete_ms: 1800000, trim: {
|
||||
min_chars: 600,
|
||||
rules: [
|
||||
// 参数正文只是写盘时的快照:不带 turns,回合内也照裁(省的是每轮重发整个文件)
|
||||
{ tool: 'write_file', side: 'args', fields: ['content'], rounds: 1 },
|
||||
{ tool: 'fetch', side: 'result', rounds: 2, status_regex: '^HTTP (\\d{3})' },
|
||||
{ tool: 'read_file', side: 'result', rounds: 2 },
|
||||
{ tool: 'web_search', side: 'result', rounds: 2 },
|
||||
// 回执可再生(页面可重抓 / 文件可重读 / 可重搜):turns:1 保证同一回合内不裁,回合结束后按 rounds 兜底
|
||||
{ tool: 'fetch', side: 'result', rounds: 2, turns: 1, status_regex: '^HTTP (\\d{3})' },
|
||||
{ tool: 'read_file', side: 'result', rounds: 2, turns: 1 },
|
||||
{ tool: 'web_search', side: 'result', rounds: 2, turns: 1 },
|
||||
],
|
||||
} },
|
||||
// 大图上传预处理(浏览器端 canvas 压缩,参数经 /api/system/status 下发前端)。
|
||||
@@ -246,11 +252,12 @@ function normalizePlatforms(rawPlatforms, profileNames) {
|
||||
}
|
||||
|
||||
// ---------- 历史重放的重负载占位裁剪(agent.trim) ----------
|
||||
// 语义:某条工具调用/回执距对话尾部 ≥rounds 个 assistant 轮后,把其中「可再生的重负载」
|
||||
// 换成单行占位再发给上游(llm.buildMessages;DB/UI 不动)。
|
||||
// 只该给「内容可再生或只是当时快照」的工具开:read_file/fetch 能重读重抓、write_file 参数
|
||||
// 语义:某条工具调用/回执距对话尾部 ≥rounds 个 assistant 轮、且 ≥turns 个真人回合后,
|
||||
// 把其中「可再生的重负载」换成单行占位再发给上游(llm.buildMessages;DB/UI 不动)。
|
||||
// rounds = 「后面 AI 又说了几句话」;turns = 「后面又来了几条真人消息」(type 为空的 user 行)。
|
||||
// 两者同时满足才裁。只该给「内容可再生或只是当时快照」的工具开:read_file/fetch 能重读重抓、write_file 参数
|
||||
// 只是快照;terminal 输出是一次性的,裁掉模型就永久看不到了。
|
||||
const TRIM_RULE_KEYS = new Set(['tool', 'side', 'rounds', 'fields', 'min_chars', 'status_regex']);
|
||||
const TRIM_RULE_KEYS = new Set(['tool', 'side', 'rounds', 'turns', 'fields', 'min_chars', 'status_regex']);
|
||||
|
||||
function toNonNegInt(v, d) {
|
||||
const n = Math.floor(Number(v));
|
||||
@@ -272,10 +279,11 @@ function normalizeTrimRule(r, i, defMinChars) {
|
||||
if (side !== 'args' && side !== 'result') {
|
||||
throw new Error(at + '.side 只能是 args(裁调用参数)/ result(裁回执正文),收到:' + side);
|
||||
}
|
||||
// rounds=0 = 该条规则关闭;直接丢掉,不留空规则。
|
||||
// rounds / turns 都是「距尾多远」,同时满足才裁;两个都写 0(或不写)= 该条规则关闭,直接丢掉,不留空规则。
|
||||
const rounds = toNonNegInt(r.rounds, 0);
|
||||
if (rounds <= 0) return null;
|
||||
const out = { tool, side, rounds, min_chars: toNonNegInt(r.min_chars, defMinChars) };
|
||||
const turns = toNonNegInt(r.turns, 0);
|
||||
if (rounds <= 0 && turns <= 0) return null;
|
||||
const out = { tool, side, rounds, turns, min_chars: toNonNegInt(r.min_chars, defMinChars) };
|
||||
if (side === 'args') {
|
||||
if (!Array.isArray(r.fields) || !r.fields.length) {
|
||||
throw new Error(at + '(args 侧)必须配 fields 数组(要占位的顶层参数名,如 [content])');
|
||||
|
||||
+26
-14
@@ -198,16 +198,23 @@ function buildMessages(session, language, mintUrl, profile, withProfile, trimOn)
|
||||
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
|
||||
// (result 侧规则需要反查工具名)。trimOn=false(压缩回合)或没配规则时整个跳过。
|
||||
// 裁剪预扫:① 每行距尾的 assistant 轮数(rounds)与真人回合数(turns),一趟从尾往头扫;
|
||||
// ② tool_call id → function(result 侧规则需要反查工具名)。
|
||||
// turns 只数「真实用户消息」(role=user 且 type 为空):channel_note / memory_note 这类注入行
|
||||
// 不是回合边界(与 engine 立题同口径),数进去会让回合数虚高、提前裁掉本回合的内容。
|
||||
// trimOn=false(压缩回合)或没配规则时整个跳过。
|
||||
const trimActive = trimOn !== false && (TRIM.argsByTool.size > 0 || TRIM.resultByTool.size > 0);
|
||||
const roundsAfter = new Map();
|
||||
const turnsAfter = new Map();
|
||||
const callById = new Map();
|
||||
if (trimActive) {
|
||||
let n = 0;
|
||||
let t = 0;
|
||||
for (let i = history.length - 1; i >= 0; i--) {
|
||||
const r = history[i];
|
||||
roundsAfter.set(r.id, n);
|
||||
turnsAfter.set(r.id, t);
|
||||
if (r.role === 'user' && r.type == null) t++;
|
||||
if (r.role === 'assistant' && r.type !== 'archived' && r.type !== 'errAck') n++;
|
||||
}
|
||||
if (TRIM.resultByTool.size > 0) {
|
||||
@@ -331,7 +338,7 @@ function buildMessages(session, language, mintUrl, profile, withProfile, trimOn)
|
||||
} else {
|
||||
m.tool_calls = paired;
|
||||
for (const c of paired) emittedCallIds.add(c.id);
|
||||
if (trimActive && TRIM.argsByTool.size > 0) trimToolCallArgs(m.tool_calls, roundsAfter.get(row.id) || 0);
|
||||
if (trimActive && TRIM.argsByTool.size > 0) trimToolCallArgs(m.tool_calls, roundsAfter.get(row.id) || 0, turnsAfter.get(row.id) || 0);
|
||||
}
|
||||
}
|
||||
if (!m.content) delete m.content;
|
||||
@@ -394,7 +401,7 @@ function buildMessages(session, language, mintUrl, profile, withProfile, trimOn)
|
||||
}
|
||||
}
|
||||
if (row.role === 'tool') {
|
||||
if (trimActive && typeof content === 'string') content = trimToolContent(content, row, callById, roundsAfter);
|
||||
if (trimActive && typeof content === 'string') content = trimToolContent(content, row, callById, roundsAfter, turnsAfter);
|
||||
messages.push({ role: 'tool', tool_call_id: row.tool_call_id, name: row.name, content });
|
||||
} else {
|
||||
messages.push({ role: row.role, content });
|
||||
@@ -424,22 +431,26 @@ function buildMessages(session, language, mintUrl, profile, withProfile, trimOn)
|
||||
// ---------- 重负载占位裁剪(历史重放省 token) ----------
|
||||
// 策略只在配置里:默认值来自 config.js 的 DEFAULTS.agent.trim.rules,config.yml 写了
|
||||
// agent.trim.rules 就整体替换([] = 全关)。本模块不含任何工具名或默认阈值;未注入 = 不裁剪。
|
||||
// 语义:某条工具调用/回执距对话尾部 ≥rounds 个 assistant 轮后,把其中「可再生的重负载」
|
||||
// ——args 侧指定参数正文、result 侧整段回执——换成单行占位再发给上游。
|
||||
// 语义:某条工具调用/回执距对话尾部 ≥rounds 个 assistant 轮**且** ≥turns 个真人回合后,把其中
|
||||
//「可再生的重负载」——args 侧指定参数正文、result 侧整段回执——换成单行占位再发给上游。
|
||||
// rounds = 「后面 AI 又说了几句话」,turns = 「后面又来了几条真人消息」;
|
||||
// turns:1 就是「回合内绝不裁,跨过用户回合边界才裁」(长回合里防模型反复重读同一段)。
|
||||
// 只应给可再生/快照型工具开规则:占位只改发给上游的副本(DB/UI 照旧可看全量),但模型侧
|
||||
// 一旦被裁就再也看不到那段内容(如 terminal 的一次性输出)。
|
||||
// 纯函数、与压缩归档正交(archived 行本来就不进模型)。距离按现存行数 assistant 轮:
|
||||
// 删中间消息可让已占位行短暂复活全文(一次性断前缀缓存,与图片消费 i<lastAck 同一
|
||||
// 语义,接受)。
|
||||
// 纯函数、与压缩归档正交(archived 行本来就不进模型)。距离按现存行数:删中间消息可让已占位行
|
||||
// 短暂复活全文(一次性断前缀缓存,与图片消费 i<lastAck 同一语义,接受)。
|
||||
function compileTrimRules(rules) {
|
||||
const argsByTool = new Map();
|
||||
const resultByTool = new Map();
|
||||
for (const r of rules || []) {
|
||||
if (!r || !r.tool) continue;
|
||||
const rounds = Math.floor(Number(r.rounds));
|
||||
if (!(rounds > 0)) continue;
|
||||
const rounds = Math.max(0, Math.floor(Number(r.rounds)) || 0);
|
||||
const turns = Math.max(0, Math.floor(Number(r.turns)) || 0);
|
||||
// 两个距离都写 0 = 规则关闭(config.js 归一化时已丢弃,这里兼容手搓 cfg)
|
||||
if (rounds <= 0 && turns <= 0) continue;
|
||||
const rule = {
|
||||
rounds,
|
||||
turns,
|
||||
minChars: Math.max(0, Math.floor(Number(r.min_chars)) || 0),
|
||||
fields: Array.isArray(r.fields) ? r.fields.map(String) : [],
|
||||
statusRe: null,
|
||||
@@ -463,11 +474,11 @@ function omittedMarker(size, extra) {
|
||||
}
|
||||
// args 侧占位:只洗规则点名的顶层字符串参数,其余参数原样保留(path/url 等恢复指针不动)。
|
||||
// 改写的是已 parse 副本,DB 行不动,重新 stringify 保持 JSON 合法。
|
||||
function trimToolCallArgs(calls, rounds) {
|
||||
function trimToolCallArgs(calls, rounds, turns) {
|
||||
for (const c of calls || []) {
|
||||
if (!c || !c.function) continue;
|
||||
const rule = TRIM.argsByTool.get(c.function.name);
|
||||
if (!rule || rounds < rule.rounds) continue;
|
||||
if (!rule || rounds < rule.rounds || turns < rule.turns) continue;
|
||||
let args;
|
||||
try { args = JSON.parse(c.function.arguments || ''); } catch (_) { continue; }
|
||||
if (!args || typeof args !== 'object' || Array.isArray(args)) continue;
|
||||
@@ -483,13 +494,14 @@ function trimToolCallArgs(calls, rounds) {
|
||||
}
|
||||
// result 侧占位:整段回执换单行。图片回执(blocks 非空)在上游分支里已还原成文字/图块,
|
||||
// 这里只处理纯文本;status_regex 可把状态码写进占位(如 fetch 的 HTTP 状态)。
|
||||
function trimToolContent(content, row, callById, roundsAfter) {
|
||||
function trimToolContent(content, row, callById, roundsAfter, turnsAfter) {
|
||||
if (typeof content !== 'string') return content;
|
||||
const fn = callById.get(row.tool_call_id);
|
||||
const name = row.name || (fn && fn.name) || '';
|
||||
const rule = TRIM.resultByTool.get(name);
|
||||
if (!rule) return content;
|
||||
if ((roundsAfter.get(row.id) || 0) < rule.rounds) return content;
|
||||
if ((turnsAfter.get(row.id) || 0) < rule.turns) return content;
|
||||
if (content.length < rule.minChars) return content;
|
||||
let extra = '';
|
||||
if (rule.statusRe) {
|
||||
|
||||
@@ -21,7 +21,7 @@ log.setLevel(cfg.log_level); // 分级日志开关(log.js;config.yml log_level,
|
||||
llm.setPayloadTrim(cfg.agent); // 历史重放的重负载占位裁剪规则(llm.buildMessages;config agent.trim.rules)
|
||||
// 启动时把生效规则打出来:工具名拼错不会报错(MCP 工具要等发现后才存在),只能靠这行核对。
|
||||
log.info('startup', '历史重放占位裁剪:' + (((cfg.agent && cfg.agent.trim && cfg.agent.trim.rules) || [])
|
||||
.map(r => r.tool + '/' + r.side + '≥' + r.rounds + '轮').join(', ') || '关闭'));
|
||||
.map(r => r.tool + '/' + r.side + '(轮≥' + r.rounds + ',回合≥' + r.turns + ')').join(', ') || '关闭'));
|
||||
const db = initDb(cfg.db_path);
|
||||
const bus = createBus();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user