@playwright/mcp 0.0.7 → 0.0.10
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 +96 -75
- package/index.d.ts +7 -0
- package/lib/context.js +256 -86
- package/lib/index.js +32 -64
- package/lib/program.js +40 -5
- package/lib/resources/console.js +1 -1
- package/lib/server.js +2 -2
- package/lib/tools/common.js +8 -111
- package/lib/tools/files.js +44 -0
- package/lib/tools/install.js +59 -0
- package/lib/tools/keyboard.js +45 -0
- package/lib/tools/navigate.js +79 -0
- package/lib/tools/pdf.js +48 -0
- package/lib/tools/{screenshot.js → screen.js} +45 -29
- package/lib/tools/snapshot.js +60 -26
- package/lib/tools/tabs.js +100 -0
- package/lib/tools/utils.js +4 -25
- package/package.json +5 -4
package/lib/index.js
CHANGED
|
@@ -14,89 +14,57 @@
|
|
|
14
14
|
* See the License for the specific language governing permissions and
|
|
15
15
|
* limitations under the License.
|
|
16
16
|
*/
|
|
17
|
-
var
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
21
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
22
|
-
}
|
|
23
|
-
Object.defineProperty(o, k2, desc);
|
|
24
|
-
}) : (function(o, m, k, k2) {
|
|
25
|
-
if (k2 === undefined) k2 = k;
|
|
26
|
-
o[k2] = m[k];
|
|
27
|
-
}));
|
|
28
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
29
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
30
|
-
}) : function(o, v) {
|
|
31
|
-
o["default"] = v;
|
|
32
|
-
});
|
|
33
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
34
|
-
var ownKeys = function(o) {
|
|
35
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
36
|
-
var ar = [];
|
|
37
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
38
|
-
return ar;
|
|
39
|
-
};
|
|
40
|
-
return ownKeys(o);
|
|
41
|
-
};
|
|
42
|
-
return function (mod) {
|
|
43
|
-
if (mod && mod.__esModule) return mod;
|
|
44
|
-
var result = {};
|
|
45
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
46
|
-
__setModuleDefault(result, mod);
|
|
47
|
-
return result;
|
|
48
|
-
};
|
|
49
|
-
})();
|
|
17
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
18
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
19
|
+
};
|
|
50
20
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
51
21
|
exports.createServer = createServer;
|
|
52
22
|
const server_1 = require("./server");
|
|
53
|
-
const
|
|
54
|
-
const
|
|
55
|
-
const
|
|
23
|
+
const common_1 = __importDefault(require("./tools/common"));
|
|
24
|
+
const files_1 = __importDefault(require("./tools/files"));
|
|
25
|
+
const install_1 = __importDefault(require("./tools/install"));
|
|
26
|
+
const keyboard_1 = __importDefault(require("./tools/keyboard"));
|
|
27
|
+
const navigate_1 = __importDefault(require("./tools/navigate"));
|
|
28
|
+
const pdf_1 = __importDefault(require("./tools/pdf"));
|
|
29
|
+
const snapshot_1 = __importDefault(require("./tools/snapshot"));
|
|
30
|
+
const tabs_1 = __importDefault(require("./tools/tabs"));
|
|
31
|
+
const screen_1 = __importDefault(require("./tools/screen"));
|
|
56
32
|
const console_1 = require("./resources/console");
|
|
57
|
-
const commonTools = [
|
|
58
|
-
common.pressKey,
|
|
59
|
-
common.wait,
|
|
60
|
-
common.pdf,
|
|
61
|
-
common.close,
|
|
62
|
-
];
|
|
63
33
|
const snapshotTools = [
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
snapshot.selectOption,
|
|
73
|
-
snapshot.screenshot,
|
|
74
|
-
...commonTools,
|
|
34
|
+
...common_1.default,
|
|
35
|
+
...(0, files_1.default)(true),
|
|
36
|
+
...install_1.default,
|
|
37
|
+
...(0, keyboard_1.default)(true),
|
|
38
|
+
...(0, navigate_1.default)(true),
|
|
39
|
+
...pdf_1.default,
|
|
40
|
+
...snapshot_1.default,
|
|
41
|
+
...(0, tabs_1.default)(true),
|
|
75
42
|
];
|
|
76
43
|
const screenshotTools = [
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
screenshot.type,
|
|
86
|
-
...commonTools,
|
|
44
|
+
...common_1.default,
|
|
45
|
+
...(0, files_1.default)(false),
|
|
46
|
+
...install_1.default,
|
|
47
|
+
...(0, keyboard_1.default)(false),
|
|
48
|
+
...(0, navigate_1.default)(false),
|
|
49
|
+
...pdf_1.default,
|
|
50
|
+
...screen_1.default,
|
|
51
|
+
...(0, tabs_1.default)(false),
|
|
87
52
|
];
|
|
88
53
|
const resources = [
|
|
89
54
|
console_1.console,
|
|
90
55
|
];
|
|
91
56
|
const packageJSON = require('../package.json');
|
|
92
57
|
function createServer(options) {
|
|
93
|
-
const
|
|
58
|
+
const allTools = options?.vision ? screenshotTools : snapshotTools;
|
|
59
|
+
const tools = allTools.filter(tool => !options?.capabilities || tool.capability === 'core' || options.capabilities.includes(tool.capability));
|
|
94
60
|
return (0, server_1.createServerWithTools)({
|
|
95
61
|
name: 'Playwright',
|
|
96
62
|
version: packageJSON.version,
|
|
97
63
|
tools,
|
|
98
64
|
resources,
|
|
65
|
+
browserName: options?.browserName,
|
|
99
66
|
userDataDir: options?.userDataDir ?? '',
|
|
100
67
|
launchOptions: options?.launchOptions,
|
|
68
|
+
cdpEndpoint: options?.cdpEndpoint,
|
|
101
69
|
});
|
|
102
70
|
}
|
package/lib/program.js
CHANGED
|
@@ -32,20 +32,55 @@ const packageJSON = require('../package.json');
|
|
|
32
32
|
commander_1.program
|
|
33
33
|
.version('Version ' + packageJSON.version)
|
|
34
34
|
.name(packageJSON.name)
|
|
35
|
+
.option('--browser <browser>', 'Browser or chrome channel to use, possible values: chrome, firefox, webkit, msedge.')
|
|
36
|
+
.option('--caps <caps>', 'Comma-separated list of capabilities to enable, possible values: tabs, pdf, history, wait, files, install. Default is all.')
|
|
37
|
+
.option('--cdp-endpoint <endpoint>', 'CDP endpoint to connect to.')
|
|
38
|
+
.option('--executable-path <path>', 'Path to the browser executable.')
|
|
35
39
|
.option('--headless', 'Run browser in headless mode, headed by default')
|
|
40
|
+
.option('--port <port>', 'Port to listen on for SSE transport.')
|
|
36
41
|
.option('--user-data-dir <path>', 'Path to the user data directory')
|
|
37
42
|
.option('--vision', 'Run server that uses screenshots (Aria snapshots are used by default)')
|
|
38
|
-
.option('--port <port>', 'Port to listen on for SSE transport.')
|
|
39
43
|
.action(async (options) => {
|
|
44
|
+
let browserName;
|
|
45
|
+
let channel;
|
|
46
|
+
switch (options.browser) {
|
|
47
|
+
case 'chrome':
|
|
48
|
+
case 'chrome-beta':
|
|
49
|
+
case 'chrome-canary':
|
|
50
|
+
case 'chrome-dev':
|
|
51
|
+
case 'msedge':
|
|
52
|
+
case 'msedge-beta':
|
|
53
|
+
case 'msedge-canary':
|
|
54
|
+
case 'msedge-dev':
|
|
55
|
+
browserName = 'chromium';
|
|
56
|
+
channel = options.browser;
|
|
57
|
+
break;
|
|
58
|
+
case 'chromium':
|
|
59
|
+
browserName = 'chromium';
|
|
60
|
+
break;
|
|
61
|
+
case 'firefox':
|
|
62
|
+
browserName = 'firefox';
|
|
63
|
+
break;
|
|
64
|
+
case 'webkit':
|
|
65
|
+
browserName = 'webkit';
|
|
66
|
+
break;
|
|
67
|
+
default:
|
|
68
|
+
browserName = 'chromium';
|
|
69
|
+
channel = 'chrome';
|
|
70
|
+
}
|
|
40
71
|
const launchOptions = {
|
|
41
72
|
headless: !!options.headless,
|
|
42
|
-
channel
|
|
73
|
+
channel,
|
|
74
|
+
executablePath: options.executablePath,
|
|
43
75
|
};
|
|
44
|
-
const userDataDir = options.userDataDir ?? await createUserDataDir();
|
|
76
|
+
const userDataDir = options.userDataDir ?? await createUserDataDir(browserName);
|
|
45
77
|
const serverList = new server_1.ServerList(() => (0, index_1.createServer)({
|
|
78
|
+
browserName,
|
|
46
79
|
userDataDir,
|
|
47
80
|
launchOptions,
|
|
48
81
|
vision: !!options.vision,
|
|
82
|
+
cdpEndpoint: options.cdpEndpoint,
|
|
83
|
+
capabilities: options.caps?.split(',').map((c) => c.trim()),
|
|
49
84
|
}));
|
|
50
85
|
setupExitWatchdog(serverList);
|
|
51
86
|
if (options.port) {
|
|
@@ -64,7 +99,7 @@ function setupExitWatchdog(serverList) {
|
|
|
64
99
|
});
|
|
65
100
|
}
|
|
66
101
|
commander_1.program.parse(process.argv);
|
|
67
|
-
async function createUserDataDir() {
|
|
102
|
+
async function createUserDataDir(browserName) {
|
|
68
103
|
let cacheDirectory;
|
|
69
104
|
if (process.platform === 'linux')
|
|
70
105
|
cacheDirectory = process.env.XDG_CACHE_HOME || path_1.default.join(os_1.default.homedir(), '.cache');
|
|
@@ -74,7 +109,7 @@ async function createUserDataDir() {
|
|
|
74
109
|
cacheDirectory = process.env.LOCALAPPDATA || path_1.default.join(os_1.default.homedir(), 'AppData', 'Local');
|
|
75
110
|
else
|
|
76
111
|
throw new Error('Unsupported platform: ' + process.platform);
|
|
77
|
-
const result = path_1.default.join(cacheDirectory, 'ms-playwright',
|
|
112
|
+
const result = path_1.default.join(cacheDirectory, 'ms-playwright', `mcp-${browserName}-profile`);
|
|
78
113
|
await fs_1.default.promises.mkdir(result, { recursive: true });
|
|
79
114
|
return result;
|
|
80
115
|
}
|
package/lib/resources/console.js
CHANGED
|
@@ -23,7 +23,7 @@ exports.console = {
|
|
|
23
23
|
mimeType: 'text/plain',
|
|
24
24
|
},
|
|
25
25
|
read: async (context, uri) => {
|
|
26
|
-
const messages = await context.console();
|
|
26
|
+
const messages = await context.currentTab().console();
|
|
27
27
|
const log = messages.map(message => `[${message.type().toUpperCase()}] ${message.text()}`).join('\n');
|
|
28
28
|
return [{
|
|
29
29
|
uri,
|
package/lib/server.js
CHANGED
|
@@ -21,8 +21,8 @@ const index_js_1 = require("@modelcontextprotocol/sdk/server/index.js");
|
|
|
21
21
|
const types_js_1 = require("@modelcontextprotocol/sdk/types.js");
|
|
22
22
|
const context_1 = require("./context");
|
|
23
23
|
function createServerWithTools(options) {
|
|
24
|
-
const { name, version, tools, resources
|
|
25
|
-
const context = new context_1.Context(
|
|
24
|
+
const { name, version, tools, resources } = options;
|
|
25
|
+
const context = new context_1.Context(options);
|
|
26
26
|
const server = new index_js_1.Server({ name, version }, {
|
|
27
27
|
capabilities: {
|
|
28
28
|
tools: {},
|
package/lib/tools/common.js
CHANGED
|
@@ -14,70 +14,14 @@
|
|
|
14
14
|
* See the License for the specific language governing permissions and
|
|
15
15
|
* limitations under the License.
|
|
16
16
|
*/
|
|
17
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
18
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
19
|
-
};
|
|
20
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
21
|
-
exports.chooseFile = exports.close = exports.pdf = exports.pressKey = exports.wait = exports.goForward = exports.goBack = exports.navigate = void 0;
|
|
22
|
-
const os_1 = __importDefault(require("os"));
|
|
23
|
-
const path_1 = __importDefault(require("path"));
|
|
24
18
|
const zod_1 = require("zod");
|
|
25
19
|
const zod_to_json_schema_1 = require("zod-to-json-schema");
|
|
26
|
-
const utils_1 = require("./utils");
|
|
27
|
-
const navigateSchema = zod_1.z.object({
|
|
28
|
-
url: zod_1.z.string().describe('The URL to navigate to'),
|
|
29
|
-
});
|
|
30
|
-
const navigate = snapshot => ({
|
|
31
|
-
schema: {
|
|
32
|
-
name: 'browser_navigate',
|
|
33
|
-
description: 'Navigate to a URL',
|
|
34
|
-
inputSchema: (0, zod_to_json_schema_1.zodToJsonSchema)(navigateSchema),
|
|
35
|
-
},
|
|
36
|
-
handle: async (context, params) => {
|
|
37
|
-
const validatedParams = navigateSchema.parse(params);
|
|
38
|
-
const page = await context.createPage();
|
|
39
|
-
await page.goto(validatedParams.url, { waitUntil: 'domcontentloaded' });
|
|
40
|
-
// Cap load event to 5 seconds, the page is operational at this point.
|
|
41
|
-
await page.waitForLoadState('load', { timeout: 5000 }).catch(() => { });
|
|
42
|
-
if (snapshot)
|
|
43
|
-
return (0, utils_1.captureAriaSnapshot)(context);
|
|
44
|
-
return {
|
|
45
|
-
content: [{
|
|
46
|
-
type: 'text',
|
|
47
|
-
text: `Navigated to ${validatedParams.url}`,
|
|
48
|
-
}],
|
|
49
|
-
};
|
|
50
|
-
},
|
|
51
|
-
});
|
|
52
|
-
exports.navigate = navigate;
|
|
53
|
-
const goBackSchema = zod_1.z.object({});
|
|
54
|
-
const goBack = snapshot => ({
|
|
55
|
-
schema: {
|
|
56
|
-
name: 'browser_go_back',
|
|
57
|
-
description: 'Go back to the previous page',
|
|
58
|
-
inputSchema: (0, zod_to_json_schema_1.zodToJsonSchema)(goBackSchema),
|
|
59
|
-
},
|
|
60
|
-
handle: async (context) => {
|
|
61
|
-
return await (0, utils_1.runAndWait)(context, 'Navigated back', async (page) => page.goBack(), snapshot);
|
|
62
|
-
},
|
|
63
|
-
});
|
|
64
|
-
exports.goBack = goBack;
|
|
65
|
-
const goForwardSchema = zod_1.z.object({});
|
|
66
|
-
const goForward = snapshot => ({
|
|
67
|
-
schema: {
|
|
68
|
-
name: 'browser_go_forward',
|
|
69
|
-
description: 'Go forward to the next page',
|
|
70
|
-
inputSchema: (0, zod_to_json_schema_1.zodToJsonSchema)(goForwardSchema),
|
|
71
|
-
},
|
|
72
|
-
handle: async (context) => {
|
|
73
|
-
return await (0, utils_1.runAndWait)(context, 'Navigated forward', async (page) => page.goForward(), snapshot);
|
|
74
|
-
},
|
|
75
|
-
});
|
|
76
|
-
exports.goForward = goForward;
|
|
77
20
|
const waitSchema = zod_1.z.object({
|
|
78
21
|
time: zod_1.z.number().describe('The time to wait in seconds'),
|
|
79
22
|
});
|
|
80
|
-
|
|
23
|
+
const wait = {
|
|
24
|
+
capability: 'wait',
|
|
81
25
|
schema: {
|
|
82
26
|
name: 'browser_wait',
|
|
83
27
|
description: 'Wait for a specified time in seconds',
|
|
@@ -94,43 +38,9 @@ exports.wait = {
|
|
|
94
38
|
};
|
|
95
39
|
},
|
|
96
40
|
};
|
|
97
|
-
const pressKeySchema = zod_1.z.object({
|
|
98
|
-
key: zod_1.z.string().describe('Name of the key to press or a character to generate, such as `ArrowLeft` or `a`'),
|
|
99
|
-
});
|
|
100
|
-
exports.pressKey = {
|
|
101
|
-
schema: {
|
|
102
|
-
name: 'browser_press_key',
|
|
103
|
-
description: 'Press a key on the keyboard',
|
|
104
|
-
inputSchema: (0, zod_to_json_schema_1.zodToJsonSchema)(pressKeySchema),
|
|
105
|
-
},
|
|
106
|
-
handle: async (context, params) => {
|
|
107
|
-
const validatedParams = pressKeySchema.parse(params);
|
|
108
|
-
return await (0, utils_1.runAndWait)(context, `Pressed key ${validatedParams.key}`, async (page) => {
|
|
109
|
-
await page.keyboard.press(validatedParams.key);
|
|
110
|
-
});
|
|
111
|
-
},
|
|
112
|
-
};
|
|
113
|
-
const pdfSchema = zod_1.z.object({});
|
|
114
|
-
exports.pdf = {
|
|
115
|
-
schema: {
|
|
116
|
-
name: 'browser_save_as_pdf',
|
|
117
|
-
description: 'Save page as PDF',
|
|
118
|
-
inputSchema: (0, zod_to_json_schema_1.zodToJsonSchema)(pdfSchema),
|
|
119
|
-
},
|
|
120
|
-
handle: async (context) => {
|
|
121
|
-
const page = context.existingPage();
|
|
122
|
-
const fileName = path_1.default.join(os_1.default.tmpdir(), `/page-${new Date().toISOString()}.pdf`);
|
|
123
|
-
await page.pdf({ path: fileName });
|
|
124
|
-
return {
|
|
125
|
-
content: [{
|
|
126
|
-
type: 'text',
|
|
127
|
-
text: `Saved as ${fileName}`,
|
|
128
|
-
}],
|
|
129
|
-
};
|
|
130
|
-
},
|
|
131
|
-
};
|
|
132
41
|
const closeSchema = zod_1.z.object({});
|
|
133
|
-
|
|
42
|
+
const close = {
|
|
43
|
+
capability: 'core',
|
|
134
44
|
schema: {
|
|
135
45
|
name: 'browser_close',
|
|
136
46
|
description: 'Close the page',
|
|
@@ -146,20 +56,7 @@ exports.close = {
|
|
|
146
56
|
};
|
|
147
57
|
},
|
|
148
58
|
};
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
schema: {
|
|
154
|
-
name: 'browser_choose_file',
|
|
155
|
-
description: 'Choose one or multiple files to upload',
|
|
156
|
-
inputSchema: (0, zod_to_json_schema_1.zodToJsonSchema)(chooseFileSchema),
|
|
157
|
-
},
|
|
158
|
-
handle: async (context, params) => {
|
|
159
|
-
const validatedParams = chooseFileSchema.parse(params);
|
|
160
|
-
return await (0, utils_1.runAndWait)(context, `Chose files ${validatedParams.paths.join(', ')}`, async () => {
|
|
161
|
-
await context.submitFileChooser(validatedParams.paths);
|
|
162
|
-
}, snapshot);
|
|
163
|
-
},
|
|
164
|
-
});
|
|
165
|
-
exports.chooseFile = chooseFile;
|
|
59
|
+
exports.default = [
|
|
60
|
+
close,
|
|
61
|
+
wait,
|
|
62
|
+
];
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Copyright (c) Microsoft Corporation.
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
const zod_1 = require("zod");
|
|
19
|
+
const zod_to_json_schema_1 = require("zod-to-json-schema");
|
|
20
|
+
const uploadFileSchema = zod_1.z.object({
|
|
21
|
+
paths: zod_1.z.array(zod_1.z.string()).describe('The absolute paths to the files to upload. Can be a single file or multiple files.'),
|
|
22
|
+
});
|
|
23
|
+
const uploadFile = captureSnapshot => ({
|
|
24
|
+
capability: 'files',
|
|
25
|
+
schema: {
|
|
26
|
+
name: 'browser_file_upload',
|
|
27
|
+
description: 'Upload one or multiple files',
|
|
28
|
+
inputSchema: (0, zod_to_json_schema_1.zodToJsonSchema)(uploadFileSchema),
|
|
29
|
+
},
|
|
30
|
+
handle: async (context, params) => {
|
|
31
|
+
const validatedParams = uploadFileSchema.parse(params);
|
|
32
|
+
const tab = context.currentTab();
|
|
33
|
+
return await tab.runAndWait(async () => {
|
|
34
|
+
await tab.submitFileChooser(validatedParams.paths);
|
|
35
|
+
}, {
|
|
36
|
+
status: `Chose files ${validatedParams.paths.join(', ')}`,
|
|
37
|
+
captureSnapshot,
|
|
38
|
+
noClearFileChooser: true,
|
|
39
|
+
});
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
exports.default = (captureSnapshot) => [
|
|
43
|
+
uploadFile(captureSnapshot),
|
|
44
|
+
];
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Copyright (c) Microsoft Corporation.
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
18
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
19
|
+
};
|
|
20
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
21
|
+
const child_process_1 = require("child_process");
|
|
22
|
+
const path_1 = __importDefault(require("path"));
|
|
23
|
+
const zod_1 = require("zod");
|
|
24
|
+
const zod_to_json_schema_1 = require("zod-to-json-schema");
|
|
25
|
+
const install = {
|
|
26
|
+
capability: 'install',
|
|
27
|
+
schema: {
|
|
28
|
+
name: 'browser_install',
|
|
29
|
+
description: 'Install the browser specified in the config. Call this if you get an error about the browser not being installed.',
|
|
30
|
+
inputSchema: (0, zod_to_json_schema_1.zodToJsonSchema)(zod_1.z.object({})),
|
|
31
|
+
},
|
|
32
|
+
handle: async (context) => {
|
|
33
|
+
const channel = context.options.launchOptions?.channel ?? context.options.browserName ?? 'chrome';
|
|
34
|
+
const cli = path_1.default.join(require.resolve('playwright/package.json'), '..', 'cli.js');
|
|
35
|
+
const child = (0, child_process_1.fork)(cli, ['install', channel], {
|
|
36
|
+
stdio: 'pipe',
|
|
37
|
+
});
|
|
38
|
+
const output = [];
|
|
39
|
+
child.stdout?.on('data', data => output.push(data.toString()));
|
|
40
|
+
child.stderr?.on('data', data => output.push(data.toString()));
|
|
41
|
+
await new Promise((resolve, reject) => {
|
|
42
|
+
child.on('close', code => {
|
|
43
|
+
if (code === 0)
|
|
44
|
+
resolve();
|
|
45
|
+
else
|
|
46
|
+
reject(new Error(`Failed to install browser: ${output.join('')}`));
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
return {
|
|
50
|
+
content: [{
|
|
51
|
+
type: 'text',
|
|
52
|
+
text: `Browser ${channel} installed`,
|
|
53
|
+
}],
|
|
54
|
+
};
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
exports.default = [
|
|
58
|
+
install,
|
|
59
|
+
];
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Copyright (c) Microsoft Corporation.
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
18
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
19
|
+
};
|
|
20
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
21
|
+
const zod_1 = require("zod");
|
|
22
|
+
const zod_to_json_schema_1 = __importDefault(require("zod-to-json-schema"));
|
|
23
|
+
const pressKeySchema = zod_1.z.object({
|
|
24
|
+
key: zod_1.z.string().describe('Name of the key to press or a character to generate, such as `ArrowLeft` or `a`'),
|
|
25
|
+
});
|
|
26
|
+
const pressKey = captureSnapshot => ({
|
|
27
|
+
capability: 'core',
|
|
28
|
+
schema: {
|
|
29
|
+
name: 'browser_press_key',
|
|
30
|
+
description: 'Press a key on the keyboard',
|
|
31
|
+
inputSchema: (0, zod_to_json_schema_1.default)(pressKeySchema),
|
|
32
|
+
},
|
|
33
|
+
handle: async (context, params) => {
|
|
34
|
+
const validatedParams = pressKeySchema.parse(params);
|
|
35
|
+
return await context.currentTab().runAndWait(async (tab) => {
|
|
36
|
+
await tab.page.keyboard.press(validatedParams.key);
|
|
37
|
+
}, {
|
|
38
|
+
status: `Pressed key ${validatedParams.key}`,
|
|
39
|
+
captureSnapshot,
|
|
40
|
+
});
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
exports.default = (captureSnapshot) => [
|
|
44
|
+
pressKey(captureSnapshot),
|
|
45
|
+
];
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Copyright (c) Microsoft Corporation.
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
const zod_1 = require("zod");
|
|
19
|
+
const zod_to_json_schema_1 = require("zod-to-json-schema");
|
|
20
|
+
const navigateSchema = zod_1.z.object({
|
|
21
|
+
url: zod_1.z.string().describe('The URL to navigate to'),
|
|
22
|
+
});
|
|
23
|
+
const navigate = captureSnapshot => ({
|
|
24
|
+
capability: 'core',
|
|
25
|
+
schema: {
|
|
26
|
+
name: 'browser_navigate',
|
|
27
|
+
description: 'Navigate to a URL',
|
|
28
|
+
inputSchema: (0, zod_to_json_schema_1.zodToJsonSchema)(navigateSchema),
|
|
29
|
+
},
|
|
30
|
+
handle: async (context, params) => {
|
|
31
|
+
const validatedParams = navigateSchema.parse(params);
|
|
32
|
+
const currentTab = await context.ensureTab();
|
|
33
|
+
return await currentTab.run(async (tab) => {
|
|
34
|
+
await tab.navigate(validatedParams.url);
|
|
35
|
+
}, {
|
|
36
|
+
status: `Navigated to ${validatedParams.url}`,
|
|
37
|
+
captureSnapshot,
|
|
38
|
+
});
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
const goBackSchema = zod_1.z.object({});
|
|
42
|
+
const goBack = snapshot => ({
|
|
43
|
+
capability: 'history',
|
|
44
|
+
schema: {
|
|
45
|
+
name: 'browser_navigate_back',
|
|
46
|
+
description: 'Go back to the previous page',
|
|
47
|
+
inputSchema: (0, zod_to_json_schema_1.zodToJsonSchema)(goBackSchema),
|
|
48
|
+
},
|
|
49
|
+
handle: async (context) => {
|
|
50
|
+
return await context.currentTab().runAndWait(async (tab) => {
|
|
51
|
+
await tab.page.goBack();
|
|
52
|
+
}, {
|
|
53
|
+
status: 'Navigated back',
|
|
54
|
+
captureSnapshot: snapshot,
|
|
55
|
+
});
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
const goForwardSchema = zod_1.z.object({});
|
|
59
|
+
const goForward = snapshot => ({
|
|
60
|
+
capability: 'history',
|
|
61
|
+
schema: {
|
|
62
|
+
name: 'browser_navigate_forward',
|
|
63
|
+
description: 'Go forward to the next page',
|
|
64
|
+
inputSchema: (0, zod_to_json_schema_1.zodToJsonSchema)(goForwardSchema),
|
|
65
|
+
},
|
|
66
|
+
handle: async (context) => {
|
|
67
|
+
return await context.currentTab().runAndWait(async (tab) => {
|
|
68
|
+
await tab.page.goForward();
|
|
69
|
+
}, {
|
|
70
|
+
status: 'Navigated forward',
|
|
71
|
+
captureSnapshot: snapshot,
|
|
72
|
+
});
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
exports.default = (captureSnapshot) => [
|
|
76
|
+
navigate(captureSnapshot),
|
|
77
|
+
goBack(captureSnapshot),
|
|
78
|
+
goForward(captureSnapshot),
|
|
79
|
+
];
|
package/lib/tools/pdf.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Copyright (c) Microsoft Corporation.
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
18
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
19
|
+
};
|
|
20
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
21
|
+
const os_1 = __importDefault(require("os"));
|
|
22
|
+
const path_1 = __importDefault(require("path"));
|
|
23
|
+
const zod_1 = require("zod");
|
|
24
|
+
const zod_to_json_schema_1 = require("zod-to-json-schema");
|
|
25
|
+
const utils_1 = require("./utils");
|
|
26
|
+
const pdfSchema = zod_1.z.object({});
|
|
27
|
+
const pdf = {
|
|
28
|
+
capability: 'pdf',
|
|
29
|
+
schema: {
|
|
30
|
+
name: 'browser_pdf_save',
|
|
31
|
+
description: 'Save page as PDF',
|
|
32
|
+
inputSchema: (0, zod_to_json_schema_1.zodToJsonSchema)(pdfSchema),
|
|
33
|
+
},
|
|
34
|
+
handle: async (context) => {
|
|
35
|
+
const tab = context.currentTab();
|
|
36
|
+
const fileName = path_1.default.join(os_1.default.tmpdir(), (0, utils_1.sanitizeForFilePath)(`page-${new Date().toISOString()}`)) + '.pdf';
|
|
37
|
+
await tab.page.pdf({ path: fileName });
|
|
38
|
+
return {
|
|
39
|
+
content: [{
|
|
40
|
+
type: 'text',
|
|
41
|
+
text: `Saved as ${fileName}`,
|
|
42
|
+
}],
|
|
43
|
+
};
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
exports.default = [
|
|
47
|
+
pdf,
|
|
48
|
+
];
|