@thinkingos/vsl-mcp-server 1.2.7 → 1.2.9

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
@@ -75,45 +75,52 @@ import {
75
75
  import { isVslDiff } from "@thinkingos/vsl-sdk";
76
76
 
77
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 };
78
+ function buildInjectionScript() {
79
+ return (objects) => {
80
+ function parseVslId(id) {
81
+ const match = id.match(/^([a-z][a-z0-9]*(?:_[a-z][a-z0-9]*)*)_(\d+(?:_\d+)*)$/);
82
+ if (!match) return null;
83
+ const tag = match[1];
84
+ const indexPath = match[2].split("_").map(Number);
85
+ return { tag, indexPath };
86
+ }
87
+ function visit(obj) {
88
+ const parsed = parseVslId(obj.id);
89
+ if (!parsed) return;
90
+ const indexPath = parsed.indexPath;
91
+ let current = document.body;
92
+ for (const idx of indexPath) {
93
+ if (!current) break;
94
+ const elementChildren = Array.from(current.children);
95
+ current = elementChildren[idx] || null;
89
96
  }
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
- }
97
+ if (current) {
98
+ current.setAttribute("data-vsl-id", obj.id);
99
+ } else {
100
+ console.warn(`[VSL] Failed to inject data-vsl-id="${obj.id}": element not found at indexPath [${indexPath.join(", ")}]`);
110
101
  }
111
- for (const obj of objects2) {
112
- visit(obj);
102
+ if (obj.ch) {
103
+ for (const child of obj.ch) {
104
+ visit(child);
105
+ }
113
106
  }
114
- },
115
- objects
116
- );
107
+ }
108
+ for (const obj of objects) {
109
+ visit(obj);
110
+ }
111
+ };
112
+ }
113
+ async function injectVslIdsIntoDom(browser, objects) {
114
+ const page = await browser.getPage();
115
+ await page.waitForTimeout(50);
116
+ await browser.evaluate(buildInjectionScript(), objects);
117
+ }
118
+ async function injectVslIdsIntoFrame(browser, frame, objects) {
119
+ try {
120
+ await browser.evaluateInFrame(frame, buildInjectionScript(), objects);
121
+ } catch (error) {
122
+ console.warn("[VSL] Failed to inject data-vsl-id into iframe:", error);
123
+ }
117
124
  }
118
125
 
119
126
  // src/utils/idMapper.ts
@@ -286,7 +293,9 @@ var CONTAINER_TYPES = /* @__PURE__ */ new Set([
286
293
  "nav",
287
294
  "header",
288
295
  "main",
289
- "footer"
296
+ "footer",
297
+ "iframe"
298
+ // M2.1: iframe objects contain sub-VSL documents
290
299
  ]);
291
300
  function filterObjectsByDetailLevel(objects, level, options) {
292
301
  if (level === "high") return objects;
@@ -575,6 +584,46 @@ function extractDomTreeInBrowser() {
575
584
  }
576
585
  }];
577
586
  }
