/** * ============================================================ * plugins/computer_use/src/verify.ts —— 半自动实机验收 * ------------------------------------------------------------ * 为什么需要它:说明书里的流程目前全部 `verified: false` —— 它们是把面向人的 * 帮助文档转写成 agent 视角,**没有一条实机跑过**。而实机验收这件事我单方面 * 做不了:它会接管用户的屏幕、需要人在场判断「这一步到底成了没有」。 * * 于是把它做成脚手架:逐步提示 → 人拍板 → 每步自动观测并记下**实际看到的** → * 一条流程全过才把 `verified` 翻真。用户什么时候方便就跑一个 app, * 结论可累积、可审计。 * * ★ 三条纪律: * ① **人说通过才算通过。** 观测只负责记录现场,不替人判断成败 —— 引擎看不出 * 「消息真的发给了张三」这种事。 * ② **只翻全过的那条流程。** 任一步失败或跳过,整条保持 false 并记下卡在哪。 * 部分通过就翻真,等于把「大概能用」写成「已验证」。 * ③ **写工作区,不动包内文件**(见 diagnostics.ts::saveVerification)。 * * 问用户走 `executeTool('AskUserQuestion')` —— 与 workflow-runner 的 pause 同 * 一条通道,非交互宿主会自动选推荐项(这里推荐项是「停止」,见下)。 * ============================================================ */ import { computerUseDiagnostics, executeTool, type ManualFlow, type ManualSource, } from '../../../src/plugin-sdk.js'; /** 每步三选一。「跳过」与「失败」都不算通过 —— 只是记下来的理由不同。 */ type StepVerdict = 'passed' | 'skipped' | 'failed' | 'aborted'; interface StepRecord { flowId: string; index: number; step: string; verdict: StepVerdict; /** 这一步执行时**实际看到的**现场。 */ scene: string; } export async function runVerify(raw: string, signal?: AbortSignal): Promise { const name = raw.trim().split(/\s+/)[0] ?? ''; if (!name) { return '用法:`/computeruse verify `(名字见 `/computeruse apps`)\n' + '它会逐步提示你操作、每步记录实际观测,全部通过才把该流程标记为已验证。'; } if (!computerUseDiagnostics.platformSupported()) { return `⚠️ 当前平台是 ${process.platform},实机验收只能在 macOS 上做。`; } const source = await computerUseDiagnostics.readManualSource(name); if (typeof source === 'string') return `# 实机验收\n\n${source}`; const flows = source.guide.manual?.flows ?? []; if (flows.length === 0) { return [ `# 实机验收:${name}`, '', '这份说明书没有声明任何 `flows:` —— 没有可验收的流程。', '先用 `/computeruse capture` 采一份草稿,把流程补上,再回来验收。', ].join('\n'); } const records: StepRecord[] = []; const verifiedFlows: string[] = []; let aborted = false; for (const flow of flows) { if (aborted) break; if (flow.verified) continue; // 已验证过的不重复折腾人 const outcome = await verifyFlow(flow, records, signal); if (outcome === 'aborted') { aborted = true; break; } if (outcome === 'passed') verifiedFlows.push(flow.id); } return renderResult({ source, flows, records, verifiedFlows, aborted, signal }); } async function verifyFlow( flow: ManualFlow, records: StepRecord[], signal?: AbortSignal, ): Promise<'passed' | 'failed' | 'aborted'> { let allPassed = flow.steps.length > 0; for (const [index, step] of flow.steps.entries()) { const verdict = await askStep(flow, index, step.do, signal); const scene = await observeScene(signal); records.push({ flowId: flow.id, index: index + 1, step: step.do, verdict, scene }); if (verdict === 'aborted') return 'aborted'; if (verdict !== 'passed') { allPassed = false; // 一步没过,这条流程后面的步骤多半也无从谈起 —— 不再逐个折腾人。 break; } } return allPassed ? 'passed' : 'failed'; } async function askStep( flow: ManualFlow, index: number, step: string, signal?: AbortSignal, ): Promise { const result = await executeTool( 'AskUserQuestion', JSON.stringify({ questions: [{ header: `验收 ${flow.id}`, question: `【${flow.id} 第 ${index + 1}/${flow.steps.length} 步】${step}\n` + (flow.goal ? `(这条流程的目标:${flow.goal})\n` : '') + '请你在真实应用里做这一步,然后告诉我结果。', options: [ // 首项是非交互宿主的自动兜底 —— 必须是「停止」:无人在场时不该 // 产出一份「全部通过」的验收结论。 { label: '停止验收', description: '结束本次验收,已记录的结果保留。' }, { label: '做到了', description: '这一步按说明书描述完成了。' }, { label: '跳过', description: '这一步这次不测(不算通过)。' }, { label: '失败了', description: '照说明书做不出来 —— 说明书与实际不符。' }, ], }], }), signal, ); if (!result.ok) return 'aborted'; const label = String(result.content ?? ''); if (label.includes('做到了')) return 'passed'; if (label.includes('跳过')) return 'skipped'; if (label.includes('失败')) return 'failed'; return 'aborted'; } /** 记下这一步执行后**实际看到的**现场。观测失败也如实写,不留空。 */ async function observeScene(signal?: AbortSignal): Promise { const response = await computerUseDiagnostics.observe(signal).catch(() => null); if (!response?.ok || !response.state) { return `观测失败(${response?.error?.code ?? 'unknown'})`; } const state = response.state; const windows = state.windows.map((w) => w.title ?? '(无标题)').slice(0, 3).join(' / '); return [ `前台 ${state.activeApp?.name ?? '未知'}(${state.activeApp?.id ?? '无标识'})`, `窗口 ${windows || '无'}`, `AX ${state.accessibility ? (state.accessibility.truncated ? '不完整' : '可用') : '无'}`, ].join(' · '); } async function renderResult(input: { source: ManualSource; flows: ManualFlow[]; records: StepRecord[]; verifiedFlows: string[]; aborted: boolean; signal?: AbortSignal; }): Promise { const { source, records, verifiedFlows, aborted } = input; const lines = [ `# 实机验收:${source.name}`, '', `说明书:${source.path}`, aborted ? '⚠️ 验收被中止,下面只是已经走过的部分。' : '', '', '## 逐步结果', '', ].filter((line) => line !== '' || true); if (records.length === 0) { return [ `# 实机验收:${source.name}`, '', '没有可验收的流程 —— 声明的流程可能都已标记为已验证。', '想重验请先把对应流程的 `verified` 改回 false。', ].join('\n'); } for (const record of records) { const mark = record.verdict === 'passed' ? '✅' : record.verdict === 'skipped' ? '⏭️' : record.verdict === 'failed' ? '❌' : '⛔'; lines.push(`${mark} ${record.flowId} 第 ${record.index} 步:${record.step}`); lines.push(` 实际观测:${record.scene}`); } lines.push( '', '## 结论', '', verifiedFlows.length > 0 ? `全部通过、可标记为已验证的流程:${verifiedFlows.join('、')}` : '没有任何流程全部通过 —— 保持未验证。', ); const notes = buildNotes(records, verifiedFlows); const written = await computerUseDiagnostics.saveVerification( { source, verifiedFlows, notes }, input.signal, ); lines.push('', written.ok ? `✅ 已写入 ${written.path}` : `未写盘:${written.error}`); if (!written.ok) { lines.push('', '实测记录(可自己贴进说明书的 `## 实测记录` 一节):', '', ...notes); } return lines.join('\n'); } /** 追加进 `## 实测记录` 的行。带日期,因为「上次能用这次不能」本身是信息。 */ function buildNotes(records: StepRecord[], verifiedFlows: string[]): string[] { const day = new Date().toISOString().slice(0, 10); const notes = [`- ${day} 实机验收:通过 ${verifiedFlows.join('、') || '(无)'}`]; for (const record of records.filter((item) => item.verdict !== 'passed')) { notes.push( ` - ${record.flowId} 第 ${record.index} 步(${record.verdict}):${record.step}` + ` —— 当时 ${record.scene}`, ); } return notes; }