Skip to content

Commit b610fe1

Browse files
committed
Updated fast_api.py and local_storage.py for fix stray app creation, this will close issue number 6667 in adk-python github.
1 parent 6ccb837 commit b610fe1

2 files changed

Lines changed: 25 additions & 3 deletions

File tree

src/google/adk/cli/fast_api.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,31 @@
6666
from .utils.service_factory import create_artifact_service_from_options
6767
from .utils.service_factory import create_memory_service_from_options
6868
from .utils.service_factory import create_session_service_from_options
69-
69+
from ..apps.app import App
70+
from collections.abc import Mapping
7071
_ALLOWED_AGENT_ENGINE_CLASS_METHODS = frozenset(
7172
method["name"] for method in _AGENT_ENGINE_CLASS_METHODS
7273
)
7374

75+
class DynamicAppMap(Mapping):
76+
def __init__(self, agent_loader):
77+
self.agent_loader = agent_loader
78+
79+
def __getitem__(self, app_name):
80+
for folder_name in self.agent_loader.list_agents():
81+
try:
82+
loaded = self.agent_loader.load_agent(folder_name)
83+
if isinstance(loaded, App) and loaded.name == app_name:
84+
return folder_name
85+
except Exception:
86+
pass
87+
return app_name
88+
89+
def __iter__(self):
90+
return iter([])
91+
92+
def __len__(self):
93+
return 1
7494

7595
class _QueryRequest(BaseModel):
7696
input: dict[str, Any] | None = None
@@ -543,13 +563,15 @@ def get_fast_api_app(
543563
)
544564
except ValueError as exc:
545565
raise click.ClickException(str(exc)) from exc
546-
566+
567+
app_name_to_dir = DynamicAppMap(agent_loader)
547568
# Build the Session service
548569
session_service = create_session_service_from_options(
549570
base_dir=agents_dir,
550571
session_service_uri=session_service_uri,
551572
session_db_kwargs=session_db_kwargs,
552573
use_local_storage=use_local_storage,
574+
app_name_to_dir=app_name_to_dir
553575
)
554576

555577
# Build the Artifact service

src/google/adk/cli/utils/local_storage.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def __init__(
141141
app_name_to_dir: Optional[Mapping[str, str]] = None,
142142
):
143143
self._agents_root = Path(agents_root).resolve()
144-
self._app_name_to_dir = dict(app_name_to_dir or {})
144+
self._app_name_to_dir = app_name_to_dir or {}
145145
self._services: dict[str, BaseSessionService] = {}
146146
self._service_lock = asyncio.Lock()
147147

0 commit comments

Comments
 (0)