cortico-world-memory-core 0.3.2 → 0.4.2
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 +33 -0
- package/ENV_PROMPT.md +5 -5
- package/README.md +17 -5
- package/describe.json +3 -3
- package/package.json +3 -3
- package/src/config.ts +12 -1
- package/src/recall.ts +10 -1
- package/src/site.ts +54 -5
- package/src/store.ts +165 -14
- package/src/tools.ts +3 -3
- package/src/world.ts +151 -5
- package/webui/app.js +2361 -2045
- package/webui/index.html +2 -0
- package/webui/style.css +721 -665
package/src/world.ts
CHANGED
|
@@ -31,7 +31,7 @@ import { importLegacy } from './importer.ts';
|
|
|
31
31
|
import { rankCards, rankRaw } from './recall.ts';
|
|
32
32
|
import { advance, createInitialState, moodLabel } from './state.ts';
|
|
33
33
|
import { formatLocal, hmToMinutes, humanSpan, inWindow, localParts } from './time.ts';
|
|
34
|
-
import { MemorySite, type Snapshot } from './site.ts';
|
|
34
|
+
import { MemorySite, type HealthReport, type ImportReport, type Snapshot } from './site.ts';
|
|
35
35
|
import { expandDirs, loadDocs, searchDocs } from './workspace.ts';
|
|
36
36
|
|
|
37
37
|
const ENV_PROMPT_FILE = fileURLToPath(new URL('../ENV_PROMPT.md', import.meta.url));
|
|
@@ -147,6 +147,16 @@ export class MemoryCoreWorld implements World {
|
|
|
147
147
|
return this.ctx.timezone || 'Asia/Shanghai';
|
|
148
148
|
}
|
|
149
149
|
|
|
150
|
+
/**
|
|
151
|
+
* 周期提醒的天数。老部署的 config.json 里没有这个键(宿主只在首次激活时写入默认值),
|
|
152
|
+
* 所以不能直接 Number(undefined),要回落到默认值。
|
|
153
|
+
*/
|
|
154
|
+
private promiseRepeat(): number {
|
|
155
|
+
const raw = this.cfg().promiseRepeatDays;
|
|
156
|
+
if (raw === undefined || raw === null) return MEMORY_CORE_DEFAULTS.promiseRepeatDays;
|
|
157
|
+
return Math.max(0, Number(raw) || 0);
|
|
158
|
+
}
|
|
159
|
+
|
|
150
160
|
private load(): void {
|
|
151
161
|
try {
|
|
152
162
|
const raw = readJson<Partial<CoreState> | null>(join(this.dataDir, 'state.json'), null);
|
|
@@ -375,7 +385,7 @@ export class MemoryCoreWorld implements World {
|
|
|
375
385
|
scopes: this.store.scopes(),
|
|
376
386
|
},
|
|
377
387
|
docs: { enabled: Boolean(cfg.workspaceScanEnabled), count: docs.length, dirs: cfg.workspaceScanEnabled ? expandDirs(cfg.workspaceDirs, this.ctx.botDir) : [] },
|
|
378
|
-
archive: this.raw.stats(),
|
|
388
|
+
archive: { ...this.raw.stats(), retention: Math.max(0, Number(cfg.rawArchiveDays) || 0) },
|
|
379
389
|
site: { port: origin ? Number(new URL(origin).port) : 0, origin },
|
|
380
390
|
};
|
|
381
391
|
}
|
|
@@ -436,7 +446,23 @@ export class MemoryCoreWorld implements World {
|
|
|
436
446
|
stale: this.store.stale().map((c) => c.id),
|
|
437
447
|
junk: this.store.junk().map((c) => c.id),
|
|
438
448
|
retired: this.store.retired().map((c) => c.id),
|
|
449
|
+
promises: {
|
|
450
|
+
noDue: this.store.promisesWithoutDue().map((c) => c.id),
|
|
451
|
+
overdue: this.store
|
|
452
|
+
.active()
|
|
453
|
+
.filter((c) => c.kind === 'promise' && !c.done && c.dueAt && Date.parse(c.dueAt) <= Date.now())
|
|
454
|
+
.map((c) => c.id),
|
|
455
|
+
},
|
|
439
456
|
}),
|
|
457
|
+
health: () => this.healthReport(),
|
|
458
|
+
importData: (payload: Record<string, unknown>) => this.importBackup(payload),
|
|
459
|
+
archiveSearch: (q: string, days: number, limit: number) => {
|
|
460
|
+
const query = String(q ?? '').trim();
|
|
461
|
+
if (!query) return [];
|
|
462
|
+
const span = Math.min(3650, Math.max(1, Number(days) || 90));
|
|
463
|
+
const cap = Math.min(50, Math.max(1, Number(limit) || 40));
|
|
464
|
+
return rankRaw(this.raw.readArchive(span, 20000), query, cap);
|
|
465
|
+
},
|
|
440
466
|
mergeCards: (keepId: string, dropIds: string[]) => this.store.merge(keepId, dropIds),
|
|
441
467
|
purgeCards: (ids: string[]) => this.store.purge(ids),
|
|
442
468
|
patchState: (input: Record<string, unknown>) => {
|
|
@@ -508,6 +534,119 @@ export class MemoryCoreWorld implements World {
|
|
|
508
534
|
}
|
|
509
535
|
|
|
510
536
|
/** 一轮结束:对方刚说过话才把下一次主动往后推,否则内部 turn 会把 dueAt 一路推没。 */
|
|
537
|
+
/** 健康面板:只报事实,不猜。给的是「现在库长什么样」,不是评分。 */
|
|
538
|
+
private healthReport(): HealthReport {
|
|
539
|
+
const now = Date.now();
|
|
540
|
+
const all = this.store.all();
|
|
541
|
+
const live = this.store.active();
|
|
542
|
+
const day = (ts: string) => formatLocal(ts, this.tz()).slice(0, 10);
|
|
543
|
+
const dead = live.filter((c) => (c.uses || 0) === 0 && now - Date.parse(c.createdAt) > 7 * 86_400_000);
|
|
544
|
+
const count = new Map<string, number>();
|
|
545
|
+
for (const c of live) {
|
|
546
|
+
const d = day(c.createdAt);
|
|
547
|
+
count.set(d, (count.get(d) || 0) + 1);
|
|
548
|
+
}
|
|
549
|
+
const perDay: Array<{ day: string; n: number }> = [];
|
|
550
|
+
for (let i = 13; i >= 0; i -= 1) {
|
|
551
|
+
const d = formatLocal(new Date(now - i * 86_400_000), this.tz()).slice(0, 10);
|
|
552
|
+
perDay.push({ day: d, n: count.get(d) || 0 });
|
|
553
|
+
}
|
|
554
|
+
const open = live.filter((c) => c.kind === 'promise' && !c.done);
|
|
555
|
+
const rawStats = this.raw.stats();
|
|
556
|
+
return {
|
|
557
|
+
generatedAt: new Date().toISOString(),
|
|
558
|
+
cards: { total: all.length, active: live.length, retired: all.length - live.length, dead: dead.length },
|
|
559
|
+
recall: {
|
|
560
|
+
calls: live.reduce((n, c) => n + (c.uses || 0), 0),
|
|
561
|
+
cardsTouched: live.filter((c) => (c.uses || 0) > 0).length,
|
|
562
|
+
},
|
|
563
|
+
perDay,
|
|
564
|
+
promises: {
|
|
565
|
+
open: open.length,
|
|
566
|
+
overdue: open.filter((c) => c.dueAt && Date.parse(c.dueAt) <= now).length,
|
|
567
|
+
noDue: open.filter((c) => !c.dueAt).length,
|
|
568
|
+
repeating: this.promiseRepeat(),
|
|
569
|
+
},
|
|
570
|
+
raw: { lines: this.raw.size, archivedDays: rawStats.days, archivedLines: this.raw.archiveLines() },
|
|
571
|
+
backup: this.store.backupInfo(),
|
|
572
|
+
deadIds: dead.map((c) => c.id),
|
|
573
|
+
};
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
/**
|
|
577
|
+
* 导入备份。只加不覆盖:已有的卡片按 id 或「同类型 + 同文本」跳过。
|
|
578
|
+
* state 只接 rawWatermark 而且只往前推——水位倒回去会把已经录过的原文再录一遍。
|
|
579
|
+
*/
|
|
580
|
+
private importBackup(payload: Record<string, unknown>): ImportReport {
|
|
581
|
+
const warnings: string[] = [];
|
|
582
|
+
const data = (payload.data && typeof payload.data === 'object' ? payload.data : payload) as Record<string, unknown>;
|
|
583
|
+
if (!Array.isArray(data.cards)) warnings.push('没有 cards 字段,只导入了原文和日程。');
|
|
584
|
+
const cards = this.store.importCards(Array.isArray(data.cards) ? data.cards : []);
|
|
585
|
+
|
|
586
|
+
const turnsIn = Array.isArray(data.turns) ? data.turns : [];
|
|
587
|
+
const entries: RawEntry[] = [];
|
|
588
|
+
let badTurns = 0;
|
|
589
|
+
for (const raw of turnsIn) {
|
|
590
|
+
const t = (raw ?? {}) as Partial<RawEntry>;
|
|
591
|
+
const cursor = Number(t.cursor);
|
|
592
|
+
if (!Number.isFinite(cursor) || typeof t.text !== 'string' || !t.text.trim()) {
|
|
593
|
+
badTurns += 1;
|
|
594
|
+
continue;
|
|
595
|
+
}
|
|
596
|
+
entries.push({
|
|
597
|
+
cursor,
|
|
598
|
+
ts: typeof t.ts === 'string' && t.ts ? t.ts : new Date().toISOString(),
|
|
599
|
+
sender: String(t.sender ?? 'import').slice(0, 40),
|
|
600
|
+
text: t.text.slice(0, 200),
|
|
601
|
+
});
|
|
602
|
+
}
|
|
603
|
+
const addedTurns = this.raw.add(entries);
|
|
604
|
+
|
|
605
|
+
const plansIn = Array.isArray(data.plans) ? data.plans : [];
|
|
606
|
+
let plansAdded = 0;
|
|
607
|
+
let plansSkipped = 0;
|
|
608
|
+
for (const raw of plansIn) {
|
|
609
|
+
const p = (raw ?? {}) as Partial<PlanItem>;
|
|
610
|
+
const title = typeof p.title === 'string' ? p.title.trim() : '';
|
|
611
|
+
const time = typeof p.time === 'string' ? p.time.trim() : '';
|
|
612
|
+
if (!title || !time) {
|
|
613
|
+
plansSkipped += 1;
|
|
614
|
+
continue;
|
|
615
|
+
}
|
|
616
|
+
if (this.plans.some((x) => x.time === time && x.title === title)) {
|
|
617
|
+
plansSkipped += 1;
|
|
618
|
+
continue;
|
|
619
|
+
}
|
|
620
|
+
this.plans.push({
|
|
621
|
+
id: typeof p.id === 'string' && p.id ? p.id : randomUUID(),
|
|
622
|
+
time,
|
|
623
|
+
title,
|
|
624
|
+
place: typeof p.place === 'string' ? p.place : undefined,
|
|
625
|
+
done: p.done === true,
|
|
626
|
+
});
|
|
627
|
+
plansAdded += 1;
|
|
628
|
+
}
|
|
629
|
+
if (plansAdded) this.save(true);
|
|
630
|
+
|
|
631
|
+
let watermark = Number(this.state.rawWatermark ?? 0) || 0;
|
|
632
|
+
const st = data.state as Partial<CoreState> | undefined;
|
|
633
|
+
const incoming = Number(st?.rawWatermark);
|
|
634
|
+
if (Number.isFinite(incoming) && incoming > watermark) {
|
|
635
|
+
watermark = incoming;
|
|
636
|
+
this.state.rawWatermark = watermark;
|
|
637
|
+
this.save(true);
|
|
638
|
+
}
|
|
639
|
+
if (badTurns) warnings.push(`${badTurns} 行原文格式不对,已跳过。`);
|
|
640
|
+
if (turnsIn.length && addedTurns === 0) warnings.push('原文都是已有的,没有新增。');
|
|
641
|
+
return {
|
|
642
|
+
cards,
|
|
643
|
+
turns: { added: addedTurns, skipped: turnsIn.length - addedTurns },
|
|
644
|
+
plans: { added: plansAdded, skipped: plansSkipped },
|
|
645
|
+
state: { watermark },
|
|
646
|
+
warnings,
|
|
647
|
+
};
|
|
648
|
+
}
|
|
649
|
+
|
|
511
650
|
onTurnEnded(): void {
|
|
512
651
|
const now = new Date();
|
|
513
652
|
this.lastUserProbe = 0;
|
|
@@ -538,8 +677,14 @@ export class MemoryCoreWorld implements World {
|
|
|
538
677
|
if (!CARD_KINDS.includes(kind)) return 'kind 只能是 fact / self / episode / promise / note。';
|
|
539
678
|
const text = str(args.text, 500);
|
|
540
679
|
if (!text) return 'text 不能为空。';
|
|
541
|
-
const dueAtRaw = str(args.dueAt, 40);
|
|
542
|
-
|
|
680
|
+
const dueAtRaw = str(args.dueAt, 40).trim();
|
|
681
|
+
// 没有时间的承诺等于永远不提醒;但也不能逼模型编一个时间。
|
|
682
|
+
// 所以给一条明确出口:确实没有截止时间就写 none,当成「无截止」存下来,体检页可以补。
|
|
683
|
+
const undated = /^(none|null|无|没有|无截止)$/i.test(dueAtRaw);
|
|
684
|
+
if (kind === 'promise' && !dueAtRaw) {
|
|
685
|
+
return '承诺必须带 dueAt(ISO 时间,例如 2026-11-06T07:20)。不知道具体时间就问对方一句;确实没有截止时间就写 dueAt:"none"。';
|
|
686
|
+
}
|
|
687
|
+
const dueAt = undated ? undefined : dueAtRaw.replace(' ', 'T');
|
|
543
688
|
const res = this.store.add({
|
|
544
689
|
kind,
|
|
545
690
|
text,
|
|
@@ -615,6 +760,7 @@ export class MemoryCoreWorld implements World {
|
|
|
615
760
|
kind: str(args.kind, 20) || undefined,
|
|
616
761
|
days: num(args.days, 0, 0, 3650) || undefined,
|
|
617
762
|
limit,
|
|
763
|
+
useAging: this.cfg().recallUseAging !== false,
|
|
618
764
|
});
|
|
619
765
|
|
|
620
766
|
if (mode === 'search' && query) {
|
|
@@ -938,7 +1084,7 @@ export class MemoryCoreWorld implements World {
|
|
|
938
1084
|
}
|
|
939
1085
|
|
|
940
1086
|
private async maybeRemind(now: Date): Promise<void> {
|
|
941
|
-
const due = this.store.duePromises(now.getTime());
|
|
1087
|
+
const due = this.store.duePromises(now.getTime(), this.promiseRepeat());
|
|
942
1088
|
if (!due.length) return;
|
|
943
1089
|
const lastUser = this.probeLastUser(now.getTime());
|
|
944
1090
|
if (this.cfg().requireRecentUserHours > 0) {
|