@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,98 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.n = (module)=>{
5
+ var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
6
+ __webpack_require__.d(getter, {
7
+ a: getter
8
+ });
9
+ return getter;
10
+ };
11
+ })();
12
+ (()=>{
13
+ __webpack_require__.d = (exports1, definition)=>{
14
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
15
+ enumerable: true,
16
+ get: definition[key]
17
+ });
18
+ };
19
+ })();
20
+ (()=>{
21
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
22
+ })();
23
+ (()=>{
24
+ __webpack_require__.r = (exports1)=>{
25
+ if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
26
+ value: 'Module'
27
+ });
28
+ Object.defineProperty(exports1, '__esModule', {
29
+ value: true
30
+ });
31
+ };
32
+ })();
33
+ var __webpack_exports__ = {};
34
+ __webpack_require__.r(__webpack_exports__);
35
+ __webpack_require__.d(__webpack_exports__, {
36
+ injectReportHtmlFromCore: ()=>injectReportHtmlFromCore
37
+ });
38
+ const external_node_fs_namespaceObject = require("node:fs");
39
+ var external_node_fs_default = /*#__PURE__*/ __webpack_require__.n(external_node_fs_namespaceObject);
40
+ const external_node_path_namespaceObject = require("node:path");
41
+ var external_node_path_default = /*#__PURE__*/ __webpack_require__.n(external_node_path_namespaceObject);
42
+ const MAGIC_STRING = 'REPLACE_ME_WITH_REPORT_HTML';
43
+ const REPLACED_MARK = '/*REPORT_HTML_REPLACED*/';
44
+ const REG_EXP_FOR_REPLACE = /\/\*REPORT_HTML_REPLACED\*\/.*/;
45
+ function injectReportHtmlFromCore(packageDir) {
46
+ return {
47
+ name: 'inject-report-html-from-core',
48
+ setup (api) {
49
+ api.onAfterBuild(()=>{
50
+ const coreUtilsPath = external_node_path_default().resolve(packageDir, '..', 'core', 'dist', 'lib', 'utils.js');
51
+ if (!external_node_fs_default().existsSync(coreUtilsPath)) return void console.warn('[inject-report-html] @midscene/core dist not found, skipping');
52
+ const coreContent = external_node_fs_default().readFileSync(coreUtilsPath, 'utf-8');
53
+ if (!coreContent.includes(REPLACED_MARK)) return void console.warn('[inject-report-html] HTML not found in core dist. Ensure report builds first.');
54
+ const markerIndex = coreContent.indexOf(REPLACED_MARK);
55
+ const jsonStart = markerIndex + REPLACED_MARK.length;
56
+ let jsonEnd = jsonStart;
57
+ if ('"' === coreContent[jsonStart]) {
58
+ jsonEnd = jsonStart + 1;
59
+ while(jsonEnd < coreContent.length)if ('\\' === coreContent[jsonEnd]) jsonEnd += 2;
60
+ else if ('"' === coreContent[jsonEnd]) {
61
+ jsonEnd += 1;
62
+ break;
63
+ } else jsonEnd += 1;
64
+ }
65
+ const jsonString = coreContent.slice(jsonStart, jsonEnd);
66
+ if (!jsonString || jsonString.length < 10) return void console.warn('[inject-report-html] Failed to extract HTML from core');
67
+ const finalContent = `${REPLACED_MARK}${jsonString}`;
68
+ const distDir = external_node_path_default().join(packageDir, 'dist');
69
+ if (!external_node_fs_default().existsSync(distDir)) return;
70
+ const jsFiles = external_node_fs_default().readdirSync(distDir).filter((f)=>f.endsWith('.js'));
71
+ let injectedCount = 0;
72
+ for (const file of jsFiles){
73
+ const filePath = external_node_path_default().join(distDir, file);
74
+ const content = external_node_fs_default().readFileSync(filePath, 'utf-8');
75
+ if (content.includes(REPLACED_MARK)) {
76
+ if (REG_EXP_FOR_REPLACE.test(content)) {
77
+ external_node_fs_default().writeFileSync(filePath, content.replace(REG_EXP_FOR_REPLACE, ()=>finalContent));
78
+ console.log(`[inject-report-html] Updated: ${file}`);
79
+ injectedCount++;
80
+ }
81
+ } else if (content.includes(`'${MAGIC_STRING}'`)) {
82
+ external_node_fs_default().writeFileSync(filePath, content.replace(`'${MAGIC_STRING}'`, ()=>finalContent));
83
+ console.log(`[inject-report-html] Injected: ${file}`);
84
+ injectedCount++;
85
+ }
86
+ }
87
+ if (injectedCount > 0) console.log(`[inject-report-html] Completed: ${injectedCount} file(s)`);
88
+ });
89
+ }
90
+ };
91
+ }
92
+ exports.injectReportHtmlFromCore = __webpack_exports__.injectReportHtmlFromCore;
93
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
94
+ "injectReportHtmlFromCore"
95
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
96
+ Object.defineProperty(exports, '__esModule', {
97
+ value: true
98
+ });
@@ -0,0 +1,86 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.d = (exports1, definition)=>{
5
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
6
+ enumerable: true,
7
+ get: definition[key]
8
+ });
9
+ };
10
+ })();
11
+ (()=>{
12
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
13
+ })();
14
+ (()=>{
15
+ __webpack_require__.r = (exports1)=>{
16
+ if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
17
+ value: 'Module'
18
+ });
19
+ Object.defineProperty(exports1, '__esModule', {
20
+ value: true
21
+ });
22
+ };
23
+ })();
24
+ var __webpack_exports__ = {};
25
+ __webpack_require__.r(__webpack_exports__);
26
+ __webpack_require__.d(__webpack_exports__, {
27
+ createMCPServerLauncher: ()=>createMCPServerLauncher
28
+ });
29
+ function createMCPServerLauncher(config) {
30
+ const { agent, platformName, ToolsManagerClass, MCPServerClass } = config;
31
+ function validateAgent() {
32
+ const device = agent.interface;
33
+ if (!device) throw new Error(`Agent must have an 'interface' property that references the underlying device.
34
+ Please ensure your agent instance is properly initialized with a device interface.
35
+ Expected: agent.interface to be defined, but got: ${typeof device}
36
+ Solution: Check that your agent constructor properly sets the interface property.`);
37
+ }
38
+ function createToolsManager() {
39
+ const toolsManager = new ToolsManagerClass();
40
+ toolsManager.agent = agent;
41
+ return toolsManager;
42
+ }
43
+ function logStartupInfo(mode, additionalInfo) {
44
+ const device = agent.interface;
45
+ console.log(`Starting Midscene ${platformName} MCP Server (${mode})...`);
46
+ console.log(`Agent: ${agent.constructor.name}`);
47
+ console.log(`Device: ${device.constructor.name}`);
48
+ if (additionalInfo?.port !== void 0) console.log(`Port: ${additionalInfo.port}`);
49
+ if (additionalInfo?.host) console.log(`Host: ${additionalInfo.host}`);
50
+ }
51
+ return {
52
+ async launch (options = {}) {
53
+ const { verbose = true } = options;
54
+ validateAgent();
55
+ if (verbose) logStartupInfo('stdio');
56
+ const toolsManager = createToolsManager();
57
+ const server = new MCPServerClass(toolsManager);
58
+ const result = await server.launch();
59
+ if (verbose) console.log(`${platformName} MCP Server started (stdio mode)`);
60
+ return result;
61
+ },
62
+ async launchHttp (options) {
63
+ const { port, host = 'localhost', verbose = true } = options;
64
+ validateAgent();
65
+ if (verbose) logStartupInfo('HTTP', {
66
+ port,
67
+ host
68
+ });
69
+ const toolsManager = createToolsManager();
70
+ const server = new MCPServerClass(toolsManager);
71
+ const result = await server.launchHttp({
72
+ port,
73
+ host
74
+ });
75
+ if (verbose) console.log(`${platformName} MCP Server started on http://${result.host}:${result.port}/mcp`);
76
+ return result;
77
+ }
78
+ };
79
+ }
80
+ exports.createMCPServerLauncher = __webpack_exports__.createMCPServerLauncher;
81
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
82
+ "createMCPServerLauncher"
83
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
84
+ Object.defineProperty(exports, '__esModule', {
85
+ value: true
86
+ });
@@ -0,0 +1,334 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.d = (exports1, definition)=>{
5
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
6
+ enumerable: true,
7
+ get: definition[key]
8
+ });
9
+ };
10
+ })();
11
+ (()=>{
12
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
13
+ })();
14
+ (()=>{
15
+ __webpack_require__.r = (exports1)=>{
16
+ if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
17
+ value: 'Module'
18
+ });
19
+ Object.defineProperty(exports1, '__esModule', {
20
+ value: true
21
+ });
22
+ };
23
+ })();
24
+ var __webpack_exports__ = {};
25
+ __webpack_require__.r(__webpack_exports__);
26
+ __webpack_require__.d(__webpack_exports__, {
27
+ generateToolsFromActionSpace: ()=>generateToolsFromActionSpace,
28
+ generateCommonTools: ()=>generateCommonTools
29
+ });
30
+ const index_js_namespaceObject = require("../img/index.js");
31
+ const external_zod_namespaceObject = require("zod");
32
+ const external_zod_schema_utils_js_namespaceObject = require("../zod-schema-utils.js");
33
+ function getErrorMessage(error) {
34
+ return error instanceof Error ? error.message : String(error);
35
+ }
36
+ function describeActionForMCP(action) {
37
+ const actionDesc = action.description || `Execute ${action.name} action`;
38
+ if (!action.paramSchema) return `${action.name} action, ${actionDesc}`;
39
+ const schema = action.paramSchema;
40
+ const isZodObjectType = schema._def?.typeName === 'ZodObject';
41
+ if (!isZodObjectType || !schema.shape) {
42
+ const typeName = (0, external_zod_schema_utils_js_namespaceObject.getZodTypeName)(schema);
43
+ const description = (0, external_zod_schema_utils_js_namespaceObject.getZodDescription)(schema);
44
+ const paramDesc = description ? `${typeName} - ${description}` : typeName;
45
+ return `${action.name} action, ${actionDesc}. Parameter: ${paramDesc}`;
46
+ }
47
+ const paramDescriptions = [];
48
+ for (const [key, field] of Object.entries(schema.shape))if (field && 'object' == typeof field) {
49
+ const isFieldOptional = 'function' == typeof field.isOptional && field.isOptional();
50
+ const typeName = (0, external_zod_schema_utils_js_namespaceObject.getZodTypeName)(field);
51
+ const description = (0, external_zod_schema_utils_js_namespaceObject.getZodDescription)(field);
52
+ let paramStr = `${key}${isFieldOptional ? '?' : ''} (${typeName})`;
53
+ if (description) paramStr += ` - ${description}`;
54
+ paramDescriptions.push(paramStr);
55
+ }
56
+ if (0 === paramDescriptions.length) return `${action.name} action, ${actionDesc}`;
57
+ return `${action.name} action, ${actionDesc}. Parameters: ${paramDescriptions.join('; ')}`;
58
+ }
59
+ function isZodOptional(value) {
60
+ return '_def' in value && value._def?.typeName === 'ZodOptional';
61
+ }
62
+ function isZodObject(value) {
63
+ return '_def' in value && value._def?.typeName === 'ZodObject' && 'shape' in value;
64
+ }
65
+ function unwrapOptional(value) {
66
+ if (isZodOptional(value)) return {
67
+ innerValue: value._def.innerType,
68
+ isOptional: true
69
+ };
70
+ return {
71
+ innerValue: value,
72
+ isOptional: false
73
+ };
74
+ }
75
+ function isLocateField(value) {
76
+ if (!isZodObject(value)) return false;
77
+ return 'prompt' in value.shape;
78
+ }
79
+ function makePromptOptional(value, wrapInOptional) {
80
+ const newShape = {
81
+ ...value.shape
82
+ };
83
+ newShape.prompt = value.shape.prompt.optional();
84
+ let newSchema = external_zod_namespaceObject.z.object(newShape).passthrough();
85
+ if (wrapInOptional) newSchema = newSchema.optional();
86
+ return newSchema;
87
+ }
88
+ function transformSchemaField(key, value) {
89
+ const { innerValue, isOptional } = unwrapOptional(value);
90
+ if (isZodObject(innerValue) && isLocateField(innerValue)) return [
91
+ key,
92
+ makePromptOptional(innerValue, isOptional)
93
+ ];
94
+ return [
95
+ key,
96
+ value
97
+ ];
98
+ }
99
+ function extractActionSchema(paramSchema) {
100
+ if (!paramSchema) return {};
101
+ const schema = paramSchema;
102
+ if (!isZodObject(schema)) return schema;
103
+ return Object.fromEntries(Object.entries(schema.shape).map(([key, value])=>transformSchemaField(key, value)));
104
+ }
105
+ function serializeArgsToDescription(args) {
106
+ try {
107
+ return Object.entries(args).map(([key, value])=>{
108
+ if ('object' == typeof value && null !== value) try {
109
+ return `${key}: ${JSON.stringify(value)}`;
110
+ } catch {
111
+ return `${key}: [object]`;
112
+ }
113
+ return `${key}: "${value}"`;
114
+ }).join(', ');
115
+ } catch (error) {
116
+ const errorMessage = getErrorMessage(error);
117
+ console.error('Error serializing args:', errorMessage);
118
+ return `[args serialization failed: ${errorMessage}]`;
119
+ }
120
+ }
121
+ function buildActionInstruction(actionName, args) {
122
+ const locatePrompt = args.locate && 'object' == typeof args.locate ? args.locate.prompt : void 0;
123
+ switch(actionName){
124
+ case 'Tap':
125
+ return locatePrompt ? `Tap on "${locatePrompt}"` : 'Tap';
126
+ case 'Input':
127
+ {
128
+ const value = args.value ?? args.content ?? '';
129
+ return locatePrompt ? `Input "${value}" into "${locatePrompt}"` : `Input "${value}"`;
130
+ }
131
+ case 'Scroll':
132
+ {
133
+ const direction = args.direction ?? 'down';
134
+ return locatePrompt ? `Scroll ${direction} on "${locatePrompt}"` : `Scroll ${direction}`;
135
+ }
136
+ case 'Hover':
137
+ return locatePrompt ? `Hover over "${locatePrompt}"` : 'Hover';
138
+ case 'KeyboardPress':
139
+ {
140
+ const key = args.value ?? args.key ?? '';
141
+ return `Press key "${key}"`;
142
+ }
143
+ default:
144
+ {
145
+ const argsDescription = serializeArgsToDescription(args);
146
+ return argsDescription ? `${actionName}: ${argsDescription}` : actionName;
147
+ }
148
+ }
149
+ }
150
+ async function captureScreenshotResult(agent, actionName) {
151
+ try {
152
+ const screenshot = await agent.page?.screenshotBase64();
153
+ if (!screenshot) return {
154
+ content: [
155
+ {
156
+ type: 'text',
157
+ text: `Action "${actionName}" completed.`
158
+ }
159
+ ]
160
+ };
161
+ const { mimeType, body } = (0, index_js_namespaceObject.parseBase64)(screenshot);
162
+ return {
163
+ content: [
164
+ {
165
+ type: 'text',
166
+ text: `Action "${actionName}" completed.`
167
+ },
168
+ {
169
+ type: 'image',
170
+ data: body,
171
+ mimeType
172
+ }
173
+ ]
174
+ };
175
+ } catch (error) {
176
+ const errorMessage = getErrorMessage(error);
177
+ console.error('Error capturing screenshot:', errorMessage);
178
+ return {
179
+ content: [
180
+ {
181
+ type: 'text',
182
+ text: `Action "${actionName}" completed (screenshot unavailable: ${errorMessage})`
183
+ }
184
+ ]
185
+ };
186
+ }
187
+ }
188
+ function createErrorResult(message) {
189
+ return {
190
+ content: [
191
+ {
192
+ type: 'text',
193
+ text: message
194
+ }
195
+ ],
196
+ isError: true
197
+ };
198
+ }
199
+ async function captureFailureResult(agent, actionName, errorMessage) {
200
+ const warningText = `Warning: Action "${actionName}" failed: ${errorMessage}. Check the screenshot below for the current page state and decide how to proceed.`;
201
+ try {
202
+ const screenshot = await agent.page?.screenshotBase64();
203
+ if (!screenshot) return {
204
+ content: [
205
+ {
206
+ type: 'text',
207
+ text: warningText
208
+ }
209
+ ]
210
+ };
211
+ const { mimeType, body } = (0, index_js_namespaceObject.parseBase64)(screenshot);
212
+ return {
213
+ content: [
214
+ {
215
+ type: 'text',
216
+ text: warningText
217
+ },
218
+ {
219
+ type: 'image',
220
+ data: body,
221
+ mimeType
222
+ }
223
+ ]
224
+ };
225
+ } catch {
226
+ return {
227
+ content: [
228
+ {
229
+ type: 'text',
230
+ text: warningText
231
+ }
232
+ ]
233
+ };
234
+ }
235
+ }
236
+ function generateToolsFromActionSpace(actionSpace, getAgent) {
237
+ return actionSpace.map((action)=>{
238
+ const schema = extractActionSchema(action.paramSchema);
239
+ return {
240
+ name: action.name,
241
+ description: describeActionForMCP(action),
242
+ schema,
243
+ handler: async (args)=>{
244
+ try {
245
+ const agent = await getAgent();
246
+ if (agent.aiAction) {
247
+ const instruction = buildActionInstruction(action.name, args);
248
+ try {
249
+ await agent.aiAction(instruction);
250
+ } catch (error) {
251
+ const errorMessage = getErrorMessage(error);
252
+ console.error(`Error executing action "${action.name}":`, errorMessage);
253
+ return await captureFailureResult(agent, action.name, errorMessage);
254
+ }
255
+ }
256
+ return await captureScreenshotResult(agent, action.name);
257
+ } catch (error) {
258
+ const errorMessage = getErrorMessage(error);
259
+ console.error(`Error in handler for "${action.name}":`, errorMessage);
260
+ return createErrorResult(`Failed to get agent or execute action "${action.name}": ${errorMessage}`);
261
+ }
262
+ }
263
+ };
264
+ });
265
+ }
266
+ function generateCommonTools(getAgent) {
267
+ return [
268
+ {
269
+ name: 'take_screenshot',
270
+ description: 'Capture screenshot of current page/screen',
271
+ schema: {},
272
+ handler: async ()=>{
273
+ try {
274
+ const agent = await getAgent();
275
+ const screenshot = await agent.page?.screenshotBase64();
276
+ if (!screenshot) return createErrorResult('Screenshot not available');
277
+ const { mimeType, body } = (0, index_js_namespaceObject.parseBase64)(screenshot);
278
+ return {
279
+ content: [
280
+ {
281
+ type: 'image',
282
+ data: body,
283
+ mimeType
284
+ }
285
+ ]
286
+ };
287
+ } catch (error) {
288
+ const errorMessage = getErrorMessage(error);
289
+ console.error('Error taking screenshot:', errorMessage);
290
+ return createErrorResult(`Failed to capture screenshot: ${errorMessage}`);
291
+ }
292
+ }
293
+ },
294
+ {
295
+ name: 'act',
296
+ description: 'Execute a natural language action. The AI will plan and perform multi-step operations in a single invocation, useful for transient UI interactions (e.g., Spotlight, dropdown menus) that disappear between separate commands.',
297
+ schema: {
298
+ prompt: external_zod_namespaceObject.z.string().describe('Natural language description of the action to perform, e.g. "press Command+Space, type Safari, press Enter"')
299
+ },
300
+ handler: async (args)=>{
301
+ const prompt = args.prompt;
302
+ try {
303
+ const agent = await getAgent();
304
+ if (!agent.aiAction) return createErrorResult('act is not supported by this agent');
305
+ const result = await agent.aiAction(prompt, {
306
+ deepThink: false
307
+ });
308
+ const screenshotResult = await captureScreenshotResult(agent, 'act');
309
+ if (result) {
310
+ const message = 'string' == typeof result ? result : JSON.stringify(result);
311
+ screenshotResult.content.unshift({
312
+ type: 'text',
313
+ text: `Task finished, message: ${message}`
314
+ });
315
+ }
316
+ return screenshotResult;
317
+ } catch (error) {
318
+ const errorMessage = getErrorMessage(error);
319
+ console.error('Error executing act:', errorMessage);
320
+ return createErrorResult(`Failed to execute act: ${errorMessage}`);
321
+ }
322
+ }
323
+ }
324
+ ];
325
+ }
326
+ exports.generateCommonTools = __webpack_exports__.generateCommonTools;
327
+ exports.generateToolsFromActionSpace = __webpack_exports__.generateToolsFromActionSpace;
328
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
329
+ "generateCommonTools",
330
+ "generateToolsFromActionSpace"
331
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
332
+ Object.defineProperty(exports, '__esModule', {
333
+ value: true
334
+ });
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.d = (exports1, definition)=>{
5
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
6
+ enumerable: true,
7
+ get: definition[key]
8
+ });
9
+ };
10
+ })();
11
+ (()=>{
12
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
13
+ })();
14
+ (()=>{
15
+ __webpack_require__.r = (exports1)=>{
16
+ if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
17
+ value: 'Module'
18
+ });
19
+ Object.defineProperty(exports1, '__esModule', {
20
+ value: true
21
+ });
22
+ };
23
+ })();
24
+ var __webpack_exports__ = {};
25
+ __webpack_require__.r(__webpack_exports__);
26
+ __webpack_require__.d(__webpack_exports__, {
27
+ defaultAppLoadingCheckIntervalMs: ()=>defaultAppLoadingCheckIntervalMs,
28
+ defaultAppLoadingTimeoutMs: ()=>defaultAppLoadingTimeoutMs
29
+ });
30
+ const defaultAppLoadingTimeoutMs = 10000;
31
+ const defaultAppLoadingCheckIntervalMs = 2000;
32
+ exports.defaultAppLoadingCheckIntervalMs = __webpack_exports__.defaultAppLoadingCheckIntervalMs;
33
+ exports.defaultAppLoadingTimeoutMs = __webpack_exports__.defaultAppLoadingTimeoutMs;
34
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
35
+ "defaultAppLoadingCheckIntervalMs",
36
+ "defaultAppLoadingTimeoutMs"
37
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
38
+ Object.defineProperty(exports, '__esModule', {
39
+ value: true
40
+ });