587
+ function extractIframesInBrowser() {
588
+ const iframes = [];
589
+ const iframeElements = document.querySelectorAll("iframe");
590
+ for (const iframe of Array.from(iframeElements)) {
591
+ const rect = iframe.getBoundingClientRect();
592
+ if (rect.width <= 0 || rect.height <= 0) {
593
+ continue;
594
+ }
595
+ let url = iframe.src || "";
596
+ try {
597
+ if (url && !url.startsWith("http://") && !url.startsWith("https://")) {
598
+ url = new URL(url, window.location.href).href;
599
+ }
600
+ } catch {
601
+ }
602
+ iframes.push({
603
+ url,
604
+ rect: {
605
+ x: rect.x + window.scrollX,
606
+ y: rect.y + window.scrollY,
607
+ width: rect.width,
608
+ height: rect.height
609
+ },
610
+ name: iframe.name || "",
611
+ id: iframe.id || ""
612
+ });
613
+ }
614
+ return iframes;
615
+ }
616
+ async function extractDomTreeFromFrame(browser, frame) {
617
+ const extractionResult = await browser.evaluateInFrame(
618
+ frame,
619
+ extractDomTreeInBrowser
620
+ );
621
+ const extraction = Array.isArray(extractionResult) ? extractionResult[0] : void 0;
622
+ if (!extraction || !Array.isArray(extraction.elements)) {
623
+ throw new Error("Failed to extract DOM tree from iframe: empty result");
624
+ }
625
+ return extraction;
626
+ }
578
627
  async function extractDomTree(browser) {
579
628
  const extractionResult = await browser.evaluate(
580
629
  extractDomTreeInBrowser
@@ -652,6 +701,46 @@ async function handleGetSnapshot(args, browser, session, _config) {
652
701
  const viewport = extraction.viewport;
653
702
  const url = args.url || await browser.evaluate(() => window.location.href);
654
703
  const title = await browser.evaluate(() => document.title);
704
+ const iframeElementsRaw = await browser.evaluate(extractIframesInBrowser);
705
+ const iframeElements = Array.isArray(iframeElementsRaw) ? iframeElementsRaw : [];
706
+ if (iframeElements.length > 0) {
707
+ console.log(`[VSL] Found ${iframeElements.length} iframe elements:`);
708
+ for (const iframe of iframeElements) {
709
+ console.log(` - URL: ${iframe.url}, name: ${iframe.name}, id: ${iframe.id}`);
710
+ }
711
+ }
712
+ const iframeSubDocs = [];
713
+ for (const iframeInfo of iframeElements) {
714
+ if (!iframeInfo.url) continue;
715
+ try {
716
+ const frame = await browser.getFrame(iframeInfo.url);
717
+ if (!frame) {
718
+ console.warn(`[VSL] No Playwright frame found for iframe URL: ${iframeInfo.url}`);
719
+ continue;
720
+ }
721
+ console.log(`[VSL] \u2705 Matched Playwright frame for iframe: ${iframeInfo.url}`);
722
+ const iframeExtraction = await extractDomTreeFromFrame(browser, frame);
723
+ const iframeViewport = iframeExtraction.viewport;
724
+ const { segmentTree: segmentTree2, buildVslDocument: buildVslDocument2 } = await import("@thinkingos/vsl-sdk");
725
+ const segmented = segmentTree2(iframeExtraction.elements);
726
+ const iframeDoc = buildVslDocument2(segmented, {
727
+ viewport: iframeViewport,
728
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
729
+ url: iframeInfo.url,
730
+ title: `iframe: ${iframeInfo.name || iframeInfo.id || iframeInfo.url}`
731
+ });
732
+ iframeSubDocs.push({
733
+ url: iframeInfo.url,
734
+ rect: iframeInfo.rect,
735
+ doc: iframeDoc,
736
+ frame
737
+ // сохраняем PlaywrightFrame для injectVslIdsIntoFrame
738
+ });
739
+ console.log(`[VSL] \u2705 Successfully extracted DOM from iframe: ${iframeInfo.url} (${iframeDoc.objects.length} objects)`);
740
+ } catch (error) {
741
+ console.warn(`[VSL] \u274C Failed to extract iframe ${iframeInfo.url}:`, error);
742
+ }
743
+ }
655
744
  const input = {
656
745
  viewport,
657
746
  url,
@@ -664,6 +753,24 @@ async function handleGetSnapshot(args, browser, session, _config) {
664
753
  input
665
754
  );
666
755
  const currentDoc = isVslDiff(result) ? session.getSnapshot() : result;
756
+ if (iframeSubDocs.length > 0) {
757
+ console.log(`[VSL] Adding ${iframeSubDocs.length} iframe objects to snapshot`);
758
+ const iframeObjects = iframeSubDocs.map((iframeInfo, index) => ({
759
+ id: `iframe_${index}`,
760
+ t: "iframe",
761
+ p: [iframeInfo.rect.x, iframeInfo.rect.y],
762
+ s: [iframeInfo.rect.width, iframeInfo.rect.height],
763
+ iframe: {
764
+ url: iframeInfo.url,
765
+ frameId: index,
766
+ vsl: iframeInfo.doc
767
+ }
768
+ }));
769
+ currentDoc.objects.push(...iframeObjects);
770
+ for (const iframeInfo of iframeSubDocs) {
771
+ await injectVslIdsIntoFrame(browser, iframeInfo.frame, iframeInfo.doc.objects);
772
+ }
773
+ }
667
774
  await injectVslIdsIntoDom(browser, currentDoc.objects);
668
775
  const scrollContext = session.getScrollContext();
669
776
  const win = computeVisibleWindow(currentDoc.canvas.viewport, scrollContext);
@@ -1006,11 +1113,27 @@ async function handleExecuteAction(args, browser, session) {
1006
1113
  }
1007
1114
  const idMap = session.getIdMap();
1008
1115
  const resolvedId = idMap.get(args.target_id) || args.target_id;
1009
- const selector = `[data-vsl-id="${resolvedId}"], #${resolvedId}, .${resolvedId}`;
1116
+ let targetFrame = null;
1117
+ let localId = resolvedId;
1118
+ const frameMatch = resolvedId.match(/^iframe_(\d+):(.+)$/);
1119
+ if (frameMatch) {
1120
+ const frameIndex = parseInt(frameMatch[1], 10);
1121
+ localId = frameMatch[2];
1122
+ const frames = await browser.getFrames();
1123
+ targetFrame = frames[frameIndex + 1] || null;
1124
+ if (!targetFrame) {
1125
+ return {
1126
+ status: "error",
1127
+ error: `Frame iframe_${frameIndex} not found. Available frames: ${frames.length}`
1128
+ };
1129
+ }
1130
+ }
1131
+ const selector = `[data-vsl-id="${localId}"], #${localId}, .${localId}`;
1010
1132
  const page = await browser.getPage();
1133
+ const target = targetFrame || page;
1011
1134
  switch (args.action) {
1012
1135
  case "click":
1013
- await page.click(selector, { timeout: 5e3 });
1136
+ await target.locator(selector).click({ timeout: 5e3 });
1014
1137
  await page.waitForTimeout(100);
1015
1138
  break;
1016
1139
  case "fill":
@@ -1021,7 +1144,7 @@ async function handleExecuteAction(args, browser, session) {
1021
1144
  error: "value is required for type action"
1022
1145
  };
1023
1146
  }
1024
- await page.fill(selector, args.value, { timeout: 5e3 });
1147
+ await target.locator(selector).fill(args.value, { timeout: 5e3 });
1025
1148
  break;
1026
1149
  case "scroll": {
1027
1150
  const DEFAULT_SCROLL_AMOUNT = 500;
@@ -1081,31 +1204,62 @@ async function handleExecuteAction(args, browser, session) {
1081
1204
  error: "value is required for select action"
1082
1205
  };
1083
1206
  }
1084
- await browser.evaluate(({ sel, val }) => {
1085
- const el = document.querySelector(sel);
1086
- if (el) el.value = val;
1087
- }, { sel: selector, val: args.value });
1207
+ if (targetFrame) {
1208
+ await browser.evaluateInFrame(targetFrame, ({ sel, val }) => {
1209
+ const el = document.querySelector(sel);
1210
+ if (el) el.value = val;
1211
+ }, { sel: selector, val: args.value });
1212
+ } else {
1213
+ await browser.evaluate(({ sel, val }) => {
1214
+ const el = document.querySelector(sel);
1215
+ if (el) el.value = val;
1216
+ }, { sel: selector, val: args.value });
1217
+ }
1088
1218
  break;
1089
1219
  case "hover":
1090
- await browser.evaluate((sel) => {
1091
- const el = document.querySelector(sel);
1092
- if (el) {
1093
- const event = new MouseEvent("mouseover", { bubbles: true });
1094
- el.dispatchEvent(event);
1095
- }
1096
- }, selector);
1097
- break;
1098
- case "focus":
1099
- await browser.evaluate((sel) => {
1100
- const el = document.querySelector(sel);
1101
- if (el) el.focus();
1102
- }, selector);
1220
+ if (targetFrame) {
1221
+ await browser.evaluateInFrame(targetFrame, (sel) => {
1222
+ const el = document.querySelector(sel);
1223
+ if (el) {
1224
+ const event = new MouseEvent("mouseover", { bubbles: true });
1225
+ el.dispatchEvent(event);
1226
+ }
1227
+ }, selector);
1228
+ } else {
1229
+ await browser.evaluate((sel) => {
1230
+ const el = document.querySelector(sel);
1231
+ if (el) {
1232
+ const event = new MouseEvent("mouseover", { bubbles: true });
1233
+ el.dispatchEvent(event);
1234
+ }
1235
+ }, selector);
1236
+ }
1103
1237
  break;
1104
1238
  case "blur":
1105
- await browser.evaluate((sel) => {
1106
- const el = document.querySelector(sel);
1107
- if (el) el.blur();
1108
- }, selector);
1239
+ if (targetFrame) {
1240
+ await browser.evaluateInFrame(targetFrame, (sel) => {
1241
+ const el = document.querySelector(sel);
1242
+ if (el) el.blur();
1243
+ }, selector);
1244
+ } else {
1245
+ await browser.evaluate((sel) => {
1246
+ const el = document.querySelector(sel);
1247
+ if (el) el.blur();
1248
+ }, selector);
1249
+ }
1250
+ break;
1251
+ case "focus":
1252
+ if (targetFrame) {
1253
+ await browser.evaluateInFrame(targetFrame, (sel) => {
1254
+ const el = document.querySelector(sel);
1255
+ if (el) el.focus();
1256
+ }, selector);
1257
+ } else {
1258
+ await browser.evaluate((sel) => {
1259
+ const el = document.querySelector(sel);
1260
+ if (el) el.focus();
1261
+ }, selector);
1262
+ }
1109
1263
  break;
1110
1264
  case "press": {
1111
1265
  if (!args.value) {
@@ -1121,19 +1275,37 @@ async function handleExecuteAction(args, browser, session) {
1121
1275
  case "download": {
1122
1276
  const downloadsBefore = browser.getDownloads();
1123
1277
  if (args.value) {
1124
- await browser.evaluate((url) => {
1125
- const a = document.createElement("a");
1126
- a.href = url;
1127
- a.download = "";
1128
- document.body.appendChild(a);
1129
- a.click();
1130
- document.body.removeChild(a);
1131
- }, args.value);
1278
+ if (targetFrame) {
1279
+ await browser.evaluateInFrame(targetFrame, (url) => {
1280
+ const a = document.createElement("a");
1281
+ a.href = url;
1282
+ a.download = "";
1283
+ document.body.appendChild(a);
1284
+ a.click();
1285
+ document.body.removeChild(a);
1286
+ }, args.value);
1287
+ } else {
1288
+ await browser.evaluate((url) => {
1289
+ const a = document.createElement("a");
1290
+ a.href = url;
1291
+ a.download = "";
1292
+ document.body.appendChild(a);
1293
+ a.click();
1294
+ document.body.removeChild(a);
1295
+ }, args.value);
1296
+ }
1132
1297
  } else {
1133
- await browser.evaluate((sel) => {
1134
- const el = document.querySelector(sel);
1135
- if (el) el.click();
1136
- }, selector);
1298
+ if (targetFrame) {
1299
+ await browser.evaluateInFrame(targetFrame, (sel) => {
1300
+ const el = document.querySelector(sel);
1301
+ if (el) el.click();
1302
+ }, selector);
1303
+ } else {
1304
+ await browser.evaluate((sel) => {
1305
+ const el = document.querySelector(sel);
1306
+ if (el) el.click();
1307
+ }, selector);
1308
+ }
1137
1309
  }
1138
1310
  const pollDeadline = Date.now() + 5e3;
1139
1311
  let newDownload;
@@ -1197,7 +1369,7 @@ async function handleExecuteAction(args, browser, session) {
1197
1369
  error: "value must contain at least one file path"
1198
1370
  };
1199
1371
  }
1200
- await browser.uploadFile(selector, filePaths);
1372
+ await target.locator(selector).setInputFiles(filePaths.length === 1 ? filePaths[0] : filePaths);
1201
1373
  const result2 = {
1202
1374
  status: "success",
1203
1375
  data: {
@@ -2416,6 +2588,33 @@ var BrowserManager = class {
2416
2588
  /**
2417
2589
  * Выполняет JavaScript в контексте страницы.
2418
2590
  */
2591
+ /**
2592
+ * Возвращает все фреймы на странице (включая iframe).
2593
+ */
2594
+ async getFrames() {
2595
+ const page = await this.getPage();
2596
+ return page.frames();
2597
+ }
2598
+ /**
2599
+ * Находит фрейм по URL или имени.
2600
+ */
2601
+ async getFrame(urlOrName) {
2602
+ const page = await this.getPage();
2603
+ return page.frame(urlOrName);
2604
+ }
2605
+ /**
2606
+ * Выполняет JavaScript в контексте конкретного фрейма.
2607
+ */
2608
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
2609
+ async evaluateInFrame(frame, fn, ...args) {
2610
+ return frame.evaluate(fn, ...args);
2611
+ }
2612
+ /**
2613
+ * Возвращает Playwright Locator для элемента в фрейме.
2614
+ */
2615
+ async locatorInFrame(frame, selector) {
2616
+ return frame.locator(selector);
2617
+ }
2419
2618
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
2420
2619
  async evaluate(fn, ...args) {
2421
2620
  const page = await this.getPage();
@@ -2903,7 +3102,7 @@ var SessionManager = class {
2903
3102
  };
2904
3103
 
2905
3104
  // src/version.ts
2906
- var VSL_MCP_SERVER_VERSION = "1.2.7";
3105
+ var VSL_MCP_SERVER_VERSION = "1.2.9";
2907
3106
 
2908
3107
  // src/index.ts
2909
3108
  async function main() {