2025-04-03 19:24:17 -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 { z } from 'zod';
|
|
|
|
import { zodToJsonSchema } from 'zod-to-json-schema';
|
|
|
|
|
|
|
|
import type { ToolFactory, Tool } from './tool';
|
|
|
|
|
2025-04-04 15:22:00 -07:00
|
|
|
const listTabs: Tool = {
|
2025-04-04 17:14:30 -07:00
|
|
|
capability: 'tabs',
|
2025-04-03 19:24:17 -07:00
|
|
|
schema: {
|
2025-04-04 15:22:00 -07:00
|
|
|
name: 'browser_tab_list',
|
2025-04-03 19:24:17 -07:00
|
|
|
description: 'List browser tabs',
|
|
|
|
inputSchema: zodToJsonSchema(z.object({})),
|
|
|
|
},
|
|
|
|
handle: async context => {
|
|
|
|
return {
|
|
|
|
content: [{
|
|
|
|
type: 'text',
|
|
|
|
text: await context.listTabs(),
|
|
|
|
}],
|
|
|
|
};
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const selectTabSchema = z.object({
|
|
|
|
index: z.number().describe('The index of the tab to select'),
|
|
|
|
});
|
|
|
|
|
2025-04-04 15:22:00 -07:00
|
|
|
const selectTab: ToolFactory = captureSnapshot => ({
|
2025-04-04 17:14:30 -07:00
|
|
|
capability: 'tabs',
|
2025-04-03 19:24:17 -07:00
|
|
|
schema: {
|
2025-04-04 15:22:00 -07:00
|
|
|
name: 'browser_tab_select',
|
2025-04-03 19:24:17 -07:00
|
|
|
description: 'Select a tab by index',
|
|
|
|
inputSchema: zodToJsonSchema(selectTabSchema),
|
|
|
|
},
|
|
|
|
handle: async (context, params) => {
|
|
|
|
const validatedParams = selectTabSchema.parse(params);
|
|
|
|
await context.selectTab(validatedParams.index);
|
|
|
|
const currentTab = await context.ensureTab();
|
2025-04-15 12:54:45 -07:00
|
|
|
return await currentTab.run(async () => {
|
|
|
|
const code = [
|
|
|
|
`// <internal code to select tab ${validatedParams.index}>`,
|
|
|
|
];
|
|
|
|
return { code };
|
|
|
|
}, { captureSnapshot });
|
2025-04-03 19:24:17 -07:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const newTabSchema = z.object({
|
|
|
|
url: z.string().optional().describe('The URL to navigate to in the new tab. If not provided, the new tab will be blank.'),
|
|
|
|
});
|
|
|
|
|
2025-04-04 15:22:00 -07:00
|
|
|
const newTab: Tool = {
|
2025-04-04 17:14:30 -07:00
|
|
|
capability: 'tabs',
|
2025-04-03 19:24:17 -07:00
|
|
|
schema: {
|
2025-04-04 15:22:00 -07:00
|
|
|
name: 'browser_tab_new',
|
2025-04-03 19:24:17 -07:00
|
|
|
description: 'Open a new tab',
|
|
|
|
inputSchema: zodToJsonSchema(newTabSchema),
|
|
|
|
},
|
|
|
|
handle: async (context, params) => {
|
|
|
|
const validatedParams = newTabSchema.parse(params);
|
|
|
|
await context.newTab();
|
|
|
|
if (validatedParams.url)
|
|
|
|
await context.currentTab().navigate(validatedParams.url);
|
2025-04-15 12:54:45 -07:00
|
|
|
return await context.currentTab().run(async () => {
|
|
|
|
const code = [
|
|
|
|
`// <internal code to open a new tab>`,
|
|
|
|
];
|
|
|
|
return { code };
|
|
|
|
}, { captureSnapshot: true });
|
2025-04-03 19:24:17 -07:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const closeTabSchema = z.object({
|
|
|
|
index: z.number().optional().describe('The index of the tab to close. Closes current tab if not provided.'),
|
|
|
|
});
|
|
|
|
|
2025-04-04 15:22:00 -07:00
|
|
|
const closeTab: ToolFactory = captureSnapshot => ({
|
2025-04-04 17:14:30 -07:00
|
|
|
capability: 'tabs',
|
2025-04-03 19:24:17 -07:00
|
|
|
schema: {
|
2025-04-04 15:22:00 -07:00
|
|
|
name: 'browser_tab_close',
|
2025-04-03 19:24:17 -07:00
|
|
|
description: 'Close a tab',
|
|
|
|
inputSchema: zodToJsonSchema(closeTabSchema),
|
|
|
|
},
|
|
|
|
handle: async (context, params) => {
|
|
|
|
const validatedParams = closeTabSchema.parse(params);
|
|
|
|
await context.closeTab(validatedParams.index);
|
2025-04-14 16:39:58 -07:00
|
|
|
const currentTab = context.currentTab();
|
2025-04-15 12:54:45 -07:00
|
|
|
if (currentTab) {
|
|
|
|
return await currentTab.run(async () => {
|
|
|
|
const code = [
|
|
|
|
`// <internal code to close tab ${validatedParams.index}>`,
|
|
|
|
];
|
|
|
|
return { code };
|
|
|
|
}, { captureSnapshot });
|
|
|
|
}
|
2025-04-03 19:24:17 -07:00
|
|
|
return {
|
|
|
|
content: [{
|
|
|
|
type: 'text',
|
|
|
|
text: await context.listTabs(),
|
|
|
|
}],
|
|
|
|
};
|
|
|
|
},
|
|
|
|
});
|
2025-04-04 15:22:00 -07:00
|
|
|
|
|
|
|
export default (captureSnapshot: boolean) => [
|
|
|
|
listTabs,
|
|
|
|
newTab,
|
|
|
|
selectTab(captureSnapshot),
|
|
|
|
closeTab(captureSnapshot),
|
|
|
|
];
|