|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2026 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import type {Dialog} from '../../third_party/index.js'; |
| 8 | +import {zod} from '../../third_party/index.js'; |
| 9 | +import {ToolCategory} from '../categories.js'; |
| 10 | +import type {ToolDefinition} from '../ToolDefinition.js'; |
| 11 | +import {defineTool} from '../ToolDefinition.js'; |
| 12 | + |
| 13 | +export const screenshot = defineTool({ |
| 14 | + name: 'screenshot', |
| 15 | + description: `Take page screenshot`, |
| 16 | + annotations: { |
| 17 | + category: ToolCategory.DEBUGGING, |
| 18 | + // Not read-only due to filePath param. |
| 19 | + readOnlyHint: false, |
| 20 | + }, |
| 21 | + schema: {}, |
| 22 | + handler: async (request, response, context) => { |
| 23 | + const page = context.getSelectedPage(); |
| 24 | + const screenshot = await page.screenshot({ |
| 25 | + type: 'png', |
| 26 | + optimizeForSpeed: true, // Bonus: optimize encoding for speed |
| 27 | + }); |
| 28 | + const {filename} = await context.saveTemporaryFile(screenshot, `image/png`); |
| 29 | + response.appendResponseLine(filename); |
| 30 | + }, |
| 31 | +}); |
| 32 | + |
| 33 | +export const navigatePage = defineTool({ |
| 34 | + name: 'navigate', |
| 35 | + description: `Load URL in the browser`, |
| 36 | + annotations: { |
| 37 | + category: ToolCategory.NAVIGATION, |
| 38 | + readOnlyHint: false, |
| 39 | + }, |
| 40 | + schema: { |
| 41 | + url: zod.string().describe('Page URL'), |
| 42 | + }, |
| 43 | + handler: async (request, response, context) => { |
| 44 | + const page = context.getSelectedPage(); |
| 45 | + const options = { |
| 46 | + timeout: 30_000, |
| 47 | + }; |
| 48 | + |
| 49 | + const dialogHandler = (dialog: Dialog) => { |
| 50 | + if (dialog.type() === 'beforeunload') { |
| 51 | + response.appendResponseLine(`Accepted a beforeunload dialog.`); |
| 52 | + void dialog.accept(); |
| 53 | + // We are not going to report the dialog like regular dialogs. |
| 54 | + context.clearDialog(); |
| 55 | + } |
| 56 | + }; |
| 57 | + |
| 58 | + page.on('dialog', dialogHandler); |
| 59 | + |
| 60 | + try { |
| 61 | + await page.goto(request.params.url, options); |
| 62 | + } finally { |
| 63 | + page.off('dialog', dialogHandler); |
| 64 | + } |
| 65 | + }, |
| 66 | +}); |
| 67 | + |
| 68 | +export const evaluateScript = defineTool({ |
| 69 | + name: 'evaluate', |
| 70 | + description: `Evaluate a JavaScript function`, |
| 71 | + annotations: { |
| 72 | + category: ToolCategory.DEBUGGING, |
| 73 | + readOnlyHint: false, |
| 74 | + }, |
| 75 | + schema: { |
| 76 | + fn: zod |
| 77 | + .string() |
| 78 | + .describe(`A JavaScript function to be executed on the active page`), |
| 79 | + }, |
| 80 | + handler: async (request, response, context) => { |
| 81 | + const page = context.getSelectedPage(); |
| 82 | + const fn = await page.evaluateHandle(`(${request.params.fn})`); |
| 83 | + const result = await page.evaluate(async fn => { |
| 84 | + // @ts-expect-error no types. |
| 85 | + return JSON.stringify(await fn()); |
| 86 | + }, fn); |
| 87 | + response.appendResponseLine(result); |
| 88 | + void fn.dispose(); |
| 89 | + }, |
| 90 | +}); |
| 91 | + |
| 92 | +export const tools = [ |
| 93 | + screenshot, |
| 94 | + evaluateScript, |
| 95 | + navigatePage, |
| 96 | +] as ToolDefinition[]; |
0 commit comments