Skip to content

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.

feat(backend/store): implement sub-agent approval support (#10842)

Zamil Majdy2e38f132e711
diff --git a/autogpt_platform/backend/backend/server/v2/store/db.py b/autogpt_platform/backend/backend/server/v2/store/db.py
index cc5cf3e554..f3beeb0f18 100644
--- a/autogpt_platform/backend/backend/server/v2/store/db.py
+++ b/autogpt_platform/backend/backend/server/v2/store/db.py
@@ -1,3 +1,4 @@
+import asyncio
import logging
from datetime import datetime, timezone
@@ -9,6 +10,7 @@ import prisma.types
import backend.server.v2.store.exceptions
import backend.server.v2.store.model
+from backend.data.db import transaction
from backend.data.graph import (
GraphMeta,
GraphModel,
@@ -70,7 +72,7 @@ async def get_store_agents(
)
sanitized_query = sanitize_query(search_query)
- where_clause = {}
+ where_clause: prisma.types.StoreAgentWhereInput = {"is_available": True}
if featured:
where_clause["featured"] = featured
if creators:
@@ -94,15 +96,13 @@ async def get_store_agents(
try:
agents = await prisma.models.StoreAgent.prisma().find_many(
- where=prisma.types.StoreAgentWhereInput(**where_clause),
+ where=where_clause,
order=order_by,
skip=(page - 1) * page_size,
take=page_size,
)
- total = await prisma.models.StoreAgent.prisma().count(
- where=prisma.types.StoreAgentWhereInput(**where_clause)
- )
+ total = await prisma.models.StoreAgent.prisma().count(where=where_clause)
total_pages = (total + page_size - 1) // page_size
store_agents: list[backend.server.v2.store.model.StoreAgent] = []
@@ -190,8 +190,8 @@ async def get_store_agent_details(
agent_name=agent.agent_name,
agent_video=agent.agent_video or "",
agent_image=agent.agent_image,
- creator=agent.creator_username,
- creator_avatar=agent.creator_avatar,
+ creator=agent.creator_username or "",
+ creator_avatar=agent.creator_avatar or "",
sub_heading=agent.sub_heading,
description=agent.description,
categories=agent.categories,
@@ -263,8 +263,8 @@ async def get_store_agent_by_version_id(
agent_name=agent.agent_name,
agent_video=agent.agent_video or "",
agent_image=agent.agent_image,
- creator=agent.creator_username,
- creator_avatar=agent.creator_avatar,
+ creator=agent.creator_username or "",
+ creator_avatar=agent.creator_avatar or "",
sub_heading=agent.sub_heading,
description=agent.description,
categories=agent.categories,
@@ -1200,40 +1200,103 @@ async def get_agent(store_listing_version_id: str) -> GraphModel:
#####################################################
-async def _get_missing_sub_store_listing(
- graph: prisma.models.AgentGraph,
-) -> list[prisma.models.AgentGraph]:
- """
- Agent graph can have sub-graphs, and those sub-graphs also need to be store listed.
- This method fetches the sub-graphs, and returns the ones not listed in the store.
- """
- sub_graphs = await get_sub_graphs(graph)
- if not sub_graphs:
- return []
-
- # Fetch all the sub-graphs that are listed, and return the ones missing.
- store_listed_sub_graphs = {
- (listing.agentGraphId, listing.agentGraphVersion)
- for listing in await prisma.models.StoreListingVersion.prisma().find_many(
- where={
- "OR": [
- {
- "agentGraphId": sub_graph.id,
- "agentGraphVersion": sub_graph.version,
- }
- for sub_graph in sub_graphs
- ],
+async def _approve_sub_agent(
+ tx,
+ sub_graph: prisma.models.AgentGraph,
+ main_agent_name: str,
+ main_agent_version: int,
+ main_agent_user_id: str,
+) -> None:
+ """Approve a single sub-agent by creating/updating store listings as needed"""
+ heading = f"Sub-agent of {main_agent_name} v{main_agent_version}"
+
+ # Find existing listing for this sub-agent
+ listing = await prisma.models.StoreListing.prisma(tx).find_first(
+ where={"agentGraphId": sub_graph.id, "isDeleted": False},
+ include={"Versions": True},
+ )
+
+ # Early return: Create new listing if none exists
+ if not listing:
+ await prisma.models.StoreListing.prisma(tx).create(
+ data=prisma.types.StoreListingCreateInput(
+ slug=f"sub-agent-{sub_graph.id[:8]}",
+ agentGraphId=sub_graph.id,
+ agentGraphVersion=sub_graph.version,
+ owningUserId=main_agent_user_id,
+ hasApprovedVersion=True,
+ Versions={
+ "create": [
+ _create_sub_agent_version_data(
+ sub_graph, heading, main_agent_name
+ )
+ ]
+ },
+ )
+ )
+ return
+
+ # Find version matching this sub-graph
+ matching_version = next(
+ (
+ v
+ for v in listing.Versions or []
+ if v.agentGraphId == sub_graph.id
+ and v.agentGraphVersion == sub_graph.version
+ ),
+ None,
+ )
+
+ # Early return: Approve existing version if found and not already approved
+ if matching_version:
+ if matching_version.submissionStatus == prisma.enums.SubmissionStatus.APPROVED:
+ return # Already approved, nothing to do
+
+ await prisma.models.StoreListingVersion.prisma(tx).update(
+ where={"id": matching_version.id},
+ data={
"submissionStatus": prisma.enums.SubmissionStatus.APPROVED,
- "isDeleted": False,
- }
+ "reviewedAt": datetime.now(tz=timezone.utc),
+ },
+ )
+ await prisma.models.StoreListing.prisma(tx).update(
+ where={"id": listing.id}, data={"hasApprovedVersion": True}
)
- }
+ return
+
+ # Create new version if no matching version found
+ next_version = max((v.version for v in listing.Versions or []), default=0) + 1
+ await prisma.models.StoreListingVersion.prisma(tx).create(
+ data={
+ **_create_sub_agent_version_data(sub_graph, heading, main_agent_name),
+ "version": next_version,
+ "storeListingId": listing.id,
+ }
+ )
+ await prisma.models.StoreListing.prisma(tx).update(
+ where={"id": listing.id}, data={"hasApprovedVersion": True}
+ )
+
- return [
- sub_graph
- for sub_graph in sub_graphs
- if (sub_graph.id, sub_graph.version) not in store_listed_sub_graphs
- ]
+def _create_sub_agent_version_data(
+ sub_graph: prisma.models.AgentGraph, heading: str, main_agent_name: str
+) -> prisma.types.StoreListingVersionCreateInput:
+ """Create store listing version data for a sub-agent"""
+ return prisma.types.StoreListingVersionCreateInput(
+ agentGraphId=sub_graph.id,
+ agentGraphVersion=sub_graph.version,
+ name=sub_graph.name or heading,
+ submissionStatus=prisma.enums.SubmissionStatus.APPROVED,
+ subHeading=heading,
+ description=(
+ f"{heading}: {sub_graph.description}" if sub_graph.description else heading
+ ),
+ changesSummary=f"Auto-approved as sub-agent of {main_agent_name}",
+ isAvailable=False,
+ submittedAt=datetime.now(tz=timezone.utc),
+ imageUrls=[], # Sub-agents don't need images
+ categories=[], # Sub-agents don't need categories
+ )
async def review_store_submission(
@@ -1271,33 +1334,30 @@ async def review_store_submission(
# If approving, update the listing to indicate it has an approved version
if is_approved and store_listing_version.AgentGraph:
- heading = f"Sub-graph of {store_listing_version.name}v{store_listing_version.agentGraphVersion}"
-
- sub_store_listing_versions = [
- prisma.types.StoreListingVersionCreateWithoutRelationsInput(
- agentGraphId=sub_graph.id,
- agentGraphVersion=sub_graph.version,
- name=sub_graph.name or heading,
- submissionStatus=prisma.enums.SubmissionStatus.APPROVED,
- subHeading=heading,
- description=f"{heading}: {sub_graph.description}",
- changesSummary=f"This listing is added as a {heading} / #{store_listing_version.agentGraphId}.",
- isAvailable=False, # Hide sub-graphs from the store by default.
- submittedAt=datetime.now(tz=timezone.utc),
- )
- for sub_graph in await _get_missing_sub_store_listing(
- store_listing_version.AgentGraph
+ async with transaction() as tx:
+ # Handle sub-agent approvals in transaction
+ await asyncio.gather(
+ *[
+ _approve_sub_agent(
+ tx,
+ sub_graph,
+ store_listing_version.name,
+ store_listing_version.agentGraphVersion,
+ store_listing_version.StoreListing.owningUserId,
+ )
+ for sub_graph in await get_sub_graphs(
+ store_listing_version.AgentGraph
+ )
+ ]
)
- ]
- await prisma.models.StoreListing.prisma().update(
- where={"id": store_listing_version.StoreListing.id},
- data={
- "hasApprovedVersion": True,
- "ActiveVersion": {"connect": {"id": store_listing_version_id}},
- "Versions": {"create": sub_store_listing_versions},
- },
- )
+ await prisma.models.StoreListing.prisma(tx).update(
+ where={"id": store_listing_version.StoreListing.id},
+ data={
+ "hasApprovedVersion": True,
+ "ActiveVersion": {"connect": {"id": store_listing_version_id}},
+ },
+ )
# If rejecting an approved agent, update the StoreListing accordingly
if is_rejecting_approved:
diff --git a/autogpt_platform/backend/backend/server/v2/store/db_test.py b/autogpt_platform/backend/backend/server/v2/store/db_test.py
index 7ad4509c19..4322e2c430 100644
--- a/autogpt_platform/backend/backend/server/v2/store/db_test.py
+++ b/autogpt_platform/backend/backend/server/v2/store/db_test.py
@@ -41,6 +41,7 @@ async def test_get_store_agents(mocker):
rating=4.5,
versions=["1.0"],
updated_at=datetime.now(),
+ is_available=False,
)
]
@@ -82,6 +83,7 @@ async def test_get_store_agent_details(mocker):
rating=4.5,
versions=["1.0"],
updated_at=datetime.now(),
+ is_available=False,
)
# Create a mock StoreListing result
diff --git a/autogpt_platform/backend/migrations/20250904171522_update_store_agent_view_with_availability/migration.sql b/autogpt_platform/backend/migrations/20250904171522_update_store_agent_view_with_availability/migration.sql
new file mode 100644
index 0000000000..9b34aaf8ec
--- /dev/null
+++ b/autogpt_platform/backend/migrations/20250904171522_update_store_agent_view_with_availability/migration.sql
@@ -0,0 +1,53 @@
+-- Update StoreAgent view to include is_available field and fix creator field nullability
+
+BEGIN;
+
+-- Drop and recreate the StoreAgent view with isAvailable field
+DROP VIEW IF EXISTS "StoreAgent";
+
+CREATE OR REPLACE VIEW "StoreAgent" AS
+WITH agent_versions AS (
+ SELECT
+ "storeListingId",
+ array_agg(DISTINCT version::text ORDER BY version::text) AS versions
+ FROM "StoreListingVersion"
+ WHERE "submissionStatus" = 'APPROVED'
+ GROUP BY "storeListingId"
+)
+SELECT
+ sl.id AS listing_id,
+ slv.id AS "storeListingVersionId",
+ slv."createdAt" AS updated_at,
+ sl.slug,
+ COALESCE(slv.name, '') AS agent_name,
+ slv."videoUrl" AS agent_video,
+ COALESCE(slv."imageUrls", ARRAY[]::text[]) AS agent_image,
+ slv."isFeatured" AS featured,
+ p.username AS creator_username, -- Allow NULL for malformed sub-agents
+ p."avatarUrl" AS creator_avatar, -- Allow NULL for malformed sub-agents
+ slv."subHeading" AS sub_heading,
+ slv.description,
+ slv.categories,
+ COALESCE(ar.run_count, 0::bigint) AS runs,
+ COALESCE(rs.avg_rating, 0.0)::double precision AS rating,
+ COALESCE(av.versions, ARRAY[slv.version::text]) AS versions,
+ slv."isAvailable" AS is_available -- Add isAvailable field to filter sub-agents
+FROM "StoreListing" sl
+INNER JOIN "StoreListingVersion" slv
+ ON slv."storeListingId" = sl.id
+ AND slv."submissionStatus" = 'APPROVED'
+JOIN "AgentGraph" a
+ ON slv."agentGraphId" = a.id
+ AND slv."agentGraphVersion" = a.version
+LEFT JOIN "Profile" p
+ ON sl."owningUserId" = p."userId"
+LEFT JOIN "mv_review_stats" rs
+ ON sl.id = rs."storeListingId"
+LEFT JOIN "mv_agent_run_counts" ar
+ ON a.id = ar."agentGraphId"
+LEFT JOIN agent_versions av
+ ON sl.id = av."storeListingId"
+WHERE sl."isDeleted" = false
+ AND sl."hasApprovedVersion" = true;
+
+COMMIT;
\ No newline at end of file
diff --git a/autogpt_platform/backend/schema.prisma b/autogpt_platform/backend/schema.prisma
index ae54808bec..cfdd879772 100644
--- a/autogpt_platform/backend/schema.prisma
+++ b/autogpt_platform/backend/schema.prisma
@@ -630,14 +630,15 @@ view StoreAgent {
agent_image String[]
featured Boolean @default(false)
- creator_username String
- creator_avatar String
+ creator_username String?
+ creator_avatar String?
sub_heading String
description String
categories String[]
runs Int
rating Float
versions String[]
+ is_available Boolean @default(true)
// Materialized views used (refreshed every 15 minutes via pg_cron):
// - mv_agent_run_counts - Pre-aggregated agent execution counts by agentGraphId
diff --git a/autogpt_platform/frontend/src/app/(platform)/library/agents/[id]/components/OldAgentLibraryView/components/agent-run-details-view.tsx b/autogpt_platform/frontend/src/app/(platform)/library/agents/[id]/components/OldAgentLibraryView/components/agent-run-details-view.tsx
index 8fcd5d3152..2671b8b51e 100644
--- a/autogpt_platform/frontend/src/app/(platform)/library/agents/[id]/components/OldAgentLibraryView/components/agent-run-details-view.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/library/agents/[id]/components/OldAgentLibraryView/components/agent-run-details-view.tsx
@@ -113,7 +113,7 @@ export function AgentRunDetailsView({
const runAgain = useCallback(() => {
if (
!run.inputs ||
- !(graph.credentials_input_schema.required ?? []).every(
+ !(graph.credentials_input_schema?.required ?? []).every(
(k) => k in (run.credential_inputs ?? {}),
)
)
@@ -194,7 +194,7 @@ export function AgentRunDetailsView({
: []),
...(["success", "failed", "stopped"].includes(runStatus) &&
!graph.has_external_trigger &&
- (graph.credentials_input_schema.required ?? []).every(
+ (graph.credentials_input_schema?.required ?? []).every(
(k) => k in (run.credential_inputs ?? {}),
)
? [
@@ -228,7 +228,7 @@ export function AgentRunDetailsView({
doDeleteRun,
doCreatePresetFromRun,
graph.has_external_trigger,
- graph.credentials_input_schema.required,
+ graph.credentials_input_schema?.required,
agent.can_access_graph,
run.graph_id,
run.graph_version,