cortico-world-memory-core 0.4.4 → 0.13.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 +218 -101
- package/ENV_PROMPT.md +15 -19
- package/README.md +182 -167
- package/describe.json +34 -53
- package/package.json +7 -4
- package/src/config.ts +275 -258
- package/src/recall.ts +263 -114
- package/src/site.ts +15 -1
- package/src/store.ts +124 -0
- package/src/tools.ts +90 -114
- package/src/world.ts +1532 -1199
- package/webui/app.js +3300 -2388
- package/webui/index.html +120 -104
- package/webui/style.css +814 -735
package/src/recall.ts
CHANGED
|
@@ -1,114 +1,263 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 检索:纯本地词法打分,不调模型、不花 token。
|
|
3
|
-
* 中文按二元组 + 单字、英文数字按词切;分数 = 命中 + 权重 + 新鲜度 - 反复使用惩罚。
|
|
4
|
-
*/
|
|
5
|
-
import type { Card, RawEntry } from './store.ts';
|
|
6
|
-
|
|
7
|
-
const STOP = new Set(
|
|
8
|
-
'的了是在我你他她它和就都也不吗呢啊吧有会要说过这个那个把被给对说着地得然后还又很太吗嘛哦呀'.split(''),
|
|
9
|
-
);
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
const
|
|
68
|
-
const
|
|
69
|
-
let
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
return
|
|
114
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* 检索:纯本地词法打分,不调模型、不花 token。
|
|
3
|
+
* 中文按二元组 + 单字、英文数字按词切;分数 = 命中 + 权重 + 新鲜度 - 反复使用惩罚。
|
|
4
|
+
*/
|
|
5
|
+
import type { Card, RawEntry } from './store.ts';
|
|
6
|
+
|
|
7
|
+
const STOP = new Set(
|
|
8
|
+
'的了是在我你他她它和就都也不吗呢啊吧有会要说过这个那个把被给对说着地得然后还又很太吗嘛哦呀'.split(''),
|
|
9
|
+
);
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* 预激活专用:这些双字词在对话里到处都是,命中它们不代表「说到同一件事」。
|
|
13
|
+
* 不在这里挡,「今天好累」会把一堆不相干的卡推上来。
|
|
14
|
+
* 只影响 minBigrams 的计数,不影响普通召回打分。
|
|
15
|
+
*/
|
|
16
|
+
const PRIME_STOP = new Set(
|
|
17
|
+
('今天 明天 昨天 后天 晚上 中午 早上 上午 下午 夜里 现在 刚才 刚刚 回来 回去 出来 起来 一下 一点 一起 '
|
|
18
|
+
+ '时候 然后 真的 可以 什么 怎么 觉得 应该 已经 还是 就是 因为 所以 但是 如果 不过 而且 '
|
|
19
|
+
+ '你们 我们 他们 自己 东西 事情 一个 有点 不是 没有 知道 看到 说过 感觉 需要 之前 之后').split(' '),
|
|
20
|
+
);
|
|
21
|
+
|
|
22
|
+
/** 预激活只认「实词」:含有虚词的二元组(不是/不能/来了/的号)一律不算线索。 */
|
|
23
|
+
export function isContentToken(token: string): boolean {
|
|
24
|
+
if (token.length < 2) return false;
|
|
25
|
+
if (PRIME_STOP.has(token)) return false;
|
|
26
|
+
for (const ch of token) if (STOP.has(ch)) return false;
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function tokenize(text: string): string[] {
|
|
31
|
+
const norm = String(text ?? '')
|
|
32
|
+
.toLowerCase()
|
|
33
|
+
.replace(/[^\p{L}\p{N}]+/gu, ' ')
|
|
34
|
+
.trim();
|
|
35
|
+
if (!norm) return [];
|
|
36
|
+
const out: string[] = [];
|
|
37
|
+
for (const word of norm.split(/\s+/)) {
|
|
38
|
+
if (!word) continue;
|
|
39
|
+
if (/^[a-z0-9]+$/.test(word)) {
|
|
40
|
+
out.push(word);
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
for (let i = 0; i < word.length; i += 1) {
|
|
44
|
+
const ch = word[i];
|
|
45
|
+
if (!STOP.has(ch)) out.push(ch);
|
|
46
|
+
if (i + 1 < word.length) out.push(word.slice(i, i + 2));
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return out;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** 供工作区文件检索复用:同一套切词与命中打分。 */
|
|
53
|
+
export function scoreText(queryTokens: readonly string[], text: string): number {
|
|
54
|
+
return overlap(queryTokens, text);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** BM25 参数:k1 控制词频饱和,b 控制长度归一。都是常用值。 */
|
|
58
|
+
const BM25_K1 = 1.2;
|
|
59
|
+
const BM25_B = 0.75;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* 给整个候选池建一次倒排:每张卡的词频、长度、以及每个词出现在几张卡里。
|
|
63
|
+
* 卡片数量级是几十到几百,一次构建比「每个词去 includes 一遍」更省。
|
|
64
|
+
*/
|
|
65
|
+
function buildIndex(pool: readonly Card[]) {
|
|
66
|
+
const df = new Map<string, number>();
|
|
67
|
+
const tf = new Map<string, Map<string, number>>();
|
|
68
|
+
const len = new Map<string, number>();
|
|
69
|
+
let total = 0;
|
|
70
|
+
for (const card of pool) {
|
|
71
|
+
const tokens = tokenize(card.text);
|
|
72
|
+
const local = new Map<string, number>();
|
|
73
|
+
for (const t of tokens) local.set(t, (local.get(t) ?? 0) + 1);
|
|
74
|
+
tf.set(card.id, local);
|
|
75
|
+
len.set(card.id, tokens.length);
|
|
76
|
+
total += tokens.length;
|
|
77
|
+
for (const t of local.keys()) df.set(t, (df.get(t) ?? 0) + 1);
|
|
78
|
+
}
|
|
79
|
+
return { df, tf, len, avg: pool.length ? total / pool.length || 1 : 1, n: pool.length };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** 一张卡的 BM25 分。字面越贴、词越罕见、卡越短,分越高。 */
|
|
83
|
+
function bm25(index: ReturnType<typeof buildIndex>, card: Card, tokens: readonly string[]): number {
|
|
84
|
+
const local = index.tf.get(card.id);
|
|
85
|
+
if (!local) return 0;
|
|
86
|
+
const dl = index.len.get(card.id) ?? 0;
|
|
87
|
+
let sum = 0;
|
|
88
|
+
for (const token of new Set(tokens)) {
|
|
89
|
+
const f = local.get(token);
|
|
90
|
+
if (!f) continue;
|
|
91
|
+
const df = index.df.get(token) ?? 0;
|
|
92
|
+
const idf = Math.log(1 + (index.n - df + 0.5) / (df + 0.5));
|
|
93
|
+
sum += (idf * (f * (BM25_K1 + 1))) / (f + BM25_K1 * (1 - BM25_B + (BM25_B * dl) / index.avg));
|
|
94
|
+
}
|
|
95
|
+
return sum;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function overlap(queryTokens: readonly string[], text: string): number {
|
|
99
|
+
if (!queryTokens.length) return 0;
|
|
100
|
+
const hay = String(text ?? '').toLowerCase();
|
|
101
|
+
let sum = 0;
|
|
102
|
+
for (const token of new Set(queryTokens)) {
|
|
103
|
+
if (!hay.includes(token)) continue;
|
|
104
|
+
sum += token.length > 1 ? 1.4 : 0.18;
|
|
105
|
+
}
|
|
106
|
+
return sum / Math.sqrt(new Set(queryTokens).size + 1);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function freshness(iso: string | undefined, now: number): number {
|
|
110
|
+
const at = Date.parse(iso ?? '');
|
|
111
|
+
if (!Number.isFinite(at)) return 0;
|
|
112
|
+
const days = Math.max(0, (now - at) / 86_400_000);
|
|
113
|
+
return 1.1 * Math.exp(-days / 45);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export interface RankOptions {
|
|
117
|
+
query?: string;
|
|
118
|
+
kind?: string;
|
|
119
|
+
days?: number;
|
|
120
|
+
limit: number;
|
|
121
|
+
now?: number;
|
|
122
|
+
/** 是否参考「上次被想起的时间」。关掉就退回只看关键词、标签、权重和新鲜度。 */
|
|
123
|
+
useAging?: boolean;
|
|
124
|
+
/**
|
|
125
|
+
* 只保留真的被线索命中的卡(关键词或标签重叠 > 0)。
|
|
126
|
+
* 每轮预激活必须开这个:否则一张高权重的无关卡会靠 weight 混进「想起来了」。
|
|
127
|
+
*/
|
|
128
|
+
requireHit?: boolean;
|
|
129
|
+
/**
|
|
130
|
+
* 至少命中几个「双字以上」的词。中文单字太容易撞(今/天/好),
|
|
131
|
+
* 只靠 requireHit 的话「今天好累」能把半库都捞上来。
|
|
132
|
+
*/
|
|
133
|
+
minBigrams?: number;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export interface ScoredCard {
|
|
137
|
+
card: Card;
|
|
138
|
+
score: number;
|
|
139
|
+
/** 词面重叠(归一化后) */
|
|
140
|
+
lexical: number;
|
|
141
|
+
/** 标签命中 */
|
|
142
|
+
tagHit: number;
|
|
143
|
+
/** 命中了几个「实词二元组」——比分数更好解释「像不像」 */
|
|
144
|
+
bigrams: number;
|
|
145
|
+
/** strong = 真的对上了,weak = 只是靠权重/新鲜度浮上来的 */
|
|
146
|
+
tier: 'strong' | 'weak';
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* 和 rankCards 同一套打分,但把分数和判断依据一起返回。
|
|
151
|
+
* 召回要分两档口气,就靠这里的 tier,不在外面重新算一遍。
|
|
152
|
+
*/
|
|
153
|
+
export function rankCardsScored(cards: readonly Card[], opts: RankOptions): ScoredCard[] {
|
|
154
|
+
const now = opts.now ?? Date.now();
|
|
155
|
+
const queryTokens = tokenize(opts.query ?? '');
|
|
156
|
+
let pool = cards.filter((c) => {
|
|
157
|
+
if (opts.kind && c.kind !== opts.kind) return false;
|
|
158
|
+
if (opts.days && opts.days > 0) {
|
|
159
|
+
const at = Date.parse(c.createdAt);
|
|
160
|
+
if (Number.isFinite(at) && now - at > opts.days * 86_400_000) return false;
|
|
161
|
+
}
|
|
162
|
+
return true;
|
|
163
|
+
});
|
|
164
|
+
if (!queryTokens.length) {
|
|
165
|
+
pool = [...pool].sort((a, b) => {
|
|
166
|
+
const sa = freshness(a.createdAt, now) + a.weight * 0.6;
|
|
167
|
+
const sb = freshness(b.createdAt, now) + b.weight * 0.6;
|
|
168
|
+
return sb - sa;
|
|
169
|
+
});
|
|
170
|
+
return pool.slice(0, opts.limit).map((card) => ({
|
|
171
|
+
card,
|
|
172
|
+
score: 0,
|
|
173
|
+
lexical: 0,
|
|
174
|
+
tagHit: 0,
|
|
175
|
+
bigrams: 0,
|
|
176
|
+
tier: 'strong' as const,
|
|
177
|
+
}));
|
|
178
|
+
}
|
|
179
|
+
// 「今天」「回来」这类词到处都是,命中它们不算线索。文档频率只能挡掉极端常见的,
|
|
180
|
+
// 挡不住「今天」(5/66)这种——所以再加一张停用词表。
|
|
181
|
+
const common = new Set<string>();
|
|
182
|
+
if (opts.minBigrams) {
|
|
183
|
+
const cap = Math.max(3, Math.ceil(pool.length * 0.3));
|
|
184
|
+
for (const token of new Set(queryTokens)) {
|
|
185
|
+
if (token.length < 2) continue;
|
|
186
|
+
let n = 0;
|
|
187
|
+
for (const c of pool) {
|
|
188
|
+
if (`${c.text} ${c.tags.join(' ')}`.toLowerCase().includes(token)) {
|
|
189
|
+
n += 1;
|
|
190
|
+
if (n > cap) break;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
if (n > cap) common.add(token);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
// 标签也过一遍 IDF:出现在三成以上卡里的标签(「日常」「工作」)不该算命中
|
|
197
|
+
const commonTag = new Set<string>();
|
|
198
|
+
{
|
|
199
|
+
const cap = Math.max(3, Math.ceil(pool.length * 0.3));
|
|
200
|
+
const df = new Map<string, number>();
|
|
201
|
+
for (const c of pool) for (const t of c.tags ?? []) df.set(t, (df.get(t) ?? 0) + 1);
|
|
202
|
+
for (const [tag, n] of df) if (n > cap) commonTag.add(tag);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const index = buildIndex(pool);
|
|
206
|
+
const scored = pool.map((card) => {
|
|
207
|
+
// BM25 取代原来的「二元组命中数 / 长度」:词频和长度都归一过了,
|
|
208
|
+
// 一句话里反复出现同一个词不会把分堆爆,长卡也不会因为词多而虚高。
|
|
209
|
+
const lexical = bm25(index, card, queryTokens);
|
|
210
|
+
// 标签单独算一次:对方说「生日」时,打了 #生日 的卡要比正文里顺嘴提到的排得靠前。
|
|
211
|
+
// 但泛标签会把所有卡都拉高,所以先剔掉「到处都是」的那些。
|
|
212
|
+
const weakTags = card.tags.filter((t) => !commonTag.has(t));
|
|
213
|
+
const tagHit = weakTags.length ? overlap(queryTokens, weakTags.join(' ')) : 0;
|
|
214
|
+
// 同一张卡被反复召回只做有限的边际压制(封顶 8 次),别让常用的重要记忆越用越沉
|
|
215
|
+
const used = Math.min(card.uses || 0, 8) * 0.03;
|
|
216
|
+
// 最近被想起过的卡给一点小加成:三个月没动过的旧卡不该把上周刚聊过的挤下去。
|
|
217
|
+
// 权重刻意压得很小(0.15 上限),压不倒一个关键词命中(2 分)。
|
|
218
|
+
const lastUsed = Date.parse(card.lastUsedAt ?? '');
|
|
219
|
+
const glow =
|
|
220
|
+
opts.useAging && Number.isFinite(lastUsed)
|
|
221
|
+
? 0.15 * Math.exp(-(now - lastUsed) / (30 * 86_400_000))
|
|
222
|
+
: 0;
|
|
223
|
+
// BM25 的量级是 0~25,乘 0.25 压回和旧版相近的比例:正文相关仍占主导,
|
|
224
|
+
// 但权重 / 新鲜度 / 用进废退还能在近似分数之间起作用。
|
|
225
|
+
const score = lexical * 0.25 + tagHit * 1.2 + card.weight * 0.5 + freshness(card.createdAt, now) + glow - used;
|
|
226
|
+
// 双字以上的命中数:预激活用它挡单字噪音
|
|
227
|
+
const hay = `${card.text} ${card.tags.join(' ')}`.toLowerCase();
|
|
228
|
+
let bigrams = 0;
|
|
229
|
+
for (const token of new Set(queryTokens)) {
|
|
230
|
+
if (isContentToken(token) && !common.has(token) && hay.includes(token)) bigrams += 1;
|
|
231
|
+
}
|
|
232
|
+
return { card, score, lexical, tagHit, hit: lexical > 0 || tagHit > 0, bigrams };
|
|
233
|
+
});
|
|
234
|
+
scored.sort((a, b) => b.score - a.score);
|
|
235
|
+
return scored
|
|
236
|
+
.filter((x) => x.score > 0.25 && (!opts.requireHit || x.hit) && (!opts.minBigrams || x.bigrams >= opts.minBigrams))
|
|
237
|
+
.slice(0, opts.limit)
|
|
238
|
+
.map((x) => ({
|
|
239
|
+
...x,
|
|
240
|
+
// 两个实词对上、或一个实词且词面确实重合,才算「对得上」;
|
|
241
|
+
// 只靠权重和新鲜度浮上来的,一律算「可能沾边」。
|
|
242
|
+
// 分档靠「实词命中数」而不是裸分:BM25 在「今天」这种高频词上也会给分,
|
|
243
|
+
// 但停用词表已经把这类词从 bigrams 里剔掉了,用它分档才不会被带偏。
|
|
244
|
+
tier: (x.bigrams >= 2 || (x.bigrams >= 1 && x.lexical >= 2.2) || x.tagHit >= 0.6
|
|
245
|
+
? 'strong'
|
|
246
|
+
: 'weak') as 'strong' | 'weak',
|
|
247
|
+
}));
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
export function rankCards(cards: readonly Card[], opts: RankOptions): Card[] {
|
|
251
|
+
return rankCardsScored(cards, opts).map((x) => x.card);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
export function rankRaw(entries: readonly RawEntry[], query: string, limit: number): RawEntry[] {
|
|
255
|
+
const queryTokens = tokenize(query);
|
|
256
|
+
if (!queryTokens.length) return [];
|
|
257
|
+
const scored = entries.map((entry) => ({
|
|
258
|
+
entry,
|
|
259
|
+
score: overlap(queryTokens, entry.text) * 2 + freshness(entry.ts, Date.now()) * 0.4,
|
|
260
|
+
}));
|
|
261
|
+
scored.sort((a, b) => b.score - a.score);
|
|
262
|
+
return scored.filter((x) => x.score > 0.8).slice(0, limit).map((x) => x.entry);
|
|
263
|
+
}
|
package/src/site.ts
CHANGED
|
@@ -70,12 +70,15 @@ export interface MaintenanceReport {
|
|
|
70
70
|
export interface HealthReport {
|
|
71
71
|
generatedAt: string;
|
|
72
72
|
cards: { total: number; active: number; retired: number; dead: number };
|
|
73
|
-
recall: { calls: number; cardsTouched: number };
|
|
73
|
+
recall: { calls: number; cardsTouched: number; primed: number };
|
|
74
74
|
perDay: Array<{ day: string; n: number }>;
|
|
75
75
|
promises: { open: number; overdue: number; noDue: number; repeating: number };
|
|
76
76
|
raw: { lines: number; archivedDays: number; archivedLines: number };
|
|
77
77
|
backup: { count: number; latest: string | null; dir: string };
|
|
78
|
+
/** 冷层:沉下去多少、还能再沉多少、门槛多少天。 */
|
|
79
|
+
chill: { cold: number; eligible: number; afterDays: number };
|
|
78
80
|
deadIds: string[];
|
|
81
|
+
coldIds: string[];
|
|
79
82
|
}
|
|
80
83
|
|
|
81
84
|
export interface ImportReport {
|
|
@@ -105,6 +108,9 @@ export interface SiteHooks {
|
|
|
105
108
|
archiveSearch(q: string, days: number, limit: number): RawEntry[];
|
|
106
109
|
/** 删掉一条原文(热数据或归档)。 */
|
|
107
110
|
deleteTurn(cursor: number): boolean;
|
|
111
|
+
/** 把记忆沉到冷层 / 从冷层捞回来。 */
|
|
112
|
+
chillCards(ids: string[]): number;
|
|
113
|
+
warmCards(ids: string[]): number;
|
|
108
114
|
patchState(input: Record<string, unknown>): CoreState;
|
|
109
115
|
planAction(input: Record<string, unknown>): unknown;
|
|
110
116
|
exportAll(fmt: string): { body: string; mime: string; filename: string };
|
|
@@ -256,6 +262,14 @@ export class MemorySite {
|
|
|
256
262
|
const ids = Array.isArray(body.ids) ? (body.ids as unknown[]).map((x) => String(x)) : [];
|
|
257
263
|
return this.json(rsp, 200, { removed: this.hooks.purgeCards(ids) });
|
|
258
264
|
}
|
|
265
|
+
if (path === '/api/chill' || path === '/api/warm') {
|
|
266
|
+
if (req.method !== 'POST') return this.text(rsp, 405, 'method not allowed');
|
|
267
|
+
const body = await this.body(req);
|
|
268
|
+
const ids = Array.isArray(body.ids) ? (body.ids as unknown[]).map((x) => String(x)) : [];
|
|
269
|
+
// 不带 ids 就是「全部符合条件的」:冷层一键沉、一键回暖
|
|
270
|
+
const n = path === '/api/chill' ? this.hooks.chillCards(ids) : this.hooks.warmCards(ids);
|
|
271
|
+
return this.json(rsp, 200, { changed: n });
|
|
272
|
+
}
|
|
259
273
|
if (path === '/api/health' && req.method === 'GET') {
|
|
260
274
|
return this.json(rsp, 200, this.hooks.health());
|
|
261
275
|
}
|
package/src/store.ts
CHANGED
|
@@ -37,6 +37,20 @@ export interface Card {
|
|
|
37
37
|
supersedes?: string[];
|
|
38
38
|
/** 记忆域:main(自己写的)/ import(旧插件导入的),给以后接 QQ 分流留位。 */
|
|
39
39
|
scope?: string;
|
|
40
|
+
/**
|
|
41
|
+
* 写这条时最近的一句原文(游标 + 时间)。卡片是模型改写过的结论,
|
|
42
|
+
* 不是原话;留一个指针,召回时才敢说「这条能追到当时的哪一句」。
|
|
43
|
+
*/
|
|
44
|
+
/** 写这条时,对方最近说的那句原话(截断 80 字,已脱敏)。有它才追得回证据。 */
|
|
45
|
+
src?: { cursor: number; ts: string; text?: string };
|
|
46
|
+
/** 被「主动想起」过几次(每轮预激活),和 uses(工具召回)分开记。 */
|
|
47
|
+
primedCount?: number;
|
|
48
|
+
lastPrimedAt?: string;
|
|
49
|
+
/**
|
|
50
|
+
* 冷层标记:长期没被想起、又不重要的卡沉到这一层。
|
|
51
|
+
* 不是删除——默认不进召回和预激活,显式搜索仍然找得到,一键回暖。
|
|
52
|
+
*/
|
|
53
|
+
coldAt?: string;
|
|
40
54
|
}
|
|
41
55
|
|
|
42
56
|
export interface RawEntry {
|
|
@@ -67,6 +81,10 @@ export interface CoreState {
|
|
|
67
81
|
proactiveDay?: string;
|
|
68
82
|
proactiveCount?: number;
|
|
69
83
|
lastNightlyDay?: string;
|
|
84
|
+
/** 夜里备好的整理材料:等下一次开口时再交付,别在凌晨三点推给没人看的会话。 */
|
|
85
|
+
nightlyPending?: { day: string; text: string };
|
|
86
|
+
/** 冷层每天只判一次,记下今天判过了。 */
|
|
87
|
+
lastChillDay?: string;
|
|
70
88
|
rawWatermark?: number;
|
|
71
89
|
/** 水位是按哪一版捕获逻辑推进的。旧版用了错的 range 语义,水位不可信,见 CAPTURE_WATERMARK_VERSION。 */
|
|
72
90
|
watermarkVersion?: number;
|
|
@@ -211,6 +229,73 @@ export class CardStore {
|
|
|
211
229
|
return this.cards.filter((c) => c.status !== 'retired');
|
|
212
230
|
}
|
|
213
231
|
|
|
232
|
+
/** 能进「主动想起」和「最近」的那一层:在用的、还没沉到冷层的。 */
|
|
233
|
+
recallable(): Card[] {
|
|
234
|
+
return this.cards.filter((c) => c.status !== 'retired' && !c.coldAt);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
cold(): Card[] {
|
|
238
|
+
return this.cards.filter((c) => c.status !== 'retired' && Boolean(c.coldAt));
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* 该冷的卡:好久没被碰过(工具召回和预激活都算)、权重不高、不是没做完的承诺。
|
|
243
|
+
* 只是候选,真正下沉要显式调用 chill()——分层必须是可逆的、看得见的。
|
|
244
|
+
*/
|
|
245
|
+
chillEligible(days = 30, now = Date.now()): Card[] {
|
|
246
|
+
const span = Math.max(0, Number(days) || 0);
|
|
247
|
+
if (!span) return [];
|
|
248
|
+
const cutoff = now - span * 86_400_000;
|
|
249
|
+
const lastTouch = (c: Card) =>
|
|
250
|
+
Math.max(
|
|
251
|
+
Date.parse(c.createdAt) || 0,
|
|
252
|
+
Date.parse(c.lastUsedAt ?? '') || 0,
|
|
253
|
+
Date.parse(c.lastPrimedAt ?? '') || 0,
|
|
254
|
+
);
|
|
255
|
+
return this.active()
|
|
256
|
+
.filter((c) => {
|
|
257
|
+
if (c.coldAt) return false;
|
|
258
|
+
// 只沉流水账和临时信息;fact / self / 未完成的承诺是「人」,不能沉
|
|
259
|
+
if (c.kind !== 'episode' && c.kind !== 'note') return false;
|
|
260
|
+
if (c.weight > 0.45) return false;
|
|
261
|
+
if ((c.uses || 0) + (c.primedCount || 0) > 1) return false;
|
|
262
|
+
return lastTouch(c) < cutoff;
|
|
263
|
+
})
|
|
264
|
+
.sort((a, b) => lastTouch(a) - lastTouch(b)); // 最久没碰的排前面,配额先吃它们
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
chill(ids: readonly string[]): number {
|
|
268
|
+
const at = new Date().toISOString();
|
|
269
|
+
let n = 0;
|
|
270
|
+
for (const id of ids) {
|
|
271
|
+
const card = this.find(id);
|
|
272
|
+
if (!card || card.status === 'retired' || card.coldAt) continue;
|
|
273
|
+
card.coldAt = at;
|
|
274
|
+
n += 1;
|
|
275
|
+
}
|
|
276
|
+
if (n) {
|
|
277
|
+
this.dirty = true;
|
|
278
|
+
this.save(true);
|
|
279
|
+
}
|
|
280
|
+
return n;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
warm(ids: readonly string[]): number {
|
|
284
|
+
let n = 0;
|
|
285
|
+
for (const id of ids) {
|
|
286
|
+
const card = this.find(id);
|
|
287
|
+
if (!card || !card.coldAt) continue;
|
|
288
|
+
delete card.coldAt;
|
|
289
|
+
card.updatedAt = new Date().toISOString();
|
|
290
|
+
n += 1;
|
|
291
|
+
}
|
|
292
|
+
if (n) {
|
|
293
|
+
this.dirty = true;
|
|
294
|
+
this.save(true);
|
|
295
|
+
}
|
|
296
|
+
return n;
|
|
297
|
+
}
|
|
298
|
+
|
|
214
299
|
retired(): Card[] {
|
|
215
300
|
return this.cards.filter((c) => c.status === 'retired');
|
|
216
301
|
}
|
|
@@ -250,6 +335,8 @@ export class CardStore {
|
|
|
250
335
|
origin?: string;
|
|
251
336
|
createdAt?: string;
|
|
252
337
|
scope?: string;
|
|
338
|
+
/** 写这条时,对方最近说的那句原话(截断 80 字,已脱敏)。有它才追得回证据。 */
|
|
339
|
+
src?: { cursor: number; ts: string; text?: string };
|
|
253
340
|
}): { card: Card; merged: boolean } {
|
|
254
341
|
const text = String(input.text ?? '').replace(/\s+/g, ' ').trim().slice(0, 500);
|
|
255
342
|
const key = normalizeText(text);
|
|
@@ -282,6 +369,7 @@ export class CardStore {
|
|
|
282
369
|
scope: String(input.scope ?? 'main').slice(0, 24),
|
|
283
370
|
...(input.dueAt ? { dueAt: input.dueAt } : {}),
|
|
284
371
|
...(input.kind === 'promise' ? { done: false } : {}),
|
|
372
|
+
...(input.src ? { src: input.src } : {}),
|
|
285
373
|
};
|
|
286
374
|
this.cards.push(card);
|
|
287
375
|
this.dirty = true;
|
|
@@ -359,6 +447,8 @@ export class CardStore {
|
|
|
359
447
|
status: 'active',
|
|
360
448
|
uses: old.uses,
|
|
361
449
|
supersedes: [...(old.supersedes ?? []), old.id],
|
|
450
|
+
// 文本换了,旧的出处就对不上了,别再挂一个会骗人的 ↩
|
|
451
|
+
src: undefined,
|
|
362
452
|
};
|
|
363
453
|
delete next.retiredAt;
|
|
364
454
|
delete next.retiredBy;
|
|
@@ -521,11 +611,30 @@ export class CardStore {
|
|
|
521
611
|
if (!card) continue;
|
|
522
612
|
card.uses = (card.uses || 0) + 1;
|
|
523
613
|
card.lastUsedAt = now;
|
|
614
|
+
// 真被用上了就不该还算「冷」的,顺手回暖
|
|
615
|
+
delete card.coldAt;
|
|
524
616
|
}
|
|
525
617
|
this.dirty = true;
|
|
526
618
|
this.save(true);
|
|
527
619
|
}
|
|
528
620
|
|
|
621
|
+
/**
|
|
622
|
+
* 记「主动想起」的账。和 touch 分开:预激活是系统推的,工具召回是模型要的。
|
|
623
|
+
* 两者都算「被用过」,但只有后者说明模型真的去查了。
|
|
624
|
+
*/
|
|
625
|
+
prime(ids: readonly string[]): void {
|
|
626
|
+
if (!ids.length) return;
|
|
627
|
+
const now = new Date().toISOString();
|
|
628
|
+
for (const id of ids) {
|
|
629
|
+
const card = this.find(id);
|
|
630
|
+
if (!card) continue;
|
|
631
|
+
card.primedCount = (card.primedCount || 0) + 1;
|
|
632
|
+
card.lastPrimedAt = now;
|
|
633
|
+
}
|
|
634
|
+
this.dirty = true;
|
|
635
|
+
this.save();
|
|
636
|
+
}
|
|
637
|
+
|
|
529
638
|
openPromises(limit = 5, now = Date.now()): Card[] {
|
|
530
639
|
return this.cards
|
|
531
640
|
.filter((c) => c.kind === 'promise' && !c.done && c.status !== 'retired')
|
|
@@ -609,6 +718,21 @@ export class CardStore {
|
|
|
609
718
|
retiredBy: typeof c.retiredBy === 'string' ? c.retiredBy : undefined,
|
|
610
719
|
supersedes: Array.isArray(c.supersedes) ? (c.supersedes as unknown[]).map((x) => String(x)).slice(0, 20) : undefined,
|
|
611
720
|
scope: typeof c.scope === 'string' && c.scope ? c.scope.slice(0, 20) : 'import',
|
|
721
|
+
src:
|
|
722
|
+
c.src && typeof c.src === 'object' && Number.isFinite(Number((c.src as { cursor?: unknown }).cursor))
|
|
723
|
+
&& typeof (c.src as { ts?: unknown }).ts === 'string'
|
|
724
|
+
? {
|
|
725
|
+
cursor: Number((c.src as { cursor: number }).cursor),
|
|
726
|
+
ts: String((c.src as { ts: string }).ts),
|
|
727
|
+
text:
|
|
728
|
+
typeof (c.src as { text?: unknown }).text === 'string'
|
|
729
|
+
? String((c.src as { text: string }).text).slice(0, 80)
|
|
730
|
+
: undefined,
|
|
731
|
+
}
|
|
732
|
+
: undefined,
|
|
733
|
+
primedCount: Number.isFinite(Number(c.primedCount)) ? Number(c.primedCount) : undefined,
|
|
734
|
+
lastPrimedAt: typeof c.lastPrimedAt === 'string' ? c.lastPrimedAt : undefined,
|
|
735
|
+
coldAt: typeof c.coldAt === 'string' ? c.coldAt : undefined,
|
|
612
736
|
};
|
|
613
737
|
this.cards.push(card);
|
|
614
738
|
byId.add(id);
|