diff --git a/app/server/llm.js b/app/server/llm.js index 8aa5cb4..38aae49 100644 --- a/app/server/llm.js +++ b/app/server/llm.js @@ -330,8 +330,8 @@ function setPayloadTrim(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 合法。 +// write_file 参数占位:只洗 content。AI 友好记号风格:键值对、不写整句——path 不必重复, +// 本条 arguments 里就还在;改写的是已 parse 副本,DB 行不动,重新 stringify 保持 JSON 合法。 function trimWriteCalls(calls, rounds) { if (rounds < TRIM.writeRounds) return; for (const c of calls || []) { @@ -339,25 +339,23 @@ function trimWriteCalls(calls, rounds) { 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 + ' 字]'; + args.content = '[OMITTED, size=' + args.content.length + ']'; c.function.arguments = JSON.stringify(args); } } -// fetch/read_file 回执占位:url/path 从对应 tool_call 参数回填(回执行自身不存调用目标), -// 占位行里留目标就是留恢复指针。图片回执(blocks 非空)不碰:已有自己的消费省略逻辑。 +// fetch/read_file 回执占位:同款键值记号。url/path 不进占位——发起调用的 assistant 行仍载着 +// 它们,参数本身就是恢复指针。图片回执(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 '[OMITTED' + (hm ? ', http_status=' + hm[1] : '') + ', size=' + content.length + ']'; } - return '[read_file 省略:' + String(arg.path || '?') + '|原 ' + content.length + ' 字]'; + return '[OMITTED, size=' + content.length + ']'; } // 拼压缩回合的 messages(参考 titleMessages 的拼法):默认 system prompt + 正常历史 diff --git a/app/server/memory.js b/app/server/memory.js index 32c694f..e5c3854 100644 --- a/app/server/memory.js +++ b/app/server/memory.js @@ -86,14 +86,13 @@ function journalFor(db, cfg, user) { 'SELECT session_id, day, text FROM memory_fragments WHERE user = ?' + INJECT_ORDER + ' LIMIT ?' ).all(String(user || ''), cap); if (!rows.length) return ''; - const head = '【长期记忆·日记(最近' + rows.length + '条,非全部——更早的可用 recall 检索)】新在前、天内按发生顺序;每条【编号】是来源会话 id。'; const groups = []; for (const r of rows) { const g = groups[groups.length - 1]; if (!g || g.day !== r.day) groups.push({ day: r.day, items: [renderFrag(r)] }); else g.items.unshift(renderFrag(r)); } - return [head].concat(groups.map(g => '=== ' + g.day + ' ===\n' + g.items.join('\n\n'))).join('\n\n'); + return groups.map(g => '=== ' + g.day + ' ===\n' + g.items.join('\n\n')).join('\n\n'); } // ---------- 读:关键词检索(recall 主入口:用户记起的是「一段事」不是「第几条」) ---------- diff --git a/app/server/user-profile.js b/app/server/user-profile.js index 594e5dd..a486959 100644 --- a/app/server/user-profile.js +++ b/app/server/user-profile.js @@ -20,7 +20,7 @@ const SET_MAX = 20; // AI 写 honorific/industry 的单值帽(称谓应当 const TAG_MAX = 15; // 单个偏好标签长度帽 const TAGS_MAX = 24; // 偏好标签数量帽 const TAG_SPLIT = /[、,,;;\n]+/; -const SNAPSHOT_HEAD = '【用户画像 · 背景参考】以下为历史会话沉淀的粗粒度认知,非任务约束;与当前请求冲突时,一律以当前请求为准。'; +const SNAPSHOT_HEAD = '【用户画像】以下为历史会话沉淀的粗粒度认知,非任务约束;与当前请求冲突时,一律以当前请求为准。'; const FIELD_LABELS = { honorific: '称谓', industry: '行业', preference: '偏好' }; function readRow(db, user) { diff --git a/app/test/trim-smoke.js b/app/test/trim-smoke.js index 45516d5..44b1745 100644 --- a/app/test/trim-smoke.js +++ b/app/test/trim-smoke.js @@ -22,11 +22,11 @@ const session = () => ({ history: rows.slice(), profile_snapshot: '', blocksCach 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); +assert(argsNotes.content === '[OMITTED, size=12000]', '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(t.content === '[OMITTED, http_status=200, size=8009]', 'fetch 回执应占位: ' + t.content); assert(msgs.find(m => m.role === 'tool' && m.tool_call_id === 'c2').content.includes('已创建文件'), 'write 回执不动'); // B) 截到第 5 行(write 刚落盘的当次调用):都太新鲜,不裁 @@ -43,7 +43,7 @@ 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 应占位'); +assert(msgs.find(m => m.tool_call_id === 'c1' && m.role === 'tool').content === '[OMITTED, size=8009]', 'read_file 应占位'); rows[2] = r3orig; rows[1] = mk(2, 'assistant', { tool_calls: JSON.stringify([call('c1', 'fetch', { url: 'https://e.com/x' })]) }); @@ -59,6 +59,6 @@ rows.push(mk(10, 'tool', { tool_call_id: 'c9', name: 'fetch', content: 'HTTP 404 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); +assert(msgs.find(m => m.tool_call_id === 'c9').content === '[OMITTED, http_status=404, size=709]', '缺 url 仍占位: ' + msgs.find(m => m.tool_call_id === 'c9').content); console.log('TRIM-SMOKE-OK');