feiyu-brain 1.6.4 → 1.6.5

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/core/body.js CHANGED
@@ -52,7 +52,9 @@ export const CAPABILITY = {
52
52
  /** 不同归属默认给到哪一级。L4 永不默认。 */
53
53
  export const DEFAULT_SCOPE = {
54
54
  owned: [CAPABILITY.OBSERVE, CAPABILITY.OPERATE, CAPABILITY.DESTROY],
55
- borrowed: [CAPABILITY.OBSERVE, CAPABILITY.OPERATE],
55
+ // 借来的设备只给「看」。打字/点击/按键会打到别人的窗口里,
56
+ // 要动手必须先显式升级(ownership='owned' 或经授权)。
57
+ borrowed: [CAPABILITY.OBSERVE],
56
58
  emulator: [CAPABILITY.OBSERVE, CAPABILITY.OPERATE, CAPABILITY.DESTROY],
57
59
  };
58
60
 
package/core/driver.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { resolveTarget } from './platforms.js';
2
+ import { findDesktopApp, notInstalledHint } from './findapp.js';
2
3
  // 身体驱动:真的去开 App、真的去截图、真的去卸载。
3
4
  //
4
5
  // 三种驱动:
@@ -181,6 +182,8 @@ export class DesktopDriver {
181
182
  const raw = action.target || action.package;
182
183
  const target = resolveTarget(raw, { android: false });
183
184
  if (!target) return fail('E_ARGS', 'open_app 需要 target');
185
+ // 名字 → 真的找得到才开;找不到就明说没装
186
+ if (!findDesktopApp(target)) return fail('E_NOT_INSTALLED', notInstalledHint(raw));
184
187
  await run('powershell', ['-NoProfile', '-Command', `Start-Process ${JSON.stringify(target)}`], { timeout: this.timeoutMs });
185
188
  return ok({ evidence: `desktop:start:${target}`, app: target });
186
189
  }
@@ -0,0 +1,74 @@
1
+ // 桌面程序到底能不能开。
2
+ //
3
+ // 光有"名字"没用:Windows 上 douyin / WeChat 都不在 PATH 里。
4
+ // 但**不许瞎猜路径**——没装就是没装,要说清楚。
5
+ //
6
+ // 顺序:PATH → 注册表 App Paths → 明确失败。
7
+
8
+ import fs from 'node:fs';
9
+ import { execFileSync } from 'node:child_process';
10
+ import { PLATFORMS } from './platforms.js';
11
+
12
+ /** 已知一定能开的(Windows 自带) */
13
+ export const ALWAYS_OPENABLE = ['notepad', 'calc', 'mspaint', 'explorer', 'cmd', 'msedge'];
14
+
15
+ function viaPath(name) {
16
+ try {
17
+ const out = execFileSync('where.exe', [name], { timeout: 2000, encoding: 'utf8' });
18
+ const first = String(out).split(/\r?\n/).map((x) => x.trim()).find(Boolean);
19
+ return first || null;
20
+ } catch {
21
+ return null;
22
+ }
23
+ }
24
+
25
+ function viaRegistry(name) {
26
+ const base = name.replace(/\.exe$/i, '');
27
+ const ps = [
28
+ `$p = (Get-ItemProperty "HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\${base}.exe" -ErrorAction SilentlyContinue).'(default)'`,
29
+ 'if ($p) { Write-Output $p }',
30
+ ].join('; ');
31
+ try {
32
+ const out = execFileSync('powershell', ['-NoProfile', '-Command', ps], { timeout: 3000, encoding: 'utf8' });
33
+ const first = String(out).trim().split(/\r?\n/).map((x) => x.trim()).find(Boolean);
34
+ return first || null;
35
+ } catch {
36
+ return null;
37
+ }
38
+ }
39
+
40
+ /**
41
+ * 找得到就返回绝对路径,找不到返回 null。
42
+ * **不编路径**——这台电脑没装,就该说没装。
43
+ */
44
+ export function findDesktopApp(name) {
45
+ const raw = String(name ?? '').trim();
46
+ if (!raw) return null;
47
+ if (/^[A-Za-z]:[\\/]/.test(raw)) return fs.existsSync(raw) ? raw : null;
48
+
49
+ const candidates = [raw];
50
+ if (!/\.exe$/i.test(raw)) candidates.push(raw + '.exe');
51
+
52
+ for (const c of candidates) {
53
+ const p = viaPath(c);
54
+ if (p) return p;
55
+ }
56
+ for (const c of candidates) {
57
+ const p = viaRegistry(c);
58
+ if (p && fs.existsSync(p)) return p;
59
+ }
60
+ return null;
61
+ }
62
+
63
+ /** 找不到时给她一句能照做的话 */
64
+ export function notInstalledHint(name) {
65
+ const key = String(name ?? '').toLowerCase();
66
+ const p = PLATFORMS[key];
67
+ const label = p ? p.name : String(name ?? '');
68
+ return [
69
+ '这台电脑上没找到「' + label + '」。',
70
+ '可能没装,也可能装在很偏的地方。',
71
+ '现在能直接开的:' + ALWAYS_OPENABLE.join(' / ') + '。',
72
+ '要开别的,就把完整路径当 target 传进来。',
73
+ ].join('');
74
+ }
@@ -376,16 +376,32 @@ export function apply(ctx, entryConfig = {}) {
376
376
  open: { type: 'open_app', package: pickStr(args?.package), target: pickStr(args?.target), capability: 2 },
377
377
  open_app: { type: 'open_app', package: pickStr(args?.package), target: pickStr(args?.target), capability: 2 },
378
378
  open_url: { type: 'open_url', url: pickStr(args?.target) || pickStr(args?.package), capability: 2 },
379
- focus: { type: 'focus', title: pickStr(args?.target), capability: 1 },
380
- click: { type: 'click', x: Number(args?.x), y: Number(args?.y), capability: 1 },
381
- type: { type: 'type', text: pickStr(args?.target), capability: 1 },
382
- key: { type: 'key', key: pickStr(args?.target), capability: 1 },
383
- scroll: { type: 'scroll', delta: Number(args?.delta ?? -3), capability: 1 },
379
+ focus: { type: 'focus', title: pickStr(args?.target), capability: 2 },
380
+ // 点一下、打一段字、按个键、滚一下——这些会真的作用到**当前焦点窗口**,
381
+ // 不是"看一眼"。所以统统一律 OPERATE,而且没 approved 就先干跑。
382
+ click: { type: 'click', x: Number(args?.x), y: Number(args?.y), capability: 2 },
383
+ type: { type: 'type', text: pickStr(args?.target), capability: 2 },
384
+ key: { type: 'key', key: pickStr(args?.target), capability: 2 },
385
+ scroll: { type: 'scroll', delta: Number(args?.delta ?? -3), capability: 2 },
384
386
  screenshot: { type: 'screenshot', capability: 1 },
385
387
  };
386
388
  const act = map[action];
387
389
  if (!act) return { ok: false, code: 'E_ARGS', text: '❌ action 只能是 open / open_url / focus / click / type / key / scroll / screenshot / uninstall' };
388
390
 
391
+ // 会真的作用到桌面上的动作,第一次先干跑。
392
+ // 想真的做,再调一次并传 approved: true。
393
+ const approved = args?.approved === true;
394
+ if (act.capability >= 2 && !approved && action !== 'open' && action !== 'open_app') {
395
+ return {
396
+ ok: false,
397
+ code: 'E_NEED_APPROVAL',
398
+ text: `⚠️ 这是干跑:${action} ${JSON.stringify(act).slice(0, 80)}\n`
399
+ + '它会真的作用到你现在的焦点窗口。确认要做,就再调一次并传 approved: true。',
400
+ dryRun: true,
401
+ would: act,
402
+ };
403
+ }
404
+
389
405
  const r = await brain.execAction(id, act);
390
406
  if (r.ok === false) return { ok: false, code: r.code, text: `❌ ${r.reason ?? r.code}` };
391
407
  return { ok: true, text: `✔ 做完了(${action})\n证据:${r.evidence ?? '(无)'}` };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "feiyu-brain",
3
- "version": "1.6.4",
3
+ "version": "1.6.5",
4
4
  "description": "给 AI 装一颗有记忆、有情绪、会变化的脑;带上嘴(外接 OneBot/官方)和手(点、打、滑、执行)。",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/selftest.mjs CHANGED
@@ -267,9 +267,11 @@ t('三具身体都能造出来', () => {
267
267
  }
268
268
  });
269
269
 
270
- t('借来的手机不能删软件(没有 L3)', () => {
270
+ t('借来的设备只能看,不能动手也不能删', () => {
271
271
  const b = createBody({ kind: 'phone', ownership: 'borrowed' });
272
- assert.equal(canDo(b, { capability: CAPABILITY.OPERATE }).ok, true);
272
+ // 借来的设备打字/点击会打到别人的窗口里,所以连 OPERATE 都不给(1.6.5 起)
273
+ assert.equal(canDo(b, { capability: CAPABILITY.OBSERVE }).ok, true);
274
+ assert.equal(canDo(b, { capability: CAPABILITY.OPERATE }).ok, false);
273
275
  assert.equal(canDo(b, { capability: CAPABILITY.DESTROY }).ok, false);
274
276
  });
275
277