diff --git a/autogpt_platform/backend/backend/data/graph.py b/autogpt_platform/backend/backend/data/graph.py index 1a8a021d45f0..551b6072d1c3 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"] = version elif not template: where_clause["isActive"] = True + # TODO: Fix hack workaround to get adding store agents to work if user_id is not None and not template: where_clause["userId"] = user_id diff --git a/autogpt_platform/backend/backend/server/v2/library/routes.py b/autogpt_platform/backend/backend/server/v2/library/routes.py index 7f3e89fe903a..0c3b1a77ec93 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 autogpt_libs.auth.depends import autogpt_libs.auth.middleware import fastapi +import prisma import backend.data.graph +import backend.integrations.creds_manager +import backend.integrations.webhooks.graph_lifecycle_hooks import backend.server.v2.library.db import backend.server.v2.library.model logger = 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(