@thinkingos/vsl-mcp-server 0.4.2 → 0.4.3

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/index.js CHANGED
@@ -73,6 +73,50 @@ import {
73
73
 
74
74
  // src/tools/getSnapshot.ts
75
75
  import { isVslDiff } from "@thinkingos/vsl-sdk";
76
+
77
+ // src/utils/injectVslIds.ts
78
+ async function injectVslIdsIntoDom(browser, objects) {
79
+ const page = await browser.getPage();
80
+ await page.waitForTimeout(50);
81
+ await browser.evaluate(
82
+ (objects2) => {
83
+ function parseVslId(id) {
84
+ const match = id.match(/^([a-z][a-z0-9]*(?:_[a-z][a-z0-9]*)*)_(\d+(?:_\d+)*)$/);
85
+ if (!match) return null;
86
+ const tag = match[1];
87
+ const indexPath = match[2].split("_").map(Number);
88
+ return { tag, indexPath };
89
+ }
90
+ function visit(obj) {
91
+ const parsed = parseVslId(obj.id);
92
+ if (!parsed) return;
93
+ const indexPath = parsed.indexPath;
94
+ let current = document.body;
95
+ for (const idx of indexPath) {
96
+ if (!current) break;
97
+ const elementChildren = Array.from(current.children);
98
+ current = elementChildren[idx] || null;
99
+ }
100
+ if (current) {
101
+ current.setAttribute("data-vsl-id", obj.id);
102
+ } else {
103
+ console.warn(`[VSL] Failed to inject data-vsl-id="${obj.id}": element not found at indexPath [${indexPath.join(", ")}]`);
104
+ }
105
+ if (obj.ch) {
106
+ for (const child of obj.ch) {
107
+ visit(child);
108
+ }
109
+ }
110
+ }
111
+ for (const obj of objects2) {
112
+ visit(obj);
113
+ }
114
+ },
115
+ objects
116
+ );
117
+ }
118
+
119
+ // src/tools/getSnapshot.ts
76
120
  var DEFAULT_SNAPSHOT_TTL_MS = 5e3;
77
121
  var snapshotCache = /* @__PURE__ */ new Map();
78
122
  function filterObjectsByDetailLevel(objects, level) {
@@ -227,14 +271,25 @@ function extractDomTreeInBrowser() {
227
271
  result.push(...collectVisibleChildren(child, path3));
228
272
  return;
229
273
  }
274
+ const attributes = extractAttributes(child);
275
+ const css = captureCss(child);
276
+ if (attributes["aria-hidden"] === "true") {
277
+ return;
278
+ }
279
+ if (css.opacity === "0" && css.pointerEvents === "none") {
280
+ return;
281
+ }
282
+ const children = collectVisibleChildren(child, path3);
283
+ const vslId = `${tag}_${path3.join("_")}`;
284
+ child.setAttribute("data-vsl-id", vslId);
230
285
  result.push({
231
286
  tag,
232
287
  indexPath: path3,
233
288
  rect,
234
289
  text: ownText(child) || null,
235
- attributes: extractAttributes(child),
236
- css: captureCss(child),
237
- children: collectVisibleChildren(child, path3)
290
+ attributes,
291
+ css,
292
+ children
238
293
  });
239
294
  });
240
295
  return result;
@@ -306,44 +361,7 @@ async function handleGetSnapshot(args, browser, session, _config) {
306
361
  input
307
362
  );
308
363
  const currentDoc = isVslDiff(result) ? session.getSnapshot() : result;
