misoai-web 1.0.1 → 1.0.3
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/LICENSE +21 -0
- package/README.md +8 -8
- package/bin/midscene-playground +2 -2
- package/package.json +23 -24
- package/dist/es/agent.js +0 -2451
- package/dist/es/agent.js.map +0 -1
- package/dist/es/bridge-mode-browser.js +0 -908
- package/dist/es/bridge-mode-browser.js.map +0 -1
- package/dist/es/bridge-mode.js +0 -2812
- package/dist/es/bridge-mode.js.map +0 -1
- package/dist/es/chrome-extension.js +0 -3152
- package/dist/es/chrome-extension.js.map +0 -1
- package/dist/es/index.js +0 -3052
- package/dist/es/index.js.map +0 -1
- package/dist/es/midscene-playground.js +0 -2781
- package/dist/es/midscene-playground.js.map +0 -1
- package/dist/es/midscene-server.js +0 -247
- package/dist/es/midscene-server.js.map +0 -1
- package/dist/es/playground.js +0 -2552
- package/dist/es/playground.js.map +0 -1
- package/dist/es/playwright-report.js +0 -120
- package/dist/es/playwright-report.js.map +0 -1
- package/dist/es/playwright.js +0 -2997
- package/dist/es/playwright.js.map +0 -1
- package/dist/es/puppeteer-agent-launcher.js +0 -2947
- package/dist/es/puppeteer-agent-launcher.js.map +0 -1
- package/dist/es/puppeteer.js +0 -2794
- package/dist/es/puppeteer.js.map +0 -1
- package/dist/es/ui-utils.js +0 -106
- package/dist/es/ui-utils.js.map +0 -1
- package/dist/es/utils.js +0 -197
- package/dist/es/utils.js.map +0 -1
- package/dist/es/yaml.js +0 -333
- package/dist/es/yaml.js.map +0 -1
- package/dist/lib/agent.js +0 -2466
- package/dist/lib/agent.js.map +0 -1
- package/dist/lib/bridge-mode-browser.js +0 -942
- package/dist/lib/bridge-mode-browser.js.map +0 -1
- package/dist/lib/bridge-mode.js +0 -2832
- package/dist/lib/bridge-mode.js.map +0 -1
- package/dist/lib/chrome-extension.js +0 -3169
- package/dist/lib/chrome-extension.js.map +0 -1
- package/dist/lib/index.js +0 -3071
- package/dist/lib/index.js.map +0 -1
- package/dist/lib/midscene-playground.js +0 -2785
- package/dist/lib/midscene-playground.js.map +0 -1
- package/dist/lib/midscene-server.js +0 -273
- package/dist/lib/midscene-server.js.map +0 -1
- package/dist/lib/playground.js +0 -2571
- package/dist/lib/playground.js.map +0 -1
- package/dist/lib/playwright-report.js +0 -148
- package/dist/lib/playwright-report.js.map +0 -1
- package/dist/lib/playwright.js +0 -3017
- package/dist/lib/playwright.js.map +0 -1
- package/dist/lib/puppeteer-agent-launcher.js +0 -2963
- package/dist/lib/puppeteer-agent-launcher.js.map +0 -1
- package/dist/lib/puppeteer.js +0 -2808
- package/dist/lib/puppeteer.js.map +0 -1
- package/dist/lib/ui-utils.js +0 -137
- package/dist/lib/ui-utils.js.map +0 -1
- package/dist/lib/utils.js +0 -235
- package/dist/lib/utils.js.map +0 -1
- package/dist/lib/yaml.js +0 -372
- package/dist/lib/yaml.js.map +0 -1
- package/dist/types/agent.d.ts +0 -254
- package/dist/types/bridge-mode-browser.d.ts +0 -9
- package/dist/types/bridge-mode.d.ts +0 -40
- package/dist/types/browser-d447695b.d.ts +0 -37
- package/dist/types/chrome-extension.d.ts +0 -18
- package/dist/types/index.d.ts +0 -16
- package/dist/types/midscene-playground.d.ts +0 -2
- package/dist/types/midscene-server.d.ts +0 -31
- package/dist/types/page-b8ada1f3.d.ts +0 -322
- package/dist/types/playground.d.ts +0 -17
- package/dist/types/playwright-report.d.ts +0 -11
- package/dist/types/playwright.d.ts +0 -87
- package/dist/types/puppeteer-agent-launcher.d.ts +0 -40
- package/dist/types/puppeteer.d.ts +0 -17
- package/dist/types/ui-utils.d.ts +0 -14
- package/dist/types/utils-badc824e.d.ts +0 -34
- package/dist/types/utils.d.ts +0 -8
- package/dist/types/yaml.d.ts +0 -15
package/dist/lib/ui-utils.js.map
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA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A,EAC5B;AACA,SAAO;AACT;AAEO,SAAS,SAAS,MAAqB;AAC5C,MAAI;AACJ,MAAI,KAAK,SAAS,YAAY;AAC5B,YAAS,MAAgC,OAAO;AAAA,EAClD;AAEA,MAAI,KAAK,SAAS,WAAW;AAC3B,YACG,MAAqC,OAAO,UAC5C,MAAqC,OAAO,MAC5C,MAAoC,OAAO,cAC3C,MAAwC,OAAO;AAAA,EACpD;AAEA,MAAI,KAAK,SAAS,UAAU;AAC1B,UAAM,SAAU,MAA8B;AAC9C,UAAM,YAAY,SAAS,eAAe,MAAM,IAAI;AAEpD,YAAQ,KAAK,WAAW;AACxB,QAAI,OAAQ,MAA8B,OAAO,WAAW,UAAU;AACpE,cAAQ,GAAI,MAA8B,OAAO,MAAM;AAAA,IACzD,WACE,OAAQ,MAA8B,OAAO,eAAe,UAC5D;AACA,cAAQ,eAAgB,MAA8B,KAAK;AAAA,IAC7D,WACE,OAAQ,MAA8B,OAAO,UAAU,aACvD;AACA,cAAS,MAA8B,OAAO;AAAA,IAChD;AAEA,QAAI,WAAW;AACb,UAAI,OAAO;AACT,gBAAQ,GAAG,SAAS,MAAM,KAAK;AAAA,MACjC,OAAO;AACL,gBAAQ;AAAA,MACV;AAAA,IACF;AAAA,EACF;AAEA,MAAI,OAAO,UAAU;AAAa,WAAO;AACzC,SAAO,OAAO,UAAU,WACpB,QACA,KAAK,UAAU,OAAO,QAAW,CAAC;AACxC;AAEO,IAAM,wBAAwB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA","names":[],"ignoreList":[],"sources":["../../src/common/ui-utils.ts"],"sourcesContent":["import type {\n DetailedLocateParam,\n ExecutionTask,\n ExecutionTaskAction,\n ExecutionTaskInsightAssertion,\n ExecutionTaskInsightLocate,\n ExecutionTaskInsightQuery,\n ExecutionTaskPlanning,\n PlanningActionParamScroll,\n} from 'misoai-core';\n\nexport function typeStr(task: ExecutionTask) {\n return task.subType && task.subType !== 'Plan'\n ? `${task.type} / ${task.subType || ''}`\n : task.type;\n}\n\nexport function getKeyCommands(\n value: string | string[],\n): Array<{ key: string; command?: string }> {\n // Ensure value is an array of keys\n const keys = Array.isArray(value) ? value : [value];\n\n // Process each key to attach a corresponding command if needed, based on the presence of 'Meta' or 'Control' in the keys array.\n // ref: https://github.com/puppeteer/puppeteer/pull/9357/files#diff-32cf475237b000f980eb214a0a823e45a902bddb7d2426d677cae96397aa0ae4R94\n return keys.reduce((acc: Array<{ key: string; command?: string }>, k) => {\n const includeMeta = keys.includes('Meta') || keys.includes('Control');\n if (includeMeta && (k === 'a' || k === 'A')) {\n return acc.concat([{ key: k, command: 'SelectAll' }]);\n }\n if (includeMeta && (k === 'c' || k === 'C')) {\n return acc.concat([{ key: k, command: 'Copy' }]);\n }\n if (includeMeta && (k === 'v' || k === 'V')) {\n return acc.concat([{ key: k, command: 'Paste' }]);\n }\n return acc.concat([{ key: k }]);\n }, []);\n}\n\nexport function locateParamStr(locate?: DetailedLocateParam) {\n if (!locate) {\n return '';\n }\n\n if (typeof locate === 'string') {\n return locate;\n }\n\n return locate.prompt;\n}\n\nexport function scrollParamStr(scrollParam?: PlanningActionParamScroll) {\n if (!scrollParam) {\n return '';\n }\n return `${scrollParam.direction || 'down'}, ${scrollParam.scrollType || 'once'}, ${scrollParam.distance || 'distance-not-set'}`;\n}\n\nexport function taskTitleStr(\n type:\n | 'Tap'\n | 'Hover'\n | 'Input'\n | 'KeyboardPress'\n | 'Scroll'\n | 'Action'\n | 'Query'\n | 'Assert'\n | 'WaitFor'\n | 'Locate'\n | 'Boolean'\n | 'Number'\n | 'String',\n prompt: string,\n) {\n if (prompt) {\n return `${type} - ${prompt}`;\n }\n return type;\n}\n\nexport function paramStr(task: ExecutionTask) {\n let value: string | undefined | object;\n if (task.type === 'Planning') {\n value = (task as ExecutionTaskPlanning)?.param?.userInstruction;\n }\n\n if (task.type === 'Insight') {\n value =\n (task as ExecutionTaskInsightLocate)?.param?.prompt ||\n (task as ExecutionTaskInsightLocate)?.param?.id ||\n (task as ExecutionTaskInsightQuery)?.param?.dataDemand ||\n (task as ExecutionTaskInsightAssertion)?.param?.assertion;\n }\n\n if (task.type === 'Action') {\n const locate = (task as ExecutionTaskAction)?.locate;\n const locateStr = locate ? locateParamStr(locate) : '';\n\n value = task.thought || '';\n if (typeof (task as ExecutionTaskAction)?.param?.timeMs === 'number') {\n value = `${(task as ExecutionTaskAction)?.param?.timeMs}ms`;\n } else if (\n typeof (task as ExecutionTaskAction)?.param?.scrollType === 'string'\n ) {\n value = scrollParamStr((task as ExecutionTaskAction)?.param);\n } else if (\n typeof (task as ExecutionTaskAction)?.param?.value !== 'undefined'\n ) {\n value = (task as ExecutionTaskAction)?.param?.value;\n }\n\n if (locateStr) {\n if (value) {\n value = `${locateStr} - ${value}`;\n } else {\n value = locateStr;\n }\n }\n }\n\n if (typeof value === 'undefined') return '';\n return typeof value === 'string'\n ? value\n : JSON.stringify(value, undefined, 2);\n}\n\nexport const limitOpenNewTabScript = `\nif (!window.__MIDSCENE_NEW_TAB_INTERCEPTOR_INITIALIZED__) {\n window.__MIDSCENE_NEW_TAB_INTERCEPTOR_INITIALIZED__ = true;\n\n // Intercept the window.open method (only once)\n window.open = function(url) {\n console.log('Blocked window.open:', url);\n window.location.href = url;\n return null;\n };\n\n // Block all a tag clicks with target=\"_blank\" (only once)\n document.addEventListener('click', function(e) {\n const target = e.target.closest('a');\n if (target && target.target === '_blank') {\n e.preventDefault();\n console.log('Blocked new tab:', target.href);\n window.location.href = target.href;\n target.removeAttribute('target');\n }\n }, true);\n}\n`;\n"]}
|
package/dist/lib/utils.js
DELETED
@@ -1,235 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
var __create = Object.create;
|
3
|
-
var __defProp = Object.defineProperty;
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
8
|
-
var __export = (target, all) => {
|
9
|
-
for (var name in all)
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
11
|
-
};
|
12
|
-
var __copyProps = (to, from, except, desc) => {
|
13
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
14
|
-
for (let key of __getOwnPropNames(from))
|
15
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
16
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
17
|
-
}
|
18
|
-
return to;
|
19
|
-
};
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
26
|
-
mod
|
27
|
-
));
|
28
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
29
|
-
|
30
|
-
// src/common/utils.ts
|
31
|
-
var utils_exports = {};
|
32
|
-
__export(utils_exports, {
|
33
|
-
ERROR_CODE_NOT_IMPLEMENTED_AS_DESIGNED: () => ERROR_CODE_NOT_IMPLEMENTED_AS_DESIGNED,
|
34
|
-
forceClosePopup: () => forceClosePopup,
|
35
|
-
generateCacheId: () => generateCacheId,
|
36
|
-
getCurrentExecutionFile: () => getCurrentExecutionFile,
|
37
|
-
matchElementFromPlan: () => matchElementFromPlan,
|
38
|
-
parseContextFromWebPage: () => parseContextFromWebPage,
|
39
|
-
printReportMsg: () => printReportMsg,
|
40
|
-
replaceIllegalPathCharsAndSpace: () => replaceIllegalPathCharsAndSpace,
|
41
|
-
reportFileName: () => reportFileName
|
42
|
-
});
|
43
|
-
module.exports = __toCommonJS(utils_exports);
|
44
|
-
var import_ai_model = require("misoai-core/ai-model");
|
45
|
-
var import_utils = require("misoai-core/utils");
|
46
|
-
var import_env = require("misoai-shared/env");
|
47
|
-
var import_extractor = require("misoai-shared/extractor");
|
48
|
-
var import_img = require("misoai-shared/img");
|
49
|
-
var import_utils2 = require("misoai-shared/utils");
|
50
|
-
var import_dayjs = __toESM(require("dayjs"));
|
51
|
-
|
52
|
-
// src/web-element.ts
|
53
|
-
var WebElementInfo = class {
|
54
|
-
constructor({
|
55
|
-
content,
|
56
|
-
rect,
|
57
|
-
// page,
|
58
|
-
locator,
|
59
|
-
id,
|
60
|
-
attributes,
|
61
|
-
indexId,
|
62
|
-
xpaths
|
63
|
-
}) {
|
64
|
-
this.content = content;
|
65
|
-
this.rect = rect;
|
66
|
-
this.center = [
|
67
|
-
Math.floor(rect.left + rect.width / 2),
|
68
|
-
Math.floor(rect.top + rect.height / 2)
|
69
|
-
];
|
70
|
-
this.locator = locator;
|
71
|
-
this.id = id;
|
72
|
-
this.attributes = attributes;
|
73
|
-
this.indexId = indexId;
|
74
|
-
this.xpaths = xpaths;
|
75
|
-
}
|
76
|
-
};
|
77
|
-
|
78
|
-
// src/common/utils.ts
|
79
|
-
async function parseContextFromWebPage(page, _opt) {
|
80
|
-
(0, import_utils2.assert)(page, "page is required");
|
81
|
-
if (page._forceUsePageContext) {
|
82
|
-
return await page._forceUsePageContext();
|
83
|
-
}
|
84
|
-
const url = await page.url();
|
85
|
-
(0, import_utils.uploadTestInfoToServer)({ testUrl: url });
|
86
|
-
let screenshotBase64;
|
87
|
-
let tree;
|
88
|
-
await Promise.all([
|
89
|
-
page.screenshotBase64().then((base64) => {
|
90
|
-
screenshotBase64 = base64;
|
91
|
-
}),
|
92
|
-
page.getElementsNodeTree().then(async (treeRoot) => {
|
93
|
-
tree = treeRoot;
|
94
|
-
})
|
95
|
-
]);
|
96
|
-
const webTree = (0, import_extractor.traverseTree)(tree, (elementInfo) => {
|
97
|
-
const { rect, id, content, attributes, locator, indexId } = elementInfo;
|
98
|
-
return new WebElementInfo({
|
99
|
-
rect,
|
100
|
-
locator,
|
101
|
-
id,
|
102
|
-
content,
|
103
|
-
attributes,
|
104
|
-
indexId
|
105
|
-
});
|
106
|
-
});
|
107
|
-
(0, import_utils2.assert)(screenshotBase64, "screenshotBase64 is required");
|
108
|
-
const elementsInfo = (0, import_extractor.treeToList)(webTree);
|
109
|
-
const size = await page.size();
|
110
|
-
if (size.dpr && size.dpr > 1) {
|
111
|
-
screenshotBase64 = await (0, import_img.resizeImgBase64)(screenshotBase64, {
|
112
|
-
width: size.width,
|
113
|
-
height: size.height
|
114
|
-
});
|
115
|
-
}
|
116
|
-
return {
|
117
|
-
content: elementsInfo,
|
118
|
-
tree: webTree,
|
119
|
-
size,
|
120
|
-
screenshotBase64,
|
121
|
-
url
|
122
|
-
};
|
123
|
-
}
|
124
|
-
function reportFileName(tag = "web") {
|
125
|
-
const reportTagName = (0, import_env.getAIConfig)(import_env.MIDSCENE_REPORT_TAG_NAME);
|
126
|
-
const dateTimeInFileName = (0, import_dayjs.default)().format("YYYY-MM-DD_HH-mm-ss");
|
127
|
-
const uniqueId = (0, import_utils2.uuid)().substring(0, 8);
|
128
|
-
return `${reportTagName || tag}-${dateTimeInFileName}-${uniqueId}`;
|
129
|
-
}
|
130
|
-
function printReportMsg(filepath) {
|
131
|
-
(0, import_utils2.logMsg)(`Midscene - report file updated: ${filepath}`);
|
132
|
-
}
|
133
|
-
function getCurrentExecutionFile(trace) {
|
134
|
-
const error = new Error();
|
135
|
-
const stackTrace = trace || error.stack;
|
136
|
-
const pkgDir = process.cwd() || "";
|
137
|
-
if (stackTrace) {
|
138
|
-
const stackLines = stackTrace.split("\n");
|
139
|
-
for (const line of stackLines) {
|
140
|
-
if (line.includes(".spec.") || line.includes(".test.") || line.includes(".ts") || line.includes(".js")) {
|
141
|
-
const match = line.match(/(?:at\s+)?(.*?\.(?:spec|test)\.[jt]s)/);
|
142
|
-
if (match?.[1]) {
|
143
|
-
const targetFileName = match[1].replace(pkgDir, "").trim().replace("at ", "");
|
144
|
-
return targetFileName;
|
145
|
-
}
|
146
|
-
}
|
147
|
-
}
|
148
|
-
}
|
149
|
-
return false;
|
150
|
-
}
|
151
|
-
var testFileIndex = /* @__PURE__ */ new Map();
|
152
|
-
function generateCacheId(fileName) {
|
153
|
-
let taskFile = fileName || getCurrentExecutionFile();
|
154
|
-
if (!taskFile) {
|
155
|
-
taskFile = (0, import_utils2.uuid)();
|
156
|
-
console.warn(
|
157
|
-
"Midscene - using random UUID for cache id. Cache may be invalid."
|
158
|
-
);
|
159
|
-
}
|
160
|
-
if (testFileIndex.has(taskFile)) {
|
161
|
-
const currentIndex = testFileIndex.get(taskFile);
|
162
|
-
if (currentIndex !== void 0) {
|
163
|
-
testFileIndex.set(taskFile, currentIndex + 1);
|
164
|
-
}
|
165
|
-
} else {
|
166
|
-
testFileIndex.set(taskFile, 1);
|
167
|
-
}
|
168
|
-
return `${taskFile}-${testFileIndex.get(taskFile)}`;
|
169
|
-
}
|
170
|
-
var ERROR_CODE_NOT_IMPLEMENTED_AS_DESIGNED = "NOT_IMPLEMENTED_AS_DESIGNED";
|
171
|
-
function replaceIllegalPathCharsAndSpace(str) {
|
172
|
-
return str.replace(/[/\\:*?"<>| ]/g, "-");
|
173
|
-
}
|
174
|
-
function forceClosePopup(page, debug) {
|
175
|
-
page.on("popup", async (popup) => {
|
176
|
-
if (!popup) {
|
177
|
-
console.warn("got a popup event, but the popup is not ready yet, skip");
|
178
|
-
return;
|
179
|
-
}
|
180
|
-
const url = await popup.url();
|
181
|
-
console.log(`Popup opened: ${url}`);
|
182
|
-
if (!popup.isClosed()) {
|
183
|
-
try {
|
184
|
-
await popup.close();
|
185
|
-
} catch (error) {
|
186
|
-
debug(`failed to close popup ${url}, error: ${error}`);
|
187
|
-
}
|
188
|
-
} else {
|
189
|
-
debug(`popup is already closed, skip close ${url}`);
|
190
|
-
}
|
191
|
-
if (!page.isClosed()) {
|
192
|
-
try {
|
193
|
-
await page.goto(url);
|
194
|
-
} catch (error) {
|
195
|
-
debug(`failed to goto ${url}, error: ${error}`);
|
196
|
-
}
|
197
|
-
} else {
|
198
|
-
debug(`page is already closed, skip goto ${url}`);
|
199
|
-
}
|
200
|
-
});
|
201
|
-
}
|
202
|
-
function matchElementFromPlan(planLocateParam, tree) {
|
203
|
-
if (!planLocateParam) {
|
204
|
-
return void 0;
|
205
|
-
}
|
206
|
-
if (planLocateParam.id) {
|
207
|
-
return (0, import_extractor.getNodeFromCacheList)(planLocateParam.id);
|
208
|
-
}
|
209
|
-
if (planLocateParam.bbox) {
|
210
|
-
const centerPosition = {
|
211
|
-
x: Math.floor((planLocateParam.bbox[0] + planLocateParam.bbox[2]) / 2),
|
212
|
-
y: Math.floor((planLocateParam.bbox[1] + planLocateParam.bbox[3]) / 2)
|
213
|
-
};
|
214
|
-
let element = (0, import_ai_model.elementByPositionWithElementInfo)(tree, centerPosition);
|
215
|
-
if (!element) {
|
216
|
-
element = (0, import_extractor.generateElementByPosition)(centerPosition);
|
217
|
-
}
|
218
|
-
return element;
|
219
|
-
}
|
220
|
-
return void 0;
|
221
|
-
}
|
222
|
-
// Annotate the CommonJS export names for ESM import in node:
|
223
|
-
0 && (module.exports = {
|
224
|
-
ERROR_CODE_NOT_IMPLEMENTED_AS_DESIGNED,
|
225
|
-
forceClosePopup,
|
226
|
-
generateCacheId,
|
227
|
-
getCurrentExecutionFile,
|
228
|
-
matchElementFromPlan,
|
229
|
-
parseContextFromWebPage,
|
230
|
-
printReportMsg,
|
231
|
-
replaceIllegalPathCharsAndSpace,
|
232
|
-
reportFileName
|
233
|
-
});
|
234
|
-
|
235
|
-
//# sourceMappingURL=utils.js.map
|
package/dist/lib/utils.js.map
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"mappings":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qC,GAAG,EAAE;AAAA,IAClD;AAAA,EACF,CAAC;AACH;AAEO,SAAS,qBACd,iBACA,MACA;AACA,MAAI,CAAC,iBAAiB;AACpB,WAAO;AAAA,EACT;AACA,MAAI,gBAAgB,IAAI;AACtB,eAAO,uCAAqB,gBAAgB,EAAE;AAAA,EAChD;AAEA,MAAI,gBAAgB,MAAM;AACxB,UAAM,iBAAiB;AAAA,MACrB,GAAG,KAAK,OAAO,gBAAgB,KAAK,CAAC,IAAI,gBAAgB,KAAK,CAAC,KAAK,CAAC;AAAA,MACrE,GAAG,KAAK,OAAO,gBAAgB,KAAK,CAAC,IAAI,gBAAgB,KAAK,CAAC,KAAK,CAAC;AAAA,IACvE;AACA,QAAI,cAAU,kDAAiC,MAAM,cAAc;AAEnE,QAAI,CAAC,SAAS;AACZ,oBAAU,4CAA0B,cAAc;AAAA,IACpD;AAEA,WAAO;AAAA,EACT;AAEA,SAAO;AACT","names":["import_utils","dayjs"],"ignoreList":[],"sources":["../../src/common/utils.ts","../../src/web-element.ts"],"sourcesContent":["import type { StaticPage } from '@/playground';\nimport type {\n BaseElement,\n ElementTreeNode,\n PlanningLocateParam,\n PlaywrightParserOpt,\n UIContext,\n} from 'misoai-core';\nimport { elementByPositionWithElementInfo } from 'misoai-core/ai-model';\nimport { uploadTestInfoToServer } from 'misoai-core/utils';\nimport { MIDSCENE_REPORT_TAG_NAME, getAIConfig } from 'misoai-shared/env';\nimport type { ElementInfo } from 'misoai-shared/extractor';\nimport {\n generateElementByPosition,\n getNodeFromCacheList,\n traverseTree,\n treeToList,\n} from 'misoai-shared/extractor';\nimport { resizeImgBase64 } from 'misoai-shared/img';\nimport type { DebugFunction } from 'misoai-shared/logger';\nimport { assert, logMsg, uuid } from 'misoai-shared/utils';\nimport dayjs from 'dayjs';\nimport type { Page as PlaywrightPage } from 'playwright';\nimport type { Page as PuppeteerPage } from 'puppeteer';\nimport { WebElementInfo } from '../web-element';\nimport type { WebPage } from './page';\n\nexport type WebUIContext = UIContext<WebElementInfo> & {\n url: string;\n};\n\nexport async function parseContextFromWebPage(\n page: WebPage,\n _opt?: PlaywrightParserOpt,\n): Promise<WebUIContext> {\n assert(page, 'page is required');\n if ((page as StaticPage)._forceUsePageContext) {\n return await (page as any)._forceUsePageContext();\n }\n const url = await page.url();\n uploadTestInfoToServer({ testUrl: url });\n\n let screenshotBase64: string;\n let tree: ElementTreeNode<ElementInfo>;\n\n await Promise.all([\n page.screenshotBase64().then((base64) => {\n screenshotBase64 = base64;\n }),\n page.getElementsNodeTree().then(async (treeRoot) => {\n tree = treeRoot;\n }),\n ]);\n\n const webTree = traverseTree(tree!, (elementInfo) => {\n const { rect, id, content, attributes, locator, indexId } = elementInfo;\n return new WebElementInfo({\n rect,\n locator,\n id,\n content,\n attributes,\n indexId,\n });\n });\n\n assert(screenshotBase64!, 'screenshotBase64 is required');\n\n const elementsInfo = treeToList(webTree);\n const size = await page.size();\n\n if (size.dpr && size.dpr > 1) {\n // console.time('resizeImgBase64');\n screenshotBase64 = await resizeImgBase64(screenshotBase64, {\n width: size.width,\n height: size.height,\n });\n // console.timeEnd('resizeImgBase64');\n }\n\n return {\n content: elementsInfo!,\n tree: webTree,\n size,\n screenshotBase64: screenshotBase64!,\n url,\n };\n}\n\nexport function reportFileName(tag = 'web') {\n const reportTagName = getAIConfig(MIDSCENE_REPORT_TAG_NAME);\n const dateTimeInFileName = dayjs().format('YYYY-MM-DD_HH-mm-ss');\n // ensure uniqueness at the same time\n const uniqueId = uuid().substring(0, 8);\n return `${reportTagName || tag}-${dateTimeInFileName}-${uniqueId}`;\n}\n\nexport function printReportMsg(filepath: string) {\n logMsg(`Midscene - report file updated: ${filepath}`);\n}\n\n/**\n * Get the current execution file name\n * @returns The name of the current execution file\n */\nexport function getCurrentExecutionFile(trace?: string): string | false {\n const error = new Error();\n const stackTrace = trace || error.stack;\n const pkgDir = process.cwd() || '';\n if (stackTrace) {\n const stackLines = stackTrace.split('\\n');\n for (const line of stackLines) {\n if (\n line.includes('.spec.') ||\n line.includes('.test.') ||\n line.includes('.ts') ||\n line.includes('.js')\n ) {\n const match = line.match(/(?:at\\s+)?(.*?\\.(?:spec|test)\\.[jt]s)/);\n if (match?.[1]) {\n const targetFileName = match[1]\n .replace(pkgDir, '')\n .trim()\n .replace('at ', '');\n return targetFileName;\n }\n }\n }\n }\n return false;\n}\n\nconst testFileIndex = new Map<string, number>();\n\nexport function generateCacheId(fileName?: string): string {\n let taskFile = fileName || getCurrentExecutionFile();\n if (!taskFile) {\n taskFile = uuid();\n console.warn(\n 'Midscene - using random UUID for cache id. Cache may be invalid.',\n );\n }\n\n if (testFileIndex.has(taskFile)) {\n const currentIndex = testFileIndex.get(taskFile);\n if (currentIndex !== undefined) {\n testFileIndex.set(taskFile, currentIndex + 1);\n }\n } else {\n testFileIndex.set(taskFile, 1);\n }\n return `${taskFile}-${testFileIndex.get(taskFile)}`;\n}\n\nexport const ERROR_CODE_NOT_IMPLEMENTED_AS_DESIGNED =\n 'NOT_IMPLEMENTED_AS_DESIGNED';\n\nexport function replaceIllegalPathCharsAndSpace(str: string) {\n return str.replace(/[/\\\\:*?\"<>| ]/g, '-');\n}\n\nexport function forceClosePopup(\n page: PuppeteerPage | PlaywrightPage,\n debug: DebugFunction,\n) {\n page.on('popup', async (popup) => {\n if (!popup) {\n console.warn('got a popup event, but the popup is not ready yet, skip');\n return;\n }\n const url = await (popup as PuppeteerPage).url();\n console.log(`Popup opened: ${url}`);\n if (!(popup as PuppeteerPage).isClosed()) {\n try {\n await (popup as PuppeteerPage).close(); // Close the newly opened TAB\n } catch (error) {\n debug(`failed to close popup ${url}, error: ${error}`);\n }\n } else {\n debug(`popup is already closed, skip close ${url}`);\n }\n\n if (!page.isClosed()) {\n try {\n await page.goto(url);\n } catch (error) {\n debug(`failed to goto ${url}, error: ${error}`);\n }\n } else {\n debug(`page is already closed, skip goto ${url}`);\n }\n });\n}\n\nexport function matchElementFromPlan(\n planLocateParam: PlanningLocateParam,\n tree: ElementTreeNode<BaseElement>,\n) {\n if (!planLocateParam) {\n return undefined;\n }\n if (planLocateParam.id) {\n return getNodeFromCacheList(planLocateParam.id);\n }\n\n if (planLocateParam.bbox) {\n const centerPosition = {\n x: Math.floor((planLocateParam.bbox[0] + planLocateParam.bbox[2]) / 2),\n y: Math.floor((planLocateParam.bbox[1] + planLocateParam.bbox[3]) / 2),\n };\n let element = elementByPositionWithElementInfo(tree, centerPosition);\n\n if (!element) {\n element = generateElementByPosition(centerPosition) as BaseElement;\n }\n\n return element;\n }\n\n return undefined;\n}\n","import type { BaseElement, Rect } from 'misoai-core';\nimport type { NodeType } from 'misoai-shared/constants';\nexport interface WebElementInfoType extends BaseElement {\n id: string;\n locator: string;\n attributes: {\n nodeType: NodeType;\n [key: string]: string;\n };\n}\n\nexport class WebElementInfo implements BaseElement {\n content: string;\n\n locator?: string;\n\n rect: Rect;\n\n center: [number, number];\n\n // page: WebPage;\n\n id: string;\n\n indexId: number;\n\n attributes: {\n nodeType: NodeType;\n [key: string]: string;\n };\n\n xpaths?: string[];\n\n constructor({\n content,\n rect,\n // page,\n locator,\n id,\n attributes,\n indexId,\n xpaths,\n }: {\n content: string;\n rect: Rect;\n // page: WebPage;\n locator?: string;\n id: string;\n attributes: {\n nodeType: NodeType;\n [key: string]: string;\n };\n indexId: number;\n xpaths?: string[];\n }) {\n this.content = content;\n this.rect = rect;\n this.center = [\n Math.floor(rect.left + rect.width / 2),\n Math.floor(rect.top + rect.height / 2),\n ];\n // this.page = page;\n this.locator = locator;\n this.id = id;\n this.attributes = attributes;\n this.indexId = indexId;\n this.xpaths = xpaths;\n }\n}\n"]}
|
package/dist/lib/yaml.js
DELETED
@@ -1,372 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
var __create = Object.create;
|
3
|
-
var __defProp = Object.defineProperty;
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
8
|
-
var __export = (target, all) => {
|
9
|
-
for (var name in all)
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
11
|
-
};
|
12
|
-
var __copyProps = (to, from, except, desc) => {
|
13
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
14
|
-
for (let key of __getOwnPropNames(from))
|
15
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
16
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
17
|
-
}
|
18
|
-
return to;
|
19
|
-
};
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
26
|
-
mod
|
27
|
-
));
|
28
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
29
|
-
|
30
|
-
// src/yaml/index.ts
|
31
|
-
var yaml_exports = {};
|
32
|
-
__export(yaml_exports, {
|
33
|
-
ScriptPlayer: () => ScriptPlayer,
|
34
|
-
buildYaml: () => buildYaml,
|
35
|
-
parseYamlScript: () => parseYamlScript
|
36
|
-
});
|
37
|
-
module.exports = __toCommonJS(yaml_exports);
|
38
|
-
|
39
|
-
// src/yaml/player.ts
|
40
|
-
var import_node_fs = require("fs");
|
41
|
-
var import_node_path = require("path");
|
42
|
-
var import_utils = require("misoai-shared/utils");
|
43
|
-
var import_common = require("misoai-shared/common");
|
44
|
-
var ScriptPlayer = class {
|
45
|
-
constructor(script, setupAgent, onTaskStatusChange) {
|
46
|
-
this.script = script;
|
47
|
-
this.setupAgent = setupAgent;
|
48
|
-
this.onTaskStatusChange = onTaskStatusChange;
|
49
|
-
this.taskStatusList = [];
|
50
|
-
this.status = "init";
|
51
|
-
this.unnamedResultIndex = 0;
|
52
|
-
this.pageAgent = null;
|
53
|
-
this.result = {};
|
54
|
-
if (import_utils.ifInBrowser) {
|
55
|
-
this.output = void 0;
|
56
|
-
} else if (script.target?.output) {
|
57
|
-
this.output = (0, import_node_path.resolve)(process.cwd(), script.target.output);
|
58
|
-
} else {
|
59
|
-
this.output = (0, import_node_path.join)((0, import_common.getMidsceneRunSubDir)("output"), `${process.pid}.json`);
|
60
|
-
}
|
61
|
-
this.taskStatusList = (script.tasks || []).map((task, taskIndex) => ({
|
62
|
-
...task,
|
63
|
-
index: taskIndex,
|
64
|
-
status: "init",
|
65
|
-
totalSteps: task.flow?.length || 0
|
66
|
-
}));
|
67
|
-
}
|
68
|
-
setResult(key, value) {
|
69
|
-
const keyToUse = key || this.unnamedResultIndex++;
|
70
|
-
if (this.result[keyToUse]) {
|
71
|
-
console.warn(`result key ${keyToUse} already exists, will overwrite`);
|
72
|
-
}
|
73
|
-
this.result[keyToUse] = value;
|
74
|
-
this.flushResult();
|
75
|
-
}
|
76
|
-
setPlayerStatus(status, error) {
|
77
|
-
this.status = status;
|
78
|
-
this.errorInSetup = error;
|
79
|
-
}
|
80
|
-
notifyCurrentTaskStatusChange(taskIndex) {
|
81
|
-
const taskIndexToNotify = typeof taskIndex === "number" ? taskIndex : this.currentTaskIndex;
|
82
|
-
if (typeof taskIndexToNotify !== "number") {
|
83
|
-
return;
|
84
|
-
}
|
85
|
-
const taskStatus = this.taskStatusList[taskIndexToNotify];
|
86
|
-
if (this.onTaskStatusChange) {
|
87
|
-
this.onTaskStatusChange(taskStatus);
|
88
|
-
}
|
89
|
-
}
|
90
|
-
async setTaskStatus(index, statusValue, error) {
|
91
|
-
this.taskStatusList[index].status = statusValue;
|
92
|
-
if (error) {
|
93
|
-
this.taskStatusList[index].error = error;
|
94
|
-
}
|
95
|
-
this.notifyCurrentTaskStatusChange(index);
|
96
|
-
}
|
97
|
-
setTaskIndex(taskIndex) {
|
98
|
-
this.currentTaskIndex = taskIndex;
|
99
|
-
}
|
100
|
-
flushResult() {
|
101
|
-
if (Object.keys(this.result).length && this.output) {
|
102
|
-
const output = (0, import_node_path.resolve)(process.cwd(), this.output);
|
103
|
-
const outputDir = (0, import_node_path.dirname)(output);
|
104
|
-
if (!(0, import_node_fs.existsSync)(outputDir)) {
|
105
|
-
(0, import_node_fs.mkdirSync)(outputDir, { recursive: true });
|
106
|
-
}
|
107
|
-
(0, import_node_fs.writeFileSync)(output, JSON.stringify(this.result, void 0, 2));
|
108
|
-
}
|
109
|
-
}
|
110
|
-
async playTask(taskStatus, agent) {
|
111
|
-
const { flow } = taskStatus;
|
112
|
-
(0, import_utils.assert)(flow, "missing flow in task");
|
113
|
-
for (const flowItemIndex in flow) {
|
114
|
-
const currentStep = Number.parseInt(flowItemIndex, 10);
|
115
|
-
taskStatus.currentStep = currentStep;
|
116
|
-
const flowItem = flow[flowItemIndex];
|
117
|
-
if ("aiAction" in flowItem || "ai" in flowItem) {
|
118
|
-
const actionTask = flowItem;
|
119
|
-
const prompt = actionTask.aiAction || actionTask.ai;
|
120
|
-
(0, import_utils.assert)(prompt, "missing prompt for ai (aiAction)");
|
121
|
-
(0, import_utils.assert)(
|
122
|
-
typeof prompt === "string",
|
123
|
-
"prompt for aiAction must be a string"
|
124
|
-
);
|
125
|
-
await agent.aiAction(prompt);
|
126
|
-
} else if ("aiAssert" in flowItem) {
|
127
|
-
const assertTask = flowItem;
|
128
|
-
const prompt = assertTask.aiAssert;
|
129
|
-
(0, import_utils.assert)(prompt, "missing prompt for aiAssert");
|
130
|
-
(0, import_utils.assert)(
|
131
|
-
typeof prompt === "string",
|
132
|
-
"prompt for aiAssert must be a string"
|
133
|
-
);
|
134
|
-
await agent.aiAssert(prompt);
|
135
|
-
} else if ("aiQuery" in flowItem) {
|
136
|
-
const queryTask = flowItem;
|
137
|
-
const prompt = queryTask.aiQuery;
|
138
|
-
(0, import_utils.assert)(prompt, "missing prompt for aiQuery");
|
139
|
-
(0, import_utils.assert)(
|
140
|
-
typeof prompt === "string",
|
141
|
-
"prompt for aiQuery must be a string"
|
142
|
-
);
|
143
|
-
const queryResult = await agent.aiQuery(prompt);
|
144
|
-
this.setResult(queryTask.name, queryResult);
|
145
|
-
} else if ("aiNumber" in flowItem) {
|
146
|
-
const numberTask = flowItem;
|
147
|
-
const prompt = numberTask.aiNumber;
|
148
|
-
(0, import_utils.assert)(prompt, "missing prompt for number");
|
149
|
-
(0, import_utils.assert)(
|
150
|
-
typeof prompt === "string",
|
151
|
-
"prompt for number must be a string"
|
152
|
-
);
|
153
|
-
const numberResult = await agent.aiNumber(prompt);
|
154
|
-
this.setResult(numberTask.name, numberResult);
|
155
|
-
} else if ("aiString" in flowItem) {
|
156
|
-
const stringTask = flowItem;
|
157
|
-
const prompt = stringTask.aiString;
|
158
|
-
(0, import_utils.assert)(prompt, "missing prompt for string");
|
159
|
-
(0, import_utils.assert)(
|
160
|
-
typeof prompt === "string",
|
161
|
-
"prompt for string must be a string"
|
162
|
-
);
|
163
|
-
const stringResult = await agent.aiString(prompt);
|
164
|
-
this.setResult(stringTask.name, stringResult);
|
165
|
-
} else if ("aiBoolean" in flowItem) {
|
166
|
-
const booleanTask = flowItem;
|
167
|
-
const prompt = booleanTask.aiBoolean;
|
168
|
-
(0, import_utils.assert)(prompt, "missing prompt for boolean");
|
169
|
-
(0, import_utils.assert)(
|
170
|
-
typeof prompt === "string",
|
171
|
-
"prompt for boolean must be a string"
|
172
|
-
);
|
173
|
-
const booleanResult = await agent.aiBoolean(prompt);
|
174
|
-
this.setResult(booleanTask.name, booleanResult);
|
175
|
-
} else if ("aiLocate" in flowItem) {
|
176
|
-
const locateTask = flowItem;
|
177
|
-
const prompt = locateTask.aiLocate;
|
178
|
-
(0, import_utils.assert)(prompt, "missing prompt for aiLocate");
|
179
|
-
(0, import_utils.assert)(
|
180
|
-
typeof prompt === "string",
|
181
|
-
"prompt for aiLocate must be a string"
|
182
|
-
);
|
183
|
-
const locateResult = await agent.aiLocate(prompt);
|
184
|
-
this.setResult(locateTask.name, locateResult);
|
185
|
-
} else if ("aiWaitFor" in flowItem) {
|
186
|
-
const waitForTask = flowItem;
|
187
|
-
const prompt = waitForTask.aiWaitFor;
|
188
|
-
(0, import_utils.assert)(prompt, "missing prompt for aiWaitFor");
|
189
|
-
(0, import_utils.assert)(
|
190
|
-
typeof prompt === "string",
|
191
|
-
"prompt for aiWaitFor must be a string"
|
192
|
-
);
|
193
|
-
const timeout = waitForTask.timeout;
|
194
|
-
await agent.aiWaitFor(prompt, { timeoutMs: timeout });
|
195
|
-
} else if ("sleep" in flowItem) {
|
196
|
-
const sleepTask = flowItem;
|
197
|
-
const ms = sleepTask.sleep;
|
198
|
-
let msNumber = ms;
|
199
|
-
if (typeof ms === "string") {
|
200
|
-
msNumber = Number.parseInt(ms, 10);
|
201
|
-
}
|
202
|
-
(0, import_utils.assert)(
|
203
|
-
msNumber && msNumber > 0,
|
204
|
-
`ms for sleep must be greater than 0, but got ${ms}`
|
205
|
-
);
|
206
|
-
await new Promise((resolve2) => setTimeout(resolve2, msNumber));
|
207
|
-
} else if ("aiTap" in flowItem) {
|
208
|
-
const tapTask = flowItem;
|
209
|
-
await agent.aiTap(tapTask.aiTap, tapTask);
|
210
|
-
} else if ("aiHover" in flowItem) {
|
211
|
-
const hoverTask = flowItem;
|
212
|
-
await agent.aiHover(hoverTask.aiHover, hoverTask);
|
213
|
-
} else if ("aiInput" in flowItem) {
|
214
|
-
const inputTask = flowItem;
|
215
|
-
await agent.aiInput(inputTask.aiInput, inputTask.locate, inputTask);
|
216
|
-
} else if ("aiKeyboardPress" in flowItem) {
|
217
|
-
const keyboardPressTask = flowItem;
|
218
|
-
await agent.aiKeyboardPress(
|
219
|
-
keyboardPressTask.aiKeyboardPress,
|
220
|
-
keyboardPressTask.locate,
|
221
|
-
keyboardPressTask
|
222
|
-
);
|
223
|
-
} else if ("aiScroll" in flowItem) {
|
224
|
-
const scrollTask = flowItem;
|
225
|
-
await agent.aiScroll(scrollTask, scrollTask.locate, scrollTask);
|
226
|
-
} else if ("javascript" in flowItem) {
|
227
|
-
const evaluateJavaScriptTask = flowItem;
|
228
|
-
const result = await agent.evaluateJavaScript(
|
229
|
-
evaluateJavaScriptTask.javascript
|
230
|
-
);
|
231
|
-
this.setResult(evaluateJavaScriptTask.name, result);
|
232
|
-
} else {
|
233
|
-
throw new Error(`unknown flowItem: ${JSON.stringify(flowItem)}`);
|
234
|
-
}
|
235
|
-
}
|
236
|
-
this.reportFile = agent.reportFile;
|
237
|
-
}
|
238
|
-
async run() {
|
239
|
-
const { target, web, android, tasks } = this.script;
|
240
|
-
const webEnv = web || target;
|
241
|
-
const androidEnv = android;
|
242
|
-
const platform = webEnv || androidEnv;
|
243
|
-
this.setPlayerStatus("running");
|
244
|
-
let agent = null;
|
245
|
-
let freeFn = [];
|
246
|
-
try {
|
247
|
-
const { agent: newAgent, freeFn: newFreeFn } = await this.setupAgent(
|
248
|
-
platform
|
249
|
-
);
|
250
|
-
agent = newAgent;
|
251
|
-
const originalOnTaskStartTip = agent.onTaskStartTip;
|
252
|
-
agent.onTaskStartTip = (tip) => {
|
253
|
-
if (this.status === "running") {
|
254
|
-
this.agentStatusTip = tip;
|
255
|
-
}
|
256
|
-
originalOnTaskStartTip?.(tip);
|
257
|
-
};
|
258
|
-
freeFn = [
|
259
|
-
...newFreeFn || [],
|
260
|
-
{
|
261
|
-
name: "restore-agent-onTaskStartTip",
|
262
|
-
fn: () => {
|
263
|
-
if (agent) {
|
264
|
-
agent.onTaskStartTip = originalOnTaskStartTip;
|
265
|
-
}
|
266
|
-
}
|
267
|
-
}
|
268
|
-
];
|
269
|
-
} catch (e) {
|
270
|
-
this.setPlayerStatus("error", e);
|
271
|
-
return;
|
272
|
-
}
|
273
|
-
this.pageAgent = agent;
|
274
|
-
let taskIndex = 0;
|
275
|
-
this.setPlayerStatus("running");
|
276
|
-
let errorFlag = false;
|
277
|
-
while (taskIndex < tasks.length) {
|
278
|
-
const taskStatus = this.taskStatusList[taskIndex];
|
279
|
-
this.setTaskStatus(taskIndex, "running");
|
280
|
-
this.setTaskIndex(taskIndex);
|
281
|
-
try {
|
282
|
-
await this.playTask(taskStatus, this.pageAgent);
|
283
|
-
this.setTaskStatus(taskIndex, "done");
|
284
|
-
} catch (e) {
|
285
|
-
this.setTaskStatus(taskIndex, "error", e);
|
286
|
-
if (taskStatus.continueOnError) {
|
287
|
-
} else {
|
288
|
-
this.reportFile = agent.reportFile;
|
289
|
-
errorFlag = true;
|
290
|
-
break;
|
291
|
-
}
|
292
|
-
}
|
293
|
-
this.reportFile = agent.reportFile;
|
294
|
-
taskIndex++;
|
295
|
-
}
|
296
|
-
if (errorFlag) {
|
297
|
-
this.setPlayerStatus("error");
|
298
|
-
} else {
|
299
|
-
this.setPlayerStatus("done");
|
300
|
-
}
|
301
|
-
this.agentStatusTip = "";
|
302
|
-
for (const fn of freeFn) {
|
303
|
-
try {
|
304
|
-
await fn.fn();
|
305
|
-
} catch (e) {
|
306
|
-
}
|
307
|
-
}
|
308
|
-
}
|
309
|
-
};
|
310
|
-
|
311
|
-
// src/yaml/builder.ts
|
312
|
-
var import_js_yaml = __toESM(require("js-yaml"));
|
313
|
-
function buildYaml(env, tasks) {
|
314
|
-
const result = {
|
315
|
-
target: env,
|
316
|
-
tasks
|
317
|
-
};
|
318
|
-
return import_js_yaml.default.dump(result, {
|
319
|
-
indent: 2
|
320
|
-
});
|
321
|
-
}
|
322
|
-
|
323
|
-
// src/yaml/utils.ts
|
324
|
-
var import_utils2 = require("misoai-shared/utils");
|
325
|
-
var import_js_yaml2 = __toESM(require("js-yaml"));
|
326
|
-
function interpolateEnvVars(content) {
|
327
|
-
return content.replace(/\$\{([^}]+)\}/g, (_, envVar) => {
|
328
|
-
const value = process.env[envVar.trim()];
|
329
|
-
if (value === void 0) {
|
330
|
-
throw new Error(`Environment variable "${envVar.trim()}" is not defined`);
|
331
|
-
}
|
332
|
-
return value;
|
333
|
-
});
|
334
|
-
}
|
335
|
-
function parseYamlScript(content, filePath, ignoreCheckingTarget) {
|
336
|
-
const interpolatedContent = interpolateEnvVars(content);
|
337
|
-
const obj = import_js_yaml2.default.load(interpolatedContent);
|
338
|
-
const pathTip = filePath ? `, failed to load ${filePath}` : "";
|
339
|
-
const android = typeof obj.android !== "undefined" ? Object.assign({}, obj.android || {}) : void 0;
|
340
|
-
const webConfig = obj.web || obj.target;
|
341
|
-
const web = typeof webConfig !== "undefined" ? Object.assign({}, webConfig || {}) : void 0;
|
342
|
-
if (!ignoreCheckingTarget) {
|
343
|
-
(0, import_utils2.assert)(
|
344
|
-
web || android,
|
345
|
-
`at least one of "target", "web", or "android" properties is required in yaml script${pathTip}`
|
346
|
-
);
|
347
|
-
(0, import_utils2.assert)(
|
348
|
-
web && !android || !web && android,
|
349
|
-
`only one of "target", "web", or "android" properties is allowed in yaml script${pathTip}`
|
350
|
-
);
|
351
|
-
if (web || android) {
|
352
|
-
(0, import_utils2.assert)(
|
353
|
-
typeof web === "object" || typeof android === "object",
|
354
|
-
`property "target/web/android" must be an object${pathTip}`
|
355
|
-
);
|
356
|
-
}
|
357
|
-
}
|
358
|
-
(0, import_utils2.assert)(obj.tasks, `property "tasks" is required in yaml script ${pathTip}`);
|
359
|
-
(0, import_utils2.assert)(
|
360
|
-
Array.isArray(obj.tasks),
|
361
|
-
`property "tasks" must be an array in yaml script, but got ${obj.tasks}`
|
362
|
-
);
|
363
|
-
return obj;
|
364
|
-
}
|
365
|
-
// Annotate the CommonJS export names for ESM import in node:
|
366
|
-
0 && (module.exports = {
|
367
|
-
ScriptPlayer,
|
368
|
-
buildYaml,
|
369
|
-
parseYamlScript
|
370
|
-
});
|
371
|
-
|
372
|
-
//# sourceMappingURL=yaml.js.map
|