Files
agent/app/server/public/memory.html
T
kicer ac399ed796 refactor(memory): C 结构重构——memory_entries blob → memory_fragments 一片一行 + 事件日语义(含 B)
用户确认 blob 拼接莫名其妙、C 值得做;顺带把 B(日期取事件日)并入,一次到位。

结构:memory_fragments(user, session_id PK, day, text, created_at)。
- (user, session_id) 主键 = 「每会话唯一碎片」的存储级保证(原 LIKE sid 文本去重、dupe
  三态语义全部消失;sessions.journaled_at 保留作扫描预排除);
- day = 事件日(会话最后活跃日),不再记「收口当日」——人回忆问哪天干的活,
  52 条积压碎片迁移后自动散布回 09-16~09-29 十一天真实日期;
- text = 纯日记正文(- 行),【来源会话 id】只在渲染层拼(journalFor/search/day 输出),
  存储零冗余元数据;FRAG_MAX 保留。

迁移(db.js,幂等):旧表存在才搬——按空行拆块、正则提取 task- id(兼容三代头格式),
解析不出的孤儿残块(DAY_MAX 时代遗物)丢弃,day 回填来源会话 updated_at,搬完 DROP。
线上实测:52 片全量搬家、孤儿 0、旧表已删。

memory.js 读写全换皮:journalFor(created_at 倒序取 inject_max、按天分组天内正序)、
searchFragments(逐行打分同前)、daysIndex(GROUP BY day)、dayEntry(WHERE day)、
prune(retain_days>0 才删)。engine 两处收口只改 day 来源与去【sid】头;
调试 API/页面按一片一卡 + 已注入徽章(= 全局最新 inject_max 片)。

探针 13 项全过:legacy 迁移(孤儿弃/事件日回填/text 零元数据)、死绑定候选链收口、
主键去重、注入窗、recall search/days/day 对新表全绿。
2026-09-29 15:32:02 +08:00