309
- const page = await browser.getPage();
310
- await page.waitForTimeout(50);
311
- await browser.evaluate(
312
- (objects) => {
313
- function parseVslId(id) {
314
- const match = id.match(/^([a-z][a-z0-9]*(?:_[a-z][a-z0-9]*)*)_(\d+(?:_\d+)*)$/);
315
- if (!match) return null;
316
- const tag = match[1];
317
- const indexPath = match[2].split("_").map(Number);
318
- return { tag, indexPath };
319
- }
320
- function visit(obj) {
321
- const parsed = parseVslId(obj.id);
322
- if (!parsed) return;
323
- const indexPath = parsed.indexPath;
324
- let current = document.body;
325
- for (const idx of indexPath) {
326
- if (!current) break;
327
- const elementChildren = Array.from(current.children);
328
- current = elementChildren[idx] || null;
329
- }
330
- if (current) {
331
- current.setAttribute("data-vsl-id", obj.id);
332
- } else {
333
- console.warn(`[VSL] Failed to inject data-vsl-id="${obj.id}": element not found at indexPath [${indexPath.join(", ")}]`);
334
- }
335
- if (obj.ch) {
336
- for (const child of obj.ch) {
337
- visit(child);
338
- }
339
- }
340
- }
341
- for (const obj of objects) {
342
- visit(obj);
343
- }
344
- },
345
- currentDoc.objects
346
- );
364
+ await injectVslIdsIntoDom(browser, currentDoc.objects);
347
365
  const filteredObjects = filterObjectsByDetailLevel(currentDoc.objects, detailLevel);
348
366
  const filteredResult = isVslDiff(result) ? { ...result, objects: filteredObjects } : { ...result, objects: filteredObjects };
349
367
  const executionTimeMs = Date.now() - startTime;
@@ -472,6 +490,8 @@ async function handleNavigate(args, browser, session) {
472
490
  };
473
491
  session.snapshotFromElements(elements, input);
474
492
  snapshot = session.getSnapshot();
