Files
agent/app/server/public/memory.html
T
kicer d714e90024 fix(memory): 「已注入」标记单点化——injectedIds 与 journalFor 同源选取,页面吃名单不另算
线上现象(用户实测):调试页「已注入」标在了最早 3 条上。根因是页面按 API 位置序
(created_at 倒序前 N)自算注入窗,而 journalFor 已改事件日优先——迁移又让 52 片
created_at 齐平(旧行 updated_at 单值),位置序退化成 rowid 序=搬家序=最老片,两边错位。

修复:memory.injectedIds() 与 journalFor 共用同一条选取规则(INJECT_ORDER:
day DESC, created_at DESC LIMIT inject_max),API 返回 injected 名单,页面按
session_id 贴标;journalFor 内部迁移兜底的 JS 重排一并清除(SQL 已保序)。
2026-09-29 15:56:13 +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 || []; // API 已按 day 降序、同日收口时刻降序(与注入选取同源)
// 「已注入」直接吃 API 给的名单(memory.injectedIds,与 journalFor 同序),页面不再自算窗。
var injected = {};
(d.injected || []).forEach(function (sid) { injected[sid] = 1; });
var groups = {}, order = [];
frs.forEach(function (f) {
if (!groups[f.day]) { groups[f.day] = []; order.push(f.day); }
groups[f.day].push({ f: f });
});
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.f.session_id] ? '<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>