Skip to content

Commit dc193f7

Browse files
wuliang229copybara-github
authored andcommitted
fix(config): fix adk create --type=config
Previously click didn't convert the input into the enum type. PiperOrigin-RevId: 791922529
1 parent 6277dae commit dc193f7

File tree

3 files changed

+22
-22
lines changed

3 files changed

+22
-22
lines changed

src/google/adk/cli/cli_create.py

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,13 @@
1414

1515
from __future__ import annotations
1616

17-
import enum
1817
import os
1918
import subprocess
2019
from typing import Optional
2120
from typing import Tuple
2221

2322
import click
2423

25-
26-
class Type(enum.Enum):
27-
CONFIG = "config"
28-
CODE = "code"
29-
30-
3124
_INIT_PY_TEMPLATE = """\
3225
from . import agent
3326
"""
@@ -179,7 +172,7 @@ def _generate_files(
179172
google_cloud_project: Optional[str] = None,
180173
google_cloud_region: Optional[str] = None,
181174
model: Optional[str] = None,
182-
type: Optional[Type] = None,
175+
type: str,
183176
):
184177
"""Generates a folder name for the agent."""
185178
os.makedirs(agent_folder, exist_ok=True)
@@ -203,7 +196,7 @@ def _generate_files(
203196
lines.append(f"GOOGLE_CLOUD_LOCATION={google_cloud_region}")
204197
f.write("\n".join(lines))
205198

206-
if type == Type.CONFIG:
199+
if type == "config":
207200
with open(agent_config_file_path, "w", encoding="utf-8") as f:
208201
f.write(_AGENT_CONFIG_TEMPLATE.format(model_name=model))
209202
with open(init_file_path, "w", encoding="utf-8") as f:
@@ -263,7 +256,7 @@ def _prompt_to_choose_backend(
263256
return google_api_key, google_cloud_project, google_cloud_region
264257

265258

266-
def _prompt_to_choose_type() -> Type:
259+
def _prompt_to_choose_type() -> str:
267260
"""Prompts user to choose type of agent to create."""
268261
type_choice = click.prompt(
269262
"""\
@@ -274,9 +267,9 @@ def _prompt_to_choose_type() -> Type:
274267
type=click.Choice(["1", "2"]),
275268
)
276269
if type_choice == "1":
277-
return Type.CONFIG
270+
return "CONFIG"
278271
else:
279-
return Type.CODE
272+
return "CODE"
280273

281274

282275
def run_cmd(
@@ -286,7 +279,7 @@ def run_cmd(
286279
google_api_key: Optional[str],
287280
google_cloud_project: Optional[str],
288281
google_cloud_region: Optional[str],
289-
type: Optional[Type],
282+
type: Optional[str],
290283
):
291284
"""Runs `adk create` command to create agent template.
292285
@@ -298,7 +291,7 @@ def run_cmd(
298291
VertexAI as backend.
299292
google_cloud_region: Optional[str], The Google Cloud region for using
300293
VertexAI as backend.
301-
type: Optional[Type], Whether to define agent with config file or code.
294+
type: Optional[str], Whether to define agent with config file or code.
302295
"""
303296
agent_folder = os.path.join(os.getcwd(), agent_name)
304297
# check folder doesn't exist or it's empty. Otherwise, throw
@@ -331,5 +324,5 @@ def run_cmd(
331324
google_cloud_project=google_cloud_project,
332325
google_cloud_region=google_cloud_region,
333326
model=model,
334-
type=type,
327+
type=type.lower(),
335328
)

src/google/adk/cli/cli_tools_click.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,13 @@ def deploy():
145145
)
146146
@click.option(
147147
"--type",
148-
type=click.Choice([t.value for t in cli_create.Type]),
148+
type=click.Choice(["CODE", "CONFIG"], case_sensitive=False),
149149
help=(
150150
"EXPERIMENTAL Optional. Type of agent to create: 'config' or 'code'."
151151
" 'config' is not ready for use so it defaults to 'code'. It may change"
152152
" later once 'config' is ready for use."
153153
),
154-
default=cli_create.Type.CODE.value,
154+
default="CODE",
155155
show_default=True,
156156
hidden=True, # Won't show in --help output. Not ready for use.
157157
)
@@ -162,7 +162,7 @@ def cli_create_cmd(
162162
api_key: Optional[str],
163163
project: Optional[str],
164164
region: Optional[str],
165-
type: Optional[cli_create.Type],
165+
type: Optional[str],
166166
):
167167
"""Creates a new app in the current folder with prepopulated agent template.
168168

tests/unittests/cli/utils/test_cli_create.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ def test_generate_files_with_api_key(agent_folder: Path) -> None:
6262
str(agent_folder),
6363
google_api_key="dummy-key",
6464
model="gemini-2.0-flash-001",
65+
type="code",
6566
)
6667

6768
env_content = (agent_folder / ".env").read_text()
@@ -78,6 +79,7 @@ def test_generate_files_with_gcp(agent_folder: Path) -> None:
7879
google_cloud_project="proj",
7980
google_cloud_region="us-central1",
8081
model="gemini-2.0-flash-001",
82+
type="code",
8183
)
8284

8385
env_content = (agent_folder / ".env").read_text()
@@ -95,6 +97,7 @@ def test_generate_files_overwrite(agent_folder: Path) -> None:
9597
str(agent_folder),
9698
google_api_key="new-key",
9799
model="gemini-2.0-flash-001",
100+
type="code",
98101
)
99102

100103
assert "GOOGLE_API_KEY=new-key" in (agent_folder / ".env").read_text()
@@ -108,12 +111,16 @@ def test_generate_files_permission_error(
108111
os, "makedirs", lambda *a, **k: (_ for _ in ()).throw(PermissionError())
109112
)
110113
with pytest.raises(PermissionError):
111-
cli_create._generate_files(str(agent_folder), model="gemini-2.0-flash-001")
114+
cli_create._generate_files(
115+
str(agent_folder), model="gemini-2.0-flash-001", type="code"
116+
)
112117

113118

114119
def test_generate_files_no_params(agent_folder: Path) -> None:
115120
"""No backend parameters → minimal .env file is generated."""
116-
cli_create._generate_files(str(agent_folder), model="gemini-2.0-flash-001")
121+
cli_create._generate_files(
122+
str(agent_folder), model="gemini-2.0-flash-001", type="code"
123+
)
117124

118125
env_content = (agent_folder / ".env").read_text()
119126
for key in (
@@ -147,7 +154,7 @@ def test_run_cmd_overwrite_reject(
147154
google_api_key=None,
148155
google_cloud_project=None,
149156
google_cloud_region=None,
150-
type=cli_create.Type.CODE,
157+
type="code",
151158
)
152159

153160

@@ -166,7 +173,7 @@ def test_run_cmd_with_type_config(
166173
google_api_key="test-key",
167174
google_cloud_project=None,
168175
google_cloud_region=None,
169-
type=cli_create.Type.CONFIG,
176+
type="config",
170177
)
171178

172179
agent_dir = tmp_path / agent_name

0 commit comments

Comments
 (0)