Skip to content

Commit 5c060e2

Browse files
committed
Add configurable viewport sizes + new defaults for Anthropic Templates
Introduces viewportWidth and viewportHeight parameters to both Python and TypeScript anthropic templates, allowing the viewport size to be set when initializing sessions and tools. Updates default values to 1280x800 and ensures these values are used throughout session creation and tool instantiation.
1 parent 152c961 commit 5c060e2

File tree

6 files changed

+47
-16
lines changed

6 files changed

+47
-16
lines changed

pkg/templates/python/anthropic-computer-use/loop.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ async def sampling_loop(
7878
tool_version: ToolVersion = "computer_use_20250124",
7979
thinking_budget: int | None = None,
8080
token_efficient_tools_beta: bool = False,
81+
viewport_width: int = 1280,
82+
viewport_height: int = 800,
8183
):
8284
"""
8385
Agentic sampling loop for the assistant/tool interaction of computer use.
@@ -99,7 +101,7 @@ async def sampling_loop(
99101
tool_group = TOOL_GROUPS_BY_VERSION[tool_version]
100102
tool_collection = ToolCollection(
101103
*(
102-
ToolCls(kernel=kernel, session_id=session_id) if ToolCls.__name__.startswith("ComputerTool") else ToolCls()
104+
ToolCls(kernel=kernel, session_id=session_id, width=viewport_width, height=viewport_height) if ToolCls.__name__.startswith("ComputerTool") else ToolCls()
103105
for ToolCls in tool_group.tools
104106
)
105107
)

pkg/templates/python/anthropic-computer-use/session.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ class KernelBrowserSession:
3232
stealth: bool = True
3333
timeout_seconds: int = 300
3434

35+
viewport_width: int = 1280
36+
viewport_height: int = 800
37+
3538
# Replay recording options
3639
record_replay: bool = False
3740
replay_grace_period: float = 5.0 # Seconds to wait before stopping replay
@@ -52,8 +55,8 @@ async def __aenter__(self) -> "KernelBrowserSession":
5255
stealth=self.stealth,
5356
timeout_seconds=self.timeout_seconds,
5457
viewport={
55-
"width": 1024,
56-
"height": 768,
58+
"width": self.viewport_width,
59+
"height": self.viewport_height,
5760
},
5861
)
5962

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,6 @@ class BaseComputerTool:
107107
"""
108108

109109
name: Literal["computer"] = "computer"
110-
width: int = 1024
111-
height: int = 768
112110
display_num: int | None = None
113111

114112
# Kernel client and session
@@ -127,10 +125,12 @@ def options(self) -> ComputerToolOptions:
127125
"display_number": self.display_num,
128126
}
129127

130-
def __init__(self, kernel: Kernel | None = None, session_id: str | None = None):
128+
def __init__(self, kernel: Kernel | None = None, session_id: str | None = None, width: int = 1280, height: int = 800):
131129
super().__init__()
132130
self.kernel = kernel
133131
self.session_id = session_id
132+
self.width = width
133+
self.height = height
134134

135135
def validate_coordinates(self, coordinate: tuple[int, int] | list[int] | None = None) -> tuple[int, int] | None:
136136
"""Validate that coordinates are non-negative integers and convert lists to tuples if needed."""

