@midscene/shared 0.30.10 → 1.0.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.
- package/dist/es/build/rspack-config.mjs +4 -0
- package/dist/es/constants/example-code.mjs +4 -4
- package/dist/es/env/constants.mjs +27 -82
- package/dist/es/env/global-config-manager.mjs +2 -3
- package/dist/es/env/helper.mjs +12 -17
- package/dist/es/env/init-debug.mjs +6 -6
- package/dist/es/env/model-config-manager.mjs +45 -65
- package/dist/es/env/parse-model-config.mjs +112 -0
- package/dist/es/env/types.mjs +70 -162
- package/dist/es/extractor/dom-util.mjs +10 -18
- package/dist/es/extractor/index.mjs +2 -3
- package/dist/es/extractor/locator.mjs +8 -15
- package/dist/es/extractor/tree.mjs +2 -5
- package/dist/es/extractor/util.mjs +4 -28
- package/dist/es/extractor/web-extractor.mjs +7 -14
- package/dist/es/index.mjs +2 -1
- package/dist/es/mcp/base-server.mjs +250 -0
- package/dist/es/mcp/base-tools.mjs +84 -0
- package/dist/es/mcp/index.mjs +5 -0
- package/dist/es/mcp/inject-report-html-plugin.mjs +53 -0
- package/dist/es/mcp/tool-generator.mjs +207 -0
- package/dist/es/mcp/types.mjs +3 -0
- package/dist/es/node/fs.mjs +2 -2
- package/dist/es/utils.mjs +2 -3
- package/dist/es/zod-schema-utils.mjs +54 -0
- package/dist/lib/baseDB.js +2 -2
- package/dist/lib/build/copy-static.js +4 -4
- package/dist/lib/build/rspack-config.js +38 -0
- package/dist/lib/common.js +4 -4
- package/dist/lib/constants/example-code.js +6 -6
- package/dist/lib/constants/index.js +13 -13
- package/dist/lib/env/basic.js +2 -2
- package/dist/lib/env/constants.js +32 -90
- package/dist/lib/env/global-config-manager.js +4 -5
- package/dist/lib/env/helper.js +13 -22
- package/dist/lib/env/index.js +24 -28
- package/dist/lib/env/init-debug.js +7 -7
- package/dist/lib/env/model-config-manager.js +47 -67
- package/dist/lib/env/parse-model-config.js +155 -0
- package/dist/lib/env/types.js +146 -379
- package/dist/lib/env/utils.js +4 -4
- package/dist/lib/extractor/constants.js +4 -4
- package/dist/lib/extractor/debug.js +1 -1
- package/dist/lib/extractor/dom-util.js +18 -26
- package/dist/lib/extractor/index.js +11 -21
- package/dist/lib/extractor/locator.js +10 -20
- package/dist/lib/extractor/tree.js +4 -7
- package/dist/lib/extractor/util.js +17 -50
- package/dist/lib/extractor/web-extractor.js +12 -19
- package/dist/lib/img/box-select.js +4 -4
- package/dist/lib/img/draw-box.js +2 -2
- package/dist/lib/img/get-jimp.js +16 -34
- package/dist/lib/img/get-photon.js +24 -47
- package/dist/lib/img/get-sharp.js +16 -34
- package/dist/lib/img/index.js +18 -18
- package/dist/lib/img/info.js +4 -4
- package/dist/lib/img/transform.js +10 -10
- package/dist/lib/index.js +8 -4
- package/dist/lib/logger.js +4 -4
- package/dist/lib/mcp/base-server.js +300 -0
- package/dist/lib/mcp/base-tools.js +118 -0
- package/dist/lib/mcp/index.js +86 -0
- package/dist/lib/mcp/inject-report-html-plugin.js +98 -0
- package/dist/lib/mcp/tool-generator.js +244 -0
- package/dist/lib/mcp/types.js +40 -0
- package/dist/lib/node/fs.js +6 -6
- package/dist/lib/node/index.js +6 -8
- package/dist/lib/polyfills/async-hooks.js +2 -2
- package/dist/lib/polyfills/index.js +6 -8
- package/dist/lib/types/index.js +2 -2
- package/dist/lib/us-keyboard-layout.js +2 -2
- package/dist/lib/utils.js +13 -14
- package/dist/lib/zod-schema-utils.js +97 -0
- package/dist/types/build/rspack-config.d.ts +8 -0
- package/dist/types/constants/example-code.d.ts +1 -1
- package/dist/types/env/constants.d.ts +5 -18
- package/dist/types/env/global-config-manager.d.ts +1 -2
- package/dist/types/env/helper.d.ts +2 -4
- package/dist/types/env/model-config-manager.d.ts +8 -7
- package/dist/types/env/parse-model-config.d.ts +28 -0
- package/dist/types/env/types.d.ts +152 -191
- package/dist/types/extractor/dom-util.d.ts +2 -15
- package/dist/types/extractor/index.d.ts +1 -2
- package/dist/types/extractor/locator.d.ts +0 -1
- package/dist/types/extractor/tree.d.ts +1 -4
- package/dist/types/extractor/util.d.ts +0 -3
- package/dist/types/index.d.ts +1 -0
- package/dist/types/mcp/base-server.d.ts +77 -0
- package/dist/types/mcp/base-tools.d.ts +55 -0
- package/dist/types/mcp/index.d.ts +5 -0
- package/dist/types/mcp/inject-report-html-plugin.d.ts +18 -0
- package/dist/types/mcp/tool-generator.d.ts +11 -0
- package/dist/types/mcp/types.d.ts +100 -0
- package/dist/types/types/index.d.ts +5 -2
- package/dist/types/zod-schema-utils.d.ts +23 -0
- package/package.json +19 -4
- package/src/build/rspack-config.ts +12 -0
- package/src/constants/example-code.ts +4 -4
- package/src/env/constants.ts +58 -203
- package/src/env/global-config-manager.ts +7 -7
- package/src/env/helper.ts +10 -31
- package/src/env/init-debug.ts +11 -6
- package/src/env/model-config-manager.ts +91 -87
- package/src/env/parse-model-config.ts +265 -0
- package/src/env/types.ts +212 -344
- package/src/extractor/dom-util.ts +15 -12
- package/src/extractor/index.ts +0 -3
- package/src/extractor/locator.ts +3 -12
- package/src/extractor/tree.ts +4 -4
- package/src/extractor/util.ts +0 -32
- package/src/index.ts +2 -0
- package/src/mcp/base-server.ts +435 -0
- package/src/mcp/base-tools.ts +196 -0
- package/src/mcp/index.ts +5 -0
- package/src/mcp/inject-report-html-plugin.ts +119 -0
- package/src/mcp/tool-generator.ts +330 -0
- package/src/mcp/types.ts +108 -0
- package/src/node/fs.ts +1 -1
- package/src/types/index.ts +8 -2
- package/src/utils.ts +1 -1
- package/src/zod-schema-utils.ts +133 -0
- package/dist/es/env/decide-model-config.mjs +0 -172
- package/dist/es/env/parse.mjs +0 -69
- package/dist/lib/env/decide-model-config.js +0 -212
- package/dist/lib/env/parse.js +0 -106
- package/dist/types/env/decide-model-config.d.ts +0 -14
- package/dist/types/env/parse.d.ts +0 -12
- package/src/env/decide-model-config.ts +0 -319
- package/src/env/parse.ts +0 -131
|
@@ -0,0 +1,244 @@
|
|
|
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 img_namespaceObject = require("@midscene/shared/img");
|
|
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 argsDescription = serializeArgsToDescription(args);
|
|
123
|
+
return argsDescription ? `Use the action "${actionName}" with ${argsDescription}` : `Use the action "${actionName}"`;
|
|
124
|
+
}
|
|
125
|
+
async function captureScreenshotResult(agent, actionName) {
|
|
126
|
+
try {
|
|
127
|
+
const screenshot = await agent.page?.screenshotBase64();
|
|
128
|
+
if (!screenshot) return {
|
|
129
|
+
content: [
|
|
130
|
+
{
|
|
131
|
+
type: 'text',
|
|
132
|
+
text: `Action "${actionName}" completed.`
|
|
133
|
+
}
|
|
134
|
+
]
|
|
135
|
+
};
|
|
136
|
+
const { mimeType, body } = (0, img_namespaceObject.parseBase64)(screenshot);
|
|
137
|
+
return {
|
|
138
|
+
content: [
|
|
139
|
+
{
|
|
140
|
+
type: 'text',
|
|
141
|
+
text: `Action "${actionName}" completed.`
|
|
142
|
+
},
|
|
143
|
+
{
|
|
144
|
+
type: 'image',
|
|
145
|
+
data: body,
|
|
146
|
+
mimeType
|
|
147
|
+
}
|
|
148
|
+
]
|
|
149
|
+
};
|
|
150
|
+
} catch (error) {
|
|
151
|
+
const errorMessage = getErrorMessage(error);
|
|
152
|
+
console.error('Error capturing screenshot:', errorMessage);
|
|
153
|
+
return {
|
|
154
|
+
content: [
|
|
155
|
+
{
|
|
156
|
+
type: 'text',
|
|
157
|
+
text: `Action "${actionName}" completed (screenshot unavailable: ${errorMessage})`
|
|
158
|
+
}
|
|
159
|
+
]
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
function createErrorResult(message) {
|
|
164
|
+
return {
|
|
165
|
+
content: [
|
|
166
|
+
{
|
|
167
|
+
type: 'text',
|
|
168
|
+
text: message
|
|
169
|
+
}
|
|
170
|
+
],
|
|
171
|
+
isError: true
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
function generateToolsFromActionSpace(actionSpace, getAgent) {
|
|
175
|
+
return actionSpace.map((action)=>{
|
|
176
|
+
const schema = extractActionSchema(action.paramSchema);
|
|
177
|
+
return {
|
|
178
|
+
name: action.name,
|
|
179
|
+
description: describeActionForMCP(action),
|
|
180
|
+
schema,
|
|
181
|
+
handler: async (args)=>{
|
|
182
|
+
try {
|
|
183
|
+
const agent = await getAgent();
|
|
184
|
+
if (agent.aiAction) {
|
|
185
|
+
const instruction = buildActionInstruction(action.name, args);
|
|
186
|
+
try {
|
|
187
|
+
await agent.aiAction(instruction);
|
|
188
|
+
} catch (error) {
|
|
189
|
+
const errorMessage = getErrorMessage(error);
|
|
190
|
+
console.error(`Error executing action "${action.name}":`, errorMessage);
|
|
191
|
+
return createErrorResult(`Failed to execute action "${action.name}": ${errorMessage}`);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
return await captureScreenshotResult(agent, action.name);
|
|
195
|
+
} catch (error) {
|
|
196
|
+
const errorMessage = getErrorMessage(error);
|
|
197
|
+
console.error(`Error in handler for "${action.name}":`, errorMessage);
|
|
198
|
+
return createErrorResult(`Failed to get agent or execute action "${action.name}": ${errorMessage}`);
|
|
199
|
+
}
|
|
200
|
+
},
|
|
201
|
+
autoDestroy: true
|
|
202
|
+
};
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
function generateCommonTools(getAgent) {
|
|
206
|
+
return [
|
|
207
|
+
{
|
|
208
|
+
name: 'take_screenshot',
|
|
209
|
+
description: 'Capture screenshot of current page/screen',
|
|
210
|
+
schema: {},
|
|
211
|
+
handler: async ()=>{
|
|
212
|
+
try {
|
|
213
|
+
const agent = await getAgent();
|
|
214
|
+
const screenshot = await agent.page?.screenshotBase64();
|
|
215
|
+
if (!screenshot) return createErrorResult('Screenshot not available');
|
|
216
|
+
const { mimeType, body } = (0, img_namespaceObject.parseBase64)(screenshot);
|
|
217
|
+
return {
|
|
218
|
+
content: [
|
|
219
|
+
{
|
|
220
|
+
type: 'image',
|
|
221
|
+
data: body,
|
|
222
|
+
mimeType
|
|
223
|
+
}
|
|
224
|
+
]
|
|
225
|
+
};
|
|
226
|
+
} catch (error) {
|
|
227
|
+
const errorMessage = getErrorMessage(error);
|
|
228
|
+
console.error('Error taking screenshot:', errorMessage);
|
|
229
|
+
return createErrorResult(`Failed to capture screenshot: ${errorMessage}`);
|
|
230
|
+
}
|
|
231
|
+
},
|
|
232
|
+
autoDestroy: true
|
|
233
|
+
}
|
|
234
|
+
];
|
|
235
|
+
}
|
|
236
|
+
exports.generateCommonTools = __webpack_exports__.generateCommonTools;
|
|
237
|
+
exports.generateToolsFromActionSpace = __webpack_exports__.generateToolsFromActionSpace;
|
|
238
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
239
|
+
"generateCommonTools",
|
|
240
|
+
"generateToolsFromActionSpace"
|
|
241
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
242
|
+
Object.defineProperty(exports, '__esModule', {
|
|
243
|
+
value: true
|
|
244
|
+
});
|
|
@@ -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
|
+
});
|