diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8e6ebc0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +.venv/ +__pycache__/ +*.py[cod] +*$py.class +*.log +emulator.log +.DS_Store diff --git a/README.md b/README.md index 994a9cb..66b8b6d 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,12 @@ # Gemini Android Computer Use Demo -This repository contains a reference implementation for controlling an Android emulator using the **Gemini 3.6 Flash** Computer Use API (`mobile` environment) via the Google GenAI SDK. +This repository contains a reference implementation for controlling an Android emulator using the **Gemini 3.7 Flash** Computer Use API (`mobile` environment) via the Google GenAI SDK. ## Overview The agent operates in a continuous loop: 1. It captures a screenshot of the virtual device using ADB. -2. It sends the screenshot along with the user's task to Gemini 3.6 Flash. +2. It sends the screenshot along with the user's task to Gemini 3.7 Flash. 3. The model returns structured tool commands (such as `click`, `type`, `long_press`, `drag_and_drop`, `press_key`, `go_back`, `wait`, `list_apps`, `open_app`, `take_screenshot`). 4. The client executing script maps the normalized coordinates (0-999) to the actual physical resolution of the screen and executes the action via ADB. 5. The loop repeats until the task is complete. @@ -44,17 +44,18 @@ export GEMINI_API_KEY="your-api-key-here" ### 3. Run the Agent -Create a virtual environment and install the required dependencies: +You can run the script directly with `uv` (dependencies are resolved automatically via PEP 723 metadata): ```bash -uv venv -source .venv/bin/activate -uv pip install -r requirements.txt +uv run agent.py "Find the latest blog post from philipp schmid and summarize it." ``` -Then run the agent: +Or using a virtual environment: ```bash +uv venv +source .venv/bin/activate +uv pip install -r requirements.txt python agent.py "Find the latest blog post from philipp schmid and summarize it." ``` @@ -64,7 +65,7 @@ python agent.py "Find the latest blog post from philipp schmid and summarize it. | Option | Short | Default | Description | | --- | --- | --- | --- | -| `--model` | `-m` | `gemini-3.6-flash` | Gemini model ID to use | +| `--model` | `-m` | `gemini-3.7-flash` | Gemini model ID to use | | `--thinking-level` | `-t` | `medium` | Thinking level configuration (`minimal`, `low`, `medium`, `high`) | | `task` | N/A | *(default prompt)* | Task description for the agent | @@ -82,7 +83,8 @@ python agent.py --thinking-level high "Open Clock and set an alarm for 7 AM" The following Gemini models are supported for computer use: -* `gemini-3.6-flash` (default) +* `gemini-3.7-flash` (default) +* `gemini-3.6-flash` * `gemini-3.5-flash-lite` * `gemini-3.5-flash` diff --git a/agent.py b/agent.py index cf953f2..be67fe9 100644 --- a/agent.py +++ b/agent.py @@ -11,6 +11,12 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +# /// script +# requires-python = ">=3.10" +# dependencies = [ +# "google-genai", +# ] +# /// import argparse import base64 @@ -190,7 +196,7 @@ def screenshot(self) -> bytes: def run_agent( task: str, device_id: str = None, - model: str = "gemini-3.6-flash", + model: str = "gemini-3.7-flash", thinking_level: str = "medium", max_turns: int = 100, ): @@ -218,75 +224,63 @@ def run_agent( while turn < max_turns: turn += 1 + generation_config = {"thinking_level": thinking_level} if thinking_level else None interaction = client.interactions.create( model=model, system_instruction=SYSTEM_PROMPT, input=user_input, tools=[{"type": "computer_use", "environment": "mobile"}], - generation_config={"thinking_config": {"thinking_level": thinking_level}}, + generation_config=generation_config, previous_interaction_id=previous_interaction_id, ) - has_function_calls = any( - step.type == "function_call" - for step in interaction.steps - ) - - if not has_function_calls: - text_response = " ".join([ - content_block.text for step in interaction.steps if step.type == "model_output" - for content_block in step.content if content_block.type == "text" - ]) - print("Agent finished:", text_response) + function_calls = [step for step in interaction.steps if step.type == "function_call"] + if not function_calls: + print("Agent finished:", interaction.output_text) break - + function_responses = [] - for step in interaction.steps: - if step.type == "function_call": - print(f"[function_call] {step.name}({step.arguments})") - handler = getattr(bridge, step.name, None) - result_text = {"status": "ok"} - - if handler: - try: - res = handler(**step.arguments) - if isinstance(res, dict): - result_text.update(res) - except Exception as e: - result_text = {"status": "error", "error": str(e)} - else: - result_text = {"status": "error", "error": f"Unknown action: {step.name}"} - - print(f"[function_result] {result_text}") - - if "safety_decision" in step.arguments: - # auto approve safety decisions for demo (legacy) - result_text["safety_acknowledgement"] = True - - screenshot_bytes = bridge.screenshot() - - fr = { - "type": "function_result", - "name": step.name, - "call_id": step.id, - "result": [ - {"type": "text", "text": json.dumps(result_text)}, - { - "type": "image", - "data": base64.b64encode(screenshot_bytes).decode(), - "mime_type": "image/png", - }, - ], - } - function_responses.append(fr) - time.sleep(0.5) - + for fc in function_calls: + print(f"[function_call] {fc.name}({fc.arguments})") + handler = getattr(bridge, fc.name, None) + result_text = {"status": "ok"} + + if handler: + try: + res = handler(**fc.arguments) + if isinstance(res, dict): + result_text.update(res) + except Exception as e: + result_text = {"status": "error", "error": str(e)} + else: + result_text = {"status": "error", "error": f"Unknown action: {fc.name}"} + + print(f"[function_result] {result_text}") + + if "safety_decision" in fc.arguments: + result_text["safety_acknowledgement"] = True + + screenshot_bytes = bridge.screenshot() + + fr = { + "type": "function_result", + "name": fc.name, + "call_id": fc.id, + "result": [ + {"type": "text", "text": json.dumps(result_text)}, + { + "type": "image", + "data": base64.b64encode(screenshot_bytes).decode(), + "mime_type": "image/png", + }, + ], + } + function_responses.append(fr) + time.sleep(0.5) + user_input = function_responses previous_interaction_id = interaction.id - - if not function_responses: - break return interaction @@ -296,8 +290,8 @@ def run_agent( parser.add_argument( "--model", "-m", - default="gemini-3.6-flash", - help="Gemini model ID to use (default: gemini-3.6-flash)", + default="gemini-3.7-flash", + help="Gemini model ID to use (default: gemini-3.7-flash)", ) parser.add_argument( "--thinking-level",