feiyu-brain 1.2.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.
@@ -3,7 +3,7 @@
3
3
  * ============================================================================
4
4
  * 同一个包有两种用法:
5
5
  * ① 库:`import { createBrain } from 'feiyu-brain'`(下面 re-export 了全部)
6
- * ② 插件:`dsh plugin add feiyu-brain` → 注册 4 个工具,模型可以直接读写这条鱼
6
+ * ② 插件:`dsh plugin add feiyu-brain` → 注册 6 个工具,模型可以直接读写这条鱼
7
7
  *
8
8
  * 工具是**读写大脑**,不是"生成对话"——对话仍然由宿主自己的模型完成。
9
9
  *
@@ -11,6 +11,8 @@
11
11
  * feiyu_wake 取"此刻的她",宿主把它拼进 system prompt
12
12
  * feiyu_observe 记一次经历(权重会动、记忆会进)
13
13
  * feiyu_tick 每日结算(衰减 / 遗忘 / 蜕皮)
14
+ * feiyu_feel 对一件事产生感受(PAD → 找词)
15
+ * feiyu_app 真的开 App / 截屏 / 卸载(adb 或 PowerShell,留证据)
14
16
  *
15
17
  * 设计约束:
16
18
  * - `@deepseek-ai/dsh-tools` 是 optional peer,桌面版可能没有 ⇒ 取不到就降级,
