@vmosedge/workflow-agent-sdk 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +288 -0
- package/dist/chunks/scriptGenerator-0SXH7-UK.cjs +1 -0
- package/dist/chunks/scriptGenerator-D5dNotBm.js +1 -0
- package/dist/index.cjs +1 -0
- package/dist/index.d.ts +143 -0
- package/dist/index.js +1 -0
- package/dist/runtime/index.cjs +1 -0
- package/dist/runtime/index.d.ts +294 -0
- package/dist/runtime/index.js +1 -0
- package/dist/types/index.cjs +1 -0
- package/dist/types/index.d.ts +346 -0
- package/dist/types/index.js +1 -0
- package/package.json +81 -0
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
import { BaseChatModel } from '@langchain/core/language_models/chat_models';
|
|
2
|
+
import { ZodTypeAny } from 'zod';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 工具执行器
|
|
6
|
+
*
|
|
7
|
+
* 将 Agent 的工具调用映射为 HTTP 请求,发送到设备的 Control API。
|
|
8
|
+
* 每个工具名对应一个 API 路径和参数转换逻辑。
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/** dump_compact 解析后的结构化节点 */
|
|
12
|
+
interface DumpNode {
|
|
13
|
+
index: number;
|
|
14
|
+
className: string;
|
|
15
|
+
resourceId?: string;
|
|
16
|
+
text?: string;
|
|
17
|
+
contentDesc?: string;
|
|
18
|
+
packageName?: string;
|
|
19
|
+
bounds?: string;
|
|
20
|
+
clickable: boolean;
|
|
21
|
+
longClickable: boolean;
|
|
22
|
+
scrollable: boolean;
|
|
23
|
+
focusable: boolean;
|
|
24
|
+
enabled: boolean;
|
|
25
|
+
checked: boolean;
|
|
26
|
+
selected: boolean;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
type AIVendor = 'deepseek' | 'openai' | 'anthropic' | 'claude' | 'google' | 'gemini' | 'azure' | 'custom';
|
|
30
|
+
interface AIProviderConfig {
|
|
31
|
+
vendor: AIVendor;
|
|
32
|
+
baseUrl?: string;
|
|
33
|
+
apiKey: string;
|
|
34
|
+
model: string;
|
|
35
|
+
headers?: Record<string, string>;
|
|
36
|
+
}
|
|
37
|
+
interface DeviceConnection {
|
|
38
|
+
hostIp: string;
|
|
39
|
+
deviceId: string;
|
|
40
|
+
}
|
|
41
|
+
type ToolCategory = 'observation' | 'action';
|
|
42
|
+
interface ToolDefinition {
|
|
43
|
+
name: string;
|
|
44
|
+
description: string;
|
|
45
|
+
schema: ZodTypeAny;
|
|
46
|
+
parameters: Record<string, unknown>;
|
|
47
|
+
category: ToolCategory;
|
|
48
|
+
}
|
|
49
|
+
interface ToolCall {
|
|
50
|
+
id: string;
|
|
51
|
+
name: string;
|
|
52
|
+
arguments: Record<string, unknown>;
|
|
53
|
+
source?: 'local' | 'mcp';
|
|
54
|
+
server?: string;
|
|
55
|
+
}
|
|
56
|
+
type ToolErrorCode = 'TIMEOUT' | 'NETWORK' | 'HTTP_4XX' | 'HTTP_5XX' | 'VALIDATION' | 'UNKNOWN_TOOL' | 'ABORTED' | 'RUNTIME_GUARD' | 'MODEL_OUTPUT_INVALID' | 'PLAN_VALIDATION_FAILED' | 'SCRIPT_VALIDATION_FAILED' | 'UNKNOWN';
|
|
57
|
+
interface ToolResult {
|
|
58
|
+
toolCallId: string;
|
|
59
|
+
name: string;
|
|
60
|
+
success: boolean;
|
|
61
|
+
data?: unknown;
|
|
62
|
+
error?: string;
|
|
63
|
+
warning?: string;
|
|
64
|
+
errorCode?: ToolErrorCode;
|
|
65
|
+
retryable?: boolean;
|
|
66
|
+
attempt?: number;
|
|
67
|
+
latencyMs?: number;
|
|
68
|
+
source?: 'local' | 'mcp';
|
|
69
|
+
server?: string;
|
|
70
|
+
}
|
|
71
|
+
interface ExecutionLogEntry {
|
|
72
|
+
index: number;
|
|
73
|
+
toolName: string;
|
|
74
|
+
arguments: Record<string, unknown>;
|
|
75
|
+
result: ToolResult;
|
|
76
|
+
category: ToolCategory;
|
|
77
|
+
timestamp: number;
|
|
78
|
+
}
|
|
79
|
+
interface WorkflowScript {
|
|
80
|
+
id: string;
|
|
81
|
+
name: string;
|
|
82
|
+
version: string;
|
|
83
|
+
steps: Record<string, WorkflowStep>;
|
|
84
|
+
flow: string[];
|
|
85
|
+
description?: string;
|
|
86
|
+
timeout?: number;
|
|
87
|
+
exception_handlers?: ExceptionHandler[];
|
|
88
|
+
}
|
|
89
|
+
interface ExceptionHandler {
|
|
90
|
+
description?: string;
|
|
91
|
+
name?: string;
|
|
92
|
+
selector: Record<string, unknown>;
|
|
93
|
+
action: string;
|
|
94
|
+
action_params?: Record<string, unknown>;
|
|
95
|
+
max_trigger_count?: number;
|
|
96
|
+
}
|
|
97
|
+
interface WorkflowStep {
|
|
98
|
+
description?: string;
|
|
99
|
+
completed?: string;
|
|
100
|
+
loop?: {
|
|
101
|
+
count: number;
|
|
102
|
+
interval?: number;
|
|
103
|
+
} | {
|
|
104
|
+
max_count: number;
|
|
105
|
+
interval?: number;
|
|
106
|
+
};
|
|
107
|
+
actions: WorkflowAction[];
|
|
108
|
+
}
|
|
109
|
+
interface WorkflowAction {
|
|
110
|
+
path: string;
|
|
111
|
+
params?: Record<string, unknown>;
|
|
112
|
+
throw_if_empty?: string[];
|
|
113
|
+
}
|
|
114
|
+
type MessageRole = 'user' | 'assistant' | 'system' | 'tool';
|
|
115
|
+
interface ChatMessage {
|
|
116
|
+
role: MessageRole;
|
|
117
|
+
content: string;
|
|
118
|
+
toolCalls?: ToolCall[];
|
|
119
|
+
toolCallId?: string;
|
|
120
|
+
timestamp?: number;
|
|
121
|
+
}
|
|
122
|
+
interface TaskPlanSubtask {
|
|
123
|
+
id: number;
|
|
124
|
+
description: string;
|
|
125
|
+
successCriteria: string;
|
|
126
|
+
estimatedActions: string[];
|
|
127
|
+
}
|
|
128
|
+
interface TaskPlan {
|
|
129
|
+
goal: string;
|
|
130
|
+
subtasks: TaskPlanSubtask[];
|
|
131
|
+
risks: string[];
|
|
132
|
+
assumptions: string[];
|
|
133
|
+
estimatedSteps: number;
|
|
134
|
+
}
|
|
135
|
+
interface ToolRetryPolicy {
|
|
136
|
+
observation: number;
|
|
137
|
+
action: number;
|
|
138
|
+
}
|
|
139
|
+
interface ToolTimeoutPolicy {
|
|
140
|
+
defaultMs: number;
|
|
141
|
+
observeScreenMs: number;
|
|
142
|
+
startAppMs: number;
|
|
143
|
+
}
|
|
144
|
+
interface RuntimeLimitPolicy {
|
|
145
|
+
/** Positive hard cap on dialogue iterations. */
|
|
146
|
+
maxIterations: number;
|
|
147
|
+
maxConsecutiveFailures: number;
|
|
148
|
+
maxSameToolFingerprint: number;
|
|
149
|
+
}
|
|
150
|
+
interface PlannerPolicy {
|
|
151
|
+
maxAttempts: number;
|
|
152
|
+
}
|
|
153
|
+
interface ScriptGenerationPolicy {
|
|
154
|
+
maxAttempts: number;
|
|
155
|
+
}
|
|
156
|
+
interface ReliabilityConfig {
|
|
157
|
+
retries: ToolRetryPolicy;
|
|
158
|
+
timeouts: ToolTimeoutPolicy;
|
|
159
|
+
limits: RuntimeLimitPolicy;
|
|
160
|
+
planner: PlannerPolicy;
|
|
161
|
+
scriptGeneration: ScriptGenerationPolicy;
|
|
162
|
+
}
|
|
163
|
+
interface AgentRuntimeStartParams {
|
|
164
|
+
sessionId?: string;
|
|
165
|
+
goal: string;
|
|
166
|
+
provider: AIProviderConfig;
|
|
167
|
+
device: DeviceConnection;
|
|
168
|
+
/** 默认启用任务规划;传 false 可关闭 */
|
|
169
|
+
enablePlanning?: boolean;
|
|
170
|
+
/** Runtime v2 可靠性策略 */
|
|
171
|
+
reliability?: Partial<ReliabilityConfig>;
|
|
172
|
+
}
|
|
173
|
+
interface AgentRuntimeResumeParams {
|
|
174
|
+
sessionId: string;
|
|
175
|
+
message: string;
|
|
176
|
+
}
|
|
177
|
+
interface AgentThinkingData {
|
|
178
|
+
sessionId: string;
|
|
179
|
+
text: string;
|
|
180
|
+
iteration: number;
|
|
181
|
+
}
|
|
182
|
+
interface AgentToolCallData {
|
|
183
|
+
sessionId: string;
|
|
184
|
+
toolCall: ToolCall;
|
|
185
|
+
iteration: number;
|
|
186
|
+
}
|
|
187
|
+
interface AgentToolResultData {
|
|
188
|
+
sessionId: string;
|
|
189
|
+
result: ToolResult;
|
|
190
|
+
iteration: number;
|
|
191
|
+
}
|
|
192
|
+
interface AgentPausedData {
|
|
193
|
+
sessionId: string;
|
|
194
|
+
text: string;
|
|
195
|
+
iteration: number;
|
|
196
|
+
}
|
|
197
|
+
interface AgentDiagnosticData {
|
|
198
|
+
sessionId: string;
|
|
199
|
+
phase: 'planning' | 'runtime' | 'recovery' | 'script_generation' | 'policy';
|
|
200
|
+
code: string;
|
|
201
|
+
message: string;
|
|
202
|
+
iteration?: number;
|
|
203
|
+
details?: Record<string, unknown>;
|
|
204
|
+
timestamp: number;
|
|
205
|
+
}
|
|
206
|
+
interface RuntimeDiagnosticsSummary {
|
|
207
|
+
planningRetries: number;
|
|
208
|
+
toolRetries: number;
|
|
209
|
+
scriptGenerationRetries: number;
|
|
210
|
+
convergenceTriggers: number;
|
|
211
|
+
totalToolCalls: number;
|
|
212
|
+
successfulToolCalls: number;
|
|
213
|
+
runtimeSuccessRate: number | null;
|
|
214
|
+
totalActionCalls: number;
|
|
215
|
+
successfulActionCalls: number;
|
|
216
|
+
automationSuccessRate: number | null;
|
|
217
|
+
totalFailures: number;
|
|
218
|
+
failureByCode: Record<string, number>;
|
|
219
|
+
finalFailureCode?: string;
|
|
220
|
+
}
|
|
221
|
+
interface AgentCompleteData {
|
|
222
|
+
sessionId: string;
|
|
223
|
+
workflow: WorkflowScript;
|
|
224
|
+
executionLog: ExecutionLogEntry[];
|
|
225
|
+
totalIterations: number;
|
|
226
|
+
diagnostics?: RuntimeDiagnosticsSummary;
|
|
227
|
+
}
|
|
228
|
+
interface AgentPlanningData {
|
|
229
|
+
sessionId: string;
|
|
230
|
+
status: 'started' | 'completed';
|
|
231
|
+
plan?: TaskPlan;
|
|
232
|
+
}
|
|
233
|
+
interface AgentScriptGeneratingData {
|
|
234
|
+
sessionId: string;
|
|
235
|
+
}
|
|
236
|
+
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';
|
|
237
|
+
interface AgentErrorData {
|
|
238
|
+
sessionId: string;
|
|
239
|
+
error: string;
|
|
240
|
+
code?: AgentErrorCode;
|
|
241
|
+
details?: Record<string, unknown>;
|
|
242
|
+
}
|
|
243
|
+
type RuntimeEventName = 'planning' | 'thinking' | 'toolcall' | 'toolresult' | 'diagnostic' | 'paused' | 'scriptgenerating' | 'complete' | 'error';
|
|
244
|
+
interface RuntimeEventPayloadMap {
|
|
245
|
+
planning: AgentPlanningData;
|
|
246
|
+
thinking: AgentThinkingData;
|
|
247
|
+
toolcall: AgentToolCallData;
|
|
248
|
+
toolresult: AgentToolResultData;
|
|
249
|
+
diagnostic: AgentDiagnosticData;
|
|
250
|
+
paused: AgentPausedData;
|
|
251
|
+
scriptgenerating: AgentScriptGeneratingData;
|
|
252
|
+
complete: AgentCompleteData;
|
|
253
|
+
error: AgentErrorData;
|
|
254
|
+
}
|
|
255
|
+
type RuntimeEventHandler<TEvent extends RuntimeEventName> = (data: RuntimeEventPayloadMap[TEvent]) => void;
|
|
256
|
+
type McpServerConfig = {
|
|
257
|
+
transport: 'stdio';
|
|
258
|
+
command: string;
|
|
259
|
+
args?: string[];
|
|
260
|
+
env?: Record<string, string>;
|
|
261
|
+
} | {
|
|
262
|
+
transport: 'http' | 'streamable_http' | 'sse';
|
|
263
|
+
url: string;
|
|
264
|
+
headers?: Record<string, string>;
|
|
265
|
+
};
|
|
266
|
+
interface AgentRuntimeOptions {
|
|
267
|
+
mcpServers?: Record<string, McpServerConfig>;
|
|
268
|
+
persistence?: {
|
|
269
|
+
mode?: 'sqlite' | 'memory';
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
interface RuntimeContextState {
|
|
273
|
+
lastObserveRawDump: string | null;
|
|
274
|
+
lastObserveNodes: DumpNode[];
|
|
275
|
+
pendingVerifyAction: {
|
|
276
|
+
index: number;
|
|
277
|
+
toolName: string;
|
|
278
|
+
} | null;
|
|
279
|
+
/** 软提醒:所有 action 工具(除 wait)成功后设置,verify_ui_state 成功后清除 */
|
|
280
|
+
softPendingVerify: {
|
|
281
|
+
index: number;
|
|
282
|
+
toolName: string;
|
|
283
|
+
} | null;
|
|
284
|
+
/** record_search_context 最后成功执行的时间戳 */
|
|
285
|
+
lastSearchContextTimestamp: number | null;
|
|
286
|
+
consecutiveFailures: number;
|
|
287
|
+
sameToolFingerprintCount: number;
|
|
288
|
+
lastToolFingerprint: string | null;
|
|
289
|
+
planningRetries: number;
|
|
290
|
+
toolRetries: number;
|
|
291
|
+
scriptGenerationRetries: number;
|
|
292
|
+
convergenceTriggers: number;
|
|
293
|
+
}
|
|
294
|
+
interface AgentSessionState {
|
|
295
|
+
sessionId: string;
|
|
296
|
+
goal: string;
|
|
297
|
+
provider: AIProviderConfig;
|
|
298
|
+
device: DeviceConnection;
|
|
299
|
+
reliability: ReliabilityConfig;
|
|
300
|
+
iteration: number;
|
|
301
|
+
running: boolean;
|
|
302
|
+
paused: boolean;
|
|
303
|
+
messages: ChatMessage[];
|
|
304
|
+
executionLog: ExecutionLogEntry[];
|
|
305
|
+
diagnostics: AgentDiagnosticData[];
|
|
306
|
+
runtimeContext: RuntimeContextState;
|
|
307
|
+
plan?: TaskPlan;
|
|
308
|
+
}
|
|
309
|
+
type AgentSessionStatus = 'running' | 'paused' | 'stopped';
|
|
310
|
+
interface SessionQueryOptions {
|
|
311
|
+
status?: AgentSessionStatus;
|
|
312
|
+
deviceId?: string;
|
|
313
|
+
keyword?: string;
|
|
314
|
+
limit?: number;
|
|
315
|
+
offset?: number;
|
|
316
|
+
}
|
|
317
|
+
interface SessionSummary {
|
|
318
|
+
sessionId: string;
|
|
319
|
+
goal: string;
|
|
320
|
+
status: AgentSessionStatus;
|
|
321
|
+
providerVendor: string;
|
|
322
|
+
deviceId: string;
|
|
323
|
+
totalIterations: number;
|
|
324
|
+
lastError?: string;
|
|
325
|
+
lastErrorCode?: string;
|
|
326
|
+
createTime: number;
|
|
327
|
+
updateTime: number;
|
|
328
|
+
}
|
|
329
|
+
interface ProviderResponse {
|
|
330
|
+
content: string;
|
|
331
|
+
toolCalls: ToolCall[];
|
|
332
|
+
finishReason?: string;
|
|
333
|
+
}
|
|
334
|
+
interface ProviderCapabilities {
|
|
335
|
+
toolCalling: boolean;
|
|
336
|
+
structuredJson: boolean;
|
|
337
|
+
jsonMode: boolean;
|
|
338
|
+
}
|
|
339
|
+
interface IModelProvider {
|
|
340
|
+
readonly capabilities: ProviderCapabilities;
|
|
341
|
+
getModel(): BaseChatModel;
|
|
342
|
+
chatWithTools(messages: ChatMessage[], tools: Array<ToolDefinition | unknown>, signal?: AbortSignal): Promise<ProviderResponse>;
|
|
343
|
+
chatStructuredJson<T extends Record<string, unknown>>(messages: ChatMessage[], schema: ZodTypeAny, signal?: AbortSignal): Promise<T>;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
export type { AIProviderConfig, AIVendor, AgentCompleteData, AgentDiagnosticData, AgentErrorCode, AgentErrorData, AgentPausedData, AgentPlanningData, AgentRuntimeOptions, AgentRuntimeResumeParams, AgentRuntimeStartParams, AgentScriptGeneratingData, AgentSessionState, AgentSessionStatus, AgentThinkingData, AgentToolCallData, AgentToolResultData, ChatMessage, DeviceConnection, ExceptionHandler, ExecutionLogEntry, IModelProvider, McpServerConfig, MessageRole, PlannerPolicy, ProviderCapabilities, ProviderResponse, ReliabilityConfig, RuntimeContextState, RuntimeDiagnosticsSummary, RuntimeEventHandler, RuntimeEventName, RuntimeEventPayloadMap, RuntimeLimitPolicy, ScriptGenerationPolicy, SessionQueryOptions, SessionSummary, TaskPlan, TaskPlanSubtask, ToolCall, ToolCategory, ToolDefinition, ToolErrorCode, ToolResult, ToolRetryPolicy, ToolTimeoutPolicy, WorkflowAction, WorkflowScript, WorkflowStep };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
package/package.json
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vmosedge/workflow-agent-sdk",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "VMOS Edge 自动化 SDK,提供任务规划、设备执行与工作流脚本生成能力",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/index.cjs",
|
|
8
|
+
"module": "./dist/index.js",
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"sideEffects": false,
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"import": "./dist/index.js",
|
|
15
|
+
"require": "./dist/index.cjs",
|
|
16
|
+
"default": "./dist/index.js"
|
|
17
|
+
},
|
|
18
|
+
"./runtime": {
|
|
19
|
+
"types": "./dist/runtime/index.d.ts",
|
|
20
|
+
"import": "./dist/runtime/index.js",
|
|
21
|
+
"require": "./dist/runtime/index.cjs",
|
|
22
|
+
"default": "./dist/runtime/index.js"
|
|
23
|
+
},
|
|
24
|
+
"./types": {
|
|
25
|
+
"types": "./dist/types/index.d.ts",
|
|
26
|
+
"import": "./dist/types/index.js",
|
|
27
|
+
"require": "./dist/types/index.cjs",
|
|
28
|
+
"default": "./dist/types/index.js"
|
|
29
|
+
},
|
|
30
|
+
"./package.json": "./package.json"
|
|
31
|
+
},
|
|
32
|
+
"files": [
|
|
33
|
+
"dist"
|
|
34
|
+
],
|
|
35
|
+
"publishConfig": {
|
|
36
|
+
"access": "public"
|
|
37
|
+
},
|
|
38
|
+
"engines": {
|
|
39
|
+
"node": ">=20"
|
|
40
|
+
},
|
|
41
|
+
"keywords": [
|
|
42
|
+
"vmos",
|
|
43
|
+
"vmos-edge",
|
|
44
|
+
"automation",
|
|
45
|
+
"android",
|
|
46
|
+
"agent",
|
|
47
|
+
"mcp",
|
|
48
|
+
"langchain",
|
|
49
|
+
"workflow"
|
|
50
|
+
],
|
|
51
|
+
"scripts": {
|
|
52
|
+
"clean": "rm -rf dist .rollup-tmp",
|
|
53
|
+
"build:js": "tsc -p tsconfig.rollup.json",
|
|
54
|
+
"build:bundle": "rollup -c rollup.config.mjs",
|
|
55
|
+
"build:types": "rollup -c rollup.config.types.mjs",
|
|
56
|
+
"build": "pnpm run clean && pnpm run build:js && pnpm run build:bundle && pnpm run build:types && rm -rf .rollup-tmp",
|
|
57
|
+
"prepublishOnly": "pnpm run build && npm pack --dry-run",
|
|
58
|
+
"typecheck": "tsc --noEmit -p tsconfig.json",
|
|
59
|
+
"test": "vitest run"
|
|
60
|
+
},
|
|
61
|
+
"dependencies": {
|
|
62
|
+
"@langchain/anthropic": "1.3.21",
|
|
63
|
+
"@langchain/core": "^1.1.29",
|
|
64
|
+
"@langchain/google-genai": "2.1.23",
|
|
65
|
+
"@langchain/mcp-adapters": "^1.1.3",
|
|
66
|
+
"@langchain/openai": "^1.2.10",
|
|
67
|
+
"better-sqlite3": "^12.5.0",
|
|
68
|
+
"uuid": "^13.0.0",
|
|
69
|
+
"zod": "^3.25.76",
|
|
70
|
+
"zod-to-json-schema": "^3.24.6"
|
|
71
|
+
},
|
|
72
|
+
"devDependencies": {
|
|
73
|
+
"@rollup/plugin-terser": "^1.0.0",
|
|
74
|
+
"@types/better-sqlite3": "^7.6.13",
|
|
75
|
+
"@types/node": "^22.13.11",
|
|
76
|
+
"rollup": "^4.59.0",
|
|
77
|
+
"rollup-plugin-dts": "^6.3.0",
|
|
78
|
+
"typescript": "^5.9.2",
|
|
79
|
+
"vitest": "^3.2.4"
|
|
80
|
+
}
|
|
81
|
+
}
|