129 lines
7.0 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>长期记忆 · 调试</title>
<style>
body { margin: 0; padding: 20px 16px 60px; background: #f4f6fa; color: #2b3441;
font: 14px/1.65 -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif; }
.wrap { max-width: 860px; margin: 0 auto; }
h1 { font-size: 18px; margin: 0 0 4px; }
.meta { color: #7a8699; font-size: 12px; margin-bottom: 14px; }
.meta b { color: #2b3441; font-weight: 600; }
.badge { display: inline-block; padding: 1px 8px; border-radius: 10px; font-size: 12px; margin-left: 6px; }
.badge.on { background: #e3f2e6; color: #1c7a2e; }
.badge.off { background: #fdecea; color: #b3382c; }
.badge.out { background: #eceff5; color: #7a8699; }
button { border: 1px solid #cdd5e0; background: #fff; color: #2b3441; border-radius: 8px;
padding: 5px 14px; font-size: 13px; cursor: pointer; }
button:active { background: #eef1f6; }
h2 { font-size: 15px; margin: 26px 0 10px; padding-left: 8px; border-left: 3px solid #4c6ef5; }
.card { background: #fff; border: 1px solid #e3e8f0; border-radius: 10px; padding: 12px 14px; margin-bottom: 10px; }
.day { font-weight: 600; font-size: 14px; }
.day .t { color: #9aa4b2; font-weight: 400; font-size: 12px; margin-left: 8px; }
.frag { margin-top: 8px; padding: 8px 10px; background: #f8fafc; border: 1px solid #eef1f6; border-radius: 8px; white-space: pre-wrap; word-break: break-all; }
.frag .sid a { color: #4c6ef5; text-decoration: none; font-size: 12px; }
.inj-tag { display: block; width: fit-content; margin: 0 0 6px; }
pre.inj { background: #fff; border: 1px solid #e3e8f0; border-radius: 10px; padding: 12px 14px;
white-space: pre-wrap; word-break: break-all; margin: 0; font: 13px/1.7 ui-monospace, Menlo, monospace; }
table { width: 100%; border-collapse: collapse; background: #fff; border: 1px solid #e3e8f0; border-radius: 10px; overflow: hidden; }
th, td { padding: 7px 10px; text-align: left; font-size: 12.5px; border-bottom: 1px solid #eef1f6; }
th { background: #f8fafc; color: #7a8699; font-weight: 500; }
tr:last-child td { border-bottom: 0; }
td a { color: #4c6ef5; text-decoration: none; }
.empty { color: #9aa4b2; padding: 14px 2px; }
.st { font-size: 12px; }
.st.held { color: #8a6400; } .st.completed { color: #1c7a2e; } .st.active { color: #4c6ef5; } .st.cancelled { color: #b3382c; }
</style>
</head>
<body>
<div class="wrap">
<h1>长期记忆 · 日记调试台
<button style="float:right;margin-top:2px" onclick="load()">刷新</button>
</h1>
<div class="meta" id="meta">加载中…</div>
<h2>注入预览(新会话实际带上的文本,与 journalFor 同源)</h2>
<pre class="inj" id="injection">—</pre>
<h2>碎片全量(memory_fragments 一片一行)</h2>
<div id="entries"></div>
<h2>已收口会话(journaled_at 非空,与碎片对照)</h2>
<div id="sessions"></div>
</div>
<script>
'use strict';
function esc(s) { return String(s == null ? '' : s).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;'); }
function fmtTime(ms) {
if (!ms) return '-';
const d = new Date(Number(ms)), p = n => String(n).padStart(2, '0');
return d.getFullYear() + '-' + p(d.getMonth() + 1) + '-' + p(d.getDate()) + ' ' + p(d.getHours()) + ':' + p(d.getMinutes());
}
function fmtMs(ms) { return ms >= 3600000 ? (ms / 3600000).toFixed(ms % 3600000 ? 1 : 0) + ' 小时' : Math.round(ms / 60000) + ' 分钟'; }
// 碎片行 = memory_fragments 一行一片;session_id 是独立列,渲染成可点链接(新开标签看该会话原始 JSON,溯源用)
function sidLink(sid) {
return '【<a href="/api/sessions/' + encodeURIComponent(sid) + '" target="_blank">' + esc(sid) + '</a>】';
}
function render(d) {
var meta = document.getElementById('meta');
meta.innerHTML = '用户 <b>' + (d.user ? esc(d.user) : '<i>(未启用登录 · 隐式单用户)</i>') + '</b>'
+ '<span class="badge ' + (d.inject_max ? 'on">注入最近 ' + d.inject_max + ' 条' : 'off">注入已关(仍记录)') + '</span>'
+ ' · 存储 <b>' + (d.retain_days ? '保留 ' + d.retain_days + ' 天' : '永久') + '</b>'
+ ' · 空闲收口 <b>' + (d.wrap_idle_ms ? fmtMs(d.wrap_idle_ms) : '关') + '</b>'
+ ' · held 归档 <b>' + (d.auto_complete_ms ? fmtMs(d.auto_complete_ms) : '关') + '</b>';
document.getElementById('injection').textContent = d.injection || '(无注入文本:无记忆或功能关闭)';
var frs = d.entries || []; // created_at 倒序的全量碎片行
// 注入窗 = 按收口时刻取最新 inject_max 片(与 journalFor 选取规则一致),直接标在卡上。
var injected = {};
for (var ii = 0; ii < Math.min(d.inject_max || 0, frs.length); ii++) injected[ii] = 1;
var groups = {}, order = [];
frs.forEach(function (f, fi) {
if (!groups[f.day]) { groups[f.day] = []; order.push(f.day); }
groups[f.day].push({ f: f, fi: fi });
});
order.sort(function (a, b) { return a < b ? 1 : -1; }); // 新日在前
var cutoffStale = !d.retain_days ? '' : '(保留 ' + d.retain_days + ' 天)';
var eHtml = order.map(function (day) {
var items = groups[day];
return '<div class="card"><div class="day">' + esc(day)
+ (items[0].f.in_window ? '' : '<span class="badge out">窗外' + cutoffStale + ')</span>')
+ '<span class="t">' + items.length + ' 片</span></div>'
+ items.map(function (it) {
return '<div class="frag">' + (injected[it.fi] ? '<span class="badge on inj-tag">已注入</span>' : '')
+ sidLink(it.f.session_id) + ' <span class="t">收口 ' + fmtTime(it.f.created_at) + ' · 事件日 ' + esc(it.f.day) + '</span>\n' + esc(it.f.text)
+ '</div>';
}).join('')
+ '</div>';
}).join('');
document.getElementById('entries').innerHTML = eHtml || '<div class="empty">暂无条目——等一次收口,或先在微信/网页聊几句。</div>';
var sHtml = '';
(d.sessions || []).forEach(function (s) {
sHtml += '<tr><td><a href="/api/sessions/' + encodeURIComponent(s.id) + '" target="_blank">' + esc(s.id) + '</a></td>'
+ '<td>' + esc(s.title || '(未命名)') + '</td>'
+ '<td><span class="st ' + esc(s.status) + '">' + esc(s.status) + '</span></td>'
+ '<td>' + esc(s.model_id || '-') + '</td>'
+ '<td>' + fmtTime(s.journaled_at) + '</td></tr>';
});
document.getElementById('sessions').innerHTML = sHtml
? '<table><tr><th>会话</th><th>标题</th><th>状态</th><th>模型</th><th>收口时间</th></tr>' + sHtml + '</table>'
: '<div class="empty">暂无已收口会话。</div>';
}
function load() {
fetch('/api/memory').then(function (r) { return r.json(); }).then(function (d) {
if (d.error) { document.getElementById('meta').textContent = '加载失败: ' + d.error; return; }
render(d);
}).catch(function (e) { document.getElementById('meta').textContent = '加载失败: ' + e.message; });
}
load();
</script>
</body>
</html>