@midscene/shared 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 (46) hide show
  1. package/dist/es/common.js +2 -0
  2. package/dist/es/env.d.ts +3 -1
  3. package/dist/es/env.js +3 -0
  4. package/dist/es/extractor-debug.js +16 -21
  5. package/dist/es/extractor.d.ts +47 -2
  6. package/dist/es/extractor.js +168 -104
  7. package/dist/es/fs.d.ts +1 -2
  8. package/dist/es/fs.js +4 -9
  9. package/dist/es/img.d.ts +1 -1
  10. package/dist/es/index-305e7a7e.d.ts +32 -0
  11. package/dist/es/logger.js +2 -0
  12. package/dist/lib/common.js +2 -0
  13. package/dist/lib/env.d.ts +3 -1
  14. package/dist/lib/env.js +4 -0
  15. package/dist/lib/extractor-debug.js +16 -21
  16. package/dist/lib/extractor.d.ts +47 -2
  17. package/dist/lib/extractor.js +169 -107
  18. package/dist/lib/fs.d.ts +1 -2
  19. package/dist/lib/fs.js +4 -10
  20. package/dist/lib/img.d.ts +1 -1
  21. package/dist/lib/index-305e7a7e.d.ts +32 -0
  22. package/dist/lib/logger.js +2 -0
  23. package/dist/script/htmlElement.js +166 -101
  24. package/dist/script/htmlElementDebug.js +16 -21
  25. package/dist/types/env.d.ts +3 -1
  26. package/dist/types/extractor.d.ts +47 -2
  27. package/dist/types/fs.d.ts +1 -2
  28. package/dist/types/img.d.ts +1 -1
  29. package/dist/types/index-305e7a7e.d.ts +32 -0
  30. package/package.json +2 -6
  31. package/src/env.ts +3 -0
  32. package/src/extractor/client-extractor.ts +211 -0
  33. package/src/extractor/index.ts +3 -5
  34. package/src/extractor/util.ts +2 -31
  35. package/src/extractor/web-extractor.ts +19 -2
  36. package/src/node/fs.ts +4 -12
  37. package/src/types/index.ts +0 -5
  38. package/dist/es/index-df4e781b.d.ts +0 -77
  39. package/dist/es/types.d.ts +0 -2
  40. package/dist/es/types.js +0 -6
  41. package/dist/lib/index-df4e781b.d.ts +0 -77
  42. package/dist/lib/types.d.ts +0 -2
  43. package/dist/lib/types.js +0 -31
  44. package/dist/types/index-df4e781b.d.ts +0 -77
  45. package/dist/types/types.d.ts +0 -2
  46. package/src/extractor/locator.ts +0 -117
package/dist/es/common.js CHANGED
@@ -14,6 +14,7 @@ var MIDSCENE_DEBUG_MODE = "MIDSCENE_DEBUG_MODE";
14
14
  var MIDSCENE_MCP_USE_PUPPETEER_MODE = "MIDSCENE_MCP_USE_PUPPETEER_MODE";
15
15
  var MIDSCENE_FORCE_DEEP_THINK = "MIDSCENE_FORCE_DEEP_THINK";
16
16
  var MIDSCENE_OPENAI_SOCKS_PROXY = "MIDSCENE_OPENAI_SOCKS_PROXY";
17
+ var MIDSCENE_OPENAI_HTTP_PROXY = "MIDSCENE_OPENAI_HTTP_PROXY";
17
18
  var OPENAI_API_KEY = "OPENAI_API_KEY";
18
19
  var OPENAI_BASE_URL = "OPENAI_BASE_URL";
19
20
  var OPENAI_MAX_TOKENS = "OPENAI_MAX_TOKENS";
