-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathtools.ts
More file actions
87 lines (80 loc) · 2.35 KB
/
tools.ts
File metadata and controls
87 lines (80 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
* @license
* Copyright 2026 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import type {Dialog} from '../../third_party/index.js';
import {zod} from '../../third_party/index.js';
import {ToolCategory} from '../categories.js';
import {defineTool} from '../ToolDefinition.js';
export const screenshot = defineTool({
name: 'screenshot',
description: `Takes a screenshot`,
annotations: {
category: ToolCategory.DEBUGGING,
// Not read-only due to filePath param.
readOnlyHint: false,
},
schema: {},
handler: async (request, response, context) => {
const page = context.getSelectedPage();
const screenshot = await page.screenshot({
type: 'png',
optimizeForSpeed: true,
});
const {filename} = await context.saveTemporaryFile(screenshot, `image/png`);
response.appendResponseLine(filename);
},
});
export const navigate = defineTool({
name: 'navigate',
description: `Loads a URL`,
annotations: {
category: ToolCategory.NAVIGATION,
readOnlyHint: false,
},
schema: {
url: zod.string().describe('URL to navigate to'),
},
handler: async (request, response, context) => {
const page = context.getSelectedPage();
const options = {
timeout: 30_000,
};
const dialogHandler = (dialog: Dialog) => {
if (dialog.type() === 'beforeunload') {
response.appendResponseLine(`Accepted a beforeunload dialog.`);
void dialog.accept();
// We are not going to report the dialog like regular dialogs.
context.clearDialog();
}
};
page.on('dialog', dialogHandler);
try {
await page.goto(request.params.url, options);
response.appendResponseLine(`Navigated to ${page.url()}.`);
} finally {
page.off('dialog', dialogHandler);
}
},
});
export const evaluate = defineTool({
name: 'evaluate',
description: `Evaluates a JavaScript script`,
annotations: {
category: ToolCategory.DEBUGGING,
readOnlyHint: false,
},
schema: {
script: zod.string().describe(`JS script to run on the page`),
},
handler: async (request, response, context) => {
const page = context.getSelectedPage();
try {
const result = await page.evaluate(request.params.script);
response.appendResponseLine(JSON.stringify(result));
} catch (err) {
response.appendResponseLine(String(err.message));
}
},
});