@@ -103,6 +103,14 @@ def get_agent(db: Session, agent_id: Union[uuid.UUID, str]) -> Optional[Agent]:
103103 logger .warning (f"Agent not found: { agent_id } " )
104104 return None
105105
106+ # Sanitize agent name if it contains spaces or special characters
107+ if agent .name and any (c for c in agent .name if not (c .isalnum () or c == "_" )):
108+ agent .name = "" .join (
109+ c if c .isalnum () or c == "_" else "_" for c in agent .name
110+ )
111+ # Update in database
112+ db .commit ()
113+
106114 return agent
107115 except SQLAlchemyError as e :
108116 logger .error (f"Error searching for agent { agent_id } : { str (e )} " )
@@ -144,6 +152,17 @@ def get_agents_by_client(
144152
145153 agents = query .offset (skip ).limit (limit ).all ()
146154
155+ # Sanitize agent names if they contain spaces or special characters
156+ for agent in agents :
157+ if agent .name and any (
158+ c for c in agent .name if not (c .isalnum () or c == "_" )
159+ ):
160+ agent .name = "" .join (
161+ c if c .isalnum () or c == "_" else "_" for c in agent .name
162+ )
163+ # Update in database
164+ db .commit ()
165+
147166 return agents
148167 except SQLAlchemyError as e :
149168 logger .error (f"Error searching for client agents { client_id } : { str (e )} " )
@@ -176,7 +195,15 @@ async def create_agent(db: Session, agent: AgentCreate) -> Agent:
176195 agent_card = response .json ()
177196
178197 # Update agent with information from agent card
179- agent .name = agent_card .get ("name" , "Unknown Agent" )
198+ # Only update name if not provided or empty, or sanitize it
199+ if not agent .name or agent .name .strip () == "" :
200+ # Sanitize name: remove spaces and special characters
201+ card_name = agent_card .get ("name" , "Unknown Agent" )
202+ sanitized_name = "" .join (
203+ c if c .isalnum () or c == "_" else "_" for c in card_name
204+ )
205+ agent .name = sanitized_name
206+
180207 agent .description = agent_card .get ("description" , "" )
181208
182209 if agent .config is None :
@@ -499,7 +526,14 @@ async def update_agent(
499526 )
500527 agent_card = response .json ()
501528
502- agent_data ["name" ] = agent_card .get ("name" , "Unknown Agent" )
529+ # Only update name if the original update doesn't specify a name
530+ if "name" not in agent_data or not agent_data ["name" ].strip ():
531+ # Sanitize name: remove spaces and special characters
532+ card_name = agent_card .get ("name" , "Unknown Agent" )
533+ sanitized_name = "" .join (
534+ c if c .isalnum () or c == "_" else "_" for c in card_name
535+ )
536+ agent_data ["name" ] = sanitized_name
503537 agent_data ["description" ] = agent_card .get ("description" , "" )
504538
505539 if "config" not in agent_data or agent_data ["config" ] is None :
@@ -537,7 +571,14 @@ async def update_agent(
537571 )
538572 agent_card = response .json ()
539573
540- agent_data ["name" ] = agent_card .get ("name" , "Unknown Agent" )
574+ # Only update name if the original update doesn't specify a name
575+ if "name" not in agent_data or not agent_data ["name" ].strip ():
576+ # Sanitize name: remove spaces and special characters
577+ card_name = agent_card .get ("name" , "Unknown Agent" )
578+ sanitized_name = "" .join (
579+ c if c .isalnum () or c == "_" else "_" for c in card_name
580+ )
581+ agent_data ["name" ] = sanitized_name
541582 agent_data ["description" ] = agent_card .get ("description" , "" )
542583
543584 if "config" not in agent_data or agent_data ["config" ] is None :
0 commit comments