@midscene/web 0.16.9-beta-20250508125518.0 → 0.16.9

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.
Files changed (61) hide show
  1. package/dist/es/agent.js +33 -106
  2. package/dist/es/agent.js.map +1 -1
  3. package/dist/es/bridge-mode-browser.js +3 -3
  4. package/dist/es/bridge-mode-browser.js.map +1 -1
  5. package/dist/es/bridge-mode.js +35 -108
  6. package/dist/es/bridge-mode.js.map +1 -1
  7. package/dist/es/chrome-extension.js +34 -107
  8. package/dist/es/chrome-extension.js.map +1 -1
  9. package/dist/es/index.js +33 -106
  10. package/dist/es/index.js.map +1 -1
  11. package/dist/es/midscene-playground.js +33 -106
  12. package/dist/es/midscene-playground.js.map +1 -1
  13. package/dist/es/playground.js +33 -106
  14. package/dist/es/playground.js.map +1 -1
  15. package/dist/es/playwright.js +33 -106
  16. package/dist/es/playwright.js.map +1 -1
  17. package/dist/es/puppeteer-agent-launcher.js +33 -106
  18. package/dist/es/puppeteer-agent-launcher.js.map +1 -1
  19. package/dist/es/puppeteer.js +33 -106
  20. package/dist/es/puppeteer.js.map +1 -1
  21. package/dist/es/utils.js +1 -3
  22. package/dist/es/utils.js.map +1 -1
  23. package/dist/lib/agent.js +33 -106
  24. package/dist/lib/agent.js.map +1 -1
  25. package/dist/lib/bridge-mode-browser.js +3 -3
  26. package/dist/lib/bridge-mode-browser.js.map +1 -1
  27. package/dist/lib/bridge-mode.js +35 -108
  28. package/dist/lib/bridge-mode.js.map +1 -1
  29. package/dist/lib/chrome-extension.js +34 -107
  30. package/dist/lib/chrome-extension.js.map +1 -1
  31. package/dist/lib/index.js +35 -108
  32. package/dist/lib/index.js.map +1 -1
  33. package/dist/lib/midscene-playground.js +33 -106
  34. package/dist/lib/midscene-playground.js.map +1 -1
  35. package/dist/lib/playground.js +33 -106
  36. package/dist/lib/playground.js.map +1 -1
  37. package/dist/lib/playwright.js +35 -108
  38. package/dist/lib/playwright.js.map +1 -1
  39. package/dist/lib/puppeteer-agent-launcher.js +35 -108
  40. package/dist/lib/puppeteer-agent-launcher.js.map +1 -1
  41. package/dist/lib/puppeteer.js +35 -108
  42. package/dist/lib/puppeteer.js.map +1 -1
  43. package/dist/lib/utils.js +1 -3
  44. package/dist/lib/utils.js.map +1 -1
  45. package/dist/types/agent.d.ts +6 -10
  46. package/dist/types/bridge-mode-browser.d.ts +2 -2
  47. package/dist/types/bridge-mode.d.ts +2 -2
  48. package/dist/types/{browser-371a2ce4.d.ts → browser-a8afbca5.d.ts} +1 -1
  49. package/dist/types/chrome-extension.d.ts +2 -2
  50. package/dist/types/index.d.ts +1 -1
  51. package/dist/types/midscene-server.d.ts +1 -1
  52. package/dist/types/{page-15629bab.d.ts → page-1ed3d6de.d.ts} +1 -3
  53. package/dist/types/playground.d.ts +2 -2
  54. package/dist/types/playwright.d.ts +1 -1
  55. package/dist/types/puppeteer-agent-launcher.d.ts +1 -1
  56. package/dist/types/puppeteer.d.ts +1 -1
  57. package/dist/types/utils.d.ts +1 -1
  58. package/dist/types/yaml.d.ts +1 -1
  59. package/iife-script/htmlElement.js +166 -101
  60. package/iife-script/htmlElementDebug.js +16 -21
  61. package/package.json +3 -5