@@ -56,6 +57,7 @@ var allConfigFromEnv = () => {
56
57
  [MATCH_BY_POSITION]: process.env[MATCH_BY_POSITION] || void 0,
57
58
  [MIDSCENE_REPORT_TAG_NAME]: process.env[MIDSCENE_REPORT_TAG_NAME] || void 0,
58
59
  [MIDSCENE_OPENAI_SOCKS_PROXY]: process.env[MIDSCENE_OPENAI_SOCKS_PROXY] || void 0,
60
+ [MIDSCENE_OPENAI_HTTP_PROXY]: process.env[MIDSCENE_OPENAI_HTTP_PROXY] || void 0,
59
61
  [MIDSCENE_USE_AZURE_OPENAI]: process.env[MIDSCENE_USE_AZURE_OPENAI] || void 0,
60
62
  [MIDSCENE_AZURE_OPENAI_SCOPE]: process.env[MIDSCENE_AZURE_OPENAI_SCOPE] || void 0,
61
63
  [MIDSCENE_AZURE_OPENAI_INIT_CONFIG_JSON]: process.env[MIDSCENE_AZURE_OPENAI_INIT_CONFIG_JSON] || void 0,
package/dist/es/env.d.ts CHANGED
@@ -8,6 +8,7 @@ declare const MIDSCENE_DEBUG_MODE = "MIDSCENE_DEBUG_MODE";
8
8
  declare const MIDSCENE_MCP_USE_PUPPETEER_MODE = "MIDSCENE_MCP_USE_PUPPETEER_MODE";
9
9
  declare const MIDSCENE_FORCE_DEEP_THINK = "MIDSCENE_FORCE_DEEP_THINK";
10
10
  declare const MIDSCENE_OPENAI_SOCKS_PROXY = "MIDSCENE_OPENAI_SOCKS_PROXY";
11
+ declare const MIDSCENE_OPENAI_HTTP_PROXY = "MIDSCENE_OPENAI_HTTP_PROXY";
11
12
  declare const OPENAI_API_KEY = "OPENAI_API_KEY";
12
13
  declare const OPENAI_BASE_URL = "OPENAI_BASE_URL";
13
14
  declare const OPENAI_MAX_TOKENS = "OPENAI_MAX_TOKENS";
@@ -50,6 +51,7 @@ declare const allConfigFromEnv: () => {
50
51
  MATCH_BY_POSITION: string | undefined;
51
52
  MIDSCENE_REPORT_TAG_NAME: string | undefined;
52
53
  MIDSCENE_OPENAI_SOCKS_PROXY: string | undefined;
54
+ MIDSCENE_OPENAI_HTTP_PROXY: string | undefined;
53
55
  MIDSCENE_USE_AZURE_OPENAI: string | undefined;
54
56
  MIDSCENE_AZURE_OPENAI_SCOPE: string | undefined;
55
57
  MIDSCENE_AZURE_OPENAI_INIT_CONFIG_JSON: string | undefined;
@@ -80,4 +82,4 @@ declare const getAIConfigInBoolean: (configKey: keyof ReturnType<typeof allConfi
80
82
  declare const getAIConfigInJson: (configKey: keyof ReturnType<typeof allConfigFromEnv>) => any;
81
83
  declare const overrideAIConfig: (newConfig: Partial<ReturnType<typeof allConfigFromEnv>>, extendMode?: boolean) => void;
82
84
 
83
- export { ANTHROPIC_API_KEY, AZURE_OPENAI_API_VERSION, AZURE_OPENAI_DEPLOYMENT, AZURE_OPENAI_ENDPOINT, AZURE_OPENAI_KEY, MATCH_BY_POSITION, MIDSCENE_ADB_PATH, MIDSCENE_API_TYPE, MIDSCENE_AZURE_OPENAI_INIT_CONFIG_JSON, MIDSCENE_AZURE_OPENAI_SCOPE, MIDSCENE_CACHE, MIDSCENE_DANGEROUSLY_PRINT_ALL_CONFIG, MIDSCENE_DEBUG_AI_PROFILE, MIDSCENE_DEBUG_AI_RESPONSE, MIDSCENE_DEBUG_MODE, MIDSCENE_FORCE_DEEP_THINK, MIDSCENE_LANGSMITH_DEBUG, MIDSCENE_MCP_USE_PUPPETEER_MODE, MIDSCENE_MODEL_NAME, MIDSCENE_OPENAI_INIT_CONFIG_JSON, MIDSCENE_OPENAI_SOCKS_PROXY, MIDSCENE_REPORT_TAG_NAME, MIDSCENE_RUN_DIR, MIDSCENE_USE_ANTHROPIC_SDK, MIDSCENE_USE_AZURE_OPENAI, MIDSCENE_USE_DOUBAO_VISION, MIDSCENE_USE_GEMINI, MIDSCENE_USE_QWEN_VL, MIDSCENE_USE_VLM_UI_TARS, MIDSCENE_USE_VL_MODEL, OPENAI_API_KEY, OPENAI_BASE_URL, OPENAI_MAX_TOKENS, OPENAI_USE_AZURE, UITarsModelVersion, allConfigFromEnv, getAIConfig, getAIConfigInBoolean, getAIConfigInJson, overrideAIConfig, uiTarsModelVersion, vlLocateMode };
85
+ export { ANTHROPIC_API_KEY, AZURE_OPENAI_API_VERSION, AZURE_OPENAI_DEPLOYMENT, AZURE_OPENAI_ENDPOINT, AZURE_OPENAI_KEY, MATCH_BY_POSITION, MIDSCENE_ADB_PATH, MIDSCENE_API_TYPE, MIDSCENE_AZURE_OPENAI_INIT_CONFIG_JSON, MIDSCENE_AZURE_OPENAI_SCOPE, MIDSCENE_CACHE, MIDSCENE_DANGEROUSLY_PRINT_ALL_CONFIG, MIDSCENE_DEBUG_AI_PROFILE, MIDSCENE_DEBUG_AI_RESPONSE, MIDSCENE_DEBUG_MODE, MIDSCENE_FORCE_DEEP_THINK, MIDSCENE_LANGSMITH_DEBUG, MIDSCENE_MCP_USE_PUPPETEER_MODE, MIDSCENE_MODEL_NAME, MIDSCENE_OPENAI_HTTP_PROXY, MIDSCENE_OPENAI_INIT_CONFIG_JSON, MIDSCENE_OPENAI_SOCKS_PROXY, MIDSCENE_REPORT_TAG_NAME, MIDSCENE_RUN_DIR, MIDSCENE_USE_ANTHROPIC_SDK, MIDSCENE_USE_AZURE_OPENAI, MIDSCENE_USE_DOUBAO_VISION, MIDSCENE_USE_GEMINI, MIDSCENE_USE_QWEN_VL, MIDSCENE_USE_VLM_UI_TARS, MIDSCENE_USE_VL_MODEL, OPENAI_API_KEY, OPENAI_BASE_URL, OPENAI_MAX_TOKENS, OPENAI_USE_AZURE, UITarsModelVersion, allConfigFromEnv, getAIConfig, getAIConfigInBoolean, getAIConfigInJson, overrideAIConfig, uiTarsModelVersion, vlLocateMode };
package/dist/es/env.js CHANGED
@@ -9,6 +9,7 @@ var MIDSCENE_DEBUG_MODE = "MIDSCENE_DEBUG_MODE";
9
9
  var MIDSCENE_MCP_USE_PUPPETEER_MODE = "MIDSCENE_MCP_USE_PUPPETEER_MODE";
10
10
  var MIDSCENE_FORCE_DEEP_THINK = "MIDSCENE_FORCE_DEEP_THINK";
11
11
  var MIDSCENE_OPENAI_SOCKS_PROXY = "MIDSCENE_OPENAI_SOCKS_PROXY";
12
+ var MIDSCENE_OPENAI_HTTP_PROXY = "MIDSCENE_OPENAI_HTTP_PROXY";
12
13
  var OPENAI_API_KEY = "OPENAI_API_KEY";
13
14
  var OPENAI_BASE_URL = "OPENAI_BASE_URL";
14
15
  var OPENAI_MAX_TOKENS = "OPENAI_MAX_TOKENS";
@@ -52,6 +53,7 @@ var allConfigFromEnv = () => {
52
53
  [MATCH_BY_POSITION]: process.env[MATCH_BY_POSITION] || void 0,
53
54
  [MIDSCENE_REPORT_TAG_NAME]: process.env[MIDSCENE_REPORT_TAG_NAME] || void 0,
54
55
  [MIDSCENE_OPENAI_SOCKS_PROXY]: process.env[MIDSCENE_OPENAI_SOCKS_PROXY] || void 0,
56
+ [MIDSCENE_OPENAI_HTTP_PROXY]: process.env[MIDSCENE_OPENAI_HTTP_PROXY] || void 0,
55
57
  [MIDSCENE_USE_AZURE_OPENAI]: process.env[MIDSCENE_USE_AZURE_OPENAI] || void 0,
56
58
  [MIDSCENE_AZURE_OPENAI_SCOPE]: process.env[MIDSCENE_AZURE_OPENAI_SCOPE] || void 0,
57
59
  [MIDSCENE_AZURE_OPENAI_INIT_CONFIG_JSON]: process.env[MIDSCENE_AZURE_OPENAI_INIT_CONFIG_JSON] || void 0,
@@ -191,6 +193,7 @@ export {
191
193
  MIDSCENE_LANGSMITH_DEBUG,
192
194
  MIDSCENE_MCP_USE_PUPPETEER_MODE,
193
195
  MIDSCENE_MODEL_NAME,
196
+ MIDSCENE_OPENAI_HTTP_PROXY,
194
197
  MIDSCENE_OPENAI_INIT_CONFIG_JSON,
195
198
  MIDSCENE_OPENAI_SOCKS_PROXY,
196
199
  MIDSCENE_REPORT_TAG_NAME,
@@ -362,24 +362,8 @@ function getNodeAttributes(node, currentWindow) {
362
362
  }
363
363
  function midsceneGenerateHash(node, content, rect) {
364
364
  const slicedHash = generateHashId(rect, content);
365
- if (node) {
366
- if (!window.midsceneNodeHashCacheList) {
367
- setNodeHashCacheListOnWindow();
368
- }
369
- setNodeToCacheList(node, slicedHash);
370
- }
371
365
  return slicedHash;
372
366
  }
373
- function setNodeHashCacheListOnWindow() {
374
- if (typeof window !== "undefined") {
375
- window.midsceneNodeHashCacheList = [];
376
- }
377
- }
378
- function setNodeToCacheList(node, id) {
379
- if (typeof window !== "undefined") {
380
- window.midsceneNodeHashCacheList.push({ node, id });
381
- }
382
- }
383
367
  function setMidsceneVisibleRectOnWindow() {
384
368
  if (typeof window !== "undefined") {
385
369
  window.midsceneVisibleRect = visibleRect;
@@ -450,7 +434,9 @@ function collectElementInfo(node, currentWindow, currentDocument, baseZoom = 1,
450
434
  Math.round(rect.left + rect.width / 2),
451
435
  Math.round(rect.top + rect.height / 2)
452
436
  ],
453
- zoom: rect.zoom
437
+ zoom: rect.zoom,
438
+ screenWidth: currentWindow.innerWidth,
439
+ screenHeight: currentWindow.innerHeight
454
440
  };
455
441
  return elementInfo;
456
442
  }
@@ -477,7 +463,9 @@ function collectElementInfo(node, currentWindow, currentDocument, baseZoom = 1,
477
463
  Math.round(rect.left + rect.width / 2),
478
464
  Math.round(rect.top + rect.height / 2)
479
465
  ],
480
- zoom: rect.zoom
466
+ zoom: rect.zoom,
467
+ screenWidth: currentWindow.innerWidth,
468
+ screenHeight: currentWindow.innerHeight
481
469
  };
482
470
  return elementInfo;
483
471
  }
@@ -505,7 +493,9 @@ function collectElementInfo(node, currentWindow, currentDocument, baseZoom = 1,
505
493
  Math.round(rect.left + rect.width / 2),
506
494
  Math.round(rect.top + rect.height / 2)
507
495
  ],
508
- zoom: rect.zoom
496
+ zoom: rect.zoom,
497
+ screenWidth: currentWindow.innerWidth,
498
+ screenHeight: currentWindow.innerHeight
509
499
  };
510
500
  return elementInfo;
511
501
  }
@@ -536,9 +526,12 @@ function collectElementInfo(node, currentWindow, currentDocument, baseZoom = 1,
536
526
  Math.round(rect.left + rect.width / 2),
537
527
  Math.round(rect.top + rect.height / 2)
538
528
  ],
529
+ // attributes,
539
530
  content: text,
540
531
  rect,
541
- zoom: rect.zoom
532
+ zoom: rect.zoom,
533
+ screenWidth: currentWindow.innerWidth,
534
+ screenHeight: currentWindow.innerHeight
542
535
  };
