|
| 1 | +import { z } from "zod"; |
| 2 | +import { Logger, LogLevel } from "../../../infrastructure/logger/logger"; |
| 3 | +import { BrowserTool } from "../../../tools/tools"; |
| 4 | +import { BROWSER_ACTIONS } from "../../../tools/browser-actions"; |
| 5 | +import { StructuredTool } from "@langchain/core/tools"; |
| 6 | + |
| 7 | +const browserSchema = z.object({ |
| 8 | + action: z.enum(BROWSER_ACTIONS).describe("The browser action to perform."), |
| 9 | + url: z.string().optional().describe("URL for navigate/tab_new actions."), |
| 10 | + ref: z |
| 11 | + .string() |
| 12 | + .optional() |
| 13 | + .describe( |
| 14 | + "Element reference from a previous snapshot (for click, type, hover, select_option).", |
| 15 | + ), |
| 16 | + text: z.string().optional().describe("Text to type (for type action)."), |
| 17 | + value: z |
| 18 | + .string() |
| 19 | + .optional() |
| 20 | + .describe("Value to select (for select_option action)."), |
| 21 | + expression: z |
| 22 | + .string() |
| 23 | + .optional() |
| 24 | + .describe("JavaScript expression to evaluate in the page context."), |
| 25 | + key: z |
| 26 | + .string() |
| 27 | + .optional() |
| 28 | + .describe( |
| 29 | + "Key to press (for press_key action, e.g. 'Enter', 'Tab', 'ArrowDown').", |
| 30 | + ), |
| 31 | + time: z |
| 32 | + .number() |
| 33 | + .optional() |
| 34 | + .describe("Time to wait in ms (for wait action, default 2000)."), |
| 35 | +}); |
| 36 | + |
| 37 | +type BrowserToolInput = z.infer<typeof browserSchema>; |
| 38 | + |
| 39 | +// StructuredTool<any> is intentional — StructuredTool<typeof browserSchema> |
| 40 | +// triggers TS2589 ("Type instantiation is excessively deep"). All tools |
| 41 | +// in this codebase use <any> for the same reason. |
| 42 | +export class LangChainBrowserTool extends StructuredTool<any> { |
| 43 | + private readonly logger: Logger; |
| 44 | + constructor(private readonly toolInstance: BrowserTool) { |
| 45 | + super(); |
| 46 | + this.logger = Logger.initialize("LangChainBrowserTool", { |
| 47 | + minLevel: LogLevel.DEBUG, |
| 48 | + enableConsole: true, |
| 49 | + enableFile: true, |
| 50 | + enableTelemetry: true, |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + name = "browser"; |
| 55 | + description = |
| 56 | + "Control a headless browser via Playwright. Navigate pages, click elements, type text, take screenshots, read page snapshots (accessibility tree), and execute JavaScript. " + |
| 57 | + "Workflow: 1) navigate to a URL, 2) snapshot to see the page structure and element refs, 3) interact using refs from the snapshot."; |
| 58 | + |
| 59 | + schema = browserSchema; |
| 60 | + |
| 61 | + async _call(input: BrowserToolInput): Promise<string> { |
| 62 | + this.logger.info( |
| 63 | + `Executing tool: ${this.name} with action: ${input.action}`, |
| 64 | + ); |
| 65 | + try { |
| 66 | + const result = await this.toolInstance.execute(input); |
| 67 | + return result; |
| 68 | + } catch (error: unknown) { |
| 69 | + const message = error instanceof Error ? error.message : String(error); |
| 70 | + this.logger.error(`Error in tool ${this.name}: ${message}`, { input }); |
| 71 | + return `Error: ${message}`; |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments