UNPKG

donobu

Version:

Create browser automations with an LLM agent and replay them as Playwright scripts.

96 lines 4.76 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.InputRandomizedEmailAddressTool = exports.InputRandomizedEmailAddressGptSchema = exports.InputRandomizedEmailAddressNonGptSchema = exports.InputRandomizedEmailAddressCoreSchema = void 0; const v4_1 = require("zod/v4"); const MiscUtils_1 = require("../utils/MiscUtils"); const TargetUtils_1 = require("../utils/TargetUtils"); const ReplayableInteraction_1 = require("./ReplayableInteraction"); exports.InputRandomizedEmailAddressCoreSchema = v4_1.z.object({ baseEmail: v4_1.z .string() .describe('The base email to create a derived email from'), finalizeWithSubmit: v4_1.z .boolean() .optional() .describe("Attempt to submit the data after inputting the randomized email (i.e. hitting 'Enter' at the end)"), }); exports.InputRandomizedEmailAddressNonGptSchema = v4_1.z.object({ ...ReplayableInteraction_1.SelectorBasedSchema.shape, ...exports.InputRandomizedEmailAddressCoreSchema.shape, }); exports.InputRandomizedEmailAddressGptSchema = v4_1.z.object({ ...ReplayableInteraction_1.AnnotationBasedSchema.shape, ...exports.InputRandomizedEmailAddressCoreSchema.shape, }); class InputRandomizedEmailAddressTool extends ReplayableInteraction_1.ReplayableInteraction { constructor() { super(InputRandomizedEmailAddressTool.NAME, `Create a new randomized email address based on a given email and inputs it to a specific input text field. For example if passed "foo@gmail.com", this tool will create "foo+RANDOM@gmail.com", where RANDOM is a random string of characters safe for use in an email address, and input it in the specified text field.`, exports.InputRandomizedEmailAddressCoreSchema, exports.InputRandomizedEmailAddressNonGptSchema, exports.InputRandomizedEmailAddressGptSchema); } async invoke(context, parameters, handles) { const randomizedEmail = this.randomizeEmail(parameters.baseEmail); const element = handles.target; // Clear any existing text first. await this.clearField(element); // Focus the element, then type via the page keyboard. See InputTextTool // for the rationale behind this approach. await element.focus(); const page = (0, TargetUtils_1.webPage)(context); for (const char of randomizedEmail) { await page.keyboard.press(char, { delay: MiscUtils_1.MiscUtils.generateHumanLikeKeyPressDurationInMs(char), }); } // Submit if requested if (parameters.finalizeWithSubmit) { const enterKey = 'Enter'; await page.keyboard.press(enterKey, { delay: MiscUtils_1.MiscUtils.generateHumanLikeKeyPressDurationInMs(enterKey), }); } return `Inputted email: "${randomizedEmail}" at: `; } randomizeEmail(baseEmail) { const atIndex = baseEmail.indexOf('@'); if (atIndex === -1) { throw new Error(`Invalid email address: ${baseEmail}`); } const localPart = baseEmail.substring(0, atIndex); const domainPart = baseEmail.substring(atIndex); const randomString = this.generateRandomString(InputRandomizedEmailAddressTool.RANDOM_STRING_LENGTH); return `${localPart}+${randomString}${domainPart}`; } generateRandomString(length) { const result = []; const charactersLength = InputRandomizedEmailAddressTool.CHARACTERS.length; for (let i = 0; i < length; i++) { const randomIndex = Math.floor(Math.random() * charactersLength); result.push(InputRandomizedEmailAddressTool.CHARACTERS[randomIndex]); } return result.join(''); } async clearField(element) { try { const value = await element.inputValue(); if (value !== '') { await element.selectText({ timeout: 3000 }); const backspaceKey = 'Backspace'; await element.press(backspaceKey, { delay: MiscUtils_1.MiscUtils.generateHumanLikeKeyPressDurationInMs(backspaceKey), timeout: 3000, }); } } catch (_e) { // This can happen if the element is not a text element, but still accepts text inputs. // Pass. } } } exports.InputRandomizedEmailAddressTool = InputRandomizedEmailAddressTool; InputRandomizedEmailAddressTool.NAME = 'inputRandomizedEmailAddress'; InputRandomizedEmailAddressTool.CHARACTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; InputRandomizedEmailAddressTool.RANDOM_STRING_LENGTH = 10; //# sourceMappingURL=InputRandomizedEmailAddressTool.js.map