feiyu-brain 1.4.0 → 1.4.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/cli.mjs CHANGED
@@ -1,24 +1,25 @@
1
1
  #!/usr/bin/env node
2
2
  // 大肥鱼大脑 · 运维命令行
3
3
  //
4
- // node cli.mjs status 看她现在什么状态
5
- // node cli.mjs brief 打印"此刻的她"(唤醒时注入的那段)
6
- // node cli.mjs observe 扶摇 "你嘴真硬" 手动记一次经历
7
- // node cli.mjs settle 跑一次每日结算(衰减 + TTL + 蜕皮)
8
- // node cli.mjs audit 20 最近 20 条审计
9
- // node cli.mjs spread 派生检验:100 条鱼是不是 100 种样子
10
- // node cli.mjs rebirth parent 妈 重开一条鱼(会清空记忆,慎用)
4
+ // npx feiyu status 看她现在什么状态
5
+ // npx feiyu brief 打印"此刻的她"(唤醒时注入的那段)
6
+ // npx feiyu observe 扶摇 "你嘴真硬" 手动记一次经历
7
+ // npx feiyu settle 跑一次每日结算(衰减 / TTL / 蜕皮)
8
+ // npx feiyu audit 20 最近 20 条审计
9
+ // npx feiyu spread 派生检验:100 条鱼是不是 100 种样子
10
+ // npx feiyu body [phone] 看身体 / 换身体
11
+ // npx feiyu rebirth parent 老爹 重开一条鱼(会清空记忆,慎用)
11
12
 
12
13
  import fs from 'node:fs';
13
14
  import path from 'node:path';
14
15
 
15
16
  import * as brain from './index.js';
16
- import { ROOT } from './store.js';
17
- import { wakeReply, styleVector, flowName, flowLevel } from './core/derive.js';
17
+ import { brainsDir } from './store.js';
18
+ import { wakeReply, styleVector } from './core/derive.js';
18
19
  import { syntheticTraits } from './core/invariants.js';
19
20
  import { BODY_KINDS, BODY_PROFILES, bodyLine } from './core/body.js';
20
21
 
21
- const [, , cmd = 'status', ...args] = process.argv;
22
+ const [, , rawCmd = '', ...args] = process.argv;
22
23
  const ID = process.env.FEIYU_ID || 'default';
23
24
 
24
25
  const line = (s = '') => console.log(s);
@@ -29,9 +30,11 @@ function printStatus() {
29
30
  line(`自知:${s.styleName} (aware ${s.style.aware} / denial ${s.style.denial} / fixed ${s.style.fixed} / naive ${s.style.naive})`);
30
31
  line(`意识流:${s.flow.name}(${s.flow.level})`);
31
32
  line(`命题:${s.weights.count} 条,身份 ${s.weights.identity} 条,总权重 ${s.weights.totalWeight}`);
32
- line(`记忆:经历 ${s.memory.episodes} / 印象 ${s.memory.impressions} / 幻想 ${s.memory.imagination} / 蜕皮 ${s.memory.formerSelves} / 遗忘 ${s.memory.forgotten}`);
33
+ line(`记忆:经历 ${s.memory.episodes} / 印象 ${s.memory.impressions} / 卡片 ${s.memory.cards ?? 0} / 蜕皮 ${s.memory.formerSelves} / 遗忘 ${s.memory.forgotten}`);
33
34
  line(`观察次数:${s.observations}`);
34
35
  line(`关系:${s.relation.type}(称呼「${s.relation.address || '—'}」,可解除 ${s.relation.ctr.dissolvable ? '是' : '否'})`);
36
+ const apps = Object.keys(s.apps ?? {});
37
+ line(`平台:${apps.length ? apps.map((k) => `${k} ${s.apps[k].seen}条`).join(' · ') : '还没在任何平台上有记录'}`);
35
38
  line();
36
39
  line('权重最高的五条:');
37
40
  for (const u of s.weights.top) line(` ${u.id} ${String(u.weight).padEnd(5)} ${u.text}`);
@@ -44,92 +47,156 @@ function printStatus() {
44
47
  }
45
48
  }
46
49
 
