From a3fe1ede556fea71efd0e6a95d72a3c783d919c3 Mon Sep 17 00:00:00 2001 From: Zamil Majdy Date: Thu, 29 Jan 2026 18:22:12 -0600 Subject: [PATCH] fix: address PR review comments - Add try/except error handling to get_library_agents_for_generation for graceful degradation (consistent with marketplace search) - Add null checks when deduplicating agents by name to prevent AttributeError if agent name is None - Use actual graph ID from current_agent in edit_agent.py to properly exclude the agent being edited (agent_id might be a library agent ID) --- .../chat/tools/agent_generator/core.py | 47 ++++++++++--------- .../api/features/chat/tools/edit_agent.py | 5 +- 2 files changed, 30 insertions(+), 22 deletions(-) diff --git a/autogpt_platform/backend/backend/api/features/chat/tools/agent_generator/core.py b/autogpt_platform/backend/backend/api/features/chat/tools/agent_generator/core.py index 0f4944dea0..88262b0cef 100644 --- a/autogpt_platform/backend/backend/api/features/chat/tools/agent_generator/core.py +++ b/autogpt_platform/backend/backend/api/features/chat/tools/agent_generator/core.py @@ -56,26 +56,30 @@ async def get_library_agents_for_generation( Returns: List of library agent dicts with schemas for sub-agent composition """ - response = await library_db.list_library_agents( - user_id=user_id, - search_term=search_query, # Use search API - page=1, - page_size=max_results, - ) + try: + response = await library_db.list_library_agents( + user_id=user_id, + search_term=search_query, # Use search API + page=1, + page_size=max_results, + ) - return [ - { - "graph_id": agent.graph_id, - "graph_version": agent.graph_version, - "name": agent.name, - "description": agent.description, - "input_schema": agent.input_schema, - "output_schema": agent.output_schema, - } - for agent in response.agents - # Exclude the agent being generated/edited to prevent circular references - if exclude_graph_id is None or agent.graph_id != exclude_graph_id - ] + return [ + { + "graph_id": agent.graph_id, + "graph_version": agent.graph_version, + "name": agent.name, + "description": agent.description, + "input_schema": agent.input_schema, + "output_schema": agent.output_schema, + } + for agent in response.agents + # Exclude the agent being generated/edited to prevent circular references + if exclude_graph_id is None or agent.graph_id != exclude_graph_id + ] + except Exception as e: + logger.warning(f"Failed to fetch library agents: {e}") + return [] async def search_marketplace_agents_for_generation( @@ -163,9 +167,10 @@ async def get_all_relevant_agents_for_generation( max_results=max_marketplace_results, ) # Add marketplace agents that aren't already in library (by name) - library_names = {a["name"].lower() for a in library_agents} + library_names = {a["name"].lower() for a in library_agents if a.get("name")} for agent in marketplace_agents: - if agent["name"].lower() not in library_names: + agent_name = agent.get("name") + if agent_name and agent_name.lower() not in library_names: agents.append(agent) return agents diff --git a/autogpt_platform/backend/backend/api/features/chat/tools/edit_agent.py b/autogpt_platform/backend/backend/api/features/chat/tools/edit_agent.py index e4792821e6..f7c1784e1a 100644 --- a/autogpt_platform/backend/backend/api/features/chat/tools/edit_agent.py +++ b/autogpt_platform/backend/backend/api/features/chat/tools/edit_agent.py @@ -132,10 +132,13 @@ class EditAgentTool(BaseTool): library_agents = None if user_id: try: + # Use the actual graph ID from current_agent to properly exclude + # the agent being edited (agent_id might be a library agent ID) + exclude_id = current_agent.get("id") or agent_id library_agents = await get_all_relevant_agents_for_generation( user_id=user_id, search_query=changes, # Use changes as search term - exclude_graph_id=agent_id, # Don't include the agent being edited + exclude_graph_id=exclude_id, # Don't include the agent being edited include_marketplace=True, ) logger.debug(