@playwright/mcp 0.0.36-alpha-2025-09-03 → 0.0.36-alpha-2025-09-04

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.
Files changed (68) hide show
  1. package/cli.js +1 -1
  2. package/index.d.ts +1 -1
  3. package/index.js +2 -2
  4. package/lib/{browserContextFactory.js → browser/browserContextFactory.js} +76 -36
  5. package/lib/{browserServerBackend.js → browser/browserServerBackend.js} +24 -22
  6. package/lib/{utils → browser}/codegen.js +8 -3
  7. package/lib/{config.js → browser/config.js} +43 -26
  8. package/lib/{context.js → browser/context.js} +27 -30
  9. package/lib/{response.js → browser/response.js} +14 -14
  10. package/lib/{sessionLog.js → browser/sessionLog.js} +23 -18
  11. package/lib/{tab.js → browser/tab.js} +29 -27
  12. package/lib/{tools → browser/tools}/common.js +11 -9
  13. package/lib/{tools → browser/tools}/console.js +7 -5
  14. package/lib/{tools → browser/tools}/dialogs.js +9 -7
  15. package/lib/browser/tools/evaluate.js +88 -0
  16. package/lib/{tools → browser/tools}/files.js +8 -6
  17. package/lib/browser/tools/form.js +92 -0
  18. package/lib/{tools → browser/tools}/install.js +18 -14
  19. package/lib/browser/tools/keyboard.js +113 -0
  20. package/lib/{tools → browser/tools}/mouse.js +18 -16
  21. package/lib/{tools → browser/tools}/navigate.js +10 -8
  22. package/lib/{tools → browser/tools}/network.js +7 -5
  23. package/lib/browser/tools/pdf.js +76 -0
  24. package/lib/browser/tools/screenshot.js +115 -0
  25. package/lib/browser/tools/snapshot.js +175 -0
  26. package/lib/{tools → browser/tools}/tabs.js +9 -7
  27. package/lib/{tools → browser/tools}/tool.js +6 -2
  28. package/lib/{tools → browser/tools}/utils.js +10 -5
  29. package/lib/{tools → browser/tools}/verify.js +59 -24
  30. package/lib/{tools → browser/tools}/wait.js +10 -8
  31. package/lib/browser/tools.js +61 -0
  32. package/lib/extension/cdpRelay.js +85 -48
  33. package/lib/extension/extensionContextFactory.js +48 -11
  34. package/lib/extension/protocol.js +4 -1
  35. package/lib/index.js +47 -12
  36. package/lib/{utils/log.js → log.js} +11 -4
  37. package/lib/{utils/package.js → package.js} +9 -5
  38. package/lib/program.js +68 -39
  39. package/lib/sdk/bundle.js +79 -0
  40. package/lib/{mcp → sdk}/http.js +57 -17
  41. package/lib/{mcp → sdk}/inProcessTransport.js +15 -20
  42. package/lib/{mcp → sdk}/manualPromise.js +11 -9
  43. package/lib/{mcp → sdk}/mdb.js +77 -38
  44. package/lib/{mcp → sdk}/proxyBackend.js +53 -16
  45. package/lib/sdk/server.js +164 -0
  46. package/lib/{mcp → sdk}/tool.js +8 -4
  47. package/lib/vscode/host.js +64 -35
  48. package/lib/vscode/main.js +48 -13
  49. package/package.json +6 -7
  50. package/lib/loop/loop.js +0 -69
  51. package/lib/loop/loopClaude.js +0 -152
  52. package/lib/loop/loopOpenAI.js +0 -141
  53. package/lib/loop/main.js +0 -60
  54. package/lib/loopTools/context.js +0 -67
  55. package/lib/loopTools/main.js +0 -54
  56. package/lib/loopTools/perform.js +0 -32
  57. package/lib/loopTools/snapshot.js +0 -29
  58. package/lib/loopTools/tool.js +0 -18
  59. package/lib/mcp/server.js +0 -123
  60. package/lib/tools/evaluate.js +0 -53
  61. package/lib/tools/form.js +0 -57
  62. package/lib/tools/keyboard.js +0 -78
  63. package/lib/tools/pdf.js +0 -40
  64. package/lib/tools/screenshot.js +0 -79
  65. package/lib/tools/snapshot.js +0 -139
  66. package/lib/tools.js +0 -54
  67. package/lib/utils/fileUtils.js +0 -36
  68. package/lib/utils/guid.js +0 -22
