|
| 1 | +# Copyright 2026 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 | +"""A ManagedAgent that runs server-side code execution. |
| 16 | +
|
| 17 | +``ManagedAgent`` calls the Managed Agents API directly from its run loop instead |
| 18 | +of running a local model loop. It currently supports server-side tools only. |
| 19 | +
|
| 20 | +Unlike ``LlmAgent``, ``ManagedAgent`` has no ``code_executor`` field, so code |
| 21 | +execution is enabled by passing the raw built-in tool config |
| 22 | +``types.Tool(code_execution=types.ToolCodeExecution())`` in ``tools`` -- the |
| 23 | +same config ``BuiltInCodeExecutor`` produces under the hood. The model writes |
| 24 | +and runs code on the server to compute answers. |
| 25 | +
|
| 26 | +A fresh remote sandbox is provisioned via ``environment={'type': 'remote'}``; |
| 27 | +the environment id is recovered from prior events so multi-turn conversations |
| 28 | +reuse the same sandbox. |
| 29 | +
|
| 30 | +Run with ``adk web`` / |
| 31 | +``adk run contributing/samples/managed_agent/code_execution``. See the README |
| 32 | +for the required environment / auth setup. |
| 33 | +""" |
| 34 | + |
| 35 | +import os |
| 36 | + |
| 37 | +from google.adk.agents import ManagedAgent |
| 38 | +from google.genai import types |
| 39 | + |
| 40 | +# The Managed Agent id served by the Managed Agents API. Override with the |
| 41 | +# MANAGED_AGENT_ID environment variable if your project has access to a |
| 42 | +# different agent. |
| 43 | +_DEFAULT_AGENT_ID = 'antigravity-preview-05-2026' |
| 44 | + |
| 45 | +root_agent = ManagedAgent( |
| 46 | + name='managed_code_execution_agent', |
| 47 | + agent_id=os.environ.get('MANAGED_AGENT_ID', _DEFAULT_AGENT_ID), |
| 48 | + # Provision a remote sandbox for the agent. The environment id is recovered |
| 49 | + # from prior events, so follow-up turns reuse the same sandbox. |
| 50 | + environment={'type': 'remote'}, |
| 51 | + # ManagedAgent has no `code_executor` field; enable server-side code |
| 52 | + # execution by passing the raw built-in tool config. This is the same config |
| 53 | + # BuiltInCodeExecutor appends for a regular LlmAgent. |
| 54 | + tools=[types.Tool(code_execution=types.ToolCodeExecution())], |
| 55 | +) |
0 commit comments