cortico-world-memory-core 0.1.1 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +38 -0
- package/ENV_PROMPT.md +1 -1
- package/README.md +29 -4
- package/assets/screenshots/graph.png +0 -0
- package/assets/screenshots/maintenance.png +0 -0
- package/assets/screenshots/table.png +0 -0
- package/assets/screenshots/timeline.png +0 -0
- package/describe.json +264 -0
- package/icon.svg +32 -0
- package/package.json +72 -49
- package/src/config.ts +19 -4
- package/src/recall.ts +6 -2
- package/src/site.ts +1 -0
- package/src/store.ts +211 -16
- package/src/tools.ts +7 -2
- package/src/world.ts +151 -31
- package/webui/app.js +141 -44
- package/webui/style.css +7 -0
package/webui/app.js
CHANGED
|
@@ -10,6 +10,9 @@ const KINDS = [
|
|
|
10
10
|
{ id: 'note', label: '临时', css: '--k-note' },
|
|
11
11
|
];
|
|
12
12
|
|
|
13
|
+
/** 原文页一次渲染多少条。7 天窗口实测稳定在 1000 条上下,全量拼 DOM 会卡。 */
|
|
14
|
+
const TURNS_PAGE = 200;
|
|
15
|
+
|
|
13
16
|
const S = {
|
|
14
17
|
snap: null,
|
|
15
18
|
view: 'graph',
|
|
@@ -30,6 +33,7 @@ const S = {
|
|
|
30
33
|
status: 'active', // active | all | retired
|
|
31
34
|
scope: null,
|
|
32
35
|
maintenance: null,
|
|
36
|
+
turnsLimit: TURNS_PAGE,
|
|
33
37
|
};
|
|
34
38
|
|
|
35
39
|
const el = {
|
|
@@ -312,7 +316,7 @@ function renderMetrics() {
|
|
|
312
316
|
{ label: '今天新增', value: st.todayNew, unit: '条' },
|
|
313
317
|
{ label: '近 7 天', value: st.weekNew, unit: '条' },
|
|
314
318
|
{ label: '平均权重', value: st.avgWeight.toFixed(2), unit: '' },
|
|
315
|
-
{ label: '对话原文', value: (S.snap.turns ?? []).length, unit: '行' },
|
|
319
|
+
{ label: '对话原文', value: (S.snap.turns ?? []).length, unit: S.snap.archive?.days ? `行 · 归档 ${S.snap.archive.days} 天` : '行' },
|
|
316
320
|
{ label: '工作区文件', value: S.snap.docs?.count ?? 0, unit: '个', tone: S.snap.docs?.enabled ? '' : '' },
|
|
317
321
|
];
|
|
318
322
|
el.metrics.innerHTML =
|
|
@@ -655,6 +659,29 @@ class Graph {
|
|
|
655
659
|
links.push({ a: group[0], b: group[i], w: 1, tag });
|
|
656
660
|
}
|
|
657
661
|
}
|
|
662
|
+
// 版本链:新卡 supersedes 旧卡。这条边比标签共现更有信息量,所以优先级更高——
|
|
663
|
+
// 两卡之间如果已经有标签边,就把它升级成版本链,不再画第二条重合的线。
|
|
664
|
+
// 方向按时间流:旧 → 新,箭头指向新卡。
|
|
665
|
+
const pairKey = (x, y) => (x.id < y.id ? `${x.id}|${y.id}` : `${y.id}|${x.id}`);
|
|
666
|
+
const seen = new Map(links.map((l) => [pairKey(l.a, l.b), l]));
|
|
667
|
+
for (const n of nodes) {
|
|
668
|
+
for (const oldId of n.card.supersedes ?? []) {
|
|
669
|
+
const target = index.get(oldId);
|
|
670
|
+
if (!target) continue;
|
|
671
|
+
const key = pairKey(target, n);
|
|
672
|
+
const exist = seen.get(key);
|
|
673
|
+
if (exist) {
|
|
674
|
+
exist.kind = 'supersede';
|
|
675
|
+
exist.w = 1.2;
|
|
676
|
+
exist.a = target;
|
|
677
|
+
exist.b = n;
|
|
678
|
+
} else {
|
|
679
|
+
const l = { a: target, b: n, w: 1.2, tag: null, kind: 'supersede' };
|
|
680
|
+
links.push(l);
|
|
681
|
+
seen.set(key, l);
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
}
|
|
658
685
|
// 没有标签连线的节点,按同一天挂到最近的一个节点上,避免孤岛
|
|
659
686
|
const linked = new Set(links.flatMap((l) => [l.a.id, l.b.id]));
|
|
660
687
|
for (const n of nodes) {
|
|
@@ -798,35 +825,64 @@ class Graph {
|
|
|
798
825
|
if (l.b.id === this.hover) hoverNeighbors.add(l.a.id);
|
|
799
826
|
}
|
|
800
827
|
}
|
|
828
|
+
// 版本链两端的节点一定要带标签,否则「被取代的是什么」看不出来
|
|
829
|
+
const chainIds = new Set();
|
|
830
|
+
for (const l of this.links) {
|
|
831
|
+
if (l.kind !== 'supersede') continue;
|
|
832
|
+
chainIds.add(l.a.id);
|
|
833
|
+
chainIds.add(l.b.id);
|
|
834
|
+
}
|
|
801
835
|
|
|
802
|
-
const
|
|
836
|
+
const P = palette();
|
|
837
|
+
const ink = P.ink;
|
|
838
|
+
const line = P.line;
|
|
803
839
|
for (const l of this.links) {
|
|
804
840
|
const dim = this.hover && l.a.id !== this.hover && l.b.id !== this.hover;
|
|
805
|
-
|
|
806
|
-
ctx.
|
|
807
|
-
ctx.
|
|
841
|
+
const sup = l.kind === 'supersede';
|
|
842
|
+
ctx.strokeStyle = sup ? ink : line;
|
|
843
|
+
ctx.globalAlpha = dim ? 0.07 : sup ? 0.45 : 0.16 + l.w * 0.22;
|
|
844
|
+
ctx.lineWidth = (sup ? 1.5 : 0.8 + l.w * 1.1) / k;
|
|
845
|
+
ctx.setLineDash(sup ? [4 / k, 3 / k] : []);
|
|
808
846
|
ctx.beginPath();
|
|
809
847
|
ctx.moveTo(l.a.x, l.a.y);
|
|
810
848
|
ctx.lineTo(l.b.x, l.b.y);
|
|
811
849
|
ctx.stroke();
|
|
850
|
+
if (sup) {
|
|
851
|
+
// 箭头指向新卡:这条线读作「旧的那条演进成了它」
|
|
852
|
+
const ang = Math.atan2(l.b.y - l.a.y, l.b.x - l.a.x);
|
|
853
|
+
const r = l.b.r + 3 / k;
|
|
854
|
+
const tipX = l.b.x - Math.cos(ang) * r;
|
|
855
|
+
const tipY = l.b.y - Math.sin(ang) * r;
|
|
856
|
+
const size = 5 / k;
|
|
857
|
+
ctx.setLineDash([]);
|
|
858
|
+
ctx.beginPath();
|
|
859
|
+
ctx.moveTo(tipX, tipY);
|
|
860
|
+
ctx.lineTo(tipX - Math.cos(ang - 0.42) * size, tipY - Math.sin(ang - 0.42) * size);
|
|
861
|
+
ctx.lineTo(tipX - Math.cos(ang + 0.42) * size, tipY - Math.sin(ang + 0.42) * size);
|
|
862
|
+
ctx.closePath();
|
|
863
|
+
ctx.fillStyle = ink;
|
|
864
|
+
ctx.fill();
|
|
865
|
+
}
|
|
812
866
|
}
|
|
867
|
+
ctx.setLineDash([]);
|
|
813
868
|
ctx.globalAlpha = 1;
|
|
814
869
|
|
|
815
|
-
const P = palette();
|
|
816
|
-
const ink = P.ink;
|
|
817
870
|
for (const n of this.nodes) {
|
|
818
871
|
const c = P.kinds[n.card.kind] ?? ink;
|
|
819
872
|
const isSel = n.card.id === S.selected;
|
|
820
873
|
const isHover = n.card.id === this.hover;
|
|
821
874
|
const dim = this.hover && !isHover && !hoverNeighbors.has(n.card.id) && !isSel;
|
|
822
|
-
|
|
875
|
+
// 被版本链补进来的旧卡已经退休,压暗一档,别让人以为它还在用
|
|
876
|
+
const retired = n.card.status === 'retired';
|
|
877
|
+
const baseAlpha = dim ? 0.28 : retired ? 0.5 : 1;
|
|
878
|
+
ctx.globalAlpha = baseAlpha;
|
|
823
879
|
if (isSel || isHover) {
|
|
824
880
|
ctx.beginPath();
|
|
825
881
|
ctx.arc(n.x, n.y, n.r + 5 / k, 0, Math.PI * 2);
|
|
826
882
|
ctx.fillStyle = c;
|
|
827
883
|
ctx.globalAlpha = 0.16;
|
|
828
884
|
ctx.fill();
|
|
829
|
-
ctx.globalAlpha =
|
|
885
|
+
ctx.globalAlpha = baseAlpha;
|
|
830
886
|
}
|
|
831
887
|
ctx.beginPath();
|
|
832
888
|
ctx.arc(n.x, n.y, n.r, 0, Math.PI * 2);
|
|
@@ -838,9 +894,10 @@ class Graph {
|
|
|
838
894
|
ctx.stroke();
|
|
839
895
|
}
|
|
840
896
|
const showLabel =
|
|
841
|
-
isSel || isHover
|
|
897
|
+
isSel || isHover
|
|
898
|
+
|| (S.showLabels && (chainIds.has(n.card.id) || k > 1.32 || this.nodes.length <= 16));
|
|
842
899
|
if (showLabel) {
|
|
843
|
-
ctx.globalAlpha = dim ? 0.3 : 0.95;
|
|
900
|
+
ctx.globalAlpha = dim ? 0.3 : retired ? 0.55 : 0.95;
|
|
844
901
|
const size = Math.max(9.5, 11.5 / k);
|
|
845
902
|
ctx.font = `${size}px -apple-system, "PingFang SC", sans-serif`;
|
|
846
903
|
ctx.textAlign = 'center';
|
|
@@ -865,8 +922,10 @@ function renderGraph() {
|
|
|
865
922
|
if (!graph) graph = new Graph(canvas);
|
|
866
923
|
graph.onSelect = (id) => selectCard(id);
|
|
867
924
|
graph.showLabels = S.showLabels;
|
|
868
|
-
const
|
|
869
|
-
|
|
925
|
+
const base = filtered();
|
|
926
|
+
// 版本链的另一头通常是已退休的旧卡,默认筛选看不到——补进图里,边才画得出来
|
|
927
|
+
const list = withSuperseded(base);
|
|
928
|
+
$('#graph-empty').hidden = base.length > 0;
|
|
870
929
|
graph.draw();
|
|
871
930
|
// 结构变化才重排;只改选中不重排
|
|
872
931
|
const signature = list.map((c) => c.id).join(',');
|
|
@@ -884,9 +943,28 @@ function renderLegend() {
|
|
|
884
943
|
const box = $('#graph-legend');
|
|
885
944
|
const counts = {};
|
|
886
945
|
for (const c of filtered()) counts[c.kind] = (counts[c.kind] || 0) + 1;
|
|
946
|
+
const hasChain = withSuperseded(filtered()).some((c) => (c.supersedes ?? []).length);
|
|
887
947
|
box.innerHTML =
|
|
888
948
|
'<span style="color:var(--ink-dim)">大小=权重</span>' +
|
|
889
|
-
KINDS.filter((k) => counts[k.id]).map((k) => `<span><i style="background:var(${k.css})"></i>${k.label} ${counts[k.id]}</span>`).join('')
|
|
949
|
+
KINDS.filter((k) => counts[k.id]).map((k) => `<span><i style="background:var(${k.css})"></i>${k.label} ${counts[k.id]}</span>`).join('') +
|
|
950
|
+
(hasChain ? '<span style="color:var(--ink-dim)">虚线箭头=被新版本接替</span>' : '');
|
|
951
|
+
}
|
|
952
|
+
|
|
953
|
+
/** 把被在用卡 supersedes 掉的旧卡补进图。它们默认已退休、不在筛选结果里,
|
|
954
|
+
* 但版本链恰恰是这个视图最该看得见的关系。 */
|
|
955
|
+
function withSuperseded(list) {
|
|
956
|
+
const ids = new Set(list.map((c) => c.id));
|
|
957
|
+
const extra = [];
|
|
958
|
+
for (const c of list) {
|
|
959
|
+
for (const oldId of c.supersedes ?? []) {
|
|
960
|
+
if (ids.has(oldId)) continue;
|
|
961
|
+
const old = cards().find((x) => x.id === oldId);
|
|
962
|
+
if (!old) continue;
|
|
963
|
+
ids.add(oldId);
|
|
964
|
+
extra.push(old);
|
|
965
|
+
}
|
|
966
|
+
}
|
|
967
|
+
return extra.length ? [...list, ...extra] : list;
|
|
890
968
|
}
|
|
891
969
|
|
|
892
970
|
$('#graph-fit').addEventListener('click', () => graph?.fit());
|
|
@@ -1037,25 +1115,40 @@ function renderTable() {
|
|
|
1037
1115
|
function renderTurns() {
|
|
1038
1116
|
const host = $('#view-turns');
|
|
1039
1117
|
const toks = tokens(S.query);
|
|
1040
|
-
const
|
|
1041
|
-
if (!
|
|
1118
|
+
const matched = (S.snap?.turns ?? []).filter((t) => matches(t.text, toks)).slice().reverse();
|
|
1119
|
+
if (!matched.length) {
|
|
1120
|
+
const dbg = S.snap?.state?.captureDebug;
|
|
1121
|
+
let hint = '在插件设置里确认「存对话原文」是开的,聊几句就会出现在这里。';
|
|
1122
|
+
if (dbg && dbg.scanned && !dbg.kept) {
|
|
1123
|
+
const keys = (dbg.keys ?? []).map((k) => esc(k)).join('、') || '无';
|
|
1124
|
+
hint = `上次扫了 ${dbg.scanned} 条外部事件,一条都没留下——被「算作对话的来源」白名单挡掉了(样例:${keys}),把这几个 key 加进设置里的正则就行。`;
|
|
1125
|
+
}
|
|
1042
1126
|
host.innerHTML = `<div class="turns-wrap"><div class="empty"><p>${S.query ? '没有匹配的对话。' : '还没有存下对话原文。'}</p><p class="dim">${
|
|
1043
|
-
S.query ? '' :
|
|
1127
|
+
S.query ? '' : hint
|
|
1044
1128
|
}</p></div></div>`;
|
|
1045
1129
|
return;
|
|
1046
1130
|
}
|
|
1131
|
+
// 全量拼 DOM 在 1000 条时会明显卡:先渲染最近 200 条,要更多再点。
|
|
1132
|
+
// 分组按匹配总数算,组头显示「已显示/该天总数」,不让人误以为那天只有这么几句。
|
|
1047
1133
|
const groups = new Map();
|
|
1048
|
-
for (const t of
|
|
1134
|
+
for (const t of matched) {
|
|
1049
1135
|
const key = dayKey(t.ts);
|
|
1050
1136
|
if (!groups.has(key)) groups.set(key, []);
|
|
1051
1137
|
groups.get(key).push(t);
|
|
1052
1138
|
}
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1139
|
+
const limit = Math.max(TURNS_PAGE, Math.min(S.turnsLimit ?? TURNS_PAGE, matched.length));
|
|
1140
|
+
let remain = limit;
|
|
1141
|
+
const parts = [];
|
|
1142
|
+
for (const [key, items] of groups) {
|
|
1143
|
+
if (remain <= 0) break;
|
|
1144
|
+
const take = items.slice(0, remain);
|
|
1145
|
+
remain -= take.length;
|
|
1146
|
+
parts.push(`
|
|
1056
1147
|
<div class="day-group">
|
|
1057
|
-
<div class="day-head"><b>${dayLabel(key)}</b><span>${
|
|
1058
|
-
|
|
1148
|
+
<div class="day-head"><b>${dayLabel(key)}</b><span>${
|
|
1149
|
+
take.length === items.length ? `${items.length} 条` : `${take.length} / ${items.length} 条`
|
|
1150
|
+
}</span></div>
|
|
1151
|
+
${take
|
|
1059
1152
|
.map(
|
|
1060
1153
|
(t) => `<div class="turn">
|
|
1061
1154
|
<div><time>${fmtTime(t.ts).slice(6)}</time><div class="who">${esc(shortSender(t.sender))}</div></div>
|
|
@@ -1063,15 +1156,24 @@ function renderTurns() {
|
|
|
1063
1156
|
</div>`,
|
|
1064
1157
|
)
|
|
1065
1158
|
.join('')}
|
|
1066
|
-
</div
|
|
1067
|
-
|
|
1068
|
-
|
|
1159
|
+
</div>`);
|
|
1160
|
+
}
|
|
1161
|
+
const more =
|
|
1162
|
+
matched.length > limit
|
|
1163
|
+
? `<button class="btn turns-more" id="turns-more">还有 ${matched.length - limit} 条 · 继续加载</button>`
|
|
1164
|
+
: '';
|
|
1165
|
+
host.innerHTML = `<div class="turns-wrap">${parts.join('')}${more}</div>`;
|
|
1166
|
+
$('#turns-more')?.addEventListener('click', () => {
|
|
1167
|
+
S.turnsLimit = limit + TURNS_PAGE;
|
|
1168
|
+
renderTurns();
|
|
1169
|
+
});
|
|
1069
1170
|
}
|
|
1070
1171
|
|
|
1071
1172
|
function shortSender(key) {
|
|
1072
|
-
const
|
|
1073
|
-
const base =
|
|
1074
|
-
|
|
1173
|
+
const raw = String(key ?? '');
|
|
1174
|
+
const base = raw.split(/[.:]/)[0];
|
|
1175
|
+
const map = { starpost: '星邮', qq: 'QQ', private: 'QQ 私聊', 'desktop-pet': '桌宠' };
|
|
1176
|
+
return map[base] ?? (raw || '—').slice(0, 12);
|
|
1075
1177
|
}
|
|
1076
1178
|
|
|
1077
1179
|
/* ── 工作区文件 ─────────────────────────────────────── */
|
|
@@ -1192,7 +1294,6 @@ function renderInspector() {
|
|
|
1192
1294
|
? `<button class="btn tiny" id="btn-restore">恢复这条</button>`
|
|
1193
1295
|
: `<button class="btn tiny" id="btn-retire">退休</button>`
|
|
1194
1296
|
}
|
|
1195
|
-
<button class="btn tiny danger" id="btn-delete">彻底删除</button>
|
|
1196
1297
|
</div>
|
|
1197
1298
|
${lineageHtml(card)}
|
|
1198
1299
|
${
|
|
@@ -1229,18 +1330,7 @@ function renderInspector() {
|
|
|
1229
1330
|
toast('复制失败,浏览器不给剪贴板权限', true);
|
|
1230
1331
|
}
|
|
1231
1332
|
});
|
|
1232
|
-
|
|
1233
|
-
if (!confirm(`彻底删掉这条?删了就找不回来了。\n\n${card.text}`)) return;
|
|
1234
|
-
try {
|
|
1235
|
-
await api('/api/purge', { method: 'POST', body: JSON.stringify({ ids: [card.id] }) });
|
|
1236
|
-
S.selected = null;
|
|
1237
|
-
setInspCollapsed(true);
|
|
1238
|
-
toast('已彻底删除');
|
|
1239
|
-
await load();
|
|
1240
|
-
} catch (err) {
|
|
1241
|
-
toast(`删除失败:${err.message}`, true);
|
|
1242
|
-
}
|
|
1243
|
-
});
|
|
1333
|
+
// 彻底删除只在维护面板 → 回收站里给:详情栏紧挨「退休」,误触一次就没了
|
|
1244
1334
|
const retireBtn = $('#btn-retire');
|
|
1245
1335
|
if (retireBtn) {
|
|
1246
1336
|
retireBtn.addEventListener('click', async () => {
|
|
@@ -1589,15 +1679,22 @@ document.addEventListener('keydown', (event) => {
|
|
|
1589
1679
|
}
|
|
1590
1680
|
});
|
|
1591
1681
|
|
|
1682
|
+
// 逐字 render 会把原文 1000+ 条、表格几十行整体重建,打字快了肉眼可见地卡。
|
|
1683
|
+
// 输入框状态立刻更新(清除按钮要跟手),重绘压到 150ms 后。
|
|
1684
|
+
let searchTimer = 0;
|
|
1592
1685
|
el.search.addEventListener('input', () => {
|
|
1593
1686
|
S.query = el.search.value;
|
|
1594
1687
|
S.searchClear.hidden = !S.query;
|
|
1595
|
-
|
|
1688
|
+
S.turnsLimit = TURNS_PAGE;
|
|
1689
|
+
clearTimeout(searchTimer);
|
|
1690
|
+
searchTimer = setTimeout(() => render(), 150);
|
|
1596
1691
|
});
|
|
1597
1692
|
el.searchClear.addEventListener('click', () => {
|
|
1598
1693
|
S.query = '';
|
|
1599
1694
|
el.search.value = '';
|
|
1600
1695
|
S.searchClear.hidden = true;
|
|
1696
|
+
S.turnsLimit = TURNS_PAGE;
|
|
1697
|
+
clearTimeout(searchTimer);
|
|
1601
1698
|
render();
|
|
1602
1699
|
});
|
|
1603
1700
|
|
|
@@ -1691,7 +1788,7 @@ function renderMaintenance(tab) {
|
|
|
1691
1788
|
const list = rows(m.stale);
|
|
1692
1789
|
content = list.length
|
|
1693
1790
|
? list.map((c) => maintRow(c, `<button class="btn tiny" data-retire="${c.id}">退休</button>`)).join('')
|
|
1694
|
-
: '<p class="maint-empty">没有疑似作废的记忆。文案里带「取消了 / 改到 /
|
|
1791
|
+
: '<p class="maint-empty">没有疑似作废的记忆。文案里带「取消了 / 改到 / 作废」,或者 90 天没被召回、权重又低的,会出现在这里。</p>';
|
|
1695
1792
|
if (list.length) bulk = `<button class="btn tiny" data-bulk="stale">全部退休(${list.length})</button>`;
|
|
1696
1793
|
} else if (tab === 'junk') {
|
|
1697
1794
|
const list = rows(m.junk);
|
package/webui/style.css
CHANGED
|
@@ -419,6 +419,13 @@ td .tag-list { max-height: 40px; overflow: hidden; }
|
|
|
419
419
|
|
|
420
420
|
/* 原文 / 文件 */
|
|
421
421
|
.turns-wrap, .docs-wrap { height: 100%; overflow-y: auto; padding: 14px 18px 40px; }
|
|
422
|
+
.turns-more {
|
|
423
|
+
display: block; width: 100%; margin: 6px 0 18px; padding: 9px;
|
|
424
|
+
font: inherit; font-size: 12px; color: var(--ink-dim);
|
|
425
|
+
background: transparent; border: 1px dashed var(--line-strong);
|
|
426
|
+
border-radius: 8px; cursor: pointer;
|
|
427
|
+
}
|
|
428
|
+
.turns-more:hover { color: var(--ink); border-color: var(--ink-dim); }
|
|
422
429
|
.turn { display: grid; grid-template-columns: 84px minmax(0, 1fr); gap: 10px; padding: 5px 0; border-bottom: 1px dashed color-mix(in srgb, var(--line) 70%, transparent); }
|
|
423
430
|
.turn time { font-size: 11px; color: var(--ink-dim); font-family: var(--mono); padding-top: 1px; }
|
|
424
431
|
.turn .who { font-size: 10.5px; color: var(--ink-dim); }
|