package/lib/loop/main.js DELETED
@@ -1,60 +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
- /* eslint-disable no-console */
17
- import path from 'path';
18
- import url from 'url';
19
- import dotenv from 'dotenv';
20
- import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js';
21
- import { Client } from '@modelcontextprotocol/sdk/client/index.js';
22
- import { program } from 'commander';
23
- import { OpenAIDelegate } from './loopOpenAI.js';
24
- import { ClaudeDelegate } from './loopClaude.js';
25
- import { runTask } from './loop.js';
26
- dotenv.config();
27
- const __filename = url.fileURLToPath(import.meta.url);
28
- async function run(delegate) {
29
- const transport = new StdioClientTransport({
30
- command: 'node',
31
- args: [
32
- path.resolve(__filename, '../../../cli.js'),
33
- '--save-session',
34
- '--output-dir', path.resolve(__filename, '../../../sessions')
35
- ],
36
- stderr: 'inherit',
37
- env: process.env,
38
- });
39
- const client = new Client({ name: 'test', version: '1.0.0' });
40
- await client.connect(transport);
41
- await client.ping();
42
- for (const task of tasks) {
43
- const messages = await runTask(delegate, client, task);
44
- for (const message of messages)
45
- console.log(`${message.role}: ${message.content}`);
46
- }
47
- await client.close();
48
- }
49
- const tasks = [
50
- 'Open https://playwright.dev/',
51
- ];
52
- program
53
- .option('--model <model>', 'model to use')
54
- .action(async (options) => {
55
- if (options.model === 'claude')
56
- await run(new ClaudeDelegate());
57
- else
58
- await run(new OpenAIDelegate());
59
- });
60
- void program.parseAsync(process.argv);
@@ -1,67 +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 { Client } from '@modelcontextprotocol/sdk/client/index.js';
17
- import { contextFactory } from '../browserContextFactory.js';
18
- import { BrowserServerBackend } from '../browserServerBackend.js';
19
- import { Context as BrowserContext } from '../context.js';
20
- import { runTask } from '../loop/loop.js';
21
- import { OpenAIDelegate } from '../loop/loopOpenAI.js';
22
- import { ClaudeDelegate } from '../loop/loopClaude.js';
23
- import { InProcessTransport } from '../mcp/inProcessTransport.js';
24
- import * as mcpServer from '../mcp/server.js';
25
- import { packageJSON } from '../utils/package.js';
26
- export class Context {
27
- config;
28
- _client;
29
- _delegate;
30
- constructor(config, client) {
31
- this.config = config;
32
- this._client = client;
33
- if (process.env.OPENAI_API_KEY)
34
- this._delegate = new OpenAIDelegate();
35
- else if (process.env.ANTHROPIC_API_KEY)
36
- this._delegate = new ClaudeDelegate();
37
- else
38
- throw new Error('No LLM API key found. Please set OPENAI_API_KEY or ANTHROPIC_API_KEY environment variable.');
39
- }
40
- static async create(config) {
41
- const client = new Client({ name: 'Playwright Proxy', version: packageJSON.version });
42
- const browserContextFactory = contextFactory(config);
43
- const server = mcpServer.createServer('Playwright Subagent', packageJSON.version, new BrowserServerBackend(config, browserContextFactory), false);
44
- await client.connect(new InProcessTransport(server));
45
- await client.ping();
46
- return new Context(config, client);
47
- }
48
- async runTask(task, oneShot = false) {
49
- const messages = await runTask(this._delegate, this._client, task, oneShot);
50
- const lines = [];
51
- // Skip the first message, which is the user's task.
52
- for (const message of messages.slice(1)) {
53
- // Trim out all page snapshots.
54
- if (!message.content.trim())
55
- continue;
56
- const index = oneShot ? -1 : message.content.indexOf('### Page state');
57
- const trimmedContent = index === -1 ? message.content : message.content.substring(0, index);
58
- lines.push(`[${message.role}]:`, trimmedContent);
59
- }
60
- return {
61
- content: [{ type: 'text', text: lines.join('\n') }],
62
- };
63
- }
64
- async close() {
65
- await BrowserContext.disposeAll();
66
- }
67
- }
@@ -1,54 +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 dotenv from 'dotenv';
17
- import * as mcpServer from '../mcp/server.js';
18
- import { packageJSON } from '../utils/package.js';
19
- import { Context } from './context.js';
20
- import { perform } from './perform.js';
21
- import { snapshot } from './snapshot.js';
22
- import { toMcpTool } from '../mcp/tool.js';
23
- export async function runLoopTools(config) {
24
- dotenv.config();
25
- const serverBackendFactory = {
26
- name: 'Playwright',
27
- nameInConfig: 'playwright-loop',
28
- version: packageJSON.version,
29
- create: () => new LoopToolsServerBackend(config)
30
- };
31
- await mcpServer.start(serverBackendFactory, config.server);
32
- }
33
- class LoopToolsServerBackend {
34
- _config;
35
- _context;
36
- _tools = [perform, snapshot];
37
- constructor(config) {
38
- this._config = config;
39
- }
40
- async initialize() {
41
- this._context = await Context.create(this._config);
42
- }
43
- async listTools() {
44
- return this._tools.map(tool => toMcpTool(tool.schema));
45
- }
46
- async callTool(name, args) {
47
- const tool = this._tools.find(tool => tool.schema.name === name);
48
- const parsedArguments = tool.schema.inputSchema.parse(args || {});
49
- return await tool.handle(this._context, parsedArguments);
50
- }
51
- serverClosed() {
52
- void this._context.close();
53
- }
54
- }
@@ -1,32 +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 { z } from 'zod';
17
- import { defineTool } from './tool.js';
18
- const performSchema = z.object({
19
- task: z.string().describe('The task to perform with the browser'),
20
- });
21
- export const perform = defineTool({
22
- schema: {
23
- name: 'browser_perform',
24
- title: 'Perform a task with the browser',
25
- description: 'Perform a task with the browser. It can click, type, export, capture screenshot, drag, hover, select options, etc.',
26
- inputSchema: performSchema,
27
- type: 'destructive',
28
- },
29
- handle: async (context, params) => {
30
- return await context.runTask(params.task);
31
- },
32
- });
@@ -1,29 +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 { z } from 'zod';
17
- import { defineTool } from './tool.js';
18
- export const snapshot = defineTool({
19
- schema: {
20
- name: 'browser_snapshot',
21
- title: 'Take a snapshot of the browser',
22
- description: 'Take a snapshot of the browser to read what is on the page.',
23
- inputSchema: z.object({}),
24
- type: 'readOnly',
25
- },
26
- handle: async (context, params) => {
27
- return await context.runTask('Capture browser snapshot', true);
28
- },
29
- });
@@ -1,18 +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
- export function defineTool(tool) {
17
- return tool;
18
- }
package/lib/mcp/server.js DELETED
@@ -1,123 +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 debug from 'debug';
17
- import { Server } from '@modelcontextprotocol/sdk/server/index.js';
18
- import { CallToolRequestSchema, ListToolsRequestSchema } from '@modelcontextprotocol/sdk/types.js';
19
- import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
20
- import { httpAddressToString, installHttpTransport, startHttpServer } from './http.js';
21
- import { InProcessTransport } from './inProcessTransport.js';
22
- const serverDebug = debug('pw:mcp:server');
23
- const errorsDebug = debug('pw:mcp:errors');
24
- export async function connect(factory, transport, runHeartbeat) {
25
- const server = createServer(factory.name, factory.version, factory.create(), runHeartbeat);
26
- await server.connect(transport);
27
- }
28
- export async function wrapInProcess(backend) {
29
- const server = createServer('Internal', '0.0.0', backend, false);
30
- return new InProcessTransport(server);
31
- }
32
- export function createServer(name, version, backend, runHeartbeat) {
33
- let initializedPromiseResolve = () => { };
34
- const initializedPromise = new Promise(resolve => initializedPromiseResolve = resolve);
35
- const server = new Server({ name, version }, {
36
- capabilities: {
37
- tools: {},
38
- }
39
- });
40
- server.setRequestHandler(ListToolsRequestSchema, async () => {
41
- serverDebug('listTools');
42
- await initializedPromise;
43
- const tools = await backend.listTools();
44
- return { tools };
45
- });
46
- let heartbeatRunning = false;
47
- server.setRequestHandler(CallToolRequestSchema, async (request) => {
48
- serverDebug('callTool', request);
49
- await initializedPromise;
50
- if (runHeartbeat && !heartbeatRunning) {
51
- heartbeatRunning = true;
52
- startHeartbeat(server);
53
- }
54
- try {
55
- return await backend.callTool(request.params.name, request.params.arguments || {});
56
- }
57
- catch (error) {
58
- return {
59
- content: [{ type: 'text', text: '### Result\n' + String(error) }],
60
- isError: true,
61
- };
62
- }
63
- });
64
- addServerListener(server, 'initialized', async () => {
65
- try {
66
- const capabilities = server.getClientCapabilities();
67
- let clientRoots = [];
68
- if (capabilities?.roots) {
69
- const { roots } = await server.listRoots(undefined, { timeout: 2_000 }).catch(() => ({ roots: [] }));
70
- clientRoots = roots;
71
- }
72
- const clientVersion = server.getClientVersion() ?? { name: 'unknown', version: 'unknown' };
73
- await backend.initialize?.(server, clientVersion, clientRoots);
74
- initializedPromiseResolve();
75
- }
76
- catch (e) {
77
- errorsDebug(e);
78
- }
79
- });
80
- addServerListener(server, 'close', () => backend.serverClosed?.(server));
81
- return server;
82
- }
83
- const startHeartbeat = (server) => {
84
- const beat = () => {
85
- Promise.race([
86
- server.ping(),
87
- new Promise((_, reject) => setTimeout(() => reject(new Error('ping timeout')), 5000)),
88
- ]).then(() => {
89
- setTimeout(beat, 3000);
90
- }).catch(() => {
91
- void server.close();
92
- });
93
- };
94
- beat();
95
- };
96
- function addServerListener(server, event, listener) {
97
- const oldListener = server[`on${event}`];
98
- server[`on${event}`] = () => {
99
- oldListener?.();
100
- listener();
101
- };
102
- }
103
- export async function start(serverBackendFactory, options) {
104
- if (options.port === undefined) {
105
- await connect(serverBackendFactory, new StdioServerTransport(), false);
106
- return;
107
- }
108
- const httpServer = await startHttpServer(options);
109
- await installHttpTransport(httpServer, serverBackendFactory);
110
- const url = httpAddressToString(httpServer.address());
111
- const mcpConfig = { mcpServers: {} };
112
- mcpConfig.mcpServers[serverBackendFactory.nameInConfig] = {
113
- url: `${url}/mcp`
114
- };
115
- const message = [
116
- `Listening on ${url}`,
117
- 'Put this in your client config:',
118
- JSON.stringify(mcpConfig, undefined, 2),
119
- 'For legacy SSE transport support, you can use the /sse endpoint instead.',
120
- ].join('\n');
121
- // eslint-disable-next-line no-console
122
- console.error(message);
123
- }
@@ -1,53 +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 { z } from 'zod';
17
- import { defineTabTool } from './tool.js';
18
- import * as javascript from '../utils/codegen.js';
19
- import { generateLocator } from './utils.js';
20
- const evaluateSchema = z.object({
21
- function: z.string().describe('() => { /* code */ } or (element) => { /* code */ } when element is provided'),
22
- element: z.string().optional().describe('Human-readable element description used to obtain permission to interact with the element'),
23
- ref: z.string().optional().describe('Exact target element reference from the page snapshot'),
24
- });
25
- const evaluate = defineTabTool({
26
- capability: 'core',
27
- schema: {
28
- name: 'browser_evaluate',
29
- title: 'Evaluate JavaScript',
30
- description: 'Evaluate JavaScript expression on page or element',
31
- inputSchema: evaluateSchema,
32
- type: 'destructive',
33
- },
34
- handle: async (tab, params, response) => {
35
- response.setIncludeSnapshot();
36
- let locator;
37
- if (params.ref && params.element) {
38
- locator = await tab.refLocator({ ref: params.ref, element: params.element });
39
- response.addCode(`await page.${await generateLocator(locator)}.evaluate(${javascript.quote(params.function)});`);
40
- }
41
- else {
42
- response.addCode(`await page.evaluate(${javascript.quote(params.function)});`);
43
- }
44
- await tab.waitForCompletion(async () => {
45
- const receiver = locator ?? tab.page;
46
- const result = await receiver._evaluateFunction(params.function);
47
- response.addResult(JSON.stringify(result, null, 2) || 'undefined');
48
- });
49
- },
50
- });
51
- export default [
52
- evaluate,
53
- ];
package/lib/tools/form.js DELETED
@@ -1,57 +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 { z } from 'zod';
17
- import { defineTabTool } from './tool.js';
18
- import { generateLocator } from './utils.js';
19
- import * as javascript from '../utils/codegen.js';
20
- const fillForm = defineTabTool({
21
- capability: 'core',
22
- schema: {
23
- name: 'browser_fill_form',
24
- title: 'Fill form',
25
- description: 'Fill multiple form fields',
26
- inputSchema: z.object({
27
- fields: z.array(z.object({
28
- name: z.string().describe('Human-readable field name'),
29
- type: z.enum(['textbox', 'checkbox', 'radio', 'combobox', 'slider']).describe('Type of the field'),
30
- ref: z.string().describe('Exact target field reference from the page snapshot'),
31
- value: z.string().describe('Value to fill in the field. If the field is a checkbox, the value should be `true` or `false`. If the field is a combobox, the value should be the text of the option.'),
32
- })).describe('Fields to fill in'),
33
- }),
34
- type: 'destructive',
35
- },
36
- handle: async (tab, params, response) => {
37
- for (const field of params.fields) {
38
- const locator = await tab.refLocator({ element: field.name, ref: field.ref });
39
- const locatorSource = `await page.${await generateLocator(locator)}`;
40
- if (field.type === 'textbox' || field.type === 'slider') {
41
- await locator.fill(field.value);
42
- response.addCode(`${locatorSource}.fill(${javascript.quote(field.value)});`);
43
- }
44
- else if (field.type === 'checkbox' || field.type === 'radio') {
45
- await locator.setChecked(field.value === 'true');
46
- response.addCode(`${locatorSource}.setChecked(${javascript.quote(field.value)});`);
47
- }
48
- else if (field.type === 'combobox') {
49
- await locator.selectOption({ label: field.value });
50
- response.addCode(`${locatorSource}.selectOption(${javascript.quote(field.value)});`);
51
- }
52
- }
53
- },
54
- });
55
- export default [
56
- fillForm,
57
- ];
@@ -1,78 +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 { z } from 'zod';
17
- import { defineTabTool } from './tool.js';
18
- import { elementSchema } from './snapshot.js';
19
- import { generateLocator } from './utils.js';
20
- import * as javascript from '../utils/codegen.js';
21
- const pressKey = defineTabTool({
22
- capability: 'core',
23
- schema: {
24
- name: 'browser_press_key',
25
- title: 'Press a key',
26
- description: 'Press a key on the keyboard',
27
- inputSchema: z.object({
28
- key: z.string().describe('Name of the key to press or a character to generate, such as `ArrowLeft` or `a`'),
29
- }),
30
- type: 'destructive',
31
- },
32
- handle: async (tab, params, response) => {
33
- response.setIncludeSnapshot();
34
- response.addCode(`// Press ${params.key}`);
35
- response.addCode(`await page.keyboard.press('${params.key}');`);
36
- await tab.waitForCompletion(async () => {
37
- await tab.page.keyboard.press(params.key);
38
- });
39
- },
40
- });
41
- const typeSchema = elementSchema.extend({
42
- text: z.string().describe('Text to type into the element'),
43
- submit: z.boolean().optional().describe('Whether to submit entered text (press Enter after)'),
44
- slowly: z.boolean().optional().describe('Whether to type one character at a time. Useful for triggering key handlers in the page. By default entire text is filled in at once.'),
45
- });
46
- const type = defineTabTool({
47
- capability: 'core',
48
- schema: {
49
- name: 'browser_type',
50
- title: 'Type text',
51
- description: 'Type text into editable element',
52
- inputSchema: typeSchema,
53
- type: 'destructive',
54
- },
55
- handle: async (tab, params, response) => {
56
- const locator = await tab.refLocator(params);
57
- await tab.waitForCompletion(async () => {
58
- if (params.slowly) {
59
- response.setIncludeSnapshot();
60
- response.addCode(`await page.${await generateLocator(locator)}.pressSequentially(${javascript.quote(params.text)});`);
61
- await locator.pressSequentially(params.text);
62
- }
63
- else {
64
- response.addCode(`await page.${await generateLocator(locator)}.fill(${javascript.quote(params.text)});`);
65
- await locator.fill(params.text);
66
- }
67
- if (params.submit) {
68
- response.setIncludeSnapshot();
69
- response.addCode(`await page.${await generateLocator(locator)}.press('Enter');`);
70
- await locator.press('Enter');
71
- }
72
- });
73
- },
74
- });
75
- export default [
76
- pressKey,
77
- type,
78
- ];
package/lib/tools/pdf.js DELETED
@@ -1,40 +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 { z } from 'zod';
17
- import { defineTabTool } from './tool.js';
18
- import * as javascript from '../utils/codegen.js';
19
- const pdfSchema = z.object({
20
- filename: z.string().optional().describe('File name to save the pdf to. Defaults to `page-{timestamp}.pdf` if not specified.'),
21
- });
22
- const pdf = defineTabTool({
23
- capability: 'pdf',
24
- schema: {
25
- name: 'browser_pdf_save',
26
- title: 'Save as PDF',
27
- description: 'Save page as PDF',
28
- inputSchema: pdfSchema,
29
- type: 'readOnly',
30
- },
31
- handle: async (tab, params, response) => {
32
- const fileName = await tab.context.outputFile(params.filename ?? `page-${new Date().toISOString()}.pdf`);
33
- response.addCode(`await page.pdf(${javascript.formatObject({ path: fileName })});`);
34
- response.addResult(`Saved page as ${fileName}`);
35
- await tab.page.pdf({ path: fileName });
36
- },
37
- });
38
- export default [
39
- pdf,
40
- ];