@thinkingos/vsl-mcp-server 0.1.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/README.md +344 -0
- package/bin/setup.js +504 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1686 -0
- package/dist/index.js.map +1 -0
- package/package.json +48 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,1686 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// src/index.ts
|
|
4
|
+
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
|
|
5
|
+
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
6
|
+
|
|
7
|
+
// src/config/loader.ts
|
|
8
|
+
import { readFileSync, existsSync } from "fs";
|
|
9
|
+
import { homedir } from "os";
|
|
10
|
+
import { join } from "path";
|
|
11
|
+
var DEFAULT_CONFIG_PATH = join(homedir(), ".vsl", "config.json");
|
|
12
|
+
var DEFAULT_VISION_MODELS = {
|
|
13
|
+
openai: "gpt-6-luna",
|
|
14
|
+
anthropic: "claude-haiku-4-5",
|
|
15
|
+
custom: ""
|
|
16
|
+
// Пользователь должен указать модель для custom провайдера
|
|
17
|
+
};
|
|
18
|
+
var DEFAULT_BROWSER_CONFIG = {
|
|
19
|
+
headless: true,
|
|
20
|
+
navigationTimeout: 3e4,
|
|
21
|
+
renderTimeout: 1e4,
|
|
22
|
+
downloadsPath: join(homedir(), ".vsl", "downloads"),
|
|
23
|
+
downloadTimeout: 6e4,
|
|
24
|
+
acceptDownloads: true
|
|
25
|
+
};
|
|
26
|
+
function loadConfig() {
|
|
27
|
+
const fileConfig = loadConfigFile();
|
|
28
|
+
const apiKeys = {
|
|
29
|
+
openai: process.env.OPENAI_API_KEY,
|
|
30
|
+
anthropic: process.env.ANTHROPIC_API_KEY
|
|
31
|
+
};
|
|
32
|
+
const provider = process.env.VSL_VISION_PROVIDER || fileConfig.vision?.provider || "openai";
|
|
33
|
+
const model = process.env.VSL_VISION_MODEL || fileConfig.vision?.model || DEFAULT_VISION_MODELS[provider];
|
|
34
|
+
const apiKey = provider === "custom" ? fileConfig.vision?.apiKey || "" : apiKeys[provider] || "";
|
|
35
|
+
const baseUrl = process.env.VSL_VISION_BASE_URL || fileConfig.vision?.baseUrl;
|
|
36
|
+
const browser = {
|
|
37
|
+
headless: process.env.VSL_BROWSER_HEADLESS ? process.env.VSL_BROWSER_HEADLESS === "true" : fileConfig.browser?.headless ?? DEFAULT_BROWSER_CONFIG.headless,
|
|
38
|
+
navigationTimeout: process.env.VSL_NAVIGATION_TIMEOUT ? parseInt(process.env.VSL_NAVIGATION_TIMEOUT, 10) : fileConfig.browser?.navigationTimeout ?? DEFAULT_BROWSER_CONFIG.navigationTimeout,
|
|
39
|
+
renderTimeout: process.env.VSL_RENDER_TIMEOUT ? parseInt(process.env.VSL_RENDER_TIMEOUT, 10) : fileConfig.browser?.renderTimeout ?? DEFAULT_BROWSER_CONFIG.renderTimeout,
|
|
40
|
+
downloadsPath: process.env.VSL_DOWNLOADS_PATH || fileConfig.browser?.downloadsPath || DEFAULT_BROWSER_CONFIG.downloadsPath,
|
|
41
|
+
downloadTimeout: process.env.VSL_DOWNLOAD_TIMEOUT ? parseInt(process.env.VSL_DOWNLOAD_TIMEOUT, 10) : fileConfig.browser?.downloadTimeout ?? DEFAULT_BROWSER_CONFIG.downloadTimeout,
|
|
42
|
+
acceptDownloads: process.env.VSL_ACCEPT_DOWNLOADS ? process.env.VSL_ACCEPT_DOWNLOADS === "true" : fileConfig.browser?.acceptDownloads ?? DEFAULT_BROWSER_CONFIG.acceptDownloads
|
|
43
|
+
};
|
|
44
|
+
return {
|
|
45
|
+
vision: {
|
|
46
|
+
provider,
|
|
47
|
+
model,
|
|
48
|
+
apiKey,
|
|
49
|
+
...baseUrl ? { baseUrl } : {}
|
|
50
|
+
},
|
|
51
|
+
browser,
|
|
52
|
+
apiKeys
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
function loadConfigFile() {
|
|
56
|
+
if (!existsSync(DEFAULT_CONFIG_PATH)) {
|
|
57
|
+
return {};
|
|
58
|
+
}
|
|
59
|
+
try {
|
|
60
|
+
const content = readFileSync(DEFAULT_CONFIG_PATH, "utf-8");
|
|
61
|
+
return JSON.parse(content);
|
|
62
|
+
} catch (error) {
|
|
63
|
+
console.error(`[VSL MCP Server] Failed to load config file: ${error}`);
|
|
64
|
+
return {};
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// src/tools/index.ts
|
|
69
|
+
import {
|
|
70
|
+
CallToolRequestSchema,
|
|
71
|
+
ListToolsRequestSchema
|
|
72
|
+
} from "@modelcontextprotocol/sdk/types.js";
|
|
73
|
+
|
|
74
|
+
// src/tools/getSnapshot.ts
|
|
75
|
+
async function handleGetSnapshot(args, browser, session, _config) {
|
|
76
|
+
try {
|
|
77
|
+
if (args.url) {
|
|
78
|
+
await browser.navigate(args.url);
|
|
79
|
+
}
|
|
80
|
+
const isAvailable = await browser.isAvailable();
|
|
81
|
+
if (!isAvailable) {
|
|
82
|
+
return {
|
|
83
|
+
status: "error",
|
|
84
|
+
error: 'Playwright is not installed. Install it with: npm install playwright\nOr use vsl_read_page with mode="http" for static pages.'
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
const domTree = await browser.evaluate(() => {
|
|
88
|
+
const extractElement = (el) => {
|
|
89
|
+
const rect = el.getBoundingClientRect();
|
|
90
|
+
const computedStyle = window.getComputedStyle(el);
|
|
91
|
+
return {
|
|
92
|
+
tag: el.tagName.toLowerCase(),
|
|
93
|
+
id: el.id || void 0,
|
|
94
|
+
className: el.className || void 0,
|
|
95
|
+
text: el.textContent?.trim().substring(0, 100) || void 0,
|
|
96
|
+
rect: {
|
|
97
|
+
x: rect.x,
|
|
98
|
+
y: rect.y,
|
|
99
|
+
width: rect.width,
|
|
100
|
+
height: rect.height
|
|
101
|
+
},
|
|
102
|
+
role: el.getAttribute("role") || void 0,
|
|
103
|
+
ariaLabel: el.getAttribute("aria-label") || void 0,
|
|
104
|
+
ariaHidden: el.getAttribute("aria-hidden") || void 0,
|
|
105
|
+
display: computedStyle.display,
|
|
106
|
+
visibility: computedStyle.visibility,
|
|
107
|
+
children: Array.from(el.children).filter((child) => {
|
|
108
|
+
const style = window.getComputedStyle(child);
|
|
109
|
+
return style.display !== "none" && style.visibility !== "hidden";
|
|
110
|
+
}).map(extractElement)
|
|
111
|
+
};
|
|
112
|
+
};
|
|
113
|
+
return extractElement(document.documentElement);
|
|
114
|
+
});
|
|
115
|
+
const vslDocument = {
|
|
116
|
+
vsl_version: "1.0.0",
|
|
117
|
+
canvas: {
|
|
118
|
+
viewport: {
|
|
119
|
+
width: domTree.rect ? domTree.rect.width : 1280,
|
|
120
|
+
height: domTree.rect ? domTree.rect.height : 800,
|
|
121
|
+
unit: "px"
|
|
122
|
+
},
|
|
123
|
+
background: "#ffffff",
|
|
124
|
+
scale: 1,
|
|
125
|
+
orientation: "landscape",
|
|
126
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
127
|
+
},
|
|
128
|
+
objects: [
|
|
129
|
+
{
|
|
130
|
+
id: "root_0",
|
|
131
|
+
t: "container",
|
|
132
|
+
p: [0, 0],
|
|
133
|
+
s: [1280, 800],
|
|
134
|
+
raw_dom: domTree
|
|
135
|
+
}
|
|
136
|
+
]
|
|
137
|
+
};
|
|
138
|
+
session.setSnapshot(vslDocument);
|
|
139
|
+
return {
|
|
140
|
+
status: "success",
|
|
141
|
+
data: vslDocument
|
|
142
|
+
};
|
|
143
|
+
} catch (error) {
|
|
144
|
+
return {
|
|
145
|
+
status: "error",
|
|
146
|
+
error: `vsl_get_snapshot failed: ${error instanceof Error ? error.message : String(error)}`
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// src/tools/getDiff.ts
|
|
152
|
+
async function handleGetDiff(session) {
|
|
153
|
+
try {
|
|
154
|
+
if (!session.hasSnapshot()) {
|
|
155
|
+
return {
|
|
156
|
+
status: "error",
|
|
157
|
+
error: "No snapshot available. Call vsl_get_snapshot first."
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
const diff = session.getDiff();
|
|
161
|
+
if (!diff) {
|
|
162
|
+
return {
|
|
163
|
+
status: "error",
|
|
164
|
+
error: "No previous snapshot to compare against. This is the first snapshot."
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
return {
|
|
168
|
+
status: "success",
|
|
169
|
+
data: diff
|
|
170
|
+
};
|
|
171
|
+
} catch (error) {
|
|
172
|
+
return {
|
|
173
|
+
status: "error",
|
|
174
|
+
error: `vsl_get_diff failed: ${error instanceof Error ? error.message : String(error)}`
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// src/tools/navigate.ts
|
|
180
|
+
async function handleNavigate(args, browser) {
|
|
181
|
+
try {
|
|
182
|
+
if (!args.url || typeof args.url !== "string") {
|
|
183
|
+
return {
|
|
184
|
+
status: "error",
|
|
185
|
+
error: "URL is required"
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
try {
|
|
189
|
+
new URL(args.url);
|
|
190
|
+
} catch {
|
|
191
|
+
return {
|
|
192
|
+
status: "error",
|
|
193
|
+
error: `Invalid URL: ${args.url}`
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
const isAvailable = await browser.isAvailable();
|
|
197
|
+
if (!isAvailable) {
|
|
198
|
+
return {
|
|
199
|
+
status: "error",
|
|
200
|
+
error: "Playwright is not installed. Install it with: npm install playwright"
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
await browser.navigate(args.url);
|
|
204
|
+
const title = await browser.evaluate(() => document.title);
|
|
205
|
+
return {
|
|
206
|
+
status: "success",
|
|
207
|
+
data: {
|
|
208
|
+
url: args.url,
|
|
209
|
+
title
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
} catch (error) {
|
|
213
|
+
return {
|
|
214
|
+
status: "error",
|
|
215
|
+
error: `vsl_navigate failed: ${error instanceof Error ? error.message : String(error)}`
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// src/tools/clearCache.ts
|
|
221
|
+
async function handleClearCache(session) {
|
|
222
|
+
try {
|
|
223
|
+
session.clear();
|
|
224
|
+
return {
|
|
225
|
+
status: "success",
|
|
226
|
+
data: {
|
|
227
|
+
message: "Cache cleared. Next vsl_get_snapshot will create a new snapshot from scratch."
|
|
228
|
+
}
|
|
229
|
+
};
|
|
230
|
+
} catch (error) {
|
|
231
|
+
return {
|
|
232
|
+
status: "error",
|
|
233
|
+
error: `vsl_clear_cache failed: ${error instanceof Error ? error.message : String(error)}`
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// src/tools/executeAction.ts
|
|
239
|
+
var VALID_ACTIONS = [
|
|
240
|
+
"click",
|
|
241
|
+
"type",
|
|
242
|
+
"scroll",
|
|
243
|
+
"select",
|
|
244
|
+
"hover",
|
|
245
|
+
"focus",
|
|
246
|
+
"blur",
|
|
247
|
+
"check",
|
|
248
|
+
"uncheck",
|
|
249
|
+
"press",
|
|
250
|
+
"download"
|
|
251
|
+
];
|
|
252
|
+
async function handleExecuteAction(args, browser, session) {
|
|
253
|
+
try {
|
|
254
|
+
if (!args.action || typeof args.action !== "string") {
|
|
255
|
+
return {
|
|
256
|
+
status: "error",
|
|
257
|
+
error: "action is required"
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
if (!args.target_id || typeof args.target_id !== "string") {
|
|
261
|
+
return {
|
|
262
|
+
status: "error",
|
|
263
|
+
error: "target_id is required"
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
if (!VALID_ACTIONS.includes(args.action)) {
|
|
267
|
+
return {
|
|
268
|
+
status: "error",
|
|
269
|
+
error: `Unknown action: ${args.action}. Valid actions: ${VALID_ACTIONS.join(", ")}`
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
if (!session.hasSnapshot()) {
|
|
273
|
+
return {
|
|
274
|
+
status: "error",
|
|
275
|
+
error: "No snapshot available. Call vsl_get_snapshot first."
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
const isAvailable = await browser.isAvailable();
|
|
279
|
+
if (!isAvailable) {
|
|
280
|
+
return {
|
|
281
|
+
status: "error",
|
|
282
|
+
error: "Playwright is not installed. Install it with: npm install playwright"
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
const snapshot = session.getSnapshot();
|
|
286
|
+
const snapshotUrl = snapshot.canvas.url;
|
|
287
|
+
if (snapshotUrl) {
|
|
288
|
+
const currentUrl = await browser.evaluate(() => window.location.href);
|
|
289
|
+
if (currentUrl !== snapshotUrl) {
|
|
290
|
+
await browser.navigate(snapshotUrl);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
const selector = `[data-vsl-id="${args.target_id}"], #${args.target_id}, .${args.target_id}`;
|
|
294
|
+
switch (args.action) {
|
|
295
|
+
case "click":
|
|
296
|
+
await browser.evaluate((sel) => {
|
|
297
|
+
const el = document.querySelector(sel);
|
|
298
|
+
if (el) el.click();
|
|
299
|
+
}, selector);
|
|
300
|
+
break;
|
|
301
|
+
case "type":
|
|
302
|
+
if (!args.value) {
|
|
303
|
+
return {
|
|
304
|
+
status: "error",
|
|
305
|
+
error: "value is required for type action"
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
await browser.evaluate((sel, val) => {
|
|
309
|
+
const el = document.querySelector(sel);
|
|
310
|
+
if (el) el.value = val;
|
|
311
|
+
}, selector, args.value);
|
|
312
|
+
break;
|
|
313
|
+
case "scroll":
|
|
314
|
+
await browser.evaluate((val) => {
|
|
315
|
+
if (val === "down") {
|
|
316
|
+
window.scrollBy(0, 500);
|
|
317
|
+
} else if (val === "up") {
|
|
318
|
+
window.scrollBy(0, -500);
|
|
319
|
+
}
|
|
320
|
+
}, args.value || "down");
|
|
321
|
+
break;
|
|
322
|
+
case "select":
|
|
323
|
+
if (!args.value) {
|
|
324
|
+
return {
|
|
325
|
+
status: "error",
|
|
326
|
+
error: "value is required for select action"
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
await browser.evaluate((sel, val) => {
|
|
330
|
+
const el = document.querySelector(sel);
|
|
331
|
+
if (el) el.value = val;
|
|
332
|
+
}, selector, args.value);
|
|
333
|
+
break;
|
|
334
|
+
case "hover":
|
|
335
|
+
await browser.evaluate((sel) => {
|
|
336
|
+
const el = document.querySelector(sel);
|
|
337
|
+
if (el) {
|
|
338
|
+
const event = new MouseEvent("mouseover", { bubbles: true });
|
|
339
|
+
el.dispatchEvent(event);
|
|
340
|
+
}
|
|
341
|
+
}, selector);
|
|
342
|
+
break;
|
|
343
|
+
case "focus":
|
|
344
|
+
await browser.evaluate((sel) => {
|
|
345
|
+
const el = document.querySelector(sel);
|
|
346
|
+
if (el) el.focus();
|
|
347
|
+
}, selector);
|
|
348
|
+
break;
|
|
349
|
+
case "blur":
|
|
350
|
+
await browser.evaluate((sel) => {
|
|
351
|
+
const el = document.querySelector(sel);
|
|
352
|
+
if (el) el.blur();
|
|
353
|
+
}, selector);
|
|
354
|
+
break;
|
|
355
|
+
case "download": {
|
|
356
|
+
if (args.value) {
|
|
357
|
+
const downloadId = await browser.evaluate((url) => {
|
|
358
|
+
const a = document.createElement("a");
|
|
359
|
+
a.href = url;
|
|
360
|
+
a.download = "";
|
|
361
|
+
document.body.appendChild(a);
|
|
362
|
+
a.click();
|
|
363
|
+
document.body.removeChild(a);
|
|
364
|
+
const id = `download_${Date.now()}_${Math.random().toString(36).slice(2)}`;
|
|
365
|
+
return id;
|
|
366
|
+
}, args.value);
|
|
367
|
+
const filename = args.value.split("/").pop() || "download";
|
|
368
|
+
return {
|
|
369
|
+
status: "success",
|
|
370
|
+
data: {
|
|
371
|
+
action: args.action,
|
|
372
|
+
target_id: args.target_id,
|
|
373
|
+
success: true,
|
|
374
|
+
download: {
|
|
375
|
+
downloadId,
|
|
376
|
+
filename,
|
|
377
|
+
url: args.value,
|
|
378
|
+
status: "pending"
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
};
|
|
382
|
+
}
|
|
383
|
+
await browser.evaluate((sel) => {
|
|
384
|
+
const el = document.querySelector(sel);
|
|
385
|
+
if (el) el.click();
|
|
386
|
+
}, selector);
|
|
387
|
+
break;
|
|
388
|
+
}
|
|
389
|
+
default:
|
|
390
|
+
return {
|
|
391
|
+
status: "error",
|
|
392
|
+
error: `Action ${args.action} is not yet implemented`
|
|
393
|
+
};
|
|
394
|
+
}
|
|
395
|
+
return {
|
|
396
|
+
status: "success",
|
|
397
|
+
data: {
|
|
398
|
+
action: args.action,
|
|
399
|
+
target_id: args.target_id,
|
|
400
|
+
success: true
|
|
401
|
+
}
|
|
402
|
+
};
|
|
403
|
+
} catch (error) {
|
|
404
|
+
return {
|
|
405
|
+
status: "error",
|
|
406
|
+
error: `vsl_execute_action failed: ${error instanceof Error ? error.message : String(error)}`
|
|
407
|
+
};
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
// src/tools/getVisual.ts
|
|
412
|
+
async function handleGetVisual(args, browser) {
|
|
413
|
+
try {
|
|
414
|
+
if (!args.element_id || typeof args.element_id !== "string") {
|
|
415
|
+
return {
|
|
416
|
+
status: "error",
|
|
417
|
+
error: "element_id is required"
|
|
418
|
+
};
|
|
419
|
+
}
|
|
420
|
+
const isAvailable = await browser.isAvailable();
|
|
421
|
+
if (!isAvailable) {
|
|
422
|
+
return {
|
|
423
|
+
status: "error",
|
|
424
|
+
error: "Playwright is not installed. Install it with: npm install playwright"
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
const selector = `[data-vsl-id="${args.element_id}"], #${args.element_id}, .${args.element_id}`;
|
|
428
|
+
const elementExists = await browser.evaluate((sel) => {
|
|
429
|
+
return document.querySelector(sel) !== null;
|
|
430
|
+
}, selector);
|
|
431
|
+
if (!elementExists) {
|
|
432
|
+
return {
|
|
433
|
+
status: "error",
|
|
434
|
+
error: `Element not found: ${args.element_id}`
|
|
435
|
+
};
|
|
436
|
+
}
|
|
437
|
+
const page = await browser.getPage();
|
|
438
|
+
const rect = await browser.evaluate((sel) => {
|
|
439
|
+
const el = document.querySelector(sel);
|
|
440
|
+
if (!el) return null;
|
|
441
|
+
const r = el.getBoundingClientRect();
|
|
442
|
+
return { x: r.x, y: r.y, width: r.width, height: r.height };
|
|
443
|
+
}, selector);
|
|
444
|
+
if (!rect) {
|
|
445
|
+
return {
|
|
446
|
+
status: "error",
|
|
447
|
+
error: `Could not get bounding box for element: ${args.element_id}`
|
|
448
|
+
};
|
|
449
|
+
}
|
|
450
|
+
const screenshotBuffer = await page.screenshot({
|
|
451
|
+
type: "png",
|
|
452
|
+
clip: rect
|
|
453
|
+
});
|
|
454
|
+
const base64Image = screenshotBuffer.toString("base64");
|
|
455
|
+
return {
|
|
456
|
+
status: "success",
|
|
457
|
+
data: {
|
|
458
|
+
element_id: args.element_id,
|
|
459
|
+
image: base64Image,
|
|
460
|
+
mediaType: "image/png"
|
|
461
|
+
}
|
|
462
|
+
};
|
|
463
|
+
} catch (error) {
|
|
464
|
+
return {
|
|
465
|
+
status: "error",
|
|
466
|
+
error: `vsl_get_visual failed: ${error instanceof Error ? error.message : String(error)}`
|
|
467
|
+
};
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
// src/tools/httpExtractor.ts
|
|
472
|
+
import * as cheerio from "cheerio";
|
|
473
|
+
var SPA_MARKERS = [
|
|
474
|
+
'id="app"',
|
|
475
|
+
'id="root"',
|
|
476
|
+
"data-reactroot",
|
|
477
|
+
"data-vue-root",
|
|
478
|
+
"ng-app"
|
|
479
|
+
];
|
|
480
|
+
var NOISY_SELECTORS = [
|
|
481
|
+
"nav",
|
|
482
|
+
"footer",
|
|
483
|
+
"header",
|
|
484
|
+
'[role="navigation"]',
|
|
485
|
+
'[role="banner"]',
|
|
486
|
+
'[role="contentinfo"]',
|
|
487
|
+
".cookie-banner",
|
|
488
|
+
".cookie-consent",
|
|
489
|
+
"#cookie-banner",
|
|
490
|
+
".ad",
|
|
491
|
+
".advertisement",
|
|
492
|
+
'[aria-hidden="true"]'
|
|
493
|
+
];
|
|
494
|
+
async function extractViaHttp(url, options = {}) {
|
|
495
|
+
const { readable = false, userAgent = "VSL-MCP-Server/1.0 (compatible; AI Agent)", timeout = 1e4 } = options;
|
|
496
|
+
const controller = new AbortController();
|
|
497
|
+
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
498
|
+
let response;
|
|
499
|
+
try {
|
|
500
|
+
response = await fetch(url, {
|
|
501
|
+
headers: {
|
|
502
|
+
"User-Agent": userAgent
|
|
503
|
+
},
|
|
504
|
+
signal: controller.signal
|
|
505
|
+
});
|
|
506
|
+
} catch (error) {
|
|
507
|
+
clearTimeout(timeoutId);
|
|
508
|
+
if (error instanceof Error && error.name === "AbortError") {
|
|
509
|
+
throw new Error(`HTTP request timeout (${timeout}ms): ${url}`);
|
|
510
|
+
}
|
|
511
|
+
throw error;
|
|
512
|
+
} finally {
|
|
513
|
+
clearTimeout(timeoutId);
|
|
514
|
+
}
|
|
515
|
+
if (!response.ok) {
|
|
516
|
+
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
|
517
|
+
}
|
|
518
|
+
const rawHtml = await response.text();
|
|
519
|
+
const isSpa = detectSpa(rawHtml);
|
|
520
|
+
const filteredHtml = readable ? applyReadableFilter(rawHtml) : rawHtml;
|
|
521
|
+
const textContent = extractTextContent(filteredHtml);
|
|
522
|
+
const title = extractTitle(rawHtml);
|
|
523
|
+
const wordCount = countWords(textContent);
|
|
524
|
+
const vslDocument = buildVslFromDom(rawHtml, { url, title });
|
|
525
|
+
return {
|
|
526
|
+
url,
|
|
527
|
+
rawHtml,
|
|
528
|
+
filteredHtml,
|
|
529
|
+
textContent,
|
|
530
|
+
title,
|
|
531
|
+
wordCount,
|
|
532
|
+
isSpa,
|
|
533
|
+
readableApplied: readable,
|
|
534
|
+
vslDocument
|
|
535
|
+
};
|
|
536
|
+
}
|
|
537
|
+
function detectSpa(html) {
|
|
538
|
+
return SPA_MARKERS.some((marker) => html.includes(marker));
|
|
539
|
+
}
|
|
540
|
+
function applyReadableFilter(html) {
|
|
541
|
+
let filtered = html;
|
|
542
|
+
for (const selector of NOISY_SELECTORS) {
|
|
543
|
+
const tagName = selector.replace(/^[.#\[]+/, "").replace(/[\]"]$/g, "");
|
|
544
|
+
const regex = new RegExp(`<${tagName}[^>]*>.*?</${tagName}>`, "gis");
|
|
545
|
+
filtered = filtered.replace(regex, "");
|
|
546
|
+
}
|
|
547
|
+
return filtered;
|
|
548
|
+
}
|
|
549
|
+
function extractTextContent(html) {
|
|
550
|
+
let text = html.replace(/<script[^>]*>.*?<\/script>/gis, "");
|
|
551
|
+
text = text.replace(/<style[^>]*>.*?<\/style>/gis, "");
|
|
552
|
+
text = text.replace(/<[^>]+>/g, " ");
|
|
553
|
+
text = text.replace(/\s+/g, " ").trim();
|
|
554
|
+
return text;
|
|
555
|
+
}
|
|
556
|
+
function extractTitle(html) {
|
|
557
|
+
const match = html.match(/<title[^>]*>(.*?)<\/title>/i);
|
|
558
|
+
return match?.[1]?.trim();
|
|
559
|
+
}
|
|
560
|
+
function countWords(text) {
|
|
561
|
+
return text.split(/\s+/).filter((word) => word.length > 0).length;
|
|
562
|
+
}
|
|
563
|
+
function buildVslFromDom(html, options = {}) {
|
|
564
|
+
const $ = cheerio.load(html);
|
|
565
|
+
$("script, style, link, meta, noscript, template, head, title, base").remove();
|
|
566
|
+
$('[aria-hidden="true"]').remove();
|
|
567
|
+
const title = options.title || $("title").text().trim() || void 0;
|
|
568
|
+
const objects = [];
|
|
569
|
+
const textBlocks = /* @__PURE__ */ new Map();
|
|
570
|
+
let textBlockCounter = 0;
|
|
571
|
+
const LAZY_TEXT_THRESHOLD = 200;
|
|
572
|
+
const LAZY_TEXT_PREVIEW_LENGTH = 50;
|
|
573
|
+
function extractElement(element) {
|
|
574
|
+
if (!element || element.type !== "tag") return null;
|
|
575
|
+
const $el = $(element);
|
|
576
|
+
const tag = element.tagName?.toLowerCase() || "div";
|
|
577
|
+
const style = $el.attr("style") || "";
|
|
578
|
+
if (style.includes("display: none") || style.includes("display:none")) {
|
|
579
|
+
return null;
|
|
580
|
+
}
|
|
581
|
+
const role = $el.attr("role");
|
|
582
|
+
const ariaLabel = $el.attr("aria-label");
|
|
583
|
+
const ariaPressed = $el.attr("aria-pressed");
|
|
584
|
+
const ariaExpanded = $el.attr("aria-expanded");
|
|
585
|
+
const ariaDisabled = $el.attr("aria-disabled");
|
|
586
|
+
const type = determineElementType(tag, role, $el);
|
|
587
|
+
let state;
|
|
588
|
+
if (ariaDisabled === "true") state = "disabled";
|
|
589
|
+
else if (ariaExpanded === "true") state = "expanded";
|
|
590
|
+
else if (ariaPressed === "true") state = "pressed";
|
|
591
|
+
const text = ariaLabel || $el.clone().children().remove().end().text().trim();
|
|
592
|
+
const children = [];
|
|
593
|
+
$el.children().each((_, child) => {
|
|
594
|
+
const childObj = extractElement(child);
|
|
595
|
+
if (childObj) children.push(childObj);
|
|
596
|
+
});
|
|
597
|
+
const obj = {
|
|
598
|
+
id: `${tag}_${objects.length}`,
|
|
599
|
+
t: type,
|
|
600
|
+
p: null,
|
|
601
|
+
// HTTP-путь: нет координат
|
|
602
|
+
s: null
|
|
603
|
+
// HTTP-путь: нет размеров
|
|
604
|
+
};
|
|
605
|
+
if (role) obj.r = role;
|
|
606
|
+
if (state) obj.st = state;
|
|
607
|
+
if (text) {
|
|
608
|
+
if (text.length > LAZY_TEXT_THRESHOLD) {
|
|
609
|
+
obj.txt_preview = text.slice(0, LAZY_TEXT_PREVIEW_LENGTH) + "\u2026";
|
|
610
|
+
const refId = `tb_${String(textBlockCounter++).padStart(3, "0")}`;
|
|
611
|
+
obj.txt_ref = refId;
|
|
612
|
+
textBlocks.set(refId, text);
|
|
613
|
+
} else {
|
|
614
|
+
obj.txt = text;
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
const actions = determineActions(type, state, $el);
|
|
618
|
+
if (actions) obj.act = actions;
|
|
619
|
+
if (children.length > 0) obj.ch = children;
|
|
620
|
+
const attributes = {};
|
|
621
|
+
for (const [key, value] of Object.entries($el.attr() || {})) {
|
|
622
|
+
if (key.startsWith("data-") || key.startsWith("aria-")) {
|
|
623
|
+
attributes[key] = value;
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
if (Object.keys(attributes).length > 0) {
|
|
627
|
+
obj.attributes = attributes;
|
|
628
|
+
}
|
|
629
|
+
return obj;
|
|
630
|
+
}
|
|
631
|
+
$("body").children().each((_, child) => {
|
|
632
|
+
const obj = extractElement(child);
|
|
633
|
+
if (obj) objects.push(obj);
|
|
634
|
+
});
|
|
635
|
+
return {
|
|
636
|
+
vsl_version: "1.0.0",
|
|
637
|
+
canvas: {
|
|
638
|
+
viewport: { width: 1280, height: 800, unit: "px" },
|
|
639
|
+
background: "#ffffff",
|
|
640
|
+
scale: 1,
|
|
641
|
+
orientation: "landscape",
|
|
642
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
643
|
+
url: options.url,
|
|
644
|
+
title
|
|
645
|
+
},
|
|
646
|
+
objects,
|
|
647
|
+
text_blocks: textBlocks.size > 0 ? Object.fromEntries(textBlocks) : void 0
|
|
648
|
+
};
|
|
649
|
+
}
|
|
650
|
+
function determineElementType(tag, role, $el) {
|
|
651
|
+
if (role) {
|
|
652
|
+
const roleMap = {
|
|
653
|
+
button: "button",
|
|
654
|
+
link: "link",
|
|
655
|
+
input: "input",
|
|
656
|
+
checkbox: "input",
|
|
657
|
+
radio: "input",
|
|
658
|
+
textbox: "input",
|
|
659
|
+
select: "select",
|
|
660
|
+
textarea: "textarea",
|
|
661
|
+
modal: "modal",
|
|
662
|
+
dialog: "modal",
|
|
663
|
+
tab: "tab",
|
|
664
|
+
tabpanel: "container",
|
|
665
|
+
navigation: "container",
|
|
666
|
+
banner: "container",
|
|
667
|
+
contentinfo: "container",
|
|
668
|
+
main: "container"
|
|
669
|
+
};
|
|
670
|
+
return roleMap[role] || "container";
|
|
671
|
+
}
|
|
672
|
+
const tagMap = {
|
|
673
|
+
button: "button",
|
|
674
|
+
a: "link",
|
|
675
|
+
input: "input",
|
|
676
|
+
select: "select",
|
|
677
|
+
textarea: "textarea",
|
|
678
|
+
form: "container",
|
|
679
|
+
nav: "container",
|
|
680
|
+
header: "container",
|
|
681
|
+
footer: "container",
|
|
682
|
+
main: "container",
|
|
683
|
+
article: "container",
|
|
684
|
+
section: "container",
|
|
685
|
+
div: "container",
|
|
686
|
+
span: "container",
|
|
687
|
+
p: "text",
|
|
688
|
+
h1: "heading",
|
|
689
|
+
h2: "heading",
|
|
690
|
+
h3: "heading",
|
|
691
|
+
h4: "heading",
|
|
692
|
+
h5: "heading",
|
|
693
|
+
h6: "heading",
|
|
694
|
+
img: "image",
|
|
695
|
+
video: "media",
|
|
696
|
+
audio: "media"
|
|
697
|
+
};
|
|
698
|
+
const baseType = tagMap[tag] || "container";
|
|
699
|
+
const className = $el.attr("class") || "";
|
|
700
|
+
if (className.includes("btn") || className.includes("button")) return "button";
|
|
701
|
+
if (className.includes("modal") || className.includes("dialog")) return "modal";
|
|
702
|
+
if (className.includes("tab")) return "tab";
|
|
703
|
+
if (tag === "input") {
|
|
704
|
+
const inputType = $el.attr("type") || "text";
|
|
705
|
+
if (inputType === "checkbox" || inputType === "radio") return "input";
|
|
706
|
+
return "input";
|
|
707
|
+
}
|
|
708
|
+
return baseType;
|
|
709
|
+
}
|
|
710
|
+
function determineActions(type, state, $el) {
|
|
711
|
+
if (state === "disabled") return void 0;
|
|
712
|
+
switch (type) {
|
|
713
|
+
case "button":
|
|
714
|
+
case "link":
|
|
715
|
+
case "tab":
|
|
716
|
+
return ["click"];
|
|
717
|
+
case "modal":
|
|
718
|
+
return ["close"];
|
|
719
|
+
case "input": {
|
|
720
|
+
const inputType = $el.attr("type") || "text";
|
|
721
|
+
if (inputType === "checkbox" || inputType === "radio") {
|
|
722
|
+
return ["click", "check", "uncheck"];
|
|
723
|
+
}
|
|
724
|
+
return ["click", "type", "clear"];
|
|
725
|
+
}
|
|
726
|
+
case "select":
|
|
727
|
+
return ["click", "select"];
|
|
728
|
+
case "textarea":
|
|
729
|
+
return ["click", "type", "clear"];
|
|
730
|
+
default:
|
|
731
|
+
return void 0;
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
// src/tools/readPage.ts
|
|
736
|
+
async function handleReadPage(args, browser, session, _config) {
|
|
737
|
+
try {
|
|
738
|
+
const readable = args.readable ?? false;
|
|
739
|
+
let detectedMode = "http";
|
|
740
|
+
let httpResult = null;
|
|
741
|
+
try {
|
|
742
|
+
httpResult = await extractViaHttp(args.url, { readable });
|
|
743
|
+
if (httpResult.isSpa) {
|
|
744
|
+
detectedMode = "render";
|
|
745
|
+
}
|
|
746
|
+
} catch {
|
|
747
|
+
detectedMode = "render";
|
|
748
|
+
}
|
|
749
|
+
if (detectedMode === "render") {
|
|
750
|
+
const isAvailable = await browser.isAvailable();
|
|
751
|
+
if (!isAvailable) {
|
|
752
|
+
return {
|
|
753
|
+
status: "error",
|
|
754
|
+
error: "Playwright is not installed. Install it with: npm install playwright\nStatic pages can still be read via HTTP path (no browser required)."
|
|
755
|
+
};
|
|
756
|
+
}
|
|
757
|
+
await browser.navigate(args.url);
|
|
758
|
+
const html = await browser.getContent();
|
|
759
|
+
const isSpa = detectSpa(html);
|
|
760
|
+
const vslDocument = await extractVslFromPage(browser);
|
|
761
|
+
const hadPreviousSnapshot2 = session.hasSnapshot();
|
|
762
|
+
session.setSnapshot(vslDocument);
|
|
763
|
+
const hasDiff2 = hadPreviousSnapshot2;
|
|
764
|
+
const readableHtml = readable ? applyReadableFilter(html) : html;
|
|
765
|
+
const content = extractTextContent(readableHtml);
|
|
766
|
+
return {
|
|
767
|
+
status: "success",
|
|
768
|
+
data: {
|
|
769
|
+
url: args.url,
|
|
770
|
+
mode: "render",
|
|
771
|
+
content,
|
|
772
|
+
vslDocument,
|
|
773
|
+
hasDiff: hasDiff2,
|
|
774
|
+
metadata: {
|
|
775
|
+
title: extractTitle(html),
|
|
776
|
+
wordCount: countWords(content),
|
|
777
|
+
isSpa,
|
|
778
|
+
readableApplied: readable
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
};
|
|
782
|
+
}
|
|
783
|
+
if (!httpResult) {
|
|
784
|
+
throw new Error("HTTP extraction failed unexpectedly");
|
|
785
|
+
}
|
|
786
|
+
const hadPreviousSnapshot = session.hasSnapshot();
|
|
787
|
+
session.setSnapshot(httpResult.vslDocument);
|
|
788
|
+
const hasDiff = hadPreviousSnapshot;
|
|
789
|
+
return {
|
|
790
|
+
status: "success",
|
|
791
|
+
data: {
|
|
792
|
+
url: args.url,
|
|
793
|
+
mode: "http",
|
|
794
|
+
content: httpResult.textContent,
|
|
795
|
+
vslDocument: httpResult.vslDocument,
|
|
796
|
+
hasDiff,
|
|
797
|
+
metadata: {
|
|
798
|
+
title: httpResult.title,
|
|
799
|
+
wordCount: httpResult.wordCount,
|
|
800
|
+
isSpa: httpResult.isSpa,
|
|
801
|
+
readableApplied: httpResult.readableApplied
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
};
|
|
805
|
+
} catch (error) {
|
|
806
|
+
return {
|
|
807
|
+
status: "error",
|
|
808
|
+
error: `vsl_read_page failed: ${error instanceof Error ? error.message : String(error)}`
|
|
809
|
+
};
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
async function extractVslFromPage(browser) {
|
|
813
|
+
const domTree = await browser.evaluate(() => {
|
|
814
|
+
const extractElement = (el) => {
|
|
815
|
+
const rect = el.getBoundingClientRect();
|
|
816
|
+
const computedStyle = window.getComputedStyle(el);
|
|
817
|
+
return {
|
|
818
|
+
tag: el.tagName.toLowerCase(),
|
|
819
|
+
id: el.id || void 0,
|
|
820
|
+
className: el.className || void 0,
|
|
821
|
+
text: el.textContent?.trim().substring(0, 100) || void 0,
|
|
822
|
+
rect: {
|
|
823
|
+
x: rect.x,
|
|
824
|
+
y: rect.y,
|
|
825
|
+
width: rect.width,
|
|
826
|
+
height: rect.height
|
|
827
|
+
},
|
|
828
|
+
role: el.getAttribute("role") || void 0,
|
|
829
|
+
ariaLabel: el.getAttribute("aria-label") || void 0,
|
|
830
|
+
ariaHidden: el.getAttribute("aria-hidden") || void 0,
|
|
831
|
+
display: computedStyle.display,
|
|
832
|
+
visibility: computedStyle.visibility,
|
|
833
|
+
children: Array.from(el.children).filter((child) => {
|
|
834
|
+
const style = window.getComputedStyle(child);
|
|
835
|
+
return style.display !== "none" && style.visibility !== "hidden";
|
|
836
|
+
}).map(extractElement)
|
|
837
|
+
};
|
|
838
|
+
};
|
|
839
|
+
return extractElement(document.documentElement);
|
|
840
|
+
});
|
|
841
|
+
return {
|
|
842
|
+
vsl_version: "1.0.0",
|
|
843
|
+
canvas: {
|
|
844
|
+
viewport: {
|
|
845
|
+
width: domTree.rect ? domTree.rect.width : 1280,
|
|
846
|
+
height: domTree.rect ? domTree.rect.height : 800,
|
|
847
|
+
unit: "px"
|
|
848
|
+
},
|
|
849
|
+
background: "#ffffff",
|
|
850
|
+
scale: 1,
|
|
851
|
+
orientation: "landscape",
|
|
852
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
853
|
+
},
|
|
854
|
+
objects: [
|
|
855
|
+
{
|
|
856
|
+
id: "root_0",
|
|
857
|
+
t: "container",
|
|
858
|
+
p: [0, 0],
|
|
859
|
+
s: [1280, 800],
|
|
860
|
+
raw_dom: domTree
|
|
861
|
+
}
|
|
862
|
+
]
|
|
863
|
+
};
|
|
864
|
+
}
|
|
865
|
+
|
|
866
|
+
// src/tools/getFullJson.ts
|
|
867
|
+
async function handleGetFullJson(_args, session) {
|
|
868
|
+
try {
|
|
869
|
+
if (!session.hasSnapshot()) {
|
|
870
|
+
return {
|
|
871
|
+
status: "error",
|
|
872
|
+
error: "No VSL snapshot available",
|
|
873
|
+
hint: "Call vsl_get_snapshot first to capture the current page state."
|
|
874
|
+
};
|
|
875
|
+
}
|
|
876
|
+
const snapshot = session.getSnapshot();
|
|
877
|
+
return {
|
|
878
|
+
status: "success",
|
|
879
|
+
data: snapshot
|
|
880
|
+
};
|
|
881
|
+
} catch (error) {
|
|
882
|
+
return {
|
|
883
|
+
status: "error",
|
|
884
|
+
error: `vsl_get_full_json failed: ${error instanceof Error ? error.message : String(error)}`
|
|
885
|
+
};
|
|
886
|
+
}
|
|
887
|
+
}
|
|
888
|
+
|
|
889
|
+
// src/tools/getTextBlock.ts
|
|
890
|
+
async function handleGetTextBlock(args, session) {
|
|
891
|
+
try {
|
|
892
|
+
if (!args.block_id || typeof args.block_id !== "string") {
|
|
893
|
+
return {
|
|
894
|
+
status: "error",
|
|
895
|
+
error: "block_id is required and must be a string"
|
|
896
|
+
};
|
|
897
|
+
}
|
|
898
|
+
if (!session.hasSnapshot()) {
|
|
899
|
+
return {
|
|
900
|
+
status: "error",
|
|
901
|
+
error: "No VSL snapshot available. Call vsl_get_snapshot first."
|
|
902
|
+
};
|
|
903
|
+
}
|
|
904
|
+
const snapshot = session.getSnapshot();
|
|
905
|
+
if (!snapshot.text_blocks) {
|
|
906
|
+
return {
|
|
907
|
+
status: "error",
|
|
908
|
+
error: "No text blocks in current snapshot. The document may not contain long texts."
|
|
909
|
+
};
|
|
910
|
+
}
|
|
911
|
+
const fullText = snapshot.text_blocks[args.block_id];
|
|
912
|
+
if (fullText === void 0) {
|
|
913
|
+
return {
|
|
914
|
+
status: "error",
|
|
915
|
+
error: `Text block not found: ${args.block_id}`
|
|
916
|
+
};
|
|
917
|
+
}
|
|
918
|
+
return {
|
|
919
|
+
status: "success",
|
|
920
|
+
data: {
|
|
921
|
+
block_id: args.block_id,
|
|
922
|
+
text: fullText
|
|
923
|
+
}
|
|
924
|
+
};
|
|
925
|
+
} catch (error) {
|
|
926
|
+
return {
|
|
927
|
+
status: "error",
|
|
928
|
+
error: `vsl_get_text_block failed: ${error instanceof Error ? error.message : String(error)}`
|
|
929
|
+
};
|
|
930
|
+
}
|
|
931
|
+
}
|
|
932
|
+
|
|
933
|
+
// src/tools/download.ts
|
|
934
|
+
import path from "path";
|
|
935
|
+
async function handleDownload(args, browser, session) {
|
|
936
|
+
try {
|
|
937
|
+
if (!args.target_id && !args.value) {
|
|
938
|
+
return {
|
|
939
|
+
status: "error",
|
|
940
|
+
error: "Either target_id (element to click) or value (URL to download) is required"
|
|
941
|
+
};
|
|
942
|
+
}
|
|
943
|
+
if (args.target_id && !session.hasSnapshot()) {
|
|
944
|
+
return {
|
|
945
|
+
status: "error",
|
|
946
|
+
error: "No snapshot available. Call vsl_get_snapshot first."
|
|
947
|
+
};
|
|
948
|
+
}
|
|
949
|
+
const isAvailable = await browser.isAvailable();
|
|
950
|
+
if (!isAvailable) {
|
|
951
|
+
return {
|
|
952
|
+
status: "error",
|
|
953
|
+
error: "Playwright is not installed. Install it with: npm install playwright"
|
|
954
|
+
};
|
|
955
|
+
}
|
|
956
|
+
if (session.hasSnapshot()) {
|
|
957
|
+
const snapshot = session.getSnapshot();
|
|
958
|
+
const snapshotUrl = snapshot.canvas.url;
|
|
959
|
+
if (snapshotUrl) {
|
|
960
|
+
const currentUrl = await browser.evaluate(() => window.location.href);
|
|
961
|
+
if (currentUrl !== snapshotUrl) {
|
|
962
|
+
await browser.navigate(snapshotUrl);
|
|
963
|
+
}
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
let downloadUrl;
|
|
967
|
+
if (args.value) {
|
|
968
|
+
downloadUrl = args.value;
|
|
969
|
+
await browser.evaluate((url) => {
|
|
970
|
+
const a = document.createElement("a");
|
|
971
|
+
a.href = url;
|
|
972
|
+
a.download = "";
|
|
973
|
+
document.body.appendChild(a);
|
|
974
|
+
a.click();
|
|
975
|
+
document.body.removeChild(a);
|
|
976
|
+
}, args.value);
|
|
977
|
+
} else if (args.target_id) {
|
|
978
|
+
const selector = `[data-vsl-id="${args.target_id}"], #${args.target_id}, .${args.target_id}`;
|
|
979
|
+
downloadUrl = await browser.evaluate((sel) => {
|
|
980
|
+
const el = document.querySelector(sel);
|
|
981
|
+
if (el) {
|
|
982
|
+
let url = "";
|
|
983
|
+
if (el instanceof HTMLAnchorElement && el.href) {
|
|
984
|
+
url = el.href;
|
|
985
|
+
}
|
|
986
|
+
el.click();
|
|
987
|
+
return url;
|
|
988
|
+
}
|
|
989
|
+
return "";
|
|
990
|
+
}, selector);
|
|
991
|
+
if (!downloadUrl) {
|
|
992
|
+
downloadUrl = await browser.evaluate(() => window.location.href);
|
|
993
|
+
}
|
|
994
|
+
} else {
|
|
995
|
+
return {
|
|
996
|
+
status: "error",
|
|
997
|
+
error: "Invalid arguments: provide either target_id or value"
|
|
998
|
+
};
|
|
999
|
+
}
|
|
1000
|
+
const downloads = browser.getDownloads();
|
|
1001
|
+
if (downloads.length === 0) {
|
|
1002
|
+
return {
|
|
1003
|
+
status: "error",
|
|
1004
|
+
error: "Download was not initiated. Check if the element triggers a file download."
|
|
1005
|
+
};
|
|
1006
|
+
}
|
|
1007
|
+
const latestDownload = downloads[downloads.length - 1];
|
|
1008
|
+
const downloadId = latestDownload.downloadId;
|
|
1009
|
+
const timeout = args.timeout;
|
|
1010
|
+
const result = await browser.waitForDownload(downloadId, timeout);
|
|
1011
|
+
if (args.save_path && result.status === "completed") {
|
|
1012
|
+
const resolvedBase = path.resolve((await import("os")).homedir(), ".vsl", "downloads");
|
|
1013
|
+
const resolvedSave = path.resolve(resolvedBase, args.save_path);
|
|
1014
|
+
if (!resolvedSave.startsWith(resolvedBase + path.sep) && resolvedSave !== resolvedBase) {
|
|
1015
|
+
return {
|
|
1016
|
+
status: "error",
|
|
1017
|
+
error: `Path traversal detected: save_path '${args.save_path}' escapes downloads directory`
|
|
1018
|
+
};
|
|
1019
|
+
}
|
|
1020
|
+
await browser.saveDownload(downloadId, args.save_path);
|
|
1021
|
+
result.path = args.save_path;
|
|
1022
|
+
}
|
|
1023
|
+
return {
|
|
1024
|
+
status: "success",
|
|
1025
|
+
data: {
|
|
1026
|
+
downloadId: result.downloadId,
|
|
1027
|
+
filename: result.filename,
|
|
1028
|
+
url: result.url,
|
|
1029
|
+
status: result.status,
|
|
1030
|
+
path: result.path
|
|
1031
|
+
}
|
|
1032
|
+
};
|
|
1033
|
+
} catch (error) {
|
|
1034
|
+
return {
|
|
1035
|
+
status: "error",
|
|
1036
|
+
error: `vsl_download failed: ${error instanceof Error ? error.message : String(error)}`
|
|
1037
|
+
};
|
|
1038
|
+
}
|
|
1039
|
+
}
|
|
1040
|
+
|
|
1041
|
+
// src/tools/index.ts
|
|
1042
|
+
var VSL_TOOLS = [
|
|
1043
|
+
{
|
|
1044
|
+
name: "vsl_get_snapshot",
|
|
1045
|
+
description: "\u041F\u043E\u043B\u0443\u0447\u0438\u0442\u044C \u0442\u0435\u043A\u0443\u0449\u0438\u0439 VSL snapshot \u0441\u0442\u0440\u0430\u043D\u0438\u0446\u044B. \u0412\u043E\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 VSL JSON \u0441 \u0441\u0435\u043C\u0430\u043D\u0442\u0438\u0447\u0435\u0441\u043A\u043E\u0439 \u0441\u0442\u0440\u0443\u043A\u0442\u0443\u0440\u043E\u0439 \u044D\u043B\u0435\u043C\u0435\u043D\u0442\u043E\u0432. VSL (Visual Scene Language) \u2014 \u044D\u0442\u043E JSON-\u043F\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043B\u0435\u043D\u0438\u0435 \u0441\u0442\u0440\u0430\u043D\u0438\u0446\u044B, \u0433\u0434\u0435 \u043A\u0430\u0436\u0434\u044B\u0439 \u044D\u043B\u0435\u043C\u0435\u043D\u0442 \u0438\u043C\u0435\u0435\u0442: id (\u0443\u043D\u0438\u043A\u0430\u043B\u044C\u043D\u044B\u0439 \u0438\u0434\u0435\u043D\u0442\u0438\u0444\u0438\u043A\u0430\u0442\u043E\u0440, \u043D\u0430\u043F\u0440\u0438\u043C\u0435\u0440 btn_123), type (button/input/link/text/image), text (\u0442\u0435\u043A\u0441\u0442\u043E\u0432\u043E\u0435 \u0441\u043E\u0434\u0435\u0440\u0436\u0438\u043C\u043E\u0435), bbox ([x,y,width,height]), children (\u0432\u043B\u043E\u0436\u0435\u043D\u043D\u044B\u0435 \u044D\u043B\u0435\u043C\u0435\u043D\u0442\u044B). \u041F\u0440\u0438\u043C\u0435\u0440: { viewport:{width:1920,height:1080}, objects:[{id:btn_1,type:button,text:\u0412\u043E\u0439\u0442\u0438,bbox:[100,200,80,30]},{id:inp_2,type:input,text:,bbox:[100,250,200,30]}] }. \u0418\u0441\u043F\u043E\u043B\u044C\u0437\u043E\u0432\u0430\u043D\u0438\u0435: \u043F\u043E\u043B\u0443\u0447\u0438\u0442\u0435 snapshot, \u043D\u0430\u0439\u0434\u0438\u0442\u0435 \u043D\u0443\u0436\u043D\u044B\u0439 \u044D\u043B\u0435\u043C\u0435\u043D\u0442 \u043F\u043E id, \u0437\u0430\u0442\u0435\u043C \u0438\u0441\u043F\u043E\u043B\u044C\u0437\u0443\u0439\u0442\u0435 vsl_execute_action \u0434\u043B\u044F \u0432\u0437\u0430\u0438\u043C\u043E\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044F.",
|
|
1046
|
+
inputSchema: {
|
|
1047
|
+
type: "object",
|
|
1048
|
+
properties: {
|
|
1049
|
+
url: {
|
|
1050
|
+
type: "string",
|
|
1051
|
+
description: "URL \u0441\u0442\u0440\u0430\u043D\u0438\u0446\u044B (\u043E\u043F\u0446\u0438\u043E\u043D\u0430\u043B\u044C\u043D\u043E, \u0435\u0441\u043B\u0438 \u043D\u0435 \u0443\u043A\u0430\u0437\u0430\u043D \u2014 \u0438\u0441\u043F\u043E\u043B\u044C\u0437\u0443\u0435\u0442\u0441\u044F \u0442\u0435\u043A\u0443\u0449\u0430\u044F \u0441\u0442\u0440\u0430\u043D\u0438\u0446\u0430)"
|
|
1052
|
+
}
|
|
1053
|
+
}
|
|
1054
|
+
}
|
|
1055
|
+
},
|
|
1056
|
+
{
|
|
1057
|
+
name: "vsl_get_diff",
|
|
1058
|
+
description: "\u041F\u043E\u043B\u0443\u0447\u0438\u0442\u044C \u0442\u043E\u043B\u044C\u043A\u043E \u0438\u0437\u043C\u0435\u043D\u0435\u043D\u0438\u044F \u0441 \u043C\u043E\u043C\u0435\u043D\u0442\u0430 \u043F\u043E\u0441\u043B\u0435\u0434\u043D\u0435\u0433\u043E snapshot. \u0412\u043E\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 Diff JSON \u0441 \u0441\u0435\u043A\u0446\u0438\u044F\u043C\u0438 added (\u043D\u043E\u0432\u044B\u0435 \u043E\u0431\u044A\u0435\u043A\u0442\u044B), modified (\u0438\u0437\u043C\u0435\u043D\u0451\u043D\u043D\u044B\u0435), removed (\u0443\u0434\u0430\u043B\u0451\u043D\u043D\u044B\u0435). \u041F\u0440\u0438\u043C\u0435\u0440: { changes:{ added:[{id:btn_3,type:button,text:\u041E\u0442\u043F\u0440\u0430\u0432\u0438\u0442\u044C,bbox:[300,400,100,30]}], modified:[{id:btn_1,changes:{text:[\u0412\u043E\u0439\u0442\u0438,\u0410\u0432\u0442\u043E\u0440\u0438\u0437\u0430\u0446\u0438\u044F]}}], removed:[{id:inp_2}] } }. \u0418\u0441\u043F\u043E\u043B\u044C\u0437\u0443\u0439\u0442\u0435 \u0434\u043B\u044F \u043E\u0442\u0441\u043B\u0435\u0436\u0438\u0432\u0430\u043D\u0438\u044F \u0438\u0437\u043C\u0435\u043D\u0435\u043D\u0438\u0439 \u043D\u0430 \u0441\u0442\u0440\u0430\u043D\u0438\u0446\u0435 \u043F\u043E\u0441\u043B\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439 (\u043A\u043B\u0438\u043A, \u043D\u0430\u0432\u0438\u0433\u0430\u0446\u0438\u044F, \u0444\u043E\u0440\u043C\u0430).",
|
|
1059
|
+
inputSchema: {
|
|
1060
|
+
type: "object",
|
|
1061
|
+
properties: {}
|
|
1062
|
+
}
|
|
1063
|
+
},
|
|
1064
|
+
{
|
|
1065
|
+
name: "vsl_execute_action",
|
|
1066
|
+
description: "\u0412\u044B\u043F\u043E\u043B\u043D\u0438\u0442\u044C \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435 \u043D\u0430\u0434 \u044D\u043B\u0435\u043C\u0435\u043D\u0442\u043E\u043C VSL. \u041D\u0430\u0439\u0434\u0438\u0442\u0435 \u044D\u043B\u0435\u043C\u0435\u043D\u0442 \u043F\u043E id \u0432 snapshot (vsl_get_snapshot), \u0437\u0430\u0442\u0435\u043C \u0432\u044B\u0437\u043E\u0432\u0438\u0442\u0435 \u044D\u0442\u043E \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435. \u041F\u043E\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u043C\u044B\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044F: click (\u043A\u043B\u0438\u043A \u043F\u043E \u044D\u043B\u0435\u043C\u0435\u043D\u0442\u0443), type (\u0432\u0432\u043E\u0434 \u0442\u0435\u043A\u0441\u0442\u0430, \u0442\u0440\u0435\u0431\u0443\u0435\u0442 value), scroll (\u043F\u0440\u043E\u043A\u0440\u0443\u0442\u043A\u0430), select (\u0432\u044B\u0431\u043E\u0440 \u043E\u043F\u0446\u0438\u0438, \u0442\u0440\u0435\u0431\u0443\u0435\u0442 value), hover, focus, blur, check, uncheck, press (\u043D\u0430\u0436\u0430\u0442\u0438\u0435 \u043A\u043B\u0430\u0432\u0438\u0448\u0438). \u041F\u0440\u0438\u043C\u0435\u0440: {action:click, target_id:btn_1} \u0438\u043B\u0438 {action:type, target_id:inp_2, value:hello@mail.com}.",
|
|
1067
|
+
inputSchema: {
|
|
1068
|
+
type: "object",
|
|
1069
|
+
properties: {
|
|
1070
|
+
action: {
|
|
1071
|
+
type: "string",
|
|
1072
|
+
description: "\u0418\u043C\u044F \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044F (click, type, scroll, select, \u0438 \u0434\u0440.)"
|
|
1073
|
+
},
|
|
1074
|
+
target_id: {
|
|
1075
|
+
type: "string",
|
|
1076
|
+
description: "ID \u044D\u043B\u0435\u043C\u0435\u043D\u0442\u0430-\u0446\u0435\u043B\u0438 \u0432 VSL JSON"
|
|
1077
|
+
},
|
|
1078
|
+
value: {
|
|
1079
|
+
type: "string",
|
|
1080
|
+
description: "\u0417\u043D\u0430\u0447\u0435\u043D\u0438\u0435 \u0434\u043B\u044F \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044F (\u043D\u0430\u043F\u0440\u0438\u043C\u0435\u0440, \u0442\u0435\u043A\u0441\u0442 \u0434\u043B\u044F type)"
|
|
1081
|
+
}
|
|
1082
|
+
},
|
|
1083
|
+
required: ["action", "target_id"]
|
|
1084
|
+
}
|
|
1085
|
+
},
|
|
1086
|
+
{
|
|
1087
|
+
name: "vsl_navigate",
|
|
1088
|
+
description: '\u041F\u0435\u0440\u0435\u0439\u0442\u0438 \u043F\u043E URL \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0435. \u0418\u0441\u043F\u043E\u043B\u044C\u0437\u0443\u0439\u0442\u0435 \u0434\u043B\u044F \u043E\u0442\u043A\u0440\u044B\u0442\u0438\u044F \u043D\u043E\u0432\u043E\u0439 \u0441\u0442\u0440\u0430\u043D\u0438\u0446\u044B \u043F\u0435\u0440\u0435\u0434 \u043F\u043E\u043B\u0443\u0447\u0435\u043D\u0438\u0435\u043C snapshot. \u041F\u0440\u0438\u043C\u0435\u0440: {url:"https://example.com"}. \u0412\u043E\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 {status:"success"} \u0438\u043B\u0438 {status:"error", message:"..."}. \u041F\u043E\u0441\u043B\u0435 \u043D\u0430\u0432\u0438\u0433\u0430\u0446\u0438\u0438 \u0432\u044B\u0437\u043E\u0432\u0438\u0442\u0435 vsl_get_snapshot \u0434\u043B\u044F \u043F\u043E\u043B\u0443\u0447\u0435\u043D\u0438\u044F \u0441\u0442\u0440\u0443\u043A\u0442\u0443\u0440\u044B \u0441\u0442\u0440\u0430\u043D\u0438\u0446\u044B.',
|
|
1089
|
+
inputSchema: {
|
|
1090
|
+
type: "object",
|
|
1091
|
+
properties: {
|
|
1092
|
+
url: {
|
|
1093
|
+
type: "string",
|
|
1094
|
+
description: "URL \u0434\u043B\u044F \u043D\u0430\u0432\u0438\u0433\u0430\u0446\u0438\u0438"
|
|
1095
|
+
}
|
|
1096
|
+
},
|
|
1097
|
+
required: ["url"]
|
|
1098
|
+
}
|
|
1099
|
+
},
|
|
1100
|
+
{
|
|
1101
|
+
name: "vsl_clear_cache",
|
|
1102
|
+
description: "\u0421\u0431\u0440\u043E\u0441\u0438\u0442\u044C \u043A\u044D\u0448 VSL snapshot. \u0418\u0441\u043F\u043E\u043B\u044C\u0437\u0443\u0439\u0442\u0435 \u043A\u043E\u0433\u0434\u0430 \u043D\u0443\u0436\u043D\u043E \u043F\u043E\u043B\u0443\u0447\u0438\u0442\u044C \u0441\u0432\u0435\u0436\u0438\u0439 snapshot \u0441\u0442\u0440\u0430\u043D\u0438\u0446\u044B \u0441 \u043D\u0443\u043B\u044F (\u043D\u0430\u043F\u0440\u0438\u043C\u0435\u0440, \u043F\u043E\u0441\u043B\u0435 \u0437\u043D\u0430\u0447\u0438\u0442\u0435\u043B\u044C\u043D\u044B\u0445 \u0438\u0437\u043C\u0435\u043D\u0435\u043D\u0438\u0439 \u043D\u0430 \u0441\u0442\u0440\u0430\u043D\u0438\u0446\u0435 \u0438\u043B\u0438 \u043F\u0440\u0438 \u043F\u0435\u0440\u0435\u0445\u043E\u0434\u0435 \u043D\u0430 \u0434\u0440\u0443\u0433\u043E\u0439 \u0441\u0430\u0439\u0442). \u041F\u0440\u0438\u043C\u0435\u0440: {}. \u0421\u043B\u0435\u0434\u0443\u044E\u0449\u0438\u0439 \u0432\u044B\u0437\u043E\u0432 vsl_get_snapshot \u0441\u043E\u0437\u0434\u0430\u0441\u0442 \u043D\u043E\u0432\u044B\u0439 snapshot.",
|
|
1103
|
+
inputSchema: {
|
|
1104
|
+
type: "object",
|
|
1105
|
+
properties: {}
|
|
1106
|
+
}
|
|
1107
|
+
},
|
|
1108
|
+
{
|
|
1109
|
+
name: "vsl_get_visual",
|
|
1110
|
+
description: "\u041F\u043E\u043B\u0443\u0447\u0438\u0442\u044C visual fragment (\u0441\u043A\u0440\u0438\u043D\u0448\u043E\u0442) \u044D\u043B\u0435\u043C\u0435\u043D\u0442\u0430 \u0432 \u0444\u043E\u0440\u043C\u0430\u0442\u0435 base64 WebP. \u0418\u0441\u043F\u043E\u043B\u044C\u0437\u0443\u0439\u0442\u0435 \u0434\u043B\u044F \u044D\u043B\u0435\u043C\u0435\u043D\u0442\u043E\u0432, \u043A\u043E\u0442\u043E\u0440\u044B\u0435 \u0441\u043B\u043E\u0436\u043D\u043E \u043A\u043B\u0430\u0441\u0441\u0438\u0444\u0438\u0446\u0438\u0440\u043E\u0432\u0430\u0442\u044C \u043F\u043E \u0442\u0435\u043A\u0441\u0442\u0443 (\u0438\u043A\u043E\u043D\u043A\u0438, \u0433\u0440\u0430\u0444\u0438\u043A\u0438, \u043A\u0430\u0441\u0442\u043E\u043C\u043D\u044B\u0435 \u0432\u0438\u0434\u0436\u0435\u0442\u044B). \u0412\u043E\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 {mediaType:image/webp, data:base64data}. \u041F\u0440\u0438\u043C\u0435\u0440: {element_id: img_5}.",
|
|
1111
|
+
inputSchema: {
|
|
1112
|
+
type: "object",
|
|
1113
|
+
properties: {
|
|
1114
|
+
element_id: {
|
|
1115
|
+
type: "string",
|
|
1116
|
+
description: "ID \u044D\u043B\u0435\u043C\u0435\u043D\u0442\u0430 \u0432 VSL JSON"
|
|
1117
|
+
}
|
|
1118
|
+
},
|
|
1119
|
+
required: ["element_id"]
|
|
1120
|
+
}
|
|
1121
|
+
},
|
|
1122
|
+
{
|
|
1123
|
+
name: "vsl_read_page",
|
|
1124
|
+
description: "\u0413\u0438\u0431\u0440\u0438\u0434\u043D\u043E\u0435 \u0447\u0442\u0435\u043D\u0438\u0435 \u0432\u0435\u0431-\u0441\u0442\u0440\u0430\u043D\u0438\u0446. mode=auto (\u043F\u043E \u0443\u043C\u043E\u043B\u0447\u0430\u043D\u0438\u044E): \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043A\u0438\u0435 \u0441\u0442\u0440\u0430\u043D\u0438\u0446\u044B \u0447\u0438\u0442\u0430\u044E\u0442\u0441\u044F \u0447\u0435\u0440\u0435\u0437 HTTP (\u0431\u044B\u0441\u0442\u0440\u043E), SPA \u0440\u0435\u043D\u0434\u0435\u0440\u044F\u0442\u0441\u044F \u0447\u0435\u0440\u0435\u0437 \u0431\u0440\u0430\u0443\u0437\u0435\u0440. mode=http: \u0442\u043E\u043B\u044C\u043A\u043E HTTP (\u0431\u0435\u0437 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0430). mode=render: \u0432\u0441\u0435\u0433\u0434\u0430 \u0440\u0435\u043D\u0434\u0435\u0440\u0438\u0442\u044C \u0447\u0435\u0440\u0435\u0437 \u0431\u0440\u0430\u0443\u0437\u0435\u0440. readable=true: \u0444\u0438\u043B\u044C\u0442\u0440\u0443\u0435\u0442 \u0448\u0443\u043C (\u043D\u0430\u0432\u0438\u0433\u0430\u0446\u0438\u044F, \u0444\u0443\u0442\u0435\u0440\u044B, cookie-\u0431\u0430\u043D\u043D\u0435\u0440\u044B) \u0434\u043B\u044F \u0447\u0438\u0441\u0442\u043E\u0433\u043E \u043A\u043E\u043D\u0442\u0435\u043D\u0442\u0430. \u041F\u0440\u0438 \u043F\u043E\u0432\u0442\u043E\u0440\u043D\u044B\u0445 \u0432\u044B\u0437\u043E\u0432\u0430\u0445 \u0432\u043E\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 diff (\u0438\u0437\u043C\u0435\u043D\u0435\u043D\u0438\u044F), \u0430 \u043D\u0435 \u043F\u043E\u043B\u043D\u044B\u0439 \u043A\u043E\u043D\u0442\u0435\u043D\u0442. \u041F\u0440\u0438\u043C\u0435\u0440: {url:https://example.com, mode:auto, readable:true}.",
|
|
1125
|
+
inputSchema: {
|
|
1126
|
+
type: "object",
|
|
1127
|
+
properties: {
|
|
1128
|
+
url: {
|
|
1129
|
+
type: "string",
|
|
1130
|
+
description: "URL \u0441\u0442\u0440\u0430\u043D\u0438\u0446\u044B \u0434\u043B\u044F \u0447\u0442\u0435\u043D\u0438\u044F"
|
|
1131
|
+
},
|
|
1132
|
+
mode: {
|
|
1133
|
+
type: "string",
|
|
1134
|
+
enum: ["auto", "http", "render"],
|
|
1135
|
+
description: "\u0420\u0435\u0436\u0438\u043C \u0447\u0442\u0435\u043D\u0438\u044F: auto (\u0430\u0432\u0442\u043E-\u0434\u0435\u0442\u0435\u043A\u0442), http (\u0442\u043E\u043B\u044C\u043A\u043E HTTP), render (\u0432\u0441\u0435\u0433\u0434\u0430 \u0440\u0435\u043D\u0434\u0435\u0440\u0438\u0442\u044C)"
|
|
1136
|
+
},
|
|
1137
|
+
readable: {
|
|
1138
|
+
type: "boolean",
|
|
1139
|
+
description: "Readable-\u0440\u0435\u0436\u0438\u043C: \u0444\u0438\u043B\u044C\u0442\u0440\u0430\u0446\u0438\u044F \u0448\u0443\u043C\u0430 (nav, footer, cookie banners) \u0434\u043B\u044F \u0447\u0438\u0441\u0442\u043E\u0433\u043E \u043A\u043E\u043D\u0442\u0435\u043D\u0442\u0430"
|
|
1140
|
+
}
|
|
1141
|
+
},
|
|
1142
|
+
required: ["url"]
|
|
1143
|
+
}
|
|
1144
|
+
},
|
|
1145
|
+
{
|
|
1146
|
+
name: "vsl_get_full_json",
|
|
1147
|
+
description: "\u041F\u043E\u043B\u0443\u0447\u0438\u0442\u044C \u043F\u043E\u043B\u043D\u044B\u0439 VSL JSON \u0442\u0435\u043A\u0443\u0449\u0435\u0439 \u0441\u0442\u0440\u0430\u043D\u0438\u0446\u044B, \u043C\u0438\u043D\u0443\u044F diff-first \u043B\u043E\u0433\u0438\u043A\u0443. \u0418\u0441\u043F\u043E\u043B\u044C\u0437\u0443\u0439\u0442\u0435 \u043A\u043E\u0433\u0434\u0430: (1) \u0438\u0441\u0442\u043E\u0440\u0438\u044F \u0430\u0433\u0435\u043D\u0442\u0430 \u043E\u0431\u0440\u0435\u0437\u0430\u043B\u0430\u0441\u044C \u0438 \u0432\u044B \u043F\u043E\u0442\u0435\u0440\u044F\u043B\u0438 \u043A\u043E\u043D\u0442\u0435\u043A\u0441\u0442, (2) \u043D\u0443\u0436\u043D\u043E \u0443\u0432\u0438\u0434\u0435\u0442\u044C \u043F\u043E\u043B\u043D\u0443\u044E \u043A\u0430\u0440\u0442\u0438\u043D\u0443 \u0441\u0442\u0440\u0430\u043D\u0438\u0446\u044B, (3) \u0438\u043D\u043A\u0440\u0435\u043C\u0435\u043D\u0442\u0430\u043B\u044C\u043D\u044B\u0435 \u0434\u0438\u0444\u0444\u044B \u043D\u0435 \u0434\u0430\u044E\u0442 \u0434\u043E\u0441\u0442\u0430\u0442\u043E\u0447\u043D\u043E \u0438\u043D\u0444\u043E\u0440\u043C\u0430\u0446\u0438\u0438. \u041F\u0440\u0438\u043C\u0435\u0440: {}. \u0412\u043E\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043F\u043E\u043B\u043D\u044B\u0439 VSL Document \u0441 viewport \u0438 objects.",
|
|
1148
|
+
inputSchema: {
|
|
1149
|
+
type: "object",
|
|
1150
|
+
properties: {}
|
|
1151
|
+
}
|
|
1152
|
+
},
|
|
1153
|
+
{
|
|
1154
|
+
name: "vsl_get_text_block",
|
|
1155
|
+
description: '\u041F\u043E\u043B\u0443\u0447\u0438\u0442\u044C \u043F\u043E\u043B\u043D\u044B\u0439 \u0442\u0435\u043A\u0441\u0442 \u043F\u043E txt_ref \u0438\u0437 lazy text loading (M1.7). \u041A\u043E\u0433\u0434\u0430 \u0442\u0435\u043A\u0441\u0442 \u044D\u043B\u0435\u043C\u0435\u043D\u0442\u0430 > 200 \u0441\u0438\u043C\u0432\u043E\u043B\u043E\u0432, \u0432 VSL JSON \u0445\u0440\u0430\u043D\u0438\u0442\u0441\u044F \u0442\u043E\u043B\u044C\u043A\u043E txt_preview (\u043F\u0435\u0440\u0432\u044B\u0435 ~50 \u0441\u0438\u043C\u0432\u043E\u043B\u043E\u0432) \u0438 txt_ref (\u043D\u0430\u043F\u0440\u0438\u043C\u0435\u0440, tb_001). \u0418\u0441\u043F\u043E\u043B\u044C\u0437\u0443\u0439\u0442\u0435 \u044D\u0442\u043E\u0442 tool \u0434\u043B\u044F \u043F\u043E\u043B\u0443\u0447\u0435\u043D\u0438\u044F \u043F\u043E\u043B\u043D\u043E\u0433\u043E \u0442\u0435\u043A\u0441\u0442\u0430. \u041F\u0440\u0438\u043C\u0435\u0440: {block_id: "tb_001"}. \u0412\u043E\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 {block_id, text} \u0441 \u043F\u043E\u043B\u043D\u044B\u043C \u0441\u043E\u0434\u0435\u0440\u0436\u0438\u043C\u044B\u043C.',
|
|
1156
|
+
inputSchema: {
|
|
1157
|
+
type: "object",
|
|
1158
|
+
properties: {
|
|
1159
|
+
block_id: {
|
|
1160
|
+
type: "string",
|
|
1161
|
+
description: "ID \u0442\u0435\u043A\u0441\u0442\u043E\u0432\u043E\u0433\u043E \u0431\u043B\u043E\u043A\u0430 \u0438\u0437 txt_ref \u043F\u043E\u043B\u044F VSL \u043E\u0431\u044A\u0435\u043A\u0442\u0430 (\u0444\u043E\u0440\u043C\u0430\u0442 tb_xxx)"
|
|
1162
|
+
}
|
|
1163
|
+
},
|
|
1164
|
+
required: ["block_id"]
|
|
1165
|
+
}
|
|
1166
|
+
},
|
|
1167
|
+
{
|
|
1168
|
+
name: "vsl_download",
|
|
1169
|
+
description: '\u0423\u043F\u0440\u0430\u0432\u043B\u0435\u043D\u0438\u0435 \u0441\u043A\u0430\u0447\u0438\u0432\u0430\u043D\u0438\u0435\u043C \u0444\u0430\u0439\u043B\u043E\u0432 \u0447\u0435\u0440\u0435\u0437 Playwright BrowserContext. \u0414\u0432\u0430 \u0440\u0435\u0436\u0438\u043C\u0430: (1) target_id \u2014 \u043A\u043B\u0438\u043A \u043F\u043E \u044D\u043B\u0435\u043C\u0435\u043D\u0442\u0443 (\u043A\u043D\u043E\u043F\u043A\u0430/\u0441\u0441\u044B\u043B\u043A\u0430 \u0434\u043B\u044F \u0441\u043A\u0430\u0447\u0438\u0432\u0430\u043D\u0438\u044F), \u0438\u043D\u0438\u0446\u0438\u0438\u0440\u0443\u0435\u0442 download \u0447\u0435\u0440\u0435\u0437 Playwright; (2) value \u2014 \u043F\u0440\u044F\u043C\u043E\u0435 \u0441\u043A\u0430\u0447\u0438\u0432\u0430\u043D\u0438\u0435 \u043F\u043E URL (\u0441\u043E\u0437\u0434\u0430\u0451\u0442 <a download> \u0438 \u043A\u043B\u0438\u043A\u0430\u0435\u0442). \u0412\u043E\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 downloadId, filename, url, status (completed/failed/cancelled), path. \u041F\u0440\u0438\u043C\u0435\u0440\u044B: {target_id: btn_download} \u0438\u043B\u0438 {value: "https://example.com/file.pdf"}. \u041E\u043F\u0446\u0438\u043E\u043D\u0430\u043B\u044C\u043D\u043E: timeout (\u043C\u0441), save_path (\u043F\u0443\u0442\u044C \u0434\u043B\u044F \u0441\u043E\u0445\u0440\u0430\u043D\u0435\u043D\u0438\u044F).',
|
|
1170
|
+
inputSchema: {
|
|
1171
|
+
type: "object",
|
|
1172
|
+
properties: {
|
|
1173
|
+
target_id: {
|
|
1174
|
+
type: "string",
|
|
1175
|
+
description: "ID \u044D\u043B\u0435\u043C\u0435\u043D\u0442\u0430 \u0434\u043B\u044F \u043A\u043B\u0438\u043A\u0430 (\u043A\u043D\u043E\u043F\u043A\u0430/\u0441\u0441\u044B\u043B\u043A\u0430 \u0434\u043B\u044F \u0441\u043A\u0430\u0447\u0438\u0432\u0430\u043D\u0438\u044F)"
|
|
1176
|
+
},
|
|
1177
|
+
value: {
|
|
1178
|
+
type: "string",
|
|
1179
|
+
description: "URL \u0434\u043B\u044F \u043F\u0440\u044F\u043C\u043E\u0433\u043E \u0441\u043A\u0430\u0447\u0438\u0432\u0430\u043D\u0438\u044F"
|
|
1180
|
+
},
|
|
1181
|
+
timeout: {
|
|
1182
|
+
type: "number",
|
|
1183
|
+
description: "\u0422\u0430\u0439\u043C\u0430\u0443\u0442 \u043E\u0436\u0438\u0434\u0430\u043D\u0438\u044F \u0437\u0430\u0433\u0440\u0443\u0437\u043A\u0438 \u0432 \u043C\u0441 (\u043F\u043E \u0443\u043C\u043E\u043B\u0447\u0430\u043D\u0438\u044E \u0438\u0437 config)"
|
|
1184
|
+
},
|
|
1185
|
+
save_path: {
|
|
1186
|
+
type: "string",
|
|
1187
|
+
description: "\u041F\u0443\u0442\u044C \u0434\u043B\u044F \u0441\u043E\u0445\u0440\u0430\u043D\u0435\u043D\u0438\u044F \u0444\u0430\u0439\u043B\u0430 (\u043E\u043F\u0446\u0438\u043E\u043D\u0430\u043B\u044C\u043D\u043E)"
|
|
1188
|
+
}
|
|
1189
|
+
}
|
|
1190
|
+
}
|
|
1191
|
+
}
|
|
1192
|
+
];
|
|
1193
|
+
function registerTools(server, config, browser, session) {
|
|
1194
|
+
server.setRequestHandler(ListToolsRequestSchema, async () => {
|
|
1195
|
+
return {
|
|
1196
|
+
tools: VSL_TOOLS
|
|
1197
|
+
};
|
|
1198
|
+
});
|
|
1199
|
+
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
1200
|
+
const { name, arguments: args } = request.params;
|
|
1201
|
+
try {
|
|
1202
|
+
let result;
|
|
1203
|
+
switch (name) {
|
|
1204
|
+
case "vsl_get_snapshot":
|
|
1205
|
+
result = await handleGetSnapshot(args, browser, session, config);
|
|
1206
|
+
break;
|
|
1207
|
+
case "vsl_get_diff":
|
|
1208
|
+
result = await handleGetDiff(session);
|
|
1209
|
+
break;
|
|
1210
|
+
case "vsl_execute_action":
|
|
1211
|
+
result = await handleExecuteAction(args, browser, session);
|
|
1212
|
+
break;
|
|
1213
|
+
case "vsl_navigate":
|
|
1214
|
+
result = await handleNavigate(args, browser);
|
|
1215
|
+
break;
|
|
1216
|
+
case "vsl_clear_cache":
|
|
1217
|
+
result = await handleClearCache(session);
|
|
1218
|
+
break;
|
|
1219
|
+
case "vsl_get_visual":
|
|
1220
|
+
result = await handleGetVisual(args, browser);
|
|
1221
|
+
break;
|
|
1222
|
+
case "vsl_read_page":
|
|
1223
|
+
result = await handleReadPage(args, browser, session, config);
|
|
1224
|
+
break;
|
|
1225
|
+
case "vsl_get_full_json":
|
|
1226
|
+
result = await handleGetFullJson(args, session);
|
|
1227
|
+
break;
|
|
1228
|
+
case "vsl_get_text_block":
|
|
1229
|
+
result = await handleGetTextBlock(args, session);
|
|
1230
|
+
break;
|
|
1231
|
+
case "vsl_download":
|
|
1232
|
+
result = await handleDownload(args, browser, session);
|
|
1233
|
+
break;
|
|
1234
|
+
default:
|
|
1235
|
+
return {
|
|
1236
|
+
content: [
|
|
1237
|
+
{
|
|
1238
|
+
type: "text",
|
|
1239
|
+
text: JSON.stringify({
|
|
1240
|
+
error: `Unknown tool: ${name}`
|
|
1241
|
+
})
|
|
1242
|
+
}
|
|
1243
|
+
],
|
|
1244
|
+
isError: true
|
|
1245
|
+
};
|
|
1246
|
+
}
|
|
1247
|
+
return {
|
|
1248
|
+
content: [
|
|
1249
|
+
{
|
|
1250
|
+
type: "text",
|
|
1251
|
+
text: JSON.stringify(result)
|
|
1252
|
+
}
|
|
1253
|
+
]
|
|
1254
|
+
};
|
|
1255
|
+
} catch (error) {
|
|
1256
|
+
return {
|
|
1257
|
+
content: [
|
|
1258
|
+
{
|
|
1259
|
+
type: "text",
|
|
1260
|
+
text: JSON.stringify({
|
|
1261
|
+
error: `Tool execution failed: ${error instanceof Error ? error.message : String(error)}`
|
|
1262
|
+
})
|
|
1263
|
+
}
|
|
1264
|
+
],
|
|
1265
|
+
isError: true
|
|
1266
|
+
};
|
|
1267
|
+
}
|
|
1268
|
+
});
|
|
1269
|
+
}
|
|
1270
|
+
|
|
1271
|
+
// src/resources/index.ts
|
|
1272
|
+
import {
|
|
1273
|
+
ListResourcesRequestSchema,
|
|
1274
|
+
ReadResourceRequestSchema
|
|
1275
|
+
} from "@modelcontextprotocol/sdk/types.js";
|
|
1276
|
+
var VSL_RESOURCES = [
|
|
1277
|
+
{
|
|
1278
|
+
uri: "vsl://current",
|
|
1279
|
+
name: "Current VSL Snapshot",
|
|
1280
|
+
description: "\u0422\u0435\u043A\u0443\u0449\u0438\u0439 VSL snapshot \u0441\u0442\u0440\u0430\u043D\u0438\u0446\u044B. \u0421\u043E\u0434\u0435\u0440\u0436\u0438\u0442 \u043F\u043E\u043B\u043D\u0443\u044E \u0441\u0435\u043C\u0430\u043D\u0442\u0438\u0447\u0435\u0441\u043A\u0443\u044E \u0441\u0442\u0440\u0443\u043A\u0442\u0443\u0440\u0443 \u044D\u043B\u0435\u043C\u0435\u043D\u0442\u043E\u0432.",
|
|
1281
|
+
mimeType: "application/json"
|
|
1282
|
+
},
|
|
1283
|
+
{
|
|
1284
|
+
uri: "vsl://diff",
|
|
1285
|
+
name: "Latest VSL Diff",
|
|
1286
|
+
description: "\u041F\u043E\u0441\u043B\u0435\u0434\u043D\u0438\u0439 VSL diff \u0441 \u043C\u043E\u043C\u0435\u043D\u0442\u0430 \u043F\u0440\u0435\u0434\u044B\u0434\u0443\u0449\u0435\u0433\u043E snapshot. \u0421\u043E\u0434\u0435\u0440\u0436\u0438\u0442 \u0442\u043E\u043B\u044C\u043A\u043E \u0438\u0437\u043C\u0435\u043D\u0435\u043D\u0438\u044F (added/modified/removed).",
|
|
1287
|
+
mimeType: "application/json"
|
|
1288
|
+
}
|
|
1289
|
+
];
|
|
1290
|
+
function registerResources(server, _config, session) {
|
|
1291
|
+
server.setRequestHandler(ListResourcesRequestSchema, async () => {
|
|
1292
|
+
return {
|
|
1293
|
+
resources: VSL_RESOURCES
|
|
1294
|
+
};
|
|
1295
|
+
});
|
|
1296
|
+
server.setRequestHandler(ReadResourceRequestSchema, async (request) => {
|
|
1297
|
+
const { uri } = request.params;
|
|
1298
|
+
switch (uri) {
|
|
1299
|
+
case "vsl://current":
|
|
1300
|
+
try {
|
|
1301
|
+
const snapshot = session.getSnapshot();
|
|
1302
|
+
return {
|
|
1303
|
+
contents: [
|
|
1304
|
+
{
|
|
1305
|
+
uri,
|
|
1306
|
+
mimeType: "application/json",
|
|
1307
|
+
text: JSON.stringify(snapshot, null, 2)
|
|
1308
|
+
}
|
|
1309
|
+
]
|
|
1310
|
+
};
|
|
1311
|
+
} catch {
|
|
1312
|
+
return {
|
|
1313
|
+
contents: [
|
|
1314
|
+
{
|
|
1315
|
+
uri,
|
|
1316
|
+
mimeType: "application/json",
|
|
1317
|
+
text: JSON.stringify({
|
|
1318
|
+
error: "No snapshot available. Call vsl_get_snapshot first."
|
|
1319
|
+
})
|
|
1320
|
+
}
|
|
1321
|
+
]
|
|
1322
|
+
};
|
|
1323
|
+
}
|
|
1324
|
+
case "vsl://diff":
|
|
1325
|
+
try {
|
|
1326
|
+
const diff = session.getDiff();
|
|
1327
|
+
if (!diff) {
|
|
1328
|
+
return {
|
|
1329
|
+
contents: [
|
|
1330
|
+
{
|
|
1331
|
+
uri,
|
|
1332
|
+
mimeType: "application/json",
|
|
1333
|
+
text: JSON.stringify({
|
|
1334
|
+
error: "No diff available. This is the first snapshot or no changes detected."
|
|
1335
|
+
})
|
|
1336
|
+
}
|
|
1337
|
+
]
|
|
1338
|
+
};
|
|
1339
|
+
}
|
|
1340
|
+
return {
|
|
1341
|
+
contents: [
|
|
1342
|
+
{
|
|
1343
|
+
uri,
|
|
1344
|
+
mimeType: "application/json",
|
|
1345
|
+
text: JSON.stringify(diff, null, 2)
|
|
1346
|
+
}
|
|
1347
|
+
]
|
|
1348
|
+
};
|
|
1349
|
+
} catch {
|
|
1350
|
+
return {
|
|
1351
|
+
contents: [
|
|
1352
|
+
{
|
|
1353
|
+
uri,
|
|
1354
|
+
mimeType: "application/json",
|
|
1355
|
+
text: JSON.stringify({
|
|
1356
|
+
error: "Failed to compute diff."
|
|
1357
|
+
})
|
|
1358
|
+
}
|
|
1359
|
+
]
|
|
1360
|
+
};
|
|
1361
|
+
}
|
|
1362
|
+
default:
|
|
1363
|
+
throw new Error(`Unknown resource: ${uri}`);
|
|
1364
|
+
}
|
|
1365
|
+
});
|
|
1366
|
+
session.setOnSnapshotChange(() => {
|
|
1367
|
+
server.sendResourceUpdated({ uri: "vsl://current" }).catch(() => {
|
|
1368
|
+
});
|
|
1369
|
+
if (session.getDiff()) {
|
|
1370
|
+
server.sendResourceUpdated({ uri: "vsl://diff" }).catch(() => {
|
|
1371
|
+
});
|
|
1372
|
+
}
|
|
1373
|
+
});
|
|
1374
|
+
}
|
|
1375
|
+
|
|
1376
|
+
// src/browser/manager.ts
|
|
1377
|
+
import path2 from "path";
|
|
1378
|
+
import os from "os";
|
|
1379
|
+
var BrowserManager = class {
|
|
1380
|
+
browser = null;
|
|
1381
|
+
context = null;
|
|
1382
|
+
page = null;
|
|
1383
|
+
playwright = null;
|
|
1384
|
+
config;
|
|
1385
|
+
/** Активные загрузки: downloadId -> PlaywrightDownload. */
|
|
1386
|
+
activeDownloads = /* @__PURE__ */ new Map();
|
|
1387
|
+
constructor(config) {
|
|
1388
|
+
this.config = config;
|
|
1389
|
+
}
|
|
1390
|
+
/**
|
|
1391
|
+
* Загружает Playwright (динамический импорт).
|
|
1392
|
+
* Если Playwright не установлен, возвращает null.
|
|
1393
|
+
*/
|
|
1394
|
+
async loadPlaywright() {
|
|
1395
|
+
if (this.playwright) return this.playwright;
|
|
1396
|
+
try {
|
|
1397
|
+
const pw = await import("playwright");
|
|
1398
|
+
this.playwright = pw;
|
|
1399
|
+
return this.playwright;
|
|
1400
|
+
} catch {
|
|
1401
|
+
return null;
|
|
1402
|
+
}
|
|
1403
|
+
}
|
|
1404
|
+
/**
|
|
1405
|
+
* Запускает браузер (lazy, при первом вызове).
|
|
1406
|
+
* Создаёт BrowserContext с настройками для download.
|
|
1407
|
+
* @throws Error если Playwright не установлен
|
|
1408
|
+
*/
|
|
1409
|
+
async launch() {
|
|
1410
|
+
if (this.browser) return;
|
|
1411
|
+
const pw = await this.loadPlaywright();
|
|
1412
|
+
if (!pw) {
|
|
1413
|
+
throw new Error(
|
|
1414
|
+
'Playwright is not installed. Install it with: npm install playwright\nOr use vsl_read_page with mode="http" for static pages (no browser required).'
|
|
1415
|
+
);
|
|
1416
|
+
}
|
|
1417
|
+
this.browser = await pw.chromium.launch({
|
|
1418
|
+
headless: this.config.headless
|
|
1419
|
+
});
|
|
1420
|
+
const contextOptions = {
|
|
1421
|
+
acceptDownloads: this.config.acceptDownloads ?? true
|
|
1422
|
+
};
|
|
1423
|
+
if (this.config.downloadsPath) {
|
|
1424
|
+
contextOptions.downloadsPath = this.config.downloadsPath;
|
|
1425
|
+
}
|
|
1426
|
+
this.context = await this.browser.newContext(contextOptions);
|
|
1427
|
+
}
|
|
1428
|
+
/**
|
|
1429
|
+
* Возвращает страницу (создаёт новую из context, если нет).
|
|
1430
|
+
* @throws Error если браузер не запущен
|
|
1431
|
+
*/
|
|
1432
|
+
async getPage() {
|
|
1433
|
+
if (!this.browser) {
|
|
1434
|
+
await this.launch();
|
|
1435
|
+
}
|
|
1436
|
+
if (!this.browser || !this.context) {
|
|
1437
|
+
throw new Error("Browser not available");
|
|
1438
|
+
}
|
|
1439
|
+
if (!this.page) {
|
|
1440
|
+
this.page = await this.context.newPage();
|
|
1441
|
+
this.context.on("download", (download) => {
|
|
1442
|
+
const downloadId = `download_${Date.now()}_${Math.random().toString(36).slice(2)}`;
|
|
1443
|
+
this.activeDownloads.set(downloadId, download);
|
|
1444
|
+
});
|
|
1445
|
+
}
|
|
1446
|
+
return this.page;
|
|
1447
|
+
}
|
|
1448
|
+
/**
|
|
1449
|
+
* Переходит по URL.
|
|
1450
|
+
*/
|
|
1451
|
+
async navigate(url) {
|
|
1452
|
+
const page = await this.getPage();
|
|
1453
|
+
await page.goto(url, {
|
|
1454
|
+
timeout: this.config.navigationTimeout,
|
|
1455
|
+
waitUntil: "networkidle"
|
|
1456
|
+
});
|
|
1457
|
+
}
|
|
1458
|
+
/**
|
|
1459
|
+
* Получает HTML-контент текущей страницы.
|
|
1460
|
+
*/
|
|
1461
|
+
async getContent() {
|
|
1462
|
+
const page = await this.getPage();
|
|
1463
|
+
return page.content();
|
|
1464
|
+
}
|
|
1465
|
+
/**
|
|
1466
|
+
* Выполняет JavaScript в контексте страницы.
|
|
1467
|
+
*/
|
|
1468
|
+
async evaluate(fn, ...args) {
|
|
1469
|
+
const page = await this.getPage();
|
|
1470
|
+
return page.evaluate(fn, ...args);
|
|
1471
|
+
}
|
|
1472
|
+
/**
|
|
1473
|
+
* Делает скриншот страницы.
|
|
1474
|
+
*/
|
|
1475
|
+
async screenshot(options) {
|
|
1476
|
+
const page = await this.getPage();
|
|
1477
|
+
return page.screenshot({ type: "png", fullPage: options?.fullPage, clip: options?.clip });
|
|
1478
|
+
}
|
|
1479
|
+
/**
|
|
1480
|
+
* Проверяет, установлен ли Playwright.
|
|
1481
|
+
*/
|
|
1482
|
+
async isAvailable() {
|
|
1483
|
+
const pw = await this.loadPlaywright();
|
|
1484
|
+
return pw !== null;
|
|
1485
|
+
}
|
|
1486
|
+
/**
|
|
1487
|
+
* Ожидает завершения загрузки по downloadId.
|
|
1488
|
+
* @param downloadId - ID загрузки (из activeDownloads)
|
|
1489
|
+
* @param timeout - Таймаут в мс (по умолчанию из config.downloadTimeout)
|
|
1490
|
+
* @returns Информация о завершённой загрузке
|
|
1491
|
+
*/
|
|
1492
|
+
async waitForDownload(downloadId, timeout) {
|
|
1493
|
+
const download = this.activeDownloads.get(downloadId);
|
|
1494
|
+
if (!download) {
|
|
1495
|
+
throw new Error(`Download not found: ${downloadId}`);
|
|
1496
|
+
}
|
|
1497
|
+
const waitTimeout = timeout ?? this.config.downloadTimeout ?? 6e4;
|
|
1498
|
+
try {
|
|
1499
|
+
await Promise.race([
|
|
1500
|
+
download.path(),
|
|
1501
|
+
new Promise(
|
|
1502
|
+
(_, reject) => setTimeout(() => reject(new Error("Download timeout")), waitTimeout)
|
|
1503
|
+
)
|
|
1504
|
+
]);
|
|
1505
|
+
const failure = await download.failure();
|
|
1506
|
+
if (failure) {
|
|
1507
|
+
return {
|
|
1508
|
+
downloadId,
|
|
1509
|
+
filename: download.suggestedFilename(),
|
|
1510
|
+
url: download.url(),
|
|
1511
|
+
status: "failed",
|
|
1512
|
+
path: null
|
|
1513
|
+
};
|
|
1514
|
+
}
|
|
1515
|
+
const path3 = await download.path();
|
|
1516
|
+
return {
|
|
1517
|
+
downloadId,
|
|
1518
|
+
filename: download.suggestedFilename(),
|
|
1519
|
+
url: download.url(),
|
|
1520
|
+
status: "completed",
|
|
1521
|
+
path: path3
|
|
1522
|
+
};
|
|
1523
|
+
} catch {
|
|
1524
|
+
return {
|
|
1525
|
+
downloadId,
|
|
1526
|
+
filename: download.suggestedFilename(),
|
|
1527
|
+
url: download.url(),
|
|
1528
|
+
status: "failed",
|
|
1529
|
+
path: null
|
|
1530
|
+
};
|
|
1531
|
+
}
|
|
1532
|
+
}
|
|
1533
|
+
/**
|
|
1534
|
+
* Возвращает список активных загрузок.
|
|
1535
|
+
*/
|
|
1536
|
+
getDownloads() {
|
|
1537
|
+
return Array.from(this.activeDownloads.entries()).map(([downloadId, download]) => ({
|
|
1538
|
+
downloadId,
|
|
1539
|
+
filename: download.suggestedFilename(),
|
|
1540
|
+
url: download.url()
|
|
1541
|
+
}));
|
|
1542
|
+
}
|
|
1543
|
+
/**
|
|
1544
|
+
* Сохраняет загрузку в указанный путь.
|
|
1545
|
+
* @param downloadId - ID загрузки
|
|
1546
|
+
* @param savePath - Путь для сохранения
|
|
1547
|
+
*/
|
|
1548
|
+
async saveDownload(downloadId, savePath) {
|
|
1549
|
+
const download = this.activeDownloads.get(downloadId);
|
|
1550
|
+
if (!download) {
|
|
1551
|
+
throw new Error(`Download not found: ${downloadId}`);
|
|
1552
|
+
}
|
|
1553
|
+
const resolvedBase = path2.resolve(this.config.downloadsPath.replace(/^~/, os.homedir()));
|
|
1554
|
+
const resolvedSave = path2.resolve(resolvedBase, savePath);
|
|
1555
|
+
if (!resolvedSave.startsWith(resolvedBase + path2.sep) && resolvedSave !== resolvedBase) {
|
|
1556
|
+
throw new Error(`Path traversal detected: save_path '${savePath}' escapes downloads directory '${resolvedBase}'`);
|
|
1557
|
+
}
|
|
1558
|
+
await download.saveAs(resolvedSave);
|
|
1559
|
+
}
|
|
1560
|
+
/**
|
|
1561
|
+
* Отменяет загрузку.
|
|
1562
|
+
* @param downloadId - ID загрузки
|
|
1563
|
+
*/
|
|
1564
|
+
async cancelDownload(downloadId) {
|
|
1565
|
+
const download = this.activeDownloads.get(downloadId);
|
|
1566
|
+
if (!download) {
|
|
1567
|
+
throw new Error(`Download not found: ${downloadId}`);
|
|
1568
|
+
}
|
|
1569
|
+
await download.cancel();
|
|
1570
|
+
this.activeDownloads.delete(downloadId);
|
|
1571
|
+
}
|
|
1572
|
+
/**
|
|
1573
|
+
* Очищает завершённые загрузки из activeDownloads.
|
|
1574
|
+
*/
|
|
1575
|
+
clearDownloads() {
|
|
1576
|
+
this.activeDownloads.clear();
|
|
1577
|
+
}
|
|
1578
|
+
/**
|
|
1579
|
+
* Закрывает браузер и очищает ресурсы.
|
|
1580
|
+
*/
|
|
1581
|
+
async close() {
|
|
1582
|
+
this.activeDownloads.clear();
|
|
1583
|
+
if (this.context) {
|
|
1584
|
+
await this.context.close();
|
|
1585
|
+
this.context = null;
|
|
1586
|
+
}
|
|
1587
|
+
if (this.browser) {
|
|
1588
|
+
await this.browser.close();
|
|
1589
|
+
this.browser = null;
|
|
1590
|
+
}
|
|
1591
|
+
this.page = null;
|
|
1592
|
+
}
|
|
1593
|
+
};
|
|
1594
|
+
|
|
1595
|
+
// src/session/serverSession.ts
|
|
1596
|
+
import { diffVslDocuments } from "@thinkingos/vsl-sdk";
|
|
1597
|
+
var ServerSession = class {
|
|
1598
|
+
currentSnapshot = null;
|
|
1599
|
+
previousSnapshot = null;
|
|
1600
|
+
onSnapshotChange = null;
|
|
1601
|
+
/**
|
|
1602
|
+
* Регистрирует callback для уведомлений об изменении snapshot.
|
|
1603
|
+
* Вызывается при каждом вызове setSnapshot().
|
|
1604
|
+
*/
|
|
1605
|
+
setOnSnapshotChange(callback) {
|
|
1606
|
+
this.onSnapshotChange = callback;
|
|
1607
|
+
}
|
|
1608
|
+
/**
|
|
1609
|
+
* Устанавливает новый snapshot.
|
|
1610
|
+
* Предыдущий snapshot сохраняется для вычисления diff.
|
|
1611
|
+
* Вызывает onSnapshotChange callback если зарегистрирован.
|
|
1612
|
+
*/
|
|
1613
|
+
setSnapshot(doc) {
|
|
1614
|
+
this.previousSnapshot = this.currentSnapshot;
|
|
1615
|
+
this.currentSnapshot = doc;
|
|
1616
|
+
if (this.onSnapshotChange) {
|
|
1617
|
+
this.onSnapshotChange();
|
|
1618
|
+
}
|
|
1619
|
+
}
|
|
1620
|
+
/**
|
|
1621
|
+
* Возвращает текущий snapshot.
|
|
1622
|
+
* @throws Error если snapshot не установлен
|
|
1623
|
+
*/
|
|
1624
|
+
getSnapshot() {
|
|
1625
|
+
if (!this.currentSnapshot) {
|
|
1626
|
+
throw new Error("No snapshot available. Call vsl_get_snapshot first.");
|
|
1627
|
+
}
|
|
1628
|
+
return this.currentSnapshot;
|
|
1629
|
+
}
|
|
1630
|
+
/**
|
|
1631
|
+
* Проверяет, есть ли текущий snapshot.
|
|
1632
|
+
*/
|
|
1633
|
+
hasSnapshot() {
|
|
1634
|
+
return this.currentSnapshot !== null;
|
|
1635
|
+
}
|
|
1636
|
+
/**
|
|
1637
|
+
* Вычисляет diff между текущим и предыдущим snapshot.
|
|
1638
|
+
* @returns VslDiff или null если нет предыдущего snapshot
|
|
1639
|
+
*/
|
|
1640
|
+
getDiff() {
|
|
1641
|
+
if (!this.currentSnapshot || !this.previousSnapshot) {
|
|
1642
|
+
return null;
|
|
1643
|
+
}
|
|
1644
|
+
return diffVslDocuments(this.previousSnapshot, this.currentSnapshot, { diffVersion: 2, baseVersion: 1 });
|
|
1645
|
+
}
|
|
1646
|
+
/**
|
|
1647
|
+
* Сбрасывает сессию (кэш).
|
|
1648
|
+
*/
|
|
1649
|
+
clear() {
|
|
1650
|
+
this.currentSnapshot = null;
|
|
1651
|
+
this.previousSnapshot = null;
|
|
1652
|
+
}
|
|
1653
|
+
};
|
|
1654
|
+
|
|
1655
|
+
// src/version.ts
|
|
1656
|
+
var VSL_MCP_SERVER_VERSION = "0.1.0";
|
|
1657
|
+
|
|
1658
|
+
// src/index.ts
|
|
1659
|
+
async function main() {
|
|
1660
|
+
const config = loadConfig();
|
|
1661
|
+
const browser = new BrowserManager(config.browser);
|
|
1662
|
+
const session = new ServerSession();
|
|
1663
|
+
const server = new Server(
|
|
1664
|
+
{
|
|
1665
|
+
name: "vsl-mcp-server",
|
|
1666
|
+
version: VSL_MCP_SERVER_VERSION
|
|
1667
|
+
},
|
|
1668
|
+
{
|
|
1669
|
+
capabilities: {
|
|
1670
|
+
tools: {},
|
|
1671
|
+
resources: { subscribe: true }
|
|
1672
|
+
}
|
|
1673
|
+
}
|
|
1674
|
+
);
|
|
1675
|
+
registerTools(server, config, browser, session);
|
|
1676
|
+
registerResources(server, config, session);
|
|
1677
|
+
const transport = new StdioServerTransport();
|
|
1678
|
+
await server.connect(transport);
|
|
1679
|
+
console.error(`[VSL MCP Server] v${VSL_MCP_SERVER_VERSION} started (stdio transport)`);
|
|
1680
|
+
console.error(`[VSL MCP Server] Config: vision=${config.vision.provider}, model=${config.vision.model}`);
|
|
1681
|
+
}
|
|
1682
|
+
main().catch((error) => {
|
|
1683
|
+
console.error("[VSL MCP Server] Fatal error:", error);
|
|
1684
|
+
process.exit(1);
|
|
1685
|
+
});
|
|
1686
|
+
//# sourceMappingURL=index.js.map
|