@auto-browse/auto-browse
Version:
AI-powered browser automation
52 lines (51 loc) • 1.93 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.browser_type = 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");
const typeSchema = zod_1.z.object({
element: zod_1.z
.string()
.describe("Human-readable element description for the target field"),
ref: zod_1.z
.string()
.describe("Element reference from page snapshot to locate the field"),
text: zod_1.z.string().describe("The text to type into the element"),
submit: zod_1.z
.boolean()
.optional()
.describe("Whether to submit by pressing Enter after typing"),
});
exports.browser_type = (0, tools_1.tool)(async ({ element, ref, text, submit }) => {
try {
console.log(`[Type Tool] Starting operation:`, {
element,
ref,
text,
submit,
});
const result = await test_1.test.step(`Fill "${text}" in "${element}"`, async () => {
return await (0, utils_1.runAndWait)(context_1.context, `Typed "${text}" into "${element}"`, async () => {
const locator = context_1.context.refLocator(ref);
await locator.fill(text);
if (submit) {
await locator.press("Enter");
}
}, true);
});
console.log(`[Type Tool] Operation completed with result:`, result);
return result;
}
catch (error) {
const errorMessage = `Failed to type: ${error instanceof Error ? error.message : "Unknown error"}`;
console.error(`[Type Tool] Error:`, errorMessage);
return errorMessage;
}
}, {
name: "type",
description: "Type text into an editable element on the page",
schema: typeSchema,
});