donobu
Version:
Create browser automations with an LLM agent and replay them as Playwright scripts.
86 lines • 3.88 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ScrollPageTool = exports.ScrollPageGptSchema = exports.ScrollPageNonGptSchema = exports.ScrollPageCoreSchema = void 0;
const v4_1 = require("zod/v4");
const ReplayableInteraction_1 = require("./ReplayableInteraction");
exports.ScrollPageCoreSchema = v4_1.z.object({
direction: v4_1.z
.enum(['UP', 'DOWN', 'LEFT', 'RIGHT'])
.describe('The direction to scroll'),
maxScroll: v4_1.z
.boolean()
.optional()
.describe(`Optional. Set to true if the page or element should be scrolled maximally in the given direction.
If not specified, the page or element will scroll no more than a viewport's worth.`),
});
exports.ScrollPageNonGptSchema = v4_1.z.object({
...ReplayableInteraction_1.SelectorBasedSchema.shape,
...exports.ScrollPageCoreSchema.shape,
});
exports.ScrollPageGptSchema = v4_1.z.object({
...ReplayableInteraction_1.AnnotationBasedSchema.shape,
...exports.ScrollPageCoreSchema.shape,
});
class ScrollPageTool extends ReplayableInteraction_1.ReplayableInteraction {
constructor() {
super(ScrollPageTool.NAME, `Scroll the current page or element. This helps you see more of the
current page or element, of which may contain relevant information.`, exports.ScrollPageCoreSchema, exports.ScrollPageNonGptSchema, exports.ScrollPageGptSchema);
}
async invoke(_context, parameters, handles) {
const didScroll = await handles.target.evaluate((el, parameters) => {
const beforeY = el.scrollTop;
const beforeX = el.scrollLeft;
if (parameters.maxScroll) {
/* Jump straight to the edge */
switch (parameters.direction) {
case 'UP':
el.scrollTop = 0;
break;
case 'DOWN':
el.scrollTop = el.scrollHeight;
break;
case 'LEFT':
el.scrollLeft = 0;
break;
case 'RIGHT':
el.scrollLeft = el.scrollWidth;
break;
}
}
else {
/* Incremental ¾-viewport scroll */
const visibleHeight = Math.min(el.clientHeight, window.innerHeight);
const visibleWidth = Math.min(el.clientWidth, window.innerWidth);
const deltaY = Math.floor(visibleHeight * 0.75);
const deltaX = Math.floor(visibleWidth * 0.75);
switch (parameters.direction) {
case 'UP':
el.scrollBy(0, -deltaY);
break;
case 'DOWN':
el.scrollBy(0, deltaY);
break;
case 'LEFT':
el.scrollBy(-deltaX, 0);
break;
case 'RIGHT':
el.scrollBy(deltaX, 0);
break;
}
}
const afterY = el.scrollTop;
const afterX = el.scrollLeft;
/* Return whether any movement occurred */
return beforeY !== afterY || beforeX !== afterX;
}, parameters);
return didScroll
? `Scrolled ${parameters.direction} ${parameters.maxScroll ? '(maximum amount scrolled)' : ''} for `
: `Nothing to scroll ${parameters.direction}`;
}
}
exports.ScrollPageTool = ScrollPageTool;
// WARNING: If you rename this tool, you must also update the `ReplayableInteraction.ts`
// class, since it special cases some operations for scrolling by using this
// tool name.
ScrollPageTool.NAME = 'scrollPage';
//# sourceMappingURL=ScrollPageTool.js.map