@midscene/core 0.27.0 → 0.27.1-beta-20250822094725.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/dist/es/agent/agent.mjs +518 -0
- package/dist/es/agent/agent.mjs.map +1 -0
- package/dist/es/agent/index.mjs +6 -0
- package/dist/es/agent/task-cache.mjs +149 -0
- package/dist/es/agent/task-cache.mjs.map +1 -0
- package/dist/es/agent/tasks.mjs +767 -0
- package/dist/es/agent/tasks.mjs.map +1 -0
- package/dist/es/agent/ui-utils.mjs +89 -0
- package/dist/es/agent/ui-utils.mjs.map +1 -0
- package/dist/es/agent/utils.mjs +337 -0
- package/dist/es/agent/utils.mjs.map +1 -0
- package/dist/es/device/index.mjs +20 -0
- package/dist/es/device/index.mjs.map +1 -0
- package/dist/es/index.mjs +2 -1
- package/dist/es/index.mjs.map +1 -1
- package/dist/es/types.mjs.map +1 -1
- package/dist/es/utils.mjs +2 -2
- package/dist/es/yaml/builder.mjs +13 -0
- package/dist/es/yaml/builder.mjs.map +1 -0
- package/dist/es/yaml/index.mjs +3 -0
- package/dist/es/yaml/player.mjs +375 -0
- package/dist/es/yaml/player.mjs.map +1 -0
- package/dist/es/yaml/utils.mjs +39 -0
- package/dist/es/yaml/utils.mjs.map +1 -0
- package/dist/lib/agent/agent.js +562 -0
- package/dist/lib/agent/agent.js.map +1 -0
- package/dist/lib/agent/index.js +90 -0
- package/dist/lib/agent/index.js.map +1 -0
- package/dist/lib/agent/task-cache.js +201 -0
- package/dist/lib/agent/task-cache.js.map +1 -0
- package/dist/lib/agent/tasks.js +804 -0
- package/dist/lib/agent/tasks.js.map +1 -0
- package/dist/lib/agent/ui-utils.js +141 -0
- package/dist/lib/agent/ui-utils.js.map +1 -0
- package/dist/lib/agent/utils.js +417 -0
- package/dist/lib/agent/utils.js.map +1 -0
- package/dist/lib/device/index.js +54 -0
- package/dist/lib/device/index.js.map +1 -0
- package/dist/lib/index.js +5 -1
- package/dist/lib/index.js.map +1 -1
- package/dist/lib/types.js.map +1 -1
- package/dist/lib/utils.js +2 -2
- package/dist/lib/yaml/builder.js +57 -0
- package/dist/lib/yaml/builder.js.map +1 -0
- package/dist/lib/yaml/index.js +80 -0
- package/dist/lib/yaml/index.js.map +1 -0
- package/dist/lib/yaml/player.js +409 -0
- package/dist/lib/yaml/player.js.map +1 -0
- package/dist/lib/yaml/utils.js +86 -0
- package/dist/lib/yaml/utils.js.map +1 -0
- package/dist/types/agent/agent.d.ts +123 -0
- package/dist/types/agent/index.d.ts +9 -0
- package/dist/types/agent/task-cache.d.ts +38 -0
- package/dist/types/agent/tasks.d.ts +56 -0
- package/dist/types/agent/ui-utils.d.ts +11 -0
- package/dist/types/agent/utils.d.ts +54 -0
- package/dist/types/device/index.d.ts +55 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/types.d.ts +2 -0
- package/dist/types/yaml/builder.d.ts +2 -0
- package/dist/types/yaml/index.d.ts +3 -0
- package/dist/types/yaml/player.d.ts +34 -0
- package/dist/types/yaml/utils.d.ts +3 -0
- package/package.json +31 -3
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"yaml/builder.mjs","sources":["webpack://@midscene/core/./src/yaml/builder.ts"],"sourcesContent":["import yaml from 'js-yaml';\nimport type {\n MidsceneYamlScript,\n MidsceneYamlScriptWebEnv,\n MidsceneYamlTask,\n} from '../types';\n\nexport function buildYaml(\n env: MidsceneYamlScriptWebEnv,\n tasks: MidsceneYamlTask[],\n) {\n const result: MidsceneYamlScript = {\n target: env,\n tasks,\n };\n\n return yaml.dump(result, {\n indent: 2,\n });\n}\n"],"names":["buildYaml","env","tasks","result","yaml"],"mappings":";AAOO,SAASA,UACdC,GAA6B,EAC7BC,KAAyB;IAEzB,MAAMC,SAA6B;QACjC,QAAQF;QACRC;IACF;IAEA,OAAOE,QAAAA,IAAS,CAACD,QAAQ;QACvB,QAAQ;IACV;AACF"}
|
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { basename, dirname, join, resolve as external_node_path_resolve } from "node:path";
|
|
3
|
+
import { assert, ifInBrowser, ifInWorker } from "@midscene/shared/utils";
|
|
4
|
+
import { buildDetailedLocateParamAndRestParams } from "../agent/utils.mjs";
|
|
5
|
+
import { getMidsceneRunSubDir } from "@midscene/shared/common";
|
|
6
|
+
import { getDebug } from "@midscene/shared/logger";
|
|
7
|
+
function _define_property(obj, key, value) {
|
|
8
|
+
if (key in obj) Object.defineProperty(obj, key, {
|
|
9
|
+
value: value,
|
|
10
|
+
enumerable: true,
|
|
11
|
+
configurable: true,
|
|
12
|
+
writable: true
|
|
13
|
+
});
|
|
14
|
+
else obj[key] = value;
|
|
15
|
+
return obj;
|
|
16
|
+
}
|
|
17
|
+
const debug = getDebug('yaml-player');
|
|
18
|
+
class ScriptPlayer {
|
|
19
|
+
setResult(key, value) {
|
|
20
|
+
const keyToUse = key || this.unnamedResultIndex++;
|
|
21
|
+
if (this.result[keyToUse]) console.warn(`result key ${keyToUse} already exists, will overwrite`);
|
|
22
|
+
this.result[keyToUse] = value;
|
|
23
|
+
return this.flushResult();
|
|
24
|
+
}
|
|
25
|
+
setPlayerStatus(status, error) {
|
|
26
|
+
this.status = status;
|
|
27
|
+
this.errorInSetup = error;
|
|
28
|
+
}
|
|
29
|
+
notifyCurrentTaskStatusChange(taskIndex) {
|
|
30
|
+
const taskIndexToNotify = 'number' == typeof taskIndex ? taskIndex : this.currentTaskIndex;
|
|
31
|
+
if ('number' != typeof taskIndexToNotify) return;
|
|
32
|
+
const taskStatus = this.taskStatusList[taskIndexToNotify];
|
|
33
|
+
if (this.onTaskStatusChange) this.onTaskStatusChange(taskStatus);
|
|
34
|
+
}
|
|
35
|
+
async setTaskStatus(index, statusValue, error) {
|
|
36
|
+
this.taskStatusList[index].status = statusValue;
|
|
37
|
+
if (error) this.taskStatusList[index].error = error;
|
|
38
|
+
this.notifyCurrentTaskStatusChange(index);
|
|
39
|
+
}
|
|
40
|
+
setTaskIndex(taskIndex) {
|
|
41
|
+
this.currentTaskIndex = taskIndex;
|
|
42
|
+
}
|
|
43
|
+
flushResult() {
|
|
44
|
+
if (this.output) {
|
|
45
|
+
const output = external_node_path_resolve(process.cwd(), this.output);
|
|
46
|
+
const outputDir = dirname(output);
|
|
47
|
+
if (!existsSync(outputDir)) mkdirSync(outputDir, {
|
|
48
|
+
recursive: true
|
|
49
|
+
});
|
|
50
|
+
writeFileSync(output, JSON.stringify(this.result || {}, void 0, 2));
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
flushUnstableLogContent() {
|
|
54
|
+
if (this.unstableLogContent) {
|
|
55
|
+
var _this_pageAgent;
|
|
56
|
+
const content = null == (_this_pageAgent = this.pageAgent) ? void 0 : _this_pageAgent._unstableLogContent();
|
|
57
|
+
const filePath = external_node_path_resolve(process.cwd(), this.unstableLogContent);
|
|
58
|
+
const outputDir = dirname(filePath);
|
|
59
|
+
if (!existsSync(outputDir)) mkdirSync(outputDir, {
|
|
60
|
+
recursive: true
|
|
61
|
+
});
|
|
62
|
+
writeFileSync(filePath, JSON.stringify(content, null, 2));
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
async playTask(taskStatus, agent) {
|
|
66
|
+
const { flow } = taskStatus;
|
|
67
|
+
assert(flow, 'missing flow in task');
|
|
68
|
+
for(const flowItemIndex in flow){
|
|
69
|
+
const currentStep = Number.parseInt(flowItemIndex, 10);
|
|
70
|
+
taskStatus.currentStep = currentStep;
|
|
71
|
+
const flowItem = flow[flowItemIndex];
|
|
72
|
+
debug(`playing step ${flowItemIndex}, flowItem=${JSON.stringify(flowItem)}`);
|
|
73
|
+
if ('aiAction' in flowItem || 'ai' in flowItem) {
|
|
74
|
+
const actionTask = flowItem;
|
|
75
|
+
const prompt = actionTask.aiAction || actionTask.ai;
|
|
76
|
+
assert(prompt, 'missing prompt for ai (aiAction)');
|
|
77
|
+
assert('string' == typeof prompt, 'prompt for aiAction must be a string');
|
|
78
|
+
await agent.aiAction(prompt, {
|
|
79
|
+
cacheable: actionTask.cacheable
|
|
80
|
+
});
|
|
81
|
+
} else if ('aiAssert' in flowItem) {
|
|
82
|
+
const assertTask = flowItem;
|
|
83
|
+
const prompt = assertTask.aiAssert;
|
|
84
|
+
const msg = assertTask.errorMessage;
|
|
85
|
+
assert(prompt, 'missing prompt for aiAssert');
|
|
86
|
+
assert('string' == typeof prompt, 'prompt for aiAssert must be a string');
|
|
87
|
+
const { pass, thought, message } = await agent.aiAssert(prompt, msg, {
|
|
88
|
+
keepRawResponse: true
|
|
89
|
+
}) || {};
|
|
90
|
+
this.setResult(assertTask.name, {
|
|
91
|
+
pass,
|
|
92
|
+
thought,
|
|
93
|
+
message
|
|
94
|
+
});
|
|
95
|
+
if (!pass) throw new Error(message);
|
|
96
|
+
} else if ('aiQuery' in flowItem) {
|
|
97
|
+
const queryTask = flowItem;
|
|
98
|
+
const prompt = queryTask.aiQuery;
|
|
99
|
+
const options = {
|
|
100
|
+
domIncluded: queryTask.domIncluded,
|
|
101
|
+
screenshotIncluded: queryTask.screenshotIncluded
|
|
102
|
+
};
|
|
103
|
+
assert(prompt, 'missing prompt for aiQuery');
|
|
104
|
+
assert('string' == typeof prompt, 'prompt for aiQuery must be a string');
|
|
105
|
+
const queryResult = await agent.aiQuery(prompt, options);
|
|
106
|
+
this.setResult(queryTask.name, queryResult);
|
|
107
|
+
} else if ('aiNumber' in flowItem) {
|
|
108
|
+
const numberTask = flowItem;
|
|
109
|
+
const prompt = numberTask.aiNumber;
|
|
110
|
+
const options = {
|
|
111
|
+
domIncluded: numberTask.domIncluded,
|
|
112
|
+
screenshotIncluded: numberTask.screenshotIncluded
|
|
113
|
+
};
|
|
114
|
+
assert(prompt, 'missing prompt for aiNumber');
|
|
115
|
+
assert('string' == typeof prompt, 'prompt for number must be a string');
|
|
116
|
+
const numberResult = await agent.aiNumber(prompt, options);
|
|
117
|
+
this.setResult(numberTask.name, numberResult);
|
|
118
|
+
} else if ('aiString' in flowItem) {
|
|
119
|
+
const stringTask = flowItem;
|
|
120
|
+
const prompt = stringTask.aiString;
|
|
121
|
+
const options = {
|
|
122
|
+
domIncluded: stringTask.domIncluded,
|
|
123
|
+
screenshotIncluded: stringTask.screenshotIncluded
|
|
124
|
+
};
|
|
125
|
+
assert(prompt, 'missing prompt for aiString');
|
|
126
|
+
assert('string' == typeof prompt, 'prompt for string must be a string');
|
|
127
|
+
const stringResult = await agent.aiString(prompt, options);
|
|
128
|
+
this.setResult(stringTask.name, stringResult);
|
|
129
|
+
} else if ('aiBoolean' in flowItem) {
|
|
130
|
+
const booleanTask = flowItem;
|
|
131
|
+
const prompt = booleanTask.aiBoolean;
|
|
132
|
+
const options = {
|
|
133
|
+
domIncluded: booleanTask.domIncluded,
|
|
134
|
+
screenshotIncluded: booleanTask.screenshotIncluded
|
|
135
|
+
};
|
|
136
|
+
assert(prompt, 'missing prompt for aiBoolean');
|
|
137
|
+
assert('string' == typeof prompt, 'prompt for boolean must be a string');
|
|
138
|
+
const booleanResult = await agent.aiBoolean(prompt, options);
|
|
139
|
+
this.setResult(booleanTask.name, booleanResult);
|
|
140
|
+
} else if ('aiAsk' in flowItem) {
|
|
141
|
+
const askTask = flowItem;
|
|
142
|
+
const prompt = askTask.aiAsk;
|
|
143
|
+
assert(prompt, 'missing prompt for aiAsk');
|
|
144
|
+
assert('string' == typeof prompt, 'prompt for aiAsk must be a string');
|
|
145
|
+
const askResult = await agent.aiAsk(prompt);
|
|
146
|
+
this.setResult(askTask.name, askResult);
|
|
147
|
+
} else if ('aiLocate' in flowItem) {
|
|
148
|
+
const locateTask = flowItem;
|
|
149
|
+
const prompt = locateTask.aiLocate;
|
|
150
|
+
assert(prompt, 'missing prompt for aiLocate');
|
|
151
|
+
assert('string' == typeof prompt, 'prompt for aiLocate must be a string');
|
|
152
|
+
const locateResult = await agent.aiLocate(prompt, locateTask);
|
|
153
|
+
this.setResult(locateTask.name, locateResult);
|
|
154
|
+
} else if ('aiWaitFor' in flowItem) {
|
|
155
|
+
const waitForTask = flowItem;
|
|
156
|
+
const prompt = waitForTask.aiWaitFor;
|
|
157
|
+
assert(prompt, 'missing prompt for aiWaitFor');
|
|
158
|
+
assert('string' == typeof prompt, 'prompt for aiWaitFor must be a string');
|
|
159
|
+
const timeout = waitForTask.timeout;
|
|
160
|
+
await agent.aiWaitFor(prompt, {
|
|
161
|
+
timeoutMs: timeout
|
|
162
|
+
});
|
|
163
|
+
} else if ('sleep' in flowItem) {
|
|
164
|
+
const sleepTask = flowItem;
|
|
165
|
+
const ms = sleepTask.sleep;
|
|
166
|
+
let msNumber = ms;
|
|
167
|
+
if ('string' == typeof ms) msNumber = Number.parseInt(ms, 10);
|
|
168
|
+
assert(msNumber && msNumber > 0, `ms for sleep must be greater than 0, but got ${ms}`);
|
|
169
|
+
await new Promise((resolve)=>setTimeout(resolve, msNumber));
|
|
170
|
+
} else if ("javascript" in flowItem) {
|
|
171
|
+
const evaluateJavaScriptTask = flowItem;
|
|
172
|
+
const result = await agent.evaluateJavaScript(evaluateJavaScriptTask.javascript);
|
|
173
|
+
this.setResult(evaluateJavaScriptTask.name, result);
|
|
174
|
+
} else if ('logScreenshot' in flowItem) {
|
|
175
|
+
const logScreenshotTask = flowItem;
|
|
176
|
+
await agent.logScreenshot(logScreenshotTask.logScreenshot, {
|
|
177
|
+
content: logScreenshotTask.content || ''
|
|
178
|
+
});
|
|
179
|
+
} else if ('aiInput' in flowItem) {
|
|
180
|
+
const { aiInput, ...inputTask } = flowItem;
|
|
181
|
+
let locatePrompt;
|
|
182
|
+
let value;
|
|
183
|
+
if (inputTask.locate) {
|
|
184
|
+
value = aiInput || inputTask.value;
|
|
185
|
+
locatePrompt = inputTask.locate;
|
|
186
|
+
} else {
|
|
187
|
+
locatePrompt = aiInput || '';
|
|
188
|
+
value = inputTask.value;
|
|
189
|
+
}
|
|
190
|
+
await agent.callActionInActionSpace('Input', {
|
|
191
|
+
...inputTask,
|
|
192
|
+
...void 0 !== value ? {
|
|
193
|
+
value
|
|
194
|
+
} : {},
|
|
195
|
+
...locatePrompt ? {
|
|
196
|
+
locate: locatePrompt
|
|
197
|
+
} : {}
|
|
198
|
+
});
|
|
199
|
+
} else if ('aiKeyboardPress' in flowItem) {
|
|
200
|
+
const { aiKeyboardPress, ...keyboardPressTask } = flowItem;
|
|
201
|
+
let locatePrompt;
|
|
202
|
+
let keyName;
|
|
203
|
+
if (keyboardPressTask.locate) {
|
|
204
|
+
keyName = aiKeyboardPress;
|
|
205
|
+
locatePrompt = keyboardPressTask.locate;
|
|
206
|
+
} else if (keyboardPressTask.keyName) {
|
|
207
|
+
keyName = keyboardPressTask.keyName;
|
|
208
|
+
locatePrompt = aiKeyboardPress;
|
|
209
|
+
}
|
|
210
|
+
await agent.callActionInActionSpace('KeyboardPress', {
|
|
211
|
+
...keyboardPressTask,
|
|
212
|
+
...keyName ? {
|
|
213
|
+
keyName
|
|
214
|
+
} : {},
|
|
215
|
+
...locatePrompt ? {
|
|
216
|
+
locate: locatePrompt
|
|
217
|
+
} : {}
|
|
218
|
+
});
|
|
219
|
+
} else if ('aiScroll' in flowItem) {
|
|
220
|
+
const { aiScroll, ...scrollTask } = flowItem;
|
|
221
|
+
let locatePrompt;
|
|
222
|
+
locatePrompt = scrollTask.locate ? scrollTask.locate : aiScroll;
|
|
223
|
+
await agent.callActionInActionSpace('Scroll', {
|
|
224
|
+
...scrollTask,
|
|
225
|
+
...locatePrompt ? {
|
|
226
|
+
locate: locatePrompt
|
|
227
|
+
} : {}
|
|
228
|
+
});
|
|
229
|
+
} else {
|
|
230
|
+
const actionSpace = this.actionSpace;
|
|
231
|
+
let locatePromptShortcut;
|
|
232
|
+
const matchedAction = actionSpace.find((action)=>{
|
|
233
|
+
const actionInterfaceAlias = action.interfaceAlias;
|
|
234
|
+
if (actionInterfaceAlias && Object.prototype.hasOwnProperty.call(flowItem, actionInterfaceAlias)) {
|
|
235
|
+
locatePromptShortcut = flowItem[actionInterfaceAlias];
|
|
236
|
+
return true;
|
|
237
|
+
}
|
|
238
|
+
const keyOfActionInActionSpace = action.name;
|
|
239
|
+
if (Object.prototype.hasOwnProperty.call(flowItem, keyOfActionInActionSpace)) {
|
|
240
|
+
locatePromptShortcut = flowItem[keyOfActionInActionSpace];
|
|
241
|
+
return true;
|
|
242
|
+
}
|
|
243
|
+
return false;
|
|
244
|
+
});
|
|
245
|
+
assert(matchedAction, `unknown flowItem in yaml: ${JSON.stringify(flowItem)}`);
|
|
246
|
+
assert(!(flowItem.prompt && locatePromptShortcut), `conflict locate prompt for item: ${JSON.stringify(flowItem)}`);
|
|
247
|
+
if (locatePromptShortcut) flowItem.prompt = locatePromptShortcut;
|
|
248
|
+
const { locateParam, restParams } = buildDetailedLocateParamAndRestParams(locatePromptShortcut || '', flowItem, [
|
|
249
|
+
matchedAction.name,
|
|
250
|
+
matchedAction.interfaceAlias || '_never_mind_'
|
|
251
|
+
]);
|
|
252
|
+
const flowParams = {
|
|
253
|
+
...restParams,
|
|
254
|
+
locate: locateParam
|
|
255
|
+
};
|
|
256
|
+
debug(`matchedAction: ${matchedAction.name}`, `flowParams: ${JSON.stringify(flowParams, null, 2)}`);
|
|
257
|
+
await agent.callActionInActionSpace(matchedAction.name, flowParams);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
this.reportFile = agent.reportFile;
|
|
261
|
+
await this.flushUnstableLogContent();
|
|
262
|
+
}
|
|
263
|
+
async run() {
|
|
264
|
+
const { target, web, android, tasks } = this.script;
|
|
265
|
+
const webEnv = web || target;
|
|
266
|
+
const androidEnv = android;
|
|
267
|
+
const platform = webEnv || androidEnv;
|
|
268
|
+
this.setPlayerStatus('running');
|
|
269
|
+
let agent = null;
|
|
270
|
+
let freeFn = [];
|
|
271
|
+
try {
|
|
272
|
+
const { agent: newAgent, freeFn: newFreeFn } = await this.setupAgent(platform);
|
|
273
|
+
this.actionSpace = await newAgent.getActionSpace();
|
|
274
|
+
agent = newAgent;
|
|
275
|
+
const originalOnTaskStartTip = agent.onTaskStartTip;
|
|
276
|
+
agent.onTaskStartTip = (tip)=>{
|
|
277
|
+
if ('running' === this.status) this.agentStatusTip = tip;
|
|
278
|
+
null == originalOnTaskStartTip || originalOnTaskStartTip(tip);
|
|
279
|
+
};
|
|
280
|
+
freeFn = [
|
|
281
|
+
...newFreeFn || [],
|
|
282
|
+
{
|
|
283
|
+
name: 'restore-agent-onTaskStartTip',
|
|
284
|
+
fn: ()=>{
|
|
285
|
+
if (agent) agent.onTaskStartTip = originalOnTaskStartTip;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
];
|
|
289
|
+
} catch (e) {
|
|
290
|
+
this.setPlayerStatus('error', e);
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
this.pageAgent = agent;
|
|
294
|
+
let taskIndex = 0;
|
|
295
|
+
this.setPlayerStatus('running');
|
|
296
|
+
let errorFlag = false;
|
|
297
|
+
while(taskIndex < tasks.length){
|
|
298
|
+
const taskStatus = this.taskStatusList[taskIndex];
|
|
299
|
+
this.setTaskStatus(taskIndex, 'running');
|
|
300
|
+
this.setTaskIndex(taskIndex);
|
|
301
|
+
try {
|
|
302
|
+
await this.playTask(taskStatus, this.pageAgent);
|
|
303
|
+
this.setTaskStatus(taskIndex, 'done');
|
|
304
|
+
} catch (e) {
|
|
305
|
+
this.setTaskStatus(taskIndex, 'error', e);
|
|
306
|
+
if (taskStatus.continueOnError) ;
|
|
307
|
+
else {
|
|
308
|
+
this.reportFile = agent.reportFile;
|
|
309
|
+
errorFlag = true;
|
|
310
|
+
break;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
this.reportFile = null == agent ? void 0 : agent.reportFile;
|
|
314
|
+
taskIndex++;
|
|
315
|
+
}
|
|
316
|
+
if (errorFlag) this.setPlayerStatus('error');
|
|
317
|
+
else this.setPlayerStatus('done');
|
|
318
|
+
this.agentStatusTip = '';
|
|
319
|
+
for (const fn of freeFn)try {
|
|
320
|
+
await fn.fn();
|
|
321
|
+
} catch (e) {}
|
|
322
|
+
}
|
|
323
|
+
constructor(script, setupAgent, onTaskStatusChange, scriptPath){
|
|
324
|
+
var _this_target, _this_target1, _this_target2;
|
|
325
|
+
_define_property(this, "script", void 0);
|
|
326
|
+
_define_property(this, "setupAgent", void 0);
|
|
327
|
+
_define_property(this, "onTaskStatusChange", void 0);
|
|
328
|
+
_define_property(this, "currentTaskIndex", void 0);
|
|
329
|
+
_define_property(this, "taskStatusList", void 0);
|
|
330
|
+
_define_property(this, "status", void 0);
|
|
331
|
+
_define_property(this, "reportFile", void 0);
|
|
332
|
+
_define_property(this, "result", void 0);
|
|
333
|
+
_define_property(this, "unnamedResultIndex", void 0);
|
|
334
|
+
_define_property(this, "output", void 0);
|
|
335
|
+
_define_property(this, "unstableLogContent", void 0);
|
|
336
|
+
_define_property(this, "errorInSetup", void 0);
|
|
337
|
+
_define_property(this, "pageAgent", void 0);
|
|
338
|
+
_define_property(this, "agentStatusTip", void 0);
|
|
339
|
+
_define_property(this, "target", void 0);
|
|
340
|
+
_define_property(this, "actionSpace", void 0);
|
|
341
|
+
_define_property(this, "scriptPath", void 0);
|
|
342
|
+
this.script = script;
|
|
343
|
+
this.setupAgent = setupAgent;
|
|
344
|
+
this.onTaskStatusChange = onTaskStatusChange;
|
|
345
|
+
this.taskStatusList = [];
|
|
346
|
+
this.status = 'init';
|
|
347
|
+
this.unnamedResultIndex = 0;
|
|
348
|
+
this.pageAgent = null;
|
|
349
|
+
this.actionSpace = [];
|
|
350
|
+
this.scriptPath = scriptPath;
|
|
351
|
+
this.result = {};
|
|
352
|
+
this.target = script.target || script.web || script.android;
|
|
353
|
+
if (ifInBrowser || ifInWorker) this.output = void 0;
|
|
354
|
+
else if (null == (_this_target = this.target) ? void 0 : _this_target.output) this.output = external_node_path_resolve(process.cwd(), this.target.output);
|
|
355
|
+
else {
|
|
356
|
+
const scriptName = this.scriptPath ? basename(this.scriptPath, '.yaml').replace(/\.(ya?ml)$/i, '') : "script";
|
|
357
|
+
this.output = join(getMidsceneRunSubDir('output'), `${scriptName}-${Date.now()}.json`);
|
|
358
|
+
}
|
|
359
|
+
if (ifInBrowser || ifInWorker) this.unstableLogContent = void 0;
|
|
360
|
+
else if ('string' == typeof (null == (_this_target1 = this.target) ? void 0 : _this_target1.unstableLogContent)) this.unstableLogContent = external_node_path_resolve(process.cwd(), this.target.unstableLogContent);
|
|
361
|
+
else if ((null == (_this_target2 = this.target) ? void 0 : _this_target2.unstableLogContent) === true) this.unstableLogContent = join(getMidsceneRunSubDir('output'), 'unstableLogContent.json');
|
|
362
|
+
this.taskStatusList = (script.tasks || []).map((task, taskIndex)=>{
|
|
363
|
+
var _task_flow;
|
|
364
|
+
return {
|
|
365
|
+
...task,
|
|
366
|
+
index: taskIndex,
|
|
367
|
+
status: 'init',
|
|
368
|
+
totalSteps: (null == (_task_flow = task.flow) ? void 0 : _task_flow.length) || 0
|
|
369
|
+
};
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
export { ScriptPlayer };
|
|
374
|
+
|
|
375
|
+
//# sourceMappingURL=player.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"yaml/player.mjs","sources":["webpack://@midscene/core/./src/yaml/player.ts"],"sourcesContent":["import { existsSync, mkdirSync, writeFileSync } from 'node:fs';\nimport { basename, dirname, join, resolve } from 'node:path';\nimport { assert, ifInBrowser, ifInWorker } from '@midscene/shared/utils';\n\nimport type { Agent } from '@/agent/agent';\nimport { buildDetailedLocateParamAndRestParams } from '@/agent/utils';\nimport type {\n DeviceAction,\n FreeFn,\n LocateOption,\n MidsceneYamlFlowItemAIAction,\n MidsceneYamlFlowItemAIAsk,\n MidsceneYamlFlowItemAIAssert,\n MidsceneYamlFlowItemAIBoolean,\n MidsceneYamlFlowItemAIInput,\n MidsceneYamlFlowItemAIKeyboardPress,\n MidsceneYamlFlowItemAILocate,\n MidsceneYamlFlowItemAINumber,\n MidsceneYamlFlowItemAIQuery,\n MidsceneYamlFlowItemAIScroll,\n MidsceneYamlFlowItemAIString,\n MidsceneYamlFlowItemAIWaitFor,\n MidsceneYamlFlowItemEvaluateJavaScript,\n MidsceneYamlFlowItemLogScreenshot,\n MidsceneYamlFlowItemSleep,\n MidsceneYamlScript,\n MidsceneYamlScriptEnv,\n ScriptPlayerStatusValue,\n ScriptPlayerTaskStatus,\n TUserPrompt,\n} from '@/index';\nimport { getMidsceneRunSubDir } from '@midscene/shared/common';\nimport { getDebug } from '@midscene/shared/logger';\n\nconst debug = getDebug('yaml-player');\nexport class ScriptPlayer<T extends MidsceneYamlScriptEnv> {\n public currentTaskIndex?: number;\n public taskStatusList: ScriptPlayerTaskStatus[] = [];\n public status: ScriptPlayerStatusValue = 'init';\n public reportFile?: string | null;\n public result: Record<string, any>;\n private unnamedResultIndex = 0;\n public output?: string | null;\n public unstableLogContent?: string | null;\n public errorInSetup?: Error;\n private pageAgent: Agent | null = null;\n public agentStatusTip?: string;\n public target?: MidsceneYamlScriptEnv;\n private actionSpace: DeviceAction[] = [];\n private scriptPath?: string;\n constructor(\n private script: MidsceneYamlScript,\n private setupAgent: (platform: T) => Promise<{\n agent: Agent;\n freeFn: FreeFn[];\n }>,\n public onTaskStatusChange?: (taskStatus: ScriptPlayerTaskStatus) => void,\n scriptPath?: string,\n ) {\n this.scriptPath = scriptPath;\n this.result = {};\n this.target = script.target || script.web || script.android;\n\n if (ifInBrowser || ifInWorker) {\n this.output = undefined;\n } else if (this.target?.output) {\n this.output = resolve(process.cwd(), this.target.output);\n } else {\n const scriptName = this.scriptPath\n ? basename(this.scriptPath, '.yaml').replace(/\\.(ya?ml)$/i, '')\n : 'script';\n this.output = join(\n getMidsceneRunSubDir('output'),\n `${scriptName}-${Date.now()}.json`,\n );\n }\n\n if (ifInBrowser || ifInWorker) {\n this.unstableLogContent = undefined;\n } else if (typeof this.target?.unstableLogContent === 'string') {\n this.unstableLogContent = resolve(\n process.cwd(),\n this.target.unstableLogContent,\n );\n } else if (this.target?.unstableLogContent === true) {\n this.unstableLogContent = join(\n getMidsceneRunSubDir('output'),\n 'unstableLogContent.json',\n );\n }\n\n this.taskStatusList = (script.tasks || []).map((task, taskIndex) => ({\n ...task,\n index: taskIndex,\n status: 'init',\n totalSteps: task.flow?.length || 0,\n }));\n }\n\n private setResult(key: string | undefined, value: any) {\n const keyToUse = key || this.unnamedResultIndex++;\n if (this.result[keyToUse]) {\n console.warn(`result key ${keyToUse} already exists, will overwrite`);\n }\n this.result[keyToUse] = value;\n\n return this.flushResult();\n }\n\n private setPlayerStatus(status: ScriptPlayerStatusValue, error?: Error) {\n this.status = status;\n this.errorInSetup = error;\n }\n\n private notifyCurrentTaskStatusChange(taskIndex?: number) {\n const taskIndexToNotify =\n typeof taskIndex === 'number' ? taskIndex : this.currentTaskIndex;\n\n if (typeof taskIndexToNotify !== 'number') {\n return;\n }\n\n const taskStatus = this.taskStatusList[taskIndexToNotify];\n if (this.onTaskStatusChange) {\n this.onTaskStatusChange(taskStatus);\n }\n }\n\n private async setTaskStatus(\n index: number,\n statusValue: ScriptPlayerStatusValue,\n error?: Error,\n ) {\n this.taskStatusList[index].status = statusValue;\n if (error) {\n this.taskStatusList[index].error = error;\n }\n\n this.notifyCurrentTaskStatusChange(index);\n }\n\n private setTaskIndex(taskIndex: number) {\n this.currentTaskIndex = taskIndex;\n }\n\n private flushResult() {\n if (this.output) {\n const output = resolve(process.cwd(), this.output);\n const outputDir = dirname(output);\n if (!existsSync(outputDir)) {\n mkdirSync(outputDir, { recursive: true });\n }\n writeFileSync(output, JSON.stringify(this.result || {}, undefined, 2));\n }\n }\n\n private flushUnstableLogContent() {\n if (this.unstableLogContent) {\n const content = this.pageAgent?._unstableLogContent();\n const filePath = resolve(process.cwd(), this.unstableLogContent);\n const outputDir = dirname(filePath);\n if (!existsSync(outputDir)) {\n mkdirSync(outputDir, { recursive: true });\n }\n writeFileSync(filePath, JSON.stringify(content, null, 2));\n }\n }\n\n async playTask(taskStatus: ScriptPlayerTaskStatus, agent: Agent) {\n const { flow } = taskStatus;\n assert(flow, 'missing flow in task');\n\n for (const flowItemIndex in flow) {\n const currentStep = Number.parseInt(flowItemIndex, 10);\n taskStatus.currentStep = currentStep;\n const flowItem = flow[flowItemIndex];\n debug(\n `playing step ${flowItemIndex}, flowItem=${JSON.stringify(flowItem)}`,\n );\n if (\n 'aiAction' in (flowItem as MidsceneYamlFlowItemAIAction) ||\n 'ai' in (flowItem as MidsceneYamlFlowItemAIAction)\n ) {\n const actionTask = flowItem as MidsceneYamlFlowItemAIAction;\n const prompt = actionTask.aiAction || actionTask.ai;\n assert(prompt, 'missing prompt for ai (aiAction)');\n assert(\n typeof prompt === 'string',\n 'prompt for aiAction must be a string',\n );\n await agent.aiAction(prompt, {\n cacheable: actionTask.cacheable,\n });\n } else if ('aiAssert' in (flowItem as MidsceneYamlFlowItemAIAssert)) {\n const assertTask = flowItem as MidsceneYamlFlowItemAIAssert;\n const prompt = assertTask.aiAssert;\n const msg = assertTask.errorMessage;\n assert(prompt, 'missing prompt for aiAssert');\n assert(\n typeof prompt === 'string',\n 'prompt for aiAssert must be a string',\n );\n const { pass, thought, message } =\n (await agent.aiAssert(prompt, msg, {\n keepRawResponse: true,\n })) || {};\n\n this.setResult(assertTask.name, {\n pass,\n thought,\n message,\n });\n\n if (!pass) {\n throw new Error(message);\n }\n } else if ('aiQuery' in (flowItem as MidsceneYamlFlowItemAIQuery)) {\n const queryTask = flowItem as MidsceneYamlFlowItemAIQuery;\n const prompt = queryTask.aiQuery;\n const options = {\n domIncluded: queryTask.domIncluded,\n screenshotIncluded: queryTask.screenshotIncluded,\n };\n assert(prompt, 'missing prompt for aiQuery');\n assert(\n typeof prompt === 'string',\n 'prompt for aiQuery must be a string',\n );\n const queryResult = await agent.aiQuery(prompt, options);\n this.setResult(queryTask.name, queryResult);\n } else if ('aiNumber' in (flowItem as MidsceneYamlFlowItemAINumber)) {\n const numberTask = flowItem as MidsceneYamlFlowItemAINumber;\n const prompt = numberTask.aiNumber;\n const options = {\n domIncluded: numberTask.domIncluded,\n screenshotIncluded: numberTask.screenshotIncluded,\n };\n assert(prompt, 'missing prompt for aiNumber');\n assert(\n typeof prompt === 'string',\n 'prompt for number must be a string',\n );\n const numberResult = await agent.aiNumber(prompt, options);\n this.setResult(numberTask.name, numberResult);\n } else if ('aiString' in (flowItem as MidsceneYamlFlowItemAIString)) {\n const stringTask = flowItem as MidsceneYamlFlowItemAIString;\n const prompt = stringTask.aiString;\n const options = {\n domIncluded: stringTask.domIncluded,\n screenshotIncluded: stringTask.screenshotIncluded,\n };\n assert(prompt, 'missing prompt for aiString');\n assert(\n typeof prompt === 'string',\n 'prompt for string must be a string',\n );\n const stringResult = await agent.aiString(prompt, options);\n this.setResult(stringTask.name, stringResult);\n } else if ('aiBoolean' in (flowItem as MidsceneYamlFlowItemAIBoolean)) {\n const booleanTask = flowItem as MidsceneYamlFlowItemAIBoolean;\n const prompt = booleanTask.aiBoolean;\n const options = {\n domIncluded: booleanTask.domIncluded,\n screenshotIncluded: booleanTask.screenshotIncluded,\n };\n assert(prompt, 'missing prompt for aiBoolean');\n assert(\n typeof prompt === 'string',\n 'prompt for boolean must be a string',\n );\n const booleanResult = await agent.aiBoolean(prompt, options);\n this.setResult(booleanTask.name, booleanResult);\n } else if ('aiAsk' in (flowItem as MidsceneYamlFlowItemAIAsk)) {\n const askTask = flowItem as MidsceneYamlFlowItemAIAsk;\n const prompt = askTask.aiAsk;\n assert(prompt, 'missing prompt for aiAsk');\n assert(typeof prompt === 'string', 'prompt for aiAsk must be a string');\n const askResult = await agent.aiAsk(prompt);\n this.setResult(askTask.name, askResult);\n } else if ('aiLocate' in (flowItem as MidsceneYamlFlowItemAILocate)) {\n const locateTask = flowItem as MidsceneYamlFlowItemAILocate;\n const prompt = locateTask.aiLocate;\n assert(prompt, 'missing prompt for aiLocate');\n assert(\n typeof prompt === 'string',\n 'prompt for aiLocate must be a string',\n );\n const locateResult = await agent.aiLocate(prompt, locateTask);\n this.setResult(locateTask.name, locateResult);\n } else if ('aiWaitFor' in (flowItem as MidsceneYamlFlowItemAIWaitFor)) {\n const waitForTask = flowItem as MidsceneYamlFlowItemAIWaitFor;\n const prompt = waitForTask.aiWaitFor;\n assert(prompt, 'missing prompt for aiWaitFor');\n assert(\n typeof prompt === 'string',\n 'prompt for aiWaitFor must be a string',\n );\n const timeout = waitForTask.timeout;\n await agent.aiWaitFor(prompt, { timeoutMs: timeout });\n } else if ('sleep' in (flowItem as MidsceneYamlFlowItemSleep)) {\n const sleepTask = flowItem as MidsceneYamlFlowItemSleep;\n const ms = sleepTask.sleep;\n let msNumber = ms;\n if (typeof ms === 'string') {\n msNumber = Number.parseInt(ms, 10);\n }\n assert(\n msNumber && msNumber > 0,\n `ms for sleep must be greater than 0, but got ${ms}`,\n );\n await new Promise((resolve) => setTimeout(resolve, msNumber));\n } else if (\n 'javascript' in (flowItem as MidsceneYamlFlowItemEvaluateJavaScript)\n ) {\n const evaluateJavaScriptTask =\n flowItem as MidsceneYamlFlowItemEvaluateJavaScript;\n\n const result = await agent.evaluateJavaScript(\n evaluateJavaScriptTask.javascript,\n );\n this.setResult(evaluateJavaScriptTask.name, result);\n } else if (\n 'logScreenshot' in (flowItem as MidsceneYamlFlowItemLogScreenshot)\n ) {\n const logScreenshotTask = flowItem as MidsceneYamlFlowItemLogScreenshot;\n await agent.logScreenshot(logScreenshotTask.logScreenshot, {\n content: logScreenshotTask.content || '',\n });\n } else if ('aiInput' in (flowItem as MidsceneYamlFlowItemAIInput)) {\n // may be input empty string ''\n const { aiInput, ...inputTask } =\n flowItem as MidsceneYamlFlowItemAIInput;\n\n // Compatibility with previous version:\n // Old format: { aiInput: string (value), locate: TUserPrompt }\n // New format - 1: { aiInput: TUserPrompt, value: string }\n // New format - 2: { aiInput: undefined, locate: TUserPrompt, value: string }\n let locatePrompt: TUserPrompt | undefined;\n let value: string | undefined;\n if ((inputTask as any).locate) {\n // Old format - aiInput is the value, locate is the prompt\n value = (aiInput as string) || inputTask.value;\n locatePrompt = (inputTask as any).locate;\n } else {\n // New format - aiInput is the prompt, value is the value\n locatePrompt = aiInput || '';\n value = inputTask.value;\n }\n\n await agent.callActionInActionSpace('Input', {\n ...inputTask,\n ...(value !== undefined ? { value } : {}),\n ...(locatePrompt ? { locate: locatePrompt } : {}),\n });\n } else if (\n 'aiKeyboardPress' in (flowItem as MidsceneYamlFlowItemAIKeyboardPress)\n ) {\n const { aiKeyboardPress, ...keyboardPressTask } =\n flowItem as MidsceneYamlFlowItemAIKeyboardPress;\n\n // Compatibility with previous version:\n // Old format: { aiKeyboardPress: string (key), locate?: TUserPrompt }\n // New format - 1: { aiKeyboardPress: TUserPrompt, keyName: string }\n // New format - 2: { aiKeyboardPress: , locate?: TUserPrompt, keyName: string }\n let locatePrompt: TUserPrompt | undefined;\n let keyName: string | undefined;\n if ((keyboardPressTask as any).locate) {\n // Old format - aiKeyboardPress is the key, locate is the prompt\n keyName = aiKeyboardPress as string;\n locatePrompt = (keyboardPressTask as any).locate;\n } else if (keyboardPressTask.keyName) {\n // New format - aiKeyboardPress is the prompt, key is the key\n keyName = keyboardPressTask.keyName;\n locatePrompt = aiKeyboardPress;\n }\n\n await agent.callActionInActionSpace('KeyboardPress', {\n ...keyboardPressTask,\n ...(keyName ? { keyName } : {}),\n ...(locatePrompt ? { locate: locatePrompt } : {}),\n });\n } else if ('aiScroll' in (flowItem as MidsceneYamlFlowItemAIScroll)) {\n const { aiScroll, ...scrollTask } =\n flowItem as MidsceneYamlFlowItemAIScroll;\n\n // Compatibility with previous version:\n // Old format: { aiScroll: null, locate?: TUserPrompt, direction, scrollType, distance? }\n // New format - 1: { aiScroll: TUserPrompt, direction, scrollType, distance? }\n // New format - 2: { aiScroll: undefined, locate: TUserPrompt, direction, scrollType, distance? }\n let locatePrompt: TUserPrompt | undefined;\n if ((scrollTask as any).locate) {\n // Old format - locate is the prompt, aiScroll is null/ignored\n locatePrompt = (scrollTask as any).locate;\n } else {\n // New format - aiScroll is the prompt, or no prompt for global scroll\n locatePrompt = aiScroll;\n }\n\n await agent.callActionInActionSpace('Scroll', {\n ...scrollTask,\n ...(locatePrompt ? { locate: locatePrompt } : {}),\n });\n } else {\n // generic action, find the action in actionSpace\n\n /* for aiTap, aiRightClick, the parameters are a flattened data for the 'locate', these are all valid data\n\n - aiTap: 'search input box'\n - aiTap: 'search input box'\n deepThink: true\n cacheable: false\n - aiTap:\n prompt: 'search input box'\n - aiTap:\n prompt: 'search input box'\n deepThink: true\n cacheable: false\n */\n\n const actionSpace = this.actionSpace;\n let locatePromptShortcut: string | undefined;\n const matchedAction = actionSpace.find((action) => {\n const actionInterfaceAlias = action.interfaceAlias;\n if (\n actionInterfaceAlias &&\n Object.prototype.hasOwnProperty.call(flowItem, actionInterfaceAlias)\n ) {\n locatePromptShortcut = flowItem[\n actionInterfaceAlias as keyof typeof flowItem\n ] as string;\n return true;\n }\n\n const keyOfActionInActionSpace = action.name;\n if (\n Object.prototype.hasOwnProperty.call(\n flowItem,\n keyOfActionInActionSpace,\n )\n ) {\n locatePromptShortcut = flowItem[\n keyOfActionInActionSpace as keyof typeof flowItem\n ] as string;\n return true;\n }\n\n return false;\n });\n\n assert(\n matchedAction,\n `unknown flowItem in yaml: ${JSON.stringify(flowItem)}`,\n );\n\n assert(\n !((flowItem as any).prompt && locatePromptShortcut),\n `conflict locate prompt for item: ${JSON.stringify(flowItem)}`,\n );\n\n if (locatePromptShortcut) {\n (flowItem as any).prompt = locatePromptShortcut;\n }\n\n const { locateParam, restParams } =\n buildDetailedLocateParamAndRestParams(\n locatePromptShortcut || '',\n flowItem as LocateOption,\n [\n matchedAction.name,\n matchedAction.interfaceAlias || '_never_mind_',\n ],\n );\n\n const flowParams = {\n ...restParams,\n locate: locateParam,\n };\n\n debug(\n `matchedAction: ${matchedAction.name}`,\n `flowParams: ${JSON.stringify(flowParams, null, 2)}`,\n );\n await agent.callActionInActionSpace(matchedAction.name, flowParams);\n }\n }\n this.reportFile = agent.reportFile;\n await this.flushUnstableLogContent();\n }\n\n async run() {\n const { target, web, android, tasks } = this.script;\n const webEnv = web || target;\n const androidEnv = android;\n const platform = webEnv || androidEnv;\n\n this.setPlayerStatus('running');\n\n let agent: Agent | null = null;\n let freeFn: FreeFn[] = [];\n try {\n const { agent: newAgent, freeFn: newFreeFn } = await this.setupAgent(\n platform as T,\n );\n this.actionSpace = await newAgent.getActionSpace();\n agent = newAgent;\n const originalOnTaskStartTip = agent.onTaskStartTip;\n agent.onTaskStartTip = (tip) => {\n if (this.status === 'running') {\n this.agentStatusTip = tip;\n }\n originalOnTaskStartTip?.(tip);\n };\n freeFn = [\n ...(newFreeFn || []),\n {\n name: 'restore-agent-onTaskStartTip',\n fn: () => {\n if (agent) {\n agent.onTaskStartTip = originalOnTaskStartTip;\n }\n },\n },\n ];\n } catch (e) {\n this.setPlayerStatus('error', e as Error);\n return;\n }\n this.pageAgent = agent;\n\n let taskIndex = 0;\n this.setPlayerStatus('running');\n let errorFlag = false;\n while (taskIndex < tasks.length) {\n const taskStatus = this.taskStatusList[taskIndex];\n this.setTaskStatus(taskIndex, 'running' as any);\n this.setTaskIndex(taskIndex);\n\n try {\n await this.playTask(taskStatus, this.pageAgent);\n this.setTaskStatus(taskIndex, 'done' as any);\n } catch (e) {\n this.setTaskStatus(taskIndex, 'error' as any, e as Error);\n\n if (taskStatus.continueOnError) {\n // nothing more to do\n } else {\n this.reportFile = agent.reportFile;\n errorFlag = true;\n break;\n }\n }\n this.reportFile = agent?.reportFile;\n taskIndex++;\n }\n\n if (errorFlag) {\n this.setPlayerStatus('error');\n } else {\n this.setPlayerStatus('done');\n }\n this.agentStatusTip = '';\n\n // free the resources\n for (const fn of freeFn) {\n try {\n // console.log('freeing', fn.name);\n await fn.fn();\n // console.log('freed', fn.name);\n } catch (e) {\n // console.error('error freeing', fn.name, e);\n }\n }\n }\n}\n"],"names":["debug","getDebug","ScriptPlayer","key","value","keyToUse","console","status","error","taskIndex","taskIndexToNotify","taskStatus","index","statusValue","output","resolve","process","outputDir","dirname","existsSync","mkdirSync","writeFileSync","JSON","undefined","_this_pageAgent","content","filePath","agent","flow","assert","flowItemIndex","currentStep","Number","flowItem","actionTask","prompt","assertTask","msg","pass","thought","message","Error","queryTask","options","queryResult","numberTask","numberResult","stringTask","stringResult","booleanTask","booleanResult","askTask","askResult","locateTask","locateResult","waitForTask","timeout","sleepTask","ms","msNumber","Promise","setTimeout","evaluateJavaScriptTask","result","logScreenshotTask","aiInput","inputTask","locatePrompt","aiKeyboardPress","keyboardPressTask","keyName","aiScroll","scrollTask","actionSpace","locatePromptShortcut","matchedAction","action","actionInterfaceAlias","Object","keyOfActionInActionSpace","locateParam","restParams","buildDetailedLocateParamAndRestParams","flowParams","target","web","android","tasks","webEnv","androidEnv","platform","freeFn","newAgent","newFreeFn","originalOnTaskStartTip","tip","e","errorFlag","fn","script","setupAgent","onTaskStatusChange","scriptPath","_this_target","_this_target1","_this_target2","ifInBrowser","ifInWorker","scriptName","basename","join","getMidsceneRunSubDir","Date","task","_task_flow"],"mappings":";;;;;;;;;;;;;;;;AAkCA,MAAMA,QAAQC,SAAS;AAChB,MAAMC;IAgEH,UAAUC,GAAuB,EAAEC,KAAU,EAAE;QACrD,MAAMC,WAAWF,OAAO,IAAI,CAAC,kBAAkB;QAC/C,IAAI,IAAI,CAAC,MAAM,CAACE,SAAS,EACvBC,QAAQ,IAAI,CAAC,CAAC,WAAW,EAAED,SAAS,+BAA+B,CAAC;QAEtE,IAAI,CAAC,MAAM,CAACA,SAAS,GAAGD;QAExB,OAAO,IAAI,CAAC,WAAW;IACzB;IAEQ,gBAAgBG,MAA+B,EAAEC,KAAa,EAAE;QACtE,IAAI,CAAC,MAAM,GAAGD;QACd,IAAI,CAAC,YAAY,GAAGC;IACtB;IAEQ,8BAA8BC,SAAkB,EAAE;QACxD,MAAMC,oBACJ,AAAqB,YAArB,OAAOD,YAAyBA,YAAY,IAAI,CAAC,gBAAgB;QAEnE,IAAI,AAA6B,YAA7B,OAA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{
|
|
4
|
+
return content.replace(/\$\{([^}]+)\}/g, (_, envVar)=>{
|
|
5
|
+
const value = process.env[envVar.trim()];
|
|
6
|
+
if (void 0 === value) throw new Error(`Environment variable "${envVar.trim()}" is not defined`);
|
|
7
|
+
return value;
|
|
8
|
+
});
|
|
9
|
+
}
|
|
10
|
+
function parseYamlScript(content, filePath, ignoreCheckingTarget) {
|
|
11
|
+
let processedContent = content;
|
|
12
|
+
if (-1 !== content.indexOf('android') && content.match(/deviceId:\s*(\d+)/)) {
|
|
13
|
+
let matchedDeviceId;
|
|
14
|
+
processedContent = content.replace(/deviceId:\s*(\d+)/g, (match, deviceId)=>{
|
|
15
|
+
matchedDeviceId = deviceId;
|
|
16
|
+
return `deviceId: '${deviceId}'`;
|
|
17
|
+
});
|
|
18
|
+
console.warn(`please use string-style deviceId in yaml script, for example: deviceId: "${matchedDeviceId}"`);
|
|
19
|
+
}
|
|
20
|
+
const interpolatedContent = interpolateEnvVars(processedContent);
|
|
21
|
+
const obj = js_yaml.load(interpolatedContent, {
|
|
22
|
+
schema: js_yaml.JSON_SCHEMA
|
|
23
|
+
});
|
|
24
|
+
const pathTip = filePath ? `, failed to load ${filePath}` : '';
|
|
25
|
+
const android = void 0 !== obj.android ? Object.assign({}, obj.android || {}) : void 0;
|
|
26
|
+
const webConfig = obj.web || obj.target;
|
|
27
|
+
const web = void 0 !== webConfig ? Object.assign({}, webConfig || {}) : void 0;
|
|
28
|
+
if (!ignoreCheckingTarget) {
|
|
29
|
+
assert(web || android, `at least one of "target", "web", or "android" properties is required in yaml script${pathTip}`);
|
|
30
|
+
assert(web && !android || !web && android, `only one of "target", "web", or "android" properties is allowed in yaml script${pathTip}`);
|
|
31
|
+
if (web || android) assert('object' == typeof web || 'object' == typeof android, `property "target/web/android" must be an object${pathTip}`);
|
|
32
|
+
}
|
|
33
|
+
assert(obj.tasks, `property "tasks" is required in yaml script ${pathTip}`);
|
|
34
|
+
assert(Array.isArray(obj.tasks), `property "tasks" must be an array in yaml script, but got ${obj.tasks}`);
|
|
35
|
+
return obj;
|
|
36
|
+
}
|
|
37
|
+
export { interpolateEnvVars, parseYamlScript };
|
|
38
|
+
|
|
39
|
+
//# sourceMappingURL=utils.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"yaml/utils.mjs","sources":["webpack://@midscene/core/./src/yaml/utils.ts"],"sourcesContent":["import { assert } from '@midscene/shared/utils';\nimport yaml from 'js-yaml';\n\nimport type { MidsceneYamlScript } from '@/index';\n\nexport function interpolateEnvVars(content: string): string {\n return content.replace(/\\$\\{([^}]+)\\}/g, (_, envVar) => {\n const value = process.env[envVar.trim()];\n if (value === undefined) {\n throw new Error(`Environment variable \"${envVar.trim()}\" is not defined`);\n }\n return value;\n });\n}\n\nexport function parseYamlScript(\n content: string,\n filePath?: string,\n ignoreCheckingTarget?: boolean,\n): MidsceneYamlScript {\n let processedContent = content;\n if (content.indexOf('android') !== -1 && content.match(/deviceId:\\s*(\\d+)/)) {\n let matchedDeviceId;\n processedContent = content.replace(\n /deviceId:\\s*(\\d+)/g,\n (match, deviceId) => {\n matchedDeviceId = deviceId;\n return `deviceId: '${deviceId}'`;\n },\n );\n console.warn(\n `please use string-style deviceId in yaml script, for example: deviceId: \"${matchedDeviceId}\"`,\n );\n }\n const interpolatedContent = interpolateEnvVars(processedContent);\n const obj = yaml.load(interpolatedContent, {\n schema: yaml.JSON_SCHEMA,\n }) as MidsceneYamlScript;\n\n const pathTip = filePath ? `, failed to load ${filePath}` : '';\n const android =\n typeof obj.android !== 'undefined'\n ? Object.assign({}, obj.android || {})\n : undefined;\n const webConfig = obj.web || obj.target; // no need to handle null case, because web has required parameters url\n const web =\n typeof webConfig !== 'undefined'\n ? Object.assign({}, webConfig || {})\n : undefined;\n\n if (!ignoreCheckingTarget) {\n // make sure at least one of target/web/android is provided\n assert(\n web || android,\n `at least one of \"target\", \"web\", or \"android\" properties is required in yaml script${pathTip}`,\n );\n\n // make sure only one of target/web/android is provided\n assert(\n (web && !android) || (!web && android),\n `only one of \"target\", \"web\", or \"android\" properties is allowed in yaml script${pathTip}`,\n );\n\n // make sure the config is valid\n if (web || android) {\n assert(\n typeof web === 'object' || typeof android === 'object',\n `property \"target/web/android\" must be an object${pathTip}`,\n );\n }\n }\n\n assert(obj.tasks, `property \"tasks\" is required in yaml script ${pathTip}`);\n assert(\n Array.isArray(obj.tasks),\n `property \"tasks\" must be an array in yaml script, but got ${obj.tasks}`,\n );\n return obj;\n}\n"],"names":["interpolateEnvVars","content","_","envVar","value","process","undefined","Error","parseYamlScript","filePath","ignoreCheckingTarget","processedContent","matchedDeviceId","match","deviceId","console","interpolatedContent","obj","yaml","pathTip","android","Object","webConfig","web","assert","Array"],"mappings":";;AAKO,SAASA,mBAAmBC,OAAe;IAChD,OAAOA,QAAQ,OAAO,CAAC,kBAAkB,CAACC,GAAGC;QAC3C,MAAMC,QAAQC,QAAQ,GAAG,CAACF,OAAO,IAAI,GAAG;QACxC,IAAIC,AAAUE,WAAVF,OACF,MAAM,IAAIG,MAAM,CAAC,sBAAsB,EAAEJ,OAAO,IAAI,GAAG,gBAAgB,CAAC;QAE1E,OAAOC;IACT;AACF;AAEO,SAASI,gBACdP,OAAe,EACfQ,QAAiB,EACjBC,oBAA8B;IAE9B,IAAIC,mBAAmBV;IACvB,IAAIA,AAA+B,OAA/BA,QAAQ,OAAO,CAAC,cAAqBA,QAAQ,KAAK,CAAC,sBAAsB;QAC3E,IAAIW;QACJD,mBAAmBV,QAAQ,OAAO,CAChC,sBACA,CAACY,OAAOC;YACNF,kBAAkBE;YAClB,OAAO,CAAC,WAAW,EAAEA,SAAS,CAAC,CAAC;QAClC;QAEFC,QAAQ,IAAI,CACV,CAAC,yEAAyE,EAAEH,gBAAgB,CAAC,CAAC;IAElG;IACA,MAAMI,sBAAsBhB,mBAAmBW;IAC/C,MAAMM,MAAMC,QAAAA,IAAS,CAACF,qBAAqB;QACzC,QAAQE,QAAAA,WAAgB;IAC1B;IAEA,MA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