@playwright/mcp 0.0.31 → 0.0.33
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/README.md +27 -6
- package/config.d.ts +5 -0
- package/index.d.ts +1 -6
- package/lib/browserContextFactory.js +64 -54
- package/lib/browserServerBackend.js +121 -0
- package/lib/config.js +10 -9
- package/lib/context.js +107 -182
- package/lib/extension/cdpRelay.js +346 -0
- package/lib/extension/extensionContextFactory.js +56 -0
- package/lib/extension/main.js +26 -0
- package/lib/httpServer.js +20 -182
- package/lib/index.js +6 -3
- package/lib/loop/loop.js +69 -0
- package/lib/loop/loopClaude.js +152 -0
- package/lib/loop/loopOpenAI.js +141 -0
- package/lib/loop/main.js +60 -0
- package/lib/loopTools/context.js +66 -0
- package/lib/loopTools/main.js +49 -0
- package/lib/loopTools/perform.js +32 -0
- package/lib/loopTools/snapshot.js +29 -0
- package/lib/loopTools/tool.js +18 -0
- package/lib/mcp/inProcessTransport.js +72 -0
- package/lib/mcp/server.js +93 -0
- package/lib/{transport.js → mcp/transport.js} +30 -42
- package/lib/package.js +3 -3
- package/lib/program.js +39 -9
- package/lib/response.js +165 -0
- package/lib/sessionLog.js +121 -0
- package/lib/tab.js +138 -24
- package/lib/tools/common.js +10 -23
- package/lib/tools/console.js +4 -15
- package/lib/tools/dialogs.js +12 -17
- package/lib/tools/evaluate.js +12 -21
- package/lib/tools/files.js +9 -16
- package/lib/tools/install.js +3 -7
- package/lib/tools/keyboard.js +28 -42
- package/lib/tools/mouse.js +27 -50
- package/lib/tools/navigate.js +12 -35
- package/lib/tools/network.js +5 -15
- package/lib/tools/pdf.js +7 -16
- package/lib/tools/screenshot.js +35 -33
- package/lib/tools/snapshot.js +44 -69
- package/lib/tools/tabs.js +10 -41
- package/lib/tools/tool.js +15 -0
- package/lib/tools/utils.js +2 -9
- package/lib/tools/wait.js +3 -6
- package/lib/tools.js +3 -0
- package/lib/utils.js +26 -0
- package/package.json +11 -6
- package/lib/connection.js +0 -81
- package/lib/pageSnapshot.js +0 -43
- package/lib/server.js +0 -48
|
@@ -0,0 +1,72 @@
|
|
|
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
|
+
export class InProcessTransport {
|
|
17
|
+
_server;
|
|
18
|
+
_serverTransport;
|
|
19
|
+
_connected = false;
|
|
20
|
+
constructor(server) {
|
|
21
|
+
this._server = server;
|
|
22
|
+
this._serverTransport = new InProcessServerTransport(this);
|
|
23
|
+
}
|
|
24
|
+
async start() {
|
|
25
|
+
if (this._connected)
|
|
26
|
+
throw new Error('InprocessTransport already started!');
|
|
27
|
+
await this._server.connect(this._serverTransport);
|
|
28
|
+
this._connected = true;
|
|
29
|
+
}
|
|
30
|
+
async send(message, options) {
|
|
31
|
+
if (!this._connected)
|
|
32
|
+
throw new Error('Transport not connected');
|
|
33
|
+
this._serverTransport._receiveFromClient(message);
|
|
34
|
+
}
|
|
35
|
+
async close() {
|
|
36
|
+
if (this._connected) {
|
|
37
|
+
this._connected = false;
|
|
38
|
+
this.onclose?.();
|
|
39
|
+
this._serverTransport.onclose?.();
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
onclose;
|
|
43
|
+
onerror;
|
|
44
|
+
onmessage;
|
|
45
|
+
sessionId;
|
|
46
|
+
setProtocolVersion;
|
|
47
|
+
_receiveFromServer(message, extra) {
|
|
48
|
+
this.onmessage?.(message, extra);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
class InProcessServerTransport {
|
|
52
|
+
_clientTransport;
|
|
53
|
+
constructor(clientTransport) {
|
|
54
|
+
this._clientTransport = clientTransport;
|
|
55
|
+
}
|
|
56
|
+
async start() {
|
|
57
|
+
}
|
|
58
|
+
async send(message, options) {
|
|
59
|
+
this._clientTransport._receiveFromServer(message);
|
|
60
|
+
}
|
|
61
|
+
async close() {
|
|
62
|
+
this.onclose?.();
|
|
63
|
+
}
|
|
64
|
+
onclose;
|
|
65
|
+
onerror;
|
|
66
|
+
onmessage;
|
|
67
|
+
sessionId;
|
|
68
|
+
setProtocolVersion;
|
|
69
|
+
_receiveFromClient(message) {
|
|
70
|
+
this.onmessage?.(message);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
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 { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
|
17
|
+
import { CallToolRequestSchema, ListToolsRequestSchema } from '@modelcontextprotocol/sdk/types.js';
|
|
18
|
+
import { zodToJsonSchema } from 'zod-to-json-schema';
|
|
19
|
+
import { ManualPromise } from '../manualPromise.js';
|
|
20
|
+
import { logUnhandledError } from '../log.js';
|
|
21
|
+
export async function connect(serverBackendFactory, transport, runHeartbeat) {
|
|
22
|
+
const backend = serverBackendFactory();
|
|
23
|
+
const server = createServer(backend, runHeartbeat);
|
|
24
|
+
await server.connect(transport);
|
|
25
|
+
}
|
|
26
|
+
export function createServer(backend, runHeartbeat) {
|
|
27
|
+
const initializedPromise = new ManualPromise();
|
|
28
|
+
const server = new Server({ name: backend.name, version: backend.version }, {
|
|
29
|
+
capabilities: {
|
|
30
|
+
tools: {},
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
const tools = backend.tools();
|
|
34
|
+
server.setRequestHandler(ListToolsRequestSchema, async () => {
|
|
35
|
+
return { tools: tools.map(tool => ({
|
|
36
|
+
name: tool.name,
|
|
37
|
+
description: tool.description,
|
|
38
|
+
inputSchema: zodToJsonSchema(tool.inputSchema),
|
|
39
|
+
annotations: {
|
|
40
|
+
title: tool.title,
|
|
41
|
+
readOnlyHint: tool.type === 'readOnly',
|
|
42
|
+
destructiveHint: tool.type === 'destructive',
|
|
43
|
+
openWorldHint: true,
|
|
44
|
+
},
|
|
45
|
+
})) };
|
|
46
|
+
});
|
|
47
|
+
let heartbeatRunning = false;
|
|
48
|
+
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
49
|
+
await initializedPromise;
|
|
50
|
+
if (runHeartbeat && !heartbeatRunning) {
|
|
51
|
+
heartbeatRunning = true;
|
|
52
|
+
startHeartbeat(server);
|
|
53
|
+
}
|
|
54
|
+
const errorResult = (...messages) => ({
|
|
55
|
+
content: [{ type: 'text', text: '### Result\n' + messages.join('\n') }],
|
|
56
|
+
isError: true,
|
|
57
|
+
});
|
|
58
|
+
const tool = tools.find(tool => tool.name === request.params.name);
|
|
59
|
+
if (!tool)
|
|
60
|
+
return errorResult(`Error: Tool "${request.params.name}" not found`);
|
|
61
|
+
try {
|
|
62
|
+
return await backend.callTool(tool, tool.inputSchema.parse(request.params.arguments || {}));
|
|
63
|
+
}
|
|
64
|
+
catch (error) {
|
|
65
|
+
return errorResult(String(error));
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
addServerListener(server, 'initialized', () => {
|
|
69
|
+
backend.initialize?.(server).then(() => initializedPromise.resolve()).catch(logUnhandledError);
|
|
70
|
+
});
|
|
71
|
+
addServerListener(server, 'close', () => backend.serverClosed?.());
|
|
72
|
+
return server;
|
|
73
|
+
}
|
|
74
|
+
const startHeartbeat = (server) => {
|
|
75
|
+
const beat = () => {
|
|
76
|
+
Promise.race([
|
|
77
|
+
server.ping(),
|
|
78
|
+
new Promise((_, reject) => setTimeout(() => reject(new Error('ping timeout')), 5000)),
|
|
79
|
+
]).then(() => {
|
|
80
|
+
setTimeout(beat, 3000);
|
|
81
|
+
}).catch(() => {
|
|
82
|
+
void server.close();
|
|
83
|
+
});
|
|
84
|
+
};
|
|
85
|
+
beat();
|
|
86
|
+
};
|
|
87
|
+
function addServerListener(server, event, listener) {
|
|
88
|
+
const oldListener = server[`on${event}`];
|
|
89
|
+
server[`on${event}`] = () => {
|
|
90
|
+
oldListener?.();
|
|
91
|
+
listener();
|
|
92
|
+
};
|
|
93
|
+
}
|
|
@@ -13,18 +13,27 @@
|
|
|
13
13
|
* See the License for the specific language governing permissions and
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
|
-
import
|
|
17
|
-
import assert from 'node:assert';
|
|
18
|
-
import crypto from 'node:crypto';
|
|
16
|
+
import crypto from 'crypto';
|
|
19
17
|
import debug from 'debug';
|
|
20
18
|
import { SSEServerTransport } from '@modelcontextprotocol/sdk/server/sse.js';
|
|
21
19
|
import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
|
|
22
20
|
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
23
|
-
|
|
24
|
-
|
|
21
|
+
import { httpAddressToString, startHttpServer } from '../httpServer.js';
|
|
22
|
+
import * as mcpServer from './server.js';
|
|
23
|
+
export async function start(serverBackendFactory, options) {
|
|
24
|
+
if (options.port !== undefined) {
|
|
25
|
+
const httpServer = await startHttpServer(options);
|
|
26
|
+
startHttpTransport(httpServer, serverBackendFactory);
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
await startStdioTransport(serverBackendFactory);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
async function startStdioTransport(serverBackendFactory) {
|
|
33
|
+
await mcpServer.connect(serverBackendFactory, new StdioServerTransport(), false);
|
|
25
34
|
}
|
|
26
35
|
const testDebug = debug('pw:mcp:test');
|
|
27
|
-
async function handleSSE(
|
|
36
|
+
async function handleSSE(serverBackendFactory, req, res, url, sessions) {
|
|
28
37
|
if (req.method === 'POST') {
|
|
29
38
|
const sessionId = url.searchParams.get('sessionId');
|
|
30
39
|
if (!sessionId) {
|
|
@@ -42,19 +51,17 @@ async function handleSSE(server, req, res, url, sessions) {
|
|
|
42
51
|
const transport = new SSEServerTransport('/sse', res);
|
|
43
52
|
sessions.set(transport.sessionId, transport);
|
|
44
53
|
testDebug(`create SSE session: ${transport.sessionId}`);
|
|
45
|
-
|
|
54
|
+
await mcpServer.connect(serverBackendFactory, transport, false);
|
|
46
55
|
res.on('close', () => {
|
|
47
56
|
testDebug(`delete SSE session: ${transport.sessionId}`);
|
|
48
57
|
sessions.delete(transport.sessionId);
|
|
49
|
-
// eslint-disable-next-line no-console
|
|
50
|
-
void connection.close().catch(e => console.error(e));
|
|
51
58
|
});
|
|
52
59
|
return;
|
|
53
60
|
}
|
|
54
61
|
res.statusCode = 405;
|
|
55
62
|
res.end('Method not allowed');
|
|
56
63
|
}
|
|
57
|
-
async function handleStreamable(
|
|
64
|
+
async function handleStreamable(serverBackendFactory, req, res, sessions) {
|
|
58
65
|
const sessionId = req.headers['mcp-session-id'];
|
|
59
66
|
if (sessionId) {
|
|
60
67
|
const transport = sessions.get(sessionId);
|
|
@@ -68,42 +75,33 @@ async function handleStreamable(server, req, res, sessions) {
|
|
|
68
75
|
if (req.method === 'POST') {
|
|
69
76
|
const transport = new StreamableHTTPServerTransport({
|
|
70
77
|
sessionIdGenerator: () => crypto.randomUUID(),
|
|
71
|
-
onsessioninitialized: sessionId => {
|
|
78
|
+
onsessioninitialized: async (sessionId) => {
|
|
79
|
+
testDebug(`create http session: ${transport.sessionId}`);
|
|
80
|
+
await mcpServer.connect(serverBackendFactory, transport, true);
|
|
72
81
|
sessions.set(sessionId, transport);
|
|
73
82
|
}
|
|
74
83
|
});
|
|
75
84
|
transport.onclose = () => {
|
|
76
|
-
if (transport.sessionId)
|
|
77
|
-
|
|
85
|
+
if (!transport.sessionId)
|
|
86
|
+
return;
|
|
87
|
+
sessions.delete(transport.sessionId);
|
|
88
|
+
testDebug(`delete http session: ${transport.sessionId}`);
|
|
78
89
|
};
|
|
79
|
-
await server.createConnection(transport);
|
|
80
90
|
await transport.handleRequest(req, res);
|
|
81
91
|
return;
|
|
82
92
|
}
|
|
83
93
|
res.statusCode = 400;
|
|
84
94
|
res.end('Invalid request');
|
|
85
95
|
}
|
|
86
|
-
|
|
87
|
-
const { host, port } = config;
|
|
88
|
-
const httpServer = http.createServer();
|
|
89
|
-
await new Promise((resolve, reject) => {
|
|
90
|
-
httpServer.on('error', reject);
|
|
91
|
-
httpServer.listen(port, host, () => {
|
|
92
|
-
resolve();
|
|
93
|
-
httpServer.removeListener('error', reject);
|
|
94
|
-
});
|
|
95
|
-
});
|
|
96
|
-
return httpServer;
|
|
97
|
-
}
|
|
98
|
-
export function startHttpTransport(httpServer, mcpServer) {
|
|
96
|
+
function startHttpTransport(httpServer, serverBackendFactory) {
|
|
99
97
|
const sseSessions = new Map();
|
|
100
98
|
const streamableSessions = new Map();
|
|
101
99
|
httpServer.on('request', async (req, res) => {
|
|
102
100
|
const url = new URL(`http://localhost${req.url}`);
|
|
103
|
-
if (url.pathname.startsWith('/
|
|
104
|
-
await
|
|
101
|
+
if (url.pathname.startsWith('/sse'))
|
|
102
|
+
await handleSSE(serverBackendFactory, req, res, url, sseSessions);
|
|
105
103
|
else
|
|
106
|
-
await
|
|
104
|
+
await handleStreamable(serverBackendFactory, req, res, streamableSessions);
|
|
107
105
|
});
|
|
108
106
|
const url = httpAddressToString(httpServer.address());
|
|
109
107
|
const message = [
|
|
@@ -112,22 +110,12 @@ export function startHttpTransport(httpServer, mcpServer) {
|
|
|
112
110
|
JSON.stringify({
|
|
113
111
|
'mcpServers': {
|
|
114
112
|
'playwright': {
|
|
115
|
-
'url': `${url}/
|
|
113
|
+
'url': `${url}/mcp`
|
|
116
114
|
}
|
|
117
115
|
}
|
|
118
116
|
}, undefined, 2),
|
|
119
|
-
'
|
|
117
|
+
'For legacy SSE transport support, you can use the /sse endpoint instead.',
|
|
120
118
|
].join('\n');
|
|
121
119
|
// eslint-disable-next-line no-console
|
|
122
120
|
console.error(message);
|
|
123
121
|
}
|
|
124
|
-
export function httpAddressToString(address) {
|
|
125
|
-
assert(address, 'Could not bind server socket');
|
|
126
|
-
if (typeof address === 'string')
|
|
127
|
-
return address;
|
|
128
|
-
const resolvedPort = address.port;
|
|
129
|
-
let resolvedHost = address.family === 'IPv4' ? address.address : `[${address.address}]`;
|
|
130
|
-
if (resolvedHost === '0.0.0.0' || resolvedHost === '[::]')
|
|
131
|
-
resolvedHost = 'localhost';
|
|
132
|
-
return `http://${resolvedHost}:${resolvedPort}`;
|
|
133
|
-
}
|
package/lib/package.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 fs from '
|
|
17
|
-
import
|
|
18
|
-
import
|
|
16
|
+
import fs from 'fs';
|
|
17
|
+
import path from 'path';
|
|
18
|
+
import url from 'url';
|
|
19
19
|
const __filename = url.fileURLToPath(import.meta.url);
|
|
20
20
|
export const packageJSON = JSON.parse(fs.readFileSync(path.join(path.dirname(__filename), '..', 'package.json'), 'utf8'));
|
package/lib/program.js
CHANGED
|
@@ -16,10 +16,14 @@
|
|
|
16
16
|
import { program, Option } from 'commander';
|
|
17
17
|
// @ts-ignore
|
|
18
18
|
import { startTraceViewerServer } from 'playwright-core/lib/server';
|
|
19
|
-
import
|
|
19
|
+
import * as mcpTransport from './mcp/transport.js';
|
|
20
20
|
import { commaSeparatedList, resolveCLIConfig, semicolonSeparatedList } from './config.js';
|
|
21
|
-
import { Server } from './server.js';
|
|
22
21
|
import { packageJSON } from './package.js';
|
|
22
|
+
import { createExtensionContextFactory, runWithExtension } from './extension/main.js';
|
|
23
|
+
import { BrowserServerBackend } from './browserServerBackend.js';
|
|
24
|
+
import { Context } from './context.js';
|
|
25
|
+
import { contextFactory } from './browserContextFactory.js';
|
|
26
|
+
import { runLoopTools } from './loopTools/main.js';
|
|
23
27
|
program
|
|
24
28
|
.version('Version ' + packageJSON.version)
|
|
25
29
|
.name(packageJSON.name)
|
|
@@ -42,26 +46,38 @@ program
|
|
|
42
46
|
.option('--port <port>', 'port to listen on for SSE transport.')
|
|
43
47
|
.option('--proxy-bypass <bypass>', 'comma-separated domains to bypass proxy, for example ".com,chromium.org,.domain.com"')
|
|
44
48
|
.option('--proxy-server <proxy>', 'specify proxy server, for example "http://myproxy:3128" or "socks5://myproxy:8080"')
|
|
49
|
+
.option('--save-session', 'Whether to save the Playwright MCP session into the output directory.')
|
|
45
50
|
.option('--save-trace', 'Whether to save the Playwright Trace of the session into the output directory.')
|
|
46
51
|
.option('--storage-state <path>', 'path to the storage state file for isolated sessions.')
|
|
47
52
|
.option('--user-agent <ua string>', 'specify user agent string')
|
|
48
53
|
.option('--user-data-dir <path>', 'path to the user data directory. If not specified, a temporary directory will be created.')
|
|
49
54
|
.option('--viewport-size <size>', 'specify browser viewport size in pixels, for example "1280, 720"')
|
|
55
|
+
.addOption(new Option('--extension', 'Connect to a running browser instance (Edge/Chrome only). Requires the "Playwright MCP Bridge" browser extension to be installed.').hideHelp())
|
|
56
|
+
.addOption(new Option('--connect-tool', 'Allow to switch between different browser connection methods.').hideHelp())
|
|
57
|
+
.addOption(new Option('--loop-tools', 'Run loop tools').hideHelp())
|
|
50
58
|
.addOption(new Option('--vision', 'Legacy option, use --caps=vision instead').hideHelp())
|
|
51
59
|
.action(async (options) => {
|
|
60
|
+
setupExitWatchdog();
|
|
52
61
|
if (options.vision) {
|
|
53
62
|
// eslint-disable-next-line no-console
|
|
54
63
|
console.error('The --vision option is deprecated, use --caps=vision instead');
|
|
55
64
|
options.caps = 'vision';
|
|
56
65
|
}
|
|
57
66
|
const config = await resolveCLIConfig(options);
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
67
|
+
if (options.extension) {
|
|
68
|
+
await runWithExtension(config);
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
if (options.loopTools) {
|
|
72
|
+
await runLoopTools(config);
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
const browserContextFactory = contextFactory(config);
|
|
76
|
+
const factories = [browserContextFactory];
|
|
77
|
+
if (options.connectTool)
|
|
78
|
+
factories.push(createExtensionContextFactory(config));
|
|
79
|
+
const serverBackendFactory = () => new BrowserServerBackend(config, factories);
|
|
80
|
+
await mcpTransport.start(serverBackendFactory, config.server);
|
|
65
81
|
if (config.saveTrace) {
|
|
66
82
|
const server = await startTraceViewerServer();
|
|
67
83
|
const urlPrefix = server.urlPrefix('human-readable');
|
|
@@ -70,4 +86,18 @@ program
|
|
|
70
86
|
console.error('\nTrace viewer listening on ' + url);
|
|
71
87
|
}
|
|
72
88
|
});
|
|
89
|
+
function setupExitWatchdog() {
|
|
90
|
+
let isExiting = false;
|
|
91
|
+
const handleExit = async () => {
|
|
92
|
+
if (isExiting)
|
|
93
|
+
return;
|
|
94
|
+
isExiting = true;
|
|
95
|
+
setTimeout(() => process.exit(0), 15000);
|
|
96
|
+
await Context.disposeAll();
|
|
97
|
+
process.exit(0);
|
|
98
|
+
};
|
|
99
|
+
process.stdin.on('close', handleExit);
|
|
100
|
+
process.on('SIGINT', handleExit);
|
|
101
|
+
process.on('SIGTERM', handleExit);
|
|
102
|
+
}
|
|
73
103
|
void program.parseAsync(process.argv);
|
package/lib/response.js
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
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
|
+
export class Response {
|
|
18
|
+
_result = [];
|
|
19
|
+
_code = [];
|
|
20
|
+
_images = [];
|
|
21
|
+
_context;
|
|
22
|
+
_includeSnapshot = false;
|
|
23
|
+
_includeTabs = false;
|
|
24
|
+
_tabSnapshot;
|
|
25
|
+
toolName;
|
|
26
|
+
toolArgs;
|
|
27
|
+
_isError;
|
|
28
|
+
constructor(context, toolName, toolArgs) {
|
|
29
|
+
this._context = context;
|
|
30
|
+
this.toolName = toolName;
|
|
31
|
+
this.toolArgs = toolArgs;
|
|
32
|
+
}
|
|
33
|
+
addResult(result) {
|
|
34
|
+
this._result.push(result);
|
|
35
|
+
}
|
|
36
|
+
addError(error) {
|
|
37
|
+
this._result.push(error);
|
|
38
|
+
this._isError = true;
|
|
39
|
+
}
|
|
40
|
+
isError() {
|
|
41
|
+
return this._isError;
|
|
42
|
+
}
|
|
43
|
+
result() {
|
|
44
|
+
return this._result.join('\n');
|
|
45
|
+
}
|
|
46
|
+
addCode(code) {
|
|
47
|
+
this._code.push(code);
|
|
48
|
+
}
|
|
49
|
+
code() {
|
|
50
|
+
return this._code.join('\n');
|
|
51
|
+
}
|
|
52
|
+
addImage(image) {
|
|
53
|
+
this._images.push(image);
|
|
54
|
+
}
|
|
55
|
+
images() {
|
|
56
|
+
return this._images;
|
|
57
|
+
}
|
|
58
|
+
setIncludeSnapshot() {
|
|
59
|
+
this._includeSnapshot = true;
|
|
60
|
+
}
|
|
61
|
+
setIncludeTabs() {
|
|
62
|
+
this._includeTabs = true;
|
|
63
|
+
}
|
|
64
|
+
async finish() {
|
|
65
|
+
// All the async snapshotting post-action is happening here.
|
|
66
|
+
// Everything below should race against modal states.
|
|
67
|
+
if (this._includeSnapshot && this._context.currentTab())
|
|
68
|
+
this._tabSnapshot = await this._context.currentTabOrDie().captureSnapshot();
|
|
69
|
+
for (const tab of this._context.tabs())
|
|
70
|
+
await tab.updateTitle();
|
|
71
|
+
}
|
|
72
|
+
tabSnapshot() {
|
|
73
|
+
return this._tabSnapshot;
|
|
74
|
+
}
|
|
75
|
+
serialize() {
|
|
76
|
+
const response = [];
|
|
77
|
+
// Start with command result.
|
|
78
|
+
if (this._result.length) {
|
|
79
|
+
response.push('### Result');
|
|
80
|
+
response.push(this._result.join('\n'));
|
|
81
|
+
response.push('');
|
|
82
|
+
}
|
|
83
|
+
// Add code if it exists.
|
|
84
|
+
if (this._code.length) {
|
|
85
|
+
response.push(`### Ran Playwright code
|
|
86
|
+
\`\`\`js
|
|
87
|
+
${this._code.join('\n')}
|
|
88
|
+
\`\`\``);
|
|
89
|
+
response.push('');
|
|
90
|
+
}
|
|
91
|
+
// List browser tabs.
|
|
92
|
+
if (this._includeSnapshot || this._includeTabs)
|
|
93
|
+
response.push(...renderTabsMarkdown(this._context.tabs(), this._includeTabs));
|
|
94
|
+
// Add snapshot if provided.
|
|
95
|
+
if (this._tabSnapshot?.modalStates.length) {
|
|
96
|
+
response.push(...renderModalStates(this._context, this._tabSnapshot.modalStates));
|
|
97
|
+
response.push('');
|
|
98
|
+
}
|
|
99
|
+
else if (this._tabSnapshot) {
|
|
100
|
+
response.push(renderTabSnapshot(this._tabSnapshot));
|
|
101
|
+
response.push('');
|
|
102
|
+
}
|
|
103
|
+
// Main response part
|
|
104
|
+
const content = [
|
|
105
|
+
{ type: 'text', text: response.join('\n') },
|
|
106
|
+
];
|
|
107
|
+
// Image attachments.
|
|
108
|
+
if (this._context.config.imageResponses !== 'omit') {
|
|
109
|
+
for (const image of this._images)
|
|
110
|
+
content.push({ type: 'image', data: image.data.toString('base64'), mimeType: image.contentType });
|
|
111
|
+
}
|
|
112
|
+
return { content, isError: this._isError };
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
function renderTabSnapshot(tabSnapshot) {
|
|
116
|
+
const lines = [];
|
|
117
|
+
if (tabSnapshot.consoleMessages.length) {
|
|
118
|
+
lines.push(`### New console messages`);
|
|
119
|
+
for (const message of tabSnapshot.consoleMessages)
|
|
120
|
+
lines.push(`- ${trim(message.toString(), 100)}`);
|
|
121
|
+
lines.push('');
|
|
122
|
+
}
|
|
123
|
+
if (tabSnapshot.downloads.length) {
|
|
124
|
+
lines.push(`### Downloads`);
|
|
125
|
+
for (const entry of tabSnapshot.downloads) {
|
|
126
|
+
if (entry.finished)
|
|
127
|
+
lines.push(`- Downloaded file ${entry.download.suggestedFilename()} to ${entry.outputFile}`);
|
|
128
|
+
else
|
|
129
|
+
lines.push(`- Downloading file ${entry.download.suggestedFilename()} ...`);
|
|
130
|
+
}
|
|
131
|
+
lines.push('');
|
|
132
|
+
}
|
|
133
|
+
lines.push(`### Page state`);
|
|
134
|
+
lines.push(`- Page URL: ${tabSnapshot.url}`);
|
|
135
|
+
lines.push(`- Page Title: ${tabSnapshot.title}`);
|
|
136
|
+
lines.push(`- Page Snapshot:`);
|
|
137
|
+
lines.push('```yaml');
|
|
138
|
+
lines.push(tabSnapshot.ariaSnapshot);
|
|
139
|
+
lines.push('```');
|
|
140
|
+
return lines.join('\n');
|
|
141
|
+
}
|
|
142
|
+
function renderTabsMarkdown(tabs, force = false) {
|
|
143
|
+
if (tabs.length === 1 && !force)
|
|
144
|
+
return [];
|
|
145
|
+
if (!tabs.length) {
|
|
146
|
+
return [
|
|
147
|
+
'### Open tabs',
|
|
148
|
+
'No open tabs. Use the "browser_navigate" tool to navigate to a page first.',
|
|
149
|
+
'',
|
|
150
|
+
];
|
|
151
|
+
}
|
|
152
|
+
const lines = ['### Open tabs'];
|
|
153
|
+
for (let i = 0; i < tabs.length; i++) {
|
|
154
|
+
const tab = tabs[i];
|
|
155
|
+
const current = tab.isCurrentTab() ? ' (current)' : '';
|
|
156
|
+
lines.push(`- ${i}:${current} [${tab.lastTitle()}] (${tab.page.url()})`);
|
|
157
|
+
}
|
|
158
|
+
lines.push('');
|
|
159
|
+
return lines;
|
|
160
|
+
}
|
|
161
|
+
function trim(text, maxLength) {
|
|
162
|
+
if (text.length <= maxLength)
|
|
163
|
+
return text;
|
|
164
|
+
return text.slice(0, maxLength) + '...';
|
|
165
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
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 './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
|
+
}
|