-
Notifications
You must be signed in to change notification settings - Fork 358
Expand file tree
/
Copy pathagent.ts
More file actions
executable file
·83 lines (72 loc) · 2.41 KB
/
agent.ts
File metadata and controls
executable file
·83 lines (72 loc) · 2.41 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
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";
/**
* Stagehand Agent
* Docs: https://docs.stagehand.dev/basics/agent
*
* This tool uses Gemini Computer Use to autonomously complete web-based tasks.
* The agent will navigate, interact, and complete the task described in the prompt.
*/
const AgentInputSchema = z.object({
prompt: z.string().describe(
`The task prompt describing what you want the sub-agent to accomplish.
Be clear and specific about the goal. For example:
'Go to Hacker News and find the most controversial post from today, then summarize the top 3 comments'.
The agent will autonomously navigate and interact with web pages to complete this task.`,
),
});
type AgentInput = z.infer<typeof AgentInputSchema>;
const agentSchema: ToolSchema<typeof AgentInputSchema> = {
name: "browserbase_stagehand_agent",
description: `Execute a task autonomously using Gemini Computer Use agent. The agent will navigate and interact with web pages to complete the given task.`,
inputSchema: AgentInputSchema,
};
async function handleAgent(
context: Context,
params: AgentInput,
): Promise<ToolResult> {
const action = async (): Promise<ToolActionResult> => {
try {
const stagehand = await context.getStagehand();
// You need to provide GOOGLE_GENERATIVE_AI_API_KEY
const agent = stagehand.agent({
cua: true,
model: {
modelName: "google/gemini-2.5-computer-use-preview-10-2025",
apiKey:
process.env.GEMINI_API_KEY ||
process.env.GOOGLE_API_KEY ||
process.env.GOOGLE_GENERATIVE_AI_API_KEY,
},
});
// Execute the task
const result = await agent.execute({
instruction: params.prompt,
maxSteps: 20,
});
return {
content: [
{
type: "text",
text: `${result.message}`,
},
],
};
} catch (error) {
const errorMsg = error instanceof Error ? error.message : String(error);
throw new Error(`Failed to execute agent task: ${errorMsg}`);
}
};
return {
action,
waitForNetwork: false,
};
}
const agentTool: Tool<typeof AgentInputSchema> = {
capability: "core",
schema: agentSchema,
handle: handleAgent,
};
export default agentTool;