/** * ============================================================ * plugins/computer_use/src/manuals.ts —— 应用说明书的清单与详情 * ------------------------------------------------------------ * 说明书文件住在本插件的 `app/`,但**解析与匹配**用内核的 * `vision/appGuide.ts` —— 内核工具在每次观测时也用同一份逻辑,两边必须 * 是同一套判断,否则 `/computeruse apps` 说"命中了"而模型那边没命中。 * ============================================================ */ import { computerUseDiagnostics, describeMatch, EMPTY_MANUAL, loadAllAppManuals, matchGuide, validateManual, type LoadedManuals, } from '../../../src/plugin-sdk.js'; /** 全部说明书 + 冲突报告。与内核工具用**同一个**加载器,不另写一份。 */ export async function loadAllManuals(): Promise { return loadAllAppManuals(); } /** 当前前台应用身份。拿不到就返回 undefined —— 不猜。 */ async function foregroundIdentity( signal?: AbortSignal, ): Promise<{ id?: string; name?: string } | undefined> { if (!computerUseDiagnostics.platformSupported()) return undefined; const response = await computerUseDiagnostics.observe(signal).catch(() => null); if (!response?.ok || !response.state?.activeApp) return undefined; return { id: response.state.activeApp.id, name: response.state.activeApp.name }; } export async function runApps(signal?: AbortSignal): Promise { const { guides, conflicts, rejected } = await loadAllManuals(); const lines = ['# 应用说明书', '']; if (guides.length === 0 && rejected.length === 0) { lines.push( '(一份都没有。工作区说明书放 `app-manuals/.md`;', '插件提供的要在 plugin.json 里声明 `"appManuals": "app"`。)', ); return lines.join('\n'); } for (const guide of guides) { const identifiers = [...guide.bundleIds, ...guide.processNames, ...guide.displayNames]; lines.push( `- **${guide.name}** — 匹配标识 ${identifiers.length} 个 · 不可逆声明 ${guide.irreversible.length} 条`, ); } // 被隔离的说明书必须可见,而且要说得出错在哪 —— 它们的失败方式全是静默的: // match 写错就永远匹配不上、precondition 写错就永远不生效。不报出来,用户 // 会以为自己没写过这份说明书。 if (rejected.length > 0) { lines.push('', '## ⛔ 已隔离(写错了,不会进入模型上下文)'); for (const item of rejected) { lines.push(`- **${item.guide.name}** — ${item.guide.path}`); for (const message of item.errors) lines.push(` - ${message}`); } } // 冲突必须可见:靠目录顺序决定「哪份知识生效」而没人知道,是最难查的那类问题。 if (conflicts.length > 0) { lines.push('', '## ⚠️ 冲突'); for (const conflict of conflicts) { lines.push(conflict.kind === 'same-name' ? `- 同名说明书 \`${conflict.key}\`:生效 ${conflict.winner},被遮蔽 ${conflict.shadowed}` : `- 标识 \`${conflict.key}\` 被两份说明书同时认领:生效 ${conflict.winner},被遮蔽 ${conflict.shadowed}`); } } const identity = await foregroundIdentity(signal); lines.push('', '## 当前前台应用'); if (!identity) { lines.push('拿不到前台应用(平台不支持、权限未授予或守护进程不可用)。'); } else { lines.push(`${identity.name ?? '未知'}(${identity.id ?? '无标识'})`, ''); lines.push(describeMatch(matchGuide(guides, identity), guides, identity)); } return lines.join('\n'); } export async function runApp(name: string): Promise { const trimmed = name.trim(); if (!trimmed) return '用法:`/computeruse app `(名字见 `/computeruse apps`)'; const { guides, rejected } = await loadAllManuals(); // 被隔离的那些也要能查 —— 用户最需要看详情的恰恰是写坏的那一份。 const blocked = rejected.find((item) => item.guide.name === trimmed); const guide = guides.find((candidate) => candidate.name === trimmed) ?? blocked?.guide; if (!guide) { return [ `没有名为「${trimmed}」的说明书。`, `已有:${guides.map((g) => g.name).join('、') || '(无)'}`, ].join('\n'); } const checkable = guide.irreversible.filter( (entry) => entry.startsWith('key:') || entry.startsWith('region:'), ); const manual = guide.manual ?? EMPTY_MANUAL; const issues = validateManual({ manual, irreversible: guide.irreversible, identifiers: guide.bundleIds.length + guide.processNames.length + guide.displayNames.length, }); const unverified = [ ...manual.layout.filter((r) => !r.verified).map((r) => `分区 ${r.id}`), ...manual.controls.filter((c) => !c.verified).map((c) => `控件 ${c.id}`), ...manual.flows.filter((f) => !f.verified).map((f) => `流程 ${f.id}`), ]; return [ `# 说明书 ${guide.name}`, '', // 隔离状态放最前面:其它信息都建立在「它到底生不生效」之上。 ...(blocked ? ['⛔ **已隔离**:这份说明书写错了,不会进入模型上下文。修完下面的 ❌ 才会生效。', ''] : []), `文件:${guide.path}`, `schema v${manual.schemaVersion}`, `bundleId:${guide.bundleIds.join('、') || '(无)'}`, `进程名:${guide.processNames.join('、') || '(无)'}`, `显示名:${guide.displayNames.join('、') || '(无)'}`, '', '## 不可逆声明', ...(guide.irreversible.length > 0 ? guide.irreversible.map((entry) => `- ${entry}`) : ['(无)']), '', checkable.length > 0 ? `其中 ${checkable.length} 条是**引擎可独立验证**的(key/region)。` : '⚠️ 全部是 `intent:` 形式 —— 只能靠模型自报,模型漏报就拦不住。' + '等实机验证出稳定的按键或区域,改成 key:/region: 才是真拦截。', '', '## 结构化内容', `分区 ${manual.layout.length} · 控件 ${manual.controls.length} · 流程 ${manual.flows.length}`, ...manual.flows.map((flow) => { const guarded = flow.steps.filter((s) => s.precondition).length; return `- ${flow.id}:${flow.steps.length} 步` + (guarded > 0 ? `,其中 ${guarded} 步有机器可检查的前置条件` : ',无前置条件'); }), '', '## 校验', ...(issues.length > 0 ? issues.map((issue) => `${issue.level === 'error' ? '❌' : '⚠️'} ${issue.message}`) : ['✅ 无问题']), '', '## 未验证条目', // 未验证不是缺陷,是**状态**。说清楚才能知道哪些还得实机跑一遍; // 不说,就等于把猜测当事实用。 unverified.length > 0 ? `${unverified.length} 项:${unverified.join('、')}` : '(无 —— 所有结构化条目都已标记 verified)', ].join('\n'); }