-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathpages.ts
More file actions
211 lines (192 loc) · 5.44 KB
/
pages.ts
File metadata and controls
211 lines (192 loc) · 5.44 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import z from 'zod';
import {CLOSE_PAGE_ERROR, defineTool} from './ToolDefinition.js';
import {ToolCategories} from './categories.js';
export const listPages = defineTool({
name: 'list_pages',
description: `Get a list of pages open in the browser.`,
annotations: {
category: ToolCategories.NAVIGATION_AUTOMATION,
readOnlyHint: true,
},
schema: {},
handler: async (_request, response) => {
response.setIncludePages(true);
},
});
export const selectPage = defineTool({
name: 'select_page',
description: `Select a page as a context for future tool calls.`,
annotations: {
category: ToolCategories.NAVIGATION_AUTOMATION,
readOnlyHint: true,
},
schema: {
pageIdx: z
.number()
.describe(
'The index of the page to select. Call list_pages to list pages.',
),
},
handler: async (request, response, context) => {
const page = context.getPageByIdx(request.params.pageIdx);
await page.bringToFront();
context.setSelectedPageIdx(request.params.pageIdx);
response.setIncludePages(true);
},
});
export const closePage = defineTool({
name: 'close_page',
description: `Closes the page by its index. The last open page cannot be closed.`,
annotations: {
category: ToolCategories.NAVIGATION_AUTOMATION,
readOnlyHint: false,
},
schema: {
pageIdx: z
.number()
.describe(
'The index of the page to close. Call list_pages to list pages.',
),
},
handler: async (request, response, context) => {
try {
await context.closePage(request.params.pageIdx);
} catch (err) {
if (err.message === CLOSE_PAGE_ERROR) {
response.appendResponseLine(err.message);
} else {
throw err;
}
}
response.setIncludePages(true);
},
});
export const newPage = defineTool({
name: 'new_page',
description: `Creates a new page`,
annotations: {
category: ToolCategories.NAVIGATION_AUTOMATION,
readOnlyHint: false,
},
schema: {
url: z.string().describe('URL to load in a new page.'),
},
handler: async (request, response, context) => {
const page = await context.newPage();
await context.waitForEventsAfterAction(async () => {
await page.goto(request.params.url);
});
response.setIncludePages(true);
},
});
export const navigatePage = defineTool({
name: 'navigate_page',
description: `Navigates the currently selected page to a URL.`,
annotations: {
category: ToolCategories.NAVIGATION_AUTOMATION,
readOnlyHint: false,
},
schema: {
url: z.string().describe('URL to navigate the page to'),
},
handler: async (request, response, context) => {
const page = context.getSelectedPage();
await context.waitForEventsAfterAction(async () => {
await page.goto(request.params.url);
});
response.setIncludePages(true);
},
});
export const navigatePageHistory = defineTool({
name: 'navigate_page_history',
description: `Navigates the currently selected page.`,
annotations: {
category: ToolCategories.NAVIGATION_AUTOMATION,
readOnlyHint: false,
},
schema: {
navigate: z
.enum(['back', 'forward'])
.describe(
'Whether to navigate back or navigate forward in the selected pages history',
),
},
handler: async (request, response, context) => {
const page = context.getSelectedPage();
try {
if (request.params.navigate === 'back') {
await page.goBack();
} else {
await page.goForward();
}
} catch {
response.appendResponseLine(
`Unable to navigate ${request.params.navigate} in currently selected page.`,
);
}
response.setIncludePages(true);
},
});
export const resizePage = defineTool({
name: 'resize_page',
description: `Resizes the selected page's window so that the page has specified dimension`,
annotations: {
category: ToolCategories.EMULATION,
readOnlyHint: false,
},
schema: {
width: z.number().describe('Page width'),
height: z.number().describe('Page height'),
},
handler: async (request, response, context) => {
const page = context.getSelectedPage();
// @ts-expect-error internal API for now.
await page.resize({
contentWidth: request.params.width,
contentHeight: request.params.height,
});
response.setIncludePages(true);
},
});
export const handleDialog = defineTool({
name: 'handle_dialog',
description: `If a browser dialog was opened, use this command to handle it`,
annotations: {
category: ToolCategories.INPUT_AUTOMATION,
readOnlyHint: false,
},
schema: {
action: z
.enum(['accept', 'dismiss'])
.describe('Whether to dismiss or accept the dialog'),
promptText: z
.string()
.optional()
.describe('Optional prompt text to enter into the dialog.'),
},
handler: async (request, response, context) => {
const dialog = context.getDialog();
if (!dialog) {
throw new Error('No open dialog found');
}
switch (request.params.action) {
case 'accept': {
await dialog.accept(request.params.promptText);
response.appendResponseLine('Successfully accepted the dialog');
break;
}
case 'dismiss': {
await dialog.dismiss();
response.appendResponseLine('Successfully dismissed the dialog');
break;
}
}
context.clearDialog();
response.setIncludePages(true);
},
});