donobu
Version:
Create browser automations with an LLM agent and replay them as Playwright scripts.
62 lines (61 loc) • 2.97 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PressKeyTool = exports.PressKeyGptSchema = exports.PressKeyNonGptSchema = exports.PressKeyCoreSchema = 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.PressKeyCoreSchema = v4_1.z.object({
key: v4_1.z.string().describe('The key to press'),
});
exports.PressKeyNonGptSchema = v4_1.z.object({
...ReplayableInteraction_1.SelectorBasedSchema.shape,
...exports.PressKeyCoreSchema.shape,
});
exports.PressKeyGptSchema = v4_1.z.object({
...ReplayableInteraction_1.AnnotationBasedSchema.shape,
...exports.PressKeyCoreSchema.shape,
});
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.`, exports.PressKeyCoreSchema, exports.PressKeyNonGptSchema, exports.PressKeyGptSchema);
}
async invoke(context, parameters, handles) {
const element = handles.target;
await element.focus();
await context.interactionVisualizer.pointAt((0, TargetUtils_1.webPage)(context), element);
// Move cursor to end of existing text
await element.evaluate((el) => {
const isTextInput = el instanceof HTMLTextAreaElement ||
(el instanceof HTMLInputElement &&
['text', 'search', 'url', 'tel', 'password'].includes(el.type));
if (isTextInput) {
el.setSelectionRange(el.value.length, el.value.length);
return;
}
if (el instanceof HTMLElement && el.isContentEditable) {
const range = document.createRange();
const sel = window.getSelection();
range.selectNodeContents(el);
range.collapse(false); // false means collapse to end
sel?.removeAllRanges();
sel?.addRange(range);
return;
}
});
await element.press('End');
await element.press(parameters.key, {
delay: MiscUtils_1.MiscUtils.generateHumanLikeKeyPressDurationInMs(parameters.key),
timeout: 3000,
});
return `Pressed the '${parameters.key}' key at: `;
}
}
exports.PressKeyTool = PressKeyTool;
PressKeyTool.NAME = 'pressKey';
//# sourceMappingURL=PressKeyTool.js.map