Skip to content

Commit c6db199

Browse files
fix(computer-use): scale scroll amounts (#23)
* fix(computer-use): scale scroll amounts in TypeScript * fix(computer-use): python template scrolling scale * fix(computer-use): print scroll distance in pixels in TypeScript --------- Co-authored-by: Matt Marangoni <[email protected]>
1 parent 35bbc87 commit c6db199

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

templates/python/computer-use/tools/computer.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -322,16 +322,26 @@ async def __call__(
322322
await self.page.mouse.move(x, y)
323323

324324
# Map scroll directions to Playwright's wheel events
325+
page_dimensions = await self.page.evaluate(
326+
"() => Promise.resolve({ h: window.innerHeight, w: window.innerWidth })"
327+
)
328+
page_partitions = 25
329+
scroll_factor = scroll_amount / page_partitions
330+
page_width = page_dimensions['w']
331+
page_height = page_dimensions['h']
332+
325333
delta_x = 0
326334
delta_y = 0
327335
if scroll_direction == "up":
328-
delta_y = -scroll_amount * 100
336+
delta_y = -scroll_factor * page_height
329337
elif scroll_direction == "down":
330-
delta_y = scroll_amount * 100
338+
delta_y = scroll_factor * page_height
331339
elif scroll_direction == "left":
332-
delta_x = -scroll_amount * 100
340+
delta_x = -scroll_factor * page_width
333341
elif scroll_direction == "right":
334-
delta_x = scroll_amount * 100
342+
delta_x = scroll_factor * page_width
343+
344+
print(f"Scrolling {abs(delta_x) if delta_x != 0 else abs(delta_y):.02f} pixels {scroll_direction}")
335345

336346
await self.page.mouse.wheel(delta_x=delta_x, delta_y=delta_y)
337347
return await self.screenshot()
@@ -391,4 +401,4 @@ async def __call__(
391401

392402
return await super().__call__(
393403
action=action, text=text, coordinate=coordinate, key=key, **kwargs
394-
)
404+
)

templates/typescript/computer-use/tools/computer.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,19 @@ export class ComputerTool implements BaseAnthropicTool {
190190
await this.page.waitForTimeout(100);
191191
}
192192

193-
const amount = scrollAmountValue || 100;
193+
const pageDimensions = await this.page.evaluate(() => {
194+
return { h: window.innerHeight, w: window.innerWidth };
195+
});
196+
const pagePartitions = 25;
197+
const scrollFactor = (scrollAmountValue || 10) / pagePartitions;
194198

195199
if (scrollDirection === 'down' || scrollDirection === 'up') {
200+
const amount = pageDimensions.h * scrollFactor;
201+
console.log(`Scrolling ${amount.toFixed(2)} pixels ${scrollDirection}`);
196202
await this.page.mouse.wheel(0, scrollDirection === 'down' ? amount : -amount);
197203
} else {
204+
const amount = pageDimensions.w * scrollFactor;
205+
console.log(`Scrolling ${amount.toFixed(2)} pixels ${scrollDirection}`);
198206
await this.page.mouse.wheel(scrollDirection === 'right' ? amount : -amount, 0);
199207
}
200208

0 commit comments

Comments
 (0)