@midscene/web 0.7.2 → 0.7.3-beta-20241104100519.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,1491 @@
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"), require("@midscene/shared/constants"), require("@midscene/core/utils"), require("assert"), require("fs"), require("path"), require("@midscene/shared/fs"), require("@midscene/shared/utils"), require("@midscene/shared/img"));
3
+ else if (typeof define === "function" && define.amd) define([
4
+ "exports",
5
+ "@midscene/core",
6
+ "@midscene/shared/constants",
7
+ "@midscene/core/utils",
8
+ "assert",
9
+ "fs",
10
+ "path",
11
+ "@midscene/shared/fs",
12
+ "@midscene/shared/utils",
13
+ "@midscene/shared/img"
14
+ ], factory);
15
+ else if (global = typeof globalThis !== "undefined" ? globalThis : global || self) factory(global.puppeteer = {}, global.core, global.constants, global.utils, global.assert, global.fs, global.path, global.fs, global.utils, global.img);
16
+ })(this, function(exports, _core, _constants, _utils, _assert, _fs, _path, _fs1, _utils1, _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);
501
49
  }
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: []
515
- };
516
- this.newCache = {
517
- aiTasks: []
518
- };
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;
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
+ };
532
58
  }
533
- if (type === "plan") {
534
- return this.readCache(
535
- pageContext,
536
- type,
537
- actionPrompt,
538
- aiTasks[index].tasks
539
- );
59
+ var cache = _getRequireWildcardCache(nodeInterop);
60
+ if (cache && cache.has(obj)) {
61
+ return cache.get(obj);
540
62
  }
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
- };
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;
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
+ }
568
76
  }
