@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/lib/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/lib/env.js CHANGED
@@ -39,6 +39,7 @@ __export(env_exports, {
39
39
  MIDSCENE_LANGSMITH_DEBUG: () => MIDSCENE_LANGSMITH_DEBUG,
40
40
  MIDSCENE_MCP_USE_PUPPETEER_MODE: () => MIDSCENE_MCP_USE_PUPPETEER_MODE,
41
41
  MIDSCENE_MODEL_NAME: () => MIDSCENE_MODEL_NAME,
42
+ MIDSCENE_OPENAI_HTTP_PROXY: () => MIDSCENE_OPENAI_HTTP_PROXY,
42
43
  MIDSCENE_OPENAI_INIT_CONFIG_JSON: () => MIDSCENE_OPENAI_INIT_CONFIG_JSON,
43
44
  MIDSCENE_OPENAI_SOCKS_PROXY: () => MIDSCENE_OPENAI_SOCKS_PROXY,
44
45
  MIDSCENE_REPORT_TAG_NAME: () => MIDSCENE_REPORT_TAG_NAME,
@@ -74,6 +75,7 @@ var MIDSCENE_DEBUG_MODE = "MIDSCENE_DEBUG_MODE";
74
75
  var MIDSCENE_MCP_USE_PUPPETEER_MODE = "MIDSCENE_MCP_USE_PUPPETEER_MODE";
75
76
  var MIDSCENE_FORCE_DEEP_THINK = "MIDSCENE_FORCE_DEEP_THINK";
76
77
  var MIDSCENE_OPENAI_SOCKS_PROXY = "MIDSCENE_OPENAI_SOCKS_PROXY";
78
+ var MIDSCENE_OPENAI_HTTP_PROXY = "MIDSCENE_OPENAI_HTTP_PROXY";
77
79
  var OPENAI_API_KEY = "OPENAI_API_KEY";
78
80
  var OPENAI_BASE_URL = "OPENAI_BASE_URL";
79
81
  var OPENAI_MAX_TOKENS = "OPENAI_MAX_TOKENS";
@@ -117,6 +119,7 @@ var allConfigFromEnv = () => {
117
119
  [MATCH_BY_POSITION]: process.env[MATCH_BY_POSITION] || void 0,
118
120
  [MIDSCENE_REPORT_TAG_NAME]: process.env[MIDSCENE_REPORT_TAG_NAME] || void 0,
119
121
  [MIDSCENE_OPENAI_SOCKS_PROXY]: process.env[MIDSCENE_OPENAI_SOCKS_PROXY] || void 0,
122
+ [MIDSCENE_OPENAI_HTTP_PROXY]: process.env[MIDSCENE_OPENAI_HTTP_PROXY] || void 0,
120
123
  [MIDSCENE_USE_AZURE_OPENAI]: process.env[MIDSCENE_USE_AZURE_OPENAI] || void 0,
121
124
  [MIDSCENE_AZURE_OPENAI_SCOPE]: process.env[MIDSCENE_AZURE_OPENAI_SCOPE] || void 0,
122
125
  [MIDSCENE_AZURE_OPENAI_INIT_CONFIG_JSON]: process.env[MIDSCENE_AZURE_OPENAI_INIT_CONFIG_JSON] || void 0,
@@ -257,6 +260,7 @@ var overrideAIConfig = (newConfig, extendMode = false) => {
257
260
  MIDSCENE_LANGSMITH_DEBUG,
258
261
  MIDSCENE_MCP_USE_PUPPETEER_MODE,
259
262
  MIDSCENE_MODEL_NAME,
263
+ MIDSCENE_OPENAI_HTTP_PROXY,
260
264
  MIDSCENE_OPENAI_INIT_CONFIG_JSON,
261
265
  MIDSCENE_OPENAI_SOCKS_PROXY,
262
266
  MIDSCENE_REPORT_TAG_NAME,
@@ -364,24 +364,8 @@ function getNodeAttributes(node, currentWindow) {
364
364
  }
365
365
  function midsceneGenerateHash(node, content, rect) {
366
366
  const slicedHash = generateHashId(rect, content);
367
- if (node) {
368
- if (!window.midsceneNodeHashCacheList) {
369
- setNodeHashCacheListOnWindow();
370
- }
371
- setNodeToCacheList(node, slicedHash);
372
- }
373
367
  return slicedHash;
374
368
  }
375
- function setNodeHashCacheListOnWindow() {
376
- if (typeof window !== "undefined") {
377
- window.midsceneNodeHashCacheList = [];
378
- }
379
- }
380
- function setNodeToCacheList(node, id) {
381
- if (typeof window !== "undefined") {
382
- window.midsceneNodeHashCacheList.push({ node, id });
383
- }
384
- }
385
369
  function setMidsceneVisibleRectOnWindow() {
386
370
  if (typeof window !== "undefined") {
387
371
  window.midsceneVisibleRect = visibleRect;
@@ -452,7 +436,9 @@ function collectElementInfo(node, currentWindow, currentDocument, baseZoom = 1,
452
436
  Math.round(rect.left + rect.width / 2),
453
437
  Math.round(rect.top + rect.height / 2)
454
438
  ],
455
- zoom: rect.zoom
439
+ zoom: rect.zoom,
440
+ screenWidth: currentWindow.innerWidth,
441
+ screenHeight: currentWindow.innerHeight
456
442
  };
457
443
  return elementInfo;
458
444
  }
@@ -479,7 +465,9 @@ function collectElementInfo(node, currentWindow, currentDocument, baseZoom = 1,
479
465
  Math.round(rect.left + rect.width / 2),
480
466
  Math.round(rect.top + rect.height / 2)
481
467
  ],
482
- zoom: rect.zoom
468
+ zoom: rect.zoom,
469
+ screenWidth: currentWindow.innerWidth,
470
+ screenHeight: currentWindow.innerHeight
483
471
  };
484
472
  return elementInfo;
485
473
  }
@@ -507,7 +495,9 @@ function collectElementInfo(node, currentWindow, currentDocument, baseZoom = 1,
507
495
  Math.round(rect.left + rect.width / 2),
508
496
  Math.round(rect.top + rect.height / 2)
509
497
  ],
510
- zoom: rect.zoom
498
+ zoom: rect.zoom,
499
+ screenWidth: currentWindow.innerWidth,
500
+ screenHeight: currentWindow.innerHeight
511
501
  };
512
502
  return elementInfo;
513
503
  }