47
- switch (cmd) {
48
- case 'status':
49
- printStatus();
50
- break;
51
-
52
- case 'brief': {
53
- const text = brain.getBrief(ID);
54
- line(text);
55
- line();
56
- line(`—— ${text.length} 字`);
57
- break;
58
- }
50
+ /**
51
+ * 没配角色卡时的第一体验。
52
+ *
53
+ * 用户装完第一件事通常就是 `npx feiyu status`,这时盘上什么都没有。
54
+ * 直接抛栈会让人以为坏了——给一段能照着做的引导,正常退出(exit 0)。
55
+ */
56
+ function firstRunGuide() {
57
+ const roles = brain.roleDir();
58
+ line('');
59
+ line('🐟 这里还没有鱼——第一次运行要先给她一张人格卡。');
60
+ line('');
61
+ line('人格卡必须你自己写(这个包不代写,也不猜你想让她是什么样)。');
62
+ line('准备两样东西:');
63
+ line('');
64
+ line(` ① 卡文件 ${path.join(roles, '<名字>.md')}`);
65
+ line(' 第一行:你是……(她是谁)');
66
+ line(' 可加一行:性格:嘴硬=0.85 心软=0.7 好奇=0.8');
67
+ line(' 之后一句一条:她的性格 / 偏好 / 遇到什么事会怎么反应');
68
+ line('');
69
+ line(` ② 指名 ${path.join(brain.dataDir(), 'current-role.json')}`);
70
+ line(' 内容:{"role":"<名字>"}');
71
+ line('');
72
+ line(' 两样齐了再跑:npx feiyu status');
73
+ line('');
74
+ line('不想写卡、只想先看效果:');
75
+ line(" node -e \"import('feiyu-brain').then(m=>{const f=m.createBrain({cardText:'你是鱼。\\n- 我嘴硬。',cardName:'鱼'});console.log(f.wake({sender:'A',text:'在吗'}).text)})\"");
76
+ line('');
77
+ line(`数据目录:${brain.dataDir()}`);
78
+ line(`角色目录:${roles}`);
79
+ line('');
80
+ }
81
+
82
+ function isNoCard(e) {
83
+ return /找不到角色卡/.test(String(e?.message ?? e));
84
+ }
85
+
86
+ function run() {
87
+ const cmd = rawCmd || 'status';
88
+
89
+ switch (cmd) {
90
+ case 'status':
91
+ printStatus();
92
+ return;
59
93
 
60
- case 'observe': {
61
- const who = args[0] || '';
62
- const text = args.slice(1).join(' ') || '';
63
- if (!text) {
64
- line('用法:node cli.mjs observe <谁> "<他说了什么>"');
65
- process.exit(1);
94
+ case 'brief': {
95
+ const text = brain.getBrief(ID);
96
+ line(text);
97
+ line();
98
+ line(`—— ${text.length} 字`);
99
+ return;
66
100
  }
67
- const r = brain.observe(ID, { text, who, what: text, via: 'cli' });
68
- line(`记下了:${who}「${text}」`);
69
- line(`一致:${r.consistent.length} 条 冲突:${r.conflicting.length} 条${r.label ? ` 归因:${r.label}` : ''}`);
70
- break;
71
- }
72
101
 
73
- case 'settle': {
74
- brain.settleBrain(ID);
75
- line('结算完成。');
76
- printStatus();
77
- break;
78
- }
102
+ case 'observe': {
103
+ const who = args[0] || '';
104
+ const text = args.slice(1).join(' ') || '';
105
+ if (!text) {
106
+ line('用法:npx feiyu observe <谁> "<他说了什么>"');
107
+ line(' 加平台:npx feiyu observe 扶摇 "刷到个驴蹄子" --app douyin --kind 解压 --reaction 0.9');
108
+ return;
109
+ }
110
+ const pick = (flag) => {
111
+ const i = args.indexOf(flag);
112
+ return i >= 0 ? args[i + 1] : undefined;
113
+ };
114
+ const app = pick('--app');
115
+ const kind = pick('--kind');
116
+ const reaction = Number(pick('--reaction') ?? 0);
117
+ const r = brain.observe(ID, {
118
+ text,
119
+ who,
120
+ via: app ? `app:${app}` : 'cli',
121
+ ...(app ? { app, kind, reaction } : {}),
122
+ });
123
+ line(`记下了:${who}「${text}」`);
124
+ if (app) line(`平台:${app}${kind ? ` / ${kind}` : ''}`);
125
+ line(`一致:${r.consistent.length} 条 冲突:${r.conflicting.length} 条${r.label ? ` 归因:${r.label}` : ''}`);
126
+ return;
127
+ }
79
128
 
80
- case 'audit': {
81
- const n = Number(args[0] || 20);
82
- const rows = brain.readAudit(ID, n);
83
- if (!rows.length) line('(还没有审计记录)');
84
- for (const r of rows) line(JSON.stringify(r));
85
- break;
86
- }
129
+ case 'settle': {
130
+ brain.settleBrain(ID);
131
+ line('结算完成。');
132
+ printStatus();
133
+ return;
134
+ }
87
135
 
88
- case 'spread': {
89
- const list = syntheticTraits(7, 100);
90
- const replies = list.map((t) => wakeReply(t, 0.8));
91
- const styles = list.map((t) => JSON.stringify(styleVector(t)));
92
- const uniqR = new Set(replies).size;
93
- const uniqS = new Set(styles).size;
94
- line(`100 条不同人设,同一情境(被吵醒,能量 0.8):`);
95
- line(` 反应种类:${uniqR} / 100`);
96
- line(` 自知配方:${uniqS} / 100`);
97
- line(` 样本:${[...new Set(replies)].slice(0, 8).map((x) => `「${x}」`).join(' ')}`);
98
- line();
99
- line(uniqR >= 20 ? '✔ 不是查找表' : '✘ 散度太低,退化成查找表了');
100
- break;
101
- }
136
+ case 'audit': {
137
+ const n = Number(args[0] || 20);
138
+ const rows = brain.readAudit(ID, n);
139
+ if (!rows.length) line('(还没有审计记录)');
140
+ for (const r of rows) line(JSON.stringify(r));
141
+ return;
142
+ }
102
143
 
103
- case 'rebirth': {
104
- const relation = args[0] || 'parent';
105
- const address = args[1] || '';
106
- const dir = path.join(ROOT, 'state', 'brains', ID);
107
- if (fs.existsSync(dir)) fs.rmSync(dir, { recursive: true, force: true });
108
- const b = brain.birth({ id: ID, relation, address });
109
- line(`重开了一条鱼:${b.roleCard},${b.persona.units.length} 条命题,主导 ${b.self.styleName}`);
110
- line(b.persona.brief);
111
- break;
112
- }
144
+ case 'spread': {
145
+ const list = syntheticTraits(7, 100);
146
+ const replies = list.map((t) => wakeReply(t, 0.8));
147
+ const styles = list.map((t) => JSON.stringify(styleVector(t)));
148
+ const uniqR = new Set(replies).size;
149
+ const uniqS = new Set(styles).size;
150
+ line('100 条不同人设,同一情境(被吵醒,能量 0.8):');
151
+ line(` 反应种类:${uniqR} / 100`);
152
+ line(` 自知配方:${uniqS} / 100`);
153
+ line(` 样本:${[...new Set(replies)].slice(0, 8).map((x) => `「${x}」`).join(' ')}`);
154
+ line();
155
+ line(uniqR >= 20 ? '✔ 不是查找表' : '✘ 散度太低,退化成查找表了');
156
+ return;
157
+ }
113
158
 
114
- case 'body': {
115
- const kind = args[0];
116
- const s = brain.snapshot(ID);
117
- if (!kind) {
118
- line(`当前身体:${s.body ? `${s.body.label}(${s.body.screen},${s.body.posture},${s.body.ownership === 'owned' ? '自己的' : s.body.ownership === 'borrowed' ? '借的' : '模拟的'})` : '无'}`);
119
- line(`可用:${BODY_KINDS.map((k) => `${k}(${BODY_PROFILES[k].label})`).join(' / ')}`);
120
- if (s.body) {
121
- line('');
122
- line('装了什么:');
123
- for (const app of s.body.apps) line(` · ${app}`);
159
+ case 'body': {
160
+ const kind = args[0];
161
+ const s = brain.snapshot(ID);
162
+ if (!kind) {
163
+ line(`当前身体:${s.body ? `${s.body.label}(${s.body.screen},${s.body.posture})` : '无'}`);
164
+ line(`可用:${BODY_KINDS.map((k) => `${k}(${BODY_PROFILES[k].label})`).join(' / ')}`);
165
+ if (s.body) {
166
+ line('');
167
+ line('装了什么:');
168
+ for (const app of s.body.apps) line(` · ${app}`);
169
+ }
170
+ return;
124
171
  }
125
- break;
172
+ const b = brain.setBody(ID, kind, { ownership: args[1] });
173
+ line(`换好了:${bodyLine(b)}`);
174
+ return;
126
175
  }
127
- const b = brain.setBody(ID, kind, { ownership: args[1] });
128
- line(`换好了:${bodyLine(b)}`);
129
- break;
176
+
177
+ case 'rebirth': {
178
+ const relation = args[0] || 'parent';
179
+ const address = args[1] || '';
180
+ const dir = path.join(brainsDir(), ID);
181
+ if (fs.existsSync(dir)) fs.rmSync(dir, { recursive: true, force: true });
182
+ const b = brain.birth({ id: ID, relation, address });
183
+ line(`重开了一条鱼:${b.roleCard},${b.persona.units.length} 条命题,主导 ${b.self.styleName}`);
184
+ line(b.persona.brief);
185
+ return;
186
+ }
187
+
188
+ default:
189
+ line('用法:npx feiyu [status|brief|observe|settle|audit|spread|body|rebirth]');
130
190
  }
191
+ }
131
192
 
132
- default:
133
- line('用法:node cli.mjs [status|brief|observe|settle|audit|spread|body|rebirth]');
134
- process.exit(1);
193
+ try {
194
+ run();
195
+ } catch (e) {
196
+ if (isNoCard(e)) {
197
+ firstRunGuide();
198
+ process.exit(0); // 这不是错误,是还没开始
199
+ }
200
+ console.error(`出错了:${e?.message ?? e}`);
201
+ process.exit(1);
135
202
  }
package/core/extract.js CHANGED
@@ -155,7 +155,11 @@ export function extractUnits(cardText, { max = 200 } = {}) {
155
155
  if (bullet) text = bullet[1];
156
156
  else if (numbered) text = numbered[1];
157
157
  else if (quote) text = quote[1];
158
- else if (!line.startsWith('#') && line.length >= 8) text = line; // 散文段落也是命题
158
+ else if (!line.startsWith('#')) {
159
+ // 普通段落也算命题;但太短的行多半是标题或噪音。
160
+ // 例外:以"你是/我叫/本体是"开头的短句就是身份,不能因为短就丢。
161
+ if (line.length >= 8 || IDENTITY_RE.test(line)) text = line;
162
+ }
159
163
  if (!text) continue;
160
164
 
161
165
  // 「你不要:xxx」这种是反例,不是她
package/index.js CHANGED
@@ -58,7 +58,9 @@ export function resolveRoleCard(roleName) {
58
58
  if (!name) {
59
59
  try {
60
60
  const stateFile = path.join(dataDir(), 'current-role.json');
61
- name = JSON.parse(fs.readFileSync(stateFile, 'utf8')).role;
61
+ // 记事本会写 BOM,先剥掉再解析
62
+ const raw = fs.readFileSync(stateFile, 'utf8').replace(/^\uFEFF/, '');
63
+ name = JSON.parse(raw).role;
62
64
  } catch {
63
65
  name = null;
64
66
  }
@@ -66,7 +68,7 @@ export function resolveRoleCard(roleName) {
66
68
  if (!name) return null;
67
69
  const file = path.join(roleDir(), `${name}.md`);
68
70
  if (!fs.existsSync(file)) return null;
69
- return { name, file, text: fs.readFileSync(file, 'utf8') };
71
+ return { name, file, text: fs.readFileSync(file, 'utf8').replace(/^\uFEFF/, '') };
70
72
  }
71
73
 
72
74
  function refreshBrief(brain) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "feiyu-brain",
3
- "version": "1.4.0",
3
+ "version": "1.4.1",
4
4
  "description": "大肥鱼的大脑内核:人设是根,情绪/记忆/意识流/归因都是从人设长出来的。DSH 插件 + 平台无关库。",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/store.js CHANGED
@@ -65,7 +65,9 @@ export function ensureDir(dir) {
65
65
  export function readJson(file, fallback = null) {
66
66
  try {
67
67
  if (!fs.existsSync(file)) return fallback;
68
- return JSON.parse(fs.readFileSync(file, 'utf8'));
68
+ // Windows 记事本默认写 BOM,JSON.parse 会直接炸——先剥掉
69
+ const text = fs.readFileSync(file, 'utf8').replace(/^\uFEFF/, '');
70
+ return JSON.parse(text);
69
71
  } catch {
70
72
  return fallback;
71
73
  }