Skip to content

Commit

Permalink
fix(store): Fixing add agent to library (#9098)
Browse files Browse the repository at this point in the history
Do a deep copy of the store agent so the new agent is under the current
users id

⚠️  Hacky fix!!
  • Loading branch information
Swiftyos authored Dec 20, 2024
1 parent 6025506 commit 6584935
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
2 changes: 1 addition & 1 deletion autogpt_platform/backend/backend/data/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
53 changes: 51 additions & 2 deletions autogpt_platform/backend/backend/server/v2/library/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 6584935

Please sign in to comment.