cortico-world-memory-core 0.25.0 → 0.26.1
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 +29 -0
- package/package.json +2 -2
- package/src/config.ts +4 -0
- package/src/recall.ts +114 -8
- package/src/store.ts +4 -0
- package/src/tools.ts +3 -1
- package/src/world.ts +63 -4
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,34 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.26.1
|
|
4
|
+
|
|
5
|
+
**能看出「她到底写没写」了。**
|
|
6
|
+
|
|
7
|
+
- 写入回执带上 8 位编号:`已记下(fact · a1b2c3d4)`。改口仍是 `旧 → 新`。
|
|
8
|
+
- 写入 / 改口 / 元数据修改都记日志(只记 id、类型、字数、是否合并,**不记正文**):
|
|
9
|
+
```text
|
|
10
|
+
[memory-core] 写入 { id: 'a1b2c3d4', kind: 'fact', chars: 32, merged: false }
|
|
11
|
+
```
|
|
12
|
+
工具报错也记一条,方便分清「没调」和「调了失败」。
|
|
13
|
+
- 工具描述里收窄了触发条件:对方**明确说**「记一下 / 记住 / 别忘了」时才必须调用,
|
|
14
|
+
并按语义选 kind;编号只在对方问起时给,避免每句都念编号。
|
|
15
|
+
- 不改卡片内容、不自动写卡;测试 6 → 10 项。
|
|
16
|
+
|
|
17
|
+
## 0.26.0
|
|
18
|
+
|
|
19
|
+
**换一种说法也能搜到了;检索比之前快,还能解释为什么命中。**
|
|
20
|
+
|
|
21
|
+
- **同义词 / 别名扩展**:内置 24 组生活常用词(快递=包裹/取件、猫=猫咪/小猫…),
|
|
22
|
+
配置里还能自己补:`快递=包裹,取件; 奶茶=波霸,珍珠`。
|
|
23
|
+
扩展词权重 0.55——能补漏,但压不过字面命中。
|
|
24
|
+
- **索引缓存**:BM25 倒排不再每轮重建。卡片库有版本号,写卡 / 改口 / 删除 / 导入后自动失效。
|
|
25
|
+
卡片越多,收益越大。
|
|
26
|
+
- **命中解释**:召回行末尾会标 `〔快递+包裹〕`,一眼看出是字面对上还是同义词补进来的。
|
|
27
|
+
- **漏检记录**:搜不到时写一条到 `misses.jsonl`(只在本机,同一查询 10 分钟内只记一次),
|
|
28
|
+
以后按真实缺口补同义词,不用拍脑袋。
|
|
29
|
+
- 同义词只影响召回,不改卡片内容;预激活也同样生效。
|
|
30
|
+
- 测试 `test-recall.ts` 8 项:同义词、缓存失效、改口后可搜、漏检落盘、自定义词表。
|
|
31
|
+
|
|
3
32
|
## 0.25.0
|
|
4
33
|
|
|
5
34
|
**承诺到期终于会提醒了;记忆也能改了。**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cortico-world-memory-core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.26.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "四个工具写卡、查卡、改状态、排日程,改口不覆盖。每轮自动把 2 条相关旧记忆推到眼前,不用模型自己查;搜索分「对得上 / 可能沾边」两档。冷层收走三个月没想起的流水账,不删、可回暖。3D 星图一眼看完整个库。常驻约 1,000 token,本地不联网,MIT。\n\nQQ 交流群:745879632",
|
|
6
6
|
"keywords": [
|
|
@@ -73,6 +73,6 @@
|
|
|
73
73
|
"access": "public"
|
|
74
74
|
},
|
|
75
75
|
"scripts": {
|
|
76
|
-
"test": "node --experimental-transform-types --no-warnings tools/test-store.ts && node --experimental-transform-types --no-warnings tools/test-world.ts && node --experimental-transform-types --no-warnings tools/test-capture.ts && node --experimental-transform-types --no-warnings tools/test-upgrade.ts && node --experimental-transform-types --no-warnings tools/test-urge.ts && node --experimental-transform-types --no-warnings tools/test-delete.ts && node --experimental-transform-types --no-warnings tools/test-remind.ts && node --experimental-transform-types --no-warnings tools/test-update.ts && node --experimental-transform-types --no-warnings tools/test-site-auth.ts && node --experimental-transform-types --no-warnings tools/audit-webui.ts"
|
|
76
|
+
"test": "node --experimental-transform-types --no-warnings tools/test-store.ts && node --experimental-transform-types --no-warnings tools/test-world.ts && node --experimental-transform-types --no-warnings tools/test-capture.ts && node --experimental-transform-types --no-warnings tools/test-upgrade.ts && node --experimental-transform-types --no-warnings tools/test-urge.ts && node --experimental-transform-types --no-warnings tools/test-delete.ts && node --experimental-transform-types --no-warnings tools/test-remind.ts && node --experimental-transform-types --no-warnings tools/test-update.ts && node --experimental-transform-types --no-warnings tools/test-recall.ts && node --experimental-transform-types --no-warnings tools/test-site-auth.ts && node --experimental-transform-types --no-warnings tools/audit-webui.ts"
|
|
77
77
|
}
|
|
78
78
|
}
|
package/src/config.ts
CHANGED
|
@@ -67,6 +67,8 @@ export interface MemoryCoreConfig {
|
|
|
67
67
|
recallUseAging: boolean;
|
|
68
68
|
/** 每轮主动想起几条(0 = 关)。预激活,不占工具调用。 */
|
|
69
69
|
preactivateLines: number;
|
|
70
|
+
/** 额外的同义词组,`快递=包裹,取件`,用分号隔开。内置表之外再补。 */
|
|
71
|
+
synonyms: string;
|
|
70
72
|
/** 多久没被想起就沉到冷层(天)。0 = 关闭自动分层。 */
|
|
71
73
|
chillAfterDays: number;
|
|
72
74
|
/** 每隔多少分钟把状态作为一条事件投递一次(0 = 不投递,最省)。 */
|
|
@@ -128,6 +130,7 @@ export const MEMORY_CORE_DEFAULTS: MemoryCoreConfig = {
|
|
|
128
130
|
recallMaxChars: 600,
|
|
129
131
|
recallUseAging: true,
|
|
130
132
|
preactivateLines: 2,
|
|
133
|
+
synonyms: '',
|
|
131
134
|
/** 对方开口时,把「自我意识」插件独处时写下的那一句带进上下文。 */
|
|
132
135
|
shareSelfThought: true,
|
|
133
136
|
/** 把她独处时特别在意的那些(重要度 8 以上)写成 self 卡片,进星图。 */
|
|
@@ -348,6 +351,7 @@ const SETTINGS_ORDER = [
|
|
|
348
351
|
'worlds.memory-core.siteEnabled',
|
|
349
352
|
'worlds.memory-core.sitePort',
|
|
350
353
|
'worlds.memory-core.preactivateLines',
|
|
354
|
+
'worlds.memory-core.synonyms',
|
|
351
355
|
'worlds.memory-core.shareSelfThought',
|
|
352
356
|
'worlds.memory-core.absorbSelfThought',
|
|
353
357
|
'worlds.memory-core.captureTurns',
|
package/src/recall.ts
CHANGED
|
@@ -62,7 +62,17 @@ const BM25_B = 0.75;
|
|
|
62
62
|
* 给整个候选池建一次倒排:每张卡的词频、长度、以及每个词出现在几张卡里。
|
|
63
63
|
* 卡片数量级是几十到几百,一次构建比「每个词去 includes 一遍」更省。
|
|
64
64
|
*/
|
|
65
|
-
|
|
65
|
+
let cached: { key: string; index: ReturnType<typeof buildIndexRaw> } | null = null;
|
|
66
|
+
|
|
67
|
+
function buildIndex(pool: readonly Card[], cacheKey?: string) {
|
|
68
|
+
if (!cacheKey) return buildIndexRaw(pool);
|
|
69
|
+
if (cached && cached.key === cacheKey) return cached.index;
|
|
70
|
+
const index = buildIndexRaw(pool);
|
|
71
|
+
cached = { key: cacheKey, index };
|
|
72
|
+
return index;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function buildIndexRaw(pool: readonly Card[]) {
|
|
66
76
|
const df = new Map<string, number>();
|
|
67
77
|
const tf = new Map<string, Map<string, number>>();
|
|
68
78
|
const len = new Map<string, number>();
|
|
@@ -80,7 +90,12 @@ function buildIndex(pool: readonly Card[]) {
|
|
|
80
90
|
}
|
|
81
91
|
|
|
82
92
|
/** 一张卡的 BM25 分。字面越贴、词越罕见、卡越短,分越高。 */
|
|
83
|
-
function bm25(
|
|
93
|
+
function bm25(
|
|
94
|
+
index: ReturnType<typeof buildIndex>,
|
|
95
|
+
card: Card,
|
|
96
|
+
tokens: readonly string[],
|
|
97
|
+
weight?: Map<string, number>,
|
|
98
|
+
): number {
|
|
84
99
|
const local = index.tf.get(card.id);
|
|
85
100
|
if (!local) return 0;
|
|
86
101
|
const dl = index.len.get(card.id) ?? 0;
|
|
@@ -88,9 +103,10 @@ function bm25(index: ReturnType<typeof buildIndex>, card: Card, tokens: readonly
|
|
|
88
103
|
for (const token of new Set(tokens)) {
|
|
89
104
|
const f = local.get(token);
|
|
90
105
|
if (!f) continue;
|
|
106
|
+
const w = weight?.get(token) ?? 1;
|
|
91
107
|
const df = index.df.get(token) ?? 0;
|
|
92
108
|
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));
|
|
109
|
+
sum += (w * idf * (f * (BM25_K1 + 1))) / (f + BM25_K1 * (1 - BM25_B + (BM25_B * dl) / index.avg));
|
|
94
110
|
}
|
|
95
111
|
return sum;
|
|
96
112
|
}
|
|
@@ -113,6 +129,81 @@ function freshness(iso: string | undefined, now: number): number {
|
|
|
113
129
|
return 1.1 * Math.exp(-days / 45);
|
|
114
130
|
}
|
|
115
131
|
|
|
132
|
+
/** 内置的常用同义词组:只放「生活里真的会换说法」的,不放近义但语义可能相反的。 */
|
|
133
|
+
const DEFAULT_SYNONYMS: string[][] = [
|
|
134
|
+
['快递', '包裹', '取件', '菜鸟', '驿站'],
|
|
135
|
+
['猫', '猫咪', '小猫', '猫猫'],
|
|
136
|
+
['狗', '狗狗', '小狗', '汪'],
|
|
137
|
+
['吃饭', '用餐', '吃东西', '干饭'],
|
|
138
|
+
['睡觉', '睡了', '晚安', '歇了'],
|
|
139
|
+
['房租', '租金', '交租'],
|
|
140
|
+
['生日', '寿辰', '过生'],
|
|
141
|
+
['下雨', '雨天', '降雨'],
|
|
142
|
+
['感冒', '着凉', '受凉'],
|
|
143
|
+
['手机', '电话', '号码'],
|
|
144
|
+
['工作', '上班', '公司', '单位'],
|
|
145
|
+
['学校', '上课', '校园', '教室'],
|
|
146
|
+
['工资', '薪水', '收入', '发薪'],
|
|
147
|
+
['害怕', '怕', '恐惧', '吓'],
|
|
148
|
+
['开心', '高兴', '快乐', '开怀'],
|
|
149
|
+
['难过', '伤心', '低落', '心里不好受'],
|
|
150
|
+
['累', '疲惫', '困', '乏'],
|
|
151
|
+
['医院', '看病', '挂号', '诊所'],
|
|
152
|
+
['搬家', '搬迁', '挪窝'],
|
|
153
|
+
['考试', '测验', '测试', '期末'],
|
|
154
|
+
['礼物', '礼品', '惊喜'],
|
|
155
|
+
['道歉', '对不起', '抱歉', '不好意思'],
|
|
156
|
+
['谢谢', '感谢', '多谢'],
|
|
157
|
+
['早安', '早上好', '醒了'],
|
|
158
|
+
];
|
|
159
|
+
|
|
160
|
+
/** 把配置里的 `快递=包裹,取件; 猫=猫咪,小猫` 解析成组;写错的段直接跳过。 */
|
|
161
|
+
export function parseSynonyms(raw: string): string[][] {
|
|
162
|
+
const out: string[][] = [];
|
|
163
|
+
for (const part of String(raw ?? '').split(/[;;\n]/)) {
|
|
164
|
+
const m = /^(.+?)[=::](.+)$/.exec(part.trim());
|
|
165
|
+
if (!m) continue;
|
|
166
|
+
const group = [m[1], ...m[2].split(/[,,、\s]+/)].map((x) => x.trim()).filter(Boolean);
|
|
167
|
+
if (group.length >= 2) out.push(group);
|
|
168
|
+
}
|
|
169
|
+
return out;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** token -> 同组其它词;同一组内的词互相映射。 */
|
|
173
|
+
function synonymMap(groups: readonly string[][]): Map<string, string[]> {
|
|
174
|
+
const map = new Map<string, string[]>();
|
|
175
|
+
for (const group of groups) {
|
|
176
|
+
for (const word of group) {
|
|
177
|
+
const others = group.filter((w) => w !== word);
|
|
178
|
+
const prev = map.get(word) ?? [];
|
|
179
|
+
map.set(word, [...new Set([...prev, ...others])]);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
return map;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** 扩展查询词:原词权重 1,同义词 0.55(能补漏,但压不过字面命中)。 */
|
|
186
|
+
export function expandTokens(tokens: readonly string[], rawExtra = ''): { tokens: string[]; weight: Map<string, number> } {
|
|
187
|
+
const groups = [...DEFAULT_SYNONYMS, ...parseSynonyms(rawExtra)];
|
|
188
|
+
const map = synonymMap(groups);
|
|
189
|
+
const weight = new Map<string, number>();
|
|
190
|
+
const out: string[] = [];
|
|
191
|
+
for (const t of tokens) {
|
|
192
|
+
if (!weight.has(t)) {
|
|
193
|
+
weight.set(t, 1);
|
|
194
|
+
out.push(t);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
for (const t of tokens) {
|
|
198
|
+
for (const alt of map.get(t) ?? []) {
|
|
199
|
+
if (weight.has(alt)) continue;
|
|
200
|
+
weight.set(alt, 0.55);
|
|
201
|
+
out.push(alt);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
return { tokens: out, weight };
|
|
205
|
+
}
|
|
206
|
+
|
|
116
207
|
export interface RankOptions {
|
|
117
208
|
query?: string;
|
|
118
209
|
kind?: string;
|
|
@@ -142,6 +233,8 @@ export interface ScoredCard {
|
|
|
142
233
|
tagHit: number;
|
|
143
234
|
/** 命中了几个「实词二元组」——比分数更好解释「像不像」 */
|
|
144
235
|
bigrams: number;
|
|
236
|
+
/** 实际对上的词(含同义词扩展),给召回行做解释。 */
|
|
237
|
+
matched: string[];
|
|
145
238
|
/** strong = 真的对上了,weak = 只是靠权重/新鲜度浮上来的 */
|
|
146
239
|
tier: 'strong' | 'weak';
|
|
147
240
|
}
|
|
@@ -152,7 +245,8 @@ export interface ScoredCard {
|
|
|
152
245
|
*/
|
|
153
246
|
export function rankCardsScored(cards: readonly Card[], opts: RankOptions): ScoredCard[] {
|
|
154
247
|
const now = opts.now ?? Date.now();
|
|
155
|
-
const
|
|
248
|
+
const baseTokens = tokenize(opts.query ?? '');
|
|
249
|
+
const { tokens: queryTokens, weight: tokenWeight } = expandTokens(baseTokens, opts.synonyms);
|
|
156
250
|
let pool = cards.filter((c) => {
|
|
157
251
|
if (opts.kind && c.kind !== opts.kind) return false;
|
|
158
252
|
if (opts.days && opts.days > 0) {
|
|
@@ -173,6 +267,7 @@ export function rankCardsScored(cards: readonly Card[], opts: RankOptions): Scor
|
|
|
173
267
|
lexical: 0,
|
|
174
268
|
tagHit: 0,
|
|
175
269
|
bigrams: 0,
|
|
270
|
+
matched: [],
|
|
176
271
|
tier: 'strong' as const,
|
|
177
272
|
}));
|
|
178
273
|
}
|
|
@@ -202,11 +297,11 @@ export function rankCardsScored(cards: readonly Card[], opts: RankOptions): Scor
|
|
|
202
297
|
for (const [tag, n] of df) if (n > cap) commonTag.add(tag);
|
|
203
298
|
}
|
|
204
299
|
|
|
205
|
-
const index = buildIndex(pool);
|
|
300
|
+
const index = buildIndex(pool, opts.cacheKey ? `${opts.cacheKey}|${opts.kind ?? ''}|${opts.days ?? ''}` : undefined);
|
|
206
301
|
const scored = pool.map((card) => {
|
|
207
302
|
// BM25 取代原来的「二元组命中数 / 长度」:词频和长度都归一过了,
|
|
208
303
|
// 一句话里反复出现同一个词不会把分堆爆,长卡也不会因为词多而虚高。
|
|
209
|
-
const lexical = bm25(index, card, queryTokens);
|
|
304
|
+
const lexical = bm25(index, card, queryTokens, tokenWeight);
|
|
210
305
|
// 标签单独算一次:对方说「生日」时,打了 #生日 的卡要比正文里顺嘴提到的排得靠前。
|
|
211
306
|
// 但泛标签会把所有卡都拉高,所以先剔掉「到处都是」的那些。
|
|
212
307
|
const weakTags = card.tags.filter((t) => !commonTag.has(t));
|
|
@@ -226,10 +321,21 @@ export function rankCardsScored(cards: readonly Card[], opts: RankOptions): Scor
|
|
|
226
321
|
// 双字以上的命中数:预激活用它挡单字噪音
|
|
227
322
|
const hay = `${card.text} ${card.tags.join(' ')}`.toLowerCase();
|
|
228
323
|
let bigrams = 0;
|
|
229
|
-
|
|
324
|
+
// 只用原词计数:同义词补进来的不算「实词命中」,否则分档会虚高
|
|
325
|
+
for (const token of new Set(baseTokens)) {
|
|
230
326
|
if (isContentToken(token) && !common.has(token) && hay.includes(token)) bigrams += 1;
|
|
231
327
|
}
|
|
232
|
-
|
|
328
|
+
// 同义词命中最多算一个实词:能进「对得上」,但不会被当成两个词
|
|
329
|
+
if (!bigrams) {
|
|
330
|
+
const synHit = [...queryTokens].some(
|
|
331
|
+
(t) => (tokenWeight.get(t) ?? 1) < 1 && isContentToken(t) && !common.has(t) && hay.includes(t),
|
|
332
|
+
);
|
|
333
|
+
if (synHit) bigrams = 1;
|
|
334
|
+
}
|
|
335
|
+
const matched = [...new Set([...baseTokens, ...queryTokens])]
|
|
336
|
+
.filter((t) => t.length >= 1 && hay.includes(t))
|
|
337
|
+
.slice(0, 3);
|
|
338
|
+
return { card, score, lexical, tagHit, hit: lexical > 0 || tagHit > 0, bigrams, matched };
|
|
233
339
|
});
|
|
234
340
|
scored.sort((a, b) => b.score - a.score);
|
|
235
341
|
return scored
|
package/src/store.ts
CHANGED
|
@@ -203,6 +203,9 @@ export function demoteTech(kind: CardKind, text: string): CardKind {
|
|
|
203
203
|
}
|
|
204
204
|
|
|
205
205
|
export class CardStore {
|
|
206
|
+
/** 每次落盘 +1。召回索引缓存用它判断「库变没变」,比逐张比对便宜。 */
|
|
207
|
+
revision = 0;
|
|
208
|
+
|
|
206
209
|
private cards: Card[] = [];
|
|
207
210
|
private readonly file: string;
|
|
208
211
|
private readonly backupDir: string;
|
|
@@ -784,6 +787,7 @@ export class CardStore {
|
|
|
784
787
|
}
|
|
785
788
|
|
|
786
789
|
save(force = false): void {
|
|
790
|
+
this.revision += 1;
|
|
787
791
|
if (!this.dirty && !force) return;
|
|
788
792
|
this.dirty = false;
|
|
789
793
|
this.backupOnceADay();
|
package/src/tools.ts
CHANGED
|
@@ -20,7 +20,9 @@ export const TOOL_SPECS: ToolSpec[] = [
|
|
|
20
20
|
'写一条记忆。kind:fact=关于对方的事实,self=关于你自己的,'
|
|
21
21
|
+ 'episode=你自己经历过的(有画面有情绪的事,一天 3-5 条正常),'
|
|
22
22
|
+ 'promise=答应过的事(必须写 dueAt),note=临时信息和技术操作。'
|
|
23
|
-
+ '一条只写一件事,40-80 字。旧记录用 closes 关掉。'
|
|
23
|
+
+ '一条只写一件事,40-80 字。旧记录用 closes 关掉。'
|
|
24
|
+
+ '对方明确说「记一下 / 记住 / 别忘了」时必须调用,按语义选 kind;'
|
|
25
|
+
+ '编号只在对方问起时给。',
|
|
24
26
|
parameters: obj(
|
|
25
27
|
{
|
|
26
28
|
kind: {
|
package/src/world.ts
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
* 2) 不每轮推送状态,只有 contextEveryMin>0 才推,默认 0;
|
|
8
8
|
* 3) 主动开口有「对方最近说过话」这道闸,避免对着空房间调用模型。
|
|
9
9
|
*/
|
|
10
|
-
import { existsSync, readFileSync } from 'node:fs';
|
|
10
|
+
import { appendFileSync, existsSync, readFileSync } from 'node:fs';
|
|
11
11
|
import { fileURLToPath } from 'node:url';
|
|
12
12
|
import { join } from 'node:path';
|
|
13
13
|
import { randomUUID } from 'node:crypto';
|
|
@@ -785,7 +785,13 @@ export class MemoryCoreWorld implements World {
|
|
|
785
785
|
if (name === 'mcore_plan') return this.handlePlan(args);
|
|
786
786
|
return `未知工具:${name}`;
|
|
787
787
|
} catch (error) {
|
|
788
|
-
|
|
788
|
+
const msg = error instanceof Error ? error.message : String(error);
|
|
789
|
+
try {
|
|
790
|
+
this.host?.log?.warn?.('[memory-core] 工具失败', { tool: name, error: msg });
|
|
791
|
+
} catch {
|
|
792
|
+
// ignore
|
|
793
|
+
}
|
|
794
|
+
return `执行失败:${msg}`;
|
|
789
795
|
}
|
|
790
796
|
}
|
|
791
797
|
|
|
@@ -832,6 +838,11 @@ export class MemoryCoreWorld implements World {
|
|
|
832
838
|
});
|
|
833
839
|
if (card.kind === 'promise') this.store.close(card.id);
|
|
834
840
|
this.store.supersede(card.id, res.card);
|
|
841
|
+
try {
|
|
842
|
+
this.host?.log?.info?.('[memory-core] 改口', { from: card.id.slice(0, 8), to: res.card.id.slice(0, 8) });
|
|
843
|
+
} catch {
|
|
844
|
+
// ignore
|
|
845
|
+
}
|
|
835
846
|
return `已改口:${card.id.slice(0, 8)} → ${res.card.id.slice(0, 8)}。旧的那条留在库里可回溯。`;
|
|
836
847
|
}
|
|
837
848
|
|
|
@@ -848,6 +859,11 @@ export class MemoryCoreWorld implements World {
|
|
|
848
859
|
}
|
|
849
860
|
if (!Object.keys(patch).length) return '没有要改的字段(正文相同、元数据也没变)。';
|
|
850
861
|
this.store.update(card.id, patch);
|
|
862
|
+
try {
|
|
863
|
+
this.host?.log?.info?.('[memory-core] 改', { id: card.id.slice(0, 8), fields: Object.keys(patch) });
|
|
864
|
+
} catch {
|
|
865
|
+
// ignore
|
|
866
|
+
}
|
|
851
867
|
return `已改:${card.id.slice(0, 8)}…(${Object.keys(patch).filter((k) => k !== 'remindedAt' && k !== 'remindCount').join('、') || '提醒状态'})`;
|
|
852
868
|
}
|
|
853
869
|
|
|
@@ -908,7 +924,18 @@ export class MemoryCoreWorld implements World {
|
|
|
908
924
|
pile = `\n(提醒:「${top.word}」这个话题已经有 ${top.count} 条了。同一次事别再记一遍——真要合并就写一条新的、用 closes 把旧的收起来;如果它其实是条长期规矩,写成 fact。)`;
|
|
909
925
|
}
|
|
910
926
|
}
|
|
911
|
-
|
|
927
|
+
try {
|
|
928
|
+
this.host?.log?.info?.('[memory-core] 写入', {
|
|
929
|
+
id: res.card.id.slice(0, 8),
|
|
930
|
+
kind,
|
|
931
|
+
chars: text.length,
|
|
932
|
+
merged: res.merged,
|
|
933
|
+
...(closes ? { closes } : {}),
|
|
934
|
+
});
|
|
935
|
+
} catch {
|
|
936
|
+
// 日志失败不影响写入
|
|
937
|
+
}
|
|
938
|
+
return `已记下(${kind}${res.merged ? ',与已有记录合并' : ''} · ${res.card.id.slice(0, 8)})${extra}。共 ${this.store.stats().total} 条。${pile}`;
|
|
912
939
|
}
|
|
913
940
|
|
|
914
941
|
/**
|
|
@@ -951,6 +978,25 @@ export class MemoryCoreWorld implements World {
|
|
|
951
978
|
return this.store.find(prefix) ?? this.store.all().find((c) => c.id.startsWith(prefix)) ?? undefined;
|
|
952
979
|
}
|
|
953
980
|
|
|
981
|
+
private missNote = { q: '', at: 0 };
|
|
982
|
+
|
|
983
|
+
/** 搜不到时记一笔,之后按真实缺口补同义词,不靠拍脑袋。 */
|
|
984
|
+
private logMiss(query: string, kind?: string): void {
|
|
985
|
+
const q = query.trim().slice(0, 80);
|
|
986
|
+
if (!q) return;
|
|
987
|
+
if (this.missNote.q === q && Date.now() - this.missNote.at < 10 * 60_000) return;
|
|
988
|
+
this.missNote = { q, at: Date.now() };
|
|
989
|
+
try {
|
|
990
|
+
appendFileSync(
|
|
991
|
+
join(this.dataDir, 'misses.jsonl'),
|
|
992
|
+
`${JSON.stringify({ ts: new Date().toISOString(), query: q, kind: kind ?? null })}\n`,
|
|
993
|
+
'utf8',
|
|
994
|
+
);
|
|
995
|
+
} catch {
|
|
996
|
+
// 记不上不影响召回
|
|
997
|
+
}
|
|
998
|
+
}
|
|
999
|
+
|
|
954
1000
|
private handleRecall(args: Record<string, unknown>): string {
|
|
955
1001
|
if (!this.cfg().memoryEnabled) return '记忆已关闭(设置页里可以打开)。';
|
|
956
1002
|
const idArg = str(args.id, 60);
|
|
@@ -1002,6 +1048,8 @@ export class MemoryCoreWorld implements World {
|
|
|
1002
1048
|
days: num(args.days, 0, 0, 3650) || undefined,
|
|
1003
1049
|
limit,
|
|
1004
1050
|
useAging: this.cfg().recallUseAging !== false,
|
|
1051
|
+
synonyms: this.cfg().synonyms,
|
|
1052
|
+
cacheKey: String(this.store.revision),
|
|
1005
1053
|
});
|
|
1006
1054
|
const cards = ranked.map((x) => x.card);
|
|
1007
1055
|
const strongIds = new Set(ranked.filter((x) => x.tier === 'strong').map((x) => x.card.id));
|
|
@@ -1027,15 +1075,24 @@ export class MemoryCoreWorld implements World {
|
|
|
1027
1075
|
if (withSrc.length) push('(↩ 是写它时最近的那句原话;卡片本身是改写的)');
|
|
1028
1076
|
if (cards.some((c) => c.coldAt)) push('(❄ 是很久没被想起的旧记忆,仅供参考)');
|
|
1029
1077
|
|
|
1078
|
+
const hitMap = new Map(ranked.map((x) => [x.card.id, x.matched]));
|
|
1030
1079
|
const cardLine = (card: (typeof cards)[number]) => {
|
|
1031
1080
|
const tag = card.kind === 'fact' ? '' : `(${card.kind})`;
|
|
1032
1081
|
const when = card.src ? formatLocal(card.src.ts, tz).slice(5, 10) : '';
|
|
1033
1082
|
const mark = when ? ` ↩${when}` : '';
|
|
1034
1083
|
const cold = card.coldAt ? '❄' : '';
|
|
1084
|
+
// 解释一下为什么它被召回:模型能判断是字面对上,还是同义词补进来的
|
|
1085
|
+
const hit = mode === 'search' ? hitMap.get(card.id) ?? [] : [];
|
|
1086
|
+
const why = hit.length ? ` 〔${hit.join('+')}〕` : '';
|
|
1035
1087
|
// 行首这 8 位就是 id:改口/作废时要用它传给 mcore_write 的 closes
|
|
1036
|
-
return `· ${card.id.slice(0, 8)} ${cold}${formatLocal(card.createdAt, tz).slice(5)} ${clip(card.text, 110)}${tag}${mark}`;
|
|
1088
|
+
return `· ${card.id.slice(0, 8)} ${cold}${formatLocal(card.createdAt, tz).slice(5)} ${clip(card.text, 110)}${tag}${mark}${why}`;
|
|
1037
1089
|
};
|
|
1038
1090
|
|
|
1091
|
+
// 漏检记录:本地留一份,用来补同义词表;同一条查询 10 分钟内只记一次
|
|
1092
|
+
if (mode === 'search' && query && !ranked.some((x) => x.tier === 'strong')) {
|
|
1093
|
+
this.logMiss(query, str(args.kind, 20) || undefined);
|
|
1094
|
+
}
|
|
1095
|
+
|
|
1039
1096
|
// 搜出来的分两档:真对得上的,和只是靠权重/新鲜度浮上来的。
|
|
1040
1097
|
// 不分档的话,模型会把「有点像」当成「就是」,这也是它编故事的起点。
|
|
1041
1098
|
if (mode === 'search' && query && cards.length) {
|
|
@@ -1510,6 +1567,8 @@ export class MemoryCoreWorld implements World {
|
|
|
1510
1567
|
useAging: cfg.recallUseAging !== false,
|
|
1511
1568
|
requireHit: true,
|
|
1512
1569
|
minBigrams: 1,
|
|
1570
|
+
synonyms: cfg.synonyms,
|
|
1571
|
+
cacheKey: String(this.store.revision),
|
|
1513
1572
|
})
|
|
1514
1573
|
.map((x) => ({ card: x.card, adj: x.score * (kindBias[x.card.kind] ?? 1) }))
|
|
1515
1574
|
.sort((a, b) => b.adj - a.adj)
|