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