@thinkingos/vsl-mcp-server 1.2.2 → 1.2.4
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 +222 -12
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -116,6 +116,152 @@ async function injectVslIdsIntoDom(browser, objects) {
|
|
|
116
116
|
);
|
|
117
117
|
}
|
|
118
118
|
|
|
119
|
+
// src/utils/idMapper.ts
|
|
120
|
+
var TAG_PREFIXES = {
|
|
121
|
+
button: "btn",
|
|
122
|
+
input: "inp",
|
|
123
|
+
div: "div",
|
|
124
|
+
a: "a",
|
|
125
|
+
img: "img",
|
|
126
|
+
span: "spn",
|
|
127
|
+
p: "p",
|
|
128
|
+
h1: "h1",
|
|
129
|
+
h2: "h2",
|
|
130
|
+
h3: "h3",
|
|
131
|
+
h4: "h4",
|
|
132
|
+
h5: "h5",
|
|
133
|
+
h6: "h6",
|
|
134
|
+
nav: "nav",
|
|
135
|
+
header: "hdr",
|
|
136
|
+
footer: "ftr",
|
|
137
|
+
main: "mn",
|
|
138
|
+
section: "sec",
|
|
139
|
+
article: "art",
|
|
140
|
+
aside: "asd",
|
|
141
|
+
ul: "ul",
|
|
142
|
+
ol: "ol",
|
|
143
|
+
li: "li",
|
|
144
|
+
table: "tbl",
|
|
145
|
+
tr: "tr",
|
|
146
|
+
td: "td",
|
|
147
|
+
th: "th",
|
|
148
|
+
form: "frm",
|
|
149
|
+
label: "lbl",
|
|
150
|
+
textarea: "txt",
|
|
151
|
+
select: "sel",
|
|
152
|
+
option: "opt",
|
|
153
|
+
video: "vid",
|
|
154
|
+
audio: "aud",
|
|
155
|
+
canvas: "cvs",
|
|
156
|
+
svg: "svg",
|
|
157
|
+
iframe: "ifr",
|
|
158
|
+
script: "scr",
|
|
159
|
+
style: "sty",
|
|
160
|
+
link: "lnk",
|
|
161
|
+
meta: "mta",
|
|
162
|
+
title: "ttl",
|
|
163
|
+
body: "bdy",
|
|
164
|
+
html: "htm",
|
|
165
|
+
head: "hd"
|
|
166
|
+
};
|
|
167
|
+
function tagPrefix(tag) {
|
|
168
|
+
if (!tag) return "el";
|
|
169
|
+
return TAG_PREFIXES[tag] ?? (tag.length <= 3 ? tag : tag.slice(0, 3));
|
|
170
|
+
}
|
|
171
|
+
function extractTag(longId) {
|
|
172
|
+
const match = longId.match(/^(.+?)_(\d+)/);
|
|
173
|
+
return match?.[1] ?? longId;
|
|
174
|
+
}
|
|
175
|
+
function toBase36(n) {
|
|
176
|
+
return n.toString(36);
|
|
177
|
+
}
|
|
178
|
+
function buildIdMap(objects) {
|
|
179
|
+
const map = /* @__PURE__ */ new Map();
|
|
180
|
+
const counters = /* @__PURE__ */ new Map();
|
|
181
|
+
function visit(obj) {
|
|
182
|
+
const tag = extractTag(obj.id);
|
|
183
|
+
const prefix = tagPrefix(tag);
|
|
184
|
+
const counter = counters.get(prefix) ?? 0;
|
|
185
|
+
counters.set(prefix, counter + 1);
|
|
186
|
+
const shortId = `${prefix}_${toBase36(counter)}`;
|
|
187
|
+
map.set(shortId, obj.id);
|
|
188
|
+
if (obj.ch) {
|
|
189
|
+
for (const child of obj.ch) {
|
|
190
|
+
visit(child);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
for (const obj of objects) {
|
|
195
|
+
visit(obj);
|
|
196
|
+
}
|
|
197
|
+
return map;
|
|
198
|
+
}
|
|
199
|
+
function buildReverseIdMap(objects) {
|
|
200
|
+
const forward = buildIdMap(objects);
|
|
201
|
+
const reverse = /* @__PURE__ */ new Map();
|
|
202
|
+
for (const [shortId, longId] of forward) {
|
|
203
|
+
reverse.set(longId, shortId);
|
|
204
|
+
}
|
|
205
|
+
return reverse;
|
|
206
|
+
}
|
|
207
|
+
function replaceIdsInDocument(document2, longToShort) {
|
|
208
|
+
if (!document2.objects) return document2;
|
|
209
|
+
function replaceObj(obj) {
|
|
210
|
+
const shortId = longToShort.get(obj.id) ?? obj.id;
|
|
211
|
+
const result = { ...obj, id: shortId };
|
|
212
|
+
if (obj.ch) {
|
|
213
|
+
result.ch = obj.ch.map(replaceObj);
|
|
214
|
+
}
|
|
215
|
+
return result;
|
|
216
|
+
}
|
|
217
|
+
return { ...document2, objects: document2.objects.map(replaceObj) };
|
|
218
|
+
}
|
|
219
|
+
function replaceIdsInDiff(diff, reverseIdMap) {
|
|
220
|
+
if (!diff.changes) return diff;
|
|
221
|
+
const changes = diff.changes;
|
|
222
|
+
const added = changes.added?.map((obj) => replaceIdsInVslObject(obj, reverseIdMap)) ?? [];
|
|
223
|
+
const modified = changes.modified?.map((obj) => replaceIdsInModifiedObject(obj, reverseIdMap)) ?? [];
|
|
224
|
+
const removed = changes.removed?.map((obj) => replaceIdsInRemovedObject(obj, reverseIdMap)) ?? [];
|
|
225
|
+
const unchanged_refs = changes.unchanged_refs?.map(
|
|
226
|
+
(ref) => reverseIdMap.get(ref) ?? ref
|
|
227
|
+
) ?? [];
|
|
228
|
+
return {
|
|
229
|
+
...diff,
|
|
230
|
+
changes: {
|
|
231
|
+
added,
|
|
232
|
+
modified,
|
|
233
|
+
removed,
|
|
234
|
+
unchanged_refs
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
function replaceIdsInModifiedObject(obj, reverseIdMap) {
|
|
239
|
+
const result = {
|
|
240
|
+
id: reverseIdMap.get(obj.id) ?? obj.id
|
|
241
|
+
};
|
|
242
|
+
if (obj.p !== void 0) result.p = obj.p;
|
|
243
|
+
if (obj.s !== void 0) result.s = obj.s;
|
|
244
|
+
if (obj.t !== void 0) result.t = obj.t;
|
|
245
|
+
if (obj.txt !== void 0) result.txt = obj.txt;
|
|
246
|
+
if (obj.act !== void 0) result.act = obj.act;
|
|
247
|
+
return result;
|
|
248
|
+
}
|
|
249
|
+
function replaceIdsInRemovedObject(obj, reverseIdMap) {
|
|
250
|
+
return {
|
|
251
|
+
id: reverseIdMap.get(obj.id) ?? obj.id
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
function replaceIdsInVslObject(obj, reverseIdMap) {
|
|
255
|
+
const result = {
|
|
256
|
+
...obj,
|
|
257
|
+
id: reverseIdMap.get(obj.id) ?? obj.id
|
|
258
|
+
};
|
|
259
|
+
if (obj.ch && obj.ch.length > 0) {
|
|
260
|
+
result.ch = obj.ch.map((child) => replaceIdsInVslObject(child, reverseIdMap));
|
|
261
|
+
}
|
|
262
|
+
return result;
|
|
263
|
+
}
|
|
264
|
+
|
|
119
265
|
// src/utils/detailLevelFilter.ts
|
|
120
266
|
var INTERACTIVE_TYPES = /* @__PURE__ */ new Set([
|
|
121
267
|
"button",
|
|
@@ -529,8 +675,10 @@ async function handleGetSnapshot(args, browser, session, _config) {
|
|
|
529
675
|
currentDoc,
|
|
530
676
|
detailLevel
|
|
531
677
|
) : { ...currentDoc, objects: filteredObjects };
|
|
678
|
+
const reverseIdMap = session.getReverseIdMap();
|
|
679
|
+
const finalResult = isVslDiff(filteredResult) ? replaceIdsInDiff(filteredResult, reverseIdMap) : replaceIdsInDocument(filteredResult, reverseIdMap);
|
|
532
680
|
const executionTimeMs = Date.now() - startTime;
|
|
533
|
-
const resultJson = JSON.stringify(
|
|
681
|
+
const resultJson = JSON.stringify(finalResult);
|
|
534
682
|
const jsonSizeBytes = Buffer.byteLength(resultJson, "utf-8");
|
|
535
683
|
const estimatedTokens = Math.ceil(jsonSizeBytes / 4);
|
|
536
684
|
const objectCount = countObjects(filteredObjects);
|
|
@@ -555,7 +703,7 @@ async function handleGetSnapshot(args, browser, session, _config) {
|
|
|
555
703
|
}
|
|
556
704
|
return {
|
|
557
705
|
status: "success",
|
|
558
|
-
data:
|
|
706
|
+
data: finalResult,
|
|
559
707
|
metadata
|
|
560
708
|
};
|
|
561
709
|
} catch (error) {
|
|
@@ -627,7 +775,8 @@ async function handleNavigate(args, browser, session) {
|
|
|
627
775
|
scrollableMeta = computeScrollable(currentDoc.canvas.viewport, scrollContext);
|
|
628
776
|
const viewportFiltered = filterObjectsByViewport(currentDoc.objects, win);
|
|
629
777
|
const filteredObjects = filterObjectsByDetailLevel(viewportFiltered, "medium");
|
|
630
|
-
|
|
778
|
+
const reverseIdMap = session.getReverseIdMap();
|
|
779
|
+
snapshot = replaceIdsInDocument({ ...currentDoc, objects: filteredObjects }, reverseIdMap);
|
|
631
780
|
} catch (error) {
|
|
632
781
|
const errorMessage = `Failed to extract snapshot: ${error instanceof Error ? error.message : String(error)}`;
|
|
633
782
|
console.error("[vsl_navigate]", errorMessage, error);
|
|
@@ -717,7 +866,10 @@ async function getStateAfterAction(session, browser, url) {
|
|
|
717
866
|
const viewportFiltered = filterObjectsByViewport(fullDoc.objects, win);
|
|
718
867
|
const filteredObjects = filterObjectsByDetailLevel(viewportFiltered, "medium");
|
|
719
868
|
const snapshot = { ...fullDoc, objects: filteredObjects };
|
|
720
|
-
|
|
869
|
+
const reverseIdMap = session.getReverseIdMap();
|
|
870
|
+
const finalSnapshot = replaceIdsInDocument(snapshot, reverseIdMap);
|
|
871
|
+
const finalDiff = filteredDiff ? replaceIdsInDiff(filteredDiff, reverseIdMap) : filteredDiff;
|
|
872
|
+
return { diff: finalDiff, snapshot: finalSnapshot, scrollable };
|
|
721
873
|
} catch (error) {
|
|
722
874
|
const errorMessage = `Failed to get state after action: ${error instanceof Error ? error.message : String(error)}`;
|
|
723
875
|
console.error("[vsl_execute_action]", errorMessage, error);
|
|
@@ -748,7 +900,9 @@ function buildScrollStateFromCache(session, scrollContext) {
|
|
|
748
900
|
const viewportFiltered = filterObjectsByViewport(fullDoc.objects ?? [], win);
|
|
749
901
|
const filteredObjects = filterObjectsByDetailLevel(viewportFiltered, "medium");
|
|
750
902
|
const snapshot = { ...fullDoc, objects: filteredObjects };
|
|
751
|
-
|
|
903
|
+
const reverseIdMap = session.getReverseIdMap();
|
|
904
|
+
const finalSnapshot = replaceIdsInDocument(snapshot, reverseIdMap);
|
|
905
|
+
return { snapshot: finalSnapshot, scrollable };
|
|
752
906
|
} catch (error) {
|
|
753
907
|
const errorMessage = `Failed to build scroll state from cache: ${error instanceof Error ? error.message : String(error)}`;
|
|
754
908
|
console.error("[vsl_execute_action]", errorMessage, error);
|
|
@@ -850,7 +1004,9 @@ async function handleExecuteAction(args, browser, session) {
|
|
|
850
1004
|
};
|
|
851
1005
|
session.snapshotFromElements(extraction.elements, input);
|
|
852
1006
|
}
|
|
853
|
-
const
|
|
1007
|
+
const idMap = session.getIdMap();
|
|
1008
|
+
const resolvedId = idMap.get(args.target_id) || args.target_id;
|
|
1009
|
+
const selector = `[data-vsl-id="${resolvedId}"], #${resolvedId}, .${resolvedId}`;
|
|
854
1010
|
const page = await browser.getPage();
|
|
855
1011
|
switch (args.action) {
|
|
856
1012
|
case "click":
|
|
@@ -1151,7 +1307,9 @@ async function handleGetVisual(args, browser, session) {
|
|
|
1151
1307
|
};
|
|
1152
1308
|
return result2;
|
|
1153
1309
|
}
|
|
1154
|
-
const
|
|
1310
|
+
const idMap = session?.getIdMap() ?? /* @__PURE__ */ new Map();
|
|
1311
|
+
const resolvedId = idMap.get(args.element_id) || args.element_id;
|
|
1312
|
+
const selector = `[data-vsl-id="${resolvedId}"], #${resolvedId}, [id="${resolvedId}"]`;
|
|
1155
1313
|
let elementExists = await browser.evaluate((sel) => {
|
|
1156
1314
|
return document.querySelector(sel) !== null;
|
|
1157
1315
|
}, selector);
|
|
@@ -1613,13 +1771,16 @@ async function handleReadPage(args, browser, session, _config) {
|
|
|
1613
1771
|
rawSnapshot2,
|
|
1614
1772
|
detailLevel
|
|
1615
1773
|
) : rawDiff2;
|
|
1774
|
+
const reverseIdMap2 = session.getReverseIdMap();
|
|
1775
|
+
const finalSnapshot2 = replaceIdsInDocument(fullSnapshot2, reverseIdMap2);
|
|
1776
|
+
const finalDiff2 = fullDiff2 ? replaceIdsInDiff(fullDiff2, reverseIdMap2) : fullDiff2;
|
|
1616
1777
|
const data2 = {
|
|
1617
1778
|
url: args.url,
|
|
1618
1779
|
mode: "render",
|
|
1619
1780
|
content,
|
|
1620
1781
|
vslDocument: rawSnapshot2,
|
|
1621
|
-
snapshot:
|
|
1622
|
-
diff:
|
|
1782
|
+
snapshot: finalSnapshot2,
|
|
1783
|
+
diff: finalDiff2,
|
|
1623
1784
|
hasDiff: hasDiff2,
|
|
1624
1785
|
metadata: {
|
|
1625
1786
|
title: extractTitle(html),
|
|
@@ -1656,13 +1817,16 @@ async function handleReadPage(args, browser, session, _config) {
|
|
|
1656
1817
|
rawSnapshot,
|
|
1657
1818
|
detailLevel
|
|
1658
1819
|
) : rawDiff;
|
|
1820
|
+
const reverseIdMap = session.getReverseIdMap();
|
|
1821
|
+
const finalSnapshot = replaceIdsInDocument(fullSnapshot, reverseIdMap);
|
|
1822
|
+
const finalDiff = fullDiff ? replaceIdsInDiff(fullDiff, reverseIdMap) : fullDiff;
|
|
1659
1823
|
const data = {
|
|
1660
1824
|
url: args.url,
|
|
1661
1825
|
mode: "http",
|
|
1662
1826
|
content: httpResult.textContent,
|
|
1663
1827
|
vslDocument: httpResult.vslDocument,
|
|
1664
|
-
snapshot:
|
|
1665
|
-
diff:
|
|
1828
|
+
snapshot: finalSnapshot,
|
|
1829
|
+
diff: finalDiff,
|
|
1666
1830
|
hasDiff,
|
|
1667
1831
|
metadata: {
|
|
1668
1832
|
title: httpResult.title,
|
|
@@ -2443,6 +2607,10 @@ var ServerSession = class {
|
|
|
2443
2607
|
onSnapshotChange = null;
|
|
2444
2608
|
/** Метаданные скролла последнего снапшота (единый пайплайн отдачи, АС[3]). */
|
|
2445
2609
|
scrollContext = null;
|
|
2610
|
+
/** Карта shortId → longId для маппинга коротких ID в длинные (rw1_idmapper). */
|
|
2611
|
+
idMap = /* @__PURE__ */ new Map();
|
|
2612
|
+
/** Обратная карта longId → shortId для замены ID в выдаче LLM. */
|
|
2613
|
+
reverseIdMap = /* @__PURE__ */ new Map();
|
|
2446
2614
|
/**
|
|
2447
2615
|
* Регистрирует callback для уведомлений об изменении snapshot.
|
|
2448
2616
|
* Вызывается при каждом вызове setSnapshot().
|
|
@@ -2461,6 +2629,19 @@ var ServerSession = class {
|
|
|
2461
2629
|
this.currentDocument = doc;
|
|
2462
2630
|
this.version += 1;
|
|
2463
2631
|
this.scrollContext = null;
|
|
2632
|
+
const currentObjects = doc.objects ?? [];
|
|
2633
|
+
const previousObjects = this.previousDocument?.objects ?? [];
|
|
2634
|
+
const unionObjects = [...currentObjects, ...previousObjects];
|
|
2635
|
+
if (currentObjects.length > 0) {
|
|
2636
|
+
this.idMap = buildIdMap(currentObjects);
|
|
2637
|
+
} else {
|
|
2638
|
+
this.idMap = /* @__PURE__ */ new Map();
|
|
2639
|
+
}
|
|
2640
|
+
if (unionObjects.length > 0) {
|
|
2641
|
+
this.reverseIdMap = buildReverseIdMap(unionObjects);
|
|
2642
|
+
} else {
|
|
2643
|
+
this.reverseIdMap = /* @__PURE__ */ new Map();
|
|
2644
|
+
}
|
|
2464
2645
|
if (this.onSnapshotChange) {
|
|
2465
2646
|
this.onSnapshotChange();
|
|
2466
2647
|
}
|
|
@@ -2484,6 +2665,19 @@ var ServerSession = class {
|
|
|
2484
2665
|
this.currentDocument = doc;
|
|
2485
2666
|
this.version += 1;
|
|
2486
2667
|
this.scrollContext = input.scroll ? { ...input.scroll } : null;
|
|
2668
|
+
const currentObjects = doc.objects ?? [];
|
|
2669
|
+
const previousObjects = this.previousDocument?.objects ?? [];
|
|
2670
|
+
const unionObjects = [...currentObjects, ...previousObjects];
|
|
2671
|
+
if (currentObjects.length > 0) {
|
|
2672
|
+
this.idMap = buildIdMap(currentObjects);
|
|
2673
|
+
} else {
|
|
2674
|
+
this.idMap = /* @__PURE__ */ new Map();
|
|
2675
|
+
}
|
|
2676
|
+
if (unionObjects.length > 0) {
|
|
2677
|
+
this.reverseIdMap = buildReverseIdMap(unionObjects);
|
|
2678
|
+
} else {
|
|
2679
|
+
this.reverseIdMap = /* @__PURE__ */ new Map();
|
|
2680
|
+
}
|
|
2487
2681
|
if (this.onSnapshotChange) {
|
|
2488
2682
|
this.onSnapshotChange();
|
|
2489
2683
|
}
|
|
@@ -2540,6 +2734,8 @@ var ServerSession = class {
|
|
|
2540
2734
|
this.previousDocument = null;
|
|
2541
2735
|
this.version = 0;
|
|
2542
2736
|
this.scrollContext = null;
|
|
2737
|
+
this.idMap = /* @__PURE__ */ new Map();
|
|
2738
|
+
this.reverseIdMap = /* @__PURE__ */ new Map();
|
|
2543
2739
|
}
|
|
2544
2740
|
/**
|
|
2545
2741
|
* Возвращает текущую версию snapshot.
|
|
@@ -2561,6 +2757,20 @@ var ServerSession = class {
|
|
|
2561
2757
|
setScrollContext(scroll) {
|
|
2562
2758
|
this.scrollContext = scroll;
|
|
2563
2759
|
}
|
|
2760
|
+
/**
|
|
2761
|
+
* Возвращает карту shortId → longId для маппинга коротких ID в длинные.
|
|
2762
|
+
* Используется в executeAction/getVisual для резолвинга коротких ID от агента.
|
|
2763
|
+
*/
|
|
2764
|
+
getIdMap() {
|
|
2765
|
+
return this.idMap;
|
|
2766
|
+
}
|
|
2767
|
+
/**
|
|
2768
|
+
* Возвращает обратную карту longId → shortId для замены ID в выдаче LLM.
|
|
2769
|
+
* Используется в путях отдачи для замены длинных ID на короткие.
|
|
2770
|
+
*/
|
|
2771
|
+
getReverseIdMap() {
|
|
2772
|
+
return this.reverseIdMap;
|
|
2773
|
+
}
|
|
2564
2774
|
};
|
|
2565
2775
|
|
|
2566
2776
|
// src/session/sessionManager.ts
|
|
@@ -2693,7 +2903,7 @@ var SessionManager = class {
|
|
|
2693
2903
|
};
|
|
2694
2904
|
|
|
2695
2905
|
// src/version.ts
|
|
2696
|
-
var VSL_MCP_SERVER_VERSION = "1.2.
|
|
2906
|
+
var VSL_MCP_SERVER_VERSION = "1.2.4";
|
|
2697
2907
|
|
|
2698
2908
|
// src/index.ts
|
|
2699
2909
|
async function main() {
|