pkg/templates/typescript/anthropic-computer-use/loop.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ export async function samplingLoop({
5757
tokenEfficientToolsBeta = false,
5858
kernel,
5959
sessionId,
60+
viewportWidth = 1280,
61+
viewportHeight = 800,
6062
}: {
6163
model: string;
6264
systemPromptSuffix?: string;
@@ -69,10 +71,12 @@ export async function samplingLoop({
6971
tokenEfficientToolsBeta?: boolean;
7072
kernel: Kernel;
7173
sessionId: string;
74+
viewportWidth?: number;
75+
viewportHeight?: number;
7276
}): Promise<BetaMessageParam[]> {
7377
const selectedVersion = toolVersion || DEFAULT_TOOL_VERSION;
7478
const toolGroup = TOOL_GROUPS_BY_VERSION[selectedVersion];
75-
const toolCollection = new ToolCollection(...toolGroup.tools.map((Tool: typeof ComputerTool20241022 | typeof ComputerTool20250124) => new Tool(kernel, sessionId)));
79+
const toolCollection = new ToolCollection(...toolGroup.tools.map((Tool: typeof ComputerTool20241022 | typeof ComputerTool20250124) => new Tool(kernel, sessionId, viewportWidth, viewportHeight)));
7680

7781
const system: BetaTextBlock = {
7882
type: 'text',

pkg/templates/typescript/anthropic-computer-use/session.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,28 @@ export interface SessionOptions {
1616
recordReplay?: boolean;
1717
/** Grace period in seconds before stopping replay */
1818
replayGracePeriod?: number;
19+
/** Viewport width */
20+
viewportWidth?: number;
21+
/** Viewport height */
22+
viewportHeight?: number;
1923
}
2024

2125
export interface SessionInfo {
2226
sessionId: string;
2327
liveViewUrl: string;
2428
replayId?: string;
2529
replayViewUrl?: string;
30+
viewportWidth: number;
31+
viewportHeight: number;
2632
}
2733

2834
const DEFAULT_OPTIONS: Required<SessionOptions> = {
2935
stealth: true,
3036
timeoutSeconds: 300,
3137
recordReplay: false,
3238
replayGracePeriod: 5.0,
39+
viewportWidth: 1280,
40+
viewportHeight: 800,
3341
};
3442

3543
/**
@@ -76,12 +84,22 @@ export class KernelBrowserSession {
7684
return this._replayViewUrl;
7785
}
7886

87+
get viewportWidth(): number {
88+
return this.options.viewportWidth;
89+
}
90+
91+
get viewportHeight(): number {
92+
return this.options.viewportHeight;
93+
}
94+
7995
get info(): SessionInfo {
8096
return {
8197
sessionId: this.sessionId,
8298
liveViewUrl: this._liveViewUrl || '',
8399
replayId: this._replayId || undefined,
84100
replayViewUrl: this._replayViewUrl || undefined,
101+
viewportWidth: this.options.viewportWidth,
102+
viewportHeight: this.options.viewportHeight,
85103
};
86104
}
87105

@@ -94,8 +112,8 @@ export class KernelBrowserSession {
94112
stealth: this.options.stealth,
95113
timeout_seconds: this.options.timeoutSeconds,
96114
viewport: {
97-
width: 1024,
98-
height: 768,
115+
width: this.options.viewportWidth,
116+
height: this.options.viewportHeight,
99117
},
100118
});
101119

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ export class ComputerTool implements BaseAnthropicTool {
1212
protected sessionId: string;
1313
protected _screenshotDelay = 2.0;
1414
protected version: '20241022' | '20250124';
15+
protected width: number;
16+
protected height: number;
1517

1618
private lastMousePosition: [number, number] = [0, 0];
1719

@@ -39,10 +41,12 @@ export class ComputerTool implements BaseAnthropicTool {
3941
Action.WAIT,
4042
]);
4143

42-
constructor(kernel: Kernel, sessionId: string, version: '20241022' | '20250124' = '20250124') {
44+
constructor(kernel: Kernel, sessionId: string, version: '20241022' | '20250124' = '20250124', width = 1280, height = 800) {
4345
this.kernel = kernel;
4446
this.sessionId = sessionId;
4547
this.version = version;
48+
this.width = width;
49+
this.height = height;
4650
}
4751

4852
get apiType(): 'computer_20241022' | 'computer_20250124' {
@@ -53,8 +57,8 @@ export class ComputerTool implements BaseAnthropicTool {
5357
const params = {
5458
name: this.name,
5559
type: this.apiType,
56-
display_width_px: 1024,
57-
display_height_px: 768,
60+
display_width_px: this.width,
61+
display_height_px: this.height,
5862
display_number: null,
5963
};
6064
return params;
@@ -380,13 +384,13 @@ export class ComputerTool implements BaseAnthropicTool {
380384

381385
// For backward compatibility
382386
export class ComputerTool20241022 extends ComputerTool {
383-
constructor(kernel: Kernel, sessionId: string) {
384-
super(kernel, sessionId, '20241022');
387+
constructor(kernel: Kernel, sessionId: string, width = 1280, height = 800) {
388+
super(kernel, sessionId, '20241022', width, height);
385389
}
386390
}
387391

388392
export class ComputerTool20250124 extends ComputerTool {
389-
constructor(kernel: Kernel, sessionId: string) {
390-
super(kernel, sessionId, '20250124');
393+
constructor(kernel: Kernel, sessionId: string, width = 1280, height = 800) {
394+
super(kernel, sessionId, '20250124', width, height);
391395
}
392396
}

0 commit comments

Comments
 (0)