text
stringlengths
0
59.1k
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
throw new Error(`Observation failed: ${errorMessage}`);
}
},
});
```
</details>
Features:
- AI vision for element location via natural language
- Identifies buttons, forms, images, interactive elements
- Returns element selectors and properties
- Extends beyond CSS selector limitations
#### Page Act Tool
Performs web page interactions:
<details>
<summary>Show page-act.tool.ts</summary>
```typescript
import { createTool } from "@voltagent/core";
import { z } from "zod";
import { sessionManager } from "../../stagehand-manager";
export const pageActTool = createTool({
name: "page_act",
description: "Perform actions on web page elements using natural language",
parameters: z.object({
action: z.string().describe("The action to perform (e.g., 'click the login button')"),
useVision: z.boolean().optional().default(true).describe("Use vision model to find elements"),
}),
execute: async ({ action, useVision }) => {
try {
const stagehand = await sessionManager.ensureStagehand();
const page = stagehand.page;
console.log(`Performing action: ${action}`);
// Use Stagehand's act method
await stagehand.act({
action,
useVision,
});
console.log(`Action completed: ${action}`);
return {
success: true,
action,
url: page.url(),
message: `Successfully performed: ${action}`,
};
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
throw new Error(`Action failed: ${errorMessage}`);
}
},
});
```
</details>
Features:
- Natural language-based interactions (clicks, form fills)
- AI vision for element identification
- Complex interaction sequence handling
- Multi-step workflow navigation
#### Screenshot Tool
Captures visual references:
<details>
<summary>Show screenshot.tool.ts</summary>
```typescript
import { createTool } from "@voltagent/core";
import { z } from "zod";
import { sessionManager } from "../../stagehand-manager";
import * as fs from "fs/promises";
import * as path from "path";
export const screenshotTool = createTool({
name: "take_screenshot",
description: "Take a screenshot of the current page or a specific element",
parameters: z.object({
url: z.string().url().optional().describe("URL to navigate to (optional if already on a page)"),
fullPage: z.boolean().optional().default(false).describe("Whether to capture the full page"),
selector: z.string().optional().describe("CSS selector for specific element to capture"),
filename: z.string().optional().describe("Custom filename for the screenshot"),
}),
execute: async ({ url, fullPage, selector, filename }, context) => {
const stagehand = await sessionManager.ensureStagehand();
const page = stagehand.page;