Skip to content

Commit f67714d

Browse files
committed
refactor(extract): modify dependencies
1 parent 067cc31 commit f67714d

34 files changed

+349
-310
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ test-results/
9696
playwright-report/
9797
blob-report/
9898
playwright/.cache/
99+
iife-script/
99100

100101
# Midscene.js dump files
101102
__ai_responses__/

biome.json

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"**/doc_build",
1717
"*-dump.json",
1818
"test-results/**",
19+
"iife-script/**",
1920
"script_get_all_texts.tmp.js",
2021
"**/playwright-report/**",
2122
"**/todo-report.spec.ts-snapshots/**",

packages/cli/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"forceConsistentCasingInFileNames": true,
88
"isolatedModules": true,
99
"jsx": "preserve",
10-
"lib": ["ESNext"],
10+
"lib": ["ESNext", "DOM"],
1111
"moduleResolution": "node",
1212
"paths": {
1313
"@/*": ["./src/*"]

packages/midscene/src/ai-model/prompt/util.ts

+5-16
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import assert from 'node:assert';
22
import { MATCH_BY_POSITION, getAIConfig } from '@/env';
33
import { imageInfoOfBase64 } from '@/image';
4-
import { descriptionOfTree } from '@/tree';
54
import type { BaseElement, ElementTreeNode, Size, UIContext } from '@/types';
65
import { PromptTemplate } from '@langchain/core/prompts';
76
import { NodeType } from '@midscene/shared/constants';
7+
import { descriptionOfTree, treeToList } from '@midscene/shared/extractor';
88
import { generateHashId } from '@midscene/shared/utils';
99
import type { ResponseFormatJSONSchema } from 'openai/resources';
1010

@@ -261,21 +261,10 @@ export async function describeUserPage<
261261
const treeRoot = context.tree;
262262
// dfs tree, save the id and element info
263263
const idElementMap: Record<string, ElementType> = {};
264-
const flatElements: ElementType[] = [];
265-
function dfsTree(node: ElementTreeNode<ElementType>) {
266-
if (node?.node) {
267-
idElementMap[node.node.id] = node.node;
268-
269-
if (typeof node.node.indexId !== 'undefined') {
270-
idElementMap[`${node.node.indexId}`] = node.node;
271-
}
272-
flatElements.push(node.node);
273-
}
274-
for (let i = 0; i < (node.children || []).length; i++) {
275-
dfsTree(node.children[i]);
276-
}
277-
}
278-
dfsTree(treeRoot);
264+
const flatElements: ElementType[] = treeToList(treeRoot);
265+
flatElements.forEach((element) => {
266+
idElementMap[element.id] = element;
267+
});
279268

280269
const contentTree = await descriptionOfTree(
281270
treeRoot,

packages/shared/modern.config.ts

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ const commonConfig = {
55
index: './src/index.ts',
66
img: './src/img/index.ts',
77
constants: './src/constants/index.ts',
8+
extractor: './src/extractor/index.ts',
9+
'extractor-debug': './src/extractor/debug.ts',
810
fs: './src/fs/index.ts',
911
utils: './src/utils.ts',
1012
'us-keyboard-layout': './src/us-keyboard-layout.ts',

packages/shared/package.json

+27-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@
3838
"require": "./dist/lib/utils.js",
3939
"import": "./dist/es/utils.js"
4040
},
41+
"./extractor": {
42+
"types": "./src/extractor/index.ts",
43+
"require": "./dist/lib/extractor.js",
44+
"import": "./dist/es/extractor.js"
45+
},
46+
"./extractor-debug": {
47+
"types": "./src/extractor/debug.ts",
48+
"require": "./dist/lib/extractor-debug.js",
49+
"import": "./dist/es/extractor-debug.js"
50+
},
4151
"./keyboard-layout": {
4252
"types": "./src/us-keyboard-layout.ts",
4353
"require": "./dist/lib/us-keyboard-layout.js",
@@ -52,13 +62,17 @@
5262
"browser/img": ["./src/img/index.ts"],
5363
"fs": ["./src/fs/index.ts"],
5464
"utils": ["./src/utils.ts"],
65+
"extractor": ["./src/extractor/index.ts"],
66+
"extractor-debug": ["./src/extractor/debug.ts"],
5567
"keyboard-layout": ["./src/us-keyboard-layout.ts"]
5668
}
5769
},
5870
"files": ["dist", "src", "README.md"],
5971
"scripts": {
6072
"dev": "modern dev",
61-
"build": "modern build",
73+
"build": "npm run build:pkg && npm run build:script",
74+
"build:pkg": "modern build -c ./modern.config.ts",
75+
"build:script": "modern build -c ./modern.inspect.config.ts",
6276
"build:watch": "modern build -w",
6377
"reset": "rimraf ./**/node_modules",
6478
"lint": "modern lint",
@@ -120,6 +134,16 @@
120134
"require": "./dist/lib/utils.js",
121135
"import": "./dist/es/utils.js"
122136
},
137+
"./extractor": {
138+
"types": "./dist/lib/extractor.d.ts",
139+
"require": "./dist/lib/extractor.js",
140+
"import": "./dist/es/extractor.js"
141+
},
142+
"./extractor-debug": {
143+
"types": "./dist/lib/extractor-debug.d.ts",
144+
"require": "./dist/lib/extractor-debug.js",
145+
"import": "./dist/es/extractor-debug.js"
146+
},
123147
"./keyboard-layout": {
124148
"types": "./dist/lib/us-keyboard-layout.d.ts",
125149
"require": "./dist/lib/us-keyboard-layout.js",
@@ -134,6 +158,8 @@
134158
"browser/img": ["./dist/browser/img.d.ts"],
135159
"fs": ["./dist/lib/fs.d.ts"],
136160
"utils": ["./dist/lib/utils.d.ts"],
161+
"extractor": ["./dist/lib/extractor.d.ts"],
162+
"extractor-debug": ["./dist/lib/extractor-debug.d.ts"],
137163
"keyboard-layout": ["./dist/lib/us-keyboard-layout.d.ts"]
138164
}
139165
}

