@@ -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