package/dist/es/agent.js CHANGED
@@ -336,7 +336,6 @@ import { sleep } from "@midscene/core/utils";
336
336
  import { UITarsModelVersion } from "@midscene/shared/env";
337
337
  import { uiTarsModelVersion } from "@midscene/shared/env";
338
338
  import { vlLocateMode } from "@midscene/shared/env";
339
- import { getElementInfosScriptContent } from "@midscene/shared/fs";
340
339
  import { imageInfo, resizeImgBase64 as resizeImgBase642 } from "@midscene/shared/img";
341
340
  import { getDebug as getDebug2 } from "@midscene/shared/logger";
342
341
  import { assert as assert4 } from "@midscene/shared/utils";
@@ -350,7 +349,6 @@ import { getAIConfigInBoolean } from "@midscene/shared/env";
350
349
  import { getRunningPkgInfo } from "@midscene/shared/fs";
351
350
  import { getDebug } from "@midscene/shared/logger";
352
351
  import { ifInBrowser as ifInBrowser2 } from "@midscene/shared/utils";
353
- import semver from "semver";
354
352
 
355
353
  // src/common/utils.ts
356
354
  import { uploadTestInfoToServer } from "@midscene/core/utils";
@@ -369,8 +367,7 @@ var WebElementInfo = class {
369
367
  locator,
370
368
  id,
371
369
  attributes,
372
- indexId,
373
- xpaths
370
+ indexId
374
371
  }) {
375
372
  this.content = content;
376
373
  this.rect = rect;
@@ -382,7 +379,6 @@ var WebElementInfo = class {
382
379
  this.id = id;
383
380
  this.attributes = attributes;
384
381
  this.indexId = indexId;
385
- this.xpaths = xpaths;
386
382
  }
387
383
  };
388
384
 