@@ -538,9 +528,12 @@ function collectElementInfo(node, currentWindow, currentDocument, baseZoom = 1,
538
528
  Math.round(rect.left + rect.width / 2),
539
529
  Math.round(rect.top + rect.height / 2)
540
530
  ],
531
+ // attributes,
541
532
  content: text,
542
533
  rect,
543
- zoom: rect.zoom
534
+ zoom: rect.zoom,
535
+ screenWidth: currentWindow.innerWidth,
536
+ screenHeight: currentWindow.innerHeight
544
537
  };
545
538
  return elementInfo;
546
539
  }
@@ -565,7 +558,9 @@ function collectElementInfo(node, currentWindow, currentDocument, baseZoom = 1,
565
558
  Math.round(rect.left + rect.width / 2),
566
559
  Math.round(rect.top + rect.height / 2)
567
560
  ],
568
- zoom: rect.zoom
561
+ zoom: rect.zoom,
562
+ screenWidth: currentWindow.innerWidth,
563
+ screenHeight: currentWindow.innerHeight
569
564
  };
570
565
  return elementInfo;
571
566
  }
@@ -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 };
@@ -20,10 +20,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/extractor/index.ts
21
21
  var extractor_exports = {};
22
22
  __export(extractor_exports, {
23
+ clientExtractTextWithPosition: () => extractTextWithPosition2,
23
24
  descriptionOfTree: () => descriptionOfTree,
24
- getElementInfoByNode: () => getElementInfoByNode,
25
- getXpathsById: () => getXpathsById,
26
- setNodeHashCacheListOnWindow: () => setNodeHashCacheListOnWindow,
27
25
  traverseTree: () => traverseTree,
28
26
  treeToList: () => treeToList,
29
27
  webExtractNodeTree: () => extractTreeNode,
@@ -517,32 +515,8 @@ function getNodeAttributes(node, currentWindow) {
517
515
  }
518
516
  function midsceneGenerateHash(node, content, rect) {
519
517
  const slicedHash = generateHashId(rect, content);
520
- if (node) {
521
- if (!window.midsceneNodeHashCacheList) {
522
- setNodeHashCacheListOnWindow();
523
- }
524
- setNodeToCacheList(node, slicedHash);
525
- }
526
518
  return slicedHash;
527
519
  }
528
- function setNodeHashCacheListOnWindow() {
529
- if (typeof window !== "undefined") {
530
- window.midsceneNodeHashCacheList = [];
531
- }
532
- }
533
- function setNodeToCacheList(node, id) {
534
- if (typeof window !== "undefined") {
535
- window.midsceneNodeHashCacheList.push({ node, id });
536
- }
537
- }
538
- function getNodeFromCacheList(id) {
539
- if (typeof window !== "undefined") {
540
- return window.midsceneNodeHashCacheList.find(
541
- (item) => item.id === id
542
- )?.node;
543
- }
544
- return null;
545
- }
546
520
  function getTopDocument() {
547
521
  const container = document.body || document;
548
522
  return container;
@@ -603,7 +577,9 @@ function collectElementInfo(node, currentWindow, currentDocument, baseZoom = 1,
603
577
  Math.round(rect.left + rect.width / 2),
604
578
  Math.round(rect.top + rect.height / 2)
605
579
  ],
606
- zoom: rect.zoom
580
+ zoom: rect.zoom,
581
+ screenWidth: currentWindow.innerWidth,
582
+ screenHeight: currentWindow.innerHeight
607
583
  };
608
584
  return elementInfo;
609
585
  }
@@ -630,7 +606,9 @@ function collectElementInfo(node, currentWindow, currentDocument, baseZoom = 1,
630
606
  Math.round(rect.left + rect.width / 2),
631
607
  Math.round(rect.top + rect.height / 2)
632
608
  ],
633
- zoom: rect.zoom
609
+ zoom: rect.zoom,
610
+ screenWidth: currentWindow.innerWidth,
611
+ screenHeight: currentWindow.innerHeight
634
612
  };