@@ -133,7 +135,9 @@ export function apply(ctx, entryConfig = {}) {
133
135
  `意识流 ${s.flow.name}(${s.flow.level})`,
134
136
  `身体 ${s.body ? `${s.body.label}(${s.body.screen},${s.body.posture},${s.body.ownership})` : '无'}`,
135
137
  `命题 ${s.weights.count} 条(身份 ${s.weights.identity} 条,总权重 ${s.weights.totalWeight})`,
136
- `记忆 经历 ${s.memory.episodes} / 印象 ${s.memory.impressions} / 幻想 ${s.memory.imagination} / 蜕皮 ${s.memory.formerSelves} / 遗忘 ${s.memory.forgotten}`,
138
+ `记忆 经历 ${s.memory.episodes} / 印象 ${s.memory.impressions} / 卡片 ${s.memory.cards} / 蜕皮 ${s.memory.formerSelves} / 遗忘 ${s.memory.forgotten}`,
139
+ `平台 ${Object.keys(s.apps ?? {}).length ? Object.entries(s.apps).map(([k, v]) => `${k} ${v.seen}条`).join(' · ') : '还没在任何平台上有记录'}`,
140
+ `情绪 ${s.emotion.stage === 1 ? '只会说"说不清"' : `会 ${s.emotion.words.length} 个词`}`,
137
141
  `观察 ${s.observations} 次`,
138
142
  '',
139
143
  '权重最高的五条:',
@@ -184,6 +188,9 @@ export function apply(ctx, entryConfig = {}) {
184
188
  who: { type: 'string', description: '和谁发生了这件事' },
185
189
  text: { type: 'string', description: '发生了什么(她听到/看到的那句话)' },
186
190
  feel: { type: 'string', description: '她当时的感受,可留空' },
191
+ app: { type: 'string', description: '如果是某个平台上的事:douyin / bilibili / wechat / qq' },
192
+ kind: { type: 'string', description: '内容类型,例如 解压 / 搞笑 / 做饭,用来累计口味' },
193
+ reaction: { type: 'number', description: '她的反应,-1 到 1,正数表示喜欢' },
187
194
  },
188
195
  required: ['text'],
189
196
  additionalProperties: false,
@@ -194,12 +201,22 @@ export function apply(ctx, entryConfig = {}) {
194
201
  const text = pickStr(args?.text);
195
202
  if (!text) return fail('text 不能为空');
196
203
 
197
- const r = brain.observe(id, { who, text, what: text.slice(0, 80), feel: pickStr(args?.feel), via: 'dsh' });
204
+ const app = pickStr(args?.app);
205
+ const r = brain.observe(id, {
206
+ who,
207
+ text,
208
+ what: text.slice(0, 80),
209
+ feel: pickStr(args?.feel),
210
+ via: app ? `app:${app}` : 'dsh',
211
+ ...(app ? { app, kind: pickStr(args?.kind), reaction: Number(args?.reaction ?? 0) } : {}),
212
+ });
213
+
198
214
  const changed = r.consistent.length + r.conflicting.length;
199
215
  return {
200
216
  ok: true,
201
217
  text: [
202
218
  `记下了:${who ? `${who}「${text}」` : `「${text}」`}`,
219
+ app ? `平台 ${app}${pickStr(args?.kind) ? ` / ${pickStr(args?.kind)}` : ''}` : '',
203
220
  `权重 一致 ${r.consistent.length} 条 / 冲突 ${r.conflicting.length} 条${changed ? '' : '(没匹配到命题,人设不动)'}`,
204
221
  r.label ? `归因 ${r.label}` : '',
205
222
  ].filter(Boolean).join('\n'),
@@ -241,6 +258,86 @@ export function apply(ctx, entryConfig = {}) {
241
258
  },
242
259
  });
243
260
 
261
+ /* ── 5. 情绪 ─────────────────────────────────────────── */
262
+ tool({
263
+ name: 'feiyu_feel',
264
+ description:
265
+ '让她对一件事产生感受:先算 PAD 三维(愉悦/唤醒/掌控),再找词。'
266
+ + '同一件事,有劲的鱼偏向生气,累了的鱼偏向委屈——由人设和当前状态算出来,不查表。'
267
+ + '词表空的时候只会说「我不知道,就是很不舒服」,这是正常的。',
268
+ parameters: {
269
+ type: 'object',
270
+ properties: {
271
+ id: { type: 'string', description: '鱼的 id,默认 default' },
272
+ valence: { type: 'number', description: '这件事对她好不好,-1 到 1' },
273
+ intensity: { type: 'number', description: '强度,0 到 1' },
274
+ what: { type: 'string', description: '发生了什么' },
275
+ },
276
+ required: ['valence'],
277
+ additionalProperties: false,
278
+ },
279
+ async execute(args) {
280
+ const id = idOf(args);
281
+ const v = Math.max(-1, Math.min(1, Number(args?.valence ?? 0)));
282
+ const i = Math.max(0, Math.min(1, Number(args?.intensity ?? 0.5)));
283
+ const r = brain.feel(id, { valence: v, intensity: i, what: pickStr(args?.what) });
284
+ return {
285
+ ok: true,
286
+ text: `${r.text}\n(PAD ${r.pad.map((x) => x.toFixed(2)).join(' / ')},阶段 ${r.stage},会 ${r.knownWords} 个词)`,
287
+ pad: r.pad,
288
+ stage: r.stage,
289
+ label: r.label,
290
+ };
291
+ },
292
+ });
293
+
294
+ /* ── 6. 身体动作 ─────────────────────────────────────── */
295
+ tool({
296
+ name: 'feiyu_app',
297
+ description:
298
+ '让她真的动起来:开 App、截屏、卸载(卸载要过临界点 + 三问)。'
299
+ + '走真实设备——手机/平板用 adb,电脑用 PowerShell。没有设备返回 E_NO_DEVICE。'
300
+ + '每个动作都留证据;没有证据的动作不算发生。',
301
+ parameters: {
302
+ type: 'object',
303
+ properties: {
304
+ id: { type: 'string', description: '鱼的 id,默认 default' },
305
+ action: { type: 'string', description: 'open / screenshot / uninstall' },
306
+ package: { type: 'string', description: 'Android 包名,例如 com.ss.android.ugc.aweme' },
307
+ target: { type: 'string', description: '电脑上要启动的程序' },
308
+ why: { type: 'string', description: '卸载 Q1:为什么想删' },
309
+ consequence: { type: 'string', description: '卸载 Q3:删了会怎样' },
310
+ },
311
+ required: ['action'],
312
+ additionalProperties: false,
313
+ },
314
+ async execute(args) {
315
+ const id = idOf(args);
316
+ const action = pickStr(args?.action).toLowerCase();
317
+
318
+ if (action === 'uninstall') {
319
+ const r = await brain.requestUninstall(id, pickStr(args?.package), {
320
+ why: pickStr(args?.why),
321
+ fits: true,
322
+ consequence: pickStr(args?.consequence),
323
+ });
324
+ if (r.ok === false) return { ok: false, code: r.code, text: `❌ ${r.reason ?? r.code}` };
325
+ return { ok: true, text: `已卸载 ${args.package}\n证据:${r.evidence}` };
326
+ }
327
+
328
+ const map = {
329
+ open: { type: 'open_app', package: pickStr(args?.package), target: pickStr(args?.target), capability: 2 },
330
+ screenshot: { type: 'screenshot', capability: 1 },
331
+ };
332
+ const act = map[action];
333
+ if (!act) return { ok: false, code: 'E_ARGS', text: '❌ action 只能是 open / screenshot / uninstall' };
334
+
335
+ const r = await brain.execAction(id, act);
336
+ if (r.ok === false) return { ok: false, code: r.code, text: `❌ ${r.reason ?? r.code}` };
337
+ return { ok: true, text: `✔ 做完了(${action})\n证据:${r.evidence ?? '(无)'}` };
338
+ },
339
+ });
340
+
244
341
  /* ── 配置检查(不注册工具,只打一行日志)───────────── */
245
342
  try {
246
343
  const s = brain.snapshot(conf.brainId);
@@ -14,5 +14,5 @@ name: fangqiuci/feiyu-brain
14
14
  category: memory
15
15
 
16
16
  description:
17
- en: 'Persona-grounded brain kernel with four tools (feiyu_status, feiyu_wake, feiyu_observe, feiyu_tick): weighted traits that erode into memories, a 30-day recall window, PAD emotions, and phone/tablet/desktop bodies.'
18
- zh: '以人设为根的大脑内核,提供 4 个工具(feiyu_status / feiyu_wake / feiyu_observe / feiyu_tick):带权重的命题随经历涨落、蜕皮成记忆,30 天回忆窗口,PAD 三维情绪,手机/平板/电脑三具身体。'
17
+ en: 'Persona-grounded brain kernel with six tools (feiyu_status, feiyu_wake, feiyu_observe, feiyu_tick, feiyu_feel, feiyu_app): weighted traits that erode into memories, a 30-day recall window, PAD emotions, and phone/tablet/desktop bodies.'
18
+ zh: '以人设为根的大脑内核,提供 6 个工具(feiyu_status / feiyu_wake / feiyu_observe / feiyu_tick / feiyu_feel / feiyu_app):带权重的命题随经历涨落、蜕皮成记忆,30 天回忆窗口,PAD 三维情绪,手机/平板/电脑三具身体。'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "feiyu-brain",
3
- "version": "1.2.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
  }
@@ -102,10 +104,23 @@ export function listBrains() {
102
104
  }
103
105
 
104
106
  export function loadBrain(id) {
105
- return readJson(brainFile(id), null);
107
+ const file = brainFile(id);
108
+ const main = readJson(file, null);
109
+ if (main) return main;
110
+ // 主文件坏了就退到备份——评审说得对,单文件损坏不该等于丢档
111
+ const bak = readJson(`${file}.bak`, null);
112
+ if (bak) {
113
+ try { writeJsonAtomic(file, bak); } catch { /* 修不回来也不影响这次读 */ }
114
+ return bak;
115
+ }
116
+ return null;
106
117
  }
107
118
 
108
119
  export function saveBrain(brain) {
109
- writeJsonAtomic(brainFile(brain.id), brain);
120
+ const file = brainFile(brain.id);
121
+ try {
122
+ if (fs.existsSync(file)) fs.copyFileSync(file, `${file}.bak`);
123
+ } catch { /* 备份失败不影响写入 */ }
124
+ writeJsonAtomic(file, brain);
110
125
  return brain;
111
126
  }