@thinkingos/vsl-mcp-server 1.1.4 → 1.2.2

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
@@ -123,10 +123,14 @@ var INTERACTIVE_TYPES = /* @__PURE__ */ new Set([
123
123
  "input",
124
124
  "select",
125
125
  "textarea",
126
- "file_input"
126
+ "file_input",
127
+ "tab",
128
+ "modal",
129
+ "dropdown_toggle"
127
130
  ]);
128
131
  var CONTAINER_TYPES = /* @__PURE__ */ new Set([
129
132
  "container",
133
+ "scrollable_container",
130
134
  "layout",
131
135
  "list",
132
136
  "grid",
@@ -189,12 +193,100 @@ function filterObjectsByDetailLevel(objects, level, options) {
189
193
  }
190
194
  return objects.map(filterRecursive).filter((obj) => obj !== null);
191
195
  }
192
- function applyDetailLevelFilter(document2, level, options) {
193
- if (level === "high") return document2;
194
- const objects = document2.objects;
195
- if (!objects || !Array.isArray(objects)) return document2;
196
- const filteredObjects = filterObjectsByDetailLevel(objects, level, options);
197
- return { ...document2, objects: filteredObjects };
196
+ function filterDiffByDetailLevel(diff, nextDoc, level, options) {
197
+ if (level === "high") return diff;
198
+ const visibleIds = /* @__PURE__ */ new Set();
199
+ const collectIds = (objects) => {
200
+ for (const obj of objects) {
201
+ visibleIds.add(obj.id);
202
+ if (obj.ch && obj.ch.length > 0) collectIds(obj.ch);
203
+ }
204
+ };
205
+ collectIds(filterObjectsByDetailLevel([...nextDoc.objects], level, options));
206
+ return {
207
+ ...diff,
208
+ changes: {
209
+ added: filterObjectsByDetailLevel([...diff.changes.added], level, options),
210
+ modified: diff.changes.modified.filter((m) => visibleIds.has(m.id)),
211
+ removed: diff.changes.removed,
212
+ unchanged_refs: diff.changes.unchanged_refs.filter((id) => visibleIds.has(id))
213
+ }
214
+ };
215
+ }
216
+
217
+ // src/utils/viewportFilter.ts
218
+ function computeVisibleWindow(viewport, scroll) {
219
+ if (!scroll) {
220
+ return { x: 0, y: 0, width: viewport.width, height: viewport.height };
221
+ }
222
+ return { x: scroll.x, y: scroll.y, width: viewport.width, height: viewport.height };
223
+ }
224
+ function computeScrollable(viewport, scroll) {
225
+ if (!scroll) {
226
+ return { top: false, bottom: false };
227
+ }
228
+ return {
229
+ top: scroll.y > 0,
230
+ bottom: scroll.y + viewport.height < scroll.height
231
+ };
232
+ }
233
+ function isVisible(p, s, win) {
234
+ return p[0] < win.x + win.width && p[0] + s[0] > win.x && p[1] < win.y + win.height && p[1] + s[1] > win.y;
235
+ }
236
+ function filterObjectsByViewport(objects, win) {
237
+ function filterRecursive(obj) {
238
+ const noCoords = !obj.p || !obj.s;
239
+ const selfVisible = noCoords || isVisible(obj.p, obj.s, win);
240
+ let filteredChildren = null;
241
+ let childrenChanged = false;
242
+ if (obj.ch && obj.ch.length > 0) {
243
+ filteredChildren = obj.ch.map(filterRecursive).filter((child) => child !== null);
244
+ childrenChanged = filteredChildren.length !== obj.ch.length || filteredChildren.some((child, index) => child !== obj.ch[index]);
245
+ }
246
+ if (selfVisible) {
247
+ if (filteredChildren && obj.ch && childrenChanged) {
248
+ const copy = { ...obj };
249
+ if (filteredChildren.length > 0) {
250
+ copy.ch = filteredChildren;
251
+ } else {
252
+ delete copy.ch;
253
+ }
254
+ return copy;
255
+ }
256
+ return obj;
257
+ }
258
+ if (filteredChildren && filteredChildren.length > 0) {
259
+ return { ...obj, ch: filteredChildren };
260
+ }
261
+ return null;
262
+ }
263
+ return objects.map(filterRecursive).filter((obj) => obj !== null);
264
+ }
265
+ function collectVisibleIds(objects, win, acc = /* @__PURE__ */ new Set()) {
266
+ for (const obj of objects) {
267
+ if (!obj.p || !obj.s || isVisible(obj.p, obj.s, win)) {
268
+ acc.add(obj.id);
269
+ }
270
+ if (obj.ch && obj.ch.length > 0) {
271
+ collectVisibleIds(obj.ch, win, acc);
272
+ }
273
+ }
274
+ return acc;
275
+ }
276
+ function filterDiffByViewport(diff, nextDoc, prevDoc, win) {
277
+ const nextVisible = collectVisibleIds(nextDoc.objects, win);
278
+ const prevVisible = prevDoc ? collectVisibleIds(prevDoc.objects, win) : null;
279
+ return {
280
+ ...diff,
281
+ changes: {
282
+ added: filterObjectsByViewport(diff.changes.added, win),
283
+ modified: diff.changes.modified.filter((m) => nextVisible.has(m.id)),
284
+ removed: diff.changes.removed.filter(
285
+ (r) => prevVisible ? prevVisible.has(r.id) : true
286
+ ),
287
+ unchanged_refs: diff.changes.unchanged_refs.filter((id) => nextVisible.has(id))
288
+ }
289
+ };
198
290
  }
199
291
 
200
292
  // src/tools/getSnapshot.ts
@@ -210,11 +302,9 @@ function countObjects(objects) {
210
302
  }
211
303
  return count;
212
304
  }
213
- function extractDomTreeInBrowser(options) {
214
- const includeOffscreen = options?.includeOffscreen ?? false;
305
+ function extractDomTreeInBrowser() {
215
306
  const viewportWidth = window.innerWidth;
216
307
  const viewportHeight = window.innerHeight;
217
- let culledCount = 0;
218
308
  const NON_RENDERABLE_TAGS = /* @__PURE__ */ new Set([
219
309
  "script",
220
310
  "style",
@@ -278,45 +368,43 @@ function extractDomTreeInBrowser(options) {
278
368
  return css;
279
369
  }
280
370
  function toRect(rect) {
281
- return { x: rect.x, y: rect.y, width: rect.width, height: rect.height };
371
+ return {
372
+ x: rect.x + window.scrollX,
373
+ y: rect.y + window.scrollY,
374
+ width: rect.width,
375
+ height: rect.height
376
+ };
282
377
  }
283
378
  function hasZeroBox(rect) {
284
379
  return rect.width <= 0 || rect.height <= 0;
285
380
  }
286
- function isInViewport(rect) {
287
- return rect.y < viewportHeight && rect.y + rect.height > 0;
288
- }
289
381
  function collectVisibleChildren(parent, parentPath) {
290
382
  const result = [];
291
383
  Array.from(parent.children).forEach((child, index) => {
292
- const path3 = [...parentPath, index];
384
+ const path2 = [...parentPath, index];
293
385
  const tag = child.tagName.toLowerCase();
294
386
  if (NON_RENDERABLE_TAGS.has(tag) || isDisplayNone(child)) {
295
387
  return;
296
388
  }
297
389
  const rect = toRect(child.getBoundingClientRect());
298
390
  if (isVisibilityHidden(child) || hasZeroBox(rect)) {
299
- result.push(...collectVisibleChildren(child, path3));
391
+ result.push(...collectVisibleChildren(child, path2));
300
392
  return;
301
393
  }
302
394
  const attributes = extractAttributes(child);
303
395
  const css = captureCss(child);
304
- if (attributes["aria-hidden"] === "true") {
396
+ if (attributes?.["aria-hidden"] === "true") {
305
397
  return;
306
398
  }
307
399
  if (css.opacity === "0" && css.pointerEvents === "none") {
308
400
  return;
309
401
  }
310
- if (!includeOffscreen && !isInViewport(rect)) {
311
- culledCount++;
312
- return;
313
- }
314
- const children = collectVisibleChildren(child, path3);
315
- const vslId = `${tag}_${path3.join("_")}`;
402
+ const children = collectVisibleChildren(child, path2);
403
+ const vslId = `${tag}_${path2.join("_")}`;
316
404
  child.setAttribute("data-vsl-id", vslId);
317
405
  result.push({
318
406
  tag,
319
- indexPath: path3,
407
+ indexPath: path2,
320
408
  rect,
321
409
  text: ownText(child) || null,
322
410
  attributes,
@@ -332,10 +420,25 @@ function extractDomTreeInBrowser(options) {
332
420
  return [{
333
421
  __type: "extraction_result",
334
422
  elements,
335
- culled_count: culledCount,
336
- viewport: { width: viewportWidth, height: viewportHeight }
423
+ viewport: { width: viewportWidth, height: viewportHeight },
424
+ scroll: {
425
+ x: window.scrollX,
426
+ y: window.scrollY,
427
+ width: document.documentElement ? document.documentElement.scrollWidth : 0,
428
+ height: document.documentElement ? document.documentElement.scrollHeight : 0
429
+ }
337
430
  }];
338
431
  }
432
+ async function extractDomTree(browser) {
433
+ const extractionResult = await browser.evaluate(
434
+ extractDomTreeInBrowser
435
+ );
436
+ const extraction = Array.isArray(extractionResult) ? extractionResult[0] : void 0;
437
+ if (!extraction || !Array.isArray(extraction.elements)) {
438
+ throw new Error("Failed to extract DOM tree: empty result");
439
+ }
440
+ return extraction;
441
+ }
339
442
  async function handleGetSnapshot(args, browser, session, _config) {
340
443
  const startTime = Date.now();
341
444
  const detailLevel = args.detail_level || "medium";
@@ -355,18 +458,35 @@ async function handleGetSnapshot(args, browser, session, _config) {
355
458
  }
356
459
  try {
357
460
  if (ttl > 0) {
358
- const cacheKey = `${args.url || "current"}:${detailLevel}`;
461
+ const cacheKey = args.url || "current";
359
462
  const cached = snapshotCache.get(cacheKey);
360
463
  if (cached && Date.now() - cached.timestamp < ttl) {
464
+ const scrollable2 = computeScrollable(cached.fullDocument.canvas.viewport, cached.scrollContext);
465
+ const filteredObjects2 = args.full ? cached.fullDocument.objects : filterObjectsByDetailLevel(
466
+ filterObjectsByViewport(
467
+ cached.fullDocument.objects,
468
+ computeVisibleWindow(cached.fullDocument.canvas.viewport, cached.scrollContext)
469
+ ),
470
+ detailLevel
471
+ );
472
+ const filteredResult2 = { ...cached.fullDocument, objects: filteredObjects2 };
473
+ const executionTimeMs2 = Date.now() - startTime;
474
+ const resultJson2 = JSON.stringify(filteredResult2);
475
+ const jsonSizeBytes2 = Buffer.byteLength(resultJson2, "utf-8");
476
+ const estimatedTokens2 = Math.ceil(jsonSizeBytes2 / 4);
477
+ const objectCount2 = countObjects(filteredObjects2);
478
+ const totalObjectsCount2 = countObjects(cached.fullDocument.objects);
361
479
  return {
362
- ...cached.result,
480
+ status: "success",
481
+ data: filteredResult2,
363
482
  metadata: {
364
- ...cached.result.metadata,
365
- json_size_bytes: cached.result.metadata?.json_size_bytes ?? 0,
366
- estimated_tokens: cached.result.metadata?.estimated_tokens ?? 0,
367
- object_count: cached.result.metadata?.object_count ?? 0,
368
- execution_time_ms: Date.now() - startTime,
369
- timestamp: (/* @__PURE__ */ new Date()).toISOString()
483
+ json_size_bytes: jsonSizeBytes2,
484
+ estimated_tokens: estimatedTokens2,
485
+ object_count: objectCount2,
486
+ total_objects_count: totalObjectsCount2,
487
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
488
+ execution_time_ms: executionTimeMs2,
489
+ scrollable: scrollable2
370
490
  }
371
491
  };
372
492
  }
@@ -381,26 +501,8 @@ async function handleGetSnapshot(args, browser, session, _config) {
381
501
  error: "Playwright is not installed. Install it with: npm install playwright"
382
502
  };
383
503
  }
384
- const includeOffscreen = args.include_offscreen ?? false;
385
- const extractionResult = await browser.evaluate(
386
- (opts) => extractDomTreeInBrowser(opts),
387
- { includeOffscreen }
388
- );
389
- if (!extractionResult || extractionResult.length === 0) {
390
- return {
391
- status: "error",
392
- error: "Failed to extract DOM tree: empty result"
393
- };
394
- }
395
- const extraction = extractionResult[0];
396
- if (!extraction) {
397
- return {
398
- status: "error",
399
- error: "Failed to extract DOM tree: extraction result is undefined"
400
- };
401
- }
504
+ const extraction = await extractDomTree(browser);
402
505
  const extractedElements = extraction.elements;
403
- const culledCount = extraction.culled_count;
404
506
  const viewport = extraction.viewport;
405
507
  const url = args.url || await browser.evaluate(() => window.location.href);
406
508
  const title = await browser.evaluate(() => document.title);
@@ -408,7 +510,8 @@ async function handleGetSnapshot(args, browser, session, _config) {
408
510
  viewport,
409
511
  url,
410
512
  title,
411
- timestamp: (/* @__PURE__ */ new Date()).toISOString()
513
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
514
+ scroll: extraction.scroll
412
515
  };
413
516
  const result = session.snapshotFromElements(
414
517
  extractedElements,
@@ -416,14 +519,22 @@ async function handleGetSnapshot(args, browser, session, _config) {
416
519
  );
417
520
  const currentDoc = isVslDiff(result) ? session.getSnapshot() : result;
418
521
  await injectVslIdsIntoDom(browser, currentDoc.objects);
419
- const filteredObjects = filterObjectsByDetailLevel(currentDoc.objects, detailLevel);
420
- const filteredResult = isVslDiff(result) ? { ...result, objects: filteredObjects } : { ...result, objects: filteredObjects };
522
+ const scrollContext = session.getScrollContext();
523
+ const win = computeVisibleWindow(currentDoc.canvas.viewport, scrollContext);
524
+ const scrollable = computeScrollable(currentDoc.canvas.viewport, scrollContext);
525
+ const viewportFiltered = filterObjectsByViewport(currentDoc.objects, win);
526
+ const filteredObjects = args.full ? currentDoc.objects : filterObjectsByDetailLevel(viewportFiltered, detailLevel);
527
+ const filteredResult = isVslDiff(result) && !args.full ? filterDiffByDetailLevel(
528
+ filterDiffByViewport(result, currentDoc, session.getPreviousSnapshot(), win),
529
+ currentDoc,
530
+ detailLevel
531
+ ) : { ...currentDoc, objects: filteredObjects };
421
532
  const executionTimeMs = Date.now() - startTime;
422
533
  const resultJson = JSON.stringify(filteredResult);
423
534
  const jsonSizeBytes = Buffer.byteLength(resultJson, "utf-8");
424
535
  const estimatedTokens = Math.ceil(jsonSizeBytes / 4);
425
536
  const objectCount = countObjects(filteredObjects);
426
- const totalObjectsCount = objectCount + culledCount;
537
+ const totalObjectsCount = countObjects(currentDoc.objects);
427
538
  const metadata = {
428
539
  json_size_bytes: jsonSizeBytes,
429
540
  estimated_tokens: estimatedTokens,
@@ -431,20 +542,15 @@ async function handleGetSnapshot(args, browser, session, _config) {
431
542
  total_objects_count: totalObjectsCount,
432
543
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
433
544
  execution_time_ms: executionTimeMs,
434
- culled_offscreen_count: culledCount
545
+ scrollable
435
546
  };
436
547
  if (ttl > 0) {
437
- const cacheKey = `${args.url || "current"}:${detailLevel}`;
438
- const resultToCache = {
439
- status: "success",
440
- data: filteredResult,
441
- metadata
442
- };
548
+ const cacheKey = args.url || "current";
443
549
  snapshotCache.set(cacheKey, {
444
- result: resultToCache,
550
+ fullDocument: currentDoc,
551
+ scrollContext: session.getScrollContext(),
445
552
  timestamp: Date.now(),
446
- url: args.url || "current",
447
- detailLevel
553
+ url: args.url || "current"
448
554
  });
449
555
  }
450
556
  return {
@@ -474,36 +580,6 @@ function computeToolMetrics(result, startTime) {
474
580
  };
475
581
  }
476
582
 
477
- // src/tools/getDiff.ts
478
- async function handleGetDiff(session) {
479
- const startTime = Date.now();
480
- try {
481
- if (!session.hasSnapshot()) {
482
- return {
483
- status: "error",
484
- error: "No snapshot available. Call vsl_get_snapshot first."
485
- };
486
- }
487
- const diff = session.getDiff();
488
- if (!diff) {
489
- return {
490
- status: "error",
491
- error: "No previous snapshot to compare against. This is the first snapshot."
492
- };
493
- }
494
- return {
495
- status: "success",
496
- data: diff,
497
- metadata: computeToolMetrics(diff, startTime)
498
- };
499
- } catch (error) {
500
- return {
501
- status: "error",
502
- error: `vsl_get_diff failed: ${error instanceof Error ? error.message : String(error)}`
503
- };
504
- }
505
- }
506
-
507
583
  // src/tools/navigate.ts
508
584
  async function handleNavigate(args, browser, session) {
509
585
  const startTime = Date.now();
@@ -533,22 +609,25 @@ async function handleNavigate(args, browser, session) {
533
609
  const title = await browser.evaluate(() => document.title);
534
610
  let snapshot;
535
611
  let snapshotWarning;
612
+ let scrollableMeta;
536
613
  try {
537
- const elements = await browser.evaluate(extractDomTreeInBrowser);
538
- const viewport = await browser.evaluate(() => ({
539
- width: window.innerWidth,
540
- height: window.innerHeight
541
- }));
614
+ const extraction = await extractDomTree(browser);
542
615
  const input = {
543
- viewport,
616
+ viewport: extraction.viewport,
544
617
  url: args.url,
545
618
  title,
546
- timestamp: (/* @__PURE__ */ new Date()).toISOString()
619
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
620
+ scroll: extraction.scroll
547
621
  };
548
- session.snapshotFromElements(elements, input);
549
- snapshot = session.getSnapshot();
622
+ session.snapshotFromElements(extraction.elements, input);
550
623
  const currentDoc = session.getSnapshot();
551
624
  await injectVslIdsIntoDom(browser, currentDoc.objects);
625
+ const scrollContext = session.getScrollContext();
626
+ const win = computeVisibleWindow(currentDoc.canvas.viewport, scrollContext);
627
+ scrollableMeta = computeScrollable(currentDoc.canvas.viewport, scrollContext);
628
+ const viewportFiltered = filterObjectsByViewport(currentDoc.objects, win);
629
+ const filteredObjects = filterObjectsByDetailLevel(viewportFiltered, "medium");
630
+ snapshot = { ...currentDoc, objects: filteredObjects };
552
631
  } catch (error) {
553
632
  const errorMessage = `Failed to extract snapshot: ${error instanceof Error ? error.message : String(error)}`;
554
633
  console.error("[vsl_navigate]", errorMessage, error);
@@ -562,7 +641,7 @@ async function handleNavigate(args, browser, session) {
562
641
  const result = {
563
642
  status: "success",
564
643
  data,
565
- metadata: computeToolMetrics(data, startTime)
644
+ metadata: { ...computeToolMetrics(data, startTime), scrollable: scrollableMeta }
566
645
  };
567
646
  if (snapshotWarning) {
568
647
  result.warning = snapshotWarning;
@@ -615,28 +694,67 @@ var VALID_ACTIONS = [
615
694
  ];
616
695
  async function getStateAfterAction(session, browser, url) {
617
696
  try {
618
- const elements = await browser.evaluate(extractDomTreeInBrowser);
619
- const viewport = await browser.evaluate(() => ({
620
- width: window.innerWidth,
621
- height: window.innerHeight
622
- }));
697
+ const extraction = await extractDomTree(browser);
623
698
  const input = {
624
- viewport,
699
+ viewport: extraction.viewport,
625
700
  url,
626
701
  title: "",
627
- timestamp: (/* @__PURE__ */ new Date()).toISOString()
702
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
703
+ scroll: extraction.scroll
628
704
  };
629
- session.snapshotFromElements(elements, input);
705
+ session.snapshotFromElements(extraction.elements, input);
706
+ const fullDoc = session.getSnapshot();
707
+ await injectVslIdsIntoDom(browser, fullDoc.objects);
708
+ const scrollContext = session.getScrollContext();
709
+ const win = computeVisibleWindow(fullDoc.canvas.viewport, scrollContext);
710
+ const scrollable = computeScrollable(fullDoc.canvas.viewport, scrollContext);
630
711
  const diff = session.getDiff();
631
- const snapshot = session.getSnapshot();
632
- await injectVslIdsIntoDom(browser, snapshot.objects);
633
- return { diff, snapshot };
712
+ const filteredDiff = diff ? filterDiffByDetailLevel(
713
+ filterDiffByViewport(diff, fullDoc, session.getPreviousSnapshot(), win),
714
+ fullDoc,
715
+ "medium"
716
+ ) : diff;
717
+ const viewportFiltered = filterObjectsByViewport(fullDoc.objects, win);
718
+ const filteredObjects = filterObjectsByDetailLevel(viewportFiltered, "medium");
719
+ const snapshot = { ...fullDoc, objects: filteredObjects };
720
+ return { diff: filteredDiff, snapshot, scrollable };
634
721
  } catch (error) {
635
722
  const errorMessage = `Failed to get state after action: ${error instanceof Error ? error.message : String(error)}`;
636
723
  console.error("[vsl_execute_action]", errorMessage, error);
637
724
  return { error: errorMessage };
638
725
  }
639
726
  }
727
+ function serverSideScrollFallback(prev, dx, dy, viewport) {
728
+ const width = prev?.width ?? 0;
729
+ const height = prev?.height ?? 0;
730
+ const maxX = Math.max(0, width - (viewport?.width ?? 0));
731
+ const maxY = Math.max(0, height - (viewport?.height ?? 0));
732
+ return {
733
+ x: Math.min(Math.max(0, (prev?.x ?? 0) + dx), maxX),
734
+ y: Math.min(Math.max(0, (prev?.y ?? 0) + dy), maxY),
735
+ width,
736
+ height
737
+ };
738
+ }
739
+ function buildScrollStateFromCache(session, scrollContext) {
740
+ try {
741
+ const fullDoc = session.getSnapshot();
742
+ const viewport = fullDoc.canvas?.viewport;
743
+ if (!viewport || typeof viewport.width !== "number" || typeof viewport.height !== "number") {
744
+ return { snapshot: fullDoc };
745
+ }
746
+ const win = computeVisibleWindow(viewport, scrollContext);
747
+ const scrollable = computeScrollable(viewport, scrollContext);
748
+ const viewportFiltered = filterObjectsByViewport(fullDoc.objects ?? [], win);
749
+ const filteredObjects = filterObjectsByDetailLevel(viewportFiltered, "medium");
750
+ const snapshot = { ...fullDoc, objects: filteredObjects };
751
+ return { snapshot, scrollable };
752
+ } catch (error) {
753
+ const errorMessage = `Failed to build scroll state from cache: ${error instanceof Error ? error.message : String(error)}`;
754
+ console.error("[vsl_execute_action]", errorMessage, error);
755
+ return { error: errorMessage };
756
+ }
757
+ }
640
758
  async function handleExecuteAction(args, browser, session) {
641
759
  const startTime = Date.now();
642
760
  try {
@@ -721,15 +839,16 @@ async function handleExecuteAction(args, browser, session) {
721
839
  return document.querySelector("[data-vsl-id]") !== null;
722
840
  });
723
841
  if (!hasVslIds) {
724
- const elements = await browser.evaluate(extractDomTreeInBrowser);
725
- const viewport = await browser.evaluate(() => ({ width: window.innerWidth, height: window.innerHeight }));
842
+ const extraction = await extractDomTree(browser);
726
843
  const input = {
727
- viewport,
844
+ viewport: extraction.viewport,
728
845
  url: snapshotUrl || "",
729
846
  title: "",
730
- timestamp: (/* @__PURE__ */ new Date()).toISOString()
847
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
848
+ // Метаданные скролла для единого пайплайна отдачи (АС[3])
849
+ scroll: extraction.scroll
731
850
  };
732
- session.snapshotFromElements(elements, input);
851
+ session.snapshotFromElements(extraction.elements, input);
733
852
  }
734
853
  const selector = `[data-vsl-id="${args.target_id}"], #${args.target_id}, .${args.target_id}`;
735
854
  const page = await browser.getPage();
@@ -760,7 +879,44 @@ async function handleExecuteAction(args, browser, session) {
760
879
  window.scrollBy(dx2, dy2);
761
880
  }, { dx, dy });
762
881
  await page.waitForTimeout(50);
763
- break;
882
+ const prevScroll = session.getScrollContext();
883
+ let newScroll = null;
884
+ try {
885
+ const raw = await browser.evaluate(() => ({
886
+ x: window.scrollX,
887
+ y: window.scrollY,
888
+ width: document.documentElement.scrollWidth,
889
+ height: document.documentElement.scrollHeight
890
+ }));
891
+ const r = raw;
892
+ if (r !== null && typeof r === "object" && typeof r.x === "number" && typeof r.y === "number" && typeof r.width === "number" && typeof r.height === "number") {
893
+ newScroll = r;
894
+ }
895
+ } catch {
896
+ }
897
+ if (!newScroll) {
898
+ newScroll = serverSideScrollFallback(prevScroll, dx, dy, session.getSnapshot().canvas?.viewport);
899
+ }
900
+ session.setScrollContext(newScroll);
901
+ const scrollResult = {
902
+ status: "success",
903
+ data: {
904
+ action: args.action,
905
+ target_id: args.target_id,
906
+ success: true
907
+ }
908
+ };
909
+ if (args.return_state !== false) {
910
+ const state = buildScrollStateFromCache(session, newScroll);
911
+ if (scrollResult.data) {
912
+ scrollResult.data.state = state;
913
+ if (state.error) {
914
+ scrollResult.warning = `State extraction failed: ${state.error}`;
915
+ }
916
+ }
917
+ }
918
+ scrollResult.metadata = computeToolMetrics(scrollResult.data, startTime);
919
+ return scrollResult;
764
920
  }
765
921
  case "select":
766
922
  if (!args.value) {
@@ -807,50 +963,69 @@ async function handleExecuteAction(args, browser, session) {
807
963
  break;
808
964
  }
809
965
  case "download": {
966
+ const downloadsBefore = browser.getDownloads();
810
967
  if (args.value) {
811
- const downloadId = await browser.evaluate((url) => {
968
+ await browser.evaluate((url) => {
812
969
  const a = document.createElement("a");
813
970
  a.href = url;
814
971
  a.download = "";
815
972
  document.body.appendChild(a);
816
973
  a.click();
817
974
  document.body.removeChild(a);
818
- const id = `download_${Date.now()}_${Math.random().toString(36).slice(2)}`;
819
- return id;
820
975
  }, args.value);
821
- const filename = args.value.split("/").pop() || "download";
822
- const result2 = {
823
- status: "success",
824
- data: {
825
- action: args.action,
826
- target_id: args.target_id,
827
- success: true,
828
- download: {
829
- downloadId,
830
- filename,
831
- url: args.value,
832
- status: "pending"
833
- }
834
- }
976
+ } else {
977
+ await browser.evaluate((sel) => {
978
+ const el = document.querySelector(sel);
979
+ if (el) el.click();
980
+ }, selector);
981
+ }
982
+ const pollDeadline = Date.now() + 5e3;
983
+ let newDownload;
984
+ while (!newDownload && Date.now() < pollDeadline) {
985
+ newDownload = browser.getDownloads().find(
986
+ (d) => !downloadsBefore.some((b) => b.downloadId === d.downloadId)
987
+ );
988
+ if (!newDownload) {
989
+ await browser.evaluate(() => new Promise((resolve) => setTimeout(resolve, 50)));
990
+ }
991
+ }
992
+ if (!newDownload) {
993
+ return {
994
+ status: "error",
995
+ error: "Download not detected after click (timeout 5000ms). The page may navigate instead of downloading."
835
996
  };
836
- if (args.return_state !== false) {
837
- const currentUrl = await browser.evaluate(() => window.location.href);
838
- const state = await getStateAfterAction(session, browser, currentUrl);
839
- if (result2.data) {
840
- result2.data.state = state;
841
- if (state.error) {
842
- result2.warning = `State extraction failed: ${state.error}`;
843
- }
997
+ }
998
+ const downloadInfo = await browser.waitForDownload(newDownload.downloadId, args.timeout);
999
+ if (args.save_path) {
1000
+ await browser.saveDownload(downloadInfo.downloadId, args.save_path);
1001
+ }
1002
+ const result2 = {
1003
+ status: "success",
1004
+ data: {
1005
+ action: args.action,
1006
+ target_id: args.target_id,
1007
+ success: downloadInfo.status === "completed",
1008
+ download: {
1009
+ downloadId: downloadInfo.downloadId,
1010
+ filename: downloadInfo.filename,
1011
+ url: downloadInfo.url,
1012
+ status: downloadInfo.status,
1013
+ path: args.save_path ?? downloadInfo.path ?? void 0
1014
+ }
1015
+ }
1016
+ };
1017
+ if (args.return_state !== false) {
1018
+ const currentUrl = await browser.evaluate(() => window.location.href);
1019
+ const state = await getStateAfterAction(session, browser, currentUrl);
1020
+ if (result2.data) {
1021
+ result2.data.state = state;
1022
+ if (state.error) {
1023
+ result2.warning = `State extraction failed: ${state.error}`;
844
1024
  }
845
1025
  }
846
- result2.metadata = computeToolMetrics(result2.data, startTime);
847
- return result2;
848
1026
  }
849
- await browser.evaluate((sel) => {
850
- const el = document.querySelector(sel);
851
- if (el) el.click();
852
- }, selector);
853
- break;
1027
+ result2.metadata = computeToolMetrics(result2.data, startTime);
1028
+ return result2;
854
1029
  }
855
1030
  case "upload": {
856
1031
  if (!args.value) {
@@ -1015,8 +1190,8 @@ async function handleGetVisual(args, browser, session) {
1015
1190
  const rect2 = el.getBoundingClientRect();
1016
1191
  const viewportWidth = window.innerWidth;
1017
1192
  const viewportHeight = window.innerHeight;
1018
- const isVisible = rect2.top >= 0 && rect2.left >= 0 && rect2.bottom <= viewportHeight && rect2.right <= viewportWidth;
1019
- if (isVisible) {
1193
+ const isVisible2 = rect2.top >= 0 && rect2.left >= 0 && rect2.bottom <= viewportHeight && rect2.right <= viewportWidth;
1194
+ if (isVisible2) {
1020
1195
  return { needsScroll: false, scrolled: false };
1021
1196
  }
1022
1197
  el.scrollIntoView({ block: "center", inline: "center", behavior: "instant" });
@@ -1406,21 +1581,43 @@ async function handleReadPage(args, browser, session, _config) {
1406
1581
  await browser.navigate(args.url);
1407
1582
  const html = await browser.getContent();
1408
1583
  const isSpa = detectSpa(html);
1409
- const vslDocument = await extractVslFromPage(browser);
1410
1584
  const hadPreviousSnapshot2 = session.hasSnapshot();
1411
- session.setSnapshot(vslDocument);
1585
+ try {
1586
+ const extraction = await extractDomTree(browser);
1587
+ session.snapshotFromElements(extraction.elements, {
1588
+ viewport: extraction.viewport,
1589
+ url: args.url,
1590
+ title: extractTitle(html) || "",
1591
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
1592
+ scroll: extraction.scroll
1593
+ });
1594
+ await injectVslIdsIntoDom(browser, session.getSnapshot().objects);
1595
+ } catch (extractError) {
1596
+ console.error("[vsl_read_page] SDK extraction failed, falling back to legacy extraction:", extractError);
1597
+ const fallbackDoc = await extractVslFromPage(browser);
1598
+ session.setSnapshot(fallbackDoc);
1599
+ }
1412
1600
  const hasDiff2 = hadPreviousSnapshot2;
1413
1601
  const readableHtml = readable ? applyReadableFilter(html) : html;
1414
1602
  const content = extractTextContent(readableHtml);
1415
1603
  const rawSnapshot2 = session.getSnapshot();
1416
1604
  const rawDiff2 = hasDiff2 ? session.getDiff() : void 0;
1417
- const fullSnapshot2 = rawSnapshot2 ? applyDetailLevelFilter(rawSnapshot2, detailLevel) : void 0;
1418
- const fullDiff2 = rawDiff2;
1605
+ const scrollContext2 = session.getScrollContext();
1606
+ const win2 = computeVisibleWindow(rawSnapshot2.canvas.viewport, scrollContext2);
1607
+ const scrollable2 = computeScrollable(rawSnapshot2.canvas.viewport, scrollContext2);
1608
+ const viewportFiltered2 = filterObjectsByViewport(rawSnapshot2.objects, win2);
1609
+ const filteredObjects2 = filterObjectsByDetailLevel(viewportFiltered2, detailLevel);
1610
+ const fullSnapshot2 = { ...rawSnapshot2, objects: filteredObjects2 };
1611
+ const fullDiff2 = rawSnapshot2 && rawDiff2 ? filterDiffByDetailLevel(
1612
+ filterDiffByViewport(rawDiff2, rawSnapshot2, session.getPreviousSnapshot(), win2),
1613
+ rawSnapshot2,
1614
+ detailLevel
1615
+ ) : rawDiff2;
1419
1616
  const data2 = {
1420
1617
  url: args.url,
1421
1618
  mode: "render",
1422
1619
  content,
1423
- vslDocument,
1620
+ vslDocument: rawSnapshot2,
1424
1621
  snapshot: fullSnapshot2,
1425
1622
  diff: fullDiff2,
1426
1623
  hasDiff: hasDiff2,
@@ -1430,7 +1627,8 @@ async function handleReadPage(args, browser, session, _config) {
1430
1627
  isSpa,
1431
1628
  readableApplied: readable,
1432
1629
  detail_level: detailLevel,
1433
- ...computeVslMetrics(fullSnapshot2)
1630
+ ...computeVslMetrics(fullSnapshot2),
1631
+ scrollable: scrollable2
1434
1632
  }
1435
1633
  };
1436
1634
  return {
@@ -1447,8 +1645,17 @@ async function handleReadPage(args, browser, session, _config) {
1447
1645
  const hasDiff = hadPreviousSnapshot;
1448
1646
  const rawSnapshot = session.getSnapshot();
1449
1647
  const rawDiff = hasDiff ? session.getDiff() : void 0;
1450
- const fullSnapshot = rawSnapshot ? applyDetailLevelFilter(rawSnapshot, detailLevel) : void 0;
1451
- const fullDiff = rawDiff;
1648
+ const scrollContext = session.getScrollContext();
1649
+ const win = computeVisibleWindow(rawSnapshot.canvas.viewport, scrollContext);
1650
+ const scrollable = computeScrollable(rawSnapshot.canvas.viewport, scrollContext);
1651
+ const viewportFiltered = filterObjectsByViewport(rawSnapshot.objects, win);
1652
+ const filteredObjects = filterObjectsByDetailLevel(viewportFiltered, detailLevel);
1653
+ const fullSnapshot = { ...rawSnapshot, objects: filteredObjects };
1654
+ const fullDiff = rawSnapshot && rawDiff ? filterDiffByDetailLevel(
1655
+ filterDiffByViewport(rawDiff, rawSnapshot, session.getPreviousSnapshot(), win),
1656
+ rawSnapshot,
1657
+ detailLevel
1658
+ ) : rawDiff;
1452
1659
  const data = {
1453
1660
  url: args.url,
1454
1661
  mode: "http",
@@ -1463,7 +1670,8 @@ async function handleReadPage(args, browser, session, _config) {
1463
1670
  isSpa: httpResult.isSpa,
1464
1671
  readableApplied: httpResult.readableApplied,
1465
1672
  detail_level: detailLevel,
1466
- ...computeVslMetrics(fullSnapshot)
1673
+ ...computeVslMetrics(fullSnapshot),
1674
+ scrollable
1467
1675
  }
1468
1676
  };
1469
1677
  return {
@@ -1583,31 +1791,6 @@ function computeVslMetrics(snapshot) {
1583
1791
  }
1584
1792
  }
1585
1793
 
1586
- // src/tools/getFullJson.ts
1587
- async function handleGetFullJson(_args, session) {
1588
- const startTime = Date.now();
1589
- try {
1590
- if (!session.hasSnapshot()) {
1591
- return {
1592
- status: "error",
1593
- error: "No VSL snapshot available",
1594
- hint: "Call vsl_get_snapshot first to capture the current page state."
1595
- };
1596
- }
1597
- const snapshot = session.getSnapshot();
1598
- return {
1599
- status: "success",
1600
- data: snapshot,
1601
- metadata: computeToolMetrics(snapshot, startTime)
1602
- };
1603
- } catch (error) {
1604
- return {
1605
- status: "error",
1606
- error: `vsl_get_full_json failed: ${error instanceof Error ? error.message : String(error)}`
1607
- };
1608
- }
1609
- }
1610
-
1611
1794
  // src/tools/getTextBlock.ts
1612
1795
  async function handleGetTextBlock(args, session) {
1613
1796
  const startTime = Date.now();
@@ -1655,147 +1838,6 @@ async function handleGetTextBlock(args, session) {
1655
1838
  }
1656
1839
  }
1657
1840
 
1658
- // src/tools/download.ts
1659
- import path from "path";
1660
- async function handleDownload(args, browser, session) {
1661
- try {
1662
- if (!args.target_id && !args.value) {
1663
- return {
1664
- status: "error",
1665
- error: "Either target_id (element to click) or value (URL to download) is required"
1666
- };
1667
- }
1668
- if (args.target_id !== void 0 && typeof args.target_id !== "string") {
1669
- return {
1670
- status: "error",
1671
- error: "target_id must be a string"
1672
- };
1673
- }
1674
- if (args.value !== void 0 && typeof args.value !== "string") {
1675
- return {
1676
- status: "error",
1677
- error: "value must be a string"
1678
- };
1679
- }
1680
- if (args.timeout !== void 0 && (typeof args.timeout !== "number" || args.timeout <= 0)) {
1681
- return {
1682
- status: "error",
1683
- error: "timeout must be a positive number (milliseconds)"
1684
- };
1685
- }
1686
- if (args.save_path !== void 0 && typeof args.save_path !== "string") {
1687
- return {
1688
- status: "error",
1689
- error: "save_path must be a string"
1690
- };
1691
- }
1692
- if (args.target_id && !session.hasSnapshot()) {
1693
- return {
1694
- status: "error",
1695
- error: "No snapshot available. Call vsl_get_snapshot first."
1696
- };
1697
- }
1698
- const isAvailable = await browser.isAvailable();
1699
- if (!isAvailable) {
1700
- return {
1701
- status: "error",
1702
- error: "Playwright is not installed. Install it with: npm install playwright"
1703
- };
1704
- }
1705
- if (session.hasSnapshot()) {
1706
- const snapshot = session.getSnapshot();
1707
- const snapshotUrl = snapshot.canvas.url;
1708
- if (snapshotUrl) {
1709
- const currentUrl = await browser.evaluate(() => window.location.href);
1710
- if (currentUrl !== snapshotUrl) {
1711
- await browser.navigate(snapshotUrl);
1712
- }
1713
- }
1714
- }
1715
- let downloadUrl;
1716
- if (args.value) {
1717
- downloadUrl = args.value;
1718
- await browser.evaluate((url) => {
1719
- const a = document.createElement("a");
1720
- a.href = url;
1721
- a.download = "";
1722
- document.body.appendChild(a);
1723
- a.click();
1724
- document.body.removeChild(a);
1725
- }, args.value);
1726
- } else if (args.target_id) {
1727
- const selector = `[data-vsl-id="${args.target_id}"], #${args.target_id}, .${args.target_id}`;
1728
- downloadUrl = await browser.evaluate((sel) => {
1729
- const el = document.querySelector(sel);
1730
- if (el) {
1731
- let url = "";
1732
- if (el instanceof HTMLAnchorElement && el.href) {
1733
- url = el.href;
1734
- }
1735
- el.click();
1736
- return url;
1737
- }
1738
- return "";
1739
- }, selector);
1740
- if (!downloadUrl) {
1741
- downloadUrl = await browser.evaluate(() => window.location.href);
1742
- }
1743
- } else {
1744
- return {
1745
- status: "error",
1746
- error: "Invalid arguments: provide either target_id or value"
1747
- };
1748
- }
1749
- const downloads = browser.getDownloads();
1750
- if (downloads.length === 0) {
1751
- return {
1752
- status: "error",
1753
- error: "Download was not initiated. Check if the element triggers a file download."
1754
- };
1755
- }
1756
- const latestDownload = downloads[downloads.length - 1];
1757
- if (!latestDownload) {
1758
- return {
1759
- status: "error",
1760
- error: "No active downloads found after triggering download."
1761
- };
1762
- }
1763
- const downloadId = latestDownload.downloadId;
1764
- const timeout = args.timeout;
1765
- const result = await browser.waitForDownload(downloadId, timeout);
1766
- if (args.save_path && result.status === "completed") {
1767
- const isAbsolute = args.save_path.startsWith("/");
1768
- if (!isAbsolute) {
1769
- const resolvedBase = path.resolve((await import("os")).homedir(), ".vsl", "downloads");
1770
- const resolvedSave = path.resolve(resolvedBase, args.save_path);
1771
- if (!resolvedSave.startsWith(resolvedBase + path.sep) && resolvedSave !== resolvedBase) {
1772
- return {
1773
- status: "error",
1774
- error: `Path traversal detected: save_path '${args.save_path}' escapes downloads directory`
1775
- };
1776
- }
1777
- }
1778
- await browser.saveDownload(downloadId, args.save_path);
1779
- result.path = args.save_path;
1780
- }
1781
- return {
1782
- status: "success",
1783
- data: {
1784
- downloadId: result.downloadId,
1785
- filename: result.filename,
1786
- url: result.url,
1787
- status: result.status,
1788
- path: result.path
1789
- }
1790
- };
1791
- } catch (error) {
1792
- return {
1793
- status: "error",
1794
- error: `vsl_download failed: ${error instanceof Error ? error.message : String(error)}`
1795
- };
1796
- }
1797
- }
1798
-
1799
1841
  // src/tools/index.ts
1800
1842
  var VSL_TOOLS = [
1801
1843
  {
@@ -1816,18 +1858,14 @@ var VSL_TOOLS = [
1816
1858
  ttl: {
1817
1859
  type: "number",
1818
1860
  description: "TTL \u043A\u044D\u0448\u0430 \u0432 \u043C\u0438\u043B\u043B\u0438\u0441\u0435\u043A\u0443\u043D\u0434\u0430\u0445 (default: 5000 = 5s). \u0423\u0441\u0442\u0430\u043D\u043E\u0432\u0438\u0442\u0435 0 \u0434\u043B\u044F \u043E\u0442\u043A\u043B\u044E\u0447\u0435\u043D\u0438\u044F \u043A\u044D\u0448\u0438\u0440\u043E\u0432\u0430\u043D\u0438\u044F."
1861
+ },
1862
+ full: {
1863
+ type: "boolean",
1864
+ description: "\u041F\u043E\u043B\u043D\u044B\u0439 \u0440\u0435\u0436\u0438\u043C: \u0432\u043E\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0432\u0435\u0441\u044C \u0434\u043E\u043A\u0443\u043C\u0435\u043D\u0442 \u0431\u0435\u0437 \u0444\u0438\u043B\u044C\u0442\u0440\u043E\u0432 (\u0437\u0430\u043C\u0435\u043D\u0430 \u0443\u0434\u0430\u043B\u0451\u043D\u043D\u043E\u0433\u043E \u0442\u0443\u043B\u0430 \u043F\u043E\u043B\u043D\u043E\u0433\u043E \u0441\u043D\u0430\u043F\u0448\u043E\u0442\u0430)"
1819
1865
  }
1820
1866
  }
1821
1867
  }
1822
1868
  },
1823
- {
1824
- name: "vsl_get_diff",
1825
- 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).",
1826
- inputSchema: {
1827
- type: "object",
1828
- properties: {}
1829
- }
1830
- },
1831
1869
  {
1832
1870
  name: "vsl_execute_action",
1833
1871
  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 \u043D\u0430 \u043A\u043B\u0430\u0432\u0438\u0430\u0442\u0443\u0440\u0435, \u0442\u0440\u0435\u0431\u0443\u0435\u0442 value \u2014 \u043D\u0430\u0437\u0432\u0430\u043D\u0438\u0435 \u043A\u043B\u0430\u0432\u0438\u0448\u0438, \u043D\u0430\u043F\u0440\u0438\u043C\u0435\u0440 "Enter", "Tab", "Escape", "ArrowDown", "ArrowUp", "Space", "Backspace". \u0418\u0441\u043F\u043E\u043B\u044C\u0437\u0443\u0435\u0442\u0441\u044F \u0434\u043B\u044F \u043E\u0442\u043F\u0440\u0430\u0432\u043A\u0438 \u0444\u043E\u0440\u043C \u0447\u0435\u0440\u0435\u0437 Enter, \u043D\u0430\u0432\u0438\u0433\u0430\u0446\u0438\u0438 \u0447\u0435\u0440\u0435\u0437 Tab, \u0437\u0430\u043A\u0440\u044B\u0442\u0438\u044F \u043C\u043E\u0434\u0430\u043B\u043E\u043A \u0447\u0435\u0440\u0435\u0437 Escape \u0438 \u0442.\u0434.), 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:press, target_id:inp_1, value:Enter} \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.',
@@ -1849,6 +1887,14 @@ var VSL_TOOLS = [
1849
1887
  return_state: {
1850
1888
  type: "boolean",
1851
1889
  description: "\u0415\u0441\u043B\u0438 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. \u041F\u043E\u043B\u0435\u0437\u043D\u043E \u0434\u043B\u044F \u043E\u0442\u0441\u043B\u0435\u0436\u0438\u0432\u0430\u043D\u0438\u044F \u0438\u0437\u043C\u0435\u043D\u0435\u043D\u0438\u0439 DOM \u0431\u0435\u0437 \u0434\u043E\u043F\u043E\u043B\u043D\u0438\u0442\u0435\u043B\u044C\u043D\u043E\u0433\u043E \u0432\u044B\u0437\u043E\u0432\u0430 vsl_get_diff. Default: true \u2014 \u0432\u0441\u0435\u0433\u0434\u0430 \u0432\u043E\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044C \u0441\u043E\u0441\u0442\u043E\u044F\u043D\u0438\u0435 \u0434\u043B\u044F \u044D\u043A\u043E\u043D\u043E\u043C\u0438\u0438 \u0445\u043E\u0434\u043E\u0432 \u0430\u0433\u0435\u043D\u0442\u0430. \u0423\u0441\u0442\u0430\u043D\u043E\u0432\u0438\u0442\u0435 false \u0434\u043B\u044F \u043E\u0442\u043A\u043B\u044E\u0447\u0435\u043D\u0438\u044F."
1890
+ },
1891
+ timeout: {
1892
+ type: "number",
1893
+ description: "\u0422\u0430\u0439\u043C\u0430\u0443\u0442 \u043E\u0436\u0438\u0434\u0430\u043D\u0438\u044F \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043D\u0438\u044F \u0441\u043A\u0430\u0447\u0438\u0432\u0430\u043D\u0438\u044F \u0432 \u043C\u0441 (\u0442\u043E\u043B\u044C\u043A\u043E \u0434\u043B\u044F \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044F download)"
1894
+ },
1895
+ save_path: {
1896
+ type: "string",
1897
+ description: "\u041F\u0443\u0442\u044C \u0434\u043B\u044F \u0441\u043E\u0445\u0440\u0430\u043D\u0435\u043D\u0438\u044F \u0441\u043A\u0430\u0447\u0430\u043D\u043D\u043E\u0433\u043E \u0444\u0430\u0439\u043B\u0430 (\u0442\u043E\u043B\u044C\u043A\u043E \u0434\u043B\u044F \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044F download)"
1852
1898
  }
1853
1899
  },
1854
1900
  required: ["action", "target_id"]
@@ -1912,14 +1958,6 @@ var VSL_TOOLS = [
1912
1958
  required: ["url"]
1913
1959
  }
1914
1960
  },
1915
- {
1916
- name: "vsl_get_full_json",
1917
- 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.",
1918
- inputSchema: {
1919
- type: "object",
1920
- properties: {}
1921
- }
1922
- },
1923
1961
  {
1924
1962
  name: "vsl_get_text_block",
1925
1963
  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.',
@@ -1933,31 +1971,6 @@ var VSL_TOOLS = [
1933
1971
  },
1934
1972
  required: ["block_id"]
1935
1973
  }
1936
- },
1937
- {
1938
- name: "vsl_download",
1939
- 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).',
1940
- inputSchema: {
1941
- type: "object",
1942
- properties: {
1943
- target_id: {
1944
- type: "string",
1945
- 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)"
1946
- },
1947
- value: {
1948
- type: "string",
1949
- description: "URL \u0434\u043B\u044F \u043F\u0440\u044F\u043C\u043E\u0433\u043E \u0441\u043A\u0430\u0447\u0438\u0432\u0430\u043D\u0438\u044F"
1950
- },
1951
- timeout: {
1952
- type: "number",
1953
- 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)"
1954
- },
1955
- save_path: {
1956
- type: "string",
1957
- 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)"
1958
- }
1959
- }
1960
- }
1961
1974
  }
1962
1975
  ];
1963
1976
  function registerTools(server, config, sessionManager) {
@@ -1976,9 +1989,6 @@ function registerTools(server, config, sessionManager) {
1976
1989
  case "vsl_get_snapshot":
1977
1990
  result = await handleGetSnapshot(args, browser, session, config);
1978
1991
  break;
1979
- case "vsl_get_diff":
1980
- result = await handleGetDiff(session);
1981
- break;
1982
1992
  case "vsl_execute_action":
1983
1993
  result = await handleExecuteAction(args, browser, session);
1984
1994
  break;
@@ -1994,15 +2004,9 @@ function registerTools(server, config, sessionManager) {
1994
2004
  case "vsl_read_page":
1995
2005
  result = await handleReadPage(args, browser, session, config);
1996
2006
  break;
1997
- case "vsl_get_full_json":
1998
- result = await handleGetFullJson(args, session);
1999
- break;
2000
2007
  case "vsl_get_text_block":
2001
2008
  result = await handleGetTextBlock(args, session);
2002
2009
  break;
2003
- case "vsl_download":
2004
- result = await handleDownload(args, browser, session);
2005
- break;
2006
2010
  default:
2007
2011
  return {
2008
2012
  content: [
@@ -2157,7 +2161,7 @@ function registerResources(server, _config, sessionManager) {
2157
2161
  }
2158
2162
 
2159
2163
  // src/browser/manager.ts
2160
- import path2 from "path";
2164
+ import path from "path";
2161
2165
  import os from "os";
2162
2166
  var BrowserManager = class {
2163
2167
  browser = null;
@@ -2296,13 +2300,13 @@ var BrowserManager = class {
2296
2300
  path: null
2297
2301
  };
2298
2302
  }
2299
- const path3 = await download.path();
2303
+ const path2 = await download.path();
2300
2304
  return {
2301
2305
  downloadId,
2302
2306
  filename: download.suggestedFilename(),
2303
2307
  url: download.url(),
2304
2308
  status: "completed",
2305
- path: path3
2309
+ path: path2
2306
2310
  };
2307
2311
  } catch {
2308
2312
  return {
@@ -2339,9 +2343,9 @@ var BrowserManager = class {
2339
2343
  await download.saveAs(savePath);
2340
2344
  } else {
2341
2345
  const downloadsPath = this.config.downloadsPath ?? os.tmpdir();
2342
- const resolvedBase = path2.resolve(downloadsPath.replace(/^~/, os.homedir()));
2343
- const resolvedSave = path2.resolve(resolvedBase, savePath);
2344
- if (!resolvedSave.startsWith(resolvedBase + path2.sep) && resolvedSave !== resolvedBase) {
2346
+ const resolvedBase = path.resolve(downloadsPath.replace(/^~/, os.homedir()));
2347
+ const resolvedSave = path.resolve(resolvedBase, savePath);
2348
+ if (!resolvedSave.startsWith(resolvedBase + path.sep) && resolvedSave !== resolvedBase) {
2345
2349
  throw new Error(`Path traversal detected: save_path '${savePath}' escapes downloads directory '${resolvedBase}'`);
2346
2350
  }
2347
2351
  await download.saveAs(resolvedSave);
@@ -2381,13 +2385,13 @@ var BrowserManager = class {
2381
2385
  }
2382
2386
  const resolvedPaths = [];
2383
2387
  for (const filePath of filePaths) {
2384
- const isAbsolute = path2.isAbsolute(filePath);
2388
+ const isAbsolute = path.isAbsolute(filePath);
2385
2389
  if (isAbsolute) {
2386
2390
  resolvedPaths.push(filePath);
2387
2391
  } else {
2388
- const resolved = path2.resolve(process.cwd(), filePath);
2389
- const cwd = path2.resolve(process.cwd());
2390
- if (!resolved.startsWith(cwd + path2.sep) && resolved !== cwd) {
2392
+ const resolved = path.resolve(process.cwd(), filePath);
2393
+ const cwd = path.resolve(process.cwd());
2394
+ if (!resolved.startsWith(cwd + path.sep) && resolved !== cwd) {
2391
2395
  throw new Error(`Path traversal detected: file path '${filePath}' resolves outside working directory '${cwd}'`);
2392
2396
  }
2393
2397
  resolvedPaths.push(resolved);
@@ -2437,6 +2441,8 @@ var ServerSession = class {
2437
2441
  previousDocument = null;
2438
2442
  version = 0;
2439
2443
  onSnapshotChange = null;
2444
+ /** Метаданные скролла последнего снапшота (единый пайплайн отдачи, АС[3]). */
2445
+ scrollContext = null;
2440
2446
  /**
2441
2447
  * Регистрирует callback для уведомлений об изменении snapshot.
2442
2448
  * Вызывается при каждом вызове setSnapshot().
@@ -2454,6 +2460,7 @@ var ServerSession = class {
2454
2460
  }
2455
2461
  this.currentDocument = doc;
2456
2462
  this.version += 1;
2463
+ this.scrollContext = null;
2457
2464
  if (this.onSnapshotChange) {
2458
2465
  this.onSnapshotChange();
2459
2466
  }
@@ -2476,6 +2483,7 @@ var ServerSession = class {
2476
2483
  }
2477
2484
  this.currentDocument = doc;
2478
2485
  this.version += 1;
2486
+ this.scrollContext = input.scroll ? { ...input.scroll } : null;
2479
2487
  if (this.onSnapshotChange) {
2480
2488
  this.onSnapshotChange();
2481
2489
  }
@@ -2497,6 +2505,13 @@ var ServerSession = class {
2497
2505
  }
2498
2506
  return this.currentDocument;
2499
2507
  }
2508
+ /**
2509
+ * Возвращает предыдущий полный документ (для вьюпорт-фильтра диффа:
2510
+ * видимость removed-объектов определяется по prev-документу).
2511
+ */
2512
+ getPreviousSnapshot() {
2513
+ return this.previousDocument;
2514
+ }
2500
2515
  /**
2501
2516
  * Проверяет, есть ли текущий snapshot.
2502
2517
  */
@@ -2524,6 +2539,7 @@ var ServerSession = class {
2524
2539
  this.currentDocument = null;
2525
2540
  this.previousDocument = null;
2526
2541
  this.version = 0;
2542
+ this.scrollContext = null;
2527
2543
  }
2528
2544
  /**
2529
2545
  * Возвращает текущую версию snapshot.
@@ -2531,6 +2547,20 @@ var ServerSession = class {
2531
2547
  getVersion() {
2532
2548
  return this.version;
2533
2549
  }
2550
+ /**
2551
+ * Возвращает метаданные скролла последнего снапшота (единый пайплайн отдачи).
2552
+ * Используются для вычисления видимого окна и метаданных scrollable {top, bottom}.
2553
+ */
2554
+ getScrollContext() {
2555
+ return this.scrollContext;
2556
+ }
2557
+ /**
2558
+ * Устанавливает скролл-контекст явно (для путей, идущих мимо
2559
+ * snapshotFromElements).
2560
+ */
2561
+ setScrollContext(scroll) {
2562
+ this.scrollContext = scroll;
2563
+ }
2534
2564
  };
2535
2565
 
2536
2566
  // src/session/sessionManager.ts
@@ -2663,7 +2693,7 @@ var SessionManager = class {
2663
2693
  };
2664
2694
 
2665
2695
  // src/version.ts
2666
- var VSL_MCP_SERVER_VERSION = "1.1.4";
2696
+ var VSL_MCP_SERVER_VERSION = "1.2.2";
2667
2697
 
2668
2698
  // src/index.ts
2669
2699
  async function main() {