import type { HumanInterventionCoordinator, HumanInterventionReason, PendingHumanIntervention, } from '../../humanIntervention.js'; import { detectComputerHumanBoundary } from './humanBoundary.js'; import type { ManualHumanBoundary } from './manualSchema.js'; import type { ComputerState, ComputerUseRunContext } from './protocol.js'; export interface ComputerInterventionAdapter { inspect( state: ComputerState, signal?: AbortSignal, ): Promise<{ resolution: 'none' | 'resumed' | 'cancelled'; state?: ComputerState }>; pause( reason: HumanInterventionReason, summary: string, signal?: AbortSignal, ): Promise<{ resolution: 'resumed' | 'cancelled'; state?: ComputerState }>; } export function createComputerInterventionAdapter(input: { coordinator: HumanInterventionCoordinator; context: ComputerUseRunContext; observe(signal?: AbortSignal): Promise; onResumed?(state: ComputerState): void; /** * 按当前观测解析出该应用的说明书,供应用专属的人工介入信号使用。 * * ⚠️ 以依赖注入的形式给进来,而不是让本模块自己去 import 说明书加载器 —— * 那会让「通用的人机边界判定」反向依赖「具体应用的知识加载」,正是要避免的 * 方向。不注入时退化成只用内核的通用判据,行为与从前一致。 */ manualFor?(state: ComputerState): { humanBoundary?: ManualHumanBoundary[] } | undefined; }): ComputerInterventionAdapter { const detect = (state: ComputerState) => detectComputerHumanBoundary(state, input.manualFor?.(state)); const waitForOwner = async ( pending: PendingHumanIntervention, signal?: AbortSignal, ): Promise<{ resolution: 'resumed' | 'cancelled'; state?: ComputerState }> => { const resolution = await input.coordinator.wait(pending.id, signal); if (resolution.status !== 'resumed') return { resolution: 'cancelled' }; const refreshed = await input.observe(signal); if (!refreshed) return { resolution: 'cancelled' }; return { resolution: 'resumed', state: refreshed }; }; return { async pause(reason, summary, signal) { let pending = input.coordinator.get(input.context) ?? input.coordinator.request({ ownerId: input.context.ownerId, conversationKey: input.context.conversationKey, runId: input.context.runId, surface: 'computer', reason, sanitizedSummary: summary, }); while (true) { const resolution = await waitForOwner(pending, signal); if (resolution.resolution !== 'resumed' || !resolution.state) return resolution; const boundary = detect(resolution.state); if (!boundary) { input.onResumed?.(resolution.state); return resolution; } pending = input.coordinator.request({ ownerId: input.context.ownerId, conversationKey: input.context.conversationKey, runId: input.context.runId, surface: 'computer', reason: boundary.reason, sanitizedSummary: boundary.summary, target: boundary.target, }); } }, async inspect(initialState, signal) { let state = initialState; let pending = input.coordinator.get(input.context); let boundary = detect(state); if (!pending && !boundary) return { resolution: 'none' }; while (true) { if (!pending) { if (!boundary) return { resolution: 'resumed', state }; pending = input.coordinator.request({ ownerId: input.context.ownerId, conversationKey: input.context.conversationKey, runId: input.context.runId, surface: 'computer', reason: boundary.reason, sanitizedSummary: boundary.summary, target: boundary.target, }); } const resolution = await waitForOwner(pending, signal); if (resolution.resolution !== 'resumed' || !resolution.state) { return { resolution: 'cancelled' }; } state = resolution.state; boundary = detect(state); pending = undefined; if (!boundary) { input.onResumed?.(state); return { resolution: 'resumed', state }; } } }, }; }