@playwright/mcp 0.0.32 → 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 +2 -2
- package/lib/browserContextFactory.js +64 -54
- package/lib/browserServerBackend.js +81 -14
- package/lib/config.js +8 -8
- package/lib/context.js +75 -27
- package/lib/extension/cdpRelay.js +26 -50
- package/lib/extension/extensionContextFactory.js +56 -0
- package/lib/extension/main.js +7 -14
- package/lib/index.js +4 -2
- package/lib/loopTools/context.js +2 -2
- package/lib/mcp/server.js +9 -4
- package/lib/program.js +9 -8
- package/lib/response.js +81 -14
- package/lib/sessionLog.js +85 -34
- package/lib/tab.js +53 -50
- package/lib/tools/common.js +0 -1
- package/lib/tools/files.js +0 -1
- package/lib/tools/keyboard.js +0 -2
- package/lib/tools/navigate.js +0 -3
- package/lib/tools/pdf.js +1 -3
- package/lib/tools/screenshot.js +12 -9
- package/lib/tools/snapshot.js +2 -7
- package/lib/tools/tool.js +5 -4
- package/lib/tools/utils.js +0 -7
- package/lib/utils.js +26 -0
- package/package.json +7 -8
|
@@ -0,0 +1,56 @@
|
|
|
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 debug from 'debug';
|
|
17
|
+
import * as playwright from 'playwright';
|
|
18
|
+
import { startHttpServer } from '../httpServer.js';
|
|
19
|
+
import { CDPRelayServer } from './cdpRelay.js';
|
|
20
|
+
const debugLogger = debug('pw:mcp:relay');
|
|
21
|
+
export class ExtensionContextFactory {
|
|
22
|
+
name = 'extension';
|
|
23
|
+
description = 'Connect to a browser using the Playwright MCP extension';
|
|
24
|
+
_browserChannel;
|
|
25
|
+
_userDataDir;
|
|
26
|
+
constructor(browserChannel, userDataDir) {
|
|
27
|
+
this._browserChannel = browserChannel;
|
|
28
|
+
this._userDataDir = userDataDir;
|
|
29
|
+
}
|
|
30
|
+
async createContext(clientInfo, abortSignal) {
|
|
31
|
+
const browser = await this._obtainBrowser(clientInfo, abortSignal);
|
|
32
|
+
return {
|
|
33
|
+
browserContext: browser.contexts()[0],
|
|
34
|
+
close: async () => {
|
|
35
|
+
debugLogger('close() called for browser context');
|
|
36
|
+
await browser.close();
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
async _obtainBrowser(clientInfo, abortSignal) {
|
|
41
|
+
const relay = await this._startRelay(abortSignal);
|
|
42
|
+
await relay.ensureExtensionConnectionForMCPContext(clientInfo, abortSignal);
|
|
43
|
+
return await playwright.chromium.connectOverCDP(relay.cdpEndpoint());
|
|
44
|
+
}
|
|
45
|
+
async _startRelay(abortSignal) {
|
|
46
|
+
const httpServer = await startHttpServer({});
|
|
47
|
+
if (abortSignal.aborted) {
|
|
48
|
+
httpServer.close();
|
|
49
|
+
throw new Error(abortSignal.reason);
|
|
50
|
+
}
|
|
51
|
+
const cdpRelayServer = new CDPRelayServer(httpServer, this._browserChannel, this._userDataDir);
|
|
52
|
+
abortSignal.addEventListener('abort', () => cdpRelayServer.stop());
|
|
53
|
+
debugLogger(`CDP relay server started, extension endpoint: ${cdpRelayServer.extensionEndpoint()}.`);
|
|
54
|
+
return cdpRelayServer;
|
|
55
|
+
}
|
|
56
|
+
}
|
package/lib/extension/main.js
CHANGED
|
@@ -13,21 +13,14 @@
|
|
|
13
13
|
* See the License for the specific language governing permissions and
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
|
-
import {
|
|
16
|
+
import { ExtensionContextFactory } from './extensionContextFactory.js';
|
|
17
17
|
import { BrowserServerBackend } from '../browserServerBackend.js';
|
|
18
18
|
import * as mcpTransport from '../mcp/transport.js';
|
|
19
|
-
export async function runWithExtension(config
|
|
20
|
-
const contextFactory =
|
|
21
|
-
|
|
22
|
-
const serverBackendFactory = () => {
|
|
23
|
-
if (backend)
|
|
24
|
-
throw new Error('Another MCP client is still connected. Only one connection at a time is allowed.');
|
|
25
|
-
backend = new BrowserServerBackend(config, contextFactory);
|
|
26
|
-
backend.onclose = () => {
|
|
27
|
-
contextFactory.clientDisconnected();
|
|
28
|
-
backend = undefined;
|
|
29
|
-
};
|
|
30
|
-
return backend;
|
|
31
|
-
};
|
|
19
|
+
export async function runWithExtension(config) {
|
|
20
|
+
const contextFactory = new ExtensionContextFactory(config.browser.launchOptions.channel || 'chrome', config.browser.userDataDir);
|
|
21
|
+
const serverBackendFactory = () => new BrowserServerBackend(config, [contextFactory]);
|
|
32
22
|
await mcpTransport.start(serverBackendFactory, config.server);
|
|
33
23
|
}
|
|
24
|
+
export function createExtensionContextFactory(config) {
|
|
25
|
+
return new ExtensionContextFactory(config.browser.launchOptions.channel || 'chrome', config.browser.userDataDir);
|
|
26
|
+
}
|
package/lib/index.js
CHANGED
|
@@ -19,10 +19,12 @@ import { contextFactory } from './browserContextFactory.js';
|
|
|
19
19
|
import * as mcpServer from './mcp/server.js';
|
|
20
20
|
export async function createConnection(userConfig = {}, contextGetter) {
|
|
21
21
|
const config = await resolveConfig(userConfig);
|
|
22
|
-
const factory = contextGetter ? new SimpleBrowserContextFactory(contextGetter) : contextFactory(config
|
|
23
|
-
return mcpServer.createServer(new BrowserServerBackend(config, factory), false);
|
|
22
|
+
const factory = contextGetter ? new SimpleBrowserContextFactory(contextGetter) : contextFactory(config);
|
|
23
|
+
return mcpServer.createServer(new BrowserServerBackend(config, [factory]), false);
|
|
24
24
|
}
|
|
25
25
|
class SimpleBrowserContextFactory {
|
|
26
|
+
name = 'custom';
|
|
27
|
+
description = 'Connect to a browser using a custom context getter';
|
|
26
28
|
_contextGetter;
|
|
27
29
|
constructor(contextGetter) {
|
|
28
30
|
this._contextGetter = contextGetter;
|
package/lib/loopTools/context.js
CHANGED
|
@@ -38,8 +38,8 @@ export class Context {
|
|
|
38
38
|
}
|
|
39
39
|
static async create(config) {
|
|
40
40
|
const client = new Client({ name: 'Playwright Proxy', version: '1.0.0' });
|
|
41
|
-
const browserContextFactory = contextFactory(config
|
|
42
|
-
const server = mcpServer.createServer(new BrowserServerBackend(config, browserContextFactory), false);
|
|
41
|
+
const browserContextFactory = contextFactory(config);
|
|
42
|
+
const server = mcpServer.createServer(new BrowserServerBackend(config, [browserContextFactory]), false);
|
|
43
43
|
await client.connect(new InProcessTransport(server));
|
|
44
44
|
await client.ping();
|
|
45
45
|
return new Context(config, client);
|
package/lib/mcp/server.js
CHANGED
|
@@ -16,13 +16,15 @@
|
|
|
16
16
|
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
|
17
17
|
import { CallToolRequestSchema, ListToolsRequestSchema } from '@modelcontextprotocol/sdk/types.js';
|
|
18
18
|
import { zodToJsonSchema } from 'zod-to-json-schema';
|
|
19
|
+
import { ManualPromise } from '../manualPromise.js';
|
|
20
|
+
import { logUnhandledError } from '../log.js';
|
|
19
21
|
export async function connect(serverBackendFactory, transport, runHeartbeat) {
|
|
20
22
|
const backend = serverBackendFactory();
|
|
21
|
-
await backend.initialize?.();
|
|
22
23
|
const server = createServer(backend, runHeartbeat);
|
|
23
24
|
await server.connect(transport);
|
|
24
25
|
}
|
|
25
26
|
export function createServer(backend, runHeartbeat) {
|
|
27
|
+
const initializedPromise = new ManualPromise();
|
|
26
28
|
const server = new Server({ name: backend.name, version: backend.version }, {
|
|
27
29
|
capabilities: {
|
|
28
30
|
tools: {},
|
|
@@ -44,17 +46,18 @@ export function createServer(backend, runHeartbeat) {
|
|
|
44
46
|
});
|
|
45
47
|
let heartbeatRunning = false;
|
|
46
48
|
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
49
|
+
await initializedPromise;
|
|
47
50
|
if (runHeartbeat && !heartbeatRunning) {
|
|
48
51
|
heartbeatRunning = true;
|
|
49
52
|
startHeartbeat(server);
|
|
50
53
|
}
|
|
51
54
|
const errorResult = (...messages) => ({
|
|
52
|
-
content: [{ type: 'text', text: messages.join('\n') }],
|
|
55
|
+
content: [{ type: 'text', text: '### Result\n' + messages.join('\n') }],
|
|
53
56
|
isError: true,
|
|
54
57
|
});
|
|
55
58
|
const tool = tools.find(tool => tool.name === request.params.name);
|
|
56
59
|
if (!tool)
|
|
57
|
-
return errorResult(`Tool "${request.params.name}" not found`);
|
|
60
|
+
return errorResult(`Error: Tool "${request.params.name}" not found`);
|
|
58
61
|
try {
|
|
59
62
|
return await backend.callTool(tool, tool.inputSchema.parse(request.params.arguments || {}));
|
|
60
63
|
}
|
|
@@ -62,7 +65,9 @@ export function createServer(backend, runHeartbeat) {
|
|
|
62
65
|
return errorResult(String(error));
|
|
63
66
|
}
|
|
64
67
|
});
|
|
65
|
-
addServerListener(server, 'initialized', () =>
|
|
68
|
+
addServerListener(server, 'initialized', () => {
|
|
69
|
+
backend.initialize?.(server).then(() => initializedPromise.resolve()).catch(logUnhandledError);
|
|
70
|
+
});
|
|
66
71
|
addServerListener(server, 'close', () => backend.serverClosed?.());
|
|
67
72
|
return server;
|
|
68
73
|
}
|
package/lib/program.js
CHANGED
|
@@ -19,7 +19,7 @@ import { startTraceViewerServer } from 'playwright-core/lib/server';
|
|
|
19
19
|
import * as mcpTransport from './mcp/transport.js';
|
|
20
20
|
import { commaSeparatedList, resolveCLIConfig, semicolonSeparatedList } from './config.js';
|
|
21
21
|
import { packageJSON } from './package.js';
|
|
22
|
-
import { runWithExtension } from './extension/main.js';
|
|
22
|
+
import { createExtensionContextFactory, runWithExtension } from './extension/main.js';
|
|
23
23
|
import { BrowserServerBackend } from './browserServerBackend.js';
|
|
24
24
|
import { Context } from './context.js';
|
|
25
25
|
import { contextFactory } from './browserContextFactory.js';
|
|
@@ -53,10 +53,11 @@ program
|
|
|
53
53
|
.option('--user-data-dir <path>', 'path to the user data directory. If not specified, a temporary directory will be created.')
|
|
54
54
|
.option('--viewport-size <size>', 'specify browser viewport size in pixels, for example "1280, 720"')
|
|
55
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())
|
|
56
57
|
.addOption(new Option('--loop-tools', 'Run loop tools').hideHelp())
|
|
57
58
|
.addOption(new Option('--vision', 'Legacy option, use --caps=vision instead').hideHelp())
|
|
58
59
|
.action(async (options) => {
|
|
59
|
-
|
|
60
|
+
setupExitWatchdog();
|
|
60
61
|
if (options.vision) {
|
|
61
62
|
// eslint-disable-next-line no-console
|
|
62
63
|
console.error('The --vision option is deprecated, use --caps=vision instead');
|
|
@@ -64,15 +65,18 @@ program
|
|
|
64
65
|
}
|
|
65
66
|
const config = await resolveCLIConfig(options);
|
|
66
67
|
if (options.extension) {
|
|
67
|
-
await runWithExtension(config
|
|
68
|
+
await runWithExtension(config);
|
|
68
69
|
return;
|
|
69
70
|
}
|
|
70
71
|
if (options.loopTools) {
|
|
71
72
|
await runLoopTools(config);
|
|
72
73
|
return;
|
|
73
74
|
}
|
|
74
|
-
const browserContextFactory = contextFactory(config
|
|
75
|
-
const
|
|
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);
|
|
76
80
|
await mcpTransport.start(serverBackendFactory, config.server);
|
|
77
81
|
if (config.saveTrace) {
|
|
78
82
|
const server = await startTraceViewerServer();
|
|
@@ -83,20 +87,17 @@ program
|
|
|
83
87
|
}
|
|
84
88
|
});
|
|
85
89
|
function setupExitWatchdog() {
|
|
86
|
-
const abortController = new AbortController();
|
|
87
90
|
let isExiting = false;
|
|
88
91
|
const handleExit = async () => {
|
|
89
92
|
if (isExiting)
|
|
90
93
|
return;
|
|
91
94
|
isExiting = true;
|
|
92
95
|
setTimeout(() => process.exit(0), 15000);
|
|
93
|
-
abortController.abort('Process exiting');
|
|
94
96
|
await Context.disposeAll();
|
|
95
97
|
process.exit(0);
|
|
96
98
|
};
|
|
97
99
|
process.stdin.on('close', handleExit);
|
|
98
100
|
process.on('SIGINT', handleExit);
|
|
99
101
|
process.on('SIGTERM', handleExit);
|
|
100
|
-
return abortController;
|
|
101
102
|
}
|
|
102
103
|
void program.parseAsync(process.argv);
|
package/lib/response.js
CHANGED
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
* See the License for the specific language governing permissions and
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
|
+
import { renderModalStates } from './tab.js';
|
|
16
17
|
export class Response {
|
|
17
18
|
_result = [];
|
|
18
19
|
_code = [];
|
|
@@ -20,9 +21,10 @@ export class Response {
|
|
|
20
21
|
_context;
|
|
21
22
|
_includeSnapshot = false;
|
|
22
23
|
_includeTabs = false;
|
|
23
|
-
|
|
24
|
+
_tabSnapshot;
|
|
24
25
|
toolName;
|
|
25
26
|
toolArgs;
|
|
27
|
+
_isError;
|
|
26
28
|
constructor(context, toolName, toolArgs) {
|
|
27
29
|
this._context = context;
|
|
28
30
|
this.toolName = toolName;
|
|
@@ -31,6 +33,13 @@ export class Response {
|
|
|
31
33
|
addResult(result) {
|
|
32
34
|
this._result.push(result);
|
|
33
35
|
}
|
|
36
|
+
addError(error) {
|
|
37
|
+
this._result.push(error);
|
|
38
|
+
this._isError = true;
|
|
39
|
+
}
|
|
40
|
+
isError() {
|
|
41
|
+
return this._isError;
|
|
42
|
+
}
|
|
34
43
|
result() {
|
|
35
44
|
return this._result.join('\n');
|
|
36
45
|
}
|
|
@@ -52,16 +61,18 @@ export class Response {
|
|
|
52
61
|
setIncludeTabs() {
|
|
53
62
|
this._includeTabs = true;
|
|
54
63
|
}
|
|
55
|
-
async
|
|
56
|
-
|
|
57
|
-
|
|
64
|
+
async finish() {
|
|
65
|
+
// All the async snapshotting post-action is happening here.
|
|
66
|
+
// Everything below should race against modal states.
|
|
58
67
|
if (this._includeSnapshot && this._context.currentTab())
|
|
59
|
-
this.
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
return this._snapshot;
|
|
68
|
+
this._tabSnapshot = await this._context.currentTabOrDie().captureSnapshot();
|
|
69
|
+
for (const tab of this._context.tabs())
|
|
70
|
+
await tab.updateTitle();
|
|
63
71
|
}
|
|
64
|
-
|
|
72
|
+
tabSnapshot() {
|
|
73
|
+
return this._tabSnapshot;
|
|
74
|
+
}
|
|
75
|
+
serialize() {
|
|
65
76
|
const response = [];
|
|
66
77
|
// Start with command result.
|
|
67
78
|
if (this._result.length) {
|
|
@@ -79,11 +90,16 @@ ${this._code.join('\n')}
|
|
|
79
90
|
}
|
|
80
91
|
// List browser tabs.
|
|
81
92
|
if (this._includeSnapshot || this._includeTabs)
|
|
82
|
-
response.push(...(
|
|
93
|
+
response.push(...renderTabsMarkdown(this._context.tabs(), this._includeTabs));
|
|
83
94
|
// Add snapshot if provided.
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
response.push(
|
|
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
|
+
}
|
|
87
103
|
// Main response part
|
|
88
104
|
const content = [
|
|
89
105
|
{ type: 'text', text: response.join('\n') },
|
|
@@ -93,6 +109,57 @@ ${this._code.join('\n')}
|
|
|
93
109
|
for (const image of this._images)
|
|
94
110
|
content.push({ type: 'image', data: image.data.toString('base64'), mimeType: image.contentType });
|
|
95
111
|
}
|
|
96
|
-
return { content };
|
|
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
|
+
];
|
|
97
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) + '...';
|
|
98
165
|
}
|
package/lib/sessionLog.js
CHANGED
|
@@ -15,56 +15,107 @@
|
|
|
15
15
|
*/
|
|
16
16
|
import fs from 'fs';
|
|
17
17
|
import path from 'path';
|
|
18
|
+
import { logUnhandledError } from './log.js';
|
|
18
19
|
import { outputFile } from './config.js';
|
|
19
|
-
let sessionOrdinal = 0;
|
|
20
20
|
export class SessionLog {
|
|
21
21
|
_folder;
|
|
22
22
|
_file;
|
|
23
23
|
_ordinal = 0;
|
|
24
|
+
_pendingEntries = [];
|
|
25
|
+
_sessionFileQueue = Promise.resolve();
|
|
26
|
+
_flushEntriesTimeout;
|
|
24
27
|
constructor(sessionFolder) {
|
|
25
28
|
this._folder = sessionFolder;
|
|
26
29
|
this._file = path.join(this._folder, 'session.md');
|
|
27
30
|
}
|
|
28
|
-
static async create(config) {
|
|
29
|
-
const sessionFolder = await outputFile(config, `session-${
|
|
31
|
+
static async create(config, rootPath) {
|
|
32
|
+
const sessionFolder = await outputFile(config, rootPath, `session-${Date.now()}`);
|
|
30
33
|
await fs.promises.mkdir(sessionFolder, { recursive: true });
|
|
31
34
|
// eslint-disable-next-line no-console
|
|
32
35
|
console.error(`Session: ${sessionFolder}`);
|
|
33
36
|
return new SessionLog(sessionFolder);
|
|
34
37
|
}
|
|
35
|
-
|
|
36
|
-
const
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
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
|
+
}
|
|
50
61
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
|
|
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;
|
|
56
67
|
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
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('', '');
|
|
61
118
|
}
|
|
62
|
-
|
|
63
|
-
await fs.promises.appendFile(this._file, lines.join('\n'));
|
|
119
|
+
this._sessionFileQueue = this._sessionFileQueue.then(() => fs.promises.appendFile(this._file, lines.join('\n')));
|
|
64
120
|
}
|
|
65
121
|
}
|
|
66
|
-
function extension(contentType) {
|
|
67
|
-
if (contentType === 'image/jpeg')
|
|
68
|
-
return 'jpg';
|
|
69
|
-
return 'png';
|
|
70
|
-
}
|