cortico-world-memory-core 0.4.2 → 0.4.3
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 +9 -0
- package/README.md +2 -2
- package/package.json +1 -1
- package/src/site.ts +6 -0
- package/src/store.ts +91 -2
- package/src/world.ts +17 -2
- package/webui/app.js +21 -2
- package/webui/style.css +15 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.4.3
|
|
4
|
+
|
|
5
|
+
隐私和手滑这两件事,都是之前列出来一直没做的。
|
|
6
|
+
|
|
7
|
+
- **原文来源脱敏**:`turns.jsonl` 里的 `sender` 原来存的是 `private:1940008084` 这种明文账号,目录一同步到网盘或备份就跟着泄露。现在只留后 4 位(`private:…8084`)。三道都做了:写入时脱敏、读取时再脱一次(老文件即使没改也不会露)、启动时把 `archive/` 下的旧文件抹一遍(`state.senderScrubbedAt` 记时间,只做一次)。
|
|
8
|
+
- **原文单条删除**:`DELETE /api/turns/:cursor`,热数据和归档都支持;删的是本地这一行,不影响对方那边,删完不会因为水位回退被重新录回来。页面上鼠标移到某一行,右上角出现「×」。
|
|
9
|
+
- **修掉 `load()` 里 `if (!quiet) render(); else render();`** 的冗余分支。
|
|
10
|
+
- **新增 `tools/audit-webui.ts`**:静态检查三件事——`$('#id')` 引用的元素在 HTML 里是否存在、`S.xxx` 读取的属性是否在 `S` 上定义、前端调的 `/api/*` 在后端是否有对应路由。0.4.2 里那个「搜索框从 0.1.0 起就抛异常」的 bug,正属于这套检查能提前抓到的类型。
|
|
11
|
+
|
|
3
12
|
## 0.4.2
|
|
4
13
|
|
|
5
14
|
按「第一批」做的三件事,其中第一件顺带挖出一个从 0.1.0 就带着的 bug。
|
package/README.md
CHANGED
|
@@ -53,7 +53,7 @@ pnpm add cortico-world-memory-core
|
|
|
53
53
|
| 图谱 | 力导向图:节点大小=权重、颜色=类型、连线=共同标签或同一天;滚轮缩放、拖动、双击适应 |
|
|
54
54
|
| 时间线 | 按天分组,带权重条与截止时间 |
|
|
55
55
|
| 列表 | 可排序表格(类型 / 内容 / 权重 / 来源 / 标签 / 记于 / 使用次数),权重带微条 |
|
|
56
|
-
| 原文 | 最近 7 天的对话原文,搜索高亮;勾「含归档」能连 `archive/`
|
|
56
|
+
| 原文 | 最近 7 天的对话原文,搜索高亮;勾「含归档」能连 `archive/` 里更早的句子一起搜,命中按天分组列在下面;鼠标移到某行可以单条删除 |
|
|
57
57
|
| 文件 | 工作区里被检索的 md / txt(只读) |
|
|
58
58
|
| 维护 | 重复 / 疑似作废 / 垃圾 / **承诺** / 回收站;承诺页能补截止时间、标记做完;彻底的删除会先列出要删的东西,10 秒内可撤销 |
|
|
59
59
|
| 体检 | 在用 / 从没被召回 / 召回次数 / 原文规模、近 14 天新增、承诺与备份状态;从没被召回的卡能一键退休 |
|
|
@@ -107,7 +107,7 @@ pnpm add cortico-world-memory-core
|
|
|
107
107
|
data/memory-core/
|
|
108
108
|
├─ cards.jsonl 卡片(一行一张,可以直接看)
|
|
109
109
|
├─ backups/ 每天第一笔写入前自动存的副本(cards-YYYY-MM-DD.jsonl,留 7 份)
|
|
110
|
-
├─ turns.jsonl
|
|
110
|
+
├─ turns.jsonl 对方最近的对话原文(sender 只留后 4 位,不留完整账号)
|
|
111
111
|
├─ archive/ 过期的原文按天归档(YYYY-MM-DD.jsonl),可搜可删
|
|
112
112
|
├─ plans.json 今天的日程
|
|
113
113
|
├─ state.json 精力/心情/活动/位置/主动计数
|
package/package.json
CHANGED
package/src/site.ts
CHANGED
|
@@ -100,6 +100,8 @@ export interface SiteHooks {
|
|
|
100
100
|
importData(payload: Record<string, unknown>): ImportReport;
|
|
101
101
|
/** 在按天归档的旧原文里搜(页面上的「含归档」开关)。 */
|
|
102
102
|
archiveSearch(q: string, days: number, limit: number): RawEntry[];
|
|
103
|
+
/** 删掉一条原文(热数据或归档)。 */
|
|
104
|
+
deleteTurn(cursor: number): boolean;
|
|
103
105
|
patchState(input: Record<string, unknown>): CoreState;
|
|
104
106
|
planAction(input: Record<string, unknown>): unknown;
|
|
105
107
|
exportAll(fmt: string): { body: string; mime: string; filename: string };
|
|
@@ -233,6 +235,10 @@ export class MemorySite {
|
|
|
233
235
|
const limit = Number(url.searchParams.get('limit') ?? 40) || 40;
|
|
234
236
|
return this.json(rsp, 200, q ? this.hooks.archiveSearch(q, days, limit) : []);
|
|
235
237
|
}
|
|
238
|
+
const turnMatch = /^\/api\/turns\/(\d{1,12})$/.exec(path);
|
|
239
|
+
if (turnMatch && req.method === 'DELETE') {
|
|
240
|
+
return this.json(rsp, 200, { removed: this.hooks.deleteTurn(Number(turnMatch[1])) });
|
|
241
|
+
}
|
|
236
242
|
if (path === '/api/import' && req.method === 'POST') {
|
|
237
243
|
let body: Record<string, unknown>;
|
|
238
244
|
try {
|
package/src/store.ts
CHANGED
|
@@ -84,6 +84,8 @@ export interface CoreState {
|
|
|
84
84
|
backfillFrom?: number;
|
|
85
85
|
backfillTo?: number;
|
|
86
86
|
};
|
|
87
|
+
/** 归档里的账号号码抹过一次之后记下时间,不再重复扫。 */
|
|
88
|
+
senderScrubbedAt?: string;
|
|
87
89
|
}
|
|
88
90
|
|
|
89
91
|
export function ensureDir(dir: string): void {
|
|
@@ -145,6 +147,14 @@ function clamp01(n: number): number {
|
|
|
145
147
|
return Number.isFinite(n) ? Math.min(1, Math.max(0, n)) : 0.5;
|
|
146
148
|
}
|
|
147
149
|
|
|
150
|
+
/**
|
|
151
|
+
* 原文的来源里带平台账号(`private:1940008084`),原样落盘就是明文号码,
|
|
152
|
+
* 目录一同步到网盘或备份就跟着泄露。只留后 4 位——够区分是谁,不够认出是谁。
|
|
153
|
+
*/
|
|
154
|
+
export function maskSender(sender: string): string {
|
|
155
|
+
return String(sender ?? '').replace(/\d{5,}/g, (digits) => `…${digits.slice(-4)}`);
|
|
156
|
+
}
|
|
157
|
+
|
|
148
158
|
export class CardStore {
|
|
149
159
|
private cards: Card[] = [];
|
|
150
160
|
private readonly file: string;
|
|
@@ -709,6 +719,8 @@ export class RawLog {
|
|
|
709
719
|
if (!e || typeof e.text !== 'string') continue;
|
|
710
720
|
const at = Date.parse(e.ts);
|
|
711
721
|
if (!Number.isFinite(at)) continue;
|
|
722
|
+
// 读的时候就脱敏:即便文件里还留着老格式,界面和快照也不会露号码
|
|
723
|
+
e.sender = maskSender(e.sender);
|
|
712
724
|
(at >= cutoff ? kept : expired).push(e);
|
|
713
725
|
} catch {
|
|
714
726
|
// 跳过坏行
|
|
@@ -809,6 +821,77 @@ export class RawLog {
|
|
|
809
821
|
return { days: files.length, bytes, oldest: files[0]?.day ?? '' };
|
|
810
822
|
}
|
|
811
823
|
|
|
824
|
+
/** 删掉一条原文:热数据从内存表里摘掉重写;在归档里就改写那天对应的文件。 */
|
|
825
|
+
remove(cursor: number): boolean {
|
|
826
|
+
const id = Number(cursor);
|
|
827
|
+
if (!Number.isFinite(id)) return false;
|
|
828
|
+
const at = this.entries.findIndex((e) => e.cursor === id);
|
|
829
|
+
if (at >= 0) {
|
|
830
|
+
this.entries.splice(at, 1);
|
|
831
|
+
this.rewrite();
|
|
832
|
+
return true;
|
|
833
|
+
}
|
|
834
|
+
for (const { file } of this.archiveFiles()) {
|
|
835
|
+
try {
|
|
836
|
+
const lines = readFileSync(file, 'utf8').split('\n');
|
|
837
|
+
let hit = -1;
|
|
838
|
+
for (let i = 0; i < lines.length; i += 1) {
|
|
839
|
+
if (!lines[i].trim()) continue;
|
|
840
|
+
try {
|
|
841
|
+
if ((JSON.parse(lines[i]) as RawEntry).cursor === id) {
|
|
842
|
+
hit = i;
|
|
843
|
+
break;
|
|
844
|
+
}
|
|
845
|
+
} catch {
|
|
846
|
+
// 坏行跳过
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
if (hit < 0) continue;
|
|
850
|
+
lines.splice(hit, 1);
|
|
851
|
+
const kept = lines.filter((line) => line.trim());
|
|
852
|
+
if (!kept.length) {
|
|
853
|
+
// 那天只有这一条,删完就把空文件收掉,免得归档里留个 0 行的日期
|
|
854
|
+
unlinkSync(file);
|
|
855
|
+
} else {
|
|
856
|
+
writeAtomic(file, `${kept.join('\n')}\n`);
|
|
857
|
+
}
|
|
858
|
+
return true;
|
|
859
|
+
} catch {
|
|
860
|
+
// 读不了就试下一个文件
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
return false;
|
|
864
|
+
}
|
|
865
|
+
|
|
866
|
+
/** 热数据里的旧格式已经由 load() 的 rewrite 抹掉了,这里只处理 archive/ 下的文件。 */
|
|
867
|
+
scrubArchiveSenders(): number {
|
|
868
|
+
let changed = 0;
|
|
869
|
+
for (const { file } of this.archiveFiles()) {
|
|
870
|
+
try {
|
|
871
|
+
const lines = readFileSync(file, 'utf8').split('\n');
|
|
872
|
+
let dirty = false;
|
|
873
|
+
const out = lines.map((line) => {
|
|
874
|
+
if (!line.trim()) return line;
|
|
875
|
+
try {
|
|
876
|
+
const e = JSON.parse(line) as RawEntry;
|
|
877
|
+
const masked = maskSender(e.sender);
|
|
878
|
+
if (masked === e.sender) return line;
|
|
879
|
+
e.sender = masked;
|
|
880
|
+
dirty = true;
|
|
881
|
+
changed += 1;
|
|
882
|
+
return JSON.stringify(e);
|
|
883
|
+
} catch {
|
|
884
|
+
return line;
|
|
885
|
+
}
|
|
886
|
+
});
|
|
887
|
+
if (dirty) writeAtomic(file, out.join('\n'));
|
|
888
|
+
} catch {
|
|
889
|
+
// 读不了就跳过
|
|
890
|
+
}
|
|
891
|
+
}
|
|
892
|
+
return changed;
|
|
893
|
+
}
|
|
894
|
+
|
|
812
895
|
/** 归档总行数。健康面板按需调用,不进热路径。 */
|
|
813
896
|
archiveLines(): number {
|
|
814
897
|
let n = 0;
|
|
@@ -835,7 +918,10 @@ export class RawLog {
|
|
|
835
918
|
if (!line.trim()) continue;
|
|
836
919
|
try {
|
|
837
920
|
const e = JSON.parse(line) as RawEntry;
|
|
838
|
-
if (e && typeof e.text === 'string')
|
|
921
|
+
if (e && typeof e.text === 'string') {
|
|
922
|
+
e.sender = maskSender(e.sender);
|
|
923
|
+
out.push(e);
|
|
924
|
+
}
|
|
839
925
|
} catch {
|
|
840
926
|
// 跳过坏行
|
|
841
927
|
}
|
|
@@ -878,7 +964,10 @@ export class RawLog {
|
|
|
878
964
|
if (!items.length) return 0;
|
|
879
965
|
// 回放时一次几千行,用 Set 判重,别拿 some() 平方级扫
|
|
880
966
|
const have = new Set(this.entries.map((e) => e.cursor));
|
|
881
|
-
const fresh = items
|
|
967
|
+
const fresh = items
|
|
968
|
+
.filter((item) => !have.has(item.cursor))
|
|
969
|
+
// 落盘前统一脱敏:不管调用方传的是原始 key 还是已脱敏的,这里都兜一道
|
|
970
|
+
.map((item) => ({ ...item, sender: maskSender(item.sender) }));
|
|
882
971
|
if (!fresh.length) return 0;
|
|
883
972
|
this.entries.push(...fresh);
|
|
884
973
|
const cutoff = Date.now() - this.retentionDays * 86_400_000;
|
package/src/world.ts
CHANGED
|
@@ -20,6 +20,7 @@ import {
|
|
|
20
20
|
CardStore,
|
|
21
21
|
RawLog,
|
|
22
22
|
ensureDir,
|
|
23
|
+
maskSender,
|
|
23
24
|
readJson,
|
|
24
25
|
writeJson,
|
|
25
26
|
type CardKind,
|
|
@@ -266,6 +267,19 @@ export class MemoryCoreWorld implements World {
|
|
|
266
267
|
this.stopped = false;
|
|
267
268
|
ensureDir(this.dataDir);
|
|
268
269
|
this.importOnce();
|
|
270
|
+
// 归档里可能还留着老格式的明文账号,抹一次就够(热数据在 load 时已经抹了)
|
|
271
|
+
if (!this.state.senderScrubbedAt) {
|
|
272
|
+
const scrubbed = this.raw.scrubArchiveSenders();
|
|
273
|
+
this.state.senderScrubbedAt = new Date().toISOString();
|
|
274
|
+
this.save(true);
|
|
275
|
+
if (scrubbed) {
|
|
276
|
+
try {
|
|
277
|
+
host.log?.info?.(`[memory-core] 归档里 ${scrubbed} 条来源已脱敏`);
|
|
278
|
+
} catch {
|
|
279
|
+
// 日志失败不影响挂载
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
}
|
|
269
283
|
try {
|
|
270
284
|
host.log?.info?.('[memory-core] 启动', {
|
|
271
285
|
dataDir: this.dataDir,
|
|
@@ -463,6 +477,7 @@ export class MemoryCoreWorld implements World {
|
|
|
463
477
|
const cap = Math.min(50, Math.max(1, Number(limit) || 40));
|
|
464
478
|
return rankRaw(this.raw.readArchive(span, 20000), query, cap);
|
|
465
479
|
},
|
|
480
|
+
deleteTurn: (cursor: number) => this.raw.remove(cursor),
|
|
466
481
|
mergeCards: (keepId: string, dropIds: string[]) => this.store.merge(keepId, dropIds),
|
|
467
482
|
purgeCards: (ids: string[]) => this.store.purge(ids),
|
|
468
483
|
patchState: (input: Record<string, unknown>) => {
|
|
@@ -596,7 +611,7 @@ export class MemoryCoreWorld implements World {
|
|
|
596
611
|
entries.push({
|
|
597
612
|
cursor,
|
|
598
613
|
ts: typeof t.ts === 'string' && t.ts ? t.ts : new Date().toISOString(),
|
|
599
|
-
sender: String(t.sender ?? 'import').slice(0, 40),
|
|
614
|
+
sender: maskSender(String(t.sender ?? 'import')).slice(0, 40),
|
|
600
615
|
text: t.text.slice(0, 200),
|
|
601
616
|
});
|
|
602
617
|
}
|
|
@@ -981,7 +996,7 @@ export class MemoryCoreWorld implements World {
|
|
|
981
996
|
if (/^\[(表情包|CQ:|图片)/.test(text)) continue;
|
|
982
997
|
if (/^[\p{P}\p{S}\s]+$/u.test(text)) continue;
|
|
983
998
|
if (/^\[[^\]]{0,12}\]$/.test(text.trim())) continue;
|
|
984
|
-
items.push({ cursor: e.cursor, ts: e.ts, sender: key.slice(0, 40), text });
|
|
999
|
+
items.push({ cursor: e.cursor, ts: e.ts, sender: maskSender(key).slice(0, 40), text });
|
|
985
1000
|
}
|
|
986
1001
|
// 按窗口边界推进,不按「最后一个匹配到的 cursor」:窗口里可能一条 external 都没有
|
|
987
1002
|
from = to;
|
package/webui/app.js
CHANGED
|
@@ -270,8 +270,7 @@ async function load({ quiet = false } = {}) {
|
|
|
270
270
|
try {
|
|
271
271
|
const snap = await api('/api/snapshot');
|
|
272
272
|
S.snap = snap;
|
|
273
|
-
|
|
274
|
-
else render();
|
|
273
|
+
render();
|
|
275
274
|
} catch (err) {
|
|
276
275
|
toast(`读不到数据:${err.message}`, true);
|
|
277
276
|
}
|
|
@@ -1170,6 +1169,7 @@ function turnRow(t) {
|
|
|
1170
1169
|
return `<div class="turn">
|
|
1171
1170
|
<div><time>${fmtTime(t.ts).slice(6)}</time><div class="who">${esc(shortSender(t.sender))}</div></div>
|
|
1172
1171
|
<div class="msg">${highlight(t.text.slice(0, 600), S.query)}</div>
|
|
1172
|
+
<button class="turn-del" data-cursor="${t.cursor}" title="删掉这条原文" aria-label="删掉这条原文">×</button>
|
|
1173
1173
|
</div>`;
|
|
1174
1174
|
}
|
|
1175
1175
|
|
|
@@ -1951,6 +1951,25 @@ $('#view-timeline').addEventListener('click', (event) => {
|
|
|
1951
1951
|
selectCard(item.dataset.id);
|
|
1952
1952
|
});
|
|
1953
1953
|
|
|
1954
|
+
// 原文单条删除:按钮随行渲染,用事件委托,重绘后不用重新绑定
|
|
1955
|
+
$('#view-turns').addEventListener('click', async (event) => {
|
|
1956
|
+
const btn = event.target.closest('.turn-del');
|
|
1957
|
+
if (!btn) return;
|
|
1958
|
+
const cursor = Number(btn.dataset.cursor);
|
|
1959
|
+
if (!Number.isFinite(cursor)) return;
|
|
1960
|
+
if (!confirm('删掉这条原文?只删本地这一行,对方那边不受影响。删了补不回来。')) return;
|
|
1961
|
+
try {
|
|
1962
|
+
const res = await api(`/api/turns/${cursor}`, { method: 'DELETE', body: '{}' });
|
|
1963
|
+
if (!res?.removed) return toast('没找到这条', true);
|
|
1964
|
+
S.archiveQ = '';
|
|
1965
|
+
S.archive = null;
|
|
1966
|
+
await load();
|
|
1967
|
+
toast('已删掉');
|
|
1968
|
+
} catch (err) {
|
|
1969
|
+
toast(`删除失败:${err.message}`, true);
|
|
1970
|
+
}
|
|
1971
|
+
});
|
|
1972
|
+
|
|
1954
1973
|
$('#view-table').addEventListener('click', (event) => {
|
|
1955
1974
|
const th = event.target.closest('th[data-sort]');
|
|
1956
1975
|
if (th) {
|
package/webui/style.css
CHANGED
|
@@ -719,3 +719,18 @@ kbd {
|
|
|
719
719
|
font-size: 11px; color: var(--warn);
|
|
720
720
|
}
|
|
721
721
|
.archive-head span { color: var(--ink-dim); }
|
|
722
|
+
|
|
723
|
+
/* ── 原文单条删除 ───────────────────────────────────── */
|
|
724
|
+
.turn { position: relative; padding-right: 22px; }
|
|
725
|
+
.turn-del {
|
|
726
|
+
position: absolute; right: 4px; top: 6px;
|
|
727
|
+
width: 20px; height: 20px; padding: 0;
|
|
728
|
+
display: grid; place-items: center;
|
|
729
|
+
border: 1px solid transparent; border-radius: 6px;
|
|
730
|
+
background: transparent; color: var(--ink-dim);
|
|
731
|
+
font-size: 14px; line-height: 1; cursor: pointer;
|
|
732
|
+
opacity: 0; transition: opacity .12s ease;
|
|
733
|
+
}
|
|
734
|
+
.turn:hover .turn-del, .turn-del:focus-visible { opacity: 1; }
|
|
735
|
+
.turn-del:hover { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 45%, transparent); background: var(--paper); }
|
|
736
|
+
@media (hover: none) { .turn-del { opacity: .55; } }
|