type AIVendor = 'deepseek' | 'openai' | 'anthropic' | 'claude' | 'google' | 'gemini' | 'azure' | 'custom'; interface AIProviderConfig { vendor: AIVendor; baseUrl?: string; apiKey: string; model: string; headers?: Record; } interface DeviceConnection { hostIp: string; deviceId: string; } type ToolCategory = 'observation' | 'action'; interface ToolCall { id: string; name: string; arguments: Record; source?: 'local' | 'mcp'; server?: string; } type ToolErrorCode = 'TIMEOUT' | 'NETWORK' | 'HTTP_4XX' | 'HTTP_5XX' | 'VALIDATION' | 'UNKNOWN_TOOL' | 'ABORTED' | 'RUNTIME_GUARD' | 'MODEL_OUTPUT_INVALID' | 'PLAN_VALIDATION_FAILED' | 'SCRIPT_VALIDATION_FAILED' | 'UNKNOWN'; interface ToolResult { toolCallId: string; name: string; success: boolean; data?: unknown; error?: string; warning?: string; errorCode?: ToolErrorCode; retryable?: boolean; attempt?: number; latencyMs?: number; source?: 'local' | 'mcp'; server?: string; } interface ExecutionLogEntry { index: number; toolName: string; arguments: Record; result: ToolResult; category: ToolCategory; timestamp: number; } interface WorkflowScript { id: string; name: string; version: string; steps: Record; flow: string[]; description?: string; timeout?: number; exception_handlers?: ExceptionHandler[]; } interface ExceptionHandler { description?: string; name?: string; selector: Record; action: string; action_params?: Record; max_trigger_count?: number; } interface WorkflowStep { description?: string; completed?: string; /** * 循环配置,两种模式二选一: * - count: 固定次数循环(-1 表示无限循环,需外部取消) * - max_count + completed="success": 条件循环(-1 表示无限循环直到成功) * - interval: 循环间隔毫秒数,默认 0 */ loop?: { count: number; interval?: number; } | { max_count: number; interval?: number; }; actions: WorkflowAction[]; } interface WorkflowAction { path: string; params?: Record; throw_if_empty?: string[]; } interface TaskPlanSubtask { id: number; description: string; successCriteria: string; estimatedActions: string[]; } interface TaskPlan { goal: string; subtasks: TaskPlanSubtask[]; risks: string[]; assumptions: string[]; estimatedSteps: number; } interface ToolRetryPolicy { observation: number; action: number; } interface ToolTimeoutPolicy { defaultMs: number; observeScreenMs: number; startAppMs: number; } interface RuntimeLimitPolicy { /** Positive hard cap on dialogue iterations. */ maxIterations: number; maxConsecutiveFailures: number; maxSameToolFingerprint: number; } interface PlannerPolicy { maxAttempts: number; } interface ScriptGenerationPolicy { maxAttempts: number; } interface ReliabilityConfig { retries: ToolRetryPolicy; timeouts: ToolTimeoutPolicy; limits: RuntimeLimitPolicy; planner: PlannerPolicy; scriptGeneration: ScriptGenerationPolicy; } interface AgentRuntimeStartParams { sessionId?: string; goal: string; provider: AIProviderConfig; device: DeviceConnection; /** 默认启用任务规划;传 false 可关闭 */ enablePlanning?: boolean; /** Runtime v2 可靠性策略 */ reliability?: Partial; } interface AgentRuntimeResumeParams { sessionId: string; message: string; } type AgentPausePhase = 'planning' | 'runtime' | 'recovery' | 'script_generation' | 'policy'; interface AgentThinkingData { sessionId: string; text: string; iteration: number; } interface AgentToolCallData { sessionId: string; toolCall: ToolCall; iteration: number; } interface AgentToolResultData { sessionId: string; result: ToolResult; iteration: number; } interface AgentPausedData { sessionId: string; text: string; iteration: number; phase: AgentPausePhase; } interface AgentDiagnosticData { sessionId: string; phase: 'planning' | 'runtime' | 'recovery' | 'script_generation' | 'policy'; code: string; message: string; iteration?: number; details?: Record; timestamp: number; } interface RuntimeDiagnosticsSummary { planningRetries: number; toolRetries: number; scriptGenerationRetries: number; convergenceTriggers: number; totalToolCalls: number; successfulToolCalls: number; runtimeSuccessRate: number | null; totalActionCalls: number; successfulActionCalls: number; automationSuccessRate: number | null; totalFailures: number; failureByCode: Record; finalFailureCode?: string; } interface AgentCompleteData { sessionId: string; workflow: WorkflowScript; executionLog: ExecutionLogEntry[]; totalIterations: number; diagnostics?: RuntimeDiagnosticsSummary; } interface AgentPlanningData { sessionId: string; status: 'started' | 'completed' | 'paused'; plan?: TaskPlan; text?: string; } interface AgentScriptGeneratingData { sessionId: string; } type AgentErrorCode = 'INVALID_CONFIG' | 'SESSION_NOT_FOUND' | 'SESSION_NOT_PAUSED' | 'AGENT_RUNNING' | 'EMPTY_EXECUTION_LOG' | 'MCP_CONNECT_FAILED' | 'SCRIPT_GENERATION_FAILED' | 'RUNTIME_LOOP_FAILED' | 'UNKNOWN'; interface AgentErrorData { sessionId: string; error: string; code?: AgentErrorCode; details?: Record; } type RuntimeEventName = 'planning' | 'thinking' | 'toolcall' | 'toolresult' | 'diagnostic' | 'paused' | 'scriptgenerating' | 'complete' | 'error'; interface RuntimeEventPayloadMap { planning: AgentPlanningData; thinking: AgentThinkingData; toolcall: AgentToolCallData; toolresult: AgentToolResultData; diagnostic: AgentDiagnosticData; paused: AgentPausedData; scriptgenerating: AgentScriptGeneratingData; complete: AgentCompleteData; error: AgentErrorData; } type RuntimeEventHandler = (data: RuntimeEventPayloadMap[TEvent]) => void; type McpServerConfig = { transport: 'stdio'; command: string; args?: string[]; env?: Record; } | { transport: 'http' | 'streamable_http' | 'sse'; url: string; headers?: Record; }; interface AgentRuntimeOptions { mcpServers?: Record; persistence?: { mode?: 'sqlite' | 'memory'; }; } type AgentSessionStatus = 'running' | 'paused' | 'stopped'; interface SessionQueryOptions { status?: AgentSessionStatus; deviceId?: string; keyword?: string; limit?: number; offset?: number; } interface SessionSummary { sessionId: string; goal: string; status: AgentSessionStatus; providerVendor: string; deviceId: string; totalIterations: number; lastError?: string; lastErrorCode?: string; createTime: number; updateTime: number; } declare class AutomationAgentRuntime { private readonly options; private readonly emitter; private readonly toolRegistry; private session; private provider; private abortController; private sessionStore; private localToolContext; constructor(options?: AgentRuntimeOptions); on(event: TEvent, handler: RuntimeEventHandler): () => void; off(event: TEvent, handler: RuntimeEventHandler): void; start(params: AgentRuntimeStartParams): Promise<{ sessionId: string; }>; resume(params: AgentRuntimeResumeParams): Promise; stop(sessionId: string): void; generateScript(sessionId: string): Promise; listSessions(options?: SessionQueryOptions): SessionSummary[]; countSessions(options?: SessionQueryOptions): number; getSessionSummary(sessionId: string): SessionSummary | null; deleteSession(sessionId: string): boolean; destroy(): Promise; private runPlanningStage; private applyPlanningDiagnostics; private runLoop; private updateRuntimeStateAfterTool; private getVerifyGateError; private markSessionPaused; private persistSession; private loadSessionIfNeeded; } declare function createAutomationAgentRuntime(options?: AgentRuntimeOptions): AutomationAgentRuntime; declare const DEFAULT_RELIABILITY_CONFIG: ReliabilityConfig; declare class AgentRuntimeError extends Error { readonly code: AgentErrorCode; readonly details?: Record; constructor(code: AgentErrorCode, message: string, details?: Record); } declare function toRuntimeError(error: unknown, fallbackCode?: AgentErrorCode): AgentRuntimeError; declare function optimizeIntent(input: string, providerConfig: AgentRuntimeStartParams['provider']): Promise; export { AgentRuntimeError, AutomationAgentRuntime, DEFAULT_RELIABILITY_CONFIG, createAutomationAgentRuntime, optimizeIntent, toRuntimeError };