@wordbricks/playwright-mcp 0.1.20 → 0.1.22
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/cli-wrapper.js +15 -14
- package/cli.js +1 -1
- package/config.d.ts +11 -6
- package/index.d.ts +7 -5
- package/index.js +1 -1
- package/lib/browserContextFactory.js +131 -58
- package/lib/browserServerBackend.js +14 -12
- package/lib/config.js +60 -46
- package/lib/context.js +41 -39
- package/lib/extension/cdpRelay.js +67 -61
- package/lib/extension/extensionContextFactory.js +10 -10
- package/lib/frameworkPatterns.js +21 -21
- package/lib/hooks/antiBotDetectionHook.js +59 -52
- package/lib/hooks/core.js +11 -10
- package/lib/hooks/eventConsumer.js +21 -21
- package/lib/hooks/events.js +3 -3
- package/lib/hooks/formatToolCallEvent.js +3 -7
- package/lib/hooks/frameworkStateHook.js +40 -40
- package/lib/hooks/grouping.js +3 -3
- package/lib/hooks/jsonLdDetectionHook.js +44 -37
- package/lib/hooks/networkFilters.js +17 -17
- package/lib/hooks/networkSetup.js +9 -7
- package/lib/hooks/networkTrackingHook.js +21 -21
- package/lib/hooks/pageHeightHook.js +9 -9
- package/lib/hooks/registry.js +15 -16
- package/lib/hooks/requireTabHook.js +3 -3
- package/lib/hooks/schema.js +38 -38
- package/lib/hooks/waitHook.js +7 -7
- package/lib/index.js +12 -10
- package/lib/mcp/inProcessTransport.js +3 -4
- package/lib/mcp/proxyBackend.js +43 -28
- package/lib/mcp/server.js +24 -19
- package/lib/mcp/tool.js +14 -8
- package/lib/mcp/transport.js +60 -53
- package/lib/playwrightTransformer.js +129 -106
- package/lib/program.js +54 -52
- package/lib/response.js +36 -30
- package/lib/sessionLog.js +19 -17
- package/lib/tab.js +41 -39
- package/lib/tools/common.js +19 -19
- package/lib/tools/console.js +11 -11
- package/lib/tools/dialogs.js +18 -15
- package/lib/tools/evaluate.js +26 -17
- package/lib/tools/extractFrameworkState.js +48 -37
- package/lib/tools/files.js +17 -14
- package/lib/tools/form.js +32 -23
- package/lib/tools/getSnapshot.js +14 -15
- package/lib/tools/getVisibleHtml.js +33 -17
- package/lib/tools/install.js +20 -20
- package/lib/tools/keyboard.js +29 -24
- package/lib/tools/mouse.js +29 -31
- package/lib/tools/navigate.js +19 -23
- package/lib/tools/network.js +12 -14
- package/lib/tools/networkDetail.js +58 -49
- package/lib/tools/networkSearch/bodySearch.js +46 -32
- package/lib/tools/networkSearch/grouping.js +15 -6
- package/lib/tools/networkSearch/helpers.js +4 -4
- package/lib/tools/networkSearch/searchHtml.js +25 -16
- package/lib/tools/networkSearch/urlSearch.js +56 -14
- package/lib/tools/networkSearch.js +46 -36
- package/lib/tools/pdf.js +13 -12
- package/lib/tools/repl.js +66 -54
- package/lib/tools/screenshot.js +57 -33
- package/lib/tools/scroll.js +29 -24
- package/lib/tools/snapshot.js +66 -49
- package/lib/tools/tabs.js +22 -19
- package/lib/tools/tool.js +5 -3
- package/lib/tools/utils.js +17 -13
- package/lib/tools/wait.js +24 -19
- package/lib/tools.js +21 -20
- package/lib/utils/adBlockFilter.js +29 -26
- package/lib/utils/codegen.js +20 -16
- package/lib/utils/extensionPath.js +4 -4
- package/lib/utils/fileUtils.js +17 -13
- package/lib/utils/graphql.js +69 -58
- package/lib/utils/guid.js +3 -3
- package/lib/utils/httpServer.js +9 -9
- package/lib/utils/log.js +3 -3
- package/lib/utils/manualPromise.js +7 -7
- package/lib/utils/networkFormat.js +7 -5
- package/lib/utils/package.js +4 -4
- package/lib/utils/sanitizeHtml.js +66 -34
- package/lib/utils/truncate.js +25 -25
- package/lib/utils/withTimeout.js +1 -1
- package/package.json +34 -57
- package/src/index.ts +27 -17
- package/LICENSE +0 -202
package/lib/response.js
CHANGED
|
@@ -13,8 +13,8 @@
|
|
|
13
13
|
* See the License for the specific language governing permissions and
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
|
-
import { renderModalStates } from
|
|
17
|
-
import {
|
|
16
|
+
import { renderModalStates } from "./tab.js";
|
|
17
|
+
import { formatTruncationLine, truncateStringTo } from "./utils/truncate.js";
|
|
18
18
|
const MAX_RESULT_CHARS = 25000;
|
|
19
19
|
export class Response {
|
|
20
20
|
_result = [];
|
|
@@ -48,13 +48,13 @@ export class Response {
|
|
|
48
48
|
return this._isError;
|
|
49
49
|
}
|
|
50
50
|
result() {
|
|
51
|
-
return this._result.join(
|
|
51
|
+
return this._result.join("\n");
|
|
52
52
|
}
|
|
53
53
|
addCode(code) {
|
|
54
54
|
this._code.push(code);
|
|
55
55
|
}
|
|
56
56
|
code() {
|
|
57
|
-
return this._code.join(
|
|
57
|
+
return this._code.join("\n");
|
|
58
58
|
}
|
|
59
59
|
addImage(image) {
|
|
60
60
|
this._images.push(image);
|
|
@@ -73,7 +73,9 @@ export class Response {
|
|
|
73
73
|
// All the async snapshotting post-action is happening here.
|
|
74
74
|
// Everything below should race against modal states.
|
|
75
75
|
if (this._includeSnapshot && this._context.currentTab())
|
|
76
|
-
this._tabSnapshot = await this._context
|
|
76
|
+
this._tabSnapshot = await this._context
|
|
77
|
+
.currentTabOrDie()
|
|
78
|
+
.captureSnapshot();
|
|
77
79
|
for (const tab of this._context.tabs())
|
|
78
80
|
await tab.updateTitle();
|
|
79
81
|
}
|
|
@@ -84,30 +86,30 @@ export class Response {
|
|
|
84
86
|
const response = [];
|
|
85
87
|
// Start with command result.
|
|
86
88
|
if (this._result.length) {
|
|
87
|
-
const resultText = this._result.join(
|
|
89
|
+
const resultText = this._result.join("\n");
|
|
88
90
|
const { text: clipped, truncated } = truncateStringTo(resultText, MAX_RESULT_CHARS);
|
|
89
|
-
response.push(
|
|
91
|
+
response.push("### Result");
|
|
90
92
|
response.push(clipped);
|
|
91
93
|
if (truncated) {
|
|
92
94
|
const total = resultText.length;
|
|
93
95
|
const shown = clipped.length;
|
|
94
|
-
response.push(formatTruncationLine(shown, total, { units:
|
|
96
|
+
response.push(formatTruncationLine(shown, total, { units: "chars" }));
|
|
95
97
|
}
|
|
96
|
-
response.push(
|
|
98
|
+
response.push("");
|
|
97
99
|
}
|
|
98
100
|
// Then show events
|
|
99
101
|
if (this._events.length) {
|
|
100
|
-
response.push(
|
|
101
|
-
response.push(this._events.join(
|
|
102
|
-
response.push(
|
|
102
|
+
response.push("### Events");
|
|
103
|
+
response.push(this._events.join("\n"));
|
|
104
|
+
response.push("");
|
|
103
105
|
}
|
|
104
106
|
// Add code if it exists.
|
|
105
107
|
if (this._includeCode && this._code.length) {
|
|
106
108
|
response.push(`### Ran Playwright code
|
|
107
109
|
\`\`\`js
|
|
108
|
-
${this._code.join(
|
|
110
|
+
${this._code.join("\n")}
|
|
109
111
|
\`\`\``);
|
|
110
|
-
response.push(
|
|
112
|
+
response.push("");
|
|
111
113
|
}
|
|
112
114
|
// List browser tabs.
|
|
113
115
|
if (this._includeSnapshot || this._includeTabs)
|
|
@@ -115,20 +117,24 @@ ${this._code.join('\n')}
|
|
|
115
117
|
// Add snapshot if provided.
|
|
116
118
|
if (this._tabSnapshot?.modalStates.length) {
|
|
117
119
|
response.push(...renderModalStates(this._context, this._tabSnapshot.modalStates));
|
|
118
|
-
response.push(
|
|
120
|
+
response.push("");
|
|
119
121
|
}
|
|
120
122
|
else if (this._tabSnapshot) {
|
|
121
123
|
response.push(renderTabSnapshot(this._tabSnapshot));
|
|
122
|
-
response.push(
|
|
124
|
+
response.push("");
|
|
123
125
|
}
|
|
124
126
|
// Main response part
|
|
125
127
|
const content = [
|
|
126
|
-
{ type:
|
|
128
|
+
{ type: "text", text: response.join("\n") },
|
|
127
129
|
];
|
|
128
130
|
// Image attachments.
|
|
129
|
-
if (this._context.config.imageResponses !==
|
|
131
|
+
if (this._context.config.imageResponses !== "omit") {
|
|
130
132
|
for (const image of this._images)
|
|
131
|
-
content.push({
|
|
133
|
+
content.push({
|
|
134
|
+
type: "image",
|
|
135
|
+
data: image.data.toString("base64"),
|
|
136
|
+
mimeType: image.contentType,
|
|
137
|
+
});
|
|
132
138
|
}
|
|
133
139
|
return { content, isError: this._isError };
|
|
134
140
|
}
|
|
@@ -139,7 +145,7 @@ function renderTabSnapshot(tabSnapshot) {
|
|
|
139
145
|
lines.push(`### New console messages`);
|
|
140
146
|
for (const message of tabSnapshot.consoleMessages)
|
|
141
147
|
lines.push(`- ${trim(message.toString(), 100)}`);
|
|
142
|
-
lines.push(
|
|
148
|
+
lines.push("");
|
|
143
149
|
}
|
|
144
150
|
if (tabSnapshot.downloads.length) {
|
|
145
151
|
lines.push(`### Downloads`);
|
|
@@ -149,38 +155,38 @@ function renderTabSnapshot(tabSnapshot) {
|
|
|
149
155
|
else
|
|
150
156
|
lines.push(`- Downloading file ${entry.download.suggestedFilename()} ...`);
|
|
151
157
|
}
|
|
152
|
-
lines.push(
|
|
158
|
+
lines.push("");
|
|
153
159
|
}
|
|
154
160
|
lines.push(`### Page state`);
|
|
155
161
|
lines.push(`- Page URL: ${tabSnapshot.url}`);
|
|
156
162
|
lines.push(`- Page Title: ${tabSnapshot.title}`);
|
|
157
163
|
lines.push(`- Page Snapshot:`);
|
|
158
|
-
lines.push(
|
|
164
|
+
lines.push("```yaml");
|
|
159
165
|
lines.push(tabSnapshot.ariaSnapshot);
|
|
160
|
-
lines.push(
|
|
161
|
-
return lines.join(
|
|
166
|
+
lines.push("```");
|
|
167
|
+
return lines.join("\n");
|
|
162
168
|
}
|
|
163
169
|
function renderTabsMarkdown(tabs, force = false) {
|
|
164
170
|
if (tabs.length === 1 && !force)
|
|
165
171
|
return [];
|
|
166
172
|
if (!tabs.length) {
|
|
167
173
|
return [
|
|
168
|
-
|
|
174
|
+
"### Open tabs",
|
|
169
175
|
'No open tabs. Use the "browser_navigate" tool to navigate to a page first.',
|
|
170
|
-
|
|
176
|
+
"",
|
|
171
177
|
];
|
|
172
178
|
}
|
|
173
|
-
const lines = [
|
|
179
|
+
const lines = ["### Open tabs"];
|
|
174
180
|
for (let i = 0; i < tabs.length; i++) {
|
|
175
181
|
const tab = tabs[i];
|
|
176
|
-
const current = tab.isCurrentTab() ?
|
|
182
|
+
const current = tab.isCurrentTab() ? " (current)" : "";
|
|
177
183
|
lines.push(`- ${i}:${current} [${tab.lastTitle()}] (${tab.page.url()})`);
|
|
178
184
|
}
|
|
179
|
-
lines.push(
|
|
185
|
+
lines.push("");
|
|
180
186
|
return lines;
|
|
181
187
|
}
|
|
182
188
|
function trim(text, maxLength) {
|
|
183
189
|
if (text.length <= maxLength)
|
|
184
190
|
return text;
|
|
185
|
-
return text.slice(0, maxLength) +
|
|
191
|
+
return text.slice(0, maxLength) + "...";
|
|
186
192
|
}
|
package/lib/sessionLog.js
CHANGED
|
@@ -13,10 +13,10 @@
|
|
|
13
13
|
* See the License for the specific language governing permissions and
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
|
-
import fs from
|
|
17
|
-
import path from
|
|
18
|
-
import {
|
|
19
|
-
import {
|
|
16
|
+
import fs from "fs";
|
|
17
|
+
import path from "path";
|
|
18
|
+
import { outputFile } from "./config.js";
|
|
19
|
+
import { logUnhandledError } from "./utils/log.js";
|
|
20
20
|
export class SessionLog {
|
|
21
21
|
_folder;
|
|
22
22
|
_file;
|
|
@@ -26,7 +26,7 @@ export class SessionLog {
|
|
|
26
26
|
_flushEntriesTimeout;
|
|
27
27
|
constructor(sessionFolder) {
|
|
28
28
|
this._folder = sessionFolder;
|
|
29
|
-
this._file = path.join(this._folder,
|
|
29
|
+
this._file = path.join(this._folder, "session.md");
|
|
30
30
|
}
|
|
31
31
|
static async create(config, rootPath) {
|
|
32
32
|
const sessionFolder = await outputFile(config, rootPath, `session-${Date.now()}`);
|
|
@@ -59,7 +59,7 @@ export class SessionLog {
|
|
|
59
59
|
return;
|
|
60
60
|
}
|
|
61
61
|
}
|
|
62
|
-
if (action.name ===
|
|
62
|
+
if (action.name === "navigate") {
|
|
63
63
|
// Already logged at this location.
|
|
64
64
|
const lastEntry = this._pendingEntries[this._pendingEntries.length - 1];
|
|
65
65
|
if (lastEntry?.tabSnapshot?.url === action.url)
|
|
@@ -71,8 +71,8 @@ export class SessionLog {
|
|
|
71
71
|
code,
|
|
72
72
|
tabSnapshot: {
|
|
73
73
|
url: tab.page.url(),
|
|
74
|
-
title:
|
|
75
|
-
ariaSnapshot: action.ariaSnapshot ||
|
|
74
|
+
title: "",
|
|
75
|
+
ariaSnapshot: action.ariaSnapshot || "",
|
|
76
76
|
modalStates: [],
|
|
77
77
|
consoleMessages: [],
|
|
78
78
|
downloads: [],
|
|
@@ -90,13 +90,13 @@ export class SessionLog {
|
|
|
90
90
|
clearTimeout(this._flushEntriesTimeout);
|
|
91
91
|
const entries = this._pendingEntries;
|
|
92
92
|
this._pendingEntries = [];
|
|
93
|
-
const lines = [
|
|
93
|
+
const lines = [""];
|
|
94
94
|
for (const entry of entries) {
|
|
95
|
-
const ordinal = (++this._ordinal).toString().padStart(3,
|
|
95
|
+
const ordinal = (++this._ordinal).toString().padStart(3, "0");
|
|
96
96
|
if (entry.toolCall) {
|
|
97
|
-
lines.push(`### Tool call: ${entry.toolCall.toolName}`, `- Args`,
|
|
97
|
+
lines.push(`### Tool call: ${entry.toolCall.toolName}`, `- Args`, "```json", JSON.stringify(entry.toolCall.toolArgs, null, 2), "```");
|
|
98
98
|
if (entry.toolCall.result) {
|
|
99
|
-
lines.push(entry.toolCall.isError ? `- Error` : `- Result`,
|
|
99
|
+
lines.push(entry.toolCall.isError ? `- Error` : `- Result`, "```", entry.toolCall.result, "```");
|
|
100
100
|
}
|
|
101
101
|
}
|
|
102
102
|
if (entry.userAction) {
|
|
@@ -104,18 +104,20 @@ export class SessionLog {
|
|
|
104
104
|
delete actionData.ariaSnapshot;
|
|
105
105
|
delete actionData.selector;
|
|
106
106
|
delete actionData.signals;
|
|
107
|
-
lines.push(`### User action: ${entry.userAction.name}`, `- Args`,
|
|
107
|
+
lines.push(`### User action: ${entry.userAction.name}`, `- Args`, "```json", JSON.stringify(actionData, null, 2), "```");
|
|
108
108
|
}
|
|
109
109
|
if (entry.code) {
|
|
110
|
-
lines.push(`- Code`,
|
|
110
|
+
lines.push(`- Code`, "```js", entry.code, "```");
|
|
111
111
|
}
|
|
112
112
|
if (entry.tabSnapshot) {
|
|
113
113
|
const fileName = `${ordinal}.snapshot.yml`;
|
|
114
|
-
fs.promises
|
|
114
|
+
fs.promises
|
|
115
|
+
.writeFile(path.join(this._folder, fileName), entry.tabSnapshot.ariaSnapshot)
|
|
116
|
+
.catch(logUnhandledError);
|
|
115
117
|
lines.push(`- Snapshot: ${fileName}`);
|
|
116
118
|
}
|
|
117
|
-
lines.push(
|
|
119
|
+
lines.push("", "");
|
|
118
120
|
}
|
|
119
|
-
this._sessionFileQueue = this._sessionFileQueue.then(() => fs.promises.appendFile(this._file, lines.join(
|
|
121
|
+
this._sessionFileQueue = this._sessionFileQueue.then(() => fs.promises.appendFile(this._file, lines.join("\n")));
|
|
120
122
|
}
|
|
121
123
|
}
|
package/lib/tab.js
CHANGED
|
@@ -13,17 +13,17 @@
|
|
|
13
13
|
* See the License for the specific language governing permissions and
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
|
-
import { EventEmitter } from
|
|
17
|
-
import { callOnPageNoTrace, waitForCompletion } from
|
|
18
|
-
import { logUnhandledError } from
|
|
19
|
-
import { ManualPromise } from
|
|
16
|
+
import { EventEmitter } from "events";
|
|
17
|
+
import { callOnPageNoTrace, waitForCompletion } from "./tools/utils.js";
|
|
18
|
+
import { logUnhandledError } from "./utils/log.js";
|
|
19
|
+
import { ManualPromise } from "./utils/manualPromise.js";
|
|
20
20
|
export const TabEvents = {
|
|
21
|
-
modalState:
|
|
21
|
+
modalState: "modalState",
|
|
22
22
|
};
|
|
23
23
|
export class Tab extends EventEmitter {
|
|
24
24
|
context;
|
|
25
25
|
page;
|
|
26
|
-
_lastTitle =
|
|
26
|
+
_lastTitle = "about:blank";
|
|
27
27
|
_consoleMessages = [];
|
|
28
28
|
_recentConsoleMessages = [];
|
|
29
29
|
_requests = new Map();
|
|
@@ -35,19 +35,19 @@ export class Tab extends EventEmitter {
|
|
|
35
35
|
this.context = context;
|
|
36
36
|
this.page = page;
|
|
37
37
|
this._onPageClose = onPageClose;
|
|
38
|
-
page.on(
|
|
39
|
-
page.on(
|
|
40
|
-
page.on(
|
|
41
|
-
page.on(
|
|
42
|
-
page.on(
|
|
43
|
-
page.on(
|
|
38
|
+
page.on("console", (event) => this._handleConsoleMessage(messageToConsoleMessage(event)));
|
|
39
|
+
page.on("pageerror", (error) => this._handleConsoleMessage(pageErrorToConsoleMessage(error)));
|
|
40
|
+
page.on("request", (request) => this._requests.set(request, null));
|
|
41
|
+
page.on("response", (response) => this._requests.set(response.request(), response));
|
|
42
|
+
page.on("close", () => this._onClose());
|
|
43
|
+
page.on("filechooser", (chooser) => {
|
|
44
44
|
this.setModalState({
|
|
45
|
-
type:
|
|
46
|
-
description:
|
|
45
|
+
type: "fileChooser",
|
|
46
|
+
description: "File chooser",
|
|
47
47
|
fileChooser: chooser,
|
|
48
48
|
});
|
|
49
49
|
});
|
|
50
|
-
page.on(
|
|
50
|
+
page.on("dialog", (dialog) => this._dialogShown(dialog));
|
|
51
51
|
// page.on('download', download => {
|
|
52
52
|
// void this._downloadStarted(download);
|
|
53
53
|
// });
|
|
@@ -66,14 +66,14 @@ export class Tab extends EventEmitter {
|
|
|
66
66
|
this.emit(TabEvents.modalState, modalState);
|
|
67
67
|
}
|
|
68
68
|
clearModalState(modalState) {
|
|
69
|
-
this._modalStates = this._modalStates.filter(state => state !== modalState);
|
|
69
|
+
this._modalStates = this._modalStates.filter((state) => state !== modalState);
|
|
70
70
|
}
|
|
71
71
|
modalStatesMarkdown() {
|
|
72
72
|
return renderModalStates(this.context, this.modalStates());
|
|
73
73
|
}
|
|
74
74
|
_dialogShown(dialog) {
|
|
75
75
|
this.setModalState({
|
|
76
|
-
type:
|
|
76
|
+
type: "dialog",
|
|
77
77
|
description: `"${dialog.type()}" dialog with message "${dialog.message()}"`,
|
|
78
78
|
dialog,
|
|
79
79
|
});
|
|
@@ -82,7 +82,7 @@ export class Tab extends EventEmitter {
|
|
|
82
82
|
const entry = {
|
|
83
83
|
download,
|
|
84
84
|
finished: false,
|
|
85
|
-
outputFile: await this.context.outputFile(download.suggestedFilename())
|
|
85
|
+
outputFile: await this.context.outputFile(download.suggestedFilename()),
|
|
86
86
|
};
|
|
87
87
|
this._downloads.push(entry);
|
|
88
88
|
await download.saveAs(entry.outputFile);
|
|
@@ -103,7 +103,7 @@ export class Tab extends EventEmitter {
|
|
|
103
103
|
}
|
|
104
104
|
async updateTitle() {
|
|
105
105
|
await this._raceAgainstModalStates(async () => {
|
|
106
|
-
this._lastTitle = await callOnPageNoTrace(this.page, page => page.title());
|
|
106
|
+
this._lastTitle = await callOnPageNoTrace(this.page, (page) => page.title());
|
|
107
107
|
});
|
|
108
108
|
}
|
|
109
109
|
lastTitle() {
|
|
@@ -113,34 +113,34 @@ export class Tab extends EventEmitter {
|
|
|
113
113
|
return this === this.context.currentTab();
|
|
114
114
|
}
|
|
115
115
|
async waitForLoadState(state, options) {
|
|
116
|
-
await callOnPageNoTrace(this.page, page => page.waitForLoadState(state, options).catch(logUnhandledError));
|
|
116
|
+
await callOnPageNoTrace(this.page, (page) => page.waitForLoadState(state, options).catch(logUnhandledError));
|
|
117
117
|
}
|
|
118
118
|
async navigate(url) {
|
|
119
119
|
this._clearCollectedArtifacts();
|
|
120
120
|
// const downloadEvent = callOnPageNoTrace(this.page, page => page.waitForEvent('download').catch(logUnhandledError));
|
|
121
121
|
const downloadEvent = Promise.resolve(undefined);
|
|
122
122
|
try {
|
|
123
|
-
await this.page.goto(url, { waitUntil:
|
|
123
|
+
await this.page.goto(url, { waitUntil: "domcontentloaded" });
|
|
124
124
|
}
|
|
125
125
|
catch (_e) {
|
|
126
126
|
const e = _e;
|
|
127
|
-
const mightBeDownload = e.message.includes(
|
|
128
|
-
|
|
127
|
+
const mightBeDownload = e.message.includes("net::ERR_ABORTED") || // chromium
|
|
128
|
+
e.message.includes("Download is starting"); // firefox + webkit
|
|
129
129
|
if (!mightBeDownload)
|
|
130
130
|
throw e;
|
|
131
131
|
// on chromium, the download event is fired *after* page.goto rejects, so we wait a lil bit
|
|
132
132
|
const download = await Promise.race([
|
|
133
133
|
downloadEvent,
|
|
134
|
-
new Promise(resolve => setTimeout(resolve, 3000)),
|
|
134
|
+
new Promise((resolve) => setTimeout(resolve, 3000)),
|
|
135
135
|
]);
|
|
136
136
|
if (!download)
|
|
137
137
|
throw e;
|
|
138
138
|
// Make sure other "download" listeners are notified first.
|
|
139
|
-
await new Promise(resolve => setTimeout(resolve, 500));
|
|
139
|
+
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
140
140
|
return;
|
|
141
141
|
}
|
|
142
142
|
// Cap load event to 5 seconds, the page is operational at this point.
|
|
143
|
-
await this.waitForLoadState(
|
|
143
|
+
await this.waitForLoadState("load", { timeout: 5000 });
|
|
144
144
|
}
|
|
145
145
|
consoleMessages() {
|
|
146
146
|
return this._consoleMessages;
|
|
@@ -166,17 +166,17 @@ export class Tab extends EventEmitter {
|
|
|
166
166
|
tabSnapshot.consoleMessages = this._recentConsoleMessages;
|
|
167
167
|
this._recentConsoleMessages = [];
|
|
168
168
|
}
|
|
169
|
-
return tabSnapshot ?? {
|
|
169
|
+
return (tabSnapshot ?? {
|
|
170
170
|
url: this.page.url(),
|
|
171
|
-
title:
|
|
172
|
-
ariaSnapshot:
|
|
171
|
+
title: "",
|
|
172
|
+
ariaSnapshot: "",
|
|
173
173
|
modalStates,
|
|
174
174
|
consoleMessages: [],
|
|
175
175
|
downloads: [],
|
|
176
|
-
};
|
|
176
|
+
});
|
|
177
177
|
}
|
|
178
178
|
_javaScriptBlocked() {
|
|
179
|
-
return this._modalStates.some(state => state.type ===
|
|
179
|
+
return this._modalStates.some((state) => state.type === "dialog");
|
|
180
180
|
}
|
|
181
181
|
async _raceAgainstModalStates(action) {
|
|
182
182
|
if (this.modalStates().length)
|
|
@@ -203,15 +203,15 @@ export class Tab extends EventEmitter {
|
|
|
203
203
|
// This avoids invalidating refs obtained from browser_get_snapshot while still
|
|
204
204
|
// initializing the mapping when needed (e.g., after navigation).
|
|
205
205
|
await this.page._snapshotForAI();
|
|
206
|
-
return params.map(param => this.page.locator(`aria-ref=${param.ref}`));
|
|
206
|
+
return params.map((param) => this.page.locator(`aria-ref=${param.ref}`));
|
|
207
207
|
}
|
|
208
208
|
async waitForTimeout(time) {
|
|
209
209
|
if (this._javaScriptBlocked()) {
|
|
210
|
-
await new Promise(f => setTimeout(f, time));
|
|
210
|
+
await new Promise((f) => setTimeout(f, time));
|
|
211
211
|
return;
|
|
212
212
|
}
|
|
213
|
-
await callOnPageNoTrace(this.page, page => {
|
|
214
|
-
return page.evaluate(() => new Promise(f => setTimeout(f, 1000)));
|
|
213
|
+
await callOnPageNoTrace(this.page, (page) => {
|
|
214
|
+
return page.evaluate(() => new Promise((f) => setTimeout(f, 1000)));
|
|
215
215
|
});
|
|
216
216
|
}
|
|
217
217
|
}
|
|
@@ -237,13 +237,15 @@ function pageErrorToConsoleMessage(errorOrValue) {
|
|
|
237
237
|
};
|
|
238
238
|
}
|
|
239
239
|
export function renderModalStates(context, modalStates) {
|
|
240
|
-
const result = [
|
|
240
|
+
const result = ["### Modal state"];
|
|
241
241
|
if (modalStates.length === 0)
|
|
242
|
-
result.push(
|
|
242
|
+
result.push("- There is no modal state present");
|
|
243
243
|
for (const state of modalStates) {
|
|
244
|
-
const tool = context.tools
|
|
244
|
+
const tool = context.tools
|
|
245
|
+
.filter((tool) => "clearsModalState" in tool)
|
|
246
|
+
.find((tool) => tool.clearsModalState === state.type);
|
|
245
247
|
result.push(`- [${state.description}]: can be handled by the "${tool?.schema.name}" tool`);
|
|
246
248
|
}
|
|
247
249
|
return result;
|
|
248
250
|
}
|
|
249
|
-
const tabSymbol = Symbol(
|
|
251
|
+
const tabSymbol = Symbol("tabSymbol");
|
package/lib/tools/common.js
CHANGED
|
@@ -13,16 +13,16 @@
|
|
|
13
13
|
* See the License for the specific language governing permissions and
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
|
-
import { z } from
|
|
17
|
-
import { defineTabTool, defineTool } from
|
|
16
|
+
import { z } from "zod";
|
|
17
|
+
import { defineTabTool, defineTool } from "./tool.js";
|
|
18
18
|
const close = defineTool({
|
|
19
|
-
capability:
|
|
19
|
+
capability: "core",
|
|
20
20
|
schema: {
|
|
21
|
-
name:
|
|
22
|
-
title:
|
|
23
|
-
description:
|
|
21
|
+
name: "browser_close",
|
|
22
|
+
title: "Close browser",
|
|
23
|
+
description: "Close the page",
|
|
24
24
|
inputSchema: z.object({}),
|
|
25
|
-
type:
|
|
25
|
+
type: "readOnly",
|
|
26
26
|
},
|
|
27
27
|
handle: async (context, params, response) => {
|
|
28
28
|
await context.closeBrowserContext();
|
|
@@ -31,25 +31,25 @@ const close = defineTool({
|
|
|
31
31
|
},
|
|
32
32
|
});
|
|
33
33
|
const resize = defineTabTool({
|
|
34
|
-
capability:
|
|
34
|
+
capability: "core",
|
|
35
35
|
schema: {
|
|
36
|
-
name:
|
|
37
|
-
title:
|
|
38
|
-
description:
|
|
36
|
+
name: "browser_resize",
|
|
37
|
+
title: "Resize browser window",
|
|
38
|
+
description: "Resize the browser window",
|
|
39
39
|
inputSchema: z.object({
|
|
40
|
-
width: z.number().describe(
|
|
41
|
-
height: z.number().describe(
|
|
40
|
+
width: z.number().describe("Width of the browser window"),
|
|
41
|
+
height: z.number().describe("Height of the browser window"),
|
|
42
42
|
}),
|
|
43
|
-
type:
|
|
43
|
+
type: "readOnly",
|
|
44
44
|
},
|
|
45
45
|
handle: async (tab, params, response) => {
|
|
46
46
|
response.addCode(`await page.setViewportSize({ width: ${params.width}, height: ${params.height} });`);
|
|
47
47
|
await tab.waitForCompletion(async () => {
|
|
48
|
-
await tab.page.setViewportSize({
|
|
48
|
+
await tab.page.setViewportSize({
|
|
49
|
+
width: params.width,
|
|
50
|
+
height: params.height,
|
|
51
|
+
});
|
|
49
52
|
});
|
|
50
53
|
},
|
|
51
54
|
});
|
|
52
|
-
export default [
|
|
53
|
-
close,
|
|
54
|
-
resize
|
|
55
|
-
];
|
|
55
|
+
export default [close, resize];
|
package/lib/tools/console.js
CHANGED
|
@@ -13,21 +13,21 @@
|
|
|
13
13
|
* See the License for the specific language governing permissions and
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
|
-
import { z } from
|
|
17
|
-
import { defineTabTool } from
|
|
16
|
+
import { z } from "zod";
|
|
17
|
+
import { defineTabTool } from "./tool.js";
|
|
18
18
|
const console = defineTabTool({
|
|
19
|
-
capability:
|
|
19
|
+
capability: "core",
|
|
20
20
|
schema: {
|
|
21
|
-
name:
|
|
22
|
-
title:
|
|
23
|
-
description:
|
|
21
|
+
name: "browser_console_messages",
|
|
22
|
+
title: "Get console messages",
|
|
23
|
+
description: "Returns all console messages",
|
|
24
24
|
inputSchema: z.object({}),
|
|
25
|
-
type:
|
|
25
|
+
type: "readOnly",
|
|
26
26
|
},
|
|
27
27
|
handle: async (tab, params, response) => {
|
|
28
|
-
tab
|
|
28
|
+
tab
|
|
29
|
+
.consoleMessages()
|
|
30
|
+
.map((message) => response.addResult(message.toString()));
|
|
29
31
|
},
|
|
30
32
|
});
|
|
31
|
-
export default [
|
|
32
|
-
console,
|
|
33
|
-
];
|
|
33
|
+
export default [console];
|
package/lib/tools/dialogs.js
CHANGED
|
@@ -13,25 +13,30 @@
|
|
|
13
13
|
* See the License for the specific language governing permissions and
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
|
-
import { z } from
|
|
17
|
-
import { defineTabTool } from
|
|
16
|
+
import { z } from "zod";
|
|
17
|
+
import { defineTabTool } from "./tool.js";
|
|
18
18
|
export const handleDialog = defineTabTool({
|
|
19
|
-
capability:
|
|
19
|
+
capability: "core",
|
|
20
20
|
schema: {
|
|
21
|
-
name:
|
|
22
|
-
title:
|
|
23
|
-
description:
|
|
21
|
+
name: "browser_handle_dialog",
|
|
22
|
+
title: "Handle a dialog",
|
|
23
|
+
description: "Handle a dialog",
|
|
24
24
|
inputSchema: z.object({
|
|
25
|
-
accept: z.boolean().describe(
|
|
26
|
-
promptText: z
|
|
25
|
+
accept: z.boolean().describe("Whether to accept the dialog."),
|
|
26
|
+
promptText: z
|
|
27
|
+
.string()
|
|
28
|
+
.optional()
|
|
29
|
+
.describe("The text of the prompt in case of a prompt dialog."),
|
|
27
30
|
}),
|
|
28
|
-
type:
|
|
31
|
+
type: "action",
|
|
29
32
|
},
|
|
30
33
|
handle: async (tab, params, response) => {
|
|
31
34
|
response.setIncludeSnapshot();
|
|
32
|
-
const dialogState = tab
|
|
35
|
+
const dialogState = tab
|
|
36
|
+
.modalStates()
|
|
37
|
+
.find((state) => state.type === "dialog");
|
|
33
38
|
if (!dialogState)
|
|
34
|
-
throw new Error(
|
|
39
|
+
throw new Error("No dialog visible");
|
|
35
40
|
tab.clearModalState(dialogState);
|
|
36
41
|
await tab.waitForCompletion(async () => {
|
|
37
42
|
if (params.accept)
|
|
@@ -40,8 +45,6 @@ export const handleDialog = defineTabTool({
|
|
|
40
45
|
await dialogState.dialog.dismiss();
|
|
41
46
|
});
|
|
42
47
|
},
|
|
43
|
-
clearsModalState:
|
|
48
|
+
clearsModalState: "dialog",
|
|
44
49
|
});
|
|
45
|
-
export default [
|
|
46
|
-
handleDialog,
|
|
47
|
-
];
|
|
50
|
+
export default [handleDialog];
|