feiyu-brain 1.5.0 → 1.6.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/README.md +1 -1
- package/cli.mjs +94 -1
- package/cordis.patch.yml +13 -1
- package/core/bond.js +186 -0
- package/core/concept.js +138 -0
- package/core/driver.js +74 -0
- package/core/extract.js +75 -2
- package/core/memory.js +539 -18
- package/core/persona.js +167 -7
- package/core/relation.js +78 -0
- package/core/self.js +72 -0
- package/docs/CHANGELOG-1.6.md +154 -0
- package/docs/MEMORY.md +180 -15
- package/index.js +679 -44
- package/lib/channels.mjs +199 -0
- package/lib/llm.mjs +105 -0
- package/lib/onebot.mjs +106 -0
- package/lib/say.mjs +120 -0
- package/lib/serve.mjs +42 -1
- package/package.json +10 -3
- package/selftest.mjs +54 -6
- package/smoke.mjs +41 -0
- package/store.js +153 -11
- package/test-adapters.mjs +132 -0
package/selftest.mjs
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
import assert from 'node:assert/strict';
|
|
6
6
|
import fs from 'node:fs';
|
|
7
|
+
import os from 'node:os';
|
|
7
8
|
import path from 'node:path';
|
|
8
9
|
|
|
9
10
|
import { ROOT } from './store.js';
|
|
@@ -26,14 +27,27 @@ function t(name, fn) {
|
|
|
26
27
|
}
|
|
27
28
|
|
|
28
29
|
const ID = 'selftest';
|
|
29
|
-
|
|
30
|
+
// 自测跑在临时目录里,不往包目录写东西(审查报告 A4)
|
|
31
|
+
const TMP = fs.mkdtempSync(path.join(os.tmpdir(), 'feiyu-selftest-'));
|
|
32
|
+
brain.configure({ dataDir: TMP, roleDir: path.join(TMP, 'roles') });
|
|
33
|
+
const dir = path.join(TMP, 'brains', ID);
|
|
30
34
|
if (fs.existsSync(dir)) fs.rmSync(dir, { recursive: true, force: true });
|
|
31
35
|
|
|
32
36
|
console.log('\n── 出生 ──');
|
|
33
|
-
|
|
37
|
+
// 内联一张测试卡:干净机器上不该因为没有角色卡就崩(审查报告 A3)
|
|
38
|
+
const TEST_CARD = [
|
|
39
|
+
'你是测试鱼。',
|
|
40
|
+
'性格:嘴硬=0.8 心软=0.6 好奇=0.7 好动=0.6',
|
|
41
|
+
'- 我嘴硬,被怼会顶回去。',
|
|
42
|
+
'- 我记仇但记不久。',
|
|
43
|
+
'- 不要客服腔。',
|
|
44
|
+
'- 我喜欢解压类的视频。',
|
|
45
|
+
'- 有人认真跟我说话,我会收起腔调。',
|
|
46
|
+
].join('\n');
|
|
47
|
+
const b = brain.birth({ id: ID, relation: 'parent', address: '妈', cardText: TEST_CARD, cardName: '测试鱼' });
|
|
34
48
|
|
|
35
49
|
t('从角色卡抽出命题', () => {
|
|
36
|
-
assert.ok(b.persona.units.length >
|
|
50
|
+
assert.ok(b.persona.units.length > 3, `只抽到 ${b.persona.units.length} 条`);
|
|
37
51
|
});
|
|
38
52
|
|
|
39
53
|
t('有身份命题', () => {
|
|
@@ -101,13 +115,47 @@ t('蜕皮后 memory.formerSelves 里查得到', () => {
|
|
|
101
115
|
});
|
|
102
116
|
|
|
103
117
|
console.log('\n── 记忆 ──');
|
|
104
|
-
t('
|
|
105
|
-
const
|
|
106
|
-
|
|
118
|
+
t('30 天没人提 → 压扁成一句"大概"(不是删除)', () => {
|
|
119
|
+
const now = Date.now();
|
|
120
|
+
const m = {
|
|
121
|
+
episodes: [{ id: 'a', kind: 'episode', who: '扶摇', what: '你嘴真硬', at: now - 31 * 86400000, lastRecall: now - 31 * 86400000, consecutiveDays: 1, longTerm: false }],
|
|
122
|
+
imagination: [], facts: {}, impressions: {}, forgotten: [], cards: [],
|
|
123
|
+
};
|
|
124
|
+
settle(m, now);
|
|
125
|
+
assert.equal(m.episodes.length, 1, '不该被删掉');
|
|
126
|
+
assert.equal(m.episodes[0].compressed, true);
|
|
127
|
+
assert.ok(m.episodes[0].what.includes('记不清'), m.episodes[0].what);
|
|
128
|
+
assert.equal(m.forgotten.length, 0);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
t('2 年没人提 → 才真的遗忘', () => {
|
|
132
|
+
const now = Date.now();
|
|
133
|
+
const DAY = 86400000;
|
|
134
|
+
const m = {
|
|
135
|
+
episodes: [{
|
|
136
|
+
id: 'a', kind: 'episode', who: '扶摇', what: '两年前的事',
|
|
137
|
+
at: now - 800 * DAY, lastRecall: now - 800 * DAY,
|
|
138
|
+
compressed: true, cardedAt: now - 800 * DAY, expiresAt: now - 70 * DAY,
|
|
139
|
+
consecutiveDays: 1, longTerm: false,
|
|
140
|
+
}],
|
|
141
|
+
imagination: [], facts: {}, impressions: {}, forgotten: [], cards: [],
|
|
142
|
+
};
|
|
143
|
+
settle(m, now);
|
|
107
144
|
assert.equal(m.episodes.length, 0);
|
|
108
145
|
assert.equal(m.forgotten.length, 1);
|
|
109
146
|
});
|
|
110
147
|
|
|
148
|
+
t('一直被提起 → 压不了也忘不了', () => {
|
|
149
|
+
const now = Date.now();
|
|
150
|
+
const m = {
|
|
151
|
+
episodes: [{ id: 'a', kind: 'episode', what: '常聊的事', at: now - 500 * 86400000, lastRecall: now, consecutiveDays: 20, longTerm: false }],
|
|
152
|
+
imagination: [], facts: {}, impressions: {}, forgotten: [], cards: [],
|
|
153
|
+
};
|
|
154
|
+
settle(m, now);
|
|
155
|
+
assert.equal(m.episodes.length, 1);
|
|
156
|
+
assert.ok(!m.episodes[0].compressed, '不该被压扁');
|
|
157
|
+
});
|
|
158
|
+
|
|
111
159
|
t('连续 15 天升级长期,永不衰减', () => {
|
|
112
160
|
const m = { episodes: [{ id: 'b', kind: 'episode', lastRecall: Date.now() - 400 * 86400000, consecutiveDays: PROMOTE_DAYS, longTerm: true }], imagination: [], facts: {}, impressions: {}, forgotten: [] };
|
|
113
161
|
settle(m);
|
package/smoke.mjs
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// 集成冒烟:完全按 bridge.js 的调用方式跑一遍。
|
|
2
|
+
// node smoke.mjs
|
|
3
|
+
// 验证:插件能被桥接 import、wakeLine 能生成、不抛异常、长度可控。
|
|
4
|
+
|
|
5
|
+
import * as brain from './index.js';
|
|
6
|
+
|
|
7
|
+
const ID = 'smoke';
|
|
8
|
+
|
|
9
|
+
// 第一次(会现场出生,等同桥接首启)
|
|
10
|
+
const first = brain.wakeLine(ID, { who: '扶摇', words: ['嘴硬', '推荐'] });
|
|
11
|
+
console.log('── 第一次唤醒注入 ──');
|
|
12
|
+
console.log(first);
|
|
13
|
+
console.log(`\n长度:${first.length} 字符`);
|
|
14
|
+
|
|
15
|
+
// 模拟桥接连续几条消息
|
|
16
|
+
for (const [who, text] of [
|
|
17
|
+
['扶摇', '你真是又嘴硬又爱怼人'],
|
|
18
|
+
['访秋辞', '给我推荐点东西呗'],
|
|
19
|
+
['许璐', '这鱼今天怎么这么收敛'],
|
|
20
|
+
]) {
|
|
21
|
+
brain.observe(ID, { text, who, what: text, via: 'chat' });
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const snap = brain.snapshot(ID);
|
|
25
|
+
console.log('\n── 快照 ──');
|
|
26
|
+
console.log(JSON.stringify({
|
|
27
|
+
styleName: snap.styleName,
|
|
28
|
+
style: snap.style,
|
|
29
|
+
briefChars: snap.briefChars,
|
|
30
|
+
flow: snap.flow,
|
|
31
|
+
weights: { count: snap.weights.count, identity: snap.weights.identity },
|
|
32
|
+
memory: snap.memory,
|
|
33
|
+
observations: snap.observations,
|
|
34
|
+
invariants: snap.invariants,
|
|
35
|
+
}, null, 2));
|
|
36
|
+
|
|
37
|
+
if (snap.invariants.length) {
|
|
38
|
+
console.error('\n✘ 不变量违规');
|
|
39
|
+
process.exit(1);
|
|
40
|
+
}
|
|
41
|
+
console.log('\n✔ 冒烟通过');
|
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`;
|
|
@@ -109,9 +172,20 @@ export function writeJsonAtomic(file, data) {
|
|
|
109
172
|
fs.renameSync(tmp, file);
|
|
110
173
|
}
|
|
111
174
|
|
|
175
|
+
const AUDIT_MAX_BYTES = 2 * 1024 * 1024;
|
|
176
|
+
|
|
112
177
|
export function appendAudit(id, entry) {
|
|
113
178
|
ensureDir(brainDir(id));
|
|
114
|
-
|
|
179
|
+
const file = auditFile(id);
|
|
180
|
+
// 轮转:审计只追加,长期跑会无限长(审查报告 B3)
|
|
181
|
+
try {
|
|
182
|
+
if (fs.existsSync(file) && fs.statSync(file).size > AUDIT_MAX_BYTES) {
|
|
183
|
+
const old = `${file}.1`;
|
|
184
|
+
try { if (fs.existsSync(old)) fs.unlinkSync(old); } catch { /* 忽略 */ }
|
|
185
|
+
fs.renameSync(file, old);
|
|
186
|
+
}
|
|
187
|
+
} catch { /* 转不过去就继续追加 */ }
|
|
188
|
+
fs.appendFileSync(file, `${JSON.stringify({ at: new Date().toISOString(), ...entry })}\n`, 'utf8');
|
|
115
189
|
}
|
|
116
190
|
|
|
117
191
|
export function readAudit(id, limit = 20) {
|
|
@@ -133,15 +207,46 @@ export function listBrains() {
|
|
|
133
207
|
|
|
134
208
|
export function loadBrain(id) {
|
|
135
209
|
const file = brainFile(id);
|
|
136
|
-
|
|
137
|
-
if (main)
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
210
|
+
let main = readJson(file, null);
|
|
211
|
+
if (!main) {
|
|
212
|
+
// 主文件坏了就退到备份——单文件损坏不该等于丢档
|
|
213
|
+
const bak = readJson(`${file}.bak`, null);
|
|
214
|
+
if (bak) {
|
|
215
|
+
main = bak;
|
|
216
|
+
try { writeJsonAtomic(file, bak); } catch { /* 修不回来也不影响这次读 */ }
|
|
217
|
+
}
|
|
143
218
|
}
|
|
144
|
-
return null;
|
|
219
|
+
if (!main) return null;
|
|
220
|
+
|
|
221
|
+
// ── 把窗口内的年份读回内存(更早的留在磁盘上,不加载)──
|
|
222
|
+
main.memory = main.memory ?? {};
|
|
223
|
+
const shards = main.memory.shards ?? {};
|
|
224
|
+
const cutoff = Date.now() - episodeWindowDays * DAY;
|
|
225
|
+
const eps = [];
|
|
226
|
+
for (const [y, meta] of Object.entries(shards)) {
|
|
227
|
+
const to = Number(meta?.to) || 0;
|
|
228
|
+
if (to && to < cutoff) continue; // 太老的年份:不加载
|
|
229
|
+
eps.push(...readYearShard(id, Number(y)));
|
|
230
|
+
}
|
|
231
|
+
// 防御:万一旧版本留下的 brain.json 里还有 episodes,也并进来
|
|
232
|
+
if (Array.isArray(main.memory.episodes) && main.memory.episodes.length) {
|
|
233
|
+
eps.push(...main.memory.episodes);
|
|
234
|
+
}
|
|
235
|
+
main.memory.episodes = eps.sort((a, b) => (a.at ?? 0) - (b.at ?? 0));
|
|
236
|
+
return main;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/** 显式读某一年(翻旧账用) */
|
|
240
|
+
export function readYear(id, year) {
|
|
241
|
+
return readYearShard(id, Number(year));
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/** 她有哪些年份的经历 */
|
|
245
|
+
export function listYears(id) {
|
|
246
|
+
const main = readJson(brainFile(id), null);
|
|
247
|
+
return Object.entries(main?.memory?.shards ?? {})
|
|
248
|
+
.map(([year, meta]) => ({ year: Number(year), ...meta }))
|
|
249
|
+
.sort((a, b) => a.year - b.year);
|
|
145
250
|
}
|
|
146
251
|
|
|
147
252
|
export function saveBrain(brain) {
|
|
@@ -149,6 +254,43 @@ export function saveBrain(brain) {
|
|
|
149
254
|
try {
|
|
150
255
|
if (fs.existsSync(file)) fs.copyFileSync(file, `${file}.bak`);
|
|
151
256
|
} catch { /* 备份失败不影响写入 */ }
|
|
152
|
-
|
|
257
|
+
|
|
258
|
+
// ── 经历按年分片,不进 brain.json ──
|
|
259
|
+
const eps = Array.isArray(brain.memory?.episodes) ? brain.memory.episodes : [];
|
|
260
|
+
const byYear = new Map();
|
|
261
|
+
for (const e of eps) {
|
|
262
|
+
const y = yearOf(e.at);
|
|
263
|
+
if (!byYear.has(y)) byYear.set(y, []);
|
|
264
|
+
byYear.get(y).push(e);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
const shards = {};
|
|
268
|
+
for (const [y, list] of byYear) {
|
|
269
|
+
const meta = writeYearShard(brain.id, y, list);
|
|
270
|
+
if (meta) shards[y] = meta;
|
|
271
|
+
}
|
|
272
|
+
// 这一年被清空了(忘光了/搬家后没剩)→ 把文件删掉
|
|
273
|
+
try {
|
|
274
|
+
const dir = yearsDir(brain.id);
|
|
275
|
+
if (fs.existsSync(dir)) {
|
|
276
|
+
for (const f of fs.readdirSync(dir)) {
|
|
277
|
+
const y = Number(f.replace(/\.jsonl$/, ''));
|
|
278
|
+
if (Number.isFinite(y) && !shards[y]) {
|
|
279
|
+
try { fs.unlinkSync(path.join(dir, f)); } catch { /* 忽略 */ }
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
} catch { /* 读不到目录就算了 */ }
|
|
284
|
+
|
|
285
|
+
const out = {
|
|
286
|
+
...brain,
|
|
287
|
+
memory: { ...(brain.memory ?? {}), shards },
|
|
288
|
+
};
|
|
289
|
+
delete out.memory.episodes; // 经历在 years/*.jsonl
|
|
290
|
+
delete out.memory.shards?.episodes; // 防御
|
|
291
|
+
|
|
292
|
+
writeJsonAtomic(file, out);
|
|
293
|
+
brain.memory = brain.memory ?? {};
|
|
294
|
+
brain.memory.shards = shards; // 内存对象保持一致
|
|
153
295
|
return brain;
|
|
154
296
|
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
// 适配器契约测试:两种接入方式都必须成立。
|
|
2
|
+
// node test-adapters.mjs
|
|
3
|
+
|
|
4
|
+
import assert from 'node:assert/strict';
|
|
5
|
+
import fs from 'node:fs';
|
|
6
|
+
import path from 'node:path';
|
|
7
|
+
import os from 'node:os';
|
|
8
|
+
|
|
9
|
+
import { createBridgeAdapter } from './adapters/qq-bridge.js';
|
|
10
|
+
import { createUniversalHost } from './adapters/universal.js';
|
|
11
|
+
import { TencentOfficialAdapter } from './adapters/tencent-official.js';
|
|
12
|
+
import { normalizeEvent } from './core/event.js';
|
|
13
|
+
|
|
14
|
+
let pass = 0;
|
|
15
|
+
const fails = [];
|
|
16
|
+
function t(name, fn) {
|
|
17
|
+
try { fn(); pass += 1; console.log(` ✔ ${name}`); }
|
|
18
|
+
catch (e) { fails.push({ name, msg: e.message }); console.log(` ✘ ${name}\n ${e.message}`); }
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
console.log('\n── 通用事件协议 ──');
|
|
22
|
+
t('任何平台的字段都能归一化成同一种事件', () => {
|
|
23
|
+
const a = normalizeEvent({ sender: 'A', text: 'hi', chatId: 'group:1' }, 'x');
|
|
24
|
+
const b = normalizeEvent({ userName: 'A', content: 'hi', channelId: 'group:1' }, 'y');
|
|
25
|
+
assert.equal(a.sender, b.sender);
|
|
26
|
+
assert.equal(a.text, b.text);
|
|
27
|
+
assert.equal(a.chatId, b.chatId);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
t('自己的消息不算经历', () => {
|
|
31
|
+
const ev = normalizeEvent({ sender: '我', text: 'x', isSelf: true });
|
|
32
|
+
assert.equal(ev.isSelf, true);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
console.log('\n── 老用户:桥接适配器 ──');
|
|
36
|
+
const fakeBrainApi = {
|
|
37
|
+
observe: (id, ev) => ({ ok: true, id, who: ev.sender }),
|
|
38
|
+
wakeLine: (id, ev) => `【此刻的她】测试鱼(who=${ev?.sender ?? '-'})`,
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
t('桥接状态 → 注入段', () => {
|
|
42
|
+
const ad = createBridgeAdapter({ log: () => {}, id: 'default' });
|
|
43
|
+
const st = { unread: [{ seq: 3, sender: '扶摇', plain: '你嘴真硬' }], recentMessages: [] };
|
|
44
|
+
const line = ad.buildBrainLine('group:1', st, fakeBrainApi);
|
|
45
|
+
assert.match(line, /【此刻的她】/);
|
|
46
|
+
assert.ok(line.endsWith('\n\n'));
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
t('同一条消息不重复"经历"', () => {
|
|
50
|
+
let count = 0;
|
|
51
|
+
const api = { observe: () => { count += 1; }, wakeLine: () => 'x' };
|
|
52
|
+
const ad = createBridgeAdapter({ log: () => {} });
|
|
53
|
+
const st = { unread: [{ seq: 7, sender: 'A', plain: 'a' }], recentMessages: [] };
|
|
54
|
+
ad.buildBrainLine('k', st, api);
|
|
55
|
+
ad.buildBrainLine('k', st, api);
|
|
56
|
+
assert.equal(count, 1, `观察了 ${count} 次`);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
t('桥接崩溃不会连带崩溃', () => {
|
|
60
|
+
const broken = { observe: () => { throw new Error('boom'); }, wakeLine: () => { throw new Error('boom'); } };
|
|
61
|
+
const ad = createBridgeAdapter({ log: () => {} });
|
|
62
|
+
const st = { unread: [{ seq: 9, sender: 'A', plain: 'a' }], recentMessages: [] };
|
|
63
|
+
assert.equal(ad.buildBrainLine('k2', st, broken), '');
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
console.log('\n── 新用户:腾讯官方适配器 ──');
|
|
67
|
+
t('缺 appId/secret 立刻报错(不静默失败)', () => {
|
|
68
|
+
assert.throws(() => new TencentOfficialAdapter({}), /appId/);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
t('群 @ 消息映射正确', () => {
|
|
72
|
+
const bot = new TencentOfficialAdapter({ appId: '1', clientSecret: 's', log: () => {} });
|
|
73
|
+
const ev = bot.toBrainEvent('GROUP_AT_MESSAGE_CREATE', {
|
|
74
|
+
id: 'msg1', content: ' 你好 ', group_openid: 'G1', author: { member_openid: 'M1' }, timestamp: 1700000000000,
|
|
75
|
+
});
|
|
76
|
+
assert.equal(ev.chatKind, 'group');
|
|
77
|
+
assert.equal(ev.chatId, 'group:G1');
|
|
78
|
+
assert.equal(ev.text, '你好');
|
|
79
|
+
assert.equal(ev.isAtMe, true);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
t('私聊消息映射正确', () => {
|
|
83
|
+
const bot = new TencentOfficialAdapter({ appId: '1', clientSecret: 's', log: () => {} });
|
|
84
|
+
const ev = bot.toBrainEvent('C2C_MESSAGE_CREATE', {
|
|
85
|
+
id: 'msg2', content: '在吗', author: { user_openid: 'U1' },
|
|
86
|
+
});
|
|
87
|
+
assert.equal(ev.chatKind, 'private');
|
|
88
|
+
assert.equal(ev.chatId, 'private:U1');
|
|
89
|
+
assert.equal(ev.text, '在吗');
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
t('未支持的事件返回 null(不瞎猜)', () => {
|
|
93
|
+
const bot = new TencentOfficialAdapter({ appId: '1', clientSecret: 's', log: () => {} });
|
|
94
|
+
assert.equal(bot.toBrainEvent('AT_MESSAGE_CREATE', {}), null);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
console.log('\n── 任意平台:通用宿主 ──');
|
|
98
|
+
t('handle 返回注入段 + 事件', () => {
|
|
99
|
+
const calls = [];
|
|
100
|
+
const brain = {
|
|
101
|
+
wake: (ev) => { calls.push(ev); return { text: `【此刻的她】x` }; },
|
|
102
|
+
};
|
|
103
|
+
const host = createUniversalHost({ brain, buildSystemPrompt: (seg) => `SYS\n${seg}` });
|
|
104
|
+
const r = host.handle({ sender: 'A', text: 'hi', chatId: 'group:1' });
|
|
105
|
+
assert.match(r.system, /^SYS/);
|
|
106
|
+
assert.equal(calls.length, 1);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
console.log('\n── 打包边界 ──');
|
|
110
|
+
t('package.json 可发布(不私藏依赖、声明 exports)', () => {
|
|
111
|
+
const pkg = JSON.parse(fs.readFileSync(new URL('./package.json', import.meta.url), 'utf8'));
|
|
112
|
+
assert.ok(pkg.name);
|
|
113
|
+
assert.equal(pkg.private, undefined);
|
|
114
|
+
assert.ok(pkg.exports['.']);
|
|
115
|
+
assert.ok(pkg.files.includes('adapters/'));
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
t('adapters 目录里有三种接法', () => {
|
|
119
|
+
const dir = new URL('./adapters/', import.meta.url);
|
|
120
|
+
const files = fs.readdirSync(dir).filter((f) => f.endsWith('.js'));
|
|
121
|
+
for (const need of ['index.js', 'qq-bridge.js', 'tencent-official.js', 'universal.js']) {
|
|
122
|
+
assert.ok(files.includes(need), `缺 ${need}`);
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
console.log(`\n${'─'.repeat(40)}`);
|
|
127
|
+
console.log(`通过 ${pass} 项,失败 ${fails.length} 项`);
|
|
128
|
+
if (fails.length) {
|
|
129
|
+
for (const f of fails) console.log(` ✘ ${f.name}: ${f.msg}`);
|
|
130
|
+
process.exit(1);
|
|
131
|
+
}
|
|
132
|
+
console.log('全部通过 ✔');
|