493
+ const currentDoc = session.getSnapshot();
494
+ await injectVslIdsIntoDom(browser, currentDoc.objects);
475
495
  } catch (error) {
476
496
  const errorMessage = `Failed to extract snapshot: ${error instanceof Error ? error.message : String(error)}`;
477
497
  console.error("[vsl_navigate]", errorMessage, error);
@@ -552,6 +572,7 @@ async function getStateAfterAction(session, browser, url) {
552
572
  session.snapshotFromElements(elements, input);
553
573
  const diff = session.getDiff();
554
574
  const snapshot = session.getSnapshot();
575
+ await injectVslIdsIntoDom(browser, snapshot.objects);
555
576
  return { diff, snapshot };
556
577
  } catch (error) {
557
578
  const errorMessage = `Failed to get state after action: ${error instanceof Error ? error.message : String(error)}`;
@@ -587,11 +608,26 @@ async function handleExecuteAction(args, browser, session) {
587
608
  };
588
609
  }
589
610
  if (args.action === "scroll" && args.value !== void 0) {
590
- const VALID_SCROLL_VALUES = ["up", "down"];
591
- if (!VALID_SCROLL_VALUES.includes(args.value)) {
611
+ const SCROLL_DIRECTIONS = /* @__PURE__ */ new Set(["up", "down", "left", "right"]);
612
+ const parts = args.value.trim().split(":");
613
+ const dir = parts[0] ?? "";
614
+ const amountPart = parts[1];
615
+ if (!SCROLL_DIRECTIONS.has(dir)) {
616
+ return {
617
+ status: "error",
618
+ error: `Invalid scroll direction: "${dir}". Valid: up, down, left, right. Optional amount: "down:300"`
619
+ };
620
+ }
621
+ if (parts.length > 2) {
592
622
  return {
593
623
  status: "error",
594
- error: "Invalid scroll value: " + args.value + ". Valid values: " + VALID_SCROLL_VALUES.join(", ")
624
+ error: 'Invalid scroll value format. Use "dir" or "dir:amount" (e.g. "down:300")'
625
+ };
626
+ }
627
+ if (amountPart !== void 0 && !/^\d+(?:\.\d+)?$/.test(amountPart)) {
628
+ return {
629
+ status: "error",
630
+ error: `Invalid scroll amount: "${amountPart}". Must be a positive number`
595
631
  };
596
632
  }
597
633
  }
@@ -655,16 +691,20 @@ async function handleExecuteAction(args, browser, session) {
655
691
  }
656
692
  await page.fill(selector, args.value, { timeout: 5e3 });
657
693
  break;
658
- case "scroll":
659
- await browser.evaluate((val) => {
660
- if (val === "down") {
661
- window.scrollBy(0, 500);
662
- } else if (val === "up") {
663
- window.scrollBy(0, -500);
664
- }
665
- }, args.value || "down");
694
+ case "scroll": {
695
+ const DEFAULT_SCROLL_AMOUNT = 500;
696
+ const scrollValue = args.value || "down";
697
+ const scrollParts = scrollValue.trim().split(":");
698
+ const scrollDir = scrollParts[0];
699
+ const scrollAmount = scrollParts[1] !== void 0 ? Number(scrollParts[1]) : DEFAULT_SCROLL_AMOUNT;
700
+ const dx = scrollDir === "left" ? -scrollAmount : scrollDir === "right" ? scrollAmount : 0;
701
+ const dy = scrollDir === "up" ? -scrollAmount : scrollDir === "down" ? scrollAmount : 0;
702
+ await browser.evaluate(({ dx: dx2, dy: dy2 }) => {
703
+ window.scrollBy(dx2, dy2);
704
+ }, { dx, dy });
666
705
  await page.waitForTimeout(50);
667
706
  break;
707
+ }
668
708
  case "select":
669
709
  if (!args.value) {
670
710
  return {
@@ -1784,7 +1824,7 @@ var VSL_TOOLS = [
1784
1824
  },
1785
1825
  {
1786
1826
  name: "vsl_execute_action",
1787
- 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), fill (\u0430\u043B\u0438\u0430\u0441 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), upload (\u0437\u0430\u0433\u0440\u0443\u0437\u043A\u0430 \u0444\u0430\u0439\u043B\u0430, \u0442\u0440\u0435\u0431\u0443\u0435\u0442 value \u2014 \u043F\u0443\u0442\u044C \u043A \u0444\u0430\u0439\u043B\u0443 \u0438\u043B\u0438 \u0441\u043F\u0438\u0441\u043E\u043A \u043F\u0443\u0442\u0435\u0439 \u0447\u0435\u0440\u0435\u0437 \u0437\u0430\u043F\u044F\u0442\u0443\u044E). \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} \u0438\u043B\u0438 {action:fill, target_id:inp_2, value:hello@mail.com} \u0438\u043B\u0438 {action:upload, target_id:file_input_0, value:/path/to/file.pdf}. \u041F\u0430\u0440\u0430\u043C\u0435\u0442\u0440 return_state=true \u0432\u043E\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 diff \u0438 snapshot \u043F\u043E\u0441\u043B\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044F.",
1827
+ 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), fill (\u0430\u043B\u0438\u0430\u0441 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, \u0444\u043E\u0440\u043C\u0430\u0442: "up", "down", "left", "right" \u0438\u043B\u0438 "dir:amount", \u043D\u0430\u043F\u0440\u0438\u043C\u0435\u0440 "down:300"), 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), upload (\u0437\u0430\u0433\u0440\u0443\u0437\u043A\u0430 \u0444\u0430\u0439\u043B\u0430, \u0442\u0440\u0435\u0431\u0443\u0435\u0442 value \u2014 \u043F\u0443\u0442\u044C \u043A \u0444\u0430\u0439\u043B\u0443 \u0438\u043B\u0438 \u0441\u043F\u0438\u0441\u043E\u043A \u043F\u0443\u0442\u0435\u0439 \u0447\u0435\u0440\u0435\u0437 \u0437\u0430\u043F\u044F\u0442\u0443\u044E). \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} \u0438\u043B\u0438 {action:fill, target_id:inp_2, value:hello@mail.com} \u0438\u043B\u0438 {action:upload, target_id:file_input_0, value:/path/to/file.pdf}. \u041F\u0430\u0440\u0430\u043C\u0435\u0442\u0440 return_state=true \u0432\u043E\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 diff \u0438 snapshot \u043F\u043E\u0441\u043B\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044F.',
1788
1828
  inputSchema: {
1789
1829
  type: "object",
1790
1830
  properties: {