UNPKG

donobu

Version:

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

43 lines (42 loc) 2.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PressKeyTool = void 0; const ReplayableInteraction_1 = require("./ReplayableInteraction"); const MiscUtils_1 = require("../utils/MiscUtils"); class PressKeyTool extends ReplayableInteraction_1.ReplayableInteraction { constructor() { super(PressKeyTool.NAME, `Press a single key, the element to input the keypress at is designated by an annotation located dead-center of the target element. Note that if there is existing text in the element, it is appended rather than cleared. Generally, prefer using the 'inputText' tool instead as it automatically clears existing text and it allows the input of multiple characters at a time. The only advantage this tool has is that it allows the passing of control keys like "Tab", "Backspace", etc.`, 'SelectorBasedPressKeyToolParameters', 'AnnotationBasedPressKeyToolParameters'); } async invoke(_context, parameters, element) { // First focus the element await element.focus(); // Move cursor to end of existing text await element.evaluate((el) => { if (el instanceof HTMLInputElement || el instanceof HTMLTextAreaElement) { el.setSelectionRange(el.value.length, el.value.length); } else if (el instanceof HTMLElement && el.isContentEditable) { // Handle contenteditable elements const range = document.createRange(); const sel = window.getSelection(); range.selectNodeContents(el); range.collapse(false); // false means collapse to end sel?.removeAllRanges(); sel?.addRange(range); } }); await element.press(parameters.key, { delay: MiscUtils_1.MiscUtils.generateHumanLikeKeyPressDurationInMs(parameters.key), timeout: 3000, }); return 'Pressed the key at the element.'; } } exports.PressKeyTool = PressKeyTool; PressKeyTool.NAME = 'pressKey'; //# sourceMappingURL=PressKeyTool.js.map