@aiscene/shared 1.6.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.
Files changed (212) hide show
  1. package/README.md +9 -0
  2. package/dist/es/baseDB.mjs +109 -0
  3. package/dist/es/build/copy-static.mjs +31 -0
  4. package/dist/es/build/rspack-config.mjs +4 -0
  5. package/dist/es/cli/cli-runner.mjs +140 -0
  6. package/dist/es/cli/index.mjs +2 -0
  7. package/dist/es/common.mjs +37 -0
  8. package/dist/es/constants/example-code.mjs +223 -0
  9. package/dist/es/constants/index.mjs +23 -0
  10. package/dist/es/env/basic.mjs +6 -0
  11. package/dist/es/env/constants.mjs +70 -0
  12. package/dist/es/env/global-config-manager.mjs +94 -0
  13. package/dist/es/env/helper.mjs +43 -0
  14. package/dist/es/env/index.mjs +5 -0
  15. package/dist/es/env/init-debug.mjs +18 -0
  16. package/dist/es/env/model-config-manager.mjs +79 -0
  17. package/dist/es/env/parse-model-config.mjs +132 -0
  18. package/dist/es/env/types.mjs +211 -0
  19. package/dist/es/env/utils.mjs +26 -0
  20. package/dist/es/extractor/constants.mjs +2 -0
  21. package/dist/es/extractor/debug.mjs +6 -0
  22. package/dist/es/extractor/dom-util.mjs +92 -0
  23. package/dist/es/extractor/index.mjs +5 -0
  24. package/dist/es/extractor/locator.mjs +250 -0
  25. package/dist/es/extractor/tree.mjs +78 -0
  26. package/dist/es/extractor/util.mjs +245 -0
  27. package/dist/es/extractor/web-extractor.mjs +303 -0
  28. package/dist/es/img/box-select.mjs +824 -0
  29. package/dist/es/img/canvas-fallback.mjs +238 -0
  30. package/dist/es/img/get-photon.mjs +45 -0
  31. package/dist/es/img/get-sharp.mjs +11 -0
  32. package/dist/es/img/index.mjs +4 -0
  33. package/dist/es/img/info.mjs +29 -0
  34. package/dist/es/img/transform.mjs +275 -0
  35. package/dist/es/index.mjs +4 -0
  36. package/dist/es/logger.mjs +70 -0
  37. package/dist/es/mcp/base-server.mjs +281 -0
  38. package/dist/es/mcp/base-tools.mjs +91 -0
  39. package/dist/es/mcp/chrome-path.mjs +35 -0
  40. package/dist/es/mcp/index.mjs +7 -0
  41. package/dist/es/mcp/inject-report-html-plugin.mjs +53 -0
  42. package/dist/es/mcp/launcher-helper.mjs +52 -0
  43. package/dist/es/mcp/tool-generator.mjs +297 -0
  44. package/dist/es/mcp/types.mjs +3 -0
  45. package/dist/es/node/fs.mjs +44 -0
  46. package/dist/es/node/index.mjs +2 -0
  47. package/dist/es/node/port.mjs +24 -0
  48. package/dist/es/polyfills/async-hooks.mjs +2 -0
  49. package/dist/es/polyfills/index.mjs +1 -0
  50. package/dist/es/types/index.mjs +3 -0
  51. package/dist/es/us-keyboard-layout.mjs +1414 -0
  52. package/dist/es/us-keyboard-layout.mjs.LICENSE.txt +5 -0
  53. package/dist/es/utils.mjs +77 -0
  54. package/dist/es/zod-schema-utils.mjs +54 -0
  55. package/dist/lib/baseDB.js +149 -0
  56. package/dist/lib/build/copy-static.js +79 -0
  57. package/dist/lib/build/rspack-config.js +38 -0
  58. package/dist/lib/cli/cli-runner.js +196 -0
  59. package/dist/lib/cli/index.js +48 -0
  60. package/dist/lib/common.js +93 -0
  61. package/dist/lib/constants/example-code.js +260 -0
  62. package/dist/lib/constants/index.js +96 -0
  63. package/dist/lib/env/basic.js +40 -0
  64. package/dist/lib/env/constants.js +113 -0
  65. package/dist/lib/env/global-config-manager.js +128 -0
  66. package/dist/lib/env/helper.js +80 -0
  67. package/dist/lib/env/index.js +90 -0
  68. package/dist/lib/env/init-debug.js +52 -0
  69. package/dist/lib/env/model-config-manager.js +113 -0
  70. package/dist/lib/env/parse-model-config.js +178 -0
  71. package/dist/lib/env/types.js +518 -0
  72. package/dist/lib/env/utils.js +72 -0
  73. package/dist/lib/extractor/constants.js +42 -0
  74. package/dist/lib/extractor/debug.js +12 -0
  75. package/dist/lib/extractor/dom-util.js +153 -0
  76. package/dist/lib/extractor/index.js +81 -0
  77. package/dist/lib/extractor/locator.js +296 -0
  78. package/dist/lib/extractor/tree.js +124 -0
  79. package/dist/lib/extractor/util.js +336 -0
  80. package/dist/lib/extractor/web-extractor.js +349 -0
  81. package/dist/lib/img/box-select.js +875 -0
  82. package/dist/lib/img/canvas-fallback.js +305 -0
  83. package/dist/lib/img/get-photon.js +82 -0
  84. package/dist/lib/img/get-sharp.js +45 -0
  85. package/dist/lib/img/index.js +92 -0
  86. package/dist/lib/img/info.js +83 -0
  87. package/dist/lib/img/transform.js +364 -0
  88. package/dist/lib/index.js +47 -0
  89. package/dist/lib/logger.js +123 -0
  90. package/dist/lib/mcp/base-server.js +331 -0
  91. package/dist/lib/mcp/base-tools.js +125 -0
  92. package/dist/lib/mcp/chrome-path.js +72 -0
  93. package/dist/lib/mcp/index.js +100 -0
  94. package/dist/lib/mcp/inject-report-html-plugin.js +98 -0
  95. package/dist/lib/mcp/launcher-helper.js +86 -0
  96. package/dist/lib/mcp/tool-generator.js +334 -0
  97. package/dist/lib/mcp/types.js +40 -0
  98. package/dist/lib/node/fs.js +97 -0
  99. package/dist/lib/node/index.js +65 -0
  100. package/dist/lib/node/port.js +61 -0
  101. package/dist/lib/polyfills/async-hooks.js +36 -0
  102. package/dist/lib/polyfills/index.js +58 -0
  103. package/dist/lib/types/index.js +37 -0
  104. package/dist/lib/us-keyboard-layout.js +1457 -0
  105. package/dist/lib/us-keyboard-layout.js.LICENSE.txt +5 -0
  106. package/dist/lib/utils.js +156 -0
  107. package/dist/lib/zod-schema-utils.js +97 -0
  108. package/dist/types/baseDB.d.ts +25 -0
  109. package/dist/types/build/copy-static.d.ts +31 -0
  110. package/dist/types/build/rspack-config.d.ts +8 -0
  111. package/dist/types/cli/cli-runner.d.ts +14 -0
  112. package/dist/types/cli/index.d.ts +2 -0
  113. package/dist/types/common.d.ts +12 -0
  114. package/dist/types/constants/example-code.d.ts +2 -0
  115. package/dist/types/constants/index.d.ts +21 -0
  116. package/dist/types/env/basic.d.ts +6 -0
  117. package/dist/types/env/constants.d.ts +40 -0
  118. package/dist/types/env/global-config-manager.d.ts +32 -0
  119. package/dist/types/env/helper.d.ts +4 -0
  120. package/dist/types/env/index.d.ts +4 -0
  121. package/dist/types/env/init-debug.d.ts +1 -0
  122. package/dist/types/env/model-config-manager.d.ts +25 -0
  123. package/dist/types/env/parse-model-config.d.ts +31 -0
  124. package/dist/types/env/types.d.ts +309 -0
  125. package/dist/types/env/utils.d.ts +38 -0
  126. package/dist/types/extractor/constants.d.ts +1 -0
  127. package/dist/types/extractor/debug.d.ts +1 -0
  128. package/dist/types/extractor/dom-util.d.ts +56 -0
  129. package/dist/types/extractor/index.d.ts +32 -0
  130. package/dist/types/extractor/locator.d.ts +9 -0
  131. package/dist/types/extractor/tree.d.ts +6 -0
  132. package/dist/types/extractor/util.d.ts +47 -0
  133. package/dist/types/extractor/web-extractor.d.ts +19 -0
  134. package/dist/types/img/box-select.d.ts +26 -0
  135. package/dist/types/img/canvas-fallback.d.ts +105 -0
  136. package/dist/types/img/get-photon.d.ts +19 -0
  137. package/dist/types/img/get-sharp.d.ts +3 -0
  138. package/dist/types/img/index.d.ts +3 -0
  139. package/dist/types/img/info.d.ts +29 -0
  140. package/dist/types/img/transform.d.ts +98 -0
  141. package/dist/types/index.d.ts +4 -0
  142. package/dist/types/logger.d.ts +6 -0
  143. package/dist/types/mcp/base-server.d.ts +93 -0
  144. package/dist/types/mcp/base-tools.d.ts +79 -0
  145. package/dist/types/mcp/chrome-path.d.ts +2 -0
  146. package/dist/types/mcp/index.d.ts +7 -0
  147. package/dist/types/mcp/inject-report-html-plugin.d.ts +18 -0
  148. package/dist/types/mcp/launcher-helper.d.ts +94 -0
  149. package/dist/types/mcp/tool-generator.d.ts +10 -0
  150. package/dist/types/mcp/types.d.ts +103 -0
  151. package/dist/types/node/fs.d.ts +15 -0
  152. package/dist/types/node/index.d.ts +2 -0
  153. package/dist/types/node/port.d.ts +8 -0
  154. package/dist/types/polyfills/async-hooks.d.ts +6 -0
  155. package/dist/types/polyfills/index.d.ts +4 -0
  156. package/dist/types/types/index.d.ts +34 -0
  157. package/dist/types/us-keyboard-layout.d.ts +32 -0
  158. package/dist/types/utils.d.ts +37 -0
  159. package/dist/types/zod-schema-utils.d.ts +23 -0
  160. package/package.json +121 -0
  161. package/src/baseDB.ts +158 -0
  162. package/src/build/copy-static.ts +68 -0
  163. package/src/build/rspack-config.ts +12 -0
  164. package/src/cli/cli-runner.ts +224 -0
  165. package/src/cli/index.ts +8 -0
  166. package/src/common.ts +67 -0
  167. package/src/constants/example-code.ts +223 -0
  168. package/src/constants/index.ts +29 -0
  169. package/src/env/basic.ts +12 -0
  170. package/src/env/constants.ts +234 -0
  171. package/src/env/global-config-manager.ts +191 -0
  172. package/src/env/helper.ts +58 -0
  173. package/src/env/index.ts +4 -0
  174. package/src/env/init-debug.ts +34 -0
  175. package/src/env/model-config-manager.ts +149 -0
  176. package/src/env/parse-model-config.ts +294 -0
  177. package/src/env/types.ts +533 -0
  178. package/src/env/utils.ts +89 -0
  179. package/src/extractor/constants.ts +5 -0
  180. package/src/extractor/debug.ts +10 -0
  181. package/src/extractor/dom-util.ts +226 -0
  182. package/src/extractor/index.ts +48 -0
  183. package/src/extractor/locator.ts +469 -0
  184. package/src/extractor/tree.ts +179 -0
  185. package/src/extractor/util.ts +482 -0
  186. package/src/extractor/web-extractor.ts +481 -0
  187. package/src/img/box-select.ts +588 -0
  188. package/src/img/canvas-fallback.ts +393 -0
  189. package/src/img/get-photon.ts +108 -0
  190. package/src/img/get-sharp.ts +18 -0
  191. package/src/img/index.ts +25 -0
  192. package/src/img/info.ts +75 -0
  193. package/src/img/transform.ts +553 -0
  194. package/src/index.ts +8 -0
  195. package/src/logger.ts +107 -0
  196. package/src/mcp/base-server.ts +502 -0
  197. package/src/mcp/base-tools.ts +185 -0
  198. package/src/mcp/chrome-path.ts +48 -0
  199. package/src/mcp/index.ts +7 -0
  200. package/src/mcp/inject-report-html-plugin.ts +119 -0
  201. package/src/mcp/launcher-helper.ts +200 -0
  202. package/src/mcp/tool-generator.ts +429 -0
  203. package/src/mcp/types.ts +112 -0
  204. package/src/node/fs.ts +84 -0
  205. package/src/node/index.ts +2 -0
  206. package/src/node/port.ts +37 -0
  207. package/src/polyfills/async-hooks.ts +6 -0
  208. package/src/polyfills/index.ts +4 -0
  209. package/src/types/index.ts +52 -0
  210. package/src/us-keyboard-layout.ts +723 -0
  211. package/src/utils.ts +166 -0
  212. package/src/zod-schema-utils.ts +133 -0
