feat: support deploying config agent to Agent Engine in CLI

Both pure config and config with code are successfully deployed.

PiperOrigin-RevId: 795645102
This commit is contained in:
Liang Wu
2025-08-15 16:01:50 -07:00
committed by Copybara-Service
parent 22f34e9d2c
commit b3b70035c4
+44 -17
View File
@@ -60,9 +60,19 @@ CMD adk {command} --port={port} {host_option} {service_option} {trace_to_cloud_o
""" """
_AGENT_ENGINE_APP_TEMPLATE = """ _AGENT_ENGINE_APP_TEMPLATE = """
from {app_name}.agent import root_agent
from vertexai.preview.reasoning_engines import AdkApp from vertexai.preview.reasoning_engines import AdkApp
if {is_config_agent}:
from google.adk.agents import config_agent_utils
try:
# This path is for local loading.
root_agent = config_agent_utils.from_config("{agent_folder}/root_agent.yaml")
except FileNotFoundError:
# This path is used to support the file structure in Agent Engine.
root_agent = config_agent_utils.from_config("./{temp_folder}/{app_name}/root_agent.yaml")
else:
from {app_name}.agent import root_agent
adk_app = AdkApp( adk_app = AdkApp(
agent=root_agent, agent=root_agent,
enable_tracing={trace_to_cloud_option}, enable_tracing={trace_to_cloud_option},
@@ -293,21 +303,26 @@ def to_agent_engine(
code. code.
temp_folder (str): The temp folder for the generated Agent Engine source temp_folder (str): The temp folder for the generated Agent Engine source
files. It will be replaced with the generated files if it already exists. files. It will be replaced with the generated files if it already exists.
project (str): Google Cloud project id. adk_app (str): The name of the file (without .py) containing the AdkApp
region (str): Google Cloud region. instance.
staging_bucket (str): The GCS bucket for staging the deployment artifacts. staging_bucket (str): The GCS bucket for staging the deployment artifacts.
trace_to_cloud (bool): Whether to enable Cloud Trace. trace_to_cloud (bool): Whether to enable Cloud Trace.
agent_engine_id (str): The ID of the Agent Engine instance to update. If not agent_engine_id (str): Optional. The ID of the Agent Engine instance to
specified, a new Agent Engine instance will be created. update. If not specified, a new Agent Engine instance will be created.
absolutize_imports (bool): Whether to absolutize imports. If True, all relative absolutize_imports (bool): Optional. Default is True. Whether to absolutize
imports will be converted to absolute import statements. Default is True. imports. If True, all relative imports will be converted to absolute
requirements_file (str): The filepath to the `requirements.txt` file to use. import statements.
If not specified, the `requirements.txt` file in the `agent_folder` will project (str): Optional. Google Cloud project id.
be used. region (str): Optional. Google Cloud region.
env_file (str): The filepath to the `.env` file for environment variables. display_name (str): Optional. The display name of the Agent Engine.
If not specified, the `.env` file in the `agent_folder` will be used. The description (str): Optional. The description of the Agent Engine.
values of `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` will be requirements_file (str): Optional. The filepath to the `requirements.txt`
overridden by `project` and `region` if they are specified. file to use. If not specified, the `requirements.txt` file in the
`agent_folder` will be used.
env_file (str): Optional. The filepath to the `.env` file for environment
variables. If not specified, the `.env` file in the `agent_folder` will be
used. The values of `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION`
will be overridden by `project` and `region` if they are specified.
""" """
app_name = os.path.basename(agent_folder) app_name = os.path.basename(agent_folder)
agent_src_path = os.path.join(temp_folder, app_name) agent_src_path = os.path.join(temp_folder, app_name)
@@ -388,12 +403,21 @@ def to_agent_engine(
) )
click.echo('Vertex AI initialized.') click.echo('Vertex AI initialized.')
is_config_agent = False
config_root_agent_file = os.path.join(agent_src_path, 'root_agent.yaml')
if os.path.exists(config_root_agent_file):
click.echo('Config agent detected.')
is_config_agent = True
adk_app_file = os.path.join(temp_folder, f'{adk_app}.py') adk_app_file = os.path.join(temp_folder, f'{adk_app}.py')
with open(adk_app_file, 'w', encoding='utf-8') as f: with open(adk_app_file, 'w', encoding='utf-8') as f:
f.write( f.write(
_AGENT_ENGINE_APP_TEMPLATE.format( _AGENT_ENGINE_APP_TEMPLATE.format(
app_name=app_name, app_name=app_name,
trace_to_cloud_option=trace_to_cloud, trace_to_cloud_option=trace_to_cloud,
is_config_agent=is_config_agent,
temp_folder=temp_folder,
agent_folder=agent_folder,
) )
) )
click.echo(f'Created {adk_app_file}') click.echo(f'Created {adk_app_file}')
@@ -449,8 +473,8 @@ def to_agent_engine(
if not agent_engine_id: if not agent_engine_id:
agent_engines.create(**agent_config) agent_engines.create(**agent_config)
else: else:
name = f'projects/{project}/locations/{region}/reasoningEngines/{agent_engine_id}' resource_name = f'projects/{project}/locations/{region}/reasoningEngines/{agent_engine_id}'
agent_engines.update(resource_name=name, **agent_config) agent_engines.update(resource_name=resource_name, **agent_config)
finally: finally:
click.echo(f'Cleaning up the temp folder: {temp_folder}') click.echo(f'Cleaning up the temp folder: {temp_folder}')
shutil.rmtree(temp_folder) shutil.rmtree(temp_folder)
@@ -485,7 +509,10 @@ def to_gke(
cluster_name: The name of the GKE cluster. cluster_name: The name of the GKE cluster.
service_name: The service name in GKE. service_name: The service name in GKE.
app_name: The name of the app, by default, it's basename of `agent_folder`. app_name: The name of the app, by default, it's basename of `agent_folder`.
temp_folder: The local directory to use as a temporary workspace for preparing deployment artifacts. The tool populates this folder with a copy of the agent's source code and auto-generates necessary files like a Dockerfile and deployment.yaml. temp_folder: The local directory to use as a temporary workspace for
preparing deployment artifacts. The tool populates this folder with a copy
of the agent's source code and auto-generates necessary files like a
Dockerfile and deployment.yaml.
port: The port of the ADK api server. port: The port of the ADK api server.
trace_to_cloud: Whether to enable Cloud Trace. trace_to_cloud: Whether to enable Cloud Trace.
with_ui: Whether to deploy with UI. with_ui: Whether to deploy with UI.