UNPKG

@auto-browse/auto-browse

Version:
46 lines (45 loc) 1.74 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.browser_get_text = void 0; const tools_1 = require("@langchain/core/tools"); const zod_1 = require("zod"); const test_1 = require("@playwright/test"); const utils_1 = require("./utils"); const context_1 = require("../browser/context"); /** * Schema for getting text from elements with descriptions for the AI model */ const getTextSchema = zod_1.z.object({ element: zod_1.z .string() .describe("Human-readable element description for the target element"), ref: zod_1.z .string() .describe("Element reference from page snapshot to locate the element"), }); exports.browser_get_text = (0, tools_1.tool)(async ({ element, ref }) => { try { console.log(`[Get Text Tool] Starting operation:`, { element, ref, }); const result = await test_1.test.step(`Get text from "${element}"`, async () => { return await (0, utils_1.runAndWait)(context_1.context, `Got text from "${element}"`, async () => { const locator = context_1.context.refLocator(ref); const text = (await locator.innerText()) || ""; return text; }, false); }); console.log(`[Get Text Tool] Operation completed with result:`, result); return result; } catch (error) { const errorMessage = `Failed to get text: ${error instanceof Error ? error.message : "Unknown error"}`; console.error(`[Get Text Tool] Error:`, errorMessage); return errorMessage; } }, { name: "getText", description: "Get text content from an element on the page", schema: getTextSchema, });