text
stringlengths
0
59.1k
description: "Extract structured data from a webpage using natural language instructions",
parameters: z.object({
url: z.string().url().optional().describe("URL to navigate to (optional if already on a page)"),
instruction: z.string().describe("What to extract (e.g., 'extract all product prices')"),
schema: z.record(z.any()).optional().describe("Zod schema definition for data extraction"),
useTextExtract: z
.boolean()
.optional()
.default(false)
.describe("Set true for larger-scale extractions"),
}),
execute: async ({ url, instruction, schema, useTextExtract }) => {
const stagehand = await sessionManager.ensureStagehand();
const page = stagehand.page;
if (url) {
await page.goto(url, { waitUntil: "networkidle" });
}
const defaultBrandSchema = {
productName: z.string().describe("The product or service name"),
tagline: z.string().describe("The main tagline or headline"),
valueProposition: z.string().describe("The unique value proposition"),
targetAudience: z.string().describe("The target audience"),
features: z.array(z.string()).describe("Key features or benefits"),
callToAction: z.string().describe("Main call-to-action text"),
};
const finalSchema = schema || defaultBrandSchema;
const schemaObject = z.object(finalSchema);
const result = await page.extract({
instruction,
schema: schemaObject,
useTextExtract,
});
return {
success: true,
data: result,
url: page.url(),
};
},
});
```
</details>
Features:
- AI-based structured data extraction
- Custom Zod schema support for type safety
- Default brand extraction schema
- Handles variable extraction scales
#### Page Observe Tool
Locates and analyzes UI elements:
<details>
<summary>Show page-observe.tool.ts</summary>
```typescript
import { createTool } from "@voltagent/core";
import { z } from "zod";
import { sessionManager } from "../../stagehand-manager";
export const pageObserveTool = createTool({
name: "page_observe",
description: "Observe and locate elements on the current page using AI vision",
parameters: z.object({
instruction: z.string().describe("Natural language instruction for what to observe"),
useVision: z
.boolean()
.optional()
.default(true)
.describe("Use vision model for element detection"),
}),
execute: async ({ instruction, useVision }) => {
try {
const stagehand = await sessionManager.ensureStagehand();
const page = stagehand.page;
console.log(`Observing page with instruction: ${instruction}`);
// Use Stagehand's observe method with vision capabilities
const observations = await stagehand.observe({
instruction,
useVision,
});
console.log(`Found ${observations.length} elements matching criteria`);
return {
success: true,
elements: observations,
count: observations.length,
instruction,
url: page.url(),
};