mirror of
https://github.com/encounter/adk-python.git
synced 2026-07-09 18:19:28 -07:00
fix(config): fix adk create --type=config
Previously click didn't convert the input into the enum type. PiperOrigin-RevId: 791922529
This commit is contained in:
committed by
Copybara-Service
parent
6277dae749
commit
dc193f7969
@@ -14,7 +14,6 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import enum
|
||||
import os
|
||||
import subprocess
|
||||
from typing import Optional
|
||||
@@ -22,12 +21,6 @@ from typing import Tuple
|
||||
|
||||
import click
|
||||
|
||||
|
||||
class Type(enum.Enum):
|
||||
CONFIG = "config"
|
||||
CODE = "code"
|
||||
|
||||
|
||||
_INIT_PY_TEMPLATE = """\
|
||||
from . import agent
|
||||
"""
|
||||
@@ -179,7 +172,7 @@ def _generate_files(
|
||||
google_cloud_project: Optional[str] = None,
|
||||
google_cloud_region: Optional[str] = None,
|
||||
model: Optional[str] = None,
|
||||
type: Optional[Type] = None,
|
||||
type: str,
|
||||
):
|
||||
"""Generates a folder name for the agent."""
|
||||
os.makedirs(agent_folder, exist_ok=True)
|
||||
@@ -203,7 +196,7 @@ def _generate_files(
|
||||
lines.append(f"GOOGLE_CLOUD_LOCATION={google_cloud_region}")
|
||||
f.write("\n".join(lines))
|
||||
|
||||
if type == Type.CONFIG:
|
||||
if type == "config":
|
||||
with open(agent_config_file_path, "w", encoding="utf-8") as f:
|
||||
f.write(_AGENT_CONFIG_TEMPLATE.format(model_name=model))
|
||||
with open(init_file_path, "w", encoding="utf-8") as f:
|
||||
@@ -263,7 +256,7 @@ def _prompt_to_choose_backend(
|
||||
return google_api_key, google_cloud_project, google_cloud_region
|
||||
|
||||
|
||||
def _prompt_to_choose_type() -> Type:
|
||||
def _prompt_to_choose_type() -> str:
|
||||
"""Prompts user to choose type of agent to create."""
|
||||
type_choice = click.prompt(
|
||||
"""\
|
||||
@@ -274,9 +267,9 @@ Choose type""",
|
||||
type=click.Choice(["1", "2"]),
|
||||
)
|
||||
if type_choice == "1":
|
||||
return Type.CONFIG
|
||||
return "CONFIG"
|
||||
else:
|
||||
return Type.CODE
|
||||
return "CODE"
|
||||
|
||||
|
||||
def run_cmd(
|
||||
@@ -286,7 +279,7 @@ def run_cmd(
|
||||
google_api_key: Optional[str],
|
||||
google_cloud_project: Optional[str],
|
||||
google_cloud_region: Optional[str],
|
||||
type: Optional[Type],
|
||||
type: Optional[str],
|
||||
):
|
||||
"""Runs `adk create` command to create agent template.
|
||||
|
||||
@@ -298,7 +291,7 @@ def run_cmd(
|
||||
VertexAI as backend.
|
||||
google_cloud_region: Optional[str], The Google Cloud region for using
|
||||
VertexAI as backend.
|
||||
type: Optional[Type], Whether to define agent with config file or code.
|
||||
type: Optional[str], Whether to define agent with config file or code.
|
||||
"""
|
||||
agent_folder = os.path.join(os.getcwd(), agent_name)
|
||||
# check folder doesn't exist or it's empty. Otherwise, throw
|
||||
@@ -331,5 +324,5 @@ def run_cmd(
|
||||
google_cloud_project=google_cloud_project,
|
||||
google_cloud_region=google_cloud_region,
|
||||
model=model,
|
||||
type=type,
|
||||
type=type.lower(),
|
||||
)
|
||||
|
||||
@@ -145,13 +145,13 @@ def deploy():
|
||||
)
|
||||
@click.option(
|
||||
"--type",
|
||||
type=click.Choice([t.value for t in cli_create.Type]),
|
||||
type=click.Choice(["CODE", "CONFIG"], case_sensitive=False),
|
||||
help=(
|
||||
"EXPERIMENTAL Optional. Type of agent to create: 'config' or 'code'."
|
||||
" 'config' is not ready for use so it defaults to 'code'. It may change"
|
||||
" later once 'config' is ready for use."
|
||||
),
|
||||
default=cli_create.Type.CODE.value,
|
||||
default="CODE",
|
||||
show_default=True,
|
||||
hidden=True, # Won't show in --help output. Not ready for use.
|
||||
)
|
||||
@@ -162,7 +162,7 @@ def cli_create_cmd(
|
||||
api_key: Optional[str],
|
||||
project: Optional[str],
|
||||
region: Optional[str],
|
||||
type: Optional[cli_create.Type],
|
||||
type: Optional[str],
|
||||
):
|
||||
"""Creates a new app in the current folder with prepopulated agent template.
|
||||
|
||||
|
||||
@@ -62,6 +62,7 @@ def test_generate_files_with_api_key(agent_folder: Path) -> None:
|
||||
str(agent_folder),
|
||||
google_api_key="dummy-key",
|
||||
model="gemini-2.0-flash-001",
|
||||
type="code",
|
||||
)
|
||||
|
||||
env_content = (agent_folder / ".env").read_text()
|
||||
@@ -78,6 +79,7 @@ def test_generate_files_with_gcp(agent_folder: Path) -> None:
|
||||
google_cloud_project="proj",
|
||||
google_cloud_region="us-central1",
|
||||
model="gemini-2.0-flash-001",
|
||||
type="code",
|
||||
)
|
||||
|
||||
env_content = (agent_folder / ".env").read_text()
|
||||
@@ -95,6 +97,7 @@ def test_generate_files_overwrite(agent_folder: Path) -> None:
|
||||
str(agent_folder),
|
||||
google_api_key="new-key",
|
||||
model="gemini-2.0-flash-001",
|
||||
type="code",
|
||||
)
|
||||
|
||||
assert "GOOGLE_API_KEY=new-key" in (agent_folder / ".env").read_text()
|
||||
@@ -108,12 +111,16 @@ def test_generate_files_permission_error(
|
||||
os, "makedirs", lambda *a, **k: (_ for _ in ()).throw(PermissionError())
|
||||
)
|
||||
with pytest.raises(PermissionError):
|
||||
cli_create._generate_files(str(agent_folder), model="gemini-2.0-flash-001")
|
||||
cli_create._generate_files(
|
||||
str(agent_folder), model="gemini-2.0-flash-001", type="code"
|
||||
)
|
||||
|
||||
|
||||
def test_generate_files_no_params(agent_folder: Path) -> None:
|
||||
"""No backend parameters → minimal .env file is generated."""
|
||||
cli_create._generate_files(str(agent_folder), model="gemini-2.0-flash-001")
|
||||
cli_create._generate_files(
|
||||
str(agent_folder), model="gemini-2.0-flash-001", type="code"
|
||||
)
|
||||
|
||||
env_content = (agent_folder / ".env").read_text()
|
||||
for key in (
|
||||
@@ -147,7 +154,7 @@ def test_run_cmd_overwrite_reject(
|
||||
google_api_key=None,
|
||||
google_cloud_project=None,
|
||||
google_cloud_region=None,
|
||||
type=cli_create.Type.CODE,
|
||||
type="code",
|
||||
)
|
||||
|
||||
|
||||
@@ -166,7 +173,7 @@ def test_run_cmd_with_type_config(
|
||||
google_api_key="test-key",
|
||||
google_cloud_project=None,
|
||||
google_cloud_region=None,
|
||||
type=cli_create.Type.CONFIG,
|
||||
type="config",
|
||||
)
|
||||
|
||||
agent_dir = tmp_path / agent_name
|
||||
|
||||
Reference in New Issue
Block a user