@wordbricks/playwright-mcp 0.1.20 → 0.1.23
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/package.json +34 -57
- package/LICENSE +0 -202
- package/lib/browserContextFactory.js +0 -326
- package/lib/browserServerBackend.js +0 -84
- package/lib/config.js +0 -286
- package/lib/context.js +0 -309
- package/lib/extension/cdpRelay.js +0 -346
- package/lib/extension/extensionContextFactory.js +0 -56
- package/lib/frameworkPatterns.js +0 -35
- package/lib/hooks/antiBotDetectionHook.js +0 -171
- package/lib/hooks/core.js +0 -144
- package/lib/hooks/eventConsumer.js +0 -52
- package/lib/hooks/events.js +0 -42
- package/lib/hooks/formatToolCallEvent.js +0 -16
- package/lib/hooks/frameworkStateHook.js +0 -182
- package/lib/hooks/grouping.js +0 -72
- package/lib/hooks/jsonLdDetectionHook.js +0 -175
- package/lib/hooks/networkFilters.js +0 -82
- package/lib/hooks/networkSetup.js +0 -59
- package/lib/hooks/networkTrackingHook.js +0 -67
- package/lib/hooks/pageHeightHook.js +0 -75
- package/lib/hooks/registry.js +0 -42
- package/lib/hooks/requireTabHook.js +0 -26
- package/lib/hooks/schema.js +0 -89
- package/lib/hooks/waitHook.js +0 -33
- package/lib/index.js +0 -39
- package/lib/mcp/inProcessTransport.js +0 -72
- package/lib/mcp/proxyBackend.js +0 -115
- package/lib/mcp/server.js +0 -86
- package/lib/mcp/tool.js +0 -38
- package/lib/mcp/transport.js +0 -181
- package/lib/playwrightTransformer.js +0 -497
- package/lib/program.js +0 -110
- package/lib/response.js +0 -186
- package/lib/sessionLog.js +0 -121
- package/lib/tab.js +0 -249
- package/lib/tools/common.js +0 -55
- package/lib/tools/console.js +0 -33
- package/lib/tools/dialogs.js +0 -47
- package/lib/tools/evaluate.js +0 -53
- package/lib/tools/extractFrameworkState.js +0 -214
- package/lib/tools/files.js +0 -45
- package/lib/tools/form.js +0 -57
- package/lib/tools/getSnapshot.js +0 -37
- package/lib/tools/getVisibleHtml.js +0 -52
- package/lib/tools/install.js +0 -51
- package/lib/tools/keyboard.js +0 -78
- package/lib/tools/mouse.js +0 -99
- package/lib/tools/navigate.js +0 -70
- package/lib/tools/network.js +0 -123
- package/lib/tools/networkDetail.js +0 -229
- package/lib/tools/networkSearch/bodySearch.js +0 -147
- package/lib/tools/networkSearch/grouping.js +0 -28
- package/lib/tools/networkSearch/helpers.js +0 -32
- package/lib/tools/networkSearch/searchHtml.js +0 -67
- package/lib/tools/networkSearch/types.js +0 -1
- package/lib/tools/networkSearch/urlSearch.js +0 -82
- package/lib/tools/networkSearch.js +0 -268
- package/lib/tools/pdf.js +0 -40
- package/lib/tools/repl.js +0 -402
- package/lib/tools/screenshot.js +0 -79
- package/lib/tools/scroll.js +0 -126
- package/lib/tools/snapshot.js +0 -144
- package/lib/tools/tabs.js +0 -59
- package/lib/tools/tool.js +0 -33
- package/lib/tools/utils.js +0 -74
- package/lib/tools/wait.js +0 -55
- package/lib/tools.js +0 -67
- package/lib/utils/adBlockFilter.js +0 -87
- package/lib/utils/codegen.js +0 -51
- package/lib/utils/extensionPath.js +0 -10
- package/lib/utils/fileUtils.js +0 -36
- package/lib/utils/graphql.js +0 -258
- package/lib/utils/guid.js +0 -22
- package/lib/utils/httpServer.js +0 -39
- package/lib/utils/log.js +0 -21
- package/lib/utils/manualPromise.js +0 -111
- package/lib/utils/networkFormat.js +0 -12
- package/lib/utils/package.js +0 -20
- package/lib/utils/result.js +0 -2
- package/lib/utils/sanitizeHtml.js +0 -98
- package/lib/utils/truncate.js +0 -103
- package/lib/utils/withTimeout.js +0 -7
- package/src/index.ts +0 -50
package/lib/program.js
DELETED
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) Microsoft Corporation.
|
|
3
|
-
*
|
|
4
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
-
* you may not use this file except in compliance with the License.
|
|
6
|
-
* You may obtain a copy of the License at
|
|
7
|
-
*
|
|
8
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
-
*
|
|
10
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
-
* See the License for the specific language governing permissions and
|
|
14
|
-
* limitations under the License.
|
|
15
|
-
*/
|
|
16
|
-
import { program, Option } from 'commander';
|
|
17
|
-
import * as mcpServer from './mcp/server.js';
|
|
18
|
-
import * as mcpTransport from './mcp/transport.js';
|
|
19
|
-
import { commaSeparatedList, resolveCLIConfig, semicolonSeparatedList } from './config.js';
|
|
20
|
-
import { packageJSON } from './utils/package.js';
|
|
21
|
-
import { Context } from './context.js';
|
|
22
|
-
import { contextFactory } from './browserContextFactory.js';
|
|
23
|
-
import { ProxyBackend } from './mcp/proxyBackend.js';
|
|
24
|
-
import { BrowserServerBackend } from './browserServerBackend.js';
|
|
25
|
-
import { ExtensionContextFactory } from './extension/extensionContextFactory.js';
|
|
26
|
-
import { InProcessTransport } from './mcp/inProcessTransport.js';
|
|
27
|
-
program
|
|
28
|
-
.version('Version ' + packageJSON.version)
|
|
29
|
-
.name(packageJSON.name)
|
|
30
|
-
.option('--allowed-origins <origins>', 'semicolon-separated list of origins to allow the browser to request. Default is to allow all.', semicolonSeparatedList)
|
|
31
|
-
.option('--blocked-origins <origins>', 'semicolon-separated list of origins to block the browser from requesting. Blocklist is evaluated before allowlist. If used without the allowlist, requests not matching the blocklist are still allowed.', semicolonSeparatedList)
|
|
32
|
-
.option('--block-service-workers', 'block service workers')
|
|
33
|
-
.option('--browser <browser>', 'browser or chrome channel to use, possible values: chrome, firefox, webkit, msedge.')
|
|
34
|
-
.option('--caps <caps>', 'comma-separated list of additional capabilities to enable, possible values: vision, pdf.', commaSeparatedList)
|
|
35
|
-
.option('--cdp-endpoint <endpoint>', 'CDP endpoint to connect to.')
|
|
36
|
-
.option('--config <path>', 'path to the configuration file.')
|
|
37
|
-
.option('--device <device>', 'device to emulate, for example: "iPhone 15"')
|
|
38
|
-
.option('--executable-path <path>', 'path to the browser executable.')
|
|
39
|
-
.option('--headless', 'run browser in headless mode, headed by default')
|
|
40
|
-
.option('--host <host>', 'host to bind server to. Default is localhost. Use 0.0.0.0 to bind to all interfaces.')
|
|
41
|
-
.option('--ignore-https-errors', 'ignore https errors')
|
|
42
|
-
.option('--init-script <path>', 'path to a JavaScript file to inject into all pages using addInitScript.')
|
|
43
|
-
.option('--isolated', 'keep the browser profile in memory, do not save it to disk.')
|
|
44
|
-
.option('--image-responses <mode>', 'whether to send image responses to the client. Can be "allow" or "omit", Defaults to "allow".')
|
|
45
|
-
.option('--no-sandbox', 'disable the sandbox for all process types that are normally sandboxed.')
|
|
46
|
-
.option('--output-dir <path>', 'path to the directory for output files.')
|
|
47
|
-
.option('--port <port>', 'port to listen on for SSE transport.')
|
|
48
|
-
.option('--proxy-bypass <bypass>', 'comma-separated domains to bypass proxy, for example ".com,chromium.org,.domain.com"')
|
|
49
|
-
.option('--proxy-server <proxy>', 'specify proxy server, for example "http://myproxy:3128" or "socks5://myproxy:8080"')
|
|
50
|
-
.option('--save-session', 'Whether to save the Playwright MCP session into the output directory.')
|
|
51
|
-
.option('--save-trace', 'Whether to save the Playwright Trace of the session into the output directory.')
|
|
52
|
-
.option('--storage-state <path>', 'path to the storage state file for isolated sessions.')
|
|
53
|
-
.option('--user-agent <ua string>', 'specify user agent string')
|
|
54
|
-
.option('--user-data-dir <path>', 'path to the user data directory. If not specified, a temporary directory will be created.')
|
|
55
|
-
.option('--viewport-size <size>', 'specify browser viewport size in pixels, for example "1280, 720"')
|
|
56
|
-
.option('--window-position <x,y>', 'specify Chrome window position in pixels, for example "100,200"')
|
|
57
|
-
.option('--window-size <width,height>', 'specify Chrome window size in pixels, for example "1280,720"')
|
|
58
|
-
.option('--app <url>', 'launch browser in app mode with the specified URL')
|
|
59
|
-
.addOption(new Option('--extension', 'Connect to a running browser instance (Edge/Chrome only). Requires the "Playwright MCP Bridge" browser extension to be installed.').hideHelp())
|
|
60
|
-
.addOption(new Option('--connect-tool', 'Allow to switch between different browser connection methods.').hideHelp())
|
|
61
|
-
.addOption(new Option('--loop-tools', 'Run loop tools').hideHelp())
|
|
62
|
-
.addOption(new Option('--vision', 'Legacy option, use --caps=vision instead').hideHelp())
|
|
63
|
-
.action(async (options) => {
|
|
64
|
-
setupExitWatchdog();
|
|
65
|
-
if (options.vision) {
|
|
66
|
-
// eslint-disable-next-line no-console
|
|
67
|
-
console.error('The --vision option is deprecated, use --caps=vision instead');
|
|
68
|
-
options.caps = 'vision';
|
|
69
|
-
}
|
|
70
|
-
const config = await resolveCLIConfig(options);
|
|
71
|
-
if (options.extension) {
|
|
72
|
-
const contextFactory = createExtensionContextFactory(config);
|
|
73
|
-
const serverBackendFactory = () => new BrowserServerBackend(config, contextFactory);
|
|
74
|
-
await mcpTransport.start(serverBackendFactory, config.server);
|
|
75
|
-
return;
|
|
76
|
-
}
|
|
77
|
-
const browserContextFactory = contextFactory(config);
|
|
78
|
-
const providers = [mcpProviderForBrowserContextFactory(config, browserContextFactory)];
|
|
79
|
-
if (options.connectTool)
|
|
80
|
-
providers.push(mcpProviderForBrowserContextFactory(config, createExtensionContextFactory(config)));
|
|
81
|
-
await mcpTransport.start(() => new ProxyBackend(providers), config.server);
|
|
82
|
-
});
|
|
83
|
-
function setupExitWatchdog() {
|
|
84
|
-
let isExiting = false;
|
|
85
|
-
const handleExit = async () => {
|
|
86
|
-
if (isExiting)
|
|
87
|
-
return;
|
|
88
|
-
isExiting = true;
|
|
89
|
-
setTimeout(() => process.exit(0), 15000);
|
|
90
|
-
await Context.disposeAll();
|
|
91
|
-
process.exit(0);
|
|
92
|
-
};
|
|
93
|
-
process.stdin.on('close', handleExit);
|
|
94
|
-
process.on('SIGINT', handleExit);
|
|
95
|
-
process.on('SIGTERM', handleExit);
|
|
96
|
-
}
|
|
97
|
-
function createExtensionContextFactory(config) {
|
|
98
|
-
return new ExtensionContextFactory(config.browser.launchOptions.channel || 'chrome', config.browser.userDataDir);
|
|
99
|
-
}
|
|
100
|
-
function mcpProviderForBrowserContextFactory(config, browserContextFactory) {
|
|
101
|
-
return {
|
|
102
|
-
name: browserContextFactory.name,
|
|
103
|
-
description: browserContextFactory.description,
|
|
104
|
-
connect: async () => {
|
|
105
|
-
const server = mcpServer.createServer(new BrowserServerBackend(config, browserContextFactory), false);
|
|
106
|
-
return new InProcessTransport(server);
|
|
107
|
-
},
|
|
108
|
-
};
|
|
109
|
-
}
|
|
110
|
-
void program.parseAsync(process.argv);
|
package/lib/response.js
DELETED
|
@@ -1,186 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) Microsoft Corporation.
|
|
3
|
-
*
|
|
4
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
-
* you may not use this file except in compliance with the License.
|
|
6
|
-
* You may obtain a copy of the License at
|
|
7
|
-
*
|
|
8
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
-
*
|
|
10
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
-
* See the License for the specific language governing permissions and
|
|
14
|
-
* limitations under the License.
|
|
15
|
-
*/
|
|
16
|
-
import { renderModalStates } from './tab.js';
|
|
17
|
-
import { truncateStringTo, formatTruncationLine } from './utils/truncate.js';
|
|
18
|
-
const MAX_RESULT_CHARS = 25000;
|
|
19
|
-
export class Response {
|
|
20
|
-
_result = [];
|
|
21
|
-
_events = [];
|
|
22
|
-
_code = [];
|
|
23
|
-
_images = [];
|
|
24
|
-
_context;
|
|
25
|
-
_includeSnapshot = false;
|
|
26
|
-
_includeTabs = false;
|
|
27
|
-
_includeCode = false;
|
|
28
|
-
_tabSnapshot;
|
|
29
|
-
toolName;
|
|
30
|
-
toolArgs;
|
|
31
|
-
_isError;
|
|
32
|
-
constructor(context, toolName, toolArgs) {
|
|
33
|
-
this._context = context;
|
|
34
|
-
this.toolName = toolName;
|
|
35
|
-
this.toolArgs = toolArgs;
|
|
36
|
-
}
|
|
37
|
-
addResult(result) {
|
|
38
|
-
this._result.push(result);
|
|
39
|
-
}
|
|
40
|
-
addEvent(event) {
|
|
41
|
-
this._events.push(event);
|
|
42
|
-
}
|
|
43
|
-
addError(error) {
|
|
44
|
-
this._result.push(error);
|
|
45
|
-
this._isError = true;
|
|
46
|
-
}
|
|
47
|
-
isError() {
|
|
48
|
-
return this._isError;
|
|
49
|
-
}
|
|
50
|
-
result() {
|
|
51
|
-
return this._result.join('\n');
|
|
52
|
-
}
|
|
53
|
-
addCode(code) {
|
|
54
|
-
this._code.push(code);
|
|
55
|
-
}
|
|
56
|
-
code() {
|
|
57
|
-
return this._code.join('\n');
|
|
58
|
-
}
|
|
59
|
-
addImage(image) {
|
|
60
|
-
this._images.push(image);
|
|
61
|
-
}
|
|
62
|
-
images() {
|
|
63
|
-
return this._images;
|
|
64
|
-
}
|
|
65
|
-
// NOTE Wordbricks Disabled: Page state logging not needed
|
|
66
|
-
setIncludeSnapshot(full) {
|
|
67
|
-
// this._includeSnapshot = full ?? 'incremental';
|
|
68
|
-
}
|
|
69
|
-
setIncludeTabs() {
|
|
70
|
-
this._includeTabs = true;
|
|
71
|
-
}
|
|
72
|
-
async finish() {
|
|
73
|
-
// All the async snapshotting post-action is happening here.
|
|
74
|
-
// Everything below should race against modal states.
|
|
75
|
-
if (this._includeSnapshot && this._context.currentTab())
|
|
76
|
-
this._tabSnapshot = await this._context.currentTabOrDie().captureSnapshot();
|
|
77
|
-
for (const tab of this._context.tabs())
|
|
78
|
-
await tab.updateTitle();
|
|
79
|
-
}
|
|
80
|
-
tabSnapshot() {
|
|
81
|
-
return this._tabSnapshot;
|
|
82
|
-
}
|
|
83
|
-
serialize() {
|
|
84
|
-
const response = [];
|
|
85
|
-
// Start with command result.
|
|
86
|
-
if (this._result.length) {
|
|
87
|
-
const resultText = this._result.join('\n');
|
|
88
|
-
const { text: clipped, truncated } = truncateStringTo(resultText, MAX_RESULT_CHARS);
|
|
89
|
-
response.push('### Result');
|
|
90
|
-
response.push(clipped);
|
|
91
|
-
if (truncated) {
|
|
92
|
-
const total = resultText.length;
|
|
93
|
-
const shown = clipped.length;
|
|
94
|
-
response.push(formatTruncationLine(shown, total, { units: 'chars' }));
|
|
95
|
-
}
|
|
96
|
-
response.push('');
|
|
97
|
-
}
|
|
98
|
-
// Then show events
|
|
99
|
-
if (this._events.length) {
|
|
100
|
-
response.push('### Events');
|
|
101
|
-
response.push(this._events.join('\n'));
|
|
102
|
-
response.push('');
|
|
103
|
-
}
|
|
104
|
-
// Add code if it exists.
|
|
105
|
-
if (this._includeCode && this._code.length) {
|
|
106
|
-
response.push(`### Ran Playwright code
|
|
107
|
-
\`\`\`js
|
|
108
|
-
${this._code.join('\n')}
|
|
109
|
-
\`\`\``);
|
|
110
|
-
response.push('');
|
|
111
|
-
}
|
|
112
|
-
// List browser tabs.
|
|
113
|
-
if (this._includeSnapshot || this._includeTabs)
|
|
114
|
-
response.push(...renderTabsMarkdown(this._context.tabs(), this._includeTabs));
|
|
115
|
-
// Add snapshot if provided.
|
|
116
|
-
if (this._tabSnapshot?.modalStates.length) {
|
|
117
|
-
response.push(...renderModalStates(this._context, this._tabSnapshot.modalStates));
|
|
118
|
-
response.push('');
|
|
119
|
-
}
|
|
120
|
-
else if (this._tabSnapshot) {
|
|
121
|
-
response.push(renderTabSnapshot(this._tabSnapshot));
|
|
122
|
-
response.push('');
|
|
123
|
-
}
|
|
124
|
-
// Main response part
|
|
125
|
-
const content = [
|
|
126
|
-
{ type: 'text', text: response.join('\n') },
|
|
127
|
-
];
|
|
128
|
-
// Image attachments.
|
|
129
|
-
if (this._context.config.imageResponses !== 'omit') {
|
|
130
|
-
for (const image of this._images)
|
|
131
|
-
content.push({ type: 'image', data: image.data.toString('base64'), mimeType: image.contentType });
|
|
132
|
-
}
|
|
133
|
-
return { content, isError: this._isError };
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
function renderTabSnapshot(tabSnapshot) {
|
|
137
|
-
const lines = [];
|
|
138
|
-
if (tabSnapshot.consoleMessages.length) {
|
|
139
|
-
lines.push(`### New console messages`);
|
|
140
|
-
for (const message of tabSnapshot.consoleMessages)
|
|
141
|
-
lines.push(`- ${trim(message.toString(), 100)}`);
|
|
142
|
-
lines.push('');
|
|
143
|
-
}
|
|
144
|
-
if (tabSnapshot.downloads.length) {
|
|
145
|
-
lines.push(`### Downloads`);
|
|
146
|
-
for (const entry of tabSnapshot.downloads) {
|
|
147
|
-
if (entry.finished)
|
|
148
|
-
lines.push(`- Downloaded file ${entry.download.suggestedFilename()} to ${entry.outputFile}`);
|
|
149
|
-
else
|
|
150
|
-
lines.push(`- Downloading file ${entry.download.suggestedFilename()} ...`);
|
|
151
|
-
}
|
|
152
|
-
lines.push('');
|
|
153
|
-
}
|
|
154
|
-
lines.push(`### Page state`);
|
|
155
|
-
lines.push(`- Page URL: ${tabSnapshot.url}`);
|
|
156
|
-
lines.push(`- Page Title: ${tabSnapshot.title}`);
|
|
157
|
-
lines.push(`- Page Snapshot:`);
|
|
158
|
-
lines.push('```yaml');
|
|
159
|
-
lines.push(tabSnapshot.ariaSnapshot);
|
|
160
|
-
lines.push('```');
|
|
161
|
-
return lines.join('\n');
|
|
162
|
-
}
|
|
163
|
-
function renderTabsMarkdown(tabs, force = false) {
|
|
164
|
-
if (tabs.length === 1 && !force)
|
|
165
|
-
return [];
|
|
166
|
-
if (!tabs.length) {
|
|
167
|
-
return [
|
|
168
|
-
'### Open tabs',
|
|
169
|
-
'No open tabs. Use the "browser_navigate" tool to navigate to a page first.',
|
|
170
|
-
'',
|
|
171
|
-
];
|
|
172
|
-
}
|
|
173
|
-
const lines = ['### Open tabs'];
|
|
174
|
-
for (let i = 0; i < tabs.length; i++) {
|
|
175
|
-
const tab = tabs[i];
|
|
176
|
-
const current = tab.isCurrentTab() ? ' (current)' : '';
|
|
177
|
-
lines.push(`- ${i}:${current} [${tab.lastTitle()}] (${tab.page.url()})`);
|
|
178
|
-
}
|
|
179
|
-
lines.push('');
|
|
180
|
-
return lines;
|
|
181
|
-
}
|
|
182
|
-
function trim(text, maxLength) {
|
|
183
|
-
if (text.length <= maxLength)
|
|
184
|
-
return text;
|
|
185
|
-
return text.slice(0, maxLength) + '...';
|
|
186
|
-
}
|
package/lib/sessionLog.js
DELETED
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) Microsoft Corporation.
|
|
3
|
-
*
|
|
4
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
-
* you may not use this file except in compliance with the License.
|
|
6
|
-
* You may obtain a copy of the License at
|
|
7
|
-
*
|
|
8
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
-
*
|
|
10
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
-
* See the License for the specific language governing permissions and
|
|
14
|
-
* limitations under the License.
|
|
15
|
-
*/
|
|
16
|
-
import fs from 'fs';
|
|
17
|
-
import path from 'path';
|
|
18
|
-
import { logUnhandledError } from './utils/log.js';
|
|
19
|
-
import { outputFile } from './config.js';
|
|
20
|
-
export class SessionLog {
|
|
21
|
-
_folder;
|
|
22
|
-
_file;
|
|
23
|
-
_ordinal = 0;
|
|
24
|
-
_pendingEntries = [];
|
|
25
|
-
_sessionFileQueue = Promise.resolve();
|
|
26
|
-
_flushEntriesTimeout;
|
|
27
|
-
constructor(sessionFolder) {
|
|
28
|
-
this._folder = sessionFolder;
|
|
29
|
-
this._file = path.join(this._folder, 'session.md');
|
|
30
|
-
}
|
|
31
|
-
static async create(config, rootPath) {
|
|
32
|
-
const sessionFolder = await outputFile(config, rootPath, `session-${Date.now()}`);
|
|
33
|
-
await fs.promises.mkdir(sessionFolder, { recursive: true });
|
|
34
|
-
// eslint-disable-next-line no-console
|
|
35
|
-
console.error(`Session: ${sessionFolder}`);
|
|
36
|
-
return new SessionLog(sessionFolder);
|
|
37
|
-
}
|
|
38
|
-
logResponse(response) {
|
|
39
|
-
const entry = {
|
|
40
|
-
timestamp: performance.now(),
|
|
41
|
-
toolCall: {
|
|
42
|
-
toolName: response.toolName,
|
|
43
|
-
toolArgs: response.toolArgs,
|
|
44
|
-
result: response.result(),
|
|
45
|
-
isError: response.isError(),
|
|
46
|
-
},
|
|
47
|
-
code: response.code(),
|
|
48
|
-
tabSnapshot: response.tabSnapshot(),
|
|
49
|
-
};
|
|
50
|
-
this._appendEntry(entry);
|
|
51
|
-
}
|
|
52
|
-
logUserAction(action, tab, code, isUpdate) {
|
|
53
|
-
code = code.trim();
|
|
54
|
-
if (isUpdate) {
|
|
55
|
-
const lastEntry = this._pendingEntries[this._pendingEntries.length - 1];
|
|
56
|
-
if (lastEntry.userAction?.name === action.name) {
|
|
57
|
-
lastEntry.userAction = action;
|
|
58
|
-
lastEntry.code = code;
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
if (action.name === 'navigate') {
|
|
63
|
-
// Already logged at this location.
|
|
64
|
-
const lastEntry = this._pendingEntries[this._pendingEntries.length - 1];
|
|
65
|
-
if (lastEntry?.tabSnapshot?.url === action.url)
|
|
66
|
-
return;
|
|
67
|
-
}
|
|
68
|
-
const entry = {
|
|
69
|
-
timestamp: performance.now(),
|
|
70
|
-
userAction: action,
|
|
71
|
-
code,
|
|
72
|
-
tabSnapshot: {
|
|
73
|
-
url: tab.page.url(),
|
|
74
|
-
title: '',
|
|
75
|
-
ariaSnapshot: action.ariaSnapshot || '',
|
|
76
|
-
modalStates: [],
|
|
77
|
-
consoleMessages: [],
|
|
78
|
-
downloads: [],
|
|
79
|
-
},
|
|
80
|
-
};
|
|
81
|
-
this._appendEntry(entry);
|
|
82
|
-
}
|
|
83
|
-
_appendEntry(entry) {
|
|
84
|
-
this._pendingEntries.push(entry);
|
|
85
|
-
if (this._flushEntriesTimeout)
|
|
86
|
-
clearTimeout(this._flushEntriesTimeout);
|
|
87
|
-
this._flushEntriesTimeout = setTimeout(() => this._flushEntries(), 1000);
|
|
88
|
-
}
|
|
89
|
-
async _flushEntries() {
|
|
90
|
-
clearTimeout(this._flushEntriesTimeout);
|
|
91
|
-
const entries = this._pendingEntries;
|
|
92
|
-
this._pendingEntries = [];
|
|
93
|
-
const lines = [''];
|
|
94
|
-
for (const entry of entries) {
|
|
95
|
-
const ordinal = (++this._ordinal).toString().padStart(3, '0');
|
|
96
|
-
if (entry.toolCall) {
|
|
97
|
-
lines.push(`### Tool call: ${entry.toolCall.toolName}`, `- Args`, '```json', JSON.stringify(entry.toolCall.toolArgs, null, 2), '```');
|
|
98
|
-
if (entry.toolCall.result) {
|
|
99
|
-
lines.push(entry.toolCall.isError ? `- Error` : `- Result`, '```', entry.toolCall.result, '```');
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
if (entry.userAction) {
|
|
103
|
-
const actionData = { ...entry.userAction };
|
|
104
|
-
delete actionData.ariaSnapshot;
|
|
105
|
-
delete actionData.selector;
|
|
106
|
-
delete actionData.signals;
|
|
107
|
-
lines.push(`### User action: ${entry.userAction.name}`, `- Args`, '```json', JSON.stringify(actionData, null, 2), '```');
|
|
108
|
-
}
|
|
109
|
-
if (entry.code) {
|
|
110
|
-
lines.push(`- Code`, '```js', entry.code, '```');
|
|
111
|
-
}
|
|
112
|
-
if (entry.tabSnapshot) {
|
|
113
|
-
const fileName = `${ordinal}.snapshot.yml`;
|
|
114
|
-
fs.promises.writeFile(path.join(this._folder, fileName), entry.tabSnapshot.ariaSnapshot).catch(logUnhandledError);
|
|
115
|
-
lines.push(`- Snapshot: ${fileName}`);
|
|
116
|
-
}
|
|
117
|
-
lines.push('', '');
|
|
118
|
-
}
|
|
119
|
-
this._sessionFileQueue = this._sessionFileQueue.then(() => fs.promises.appendFile(this._file, lines.join('\n')));
|
|
120
|
-
}
|
|
121
|
-
}
|
package/lib/tab.js
DELETED
|
@@ -1,249 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) Microsoft Corporation.
|
|
3
|
-
*
|
|
4
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
-
* you may not use this file except in compliance with the License.
|
|
6
|
-
* You may obtain a copy of the License at
|
|
7
|
-
*
|
|
8
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
-
*
|
|
10
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
-
* See the License for the specific language governing permissions and
|
|
14
|
-
* limitations under the License.
|
|
15
|
-
*/
|
|
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
|
-
export const TabEvents = {
|
|
21
|
-
modalState: 'modalState'
|
|
22
|
-
};
|
|
23
|
-
export class Tab extends EventEmitter {
|
|
24
|
-
context;
|
|
25
|
-
page;
|
|
26
|
-
_lastTitle = 'about:blank';
|
|
27
|
-
_consoleMessages = [];
|
|
28
|
-
_recentConsoleMessages = [];
|
|
29
|
-
_requests = new Map();
|
|
30
|
-
_onPageClose;
|
|
31
|
-
_modalStates = [];
|
|
32
|
-
_downloads = [];
|
|
33
|
-
constructor(context, page, onPageClose) {
|
|
34
|
-
super();
|
|
35
|
-
this.context = context;
|
|
36
|
-
this.page = page;
|
|
37
|
-
this._onPageClose = onPageClose;
|
|
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
|
-
this.setModalState({
|
|
45
|
-
type: 'fileChooser',
|
|
46
|
-
description: 'File chooser',
|
|
47
|
-
fileChooser: chooser,
|
|
48
|
-
});
|
|
49
|
-
});
|
|
50
|
-
page.on('dialog', dialog => this._dialogShown(dialog));
|
|
51
|
-
// page.on('download', download => {
|
|
52
|
-
// void this._downloadStarted(download);
|
|
53
|
-
// });
|
|
54
|
-
page.setDefaultNavigationTimeout(60000);
|
|
55
|
-
page.setDefaultTimeout(5000);
|
|
56
|
-
page[tabSymbol] = this;
|
|
57
|
-
}
|
|
58
|
-
static forPage(page) {
|
|
59
|
-
return page[tabSymbol];
|
|
60
|
-
}
|
|
61
|
-
modalStates() {
|
|
62
|
-
return this._modalStates;
|
|
63
|
-
}
|
|
64
|
-
setModalState(modalState) {
|
|
65
|
-
this._modalStates.push(modalState);
|
|
66
|
-
this.emit(TabEvents.modalState, modalState);
|
|
67
|
-
}
|
|
68
|
-
clearModalState(modalState) {
|
|
69
|
-
this._modalStates = this._modalStates.filter(state => state !== modalState);
|
|
70
|
-
}
|
|
71
|
-
modalStatesMarkdown() {
|
|
72
|
-
return renderModalStates(this.context, this.modalStates());
|
|
73
|
-
}
|
|
74
|
-
_dialogShown(dialog) {
|
|
75
|
-
this.setModalState({
|
|
76
|
-
type: 'dialog',
|
|
77
|
-
description: `"${dialog.type()}" dialog with message "${dialog.message()}"`,
|
|
78
|
-
dialog,
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
async _downloadStarted(download) {
|
|
82
|
-
const entry = {
|
|
83
|
-
download,
|
|
84
|
-
finished: false,
|
|
85
|
-
outputFile: await this.context.outputFile(download.suggestedFilename())
|
|
86
|
-
};
|
|
87
|
-
this._downloads.push(entry);
|
|
88
|
-
await download.saveAs(entry.outputFile);
|
|
89
|
-
entry.finished = true;
|
|
90
|
-
}
|
|
91
|
-
_clearCollectedArtifacts() {
|
|
92
|
-
this._consoleMessages.length = 0;
|
|
93
|
-
this._recentConsoleMessages.length = 0;
|
|
94
|
-
this._requests.clear();
|
|
95
|
-
}
|
|
96
|
-
_handleConsoleMessage(message) {
|
|
97
|
-
this._consoleMessages.push(message);
|
|
98
|
-
this._recentConsoleMessages.push(message);
|
|
99
|
-
}
|
|
100
|
-
_onClose() {
|
|
101
|
-
this._clearCollectedArtifacts();
|
|
102
|
-
this._onPageClose(this);
|
|
103
|
-
}
|
|
104
|
-
async updateTitle() {
|
|
105
|
-
await this._raceAgainstModalStates(async () => {
|
|
106
|
-
this._lastTitle = await callOnPageNoTrace(this.page, page => page.title());
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
lastTitle() {
|
|
110
|
-
return this._lastTitle;
|
|
111
|
-
}
|
|
112
|
-
isCurrentTab() {
|
|
113
|
-
return this === this.context.currentTab();
|
|
114
|
-
}
|
|
115
|
-
async waitForLoadState(state, options) {
|
|
116
|
-
await callOnPageNoTrace(this.page, page => page.waitForLoadState(state, options).catch(logUnhandledError));
|
|
117
|
-
}
|
|
118
|
-
async navigate(url) {
|
|
119
|
-
this._clearCollectedArtifacts();
|
|
120
|
-
// const downloadEvent = callOnPageNoTrace(this.page, page => page.waitForEvent('download').catch(logUnhandledError));
|
|
121
|
-
const downloadEvent = Promise.resolve(undefined);
|
|
122
|
-
try {
|
|
123
|
-
await this.page.goto(url, { waitUntil: 'domcontentloaded' });
|
|
124
|
-
}
|
|
125
|
-
catch (_e) {
|
|
126
|
-
const e = _e;
|
|
127
|
-
const mightBeDownload = e.message.includes('net::ERR_ABORTED') // chromium
|
|
128
|
-
|| e.message.includes('Download is starting'); // firefox + webkit
|
|
129
|
-
if (!mightBeDownload)
|
|
130
|
-
throw e;
|
|
131
|
-
// on chromium, the download event is fired *after* page.goto rejects, so we wait a lil bit
|
|
132
|
-
const download = await Promise.race([
|
|
133
|
-
downloadEvent,
|
|
134
|
-
new Promise(resolve => setTimeout(resolve, 3000)),
|
|
135
|
-
]);
|
|
136
|
-
if (!download)
|
|
137
|
-
throw e;
|
|
138
|
-
// Make sure other "download" listeners are notified first.
|
|
139
|
-
await new Promise(resolve => setTimeout(resolve, 500));
|
|
140
|
-
return;
|
|
141
|
-
}
|
|
142
|
-
// Cap load event to 5 seconds, the page is operational at this point.
|
|
143
|
-
await this.waitForLoadState('load', { timeout: 5000 });
|
|
144
|
-
}
|
|
145
|
-
consoleMessages() {
|
|
146
|
-
return this._consoleMessages;
|
|
147
|
-
}
|
|
148
|
-
requests() {
|
|
149
|
-
return this._requests;
|
|
150
|
-
}
|
|
151
|
-
async captureSnapshot() {
|
|
152
|
-
let tabSnapshot;
|
|
153
|
-
const modalStates = await this._raceAgainstModalStates(async () => {
|
|
154
|
-
const snapshot = await this.page._snapshotForAI();
|
|
155
|
-
tabSnapshot = {
|
|
156
|
-
url: this.page.url(),
|
|
157
|
-
title: await this.page.title(),
|
|
158
|
-
ariaSnapshot: snapshot,
|
|
159
|
-
modalStates: [],
|
|
160
|
-
consoleMessages: [],
|
|
161
|
-
downloads: this._downloads,
|
|
162
|
-
};
|
|
163
|
-
});
|
|
164
|
-
if (tabSnapshot) {
|
|
165
|
-
// Assign console message late so that we did not lose any to modal state.
|
|
166
|
-
tabSnapshot.consoleMessages = this._recentConsoleMessages;
|
|
167
|
-
this._recentConsoleMessages = [];
|
|
168
|
-
}
|
|
169
|
-
return tabSnapshot ?? {
|
|
170
|
-
url: this.page.url(),
|
|
171
|
-
title: '',
|
|
172
|
-
ariaSnapshot: '',
|
|
173
|
-
modalStates,
|
|
174
|
-
consoleMessages: [],
|
|
175
|
-
downloads: [],
|
|
176
|
-
};
|
|
177
|
-
}
|
|
178
|
-
_javaScriptBlocked() {
|
|
179
|
-
return this._modalStates.some(state => state.type === 'dialog');
|
|
180
|
-
}
|
|
181
|
-
async _raceAgainstModalStates(action) {
|
|
182
|
-
if (this.modalStates().length)
|
|
183
|
-
return this.modalStates();
|
|
184
|
-
const promise = new ManualPromise();
|
|
185
|
-
const listener = (modalState) => promise.resolve([modalState]);
|
|
186
|
-
this.once(TabEvents.modalState, listener);
|
|
187
|
-
return await Promise.race([
|
|
188
|
-
action().then(() => {
|
|
189
|
-
this.off(TabEvents.modalState, listener);
|
|
190
|
-
return [];
|
|
191
|
-
}),
|
|
192
|
-
promise,
|
|
193
|
-
]);
|
|
194
|
-
}
|
|
195
|
-
async waitForCompletion(callback) {
|
|
196
|
-
await this._raceAgainstModalStates(() => waitForCompletion(this, callback));
|
|
197
|
-
}
|
|
198
|
-
async refLocator(params) {
|
|
199
|
-
return (await this.refLocators([params]))[0];
|
|
200
|
-
}
|
|
201
|
-
async refLocators(params) {
|
|
202
|
-
// Ensure aria-ref mapping is present without validating refs against a fresh snapshot.
|
|
203
|
-
// This avoids invalidating refs obtained from browser_get_snapshot while still
|
|
204
|
-
// initializing the mapping when needed (e.g., after navigation).
|
|
205
|
-
await this.page._snapshotForAI();
|
|
206
|
-
return params.map(param => this.page.locator(`aria-ref=${param.ref}`));
|
|
207
|
-
}
|
|
208
|
-
async waitForTimeout(time) {
|
|
209
|
-
if (this._javaScriptBlocked()) {
|
|
210
|
-
await new Promise(f => setTimeout(f, time));
|
|
211
|
-
return;
|
|
212
|
-
}
|
|
213
|
-
await callOnPageNoTrace(this.page, page => {
|
|
214
|
-
return page.evaluate(() => new Promise(f => setTimeout(f, 1000)));
|
|
215
|
-
});
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
function messageToConsoleMessage(message) {
|
|
219
|
-
return {
|
|
220
|
-
type: message.type(),
|
|
221
|
-
text: message.text(),
|
|
222
|
-
toString: () => `[${message.type().toUpperCase()}] ${message.text()} @ ${message.location().url}:${message.location().lineNumber}`,
|
|
223
|
-
};
|
|
224
|
-
}
|
|
225
|
-
function pageErrorToConsoleMessage(errorOrValue) {
|
|
226
|
-
if (errorOrValue instanceof Error) {
|
|
227
|
-
return {
|
|
228
|
-
type: undefined,
|
|
229
|
-
text: errorOrValue.message,
|
|
230
|
-
toString: () => errorOrValue.stack || errorOrValue.message,
|
|
231
|
-
};
|
|
232
|
-
}
|
|
233
|
-
return {
|
|
234
|
-
type: undefined,
|
|
235
|
-
text: String(errorOrValue),
|
|
236
|
-
toString: () => String(errorOrValue),
|
|
237
|
-
};
|
|
238
|
-
}
|
|
239
|
-
export function renderModalStates(context, modalStates) {
|
|
240
|
-
const result = ['### Modal state'];
|
|
241
|
-
if (modalStates.length === 0)
|
|
242
|
-
result.push('- There is no modal state present');
|
|
243
|
-
for (const state of modalStates) {
|
|
244
|
-
const tool = context.tools.filter(tool => 'clearsModalState' in tool).find(tool => tool.clearsModalState === state.type);
|
|
245
|
-
result.push(`- [${state.description}]: can be handled by the "${tool?.schema.name}" tool`);
|
|
246
|
-
}
|
|
247
|
-
return result;
|
|
248
|
-
}
|
|
249
|
-
const tabSymbol = Symbol('tabSymbol');
|