635
613
  return elementInfo;
636
614
  }
@@ -658,7 +636,9 @@ function collectElementInfo(node, currentWindow, currentDocument, baseZoom = 1,
658
636
  Math.round(rect.left + rect.width / 2),
659
637
  Math.round(rect.top + rect.height / 2)
660
638
  ],
661
- zoom: rect.zoom
639
+ zoom: rect.zoom,
640
+ screenWidth: currentWindow.innerWidth,
641
+ screenHeight: currentWindow.innerHeight
662
642
  };
663
643
  return elementInfo;
664
644
  }
@@ -689,9 +669,12 @@ function collectElementInfo(node, currentWindow, currentDocument, baseZoom = 1,
689
669
  Math.round(rect.left + rect.width / 2),
690
670
  Math.round(rect.top + rect.height / 2)
691
671
  ],
672
+ // attributes,
692
673
  content: text,
693
674
  rect,
694
- zoom: rect.zoom
675
+ zoom: rect.zoom,
676
+ screenWidth: currentWindow.innerWidth,
677
+ screenHeight: currentWindow.innerHeight
695
678
  };
696
679
  return elementInfo;
697
680
  }
@@ -716,7 +699,9 @@ function collectElementInfo(node, currentWindow, currentDocument, baseZoom = 1,
716
699
  Math.round(rect.left + rect.width / 2),
717
700
  Math.round(rect.top + rect.height / 2)
718
701
  ],
719
- zoom: rect.zoom
702
+ zoom: rect.zoom,
703
+ screenWidth: currentWindow.innerWidth,
704
+ screenHeight: currentWindow.innerHeight
720
705
  };
721
706
  return elementInfo;
722
707
  }
@@ -825,90 +810,167 @@ function extractTreeNode(initNode, debugMode2 = false) {
825
810
  };
826
811
  }
827
812
 