543
536
  return elementInfo;
544
537
  }
@@ -563,7 +556,9 @@ function collectElementInfo(node, currentWindow, currentDocument, baseZoom = 1,
563
556
  Math.round(rect.left + rect.width / 2),
564
557
  Math.round(rect.top + rect.height / 2)
565
558
  ],
566
- zoom: rect.zoom
559
+ zoom: rect.zoom,
560
+ screenWidth: currentWindow.innerWidth,
561
+ screenHeight: currentWindow.innerHeight
567
562
  };
568
563
  return elementInfo;
569
564
  }
@@ -1,2 +1,47 @@
1
- import './constants.js';
2
- export { E as ElementInfo, a as ElementNode, d as descriptionOfTree, h as getElementInfoByNode, g as getXpathsById, s as setNodeHashCacheListOnWindow, t as traverseTree, b as treeToList, c as webExtractNodeTree, f as webExtractNodeTreeAsString, e as webExtractTextWithPosition } from './index-df4e781b.js';
1
+ import { NodeType } from './constants.js';
2
+ import { B as BaseElement, E as ElementTreeNode } from './index-305e7a7e.js';
3
+
4
+ declare function descriptionOfTree<ElementType extends BaseElement = BaseElement>(tree: ElementTreeNode<ElementType>, truncateTextLength?: number, filterNonTextContent?: boolean): string;
5
+ declare function treeToList<T extends BaseElement>(tree: ElementTreeNode<T>): T[];
6
+ declare function traverseTree<T extends BaseElement, ReturnNodeType extends BaseElement>(tree: ElementTreeNode<T>, onNode: (node: T) => ReturnNodeType): ElementTreeNode<ReturnNodeType>;
7
+
8
+ interface WebElementInfo extends ElementInfo {
9
+ zoom: number;
10
+ screenWidth?: number;
11
+ screenHeight?: number;
12
+ }
13
+ interface WebElementNode {
14
+ node: WebElementInfo | null;
15
+ children: WebElementNode[];
16
+ }
17
+ declare function extractTextWithPosition$1(initNode: globalThis.Node, debugMode?: boolean): WebElementInfo[];
18
+ declare function extractTreeNodeAsString(initNode: globalThis.Node, debugMode?: boolean): string;
19
+ declare function extractTreeNode(initNode: globalThis.Node, debugMode?: boolean): WebElementNode;
20
+
21
+ declare function extractTextWithPosition(initNode: globalThis.Document): ElementInfo[];
22
+
23
+ interface ElementInfo {
24
+ id: string;
25
+ indexId: number;
26
+ nodeHashId: string;
27
+ locator: string;
28
+ attributes: {
29
+ nodeType: NodeType;
30
+ [key: string]: string;
31
+ };
32
+ nodeType: NodeType;
33
+ content: string;
34
+ rect: {
35
+ left: number;
36
+ top: number;
37
+ width: number;
38
+ height: number;
39
+ };
40
+ center: [number, number];
41
+ }
42
+ interface ElementNode {
43
+ node: ElementInfo | null;
44
+ children: ElementNode[];
45
+ }
46
+
47
+ export { type ElementInfo, type ElementNode, extractTextWithPosition as clientExtractTextWithPosition, descriptionOfTree, traverseTree, treeToList, extractTreeNode as webExtractNodeTree, extractTreeNodeAsString as webExtractNodeTreeAsString, extractTextWithPosition$1 as webExtractTextWithPosition };
@@ -483,32 +483,8 @@ function getNodeAttributes(node, currentWindow) {
483
483
  }
