-
Notifications
You must be signed in to change notification settings - Fork 161
Expand file tree
/
Copy pathscroll.ts
More file actions
53 lines (46 loc) · 1.77 KB
/
scroll.ts
File metadata and controls
53 lines (46 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { z } from "zod";
import { ActionContext, AgentActionDefinition } from "@/types";
export const ScrollAction = z
.object({
direction: z
.enum(["up", "down", "left", "right"])
.describe("The direction to scroll."),
})
.describe("Scroll in a specific direction in the browser");
export type ScrollActionType = z.infer<typeof ScrollAction>;
export const ScrollActionDefinition: AgentActionDefinition = {
type: "scroll" as const,
actionParams: ScrollAction,
run: async (ctx: ActionContext, action: ScrollActionType) => {
const { direction } = action;
switch (direction) {
case "up":
await ctx.page.evaluate(() => window.scrollBy(0, -window.innerHeight));
break;
case "down":
await ctx.page.evaluate(() => window.scrollBy(0, window.innerHeight));
break;
case "left":
await ctx.page.evaluate(() => window.scrollBy(-window.innerWidth, 0));
break;
case "right":
await ctx.page.evaluate(() => window.scrollBy(window.innerWidth, 0));
break;
}
return { success: true, message: `Scrolled ${direction}` };
},
generateCode: async (ctx: ActionContext, action: ScrollActionType) => {
const { direction } = action;
return `
await ctx.page.evaluate(() => {
const scrollByUpDown = ${direction === "up" ? "-window.innerHeight" : direction === "down" ? "window.innerHeight" : "0"};
const scrollByLeftRight = ${direction === "left" ? "-window.innerWidth" : direction === "right" ? "window.innerWidth" : "0"};
window.scrollBy(scrollByLeftRight, scrollByUpDown);
console.log(\`Scrolled \${direction}\`);
});
`;
},
pprintAction: function (params: ScrollActionType): string {
return `Scroll ${params.direction}`;
},
};