Skip to content

Commit 6584935

Browse files
authored
fix(store): Fixing add agent to library (#9098)
Do a deep copy of the store agent so the new agent is under the current users id ⚠️ Hacky fix!!
1 parent 6025506 commit 6584935

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

autogpt_platform/backend/backend/data/graph.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -521,13 +521,13 @@ async def get_graph(
521521
"""
522522
where_clause: AgentGraphWhereInput = {
523523
"id": graph_id,
524-
"isTemplate": template,
525524
}
526525
if version is not None:
527526
where_clause["version"] = version
528527
elif not template:
529528
where_clause["isActive"] = True
530529

530+
# TODO: Fix hack workaround to get adding store agents to work
531531
if user_id is not None and not template:
532532
where_clause["userId"] = user_id
533533

autogpt_platform/backend/backend/server/v2/library/routes.py

+51-2
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,20 @@
44
import autogpt_libs.auth.depends
55
import autogpt_libs.auth.middleware
66
import fastapi
7+
import prisma
78

89
import backend.data.graph
10+
import backend.integrations.creds_manager
11+
import backend.integrations.webhooks.graph_lifecycle_hooks
912
import backend.server.v2.library.db
1013
import backend.server.v2.library.model
1114

1215
logger = logging.getLogger(__name__)
1316

1417
router = fastapi.APIRouter()
18+
integration_creds_manager = (
19+
backend.integrations.creds_manager.IntegrationCredentialsManager()
20+
)
1521

1622

1723
@router.get(
@@ -63,10 +69,53 @@ async def add_agent_to_library(
6369
HTTPException: If there is an error adding the agent to the library
6470
"""
6571
try:
66-
await backend.server.v2.library.db.add_agent_to_library(
67-
store_listing_version_id=store_listing_version_id, user_id=user_id
72+
# Get the graph from the store listing
73+
store_listing_version = (
74+
await prisma.models.StoreListingVersion.prisma().find_unique(
75+
where={"id": store_listing_version_id}, include={"Agent": True}
76+
)
77+
)
78+
79+
if not store_listing_version or not store_listing_version.Agent:
80+
raise fastapi.HTTPException(
81+
status_code=404,
82+
detail=f"Store listing version {store_listing_version_id} not found",
83+
)
84+
85+
agent = store_listing_version.Agent
86+
87+
if agent.userId == user_id:
88+
raise fastapi.HTTPException(
89+
status_code=400, detail="Cannot add own agent to library"
90+
)
91+
92+
# Create a new graph from the template
93+
graph = await backend.data.graph.get_graph(
94+
agent.id, agent.version, template=True, user_id=user_id
6895
)
96+
97+
if not graph:
98+
raise fastapi.HTTPException(
99+
status_code=404, detail=f"Agent {agent.id} not found"
100+
)
101+
102+
# Create a deep copy with new IDs
103+
graph.version = 1
104+
graph.is_template = False
105+
graph.is_active = True
106+
graph.reassign_ids(user_id=user_id, reassign_graph_id=True)
107+
108+
# Save the new graph
109+
graph = await backend.data.graph.create_graph(graph, user_id=user_id)
110+
graph = (
111+
await backend.integrations.webhooks.graph_lifecycle_hooks.on_graph_activate(
112+
graph,
113+
get_credentials=lambda id: integration_creds_manager.get(user_id, id),
114+
)
115+
)
116+
69117
return fastapi.Response(status_code=201)
118+
70119
except Exception:
71120
logger.exception("Exception occurred whilst adding agent to library")
72121
raise fastapi.HTTPException(

0 commit comments

Comments
 (0)