484
484
  function midsceneGenerateHash(node, content, rect) {
485
485
  const slicedHash = generateHashId(rect, content);
486
- if (node) {
487
- if (!window.midsceneNodeHashCacheList) {
488
- setNodeHashCacheListOnWindow();
489
- }
490
- setNodeToCacheList(node, slicedHash);
491
- }
492
486
  return slicedHash;
493
487
  }
494
- function setNodeHashCacheListOnWindow() {
495
- if (typeof window !== "undefined") {
496
- window.midsceneNodeHashCacheList = [];
497
- }
498
- }
499
- function setNodeToCacheList(node, id) {
500
- if (typeof window !== "undefined") {
501
- window.midsceneNodeHashCacheList.push({ node, id });
502
- }
503
- }
504
- function getNodeFromCacheList(id) {
505
- if (typeof window !== "undefined") {
506
- return window.midsceneNodeHashCacheList.find(
507
- (item) => item.id === id
508
- )?.node;
509
- }
510
- return null;
511
- }
512
488
  function getTopDocument() {
513
489
  const container = document.body || document;
514
490
  return container;
@@ -569,7 +545,9 @@ function collectElementInfo(node, currentWindow, currentDocument, baseZoom = 1,
569
545
  Math.round(rect.left + rect.width / 2),
570
546
  Math.round(rect.top + rect.height / 2)
571
547
  ],
572
- zoom: rect.zoom
548
+ zoom: rect.zoom,
549
+ screenWidth: currentWindow.innerWidth,
550
+ screenHeight: currentWindow.innerHeight
573
551
  };
