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(copilot): edit_agent updates existing agent instead of creating duplicate (#11981)
Ottoed07f0273877
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.pyindex b88b9b2924..f83ca30b5c 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@@ -7,15 +7,7 @@ from typing import Any, NotRequired, TypedDictfrom backend.api.features.library import db as library_dbfrom backend.api.features.store import db as store_db-from backend.data.graph import (- Graph,- Link,- Node,- create_graph,- get_graph,- get_graph_all_versions,- get_store_listed_graphs,-)+from backend.data.graph import Graph, Link, Node, get_graph, get_store_listed_graphsfrom backend.util.exceptions import DatabaseError, NotFoundErrorfrom .service import (@@ -28,8 +20,6 @@ from .service import (logger = logging.getLogger(__name__)-AGENT_EXECUTOR_BLOCK_ID = "e189baac-8c20-45a1-94a7-55177ea42565"-class ExecutionSummary(TypedDict):"""Summary of a single execution for quality assessment."""@@ -669,45 +659,6 @@ def json_to_graph(agent_json: dict[str, Any]) -> Graph:)-def _reassign_node_ids(graph: Graph) -> None:- """Reassign all node and link IDs to new UUIDs.-- This is needed when creating a new version to avoid unique constraint violations.- """- id_map = {node.id: str(uuid.uuid4()) for node in graph.nodes}-- for node in graph.nodes:- node.id = id_map[node.id]-- for link in graph.links:- link.id = str(uuid.uuid4())- if link.source_id in id_map:- link.source_id = id_map[link.source_id]- if link.sink_id in id_map:- link.sink_id = id_map[link.sink_id]---def _populate_agent_executor_user_ids(agent_json: dict[str, Any], user_id: str) -> None:- """Populate user_id in AgentExecutorBlock nodes.-- The external agent generator creates AgentExecutorBlock nodes with empty user_id.- This function fills in the actual user_id so sub-agents run with correct permissions.-- Args:- agent_json: Agent JSON dict (modified in place)- user_id: User ID to set- """- for node in agent_json.get("nodes", []):- if node.get("block_id") == AGENT_EXECUTOR_BLOCK_ID:- input_default = node.get("input_default") or {}- if not input_default.get("user_id"):- input_default["user_id"] = user_id- node["input_default"] = input_default- logger.debug(- f"Set user_id for AgentExecutorBlock node {node.get('id')}"- )--async def save_agent_to_library(agent_json: dict[str, Any], user_id: str, is_update: bool = False) -> tuple[Graph, Any]:@@ -721,35 +672,10 @@ async def save_agent_to_library(Returns:Tuple of (created Graph, LibraryAgent)"""- # Populate user_id in AgentExecutorBlock nodes before conversion- _populate_agent_executor_user_ids(agent_json, user_id)-graph = json_to_graph(agent_json)-if is_update:- if graph.id:- existing_versions = await get_graph_all_versions(graph.id, user_id)- if existing_versions:- latest_version = max(v.version for v in existing_versions)- graph.version = latest_version + 1- _reassign_node_ids(graph)- logger.info(f"Updating agent {graph.id} to version {graph.version}")- else:- graph.id = str(uuid.uuid4())- graph.version = 1- _reassign_node_ids(graph)- logger.info(f"Creating new agent with ID {graph.id}")-- created_graph = await create_graph(graph, user_id)-- library_agents = await library_db.create_library_agent(- graph=created_graph,- user_id=user_id,- sensitive_action_safe_mode=True,- create_library_agents_for_sub_graphs=False,- )-- return created_graph, library_agents[0]+ return await library_db.update_graph_in_library(graph, user_id)+ return await library_db.create_graph_in_library(graph, user_id)def graph_to_json(graph: Graph) -> dict[str, Any]:diff --git a/autogpt_platform/backend/backend/api/features/library/db.py b/autogpt_platform/backend/backend/api/features/library/db.pyindex 394f959953..6bebfb573c 100644--- a/autogpt_platform/backend/backend/api/features/library/db.py+++ b/autogpt_platform/backend/backend/api/features/library/db.py@@ -19,7 +19,10 @@ from backend.data.graph import GraphSettingsfrom backend.data.includes import AGENT_PRESET_INCLUDE, library_agent_includefrom backend.data.model import CredentialsMetaInputfrom backend.integrations.creds_manager import IntegrationCredentialsManager-from backend.integrations.webhooks.graph_lifecycle_hooks import on_graph_activate+from backend.integrations.webhooks.graph_lifecycle_hooks import (+ on_graph_activate,+ on_graph_deactivate,+)from backend.util.clients import get_scheduler_clientfrom backend.util.exceptions import DatabaseError, InvalidInputError, NotFoundErrorfrom backend.util.json import SafeJson@@ -537,6 +540,92 @@ async def update_agent_version_in_library(return library_model.LibraryAgent.from_db(lib)+async def create_graph_in_library(+ graph: graph_db.Graph,+ user_id: str,+) -> tuple[graph_db.GraphModel, library_model.LibraryAgent]:+ """Create a new graph and add it to the user's library."""+ graph.version = 1+ graph_model = graph_db.make_graph_model(graph, user_id)+ graph_model.reassign_ids(user_id=user_id, reassign_graph_id=True)++ created_graph = await graph_db.create_graph(graph_model, user_id)++ library_agents = await create_library_agent(+ graph=created_graph,+ user_id=user_id,+ sensitive_action_safe_mode=True,+ create_library_agents_for_sub_graphs=False,+ )++ if created_graph.is_active:+ created_graph = await on_graph_activate(created_graph, user_id=user_id)++ return created_graph, library_agents[0]+++async def update_graph_in_library(+ graph: graph_db.Graph,+ user_id: str,+) -> tuple[graph_db.GraphModel, library_model.LibraryAgent]:+ """Create a new version of an existing graph and update the library entry."""+ existing_versions = await graph_db.get_graph_all_versions(graph.id, user_id)+ current_active_version = (+ next((v for v in existing_versions if v.is_active), None)+ if existing_versions+ else None+ )+ graph.version = (+ max(v.version for v in existing_versions) + 1 if existing_versions else 1+ )++ graph_model = graph_db.make_graph_model(graph, user_id)+ graph_model.reassign_ids(user_id=user_id, reassign_graph_id=False)++ created_graph = await graph_db.create_graph(graph_model, user_id)++ library_agent = await get_library_agent_by_graph_id(user_id, created_graph.id)+ if not library_agent:+ raise NotFoundError(f"Library agent not found for graph {created_graph.id}")++ library_agent = await update_library_agent_version_and_settings(+ user_id, created_graph+ )++ if created_graph.is_active:+ created_graph = await on_graph_activate(created_graph, user_id=user_id)+ await graph_db.set_graph_active_version(+ graph_id=created_graph.id,+ version=created_graph.version,+ user_id=user_id,+ )+ if current_active_version:+ await on_graph_deactivate(current_active_version, user_id=user_id)++ return created_graph, library_agent+++async def update_library_agent_version_and_settings(+ user_id: str, agent_graph: graph_db.GraphModel+) -> library_model.LibraryAgent:+ """Update library agent to point to new graph version and sync settings."""+ library = await update_agent_version_in_library(+ user_id, agent_graph.id, agent_graph.version+ )+ updated_settings = GraphSettings.from_graph(+ graph=agent_graph,+ hitl_safe_mode=library.settings.human_in_the_loop_safe_mode,+ sensitive_action_safe_mode=library.settings.sensitive_action_safe_mode,+ )+ if updated_settings != library.settings:+ library = await update_library_agent(+ library_agent_id=library.id,+ user_id=user_id,+ settings=updated_settings,+ )+ return library++async def update_library_agent(library_agent_id: str,user_id: str,diff --git a/autogpt_platform/backend/backend/api/features/v1.py b/autogpt_platform/backend/backend/api/features/v1.pyindex 09d3759a65..a8610702cc 100644--- a/autogpt_platform/backend/backend/api/features/v1.py+++ b/autogpt_platform/backend/backend/api/features/v1.py@@ -101,7 +101,6 @@ from backend.util.timezone_utils import (from backend.util.virus_scanner import scan_content_safefrom .library import db as library_db-from .library import model as library_modelfrom .store.model import StoreAgentDetails@@ -823,18 +822,16 @@ async def update_graph(graph: graph_db.Graph,user_id: Annotated[str, Security(get_user_id)],) -> graph_db.GraphModel:- # Sanity checkif graph.id and graph.id != graph_id:raise HTTPException(400, detail="Graph ID does not match ID in URI")- # Determine new versionexisting_versions = await graph_db.get_graph_all_versions(graph_id, user_id=user_id)if not existing_versions:raise HTTPException(404, detail=f"Graph #{graph_id} not found")- latest_version_number = max(g.version for g in existing_versions)- graph.version = latest_version_number + 1+ graph.version = max(g.version for g in existing_versions) + 1current_active_version = next((v for v in existing_versions if v.is_active), None)+graph = graph_db.make_graph_model(graph, user_id)graph.reassign_ids(user_id=user_id, reassign_graph_id=False)graph.validate_graph(for_run=False)@@ -842,27 +839,23 @@ async def update_graph(new_graph_version = await graph_db.create_graph(graph, user_id=user_id)if new_graph_version.is_active:- # Keep the library agent up to date with the new active version- await _update_library_agent_version_and_settings(user_id, new_graph_version)-- # Handle activation of the new graph first to ensure continuity+ await library_db.update_library_agent_version_and_settings(+ user_id, new_graph_version+ )new_graph_version = await on_graph_activate(new_graph_version, user_id=user_id)- # Ensure new version is the only active versionawait graph_db.set_graph_active_version(graph_id=graph_id, version=new_graph_version.version, user_id=user_id)if current_active_version:- # Handle deactivation of the previously active versionawait on_graph_deactivate(current_active_version, user_id=user_id)- # Fetch new graph version *with sub-graphs* (needed for credentials input schema)new_graph_version_with_subgraphs = await graph_db.get_graph(graph_id,new_graph_version.version,user_id=user_id,include_subgraphs=True,)- assert new_graph_version_with_subgraphs # make type checker happy+ assert new_graph_version_with_subgraphsreturn new_graph_version_with_subgraphs@@ -900,33 +893,15 @@ async def set_graph_active_version()# Keep the library agent up to date with the new active version- await _update_library_agent_version_and_settings(user_id, new_active_graph)+ await library_db.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 versionawait on_graph_deactivate(current_active_graph, user_id=user_id)-async def _update_library_agent_version_and_settings(- user_id: str, agent_graph: graph_db.GraphModel-) -> library_model.LibraryAgent:- library = await library_db.update_agent_version_in_library(- user_id, agent_graph.id, agent_graph.version- )- updated_settings = GraphSettings.from_graph(- graph=agent_graph,- hitl_safe_mode=library.settings.human_in_the_loop_safe_mode,- sensitive_action_safe_mode=library.settings.sensitive_action_safe_mode,- )- if updated_settings != library.settings:- library = await library_db.update_library_agent(- library_agent_id=library.id,- user_id=user_id,- settings=updated_settings,- )- return library--@v1_router.patch(path="/graphs/{graph_id}/settings",summary="Update graph settings",