Skip to content

Commit 2e77804

Browse files
DeanChensjcopybara-github
authored andcommitted
feat: Support passing fully qualified agent engine resource name when constructing session service and memory service
Resolves #1760 PiperOrigin-RevId: 784353411
1 parent 36e45cd commit 2e77804

File tree

2 files changed

+46
-18
lines changed

2 files changed

+46
-18
lines changed

src/google/adk/cli/cli_tools_click.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,10 @@ def decorator(func):
461461
"--session_service_uri",
462462
help=(
463463
"""Optional. The URI of the session service.
464-
- Use 'agentengine://<agent_engine_resource_id>' to connect to Agent Engine sessions.
464+
- Use 'agentengine://<agent_engine>' to connect to Agent Engine
465+
sessions. <agent_engine> can either be the full qualified resource
466+
name 'projects/abc/locations/us-central1/reasoningEngines/123' or
467+
the resource id '123'.
465468
- Use 'sqlite://<path_to_sqlite_file>' to connect to a SQLite DB.
466469
- See https://docs.sqlalchemy.org/en/20/core/engines.html#backend-specific-urls for more details on supported database URIs."""
467470
),
@@ -487,11 +490,12 @@ def decorator(func):
487490
@click.option(
488491
"--memory_service_uri",
489492
type=str,
490-
help=(
491-
"""Optional. The URI of the memory service.
493+
help=("""Optional. The URI of the memory service.
492494
- Use 'rag://<rag_corpus_id>' to connect to Vertex AI Rag Memory Service.
493-
- Use 'agentengine://<agent_engine_resource_id>' to connect to Vertex AI Memory Bank Service. e.g. agentengine://12345"""
494-
),
495+
- Use 'agentengine://<agent_engine>' to connect to Agent Engine
496+
sessions. <agent_engine> can either be the full qualified resource
497+
name 'projects/abc/locations/us-central1/reasoningEngines/123' or
498+
the resource id '123'."""),
495499
default=None,
496500
)
497501
@functools.wraps(func)

src/google/adk/cli/fast_api.py

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,31 @@ async def internal_lifespan(app: FastAPI):
297297
eval_sets_manager = LocalEvalSetsManager(agents_dir=agents_dir)
298298
eval_set_results_manager = LocalEvalSetResultsManager(agents_dir=agents_dir)
299299

300+
def _parse_agent_engine_resource_name(agent_engine_id_or_resource_name):
301+
if not agent_engine_id_or_resource_name:
302+
raise click.ClickException(
303+
"Agent engine resource name or resource id can not be empty."
304+
)
305+
306+
# "projects/my-project/locations/us-central1/reasoningEngines/1234567890",
307+
if "/" in agent_engine_id_or_resource_name:
308+
# Validate resource name.
309+
if len(agent_engine_id_or_resource_name.split("/")) != 6:
310+
raise click.ClickException(
311+
"Agent engine resource name is mal-formatted. It should be of"
312+
" format :"
313+
" projects/{project_id}/locations/{location}/reasoningEngines/{resource_id}"
314+
)
315+
project = agent_engine_id_or_resource_name.split("/")[1]
316+
location = agent_engine_id_or_resource_name.split("/")[3]
317+
agent_engine_id = agent_engine_id_or_resource_name.split("/")[-1]
318+
else:
319+
envs.load_dotenv_for_agent("", agents_dir)
320+
project = os.environ["GOOGLE_CLOUD_PROJECT"]
321+
location = os.environ["GOOGLE_CLOUD_LOCATION"]
322+
agent_engine_id = agent_engine_id_or_resource_name
323+
return project, location, agent_engine_id
324+
300325
# Build the Memory service
301326
if memory_service_uri:
302327
if memory_service_uri.startswith("rag://"):
@@ -308,13 +333,13 @@ async def internal_lifespan(app: FastAPI):
308333
rag_corpus=f'projects/{os.environ["GOOGLE_CLOUD_PROJECT"]}/locations/{os.environ["GOOGLE_CLOUD_LOCATION"]}/ragCorpora/{rag_corpus}'
309334
)
310335
elif memory_service_uri.startswith("agentengine://"):
311-
agent_engine_id = memory_service_uri.split("://")[1]
312-
if not agent_engine_id:
313-
raise click.ClickException("Agent engine id can not be empty.")
314-
envs.load_dotenv_for_agent("", agents_dir)
336+
agent_engine_id_or_resource_name = memory_service_uri.split("://")[1]
337+
project, location, agent_engine_id = _parse_agent_engine_resource_name(
338+
agent_engine_id_or_resource_name
339+
)
315340
memory_service = VertexAiMemoryBankService(
316-
project=os.environ["GOOGLE_CLOUD_PROJECT"],
317-
location=os.environ["GOOGLE_CLOUD_LOCATION"],
341+
project=project,
342+
location=location,
318343
agent_engine_id=agent_engine_id,
319344
)
320345
else:
@@ -327,14 +352,13 @@ async def internal_lifespan(app: FastAPI):
327352
# Build the Session service
328353
if session_service_uri:
329354
if session_service_uri.startswith("agentengine://"):
330-
# Create vertex session service
331-
agent_engine_id = session_service_uri.split("://")[1]
332-
if not agent_engine_id:
333-
raise click.ClickException("Agent engine id can not be empty.")
334-
envs.load_dotenv_for_agent("", agents_dir)
355+
agent_engine_id_or_resource_name = session_service_uri.split("://")[1]
356+
project, location, agent_engine_id = _parse_agent_engine_resource_name(
357+
agent_engine_id_or_resource_name
358+
)
335359
session_service = VertexAiSessionService(
336-
project=os.environ["GOOGLE_CLOUD_PROJECT"],
337-
location=os.environ["GOOGLE_CLOUD_LOCATION"],
360+
project=project,
361+
location=location,
338362
agent_engine_id=agent_engine_id,
339363
)
340364
else:

0 commit comments

Comments
 (0)