-
Notifications
You must be signed in to change notification settings - Fork 357
Expand file tree
/
Copy pathnavigate.ts
More file actions
executable file
·66 lines (55 loc) · 1.79 KB
/
navigate.ts
File metadata and controls
executable file
·66 lines (55 loc) · 1.79 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
import { z } from "zod";
import type { Tool, ToolSchema, ToolResult } from "./tool.js";
import type { Context } from "../context.js";
import type { ToolActionResult } from "../types/types.js";
const NavigateInputSchema = z.object({
url: z.string().describe("The URL to navigate to"),
});
type NavigateInput = z.infer<typeof NavigateInputSchema>;
const navigateSchema: ToolSchema<typeof NavigateInputSchema> = {
name: "browserbase_stagehand_navigate",
description: `Navigate to a URL in the browser. Only use this tool with URLs you're confident will work and be up to date.
Otherwise, use https://google.com as the starting point`,
inputSchema: NavigateInputSchema,
};
async function handleNavigate(
context: Context,
params: NavigateInput,
): Promise<ToolResult> {
const action = async (): Promise<ToolActionResult> => {
try {
const stagehand = await context.getStagehand();
const pages = stagehand.context.pages();
const page = pages[0];
if (!page) {
throw new Error("No active page available");
}
await page.goto(params.url, { waitUntil: "domcontentloaded" });
const sessionId = stagehand.browserbaseSessionId;
if (!sessionId) {
throw new Error("No Browserbase session ID available");
}
return {
content: [
{
type: "text",
text: `Navigated to: ${params.url}`,
},
],
};
} catch (error) {
const errorMsg = error instanceof Error ? error.message : String(error);
throw new Error(`Failed to navigate: ${errorMsg}`);
}
};
return {
action,
waitForNetwork: false,
};
}
const navigateTool: Tool<typeof NavigateInputSchema> = {
capability: "core",
schema: navigateSchema,
handle: handleNavigate,
};
export default navigateTool;