@midscene/web 0.8.3 → 0.8.5-beta-20241122072506.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/es/appium.js CHANGED
@@ -1028,7 +1028,15 @@ var PageTaskExecutor = class {
1028
1028
  recorder.push(shot);
1029
1029
  const result = await taskApply.executor(param, context, ...args);
1030
1030
  if (taskApply.type === "Action") {
1031
- await (0, import_utils3.sleep)(300);
1031
+ await Promise.all([
1032
+ (async () => {
1033
+ await (0, import_utils3.sleep)(100);
1034
+ if (this.page.waitUntilNetworkIdle) {
1035
+ await this.page.waitUntilNetworkIdle();
1036
+ }
1037
+ })(),
1038
+ (0, import_utils3.sleep)(300)
1039
+ ]);
1032
1040
  }
1033
1041
  if (appendAfterExecution) {
1034
1042
  const shot2 = await this.recordScreenshot("after Action");
@@ -1049,6 +1057,7 @@ var PageTaskExecutor = class {
1049
1057
  quickAnswer: plan2.quickAnswer,
1050
1058
  executor: async (param, taskContext) => {
1051
1059
  const { task } = taskContext;
1060
+ (0, import_node_assert.default)(param == null ? void 0 : param.prompt, "No prompt to locate");
1052
1061
  let insightDump;
1053
1062
  const dumpCollector = (dump) => {
1054
1063
  insightDump = dump;
@@ -1164,7 +1173,7 @@ var PageTaskExecutor = class {
1164
1173
  executor: async (taskParam, { element }) => {
1165
1174
  if (element) {
1166
1175
  await this.page.clearInput(element);
1167
- if (!taskParam || taskParam.value === "") {
1176
+ if (!taskParam || !taskParam.value) {
1168
1177
  return;
1169
1178
  }
1170
1179
  await this.page.keyboard.type(taskParam.value);
@@ -1179,7 +1188,7 @@ var PageTaskExecutor = class {
1179
1188
  subType: "KeyboardPress",
1180
1189
  param: plan2.param,
1181
1190
  executor: async (taskParam) => {
1182
- (0, import_node_assert.default)(taskParam.value, "No key to press");
1191
+ (0, import_node_assert.default)(taskParam == null ? void 0 : taskParam.value, "No key to press");
1183
1192
  await this.page.keyboard.press(taskParam.value);
1184
1193
  }
1185
1194
  };
@@ -1243,7 +1252,7 @@ var PageTaskExecutor = class {
1243
1252
  subType: "Sleep",
1244
1253
  param: plan2.param,
1245
1254
  executor: async (taskParam) => {
1246
- await (0, import_utils3.sleep)(taskParam.timeMs || 3e3);
1255
+ await (0, import_utils3.sleep)((taskParam == null ? void 0 : taskParam.timeMs) || 3e3);
1247
1256
  }
1248
1257
  };
1249
1258
  return taskActionSleep;
@@ -1255,17 +1264,17 @@ var PageTaskExecutor = class {
1255
1264
  param: plan2.param,
1256
1265
  executor: async (taskParam) => {
1257
1266
  (0, import_node_assert.default)(
1258
- taskParam.thought,
1267
+ taskParam == null ? void 0 : taskParam.thought,
1259
1268
  "An error occurred, but no thought provided"
1260
1269
  );
1261
- throw new Error(taskParam.thought);
1270
+ throw new Error((taskParam == null ? void 0 : taskParam.thought) || "error without thought");
1262
1271
  }
1263
1272
  };
1264
1273
  return taskActionError;
1265
1274
  }
1266
1275
  throw new Error(`Unknown or Unsupported task type: ${plan2.type}`);
1267
1276
  });
1268
- const tasksWithScreenshot = tasks.map(
1277
+ const wrappedTasks = tasks.map(
1269
1278
  (task, index) => {
1270
1279
  if (task.type === "Action") {
1271
1280
  return this.prependExecutorWithScreenshot(
@@ -1276,7 +1285,7 @@ var PageTaskExecutor = class {
1276
1285
  return task;
1277
1286
  }
1278
1287
  );
1279
- return tasksWithScreenshot;
1288
+ return wrappedTasks;
1280
1289
  }
1281
1290
  async action(userPrompt) {
1282
1291
  const taskExecutor = new import_core2.Executor(userPrompt);
@@ -1717,14 +1726,29 @@ var frameId = 0;
1717
1726
  function getFrameId() {
1718
1727
  return frameId;
1719
1728
  }
1729
+ var hashMap = {};
1720
1730
  function midsceneGenerateHash(content, rect) {
1721
1731
  const combined = JSON.stringify({
1722
1732
  content,
1723
1733
  rect,
1724
1734
  _midscene_frame_id: getFrameId()
1725
1735
  });
1736
+ let sliceLength = 5;
1737
+ let hashInDigits = "";
1726
1738
  const hashHex = (0, import_js_sha256.default)(combined);
1727
- return hashHex.slice(0, 10);
1739
+ while (sliceLength < combined.length - 1) {
1740
+ hashInDigits = Number.parseInt(
1741
+ hashHex.slice(0, sliceLength),
1742
+ 16
1743
+ ).toString();
1744
+ if (hashMap[hashInDigits] && hashMap[hashInDigits] !== combined) {
1745
+ sliceLength++;
1746
+ continue;
1747
+ }
1748
+ hashMap[hashInDigits] = combined;
1749
+ break;
1750
+ }
1751
+ return hashInDigits;
1728
1752
  }
1729
1753
 
1730
1754
  // src/extractor/constants.ts
@@ -618,7 +618,15 @@ var PageTaskExecutor = class {
618
618
  recorder.push(shot);
619
619
  const result = await taskApply.executor(param, context, ...args);
620
620
  if (taskApply.type === "Action") {
621
- await (0, import_utils5.sleep)(300);
621
+ await Promise.all([
622
+ (async () => {
623
+ await (0, import_utils5.sleep)(100);
624
+ if (this.page.waitUntilNetworkIdle) {
625
+ await this.page.waitUntilNetworkIdle();
626
+ }
627
+ })(),
628
+ (0, import_utils5.sleep)(300)
629
+ ]);
622
630
  }
623
631
  if (appendAfterExecution) {
624
632
  const shot2 = await this.recordScreenshot("after Action");
@@ -639,6 +647,7 @@ var PageTaskExecutor = class {
639
647
  quickAnswer: plan2.quickAnswer,
640
648
  executor: async (param, taskContext) => {
641
649
  const { task } = taskContext;
650
+ (0, import_node_assert2.default)(param == null ? void 0 : param.prompt, "No prompt to locate");
642
651
  let insightDump;
643
652
  const dumpCollector = (dump) => {
644
653
  insightDump = dump;
@@ -754,7 +763,7 @@ var PageTaskExecutor = class {
754
763
  executor: async (taskParam, { element }) => {
755
764
  if (element) {
756
765
  await this.page.clearInput(element);
757
- if (!taskParam || taskParam.value === "") {
766
+ if (!taskParam || !taskParam.value) {
758
767
  return;
759
768
  }
760
769
  await this.page.keyboard.type(taskParam.value);
@@ -769,7 +778,7 @@ var PageTaskExecutor = class {
769
778
  subType: "KeyboardPress",
770
779
  param: plan2.param,
771
780
  executor: async (taskParam) => {
772
- (0, import_node_assert2.default)(taskParam.value, "No key to press");
781
+ (0, import_node_assert2.default)(taskParam == null ? void 0 : taskParam.value, "No key to press");
773
782
  await this.page.keyboard.press(taskParam.value);
774
783
  }
775
784
  };
@@ -833,7 +842,7 @@ var PageTaskExecutor = class {
833
842
  subType: "Sleep",
834
843
  param: plan2.param,
835
844
  executor: async (taskParam) => {
836
- await (0, import_utils5.sleep)(taskParam.timeMs || 3e3);
845
+ await (0, import_utils5.sleep)((taskParam == null ? void 0 : taskParam.timeMs) || 3e3);
837
846
  }
838
847
  };
839
848
  return taskActionSleep;
@@ -845,17 +854,17 @@ var PageTaskExecutor = class {
845
854
  param: plan2.param,
846
855
  executor: async (taskParam) => {
847
856
  (0, import_node_assert2.default)(
848
- taskParam.thought,
857
+ taskParam == null ? void 0 : taskParam.thought,
849
858
  "An error occurred, but no thought provided"
850
859
  );
851
- throw new Error(taskParam.thought);
860
+ throw new Error((taskParam == null ? void 0 : taskParam.thought) || "error without thought");
852
861
  }
853
862
  };
854
863
  return taskActionError;
855
864
  }
856
865
  throw new Error(`Unknown or Unsupported task type: ${plan2.type}`);
857
866
  });
858
- const tasksWithScreenshot = tasks.map(
867
+ const wrappedTasks = tasks.map(
859
868
  (task, index) => {
860
869
  if (task.type === "Action") {
861
870
  return this.prependExecutorWithScreenshot(
@@ -866,7 +875,7 @@ var PageTaskExecutor = class {
866
875
  return task;
867
876
  }
868
877
  );
869
- return tasksWithScreenshot;
878
+ return wrappedTasks;
870
879
  }
871
880
  async action(userPrompt) {
872
881
  const taskExecutor = new import_core2.Executor(userPrompt);
package/dist/es/index.js CHANGED
@@ -1036,7 +1036,15 @@ var PageTaskExecutor = class {
1036
1036
  recorder.push(shot);
1037
1037
  const result = await taskApply.executor(param, context, ...args);
1038
1038
  if (taskApply.type === "Action") {
1039
- await (0, import_utils3.sleep)(300);
1039
+ await Promise.all([
1040
+ (async () => {
1041
+ await (0, import_utils3.sleep)(100);
1042
+ if (this.page.waitUntilNetworkIdle) {
1043
+ await this.page.waitUntilNetworkIdle();
1044
+ }
1045
+ })(),
1046
+ (0, import_utils3.sleep)(300)
1047
+ ]);
1040
1048
  }
1041
1049
  if (appendAfterExecution) {
1042
1050
  const shot2 = await this.recordScreenshot("after Action");
@@ -1057,6 +1065,7 @@ var PageTaskExecutor = class {
1057
1065
  quickAnswer: plan2.quickAnswer,
1058
1066
  executor: async (param, taskContext) => {
1059
1067
  const { task } = taskContext;
1068
+ (0, import_node_assert.default)(param == null ? void 0 : param.prompt, "No prompt to locate");
1060
1069
  let insightDump;
1061
1070
  const dumpCollector = (dump) => {
1062
1071
  insightDump = dump;
@@ -1172,7 +1181,7 @@ var PageTaskExecutor = class {
1172
1181
  executor: async (taskParam, { element }) => {
1173
1182
  if (element) {
1174
1183
  await this.page.clearInput(element);
1175
- if (!taskParam || taskParam.value === "") {
1184
+ if (!taskParam || !taskParam.value) {
1176
1185
  return;
1177
1186
  }
1178
1187
  await this.page.keyboard.type(taskParam.value);
@@ -1187,7 +1196,7 @@ var PageTaskExecutor = class {
1187
1196
  subType: "KeyboardPress",
1188
1197
  param: plan2.param,
1189
1198
  executor: async (taskParam) => {
1190
- (0, import_node_assert.default)(taskParam.value, "No key to press");
1199
+ (0, import_node_assert.default)(taskParam == null ? void 0 : taskParam.value, "No key to press");
1191
1200
  await this.page.keyboard.press(taskParam.value);
1192
1201
  }
1193
1202
  };
@@ -1251,7 +1260,7 @@ var PageTaskExecutor = class {
1251
1260
  subType: "Sleep",
1252
1261
  param: plan2.param,
1253
1262
  executor: async (taskParam) => {
1254
- await (0, import_utils3.sleep)(taskParam.timeMs || 3e3);
1263
+ await (0, import_utils3.sleep)((taskParam == null ? void 0 : taskParam.timeMs) || 3e3);
1255
1264
  }
1256
1265
  };
1257
1266
  return taskActionSleep;
@@ -1263,17 +1272,17 @@ var PageTaskExecutor = class {
1263
1272
  param: plan2.param,
1264
1273
  executor: async (taskParam) => {
1265
1274
  (0, import_node_assert.default)(
1266
- taskParam.thought,
1275
+ taskParam == null ? void 0 : taskParam.thought,
1267
1276
  "An error occurred, but no thought provided"
1268
1277
  );
1269
- throw new Error(taskParam.thought);
1278
+ throw new Error((taskParam == null ? void 0 : taskParam.thought) || "error without thought");
1270
1279
  }
1271
1280
  };
1272
1281
  return taskActionError;
1273
1282
  }
1274
1283
  throw new Error(`Unknown or Unsupported task type: ${plan2.type}`);
1275
1284
  });
1276
- const tasksWithScreenshot = tasks.map(
1285
+ const wrappedTasks = tasks.map(
1277
1286
  (task, index) => {
1278
1287
  if (task.type === "Action") {
1279
1288
  return this.prependExecutorWithScreenshot(
@@ -1284,7 +1293,7 @@ var PageTaskExecutor = class {
1284
1293
  return task;
1285
1294
  }
1286
1295
  );
1287
- return tasksWithScreenshot;
1296
+ return wrappedTasks;
1288
1297
  }
1289
1298
  async action(userPrompt) {
1290
1299
  const taskExecutor = new import_core2.Executor(userPrompt);
@@ -1994,6 +2003,12 @@ var WebPage2 = class extends Page {
1994
2003
  constructor(page) {
1995
2004
  super(page, "puppeteer");
1996
2005
  }
2006
+ async waitUntilNetworkIdle(options) {
2007
+ await this.underlyingPage.waitForNetworkIdle({
2008
+ idleTime: (options == null ? void 0 : options.idleTime) || 500,
2009
+ concurrency: (options == null ? void 0 : options.concurrency) || 2
2010
+ });
2011
+ }
1997
2012
  };
1998
2013
 
1999
2014
  // src/puppeteer/index.ts
@@ -2020,14 +2035,29 @@ var frameId = 0;
2020
2035
  function getFrameId() {
2021
2036
  return frameId;
2022
2037
  }
2038
+ var hashMap = {};
2023
2039
  function midsceneGenerateHash(content, rect) {
2024
2040
  const combined = JSON.stringify({
2025
2041
  content,
2026
2042
  rect,
2027
2043
  _midscene_frame_id: getFrameId()
2028
2044
  });
2045
+ let sliceLength = 5;
2046
+ let hashInDigits = "";
2029
2047
  const hashHex = (0, import_js_sha256.default)(combined);
2030
- return hashHex.slice(0, 10);
2048
+ while (sliceLength < combined.length - 1) {
2049
+ hashInDigits = Number.parseInt(
2050
+ hashHex.slice(0, sliceLength),
2051
+ 16
2052
+ ).toString();
2053
+ if (hashMap[hashInDigits] && hashMap[hashInDigits] !== combined) {
2054
+ sliceLength++;
2055
+ continue;
2056
+ }
2057
+ hashMap[hashInDigits] = combined;
2058
+ break;
2059
+ }
2060
+ return hashInDigits;
2031
2061
  }
2032
2062
 
2033
2063
  // src/extractor/constants.ts
@@ -955,7 +955,15 @@ var PageTaskExecutor = class {
955
955
  recorder.push(shot);
956
956
  const result = await taskApply.executor(param, context, ...args);
957
957
  if (taskApply.type === "Action") {
958
- await (0, import_utils5.sleep)(300);
958
+ await Promise.all([
959
+ (async () => {
960
+ await (0, import_utils5.sleep)(100);
961
+ if (this.page.waitUntilNetworkIdle) {
962
+ await this.page.waitUntilNetworkIdle();
963
+ }
964
+ })(),
965
+ (0, import_utils5.sleep)(300)
966
+ ]);
959
967
  }
960
968
  if (appendAfterExecution) {
961
969
  const shot2 = await this.recordScreenshot("after Action");
@@ -976,6 +984,7 @@ var PageTaskExecutor = class {
976
984
  quickAnswer: plan2.quickAnswer,
977
985
  executor: async (param, taskContext) => {
978
986
  const { task } = taskContext;
987
+ (0, import_node_assert2.default)(param == null ? void 0 : param.prompt, "No prompt to locate");
979
988
  let insightDump;
980
989
  const dumpCollector = (dump) => {
981
990
  insightDump = dump;
@@ -1091,7 +1100,7 @@ var PageTaskExecutor = class {
1091
1100
  executor: async (taskParam, { element }) => {
1092
1101
  if (element) {
1093
1102
  await this.page.clearInput(element);
1094
- if (!taskParam || taskParam.value === "") {
1103
+ if (!taskParam || !taskParam.value) {
1095
1104
  return;
1096
1105
  }
1097
1106
  await this.page.keyboard.type(taskParam.value);
@@ -1106,7 +1115,7 @@ var PageTaskExecutor = class {
1106
1115
  subType: "KeyboardPress",
1107
1116
  param: plan2.param,
1108
1117
  executor: async (taskParam) => {
1109
- (0, import_node_assert2.default)(taskParam.value, "No key to press");
1118
+ (0, import_node_assert2.default)(taskParam == null ? void 0 : taskParam.value, "No key to press");
1110
1119
  await this.page.keyboard.press(taskParam.value);
1111
1120
  }
1112
1121
  };
@@ -1170,7 +1179,7 @@ var PageTaskExecutor = class {
1170
1179
  subType: "Sleep",
1171
1180
  param: plan2.param,
1172
1181
  executor: async (taskParam) => {
1173
- await (0, import_utils5.sleep)(taskParam.timeMs || 3e3);
1182
+ await (0, import_utils5.sleep)((taskParam == null ? void 0 : taskParam.timeMs) || 3e3);
1174
1183
  }
1175
1184
  };
1176
1185
  return taskActionSleep;
@@ -1182,17 +1191,17 @@ var PageTaskExecutor = class {
1182
1191
  param: plan2.param,
1183
1192
  executor: async (taskParam) => {
1184
1193
  (0, import_node_assert2.default)(
1185
- taskParam.thought,
1194
+ taskParam == null ? void 0 : taskParam.thought,
1186
1195
  "An error occurred, but no thought provided"
1187
1196
  );
1188
- throw new Error(taskParam.thought);
1197
+ throw new Error((taskParam == null ? void 0 : taskParam.thought) || "error without thought");
1189
1198
  }
1190
1199
  };
1191
1200
  return taskActionError;
1192
1201
  }
1193
1202
  throw new Error(`Unknown or Unsupported task type: ${plan2.type}`);
1194
1203
  });
1195
- const tasksWithScreenshot = tasks.map(
1204
+ const wrappedTasks = tasks.map(
1196
1205
  (task, index) => {
1197
1206
  if (task.type === "Action") {
1198
1207
  return this.prependExecutorWithScreenshot(
@@ -1203,7 +1212,7 @@ var PageTaskExecutor = class {
1203
1212
  return task;
1204
1213
  }
1205
1214
  );
1206
- return tasksWithScreenshot;
1215
+ return wrappedTasks;
1207
1216
  }
1208
1217
  async action(userPrompt) {
1209
1218
  const taskExecutor = new import_core2.Executor(userPrompt);
@@ -618,7 +618,15 @@ var PageTaskExecutor = class {
618
618
  recorder.push(shot);
619
619
  const result = await taskApply.executor(param, context, ...args);
620
620
  if (taskApply.type === "Action") {
621
- await (0, import_utils5.sleep)(300);
621
+ await Promise.all([
622
+ (async () => {
623
+ await (0, import_utils5.sleep)(100);
624
+ if (this.page.waitUntilNetworkIdle) {
625
+ await this.page.waitUntilNetworkIdle();
626
+ }
627
+ })(),
628
+ (0, import_utils5.sleep)(300)
629
+ ]);
622
630
  }
623
631
  if (appendAfterExecution) {
624
632
  const shot2 = await this.recordScreenshot("after Action");
@@ -639,6 +647,7 @@ var PageTaskExecutor = class {
639
647
  quickAnswer: plan2.quickAnswer,
640
648
  executor: async (param, taskContext) => {
641
649
  const { task } = taskContext;
650
+ (0, import_node_assert2.default)(param == null ? void 0 : param.prompt, "No prompt to locate");
642
651
  let insightDump;
643
652
  const dumpCollector = (dump) => {
644
653
  insightDump = dump;
@@ -754,7 +763,7 @@ var PageTaskExecutor = class {
754
763
  executor: async (taskParam, { element }) => {
755
764
  if (element) {
756
765
  await this.page.clearInput(element);
757
- if (!taskParam || taskParam.value === "") {
766
+ if (!taskParam || !taskParam.value) {
758
767
  return;
759
768
  }
760
769
  await this.page.keyboard.type(taskParam.value);
@@ -769,7 +778,7 @@ var PageTaskExecutor = class {
769
778
  subType: "KeyboardPress",
770
779
  param: plan2.param,
771
780
  executor: async (taskParam) => {
772
- (0, import_node_assert2.default)(taskParam.value, "No key to press");
781
+ (0, import_node_assert2.default)(taskParam == null ? void 0 : taskParam.value, "No key to press");
773
782
  await this.page.keyboard.press(taskParam.value);
774
783
  }
775
784
  };
@@ -833,7 +842,7 @@ var PageTaskExecutor = class {
833
842
  subType: "Sleep",
834
843
  param: plan2.param,
835
844
  executor: async (taskParam) => {
836
- await (0, import_utils5.sleep)(taskParam.timeMs || 3e3);
845
+ await (0, import_utils5.sleep)((taskParam == null ? void 0 : taskParam.timeMs) || 3e3);
837
846
  }
838
847
  };
839
848
  return taskActionSleep;
@@ -845,17 +854,17 @@ var PageTaskExecutor = class {
845
854
  param: plan2.param,
846
855
  executor: async (taskParam) => {
847
856
  (0, import_node_assert2.default)(
848
- taskParam.thought,
857
+ taskParam == null ? void 0 : taskParam.thought,
849
858
  "An error occurred, but no thought provided"
850
859
  );
851
- throw new Error(taskParam.thought);
860
+ throw new Error((taskParam == null ? void 0 : taskParam.thought) || "error without thought");
852
861
  }
853
862
  };
854
863
  return taskActionError;
855
864
  }
856
865
  throw new Error(`Unknown or Unsupported task type: ${plan2.type}`);
857
866
  });
858
- const tasksWithScreenshot = tasks.map(
867
+ const wrappedTasks = tasks.map(
859
868
  (task, index) => {
860
869
  if (task.type === "Action") {
861
870
  return this.prependExecutorWithScreenshot(
@@ -866,7 +875,7 @@ var PageTaskExecutor = class {
866
875
  return task;
867
876
  }
868
877
  );
869
- return tasksWithScreenshot;
878
+ return wrappedTasks;
870
879
  }
871
880
  async action(userPrompt) {
872
881
  const taskExecutor = new import_core2.Executor(userPrompt);
@@ -514,7 +514,15 @@ var PageTaskExecutor = class {
514
514
  recorder.push(shot);
515
515
  const result = await taskApply.executor(param, context, ...args);
516
516
  if (taskApply.type === "Action") {
517
- await (0, import_utils3.sleep)(300);
517
+ await Promise.all([
518
+ (async () => {
519
+ await (0, import_utils3.sleep)(100);
520
+ if (this.page.waitUntilNetworkIdle) {
521
+ await this.page.waitUntilNetworkIdle();
522
+ }
523
+ })(),
524
+ (0, import_utils3.sleep)(300)
525
+ ]);
518
526
  }
519
527
  if (appendAfterExecution) {
520
528
  const shot2 = await this.recordScreenshot("after Action");
@@ -535,6 +543,7 @@ var PageTaskExecutor = class {
535
543
  quickAnswer: plan2.quickAnswer,
536
544
  executor: async (param, taskContext) => {
537
545
  const { task } = taskContext;
546
+ (0, import_node_assert.default)(param == null ? void 0 : param.prompt, "No prompt to locate");
538
547
  let insightDump;
539
548
  const dumpCollector = (dump) => {
540
549
  insightDump = dump;
@@ -650,7 +659,7 @@ var PageTaskExecutor = class {
650
659
  executor: async (taskParam, { element }) => {
651
660
  if (element) {
652
661
  await this.page.clearInput(element);
653
- if (!taskParam || taskParam.value === "") {
662
+ if (!taskParam || !taskParam.value) {
654
663
  return;
655
664
  }
656
665
  await this.page.keyboard.type(taskParam.value);
@@ -665,7 +674,7 @@ var PageTaskExecutor = class {
665
674
  subType: "KeyboardPress",
666
675
  param: plan2.param,
667
676
  executor: async (taskParam) => {
668
- (0, import_node_assert.default)(taskParam.value, "No key to press");
677
+ (0, import_node_assert.default)(taskParam == null ? void 0 : taskParam.value, "No key to press");
669
678
  await this.page.keyboard.press(taskParam.value);
670
679
  }
671
680
  };
@@ -729,7 +738,7 @@ var PageTaskExecutor = class {
729
738
  subType: "Sleep",
730
739
  param: plan2.param,
731
740
  executor: async (taskParam) => {
732
- await (0, import_utils3.sleep)(taskParam.timeMs || 3e3);
741
+ await (0, import_utils3.sleep)((taskParam == null ? void 0 : taskParam.timeMs) || 3e3);
733
742
  }
734
743
  };
735
744
  return taskActionSleep;
@@ -741,17 +750,17 @@ var PageTaskExecutor = class {
741
750
  param: plan2.param,
742
751
  executor: async (taskParam) => {
743
752
  (0, import_node_assert.default)(
744
- taskParam.thought,
753
+ taskParam == null ? void 0 : taskParam.thought,
745
754
  "An error occurred, but no thought provided"
746
755
  );
747
- throw new Error(taskParam.thought);
756
+ throw new Error((taskParam == null ? void 0 : taskParam.thought) || "error without thought");
748
757
  }
749
758
  };
750
759
  return taskActionError;
751
760
  }
752
761
  throw new Error(`Unknown or Unsupported task type: ${plan2.type}`);
753
762
  });
754
- const tasksWithScreenshot = tasks.map(
763
+ const wrappedTasks = tasks.map(
755
764
  (task, index) => {
756
765
  if (task.type === "Action") {
757
766
  return this.prependExecutorWithScreenshot(
@@ -762,7 +771,7 @@ var PageTaskExecutor = class {
762
771
  return task;
763
772
  }
764
773
  );
765
- return tasksWithScreenshot;
774
+ return wrappedTasks;
766
775
  }
767
776
  async action(userPrompt) {
768
777
  const taskExecutor = new import_core2.Executor(userPrompt);
@@ -510,7 +510,15 @@ var PageTaskExecutor = class {
510
510
  recorder.push(shot);
511
511
  const result = await taskApply.executor(param, context, ...args);
512
512
  if (taskApply.type === "Action") {
513
- await (0, import_utils3.sleep)(300);
513
+ await Promise.all([
514
+ (async () => {
515
+ await (0, import_utils3.sleep)(100);
516
+ if (this.page.waitUntilNetworkIdle) {
517
+ await this.page.waitUntilNetworkIdle();
518
+ }
519
+ })(),
520
+ (0, import_utils3.sleep)(300)
521
+ ]);
514
522
  }
515
523
  if (appendAfterExecution) {
516
524
  const shot2 = await this.recordScreenshot("after Action");
@@ -531,6 +539,7 @@ var PageTaskExecutor = class {
531
539
  quickAnswer: plan2.quickAnswer,
532
540
  executor: async (param, taskContext) => {
533
541
  const { task } = taskContext;
542
+ (0, import_node_assert.default)(param == null ? void 0 : param.prompt, "No prompt to locate");
534
543
  let insightDump;
535
544
  const dumpCollector = (dump) => {
536
545
  insightDump = dump;
@@ -646,7 +655,7 @@ var PageTaskExecutor = class {
646
655
  executor: async (taskParam, { element }) => {
647
656
  if (element) {
648
657
  await this.page.clearInput(element);
649
- if (!taskParam || taskParam.value === "") {
658
+ if (!taskParam || !taskParam.value) {
650
659
  return;
651
660
  }
652
661
  await this.page.keyboard.type(taskParam.value);
@@ -661,7 +670,7 @@ var PageTaskExecutor = class {
661
670
  subType: "KeyboardPress",
662
671
  param: plan2.param,
663
672
  executor: async (taskParam) => {
664
- (0, import_node_assert.default)(taskParam.value, "No key to press");
673
+ (0, import_node_assert.default)(taskParam == null ? void 0 : taskParam.value, "No key to press");
665
674
  await this.page.keyboard.press(taskParam.value);
666
675
  }
667
676
  };
@@ -725,7 +734,7 @@ var PageTaskExecutor = class {
725
734
  subType: "Sleep",
726
735
  param: plan2.param,
727
736
  executor: async (taskParam) => {
728
- await (0, import_utils3.sleep)(taskParam.timeMs || 3e3);
737
+ await (0, import_utils3.sleep)((taskParam == null ? void 0 : taskParam.timeMs) || 3e3);
729
738
  }
730
739
  };
731
740
  return taskActionSleep;
@@ -737,17 +746,17 @@ var PageTaskExecutor = class {
737
746
  param: plan2.param,
738
747
  executor: async (taskParam) => {
739
748
  (0, import_node_assert.default)(
740
- taskParam.thought,
749
+ taskParam == null ? void 0 : taskParam.thought,
741
750
  "An error occurred, but no thought provided"
742
751
  );
743
- throw new Error(taskParam.thought);
752
+ throw new Error((taskParam == null ? void 0 : taskParam.thought) || "error without thought");
744
753
  }
745
754
  };
746
755
  return taskActionError;
747
756
  }
748
757
  throw new Error(`Unknown or Unsupported task type: ${plan2.type}`);
749
758
  });
750
- const tasksWithScreenshot = tasks.map(
759
+ const wrappedTasks = tasks.map(
751
760
  (task, index) => {
752
761
  if (task.type === "Action") {
753
762
  return this.prependExecutorWithScreenshot(
@@ -758,7 +767,7 @@ var PageTaskExecutor = class {
758
767
  return task;
759
768
  }
760
769
  );
761
- return tasksWithScreenshot;
770
+ return wrappedTasks;
762
771
  }
763
772
  async action(userPrompt) {
764
773
  const taskExecutor = new import_core2.Executor(userPrompt);
@@ -1306,6 +1315,12 @@ var WebPage = class extends Page {
1306
1315
  constructor(page) {
1307
1316
  super(page, "puppeteer");
1308
1317
  }
1318
+ async waitUntilNetworkIdle(options) {
1319
+ await this.underlyingPage.waitForNetworkIdle({
1320
+ idleTime: (options == null ? void 0 : options.idleTime) || 500,
1321
+ concurrency: (options == null ? void 0 : options.concurrency) || 2
1322
+ });
1323
+ }
1309
1324
  };
1310
1325
 
1311
1326
  // src/puppeteer/index.ts