@thinkingos/vsl-mcp-server 1.3.3 → 1.3.5
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 +726 -576
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -3679,7 +3679,7 @@ async function getStateAfterAction(session, browser, url, sessionId) {
|
|
|
3679
3679
|
};
|
|
3680
3680
|
session.snapshotFromElements(extraction.elements, input);
|
|
3681
3681
|
const fullDoc = session.getSnapshot();
|
|
3682
|
-
await injectVslIdsIntoDom(browser, fullDoc.objects);
|
|
3682
|
+
await injectVslIdsIntoDom(browser, fullDoc.objects, /* @__PURE__ */ new Map(), sessionId);
|
|
3683
3683
|
const scrollContext = session.getScrollContext();
|
|
3684
3684
|
const win = computeVisibleWindow(fullDoc.canvas.viewport, scrollContext);
|
|
3685
3685
|
const scrollable = computeScrollable(fullDoc.canvas.viewport, scrollContext);
|
|
@@ -3809,638 +3809,752 @@ async function handleExecuteAction(args, browser, session, sessionId) {
|
|
|
3809
3809
|
}
|
|
3810
3810
|
const snapshot = session.getSnapshot();
|
|
3811
3811
|
const snapshotUrl = snapshot.canvas.url;
|
|
3812
|
+
const urlBeforeAction = await browser.evaluate(() => window.location.href, sessionId);
|
|
3812
3813
|
if (snapshotUrl) {
|
|
3813
|
-
const currentUrl =
|
|
3814
|
+
const currentUrl = urlBeforeAction;
|
|
3814
3815
|
if (currentUrl !== snapshotUrl) {
|
|
3815
3816
|
await browser.navigate(snapshotUrl, sessionId);
|
|
3816
3817
|
}
|
|
3817
3818
|
}
|
|
3818
|
-
|
|
3819
|
-
|
|
3820
|
-
|
|
3821
|
-
|
|
3822
|
-
|
|
3823
|
-
|
|
3824
|
-
|
|
3825
|
-
|
|
3826
|
-
|
|
3827
|
-
|
|
3828
|
-
|
|
3829
|
-
|
|
3830
|
-
|
|
3831
|
-
|
|
3832
|
-
|
|
3833
|
-
let targetFrame = null;
|
|
3834
|
-
let localShortId = args.target_id;
|
|
3835
|
-
const frameMatch = args.target_id.match(/^iframe_(\d+):(.+)$/);
|
|
3836
|
-
if (frameMatch) {
|
|
3837
|
-
const frameIndex = parseInt(frameMatch[1], 10);
|
|
3838
|
-
localShortId = frameMatch[2];
|
|
3839
|
-
const frameUrl = iframeFrameRegistry.get(frameIndex);
|
|
3840
|
-
if (!frameUrl) {
|
|
3841
|
-
return {
|
|
3842
|
-
status: "error",
|
|
3843
|
-
error: `Frame iframe_${frameIndex} not found in registry. Call vsl_get_snapshot first.`
|
|
3844
|
-
};
|
|
3845
|
-
}
|
|
3846
|
-
const frames = await browser.getFrames(sessionId);
|
|
3847
|
-
targetFrame = frames.find((f) => {
|
|
3848
|
-
const u = f.url();
|
|
3849
|
-
return u === frameUrl || u.includes(frameUrl) || frameUrl.includes(u);
|
|
3850
|
-
}) || null;
|
|
3851
|
-
if (!targetFrame) {
|
|
3852
|
-
return {
|
|
3853
|
-
status: "error",
|
|
3854
|
-
error: `Frame with URL "${frameUrl}" not found. Available frames: ${frames.map((f) => f.url()).join(", ")}`
|
|
3819
|
+
let actionError = null;
|
|
3820
|
+
let page = null;
|
|
3821
|
+
try {
|
|
3822
|
+
const hasVslIds = await browser.evaluate(() => {
|
|
3823
|
+
return document.querySelector("[data-vsl-id]") !== null;
|
|
3824
|
+
}, sessionId);
|
|
3825
|
+
if (!hasVslIds) {
|
|
3826
|
+
const extraction = await extractDomTree(browser, sessionId);
|
|
3827
|
+
const input = {
|
|
3828
|
+
viewport: extraction.viewport,
|
|
3829
|
+
url: snapshotUrl || "",
|
|
3830
|
+
title: "",
|
|
3831
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
3832
|
+
// Метаданные скролла для единого пайплайна отдачи (АС[3])
|
|
3833
|
+
scroll: extraction.scroll
|
|
3855
3834
|
};
|
|
3856
|
-
|
|
3857
|
-
|
|
3858
|
-
|
|
3859
|
-
|
|
3860
|
-
|
|
3861
|
-
|
|
3862
|
-
|
|
3863
|
-
|
|
3864
|
-
|
|
3865
|
-
|
|
3866
|
-
|
|
3867
|
-
|
|
3868
|
-
|
|
3869
|
-
let findObjectById2 = function(objects, targetId) {
|
|
3870
|
-
for (const obj of objects) {
|
|
3871
|
-
const o = obj;
|
|
3872
|
-
if (o.id === targetId) return o;
|
|
3873
|
-
if (o.ch && Array.isArray(o.ch)) {
|
|
3874
|
-
const found = findObjectById2(o.ch, targetId);
|
|
3875
|
-
if (found) return found;
|
|
3876
|
-
}
|
|
3877
|
-
}
|
|
3878
|
-
return null;
|
|
3835
|
+
session.snapshotFromElements(extraction.elements, input);
|
|
3836
|
+
}
|
|
3837
|
+
let targetFrame = null;
|
|
3838
|
+
let localShortId = args.target_id;
|
|
3839
|
+
const frameMatch = args.target_id.match(/^iframe_(\d+):(.+)$/);
|
|
3840
|
+
if (frameMatch) {
|
|
3841
|
+
const frameIndex = parseInt(frameMatch[1], 10);
|
|
3842
|
+
localShortId = frameMatch[2];
|
|
3843
|
+
const frameUrl = iframeFrameRegistry.get(frameIndex);
|
|
3844
|
+
if (!frameUrl) {
|
|
3845
|
+
return {
|
|
3846
|
+
status: "error",
|
|
3847
|
+
error: `Frame iframe_${frameIndex} not found in registry. Call vsl_get_snapshot first.`
|
|
3879
3848
|
};
|
|
3880
|
-
var findObjectById = findObjectById2;
|
|
3881
|
-
console.warn(`[VSL] Standard locator failed for ${args.target_id}, trying positional mapping fallback`);
|
|
3882
|
-
const snapshotDoc = session.getSnapshot();
|
|
3883
|
-
const snapshotObjects = snapshotDoc.objects ?? [];
|
|
3884
|
-
const snapshotObj = findObjectById2(snapshotObjects, localId);
|
|
3885
|
-
if (!snapshotObj || !snapshotObj.rect) {
|
|
3886
|
-
console.error(`[VSL] Positional mapping failed: element ${localId} not found in snapshot or has no rect`);
|
|
3887
|
-
throw locatorError;
|
|
3888
|
-
}
|
|
3889
|
-
const rect = snapshotObj.rect;
|
|
3890
|
-
const centerX = rect.x + rect.width / 2;
|
|
3891
|
-
const centerY = rect.y + rect.height / 2;
|
|
3892
|
-
const elementFound = await target.evaluate(
|
|
3893
|
-
([x, y]) => {
|
|
3894
|
-
const el = document.elementFromPoint(x, y);
|
|
3895
|
-
if (el) {
|
|
3896
|
-
el.click();
|
|
3897
|
-
return true;
|
|
3898
|
-
}
|
|
3899
|
-
return false;
|
|
3900
|
-
},
|
|
3901
|
-
[centerX, centerY]
|
|
3902
|
-
);
|
|
3903
|
-
if (!elementFound) {
|
|
3904
|
-
console.error(`[VSL] Positional mapping failed: no element found at (${centerX}, ${centerY})`);
|
|
3905
|
-
throw locatorError;
|
|
3906
|
-
}
|
|
3907
|
-
console.log(`[VSL] Positional mapping succeeded: clicked element at (${centerX}, ${centerY}) for ${args.target_id}`);
|
|
3908
3849
|
}
|
|
3909
|
-
await
|
|
3910
|
-
|
|
3911
|
-
|
|
3912
|
-
|
|
3913
|
-
|
|
3914
|
-
if (!
|
|
3850
|
+
const frames = await browser.getFrames(sessionId);
|
|
3851
|
+
targetFrame = frames.find((f) => {
|
|
3852
|
+
const u = f.url();
|
|
3853
|
+
return u === frameUrl || u.includes(frameUrl) || frameUrl.includes(u);
|
|
3854
|
+
}) || null;
|
|
3855
|
+
if (!targetFrame) {
|
|
3915
3856
|
return {
|
|
3916
3857
|
status: "error",
|
|
3917
|
-
error: "
|
|
3858
|
+
error: `Frame with URL "${frameUrl}" not found. Available frames: ${frames.map((f) => f.url()).join(", ")}`
|
|
3918
3859
|
};
|
|
3919
3860
|
}
|
|
3920
|
-
|
|
3921
|
-
|
|
3922
|
-
|
|
3923
|
-
|
|
3924
|
-
|
|
3925
|
-
|
|
3926
|
-
|
|
3927
|
-
|
|
3928
|
-
|
|
3929
|
-
const dy = scrollDir === "up" ? -scrollAmount : scrollDir === "down" ? scrollAmount : 0;
|
|
3930
|
-
await browser.evaluate(({ dx: dx2, dy: dy2 }) => {
|
|
3931
|
-
window.scrollBy(dx2, dy2);
|
|
3932
|
-
}, sessionId, { dx, dy });
|
|
3933
|
-
await page.waitForTimeout(50);
|
|
3934
|
-
const prevScroll = session.getScrollContext();
|
|
3935
|
-
let newScroll = null;
|
|
3861
|
+
}
|
|
3862
|
+
const idMap = session.getIdMap();
|
|
3863
|
+
const resolvedId = idMap.get(args.target_id) || localShortId;
|
|
3864
|
+
const localId = resolvedId;
|
|
3865
|
+
const selector = `[data-vsl-id="${localId}"], #${localId}, .${localId}`;
|
|
3866
|
+
page = await browser.getPage(sessionId);
|
|
3867
|
+
const target = targetFrame || page;
|
|
3868
|
+
const actionsRequiringElement = ["click", "fill", "type", "check", "uncheck", "upload", "select", "hover", "focus", "blur", "clear"];
|
|
3869
|
+
if (actionsRequiringElement.includes(args.action)) {
|
|
3936
3870
|
try {
|
|
3937
|
-
const
|
|
3938
|
-
|
|
3939
|
-
|
|
3940
|
-
|
|
3941
|
-
|
|
3942
|
-
|
|
3943
|
-
|
|
3944
|
-
if (
|
|
3945
|
-
|
|
3946
|
-
|
|
3947
|
-
|
|
3948
|
-
|
|
3949
|
-
|
|
3950
|
-
|
|
3951
|
-
|
|
3952
|
-
|
|
3953
|
-
|
|
3954
|
-
|
|
3955
|
-
|
|
3956
|
-
|
|
3957
|
-
|
|
3958
|
-
|
|
3959
|
-
|
|
3960
|
-
|
|
3961
|
-
|
|
3962
|
-
|
|
3963
|
-
|
|
3964
|
-
|
|
3965
|
-
|
|
3966
|
-
|
|
3871
|
+
const expectedTag = resolvedId.match(/^([a-z][a-z0-9]*(?:_[a-z][a-z0-9]*)*)_/)?.[1] || "";
|
|
3872
|
+
const elementValidation = await target.evaluate(({ sel, expectedTag: expectedTag2 }) => {
|
|
3873
|
+
const el = document.querySelector(sel);
|
|
3874
|
+
if (!el) return { exists: false, valid: false, actualTag: "" };
|
|
3875
|
+
const actualTag = el.tagName.toLowerCase();
|
|
3876
|
+
return { exists: true, valid: actualTag === expectedTag2, actualTag };
|
|
3877
|
+
}, { sel: selector, expectedTag });
|
|
3878
|
+
if (!elementValidation.exists) {
|
|
3879
|
+
const extraction = await extractDomTree(browser, sessionId);
|
|
3880
|
+
const input = {
|
|
3881
|
+
viewport: extraction.viewport,
|
|
3882
|
+
url: snapshotUrl || "",
|
|
3883
|
+
title: "",
|
|
3884
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
3885
|
+
scroll: extraction.scroll
|
|
3886
|
+
};
|
|
3887
|
+
session.snapshotFromElements(extraction.elements, input);
|
|
3888
|
+
await injectVslIdsIntoDom(browser, extraction.elements, /* @__PURE__ */ new Map(), sessionId);
|
|
3889
|
+
const revalidation = await target.evaluate(({ sel, expectedTag: expectedTag2 }) => {
|
|
3890
|
+
const el = document.querySelector(sel);
|
|
3891
|
+
if (!el) return { exists: false, valid: false, actualTag: "" };
|
|
3892
|
+
const actualTag = el.tagName.toLowerCase();
|
|
3893
|
+
return { exists: true, valid: actualTag === expectedTag2, actualTag };
|
|
3894
|
+
}, { sel: selector, expectedTag });
|
|
3895
|
+
if (!revalidation.exists) {
|
|
3896
|
+
return {
|
|
3897
|
+
status: "error",
|
|
3898
|
+
error: `Element not found after re-injection: ${args.target_id} (selector: ${selector})`
|
|
3899
|
+
};
|
|
3900
|
+
}
|
|
3901
|
+
if (!revalidation.valid) {
|
|
3902
|
+
return {
|
|
3903
|
+
status: "error",
|
|
3904
|
+
error: `Tag mismatch after re-injection: expected ${expectedTag}, got ${revalidation.actualTag} for ${args.target_id}`
|
|
3905
|
+
};
|
|
3906
|
+
}
|
|
3907
|
+
} else if (!elementValidation.valid) {
|
|
3908
|
+
const extraction = await extractDomTree(browser, sessionId);
|
|
3909
|
+
const input = {
|
|
3910
|
+
viewport: extraction.viewport,
|
|
3911
|
+
url: snapshotUrl || "",
|
|
3912
|
+
title: "",
|
|
3913
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
3914
|
+
scroll: extraction.scroll
|
|
3915
|
+
};
|
|
3916
|
+
session.snapshotFromElements(extraction.elements, input);
|
|
3917
|
+
await injectVslIdsIntoDom(browser, extraction.elements, /* @__PURE__ */ new Map(), sessionId);
|
|
3918
|
+
const revalidation = await target.evaluate(({ sel, expectedTag: expectedTag2 }) => {
|
|
3919
|
+
const el = document.querySelector(sel);
|
|
3920
|
+
if (!el) return { exists: false, valid: false, actualTag: "" };
|
|
3921
|
+
const actualTag = el.tagName.toLowerCase();
|
|
3922
|
+
return { exists: true, valid: actualTag === expectedTag2, actualTag };
|
|
3923
|
+
}, { sel: selector, expectedTag });
|
|
3924
|
+
if (!revalidation.valid) {
|
|
3925
|
+
return {
|
|
3926
|
+
status: "error",
|
|
3927
|
+
error: `Tag mismatch after re-injection: expected ${expectedTag}, got ${revalidation.actualTag} for ${args.target_id}`
|
|
3928
|
+
};
|
|
3967
3929
|
}
|
|
3968
3930
|
}
|
|
3931
|
+
} catch (validationError) {
|
|
3932
|
+
console.warn(`[VSL] Element validation skipped (target.evaluate not available): ${validationError}`);
|
|
3969
3933
|
}
|
|
3970
|
-
scrollResult.metadata = computeToolMetrics(scrollResult.data, startTime);
|
|
3971
|
-
return scrollResult;
|
|
3972
3934
|
}
|
|
3973
|
-
|
|
3974
|
-
|
|
3975
|
-
|
|
3976
|
-
|
|
3977
|
-
|
|
3978
|
-
|
|
3979
|
-
|
|
3980
|
-
|
|
3981
|
-
|
|
3982
|
-
const el = document.querySelector(sel);
|
|
3983
|
-
if (el) el.value = val;
|
|
3984
|
-
}, sessionId, { sel: selector, val: args.value });
|
|
3985
|
-
} else {
|
|
3986
|
-
await browser.evaluate(({ sel, val }) => {
|
|
3987
|
-
const el = document.querySelector(sel);
|
|
3988
|
-
if (el) el.value = val;
|
|
3989
|
-
}, sessionId, { sel: selector, val: args.value });
|
|
3990
|
-
}
|
|
3991
|
-
break;
|
|
3992
|
-
case "hover":
|
|
3993
|
-
if (targetFrame) {
|
|
3994
|
-
await browser.evaluateInFrame(targetFrame, (sel) => {
|
|
3995
|
-
const el = document.querySelector(sel);
|
|
3996
|
-
if (el) {
|
|
3997
|
-
const event = new MouseEvent("mouseover", { bubbles: true });
|
|
3998
|
-
el.dispatchEvent(event);
|
|
3935
|
+
switch (args.action) {
|
|
3936
|
+
case "click": {
|
|
3937
|
+
try {
|
|
3938
|
+
const clickElementCount = await target.locator(selector).count();
|
|
3939
|
+
if (clickElementCount === 0) {
|
|
3940
|
+
return {
|
|
3941
|
+
status: "error",
|
|
3942
|
+
error: `Element not found for click: ${args.target_id} (selector: ${selector})`
|
|
3943
|
+
};
|
|
3999
3944
|
}
|
|
4000
|
-
|
|
4001
|
-
|
|
4002
|
-
|
|
4003
|
-
|
|
4004
|
-
|
|
4005
|
-
|
|
4006
|
-
|
|
3945
|
+
await target.locator(selector).click({ timeout: 1e3 });
|
|
3946
|
+
} catch (locatorError) {
|
|
3947
|
+
let findObjectById2 = function(objects, targetId) {
|
|
3948
|
+
for (const obj of objects) {
|
|
3949
|
+
const o = obj;
|
|
3950
|
+
if (o.id === targetId) return o;
|
|
3951
|
+
if (o.ch && Array.isArray(o.ch)) {
|
|
3952
|
+
const found = findObjectById2(o.ch, targetId);
|
|
3953
|
+
if (found) return found;
|
|
3954
|
+
}
|
|
3955
|
+
}
|
|
3956
|
+
return null;
|
|
3957
|
+
};
|
|
3958
|
+
var findObjectById = findObjectById2;
|
|
3959
|
+
console.warn(`[VSL] Standard locator failed for ${args.target_id}, trying positional mapping fallback`);
|
|
3960
|
+
const snapshotDoc = session.getSnapshot();
|
|
3961
|
+
const snapshotObjects = snapshotDoc.objects ?? [];
|
|
3962
|
+
const snapshotObj = findObjectById2(snapshotObjects, localId);
|
|
3963
|
+
if (!snapshotObj || !snapshotObj.rect) {
|
|
3964
|
+
console.error(`[VSL] Positional mapping failed: element ${localId} not found in snapshot or has no rect`);
|
|
3965
|
+
throw locatorError;
|
|
4007
3966
|
}
|
|
4008
|
-
|
|
4009
|
-
|
|
4010
|
-
|
|
4011
|
-
|
|
4012
|
-
|
|
4013
|
-
|
|
4014
|
-
|
|
4015
|
-
|
|
4016
|
-
|
|
4017
|
-
|
|
4018
|
-
|
|
4019
|
-
|
|
4020
|
-
|
|
4021
|
-
|
|
4022
|
-
|
|
4023
|
-
|
|
4024
|
-
|
|
4025
|
-
|
|
4026
|
-
|
|
4027
|
-
|
|
4028
|
-
|
|
4029
|
-
|
|
4030
|
-
} else {
|
|
4031
|
-
await browser.evaluate((sel) => {
|
|
4032
|
-
const el = document.querySelector(sel);
|
|
4033
|
-
if (el) el.focus();
|
|
4034
|
-
}, sessionId, selector);
|
|
3967
|
+
const rect = snapshotObj.rect;
|
|
3968
|
+
const centerX = rect.x + rect.width / 2;
|
|
3969
|
+
const centerY = rect.y + rect.height / 2;
|
|
3970
|
+
const elementFound = await target.evaluate(
|
|
3971
|
+
([x, y]) => {
|
|
3972
|
+
const el = document.elementFromPoint(x, y);
|
|
3973
|
+
if (el) {
|
|
3974
|
+
el.click();
|
|
3975
|
+
return true;
|
|
3976
|
+
}
|
|
3977
|
+
return false;
|
|
3978
|
+
},
|
|
3979
|
+
[centerX, centerY]
|
|
3980
|
+
);
|
|
3981
|
+
if (!elementFound) {
|
|
3982
|
+
console.error(`[VSL] Positional mapping failed: no element found at (${centerX}, ${centerY})`);
|
|
3983
|
+
throw locatorError;
|
|
3984
|
+
}
|
|
3985
|
+
console.log(`[VSL] Positional mapping succeeded: clicked element at (${centerX}, ${centerY}) for ${args.target_id}`);
|
|
3986
|
+
}
|
|
3987
|
+
await page.waitForTimeout(100);
|
|
3988
|
+
break;
|
|
4035
3989
|
}
|
|
4036
|
-
|
|
4037
|
-
|
|
4038
|
-
|
|
4039
|
-
|
|
4040
|
-
|
|
4041
|
-
|
|
3990
|
+
case "fill":
|
|
3991
|
+
case "type":
|
|
3992
|
+
if (!args.value) {
|
|
3993
|
+
return {
|
|
3994
|
+
status: "error",
|
|
3995
|
+
error: "value is required for type action"
|
|
3996
|
+
};
|
|
3997
|
+
}
|
|
3998
|
+
const fillElementCount = await target.locator(selector).count();
|
|
3999
|
+
if (fillElementCount === 0) {
|
|
4000
|
+
return {
|
|
4001
|
+
status: "error",
|
|
4002
|
+
error: `Element not found for fill: ${args.target_id} (selector: ${selector})`
|
|
4003
|
+
};
|
|
4004
|
+
}
|
|
4005
|
+
await target.locator(selector).fill(args.value, { timeout: 1e3 });
|
|
4006
|
+
break;
|
|
4007
|
+
case "scroll": {
|
|
4008
|
+
const DEFAULT_SCROLL_AMOUNT = 500;
|
|
4009
|
+
const scrollValue = args.value || "down";
|
|
4010
|
+
const scrollParts = scrollValue.trim().split(":");
|
|
4011
|
+
const scrollDir = scrollParts[0];
|
|
4012
|
+
const scrollAmount = scrollParts[1] !== void 0 ? Number(scrollParts[1]) : DEFAULT_SCROLL_AMOUNT;
|
|
4013
|
+
const dx = scrollDir === "left" ? -scrollAmount : scrollDir === "right" ? scrollAmount : 0;
|
|
4014
|
+
const dy = scrollDir === "up" ? -scrollAmount : scrollDir === "down" ? scrollAmount : 0;
|
|
4015
|
+
await browser.evaluate(({ dx: dx2, dy: dy2 }) => {
|
|
4016
|
+
window.scrollBy(dx2, dy2);
|
|
4017
|
+
}, sessionId, { dx, dy });
|
|
4018
|
+
await page.waitForTimeout(50);
|
|
4019
|
+
const prevScroll = session.getScrollContext();
|
|
4020
|
+
let newScroll = null;
|
|
4021
|
+
try {
|
|
4022
|
+
const raw = await browser.evaluate(() => ({
|
|
4023
|
+
x: window.scrollX,
|
|
4024
|
+
y: window.scrollY,
|
|
4025
|
+
width: document.documentElement.scrollWidth,
|
|
4026
|
+
height: document.documentElement.scrollHeight
|
|
4027
|
+
}), sessionId);
|
|
4028
|
+
const r = raw;
|
|
4029
|
+
if (r !== null && typeof r === "object" && typeof r.x === "number" && typeof r.y === "number" && typeof r.width === "number" && typeof r.height === "number") {
|
|
4030
|
+
newScroll = r;
|
|
4031
|
+
}
|
|
4032
|
+
} catch {
|
|
4033
|
+
}
|
|
4034
|
+
if (!newScroll) {
|
|
4035
|
+
newScroll = serverSideScrollFallback(prevScroll, dx, dy, session.getSnapshot().canvas?.viewport);
|
|
4036
|
+
}
|
|
4037
|
+
session.setScrollContext(newScroll);
|
|
4038
|
+
const scrollResult = {
|
|
4039
|
+
status: "success",
|
|
4040
|
+
data: {
|
|
4041
|
+
action: args.action,
|
|
4042
|
+
target_id: args.target_id,
|
|
4043
|
+
success: true
|
|
4044
|
+
}
|
|
4042
4045
|
};
|
|
4046
|
+
if (args.return_state !== false) {
|
|
4047
|
+
const state = buildScrollStateFromCache(session, newScroll);
|
|
4048
|
+
if (scrollResult.data) {
|
|
4049
|
+
scrollResult.data.state = state;
|
|
4050
|
+
if (state.error) {
|
|
4051
|
+
scrollResult.warning = `State extraction failed: ${state.error}`;
|
|
4052
|
+
}
|
|
4053
|
+
}
|
|
4054
|
+
}
|
|
4055
|
+
scrollResult.metadata = computeToolMetrics(scrollResult.data, startTime);
|
|
4056
|
+
return scrollResult;
|
|
4043
4057
|
}
|
|
4044
|
-
|
|
4045
|
-
|
|
4046
|
-
|
|
4047
|
-
|
|
4048
|
-
|
|
4049
|
-
|
|
4050
|
-
|
|
4058
|
+
case "select":
|
|
4059
|
+
if (!args.value) {
|
|
4060
|
+
return {
|
|
4061
|
+
status: "error",
|
|
4062
|
+
error: "value is required for select action"
|
|
4063
|
+
};
|
|
4064
|
+
}
|
|
4051
4065
|
if (targetFrame) {
|
|
4052
|
-
await browser.evaluateInFrame(targetFrame, (
|
|
4053
|
-
const
|
|
4054
|
-
|
|
4055
|
-
|
|
4056
|
-
document.body.appendChild(a);
|
|
4057
|
-
a.click();
|
|
4058
|
-
document.body.removeChild(a);
|
|
4059
|
-
}, sessionId, args.value);
|
|
4066
|
+
await browser.evaluateInFrame(targetFrame, ({ sel, val }) => {
|
|
4067
|
+
const el = document.querySelector(sel);
|
|
4068
|
+
if (el) el.value = val;
|
|
4069
|
+
}, sessionId, { sel: selector, val: args.value });
|
|
4060
4070
|
} else {
|
|
4061
|
-
await browser.evaluate((
|
|
4062
|
-
const
|
|
4063
|
-
|
|
4064
|
-
|
|
4065
|
-
document.body.appendChild(a);
|
|
4066
|
-
a.click();
|
|
4067
|
-
document.body.removeChild(a);
|
|
4068
|
-
}, sessionId, args.value);
|
|
4071
|
+
await browser.evaluate(({ sel, val }) => {
|
|
4072
|
+
const el = document.querySelector(sel);
|
|
4073
|
+
if (el) el.value = val;
|
|
4074
|
+
}, sessionId, { sel: selector, val: args.value });
|
|
4069
4075
|
}
|
|
4070
|
-
|
|
4076
|
+
break;
|
|
4077
|
+
case "hover":
|
|
4071
4078
|
if (targetFrame) {
|
|
4072
4079
|
await browser.evaluateInFrame(targetFrame, (sel) => {
|
|
4073
4080
|
const el = document.querySelector(sel);
|
|
4074
|
-
if (el)
|
|
4081
|
+
if (el) {
|
|
4082
|
+
const event = new MouseEvent("mouseover", { bubbles: true });
|
|
4083
|
+
el.dispatchEvent(event);
|
|
4084
|
+
}
|
|
4075
4085
|
}, sessionId, selector);
|
|
4076
4086
|
} else {
|
|
4077
4087
|
await browser.evaluate((sel) => {
|
|
4078
4088
|
const el = document.querySelector(sel);
|
|
4079
|
-
if (el)
|
|
4089
|
+
if (el) {
|
|
4090
|
+
const event = new MouseEvent("mouseover", { bubbles: true });
|
|
4091
|
+
el.dispatchEvent(event);
|
|
4092
|
+
}
|
|
4080
4093
|
}, sessionId, selector);
|
|
4081
4094
|
}
|
|
4082
|
-
|
|
4083
|
-
|
|
4084
|
-
|
|
4085
|
-
|
|
4086
|
-
|
|
4087
|
-
|
|
4088
|
-
|
|
4089
|
-
|
|
4090
|
-
await browser.evaluate(() =>
|
|
4095
|
+
break;
|
|
4096
|
+
case "blur":
|
|
4097
|
+
if (targetFrame) {
|
|
4098
|
+
await browser.evaluateInFrame(targetFrame, (sel) => {
|
|
4099
|
+
const el = document.querySelector(sel);
|
|
4100
|
+
if (el) el.blur();
|
|
4101
|
+
}, sessionId, selector);
|
|
4102
|
+
} else {
|
|
4103
|
+
await browser.evaluate((sel) => {
|
|
4104
|
+
const el = document.querySelector(sel);
|
|
4105
|
+
if (el) el.blur();
|
|
4106
|
+
}, sessionId, selector);
|
|
4091
4107
|
}
|
|
4092
|
-
|
|
4093
|
-
|
|
4094
|
-
|
|
4095
|
-
|
|
4096
|
-
|
|
4097
|
-
|
|
4098
|
-
|
|
4099
|
-
|
|
4100
|
-
|
|
4101
|
-
|
|
4102
|
-
|
|
4103
|
-
|
|
4104
|
-
status: "success",
|
|
4105
|
-
data: {
|
|
4106
|
-
action: args.action,
|
|
4107
|
-
target_id: args.target_id,
|
|
4108
|
-
success: downloadInfo.status === "completed",
|
|
4109
|
-
download: {
|
|
4110
|
-
downloadId: downloadInfo.downloadId,
|
|
4111
|
-
filename: downloadInfo.filename,
|
|
4112
|
-
url: downloadInfo.url,
|
|
4113
|
-
status: downloadInfo.status,
|
|
4114
|
-
path: args.save_path ?? downloadInfo.path ?? void 0
|
|
4115
|
-
}
|
|
4108
|
+
break;
|
|
4109
|
+
case "focus":
|
|
4110
|
+
if (targetFrame) {
|
|
4111
|
+
await browser.evaluateInFrame(targetFrame, (sel) => {
|
|
4112
|
+
const el = document.querySelector(sel);
|
|
4113
|
+
if (el) el.focus();
|
|
4114
|
+
}, sessionId, selector);
|
|
4115
|
+
} else {
|
|
4116
|
+
await browser.evaluate((sel) => {
|
|
4117
|
+
const el = document.querySelector(sel);
|
|
4118
|
+
if (el) el.focus();
|
|
4119
|
+
}, sessionId, selector);
|
|
4116
4120
|
}
|
|
4117
|
-
|
|
4118
|
-
|
|
4119
|
-
|
|
4120
|
-
|
|
4121
|
-
|
|
4122
|
-
|
|
4123
|
-
|
|
4124
|
-
result2.warning = `State extraction failed: ${state.error}`;
|
|
4125
|
-
}
|
|
4121
|
+
break;
|
|
4122
|
+
case "press": {
|
|
4123
|
+
if (!args.value) {
|
|
4124
|
+
return {
|
|
4125
|
+
status: "error",
|
|
4126
|
+
error: 'value is required for press action (key name, e.g. "Enter", "Tab", "Escape")'
|
|
4127
|
+
};
|
|
4126
4128
|
}
|
|
4129
|
+
await page.keyboard.press(args.value, { delay: 0 });
|
|
4130
|
+
await page.waitForTimeout(50);
|
|
4131
|
+
break;
|
|
4127
4132
|
}
|
|
4128
|
-
|
|
4129
|
-
|
|
4130
|
-
|
|
4131
|
-
|
|
4132
|
-
|
|
4133
|
-
|
|
4134
|
-
|
|
4135
|
-
|
|
4136
|
-
|
|
4137
|
-
|
|
4138
|
-
|
|
4139
|
-
|
|
4140
|
-
|
|
4141
|
-
|
|
4142
|
-
|
|
4143
|
-
|
|
4144
|
-
|
|
4145
|
-
|
|
4146
|
-
|
|
4147
|
-
|
|
4148
|
-
|
|
4149
|
-
action: args.action,
|
|
4150
|
-
target_id: args.target_id,
|
|
4151
|
-
success: true,
|
|
4152
|
-
upload: {
|
|
4153
|
-
selector,
|
|
4154
|
-
files: filePaths,
|
|
4155
|
-
success: true
|
|
4133
|
+
case "download": {
|
|
4134
|
+
const downloadsBefore = browser.getDownloads();
|
|
4135
|
+
if (args.value) {
|
|
4136
|
+
if (targetFrame) {
|
|
4137
|
+
await browser.evaluateInFrame(targetFrame, (url) => {
|
|
4138
|
+
const a = document.createElement("a");
|
|
4139
|
+
a.href = url;
|
|
4140
|
+
a.download = "";
|
|
4141
|
+
document.body.appendChild(a);
|
|
4142
|
+
a.click();
|
|
4143
|
+
document.body.removeChild(a);
|
|
4144
|
+
}, sessionId, args.value);
|
|
4145
|
+
} else {
|
|
4146
|
+
await browser.evaluate((url) => {
|
|
4147
|
+
const a = document.createElement("a");
|
|
4148
|
+
a.href = url;
|
|
4149
|
+
a.download = "";
|
|
4150
|
+
document.body.appendChild(a);
|
|
4151
|
+
a.click();
|
|
4152
|
+
document.body.removeChild(a);
|
|
4153
|
+
}, sessionId, args.value);
|
|
4156
4154
|
}
|
|
4157
|
-
}
|
|
4158
|
-
|
|
4159
|
-
|
|
4160
|
-
|
|
4161
|
-
|
|
4162
|
-
|
|
4163
|
-
|
|
4164
|
-
|
|
4165
|
-
|
|
4155
|
+
} else {
|
|
4156
|
+
if (targetFrame) {
|
|
4157
|
+
await browser.evaluateInFrame(targetFrame, (sel) => {
|
|
4158
|
+
const el = document.querySelector(sel);
|
|
4159
|
+
if (el) el.click();
|
|
4160
|
+
}, sessionId, selector);
|
|
4161
|
+
} else {
|
|
4162
|
+
await browser.evaluate((sel) => {
|
|
4163
|
+
const el = document.querySelector(sel);
|
|
4164
|
+
if (el) el.click();
|
|
4165
|
+
}, sessionId, selector);
|
|
4166
4166
|
}
|
|
4167
4167
|
}
|
|
4168
|
-
|
|
4169
|
-
|
|
4170
|
-
|
|
4171
|
-
|
|
4172
|
-
|
|
4173
|
-
|
|
4174
|
-
|
|
4175
|
-
|
|
4176
|
-
if (el) {
|
|
4177
|
-
el.value = "";
|
|
4178
|
-
el.dispatchEvent(new Event("input", { bubbles: true }));
|
|
4179
|
-
el.dispatchEvent(new Event("change", { bubbles: true }));
|
|
4168
|
+
const pollDeadline = Date.now() + 5e3;
|
|
4169
|
+
let newDownload;
|
|
4170
|
+
while (!newDownload && Date.now() < pollDeadline) {
|
|
4171
|
+
newDownload = browser.getDownloads().find(
|
|
4172
|
+
(d) => !downloadsBefore.some((b) => b.downloadId === d.downloadId)
|
|
4173
|
+
);
|
|
4174
|
+
if (!newDownload) {
|
|
4175
|
+
await browser.evaluate(() => new Promise((resolve) => setTimeout(resolve, 50)), sessionId);
|
|
4180
4176
|
}
|
|
4181
|
-
}
|
|
4182
|
-
|
|
4183
|
-
|
|
4184
|
-
|
|
4185
|
-
|
|
4186
|
-
|
|
4187
|
-
|
|
4188
|
-
|
|
4177
|
+
}
|
|
4178
|
+
if (!newDownload) {
|
|
4179
|
+
return {
|
|
4180
|
+
status: "error",
|
|
4181
|
+
error: "Download not detected after click (timeout 5000ms). The page may navigate instead of downloading."
|
|
4182
|
+
};
|
|
4183
|
+
}
|
|
4184
|
+
const downloadInfo = await browser.waitForDownload(newDownload.downloadId, args.timeout);
|
|
4185
|
+
if (args.save_path) {
|
|
4186
|
+
await browser.saveDownload(downloadInfo.downloadId, args.save_path);
|
|
4187
|
+
}
|
|
4188
|
+
const result2 = {
|
|
4189
|
+
status: "success",
|
|
4190
|
+
data: {
|
|
4191
|
+
action: args.action,
|
|
4192
|
+
target_id: args.target_id,
|
|
4193
|
+
success: downloadInfo.status === "completed",
|
|
4194
|
+
download: {
|
|
4195
|
+
downloadId: downloadInfo.downloadId,
|
|
4196
|
+
filename: downloadInfo.filename,
|
|
4197
|
+
url: downloadInfo.url,
|
|
4198
|
+
status: downloadInfo.status,
|
|
4199
|
+
path: args.save_path ?? downloadInfo.path ?? void 0
|
|
4200
|
+
}
|
|
4189
4201
|
}
|
|
4190
|
-
}, sessionId, selector);
|
|
4191
|
-
}
|
|
4192
|
-
break;
|
|
4193
|
-
}
|
|
4194
|
-
case "drag": {
|
|
4195
|
-
if (!args.value) {
|
|
4196
|
-
return {
|
|
4197
|
-
status: "error",
|
|
4198
|
-
error: "value is required for drag action (destination element ID)"
|
|
4199
4202
|
};
|
|
4200
|
-
|
|
4201
|
-
|
|
4202
|
-
|
|
4203
|
-
|
|
4204
|
-
|
|
4205
|
-
|
|
4206
|
-
|
|
4207
|
-
|
|
4208
|
-
if (source && destination) {
|
|
4209
|
-
const dispatchDragEvent = (el, type) => {
|
|
4210
|
-
if (typeof DragEvent === "function") {
|
|
4211
|
-
el.dispatchEvent(new DragEvent(type, { bubbles: true, cancelable: true }));
|
|
4212
|
-
} else {
|
|
4213
|
-
el.dispatchEvent(new Event(type, { bubbles: true, cancelable: true }));
|
|
4203
|
+
if (args.return_state !== false) {
|
|
4204
|
+
try {
|
|
4205
|
+
const currentUrl = page.url();
|
|
4206
|
+
const state = await getStateAfterAction(session, browser, currentUrl, sessionId);
|
|
4207
|
+
if (result2.data) {
|
|
4208
|
+
result2.data.state = state;
|
|
4209
|
+
if (state.error) {
|
|
4210
|
+
result2.warning = `State extraction failed: ${state.error}`;
|
|
4214
4211
|
}
|
|
4215
|
-
}
|
|
4216
|
-
|
|
4217
|
-
|
|
4218
|
-
dispatchDragEvent(destination, "dragover");
|
|
4219
|
-
dispatchDragEvent(destination, "drop");
|
|
4220
|
-
dispatchDragEvent(source, "dragend");
|
|
4221
|
-
}
|
|
4222
|
-
}, sessionId, { srcSel: selector, dstSel: destSelector });
|
|
4223
|
-
} else {
|
|
4224
|
-
await browser.evaluate(({ srcSel, dstSel }) => {
|
|
4225
|
-
const source = document.querySelector(srcSel);
|
|
4226
|
-
const destination = document.querySelector(dstSel);
|
|
4227
|
-
if (source && destination) {
|
|
4228
|
-
const dispatchDragEvent = (el, type) => {
|
|
4229
|
-
if (typeof DragEvent === "function") {
|
|
4230
|
-
el.dispatchEvent(new DragEvent(type, { bubbles: true, cancelable: true }));
|
|
4231
|
-
} else {
|
|
4232
|
-
el.dispatchEvent(new Event(type, { bubbles: true, cancelable: true }));
|
|
4233
|
-
}
|
|
4234
|
-
};
|
|
4235
|
-
dispatchDragEvent(source, "dragstart");
|
|
4236
|
-
dispatchDragEvent(destination, "dragenter");
|
|
4237
|
-
dispatchDragEvent(destination, "dragover");
|
|
4238
|
-
dispatchDragEvent(destination, "drop");
|
|
4239
|
-
dispatchDragEvent(source, "dragend");
|
|
4212
|
+
}
|
|
4213
|
+
} catch (stateError) {
|
|
4214
|
+
result2.warning = `State extraction failed: ${stateError instanceof Error ? stateError.message : String(stateError)}`;
|
|
4240
4215
|
}
|
|
4241
|
-
}
|
|
4216
|
+
}
|
|
4217
|
+
result2.metadata = computeToolMetrics(result2.data, startTime);
|
|
4218
|
+
return result2;
|
|
4242
4219
|
}
|
|
4243
|
-
|
|
4244
|
-
|
|
4245
|
-
|
|
4246
|
-
|
|
4247
|
-
|
|
4248
|
-
|
|
4249
|
-
|
|
4250
|
-
|
|
4251
|
-
|
|
4252
|
-
|
|
4253
|
-
|
|
4254
|
-
|
|
4255
|
-
|
|
4256
|
-
|
|
4257
|
-
|
|
4258
|
-
|
|
4220
|
+
case "upload": {
|
|
4221
|
+
if (!args.value) {
|
|
4222
|
+
return {
|
|
4223
|
+
status: "error",
|
|
4224
|
+
error: "value is required for upload action (file path(s), comma-separated)"
|
|
4225
|
+
};
|
|
4226
|
+
}
|
|
4227
|
+
const filePaths = args.value.split(",").map((p) => p.trim()).filter(Boolean);
|
|
4228
|
+
if (filePaths.length === 0) {
|
|
4229
|
+
return {
|
|
4230
|
+
status: "error",
|
|
4231
|
+
error: "value must contain at least one file path"
|
|
4232
|
+
};
|
|
4233
|
+
}
|
|
4234
|
+
const uploadElementCount = await target.locator(selector).count();
|
|
4235
|
+
if (uploadElementCount === 0) {
|
|
4236
|
+
return {
|
|
4237
|
+
status: "error",
|
|
4238
|
+
error: `Element not found for upload: ${args.target_id} (selector: ${selector})`
|
|
4239
|
+
};
|
|
4240
|
+
}
|
|
4241
|
+
await target.locator(selector).setInputFiles(filePaths.length === 1 ? filePaths[0] : filePaths, { timeout: 1e3 });
|
|
4242
|
+
const result2 = {
|
|
4243
|
+
status: "success",
|
|
4244
|
+
data: {
|
|
4245
|
+
action: args.action,
|
|
4246
|
+
target_id: args.target_id,
|
|
4247
|
+
success: true,
|
|
4248
|
+
upload: {
|
|
4249
|
+
selector,
|
|
4250
|
+
files: filePaths,
|
|
4251
|
+
success: true
|
|
4252
|
+
}
|
|
4259
4253
|
}
|
|
4260
|
-
}
|
|
4261
|
-
|
|
4262
|
-
|
|
4263
|
-
const
|
|
4264
|
-
if (
|
|
4265
|
-
|
|
4266
|
-
|
|
4267
|
-
|
|
4268
|
-
|
|
4269
|
-
el2.dispatchEvent(new Event(type, { bubbles: true, cancelable: true }));
|
|
4270
|
-
}
|
|
4271
|
-
};
|
|
4272
|
-
dispatchDragEvent(el, "dragover");
|
|
4273
|
-
dispatchDragEvent(el, "drop");
|
|
4254
|
+
};
|
|
4255
|
+
if (args.return_state !== false) {
|
|
4256
|
+
const currentUrl = await browser.evaluate(() => window.location.href, sessionId);
|
|
4257
|
+
const state = await getStateAfterAction(session, browser, currentUrl, sessionId);
|
|
4258
|
+
if (result2.data) {
|
|
4259
|
+
result2.data.state = state;
|
|
4260
|
+
if (state.error) {
|
|
4261
|
+
result2.warning = `State extraction failed: ${state.error}`;
|
|
4262
|
+
}
|
|
4274
4263
|
}
|
|
4275
|
-
}
|
|
4264
|
+
}
|
|
4265
|
+
result2.metadata = computeToolMetrics(result2.data, startTime);
|
|
4266
|
+
return result2;
|
|
4276
4267
|
}
|
|
4277
|
-
|
|
4278
|
-
|
|
4279
|
-
|
|
4280
|
-
|
|
4281
|
-
|
|
4282
|
-
|
|
4283
|
-
|
|
4284
|
-
|
|
4285
|
-
|
|
4286
|
-
|
|
4287
|
-
|
|
4288
|
-
|
|
4289
|
-
|
|
4290
|
-
|
|
4291
|
-
|
|
4292
|
-
|
|
4293
|
-
|
|
4268
|
+
case "clear": {
|
|
4269
|
+
if (targetFrame) {
|
|
4270
|
+
await browser.evaluateInFrame(targetFrame, (sel) => {
|
|
4271
|
+
const el = document.querySelector(sel);
|
|
4272
|
+
if (el) {
|
|
4273
|
+
el.value = "";
|
|
4274
|
+
el.dispatchEvent(new Event("input", { bubbles: true }));
|
|
4275
|
+
el.dispatchEvent(new Event("change", { bubbles: true }));
|
|
4276
|
+
}
|
|
4277
|
+
}, sessionId, selector);
|
|
4278
|
+
} else {
|
|
4279
|
+
await browser.evaluate((sel) => {
|
|
4280
|
+
const el = document.querySelector(sel);
|
|
4281
|
+
if (el) {
|
|
4282
|
+
el.value = "";
|
|
4283
|
+
el.dispatchEvent(new Event("input", { bubbles: true }));
|
|
4284
|
+
el.dispatchEvent(new Event("change", { bubbles: true }));
|
|
4285
|
+
}
|
|
4286
|
+
}, sessionId, selector);
|
|
4287
|
+
}
|
|
4288
|
+
break;
|
|
4294
4289
|
}
|
|
4295
|
-
|
|
4296
|
-
|
|
4297
|
-
|
|
4298
|
-
|
|
4299
|
-
|
|
4300
|
-
|
|
4301
|
-
|
|
4302
|
-
|
|
4303
|
-
|
|
4304
|
-
},
|
|
4305
|
-
|
|
4306
|
-
|
|
4307
|
-
|
|
4308
|
-
|
|
4309
|
-
|
|
4310
|
-
|
|
4311
|
-
|
|
4290
|
+
case "drag": {
|
|
4291
|
+
if (!args.value) {
|
|
4292
|
+
return {
|
|
4293
|
+
status: "error",
|
|
4294
|
+
error: "value is required for drag action (destination element ID)"
|
|
4295
|
+
};
|
|
4296
|
+
}
|
|
4297
|
+
const destIdMap = session.getIdMap();
|
|
4298
|
+
const destResolvedId = destIdMap.get(args.value) || args.value;
|
|
4299
|
+
const destSelector = `[data-vsl-id="${destResolvedId}"], #${destResolvedId}, .${destResolvedId}`;
|
|
4300
|
+
if (targetFrame) {
|
|
4301
|
+
await browser.evaluateInFrame(targetFrame, ({ srcSel, dstSel }) => {
|
|
4302
|
+
const source = document.querySelector(srcSel);
|
|
4303
|
+
const destination = document.querySelector(dstSel);
|
|
4304
|
+
if (source && destination) {
|
|
4305
|
+
const dispatchDragEvent = (el, type) => {
|
|
4306
|
+
if (typeof DragEvent === "function") {
|
|
4307
|
+
el.dispatchEvent(new DragEvent(type, { bubbles: true, cancelable: true }));
|
|
4308
|
+
} else {
|
|
4309
|
+
el.dispatchEvent(new Event(type, { bubbles: true, cancelable: true }));
|
|
4310
|
+
}
|
|
4311
|
+
};
|
|
4312
|
+
dispatchDragEvent(source, "dragstart");
|
|
4313
|
+
dispatchDragEvent(destination, "dragenter");
|
|
4314
|
+
dispatchDragEvent(destination, "dragover");
|
|
4315
|
+
dispatchDragEvent(destination, "drop");
|
|
4316
|
+
dispatchDragEvent(source, "dragend");
|
|
4317
|
+
}
|
|
4318
|
+
}, sessionId, { srcSel: selector, dstSel: destSelector });
|
|
4319
|
+
} else {
|
|
4320
|
+
await browser.evaluate(({ srcSel, dstSel }) => {
|
|
4321
|
+
const source = document.querySelector(srcSel);
|
|
4322
|
+
const destination = document.querySelector(dstSel);
|
|
4323
|
+
if (source && destination) {
|
|
4324
|
+
const dispatchDragEvent = (el, type) => {
|
|
4325
|
+
if (typeof DragEvent === "function") {
|
|
4326
|
+
el.dispatchEvent(new DragEvent(type, { bubbles: true, cancelable: true }));
|
|
4327
|
+
} else {
|
|
4328
|
+
el.dispatchEvent(new Event(type, { bubbles: true, cancelable: true }));
|
|
4329
|
+
}
|
|
4330
|
+
};
|
|
4331
|
+
dispatchDragEvent(source, "dragstart");
|
|
4332
|
+
dispatchDragEvent(destination, "dragenter");
|
|
4333
|
+
dispatchDragEvent(destination, "dragover");
|
|
4334
|
+
dispatchDragEvent(destination, "drop");
|
|
4335
|
+
dispatchDragEvent(source, "dragend");
|
|
4336
|
+
}
|
|
4337
|
+
}, sessionId, { srcSel: selector, dstSel: destSelector });
|
|
4338
|
+
}
|
|
4339
|
+
break;
|
|
4312
4340
|
}
|
|
4313
|
-
|
|
4314
|
-
|
|
4315
|
-
|
|
4316
|
-
|
|
4317
|
-
|
|
4318
|
-
|
|
4319
|
-
|
|
4320
|
-
|
|
4321
|
-
|
|
4322
|
-
|
|
4323
|
-
|
|
4324
|
-
|
|
4325
|
-
|
|
4326
|
-
|
|
4327
|
-
|
|
4328
|
-
|
|
4329
|
-
|
|
4330
|
-
|
|
4331
|
-
el
|
|
4332
|
-
|
|
4333
|
-
|
|
4341
|
+
case "drop": {
|
|
4342
|
+
if (targetFrame) {
|
|
4343
|
+
await browser.evaluateInFrame(targetFrame, (sel) => {
|
|
4344
|
+
const el = document.querySelector(sel);
|
|
4345
|
+
if (el) {
|
|
4346
|
+
const dispatchDragEvent = (el2, type) => {
|
|
4347
|
+
if (typeof DragEvent === "function") {
|
|
4348
|
+
el2.dispatchEvent(new DragEvent(type, { bubbles: true, cancelable: true }));
|
|
4349
|
+
} else {
|
|
4350
|
+
el2.dispatchEvent(new Event(type, { bubbles: true, cancelable: true }));
|
|
4351
|
+
}
|
|
4352
|
+
};
|
|
4353
|
+
dispatchDragEvent(el, "dragover");
|
|
4354
|
+
dispatchDragEvent(el, "drop");
|
|
4355
|
+
}
|
|
4356
|
+
}, sessionId, selector);
|
|
4357
|
+
} else {
|
|
4358
|
+
await browser.evaluate((sel) => {
|
|
4359
|
+
const el = document.querySelector(sel);
|
|
4360
|
+
if (el) {
|
|
4361
|
+
const dispatchDragEvent = (el2, type) => {
|
|
4362
|
+
if (typeof DragEvent === "function") {
|
|
4363
|
+
el2.dispatchEvent(new DragEvent(type, { bubbles: true, cancelable: true }));
|
|
4364
|
+
} else {
|
|
4365
|
+
el2.dispatchEvent(new Event(type, { bubbles: true, cancelable: true }));
|
|
4366
|
+
}
|
|
4367
|
+
};
|
|
4368
|
+
dispatchDragEvent(el, "dragover");
|
|
4369
|
+
dispatchDragEvent(el, "drop");
|
|
4370
|
+
}
|
|
4371
|
+
}, sessionId, selector);
|
|
4372
|
+
}
|
|
4373
|
+
break;
|
|
4334
4374
|
}
|
|
4335
|
-
|
|
4336
|
-
|
|
4337
|
-
|
|
4338
|
-
|
|
4339
|
-
|
|
4340
|
-
|
|
4341
|
-
|
|
4342
|
-
|
|
4343
|
-
|
|
4344
|
-
|
|
4345
|
-
|
|
4346
|
-
|
|
4347
|
-
|
|
4348
|
-
|
|
4349
|
-
|
|
4350
|
-
|
|
4351
|
-
|
|
4352
|
-
} else if (el instanceof HTMLDetailsElement) {
|
|
4353
|
-
el.open = false;
|
|
4354
|
-
}
|
|
4355
|
-
}, sessionId, selector);
|
|
4375
|
+
case "submit": {
|
|
4376
|
+
if (targetFrame) {
|
|
4377
|
+
await browser.evaluateInFrame(targetFrame, (sel) => {
|
|
4378
|
+
const form = document.querySelector(sel);
|
|
4379
|
+
if (form) {
|
|
4380
|
+
form.dispatchEvent(new Event("submit", { bubbles: true, cancelable: true }));
|
|
4381
|
+
}
|
|
4382
|
+
}, sessionId, selector);
|
|
4383
|
+
} else {
|
|
4384
|
+
await browser.evaluate((sel) => {
|
|
4385
|
+
const form = document.querySelector(sel);
|
|
4386
|
+
if (form) {
|
|
4387
|
+
form.dispatchEvent(new Event("submit", { bubbles: true, cancelable: true }));
|
|
4388
|
+
}
|
|
4389
|
+
}, sessionId, selector);
|
|
4390
|
+
}
|
|
4391
|
+
break;
|
|
4356
4392
|
}
|
|
4357
|
-
|
|
4358
|
-
|
|
4359
|
-
|
|
4360
|
-
|
|
4361
|
-
|
|
4362
|
-
|
|
4363
|
-
|
|
4364
|
-
|
|
4393
|
+
case "reset": {
|
|
4394
|
+
if (targetFrame) {
|
|
4395
|
+
await browser.evaluateInFrame(targetFrame, (sel) => {
|
|
4396
|
+
const form = document.querySelector(sel);
|
|
4397
|
+
if (form) {
|
|
4398
|
+
form.reset();
|
|
4399
|
+
}
|
|
4400
|
+
}, sessionId, selector);
|
|
4401
|
+
} else {
|
|
4402
|
+
await browser.evaluate((sel) => {
|
|
4403
|
+
const form = document.querySelector(sel);
|
|
4404
|
+
if (form) {
|
|
4405
|
+
form.reset();
|
|
4406
|
+
}
|
|
4407
|
+
}, sessionId, selector);
|
|
4408
|
+
}
|
|
4409
|
+
break;
|
|
4410
|
+
}
|
|
4411
|
+
case "open": {
|
|
4412
|
+
if (targetFrame) {
|
|
4413
|
+
await browser.evaluateInFrame(targetFrame, (sel) => {
|
|
4414
|
+
const el = document.querySelector(sel);
|
|
4415
|
+
if (typeof HTMLDialogElement !== "undefined" && el instanceof HTMLDialogElement) {
|
|
4416
|
+
el.showModal();
|
|
4417
|
+
} else if (el instanceof HTMLDetailsElement) {
|
|
4365
4418
|
el.open = true;
|
|
4366
|
-
} else if (el.hasAttribute("aria-expanded")) {
|
|
4367
|
-
el.setAttribute("aria-expanded", "true");
|
|
4368
4419
|
}
|
|
4369
|
-
}
|
|
4370
|
-
}
|
|
4371
|
-
|
|
4372
|
-
|
|
4373
|
-
|
|
4374
|
-
|
|
4375
|
-
if (el instanceof HTMLDetailsElement) {
|
|
4420
|
+
}, sessionId, selector);
|
|
4421
|
+
} else {
|
|
4422
|
+
await browser.evaluate((sel) => {
|
|
4423
|
+
const el = document.querySelector(sel);
|
|
4424
|
+
if (typeof HTMLDialogElement !== "undefined" && el instanceof HTMLDialogElement) {
|
|
4425
|
+
el.showModal();
|
|
4426
|
+
} else if (el instanceof HTMLDetailsElement) {
|
|
4376
4427
|
el.open = true;
|
|
4377
|
-
} else if (el.hasAttribute("aria-expanded")) {
|
|
4378
|
-
el.setAttribute("aria-expanded", "true");
|
|
4379
4428
|
}
|
|
4380
|
-
}
|
|
4381
|
-
}
|
|
4429
|
+
}, sessionId, selector);
|
|
4430
|
+
}
|
|
4431
|
+
break;
|
|
4382
4432
|
}
|
|
4383
|
-
|
|
4384
|
-
|
|
4385
|
-
|
|
4386
|
-
|
|
4387
|
-
|
|
4388
|
-
|
|
4389
|
-
|
|
4390
|
-
if (el instanceof HTMLDetailsElement) {
|
|
4433
|
+
case "close": {
|
|
4434
|
+
if (targetFrame) {
|
|
4435
|
+
await browser.evaluateInFrame(targetFrame, (sel) => {
|
|
4436
|
+
const el = document.querySelector(sel);
|
|
4437
|
+
if (typeof HTMLDialogElement !== "undefined" && el instanceof HTMLDialogElement) {
|
|
4438
|
+
el.close();
|
|
4439
|
+
} else if (el instanceof HTMLDetailsElement) {
|
|
4391
4440
|
el.open = false;
|
|
4392
|
-
} else if (el.hasAttribute("aria-expanded")) {
|
|
4393
|
-
el.setAttribute("aria-expanded", "false");
|
|
4394
4441
|
}
|
|
4395
|
-
}
|
|
4396
|
-
}
|
|
4397
|
-
|
|
4398
|
-
|
|
4399
|
-
|
|
4400
|
-
|
|
4401
|
-
if (el instanceof HTMLDetailsElement) {
|
|
4442
|
+
}, sessionId, selector);
|
|
4443
|
+
} else {
|
|
4444
|
+
await browser.evaluate((sel) => {
|
|
4445
|
+
const el = document.querySelector(sel);
|
|
4446
|
+
if (typeof HTMLDialogElement !== "undefined" && el instanceof HTMLDialogElement) {
|
|
4447
|
+
el.close();
|
|
4448
|
+
} else if (el instanceof HTMLDetailsElement) {
|
|
4402
4449
|
el.open = false;
|
|
4403
|
-
} else if (el.hasAttribute("aria-expanded")) {
|
|
4404
|
-
el.setAttribute("aria-expanded", "false");
|
|
4405
4450
|
}
|
|
4406
|
-
}
|
|
4407
|
-
}
|
|
4451
|
+
}, sessionId, selector);
|
|
4452
|
+
}
|
|
4453
|
+
break;
|
|
4408
4454
|
}
|
|
4409
|
-
|
|
4410
|
-
|
|
4411
|
-
|
|
4412
|
-
|
|
4455
|
+
case "expand": {
|
|
4456
|
+
if (targetFrame) {
|
|
4457
|
+
await browser.evaluateInFrame(targetFrame, (sel) => {
|
|
4458
|
+
const el = document.querySelector(sel);
|
|
4459
|
+
if (el) {
|
|
4460
|
+
if (el instanceof HTMLDetailsElement) {
|
|
4461
|
+
el.open = true;
|
|
4462
|
+
} else if (el.hasAttribute("aria-expanded")) {
|
|
4463
|
+
el.setAttribute("aria-expanded", "true");
|
|
4464
|
+
}
|
|
4465
|
+
}
|
|
4466
|
+
}, sessionId, selector);
|
|
4467
|
+
} else {
|
|
4468
|
+
await browser.evaluate((sel) => {
|
|
4469
|
+
const el = document.querySelector(sel);
|
|
4470
|
+
if (el) {
|
|
4471
|
+
if (el instanceof HTMLDetailsElement) {
|
|
4472
|
+
el.open = true;
|
|
4473
|
+
} else if (el.hasAttribute("aria-expanded")) {
|
|
4474
|
+
el.setAttribute("aria-expanded", "true");
|
|
4475
|
+
}
|
|
4476
|
+
}
|
|
4477
|
+
}, sessionId, selector);
|
|
4478
|
+
}
|
|
4479
|
+
break;
|
|
4480
|
+
}
|
|
4481
|
+
case "collapse": {
|
|
4482
|
+
if (targetFrame) {
|
|
4483
|
+
await browser.evaluateInFrame(targetFrame, (sel) => {
|
|
4484
|
+
const el = document.querySelector(sel);
|
|
4485
|
+
if (el) {
|
|
4486
|
+
if (el instanceof HTMLDetailsElement) {
|
|
4487
|
+
el.open = false;
|
|
4488
|
+
} else if (el.hasAttribute("aria-expanded")) {
|
|
4489
|
+
el.setAttribute("aria-expanded", "false");
|
|
4490
|
+
}
|
|
4491
|
+
}
|
|
4492
|
+
}, sessionId, selector);
|
|
4493
|
+
} else {
|
|
4494
|
+
await browser.evaluate((sel) => {
|
|
4495
|
+
const el = document.querySelector(sel);
|
|
4496
|
+
if (el) {
|
|
4497
|
+
if (el instanceof HTMLDetailsElement) {
|
|
4498
|
+
el.open = false;
|
|
4499
|
+
} else if (el.hasAttribute("aria-expanded")) {
|
|
4500
|
+
el.setAttribute("aria-expanded", "false");
|
|
4501
|
+
}
|
|
4502
|
+
}
|
|
4503
|
+
}, sessionId, selector);
|
|
4504
|
+
}
|
|
4505
|
+
break;
|
|
4506
|
+
}
|
|
4507
|
+
case "wait": {
|
|
4508
|
+
if (!args.value) {
|
|
4509
|
+
return {
|
|
4510
|
+
status: "error",
|
|
4511
|
+
error: 'value is required for wait action (selector or "idle")'
|
|
4512
|
+
};
|
|
4513
|
+
}
|
|
4514
|
+
const timeout = args.timeout || 5e3;
|
|
4515
|
+
const condition = args.value;
|
|
4516
|
+
if (condition === "idle") {
|
|
4517
|
+
await page.waitForLoadState("domcontentloaded", { timeout });
|
|
4518
|
+
} else {
|
|
4519
|
+
await page.waitForSelector(condition, { timeout, state: "attached" });
|
|
4520
|
+
}
|
|
4521
|
+
break;
|
|
4522
|
+
}
|
|
4523
|
+
case "go_back": {
|
|
4524
|
+
await page.goBack({ timeout: args.timeout || 5e3 });
|
|
4525
|
+
break;
|
|
4526
|
+
}
|
|
4527
|
+
case "go_forward": {
|
|
4528
|
+
await page.goForward({ timeout: args.timeout || 5e3 });
|
|
4529
|
+
break;
|
|
4530
|
+
}
|
|
4531
|
+
case "refresh": {
|
|
4532
|
+
await page.reload({ timeout: args.timeout || 5e3 });
|
|
4533
|
+
break;
|
|
4534
|
+
}
|
|
4535
|
+
case "check":
|
|
4536
|
+
case "uncheck": {
|
|
4537
|
+
const shouldBeChecked = args.action === "check";
|
|
4538
|
+
if (shouldBeChecked) {
|
|
4539
|
+
await target.locator(selector).check({ timeout: 1e3 });
|
|
4540
|
+
} else {
|
|
4541
|
+
await target.locator(selector).uncheck({ timeout: 1e3 });
|
|
4542
|
+
}
|
|
4543
|
+
break;
|
|
4544
|
+
}
|
|
4545
|
+
default:
|
|
4413
4546
|
return {
|
|
4414
4547
|
status: "error",
|
|
4415
|
-
error:
|
|
4548
|
+
error: `Action ${args.action} is not yet implemented`
|
|
4416
4549
|
};
|
|
4417
|
-
}
|
|
4418
|
-
const timeout = args.timeout || 5e3;
|
|
4419
|
-
const condition = args.value;
|
|
4420
|
-
if (condition === "idle") {
|
|
4421
|
-
await page.waitForLoadState("domcontentloaded", { timeout });
|
|
4422
|
-
} else {
|
|
4423
|
-
await page.waitForSelector(condition, { timeout, state: "attached" });
|
|
4424
|
-
}
|
|
4425
|
-
break;
|
|
4426
|
-
}
|
|
4427
|
-
case "go_back": {
|
|
4428
|
-
await page.goBack({ timeout: args.timeout || 5e3 });
|
|
4429
|
-
break;
|
|
4430
4550
|
}
|
|
4431
|
-
|
|
4432
|
-
|
|
4433
|
-
|
|
4434
|
-
|
|
4435
|
-
|
|
4436
|
-
|
|
4437
|
-
break;
|
|
4551
|
+
} catch (error) {
|
|
4552
|
+
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
4553
|
+
if (errorMsg.includes("Execution context was destroyed") || errorMsg.includes("navigation")) {
|
|
4554
|
+
actionError = error instanceof Error ? error : new Error(errorMsg);
|
|
4555
|
+
} else {
|
|
4556
|
+
throw error;
|
|
4438
4557
|
}
|
|
4439
|
-
default:
|
|
4440
|
-
return {
|
|
4441
|
-
status: "error",
|
|
4442
|
-
error: `Action ${args.action} is not yet implemented`
|
|
4443
|
-
};
|
|
4444
4558
|
}
|
|
4445
4559
|
const result = {
|
|
4446
4560
|
status: "success",
|
|
@@ -4451,17 +4565,53 @@ async function handleExecuteAction(args, browser, session, sessionId) {
|
|
|
4451
4565
|
}
|
|
4452
4566
|
};
|
|
4453
4567
|
if (args.return_state !== false) {
|
|
4454
|
-
|
|
4455
|
-
|
|
4456
|
-
|
|
4457
|
-
|
|
4458
|
-
|
|
4459
|
-
|
|
4460
|
-
|
|
4461
|
-
|
|
4462
|
-
|
|
4463
|
-
|
|
4464
|
-
|
|
4568
|
+
try {
|
|
4569
|
+
const urlAfterAction = page.url();
|
|
4570
|
+
const navigationOccurred = urlAfterAction && urlAfterAction !== urlBeforeAction;
|
|
4571
|
+
const contextDestroyed = actionError && actionError.message.includes("Execution context was destroyed");
|
|
4572
|
+
if (navigationOccurred || contextDestroyed) {
|
|
4573
|
+
try {
|
|
4574
|
+
await page.waitForLoadState("domcontentloaded", { timeout: 5e3 });
|
|
4575
|
+
await page.waitForTimeout(200);
|
|
4576
|
+
} catch (waitError) {
|
|
4577
|
+
console.warn("[vsl_execute_action] Navigation detected but waitForLoadState failed:", waitError);
|
|
4578
|
+
}
|
|
4579
|
+
} else {
|
|
4580
|
+
try {
|
|
4581
|
+
await browser.evaluate(() => {
|
|
4582
|
+
return new Promise((resolve) => {
|
|
4583
|
+
setTimeout(resolve, 100);
|
|
4584
|
+
});
|
|
4585
|
+
}, sessionId);
|
|
4586
|
+
} catch (evalError) {
|
|
4587
|
+
console.warn("[vsl_execute_action] DOM stabilization evaluate failed:", evalError);
|
|
4588
|
+
}
|
|
4589
|
+
}
|
|
4590
|
+
const currentUrl = urlAfterAction || urlBeforeAction;
|
|
4591
|
+
if ((navigationOccurred || contextDestroyed) && result.data) {
|
|
4592
|
+
result.data.navigated = true;
|
|
4593
|
+
result.data.newUrl = urlAfterAction;
|
|
4594
|
+
}
|
|
4595
|
+
try {
|
|
4596
|
+
const state = await getStateAfterAction(session, browser, currentUrl, sessionId);
|
|
4597
|
+
if (result.data) {
|
|
4598
|
+
result.data.state = state;
|
|
4599
|
+
if (state.error) {
|
|
4600
|
+
result.warning = `State extraction failed: ${state.error}`;
|
|
4601
|
+
}
|
|
4602
|
+
}
|
|
4603
|
+
} catch (stateError) {
|
|
4604
|
+
result.warning = `State extraction failed: ${stateError instanceof Error ? stateError.message : String(stateError)}`;
|
|
4605
|
+
}
|
|
4606
|
+
} catch (stateError) {
|
|
4607
|
+
result.warning = `State extraction failed: ${stateError instanceof Error ? stateError.message : String(stateError)}`;
|
|
4608
|
+
try {
|
|
4609
|
+
const urlAfterAction = page.url();
|
|
4610
|
+
if (urlAfterAction && urlAfterAction !== urlBeforeAction && result.data) {
|
|
4611
|
+
result.data.navigated = true;
|
|
4612
|
+
result.data.newUrl = urlAfterAction;
|
|
4613
|
+
}
|
|
4614
|
+
} catch {
|
|
4465
4615
|
}
|
|
4466
4616
|
}
|
|
4467
4617
|
}
|
|
@@ -6436,7 +6586,7 @@ var SessionManager = class {
|
|
|
6436
6586
|
};
|
|
6437
6587
|
|
|
6438
6588
|
// src/version.ts
|
|
6439
|
-
var VSL_MCP_SERVER_VERSION = "1.3.
|
|
6589
|
+
var VSL_MCP_SERVER_VERSION = "1.3.4";
|
|
6440
6590
|
|
|
6441
6591
|
// src/index.ts
|
|
6442
6592
|
async function main() {
|