Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

fix(backend): fix broken update_agent_version_in_library and reduce the method code duplication (#11514)

Zamil Majdy6590fcb76f65
diff --git a/autogpt_platform/backend/backend/server/routers/v1.py b/autogpt_platform/backend/backend/server/routers/v1.py
index 7ad812af8a..de4d8137c4 100644
--- a/autogpt_platform/backend/backend/server/routers/v1.py
+++ b/autogpt_platform/backend/backend/server/routers/v1.py
@@ -143,6 +143,28 @@ async def hide_activity_summary_if_disabled(
return execution
+async def _update_library_agent_version_and_settings(
+ user_id: str, agent_graph: graph_db.GraphModel
+) -> library_db.library_model.LibraryAgent:
+ # Keep the library agent up to date with the new active version
+ library = await library_db.update_agent_version_in_library(
+ user_id, agent_graph.id, agent_graph.version
+ )
+ # If the graph has HITL node, initialize the setting if it's not already set.
+ if (
+ agent_graph.has_human_in_the_loop
+ and library.settings.human_in_the_loop_safe_mode is None
+ ):
+ await library_db.update_library_agent_settings(
+ user_id=user_id,
+ agent_id=library.id,
+ settings=library.settings.model_copy(
+ update={"human_in_the_loop_safe_mode": True}
+ ),
+ )
+ return library
+
+
# Define the API routes
v1_router = APIRouter()
@@ -838,18 +860,7 @@ async def update_graph(
if new_graph_version.is_active:
# Keep the library agent up to date with the new active version
- library = await library_db.update_agent_version_in_library(
- user_id, graph.id, graph.version
- )
- if (
- new_graph_version.has_human_in_the_loop
- and library.settings.human_in_the_loop_safe_mode is None
- ):
- await library_db.update_library_agent_settings(
- user_id=user_id,
- agent_id=library.id,
- settings=GraphSettings(human_in_the_loop_safe_mode=True),
- )
+ await _update_library_agent_version_and_settings(user_id, new_graph_version)
# Handle activation of the new graph first to ensure continuity
new_graph_version = await on_graph_activate(new_graph_version, user_id=user_id)
@@ -906,18 +917,7 @@ async def set_graph_active_version(
)
# Keep the library agent up to date with the new active version
- library = await library_db.update_agent_version_in_library(
- user_id, new_active_graph.id, new_active_graph.version
- )
- if (
- new_active_graph.has_human_in_the_loop
- and library.settings.human_in_the_loop_safe_mode is None
- ):
- await library_db.update_library_agent_settings(
- user_id=user_id,
- agent_id=library.id,
- settings=GraphSettings(human_in_the_loop_safe_mode=True),
- )
+ await _update_library_agent_version_and_settings(user_id, new_active_graph)
if current_active_graph and current_active_graph.version != new_active_version:
# Handle deactivation of the previously active version
diff --git a/autogpt_platform/backend/backend/server/v2/library/db.py b/autogpt_platform/backend/backend/server/v2/library/db.py
index aae6e4cc65..17a0efa7be 100644
--- a/autogpt_platform/backend/backend/server/v2/library/db.py
+++ b/autogpt_platform/backend/backend/server/v2/library/db.py
@@ -523,6 +523,7 @@ async def update_agent_version_in_library(
},
},
},
+ include={"AgentGraph": True},
)
if lib is None:
raise NotFoundError(f"Library agent {library_agent.id} not found")