/** * ============================================================ * plugins/computer_use/src/daemon.ts —— 与守护进程打交道的确定性命令 * ------------------------------------------------------------ * 全部经内核的 `computerUseDiagnostics` 只读门面。插件**拿不到**动作派发能力 * ——不是靠这里自觉不调用,而是门面根本没有那个方法(见 diagnostics.ts 头注)。 * ============================================================ */ import { computerUseDiagnostics, describeCapabilities, type ComputerUseResponse, } from '../../../src/plugin-sdk.js'; /** 平台不支持时统一的一段话。分开写是因为它不是「出错」,是「本来就没有」。 */ function unsupportedNote(): string { return [ `⚠️ 当前平台是 ${process.platform},Computer Use 只支持 macOS。`, 'Windows 侧只有协议桩,没有 UI Automation 驱动 —— 不是「装一下就能用」。', ].join('\n'); } function describeError(response: ComputerUseResponse): string { return `❌ ${response.error?.code ?? 'failed'}: ${response.error?.message ?? '未知错误'}` + (response.error?.hint ? `\n ${response.error.hint}` : ''); } export async function runStatus(signal?: AbortSignal): Promise { const lines = ['# Computer Use 状态', '']; if (!computerUseDiagnostics.platformSupported()) { lines.push(unsupportedNote()); return lines.join('\n'); } const helper = computerUseDiagnostics.helperPath(); lines.push(`守护进程二进制:${helper ?? '未找到(vendor/computer-use 缺失)'}`); lines.push(`状态目录:${computerUseDiagnostics.stateDir()}`); lines.push(`说明书写入目录:${computerUseDiagnostics.manualDir()}`); if (!helper) { lines.push('', '没有 helper 就没有一切。请确认 `vendor/computer-use/<平台>/cu-helperd` 存在。'); return lines.join('\n'); } const hello = await computerUseDiagnostics.status(signal); if (!hello.ok) { lines.push('', describeError(hello)); return lines.join('\n'); } const data = (hello.data ?? {}) as { serviceVersion?: string; platform?: string; implementation?: string; capabilities?: string[]; }; lines.push( `服务版本:${data.serviceVersion ?? '未知'} · ${data.platform ?? '?'} / ${data.implementation ?? '?'}`, '', renderCapabilities(data.capabilities ?? []), ); return lines.join('\n'); } /** * 能力清单按三态分组呈现。映射来自内核的 `capabilityLabels.ts`,与 Desktop * 面板**共用同一份** —— 两边各自解析字符串前缀是上一版的漂移来源。 */ function renderCapabilities(ids: string[]): string { if (ids.length === 0) return '## 能力\n\n驱动没有报告任何能力。'; const labels = describeCapabilities(ids); const group = (state: string): string[] => labels.filter((label) => label.state === state) .map((label) => `- ${label.label}${label.reason ? ` —— ${label.reason}` : ''}`); const supported = group('supported'); const disabled = group('deliberately-disabled'); const missing = group('not-implemented'); return [ '## 能力', '', '### 已支持', ...(supported.length > 0 ? supported : ['- (无)']), ...(disabled.length > 0 ? ['', '### 有意禁用(不是缺口,是决定)', ...disabled] : []), ...(missing.length > 0 ? ['', '### 尚未实现', ...missing] : []), ].join('\n'); } const PERMISSION_LABEL: Record = { screenCapture: '屏幕录制', accessibility: '辅助功能', inputControl: '输入控制', ocr: 'OCR', }; /** * 权限体检。每一项都给出**能照着做的**下一步,而不是只报一个状态词 —— * 「denied」对用户来说不是信息,「去哪个开关里把它打开」才是。 */ export async function runDoctor(signal?: AbortSignal): Promise { if (!computerUseDiagnostics.platformSupported()) return unsupportedNote(); const response = await computerUseDiagnostics.permissions(signal); if (!response.ok) { return ['# Computer Use 权限体检', '', describeError(response)].join('\n'); } const snapshot = (response.data ?? {}) as Record; const lines = ['# Computer Use 权限体检', '']; let blocked = 0; for (const [key, label] of Object.entries(PERMISSION_LABEL)) { const state = snapshot[key] ?? 'unknown'; const ok = state === 'granted'; if (!ok && key !== 'ocr') blocked += 1; lines.push(`${ok ? '✅' : state === 'unsupported' ? '➖' : '❌'} ${label}:${state}`); if (!ok && key === 'screenCapture') { lines.push(' 系统设置 → 隐私与安全性 → 屏幕录制,勾选本应用后**需要重启应用**。'); } if (!ok && key === 'accessibility') { lines.push(' 系统设置 → 隐私与安全性 → 辅助功能。没有它就拿不到控件树,只能看像素。'); } if (!ok && key === 'inputControl') { lines.push(' 输入控制随辅助功能一起授予;仍为 denied 时把本应用从列表里删掉再重新添加。'); } if (key === 'ocr' && state !== 'granted') { lines.push(' OCR 是**有意不实现**的(见 SPEC),这里不是故障。'); } } lines.push('', blocked === 0 ? '权限齐全。' : `有 ${blocked} 项关键权限未就绪,Computer Use 会退化或直接失败。`); return lines.join('\n'); } /** 跑一次观测并把结果**给人看**(不进模型上下文,所以不组装图)。 */ export async function runObserve(signal?: AbortSignal): Promise { if (!computerUseDiagnostics.platformSupported()) return unsupportedNote(); const response = await computerUseDiagnostics.observe(signal); if (!response.ok || !response.state) { return ['# 一次观测', '', describeError(response)].join('\n'); } const state = response.state; const lines = [ '# 一次观测', '', `前台应用:${state.activeApp?.name ?? '未知'}(${state.activeApp?.id ?? '无标识'})`, `显示器:${state.displays.length} 台 · 窗口:${state.windows.length} 个`, `无障碍树:${state.accessibility ? (state.accessibility.truncated ? '不完整(权限不足或元素过多)' : '可用') : '无'}`, `revision ${state.revision} · epoch ${state.epoch} · backend ${state.backend}`, ]; if (state.windows.length > 0) { lines.push('', '## 窗口'); for (const window of state.windows.slice(0, 10)) { lines.push(`- ${window.title ?? '(无标题)'} — ${window.appId}`); } } return lines.join('\n'); }