2025-03-21 10:58:58 -07:00
|
|
|
/**
|
|
|
|
* Copyright (c) Microsoft Corporation.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import os from 'os';
|
|
|
|
import path from 'path';
|
|
|
|
|
|
|
|
import { z } from 'zod';
|
|
|
|
import { zodToJsonSchema } from 'zod-to-json-schema';
|
|
|
|
|
|
|
|
import { captureAriaSnapshot, runAndWait } from './utils';
|
|
|
|
|
|
|
|
import type { ToolFactory, Tool } from './tool';
|
|
|
|
|
|
|
|
const navigateSchema = z.object({
|
|
|
|
url: z.string().describe('The URL to navigate to'),
|
|
|
|
});
|
|
|
|
|
|
|
|
export const navigate: ToolFactory = snapshot => ({
|
|
|
|
schema: {
|
|
|
|
name: 'browser_navigate',
|
|
|
|
description: 'Navigate to a URL',
|
|
|
|
inputSchema: zodToJsonSchema(navigateSchema),
|
|
|
|
},
|
|
|
|
handle: async (context, params) => {
|
|
|
|
const validatedParams = navigateSchema.parse(params);
|
2025-03-26 15:02:45 -07:00
|
|
|
const page = await context.createPage();
|
2025-03-21 10:58:58 -07:00
|
|
|
await page.goto(validatedParams.url, { waitUntil: 'domcontentloaded' });
|
|
|
|
// Cap load event to 5 seconds, the page is operational at this point.
|
|
|
|
await page.waitForLoadState('load', { timeout: 5000 }).catch(() => {});
|
|
|
|
if (snapshot)
|
2025-03-27 20:22:44 +01:00
|
|
|
return captureAriaSnapshot(context);
|
2025-03-21 10:58:58 -07:00
|
|
|
return {
|
|
|
|
content: [{
|
|
|
|
type: 'text',
|
|
|
|
text: `Navigated to ${validatedParams.url}`,
|
|
|
|
}],
|
|
|
|
};
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const goBackSchema = z.object({});
|
|
|
|
|
|
|
|
export const goBack: ToolFactory = snapshot => ({
|
|
|
|
schema: {
|
|
|
|
name: 'browser_go_back',
|
|
|
|
description: 'Go back to the previous page',
|
|
|
|
inputSchema: zodToJsonSchema(goBackSchema),
|
|
|
|
},
|
|
|
|
handle: async context => {
|
2025-03-26 15:02:45 -07:00
|
|
|
return await runAndWait(context, 'Navigated back', async page => page.goBack(), snapshot);
|
2025-03-21 10:58:58 -07:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const goForwardSchema = z.object({});
|
|
|
|
|
|
|
|
export const goForward: ToolFactory = snapshot => ({
|
|
|
|
schema: {
|
|
|
|
name: 'browser_go_forward',
|
|
|
|
description: 'Go forward to the next page',
|
|
|
|
inputSchema: zodToJsonSchema(goForwardSchema),
|
|
|
|
},
|
|
|
|
handle: async context => {
|
2025-03-26 15:02:45 -07:00
|
|
|
return await runAndWait(context, 'Navigated forward', async page => page.goForward(), snapshot);
|
2025-03-21 10:58:58 -07:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const waitSchema = z.object({
|
|
|
|
time: z.number().describe('The time to wait in seconds'),
|
|
|
|
});
|
|
|
|
|
|
|
|
export const wait: Tool = {
|
|
|
|
schema: {
|
|
|
|
name: 'browser_wait',
|
|
|
|
description: 'Wait for a specified time in seconds',
|
|
|
|
inputSchema: zodToJsonSchema(waitSchema),
|
|
|
|
},
|
|
|
|
handle: async (context, params) => {
|
|
|
|
const validatedParams = waitSchema.parse(params);
|
2025-03-26 15:02:45 -07:00
|
|
|
await new Promise(f => setTimeout(f, Math.min(10000, validatedParams.time * 1000)));
|
2025-03-21 10:58:58 -07:00
|
|
|
return {
|
|
|
|
content: [{
|
|
|
|
type: 'text',
|
|
|
|
text: `Waited for ${validatedParams.time} seconds`,
|
|
|
|
}],
|
|
|
|
};
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const pressKeySchema = z.object({
|
|
|
|
key: z.string().describe('Name of the key to press or a character to generate, such as `ArrowLeft` or `a`'),
|
|
|
|
});
|
|
|
|
|
|
|
|
export const pressKey: Tool = {
|
|
|
|
schema: {
|
|
|
|
name: 'browser_press_key',
|
|
|
|
description: 'Press a key on the keyboard',
|
|
|
|
inputSchema: zodToJsonSchema(pressKeySchema),
|
|
|
|
},
|
|
|
|
handle: async (context, params) => {
|
|
|
|
const validatedParams = pressKeySchema.parse(params);
|
|
|
|
return await runAndWait(context, `Pressed key ${validatedParams.key}`, async page => {
|
|
|
|
await page.keyboard.press(validatedParams.key);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const pdfSchema = z.object({});
|
|
|
|
|
|
|
|
export const pdf: Tool = {
|
|
|
|
schema: {
|
|
|
|
name: 'browser_save_as_pdf',
|
|
|
|
description: 'Save page as PDF',
|
|
|
|
inputSchema: zodToJsonSchema(pdfSchema),
|
|
|
|
},
|
|
|
|
handle: async context => {
|
2025-03-27 07:27:34 -07:00
|
|
|
const page = context.existingPage();
|
2025-03-21 10:58:58 -07:00
|
|
|
const fileName = path.join(os.tmpdir(), `/page-${new Date().toISOString()}.pdf`);
|
|
|
|
await page.pdf({ path: fileName });
|
|
|
|
return {
|
|
|
|
content: [{
|
|
|
|
type: 'text',
|
|
|
|
text: `Saved as ${fileName}`,
|
|
|
|
}],
|
|
|
|
};
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const closeSchema = z.object({});
|
|
|
|
|
|
|
|
export const close: Tool = {
|
|
|
|
schema: {
|
|
|
|
name: 'browser_close',
|
|
|
|
description: 'Close the page',
|
|
|
|
inputSchema: zodToJsonSchema(closeSchema),
|
|
|
|
},
|
|
|
|
handle: async context => {
|
|
|
|
await context.close();
|
|
|
|
return {
|
|
|
|
content: [{
|
|
|
|
type: 'text',
|
|
|
|
text: `Page closed`,
|
|
|
|
}],
|
|
|
|
};
|
|
|
|
},
|
|
|
|
};
|
2025-03-27 20:49:57 +01:00
|
|
|
|
|
|
|
const chooseFileSchema = z.object({
|
|
|
|
paths: z.array(z.string()).describe('The absolute paths to the files to upload. Can be a single file or multiple files.'),
|
|
|
|
});
|
|
|
|
|
|
|
|
export const chooseFile: ToolFactory = snapshot => ({
|
|
|
|
schema: {
|
|
|
|
name: 'browser_choose_file',
|
|
|
|
description: 'Choose one or multiple files to upload',
|
|
|
|
inputSchema: zodToJsonSchema(chooseFileSchema),
|
|
|
|
},
|
|
|
|
handle: async (context, params) => {
|
|
|
|
const validatedParams = chooseFileSchema.parse(params);
|
|
|
|
return await runAndWait(context, `Chose files ${validatedParams.paths.join(', ')}`, async () => {
|
|
|
|
await context.submitFileChooser(validatedParams.paths);
|
|
|
|
}, snapshot);
|
|
|
|
},
|
|
|
|
});
|