import { BaseChatModel } from '@langchain/core/language_models/chat_models'; import { ZodTypeAny } from 'zod'; /** * 工具执行器 * * 将 Agent 的工具调用映射为 HTTP 请求,发送到设备的 Control API。 * 每个工具名对应一个 API 路径和参数转换逻辑。 */ /** dump_compact 解析后的结构化节点 */ interface DumpNode { index: number; className: string; resourceId?: string; text?: string; contentDesc?: string; packageName?: string; bounds?: string; clickable: boolean; longClickable: boolean; scrollable: boolean; focusable: boolean; enabled: boolean; checked: boolean; selected: boolean; } 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 ToolDefinition { name: string; description: string; schema: ZodTypeAny; parameters: Record; category: ToolCategory; } 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[]; } type MessageRole = 'user' | 'assistant' | 'system' | 'tool'; interface ChatMessage { role: MessageRole; content: string; toolCalls?: ToolCall[]; toolCallId?: string; timestamp?: number; } 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'; type AgentSessionPhase = 'planning' | 'runtime' | 'script_generation'; 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'; }; } interface RuntimeContextState { lastObserveRawDump: string | null; lastObserveNodes: DumpNode[]; pendingVerifyAction: { index: number; toolName: string; } | null; /** 软提醒:所有 action 工具(除 wait)成功后设置,verify_ui_state 成功后清除 */ softPendingVerify: { index: number; toolName: string; } | null; /** record_search_context 最后成功执行的时间戳 */ lastSearchContextTimestamp: number | null; consecutiveFailures: number; sameToolFingerprintCount: number; lastToolFingerprint: string | null; planningRetries: number; toolRetries: number; scriptGenerationRetries: number; convergenceTriggers: number; } interface PlanningContextState { messages: ChatMessage[]; } interface AgentSessionState { sessionId: string; goal: string; provider: AIProviderConfig; device: DeviceConnection; reliability: ReliabilityConfig; iteration: number; running: boolean; paused: boolean; messages: ChatMessage[]; executionLog: ExecutionLogEntry[]; diagnostics: AgentDiagnosticData[]; runtimeContext: RuntimeContextState; plan?: TaskPlan; currentPhase?: AgentSessionPhase; planningContext?: PlanningContextState; } 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; } interface ProviderResponse { content: string; toolCalls: ToolCall[]; finishReason?: string; } interface ProviderCapabilities { toolCalling: boolean; structuredJson: boolean; jsonMode: boolean; } interface IModelProvider { readonly capabilities: ProviderCapabilities; getModel(): BaseChatModel; chatWithTools(messages: ChatMessage[], tools: Array, signal?: AbortSignal): Promise; chatStructuredJson>(messages: ChatMessage[], schema: ZodTypeAny, signal?: AbortSignal): Promise; } export type { AIProviderConfig, AIVendor, AgentCompleteData, AgentDiagnosticData, AgentErrorCode, AgentErrorData, AgentPausePhase, AgentPausedData, AgentPlanningData, AgentRuntimeOptions, AgentRuntimeResumeParams, AgentRuntimeStartParams, AgentScriptGeneratingData, AgentSessionPhase, AgentSessionState, AgentSessionStatus, AgentThinkingData, AgentToolCallData, AgentToolResultData, ChatMessage, DeviceConnection, ExceptionHandler, ExecutionLogEntry, IModelProvider, McpServerConfig, MessageRole, PlannerPolicy, PlanningContextState, ProviderCapabilities, ProviderResponse, ReliabilityConfig, RuntimeContextState, RuntimeDiagnosticsSummary, RuntimeEventHandler, RuntimeEventName, RuntimeEventPayloadMap, RuntimeLimitPolicy, ScriptGenerationPolicy, SessionQueryOptions, SessionSummary, TaskPlan, TaskPlanSubtask, ToolCall, ToolCategory, ToolDefinition, ToolErrorCode, ToolResult, ToolRetryPolicy, ToolTimeoutPolicy, WorkflowAction, WorkflowScript, WorkflowStep };