569
- return true;
570
- }))) {
571
- return false;
572
- }
573
- if (taskRes && taskRes.type === type && taskRes.prompt === userPrompt && this.pageContextEqual(taskRes.pageContext, pageContext)) {
574
- return taskRes.response;
575
- }
576
- }
577
- return false;
578
- }
579
- pageContextEqual(taskPageContext, pageContext) {
580
- return taskPageContext.size.width === pageContext.size.width && taskPageContext.size.height === pageContext.size.height;
581
- }
582
- /**
583
- * Generate task cache data.
584
- * This method is mainly used to create or obtain some cached data for tasks, and it returns a new cache object.
585
- * In the cache object, it may contain task-related information, states, or other necessary data.
586
- * 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
- * @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;
77
+ newObj.default = obj;
78
+ if (cache) {
79
+ cache.set(obj, newObj);
600
80
  }
601
- return jsonData;
602
- } catch (err) {
603
- return void 0;
604
- }
81
+ return newObj;
605
82
  }
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
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;
644
105
  };
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);
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
+ });
660
118
  }
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;
119
+ return to;
120
+ };
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
+ }
678
137
  };
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);
138
+ var rejected = (value)=>{
139
+ try {
140
+ step(generator.throw(value));
141
+ } catch (e) {
142
+ reject(e);
694
143
  }
695
- locateResult = await callAI(...message);
696
- (0, import_node_assert2.default)(locateResult);
697
- return locateResult;
698
- }
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;
699
502
  });
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
- }
711
- if (!element) {
712
- task.log = {
713
- dump: insightDump
714
- };
715
- throw new Error(`Element not found: ${param.prompt}`);
503
+ }
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;
521
+ }
522
+ };
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();
716
529
  }
530
+ const url = yield 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
+ });
717
559
  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;
560
+ content: elementsInfo,
561
+ size: {
562
+ width,
563
+ height
564
+ },
565
+ screenshotBase64,
566
+ screenshotBase64WithElementMarker,
567
+ url
743
568
  };
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;
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
+ }
759
623
  }
624
+ }
625
+ return false;
626
+ }
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
+ });
760
656
  return {
761
- output: assertion,
762
- log: {
763
- dump: insightDump
764
- }
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
+ }
765
670
  };
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 === "") {
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
+ /**
699
+ * Generate task cache data.
700
+ * This method is mainly used to create or obtain some cached data for tasks, and it returns a new cache object.
701
+ * In the cache object, it may contain task-related information, states, or other necessary data.
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.
703
+ * @returns {Object} Returns a new cache object, which may include task cache data.
704
+ */ generateTaskCache() {
705
+ return this.newCache;
706
+ }
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;
725
+ }
726
+ }
727
+ return void 0;
728
+ }
729
+ writeCacheToFile() {
730
+ const midscenePkgInfo = (0, _fs1.getRunningPkgInfo)();
731
+ if (!midscenePkgInfo) {
779
732
  return;
780
- }
781
- await this.page.keyboard.type(taskParam.value);
782
733
  }
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
- );
734
+ if (!_utils1.ifInBrowser) {
735
+ (0, _utils.writeLogFile)({
736
+ fileName: `${this.cacheId}`,
737
+ fileExt: "json",
738
+ fileContent: (0, _utils.stringifyDumpData)(__spreadValues({
739
+ pkgName: midscenePkgInfo.name,
740
+ pkgVersion: midscenePkgInfo.version,
741
+ cacheId: this.cacheId
742
+ }, this.newCache), 2),
743
+ type: "cache"
744
+ });
852
745
  }
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
746
  }
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
- };
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
747
+ constructor(opts){
748
+ this.midscenePkgInfo = (0, _fs1.getRunningPkgInfo)();
749
+ this.cacheId = generateCacheId(opts == null ? void 0 : opts.fileName);
750
+ this.cache = this.readCacheFromFile() || {
751
+ aiTasks: []
752
+ };
753
+ this.newCache = {
754
+ aiTasks: []
755
+ };
1006
756
  }
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
757
  };
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: []
758
+ // src/common/tasks.ts
759
+ var PageTaskExecutor = class {
760
+ recordScreenshot(timing) {
761
+ return __async(this, null, function*() {
762
+ const base64 = yield this.page.screenshotBase64();
763
+ const item = {
764
+ type: "screenshot",
765
+ ts: Date.now(),
766
+ screenshot: base64,
767
+ timing
768
+ };
769
+ return item;
770
+ });
771
+ }
772
+ wrapExecutorWithScreenshot(taskApply) {
773
+ const taskWithScreenshot = __spreadProps(__spreadValues({}, taskApply), {
774
+ executor: (param, context, ...args)=>__async(this, null, function*() {
775
+ const recorder = [];
776
+ const { task } = context;
777
+ task.recorder = recorder;
778
+ const shot = yield this.recordScreenshot(`before ${task.type}`);
779
+ recorder.push(shot);
780
+ const result = yield taskApply.executor(param, context, ...args);
781
+ if (taskApply.type === "Action") {
782
+ yield (0, _utils.sleep)(1e3);
783
+ const shot2 = yield this.recordScreenshot("after Action");
784
+ recorder.push(shot2);
785
+ }
786
+ return result;
787
+ })
788
+ });
789
+ return taskWithScreenshot;
790
+ }
791
+ convertPlanToExecutable(plans, cacheGroup) {
792
+ return __async(this, null, function*() {
793
+ const tasks = plans.map((plan2)=>{
794
+ if (plan2.type === "Locate") {
795
+ const taskFind = {
796
+ type: "Insight",
797
+ subType: "Locate",
798
+ param: plan2.param,
799
+ quickAnswer: plan2.quickAnswer,
800
+ executor: (param, taskContext)=>__async(this, null, function*() {
801
+ const { task } = taskContext;
802
+ let insightDump;
803
+ const dumpCollector = (dump)=>{
804
+ insightDump = dump;
805
+ };
806
+ this.insight.onceDumpUpdatedFn = dumpCollector;
807
+ const pageContext = yield this.insight.contextRetrieverFn();
808
+ const locateCache = cacheGroup == null ? void 0 : cacheGroup.readCache(pageContext, "locate", param.prompt);
809
+ let locateResult;
810
+ const callAI = this.insight.aiVendorFn;
811
+ const element = yield this.insight.locate(param.prompt, {
812
+ quickAnswer: task.quickAnswer,
813
+ callAI: (...message)=>__async(this, null, function*() {
814
+ if (locateCache) {
815
+ locateResult = locateCache;
816
+ return Promise.resolve(locateCache);
817
+ }
818
+ const aiResult = yield callAI(...message);
819
+ locateResult = (0, _core.transformElementPositionToId)(aiResult, pageContext.content);
820
+ (0, _assert.default)(locateResult);
821
+ return locateResult;
822
+ })
823
+ });
824
+ if (locateResult) {
825
+ cacheGroup == null ? void 0 : cacheGroup.saveCache({
826
+ type: "locate",
827
+ pageContext: {
828
+ url: pageContext.url,
829
+ size: pageContext.size
830
+ },
831
+ prompt: param.prompt,
832
+ response: locateResult
833
+ });
834
+ }
835
+ if (!element) {
836
+ task.log = {
837
+ dump: insightDump
838
+ };
839
+ throw new Error(`Element not found: ${param.prompt}`);
840
+ }
841
+ return {
842
+ output: {
843
+ element
844
+ },
845
+ log: {
846
+ dump: insightDump
847
+ },
848
+ cache: {
849
+ hit: Boolean(locateCache)
850
+ }
851
+ };
852
+ })
853
+ };
854
+ return taskFind;
855
+ }
856
+ if (plan2.type === "Assert" || plan2.type === "AssertWithoutThrow") {
857
+ const assertPlan = plan2;
858
+ const taskAssert = {
859
+ type: "Insight",
860
+ subType: "Assert",
861
+ param: assertPlan.param,
862
+ executor: (param, taskContext)=>__async(this, null, function*() {
863
+ const { task } = taskContext;
864
+ let insightDump;
865
+ const dumpCollector = (dump)=>{
866
+ insightDump = dump;
867
+ };
868
+ this.insight.onceDumpUpdatedFn = dumpCollector;
869
+ const assertion = yield this.insight.assert(assertPlan.param.assertion);
870
+ if (!assertion.pass) {
871
+ if (plan2.type === "Assert") {
872
+ task.output = assertion;
873
+ task.log = {
874
+ dump: insightDump
875
+ };
876
+ throw new Error(assertion.thought || "Assertion failed without reason");
877
+ }
878
+ task.error = assertion.thought;
879
+ }
880
+ return {
881
+ output: assertion,
882
+ log: {
883
+ dump: insightDump
884
+ }
885
+ };
886
+ })
887
+ };
888
+ return taskAssert;
889
+ }
890
+ if (plan2.type === "Input") {
891
+ const taskActionInput = {
892
+ type: "Action",
893
+ subType: "Input",
894
+ param: plan2.param,
895
+ executor: (_0, _1)=>__async(this, [
896
+ _0,
897
+ _1
898
+ ], function*(taskParam, { element }) {
899
+ if (element) {
900
+ yield this.page.clearInput(element);
901
+ if (taskParam.value === "") {
902
+ return;
903
+ }
904
+ yield this.page.keyboard.type(taskParam.value);
905
+ }
906
+ })
907
+ };
908
+ return taskActionInput;
909
+ }
910
+ if (plan2.type === "KeyboardPress") {
911
+ const taskActionKeyboardPress = {
912
+ type: "Action",
913
+ subType: "KeyboardPress",
914
+ param: plan2.param,
915
+ executor: (taskParam)=>__async(this, null, function*() {
916
+ (0, _assert.default)(taskParam.value, "No key to press");
917
+ yield this.page.keyboard.press(taskParam.value);
918
+ })
919
+ };
920
+ return taskActionKeyboardPress;
921
+ }
922
+ if (plan2.type === "Tap") {
923
+ const taskActionTap = {
924
+ type: "Action",
925
+ subType: "Tap",
926
+ executor: (_0, _1)=>__async(this, [
927
+ _0,
928
+ _1
929
+ ], function*(param, { element }) {
930
+ (0, _assert.default)(element, "Element not found, cannot tap");
931
+ yield this.page.mouse.click(element.center[0], element.center[1]);
932
+ })
933
+ };
934
+ return taskActionTap;
935
+ }
936
+ if (plan2.type === "Hover") {
937
+ const taskActionHover = {
938
+ type: "Action",
939
+ subType: "Hover",
940
+ executor: (_0, _1)=>__async(this, [
941
+ _0,
942
+ _1
943
+ ], function*(param, { element }) {
944
+ (0, _assert.default)(element, "Element not found, cannot hover");
945
+ yield this.page.mouse.move(element.center[0], element.center[1]);
946
+ })
947
+ };
948
+ return taskActionHover;
949
+ }
950
+ if (plan2.type === "Scroll") {
951
+ const taskActionScroll = {
952
+ type: "Action",
953
+ subType: "Scroll",
954
+ param: plan2.param,
955
+ executor: (taskParam)=>__async(this, null, function*() {
956
+ const scrollToEventName = taskParam.scrollType;
957
+ switch(scrollToEventName){
958
+ case "scrollUntilTop":
959
+ yield this.page.scrollUntilTop();
960
+ break;
961
+ case "scrollUntilBottom":
962
+ yield this.page.scrollUntilBottom();
963
+ break;
964
+ case "scrollUpOneScreen":
965
+ yield this.page.scrollUpOneScreen();
966
+ break;
967
+ case "scrollDownOneScreen":
968
+ yield this.page.scrollDownOneScreen();
969
+ break;
970
+ default:
971
+ console.error("Unknown scroll event type:", scrollToEventName);
972
+ }
973
+ })
974
+ };
975
+ return taskActionScroll;
976
+ }
977
+ if (plan2.type === "Sleep") {
978
+ const taskActionSleep = {
979
+ type: "Action",
980
+ subType: "Sleep",
981
+ param: plan2.param,
982
+ executor: (taskParam)=>__async(this, null, function*() {
983
+ yield (0, _utils.sleep)(taskParam.timeMs || 3e3);
984
+ })
985
+ };
986
+ return taskActionSleep;
987
+ }
988
+ if (plan2.type === "Error") {
989
+ const taskActionError = {
990
+ type: "Action",
991
+ subType: "Error",
992
+ param: plan2.param,
993
+ executor: (taskParam)=>__async(this, null, function*() {
994
+ (0, _assert.default)(taskParam.thought, "An error occurred, but no thought provided");
995
+ throw new Error(taskParam.thought);
996
+ })
997
+ };
998
+ return taskActionError;
999
+ }
1000
+ throw new Error(`Unknown or Unsupported task type: ${plan2.type}`);
1001
+ }).map((task)=>{
1002
+ return this.wrapExecutorWithScreenshot(task);
1003
+ });
1004
+ return tasks;
1005
+ });
1006
+ }
1007
+ action(userPrompt) {
1008
+ return __async(this, null, function*() {
1009
+ const taskExecutor = new _core.Executor(userPrompt);
1010
+ const cacheGroup = this.taskCache.getCacheGroupByPrompt(userPrompt);
1011
+ let plans = [];
1012
+ const planningTask = {
1013
+ type: "Planning",
1014
+ param: {
1015
+ userPrompt
1016
+ },
1017
+ executor: (param)=>__async(this, null, function*() {
1018
+ const pageContext = yield this.insight.contextRetrieverFn();
1019
+ let planResult;
1020
+ const planCache = cacheGroup.readCache(pageContext, "plan", userPrompt);
1021
+ if (planCache) {
1022
+ planResult = planCache;
1023
+ } else {
1024
+ planResult = yield (0, _core.plan)(param.userPrompt, {
1025
+ context: pageContext
1026
+ });
1027
+ }
1028
+ (0, _assert.default)(planResult.plans.length > 0, "No plans found");
1029
+ plans = planResult.plans;
1030
+ cacheGroup.saveCache({
1031
+ type: "plan",
1032
+ pageContext: {
1033
+ url: pageContext.url,
1034
+ size: pageContext.size
1035
+ },
1036
+ prompt: userPrompt,
1037
+ response: planResult
1038
+ });
1039
+ return {
1040
+ output: planResult,
1041
+ pageContext,
1042
+ cache: {
1043
+ hit: Boolean(planCache)
1044
+ }
1045
+ };
1046
+ })
1047
+ };
1048
+ yield taskExecutor.append(this.wrapExecutorWithScreenshot(planningTask));
1049
+ let output = yield taskExecutor.flush();
1050
+ if (taskExecutor.isInErrorState()) {
1051
+ return {
1052
+ output,
1053
+ executor: taskExecutor
1054
+ };
1055
+ }
1056
+ const executables = yield this.convertPlanToExecutable(plans, cacheGroup);
1057
+ yield taskExecutor.append(executables);
1058
+ output = yield taskExecutor.flush();
1059
+ return {
1060
+ output,
1061
+ executor: taskExecutor
1062
+ };
1063
+ });
1064
+ }
1065
+ query(demand) {
1066
+ return __async(this, null, function*() {
1067
+ const description = typeof demand === "string" ? demand : JSON.stringify(demand);
1068
+ const taskExecutor = new _core.Executor(description);
1069
+ const queryTask = {
1070
+ type: "Insight",
1071
+ subType: "Query",
1072
+ param: {
1073
+ dataDemand: demand
1074
+ },
1075
+ executor: (param)=>__async(this, null, function*() {
1076
+ let insightDump;
1077
+ const dumpCollector = (dump)=>{
1078
+ insightDump = dump;
1079
+ };
1080
+ this.insight.onceDumpUpdatedFn = dumpCollector;
1081
+ const data = yield this.insight.extract(param.dataDemand);
1082
+ return {
1083
+ output: data,
1084
+ log: {
1085
+ dump: insightDump
1086
+ }
1087
+ };
1088
+ })
1089
+ };
1090
+ yield taskExecutor.append(this.wrapExecutorWithScreenshot(queryTask));
1091
+ const output = yield taskExecutor.flush();
1092
+ return {
1093
+ output,
1094
+ executor: taskExecutor
1095
+ };
1096
+ });
1097
+ }
1098
+ assert(assertion) {
1099
+ return __async(this, null, function*() {
1100
+ const description = `assert: ${assertion}`;
1101
+ const taskExecutor = new _core.Executor(description);
1102
+ const assertionPlan = {
1103
+ type: "Assert",
1104
+ param: {
1105
+ assertion
1106
+ }
1107
+ };
1108
+ const assertTask = yield this.convertPlanToExecutable([
1109
+ assertionPlan
1110
+ ]);
1111
+ yield taskExecutor.append(this.wrapExecutorWithScreenshot(assertTask[0]));
1112
+ const output = yield taskExecutor.flush();
1113
+ return {
1114
+ output,
1115
+ executor: taskExecutor
1116
+ };
1117
+ });
1118
+ }
1119
+ waitFor(assertion, opt) {
1120
+ return __async(this, null, function*() {
1121
+ const description = `waitFor: ${assertion}`;
1122
+ const taskExecutor = new _core.Executor(description);
1123
+ const { timeoutMs, checkIntervalMs } = opt;
1124
+ (0, _assert.default)(assertion, "No assertion for waitFor");
1125
+ (0, _assert.default)(timeoutMs, "No timeoutMs for waitFor");
1126
+ (0, _assert.default)(checkIntervalMs, "No checkIntervalMs for waitFor");
1127
+ const overallStartTime = Date.now();
1128
+ let startTime = Date.now();
1129
+ let errorThought = "";
1130
+ while(Date.now() - overallStartTime < timeoutMs){
1131
+ startTime = Date.now();
1132
+ const assertPlan = {
1133
+ type: "AssertWithoutThrow",
1134
+ param: {
1135
+ assertion
1136
+ }
1137
+ };
1138
+ const assertTask = yield this.convertPlanToExecutable([
1139
+ assertPlan
1140
+ ]);
1141
+ yield taskExecutor.append(this.wrapExecutorWithScreenshot(assertTask[0]));
1142
+ const output = yield taskExecutor.flush();
1143
+ if (output == null ? void 0 : output.pass) {
1144
+ return {
1145
+ output: void 0,
1146
+ executor: taskExecutor
1147
+ };
1148
+ }
1149
+ errorThought = (output == null ? void 0 : output.thought) || "unknown error";
1150
+ const now = Date.now();
1151
+ if (now - startTime < checkIntervalMs) {
1152
+ const timeRemaining = checkIntervalMs - (now - startTime);
1153
+ const sleepPlan = {
1154
+ type: "Sleep",
1155
+ param: {
1156
+ timeMs: timeRemaining
1157
+ }
1158
+ };
1159
+ const sleepTask = yield this.convertPlanToExecutable([
1160
+ sleepPlan
1161
+ ]);
1162
+ yield taskExecutor.append(this.wrapExecutorWithScreenshot(sleepTask[0]));
1163
+ yield taskExecutor.flush();
1164
+ }
1165
+ }
1166
+ const errorPlan = {
1167
+ type: "Error",
1168
+ param: {
1169
+ thought: `waitFor timeout: ${errorThought}`
1170
+ }
1171
+ };
1172
+ const errorTask = yield this.convertPlanToExecutable([
1173
+ errorPlan
1174
+ ]);
1175
+ yield taskExecutor.append(errorTask[0]);
1176
+ yield taskExecutor.flush();
1177
+ return {
1178
+ output: void 0,
1179
+ executor: taskExecutor
1180
+ };
1181
+ });
1182
+ }
1183
+ constructor(page, insight, opts){
1184
+ this.page = page;
1185
+ this.insight = insight;
1186
+ this.taskCache = new TaskCache({
1187
+ fileName: opts == null ? void 0 : opts.cacheId
1188
+ });
1189
+ }
1067
1190
  };
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}
1191
+ // src/common/agent.ts
1192
+ var PageAgent = class {
1193
+ getUIContext() {
1194
+ return __async(this, null, function*() {
1195
+ return yield parseContextFromWebPage(this.page);
1196
+ });
1197
+ }
1198
+ resetDump() {
1199
+ this.dump = {
1200
+ groupName: this.opts.groupName,
1201
+ groupDescription: this.opts.groupDescription,
1202
+ executions: []
1203
+ };
1204
+ return this.dump;
1205
+ }
1206
+ appendExecutionDump(execution) {
1207
+ const currentDump = this.dump;
1208
+ currentDump.executions.push(execution);
1209
+ }
1210
+ dumpDataString() {
1211
+ this.dump.groupName = this.opts.groupName;
1212
+ this.dump.groupDescription = this.opts.groupDescription;
1213
+ return (0, _utils.stringifyDumpData)(this.dump);
1214
+ }
1215
+ reportHTMLString() {
1216
+ return (0, _utils.reportHTMLContent)(this.dumpDataString());
1217
+ }
1218
+ writeOutActionDumps() {
1219
+ const { generateReport, autoPrintReportMsg } = this.opts;
1220
+ this.reportFile = (0, _utils.writeLogFile)({
1221
+ fileName: this.reportFileName,
1222
+ fileExt: _utils.groupedActionDumpFileExt,
1223
+ fileContent: this.dumpDataString(),
1224
+ type: "dump",
1225
+ generateReport
1226
+ });
1227
+ if (generateReport && autoPrintReportMsg && this.reportFile) {
1228
+ printReportMsg(this.reportFile);
1229
+ }
1230
+ }
1231
+ aiAction(taskPrompt) {
1232
+ return __async(this, null, function*() {
1233
+ const { executor } = yield this.taskExecutor.action(taskPrompt);
1234
+ this.appendExecutionDump(executor.dump());
1235
+ this.writeOutActionDumps();
1236
+ if (executor.isInErrorState()) {
1237
+ const errorTask = executor.latestErrorTask();
1238
+ throw new Error(`${errorTask == null ? void 0 : errorTask.error}
1102
1239
  ${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}
1240
+ }
1241
+ });
1242
+ }
1243
+ aiQuery(demand) {
1244
+ return __async(this, null, function*() {
1245
+ const { output, executor } = yield this.taskExecutor.query(demand);
1246
+ this.appendExecutionDump(executor.dump());
1247
+ this.writeOutActionDumps();
1248
+ if (executor.isInErrorState()) {
1249
+ const errorTask = executor.latestErrorTask();
1250
+ throw new Error(`${errorTask == null ? void 0 : errorTask.error}
1112
1251
  ${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}
1252
+ }
1253
+ return output;
1254
+ });
1255
+ }
1256
+ aiAssert(assertion, msg, opt) {
1257
+ return __async(this, null, function*() {
1258
+ var _a;
1259
+ const { output, executor } = yield this.taskExecutor.assert(assertion);
1260
+ this.appendExecutionDump(executor.dump());
1261
+ this.writeOutActionDumps();
1262
+ if (output && (opt == null ? void 0 : opt.keepRawResponse)) {
1263
+ return output;
1264
+ }
1265
+ if (!(output == null ? void 0 : output.pass)) {
1266
+ const errMsg = msg || `Assertion failed: ${assertion}`;
1267
+ const reasonMsg = `Reason: ${(output == null ? void 0 : output.thought) || ((_a = executor.latestErrorTask()) == null ? void 0 : _a.error) || "(no_reason)"}`;
1268
+ throw new Error(`${errMsg}
1127
1269
  ${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}
1270
+ }
1271
+ });
1272
+ }
1273
+ aiWaitFor(assertion, opt) {
1274
+ return __async(this, null, function*() {
1275
+ const { executor } = yield this.taskExecutor.waitFor(assertion, {
1276
+ timeoutMs: (opt == null ? void 0 : opt.timeoutMs) || 15 * 1e3,
1277
+ checkIntervalMs: (opt == null ? void 0 : opt.checkIntervalMs) || 3 * 1e3,
1278
+ assertion
1279
+ });
1280
+ this.appendExecutionDump(executor.dump());
1281
+ this.writeOutActionDumps();
1282
+ if (executor.isInErrorState()) {
1283
+ const errorTask = executor.latestErrorTask();
1284
+ throw new Error(`${errorTask == null ? void 0 : errorTask.error}
1141
1285
  ${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);
1286
+ }
1287
+ });
1288
+ }
1289
+ ai(taskPrompt, type = "action") {
1290
+ return __async(this, null, function*() {
1291
+ if (type === "action") {
1292
+ return this.aiAction(taskPrompt);
1293
+ }
1294
+ if (type === "query") {
1295
+ return this.aiQuery(taskPrompt);
1296
+ }
1297
+ if (type === "assert") {
1298
+ return this.aiAssert(taskPrompt);
1299
+ }
1300
+ throw new Error(`Unknown type: ${type}, only support 'action', 'query', 'assert'`);
1301
+ });
1302
+ }
1303
+ destroy() {
1304
+ return __async(this, null, function*() {
1305
+ yield this.page.destroy();
1306
+ });
1307
+ }
1308
+ constructor(page, opts){
1309
+ /**
1310
+ * If true, the agent will not perform any actions
1311
+ */ this.dryMode = false;
1312
+ this.page = page;
1313
+ this.opts = Object.assign({
1314
+ generateReport: true,
1315
+ autoPrintReportMsg: true,
1316
+ groupName: "Midscene Report",
1317
+ groupDescription: ""
1318
+ }, opts || {});
1319
+ this.insight = new _core.Insight(()=>__async(this, null, function*() {
1320
+ return this.getUIContext();
1321
+ }), {
1322
+ generateElement: ({ content, rect })=>new WebElementInfo({
1323
+ content: content || "",
1324
+ rect,
1325
+ page,
1326
+ id: "",
1327
+ attributes: {
1328
+ nodeType: _constants.NodeType.CONTAINER
1329
+ },
1330
+ indexId: 0
1331
+ })
1332
+ });
1333
+ this.taskExecutor = new PageTaskExecutor(this.page, this.insight, {
1334
+ cacheId: opts == null ? void 0 : opts.cacheId
1335
+ });
1336
+ this.dump = this.resetDump();
1337
+ this.reportFileName = reportFileName((opts == null ? void 0 : opts.testId) || "web");
1338
+ }
1339
+ };
1340
+ var Page = class {
1341
+ evaluate(pageFunction, arg) {
1342
+ if (this.pageType === "puppeteer") {
1343
+ return this.underlyingPage.evaluate(pageFunction, arg);
1344
+ }
1345
+ return this.underlyingPage.evaluate(pageFunction, arg);
1346
+ }
1347
+ getElementInfos() {
1348
+ return __async(this, null, function*() {
1349
+ const scripts = yield getExtraReturnLogic();
1350
+ const captureElementSnapshot = yield this.evaluate(scripts);
1351
+ return captureElementSnapshot;
1352
+ });
1353
+ }
1354
+ size() {
1355
+ return __async(this, null, function*() {
1356
+ if (this.viewportSize) return this.viewportSize;
1357
+ const sizeInfo = yield this.evaluate(()=>{
1358
+ return {
1359
+ width: document.documentElement.clientWidth,
1360
+ height: document.documentElement.clientHeight,
1361
+ dpr: window.devicePixelRatio
1362
+ };
1363
+ });
1364
+ this.viewportSize = sizeInfo;
1365
+ return sizeInfo;
1366
+ });
1367
+ }
1368
+ screenshotBase64() {
1369
+ return __async(this, null, function*() {
1370
+ const viewportSize = yield this.size();
1371
+ const path2 = (0, _utils.getTmpFile)("png");
1372
+ yield this.underlyingPage.screenshot({
1373
+ path: path2,
1374
+ type: "png"
1375
+ });
1376
+ let buf;
1377
+ if (viewportSize.dpr > 1) {
1378
+ buf = yield (0, _img.resizeImg)((0, _fs.readFileSync)(path2), {
1379
+ width: viewportSize.width,
1380
+ height: viewportSize.height
1381
+ });
1382
+ (0, _fs.writeFileSync)(path2, buf);
1383
+ }
1384
+ return (0, _img.base64Encoded)(path2, true);
1385
+ });
1386
+ }
1387
+ url() {
1388
+ return __async(this, null, function*() {
1389
+ return this.underlyingPage.url();
1390
+ });
1391
+ }
1392
+ get mouse() {
1393
+ return {
1394
+ click: (x, y, options)=>__async(this, null, function*() {
1395
+ return this.underlyingPage.mouse.click(x, y, {
1396
+ button: (options == null ? void 0 : options.button) || "left"
1397
+ });
1398
+ }),
1399
+ wheel: (deltaX, deltaY)=>__async(this, null, function*() {
1400
+ if (this.pageType === "puppeteer") {
1401
+ yield this.underlyingPage.mouse.wheel({
1402
+ deltaX,
1403
+ deltaY
1404
+ });
1405
+ } else if (this.pageType === "playwright") {
1406
+ yield this.underlyingPage.mouse.wheel(deltaX, deltaY);
1407
+ }
1408
+ }),
1409
+ move: (x, y)=>__async(this, null, function*() {
1410
+ return this.underlyingPage.mouse.move(x, y);
1411
+ })
1412
+ };
1413
+ }
1414
+ get keyboard() {
1415
+ return {
1416
+ type: (text)=>__async(this, null, function*() {
1417
+ return this.underlyingPage.keyboard.type(text);
1418
+ }),
1419
+ press: (key)=>__async(this, null, function*() {
1420
+ return this.underlyingPage.keyboard.press(key);
1421
+ }),
1422
+ down: (key)=>__async(this, null, function*() {
1423
+ return this.underlyingPage.keyboard.down(key);
1424
+ }),
1425
+ up: (key)=>__async(this, null, function*() {
1426
+ return this.underlyingPage.keyboard.up(key);
1427
+ })
1428
+ };
1429
+ }
1430
+ clearInput(element) {
1431
+ return __async(this, null, function*() {
1432
+ if (!element) {
1433
+ console.warn("No element to clear input");
1434
+ return;
1435
+ }
1436
+ yield this.mouse.click(element.center[0], element.center[1]);
1437
+ const isMac = process.platform === "darwin";
1438
+ if (isMac) {
1439
+ yield this.underlyingPage.keyboard.down("Meta");
1440
+ yield this.underlyingPage.keyboard.press("a");
1441
+ yield this.underlyingPage.keyboard.up("Meta");
1442
+ } else {
1443
+ yield this.underlyingPage.keyboard.down("Control");
1444
+ yield this.underlyingPage.keyboard.press("a");
1445
+ yield this.underlyingPage.keyboard.up("Control");
1446
+ }
1447
+ yield this.keyboard.press("Backspace");
1448
+ });
1449
+ }
1450
+ scrollUntilTop() {
1451
+ return this.mouse.wheel(0, -9999999);
1452
+ }
1453
+ scrollUntilBottom() {
1454
+ return this.mouse.wheel(0, 9999999);
1455
+ }
1456
+ scrollUpOneScreen() {
1457
+ return __async(this, null, function*() {
1458
+ const innerHeight = yield this.evaluate(()=>window.innerHeight);
1459
+ const distance = innerHeight * 0.7;
1460
+ yield this.mouse.wheel(0, -distance);
1461
+ });
1462
+ }
1463
+ scrollDownOneScreen() {
1464
+ return __async(this, null, function*() {
1465
+ const innerHeight = yield this.evaluate(()=>window.innerHeight);
1466
+ const distance = innerHeight * 0.7;
1467
+ yield this.mouse.wheel(0, distance);
1468
+ });
1469
+ }
1470
+ destroy() {
1471
+ return __async(this, null, function*() {});
1472
+ }
1473
+ constructor(underlyingPage, pageType){
1474
+ this.underlyingPage = underlyingPage;
1475
+ this.pageType = pageType;
1214
1476
  }
1215
- },
1216
- move: async (x, y) => this.page.mouse.move(x, y)
1217
1477
  };
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)
1478
+ // src/puppeteer/page.ts
1479
+ var WebPage = class extends Page {
1480
+ constructor(page){
1481
+ super(page, "puppeteer");
1482
+ }
1483
+ };
1484
+ // src/puppeteer/index.ts
1485
+ var PuppeteerAgent = class extends PageAgent {
1486
+ constructor(page, opts){
1487
+ const webPage = new WebPage(page);
1488
+ super(webPage, opts);
1489
+ }
1225
1490
  };
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
1491
  });