@thinkingos/vsl-mcp-server 1.2.7 → 1.2.8

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
@@ -575,6 +582,46 @@ function extractDomTreeInBrowser() {
575
582
  }
576
583
  }];
577
584
  }
585
+ function extractIframesInBrowser() {
586
+ const iframes = [];
587
+ const iframeElements = document.querySelectorAll("iframe");
588
+ for (const iframe of Array.from(iframeElements)) {
589
+ const rect = iframe.getBoundingClientRect();
590
+ if (rect.width <= 0 || rect.height <= 0) {
591
+ continue;
592
+ }
593
+ let url = iframe.src || "";
594
+ try {
595
+ if (url && !url.startsWith("http://") && !url.startsWith("https://")) {
596
+ url = new URL(url, window.location.href).href;
597
+ }
598
+ } catch {
599
+ }
600
+ iframes.push({
601
+ url,
602
+ rect: {
603
+ x: rect.x + window.scrollX,
604
+ y: rect.y + window.scrollY,
605
+ width: rect.width,
606
+ height: rect.height
607
+ },
608
+ name: iframe.name || "",
609
+ id: iframe.id || ""
610
+ });
611
+ }
612
+ return iframes;
613
+ }
614
+ async function extractDomTreeFromFrame(browser, frame) {
615
+ const extractionResult = await browser.evaluateInFrame(
616
+ frame,
617
+ extractDomTreeInBrowser
618
+ );
619
+ const extraction = Array.isArray(extractionResult) ? extractionResult[0] : void 0;
620
+ if (!extraction || !Array.isArray(extraction.elements)) {
621
+ throw new Error("Failed to extract DOM tree from iframe: empty result");
622
+ }
623
+ return extraction;
624
+ }
578
625
  async function extractDomTree(browser) {
579
626
  const extractionResult = await browser.evaluate(
580
627
  extractDomTreeInBrowser
@@ -652,6 +699,35 @@ async function handleGetSnapshot(args, browser, session, _config) {
652
699
  const viewport = extraction.viewport;
653
700
  const url = args.url || await browser.evaluate(() => window.location.href);
654
701
  const title = await browser.evaluate(() => document.title);
702
+ const iframeElementsRaw = await browser.evaluate(extractIframesInBrowser);
703
+ const iframeElements = Array.isArray(iframeElementsRaw) ? iframeElementsRaw : [];
704
+ const iframeSubDocs = [];
705
+ for (const iframeInfo of iframeElements) {
706
+ if (!iframeInfo.url) continue;
707
+ try {
708
+ const frame = await browser.getFrame(iframeInfo.url);
709
+ if (!frame) continue;
710
+ const iframeExtraction = await extractDomTreeFromFrame(browser, frame);
711
+ const iframeViewport = iframeExtraction.viewport;
712
+ const { segmentTree: segmentTree2, buildVslDocument: buildVslDocument2 } = await import("@thinkingos/vsl-sdk");
713
+ const segmented = segmentTree2(iframeExtraction.elements);
714
+ const iframeDoc = buildVslDocument2(segmented, {
715
+ viewport: iframeViewport,
716
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
717
+ url: iframeInfo.url,
718
+ title: `iframe: ${iframeInfo.name || iframeInfo.id || iframeInfo.url}`
719
+ });
720
+ iframeSubDocs.push({
721
+ url: iframeInfo.url,
722
+ rect: iframeInfo.rect,
723
+ doc: iframeDoc,
724
+ frame
725
+ // сохраняем PlaywrightFrame для injectVslIdsIntoFrame
726
+ });
727
+ } catch (error) {
728
+ console.warn(`Failed to extract iframe ${iframeInfo.url}:`, error);
729
+ }
730
+ }
655
731
  const input = {
656
732
  viewport,
657
733
  url,
@@ -664,6 +740,23 @@ async function handleGetSnapshot(args, browser, session, _config) {
664
740
  input
665
741
  );
666
742
  const currentDoc = isVslDiff(result) ? session.getSnapshot() : result;
743
+ if (iframeSubDocs.length > 0) {
744
+ const iframeObjects = iframeSubDocs.map((iframeInfo, index) => ({
745
+ id: `iframe_${index}`,
746
+ t: "iframe",
747
+ p: [iframeInfo.rect.x, iframeInfo.rect.y],
748
+ s: [iframeInfo.rect.width, iframeInfo.rect.height],
749
+ iframe: {
750
+ url: iframeInfo.url,
751
+ frameId: index,
752
+ vsl: iframeInfo.doc
753
+ }
754
+ }));
755
+ currentDoc.objects.push(...iframeObjects);
756
+ for (const iframeInfo of iframeSubDocs) {
757
+ await injectVslIdsIntoFrame(browser, iframeInfo.frame, iframeInfo.doc.objects);
758
+ }
759
+ }
667
760
  await injectVslIdsIntoDom(browser, currentDoc.objects);
668
761
  const scrollContext = session.getScrollContext();
669
762
  const win = computeVisibleWindow(currentDoc.canvas.viewport, scrollContext);
@@ -1006,11 +1099,27 @@ async function handleExecuteAction(args, browser, session) {
1006
1099
  }
1007
1100
  const idMap = session.getIdMap();
1008
1101
  const resolvedId = idMap.get(args.target_id) || args.target_id;
1009
- const selector = `[data-vsl-id="${resolvedId}"], #${resolvedId}, .${resolvedId}`;
1102
+ let targetFrame = null;
1103
+ let localId = resolvedId;
1104
+ const frameMatch = resolvedId.match(/^iframe_(\d+):(.+)$/);
1105
+ if (frameMatch) {
1106
+ const frameIndex = parseInt(frameMatch[1], 10);
1107
+ localId = frameMatch[2];
1108
+ const frames = await browser.getFrames();
1109
+ targetFrame = frames[frameIndex + 1] || null;
1110
+ if (!targetFrame) {
1111
+ return {
1112
+ status: "error",
1113
+ error: `Frame iframe_${frameIndex} not found. Available frames: ${frames.length}`
1114
+ };
1115
+ }
1116
+ }
1117
+ const selector = `[data-vsl-id="${localId}"], #${localId}, .${localId}`;
1010
1118
  const page = await browser.getPage();
1119
+ const target = targetFrame || page;
1011
1120
  switch (args.action) {
1012
1121
  case "click":
1013
- await page.click(selector, { timeout: 5e3 });
1122
+ await target.locator(selector).click({ timeout: 5e3 });
1014
1123
  await page.waitForTimeout(100);
1015
1124
  break;
1016
1125
  case "fill":
@@ -1021,7 +1130,7 @@ async function handleExecuteAction(args, browser, session) {
1021
1130
  error: "value is required for type action"
1022
1131
  };
1023
1132
  }
1024
- await page.fill(selector, args.value, { timeout: 5e3 });
1133
+ await target.locator(selector).fill(args.value, { timeout: 5e3 });
1025
1134
  break;
1026
1135
  case "scroll": {
1027
1136
  const DEFAULT_SCROLL_AMOUNT = 500;
@@ -1081,31 +1190,62 @@ async function handleExecuteAction(args, browser, session) {
1081
1190
  error: "value is required for select action"
1082
1191
  };
1083
1192
  }
1084
- await browser.evaluate(({ sel, val }) => {
1085
- const el = document.querySelector(sel);
1086
- if (el) el.value = val;
1087
- }, { sel: selector, val: args.value });
1193
+ if (targetFrame) {
1194
+ await browser.evaluateInFrame(targetFrame, ({ sel, val }) => {
1195
+ const el = document.querySelector(sel);
1196
+ if (el) el.value = val;
1197
+ }, { sel: selector, val: args.value });
1198
+ } else {
1199
+ await browser.evaluate(({ sel, val }) => {
1200
+ const el = document.querySelector(sel);
1201
+ if (el) el.value = val;
1202
+ }, { sel: selector, val: args.value });
1203
+ }
1088
1204
  break;
1089
1205
  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);
1206
+ if (targetFrame) {
1207
+ await browser.evaluateInFrame(targetFrame, (sel) => {
1208
+ const el = document.querySelector(sel);
1209
+ if (el) {
1210
+ const event = new MouseEvent("mouseover", { bubbles: true });
1211
+ el.dispatchEvent(event);
1212
+ }
1213
+ }, selector);
1214
+ } else {
1215
+ await browser.evaluate((sel) => {
1216
+ const el = document.querySelector(sel);
1217
+ if (el) {
1218
+ const event = new MouseEvent("mouseover", { bubbles: true });
1219
+ el.dispatchEvent(event);
1220
+ }
1221
+ }, selector);
1222
+ }
1103
1223
  break;
1104
1224
  case "blur":
1105
- await browser.evaluate((sel) => {
1106
- const el = document.querySelector(sel);
1107
- if (el) el.blur();
1108
- }, selector);
1225
+ if (targetFrame) {
1226
+ await browser.evaluateInFrame(targetFrame, (sel) => {
1227
+ const el = document.querySelector(sel);
1228
+ if (el) el.blur();
1229
+ }, selector);
1230
+ } else {
1231
+ await browser.evaluate((sel) => {
1232
+ const el = document.querySelector(sel);
1233
+ if (el) el.blur();
1234
+ }, selector);
1235
+ }
1236
+ break;
1237
+ case "focus":
1238
+ if (targetFrame) {
1239
+ await browser.evaluateInFrame(targetFrame, (sel) => {
1240
+ const el = document.querySelector(sel);
1241
+ if (el) el.focus();
1242
+ }, selector);
1243
+ } else {
1244
+ await browser.evaluate((sel) => {
1245
+ const el = document.querySelector(sel);
1246
+ if (el) el.focus();
1247
+ }, selector);
1248
+ }
1109
1249
  break;
1110
1250
  case "press": {
1111
1251
  if (!args.value) {
@@ -1121,19 +1261,37 @@ async function handleExecuteAction(args, browser, session) {
1121
1261
  case "download": {
1122
1262
  const downloadsBefore = browser.getDownloads();
1123
1263
  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);
1264
+ if (targetFrame) {
1265
+ await browser.evaluateInFrame(targetFrame, (url) => {
1266
+ const a = document.createElement("a");
1267
+ a.href = url;
1268
+ a.download = "";
1269
+ document.body.appendChild(a);
1270
+ a.click();
1271
+ document.body.removeChild(a);
1272
+ }, args.value);
1273
+ } else {
1274
+ await browser.evaluate((url) => {
1275
+ const a = document.createElement("a");
1276
+ a.href = url;
1277
+ a.download = "";
1278
+ document.body.appendChild(a);
1279
+ a.click();
1280
+ document.body.removeChild(a);
1281
+ }, args.value);
1282
+ }
1132
1283
  } else {
1133
- await browser.evaluate((sel) => {
1134
- const el = document.querySelector(sel);
1135
- if (el) el.click();
1136
- }, selector);
1284
+ if (targetFrame) {
1285
+ await browser.evaluateInFrame(targetFrame, (sel) => {
1286
+ const el = document.querySelector(sel);
1287
+ if (el) el.click();
1288
+ }, selector);
1289
+ } else {
1290
+ await browser.evaluate((sel) => {
1291
+ const el = document.querySelector(sel);
1292
+ if (el) el.click();
1293
+ }, selector);
1294
+ }
1137
1295
  }
1138
1296
  const pollDeadline = Date.now() + 5e3;
1139
1297
  let newDownload;
@@ -1197,7 +1355,7 @@ async function handleExecuteAction(args, browser, session) {
1197
1355
  error: "value must contain at least one file path"
1198
1356
  };
1199
1357
  }
1200
- await browser.uploadFile(selector, filePaths);
1358
+ await target.locator(selector).setInputFiles(filePaths.length === 1 ? filePaths[0] : filePaths);
1201
1359
  const result2 = {
1202
1360
  status: "success",
1203
1361
  data: {
@@ -2416,6 +2574,33 @@ var BrowserManager = class {
2416
2574
  /**
2417
2575
  * Выполняет JavaScript в контексте страницы.
2418
2576
  */
2577
+ /**
2578
+ * Возвращает все фреймы на странице (включая iframe).
2579
+ */
2580
+ async getFrames() {
2581
+ const page = await this.getPage();
2582
+ return page.frames();
2583
+ }
2584
+ /**
2585
+ * Находит фрейм по URL или имени.
2586
+ */
2587
+ async getFrame(urlOrName) {
2588
+ const page = await this.getPage();
2589
+ return page.frame(urlOrName);
2590
+ }
2591
+ /**
2592
+ * Выполняет JavaScript в контексте конкретного фрейма.
2593
+ */
2594
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
2595
+ async evaluateInFrame(frame, fn, ...args) {
2596
+ return frame.evaluate(fn, ...args);
2597
+ }
2598
+ /**
2599
+ * Возвращает Playwright Locator для элемента в фрейме.
2600
+ */
2601
+ async locatorInFrame(frame, selector) {
2602
+ return frame.locator(selector);
2603
+ }
2419
2604
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
2420
2605
  async evaluate(fn, ...args) {
2421
2606
  const page = await this.getPage();
@@ -2903,7 +3088,7 @@ var SessionManager = class {
2903
3088
  };
2904
3089
 
2905
3090
  // src/version.ts
2906
- var VSL_MCP_SERVER_VERSION = "1.2.7";
3091
+ var VSL_MCP_SERVER_VERSION = "1.2.8";
2907
3092
 
2908
3093
  // src/index.ts
2909
3094
  async function main() {