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(store): Fixing add agent to library (#9098)
Swifty658493559d18
diff --git a/autogpt_platform/backend/backend/data/graph.py b/autogpt_platform/backend/backend/data/graph.pyindex 1a8a021d45..551b6072d1 100644--- a/autogpt_platform/backend/backend/data/graph.py+++ b/autogpt_platform/backend/backend/data/graph.py@@ -521,13 +521,13 @@ async def get_graph("""where_clause: AgentGraphWhereInput = {"id": graph_id,- "isTemplate": template,}if version is not None:where_clause["version"] = versionelif not template:where_clause["isActive"] = True+ # TODO: Fix hack workaround to get adding store agents to workif user_id is not None and not template:where_clause["userId"] = user_iddiff --git a/autogpt_platform/backend/backend/server/v2/library/routes.py b/autogpt_platform/backend/backend/server/v2/library/routes.pyindex 7f3e89fe90..0c3b1a77ec 100644--- a/autogpt_platform/backend/backend/server/v2/library/routes.py+++ b/autogpt_platform/backend/backend/server/v2/library/routes.py@@ -4,14 +4,20 @@ import typingimport autogpt_libs.auth.dependsimport autogpt_libs.auth.middlewareimport fastapi+import prismaimport backend.data.graph+import backend.integrations.creds_manager+import backend.integrations.webhooks.graph_lifecycle_hooksimport backend.server.v2.library.dbimport backend.server.v2.library.modellogger = logging.getLogger(__name__)router = fastapi.APIRouter()+integration_creds_manager = (+ backend.integrations.creds_manager.IntegrationCredentialsManager()+)@router.get(@@ -63,10 +69,53 @@ async def add_agent_to_library(HTTPException: If there is an error adding the agent to the library"""try:- await backend.server.v2.library.db.add_agent_to_library(- store_listing_version_id=store_listing_version_id, user_id=user_id+ # Get the graph from the store listing+ store_listing_version = (+ await prisma.models.StoreListingVersion.prisma().find_unique(+ where={"id": store_listing_version_id}, include={"Agent": True}+ )+ )++ if not store_listing_version or not store_listing_version.Agent:+ raise fastapi.HTTPException(+ status_code=404,+ detail=f"Store listing version {store_listing_version_id} not found",+ )++ agent = store_listing_version.Agent++ if agent.userId == user_id:+ raise fastapi.HTTPException(+ status_code=400, detail="Cannot add own agent to library"+ )++ # Create a new graph from the template+ graph = await backend.data.graph.get_graph(+ agent.id, agent.version, template=True, user_id=user_id)++ if not graph:+ raise fastapi.HTTPException(+ status_code=404, detail=f"Agent {agent.id} not found"+ )++ # Create a deep copy with new IDs+ graph.version = 1+ graph.is_template = False+ graph.is_active = True+ graph.reassign_ids(user_id=user_id, reassign_graph_id=True)++ # Save the new graph+ graph = await backend.data.graph.create_graph(graph, user_id=user_id)+ graph = (+ await backend.integrations.webhooks.graph_lifecycle_hooks.on_graph_activate(+ graph,+ get_credentials=lambda id: integration_creds_manager.get(user_id, id),+ )+ )+return fastapi.Response(status_code=201)+except Exception:logger.exception("Exception occurred whilst adding agent to library")raise fastapi.HTTPException(