@midscene/shared 1.9.8 → 1.10.1-beta-20260624112700.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/{mcp → agent-tools}/base-tools.mjs +1 -8
- package/dist/es/{mcp → agent-tools}/chrome-path.mjs +14 -3
- package/dist/es/{mcp → agent-tools}/index.mjs +0 -3
- package/dist/es/{mcp → agent-tools}/tool-generator.mjs +3 -3
- package/dist/es/cli/cli-runner.mjs +1 -1
- package/dist/es/env/parse-model-config.mjs +1 -1
- package/dist/es/env/types.mjs +3 -5
- package/dist/es/utils.mjs +2 -6
- package/dist/lib/{mcp → agent-tools}/base-tools.js +1 -8
- package/dist/lib/{mcp → agent-tools}/chrome-path.js +13 -2
- package/dist/lib/{mcp → agent-tools}/index.js +16 -37
- package/dist/lib/{mcp → agent-tools}/tool-generator.js +3 -3
- package/dist/lib/cli/cli-runner.js +1 -1
- package/dist/lib/env/parse-model-config.js +1 -1
- package/dist/lib/env/types.js +5 -10
- package/dist/lib/utils.js +8 -15
- package/dist/types/{mcp → agent-tools}/base-tools.d.ts +7 -13
- package/dist/types/{mcp → agent-tools}/index.d.ts +0 -3
- package/dist/types/{mcp → agent-tools}/init-arg-utils.d.ts +3 -3
- package/dist/types/{mcp → agent-tools}/tool-defaults.d.ts +5 -6
- package/dist/types/{mcp → agent-tools}/tool-generator.d.ts +1 -1
- package/dist/types/{mcp → agent-tools}/types.d.ts +4 -9
- package/dist/types/cli/cli-args.d.ts +1 -1
- package/dist/types/cli/cli-runner.d.ts +2 -2
- package/dist/types/env/types.d.ts +8 -6
- package/dist/types/key-alias-utils.d.ts +2 -2
- package/dist/types/utils.d.ts +0 -1
- package/package.json +16 -9
- package/src/{mcp → agent-tools}/base-tools.ts +8 -33
- package/src/{mcp → agent-tools}/chrome-path.ts +20 -3
- package/src/{mcp → agent-tools}/index.ts +0 -3
- package/src/{mcp → agent-tools}/init-arg-utils.ts +3 -3
- package/src/{mcp → agent-tools}/tool-defaults.ts +5 -6
- package/src/{mcp → agent-tools}/tool-generator.ts +6 -6
- package/src/{mcp → agent-tools}/types.ts +4 -9
- package/src/cli/cli-args.ts +1 -1
- package/src/cli/cli-runner.ts +4 -4
- package/src/env/types.ts +5 -5
- package/src/key-alias-utils.ts +2 -2
- package/src/utils.ts +1 -10
- package/dist/es/mcp/base-server.mjs +0 -295
- package/dist/es/mcp/inject-report-html-plugin.mjs +0 -53
- package/dist/es/mcp/launcher-helper.mjs +0 -52
- package/dist/lib/mcp/base-server.js +0 -345
- package/dist/lib/mcp/inject-report-html-plugin.js +0 -98
- package/dist/lib/mcp/launcher-helper.js +0 -86
- package/dist/types/mcp/base-server.d.ts +0 -106
- package/dist/types/mcp/inject-report-html-plugin.d.ts +0 -18
- package/dist/types/mcp/launcher-helper.d.ts +0 -94
- package/src/mcp/base-server.ts +0 -529
- package/src/mcp/inject-report-html-plugin.ts +0 -119
- package/src/mcp/launcher-helper.ts +0 -200
- /package/dist/es/{mcp → agent-tools}/agent-behavior-init-args.mjs +0 -0
- /package/dist/es/{mcp → agent-tools}/cli-report-session.mjs +0 -0
- /package/dist/es/{mcp → agent-tools}/error-formatter.mjs +0 -0
- /package/dist/es/{mcp → agent-tools}/init-arg-utils.mjs +0 -0
- /package/dist/es/{mcp → agent-tools}/tool-defaults.mjs +0 -0
- /package/dist/es/{mcp → agent-tools}/types.mjs +0 -0
- /package/dist/es/{mcp → agent-tools}/user-prompt.mjs +0 -0
- /package/dist/lib/{mcp → agent-tools}/agent-behavior-init-args.js +0 -0
- /package/dist/lib/{mcp → agent-tools}/cli-report-session.js +0 -0
- /package/dist/lib/{mcp → agent-tools}/error-formatter.js +0 -0
- /package/dist/lib/{mcp → agent-tools}/init-arg-utils.js +0 -0
- /package/dist/lib/{mcp → agent-tools}/tool-defaults.js +0 -0
- /package/dist/lib/{mcp → agent-tools}/types.js +0 -0
- /package/dist/lib/{mcp → agent-tools}/user-prompt.js +0 -0
- /package/dist/types/{mcp → agent-tools}/agent-behavior-init-args.d.ts +0 -0
- /package/dist/types/{mcp → agent-tools}/chrome-path.d.ts +0 -0
- /package/dist/types/{mcp → agent-tools}/cli-report-session.d.ts +0 -0
- /package/dist/types/{mcp → agent-tools}/error-formatter.d.ts +0 -0
- /package/dist/types/{mcp → agent-tools}/user-prompt.d.ts +0 -0
- /package/src/{mcp → agent-tools}/agent-behavior-init-args.ts +0 -0
- /package/src/{mcp → agent-tools}/cli-report-session.ts +0 -0
- /package/src/{mcp → agent-tools}/error-formatter.ts +0 -0
- /package/src/{mcp → agent-tools}/user-prompt.ts +0 -0
package/src/utils.ts
CHANGED
|
@@ -64,17 +64,8 @@ export function assert(condition: any, message?: string): asserts condition {
|
|
|
64
64
|
}
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
-
let isMcp = false;
|
|
68
|
-
|
|
69
|
-
export function setIsMcp(value: boolean) {
|
|
70
|
-
isMcp = value;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
//mcp need use obj format to console msg: https://github.com/modelcontextprotocol/typescript-sdk/issues/244
|
|
74
67
|
export function logMsg(...message: Parameters<typeof console.log>) {
|
|
75
|
-
|
|
76
|
-
console.log(...message);
|
|
77
|
-
}
|
|
68
|
+
console.log(...message);
|
|
78
69
|
}
|
|
79
70
|
|
|
80
71
|
export async function repeat(
|
|
@@ -1,295 +0,0 @@
|
|
|
1
|
-
import { randomUUID } from "node:crypto";
|
|
2
|
-
import { setIsMcp } from "@midscene/shared/utils";
|
|
3
|
-
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
4
|
-
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
5
|
-
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
|
6
|
-
import express from "express";
|
|
7
|
-
import { getErrorMessage } from "./error-formatter.mjs";
|
|
8
|
-
import { TOOL_BEHAVIOR_FLAGS, mergeToolDefaults, resolveToolDefaults } from "./tool-defaults.mjs";
|
|
9
|
-
function _define_property(obj, key, value) {
|
|
10
|
-
if (key in obj) Object.defineProperty(obj, key, {
|
|
11
|
-
value: value,
|
|
12
|
-
enumerable: true,
|
|
13
|
-
configurable: true,
|
|
14
|
-
writable: true
|
|
15
|
-
});
|
|
16
|
-
else obj[key] = value;
|
|
17
|
-
return obj;
|
|
18
|
-
}
|
|
19
|
-
const CLI_ARGS_CONFIG = {
|
|
20
|
-
mode: {
|
|
21
|
-
type: 'string',
|
|
22
|
-
default: 'stdio'
|
|
23
|
-
},
|
|
24
|
-
port: {
|
|
25
|
-
type: 'string',
|
|
26
|
-
default: '3000'
|
|
27
|
-
},
|
|
28
|
-
host: {
|
|
29
|
-
type: 'string',
|
|
30
|
-
default: 'localhost'
|
|
31
|
-
},
|
|
32
|
-
...Object.fromEntries(TOOL_BEHAVIOR_FLAGS.map((flag)=>[
|
|
33
|
-
flag.cli,
|
|
34
|
-
{
|
|
35
|
-
type: 'boolean'
|
|
36
|
-
}
|
|
37
|
-
]))
|
|
38
|
-
};
|
|
39
|
-
function launchMCPServer(server, args) {
|
|
40
|
-
server.setToolDefaults(resolveToolDefaults((cli)=>true === args[cli]));
|
|
41
|
-
if ('http' === args.mode) return server.launchHttp({
|
|
42
|
-
port: Number.parseInt(args.port || '3000', 10),
|
|
43
|
-
host: args.host || 'localhost'
|
|
44
|
-
});
|
|
45
|
-
return server.launch();
|
|
46
|
-
}
|
|
47
|
-
const SESSION_TIMEOUT_MS = 1800000;
|
|
48
|
-
const CLEANUP_INTERVAL_MS = 300000;
|
|
49
|
-
const MAX_SESSIONS = 100;
|
|
50
|
-
class BaseMCPServer {
|
|
51
|
-
setToolDefaults(toolDefaults) {
|
|
52
|
-
this.toolDefaults = mergeToolDefaults(this.toolDefaults, toolDefaults);
|
|
53
|
-
}
|
|
54
|
-
async initializeToolsManager() {
|
|
55
|
-
setIsMcp(true);
|
|
56
|
-
this.toolsManager = this.providedToolsManager || this.createToolsManager();
|
|
57
|
-
this.toolsManager.setToolDefaults?.(this.toolDefaults);
|
|
58
|
-
try {
|
|
59
|
-
await this.toolsManager.initTools();
|
|
60
|
-
} catch (error) {
|
|
61
|
-
const message = getErrorMessage(error);
|
|
62
|
-
console.error(`Failed to initialize tools: ${message}`);
|
|
63
|
-
console.error('Tools will be initialized on first use');
|
|
64
|
-
}
|
|
65
|
-
this.toolsManager.attachToServer(this.mcpServer);
|
|
66
|
-
}
|
|
67
|
-
async performCleanup() {
|
|
68
|
-
console.error(`${this.config.name} closing...`);
|
|
69
|
-
this.mcpServer.close();
|
|
70
|
-
await this.toolsManager?.destroy?.().catch(console.error);
|
|
71
|
-
}
|
|
72
|
-
async launch() {
|
|
73
|
-
console.log = (...args)=>{
|
|
74
|
-
console.error('[LOG]', ...args);
|
|
75
|
-
};
|
|
76
|
-
console.info = (...args)=>{
|
|
77
|
-
console.error('[INFO]', ...args);
|
|
78
|
-
};
|
|
79
|
-
console.debug = (...args)=>{
|
|
80
|
-
console.error('[DEBUG]', ...args);
|
|
81
|
-
};
|
|
82
|
-
await this.initializeToolsManager();
|
|
83
|
-
const transport = new StdioServerTransport();
|
|
84
|
-
try {
|
|
85
|
-
await this.mcpServer.connect(transport);
|
|
86
|
-
} catch (error) {
|
|
87
|
-
const message = getErrorMessage(error);
|
|
88
|
-
console.error(`Failed to connect MCP stdio transport: ${message}`);
|
|
89
|
-
throw new Error(`Failed to initialize MCP stdio transport: ${message}`);
|
|
90
|
-
}
|
|
91
|
-
let isShuttingDown = false;
|
|
92
|
-
const cleanup = ()=>{
|
|
93
|
-
if (isShuttingDown) return;
|
|
94
|
-
isShuttingDown = true;
|
|
95
|
-
console.error(`${this.config.name} shutting down...`);
|
|
96
|
-
this.performCleanup().finally(()=>process.exit(0));
|
|
97
|
-
};
|
|
98
|
-
process.on('uncaughtException', (error)=>{
|
|
99
|
-
if ('EPIPE' === error.code || 'ERR_STREAM_DESTROYED' === error.code) return void cleanup();
|
|
100
|
-
console.error(`[${this.config.name}] Uncaught Exception:`, error);
|
|
101
|
-
console.error('Stack:', error.stack);
|
|
102
|
-
});
|
|
103
|
-
process.on('unhandledRejection', (reason)=>{
|
|
104
|
-
console.error(`[${this.config.name}] Unhandled Rejection:`, reason);
|
|
105
|
-
if (reason instanceof Error) console.error('Stack:', reason.stack);
|
|
106
|
-
});
|
|
107
|
-
process.stdin.on('close', cleanup);
|
|
108
|
-
process.stdin.on('end', cleanup);
|
|
109
|
-
process.stdout.on('error', cleanup);
|
|
110
|
-
process.once('SIGINT', cleanup);
|
|
111
|
-
process.once('SIGTERM', cleanup);
|
|
112
|
-
process.once('SIGHUP', cleanup);
|
|
113
|
-
return {
|
|
114
|
-
close: async ()=>{
|
|
115
|
-
this.performCleanup();
|
|
116
|
-
}
|
|
117
|
-
};
|
|
118
|
-
}
|
|
119
|
-
async launchHttp(options) {
|
|
120
|
-
if (!Number.isInteger(options.port) || options.port < 1 || options.port > 65535) throw new Error(`Invalid port number: ${options.port}. Port must be between 1 and 65535.`);
|
|
121
|
-
await this.initializeToolsManager();
|
|
122
|
-
const app = express();
|
|
123
|
-
app.use(express.json({
|
|
124
|
-
limit: '10mb'
|
|
125
|
-
}));
|
|
126
|
-
const sessions = new Map();
|
|
127
|
-
app.all('/mcp', async (req, res)=>{
|
|
128
|
-
const startTime = Date.now();
|
|
129
|
-
const requestId = randomUUID().substring(0, 8);
|
|
130
|
-
try {
|
|
131
|
-
const rawSessionId = req.headers['mcp-session-id'];
|
|
132
|
-
const sessionId = Array.isArray(rawSessionId) ? rawSessionId[0] : rawSessionId;
|
|
133
|
-
let session = sessionId ? sessions.get(sessionId) : void 0;
|
|
134
|
-
if (!session && 'POST' === req.method) {
|
|
135
|
-
if (sessions.size >= MAX_SESSIONS) {
|
|
136
|
-
console.error(`[${new Date().toISOString()}] [${requestId}] Session limit reached: ${sessions.size}/${MAX_SESSIONS}`);
|
|
137
|
-
res.status(503).json({
|
|
138
|
-
error: 'Too many active sessions',
|
|
139
|
-
message: 'Server is at maximum capacity. Please try again later.'
|
|
140
|
-
});
|
|
141
|
-
return;
|
|
142
|
-
}
|
|
143
|
-
session = await this.createHttpSession(sessions);
|
|
144
|
-
console.log(`[${new Date().toISOString()}] [${requestId}] New session created: ${session.transport.sessionId}`);
|
|
145
|
-
}
|
|
146
|
-
if (session) {
|
|
147
|
-
session.lastAccessedAt = new Date();
|
|
148
|
-
await session.transport.handleRequest(req, res, req.body);
|
|
149
|
-
const duration = Date.now() - startTime;
|
|
150
|
-
console.log(`[${new Date().toISOString()}] [${requestId}] Request completed in ${duration}ms`);
|
|
151
|
-
} else {
|
|
152
|
-
console.error(`[${new Date().toISOString()}] [${requestId}] Invalid session or GET without session`);
|
|
153
|
-
res.status(400).json({
|
|
154
|
-
error: 'Invalid session or GET without session'
|
|
155
|
-
});
|
|
156
|
-
}
|
|
157
|
-
} catch (error) {
|
|
158
|
-
const message = getErrorMessage(error);
|
|
159
|
-
const duration = Date.now() - startTime;
|
|
160
|
-
console.error(`[${new Date().toISOString()}] [${requestId}] MCP request error after ${duration}ms: ${message}`);
|
|
161
|
-
if (!res.headersSent) res.status(500).json({
|
|
162
|
-
error: 'Internal server error',
|
|
163
|
-
message: 'Failed to process MCP request'
|
|
164
|
-
});
|
|
165
|
-
}
|
|
166
|
-
});
|
|
167
|
-
const host = options.host || 'localhost';
|
|
168
|
-
const server = app.listen(options.port, host, ()=>{
|
|
169
|
-
console.log(`${this.config.name} HTTP server listening on http://${host}:${options.port}/mcp`);
|
|
170
|
-
}).on('error', (error)=>{
|
|
171
|
-
if ('EADDRINUSE' === error.code) console.error(`ERROR: Port ${options.port} is already in use.\nPlease try a different port: --port=<number>\nExample: --mode=http --port=${options.port + 1}`);
|
|
172
|
-
else if ('EACCES' === error.code) console.error(`ERROR: Permission denied to bind to port ${options.port}.\nPorts below 1024 require root/admin privileges.\nPlease use a port above 1024 or run with elevated privileges.`);
|
|
173
|
-
else console.error(`ERROR: Failed to start HTTP server on ${host}:${options.port}\nReason: ${error.message}\nCode: ${error.code || 'unknown'}`);
|
|
174
|
-
process.exit(1);
|
|
175
|
-
});
|
|
176
|
-
const cleanupInterval = this.startSessionCleanup(sessions);
|
|
177
|
-
this.setupHttpShutdownHandlers(server, sessions, cleanupInterval);
|
|
178
|
-
return {
|
|
179
|
-
port: options.port,
|
|
180
|
-
host,
|
|
181
|
-
close: async ()=>{
|
|
182
|
-
clearInterval(cleanupInterval);
|
|
183
|
-
for (const session of sessions.values())try {
|
|
184
|
-
await session.transport.close();
|
|
185
|
-
} catch (error) {
|
|
186
|
-
const message = getErrorMessage(error);
|
|
187
|
-
console.error(`Failed to close session ${session.transport.sessionId}: ${message}`);
|
|
188
|
-
}
|
|
189
|
-
sessions.clear();
|
|
190
|
-
return new Promise((resolve)=>{
|
|
191
|
-
server.close(async (err)=>{
|
|
192
|
-
if (err) console.error('Error closing HTTP server:', err);
|
|
193
|
-
await this.performCleanup();
|
|
194
|
-
resolve();
|
|
195
|
-
});
|
|
196
|
-
});
|
|
197
|
-
}
|
|
198
|
-
};
|
|
199
|
-
}
|
|
200
|
-
async createHttpSession(sessions) {
|
|
201
|
-
const transport = new StreamableHTTPServerTransport({
|
|
202
|
-
sessionIdGenerator: ()=>randomUUID(),
|
|
203
|
-
onsessioninitialized: (sid)=>{
|
|
204
|
-
sessions.set(sid, {
|
|
205
|
-
transport,
|
|
206
|
-
createdAt: new Date(),
|
|
207
|
-
lastAccessedAt: new Date()
|
|
208
|
-
});
|
|
209
|
-
console.log(`[${new Date().toISOString()}] Session ${sid} initialized (total: ${sessions.size})`);
|
|
210
|
-
}
|
|
211
|
-
});
|
|
212
|
-
transport.onclose = ()=>{
|
|
213
|
-
if (transport.sessionId) {
|
|
214
|
-
sessions.delete(transport.sessionId);
|
|
215
|
-
console.log(`[${new Date().toISOString()}] Session ${transport.sessionId} closed (remaining: ${sessions.size})`);
|
|
216
|
-
}
|
|
217
|
-
};
|
|
218
|
-
try {
|
|
219
|
-
await this.mcpServer.connect(transport);
|
|
220
|
-
} catch (error) {
|
|
221
|
-
const message = getErrorMessage(error);
|
|
222
|
-
console.error(`[${new Date().toISOString()}] Failed to connect MCP transport: ${message}`);
|
|
223
|
-
if (transport.sessionId) sessions.delete(transport.sessionId);
|
|
224
|
-
throw new Error(`Failed to initialize MCP session: ${message}`);
|
|
225
|
-
}
|
|
226
|
-
return {
|
|
227
|
-
transport,
|
|
228
|
-
createdAt: new Date(),
|
|
229
|
-
lastAccessedAt: new Date()
|
|
230
|
-
};
|
|
231
|
-
}
|
|
232
|
-
startSessionCleanup(sessions) {
|
|
233
|
-
return setInterval(()=>{
|
|
234
|
-
const now = Date.now();
|
|
235
|
-
for (const [sid, session] of sessions)if (now - session.lastAccessedAt.getTime() > SESSION_TIMEOUT_MS) try {
|
|
236
|
-
session.transport.close();
|
|
237
|
-
sessions.delete(sid);
|
|
238
|
-
console.log(`[${new Date().toISOString()}] Session ${sid} cleaned up due to inactivity (remaining: ${sessions.size})`);
|
|
239
|
-
} catch (error) {
|
|
240
|
-
const message = getErrorMessage(error);
|
|
241
|
-
console.error(`[${new Date().toISOString()}] Failed to close session ${sid} during cleanup: ${message}`);
|
|
242
|
-
sessions.delete(sid);
|
|
243
|
-
}
|
|
244
|
-
}, CLEANUP_INTERVAL_MS);
|
|
245
|
-
}
|
|
246
|
-
setupHttpShutdownHandlers(server, sessions, cleanupInterval) {
|
|
247
|
-
const cleanup = ()=>{
|
|
248
|
-
console.error(`${this.config.name} shutting down...`);
|
|
249
|
-
clearInterval(cleanupInterval);
|
|
250
|
-
for (const session of sessions.values())try {
|
|
251
|
-
session.transport.close();
|
|
252
|
-
} catch (error) {
|
|
253
|
-
const message = getErrorMessage(error);
|
|
254
|
-
console.error(`Error closing session during shutdown: ${message}`);
|
|
255
|
-
}
|
|
256
|
-
sessions.clear();
|
|
257
|
-
try {
|
|
258
|
-
server.close(()=>{
|
|
259
|
-
this.performCleanup().finally(()=>process.exit(0));
|
|
260
|
-
});
|
|
261
|
-
setTimeout(()=>{
|
|
262
|
-
console.error('Forcefully shutting down after timeout');
|
|
263
|
-
this.performCleanup().finally(()=>process.exit(1));
|
|
264
|
-
}, 5000);
|
|
265
|
-
} catch (error) {
|
|
266
|
-
const message = getErrorMessage(error);
|
|
267
|
-
console.error(`Error closing HTTP server: ${message}`);
|
|
268
|
-
this.performCleanup().finally(()=>process.exit(1));
|
|
269
|
-
}
|
|
270
|
-
};
|
|
271
|
-
process.once('SIGINT', cleanup);
|
|
272
|
-
process.once('SIGTERM', cleanup);
|
|
273
|
-
}
|
|
274
|
-
getServer() {
|
|
275
|
-
return this.mcpServer;
|
|
276
|
-
}
|
|
277
|
-
getToolsManager() {
|
|
278
|
-
return this.toolsManager;
|
|
279
|
-
}
|
|
280
|
-
constructor(config, toolsManager){
|
|
281
|
-
_define_property(this, "mcpServer", void 0);
|
|
282
|
-
_define_property(this, "toolsManager", void 0);
|
|
283
|
-
_define_property(this, "config", void 0);
|
|
284
|
-
_define_property(this, "providedToolsManager", void 0);
|
|
285
|
-
_define_property(this, "toolDefaults", {});
|
|
286
|
-
this.config = config;
|
|
287
|
-
this.mcpServer = new McpServer({
|
|
288
|
-
name: config.name,
|
|
289
|
-
version: config.version,
|
|
290
|
-
description: config.description
|
|
291
|
-
});
|
|
292
|
-
this.providedToolsManager = toolsManager;
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
export { BaseMCPServer, CLI_ARGS_CONFIG, launchMCPServer };
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import node_fs from "node:fs";
|
|
2
|
-
import node_path from "node:path";
|
|
3
|
-
const MAGIC_STRING = 'REPLACE_ME_WITH_REPORT_HTML';
|
|
4
|
-
const REPLACED_MARK = '/*REPORT_HTML_REPLACED*/';
|
|
5
|
-
const REG_EXP_FOR_REPLACE = /\/\*REPORT_HTML_REPLACED\*\/.*/;
|
|
6
|
-
function injectReportHtmlFromCore(packageDir) {
|
|
7
|
-
return {
|
|
8
|
-
name: 'inject-report-html-from-core',
|
|
9
|
-
setup (api) {
|
|
10
|
-
api.onAfterBuild(()=>{
|
|
11
|
-
const coreUtilsPath = node_path.resolve(packageDir, '..', 'core', 'dist', 'lib', 'utils.js');
|
|
12
|
-
if (!node_fs.existsSync(coreUtilsPath)) return void console.warn('[inject-report-html] @midscene/core dist not found, skipping');
|
|
13
|
-
const coreContent = node_fs.readFileSync(coreUtilsPath, 'utf-8');
|
|
14
|
-
if (!coreContent.includes(REPLACED_MARK)) return void console.warn('[inject-report-html] HTML not found in core dist. Ensure report builds first.');
|
|
15
|
-
const markerIndex = coreContent.indexOf(REPLACED_MARK);
|
|
16
|
-
const jsonStart = markerIndex + REPLACED_MARK.length;
|
|
17
|
-
let jsonEnd = jsonStart;
|
|
18
|
-
if ('"' === coreContent[jsonStart]) {
|
|
19
|
-
jsonEnd = jsonStart + 1;
|
|
20
|
-
while(jsonEnd < coreContent.length)if ('\\' === coreContent[jsonEnd]) jsonEnd += 2;
|
|
21
|
-
else if ('"' === coreContent[jsonEnd]) {
|
|
22
|
-
jsonEnd += 1;
|
|
23
|
-
break;
|
|
24
|
-
} else jsonEnd += 1;
|
|
25
|
-
}
|
|
26
|
-
const jsonString = coreContent.slice(jsonStart, jsonEnd);
|
|
27
|
-
if (!jsonString || jsonString.length < 10) return void console.warn('[inject-report-html] Failed to extract HTML from core');
|
|
28
|
-
const finalContent = `${REPLACED_MARK}${jsonString}`;
|
|
29
|
-
const distDir = node_path.join(packageDir, 'dist');
|
|
30
|
-
if (!node_fs.existsSync(distDir)) return;
|
|
31
|
-
const jsFiles = node_fs.readdirSync(distDir).filter((f)=>f.endsWith('.js'));
|
|
32
|
-
let injectedCount = 0;
|
|
33
|
-
for (const file of jsFiles){
|
|
34
|
-
const filePath = node_path.join(distDir, file);
|
|
35
|
-
const content = node_fs.readFileSync(filePath, 'utf-8');
|
|
36
|
-
if (content.includes(REPLACED_MARK)) {
|
|
37
|
-
if (REG_EXP_FOR_REPLACE.test(content)) {
|
|
38
|
-
node_fs.writeFileSync(filePath, content.replace(REG_EXP_FOR_REPLACE, ()=>finalContent));
|
|
39
|
-
console.log(`[inject-report-html] Updated: ${file}`);
|
|
40
|
-
injectedCount++;
|
|
41
|
-
}
|
|
42
|
-
} else if (content.includes(`'${MAGIC_STRING}'`)) {
|
|
43
|
-
node_fs.writeFileSync(filePath, content.replace(`'${MAGIC_STRING}'`, ()=>finalContent));
|
|
44
|
-
console.log(`[inject-report-html] Injected: ${file}`);
|
|
45
|
-
injectedCount++;
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
if (injectedCount > 0) console.log(`[inject-report-html] Completed: ${injectedCount} file(s)`);
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
export { injectReportHtmlFromCore };
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
function createMCPServerLauncher(config) {
|
|
2
|
-
const { agent, platformName, ToolsManagerClass, MCPServerClass } = config;
|
|
3
|
-
function validateAgent() {
|
|
4
|
-
const device = agent.interface;
|
|
5
|
-
if (!device) throw new Error(`Agent must have an 'interface' property that references the underlying device.
|
|
6
|
-
Please ensure your agent instance is properly initialized with a device interface.
|
|
7
|
-
Expected: agent.interface to be defined, but got: ${typeof device}
|
|
8
|
-
Solution: Check that your agent constructor properly sets the interface property.`);
|
|
9
|
-
}
|
|
10
|
-
function createToolsManager() {
|
|
11
|
-
const toolsManager = new ToolsManagerClass();
|
|
12
|
-
toolsManager.agent = agent;
|
|
13
|
-
return toolsManager;
|
|
14
|
-
}
|
|
15
|
-
function logStartupInfo(mode, additionalInfo) {
|
|
16
|
-
const device = agent.interface;
|
|
17
|
-
console.log(`Starting Midscene ${platformName} MCP Server (${mode})...`);
|
|
18
|
-
console.log(`Agent: ${agent.constructor.name}`);
|
|
19
|
-
console.log(`Device: ${device.constructor.name}`);
|
|
20
|
-
if (additionalInfo?.port !== void 0) console.log(`Port: ${additionalInfo.port}`);
|
|
21
|
-
if (additionalInfo?.host) console.log(`Host: ${additionalInfo.host}`);
|
|
22
|
-
}
|
|
23
|
-
return {
|
|
24
|
-
async launch (options = {}) {
|
|
25
|
-
const { verbose = true } = options;
|
|
26
|
-
validateAgent();
|
|
27
|
-
if (verbose) logStartupInfo('stdio');
|
|
28
|
-
const toolsManager = createToolsManager();
|
|
29
|
-
const server = new MCPServerClass(toolsManager);
|
|
30
|
-
const result = await server.launch();
|
|
31
|
-
if (verbose) console.log(`${platformName} MCP Server started (stdio mode)`);
|
|
32
|
-
return result;
|
|
33
|
-
},
|
|
34
|
-
async launchHttp (options) {
|
|
35
|
-
const { port, host = 'localhost', verbose = true } = options;
|
|
36
|
-
validateAgent();
|
|
37
|
-
if (verbose) logStartupInfo('HTTP', {
|
|
38
|
-
port,
|
|
39
|
-
host
|
|
40
|
-
});
|
|
41
|
-
const toolsManager = createToolsManager();
|
|
42
|
-
const server = new MCPServerClass(toolsManager);
|
|
43
|
-
const result = await server.launchHttp({
|
|
44
|
-
port,
|
|
45
|
-
host
|
|
46
|
-
});
|
|
47
|
-
if (verbose) console.log(`${platformName} MCP Server started on http://${result.host}:${result.port}/mcp`);
|
|
48
|
-
return result;
|
|
49
|
-
}
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
export { createMCPServerLauncher };
|