@@ -0,0 +1,103 @@
1
+ import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
2
+ import type { z } from 'zod';
3
+ /**
4
+ * Default timeout constants for app loading verification
5
+ */
6
+ export declare const defaultAppLoadingTimeoutMs = 10000;
7
+ export declare const defaultAppLoadingCheckIntervalMs = 2000;
8
+ /**
9
+ * Content item types for tool results (MCP compatible)
10
+ */
11
+ export type ToolResultContent = {
12
+ type: 'text';
13
+ text: string;
14
+ } | {
15
+ type: 'image';
16
+ data: string;
17
+ mimeType: string;
18
+ } | {
19
+ type: 'audio';
20
+ data: string;
21
+ mimeType: string;
22
+ } | {
23
+ type: 'resource';
24
+ resource: {
25
+ text: string;
26
+ uri: string;
27
+ mimeType?: string;
28
+ } | {
29
+ uri: string;
30
+ blob: string;
31
+ mimeType?: string;
32
+ };
33
+ };
34
+ /**
35
+ * Result type for tool execution (MCP compatible)
36
+ */
37
+ export interface ToolResult {
38
+ [x: string]: unknown;
39
+ content: ToolResultContent[];
40
+ isError?: boolean;
41
+ _meta?: Record<string, unknown>;
42
+ }
43
+ /**
44
+ * Tool handler function type
45
+ * Takes parsed arguments and returns a tool result
46
+ */
47
+ export type ToolHandler<T = Record<string, unknown>> = (args: T) => Promise<ToolResult>;
48
+ /**
49
+ * Tool schema type using Zod
50
+ */
51
+ export type ToolSchema = Record<string, z.ZodTypeAny>;
52
+ /**
53
+ * Tool definition for MCP server
54
+ */
55
+ export interface ToolDefinition<T = Record<string, unknown>> {
56
+ name: string;
57
+ description: string;
58
+ schema: ToolSchema;
59
+ handler: ToolHandler<T>;
60
+ }
61
+ /**
62
+ * Tool type for mcpKitForAgent return value
63
+ */
64
+ export type Tool = ToolDefinition;
65
+ /**
66
+ * Action space item definition
67
+ * Note: Intentionally no index signature to maintain compatibility with DeviceAction
68
+ */
69
+ export interface ActionSpaceItem {
70
+ name: string;
71
+ description?: string;
72
+ args?: Record<string, unknown>;
73
+ paramSchema?: z.ZodTypeAny;
74
+ }
75
+ /**
76
+ * Base agent interface
77
+ * Represents a platform-specific agent (Android, iOS, Web)
78
+ * Note: Return types use `unknown` for compatibility with platform-specific implementations
79
+ */
80
+ export interface BaseAgent {
81
+ getActionSpace(): Promise<ActionSpaceItem[]>;
82
+ destroy?(): Promise<void>;
83
+ page?: {
84
+ screenshotBase64(): Promise<string>;
85
+ };
86
+ aiAction?: (description: string, params?: Record<string, unknown>) => Promise<unknown>;
87
+ aiWaitFor?: (assertion: string, options: Record<string, unknown>) => Promise<unknown>;
88
+ }
89
+ /**
90
+ * Base device interface for temporary device instances
91
+ */
92
+ export interface BaseDevice {
93
+ actionSpace(): ActionSpaceItem[];
94
+ destroy?(): Promise<void>;
95
+ }
96
+ /**
97
+ * Interface for platform-specific MCP tools manager
98
+ */
99
+ export interface IMidsceneTools {
100
+ attachToServer(server: McpServer): void;
101
+ initTools(): Promise<void>;
102
+ destroy?(): Promise<void>;
103
+ }
@@ -0,0 +1,15 @@
1
+ interface PkgInfo {
2
+ name: string;
3
+ version: string;
4
+ dir: string;
5
+ }
6
+ export declare function getRunningPkgInfo(dir?: string): PkgInfo | null;
7
+ /**
8
+ * Find the nearest package.json file recursively
9
+ * @param {string} dir - Home directory
10
+ * @returns {string|null} - The most recent package.json file path or null
11
+ */
12
+ export declare function findNearestPackageJson(dir: string): string | null;
13
+ export declare function getElementInfosScriptContent(): string;
14
+ export declare function getExtraReturnLogic(tree?: boolean): Promise<string | null>;
15
+ export {};
@@ -0,0 +1,2 @@
1
+ export * from './fs';
2
+ export * from './port';
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Check if a port is available
3
+ */
4
+ export declare function isPortAvailable(port: number): Promise<boolean>;
5
+ /**
6
+ * Find an available port starting from the given port
7
+ */
8
+ export declare function findAvailablePort(startPort: number, maxAttempts?: number): Promise<number>;
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Browser polyfill for Node.js async_hooks module
3
+ * Provides empty implementation for browser environments where async_hooks is not available
4
+ */
5
+ declare const AsyncLocalStorage: {};
6
+ export { AsyncLocalStorage };
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Polyfills for Node.js modules in browser environments
3
+ */
4
+ export * from './async-hooks';
@@ -0,0 +1,34 @@
1
+ import type { NodeType } from '../constants';
2
+ import type { ElementInfo } from '../extractor';
3
+ export interface Point {
4
+ left: number;
5
+ top: number;
6
+ }
7
+ export interface Size {
8
+ width: number;
9
+ height: number;
10
+ }
11
+ export type Rect = Point & Size;
12
+ export declare abstract class BaseElement {
13
+ abstract id: string;
14
+ abstract attributes: {
15
+ nodeType: NodeType;
16
+ [key: string]: string;
17
+ };
18
+ abstract content: string;
19
+ abstract rect: Rect;
20
+ abstract center: [number, number];
21
+ abstract isVisible: boolean;
22
+ }
23
+ export interface ElementTreeNode<ElementType extends BaseElement = BaseElement> {
24
+ node: ElementType | null;
25
+ children: ElementTreeNode<ElementType>[];
26
+ }
27
+ export interface WebElementInfo extends ElementInfo {
28
+ zoom: number;
29
+ }
30
+ export type LocateResultElement = {
31
+ description: string;
32
+ center: [number, number];
33
+ rect: Rect;
34
+ };
@@ -0,0 +1,32 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2017 Google Inc.
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+ /**
7
+ * @internal
8
+ */
9
+ export interface KeyDefinition {
10
+ keyCode?: number;
11
+ shiftKeyCode?: number;
12
+ key?: string;
13
+ shiftKey?: string;
14
+ code?: string;
15
+ text?: string;
16
+ shiftText?: string;
17
+ location?: number;
18
+ }
19
+ /**
20
+ * All the valid keys that can be passed to functions that take user input, such
21
+ * as {@link Keyboard.press | keyboard.press }
22
+ *
23
+ * @public
24
+ */
25
+ export type KeyInput = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' | 'Power' | 'Eject' | 'Abort' | 'Help' | 'Backspace' | 'Tab' | 'Numpad5' | 'NumpadEnter' | 'Enter' | '\r' | '\n' | 'ShiftLeft' | 'ShiftRight' | 'ControlLeft' | 'ControlRight' | 'AltLeft' | 'AltRight' | 'Pause' | 'CapsLock' | 'Escape' | 'Convert' | 'NonConvert' | 'Space' | 'Numpad9' | 'PageUp' | 'Numpad3' | 'PageDown' | 'End' | 'Numpad1' | 'Home' | 'Numpad7' | 'ArrowLeft' | 'Numpad4' | 'Numpad8' | 'ArrowUp' | 'ArrowRight' | 'Numpad6' | 'Numpad2' | 'ArrowDown' | 'Select' | 'Open' | 'PrintScreen' | 'Insert' | 'Numpad0' | 'Delete' | 'NumpadDecimal' | 'Digit0' | 'Digit1' | 'Digit2' | 'Digit3' | 'Digit4' | 'Digit5' | 'Digit6' | 'Digit7' | 'Digit8' | 'Digit9' | 'KeyA' | 'KeyB' | 'KeyC' | 'KeyD' | 'KeyE' | 'KeyF' | 'KeyG' | 'KeyH' | 'KeyI' | 'KeyJ' | 'KeyK' | 'KeyL' | 'KeyM' | 'KeyN' | 'KeyO' | 'KeyP' | 'KeyQ' | 'KeyR' | 'KeyS' | 'KeyT' | 'KeyU' | 'KeyV' | 'KeyW' | 'KeyX' | 'KeyY' | 'KeyZ' | 'MetaLeft' | 'MetaRight' | 'ContextMenu' | 'NumpadMultiply' | 'NumpadAdd' | 'NumpadSubtract' | 'NumpadDivide' | 'F1' | 'F2' | 'F3' | 'F4' | 'F5' | 'F6' | 'F7' | 'F8' | 'F9' | 'F10' | 'F11' | 'F12' | 'F13' | 'F14' | 'F15' | 'F16' | 'F17' | 'F18' | 'F19' | 'F20' | 'F21' | 'F22' | 'F23' | 'F24' | 'NumLock' | 'ScrollLock' | 'AudioVolumeMute' | 'AudioVolumeDown' | 'AudioVolumeUp' | 'MediaTrackNext' | 'MediaTrackPrevious' | 'MediaStop' | 'MediaPlayPause' | 'Semicolon' | 'Equal' | 'NumpadEqual' | 'Comma' | 'Minus' | 'Period' | 'Slash' | 'Backquote' | 'BracketLeft' | 'Backslash' | 'BracketRight' | 'Quote' | 'AltGraph' | 'Props' | 'Cancel' | 'Clear' | 'Shift' | 'Control' | 'Alt' | 'Accept' | 'ModeChange' | ' ' | 'Print' | 'Execute' | '\u0000' | 'a' | 'b' | 'c' | 'd' | 'e' | 'f' | 'g' | 'h' | 'i' | 'j' | 'k' | 'l' | 'm' | 'n' | 'o' | 'p' | 'q' | 'r' | 's' | 't' | 'u' | 'v' | 'w' | 'x' | 'y' | 'z' | 'Meta' | '*' | '+' | '-' | '/' | ';' | '=' | ',' | '.' | '`' | '[' | '\\' | ']' | "'" | 'Attn' | 'CrSel' | 'ExSel' | 'EraseEof' | 'Play' | 'ZoomOut' | ')' | '!' | '@' | '#' | '$' | '%' | '^' | '&' | '(' | 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'G' | 'H' | 'I' | 'J' | 'K' | 'L' | 'M' | 'N' | 'O' | 'P' | 'Q' | 'R' | 'S' | 'T' | 'U' | 'V' | 'W' | 'X' | 'Y' | 'Z' | ':' | '<' | '_' | '>' | '?' | '~' | '{' | '|' | '}' | '"' | 'SoftLeft' | 'SoftRight' | 'Camera' | 'Call' | 'EndCall' | 'VolumeDown' | 'VolumeUp';
26
+ /**
27
+ * @internal
28
+ */
29
+ export declare const _keyDefinitions: Readonly<Record<KeyInput, KeyDefinition>>;
30
+ export declare const getKeyDefinition: (key: string) => KeyInput;
31
+ export declare const isMac: boolean;
32
+ export declare function transformHotkeyInput(keyInput: string): string[];
@@ -0,0 +1,37 @@
1
+ export declare const ifInBrowser: boolean;
2
+ export declare const ifInWorker: boolean;
3
+ export declare const ifInNode: string | false;
4
+ export declare function uuid(): string;
5
+ export declare function generateHashId(rect: any, content?: string): string;
6
+ /**
7
+ * A utility function that asserts a condition and throws an error with a message if the condition is false.
8
+ *
9
+ * @param condition - The condition to assert
10
+ * @param message - The error message to throw if the condition is false
11
+ * @throws Error with the provided message if the condition is false
12
+ */
13
+ export declare function assert(condition: any, message?: string): asserts condition;
14
+ type GlobalScope = typeof window | typeof globalThis | typeof self | undefined;
15
+ export declare function getGlobalScope(): GlobalScope;
16
+ export declare function setIsMcp(value: boolean): void;
17
+ export declare function logMsg(...message: Parameters<typeof console.log>): void;
18
+ export declare function repeat(times: number, fn: (index: number) => Promise<void>): Promise<void>;
19
+ export declare const escapeScriptTag: (html: string) => string;
20
+ export declare const antiEscapeScriptTag: (html: string) => string;
21
+ export declare function replaceIllegalPathCharsAndSpace(str: string): string;
22
+ /**
23
+ * Check if a value is a plain object (not null, not array, not other types)
24
+ */
25
+ export declare function isPlainObject(value: unknown): value is Record<string, any>;
26
+ /**
27
+ * Normalize string for comparison: lowercase and remove spaces, dashes, and underscores.
28
+ * Useful for flexible app name matching.
29
+ */
30
+ export declare function normalizeForComparison(str: string): string;
31
+ /**
32
+ * Merge and normalize app name mappings.
33
+ * Keys are normalized (lowercase, no spaces/dashes/underscores) for flexible matching.
34
+ * User-provided mapping takes precedence over default mapping.
35
+ */
36
+ export declare function mergeAndNormalizeAppNameMapping(defaultMapping: Record<string, string>, userMapping?: Record<string, string>): Record<string, string>;
37
+ export {};
@@ -0,0 +1,23 @@
1
+ import type { z } from 'zod';
2
+ /**
3
+ * Recursively unwrap optional, nullable, default, and effects wrapper types
4
+ * to get the actual inner Zod type
5
+ */
6
+ export declare function unwrapZodField(field: unknown): unknown;
7
+ /**
8
+ * Check if a field is a Midscene locator field
9
+ * Checks for either:
10
+ * 1. midscene_location_field_flag in shape (result schema)
11
+ * 2. prompt field in shape (input schema)
12
+ */
13
+ export declare function isMidsceneLocatorField(field: unknown): boolean;
14
+ /**
15
+ * Get type name string from a Zod schema field
16
+ * @param field - Zod schema field
17
+ * @param locatorTypeDescription - Optional description for MidsceneLocation fields (used by core)
18
+ */
19
+ export declare function getZodTypeName(field: unknown, locatorTypeDescription?: string): string;
20
+ /**
21
+ * Get description from a Zod schema field
22
+ */
23
+ export declare function getZodDescription(field: z.ZodTypeAny): string | null;
package/package.json ADDED
@@ -0,0 +1,121 @@
1
+ {
2
+ "name": "@aiscene/shared",
3
+ "version": "1.6.0",
4
+ "repository": "https://github.com/web-infra-dev/midscene",
5
+ "homepage": "https://midscenejs.com/",
6
+ "types": "./dist/types/index.d.ts",
7
+ "main": "./dist/lib/index.js",
8
+ "module": "./dist/es/index.mjs",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/types/index.d.ts",
12
+ "import": "./dist/es/index.mjs",
13
+ "require": "./dist/lib/index.js"
14
+ },
15
+ "./constants": {
16
+ "types": "./dist/types/constants/index.d.ts",
17
+ "import": "./dist/es/constants/index.mjs",
18
+ "require": "./dist/lib/constants/index.js"
19
+ },
20
+ "./extractor": {
21
+ "types": "./dist/types/extractor/index.d.ts",
22
+ "import": "./dist/es/extractor/index.mjs",
23
+ "require": "./dist/lib/extractor/index.js"
24
+ },
25
+ "./img": {
26
+ "types": "./dist/types/img/index.d.ts",
27
+ "import": "./dist/es/img/index.mjs",
28
+ "require": "./dist/lib/img/index.js"
29
+ },
30
+ "./node": {
31
+ "types": "./dist/types/node/index.d.ts",
32
+ "import": "./dist/es/node/index.mjs",
33
+ "require": "./dist/lib/node/index.js"
34
+ },
35
+ "./types": {
36
+ "types": "./dist/types/types/index.d.ts",
37
+ "import": "./dist/es/types/index.mjs",
38
+ "require": "./dist/lib/types/index.js"
39
+ },
40
+ "./env": {
41
+ "types": "./dist/types/env/index.d.ts",
42
+ "import": "./dist/es/env/index.mjs",
43
+ "require": "./dist/lib/env/index.js"
44
+ },
45
+ "./polyfills": {
46
+ "types": "./dist/types/polyfills/index.d.ts",
47
+ "import": "./dist/es/polyfills/index.mjs",
48
+ "require": "./dist/lib/polyfills/index.js"
49
+ },
50
+ "./utils": {
51
+ "types": "./dist/types/utils.d.ts",
52
+ "import": "./dist/es/utils.mjs",
53
+ "require": "./dist/lib/utils.js"
54
+ },
55
+ "./common": {
56
+ "types": "./dist/types/common.d.ts",
57
+ "import": "./dist/es/common.mjs",
58
+ "require": "./dist/lib/common.js"
59
+ },
60
+ "./mcp": {
61
+ "types": "./dist/types/mcp/index.d.ts",
62
+ "import": "./dist/es/mcp/index.mjs",
63
+ "require": "./dist/lib/mcp/index.js"
64
+ },
65
+ "./cli": {
66
+ "types": "./dist/types/cli/index.d.ts",
67
+ "import": "./dist/es/cli/index.mjs",
68
+ "require": "./dist/lib/cli/index.js"
69
+ },
70
+ "./logger": {
71
+ "types": "./dist/types/logger.d.ts",
72
+ "import": "./dist/es/logger.mjs",
73
+ "require": "./dist/lib/logger.js"
74
+ },
75
+ "./*": {
76
+ "types": "./dist/types/*.d.ts",
77
+ "import": "./dist/es/*.mjs",
78
+ "require": "./dist/lib/*.js"
79
+ }
80
+ },
81
+ "files": ["dist", "src", "README.md"],
82
+ "scripts": {
83
+ "dev": "npm run build:watch",
84
+ "build": "npm run build:script && npm run build:pkg",
85
+ "build:pkg": "rslib build",
86
+ "build:script": "rslib build -c ./rslib.inspect.config.ts",
87
+ "build:watch": "npm run build:script && rslib build --watch --no-clean",
88
+ "reset": "rimraf ./**/node_modules",
89
+ "test": "vitest --run",
90
+ "test:u": "vitest --run -u"
91
+ },
92
+ "dependencies": {
93
+ "@modelcontextprotocol/sdk": "1.10.2",
94
+ "@silvia-odwyer/photon": "0.3.3",
95
+ "@silvia-odwyer/photon-node": "0.3.3",
96
+ "debug": "4.4.0",
97
+ "express": "^4.21.2",
98
+ "js-sha256": "0.11.0",
99
+ "sharp": "^0.34.3",
100
+ "dotenv": "^16.4.5",
101
+ "uuid": "11.1.0",
102
+ "zod": "3.24.3"
103
+ },
104
+ "devDependencies": {
105
+ "@rslib/core": "^0.18.3",
106
+ "@types/debug": "4.1.12",
107
+ "@types/express": "^4.17.21",
108
+ "@types/node": "^18.0.0",
109
+ "@ui-tars/shared": "1.2.0",
110
+ "openai": "6.3.0",
111
+ "rimraf": "~3.0.2",
112
+ "typescript": "^5.8.3",
113
+ "vitest": "3.0.5"
114
+ },
115
+ "sideEffects": [],
116
+ "publishConfig": {
117
+ "access": "public",
118
+ "registry": "https://registry.npmjs.org/"
119
+ },
120
+ "license": "MIT"
121
+ }
package/src/baseDB.ts ADDED
@@ -0,0 +1,158 @@
1
+ // Generic database operations class
2
+ export class IndexedDBManager {
3
+ private dbPromise: Promise<IDBDatabase>;
4
+ private dbName: string;
5
+ private version: number;
6
+ private storeConfigs: Array<{ name: string; keyPath: string }>;
7
+
8
+ constructor(
9
+ dbName: string,
10
+ version: number,
11
+ storeConfigs: Array<{ name: string; keyPath: string }>,
12
+ ) {
13
+ this.dbName = dbName;
14
+ this.version = version;
15
+ this.storeConfigs = storeConfigs;
16
+ this.dbPromise = this.initDB();
17
+ }
18
+
19
+ private initDB(): Promise<IDBDatabase> {
20
+ return new Promise((resolve, reject) => {
21
+ const request = indexedDB.open(this.dbName, this.version);
22
+
23
+ request.onerror = () => reject(request.error);
24
+ request.onsuccess = () => resolve(request.result);
25
+
26
+ request.onupgradeneeded = (event) => {
27
+ const db = (event.target as IDBOpenDBRequest).result;
28
+
29
+ // Create stores if they don't exist
30
+ this.storeConfigs.forEach(({ name, keyPath }) => {
31
+ if (!db.objectStoreNames.contains(name)) {
32
+ const store = db.createObjectStore(name, { keyPath });
33
+ store.createIndex('timestamp', 'timestamp', { unique: false });
34
+ }
35
+ });
36
+ };
37
+ });
38
+ }
39
+
40
+ private async withTransaction<T>(
41
+ storeNames: string | string[],
42
+ mode: IDBTransactionMode,
43
+ operation: (stores: IDBObjectStore | IDBObjectStore[]) => Promise<T>,
44
+ ): Promise<T> {
45
+ const db = await this.dbPromise;
46
+ const transaction = db.transaction(storeNames, mode);
47
+
48
+ const stores = Array.isArray(storeNames)
49
+ ? storeNames.map((name) => transaction.objectStore(name))
50
+ : transaction.objectStore(storeNames);
51
+
52
+ return operation(stores);
53
+ }
54
+
55
+ private promisifyRequest<T>(request: IDBRequest<T>): Promise<T> {
56
+ return new Promise((resolve, reject) => {
57
+ request.onsuccess = () => resolve(request.result);
58
+ request.onerror = () => reject(request.error);
59
+ });
60
+ }
61
+
62
+ async put<T>(storeName: string, data: T): Promise<void> {
63
+ await this.withTransaction(storeName, 'readwrite', async (store) => {
64
+ await this.promisifyRequest((store as IDBObjectStore).put(data));
65
+ });
66
+ }
67
+
68
+ async get<T>(storeName: string, key: string): Promise<T | undefined> {
69
+ return this.withTransaction(storeName, 'readonly', async (store) => {
70
+ return this.promisifyRequest((store as IDBObjectStore).get(key));
71
+ });
72
+ }
73
+
74
+ async getAll<T>(storeName: string, sortByTimestamp = true): Promise<T[]> {
75
+ return this.withTransaction(storeName, 'readonly', async (store) => {
76
+ const objectStore = store as IDBObjectStore;
77
+ const results = sortByTimestamp
78
+ ? await this.promisifyRequest(objectStore.index('timestamp').getAll())
79
+ : await this.promisifyRequest(objectStore.getAll());
80
+
81
+ return sortByTimestamp
82
+ ? results.sort((a: any, b: any) => a.timestamp - b.timestamp)
83
+ : results;
84
+ });
85
+ }
86
+
87
+ async clear(storeName: string): Promise<void> {
88
+ await this.withTransaction(storeName, 'readwrite', async (store) => {
89
+ await this.promisifyRequest((store as IDBObjectStore).clear());
90
+ });
91
+ }
92
+
93
+ async delete(storeName: string, key: string): Promise<void> {
94
+ await this.withTransaction(storeName, 'readwrite', async (store) => {
95
+ await this.promisifyRequest((store as IDBObjectStore).delete(key));
96
+ });
97
+ }
98
+
99
+ async count(storeName: string): Promise<number> {
100
+ return this.withTransaction(storeName, 'readonly', async (store) => {
101
+ return this.promisifyRequest((store as IDBObjectStore).count());
102
+ });
103
+ }
104
+
105
+ getDBPromise(): Promise<IDBDatabase> {
106
+ return this.dbPromise;
107
+ }
108
+ }
109
+
110
+ // Generic error handler wrapper
111
+ export const withErrorHandling = async <T>(
112
+ operation: () => Promise<T>,
113
+ errorMessage: string,
114
+ defaultValue?: T,
115
+ onQuotaExceeded?: () => Promise<void>,
116
+ ): Promise<T | undefined> => {
117
+ try {
118
+ return await operation();
119
+ } catch (e) {
120
+ console.error(errorMessage, e);
121
+ if (
122
+ e instanceof Error &&
123
+ e.name === 'QuotaExceededError' &&
124
+ onQuotaExceeded
125
+ ) {
126
+ console.log('Storage quota exceeded, running cleanup...');
127
+ await onQuotaExceeded();
128
+ }
129
+ return defaultValue;
130
+ }
131
+ };
132
+
133
+ // Base cleanup function for managing storage space
134
+ export const createCleanupFunction = <
135
+ T extends { id: string; timestamp: number },
136
+ >(
137
+ dbManager: IndexedDBManager,
138
+ storeName: string,
139
+ maxItems: number,
140
+ ) => {
141
+ return async (): Promise<void> => {
142
+ try {
143
+ const results = await dbManager.getAll<T>(storeName);
144
+
145
+ if (results.length > maxItems) {
146
+ const toDelete = results
147
+ .sort((a, b) => a.timestamp - b.timestamp)
148
+ .slice(0, results.length - maxItems);
149
+
150
+ await Promise.all(
151
+ toDelete.map((item) => dbManager.delete(storeName, item.id)),
152
+ );
153
+ }
154
+ } catch (e) {
155
+ console.error(`Failed to cleanup ${storeName}:`, e);
156
+ }
157
+ };
158
+ };
@@ -0,0 +1,68 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+
4
+ export interface CopyStaticOptions {
5
+ /** Source directory to copy from */
6
+ srcDir: string;
7
+ /** Destination directory to copy to */
8
+ destDir: string;
9
+ /** Optional favicon source path (relative to directory containing srcDir) */
10
+ faviconPath?: string;
11
+ /** Name for the rsbuild plugin */
12
+ pluginName?: string;
13
+ }
14
+
15
+ /**
16
+ * Creates an rsbuild plugin that copies static files after build
17
+ * @param options Configuration options for copying static files
18
+ * @returns Rsbuild plugin object
19
+ */
20
+ export const createCopyStaticPlugin = (options: CopyStaticOptions) => ({
21
+ name: options.pluginName || 'copy-static',
22
+ setup(api: any) {
23
+ api.onAfterBuild(async () => {
24
+ const { srcDir, destDir, faviconPath } = options;
25
+
26
+ // Remove symlink left by dev mode before copying
27
+ const stat = await fs.promises.lstat(destDir).catch(() => null);
28
+ if (stat?.isSymbolicLink()) {
29
+ await fs.promises.unlink(destDir);
30
+ }
31
+
32
+ await fs.promises.mkdir(destDir, { recursive: true });
33
+
34
+ // Copy directory contents recursively
35
+ await fs.promises.cp(srcDir, destDir, { recursive: true });
36
+ console.log(`Copied build artifacts from ${srcDir} to ${destDir}`);
37
+
38
+ // Copy favicon if specified
39
+ if (faviconPath) {
40
+ const faviconDest = path.join(destDir, 'favicon.ico');
41
+ await fs.promises.copyFile(faviconPath, faviconDest);
42
+ console.log(`Copied favicon from ${faviconPath} to ${faviconDest}`);
43
+ }
44
+ });
45
+ },
46
+ });
47
+
48
+ /**
49
+ * Helper function to create a copy static plugin for playground builds
50
+ * @param srcDir Source directory (usually dist directory)
51
+ * @param destDir Destination directory
52
+ * @param pluginName Optional plugin name
53
+ * @param faviconSrc Optional favicon source path
54
+ * @returns Rsbuild plugin
55
+ */
56
+ export const createPlaygroundCopyPlugin = (
57
+ srcDir: string,
58
+ destDir: string,
59
+ pluginName?: string,
60
+ faviconSrc?: string,
61
+ ) => {
62
+ return createCopyStaticPlugin({
63
+ srcDir,
64
+ destDir,
65
+ faviconPath: faviconSrc,
66
+ pluginName,
67
+ });
68
+ };
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Common Rspack configuration helpers for rsbuild projects
3
+ */
4
+
5
+ /**
6
+ * Common warning patterns to ignore in Rspack builds.
7
+ * These warnings are typically from optional dependencies or known non-critical issues.
8
+ */
9
+ export const commonIgnoreWarnings = [
10
+ // Ignore dynamic import warnings from langsmith/langfuse optional dependencies
11
+ /Critical dependency: the request of a dependency is an expression/,
12
+ ];