@@ -448,20 +444,13 @@ function replaceIllegalPathCharsAndSpace(str) {
448
444
  // src/common/task-cache.ts
449
445
  var debug = getDebug("cache");
450
446
  var TaskCache = class {
451
- constructor(page, opts) {
447
+ constructor(opts) {
452
448
  this.midscenePkgInfo = getRunningPkgInfo();
453
449
  this.cacheId = replaceIllegalPathCharsAndSpace(opts?.cacheId || "");
454
- this.page = page;
455
450
  this.cache = this.readCacheFromFile() || {
456
- pkgName: "",
457
- pkgVersion: "",
458
- cacheId: "",
459
451
  aiTasks: []
460
452
  };
461
453
  this.newCache = {
462
- pkgName: this.midscenePkgInfo?.name || "",
463
- pkgVersion: this.midscenePkgInfo?.version || "",
464
- cacheId: this.cacheId,
465
454
  aiTasks: []
466
455
  };
467
456
  }
@@ -474,7 +463,7 @@ var TaskCache = class {
474
463
  tasks: newCacheGroup
475
464
  });
476
465
  return {
477
- matchCache: async (pageContext, type, actionPrompt) => {
466
+ matchCache: (pageContext, type, actionPrompt) => {
478
467
  if (index === -1) {
479
468
  return false;
480
469
  }
@@ -512,7 +501,7 @@ var TaskCache = class {
512
501
  }
513
502
  };
514
503
  }
515
- async matchCache(pageContext, type, userPrompt, cacheGroup) {
504
+ matchCache(pageContext, type, userPrompt, cacheGroup) {
516
505
  debug(
517
506
  "will read cache, type: %s, prompt: %s, cacheGroupLength: %s",
518
507
  type,
@@ -533,64 +522,21 @@ var TaskCache = class {
533
522
  taskRes?.response
534
523
  );
535
524
  if (taskRes?.type === "locate") {
536
- const xpaths = taskRes.response?.elements?.[0]?.xpaths;
537
- if (!xpaths || !xpaths.length) {
538
- debug("no xpaths in cached response");
525
+ const id = taskRes.response?.elements[0].id;
526
+ if (!id) {
527
+ debug("no id in cached response");
539
528
  return false;
540
529
  }
541
- for (const xpath of xpaths) {
542
- if (this.page.pageType === "playwright") {
543
- try {
544
- const playwrightPage = this.page.underlyingPage;
545
- const xpathLocator = playwrightPage.locator(`xpath=${xpath}`);
546
- const xpathCount = await xpathLocator.count();
547
- if (xpathCount === 1) {
548
- debug(
549
- "cache hit, type: %s, prompt: %s, xpath: %s",
550
- type,
551
- userPrompt,
552
- xpath
553
- );
554
- const xpathElement = await xpathLocator.first();
555
- await xpathElement.evaluate((element) => {
556
- element.setAttribute("data-midscene", "cache-hit");
557
- element.scrollIntoView();
558
- });
559
- return taskRes.response;
560
- }
561
- } catch (error) {
562
- debug("playwright xpath locator error", error);
563
- }
564
- } else if (this.page.pageType === "puppeteer") {
565
- try {
566
- const puppeteerPage = this.page.underlyingPage;
567
- const xpathElements = await puppeteerPage.$$(`xpath=${xpath}`);
568
- if (xpathElements && xpathElements.length === 1) {
569
- debug(
570
- "cache hit, type: %s, prompt: %s, xpath: %s",
571
- type,
572
- userPrompt,
573
- xpath
574
- );
575
- await xpathElements[0].evaluate((element) => {
576
- element.setAttribute("data-midscene", "cache-hit");
577
- element.scrollIntoView();
578
- });
579
- return taskRes.response;
580
- }
581
- } catch (error) {
582
- debug("puppeteer xpath locator error", error);
583
- }
584
- } else {
585
- debug("unknown page type, will not match cache", {
586
- pageType: this.page.pageType
587
- });
588
- }
530
+ const foundInContext = pageContext.content.find(
531
+ (contentElement) => contentElement.id === id
532
+ );
533
+ if (!foundInContext) {
589
534
  debug("cannot match element with same id in current page", {
590
535
  element: taskRes.element
591
536
  });
592
537
  return false;
593
538
  }
539
+ return taskRes.response;
594
540
  }
595
541
  if (taskRes && taskRes.type === type && taskRes.prompt === userPrompt) {
596
542
  const contextEqual = this.pageContextEqual(
@@ -648,14 +594,11 @@ var TaskCache = class {
648
594
  const data = readFileSync(cacheFile, "utf8");
649
595
  const jsonData = JSON.parse(data);
650
596
  if (!this.midscenePkgInfo) {
651
- debug("no midscene pkg info, will not read cache from file");
652
597
  return void 0;
653
598
  }
654
- if (semver.lt(jsonData.pkgVersion, "0.17.0") || jsonData.pkgVersion.includes("beta")) {
655
- console.warn(
656
- `You are using an old version of Midscene cache file, and we cannot match any info from it. Starting from Midscene v0.17, we changed our strategy to use xpath for cache info, providing better performance. Please delete the existing cache and rebuild it. Sorry for the inconvenience.
657
- cache file: ${cacheFile}`
658
- );
599
+ const jsonDataPkgVersion = jsonData.pkgVersion.split(".");
600
+ const midscenePkgInfoPkgVersion = this.midscenePkgInfo.version.split(".");
601
+ if (jsonDataPkgVersion[0] !== midscenePkgInfoPkgVersion[0] || jsonDataPkgVersion[1] !== midscenePkgInfoPkgVersion[1]) {
659
602
  return void 0;
660
603
  }
661
604
  debug("read cache from file: %s", cacheFile);
@@ -686,7 +629,15 @@ cache file: ${cacheFile}`
686
629
  writeLogFile({
687
630
  fileName: `${this.cacheId}`,
688
631
  fileExt: "json",
689
- fileContent: stringifyDumpData(this.newCache, 2),
632
+ fileContent: stringifyDumpData(
633
+ {
634
+ pkgName: midscenePkgInfo.name,
635
+ pkgVersion: midscenePkgInfo.version,
636
+ cacheId: this.cacheId,
637
+ ...this.newCache
638
+ },
639
+ 2
640
+ ),
690
641
  type: "cache"
691
642
  });
692
643
  }
@@ -777,7 +728,7 @@ var PageTaskExecutor = class {
777
728
  this.conversationHistory = [];
778
729
  this.page = page;
779
730
  this.insight = insight;
780
- this.taskCache = new TaskCache(page, {
731
+ this.taskCache = new TaskCache({
781
732
  cacheId: opts?.cacheId
782
733
  });
783
734
  this.onTaskStartCallback = opts?.onTaskStart;
@@ -865,30 +816,16 @@ var PageTaskExecutor = class {
865
816
  };
866
817
  task.recorder = [recordItem];
867
818
  const cachePrompt = param.prompt;
868
- const locateCache = await cacheGroup?.matchCache(
819
+ const locateCache = cacheGroup?.matchCache(
869
820
  pageContext,
870
821
  "locate",
871
822
  cachePrompt
872
823
  );
873
- let elementInfo = null;
874
- const oldId = locateCache?.elements?.[0]?.id;
875
- let newId = null;
876
- try {
877
- if (oldId) {
878
- const elementInfosScriptContent = getElementInfosScriptContent();
879
- elementInfo = await this.page.evaluateJavaScript?.(
880
- `${elementInfosScriptContent}midscene_element_inspector.getElementInfoByNode(document.querySelector('[data-midscene="cache-hit"]'))`
881
- );
882
- newId = elementInfo?.id;
883
- debug2("get new id by node", newId);
884
- }
885
- } catch (error) {
886
- debug2("get element info by node error: ", error);
887
- }
824
+ const idInCache = locateCache?.elements?.[0]?.id;
888
825
  let cacheHitFlag = false;
889
826
  let quickAnswerId = param?.id;
890
- if (!quickAnswerId && newId) {
891
- quickAnswerId = newId;
827
+ if (!quickAnswerId && idInCache) {
828
+ quickAnswerId = idInCache;
892
829
  }
893
830
  const quickAnswer = {
894
831
  id: quickAnswerId,
@@ -899,19 +836,10 @@ var PageTaskExecutor = class {
899
836
  quickAnswer
900
837
  });
901
838
  const aiCost = Date.now() - startTime;
902
- if (element && element.id === quickAnswerId && newId) {
839
+ if (element && element.id === quickAnswerId && idInCache) {
903
840
  cacheHitFlag = true;
904
841
  }
905
842
  if (element) {
906
- let xpaths;
907
- try {
908
- const elementInfosScriptContent = getElementInfosScriptContent();
909
- xpaths = await this.page.evaluateJavaScript?.(
910
- `${elementInfosScriptContent}midscene_element_inspector.getXpathsById('${element.id}')`
911
- );
912
- } catch (error) {
913
- debug2("getXpathsById error: ", error);
914
- }
915
843
  cacheGroup?.saveCache({
916
844
  type: "locate",
917
845
  pageContext: {
@@ -922,8 +850,7 @@ var PageTaskExecutor = class {
922
850
  response: {
923
851
  elements: [
924
852
  {
925
- id: newId || element.id,
926
- xpaths
853
+ id: element.id
927
854
  }
928
855
  ]
929
856
  },
@@ -1252,7 +1179,7 @@ var PageTaskExecutor = class {
1252
1179
  executorContext.task.recorder = [recordItem];
1253
1180
  executorContext.task.pageContext = pageContext;
1254
1181
  const cachePrompt = `${param.userInstruction} @ ${param.log || ""}`;
1255
- const planCache = await cacheGroup.matchCache(
1182
+ const planCache = cacheGroup.matchCache(
1256
1183
  pageContext,
1257
1184
  "plan",
1258
1185
  cachePrompt
@@ -1416,7 +1343,7 @@ var PageTaskExecutor = class {
1416
1343
  ]
1417
1344
  });
1418
1345
  const startTime = Date.now();
1419
- const planCache = await cacheGroup.matchCache(
1346
+ const planCache = cacheGroup.matchCache(
1420
1347
  pageContext,
1421
1348
  "ui-tars-plan",
1422
1349
  userInstruction