cortico-world-memory-core 0.25.0 → 0.27.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 +46 -9
- package/ENV_PROMPT.md +9 -10
- package/README.md +32 -14
- package/assets/readme/banner.png +0 -0
- package/assets/screenshots/detail.png +0 -0
- package/assets/screenshots/galaxy.png +0 -0
- package/assets/screenshots/health.png +0 -0
- package/assets/screenshots/turns.png +0 -0
- package/describe.json +294 -235
- package/icon.svg +32 -26
- package/package.json +12 -9
- package/src/config.ts +26 -3
- package/src/recall.ts +118 -8
- package/src/review.ts +146 -0
- package/src/site.ts +57 -10
- package/src/store.ts +85 -39
- package/src/tools.ts +6 -8
- package/src/world.ts +225 -47
- package/webui/app.js +590 -1055
- package/webui/atlas.css +116 -0
- package/webui/index.html +24 -18
- package/webui/next.css +158 -8
- package/webui/next.html +31 -29
- package/webui/next.js +5 -14
- package/webui/starfield/atlas.js +168 -0
- package/webui/starfield/film.svg +22 -0
- package/webui/starfield/lucide-LICENSE.txt +15 -0
- package/webui/starfield/pause.svg +16 -0
- package/webui/starfield/play.svg +15 -0
- package/webui/starfield/scene.js +301 -0
- package/webui/starfield/three-LICENSE.txt +21 -0
- package/webui/starfield/three.core.min.js +6 -0
- package/webui/starfield/three.module.min.js +6 -0
- package/webui/style.css +160 -10
- package/webui/turns.css +21 -0
- 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/src/store.ts
CHANGED
|
@@ -55,6 +55,12 @@ export interface Card {
|
|
|
55
55
|
* 不是删除——默认不进召回和预激活,显式搜索仍然找得到,一键回暖。
|
|
56
56
|
*/
|
|
57
57
|
coldAt?: string;
|
|
58
|
+
keepLongTerm?: boolean;
|
|
59
|
+
summaryOf?: string[];
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function lastMemoryTouch(card: Card): number {
|
|
63
|
+
return Math.max(Date.parse(card.createdAt) || 0, Date.parse(card.lastUsedAt ?? '') || 0, Date.parse(card.lastPrimedAt ?? '') || 0);
|
|
58
64
|
}
|
|
59
65
|
|
|
60
66
|
export interface RawEntry {
|
|
@@ -100,6 +106,7 @@ export interface CoreState {
|
|
|
100
106
|
remindCountToday?: number;
|
|
101
107
|
/** 已经收成卡片的想法 id(只留最近 100 个,够防重复了)。 */
|
|
102
108
|
absorbedThoughtIds?: string[];
|
|
109
|
+
handledKeepSuggestions?: string[];
|
|
103
110
|
rawWatermark?: number;
|
|
104
111
|
/** 水位是按哪一版捕获逻辑推进的。旧版用了错的 range 语义,水位不可信,见 CAPTURE_WATERMARK_VERSION。 */
|
|
105
112
|
watermarkVersion?: number;
|
|
@@ -203,6 +210,9 @@ export function demoteTech(kind: CardKind, text: string): CardKind {
|
|
|
203
210
|
}
|
|
204
211
|
|
|
205
212
|
export class CardStore {
|
|
213
|
+
/** 每次落盘 +1。召回索引缓存用它判断「库变没变」,比逐张比对便宜。 */
|
|
214
|
+
revision = 0;
|
|
215
|
+
|
|
206
216
|
private cards: Card[] = [];
|
|
207
217
|
private readonly file: string;
|
|
208
218
|
private readonly backupDir: string;
|
|
@@ -275,22 +285,16 @@ export class CardStore {
|
|
|
275
285
|
const span = Math.max(0, Number(days) || 0);
|
|
276
286
|
if (!span) return [];
|
|
277
287
|
const cutoff = now - span * 86_400_000;
|
|
278
|
-
const lastTouch = (c: Card) =>
|
|
279
|
-
Math.max(
|
|
280
|
-
Date.parse(c.createdAt) || 0,
|
|
281
|
-
Date.parse(c.lastUsedAt ?? '') || 0,
|
|
282
|
-
Date.parse(c.lastPrimedAt ?? '') || 0,
|
|
283
|
-
);
|
|
284
288
|
return this.active()
|
|
285
289
|
.filter((c) => {
|
|
286
|
-
if (c.coldAt) return false;
|
|
290
|
+
if (c.coldAt || c.keepLongTerm === true) return false;
|
|
287
291
|
// 只沉流水账和临时信息;fact / self / 未完成的承诺是「人」,不能沉
|
|
288
292
|
if (c.kind !== 'episode' && c.kind !== 'note') return false;
|
|
289
293
|
if (c.weight > 0.45) return false;
|
|
290
294
|
if ((c.uses || 0) + (c.primedCount || 0) > 1) return false;
|
|
291
|
-
return
|
|
295
|
+
return lastMemoryTouch(c) < cutoff;
|
|
292
296
|
})
|
|
293
|
-
.sort((a, b) =>
|
|
297
|
+
.sort((a, b) => lastMemoryTouch(a) - lastMemoryTouch(b)); // 最久没碰的排前面,配额先吃它们
|
|
294
298
|
}
|
|
295
299
|
|
|
296
300
|
chill(ids: readonly string[]): number {
|
|
@@ -298,7 +302,7 @@ export class CardStore {
|
|
|
298
302
|
let n = 0;
|
|
299
303
|
for (const id of ids) {
|
|
300
304
|
const card = this.find(id);
|
|
301
|
-
if (!card || card.status === 'retired' || card.coldAt) continue;
|
|
305
|
+
if (!card || card.status === 'retired' || card.coldAt || card.keepLongTerm === true) continue;
|
|
302
306
|
card.coldAt = at;
|
|
303
307
|
n += 1;
|
|
304
308
|
}
|
|
@@ -364,6 +368,8 @@ export class CardStore {
|
|
|
364
368
|
origin?: string;
|
|
365
369
|
createdAt?: string;
|
|
366
370
|
scope?: string;
|
|
371
|
+
keepLongTerm?: boolean;
|
|
372
|
+
summaryOf?: string[];
|
|
367
373
|
/** 写这条时,对方最近说的那句原话(截断 80 字,已脱敏)。有它才追得回证据。 */
|
|
368
374
|
src?: { cursor: number; ts: string; text?: string };
|
|
369
375
|
}): { card: Card; merged: boolean } {
|
|
@@ -376,7 +382,12 @@ export class CardStore {
|
|
|
376
382
|
(c) => c.kind === kind && c.status !== 'retired' && normalizeText(c.text) === key,
|
|
377
383
|
);
|
|
378
384
|
if (near) {
|
|
385
|
+
if (input.summaryOf) return { card: near, merged: true };
|
|
379
386
|
near.weight = Math.max(near.weight, clamp01(Number(input.weight ?? near.weight)));
|
|
387
|
+
if (input.keepLongTerm === true) {
|
|
388
|
+
near.keepLongTerm = true;
|
|
389
|
+
delete near.coldAt;
|
|
390
|
+
}
|
|
380
391
|
near.updatedAt = new Date().toISOString();
|
|
381
392
|
if (input.dueAt && !near.dueAt) near.dueAt = input.dueAt;
|
|
382
393
|
this.dirty = true;
|
|
@@ -401,6 +412,8 @@ export class CardStore {
|
|
|
401
412
|
...(input.dueAt ? { dueAt: input.dueAt } : {}),
|
|
402
413
|
...(kind === 'promise' ? { done: false } : {}),
|
|
403
414
|
...(input.src ? { src: input.src } : {}),
|
|
415
|
+
...(input.keepLongTerm === true ? { keepLongTerm: true } : {}),
|
|
416
|
+
...(input.summaryOf ? { summaryOf: [...input.summaryOf] } : {}),
|
|
404
417
|
};
|
|
405
418
|
this.cards.push(card);
|
|
406
419
|
this.dirty = true;
|
|
@@ -412,6 +425,7 @@ export class CardStore {
|
|
|
412
425
|
const card = this.find(id);
|
|
413
426
|
if (!card) return undefined;
|
|
414
427
|
Object.assign(card, patch, { updatedAt: new Date().toISOString() });
|
|
428
|
+
if (card.keepLongTerm === true) delete card.coldAt;
|
|
415
429
|
this.dirty = true;
|
|
416
430
|
this.save();
|
|
417
431
|
return card;
|
|
@@ -450,7 +464,7 @@ export class CardStore {
|
|
|
450
464
|
* 用新卡接替旧卡:旧卡退休、新卡记下它接替了谁。
|
|
451
465
|
* 返回 [新卡, 旧卡];旧的找不到就只返回新卡。
|
|
452
466
|
*/
|
|
453
|
-
supersede(oldId: string, next: Card): { card: Card; replaced: Card | null } {
|
|
467
|
+
supersede(oldId: string, next: Card, inheritKeepLongTerm = true): { card: Card; replaced: Card | null } {
|
|
454
468
|
const old = this.find(oldId);
|
|
455
469
|
if (!old || old.id === next.id) return { card: next, replaced: null };
|
|
456
470
|
old.status = 'retired';
|
|
@@ -458,6 +472,11 @@ export class CardStore {
|
|
|
458
472
|
old.retiredBy = next.id;
|
|
459
473
|
old.updatedAt = old.retiredAt;
|
|
460
474
|
next.supersedes = [...new Set([...(next.supersedes ?? []), old.id])];
|
|
475
|
+
if (old.summaryOf?.length) next.summaryOf = [...new Set([...(next.summaryOf ?? []), ...old.summaryOf])].filter((id) => id !== next.id);
|
|
476
|
+
if (inheritKeepLongTerm && old.keepLongTerm === true) {
|
|
477
|
+
next.keepLongTerm = true;
|
|
478
|
+
delete next.coldAt;
|
|
479
|
+
}
|
|
461
480
|
this.dirty = true;
|
|
462
481
|
this.save(true);
|
|
463
482
|
return { card: next, replaced: old };
|
|
@@ -503,8 +522,13 @@ export class CardStore {
|
|
|
503
522
|
if (!drop || drop.status === 'retired') continue;
|
|
504
523
|
kept.tags = [...new Set([...kept.tags, ...drop.tags])].slice(0, 8);
|
|
505
524
|
kept.weight = Math.max(kept.weight, drop.weight);
|
|
525
|
+
if (drop.keepLongTerm === true) {
|
|
526
|
+
kept.keepLongTerm = true;
|
|
527
|
+
delete kept.coldAt;
|
|
528
|
+
}
|
|
506
529
|
if (drop.dueAt && (!kept.dueAt || Date.parse(drop.dueAt) < Date.parse(kept.dueAt))) kept.dueAt = drop.dueAt;
|
|
507
530
|
kept.supersedes = [...new Set([...(kept.supersedes ?? []), ...(drop.supersedes ?? []), drop.id])];
|
|
531
|
+
if (drop.summaryOf?.length) kept.summaryOf = [...new Set([...(kept.summaryOf ?? []), ...drop.summaryOf])].filter((id) => id !== kept.id);
|
|
508
532
|
kept.updatedAt = new Date().toISOString();
|
|
509
533
|
drop.status = 'retired';
|
|
510
534
|
drop.retiredAt = kept.updatedAt;
|
|
@@ -636,6 +660,7 @@ export class CardStore {
|
|
|
636
660
|
}
|
|
637
661
|
|
|
638
662
|
touch(ids: readonly string[]): void {
|
|
663
|
+
if (!ids.length) return;
|
|
639
664
|
const now = new Date().toISOString();
|
|
640
665
|
for (const id of ids) {
|
|
641
666
|
const card = this.find(id);
|
|
@@ -763,7 +788,9 @@ export class CardStore {
|
|
|
763
788
|
: undefined,
|
|
764
789
|
primedCount: Number.isFinite(Number(c.primedCount)) ? Number(c.primedCount) : undefined,
|
|
765
790
|
lastPrimedAt: typeof c.lastPrimedAt === 'string' ? c.lastPrimedAt : undefined,
|
|
766
|
-
coldAt: typeof c.coldAt === 'string' ? c.coldAt : undefined,
|
|
791
|
+
coldAt: c.keepLongTerm !== true && typeof c.coldAt === 'string' ? c.coldAt : undefined,
|
|
792
|
+
keepLongTerm: c.keepLongTerm === true ? true : undefined,
|
|
793
|
+
summaryOf: Array.isArray(c.summaryOf) ? [...new Set(c.summaryOf.filter((ref) => typeof ref === 'string' && /^[\w-]{1,60}$/.test(ref) && ref !== id))] : undefined,
|
|
767
794
|
};
|
|
768
795
|
this.cards.push(card);
|
|
769
796
|
byId.add(id);
|
|
@@ -784,6 +811,7 @@ export class CardStore {
|
|
|
784
811
|
}
|
|
785
812
|
|
|
786
813
|
save(force = false): void {
|
|
814
|
+
this.revision += 1;
|
|
787
815
|
if (!this.dirty && !force) return;
|
|
788
816
|
this.dirty = false;
|
|
789
817
|
this.backupOnceADay();
|
|
@@ -978,44 +1006,44 @@ export class RawLog {
|
|
|
978
1006
|
|
|
979
1007
|
/** 删掉一条原文:热数据从内存表里摘掉重写;在归档里就改写那天对应的文件。 */
|
|
980
1008
|
remove(cursor: number): boolean {
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
1009
|
+
return this.removeMany([cursor]) > 0;
|
|
1010
|
+
}
|
|
1011
|
+
|
|
1012
|
+
removeMany(cursors: number[]): number {
|
|
1013
|
+
const remaining = new Set(cursors.filter(Number.isSafeInteger));
|
|
1014
|
+
const count = remaining.size;
|
|
1015
|
+
if (!count) return 0;
|
|
1016
|
+
const keptHot = this.entries.filter(entry => !remaining.delete(entry.cursor));
|
|
1017
|
+
if (keptHot.length !== this.entries.length) {
|
|
1018
|
+
if (!writeAtomic(this.file, keptHot.map(entry => JSON.stringify(entry)).join('\n') + '\n')) throw new Error('主人原文未能保存');
|
|
1019
|
+
this.entries = keptHot;
|
|
988
1020
|
}
|
|
989
1021
|
for (const { file } of this.archiveFiles()) {
|
|
1022
|
+
if (!remaining.size) break;
|
|
990
1023
|
try {
|
|
991
1024
|
const lines = readFileSync(file, 'utf8').split('\n');
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
if (!
|
|
1025
|
+
const hits: number[] = [];
|
|
1026
|
+
const kept = lines.filter(line => {
|
|
1027
|
+
if (!line.trim()) return false;
|
|
995
1028
|
try {
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
}
|
|
1003
|
-
}
|
|
1004
|
-
if (hit < 0) continue;
|
|
1005
|
-
lines.splice(hit, 1);
|
|
1006
|
-
const kept = lines.filter((line) => line.trim());
|
|
1029
|
+
const id = (JSON.parse(line) as RawEntry).cursor;
|
|
1030
|
+
if (remaining.has(id)) { hits.push(id); return false; }
|
|
1031
|
+
} catch { /* 保留未识别的行 */ }
|
|
1032
|
+
return true;
|
|
1033
|
+
});
|
|
1034
|
+
if (!hits.length) continue;
|
|
1007
1035
|
if (!kept.length) {
|
|
1008
1036
|
// 那天只有这一条,删完就把空文件收掉,免得归档里留个 0 行的日期
|
|
1009
1037
|
unlinkSync(file);
|
|
1010
1038
|
} else {
|
|
1011
|
-
writeAtomic(file, `${kept.join('\n')}\n`);
|
|
1039
|
+
if (!writeAtomic(file, `${kept.join('\n')}\n`)) continue;
|
|
1012
1040
|
}
|
|
1013
|
-
|
|
1041
|
+
for (const hit of hits) remaining.delete(hit);
|
|
1014
1042
|
} catch {
|
|
1015
1043
|
// 读不了就试下一个文件
|
|
1016
1044
|
}
|
|
1017
1045
|
}
|
|
1018
|
-
return
|
|
1046
|
+
return count - remaining.size;
|
|
1019
1047
|
}
|
|
1020
1048
|
|
|
1021
1049
|
/** 热数据里的旧格式已经由 load() 的 rewrite 抹掉了,这里只处理 archive/ 下的文件。 */
|
|
@@ -1060,6 +1088,24 @@ export class RawLog {
|
|
|
1060
1088
|
return n;
|
|
1061
1089
|
}
|
|
1062
1090
|
|
|
1091
|
+
sourceContext(source: NonNullable<Card['src']>): RawEntry[] {
|
|
1092
|
+
const day = dayKeyOf(source.ts);
|
|
1093
|
+
if (!day || !Number.isSafeInteger(source.cursor)) return [];
|
|
1094
|
+
const rows = new Map(this.entries.filter(row => dayKeyOf(row.ts) === day).map(row => [row.cursor, row]));
|
|
1095
|
+
const file = join(this.archiveDir, `${day}.jsonl`);
|
|
1096
|
+
if (existsSync(file)) for (const line of readFileSync(file, 'utf8').split('\n')) {
|
|
1097
|
+
try {
|
|
1098
|
+
const row = JSON.parse(line) as RawEntry;
|
|
1099
|
+
if (typeof row?.text === 'string' && !rows.has(row.cursor)) rows.set(row.cursor, row);
|
|
1100
|
+
} catch { continue; }
|
|
1101
|
+
}
|
|
1102
|
+
const anchor = rows.get(source.cursor);
|
|
1103
|
+
if (!anchor || Date.parse(anchor.ts) !== Date.parse(source.ts) || anchor.sender === 'desktop-pet.self') return [];
|
|
1104
|
+
const nearby = [...rows.values()].filter(row => row.sender === anchor.sender && Math.abs(Date.parse(row.ts) - Date.parse(anchor.ts)) <= 30 * 60_000).sort((left, right) => left.cursor - right.cursor);
|
|
1105
|
+
const at = nearby.indexOf(anchor);
|
|
1106
|
+
return nearby.slice(Math.max(0, at - 2), at + 3).map(row => ({ ...row, sender: maskSender(row.sender) }));
|
|
1107
|
+
}
|
|
1108
|
+
|
|
1063
1109
|
/** 翻归档:默认最近 30 天,最多 8000 行。只给 mode=archive 用,不进常规召回。 */
|
|
1064
1110
|
readArchive(days = 30, maxEntries = 8000): RawEntry[] {
|
|
1065
1111
|
const span = Math.max(1, Math.min(365, Number(days) || 30));
|
|
@@ -1073,7 +1119,7 @@ export class RawLog {
|
|
|
1073
1119
|
if (!line.trim()) continue;
|
|
1074
1120
|
try {
|
|
1075
1121
|
const e = JSON.parse(line) as RawEntry;
|
|
1076
|
-
if (e && typeof e.text === 'string') {
|
|
1122
|
+
if (e && typeof e.text === 'string' && e.sender !== 'desktop-pet.self') {
|
|
1077
1123
|
e.sender = maskSender(e.sender);
|
|
1078
1124
|
out.push(e);
|
|
1079
1125
|
}
|
|
@@ -1102,7 +1148,7 @@ export class RawLog {
|
|
|
1102
1148
|
}
|
|
1103
1149
|
|
|
1104
1150
|
all(): readonly RawEntry[] {
|
|
1105
|
-
return this.entries;
|
|
1151
|
+
return this.entries.filter(entry => entry.sender !== 'desktop-pet.self');
|
|
1106
1152
|
}
|
|
1107
1153
|
|
|
1108
1154
|
reset(): void {
|
|
@@ -1120,7 +1166,7 @@ export class RawLog {
|
|
|
1120
1166
|
// 回放时一次几千行,用 Set 判重,别拿 some() 平方级扫
|
|
1121
1167
|
const have = new Set(this.entries.map((e) => e.cursor));
|
|
1122
1168
|
const fresh = items
|
|
1123
|
-
.filter((item) => !have.has(item.cursor))
|
|
1169
|
+
.filter((item) => item.sender !== 'desktop-pet.self' && !have.has(item.cursor))
|
|
1124
1170
|
// 落盘前统一脱敏:不管调用方传的是原始 key 还是已脱敏的,这里都兜一道
|
|
1125
1171
|
.map((item) => ({ ...item, sender: maskSender(item.sender) }));
|
|
1126
1172
|
if (!fresh.length) return 0;
|
package/src/tools.ts
CHANGED
|
@@ -17,10 +17,8 @@ export const TOOL_SPECS: ToolSpec[] = [
|
|
|
17
17
|
{
|
|
18
18
|
name: 'mcore_write',
|
|
19
19
|
description:
|
|
20
|
-
'
|
|
21
|
-
+ '
|
|
22
|
-
+ 'promise=答应过的事(必须写 dueAt),note=临时信息和技术操作。'
|
|
23
|
-
+ '一条只写一件事,40-80 字。旧记录用 closes 关掉。',
|
|
20
|
+
'写记忆。fact=对方事实,self=自己或关系,episode=亲身经历,promise=承诺须带 dueAt,note=临时或技术操作。'
|
|
21
|
+
+ '对方要求记住时必调;一条一事,只在对方问起时给编号。',
|
|
24
22
|
parameters: obj(
|
|
25
23
|
{
|
|
26
24
|
kind: {
|
|
@@ -33,6 +31,7 @@ export const TOOL_SPECS: ToolSpec[] = [
|
|
|
33
31
|
tags: { type: 'array', items: { type: 'string' } },
|
|
34
32
|
dueAt: { type: 'string', description: 'promise 用:ISO 或 YYYY-MM-DD HH:MM;没有截止时间写 none' },
|
|
35
33
|
closes: { type: 'string', description: '要关掉的旧 id' },
|
|
34
|
+
keepLongTerm: { type: 'boolean', description: '明确要求长期保留才传 true' },
|
|
36
35
|
},
|
|
37
36
|
['kind', 'text'],
|
|
38
37
|
),
|
|
@@ -51,6 +50,7 @@ export const TOOL_SPECS: ToolSpec[] = [
|
|
|
51
50
|
tags: { type: 'array', items: { type: 'string' } },
|
|
52
51
|
dueAt: { type: 'string', description: 'promise 用;none 清空' },
|
|
53
52
|
done: { type: 'boolean', description: '做完' },
|
|
53
|
+
keepLongTerm: { type: 'boolean', description: '按对方要求开关长期保留' },
|
|
54
54
|
},
|
|
55
55
|
['id'],
|
|
56
56
|
),
|
|
@@ -59,10 +59,8 @@ export const TOOL_SPECS: ToolSpec[] = [
|
|
|
59
59
|
{
|
|
60
60
|
name: 'mcore_recall',
|
|
61
61
|
description:
|
|
62
|
-
'
|
|
63
|
-
+ '
|
|
64
|
-
+ '给 id 看这条记忆的出处(当时的原话 + 前后文)。'
|
|
65
|
-
+ '对方提到人或旧事、问「你还记得吗」时先查再答。',
|
|
62
|
+
'query 搜记忆、近 7 天原文和工作区笔记;不传看最近。'
|
|
63
|
+
+ 'open 查未完承诺和日程,archive 搜归档(须 query),id 查出处。提到旧事先查再答。',
|
|
66
64
|
parameters: obj(
|
|
67
65
|
{
|
|
68
66
|
query: { type: 'string', description: '关键词就够' },
|