574
552
  return elementInfo;
575
553
  }
@@ -596,7 +574,9 @@ function collectElementInfo(node, currentWindow, currentDocument, baseZoom = 1,
596
574
  Math.round(rect.left + rect.width / 2),
597
575
  Math.round(rect.top + rect.height / 2)
598
576
  ],
599
- zoom: rect.zoom
577
+ zoom: rect.zoom,
578
+ screenWidth: currentWindow.innerWidth,
579
+ screenHeight: currentWindow.innerHeight
600
580
  };
601
581
  return elementInfo;
602
582
  }
@@ -624,7 +604,9 @@ function collectElementInfo(node, currentWindow, currentDocument, baseZoom = 1,
624
604
  Math.round(rect.left + rect.width / 2),
625
605
  Math.round(rect.top + rect.height / 2)
626
606
  ],
627
- zoom: rect.zoom
607
+ zoom: rect.zoom,
608
+ screenWidth: currentWindow.innerWidth,
609
+ screenHeight: currentWindow.innerHeight
628
610
  };
629
611
  return elementInfo;
630
612
  }
@@ -655,9 +637,12 @@ function collectElementInfo(node, currentWindow, currentDocument, baseZoom = 1,
655
637
  Math.round(rect.left + rect.width / 2),
656
638
  Math.round(rect.top + rect.height / 2)
657
639
  ],
640
+ // attributes,
658
641
  content: text,
659
642
  rect,
660
- zoom: rect.zoom
643
+ zoom: rect.zoom,
644
+ screenWidth: currentWindow.innerWidth,
645
+ screenHeight: currentWindow.innerHeight
661
646
  };
662
647
  return elementInfo;
663
648
  }
@@ -682,7 +667,9 @@ function collectElementInfo(node, currentWindow, currentDocument, baseZoom = 1,
682
667
  Math.round(rect.left + rect.width / 2),
683
668
  Math.round(rect.top + rect.height / 2)
684
669
  ],
685
- zoom: rect.zoom
670
+ zoom: rect.zoom,
671
+ screenWidth: currentWindow.innerWidth,
672
+ screenHeight: currentWindow.innerHeight
686
673
  };
687
674
  return elementInfo;
688
675
  }
@@ -791,89 +778,166 @@ function extractTreeNode(initNode, debugMode2 = false) {
791
778
  };
792
779
  }
793
780
 
