Skip to content

Commit 5c6f6fe

Browse files
seanzhougooglecopybara-github
authored andcommitted
chore: Add sandbox computer use sample agent
Co-authored-by: Xiang (Sean) Zhou <seanzhougoogle@google.com> PiperOrigin-RevId: 900322196
1 parent 64ed1a6 commit 5c6f6fe

3 files changed

Lines changed: 266 additions & 0 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from . import agent
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Sample agent using Vertex AI Agent Engine Sandbox for computer use.
16+
17+
This sample demonstrates how to use the AgentEngineSandboxComputer with ADK
18+
to create a computer use agent that operates in a remote sandbox environment.
19+
20+
Prerequisites:
21+
1. A GCP project with Agent Engine setup (https://docs.cloud.google.com/agent-builder/agent-engine/set-up)
22+
2. A service account with roles/iam.serviceAccountTokenCreator permission
23+
3. Environment variables in contributing/samples/.env:
24+
- GOOGLE_CLOUD_PROJECT: Your GCP project ID
25+
- VMAAS_SERVICE_ACCOUNT: Your service account email
26+
- VMAAS_SANDBOX_NAME: (Optional) Existing sandbox resource name for BYOS mode
27+
28+
Usage:
29+
# Run via ADK web UI
30+
adk web contributing/samples/sandbox_computer_use
31+
32+
# Run via main.py
33+
cd contributing/samples
34+
python -m sandbox_computer_use.main
35+
"""
36+
37+
import os
38+
39+
from dotenv import load_dotenv
40+
from google.adk import Agent
41+
from google.adk.integrations.vmaas import AgentEngineSandboxComputer
42+
from google.adk.tools.computer_use.computer_use_toolset import ComputerUseToolset
43+
44+
# Load environment variables from .env file
45+
load_dotenv(override=True)
46+
47+
# Configuration from environment variables
48+
PROJECT_ID = os.environ.get("GOOGLE_CLOUD_PROJECT")
49+
SERVICE_ACCOUNT = os.environ.get("VMAAS_SERVICE_ACCOUNT")
50+
51+
# Optional: Use existing sandbox (BYOS mode)
52+
# Format: projects/{project}/locations/{location}/reasoningEngines/{id}/sandboxEnvironments/{id}
53+
SANDBOX_NAME = os.environ.get("SANDBOX_NAME") or os.environ.get(
54+
"VMAAS_SANDBOX_NAME"
55+
)
56+
57+
# Create the sandbox computer
58+
sandbox_computer = AgentEngineSandboxComputer(
59+
project_id=PROJECT_ID,
60+
service_account_email=SERVICE_ACCOUNT,
61+
sandbox_name=SANDBOX_NAME,
62+
search_engine_url="https://www.google.com",
63+
)
64+
65+
# Create agent with the computer use toolset
66+
root_agent = Agent(
67+
model="gemini-2.5-computer-use-preview-10-2025",
68+
name="sandbox_computer_use_agent",
69+
description=(
70+
"A computer use agent that operates a browser in a remote Vertex AI"
71+
" sandbox environment to complete user tasks."
72+
),
73+
instruction="""You are a computer use agent that can operate a web browser
74+
to help users complete tasks. You have access to browser controls including:
75+
- Navigation (go to URLs, back, forward, search)
76+
- Mouse actions (click, hover, scroll, drag and drop)
77+
- Keyboard input (type text, key combinations)
78+
- Screenshots (to see the current state)
79+
80+
When given a task:
81+
1. Think about what steps are needed to accomplish it
82+
2. Take actions one at a time, observing the results
83+
3. If something doesn't work, try alternative approaches
84+
4. Report back when the task is complete or if you encounter issues
85+
86+
Be careful with sensitive information and always respect website terms of service.
87+
""",
88+
tools=[ComputerUseToolset(computer=sandbox_computer)],
89+
)
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Main script to run the sandbox computer use agent.
16+
17+
This script demonstrates how to run the sandbox computer use agent
18+
programmatically using the InMemoryRunner.
19+
20+
Prerequisites:
21+
1. Set environment variables:
22+
- GOOGLE_CLOUD_PROJECT: Your GCP project ID
23+
- VMAAS_SERVICE_ACCOUNT: Your service account email with
24+
roles/iam.serviceAccountTokenCreator permission
25+
26+
Usage:
27+
cd contributing/samples
28+
python -m sandbox_computer_use.main
29+
"""
30+
31+
import asyncio
32+
import os
33+
import time
34+
35+
from dotenv import load_dotenv
36+
from google.adk.cli.utils import logs
37+
from google.adk.runners import InMemoryRunner
38+
from google.adk.sessions.session import Session
39+
from google.genai import types
40+
41+
# Import the agent module
42+
from . import agent
43+
44+
load_dotenv(override=True)
45+
logs.log_to_tmp_folder()
46+
47+
48+
async def run_prompt(
49+
runner: InMemoryRunner,
50+
session: Session,
51+
user_id: str,
52+
message: str,
53+
) -> None:
54+
"""Run a single prompt and print the response.
55+
56+
Args:
57+
runner: The agent runner.
58+
session: The session to use.
59+
user_id: The user ID.
60+
message: The user message.
61+
"""
62+
content = types.Content(
63+
role="user", parts=[types.Part.from_text(text=message)]
64+
)
65+
print(f"\n** User says: {message}")
66+
print("-" * 40)
67+
68+
async for event in runner.run_async(
69+
user_id=user_id,
70+
session_id=session.id,
71+
new_message=content,
72+
):
73+
if event.content and event.content.parts:
74+
for part in event.content.parts:
75+
if part.text:
76+
print(f"** {event.author}: {part.text}")
77+
elif hasattr(part, "inline_data") and part.inline_data:
78+
# Screenshot received
79+
print(f"** {event.author}: [Screenshot received]")
80+
81+
82+
async def main():
83+
"""Main function to run the sandbox computer use agent."""
84+
# Validate environment
85+
project_id = os.environ.get("GOOGLE_CLOUD_PROJECT")
86+
service_account = os.environ.get("VMAAS_SERVICE_ACCOUNT")
87+
88+
if not project_id:
89+
print("ERROR: GOOGLE_CLOUD_PROJECT environment variable is not set.")
90+
print("Please set it to your GCP project ID.")
91+
return
92+
93+
if not service_account:
94+
print("ERROR: VMAAS_SERVICE_ACCOUNT environment variable is not set.")
95+
print(
96+
"Please set it to your service account email with"
97+
" roles/iam.serviceAccountTokenCreator permission."
98+
)
99+
return
100+
101+
print("=" * 60)
102+
print("Sandbox Computer Use Agent Demo")
103+
print("=" * 60)
104+
print(f"Project: {project_id}")
105+
print(f"Service Account: {service_account}")
106+
print("=" * 60)
107+
108+
app_name = "sandbox_computer_use_demo"
109+
user_id = "demo_user"
110+
111+
# Create runner and session
112+
runner = InMemoryRunner(
113+
agent=agent.root_agent,
114+
app_name=app_name,
115+
)
116+
session = await runner.session_service.create_session(
117+
app_name=app_name, user_id=user_id
118+
)
119+
120+
print(f"\nSession created: {session.id}")
121+
print("\nStarting agent interaction...")
122+
123+
start_time = time.time()
124+
125+
# Example interaction: Navigate and describe
126+
await run_prompt(
127+
runner,
128+
session,
129+
user_id,
130+
"Navigate to https://www.google.com and tell me what you see.",
131+
)
132+
133+
# Example interaction: Search for something
134+
await run_prompt(
135+
runner,
136+
session,
137+
user_id,
138+
"Search for 'Vertex AI Agent Engine' and tell me the first result.",
139+
)
140+
141+
end_time = time.time()
142+
143+
print("\n" + "=" * 60)
144+
print(f"Demo completed in {end_time - start_time:.2f} seconds")
145+
print("=" * 60)
146+
147+
# Print session state to show sandbox info
148+
session = await runner.session_service.get_session(
149+
app_name=app_name, user_id=user_id, session_id=session.id
150+
)
151+
print("\nSession state (sandbox info):")
152+
for key, value in session.state.items():
153+
if key.startswith("_vmaas_"):
154+
# Mask token for security
155+
if "token" in key.lower():
156+
print(f" {key}: [REDACTED]")
157+
else:
158+
print(f" {key}: {value}")
159+
160+
161+
if __name__ == "__main__":
162+
asyncio.run(main())

0 commit comments

Comments
 (0)