@playwright/mcp 0.0.13 → 0.0.15

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.
@@ -14,133 +14,197 @@
14
14
  * See the License for the specific language governing permissions and
15
15
  * limitations under the License.
16
16
  */
17
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
18
+ if (k2 === undefined) k2 = k;
19
+ var desc = Object.getOwnPropertyDescriptor(m, k);
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
50
  Object.defineProperty(exports, "__esModule", { value: true });
18
51
  const zod_1 = require("zod");
19
- const zod_to_json_schema_1 = require("zod-to-json-schema");
20
- const screenshot = {
52
+ const tool_1 = require("./tool");
53
+ const javascript = __importStar(require("../javascript"));
54
+ const elementSchema = zod_1.z.object({
55
+ element: zod_1.z.string().describe('Human-readable element description used to obtain permission to interact with the element'),
56
+ });
57
+ const screenshot = (0, tool_1.defineTool)({
21
58
  capability: 'core',
22
59
  schema: {
23
60
  name: 'browser_screen_capture',
24
61
  description: 'Take a screenshot of the current page',
25
- inputSchema: (0, zod_to_json_schema_1.zodToJsonSchema)(zod_1.z.object({})),
62
+ inputSchema: zod_1.z.object({}),
26
63
  },
27
64
  handle: async (context) => {
28
65
  const tab = await context.ensureTab();
29
- const screenshot = await tab.page.screenshot({ type: 'jpeg', quality: 50, scale: 'css' });
66
+ const options = { type: 'jpeg', quality: 50, scale: 'css' };
67
+ const code = [
68
+ `// Take a screenshot of the current page`,
69
+ `await page.screenshot(${javascript.formatObject(options)});`,
70
+ ];
71
+ const action = () => tab.page.screenshot(options).then(buffer => {
72
+ return {
73
+ content: [{ type: 'image', data: buffer.toString('base64'), mimeType: 'image/jpeg' }],
74
+ };
75
+ });
30
76
  return {
31
- content: [{ type: 'image', data: screenshot.toString('base64'), mimeType: 'image/jpeg' }],
77
+ code,
78
+ action,
79
+ captureSnapshot: false,
80
+ waitForNetwork: false
32
81
  };
33
82
  },
34
- };
35
- const elementSchema = zod_1.z.object({
36
- element: zod_1.z.string().describe('Human-readable element description used to obtain permission to interact with the element'),
37
- });
38
- const moveMouseSchema = elementSchema.extend({
39
- x: zod_1.z.number().describe('X coordinate'),
40
- y: zod_1.z.number().describe('Y coordinate'),
41
83
  });
42
- const moveMouse = {
84
+ const moveMouse = (0, tool_1.defineTool)({
43
85
  capability: 'core',
44
86
  schema: {
45
87
  name: 'browser_screen_move_mouse',
46
88
  description: 'Move mouse to a given position',
47
- inputSchema: (0, zod_to_json_schema_1.zodToJsonSchema)(moveMouseSchema),
89
+ inputSchema: elementSchema.extend({
90
+ x: zod_1.z.number().describe('X coordinate'),
91
+ y: zod_1.z.number().describe('Y coordinate'),
92
+ }),
48
93
  },
49
94
  handle: async (context, params) => {
50
- const validatedParams = moveMouseSchema.parse(params);
51
- const tab = context.currentTab();
52
- await tab.page.mouse.move(validatedParams.x, validatedParams.y);
95
+ const tab = context.currentTabOrDie();
96
+ const code = [
97
+ `// Move mouse to (${params.x}, ${params.y})`,
98
+ `await page.mouse.move(${params.x}, ${params.y});`,
99
+ ];
100
+ const action = () => tab.page.mouse.move(params.x, params.y);
53
101
  return {
54
- content: [{ type: 'text', text: `Moved mouse to (${validatedParams.x}, ${validatedParams.y})` }],
102
+ code,
103
+ action,
104
+ captureSnapshot: false,
105
+ waitForNetwork: false
55
106
  };
56
107
  },
57
- };
58
- const clickSchema = elementSchema.extend({
59
- x: zod_1.z.number().describe('X coordinate'),
60
- y: zod_1.z.number().describe('Y coordinate'),
61
108
  });
62
- const click = {
109
+ const click = (0, tool_1.defineTool)({
63
110
  capability: 'core',
64
111
  schema: {
65
112
  name: 'browser_screen_click',
66
113
  description: 'Click left mouse button',
67
- inputSchema: (0, zod_to_json_schema_1.zodToJsonSchema)(clickSchema),
114
+ inputSchema: elementSchema.extend({
115
+ x: zod_1.z.number().describe('X coordinate'),
116
+ y: zod_1.z.number().describe('Y coordinate'),
117
+ }),
68
118
  },
69
119
  handle: async (context, params) => {
70
- return await context.currentTab().runAndWait(async (tab) => {
71
- const validatedParams = clickSchema.parse(params);
72
- const code = [
73
- `// Click mouse at coordinates (${validatedParams.x}, ${validatedParams.y})`,
74
- `await page.mouse.move(${validatedParams.x}, ${validatedParams.y});`,
75
- `await page.mouse.down();`,
76
- `await page.mouse.up();`,
77
- ];
78
- await tab.page.mouse.move(validatedParams.x, validatedParams.y);
120
+ const tab = context.currentTabOrDie();
121
+ const code = [
122
+ `// Click mouse at coordinates (${params.x}, ${params.y})`,
123
+ `await page.mouse.move(${params.x}, ${params.y});`,
124
+ `await page.mouse.down();`,
125
+ `await page.mouse.up();`,
126
+ ];
127
+ const action = async () => {
128
+ await tab.page.mouse.move(params.x, params.y);
79
129
  await tab.page.mouse.down();
80
130
  await tab.page.mouse.up();
81
- return { code };
82
- });
131
+ };
132
+ return {
133
+ code,
134
+ action,
135
+ captureSnapshot: false,
136
+ waitForNetwork: true,
137
+ };
83
138
  },
84
- };
85
- const dragSchema = elementSchema.extend({
86
- startX: zod_1.z.number().describe('Start X coordinate'),
87
- startY: zod_1.z.number().describe('Start Y coordinate'),
88
- endX: zod_1.z.number().describe('End X coordinate'),
89
- endY: zod_1.z.number().describe('End Y coordinate'),
90
139
  });
91
- const drag = {
140
+ const drag = (0, tool_1.defineTool)({
92
141
  capability: 'core',
93
142
  schema: {
94
143
  name: 'browser_screen_drag',
95
144
  description: 'Drag left mouse button',
96
- inputSchema: (0, zod_to_json_schema_1.zodToJsonSchema)(dragSchema),
145
+ inputSchema: elementSchema.extend({
146
+ startX: zod_1.z.number().describe('Start X coordinate'),
147
+ startY: zod_1.z.number().describe('Start Y coordinate'),
148
+ endX: zod_1.z.number().describe('End X coordinate'),
149
+ endY: zod_1.z.number().describe('End Y coordinate'),
150
+ }),
97
151
  },
98
152
  handle: async (context, params) => {
99
- const validatedParams = dragSchema.parse(params);
100
- return await context.currentTab().runAndWait(async (tab) => {
101
- await tab.page.mouse.move(validatedParams.startX, validatedParams.startY);
153
+ const tab = context.currentTabOrDie();
154
+ const code = [
155
+ `// Drag mouse from (${params.startX}, ${params.startY}) to (${params.endX}, ${params.endY})`,
156
+ `await page.mouse.move(${params.startX}, ${params.startY});`,
157
+ `await page.mouse.down();`,
158
+ `await page.mouse.move(${params.endX}, ${params.endY});`,
159
+ `await page.mouse.up();`,
160
+ ];
161
+ const action = async () => {
162
+ await tab.page.mouse.move(params.startX, params.startY);
102
163
  await tab.page.mouse.down();
103
- await tab.page.mouse.move(validatedParams.endX, validatedParams.endY);
164
+ await tab.page.mouse.move(params.endX, params.endY);
104
165
  await tab.page.mouse.up();
105
- const code = [
106
- `// Drag mouse from (${validatedParams.startX}, ${validatedParams.startY}) to (${validatedParams.endX}, ${validatedParams.endY})`,
107
- `await page.mouse.move(${validatedParams.startX}, ${validatedParams.startY});`,
108
- `await page.mouse.down();`,
109
- `await page.mouse.move(${validatedParams.endX}, ${validatedParams.endY});`,
110
- `await page.mouse.up();`,
111
- ];
112
- return { code };
113
- });
166
+ };
167
+ return {
168
+ code,
169
+ action,
170
+ captureSnapshot: false,
171
+ waitForNetwork: true,
172
+ };
114
173
  },
115
- };
116
- const typeSchema = zod_1.z.object({
117
- text: zod_1.z.string().describe('Text to type into the element'),
118
- submit: zod_1.z.boolean().optional().describe('Whether to submit entered text (press Enter after)'),
119
174
  });
120
- const type = {
175
+ const type = (0, tool_1.defineTool)({
121
176
  capability: 'core',
122
177
  schema: {
123
178
  name: 'browser_screen_type',
124
179
  description: 'Type text',
125
- inputSchema: (0, zod_to_json_schema_1.zodToJsonSchema)(typeSchema),
180
+ inputSchema: zod_1.z.object({
181
+ text: zod_1.z.string().describe('Text to type into the element'),
182
+ submit: zod_1.z.boolean().optional().describe('Whether to submit entered text (press Enter after)'),
183
+ }),
126
184
  },
127
185
  handle: async (context, params) => {
128
- const validatedParams = typeSchema.parse(params);
129
- return await context.currentTab().runAndWait(async (tab) => {
130
- const code = [
131
- `// Type ${validatedParams.text}`,
132
- `await page.keyboard.type('${validatedParams.text}');`,
133
- ];
134
- await tab.page.keyboard.type(validatedParams.text);
135
- if (validatedParams.submit) {
136
- code.push(`// Submit text`);
137
- code.push(`await page.keyboard.press('Enter');`);
186
+ const tab = context.currentTabOrDie();
187
+ const code = [
188
+ `// Type ${params.text}`,
189
+ `await page.keyboard.type('${params.text}');`,
190
+ ];
191
+ const action = async () => {
192
+ await tab.page.keyboard.type(params.text);
193
+ if (params.submit)
138
194
  await tab.page.keyboard.press('Enter');
139
- }
140
- return { code };
141
- });
195
+ };
196
+ if (params.submit) {
197
+ code.push(`// Submit text`);
198
+ code.push(`await page.keyboard.press('Enter');`);
199
+ }
200
+ return {
201
+ code,
202
+ action,
203
+ captureSnapshot: false,
204
+ waitForNetwork: true,
205
+ };
142
206
  },
143
- };
207
+ });
144
208
  exports.default = [
145
209
  screenshot,
146
210
  moveMouse,
@@ -51,175 +51,220 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
51
51
  return (mod && mod.__esModule) ? mod : { "default": mod };
52
52
  };
53
53
  Object.defineProperty(exports, "__esModule", { value: true });
54
+ const path_1 = __importDefault(require("path"));
55
+ const os_1 = __importDefault(require("os"));
54
56
  const zod_1 = require("zod");
55
- const zod_to_json_schema_1 = __importDefault(require("zod-to-json-schema"));
57
+ const utils_1 = require("./utils");
56
58
  const context_1 = require("../context");
57
59
  const javascript = __importStar(require("../javascript"));
58
- const snapshot = {
60
+ const tool_1 = require("./tool");
61
+ const snapshot = (0, tool_1.defineTool)({
59
62
  capability: 'core',
60
63
  schema: {
61
64
  name: 'browser_snapshot',
62
65
  description: 'Capture accessibility snapshot of the current page, this is better than screenshot',
63
- inputSchema: (0, zod_to_json_schema_1.default)(zod_1.z.object({})),
66
+ inputSchema: zod_1.z.object({}),
64
67
  },
65
68
  handle: async (context) => {
66
- const tab = await context.ensureTab();
67
- return await tab.run(async () => {
68
- const code = [`// <internal code to capture accessibility snapshot>`];
69
- return { code };
70
- }, { captureSnapshot: true });
69
+ await context.ensureTab();
70
+ return {
71
+ code: [`// <internal code to capture accessibility snapshot>`],
72
+ captureSnapshot: true,
73
+ waitForNetwork: false,
74
+ };
71
75
  },
72
- };
76
+ });
73
77
  const elementSchema = zod_1.z.object({
74
78
  element: zod_1.z.string().describe('Human-readable element description used to obtain permission to interact with the element'),
75
79
  ref: zod_1.z.string().describe('Exact target element reference from the page snapshot'),
76
80
  });
77
- const click = {
81
+ const click = (0, tool_1.defineTool)({
78
82
  capability: 'core',
79
83
  schema: {
80
84
  name: 'browser_click',
81
85
  description: 'Perform click on a web page',
82
- inputSchema: (0, zod_to_json_schema_1.default)(elementSchema),
86
+ inputSchema: elementSchema,
83
87
  },
84
88
  handle: async (context, params) => {
85
- const validatedParams = elementSchema.parse(params);
86
- return await context.currentTab().runAndWaitWithSnapshot(async (snapshot) => {
87
- const locator = snapshot.refLocator(validatedParams.ref);
88
- const code = [
89
- `// Click ${validatedParams.element}`,
90
- `await page.${await (0, context_1.generateLocator)(locator)}.click();`
91
- ];
92
- await locator.click();
93
- return { code };
94
- });
89
+ const tab = context.currentTabOrDie();
90
+ const locator = tab.snapshotOrDie().refLocator(params.ref);
91
+ const code = [
92
+ `// Click ${params.element}`,
93
+ `await page.${await (0, context_1.generateLocator)(locator)}.click();`
94
+ ];
95
+ return {
96
+ code,
97
+ action: () => locator.click(),
98
+ captureSnapshot: true,
99
+ waitForNetwork: true,
100
+ };
95
101
  },
96
- };
97
- const dragSchema = zod_1.z.object({
98
- startElement: zod_1.z.string().describe('Human-readable source element description used to obtain the permission to interact with the element'),
99
- startRef: zod_1.z.string().describe('Exact source element reference from the page snapshot'),
100
- endElement: zod_1.z.string().describe('Human-readable target element description used to obtain the permission to interact with the element'),
101
- endRef: zod_1.z.string().describe('Exact target element reference from the page snapshot'),
102
102
  });
103
- const drag = {
103
+ const drag = (0, tool_1.defineTool)({
104
104
  capability: 'core',
105
105
  schema: {
106
106
  name: 'browser_drag',
107
107
  description: 'Perform drag and drop between two elements',
108
- inputSchema: (0, zod_to_json_schema_1.default)(dragSchema),
108
+ inputSchema: zod_1.z.object({
109
+ startElement: zod_1.z.string().describe('Human-readable source element description used to obtain the permission to interact with the element'),
110
+ startRef: zod_1.z.string().describe('Exact source element reference from the page snapshot'),
111
+ endElement: zod_1.z.string().describe('Human-readable target element description used to obtain the permission to interact with the element'),
112
+ endRef: zod_1.z.string().describe('Exact target element reference from the page snapshot'),
113
+ }),
109
114
  },
110
115
  handle: async (context, params) => {
111
- const validatedParams = dragSchema.parse(params);
112
- return await context.currentTab().runAndWaitWithSnapshot(async (snapshot) => {
113
- const startLocator = snapshot.refLocator(validatedParams.startRef);
114
- const endLocator = snapshot.refLocator(validatedParams.endRef);
115
- const code = [
116
- `// Drag ${validatedParams.startElement} to ${validatedParams.endElement}`,
117
- `await page.${await (0, context_1.generateLocator)(startLocator)}.dragTo(page.${await (0, context_1.generateLocator)(endLocator)});`
118
- ];
119
- await startLocator.dragTo(endLocator);
120
- return { code };
121
- });
116
+ const snapshot = context.currentTabOrDie().snapshotOrDie();
117
+ const startLocator = snapshot.refLocator(params.startRef);
118
+ const endLocator = snapshot.refLocator(params.endRef);
119
+ const code = [
120
+ `// Drag ${params.startElement} to ${params.endElement}`,
121
+ `await page.${await (0, context_1.generateLocator)(startLocator)}.dragTo(page.${await (0, context_1.generateLocator)(endLocator)});`
122
+ ];
123
+ return {
124
+ code,
125
+ action: () => startLocator.dragTo(endLocator),
126
+ captureSnapshot: true,
127
+ waitForNetwork: true,
128
+ };
122
129
  },
123
- };
124
- const hover = {
130
+ });
131
+ const hover = (0, tool_1.defineTool)({
125
132
  capability: 'core',
126
133
  schema: {
127
134
  name: 'browser_hover',
128
135
  description: 'Hover over element on page',
129
- inputSchema: (0, zod_to_json_schema_1.default)(elementSchema),
136
+ inputSchema: elementSchema,
130
137
  },
131
138
  handle: async (context, params) => {
132
- const validatedParams = elementSchema.parse(params);
133
- return await context.currentTab().runAndWaitWithSnapshot(async (snapshot) => {
134
- const locator = snapshot.refLocator(validatedParams.ref);
135
- const code = [
136
- `// Hover over ${validatedParams.element}`,
137
- `await page.${await (0, context_1.generateLocator)(locator)}.hover();`
138
- ];
139
- await locator.hover();
140
- return { code };
141
- });
139
+ const snapshot = context.currentTabOrDie().snapshotOrDie();
140
+ const locator = snapshot.refLocator(params.ref);
141
+ const code = [
142
+ `// Hover over ${params.element}`,
143
+ `await page.${await (0, context_1.generateLocator)(locator)}.hover();`
144
+ ];
145
+ return {
146
+ code,
147
+ action: () => locator.hover(),
148
+ captureSnapshot: true,
149
+ waitForNetwork: true,
150
+ };
142
151
  },
143
- };
152
+ });
144
153
  const typeSchema = elementSchema.extend({
145
154
  text: zod_1.z.string().describe('Text to type into the element'),
146
155
  submit: zod_1.z.boolean().optional().describe('Whether to submit entered text (press Enter after)'),
147
156
  slowly: zod_1.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.'),
148
157
  });
149
- const type = {
158
+ const type = (0, tool_1.defineTool)({
150
159
  capability: 'core',
151
160
  schema: {
152
161
  name: 'browser_type',
153
162
  description: 'Type text into editable element',
154
- inputSchema: (0, zod_to_json_schema_1.default)(typeSchema),
163
+ inputSchema: typeSchema,
155
164
  },
156
165
  handle: async (context, params) => {
157
- const validatedParams = typeSchema.parse(params);
158
- return await context.currentTab().runAndWaitWithSnapshot(async (snapshot) => {
159
- const locator = snapshot.refLocator(validatedParams.ref);
160
- const code = [];
161
- if (validatedParams.slowly) {
162
- code.push(`// Press "${validatedParams.text}" sequentially into "${validatedParams.element}"`);
163
- code.push(`await page.${await (0, context_1.generateLocator)(locator)}.pressSequentially(${javascript.quote(validatedParams.text)});`);
164
- await locator.pressSequentially(validatedParams.text);
165
- }
166
- else {
167
- code.push(`// Fill "${validatedParams.text}" into "${validatedParams.element}"`);
168
- code.push(`await page.${await (0, context_1.generateLocator)(locator)}.fill(${javascript.quote(validatedParams.text)});`);
169
- await locator.fill(validatedParams.text);
170
- }
171
- if (validatedParams.submit) {
172
- code.push(`// Submit text`);
173
- code.push(`await page.${await (0, context_1.generateLocator)(locator)}.press('Enter');`);
174
- await locator.press('Enter');
175
- }
176
- return { code };
177
- });
166
+ const snapshot = context.currentTabOrDie().snapshotOrDie();
167
+ const locator = snapshot.refLocator(params.ref);
168
+ const code = [];
169
+ const steps = [];
170
+ if (params.slowly) {
171
+ code.push(`// Press "${params.text}" sequentially into "${params.element}"`);
172
+ code.push(`await page.${await (0, context_1.generateLocator)(locator)}.pressSequentially(${javascript.quote(params.text)});`);
173
+ steps.push(() => locator.pressSequentially(params.text));
174
+ }
175
+ else {
176
+ code.push(`// Fill "${params.text}" into "${params.element}"`);
177
+ code.push(`await page.${await (0, context_1.generateLocator)(locator)}.fill(${javascript.quote(params.text)});`);
178
+ steps.push(() => locator.fill(params.text));
179
+ }
180
+ if (params.submit) {
181
+ code.push(`// Submit text`);
182
+ code.push(`await page.${await (0, context_1.generateLocator)(locator)}.press('Enter');`);
183
+ steps.push(() => locator.press('Enter'));
184
+ }
185
+ return {
186
+ code,
187
+ action: () => steps.reduce((acc, step) => acc.then(step), Promise.resolve()),
188
+ captureSnapshot: true,
189
+ waitForNetwork: true,
190
+ };
178
191
  },
179
- };
192
+ });
180
193
  const selectOptionSchema = elementSchema.extend({
181
194
  values: zod_1.z.array(zod_1.z.string()).describe('Array of values to select in the dropdown. This can be a single value or multiple values.'),
182
195
  });
183
- const selectOption = {
196
+ const selectOption = (0, tool_1.defineTool)({
184
197
  capability: 'core',
185
198
  schema: {
186
199
  name: 'browser_select_option',
187
200
  description: 'Select an option in a dropdown',
188
- inputSchema: (0, zod_to_json_schema_1.default)(selectOptionSchema),
201
+ inputSchema: selectOptionSchema,
189
202
  },
190
203
  handle: async (context, params) => {
191
- const validatedParams = selectOptionSchema.parse(params);
192
- return await context.currentTab().runAndWaitWithSnapshot(async (snapshot) => {
193
- const locator = snapshot.refLocator(validatedParams.ref);
194
- const code = [
195
- `// Select options [${validatedParams.values.join(', ')}] in ${validatedParams.element}`,
196
- `await page.${await (0, context_1.generateLocator)(locator)}.selectOption(${javascript.formatObject(validatedParams.values)});`
197
- ];
198
- await locator.selectOption(validatedParams.values);
199
- return { code };
200
- });
204
+ const snapshot = context.currentTabOrDie().snapshotOrDie();
205
+ const locator = snapshot.refLocator(params.ref);
206
+ const code = [
207
+ `// Select options [${params.values.join(', ')}] in ${params.element}`,
208
+ `await page.${await (0, context_1.generateLocator)(locator)}.selectOption(${javascript.formatObject(params.values)});`
209
+ ];
210
+ return {
211
+ code,
212
+ action: () => locator.selectOption(params.values).then(() => { }),
213
+ captureSnapshot: true,
214
+ waitForNetwork: true,
215
+ };
201
216
  },
202
- };
217
+ });
203
218
  const screenshotSchema = zod_1.z.object({
204
219
  raw: zod_1.z.boolean().optional().describe('Whether to return without compression (in PNG format). Default is false, which returns a JPEG image.'),
220
+ element: zod_1.z.string().optional().describe('Human-readable element description used to obtain permission to screenshot the element. If not provided, the screenshot will be taken of viewport. If element is provided, ref must be provided too.'),
221
+ ref: zod_1.z.string().optional().describe('Exact target element reference from the page snapshot. If not provided, the screenshot will be taken of viewport. If ref is provided, element must be provided too.'),
222
+ }).refine(data => {
223
+ return !!data.element === !!data.ref;
224
+ }, {
225
+ message: 'Both element and ref must be provided or neither.',
226
+ path: ['ref', 'element']
205
227
  });
206
- const screenshot = {
228
+ const screenshot = (0, tool_1.defineTool)({
207
229
  capability: 'core',
208
230
  schema: {
209
231
  name: 'browser_take_screenshot',
210
232
  description: `Take a screenshot of the current page. You can't perform actions based on the screenshot, use browser_snapshot for actions.`,
211
- inputSchema: (0, zod_to_json_schema_1.default)(screenshotSchema),
233
+ inputSchema: screenshotSchema,
212
234
  },
213
235
  handle: async (context, params) => {
214
- const validatedParams = screenshotSchema.parse(params);
215
- const tab = context.currentTab();
216
- const options = validatedParams.raw ? { type: 'png', scale: 'css' } : { type: 'jpeg', quality: 50, scale: 'css' };
217
- const screenshot = await tab.page.screenshot(options);
236
+ const tab = context.currentTabOrDie();
237
+ const snapshot = tab.snapshotOrDie();
238
+ const fileType = params.raw ? 'png' : 'jpeg';
239
+ const fileName = path_1.default.join(os_1.default.tmpdir(), (0, utils_1.sanitizeForFilePath)(`page-${new Date().toISOString()}`)) + `.${fileType}`;
240
+ const options = { type: fileType, quality: fileType === 'png' ? undefined : 50, scale: 'css', path: fileName };
241
+ const isElementScreenshot = params.element && params.ref;
242
+ const code = [
243
+ `// Screenshot ${isElementScreenshot ? params.element : 'viewport'} and save it as ${fileName}`,
244
+ ];
245
+ const locator = params.ref ? snapshot.refLocator(params.ref) : null;
246
+ if (locator)
247
+ code.push(`await page.${await (0, context_1.generateLocator)(locator)}.screenshot(${javascript.formatObject(options)});`);
248
+ else
249
+ code.push(`await page.screenshot(${javascript.formatObject(options)});`);
250
+ const action = async () => {
251
+ const screenshot = locator ? await locator.screenshot(options) : await tab.page.screenshot(options);
252
+ return {
253
+ content: [{
254
+ type: 'image',
255
+ data: screenshot.toString('base64'),
256
+ mimeType: fileType === 'png' ? 'image/png' : 'image/jpeg',
257
+ }]
258
+ };
259
+ };
218
260
  return {
219
- content: [{ type: 'image', data: screenshot.toString('base64'), mimeType: validatedParams.raw ? 'image/png' : 'image/jpeg' }],
261
+ code,
262
+ action,
263
+ captureSnapshot: true,
264
+ waitForNetwork: false,
220
265
  };
221
- },
222
- };
266
+ }
267
+ });
223
268
  exports.default = [
224
269
  snapshot,
225
270
  click,