feiyu-brain 1.5.0 → 1.5.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/README.md +1 -1
- package/cli.mjs +31 -1
- package/cordis.patch.yml +13 -1
- package/core/bond.js +186 -0
- package/core/concept.js +138 -0
- package/core/extract.js +75 -2
- package/core/memory.js +539 -18
- package/core/persona.js +91 -4
- package/core/relation.js +78 -0
- package/core/self.js +72 -0
- package/docs/MEMORY.md +180 -15
- package/index.js +574 -44
- package/package.json +1 -1
- package/selftest.mjs +37 -3
- package/store.js +141 -10
package/package.json
CHANGED
package/selftest.mjs
CHANGED
|
@@ -101,13 +101,47 @@ t('蜕皮后 memory.formerSelves 里查得到', () => {
|
|
|
101
101
|
});
|
|
102
102
|
|
|
103
103
|
console.log('\n── 记忆 ──');
|
|
104
|
-
t('
|
|
105
|
-
const
|
|
106
|
-
|
|
104
|
+
t('30 天没人提 → 压扁成一句"大概"(不是删除)', () => {
|
|
105
|
+
const now = Date.now();
|
|
106
|
+
const m = {
|
|
107
|
+
episodes: [{ id: 'a', kind: 'episode', who: '扶摇', what: '你嘴真硬', at: now - 31 * 86400000, lastRecall: now - 31 * 86400000, consecutiveDays: 1, longTerm: false }],
|
|
108
|
+
imagination: [], facts: {}, impressions: {}, forgotten: [], cards: [],
|
|
109
|
+
};
|
|
110
|
+
settle(m, now);
|
|
111
|
+
assert.equal(m.episodes.length, 1, '不该被删掉');
|
|
112
|
+
assert.equal(m.episodes[0].compressed, true);
|
|
113
|
+
assert.ok(m.episodes[0].what.includes('记不清'), m.episodes[0].what);
|
|
114
|
+
assert.equal(m.forgotten.length, 0);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
t('2 年没人提 → 才真的遗忘', () => {
|
|
118
|
+
const now = Date.now();
|
|
119
|
+
const DAY = 86400000;
|
|
120
|
+
const m = {
|
|
121
|
+
episodes: [{
|
|
122
|
+
id: 'a', kind: 'episode', who: '扶摇', what: '两年前的事',
|
|
123
|
+
at: now - 800 * DAY, lastRecall: now - 800 * DAY,
|
|
124
|
+
compressed: true, cardedAt: now - 800 * DAY, expiresAt: now - 70 * DAY,
|
|
125
|
+
consecutiveDays: 1, longTerm: false,
|
|
126
|
+
}],
|
|
127
|
+
imagination: [], facts: {}, impressions: {}, forgotten: [], cards: [],
|
|
128
|
+
};
|
|
129
|
+
settle(m, now);
|
|
107
130
|
assert.equal(m.episodes.length, 0);
|
|
108
131
|
assert.equal(m.forgotten.length, 1);
|
|
109
132
|
});
|
|
110
133
|
|
|
134
|
+
t('一直被提起 → 压不了也忘不了', () => {
|
|
135
|
+
const now = Date.now();
|
|
136
|
+
const m = {
|
|
137
|
+
episodes: [{ id: 'a', kind: 'episode', what: '常聊的事', at: now - 500 * 86400000, lastRecall: now, consecutiveDays: 20, longTerm: false }],
|
|
138
|
+
imagination: [], facts: {}, impressions: {}, forgotten: [], cards: [],
|
|
139
|
+
};
|
|
140
|
+
settle(m, now);
|
|
141
|
+
assert.equal(m.episodes.length, 1);
|
|
142
|
+
assert.ok(!m.episodes[0].compressed, '不该被压扁');
|
|
143
|
+
});
|
|
144
|
+
|
|
111
145
|
t('连续 15 天升级长期,永不衰减', () => {
|
|
112
146
|
const m = { episodes: [{ id: 'b', kind: 'episode', lastRecall: Date.now() - 400 * 86400000, consecutiveDays: PROMOTE_DAYS, longTerm: true }], imagination: [], facts: {}, impressions: {}, forgotten: [] };
|
|
113
147
|
settle(m);
|
package/store.js
CHANGED
|
@@ -101,7 +101,70 @@ export function readJson(file, fallback = null) {
|
|
|
101
101
|
}
|
|
102
102
|
}
|
|
103
103
|
|
|
104
|
-
|
|
104
|
+
/* ── 按年分片存 ───────────────────────────────────────────────
|
|
105
|
+
*
|
|
106
|
+
* brain.json 只放"她是谁"(人设/情绪/关系/卡片…),**不放经历**。
|
|
107
|
+
* 经历按年落进 brains/<id>/years/2026.jsonl。
|
|
108
|
+
*
|
|
109
|
+
* 为什么:一条活 16 年的鱼会有 1.7 万条经历,全塞一个 json 里
|
|
110
|
+
* 打开一次要解析几百 KB,越活越慢。分片 + 按窗口懒加载才是长期解。
|
|
111
|
+
*/
|
|
112
|
+
|
|
113
|
+
const DAY = 24 * 60 * 60 * 1000;
|
|
114
|
+
let episodeWindowDays = 730; // 打开时加载最近多少天的经历
|
|
115
|
+
|
|
116
|
+
export function setEpisodeWindow(days) {
|
|
117
|
+
const n = Number(days);
|
|
118
|
+
if (Number.isFinite(n) && n > 0) episodeWindowDays = Math.floor(n);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export function episodeWindow() {
|
|
122
|
+
return episodeWindowDays;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export function yearsDir(id) {
|
|
126
|
+
return path.join(brainDir(id), 'years');
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export function yearFile(id, year) {
|
|
130
|
+
return path.join(yearsDir(id), `${year}.jsonl`);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function yearOf(ms) {
|
|
134
|
+
return new Date(Number(ms) || Date.now()).getFullYear();
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** 把一个年份的经历写成 jsonl(整年重写:一年最多一千多条,可接受) */
|
|
138
|
+
function writeYearShard(id, year, list) {
|
|
139
|
+
ensureDir(yearsDir(id));
|
|
140
|
+
const file = yearFile(id, year);
|
|
141
|
+
if (!list.length) {
|
|
142
|
+
try { if (fs.existsSync(file)) fs.unlinkSync(file); } catch { /* 删不掉也无所谓 */ }
|
|
143
|
+
return null;
|
|
144
|
+
}
|
|
145
|
+
const body = `${list.map((e) => JSON.stringify(e)).join('\n')}\n`;
|
|
146
|
+
const tmp = `${file}.${process.pid}.tmp`;
|
|
147
|
+
fs.writeFileSync(tmp, body, 'utf8');
|
|
148
|
+
fs.renameSync(tmp, file);
|
|
149
|
+
const ats = list.map((e) => Number(e.at) || 0).filter(Boolean);
|
|
150
|
+
return {
|
|
151
|
+
count: list.length,
|
|
152
|
+
from: ats.length ? Math.min(...ats) : 0,
|
|
153
|
+
to: ats.length ? Math.max(...ats) : 0,
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function readYearShard(id, year) {
|
|
158
|
+
const file = yearFile(id, year);
|
|
159
|
+
if (!fs.existsSync(file)) return [];
|
|
160
|
+
const out = [];
|
|
161
|
+
for (const line of fs.readFileSync(file, 'utf8').split(/\r?\n/)) {
|
|
162
|
+
const s = line.trim();
|
|
163
|
+
if (!s) continue;
|
|
164
|
+
try { out.push(JSON.parse(s)); } catch { /* 坏行跳过,不整年报废 */ }
|
|
165
|
+
}
|
|
166
|
+
return out;
|
|
167
|
+
}
|
|
105
168
|
export function writeJsonAtomic(file, data) {
|
|
106
169
|
ensureDir(path.dirname(file));
|
|
107
170
|
const tmp = `${file}.${process.pid}.tmp`;
|
|
@@ -133,15 +196,46 @@ export function listBrains() {
|
|
|
133
196
|
|
|
134
197
|
export function loadBrain(id) {
|
|
135
198
|
const file = brainFile(id);
|
|
136
|
-
|
|
137
|
-
if (main)
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
199
|
+
let main = readJson(file, null);
|
|
200
|
+
if (!main) {
|
|
201
|
+
// 主文件坏了就退到备份——单文件损坏不该等于丢档
|
|
202
|
+
const bak = readJson(`${file}.bak`, null);
|
|
203
|
+
if (bak) {
|
|
204
|
+
main = bak;
|
|
205
|
+
try { writeJsonAtomic(file, bak); } catch { /* 修不回来也不影响这次读 */ }
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
if (!main) return null;
|
|
209
|
+
|
|
210
|
+
// ── 把窗口内的年份读回内存(更早的留在磁盘上,不加载)──
|
|
211
|
+
main.memory = main.memory ?? {};
|
|
212
|
+
const shards = main.memory.shards ?? {};
|
|
213
|
+
const cutoff = Date.now() - episodeWindowDays * DAY;
|
|
214
|
+
const eps = [];
|
|
215
|
+
for (const [y, meta] of Object.entries(shards)) {
|
|
216
|
+
const to = Number(meta?.to) || 0;
|
|
217
|
+
if (to && to < cutoff) continue; // 太老的年份:不加载
|
|
218
|
+
eps.push(...readYearShard(id, Number(y)));
|
|
143
219
|
}
|
|
144
|
-
|
|
220
|
+
// 防御:万一旧版本留下的 brain.json 里还有 episodes,也并进来
|
|
221
|
+
if (Array.isArray(main.memory.episodes) && main.memory.episodes.length) {
|
|
222
|
+
eps.push(...main.memory.episodes);
|
|
223
|
+
}
|
|
224
|
+
main.memory.episodes = eps.sort((a, b) => (a.at ?? 0) - (b.at ?? 0));
|
|
225
|
+
return main;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/** 显式读某一年(翻旧账用) */
|
|
229
|
+
export function readYear(id, year) {
|
|
230
|
+
return readYearShard(id, Number(year));
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/** 她有哪些年份的经历 */
|
|
234
|
+
export function listYears(id) {
|
|
235
|
+
const main = readJson(brainFile(id), null);
|
|
236
|
+
return Object.entries(main?.memory?.shards ?? {})
|
|
237
|
+
.map(([year, meta]) => ({ year: Number(year), ...meta }))
|
|
238
|
+
.sort((a, b) => a.year - b.year);
|
|
145
239
|
}
|
|
146
240
|
|
|
147
241
|
export function saveBrain(brain) {
|
|
@@ -149,6 +243,43 @@ export function saveBrain(brain) {
|
|
|
149
243
|
try {
|
|
150
244
|
if (fs.existsSync(file)) fs.copyFileSync(file, `${file}.bak`);
|
|
151
245
|
} catch { /* 备份失败不影响写入 */ }
|
|
152
|
-
|
|
246
|
+
|
|
247
|
+
// ── 经历按年分片,不进 brain.json ──
|
|
248
|
+
const eps = Array.isArray(brain.memory?.episodes) ? brain.memory.episodes : [];
|
|
249
|
+
const byYear = new Map();
|
|
250
|
+
for (const e of eps) {
|
|
251
|
+
const y = yearOf(e.at);
|
|
252
|
+
if (!byYear.has(y)) byYear.set(y, []);
|
|
253
|
+
byYear.get(y).push(e);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
const shards = {};
|
|
257
|
+
for (const [y, list] of byYear) {
|
|
258
|
+
const meta = writeYearShard(brain.id, y, list);
|
|
259
|
+
if (meta) shards[y] = meta;
|
|
260
|
+
}
|
|
261
|
+
// 这一年被清空了(忘光了/搬家后没剩)→ 把文件删掉
|
|
262
|
+
try {
|
|
263
|
+
const dir = yearsDir(brain.id);
|
|
264
|
+
if (fs.existsSync(dir)) {
|
|
265
|
+
for (const f of fs.readdirSync(dir)) {
|
|
266
|
+
const y = Number(f.replace(/\.jsonl$/, ''));
|
|
267
|
+
if (Number.isFinite(y) && !shards[y]) {
|
|
268
|
+
try { fs.unlinkSync(path.join(dir, f)); } catch { /* 忽略 */ }
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
} catch { /* 读不到目录就算了 */ }
|
|
273
|
+
|
|
274
|
+
const out = {
|
|
275
|
+
...brain,
|
|
276
|
+
memory: { ...(brain.memory ?? {}), shards },
|
|
277
|
+
};
|
|
278
|
+
delete out.memory.episodes; // 经历在 years/*.jsonl
|
|
279
|
+
delete out.memory.shards?.episodes; // 防御
|
|
280
|
+
|
|
281
|
+
writeJsonAtomic(file, out);
|
|
282
|
+
brain.memory = brain.memory ?? {};
|
|
283
|
+
brain.memory.shards = shards; // 内存对象保持一致
|
|
153
284
|
return brain;
|
|
154
285
|
}
|