@thinkingos/vsl-mcp-server 0.4.5 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -116,35 +116,31 @@ async function injectVslIdsIntoDom(browser, objects) {
116
116
  );
117
117
  }
118
118
 
119
- // src/tools/getSnapshot.ts
120
- var DEFAULT_SNAPSHOT_TTL_MS = 5e3;
121
- var snapshotCache = /* @__PURE__ */ new Map();
122
- function filterObjectsByDetailLevel(objects, level) {
119
+ // src/utils/detailLevelFilter.ts
120
+ var INTERACTIVE_TYPES = /* @__PURE__ */ new Set([
121
+ "button",
122
+ "link",
123
+ "input",
124
+ "select",
125
+ "textarea",
126
+ "file_input"
127
+ ]);
128
+ var CONTAINER_TYPES = /* @__PURE__ */ new Set([
129
+ "container",
130
+ "layout",
131
+ "list",
132
+ "grid",
133
+ "toolbar",
134
+ "tab_bar",
135
+ "form_field",
136
+ "nav",
137
+ "header",
138
+ "main",
139
+ "footer"
140
+ ]);
141
+ function filterObjectsByDetailLevel(objects, level, options) {
123
142
  if (level === "high") return objects;
124
- const INTERACTIVE_TYPES = /* @__PURE__ */ new Set([
125
- "button",
126
- "link",
127
- "input",
128
- "select",
129
- "checkbox",
130
- "radio",
131
- "textarea",
132
- "file",
133
- "submit",
134
- "reset"
135
- ]);
136
- const CONTAINER_TYPES = /* @__PURE__ */ new Set([
137
- "div",
138
- "section",
139
- "article",
140
- "nav",
141
- "header",
142
- "footer",
143
- "main",
144
- "aside",
145
- "form",
146
- "fieldset"
147
- ]);
143
+ const shouldStripStyles = options?.stripStyles ?? true;
148
144
  function filterRecursive(obj) {
149
145
  const type = obj.t || "";
150
146
  if (level === "low") {
@@ -152,25 +148,39 @@ function filterObjectsByDetailLevel(objects, level) {
152
148
  if (obj.ch && obj.ch.length > 0) {
153
149
  const filteredChildren = obj.ch.map(filterRecursive).filter((child) => child !== null);
154
150
  if (filteredChildren.length > 0) {
155
- return { ...obj, ch: filteredChildren };
151
+ const result = { ...obj, ch: filteredChildren };
152
+ if (shouldStripStyles) delete result.sty;
153
+ return result;
156
154
  }
157
155
  }
158
156
  return null;
159
157
  }
158
+ if (shouldStripStyles && obj.sty) {
159
+ const { sty: _sty, ...rest } = obj;
160
+ return rest;
161
+ }
160
162
  return obj;
161
163
  }
162
164
  if (level === "medium") {
163
165
  if (INTERACTIVE_TYPES.has(type) || CONTAINER_TYPES.has(type)) {
164
166
  if (obj.ch && obj.ch.length > 0) {
165
167
  const filteredChildren = obj.ch.map(filterRecursive).filter((child) => child !== null);
166
- return { ...obj, ch: filteredChildren };
168
+ const result = { ...obj, ch: filteredChildren };
169
+ if (shouldStripStyles) delete result.sty;
170
+ return result;
171
+ }
172
+ if (shouldStripStyles && obj.sty) {
173
+ const { sty: _sty, ...rest } = obj;
174
+ return rest;
167
175
  }
168
176
  return obj;
169
177
  }
170
178
  if (obj.ch && obj.ch.length > 0) {
171
179
  const filteredChildren = obj.ch.map(filterRecursive).filter((child) => child !== null);
172
180
  if (filteredChildren.length > 0) {
173
- return { ...obj, ch: filteredChildren };
181
+ const result = { ...obj, ch: filteredChildren };
182
+ if (shouldStripStyles) delete result.sty;
183
+ return result;
174
184
  }
175
185
  }
176
186
  return null;
@@ -179,6 +189,17 @@ function filterObjectsByDetailLevel(objects, level) {
179
189
  }
180
190
  return objects.map(filterRecursive).filter((obj) => obj !== null);
181
191
  }
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 };
198
+ }
199
+
200
+ // src/tools/getSnapshot.ts
201
+ var DEFAULT_SNAPSHOT_TTL_MS = 5e3;
202
+ var snapshotCache = /* @__PURE__ */ new Map();
182
203
  function countObjects(objects) {
183
204
  let count = 0;
184
205
  for (const obj of objects) {
@@ -189,7 +210,11 @@ function countObjects(objects) {
189
210
  }
190
211
  return count;
191
212
  }
192
- function extractDomTreeInBrowser() {
213
+ function extractDomTreeInBrowser(options) {
214
+ const includeOffscreen = options?.includeOffscreen ?? false;
215
+ const viewportWidth = window.innerWidth;
216
+ const viewportHeight = window.innerHeight;
217
+ let culledCount = 0;
193
218
  const NON_RENDERABLE_TAGS = /* @__PURE__ */ new Set([
194
219
  "script",
195
220
  "style",
@@ -258,6 +283,9 @@ function extractDomTreeInBrowser() {
258
283
  function hasZeroBox(rect) {
259
284
  return rect.width <= 0 || rect.height <= 0;
260
285
  }
286
+ function isInViewport(rect) {
287
+ return rect.y < viewportHeight && rect.y + rect.height > 0;
288
+ }
261
289
  function collectVisibleChildren(parent, parentPath) {
262
290
  const result = [];
263
291
  Array.from(parent.children).forEach((child, index) => {
@@ -279,6 +307,10 @@ function extractDomTreeInBrowser() {
279
307
  if (css.opacity === "0" && css.pointerEvents === "none") {
280
308
  return;
281
309
  }
310
+ if (!includeOffscreen && !isInViewport(rect)) {
311
+ culledCount++;
312
+ return;
313
+ }
282
314
  const children = collectVisibleChildren(child, path3);
283
315
  const vslId = `${tag}_${path3.join("_")}`;
284
316
  child.setAttribute("data-vsl-id", vslId);
@@ -296,7 +328,13 @@ function extractDomTreeInBrowser() {
296
328
  }
297
329
  const body = document.body;
298
330
  if (!body) return [];
299
- return collectVisibleChildren(body, []);
331
+ const elements = collectVisibleChildren(body, []);
332
+ return [{
333
+ __type: "extraction_result",
334
+ elements,
335
+ culled_count: culledCount,
336
+ viewport: { width: viewportWidth, height: viewportHeight }
337
+ }];
300
338
  }
301
339
  async function handleGetSnapshot(args, browser, session, _config) {
302
340
  const startTime = Date.now();
@@ -343,11 +381,27 @@ async function handleGetSnapshot(args, browser, session, _config) {
343
381
  error: "Playwright is not installed. Install it with: npm install playwright"
344
382
  };
345
383
  }
346
- const extractedElements = await browser.evaluate(extractDomTreeInBrowser);
347
- const viewport = await browser.evaluate(() => ({
348
- width: window.innerWidth,
349
- height: window.innerHeight
350
- }));
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
+ }
402
+ const extractedElements = extraction.elements;
403
+ const culledCount = extraction.culled_count;
404
+ const viewport = extraction.viewport;
351
405
  const url = args.url || await browser.evaluate(() => window.location.href);
352
406
  const title = await browser.evaluate(() => document.title);
353
407
  const input = {
@@ -369,12 +423,15 @@ async function handleGetSnapshot(args, browser, session, _config) {
369
423
  const jsonSizeBytes = Buffer.byteLength(resultJson, "utf-8");
370
424
  const estimatedTokens = Math.ceil(jsonSizeBytes / 4);
371
425
  const objectCount = countObjects(filteredObjects);
426
+ const totalObjectsCount = objectCount + culledCount;
372
427
  const metadata = {
373
428
  json_size_bytes: jsonSizeBytes,
374
429
  estimated_tokens: estimatedTokens,
375
430
  object_count: objectCount,
431
+ total_objects_count: totalObjectsCount,
376
432
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
377
- execution_time_ms: executionTimeMs
433
+ execution_time_ms: executionTimeMs,
434
+ culled_offscreen_count: culledCount
378
435
  };
379
436
  if (ttl > 0) {
380
437
  const cacheKey = `${args.url || "current"}:${detailLevel}`;
@@ -1028,6 +1085,7 @@ async function handleGetVisual(args, browser, session) {
1028
1085
 
1029
1086
  // src/tools/httpExtractor.ts
1030
1087
  import * as cheerio from "cheerio";
1088
+ import { getGlobalFilter, createIdGenerator } from "@thinkingos/vsl-sdk";
1031
1089
  var SPA_MARKERS = [
1032
1090
  'id="app"',
1033
1091
  'id="root"',
@@ -1076,7 +1134,7 @@ async function extractViaHttp(url, options = {}) {
1076
1134
  const rawHtml = await response.text();
1077
1135
  const isSpa = detectSpa(rawHtml);
1078
1136
  const filteredHtml = readable ? applyReadableFilter(rawHtml) : rawHtml;
1079
- const textContent = extractTextContent(filteredHtml);
1137
+ const textContent = extractTextContent(filteredHtml, url);
1080
1138
  const title = extractTitle(rawHtml);
1081
1139
  const wordCount = countWords(textContent);
1082
1140
  const vslDocument = buildVslFromDom(rawHtml, { url, title });
@@ -1104,11 +1162,16 @@ function applyReadableFilter(html) {
1104
1162
  }
1105
1163
  return filtered;
1106
1164
  }
1107
- function extractTextContent(html) {
1165
+ function extractTextContent(html, url) {
1108
1166
  let text = html.replace(/<script[^>]*>.*?<\/script>/gis, "");
1109
1167
  text = text.replace(/<style[^>]*>.*?<\/style>/gis, "");
1110
1168
  text = text.replace(/<[^>]+>/g, " ");
1111
1169
  text = text.replace(/\s+/g, " ").trim();
1170
+ const filter = getGlobalFilter();
1171
+ if (filter.isEnabled()) {
1172
+ const result = filter.scan(text, url);
1173
+ return result.cleanText;
1174
+ }
1112
1175
  return text;
1113
1176
  }
1114
1177
  function extractTitle(html) {
@@ -1126,6 +1189,7 @@ function buildVslFromDom(html, options = {}) {
1126
1189
  const objects = [];
1127
1190
  const textBlocks = /* @__PURE__ */ new Map();
1128
1191
  let textBlockCounter = 0;
1192
+ const idGenerator = createIdGenerator();
1129
1193
  const LAZY_TEXT_THRESHOLD = 200;
1130
1194
  const LAZY_TEXT_PREVIEW_LENGTH = 50;
1131
1195
  function extractElement(element) {
@@ -1153,7 +1217,7 @@ function buildVslFromDom(html, options = {}) {
1153
1217
  if (childObj) children.push(childObj);
1154
1218
  });
1155
1219
  const obj = {
1156
- id: `${tag}_${objects.length}`,
1220
+ id: idGenerator.generate($el.attr("id"), type),
1157
1221
  t: type,
1158
1222
  p: null,
1159
1223
  // HTTP-путь: нет координат
@@ -1351,7 +1415,7 @@ async function handleReadPage(args, browser, session, _config) {
1351
1415
  const rawSnapshot2 = session.getSnapshot();
1352
1416
  const rawDiff2 = hasDiff2 ? session.getDiff() : void 0;
1353
1417
  const fullSnapshot2 = rawSnapshot2 ? applyDetailLevelFilter(rawSnapshot2, detailLevel) : void 0;
1354
- const fullDiff2 = rawDiff2 ? applyDetailLevelFilter(rawDiff2, detailLevel) : void 0;
1418
+ const fullDiff2 = rawDiff2;
1355
1419
  const data2 = {
1356
1420
  url: args.url,
1357
1421
  mode: "render",
@@ -1384,7 +1448,7 @@ async function handleReadPage(args, browser, session, _config) {
1384
1448
  const rawSnapshot = session.getSnapshot();
1385
1449
  const rawDiff = hasDiff ? session.getDiff() : void 0;
1386
1450
  const fullSnapshot = rawSnapshot ? applyDetailLevelFilter(rawSnapshot, detailLevel) : void 0;
1387
- const fullDiff = rawDiff ? applyDetailLevelFilter(rawDiff, detailLevel) : void 0;
1451
+ const fullDiff = rawDiff;
1388
1452
  const data = {
1389
1453
  url: args.url,
1390
1454
  mode: "http",
@@ -1486,72 +1550,6 @@ async function extractVslFromPage(browser) {
1486
1550
  ]
1487
1551
  };
1488
1552
  }
1489
- function applyDetailLevelFilter(document2, level) {
1490
- if (level === "high") return document2;
1491
- const objects = document2.objects;
1492
- if (!objects || !Array.isArray(objects)) return document2;
1493
- const INTERACTIVE_TYPES = /* @__PURE__ */ new Set([
1494
- "button",
1495
- "link",
1496
- "input",
1497
- "select",
1498
- "checkbox",
1499
- "radio",
1500
- "textarea",
1501
- "file",
1502
- "submit",
1503
- "reset"
1504
- ]);
1505
- const CONTAINER_TYPES = /* @__PURE__ */ new Set([
1506
- "div",
1507
- "section",
1508
- "article",
1509
- "nav",
1510
- "header",
1511
- "footer",
1512
- "main",
1513
- "aside",
1514
- "form",
1515
- "fieldset"
1516
- ]);
1517
- function filterRecursive(obj) {
1518
- const type = obj.t || obj.type || "";
1519
- if (level === "low") {
1520
- if (!INTERACTIVE_TYPES.has(type)) {
1521
- const children = obj.ch || obj.children;
1522
- if (children && Array.isArray(children) && children.length > 0) {
1523
- const filteredChildren = children.map(filterRecursive).filter((child) => child !== null);
1524
- if (filteredChildren.length > 0) {
1525
- return { ...obj, ch: filteredChildren };
1526
- }
1527
- }
1528
- return null;
1529
- }
1530
- return obj;
1531
- }
1532
- if (level === "medium") {
1533
- if (INTERACTIVE_TYPES.has(type) || CONTAINER_TYPES.has(type)) {
1534
- const children2 = obj.ch || obj.children;
1535
- if (children2 && Array.isArray(children2) && children2.length > 0) {
1536
- const filteredChildren = children2.map(filterRecursive).filter((child) => child !== null);
1537
- return { ...obj, ch: filteredChildren };
1538
- }
1539
- return obj;
1540
- }
1541
- const children = obj.ch || obj.children;
1542
- if (children && Array.isArray(children) && children.length > 0) {
1543
- const filteredChildren = children.map(filterRecursive).filter((child) => child !== null);
1544
- if (filteredChildren.length > 0) {
1545
- return { ...obj, ch: filteredChildren };
1546
- }
1547
- }
1548
- return null;
1549
- }
1550
- return obj;
1551
- }
1552
- const filteredObjects = objects.map(filterRecursive).filter((obj) => obj !== null);
1553
- return { ...document2, objects: filteredObjects };
1554
- }
1555
1553
  function computeVslMetrics(snapshot) {
1556
1554
  if (!snapshot) return {};
1557
1555
  try {
@@ -1563,12 +1561,9 @@ function computeVslMetrics(snapshot) {
1563
1561
  if (objects && Array.isArray(objects)) {
1564
1562
  let countRecursive2 = function(obj) {
1565
1563
  let count = 1;
1566
- const children = obj.ch || obj.children;
1567
- if (children && Array.isArray(children)) {
1568
- for (const child of children) {
1569
- if (child && typeof child === "object") {
1570
- count += countRecursive2(child);
1571
- }
1564
+ if (obj.ch && Array.isArray(obj.ch)) {
1565
+ for (const child of obj.ch) {
1566
+ count += countRecursive2(child);
1572
1567
  }
1573
1568
  }
1574
1569
  return count;