packages/web-integration/src/extractor/client-extractor.ts packages/shared/src/extractor/client-extractor.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@ import { generateId, midsceneGenerateHash } from './util';
55
// https://github.com/appium/appium/tree/master/packages/universal-xml-plugin
66
// Definition of NodeDescriptor interface
77
interface NodeDescriptor {
8-
node: Node;
8+
node: globalThis.Node;
99
children: NodeDescriptor[];
1010
}
1111

1212
// Retrieve attributes from a node
13-
function getNodeAttributes(node: Node): { [key: string]: string } {
13+
function getNodeAttributes(node: globalThis.Node): { [key: string]: string } {
1414
const attrs: { [key: string]: string } = {};
1515

1616
// Check if node exists and its type is ELEMENT_NODE
1717
if (node && node.nodeType === 1) {
18-
const element = node as Element;
18+
const element = node as globalThis.Element;
1919

2020
for (let i = 0; i < element.attributes.length; i++) {
2121
const attr = element.attributes[i];
@@ -115,7 +115,9 @@ function getXPathForElement(element: Node): string {
115115
}
116116

117117
// Perform DFS traversal and collect element information
118-
export function extractTextWithPosition(initNode: Document): ElementInfo[] {
118+
export function extractTextWithPosition(
119+
initNode: globalThis.Document,
120+
): ElementInfo[] {
119121
const elementInfoArray: ElementInfo[] = [];
120122
let nodeIndex = 1;
121123

packages/web-integration/src/extractor/constants.ts packages/shared/src/extractor/constants.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ export {
22
NodeType,
33
TEXT_MAX_SIZE,
44
TEXT_SIZE_THRESHOLD,
5-
} from '@midscene/shared/constants';
5+
} from '../constants';

packages/web-integration/src/extractor/dom-util.ts packages/shared/src/extractor/dom-util.ts

+15-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export function isFormElement(node: Node) {
1+
export function isFormElement(node: globalThis.Node) {
22
return (
33
node instanceof HTMLElement &&
44
(node.tagName.toLowerCase() === 'input' ||
@@ -8,11 +8,15 @@ export function isFormElement(node: Node) {
88
);
99
}
1010

11-
export function isButtonElement(node: Node): node is HTMLButtonElement {
11+
export function isButtonElement(
12+
node: globalThis.Node,
13+
): node is globalThis.HTMLButtonElement {
1214
return node instanceof HTMLElement && node.tagName.toLowerCase() === 'button';
1315
}
1416

15-
export function isImgElement(node: Node): node is HTMLImageElement {
17+
export function isImgElement(
18+
node: globalThis.Node,
19+
): node is globalThis.HTMLImageElement {
1620
// check if the node is an image element
1721
if (!includeBaseElement(node) && node instanceof Element) {
1822
const computedStyle = window.getComputedStyle(node);
@@ -32,7 +36,7 @@ export function isImgElement(node: Node): node is HTMLImageElement {
3236
);
3337
}
3438

35-
function isIconfont(node: Node): boolean {
39+
function isIconfont(node: globalThis.Node): boolean {
3640
if (node instanceof Element) {
3741
const computedStyle = window.getComputedStyle(node);
3842
const fontFamilyValue = computedStyle.fontFamily || '';
@@ -42,11 +46,15 @@ function isIconfont(node: Node): boolean {
4246
return false;
4347
}
4448

45-
export function isTextElement(node: Node): node is HTMLTextAreaElement {
49+
export function isTextElement(
50+
node: globalThis.Node,
51+
): node is globalThis.HTMLTextAreaElement {
4652
return node.nodeName.toLowerCase() === '#text' && !isIconfont(node);
4753
}
4854

49-
export function isContainerElement(node: Node): node is HTMLElement {
55+
export function isContainerElement(
56+
node: globalThis.Node,
57+
): node is globalThis.HTMLElement {
5058
if (!(node instanceof HTMLElement)) return false;
5159

5260
// include other base elements
@@ -63,7 +71,7 @@ export function isContainerElement(node: Node): node is HTMLElement {
6371
return false;
6472
}
6573

66-
function includeBaseElement(node: Node) {
74+
function includeBaseElement(node: globalThis.Node) {
6775
if (!(node instanceof HTMLElement)) return false;
6876

6977
// include text

packages/web-integration/src/extractor/index.ts packages/shared/src/extractor/index.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { NodeType } from '@midscene/shared/constants';
1+
import type { NodeType } from '../constants';
22

33
export interface ElementInfo {
44
id: string;
@@ -20,6 +20,8 @@ export interface ElementNode {
2020
children: ElementNode[];
2121
}
2222

23+
export { descriptionOfTree, traverseTree, treeToList } from './tree';
24+
2325
export { extractTextWithPosition as webExtractTextWithPosition } from './web-extractor';
2426

2527
export { extractTextWithPosition as clientExtractTextWithPosition } from './client-extractor';

0 commit comments

Comments
 (0)