828
- // src/extractor/locator.ts
829
- var getElementIndex = (element) => {
830
- let index = 1;
831
- let prev = element.previousElementSibling;
832
- while (prev) {
833
- if (prev.nodeName.toLowerCase() === element.nodeName.toLowerCase()) {
834
- index++;
835
- }
836
- prev = prev.previousElementSibling;
813
+ // src/extractor/client-extractor.ts
814
+ function getNodeAttributes2(node) {
815
+ const attrs = {};
816
+ if (node && node.nodeType === 1) {
817
+ const element = node;
818
+ for (let i = 0; i < element.attributes.length; i++) {
819
+ const attr = element.attributes[i];
820
+ attrs[attr.nodeName] = attr.nodeValue ?? "";
821
+ }
822
+ }
823
+ return attrs;
824
+ }
825
+ function getRect2(attributes) {
826
+ const x = Math.round(Number.parseFloat(attributes.x ?? "0"));
827
+ const y = Math.round(Number.parseFloat(attributes.y ?? "0"));
828
+ const width = Math.round(Number.parseFloat(attributes.width ?? "0"));
829
+ const height = Math.round(Number.parseFloat(attributes.height ?? "0"));
830
+ return {
831
+ left: Math.max(0, Math.floor(x)),
832
+ top: Math.max(0, Math.floor(y)),
833
+ width: Math.max(0, width),
834
+ height: Math.max(0, height)
835
+ };
836
+ }
837
+ function validTextNodeContent(node) {
838
+ if (node.nodeType === 3) {
839
+ return node.nodeValue?.trim() || "";
837
840
  }
838
- return index;
839
- };
840
- var findFirstAncestorWithId = (element) => {
841
- let current = element;
842
- while (current?.parentElement) {
843
- if (current.id) {
844
- return current;
845
- }
846
- current = current.parentElement;
841
+ return "";
842
+ }
843
+ function getXPathForElement(element) {
844
+ if (element.nodeType !== 1) {
845
+ return "";
847
846
  }
848
- return null;
849
- };
850
- var getElementXPath = (element) => {
851
- if (element.nodeType !== Node.ELEMENT_NODE)
847
+ const getIndex = (sib, name) => {
848
+ let count = 1;
849
+ for (let cur = sib.previousSibling; cur; cur = cur.previousSibling) {
850
+ if (cur.nodeType === 1 && cur.nodeName === name) {
851
+ count++;
852
+ }
853
+ }
854
+ return count;
855
+ };
856
+ const buildAttributePart = (elem) => {
857
+ const attributes = ["id", "resource-id", "content-desc", "class"];
858
+ for (const attr of attributes) {
859
+ if (elem.hasAttribute(attr)) {
860
+ const value = elem.getAttribute(attr);
861
+ if (value && value.trim() !== "") {
862
+ return `[@${attr}="${value}"]`;
863
+ }
864
+ }
865
+ }
852
866
  return "";
853
- const el = element;
854
- if (el === document.documentElement) {
855
- return "/html";
856
- }
857
- if (el === document.body) {
858
- return "/html/body";
859
- }
860
- if (el.id) {
861
- return `//*[@id="${el.id}"]`;
862
- }
863
- const ancestorWithId = findFirstAncestorWithId(el);
864
- if (ancestorWithId) {
865
- const ancestorPath = `//*[@id="${ancestorWithId.id}"]`;
866
- let current = el;
867
- const pathParts = [];
868
- while (current && current !== ancestorWithId) {
869
- const index2 = getElementIndex(current);
870
- const tagName2 = current.nodeName.toLowerCase();
871
- pathParts.unshift(`${tagName2}[${index2}]`);
872
- current = current.parentElement;
873
- }
874
- return pathParts.length > 0 ? `${ancestorPath}/${pathParts.join("/")}` : ancestorPath;
875
- }
876
- if (!el.parentNode) {
877
- return `/${el.nodeName.toLowerCase()}`;
878
- }
879
- const index = getElementIndex(el);
880
- const tagName = el.nodeName.toLowerCase();
881
- if (el.parentNode) {
882
- const parentXPath = getElementXPath(el.parentNode);
883
- return `${parentXPath}/${tagName}[${index}]`;
884
- }
885
- return `/${tagName}[${index}]`;
886
- };
887
- function generateXPaths(node) {
888
- if (!node)
889
- return [];
890
- const xPath = getElementXPath(node);
891
- return [xPath];
892
- }
893
- function getXpathsById(id) {
894
- let node = getNodeFromCacheList(id);
895
- if (!node) {
896
- return null;
867
+ };
868
+ const getPath = (node, path = "") => {
869
+ if (node.parentNode) {
870
+ path = getPath(node.parentNode, path);
871
+ }
872
+ if (node.nodeType === 1) {
873
+ const elem = node;
874
+ const tagName = elem.nodeName.toLowerCase();
875
+ let part = `/${tagName}`;
876
+ const attributePart = buildAttributePart(elem);
877
+ if (attributePart) {
878
+ part += attributePart;
879
+ } else {
880
+ const index = getIndex(node, node.nodeName);
881
+ if (index > 1) {
882
+ part += `[${index}]`;
883
+ }
884
+ }
885
+ path += part;
886
+ }
887
+ return path;
888
+ };
889
+ return getPath(element);
890
+ }
891
+ function extractTextWithPosition2(initNode) {
892
+ const elementInfoArray = [];
893
+ let nodeIndex = 1;
894
+ function dfs(node, parentNode = null) {
895
+ if (!node) {
896
+ return;
897
+ }
898
+ const currentNodeDes = { node, children: [] };
899
+ if (parentNode) {
900
+ parentNode.children.push(currentNodeDes);
901
+ }
902
+ collectElementInfo2(node);
903
+ if (node.childNodes && node.childNodes.length > 0) {
904
+ for (let i = 0; i < node.childNodes.length; i++) {
905
+ dfs(node.childNodes[i], currentNodeDes);
906
+ }
907
+ }
897
908
  }
898
- if (isTextElement(node)) {
899
- node = node.parentElement;
909
+ function collectElementInfo2(node) {
910
+ const attributes = getNodeAttributes2(node);
911
+ const rect = getRect2(attributes);
912
+ const nodeHashId = midsceneGenerateHash(null, attributes.placeholder, rect);
913
+ const text = validTextNodeContent(node);
914
+ let nodeType;
915
+ switch (node.nodeName.toUpperCase()) {
916
+ case "TEXT":
917
+ nodeType = "TEXT Node" /* TEXT */;
918
+ break;
919
+ case "IMAGE":
920
+ nodeType = "IMG Node" /* IMG */;
921
+ break;
922
+ case "BUTTON":
923
+ nodeType = "BUTTON Node" /* BUTTON */;
924
+ break;
925
+ case "SEARCHINPUT":
926
+ case "TEXTINPUT":
927
+ case "INPUT":
928
+ nodeType = "FORM_ITEM Node" /* FORM_ITEM */;
929
+ break;
930
+ case "NAV":
931
+ case "LIST":
932
+ case "CELL":
933
+ nodeType = "CONTAINER Node" /* CONTAINER */;
934
+ break;
935
+ default:
936
+ if (attributes.id === "android:id/input" || attributes.id === "android:id/inputArea") {
937
+ nodeType = "FORM_ITEM Node" /* FORM_ITEM */;
938
+ } else {
939
+ nodeType = "CONTAINER Node" /* CONTAINER */;
940
+ }
941
+ break;
942
+ }
943
+ const xpath = getXPathForElement(node);
944
+ const elementInfo = {
945
+ id: nodeHashId,
946
+ indexId: nodeIndex++,
947
+ nodeHashId,
948
+ locator: xpath,
949
+ attributes: {
950
+ nodeType,
951
+ ...attributes
952
+ },
953
+ content: text,
954
+ rect,
955
+ center: [
956
+ Math.round(rect.left + rect.width / 2),
957
+ Math.round(rect.top + rect.height / 2)
958
+ ],
959
+ nodeType
960
+ };
961
+ if (elementInfo.nodeType !== "CONTAINER Node" /* CONTAINER */) {
962
+ elementInfoArray.push(elementInfo);
963
+ }
900
964
  }
901
- return generateXPaths(node);
902
- }
903
- function getElementInfoByNode(node) {
904
- return collectElementInfo(node, window, document);
965
+ const rootNode = initNode;
966
+ const rootDescriptor = { node: rootNode, children: [] };
967
+ dfs(rootNode, rootDescriptor);
968
+ return elementInfoArray;
905
969
  }
906
970
  // Annotate the CommonJS export names for ESM import in node:
907
971
  0 && (module.exports = {
972
+ clientExtractTextWithPosition,
908
973
  descriptionOfTree,
909
- getElementInfoByNode,
910
- getXpathsById,
911
- setNodeHashCacheListOnWindow,
912
974
  traverseTree,
913
975
  treeToList,
914
976
  webExtractNodeTree,
package/dist/lib/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/lib/fs.js CHANGED
@@ -31,7 +31,6 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
31
31
  var fs_exports = {};
32
32
  __export(fs_exports, {
33
33
  findNearestPackageJson: () => findNearestPackageJson,
34
- getElementInfosScriptContent: () => getElementInfosScriptContent,
35
34
  getExtraReturnLogic: () => getExtraReturnLogic,
36
35
  getRunningPkgInfo: () => getRunningPkgInfo
37
36
  });
@@ -87,19 +86,15 @@ function findNearestPackageJson(dir) {
87
86
  }
88
87
  return findNearestPackageJson(parentDir);
89
88
  }
90
- function getElementInfosScriptContent() {
89
+ async function getExtraReturnLogic(tree = false) {
90
+ if (ifInBrowser) {
91
+ return null;
92
+ }
91
93
  const currentFilePath = __filename;
92
94
  const pathDir = findNearestPackageJson((0, import_node_path.dirname)(currentFilePath));
93
95
  assert(pathDir, `can't find pathDir, with ${import_node_path.dirname}`);
94
96
  const scriptPath = import_node_path2.default.join(pathDir, "./dist/script/htmlElement.js");
95
97
  const elementInfosScriptContent = (0, import_node_fs.readFileSync)(scriptPath, "utf-8");
96
- return elementInfosScriptContent;
97
- }
98
- async function getExtraReturnLogic(tree = false) {
99
- if (ifInBrowser) {
100
- return null;
101
- }
102
- const elementInfosScriptContent = `${getElementInfosScriptContent()}midscene_element_inspector.setNodeHashCacheListOnWindow();`;
103
98
  if (tree) {
104
99
  return `${elementInfosScriptContent}midscene_element_inspector.webExtractNodeTree()`;
105
100
  }
@@ -108,7 +103,6 @@ async function getExtraReturnLogic(tree = false) {
108
103
  // Annotate the CommonJS export names for ESM import in node:
109
104
  0 && (module.exports = {
110
105
  findNearestPackageJson,
111
- getElementInfosScriptContent,
112
106
  getExtraReturnLogic,
113
107
  getRunningPkgInfo
114
108
  });
package/dist/lib/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 };
@@ -55,6 +55,7 @@ var MIDSCENE_DEBUG_MODE = "MIDSCENE_DEBUG_MODE";
55
55
  var MIDSCENE_MCP_USE_PUPPETEER_MODE = "MIDSCENE_MCP_USE_PUPPETEER_MODE";
56
56
  var MIDSCENE_FORCE_DEEP_THINK = "MIDSCENE_FORCE_DEEP_THINK";
57
57
  var MIDSCENE_OPENAI_SOCKS_PROXY = "MIDSCENE_OPENAI_SOCKS_PROXY";
58
+ var MIDSCENE_OPENAI_HTTP_PROXY = "MIDSCENE_OPENAI_HTTP_PROXY";
58
59
  var OPENAI_API_KEY = "OPENAI_API_KEY";
59
60
  var OPENAI_BASE_URL = "OPENAI_BASE_URL";
60
61
  var OPENAI_MAX_TOKENS = "OPENAI_MAX_TOKENS";
@@ -97,6 +98,7 @@ var allConfigFromEnv = () => {
97
98
  [MATCH_BY_POSITION]: process.env[MATCH_BY_POSITION] || void 0,
98
99
  [MIDSCENE_REPORT_TAG_NAME]: process.env[MIDSCENE_REPORT_TAG_NAME] || void 0,
99
100
  [MIDSCENE_OPENAI_SOCKS_PROXY]: process.env[MIDSCENE_OPENAI_SOCKS_PROXY] || void 0,
101
+ [MIDSCENE_OPENAI_HTTP_PROXY]: process.env[MIDSCENE_OPENAI_HTTP_PROXY] || void 0,
100
102
  [MIDSCENE_USE_AZURE_OPENAI]: process.env[MIDSCENE_USE_AZURE_OPENAI] || void 0,
101
103
  [MIDSCENE_AZURE_OPENAI_SCOPE]: process.env[MIDSCENE_AZURE_OPENAI_SCOPE] || void 0,
102
104
  [MIDSCENE_AZURE_OPENAI_INIT_CONFIG_JSON]: process.env[MIDSCENE_AZURE_OPENAI_INIT_CONFIG_JSON] || void 0,