|
| 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