794
- // src/extractor/locator.ts
795
- var getElementIndex = (element) => {
796
- let index = 1;
797
- let prev = element.previousElementSibling;
798
- while (prev) {
799
- if (prev.nodeName.toLowerCase() === element.nodeName.toLowerCase()) {
800
- index++;
801
- }
802
- prev = prev.previousElementSibling;
781
+ // src/extractor/client-extractor.ts
782
+ function getNodeAttributes2(node) {
783
+ const attrs = {};
784
+ if (node && node.nodeType === 1) {
785
+ const element = node;
786
+ for (let i = 0; i < element.attributes.length; i++) {
787
+ const attr = element.attributes[i];
788
+ attrs[attr.nodeName] = attr.nodeValue ?? "";
789
+ }
790
+ }
791
+ return attrs;
792
+ }
793
+ function getRect2(attributes) {
794
+ const x = Math.round(Number.parseFloat(attributes.x ?? "0"));
795
+ const y = Math.round(Number.parseFloat(attributes.y ?? "0"));
796
+ const width = Math.round(Number.parseFloat(attributes.width ?? "0"));
797
+ const height = Math.round(Number.parseFloat(attributes.height ?? "0"));
798
+ return {
799
+ left: Math.max(0, Math.floor(x)),
800
+ top: Math.max(0, Math.floor(y)),
801
+ width: Math.max(0, width),
802
+ height: Math.max(0, height)
803
+ };
804
+ }
805
+ function validTextNodeContent(node) {
806
+ if (node.nodeType === 3) {
807
+ return node.nodeValue?.trim() || "";
803
808
  }
804
- return index;
805
- };
806
- var findFirstAncestorWithId = (element) => {
807
- let current = element;
808
- while (current?.parentElement) {
809
- if (current.id) {
810
- return current;
811
- }
812
- current = current.parentElement;
809
+ return "";
810
+ }
811
+ function getXPathForElement(element) {
812
+ if (element.nodeType !== 1) {
813
+ return "";
813
814
  }
814
- return null;
815
- };
816
- var getElementXPath = (element) => {
817
- if (element.nodeType !== Node.ELEMENT_NODE)
815
+ const getIndex = (sib, name) => {
816
+ let count = 1;
817
+ for (let cur = sib.previousSibling; cur; cur = cur.previousSibling) {
818
+ if (cur.nodeType === 1 && cur.nodeName === name) {
819
+ count++;
820
+ }
821
+ }
822
+ return count;
823
+ };
824
+ const buildAttributePart = (elem) => {
825
+ const attributes = ["id", "resource-id", "content-desc", "class"];
826
+ for (const attr of attributes) {
827
+ if (elem.hasAttribute(attr)) {
828
+ const value = elem.getAttribute(attr);
829
+ if (value && value.trim() !== "") {
830
+ return `[@${attr}="${value}"]`;
831
+ }
832
+ }
833
+ }
818
834
  return "";
819
- const el = element;
820
- if (el === document.documentElement) {
821
- return "/html";
822
- }
823
- if (el === document.body) {
824
- return "/html/body";
825
- }
826
- if (el.id) {
827
- return `//*[@id="${el.id}"]`;
828
- }
829
- const ancestorWithId = findFirstAncestorWithId(el);
830
- if (ancestorWithId) {
831
- const ancestorPath = `//*[@id="${ancestorWithId.id}"]`;
832
- let current = el;
833
- const pathParts = [];
834
- while (current && current !== ancestorWithId) {
835
- const index2 = getElementIndex(current);
836
- const tagName2 = current.nodeName.toLowerCase();
837
- pathParts.unshift(`${tagName2}[${index2}]`);
838
- current = current.parentElement;
839
- }
840
- return pathParts.length > 0 ? `${ancestorPath}/${pathParts.join("/")}` : ancestorPath;
841
- }
842
- if (!el.parentNode) {
843
- return `/${el.nodeName.toLowerCase()}`;
844
- }
845
- const index = getElementIndex(el);
846
- const tagName = el.nodeName.toLowerCase();
847
- if (el.parentNode) {
848
- const parentXPath = getElementXPath(el.parentNode);
849
- return `${parentXPath}/${tagName}[${index}]`;
850
- }
851
- return `/${tagName}[${index}]`;
852
- };
853
- function generateXPaths(node) {
854
- if (!node)
855
- return [];
856
- const xPath = getElementXPath(node);
857
- return [xPath];
858
- }
859
- function getXpathsById(id) {
860
- let node = getNodeFromCacheList(id);
861
- if (!node) {
862
- return null;
835
+ };
836
+ const getPath = (node, path = "") => {
837
+ if (node.parentNode) {
838
+ path = getPath(node.parentNode, path);
839
+ }
840
+ if (node.nodeType === 1) {
841
+ const elem = node;
842
+ const tagName = elem.nodeName.toLowerCase();
843
+ let part = `/${tagName}`;
844
+ const attributePart = buildAttributePart(elem);
845
+ if (attributePart) {
846
+ part += attributePart;
847
+ } else {
848
+ const index = getIndex(node, node.nodeName);
849
+ if (index > 1) {
850
+ part += `[${index}]`;
851
+ }
852
+ }
853
+ path += part;
854
+ }
855
+ return path;
856
+ };
857
+ return getPath(element);
858
+ }
859
+ function extractTextWithPosition2(initNode) {
860
+ const elementInfoArray = [];
861
+ let nodeIndex = 1;
862
+ function dfs(node, parentNode = null) {
863
+ if (!node) {
864
+ return;
865
+ }
866
+ const currentNodeDes = { node, children: [] };
867
+ if (parentNode) {
868
+ parentNode.children.push(currentNodeDes);
869
+ }
870
+ collectElementInfo2(node);
871
+ if (node.childNodes && node.childNodes.length > 0) {
872
+ for (let i = 0; i < node.childNodes.length; i++) {
873
+ dfs(node.childNodes[i], currentNodeDes);
874
+ }
875
+ }
863
876
  }
864
- if (isTextElement(node)) {
865
- node = node.parentElement;
877
+ function collectElementInfo2(node) {
878
+ const attributes = getNodeAttributes2(node);
879
+ const rect = getRect2(attributes);
880
+ const nodeHashId = midsceneGenerateHash(null, attributes.placeholder, rect);
881
+ const text = validTextNodeContent(node);
882
+ let nodeType;
883
+ switch (node.nodeName.toUpperCase()) {
884
+ case "TEXT":
885
+ nodeType = "TEXT Node" /* TEXT */;
886
+ break;
887
+ case "IMAGE":
888
+ nodeType = "IMG Node" /* IMG */;
889
+ break;
890
+ case "BUTTON":
891
+ nodeType = "BUTTON Node" /* BUTTON */;
892
+ break;
893
+ case "SEARCHINPUT":
894
+ case "TEXTINPUT":
895
+ case "INPUT":
896
+ nodeType = "FORM_ITEM Node" /* FORM_ITEM */;
897
+ break;
898
+ case "NAV":
899
+ case "LIST":
900
+ case "CELL":
901
+ nodeType = "CONTAINER Node" /* CONTAINER */;
902
+ break;
903
+ default:
904
+ if (attributes.id === "android:id/input" || attributes.id === "android:id/inputArea") {
905
+ nodeType = "FORM_ITEM Node" /* FORM_ITEM */;
906
+ } else {
907
+ nodeType = "CONTAINER Node" /* CONTAINER */;
908
+ }
909
+ break;
910
+ }
911
+ const xpath = getXPathForElement(node);
912
+ const elementInfo = {
913
+ id: nodeHashId,
914
+ indexId: nodeIndex++,
915
+ nodeHashId,
916
+ locator: xpath,
917
+ attributes: {
918
+ nodeType,
919
+ ...attributes
920
+ },
921
+ content: text,
922
+ rect,
923
+ center: [
924
+ Math.round(rect.left + rect.width / 2),
925
+ Math.round(rect.top + rect.height / 2)
926
+ ],
927
+ nodeType
928
+ };
929
+ if (elementInfo.nodeType !== "CONTAINER Node" /* CONTAINER */) {
930
+ elementInfoArray.push(elementInfo);
931
+ }
866
932
  }
867
- return generateXPaths(node);
868
- }
869
- function getElementInfoByNode(node) {
870
- return collectElementInfo(node, window, document);
933
+ const rootNode = initNode;
934
+ const rootDescriptor = { node: rootNode, children: [] };
935
+ dfs(rootNode, rootDescriptor);
936
+ return elementInfoArray;
871
937
  }
872
938
  export {
939
+ extractTextWithPosition2 as clientExtractTextWithPosition,
873
940
  descriptionOfTree,
874
- getElementInfoByNode,
875
- getXpathsById,
876
- setNodeHashCacheListOnWindow,
877
941
  traverseTree,
878
942
  treeToList,
879
943
  extractTreeNode as webExtractNodeTree,
package/dist/es/fs.d.ts CHANGED
@@ -10,7 +10,6 @@ declare function getRunningPkgInfo(dir?: string): PkgInfo | null;
10
10
  * @returns {string|null} - The most recent package.json file path or null
11
11
  */
12
12
  declare function findNearestPackageJson(dir: string): string | null;
13
- declare function getElementInfosScriptContent(): string;
14
13
  declare function getExtraReturnLogic(tree?: boolean): Promise<string | null>;
15
14
 
16
- export { findNearestPackageJson, getElementInfosScriptContent, getExtraReturnLogic, getRunningPkgInfo };
15
+ export { findNearestPackageJson, getExtraReturnLogic, getRunningPkgInfo };
package/dist/es/fs.js CHANGED
@@ -50,19 +50,15 @@ function findNearestPackageJson(dir) {
50
50
  }
51
51
  return findNearestPackageJson(parentDir);
52
52
  }
53
- function getElementInfosScriptContent() {
53
+ async function getExtraReturnLogic(tree = false) {
54
+ if (ifInBrowser) {
55
+ return null;
56
+ }
54
57
  const currentFilePath = __filename;
55
58
  const pathDir = findNearestPackageJson(dirname(currentFilePath));
56
59
  assert(pathDir, `can't find pathDir, with ${dirname}`);
57
60
  const scriptPath = path.join(pathDir, "./dist/script/htmlElement.js");
58
61
  const elementInfosScriptContent = readFileSync(scriptPath, "utf-8");
59
- return elementInfosScriptContent;
60
- }
61
- async function getExtraReturnLogic(tree = false) {
62
- if (ifInBrowser) {
63
- return null;
64
- }
65
- const elementInfosScriptContent = `${getElementInfosScriptContent()}midscene_element_inspector.setNodeHashCacheListOnWindow();`;
66
62
  if (tree) {
67
63
  return `${elementInfosScriptContent}midscene_element_inspector.webExtractNodeTree()`;
68
64
  }
@@ -70,7 +66,6 @@ async function getExtraReturnLogic(tree = false) {
70
66
  }
71
67
  export {
72
68
  findNearestPackageJson,
73
- getElementInfosScriptContent,
74
69
  getExtraReturnLogic,
75
70
  getRunningPkgInfo
76
71
  };
package/dist/es/img.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Buffer } from 'node:buffer';
2
2
  import Jimp from 'jimp';
3
- import { R as Rect, B as BaseElement } from './index-df4e781b.js';
3
+ import { R as Rect, B as BaseElement } from './index-305e7a7e.js';
4
4
  import './constants.js';
5
5
 
6
6
  interface Size {
@@ -0,0 +1,32 @@
1
+ import { NodeType } from './constants.js';
2
+
3
+ interface Point {
4
+ left: number;
5
+ top: number;
6
+ }
7
+ interface Size {
8
+ width: number;
9
+ height: number;
10
+ dpr?: number;
11
+ }
12
+ type Rect = Point & Size & {
13
+ zoom?: number;
14
+ };
15
+ declare abstract class BaseElement {
16
+ abstract id: string;
17
+ abstract indexId?: number;
18
+ abstract attributes: {
19
+ nodeType: NodeType;
20
+ [key: string]: string;
21
+ };
22
+ abstract content: string;
23
+ abstract rect: Rect;
24
+ abstract center: [number, number];
25
+ abstract locator?: string;
26
+ }
27
+ interface ElementTreeNode<ElementType extends BaseElement = BaseElement> {
28
+ node: ElementType | null;
29
+ children: ElementTreeNode<ElementType>[];
30
+ }
31
+
32
+ export { BaseElement as B, type ElementTreeNode as E, type Rect as R };
package/dist/es/logger.js CHANGED
@@ -19,6 +19,7 @@ var MIDSCENE_DEBUG_MODE = "MIDSCENE_DEBUG_MODE";
19
19
  var MIDSCENE_MCP_USE_PUPPETEER_MODE = "MIDSCENE_MCP_USE_PUPPETEER_MODE";
20
20
  var MIDSCENE_FORCE_DEEP_THINK = "MIDSCENE_FORCE_DEEP_THINK";
21
21
  var MIDSCENE_OPENAI_SOCKS_PROXY = "MIDSCENE_OPENAI_SOCKS_PROXY";
22
+ var MIDSCENE_OPENAI_HTTP_PROXY = "MIDSCENE_OPENAI_HTTP_PROXY";
22
23
  var OPENAI_API_KEY = "OPENAI_API_KEY";
23
24
  var OPENAI_BASE_URL = "OPENAI_BASE_URL";
24
25
  var OPENAI_MAX_TOKENS = "OPENAI_MAX_TOKENS";
@@ -61,6 +62,7 @@ var allConfigFromEnv = () => {
61
62
  [MATCH_BY_POSITION]: process.env[MATCH_BY_POSITION] || void 0,
62
63
  [MIDSCENE_REPORT_TAG_NAME]: process.env[MIDSCENE_REPORT_TAG_NAME] || void 0,
63
64
  [MIDSCENE_OPENAI_SOCKS_PROXY]: process.env[MIDSCENE_OPENAI_SOCKS_PROXY] || void 0,
65
+ [MIDSCENE_OPENAI_HTTP_PROXY]: process.env[MIDSCENE_OPENAI_HTTP_PROXY] || void 0,
64
66
  [MIDSCENE_USE_AZURE_OPENAI]: process.env[MIDSCENE_USE_AZURE_OPENAI] || void 0,
65
67
  [MIDSCENE_AZURE_OPENAI_SCOPE]: process.env[MIDSCENE_AZURE_OPENAI_SCOPE] || void 0,
66
68
  [MIDSCENE_AZURE_OPENAI_INIT_CONFIG_JSON]: process.env[MIDSCENE_AZURE_OPENAI_INIT_CONFIG_JSON] || void 0,
@@ -53,6 +53,7 @@ var MIDSCENE_DEBUG_MODE = "MIDSCENE_DEBUG_MODE";
53
53
  var MIDSCENE_MCP_USE_PUPPETEER_MODE = "MIDSCENE_MCP_USE_PUPPETEER_MODE";
54
54
  var MIDSCENE_FORCE_DEEP_THINK = "MIDSCENE_FORCE_DEEP_THINK";
55
55
  var MIDSCENE_OPENAI_SOCKS_PROXY = "MIDSCENE_OPENAI_SOCKS_PROXY";
56
+ var MIDSCENE_OPENAI_HTTP_PROXY = "MIDSCENE_OPENAI_HTTP_PROXY";
56
57
  var OPENAI_API_KEY = "OPENAI_API_KEY";
57
58
  var OPENAI_BASE_URL = "OPENAI_BASE_URL";
58
59
  var OPENAI_MAX_TOKENS = "OPENAI_MAX_TOKENS";
@@ -95,6 +96,7 @@ var allConfigFromEnv = () => {
95
96
  [MATCH_BY_POSITION]: process.env[MATCH_BY_POSITION] || void 0,
96
97
  [MIDSCENE_REPORT_TAG_NAME]: process.env[MIDSCENE_REPORT_TAG_NAME] || void 0,
97
98
  [MIDSCENE_OPENAI_SOCKS_PROXY]: process.env[MIDSCENE_OPENAI_SOCKS_PROXY] || void 0,
99
+ [MIDSCENE_OPENAI_HTTP_PROXY]: process.env[MIDSCENE_OPENAI_HTTP_PROXY] || void 0,
98
100
  [MIDSCENE_USE_AZURE_OPENAI]: process.env[MIDSCENE_USE_AZURE_OPENAI] || void 0,
99
101
  [MIDSCENE_AZURE_OPENAI_SCOPE]: process.env[MIDSCENE_AZURE_OPENAI_SCOPE] || void 0,
100
102
  [MIDSCENE_AZURE_OPENAI_INIT_CONFIG_JSON]: process.env[MIDSCENE_AZURE_OPENAI_INIT_CONFIG_JSON] || void 0,