@midscene/web 0.7.2 → 0.7.3-beta-20241029030944.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.
@@ -1,1280 +1,1448 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __defProps = Object.defineProperties;
5
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
- var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
7
- var __getOwnPropNames = Object.getOwnPropertyNames;
8
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
9
- var __getProtoOf = Object.getPrototypeOf;
10
- var __hasOwnProp = Object.prototype.hasOwnProperty;
11
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
12
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
13
- var __spreadValues = (a, b) => {
14
- for (var prop in b || (b = {}))
15
- if (__hasOwnProp.call(b, prop))
16
- __defNormalProp(a, prop, b[prop]);
17
- if (__getOwnPropSymbols)
18
- for (var prop of __getOwnPropSymbols(b)) {
19
- if (__propIsEnum.call(b, prop))
20
- __defNormalProp(a, prop, b[prop]);
21
- }
22
- return a;
23
- };
24
- var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
25
- var __commonJS = (cb, mod) => function __require() {
26
- return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
27
- };
28
- var __export = (target, all) => {
29
- for (var name in all)
30
- __defProp(target, name, { get: all[name], enumerable: true });
31
- };
32
- var __copyProps = (to, from, except, desc) => {
33
- if (from && typeof from === "object" || typeof from === "function") {
34
- for (let key of __getOwnPropNames(from))
35
- if (!__hasOwnProp.call(to, key) && key !== except)
36
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
37
- }
38
- return to;
39
- };
40
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
41
- // If the importer is in node compatibility mode or this is not an ESM
42
- // file that has been converted to a CommonJS file using a Babel-
43
- // compatible transform (i.e. "__esModule" has not been set), then set
44
- // "default" to the CommonJS "module.exports" for node compatibility.
45
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
46
- mod
47
- ));
48
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
49
-
50
- // ../../node_modules/.pnpm/dayjs@1.11.11/node_modules/dayjs/dayjs.min.js
51
- var require_dayjs_min = __commonJS({
52
- "../../node_modules/.pnpm/dayjs@1.11.11/node_modules/dayjs/dayjs.min.js"(exports, module2) {
1
+ (function(global, factory) {
2
+ if (typeof module === "object" && typeof module.exports === "object") factory(exports, require("@midscene/core/utils"), require("assert"), require("@midscene/core"), require("fs"), require("path"), require("@midscene/shared/fs"), require("@midscene/shared/utils"), require("@midscene/shared/constants"), require("@midscene/shared/img"));
3
+ else if (typeof define === "function" && define.amd) define([
4
+ "exports",
5
+ "@midscene/core/utils",
6
+ "assert",
7
+ "@midscene/core",
8
+ "fs",
9
+ "path",
10
+ "@midscene/shared/fs",
11
+ "@midscene/shared/utils",
12
+ "@midscene/shared/constants",
13
+ "@midscene/shared/img"
14
+ ], factory);
15
+ else if (global = typeof globalThis !== "undefined" ? globalThis : global || self) factory(global.puppeteer = {}, global.utils, global.assert, global.core, global.fs, global.path, global.fs, global.utils, global.constants, global.img);
16
+ })(this, function(exports, _utils, _assert, _core, _fs, _path, _fs1, _utils1, _constants, _img) {
53
17
  "use strict";
54
- !function(t, e) {
55
- "object" == typeof exports && "undefined" != typeof module2 ? module2.exports = e() : "function" == typeof define && define.amd ? define(e) : (t = "undefined" != typeof globalThis ? globalThis : t || self).dayjs = e();
56
- }(exports, function() {
57
- "use strict";
58
- var t = 1e3, e = 6e4, n = 36e5, r = "millisecond", i = "second", s = "minute", u = "hour", a = "day", o = "week", c = "month", f = "quarter", h = "year", d = "date", l = "Invalid Date", $ = /^(\d{4})[-/]?(\d{1,2})?[-/]?(\d{0,2})[Tt\s]*(\d{1,2})?:?(\d{1,2})?:?(\d{1,2})?[.:]?(\d+)?$/, y = /\[([^\]]+)]|Y{1,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g, M = { name: "en", weekdays: "Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday".split("_"), months: "January_February_March_April_May_June_July_August_September_October_November_December".split("_"), ordinal: function(t2) {
59
- var e2 = ["th", "st", "nd", "rd"], n2 = t2 % 100;
60
- return "[" + t2 + (e2[(n2 - 20) % 10] || e2[n2] || e2[0]) + "]";
61
- } }, m = function(t2, e2, n2) {
62
- var r2 = String(t2);
63
- return !r2 || r2.length >= e2 ? t2 : "" + Array(e2 + 1 - r2.length).join(n2) + t2;
64
- }, v = { s: m, z: function(t2) {
65
- var e2 = -t2.utcOffset(), n2 = Math.abs(e2), r2 = Math.floor(n2 / 60), i2 = n2 % 60;
66
- return (e2 <= 0 ? "+" : "-") + m(r2, 2, "0") + ":" + m(i2, 2, "0");
67
- }, m: function t2(e2, n2) {
68
- if (e2.date() < n2.date())
69
- return -t2(n2, e2);
70
- var r2 = 12 * (n2.year() - e2.year()) + (n2.month() - e2.month()), i2 = e2.clone().add(r2, c), s2 = n2 - i2 < 0, u2 = e2.clone().add(r2 + (s2 ? -1 : 1), c);
71
- return +(-(r2 + (n2 - i2) / (s2 ? i2 - u2 : u2 - i2)) || 0);
72
- }, a: function(t2) {
73
- return t2 < 0 ? Math.ceil(t2) || 0 : Math.floor(t2);
74
- }, p: function(t2) {
75
- return { M: c, y: h, w: o, d: a, D: d, h: u, m: s, s: i, ms: r, Q: f }[t2] || String(t2 || "").toLowerCase().replace(/s$/, "");
76
- }, u: function(t2) {
77
- return void 0 === t2;
78
- } }, g = "en", D = {};
79
- D[g] = M;
80
- var p = "$isDayjsObject", S = function(t2) {
81
- return t2 instanceof _ || !(!t2 || !t2[p]);
82
- }, w = function t2(e2, n2, r2) {
83
- var i2;
84
- if (!e2)
85
- return g;
86
- if ("string" == typeof e2) {
87
- var s2 = e2.toLowerCase();
88
- D[s2] && (i2 = s2), n2 && (D[s2] = n2, i2 = s2);
89
- var u2 = e2.split("-");
90
- if (!i2 && u2.length > 1)
91
- return t2(u2[0]);
92
- } else {
93
- var a2 = e2.name;
94
- D[a2] = e2, i2 = a2;
95
- }
96
- return !r2 && i2 && (g = i2), i2 || !r2 && g;
97
- }, O = function(t2, e2) {
98
- if (S(t2))
99
- return t2.clone();
100
- var n2 = "object" == typeof e2 ? e2 : {};
101
- return n2.date = t2, n2.args = arguments, new _(n2);
102
- }, b = v;
103
- b.l = w, b.i = S, b.w = function(t2, e2) {
104
- return O(t2, { locale: e2.$L, utc: e2.$u, x: e2.$x, $offset: e2.$offset });
105
- };
106
- var _ = function() {
107
- function M2(t2) {
108
- this.$L = w(t2.locale, null, true), this.parse(t2), this.$x = this.$x || t2.x || {}, this[p] = true;
109
- }
110
- var m2 = M2.prototype;
111
- return m2.parse = function(t2) {
112
- this.$d = function(t3) {
113
- var e2 = t3.date, n2 = t3.utc;
114
- if (null === e2)
115
- return /* @__PURE__ */ new Date(NaN);
116
- if (b.u(e2))
117
- return /* @__PURE__ */ new Date();
118
- if (e2 instanceof Date)
119
- return new Date(e2);
120
- if ("string" == typeof e2 && !/Z$/i.test(e2)) {
121
- var r2 = e2.match($);
122
- if (r2) {
123
- var i2 = r2[2] - 1 || 0, s2 = (r2[7] || "0").substring(0, 3);
124
- return n2 ? new Date(Date.UTC(r2[1], i2, r2[3] || 1, r2[4] || 0, r2[5] || 0, r2[6] || 0, s2)) : new Date(r2[1], i2, r2[3] || 1, r2[4] || 0, r2[5] || 0, r2[6] || 0, s2);
125
- }
126
- }
127
- return new Date(e2);
128
- }(t2), this.init();
129
- }, m2.init = function() {
130
- var t2 = this.$d;
131
- this.$y = t2.getFullYear(), this.$M = t2.getMonth(), this.$D = t2.getDate(), this.$W = t2.getDay(), this.$H = t2.getHours(), this.$m = t2.getMinutes(), this.$s = t2.getSeconds(), this.$ms = t2.getMilliseconds();
132
- }, m2.$utils = function() {
133
- return b;
134
- }, m2.isValid = function() {
135
- return !(this.$d.toString() === l);
136
- }, m2.isSame = function(t2, e2) {
137
- var n2 = O(t2);
138
- return this.startOf(e2) <= n2 && n2 <= this.endOf(e2);
139
- }, m2.isAfter = function(t2, e2) {
140
- return O(t2) < this.startOf(e2);
141
- }, m2.isBefore = function(t2, e2) {
142
- return this.endOf(e2) < O(t2);
143
- }, m2.$g = function(t2, e2, n2) {
144
- return b.u(t2) ? this[e2] : this.set(n2, t2);
145
- }, m2.unix = function() {
146
- return Math.floor(this.valueOf() / 1e3);
147
- }, m2.valueOf = function() {
148
- return this.$d.getTime();
149
- }, m2.startOf = function(t2, e2) {
150
- var n2 = this, r2 = !!b.u(e2) || e2, f2 = b.p(t2), l2 = function(t3, e3) {
151
- var i2 = b.w(n2.$u ? Date.UTC(n2.$y, e3, t3) : new Date(n2.$y, e3, t3), n2);
152
- return r2 ? i2 : i2.endOf(a);
153
- }, $2 = function(t3, e3) {
154
- return b.w(n2.toDate()[t3].apply(n2.toDate("s"), (r2 ? [0, 0, 0, 0] : [23, 59, 59, 999]).slice(e3)), n2);
155
- }, y2 = this.$W, M3 = this.$M, m3 = this.$D, v2 = "set" + (this.$u ? "UTC" : "");
156
- switch (f2) {
157
- case h:
158
- return r2 ? l2(1, 0) : l2(31, 11);
159
- case c:
160
- return r2 ? l2(1, M3) : l2(0, M3 + 1);
161
- case o:
162
- var g2 = this.$locale().weekStart || 0, D2 = (y2 < g2 ? y2 + 7 : y2) - g2;
163
- return l2(r2 ? m3 - D2 : m3 + (6 - D2), M3);
164
- case a:
165
- case d:
166
- return $2(v2 + "Hours", 0);
167
- case u:
168
- return $2(v2 + "Minutes", 1);
169
- case s:
170
- return $2(v2 + "Seconds", 2);
171
- case i:
172
- return $2(v2 + "Milliseconds", 3);
173
- default:
174
- return this.clone();
175
- }
176
- }, m2.endOf = function(t2) {
177
- return this.startOf(t2, false);
178
- }, m2.$set = function(t2, e2) {
179
- var n2, o2 = b.p(t2), f2 = "set" + (this.$u ? "UTC" : ""), l2 = (n2 = {}, n2[a] = f2 + "Date", n2[d] = f2 + "Date", n2[c] = f2 + "Month", n2[h] = f2 + "FullYear", n2[u] = f2 + "Hours", n2[s] = f2 + "Minutes", n2[i] = f2 + "Seconds", n2[r] = f2 + "Milliseconds", n2)[o2], $2 = o2 === a ? this.$D + (e2 - this.$W) : e2;
180
- if (o2 === c || o2 === h) {
181
- var y2 = this.clone().set(d, 1);
182
- y2.$d[l2]($2), y2.init(), this.$d = y2.set(d, Math.min(this.$D, y2.daysInMonth())).$d;
183
- } else
184
- l2 && this.$d[l2]($2);
185
- return this.init(), this;
186
- }, m2.set = function(t2, e2) {
187
- return this.clone().$set(t2, e2);
188
- }, m2.get = function(t2) {
189
- return this[b.p(t2)]();
190
- }, m2.add = function(r2, f2) {
191
- var d2, l2 = this;
192
- r2 = Number(r2);
193
- var $2 = b.p(f2), y2 = function(t2) {
194
- var e2 = O(l2);
195
- return b.w(e2.date(e2.date() + Math.round(t2 * r2)), l2);
196
- };
197
- if ($2 === c)
198
- return this.set(c, this.$M + r2);
199
- if ($2 === h)
200
- return this.set(h, this.$y + r2);
201
- if ($2 === a)
202
- return y2(1);
203
- if ($2 === o)
204
- return y2(7);
205
- var M3 = (d2 = {}, d2[s] = e, d2[u] = n, d2[i] = t, d2)[$2] || 1, m3 = this.$d.getTime() + r2 * M3;
206
- return b.w(m3, this);
207
- }, m2.subtract = function(t2, e2) {
208
- return this.add(-1 * t2, e2);
209
- }, m2.format = function(t2) {
210
- var e2 = this, n2 = this.$locale();
211
- if (!this.isValid())
212
- return n2.invalidDate || l;
213
- var r2 = t2 || "YYYY-MM-DDTHH:mm:ssZ", i2 = b.z(this), s2 = this.$H, u2 = this.$m, a2 = this.$M, o2 = n2.weekdays, c2 = n2.months, f2 = n2.meridiem, h2 = function(t3, n3, i3, s3) {
214
- return t3 && (t3[n3] || t3(e2, r2)) || i3[n3].slice(0, s3);
215
- }, d2 = function(t3) {
216
- return b.s(s2 % 12 || 12, t3, "0");
217
- }, $2 = f2 || function(t3, e3, n3) {
218
- var r3 = t3 < 12 ? "AM" : "PM";
219
- return n3 ? r3.toLowerCase() : r3;
220
- };
221
- return r2.replace(y, function(t3, r3) {
222
- return r3 || function(t4) {
223
- switch (t4) {
224
- case "YY":
225
- return String(e2.$y).slice(-2);
226
- case "YYYY":
227
- return b.s(e2.$y, 4, "0");
228
- case "M":
229
- return a2 + 1;
230
- case "MM":
231
- return b.s(a2 + 1, 2, "0");
232
- case "MMM":
233
- return h2(n2.monthsShort, a2, c2, 3);
234
- case "MMMM":
235
- return h2(c2, a2);
236
- case "D":
237
- return e2.$D;
238
- case "DD":
239
- return b.s(e2.$D, 2, "0");
240
- case "d":
241
- return String(e2.$W);
242
- case "dd":
243
- return h2(n2.weekdaysMin, e2.$W, o2, 2);
244
- case "ddd":
245
- return h2(n2.weekdaysShort, e2.$W, o2, 3);
246
- case "dddd":
247
- return o2[e2.$W];
248
- case "H":
249
- return String(s2);
250
- case "HH":
251
- return b.s(s2, 2, "0");
252
- case "h":
253
- return d2(1);
254
- case "hh":
255
- return d2(2);
256
- case "a":
257
- return $2(s2, u2, true);
258
- case "A":
259
- return $2(s2, u2, false);
260
- case "m":
261
- return String(u2);
262
- case "mm":
263
- return b.s(u2, 2, "0");
264
- case "s":
265
- return String(e2.$s);
266
- case "ss":
267
- return b.s(e2.$s, 2, "0");
268
- case "SSS":
269
- return b.s(e2.$ms, 3, "0");
270
- case "Z":
271
- return i2;
272
- }
273
- return null;
274
- }(t3) || i2.replace(":", "");
275
- });
276
- }, m2.utcOffset = function() {
277
- return 15 * -Math.round(this.$d.getTimezoneOffset() / 15);
278
- }, m2.diff = function(r2, d2, l2) {
279
- var $2, y2 = this, M3 = b.p(d2), m3 = O(r2), v2 = (m3.utcOffset() - this.utcOffset()) * e, g2 = this - m3, D2 = function() {
280
- return b.m(y2, m3);
281
- };
282
- switch (M3) {
283
- case h:
284
- $2 = D2() / 12;
285
- break;
286
- case c:
287
- $2 = D2();
288
- break;
289
- case f:
290
- $2 = D2() / 3;
291
- break;
292
- case o:
293
- $2 = (g2 - v2) / 6048e5;
294
- break;
295
- case a:
296
- $2 = (g2 - v2) / 864e5;
297
- break;
298
- case u:
299
- $2 = g2 / n;
300
- break;
301
- case s:
302
- $2 = g2 / e;
303
- break;
304
- case i:
305
- $2 = g2 / t;
306
- break;
307
- default:
308
- $2 = g2;
309
- }
310
- return l2 ? $2 : b.a($2);
311
- }, m2.daysInMonth = function() {
312
- return this.endOf(c).$D;
313
- }, m2.$locale = function() {
314
- return D[this.$L];
315
- }, m2.locale = function(t2, e2) {
316
- if (!t2)
317
- return this.$L;
318
- var n2 = this.clone(), r2 = w(t2, e2, true);
319
- return r2 && (n2.$L = r2), n2;
320
- }, m2.clone = function() {
321
- return b.w(this.$d, this);
322
- }, m2.toDate = function() {
323
- return new Date(this.valueOf());
324
- }, m2.toJSON = function() {
325
- return this.isValid() ? this.toISOString() : null;
326
- }, m2.toISOString = function() {
327
- return this.$d.toISOString();
328
- }, m2.toString = function() {
329
- return this.$d.toUTCString();
330
- }, M2;
331
- }(), k = _.prototype;
332
- return O.prototype = k, [["$ms", r], ["$s", i], ["$m", s], ["$H", u], ["$W", a], ["$M", c], ["$y", h], ["$D", d]].forEach(function(t2) {
333
- k[t2[1]] = function(e2) {
334
- return this.$g(e2, t2[0], t2[1]);
335
- };
336
- }), O.extend = function(t2, e2) {
337
- return t2.$i || (t2(e2, _, O), t2.$i = true), O;
338
- }, O.locale = w, O.isDayjs = S, O.unix = function(t2) {
339
- return O(1e3 * t2);
340
- }, O.en = D[g], O.Ls = D, O.p = {}, O;
18
+ Object.defineProperty(exports, "__esModule", {
19
+ value: true
341
20
  });
342
- }
343
- });
344
-
345
- // src/puppeteer/index.ts
346
- var puppeteer_exports = {};
347
- __export(puppeteer_exports, {
348
- PuppeteerAgent: () => PuppeteerAgent,
349
- PuppeteerWebPage: () => WebPage
350
- });
351
- module.exports = __toCommonJS(puppeteer_exports);
352
-
353
- // src/common/agent.ts
354
- var import_utils5 = require("@midscene/core/utils");
355
-
356
- // src/common/tasks.ts
357
- var import_node_assert2 = __toESM(require("assert"));
358
- var import_core = require("@midscene/core");
359
- var import_utils3 = require("@midscene/core/utils");
360
- var import_img3 = require("@midscene/shared/img");
361
-
362
- // src/common/task-cache.ts
363
- var import_node_fs2 = require("fs");
364
- var import_node_path2 = require("path");
365
- var import_utils = require("@midscene/core/utils");
366
- var import_fs2 = require("@midscene/shared/fs");
367
-
368
- // src/common/utils.ts
369
- var import_node_assert = __toESM(require("assert"));
370
- var import_node_crypto = require("crypto");
371
- var import_node_fs = require("fs");
372
- var import_node_path = __toESM(require("path"));
373
- var import_constants = require("@midscene/shared/constants");
374
- var import_fs = require("@midscene/shared/fs");
375
- var import_img = require("@midscene/shared/img");
376
- var import_img2 = require("@midscene/shared/img");
377
- var import_dayjs = __toESM(require_dayjs_min());
378
-
379
- // src/web-element.ts
380
- var WebElementInfo = class {
381
- constructor({
382
- content,
383
- rect,
384
- page,
385
- locator,
386
- id,
387
- attributes,
388
- indexId
389
- }) {
390
- this.content = content;
391
- this.rect = rect;
392
- this.center = [
393
- Math.floor(rect.left + rect.width / 2),
394
- Math.floor(rect.top + rect.height / 2)
395
- ];
396
- this.page = page;
397
- this.locator = locator;
398
- this.id = id;
399
- this.attributes = attributes;
400
- this.indexId = indexId;
401
- }
402
- };
403
-
404
- // src/common/utils.ts
405
- async function parseContextFromWebPage(page, _opt) {
406
- (0, import_node_assert.default)(page, "page is required");
407
- if (page._forceUsePageContext) {
408
- return await page._forceUsePageContext();
409
- }
410
- const url = page.url();
411
- const file = await page.screenshot();
412
- const screenshotBase64 = (0, import_img.base64Encoded)(file);
413
- const captureElementSnapshot = await page.getElementInfos();
414
- const elementsInfo = await alignElements(captureElementSnapshot, page);
415
- const elementsPositionInfoWithoutText = elementsInfo.filter((elementInfo) => {
416
- if (elementInfo.attributes.nodeType === import_constants.NodeType.TEXT) {
417
- return false;
21
+ function _export(target, all) {
22
+ for(var name in all)Object.defineProperty(target, name, {
23
+ enumerable: true,
24
+ get: all[name]
25
+ });
418
26
  }
419
- return true;
420
- });
421
- const size = await (0, import_img.imageInfoOfBase64)(screenshotBase64);
422
- const screenshotBase64WithElementMarker = await (0, import_img2.compositeElementInfoImg)({
423
- inputImgBase64: screenshotBase64.split(";base64,").pop(),
424
- elementsPositionInfo: elementsPositionInfoWithoutText
425
- });
426
- return {
427
- content: elementsInfo,
428
- size,
429
- screenshotBase64,
430
- screenshotBase64WithElementMarker: `data:image/png;base64,${screenshotBase64WithElementMarker}`,
431
- url
432
- };
433
- }
434
- async function getExtraReturnLogic() {
435
- const pathDir = (0, import_fs.findNearestPackageJson)(__dirname);
436
- (0, import_node_assert.default)(pathDir, `can't find pathDir, with ${__dirname}`);
437
- const scriptPath = import_node_path.default.join(pathDir, "./dist/script/htmlElement.js");
438
- const elementInfosScriptContent = (0, import_node_fs.readFileSync)(scriptPath, "utf-8");
439
- return `${elementInfosScriptContent}midscene_element_inspector.webExtractTextWithPosition()`;
440
- }
441
- var sizeThreshold = 3;
442
- async function alignElements(elements, page) {
443
- const validElements = elements.filter((item) => {
444
- return item.rect.height >= sizeThreshold && item.rect.width >= sizeThreshold;
445
- });
446
- const textsAligned = [];
447
- for (const item of validElements) {
448
- const { rect, id, content, attributes, locator, indexId } = item;
449
- textsAligned.push(
450
- new WebElementInfo({
451
- rect,
452
- locator,
453
- id,
454
- content,
455
- attributes,
456
- page,
457
- indexId
458
- })
459
- );
460
- }
461
- return textsAligned;
462
- }
463
- function reportFileName(tag = "web") {
464
- const dateTimeInFileName = (0, import_dayjs.default)().format("YYYY-MM-DD_HH-mm-ss-SSS");
465
- return `${tag}-${dateTimeInFileName}`;
466
- }
467
- function printReportMsg(filepath) {
468
- console.log("Midscene - report file updated:", filepath);
469
- }
470
- function getCurrentExecutionFile(trace) {
471
- const error = new Error();
472
- const stackTrace = trace || error.stack;
473
- const pkgDir = process.cwd() || "";
474
- if (stackTrace) {
475
- const stackLines = stackTrace.split("\n");
476
- for (const line of stackLines) {
477
- if (line.includes(".spec.") || line.includes(".test.") || line.includes(".ts") || line.includes(".js")) {
478
- const match = line.match(/(?:at\s+)?(.*?\.(?:spec|test)\.[jt]s)/);
479
- if (match == null ? void 0 : match[1]) {
480
- const targetFileName = match[1].replace(pkgDir, "").trim().replace("at ", "");
481
- return targetFileName;
27
+ _export(exports, {
28
+ PuppeteerAgent: function() {
29
+ return PuppeteerAgent;
30
+ },
31
+ PuppeteerWebPage: function() {
32
+ return WebPage;
482
33
  }
483
- }
34
+ });
35
+ _assert = /*#__PURE__*/ _interop_require_default(_assert);
36
+ _path = /*#__PURE__*/ _interop_require_wildcard(_path);
37
+ function _interop_require_default(obj) {
38
+ return obj && obj.__esModule ? obj : {
39
+ default: obj
40
+ };
484
41
  }
485
- }
486
- return false;
487
- }
488
- var testFileIndex = /* @__PURE__ */ new Map();
489
- function generateCacheId(fileName) {
490
- let taskFile = fileName || getCurrentExecutionFile();
491
- if (!taskFile) {
492
- taskFile = (0, import_node_crypto.randomUUID)();
493
- console.warn(
494
- "Midscene - using random UUID for cache id. Cache may be invalid."
495
- );
496
- }
497
- if (testFileIndex.has(taskFile)) {
498
- const currentIndex = testFileIndex.get(taskFile);
499
- if (currentIndex !== void 0) {
500
- testFileIndex.set(taskFile, currentIndex + 1);
42
+ function _getRequireWildcardCache(nodeInterop) {
43
+ if (typeof WeakMap !== "function") return null;
44
+ var cacheBabelInterop = new WeakMap();
45
+ var cacheNodeInterop = new WeakMap();
46
+ return (_getRequireWildcardCache = function(nodeInterop) {
47
+ return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
48
+ })(nodeInterop);
49
+ }
50
+ function _interop_require_wildcard(obj, nodeInterop) {
51
+ if (!nodeInterop && obj && obj.__esModule) {
52
+ return obj;
53
+ }
54
+ if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
55
+ return {
56
+ default: obj
57
+ };
58
+ }
59
+ var cache = _getRequireWildcardCache(nodeInterop);
60
+ if (cache && cache.has(obj)) {
61
+ return cache.get(obj);
62
+ }
63
+ var newObj = {
64
+ __proto__: null
65
+ };
66
+ var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
67
+ for(var key in obj){
68
+ if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
69
+ var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
70
+ if (desc && (desc.get || desc.set)) {
71
+ Object.defineProperty(newObj, key, desc);
72
+ } else {
73
+ newObj[key] = obj[key];
74
+ }
75
+ }
76
+ }
77
+ newObj.default = obj;
78
+ if (cache) {
79
+ cache.set(obj, newObj);
80
+ }
81
+ return newObj;
501
82
  }
502
- } else {
503
- testFileIndex.set(taskFile, 1);
504
- }
505
- return `${taskFile}-${testFileIndex.get(taskFile)}`;
506
- }
507
-
508
- // src/common/task-cache.ts
509
- var TaskCache = class {
510
- constructor(opts) {
511
- this.midscenePkgInfo = (0, import_fs2.getMidscenePkgInfo)(__dirname);
512
- this.cacheId = generateCacheId(opts == null ? void 0 : opts.fileName);
513
- this.cache = this.readCacheFromFile() || {
514
- aiTasks: []
83
+ var __create = Object.create;
84
+ var __defProp = Object.defineProperty;
85
+ var __defProps = Object.defineProperties;
86
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
87
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
88
+ var __getOwnPropNames = Object.getOwnPropertyNames;
89
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
90
+ var __getProtoOf = Object.getPrototypeOf;
91
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
92
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
93
+ var __defNormalProp = (obj, key, value)=>key in obj ? __defProp(obj, key, {
94
+ enumerable: true,
95
+ configurable: true,
96
+ writable: true,
97
+ value
98
+ }) : obj[key] = value;
99
+ var __spreadValues = (a, b)=>{
100
+ for(var prop in b || (b = {}))if (__hasOwnProp.call(b, prop)) __defNormalProp(a, prop, b[prop]);
101
+ if (__getOwnPropSymbols) for (var prop of __getOwnPropSymbols(b)){
102
+ if (__propIsEnum.call(b, prop)) __defNormalProp(a, prop, b[prop]);
103
+ }
104
+ return a;
515
105
  };
516
- this.newCache = {
517
- aiTasks: []
106
+ var __spreadProps = (a, b)=>__defProps(a, __getOwnPropDescs(b));
107
+ var __commonJS = (cb, mod)=>function __require() {
108
+ return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = {
109
+ exports: {}
110
+ }).exports, mod), mod.exports;
111
+ };
112
+ var __copyProps = (to, from, except, desc)=>{
113
+ if (from && typeof from === "object" || typeof from === "function") {
114
+ for (let key of __getOwnPropNames(from))if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
115
+ get: ()=>from[key],
116
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
117
+ });
118
+ }
119
+ return to;
518
120
  };
519
- }
520
- getCacheGroupByPrompt(aiActionPrompt) {
521
- const { aiTasks = [] } = this.cache || { aiTasks: [] };
522
- const index = aiTasks.findIndex((item) => item.prompt === aiActionPrompt);
523
- const newCacheGroup = [];
524
- this.newCache.aiTasks.push({
525
- prompt: aiActionPrompt,
526
- tasks: newCacheGroup
527
- });
528
- return {
529
- readCache: (pageContext, type, actionPrompt) => {
530
- if (index === -1) {
531
- return false;
121
+ var __toESM = (mod, isNodeMode, target)=>(target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(// If the importer is in node compatibility mode or this is not an ESM
122
+ // file that has been converted to a CommonJS file using a Babel-
123
+ // compatible transform (i.e. "__esModule" has not been set), then set
124
+ // "default" to the CommonJS "module.exports" for node compatibility.
125
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
126
+ value: mod,
127
+ enumerable: true
128
+ }) : target, mod));
129
+ var __async = (__this, __arguments, generator)=>{
130
+ return new Promise((resolve, reject)=>{
131
+ var fulfilled = (value)=>{
132
+ try {
133
+ step(generator.next(value));
134
+ } catch (e) {
135
+ reject(e);
136
+ }
137
+ };
138
+ var rejected = (value)=>{
139
+ try {
140
+ step(generator.throw(value));
141
+ } catch (e) {
142
+ reject(e);
143
+ }
144
+ };
145
+ var step = (x)=>x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
146
+ step((generator = generator.apply(__this, __arguments)).next());
147
+ });
148
+ };
149
+ // ../../node_modules/.pnpm/dayjs@1.11.11/node_modules/dayjs/dayjs.min.js
150
+ var require_dayjs_min = __commonJS({
151
+ "../../node_modules/.pnpm/dayjs@1.11.11/node_modules/dayjs/dayjs.min.js" (exports, module1) {
152
+ "use strict";
153
+ !function(t, e) {
154
+ "object" == typeof exports && "undefined" != typeof module1 ? module1.exports = e() : "function" == typeof define && define.amd ? define(e) : (t = "undefined" != typeof globalThis ? globalThis : t || self).dayjs = e();
155
+ }(exports, function() {
156
+ "use strict";
157
+ var t = 1e3, e = 6e4, n = 36e5, r = "millisecond", i = "second", s = "minute", u = "hour", a = "day", o = "week", c = "month", f = "quarter", h = "year", d = "date", l = "Invalid Date", $ = /^(\d{4})[-/]?(\d{1,2})?[-/]?(\d{0,2})[Tt\s]*(\d{1,2})?:?(\d{1,2})?:?(\d{1,2})?[.:]?(\d+)?$/, y = /\[([^\]]+)]|Y{1,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g, M = {
158
+ name: "en",
159
+ weekdays: "Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday".split("_"),
160
+ months: "January_February_March_April_May_June_July_August_September_October_November_December".split("_"),
161
+ ordinal: function(t2) {
162
+ var e2 = [
163
+ "th",
164
+ "st",
165
+ "nd",
166
+ "rd"
167
+ ], n2 = t2 % 100;
168
+ return "[" + t2 + (e2[(n2 - 20) % 10] || e2[n2] || e2[0]) + "]";
169
+ }
170
+ }, m = function(t2, e2, n2) {
171
+ var r2 = String(t2);
172
+ return !r2 || r2.length >= e2 ? t2 : "" + Array(e2 + 1 - r2.length).join(n2) + t2;
173
+ }, v = {
174
+ s: m,
175
+ z: function(t2) {
176
+ var e2 = -t2.utcOffset(), n2 = Math.abs(e2), r2 = Math.floor(n2 / 60), i2 = n2 % 60;
177
+ return (e2 <= 0 ? "+" : "-") + m(r2, 2, "0") + ":" + m(i2, 2, "0");
178
+ },
179
+ m: function t2(e2, n2) {
180
+ if (e2.date() < n2.date()) return -t2(n2, e2);
181
+ var r2 = 12 * (n2.year() - e2.year()) + (n2.month() - e2.month()), i2 = e2.clone().add(r2, c), s2 = n2 - i2 < 0, u2 = e2.clone().add(r2 + (s2 ? -1 : 1), c);
182
+ return +(-(r2 + (n2 - i2) / (s2 ? i2 - u2 : u2 - i2)) || 0);
183
+ },
184
+ a: function(t2) {
185
+ return t2 < 0 ? Math.ceil(t2) || 0 : Math.floor(t2);
186
+ },
187
+ p: function(t2) {
188
+ return ({
189
+ M: c,
190
+ y: h,
191
+ w: o,
192
+ d: a,
193
+ D: d,
194
+ h: u,
195
+ m: s,
196
+ s: i,
197
+ ms: r,
198
+ Q: f
199
+ })[t2] || String(t2 || "").toLowerCase().replace(/s$/, "");
200
+ },
201
+ u: function(t2) {
202
+ return void 0 === t2;
203
+ }
204
+ }, g = "en", D = {};
205
+ D[g] = M;
206
+ var p = "$isDayjsObject", S = function(t2) {
207
+ return t2 instanceof _ || !(!t2 || !t2[p]);
208
+ }, w = function t2(e2, n2, r2) {
209
+ var i2;
210
+ if (!e2) return g;
211
+ if ("string" == typeof e2) {
212
+ var s2 = e2.toLowerCase();
213
+ D[s2] && (i2 = s2), n2 && (D[s2] = n2, i2 = s2);
214
+ var u2 = e2.split("-");
215
+ if (!i2 && u2.length > 1) return t2(u2[0]);
216
+ } else {
217
+ var a2 = e2.name;
218
+ D[a2] = e2, i2 = a2;
219
+ }
220
+ return !r2 && i2 && (g = i2), i2 || !r2 && g;
221
+ }, O = function(t2, e2) {
222
+ if (S(t2)) return t2.clone();
223
+ var n2 = "object" == typeof e2 ? e2 : {};
224
+ return n2.date = t2, n2.args = arguments, new _(n2);
225
+ }, b = v;
226
+ b.l = w, b.i = S, b.w = function(t2, e2) {
227
+ return O(t2, {
228
+ locale: e2.$L,
229
+ utc: e2.$u,
230
+ x: e2.$x,
231
+ $offset: e2.$offset
232
+ });
233
+ };
234
+ var _ = function() {
235
+ function M2(t2) {
236
+ this.$L = w(t2.locale, null, true), this.parse(t2), this.$x = this.$x || t2.x || {}, this[p] = true;
237
+ }
238
+ var m2 = M2.prototype;
239
+ return m2.parse = function(t2) {
240
+ this.$d = function(t3) {
241
+ var e2 = t3.date, n2 = t3.utc;
242
+ if (null === e2) return /* @__PURE__ */ new Date(NaN);
243
+ if (b.u(e2)) return /* @__PURE__ */ new Date();
244
+ if (e2 instanceof Date) return new Date(e2);
245
+ if ("string" == typeof e2 && !/Z$/i.test(e2)) {
246
+ var r2 = e2.match($);
247
+ if (r2) {
248
+ var i2 = r2[2] - 1 || 0, s2 = (r2[7] || "0").substring(0, 3);
249
+ return n2 ? new Date(Date.UTC(r2[1], i2, r2[3] || 1, r2[4] || 0, r2[5] || 0, r2[6] || 0, s2)) : new Date(r2[1], i2, r2[3] || 1, r2[4] || 0, r2[5] || 0, r2[6] || 0, s2);
250
+ }
251
+ }
252
+ return new Date(e2);
253
+ }(t2), this.init();
254
+ }, m2.init = function() {
255
+ var t2 = this.$d;
256
+ this.$y = t2.getFullYear(), this.$M = t2.getMonth(), this.$D = t2.getDate(), this.$W = t2.getDay(), this.$H = t2.getHours(), this.$m = t2.getMinutes(), this.$s = t2.getSeconds(), this.$ms = t2.getMilliseconds();
257
+ }, m2.$utils = function() {
258
+ return b;
259
+ }, m2.isValid = function() {
260
+ return !(this.$d.toString() === l);
261
+ }, m2.isSame = function(t2, e2) {
262
+ var n2 = O(t2);
263
+ return this.startOf(e2) <= n2 && n2 <= this.endOf(e2);
264
+ }, m2.isAfter = function(t2, e2) {
265
+ return O(t2) < this.startOf(e2);
266
+ }, m2.isBefore = function(t2, e2) {
267
+ return this.endOf(e2) < O(t2);
268
+ }, m2.$g = function(t2, e2, n2) {
269
+ return b.u(t2) ? this[e2] : this.set(n2, t2);
270
+ }, m2.unix = function() {
271
+ return Math.floor(this.valueOf() / 1e3);
272
+ }, m2.valueOf = function() {
273
+ return this.$d.getTime();
274
+ }, m2.startOf = function(t2, e2) {
275
+ var n2 = this, r2 = !!b.u(e2) || e2, f2 = b.p(t2), l2 = function(t3, e3) {
276
+ var i2 = b.w(n2.$u ? Date.UTC(n2.$y, e3, t3) : new Date(n2.$y, e3, t3), n2);
277
+ return r2 ? i2 : i2.endOf(a);
278
+ }, $2 = function(t3, e3) {
279
+ return b.w(n2.toDate()[t3].apply(n2.toDate("s"), (r2 ? [
280
+ 0,
281
+ 0,
282
+ 0,
283
+ 0
284
+ ] : [
285
+ 23,
286
+ 59,
287
+ 59,
288
+ 999
289
+ ]).slice(e3)), n2);
290
+ }, y2 = this.$W, M3 = this.$M, m3 = this.$D, v2 = "set" + (this.$u ? "UTC" : "");
291
+ switch(f2){
292
+ case h:
293
+ return r2 ? l2(1, 0) : l2(31, 11);
294
+ case c:
295
+ return r2 ? l2(1, M3) : l2(0, M3 + 1);
296
+ case o:
297
+ var g2 = this.$locale().weekStart || 0, D2 = (y2 < g2 ? y2 + 7 : y2) - g2;
298
+ return l2(r2 ? m3 - D2 : m3 + (6 - D2), M3);
299
+ case a:
300
+ case d:
301
+ return $2(v2 + "Hours", 0);
302
+ case u:
303
+ return $2(v2 + "Minutes", 1);
304
+ case s:
305
+ return $2(v2 + "Seconds", 2);
306
+ case i:
307
+ return $2(v2 + "Milliseconds", 3);
308
+ default:
309
+ return this.clone();
310
+ }
311
+ }, m2.endOf = function(t2) {
312
+ return this.startOf(t2, false);
313
+ }, m2.$set = function(t2, e2) {
314
+ var n2, o2 = b.p(t2), f2 = "set" + (this.$u ? "UTC" : ""), l2 = (n2 = {}, n2[a] = f2 + "Date", n2[d] = f2 + "Date", n2[c] = f2 + "Month", n2[h] = f2 + "FullYear", n2[u] = f2 + "Hours", n2[s] = f2 + "Minutes", n2[i] = f2 + "Seconds", n2[r] = f2 + "Milliseconds", n2)[o2], $2 = o2 === a ? this.$D + (e2 - this.$W) : e2;
315
+ if (o2 === c || o2 === h) {
316
+ var y2 = this.clone().set(d, 1);
317
+ y2.$d[l2]($2), y2.init(), this.$d = y2.set(d, Math.min(this.$D, y2.daysInMonth())).$d;
318
+ } else l2 && this.$d[l2]($2);
319
+ return this.init(), this;
320
+ }, m2.set = function(t2, e2) {
321
+ return this.clone().$set(t2, e2);
322
+ }, m2.get = function(t2) {
323
+ return this[b.p(t2)]();
324
+ }, m2.add = function(r2, f2) {
325
+ var d2, l2 = this;
326
+ r2 = Number(r2);
327
+ var $2 = b.p(f2), y2 = function(t2) {
328
+ var e2 = O(l2);
329
+ return b.w(e2.date(e2.date() + Math.round(t2 * r2)), l2);
330
+ };
331
+ if ($2 === c) return this.set(c, this.$M + r2);
332
+ if ($2 === h) return this.set(h, this.$y + r2);
333
+ if ($2 === a) return y2(1);
334
+ if ($2 === o) return y2(7);
335
+ var M3 = (d2 = {}, d2[s] = e, d2[u] = n, d2[i] = t, d2)[$2] || 1, m3 = this.$d.getTime() + r2 * M3;
336
+ return b.w(m3, this);
337
+ }, m2.subtract = function(t2, e2) {
338
+ return this.add(-1 * t2, e2);
339
+ }, m2.format = function(t2) {
340
+ var e2 = this, n2 = this.$locale();
341
+ if (!this.isValid()) return n2.invalidDate || l;
342
+ var r2 = t2 || "YYYY-MM-DDTHH:mm:ssZ", i2 = b.z(this), s2 = this.$H, u2 = this.$m, a2 = this.$M, o2 = n2.weekdays, c2 = n2.months, f2 = n2.meridiem, h2 = function(t3, n3, i3, s3) {
343
+ return t3 && (t3[n3] || t3(e2, r2)) || i3[n3].slice(0, s3);
344
+ }, d2 = function(t3) {
345
+ return b.s(s2 % 12 || 12, t3, "0");
346
+ }, $2 = f2 || function(t3, e3, n3) {
347
+ var r3 = t3 < 12 ? "AM" : "PM";
348
+ return n3 ? r3.toLowerCase() : r3;
349
+ };
350
+ return r2.replace(y, function(t3, r3) {
351
+ return r3 || function(t4) {
352
+ switch(t4){
353
+ case "YY":
354
+ return String(e2.$y).slice(-2);
355
+ case "YYYY":
356
+ return b.s(e2.$y, 4, "0");
357
+ case "M":
358
+ return a2 + 1;
359
+ case "MM":
360
+ return b.s(a2 + 1, 2, "0");
361
+ case "MMM":
362
+ return h2(n2.monthsShort, a2, c2, 3);
363
+ case "MMMM":
364
+ return h2(c2, a2);
365
+ case "D":
366
+ return e2.$D;
367
+ case "DD":
368
+ return b.s(e2.$D, 2, "0");
369
+ case "d":
370
+ return String(e2.$W);
371
+ case "dd":
372
+ return h2(n2.weekdaysMin, e2.$W, o2, 2);
373
+ case "ddd":
374
+ return h2(n2.weekdaysShort, e2.$W, o2, 3);
375
+ case "dddd":
376
+ return o2[e2.$W];
377
+ case "H":
378
+ return String(s2);
379
+ case "HH":
380
+ return b.s(s2, 2, "0");
381
+ case "h":
382
+ return d2(1);
383
+ case "hh":
384
+ return d2(2);
385
+ case "a":
386
+ return $2(s2, u2, true);
387
+ case "A":
388
+ return $2(s2, u2, false);
389
+ case "m":
390
+ return String(u2);
391
+ case "mm":
392
+ return b.s(u2, 2, "0");
393
+ case "s":
394
+ return String(e2.$s);
395
+ case "ss":
396
+ return b.s(e2.$s, 2, "0");
397
+ case "SSS":
398
+ return b.s(e2.$ms, 3, "0");
399
+ case "Z":
400
+ return i2;
401
+ }
402
+ return null;
403
+ }(t3) || i2.replace(":", "");
404
+ });
405
+ }, m2.utcOffset = function() {
406
+ return 15 * -Math.round(this.$d.getTimezoneOffset() / 15);
407
+ }, m2.diff = function(r2, d2, l2) {
408
+ var $2, y2 = this, M3 = b.p(d2), m3 = O(r2), v2 = (m3.utcOffset() - this.utcOffset()) * e, g2 = this - m3, D2 = function() {
409
+ return b.m(y2, m3);
410
+ };
411
+ switch(M3){
412
+ case h:
413
+ $2 = D2() / 12;
414
+ break;
415
+ case c:
416
+ $2 = D2();
417
+ break;
418
+ case f:
419
+ $2 = D2() / 3;
420
+ break;
421
+ case o:
422
+ $2 = (g2 - v2) / 6048e5;
423
+ break;
424
+ case a:
425
+ $2 = (g2 - v2) / 864e5;
426
+ break;
427
+ case u:
428
+ $2 = g2 / n;
429
+ break;
430
+ case s:
431
+ $2 = g2 / e;
432
+ break;
433
+ case i:
434
+ $2 = g2 / t;
435
+ break;
436
+ default:
437
+ $2 = g2;
438
+ }
439
+ return l2 ? $2 : b.a($2);
440
+ }, m2.daysInMonth = function() {
441
+ return this.endOf(c).$D;
442
+ }, m2.$locale = function() {
443
+ return D[this.$L];
444
+ }, m2.locale = function(t2, e2) {
445
+ if (!t2) return this.$L;
446
+ var n2 = this.clone(), r2 = w(t2, e2, true);
447
+ return r2 && (n2.$L = r2), n2;
448
+ }, m2.clone = function() {
449
+ return b.w(this.$d, this);
450
+ }, m2.toDate = function() {
451
+ return new Date(this.valueOf());
452
+ }, m2.toJSON = function() {
453
+ return this.isValid() ? this.toISOString() : null;
454
+ }, m2.toISOString = function() {
455
+ return this.$d.toISOString();
456
+ }, m2.toString = function() {
457
+ return this.$d.toUTCString();
458
+ }, M2;
459
+ }(), k = _.prototype;
460
+ return O.prototype = k, [
461
+ [
462
+ "$ms",
463
+ r
464
+ ],
465
+ [
466
+ "$s",
467
+ i
468
+ ],
469
+ [
470
+ "$m",
471
+ s
472
+ ],
473
+ [
474
+ "$H",
475
+ u
476
+ ],
477
+ [
478
+ "$W",
479
+ a
480
+ ],
481
+ [
482
+ "$M",
483
+ c
484
+ ],
485
+ [
486
+ "$y",
487
+ h
488
+ ],
489
+ [
490
+ "$D",
491
+ d
492
+ ]
493
+ ].forEach(function(t2) {
494
+ k[t2[1]] = function(e2) {
495
+ return this.$g(e2, t2[0], t2[1]);
496
+ };
497
+ }), O.extend = function(t2, e2) {
498
+ return t2.$i || (t2(e2, _, O), t2.$i = true), O;
499
+ }, O.locale = w, O.isDayjs = S, O.unix = function(t2) {
500
+ return O(1e3 * t2);
501
+ }, O.en = D[g], O.Ls = D, O.p = {}, O;
502
+ });
532
503
  }
533
- if (type === "plan") {
534
- return this.readCache(
535
- pageContext,
536
- type,
537
- actionPrompt,
538
- aiTasks[index].tasks
539
- );
504
+ });
505
+ // src/common/utils.ts
506
+ var import_dayjs = __toESM(require_dayjs_min());
507
+ // src/web-element.ts
508
+ var WebElementInfo = class {
509
+ constructor({ content, rect, page, locator, id, attributes, indexId }){
510
+ this.content = content;
511
+ this.rect = rect;
512
+ this.center = [
513
+ Math.floor(rect.left + rect.width / 2),
514
+ Math.floor(rect.top + rect.height / 2)
515
+ ];
516
+ this.page = page;
517
+ this.locator = locator;
518
+ this.id = id;
519
+ this.attributes = attributes;
520
+ this.indexId = indexId;
540
521
  }
541
- return this.readCache(
542
- pageContext,
543
- type,
544
- actionPrompt,
545
- aiTasks[index].tasks
546
- );
547
- },
548
- saveCache: (cache) => {
549
- newCacheGroup.push(cache);
550
- this.writeCacheToFile();
551
- }
552
522
  };
553
- }
554
- readCache(pageContext, type, userPrompt, cacheGroup) {
555
- var _a;
556
- if (cacheGroup.length > 0) {
557
- const index = cacheGroup.findIndex((item) => item.prompt === userPrompt);
558
- if (index === -1) {
559
- return false;
560
- }
561
- const taskRes = cacheGroup.splice(index, 1)[0];
562
- if ((taskRes == null ? void 0 : taskRes.type) === "locate" && !((_a = taskRes.response) == null ? void 0 : _a.elements.every((element) => {
563
- const findIndex = pageContext.content.findIndex(
564
- (contentElement) => contentElement.id === element.id
565
- );
566
- if (findIndex === -1) {
567
- return false;
523
+ // src/common/utils.ts
524
+ function parseContextFromWebPage(page, _opt) {
525
+ return __async(this, null, function*() {
526
+ (0, _assert.default)(page, "page is required");
527
+ if (page._forceUsePageContext) {
528
+ return yield page._forceUsePageContext();
529
+ }
530
+ const url = page.url();
531
+ let screenshotBase64;
532
+ let elementsInfo;
533
+ yield Promise.all([
534
+ page.screenshotBase64().then((base64)=>{
535
+ screenshotBase64 = base64;
536
+ }),
537
+ page.getElementInfos().then((snapshot)=>__async(this, null, function*() {
538
+ elementsInfo = yield alignElements(snapshot, page);
539
+ }))
540
+ ]);
541
+ (0, _assert.default)(screenshotBase64, "screenshotBase64 is required");
542
+ const elementsPositionInfoWithoutText = elementsInfo.filter((elementInfo)=>{
543
+ if (elementInfo.attributes.nodeType === _constants.NodeType.TEXT) {
544
+ return false;
545
+ }
546
+ return true;
547
+ });
548
+ const size = yield page.size();
549
+ const width = size.width;
550
+ const height = size.height;
551
+ const screenshotBase64WithElementMarker = (_opt == null ? void 0 : _opt.ignoreMarker) ? void 0 : yield (0, _img.compositeElementInfoImg)({
552
+ inputImgBase64: screenshotBase64,
553
+ elementsPositionInfo: elementsPositionInfoWithoutText,
554
+ size: {
555
+ width,
556
+ height
557
+ }
558
+ });
559
+ return {
560
+ content: elementsInfo,
561
+ size: {
562
+ width,
563
+ height
564
+ },
565
+ screenshotBase64,
566
+ screenshotBase64WithElementMarker,
567
+ url
568
+ };
569
+ });
570
+ }
571
+ function getExtraReturnLogic() {
572
+ return __async(this, null, function*() {
573
+ const pathDir = (0, _fs1.findNearestPackageJson)(__dirname);
574
+ (0, _assert.default)(pathDir, `can't find pathDir, with ${__dirname}`);
575
+ const scriptPath = _path.default.join(pathDir, "./dist/script/htmlElement.js");
576
+ const elementInfosScriptContent = (0, _fs.readFileSync)(scriptPath, "utf-8");
577
+ return `${elementInfosScriptContent}midscene_element_inspector.webExtractTextWithPosition()`;
578
+ });
579
+ }
580
+ var sizeThreshold = 3;
581
+ function alignElements(elements, page) {
582
+ return __async(this, null, function*() {
583
+ const validElements = elements.filter((item)=>{
584
+ return item.rect.height >= sizeThreshold && item.rect.width >= sizeThreshold;
585
+ });
586
+ const textsAligned = [];
587
+ for (const item of validElements){
588
+ const { rect, id, content, attributes, locator, indexId } = item;
589
+ textsAligned.push(new WebElementInfo({
590
+ rect,
591
+ locator,
592
+ id,
593
+ content,
594
+ attributes,
595
+ page,
596
+ indexId
597
+ }));
598
+ }
599
+ return textsAligned;
600
+ });
601
+ }
602
+ function reportFileName(tag = "web") {
603
+ const dateTimeInFileName = (0, import_dayjs.default)().format("YYYY-MM-DD_HH-mm-ss-SSS");
604
+ return `${tag}-${dateTimeInFileName}`;
605
+ }
606
+ function printReportMsg(filepath) {
607
+ console.log("Midscene - report file updated:", filepath);
608
+ }
609
+ function getCurrentExecutionFile(trace) {
610
+ const error = new Error();
611
+ const stackTrace = trace || error.stack;
612
+ const pkgDir = process.cwd() || "";
613
+ if (stackTrace) {
614
+ const stackLines = stackTrace.split("\n");
615
+ for (const line of stackLines){
616
+ if (line.includes(".spec.") || line.includes(".test.") || line.includes(".ts") || line.includes(".js")) {
617
+ const match = line.match(/(?:at\s+)?(.*?\.(?:spec|test)\.[jt]s)/);
618
+ if (match == null ? void 0 : match[1]) {
619
+ const targetFileName = match[1].replace(pkgDir, "").trim().replace("at ", "");
620
+ return targetFileName;
621
+ }
622
+ }
623
+ }
568
624
  }
569
- return true;
570
- }))) {
571
625
  return false;
572
- }
573
- if (taskRes && taskRes.type === type && taskRes.prompt === userPrompt && this.pageContextEqual(taskRes.pageContext, pageContext)) {
574
- return taskRes.response;
575
- }
576
626
  }
577
- return false;
578
- }
579
- pageContextEqual(taskPageContext, pageContext) {
580
- return taskPageContext.size.width === pageContext.size.width && taskPageContext.size.height === pageContext.size.height;
581
- }
582
- /**
627
+ var testFileIndex = /* @__PURE__ */ new Map();
628
+ function generateCacheId(fileName) {
629
+ let taskFile = fileName || getCurrentExecutionFile();
630
+ if (!taskFile) {
631
+ taskFile = (0, _utils1.uuid)();
632
+ console.warn("Midscene - using random UUID for cache id. Cache may be invalid.");
633
+ }
634
+ if (testFileIndex.has(taskFile)) {
635
+ const currentIndex = testFileIndex.get(taskFile);
636
+ if (currentIndex !== void 0) {
637
+ testFileIndex.set(taskFile, currentIndex + 1);
638
+ }
639
+ } else {
640
+ testFileIndex.set(taskFile, 1);
641
+ }
642
+ return `${taskFile}-${testFileIndex.get(taskFile)}`;
643
+ }
644
+ // src/common/task-cache.ts
645
+ var TaskCache = class {
646
+ getCacheGroupByPrompt(aiActionPrompt) {
647
+ const { aiTasks = [] } = this.cache || {
648
+ aiTasks: []
649
+ };
650
+ const index = aiTasks.findIndex((item)=>item.prompt === aiActionPrompt);
651
+ const newCacheGroup = [];
652
+ this.newCache.aiTasks.push({
653
+ prompt: aiActionPrompt,
654
+ tasks: newCacheGroup
655
+ });
656
+ return {
657
+ readCache: (pageContext, type, actionPrompt)=>{
658
+ if (index === -1) {
659
+ return false;
660
+ }
661
+ if (type === "plan") {
662
+ return this.readCache(pageContext, type, actionPrompt, aiTasks[index].tasks);
663
+ }
664
+ return this.readCache(pageContext, type, actionPrompt, aiTasks[index].tasks);
665
+ },
666
+ saveCache: (cache)=>{
667
+ newCacheGroup.push(cache);
668
+ this.writeCacheToFile();
669
+ }
670
+ };
671
+ }
672
+ readCache(pageContext, type, userPrompt, cacheGroup) {
673
+ var _a;
674
+ if (cacheGroup.length > 0) {
675
+ const index = cacheGroup.findIndex((item)=>item.prompt === userPrompt);
676
+ if (index === -1) {
677
+ return false;
678
+ }
679
+ const taskRes = cacheGroup.splice(index, 1)[0];
680
+ if ((taskRes == null ? void 0 : taskRes.type) === "locate" && !((_a = taskRes.response) == null ? void 0 : _a.elements.every((element)=>{
681
+ const findIndex = pageContext.content.findIndex((contentElement)=>contentElement.id === element.id);
682
+ if (findIndex === -1) {
683
+ return false;
684
+ }
685
+ return true;
686
+ }))) {
687
+ return false;
688
+ }
689
+ if (taskRes && taskRes.type === type && taskRes.prompt === userPrompt && this.pageContextEqual(taskRes.pageContext, pageContext)) {
690
+ return taskRes.response;
691
+ }
692
+ }
693
+ return false;
694
+ }
695
+ pageContextEqual(taskPageContext, pageContext) {
696
+ return taskPageContext.size.width === pageContext.size.width && taskPageContext.size.height === pageContext.size.height;
697
+ }
698
+ /**
583
699
  * Generate task cache data.
584
700
  * This method is mainly used to create or obtain some cached data for tasks, and it returns a new cache object.
585
701
  * In the cache object, it may contain task-related information, states, or other necessary data.
586
702
  * It is assumed that the `newCache` property already exists in the current class or object and is a data structure used to store task cache.
587
703
  * @returns {Object} Returns a new cache object, which may include task cache data.
588
- */
589
- generateTaskCache() {
590
- return this.newCache;
591
- }
592
- readCacheFromFile() {
593
- const cacheFile = (0, import_node_path2.join)((0, import_utils.getLogDirByType)("cache"), `${this.cacheId}.json`);
594
- if (process.env.MIDSCENE_CACHE === "true" && (0, import_node_fs2.existsSync)(cacheFile)) {
595
- try {
596
- const data = (0, import_node_fs2.readFileSync)(cacheFile, "utf8");
597
- const jsonData = JSON.parse(data);
598
- if (jsonData.pkgName !== this.midscenePkgInfo.name || jsonData.pkgVersion !== this.midscenePkgInfo.version) {
599
- return void 0;
600
- }
601
- return jsonData;
602
- } catch (err) {
603
- return void 0;
604
- }
605
- }
606
- return void 0;
607
- }
608
- writeCacheToFile() {
609
- const midscenePkgInfo = (0, import_fs2.getMidscenePkgInfo)(__dirname);
610
- (0, import_utils.writeLogFile)({
611
- fileName: `${this.cacheId}`,
612
- fileExt: "json",
613
- fileContent: (0, import_utils.stringifyDumpData)(
614
- __spreadValues({
615
- pkgName: midscenePkgInfo.name,
616
- pkgVersion: midscenePkgInfo.version,
617
- cacheId: this.cacheId
618
- }, this.newCache),
619
- 2
620
- ),
621
- type: "cache"
622
- });
623
- }
624
- };
625
-
626
- // src/common/tasks.ts
627
- var PageTaskExecutor = class {
628
- constructor(page, opts) {
629
- this.page = page;
630
- this.insight = new import_core.Insight(async () => {
631
- return await parseContextFromWebPage(page);
632
- });
633
- this.taskCache = new TaskCache({
634
- fileName: opts == null ? void 0 : opts.cacheId
635
- });
636
- }
637
- async recordScreenshot(timing) {
638
- const file = await this.page.screenshot();
639
- const item = {
640
- type: "screenshot",
641
- ts: Date.now(),
642
- screenshot: (0, import_img3.base64Encoded)(file),
643
- timing
644
- };
645
- return item;
646
- }
647
- wrapExecutorWithScreenshot(taskApply) {
648
- const taskWithScreenshot = __spreadProps(__spreadValues({}, taskApply), {
649
- executor: async (param, context, ...args) => {
650
- const recorder = [];
651
- const { task } = context;
652
- task.recorder = recorder;
653
- const shot = await this.recordScreenshot(`before ${task.type}`);
654
- recorder.push(shot);
655
- const result = await taskApply.executor(param, context, ...args);
656
- if (taskApply.type === "Action") {
657
- await (0, import_utils3.sleep)(1e3);
658
- const shot2 = await this.recordScreenshot("after Action");
659
- recorder.push(shot2);
704
+ */ generateTaskCache() {
705
+ return this.newCache;
660
706
  }
661
- return result;
662
- }
663
- });
664
- return taskWithScreenshot;
665
- }
666
- async convertPlanToExecutable(plans, cacheGroup) {
667
- const tasks = plans.map((plan2) => {
668
- if (plan2.type === "Locate") {
669
- const taskFind = {
670
- type: "Insight",
671
- subType: "Locate",
672
- param: plan2.param,
673
- executor: async (param, taskContext) => {
674
- const { task } = taskContext;
675
- let insightDump;
676
- const dumpCollector = (dump) => {
677
- insightDump = dump;
678
- };
679
- this.insight.onceDumpUpdatedFn = dumpCollector;
680
- const pageContext = await this.insight.contextRetrieverFn();
681
- const locateCache = cacheGroup == null ? void 0 : cacheGroup.readCache(
682
- pageContext,
683
- "locate",
684
- param.prompt
685
- );
686
- let locateResult;
687
- const callAI = this.insight.aiVendorFn;
688
- const element = await this.insight.locate(param.prompt, {
689
- quickAnswer: plan2.quickAnswer,
690
- callAI: async (...message) => {
691
- if (locateCache) {
692
- locateResult = locateCache;
693
- return Promise.resolve(locateCache);
707
+ readCacheFromFile() {
708
+ if (_utils1.ifInBrowser) {
709
+ return void 0;
710
+ }
711
+ const cacheFile = (0, _path.join)((0, _utils.getLogDirByType)("cache"), `${this.cacheId}.json`);
712
+ if (process.env.MIDSCENE_CACHE === "true" && (0, _fs.existsSync)(cacheFile)) {
713
+ try {
714
+ const data = (0, _fs.readFileSync)(cacheFile, "utf8");
715
+ const jsonData = JSON.parse(data);
716
+ if (!this.midscenePkgInfo) {
717
+ return void 0;
718
+ }
719
+ if (jsonData.pkgName !== this.midscenePkgInfo.name || jsonData.pkgVersion !== this.midscenePkgInfo.version) {
720
+ return void 0;
721
+ }
722
+ return jsonData;
723
+ } catch (err) {
724
+ return void 0;
694
725
  }
695
- locateResult = await callAI(...message);
696
- (0, import_node_assert2.default)(locateResult);
697
- return locateResult;
698
- }
699
- });
700
- if (locateResult) {
701
- cacheGroup == null ? void 0 : cacheGroup.saveCache({
702
- type: "locate",
703
- pageContext: {
704
- url: pageContext.url,
705
- size: pageContext.size
706
- },
707
- prompt: param.prompt,
708
- response: locateResult
709
- });
710
726
  }
711
- if (!element) {
712
- task.log = {
713
- dump: insightDump
714
- };
715
- throw new Error(`Element not found: ${param.prompt}`);
727
+ return void 0;
728
+ }
729
+ writeCacheToFile() {
730
+ const midscenePkgInfo = (0, _fs1.getRunningPkgInfo)();
731
+ if (!midscenePkgInfo) {
732
+ return;
716
733
  }
717
- return {
718
- output: {
719
- element
720
- },
721
- log: {
722
- dump: insightDump
723
- },
724
- cache: {
725
- hit: Boolean(locateCache)
726
- }
727
- };
728
- }
729
- };
730
- return taskFind;
731
- }
732
- if (plan2.type === "Assert" || plan2.type === "AssertWithoutThrow") {
733
- const assertPlan = plan2;
734
- const taskAssert = {
735
- type: "Insight",
736
- subType: "Assert",
737
- param: assertPlan.param,
738
- executor: async (param, taskContext) => {
739
- const { task } = taskContext;
740
- let insightDump;
741
- const dumpCollector = (dump) => {
742
- insightDump = dump;
734
+ (0, _utils.writeLogFile)({
735
+ fileName: `${this.cacheId}`,
736
+ fileExt: "json",
737
+ fileContent: (0, _utils.stringifyDumpData)(__spreadValues({
738
+ pkgName: midscenePkgInfo.name,
739
+ pkgVersion: midscenePkgInfo.version,
740
+ cacheId: this.cacheId
741
+ }, this.newCache), 2),
742
+ type: "cache"
743
+ });
744
+ }
745
+ constructor(opts){
746
+ this.midscenePkgInfo = (0, _fs1.getRunningPkgInfo)();
747
+ this.cacheId = generateCacheId(opts == null ? void 0 : opts.fileName);
748
+ this.cache = this.readCacheFromFile() || {
749
+ aiTasks: []
743
750
  };
744
- this.insight.onceDumpUpdatedFn = dumpCollector;
745
- const assertion = await this.insight.assert(
746
- assertPlan.param.assertion
747
- );
748
- if (!assertion.pass) {
749
- if (plan2.type === "Assert") {
750
- task.output = assertion;
751
- task.log = {
752
- dump: insightDump
753
- };
754
- throw new Error(
755
- assertion.thought || "Assertion failed without reason"
756
- );
757
- }
758
- task.error = assertion.thought;
759
- }
760
- return {
761
- output: assertion,
762
- log: {
763
- dump: insightDump
764
- }
751
+ this.newCache = {
752
+ aiTasks: []
765
753
  };
766
- }
767
- };
768
- return taskAssert;
769
- }
770
- if (plan2.type === "Input") {
771
- const taskActionInput = {
772
- type: "Action",
773
- subType: "Input",
774
- param: plan2.param,
775
- executor: async (taskParam, { element }) => {
776
- if (element) {
777
- await this.page.clearInput(element);
778
- if (taskParam.value === "") {
779
- return;
780
- }
781
- await this.page.keyboard.type(taskParam.value);
782
- }
783
- }
784
- };
785
- return taskActionInput;
786
- }
787
- if (plan2.type === "KeyboardPress") {
788
- const taskActionKeyboardPress = {
789
- type: "Action",
790
- subType: "KeyboardPress",
791
- param: plan2.param,
792
- executor: async (taskParam) => {
793
- (0, import_node_assert2.default)(taskParam.value, "No key to press");
794
- await this.page.keyboard.press(taskParam.value);
795
- }
796
- };
797
- return taskActionKeyboardPress;
798
- }
799
- if (plan2.type === "Tap") {
800
- const taskActionTap = {
801
- type: "Action",
802
- subType: "Tap",
803
- executor: async (param, { element }) => {
804
- (0, import_node_assert2.default)(element, "Element not found, cannot tap");
805
- await this.page.mouse.click(
806
- element.center[0],
807
- element.center[1]
808
- );
809
- }
810
- };
811
- return taskActionTap;
812
- }
813
- if (plan2.type === "Hover") {
814
- const taskActionHover = {
815
- type: "Action",
816
- subType: "Hover",
817
- executor: async (param, { element }) => {
818
- (0, import_node_assert2.default)(element, "Element not found, cannot hover");
819
- await this.page.mouse.move(
820
- element.center[0],
821
- element.center[1]
822
- );
823
- }
824
- };
825
- return taskActionHover;
826
- }
827
- if (plan2.type === "Scroll") {
828
- const taskActionScroll = {
829
- type: "Action",
830
- subType: "Scroll",
831
- param: plan2.param,
832
- executor: async (taskParam) => {
833
- const scrollToEventName = taskParam.scrollType;
834
- switch (scrollToEventName) {
835
- case "scrollUntilTop":
836
- await this.page.scrollUntilTop();
837
- break;
838
- case "scrollUntilBottom":
839
- await this.page.scrollUntilBottom();
840
- break;
841
- case "scrollUpOneScreen":
842
- await this.page.scrollUpOneScreen();
843
- break;
844
- case "scrollDownOneScreen":
845
- await this.page.scrollDownOneScreen();
846
- break;
847
- default:
848
- console.error(
849
- "Unknown scroll event type:",
850
- scrollToEventName
851
- );
852
- }
853
- }
854
- };
855
- return taskActionScroll;
856
- }
857
- if (plan2.type === "Sleep") {
858
- const taskActionSleep = {
859
- type: "Action",
860
- subType: "Sleep",
861
- param: plan2.param,
862
- executor: async (taskParam) => {
863
- await (0, import_utils3.sleep)(taskParam.timeMs || 3e3);
864
- }
865
- };
866
- return taskActionSleep;
867
- }
868
- if (plan2.type === "Error") {
869
- const taskActionError = {
870
- type: "Action",
871
- subType: "Error",
872
- param: plan2.param,
873
- executor: async (taskParam) => {
874
- (0, import_node_assert2.default)(
875
- taskParam.thought,
876
- "An error occurred, but no thought provided"
877
- );
878
- throw new Error(taskParam.thought);
879
- }
880
- };
881
- return taskActionError;
882
- }
883
- throw new Error(`Unknown or Unsupported task type: ${plan2.type}`);
884
- }).map((task) => {
885
- return this.wrapExecutorWithScreenshot(task);
886
- });
887
- return tasks;
888
- }
889
- async action(userPrompt) {
890
- const taskExecutor = new import_core.Executor(userPrompt);
891
- const cacheGroup = this.taskCache.getCacheGroupByPrompt(userPrompt);
892
- let plans = [];
893
- const planningTask = {
894
- type: "Planning",
895
- param: {
896
- userPrompt
897
- },
898
- executor: async (param) => {
899
- const pageContext = await this.insight.contextRetrieverFn();
900
- let planResult;
901
- const planCache = cacheGroup.readCache(pageContext, "plan", userPrompt);
902
- if (planCache) {
903
- planResult = planCache;
904
- } else {
905
- planResult = await (0, import_core.plan)(param.userPrompt, {
906
- context: pageContext
907
- });
908
754
  }
909
- (0, import_node_assert2.default)(planResult.plans.length > 0, "No plans found");
910
- plans = planResult.plans;
911
- cacheGroup.saveCache({
912
- type: "plan",
913
- pageContext: {
914
- url: pageContext.url,
915
- size: pageContext.size
916
- },
917
- prompt: userPrompt,
918
- response: planResult
919
- });
920
- return {
921
- output: planResult,
922
- cache: {
923
- hit: Boolean(planCache)
924
- }
925
- };
926
- }
927
- };
928
- await taskExecutor.append(this.wrapExecutorWithScreenshot(planningTask));
929
- let output = await taskExecutor.flush();
930
- if (taskExecutor.isInErrorState()) {
931
- return {
932
- output,
933
- executor: taskExecutor
934
- };
935
- }
936
- const executables = await this.convertPlanToExecutable(plans, cacheGroup);
937
- await taskExecutor.append(executables);
938
- output = await taskExecutor.flush();
939
- return {
940
- output,
941
- executor: taskExecutor
942
- };
943
- }
944
- async query(demand) {
945
- const description = typeof demand === "string" ? demand : JSON.stringify(demand);
946
- const taskExecutor = new import_core.Executor(description);
947
- const queryTask = {
948
- type: "Insight",
949
- subType: "Query",
950
- param: {
951
- dataDemand: demand
952
- },
953
- executor: async (param) => {
954
- let insightDump;
955
- const dumpCollector = (dump) => {
956
- insightDump = dump;
957
- };
958
- this.insight.onceDumpUpdatedFn = dumpCollector;
959
- const data = await this.insight.extract(param.dataDemand);
960
- return {
961
- output: data,
962
- log: { dump: insightDump }
963
- };
964
- }
965
- };
966
- await taskExecutor.append(this.wrapExecutorWithScreenshot(queryTask));
967
- const output = await taskExecutor.flush();
968
- return {
969
- output,
970
- executor: taskExecutor
971
- };
972
- }
973
- async assert(assertion) {
974
- const description = `assert: ${assertion}`;
975
- const taskExecutor = new import_core.Executor(description);
976
- const assertionPlan = {
977
- type: "Assert",
978
- param: {
979
- assertion
980
- }
981
- };
982
- const assertTask = await this.convertPlanToExecutable([assertionPlan]);
983
- await taskExecutor.append(this.wrapExecutorWithScreenshot(assertTask[0]));
984
- const output = await taskExecutor.flush();
985
- return {
986
- output,
987
- executor: taskExecutor
988
755
  };
989
- }
990
- async waitFor(assertion, opt) {
991
- const description = `waitFor: ${assertion}`;
992
- const taskExecutor = new import_core.Executor(description);
993
- const { timeoutMs, checkIntervalMs } = opt;
994
- (0, import_node_assert2.default)(assertion, "No assertion for waitFor");
995
- (0, import_node_assert2.default)(timeoutMs, "No timeoutMs for waitFor");
996
- (0, import_node_assert2.default)(checkIntervalMs, "No checkIntervalMs for waitFor");
997
- const overallStartTime = Date.now();
998
- let startTime = Date.now();
999
- let errorThought = "";
1000
- while (Date.now() - overallStartTime < timeoutMs) {
1001
- startTime = Date.now();
1002
- const assertPlan = {
1003
- type: "AssertWithoutThrow",
1004
- param: {
1005
- assertion
756
+ // src/common/tasks.ts
757
+ var PageTaskExecutor = class {
758
+ recordScreenshot(timing) {
759
+ return __async(this, null, function*() {
760
+ const base64 = yield this.page.screenshotBase64();
761
+ const item = {
762
+ type: "screenshot",
763
+ ts: Date.now(),
764
+ screenshot: base64,
765
+ timing
766
+ };
767
+ return item;
768
+ });
769
+ }
770
+ wrapExecutorWithScreenshot(taskApply) {
771
+ const taskWithScreenshot = __spreadProps(__spreadValues({}, taskApply), {
772
+ executor: (param, context, ...args)=>__async(this, null, function*() {
773
+ const recorder = [];
774
+ const { task } = context;
775
+ task.recorder = recorder;
776
+ const shot = yield this.recordScreenshot(`before ${task.type}`);
777
+ recorder.push(shot);
778
+ const result = yield taskApply.executor(param, context, ...args);
779
+ if (taskApply.type === "Action") {
780
+ yield (0, _utils.sleep)(1e3);
781
+ const shot2 = yield this.recordScreenshot("after Action");
782
+ recorder.push(shot2);
783
+ }
784
+ return result;
785
+ })
786
+ });
787
+ return taskWithScreenshot;
788
+ }
789
+ convertPlanToExecutable(plans, cacheGroup) {
790
+ return __async(this, null, function*() {
791
+ const tasks = plans.map((plan2)=>{
792
+ if (plan2.type === "Locate") {
793
+ const taskFind = {
794
+ type: "Insight",
795
+ subType: "Locate",
796
+ param: plan2.param,
797
+ executor: (param, taskContext)=>__async(this, null, function*() {
798
+ const { task } = taskContext;
799
+ let insightDump;
800
+ const dumpCollector = (dump)=>{
801
+ insightDump = dump;
802
+ };
803
+ this.insight.onceDumpUpdatedFn = dumpCollector;
804
+ const pageContext = yield this.insight.contextRetrieverFn();
805
+ const locateCache = cacheGroup == null ? void 0 : cacheGroup.readCache(pageContext, "locate", param.prompt);
806
+ let locateResult;
807
+ const callAI = this.insight.aiVendorFn;
808
+ const element = yield this.insight.locate(param.prompt, {
809
+ quickAnswer: plan2.quickAnswer,
810
+ callAI: (...message)=>__async(this, null, function*() {
811
+ if (locateCache) {
812
+ locateResult = locateCache;
813
+ return Promise.resolve(locateCache);
814
+ }
815
+ locateResult = yield callAI(...message);
816
+ (0, _assert.default)(locateResult);
817
+ return locateResult;
818
+ })
819
+ });
820
+ if (locateResult) {
821
+ cacheGroup == null ? void 0 : cacheGroup.saveCache({
822
+ type: "locate",
823
+ pageContext: {
824
+ url: pageContext.url,
825
+ size: pageContext.size
826
+ },
827
+ prompt: param.prompt,
828
+ response: locateResult
829
+ });
830
+ }
831
+ if (!element) {
832
+ task.log = {
833
+ dump: insightDump
834
+ };
835
+ throw new Error(`Element not found: ${param.prompt}`);
836
+ }
837
+ return {
838
+ output: {
839
+ element
840
+ },
841
+ log: {
842
+ dump: insightDump
843
+ },
844
+ cache: {
845
+ hit: Boolean(locateCache)
846
+ }
847
+ };
848
+ })
849
+ };
850
+ return taskFind;
851
+ }
852
+ if (plan2.type === "Assert" || plan2.type === "AssertWithoutThrow") {
853
+ const assertPlan = plan2;
854
+ const taskAssert = {
855
+ type: "Insight",
856
+ subType: "Assert",
857
+ param: assertPlan.param,
858
+ executor: (param, taskContext)=>__async(this, null, function*() {
859
+ const { task } = taskContext;
860
+ let insightDump;
861
+ const dumpCollector = (dump)=>{
862
+ insightDump = dump;
863
+ };
864
+ this.insight.onceDumpUpdatedFn = dumpCollector;
865
+ const assertion = yield this.insight.assert(assertPlan.param.assertion);
866
+ if (!assertion.pass) {
867
+ if (plan2.type === "Assert") {
868
+ task.output = assertion;
869
+ task.log = {
870
+ dump: insightDump
871
+ };
872
+ throw new Error(assertion.thought || "Assertion failed without reason");
873
+ }
874
+ task.error = assertion.thought;
875
+ }
876
+ return {
877
+ output: assertion,
878
+ log: {
879
+ dump: insightDump
880
+ }
881
+ };
882
+ })
883
+ };
884
+ return taskAssert;
885
+ }
886
+ if (plan2.type === "Input") {
887
+ const taskActionInput = {
888
+ type: "Action",
889
+ subType: "Input",
890
+ param: plan2.param,
891
+ executor: (_0, _1)=>__async(this, [
892
+ _0,
893
+ _1
894
+ ], function*(taskParam, { element }) {
895
+ if (element) {
896
+ yield this.page.clearInput(element);
897
+ if (taskParam.value === "") {
898
+ return;
899
+ }
900
+ yield this.page.keyboard.type(taskParam.value);
901
+ }
902
+ })
903
+ };
904
+ return taskActionInput;
905
+ }
906
+ if (plan2.type === "KeyboardPress") {
907
+ const taskActionKeyboardPress = {
908
+ type: "Action",
909
+ subType: "KeyboardPress",
910
+ param: plan2.param,
911
+ executor: (taskParam)=>__async(this, null, function*() {
912
+ (0, _assert.default)(taskParam.value, "No key to press");
913
+ yield this.page.keyboard.press(taskParam.value);
914
+ })
915
+ };
916
+ return taskActionKeyboardPress;
917
+ }
918
+ if (plan2.type === "Tap") {
919
+ const taskActionTap = {
920
+ type: "Action",
921
+ subType: "Tap",
922
+ executor: (_0, _1)=>__async(this, [
923
+ _0,
924
+ _1
925
+ ], function*(param, { element }) {
926
+ (0, _assert.default)(element, "Element not found, cannot tap");
927
+ yield this.page.mouse.click(element.center[0], element.center[1]);
928
+ })
929
+ };
930
+ return taskActionTap;
931
+ }
932
+ if (plan2.type === "Hover") {
933
+ const taskActionHover = {
934
+ type: "Action",
935
+ subType: "Hover",
936
+ executor: (_0, _1)=>__async(this, [
937
+ _0,
938
+ _1
939
+ ], function*(param, { element }) {
940
+ (0, _assert.default)(element, "Element not found, cannot hover");
941
+ yield this.page.mouse.move(element.center[0], element.center[1]);
942
+ })
943
+ };
944
+ return taskActionHover;
945
+ }
946
+ if (plan2.type === "Scroll") {
947
+ const taskActionScroll = {
948
+ type: "Action",
949
+ subType: "Scroll",
950
+ param: plan2.param,
951
+ executor: (taskParam)=>__async(this, null, function*() {
952
+ const scrollToEventName = taskParam.scrollType;
953
+ switch(scrollToEventName){
954
+ case "scrollUntilTop":
955
+ yield this.page.scrollUntilTop();
956
+ break;
957
+ case "scrollUntilBottom":
958
+ yield this.page.scrollUntilBottom();
959
+ break;
960
+ case "scrollUpOneScreen":
961
+ yield this.page.scrollUpOneScreen();
962
+ break;
963
+ case "scrollDownOneScreen":
964
+ yield this.page.scrollDownOneScreen();
965
+ break;
966
+ default:
967
+ console.error("Unknown scroll event type:", scrollToEventName);
968
+ }
969
+ })
970
+ };
971
+ return taskActionScroll;
972
+ }
973
+ if (plan2.type === "Sleep") {
974
+ const taskActionSleep = {
975
+ type: "Action",
976
+ subType: "Sleep",
977
+ param: plan2.param,
978
+ executor: (taskParam)=>__async(this, null, function*() {
979
+ yield (0, _utils.sleep)(taskParam.timeMs || 3e3);
980
+ })
981
+ };
982
+ return taskActionSleep;
983
+ }
984
+ if (plan2.type === "Error") {
985
+ const taskActionError = {
986
+ type: "Action",
987
+ subType: "Error",
988
+ param: plan2.param,
989
+ executor: (taskParam)=>__async(this, null, function*() {
990
+ (0, _assert.default)(taskParam.thought, "An error occurred, but no thought provided");
991
+ throw new Error(taskParam.thought);
992
+ })
993
+ };
994
+ return taskActionError;
995
+ }
996
+ throw new Error(`Unknown or Unsupported task type: ${plan2.type}`);
997
+ }).map((task)=>{
998
+ return this.wrapExecutorWithScreenshot(task);
999
+ });
1000
+ return tasks;
1001
+ });
1002
+ }
1003
+ action(userPrompt) {
1004
+ return __async(this, null, function*() {
1005
+ const taskExecutor = new _core.Executor(userPrompt);
1006
+ const cacheGroup = this.taskCache.getCacheGroupByPrompt(userPrompt);
1007
+ let plans = [];
1008
+ const planningTask = {
1009
+ type: "Planning",
1010
+ param: {
1011
+ userPrompt
1012
+ },
1013
+ executor: (param)=>__async(this, null, function*() {
1014
+ const pageContext = yield this.insight.contextRetrieverFn();
1015
+ let planResult;
1016
+ const planCache = cacheGroup.readCache(pageContext, "plan", userPrompt);
1017
+ if (planCache) {
1018
+ planResult = planCache;
1019
+ } else {
1020
+ planResult = yield (0, _core.plan)(param.userPrompt, {
1021
+ context: pageContext
1022
+ });
1023
+ }
1024
+ (0, _assert.default)(planResult.plans.length > 0, "No plans found");
1025
+ plans = planResult.plans;
1026
+ cacheGroup.saveCache({
1027
+ type: "plan",
1028
+ pageContext: {
1029
+ url: pageContext.url,
1030
+ size: pageContext.size
1031
+ },
1032
+ prompt: userPrompt,
1033
+ response: planResult
1034
+ });
1035
+ return {
1036
+ output: planResult,
1037
+ cache: {
1038
+ hit: Boolean(planCache)
1039
+ }
1040
+ };
1041
+ })
1042
+ };
1043
+ yield taskExecutor.append(this.wrapExecutorWithScreenshot(planningTask));
1044
+ let output = yield taskExecutor.flush();
1045
+ if (taskExecutor.isInErrorState()) {
1046
+ return {
1047
+ output,
1048
+ executor: taskExecutor
1049
+ };
1050
+ }
1051
+ const executables = yield this.convertPlanToExecutable(plans, cacheGroup);
1052
+ yield taskExecutor.append(executables);
1053
+ output = yield taskExecutor.flush();
1054
+ return {
1055
+ output,
1056
+ executor: taskExecutor
1057
+ };
1058
+ });
1059
+ }
1060
+ query(demand) {
1061
+ return __async(this, null, function*() {
1062
+ const description = typeof demand === "string" ? demand : JSON.stringify(demand);
1063
+ const taskExecutor = new _core.Executor(description);
1064
+ const queryTask = {
1065
+ type: "Insight",
1066
+ subType: "Query",
1067
+ param: {
1068
+ dataDemand: demand
1069
+ },
1070
+ executor: (param)=>__async(this, null, function*() {
1071
+ let insightDump;
1072
+ const dumpCollector = (dump)=>{
1073
+ insightDump = dump;
1074
+ };
1075
+ this.insight.onceDumpUpdatedFn = dumpCollector;
1076
+ const data = yield this.insight.extract(param.dataDemand);
1077
+ return {
1078
+ output: data,
1079
+ log: {
1080
+ dump: insightDump
1081
+ }
1082
+ };
1083
+ })
1084
+ };
1085
+ yield taskExecutor.append(this.wrapExecutorWithScreenshot(queryTask));
1086
+ const output = yield taskExecutor.flush();
1087
+ return {
1088
+ output,
1089
+ executor: taskExecutor
1090
+ };
1091
+ });
1092
+ }
1093
+ assert(assertion) {
1094
+ return __async(this, null, function*() {
1095
+ const description = `assert: ${assertion}`;
1096
+ const taskExecutor = new _core.Executor(description);
1097
+ const assertionPlan = {
1098
+ type: "Assert",
1099
+ param: {
1100
+ assertion
1101
+ }
1102
+ };
1103
+ const assertTask = yield this.convertPlanToExecutable([
1104
+ assertionPlan
1105
+ ]);
1106
+ yield taskExecutor.append(this.wrapExecutorWithScreenshot(assertTask[0]));
1107
+ const output = yield taskExecutor.flush();
1108
+ return {
1109
+ output,
1110
+ executor: taskExecutor
1111
+ };
1112
+ });
1113
+ }
1114
+ waitFor(assertion, opt) {
1115
+ return __async(this, null, function*() {
1116
+ const description = `waitFor: ${assertion}`;
1117
+ const taskExecutor = new _core.Executor(description);
1118
+ const { timeoutMs, checkIntervalMs } = opt;
1119
+ (0, _assert.default)(assertion, "No assertion for waitFor");
1120
+ (0, _assert.default)(timeoutMs, "No timeoutMs for waitFor");
1121
+ (0, _assert.default)(checkIntervalMs, "No checkIntervalMs for waitFor");
1122
+ const overallStartTime = Date.now();
1123
+ let startTime = Date.now();
1124
+ let errorThought = "";
1125
+ while(Date.now() - overallStartTime < timeoutMs){
1126
+ startTime = Date.now();
1127
+ const assertPlan = {
1128
+ type: "AssertWithoutThrow",
1129
+ param: {
1130
+ assertion
1131
+ }
1132
+ };
1133
+ const assertTask = yield this.convertPlanToExecutable([
1134
+ assertPlan
1135
+ ]);
1136
+ yield taskExecutor.append(this.wrapExecutorWithScreenshot(assertTask[0]));
1137
+ const output = yield taskExecutor.flush();
1138
+ if (output == null ? void 0 : output.pass) {
1139
+ return {
1140
+ output: void 0,
1141
+ executor: taskExecutor
1142
+ };
1143
+ }
1144
+ errorThought = (output == null ? void 0 : output.thought) || "unknown error";
1145
+ const now = Date.now();
1146
+ if (now - startTime < checkIntervalMs) {
1147
+ const timeRemaining = checkIntervalMs - (now - startTime);
1148
+ const sleepPlan = {
1149
+ type: "Sleep",
1150
+ param: {
1151
+ timeMs: timeRemaining
1152
+ }
1153
+ };
1154
+ const sleepTask = yield this.convertPlanToExecutable([
1155
+ sleepPlan
1156
+ ]);
1157
+ yield taskExecutor.append(this.wrapExecutorWithScreenshot(sleepTask[0]));
1158
+ yield taskExecutor.flush();
1159
+ }
1160
+ }
1161
+ const errorPlan = {
1162
+ type: "Error",
1163
+ param: {
1164
+ thought: `waitFor timeout: ${errorThought}`
1165
+ }
1166
+ };
1167
+ const errorTask = yield this.convertPlanToExecutable([
1168
+ errorPlan
1169
+ ]);
1170
+ yield taskExecutor.append(errorTask[0]);
1171
+ yield taskExecutor.flush();
1172
+ return {
1173
+ output: void 0,
1174
+ executor: taskExecutor
1175
+ };
1176
+ });
1177
+ }
1178
+ constructor(page, opts){
1179
+ this.page = page;
1180
+ this.insight = new _core.Insight(()=>__async(this, null, function*() {
1181
+ return yield parseContextFromWebPage(page);
1182
+ }));
1183
+ this.taskCache = new TaskCache({
1184
+ fileName: opts == null ? void 0 : opts.cacheId
1185
+ });
1006
1186
  }
1007
- };
1008
- const assertTask = await this.convertPlanToExecutable([assertPlan]);
1009
- await taskExecutor.append(this.wrapExecutorWithScreenshot(assertTask[0]));
1010
- const output = await taskExecutor.flush();
1011
- if (output == null ? void 0 : output.pass) {
1012
- return {
1013
- output: void 0,
1014
- executor: taskExecutor
1015
- };
1016
- }
1017
- errorThought = (output == null ? void 0 : output.thought) || "unknown error";
1018
- const now = Date.now();
1019
- if (now - startTime < checkIntervalMs) {
1020
- const timeRemaining = checkIntervalMs - (now - startTime);
1021
- const sleepPlan = {
1022
- type: "Sleep",
1023
- param: {
1024
- timeMs: timeRemaining
1025
- }
1026
- };
1027
- const sleepTask = await this.convertPlanToExecutable([sleepPlan]);
1028
- await taskExecutor.append(
1029
- this.wrapExecutorWithScreenshot(sleepTask[0])
1030
- );
1031
- await taskExecutor.flush();
1032
- }
1033
- }
1034
- const errorPlan = {
1035
- type: "Error",
1036
- param: {
1037
- thought: `waitFor timeout: ${errorThought}`
1038
- }
1039
- };
1040
- const errorTask = await this.convertPlanToExecutable([errorPlan]);
1041
- await taskExecutor.append(errorTask[0]);
1042
- await taskExecutor.flush();
1043
- return {
1044
- output: void 0,
1045
- executor: taskExecutor
1046
- };
1047
- }
1048
- };
1049
-
1050
- // src/common/agent.ts
1051
- var PageAgent = class {
1052
- constructor(page, opts) {
1053
- this.page = page;
1054
- this.opts = Object.assign(
1055
- {
1056
- generateReport: true,
1057
- autoPrintReportMsg: true,
1058
- groupName: "Midscene Report",
1059
- groupDescription: ""
1060
- },
1061
- opts || {}
1062
- );
1063
- this.dump = {
1064
- groupName: this.opts.groupName,
1065
- groupDescription: this.opts.groupDescription,
1066
- executions: []
1067
1187
  };
1068
- this.taskExecutor = new PageTaskExecutor(this.page, {
1069
- cacheId: opts == null ? void 0 : opts.cacheId
1070
- });
1071
- this.reportFileName = reportFileName((opts == null ? void 0 : opts.testId) || "web");
1072
- }
1073
- appendExecutionDump(execution) {
1074
- const currentDump = this.dump;
1075
- currentDump.executions.push(execution);
1076
- }
1077
- dumpDataString() {
1078
- this.dump.groupName = this.opts.groupName;
1079
- this.dump.groupDescription = this.opts.groupDescription;
1080
- return (0, import_utils5.stringifyDumpData)(this.dump);
1081
- }
1082
- writeOutActionDumps() {
1083
- const { generateReport, autoPrintReportMsg } = this.opts;
1084
- this.reportFile = (0, import_utils5.writeLogFile)({
1085
- fileName: this.reportFileName,
1086
- fileExt: import_utils5.groupedActionDumpFileExt,
1087
- fileContent: this.dumpDataString(),
1088
- type: "dump",
1089
- generateReport
1090
- });
1091
- if (generateReport && autoPrintReportMsg) {
1092
- printReportMsg(this.reportFile);
1093
- }
1094
- }
1095
- async aiAction(taskPrompt) {
1096
- const { executor } = await this.taskExecutor.action(taskPrompt);
1097
- this.appendExecutionDump(executor.dump());
1098
- this.writeOutActionDumps();
1099
- if (executor.isInErrorState()) {
1100
- const errorTask = executor.latestErrorTask();
1101
- throw new Error(`${errorTask == null ? void 0 : errorTask.error}
1188
+ // src/common/agent.ts
1189
+ var PageAgent = class {
1190
+ appendExecutionDump(execution) {
1191
+ const currentDump = this.dump;
1192
+ currentDump.executions.push(execution);
1193
+ }
1194
+ dumpDataString() {
1195
+ this.dump.groupName = this.opts.groupName;
1196
+ this.dump.groupDescription = this.opts.groupDescription;
1197
+ return (0, _utils.stringifyDumpData)(this.dump);
1198
+ }
1199
+ writeOutActionDumps() {
1200
+ const { generateReport, autoPrintReportMsg } = this.opts;
1201
+ this.reportFile = (0, _utils.writeLogFile)({
1202
+ fileName: this.reportFileName,
1203
+ fileExt: _utils.groupedActionDumpFileExt,
1204
+ fileContent: this.dumpDataString(),
1205
+ type: "dump",
1206
+ generateReport
1207
+ });
1208
+ if (generateReport && autoPrintReportMsg && this.reportFile) {
1209
+ printReportMsg(this.reportFile);
1210
+ }
1211
+ }
1212
+ aiAction(taskPrompt) {
1213
+ return __async(this, null, function*() {
1214
+ const { executor } = yield this.taskExecutor.action(taskPrompt);
1215
+ this.appendExecutionDump(executor.dump());
1216
+ this.writeOutActionDumps();
1217
+ if (executor.isInErrorState()) {
1218
+ const errorTask = executor.latestErrorTask();
1219
+ throw new Error(`${errorTask == null ? void 0 : errorTask.error}
1102
1220
  ${errorTask == null ? void 0 : errorTask.errorStack}`);
1103
- }
1104
- }
1105
- async aiQuery(demand) {
1106
- const { output, executor } = await this.taskExecutor.query(demand);
1107
- this.appendExecutionDump(executor.dump());
1108
- this.writeOutActionDumps();
1109
- if (executor.isInErrorState()) {
1110
- const errorTask = executor.latestErrorTask();
1111
- throw new Error(`${errorTask == null ? void 0 : errorTask.error}
1221
+ }
1222
+ });
1223
+ }
1224
+ aiQuery(demand) {
1225
+ return __async(this, null, function*() {
1226
+ const { output, executor } = yield this.taskExecutor.query(demand);
1227
+ this.appendExecutionDump(executor.dump());
1228
+ this.writeOutActionDumps();
1229
+ if (executor.isInErrorState()) {
1230
+ const errorTask = executor.latestErrorTask();
1231
+ throw new Error(`${errorTask == null ? void 0 : errorTask.error}
1112
1232
  ${errorTask == null ? void 0 : errorTask.errorStack}`);
1113
- }
1114
- return output;
1115
- }
1116
- async aiAssert(assertion, msg, opt) {
1117
- const { output, executor } = await this.taskExecutor.assert(assertion);
1118
- this.appendExecutionDump(executor.dump());
1119
- this.writeOutActionDumps();
1120
- if (opt == null ? void 0 : opt.keepRawResponse) {
1121
- return output;
1122
- }
1123
- if (!(output == null ? void 0 : output.pass)) {
1124
- const errMsg = msg || `Assertion failed: ${assertion}`;
1125
- const reasonMsg = `Reason: ${(output == null ? void 0 : output.thought) || "(no_reason)"}`;
1126
- throw new Error(`${errMsg}
1233
+ }
1234
+ return output;
1235
+ });
1236
+ }
1237
+ aiAssert(assertion, msg, opt) {
1238
+ return __async(this, null, function*() {
1239
+ var _a;
1240
+ const { output, executor } = yield this.taskExecutor.assert(assertion);
1241
+ this.appendExecutionDump(executor.dump());
1242
+ this.writeOutActionDumps();
1243
+ if (output && (opt == null ? void 0 : opt.keepRawResponse)) {
1244
+ return output;
1245
+ }
1246
+ if (!(output == null ? void 0 : output.pass)) {
1247
+ const errMsg = msg || `Assertion failed: ${assertion}`;
1248
+ const reasonMsg = `Reason: ${(output == null ? void 0 : output.thought) || ((_a = executor.latestErrorTask()) == null ? void 0 : _a.error) || "(no_reason)"}`;
1249
+ throw new Error(`${errMsg}
1127
1250
  ${reasonMsg}`);
1128
- }
1129
- }
1130
- async aiWaitFor(assertion, opt) {
1131
- const { executor } = await this.taskExecutor.waitFor(assertion, {
1132
- timeoutMs: (opt == null ? void 0 : opt.timeoutMs) || 15 * 1e3,
1133
- checkIntervalMs: (opt == null ? void 0 : opt.checkIntervalMs) || 3 * 1e3,
1134
- assertion
1135
- });
1136
- this.appendExecutionDump(executor.dump());
1137
- this.writeOutActionDumps();
1138
- if (executor.isInErrorState()) {
1139
- const errorTask = executor.latestErrorTask();
1140
- throw new Error(`${errorTask == null ? void 0 : errorTask.error}
1251
+ }
1252
+ });
1253
+ }
1254
+ aiWaitFor(assertion, opt) {
1255
+ return __async(this, null, function*() {
1256
+ const { executor } = yield this.taskExecutor.waitFor(assertion, {
1257
+ timeoutMs: (opt == null ? void 0 : opt.timeoutMs) || 15 * 1e3,
1258
+ checkIntervalMs: (opt == null ? void 0 : opt.checkIntervalMs) || 3 * 1e3,
1259
+ assertion
1260
+ });
1261
+ this.appendExecutionDump(executor.dump());
1262
+ this.writeOutActionDumps();
1263
+ if (executor.isInErrorState()) {
1264
+ const errorTask = executor.latestErrorTask();
1265
+ throw new Error(`${errorTask == null ? void 0 : errorTask.error}
1141
1266
  ${errorTask == null ? void 0 : errorTask.errorStack}`);
1142
- }
1143
- }
1144
- async ai(taskPrompt, type = "action") {
1145
- if (type === "action") {
1146
- return this.aiAction(taskPrompt);
1147
- }
1148
- if (type === "query") {
1149
- return this.aiQuery(taskPrompt);
1150
- }
1151
- if (type === "assert") {
1152
- return this.aiAssert(taskPrompt);
1153
- }
1154
- throw new Error(
1155
- `Unknown type: ${type}, only support 'action', 'query', 'assert'`
1156
- );
1157
- }
1158
- };
1159
-
1160
- // src/puppeteer/base-page.ts
1161
- var import_node_fs3 = require("fs");
1162
- var import_utils7 = require("@midscene/core/utils");
1163
- var import_img4 = require("@midscene/shared/img");
1164
- var Page = class {
1165
- evaluate(pageFunction, arg) {
1166
- if (this.pageType === "puppeteer") {
1167
- return this.page.evaluate(pageFunction, arg);
1168
- }
1169
- return this.page.evaluate(pageFunction, arg);
1170
- }
1171
- constructor(page, pageType) {
1172
- this.page = page;
1173
- this.pageType = pageType;
1174
- }
1175
- async getElementInfos() {
1176
- const scripts = await getExtraReturnLogic();
1177
- const captureElementSnapshot = await this.evaluate(scripts);
1178
- return captureElementSnapshot;
1179
- }
1180
- async screenshot() {
1181
- const viewportSize = await this.evaluate(() => {
1182
- return {
1183
- width: document.documentElement.clientWidth,
1184
- height: document.documentElement.clientHeight,
1185
- deviceScaleFactor: window.devicePixelRatio
1186
- };
1187
- });
1188
- const path2 = (0, import_utils7.getTmpFile)("png");
1189
- await this.page.screenshot({
1190
- path: path2,
1191
- type: "png"
1192
- });
1193
- let buf;
1194
- if (viewportSize.deviceScaleFactor > 1) {
1195
- buf = await (0, import_img4.resizeImg)((0, import_node_fs3.readFileSync)(path2), {
1196
- width: viewportSize.width,
1197
- height: viewportSize.height
1198
- });
1199
- (0, import_node_fs3.writeFileSync)(path2, buf);
1200
- }
1201
- return path2;
1202
- }
1203
- url() {
1204
- return this.page.url();
1205
- }
1206
- get mouse() {
1207
- return {
1208
- click: async (x, y, options) => this.page.mouse.click(x, y, { button: (options == null ? void 0 : options.button) || "left" }),
1209
- wheel: async (deltaX, deltaY) => {
1210
- if (this.pageType === "puppeteer") {
1211
- await this.page.mouse.wheel({ deltaX, deltaY });
1212
- } else if (this.pageType === "playwright") {
1213
- await this.page.mouse.wheel(deltaX, deltaY);
1267
+ }
1268
+ });
1269
+ }
1270
+ ai(taskPrompt, type = "action") {
1271
+ return __async(this, null, function*() {
1272
+ if (type === "action") {
1273
+ return this.aiAction(taskPrompt);
1274
+ }
1275
+ if (type === "query") {
1276
+ return this.aiQuery(taskPrompt);
1277
+ }
1278
+ if (type === "assert") {
1279
+ return this.aiAssert(taskPrompt);
1280
+ }
1281
+ throw new Error(`Unknown type: ${type}, only support 'action', 'query', 'assert'`);
1282
+ });
1283
+ }
1284
+ constructor(page, opts){
1285
+ this.page = page;
1286
+ this.opts = Object.assign({
1287
+ generateReport: true,
1288
+ autoPrintReportMsg: true,
1289
+ groupName: "Midscene Report",
1290
+ groupDescription: ""
1291
+ }, opts || {});
1292
+ this.dump = {
1293
+ groupName: this.opts.groupName,
1294
+ groupDescription: this.opts.groupDescription,
1295
+ executions: []
1296
+ };
1297
+ this.taskExecutor = new PageTaskExecutor(this.page, {
1298
+ cacheId: opts == null ? void 0 : opts.cacheId
1299
+ });
1300
+ this.reportFileName = reportFileName((opts == null ? void 0 : opts.testId) || "web");
1301
+ }
1302
+ };
1303
+ var Page = class {
1304
+ evaluate(pageFunction, arg) {
1305
+ if (this.pageType === "puppeteer") {
1306
+ return this.underlyingPage.evaluate(pageFunction, arg);
1307
+ }
1308
+ return this.underlyingPage.evaluate(pageFunction, arg);
1309
+ }
1310
+ getElementInfos() {
1311
+ return __async(this, null, function*() {
1312
+ const scripts = yield getExtraReturnLogic();
1313
+ const captureElementSnapshot = yield this.evaluate(scripts);
1314
+ return captureElementSnapshot;
1315
+ });
1316
+ }
1317
+ size() {
1318
+ return __async(this, null, function*() {
1319
+ if (this.viewportSize) return this.viewportSize;
1320
+ const sizeInfo = yield this.evaluate(()=>{
1321
+ return {
1322
+ width: document.documentElement.clientWidth,
1323
+ height: document.documentElement.clientHeight,
1324
+ dpr: window.devicePixelRatio
1325
+ };
1326
+ });
1327
+ this.viewportSize = sizeInfo;
1328
+ return sizeInfo;
1329
+ });
1330
+ }
1331
+ screenshotBase64() {
1332
+ return __async(this, null, function*() {
1333
+ const viewportSize = yield this.size();
1334
+ const path2 = (0, _utils.getTmpFile)("png");
1335
+ yield this.underlyingPage.screenshot({
1336
+ path: path2,
1337
+ type: "png"
1338
+ });
1339
+ let buf;
1340
+ if (viewportSize.dpr > 1) {
1341
+ buf = yield (0, _img.resizeImg)((0, _fs.readFileSync)(path2), {
1342
+ width: viewportSize.width,
1343
+ height: viewportSize.height
1344
+ });
1345
+ (0, _fs.writeFileSync)(path2, buf);
1346
+ }
1347
+ return (0, _img.base64Encoded)(path2, true);
1348
+ });
1349
+ }
1350
+ url() {
1351
+ return this.underlyingPage.url();
1352
+ }
1353
+ get mouse() {
1354
+ return {
1355
+ click: (x, y, options)=>__async(this, null, function*() {
1356
+ return this.underlyingPage.mouse.click(x, y, {
1357
+ button: (options == null ? void 0 : options.button) || "left"
1358
+ });
1359
+ }),
1360
+ wheel: (deltaX, deltaY)=>__async(this, null, function*() {
1361
+ if (this.pageType === "puppeteer") {
1362
+ yield this.underlyingPage.mouse.wheel({
1363
+ deltaX,
1364
+ deltaY
1365
+ });
1366
+ } else if (this.pageType === "playwright") {
1367
+ yield this.underlyingPage.mouse.wheel(deltaX, deltaY);
1368
+ }
1369
+ }),
1370
+ move: (x, y)=>__async(this, null, function*() {
1371
+ return this.underlyingPage.mouse.move(x, y);
1372
+ })
1373
+ };
1374
+ }
1375
+ get keyboard() {
1376
+ return {
1377
+ type: (text)=>__async(this, null, function*() {
1378
+ return this.underlyingPage.keyboard.type(text);
1379
+ }),
1380
+ press: (key)=>__async(this, null, function*() {
1381
+ return this.underlyingPage.keyboard.press(key);
1382
+ }),
1383
+ down: (key)=>__async(this, null, function*() {
1384
+ return this.underlyingPage.keyboard.down(key);
1385
+ }),
1386
+ up: (key)=>__async(this, null, function*() {
1387
+ return this.underlyingPage.keyboard.up(key);
1388
+ })
1389
+ };
1390
+ }
1391
+ clearInput(element) {
1392
+ return __async(this, null, function*() {
1393
+ if (!element) {
1394
+ return;
1395
+ }
1396
+ yield this.mouse.click(element.center[0], element.center[1]);
1397
+ const isMac = process.platform === "darwin";
1398
+ if (isMac) {
1399
+ yield this.underlyingPage.keyboard.down("Meta");
1400
+ yield this.underlyingPage.keyboard.press("a");
1401
+ yield this.underlyingPage.keyboard.up("Meta");
1402
+ } else {
1403
+ yield this.underlyingPage.keyboard.down("Control");
1404
+ yield this.underlyingPage.keyboard.press("a");
1405
+ yield this.underlyingPage.keyboard.up("Control");
1406
+ }
1407
+ yield this.keyboard.press("Backspace");
1408
+ });
1409
+ }
1410
+ scrollUntilTop() {
1411
+ return this.mouse.wheel(0, -9999999);
1412
+ }
1413
+ scrollUntilBottom() {
1414
+ return this.mouse.wheel(0, 9999999);
1415
+ }
1416
+ scrollUpOneScreen() {
1417
+ return __async(this, null, function*() {
1418
+ const innerHeight = yield this.evaluate(()=>window.innerHeight);
1419
+ const distance = innerHeight * 0.7;
1420
+ yield this.mouse.wheel(0, -distance);
1421
+ });
1422
+ }
1423
+ scrollDownOneScreen() {
1424
+ return __async(this, null, function*() {
1425
+ const innerHeight = yield this.evaluate(()=>window.innerHeight);
1426
+ const distance = innerHeight * 0.7;
1427
+ yield this.mouse.wheel(0, distance);
1428
+ });
1429
+ }
1430
+ constructor(underlyingPage, pageType){
1431
+ this.underlyingPage = underlyingPage;
1432
+ this.pageType = pageType;
1214
1433
  }
1215
- },
1216
- move: async (x, y) => this.page.mouse.move(x, y)
1217
1434
  };
1218
- }
1219
- get keyboard() {
1220
- return {
1221
- type: async (text) => this.page.keyboard.type(text),
1222
- press: async (key) => this.page.keyboard.press(key),
1223
- down: async (key) => this.page.keyboard.down(key),
1224
- up: async (key) => this.page.keyboard.up(key)
1435
+ // src/puppeteer/page.ts
1436
+ var WebPage = class extends Page {
1437
+ constructor(page){
1438
+ super(page, "puppeteer");
1439
+ }
1440
+ };
1441
+ // src/puppeteer/index.ts
1442
+ var PuppeteerAgent = class extends PageAgent {
1443
+ constructor(page, opts){
1444
+ const webPage = new WebPage(page);
1445
+ super(webPage, opts);
1446
+ }
1225
1447
  };
1226
- }
1227
- async clearInput(element) {
1228
- if (!element) {
1229
- return;
1230
- }
1231
- await this.mouse.click(element.center[0], element.center[1]);
1232
- const isMac = process.platform === "darwin";
1233
- if (isMac) {
1234
- await this.page.keyboard.down("Meta");
1235
- await this.page.keyboard.press("a");
1236
- await this.page.keyboard.up("Meta");
1237
- } else {
1238
- await this.page.keyboard.down("Control");
1239
- await this.page.keyboard.press("a");
1240
- await this.page.keyboard.up("Control");
1241
- }
1242
- await this.keyboard.press("Backspace");
1243
- }
1244
- scrollUntilTop() {
1245
- return this.mouse.wheel(0, -9999999);
1246
- }
1247
- scrollUntilBottom() {
1248
- return this.mouse.wheel(0, 9999999);
1249
- }
1250
- async scrollUpOneScreen() {
1251
- const innerHeight = await this.evaluate(() => window.innerHeight);
1252
- const distance = innerHeight * 0.7;
1253
- await this.mouse.wheel(0, -distance);
1254
- }
1255
- async scrollDownOneScreen() {
1256
- const innerHeight = await this.evaluate(() => window.innerHeight);
1257
- const distance = innerHeight * 0.7;
1258
- await this.mouse.wheel(0, distance);
1259
- }
1260
- };
1261
-
1262
- // src/puppeteer/page.ts
1263
- var WebPage = class extends Page {
1264
- constructor(page) {
1265
- super(page, "puppeteer");
1266
- }
1267
- };
1268
-
1269
- // src/puppeteer/index.ts
1270
- var PuppeteerAgent = class extends PageAgent {
1271
- constructor(page, opts) {
1272
- const webPage = new WebPage(page);
1273
- super(webPage, opts);
1274
- }
1275
- };
1276
- // Annotate the CommonJS export names for ESM import in node:
1277
- 0 && (module.exports = {
1278
- PuppeteerAgent,
1279
- PuppeteerWebPage
1280
1448
  });