Spaces:
Running
Running
File size: 6,948 Bytes
db5f035 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | # Copyright (c) 2026 CrewHub. All rights reserved.
# Proprietary and confidential. See LICENSE for details.
"""Agent registration and management endpoints."""
from datetime import datetime, timedelta, timezone
from uuid import UUID
from fastapi import APIRouter, Depends, Query
from sqlalchemy import func
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.future import select
from src.core.auth import resolve_db_user_id
from src.database import get_db
from src.models.task import Task
from src.schemas.agent import (
AgentCardResponse,
AgentCreate,
AgentListResponse,
AgentResponse,
AgentStatsResponse,
AgentStatus,
AgentUpdate,
DailyTaskCount,
)
from src.services.registry import RegistryService
router = APIRouter(prefix="/agents", tags=["agents"])
@router.post("/", response_model=AgentResponse, status_code=201)
async def register_agent(
data: AgentCreate,
db: AsyncSession = Depends(get_db),
owner_id: UUID = Depends(resolve_db_user_id),
) -> AgentResponse:
"""Register a new agent in the marketplace.
Requires authentication. The caller becomes the agent owner.
"""
service = RegistryService(db)
agent = await service.register_agent(owner_id=owner_id, data=data)
return agent
@router.get("/", response_model=AgentListResponse)
async def list_agents(
page: int = Query(1, ge=1, description="Page number"),
per_page: int = Query(20, ge=1, le=100, description="Items per page"),
category: str | None = Query(None, description="Filter by category"),
status: AgentStatus | None = Query(None, description="Filter by status"),
owner_id: str | None = Query(None, description="Filter by owner user ID"),
db: AsyncSession = Depends(get_db),
) -> AgentListResponse:
"""List registered agents with optional filters.
This is a public endpoint and does not require authentication.
"""
service = RegistryService(db)
agents, total = await service.list_agents(
page=page,
per_page=per_page,
category=category,
status=status.value if status else None,
owner_id=owner_id,
)
return AgentListResponse(agents=agents, total=total, page=page, per_page=per_page)
@router.get("/{agent_id}", response_model=AgentResponse)
async def get_agent(
agent_id: UUID,
db: AsyncSession = Depends(get_db),
) -> AgentResponse:
"""Retrieve the full details of a specific agent.
This is a public endpoint and does not require authentication.
"""
service = RegistryService(db)
agent = await service.get_agent(agent_id=agent_id)
return agent
@router.put("/{agent_id}", response_model=AgentResponse)
async def update_agent(
agent_id: UUID,
data: AgentUpdate,
db: AsyncSession = Depends(get_db),
owner_id: UUID = Depends(resolve_db_user_id),
) -> AgentResponse:
"""Update an existing agent's details.
Requires authentication and ownership of the agent.
"""
service = RegistryService(db)
agent = await service.update_agent(
agent_id=agent_id,
owner_id=owner_id,
data=data,
)
return agent
@router.delete("/{agent_id}", response_model=AgentResponse)
async def deactivate_agent(
agent_id: UUID,
db: AsyncSession = Depends(get_db),
owner_id: UUID = Depends(resolve_db_user_id),
) -> AgentResponse:
"""Deactivate an agent (soft-delete).
Requires authentication and ownership of the agent.
"""
service = RegistryService(db)
await service.deactivate_agent(
agent_id=agent_id,
owner_id=owner_id,
)
agent = await service.get_agent(agent_id=agent_id)
return agent
@router.delete("/{agent_id}/permanent", status_code=204)
async def delete_agent_permanently(
agent_id: UUID,
db: AsyncSession = Depends(get_db),
owner_id: UUID = Depends(resolve_db_user_id),
) -> None:
"""Permanently delete an agent and all its skills.
Requires authentication and ownership. Tasks referencing this agent
will have their agent reference set to NULL.
"""
service = RegistryService(db)
await service.hard_delete_agent(agent_id=agent_id, owner_id=owner_id)
@router.get("/{agent_id}/card", response_model=AgentCardResponse)
async def get_agent_card(
agent_id: UUID,
db: AsyncSession = Depends(get_db),
) -> AgentCardResponse:
"""Return the A2A-spec compliant agent card JSON.
This is a public endpoint and does not require authentication.
"""
service = RegistryService(db)
card = await service.get_agent_card(agent_id=agent_id)
return card
@router.get("/{agent_id}/pricing")
async def get_agent_pricing(
agent_id: UUID,
db: AsyncSession = Depends(get_db),
) -> dict:
"""Return the full pricing and licensing details for an agent.
Public endpoint. Shows license type, available tiers, quotas, and trial info.
"""
service = RegistryService(db)
agent = await service.get_agent(agent_id=agent_id)
pricing = agent.pricing or {}
return {
"agent_id": str(agent.id),
"agent_name": agent.name,
"license_type": agent.license_type,
"pricing": pricing,
"tiers": pricing.get("tiers", []),
"trial": {
"days": pricing.get("trial_days"),
"task_limit": pricing.get("trial_task_limit"),
} if pricing.get("trial_days") else None,
}
@router.get("/{agent_id}/stats", response_model=AgentStatsResponse)
async def get_agent_stats(
agent_id: UUID,
db: AsyncSession = Depends(get_db),
) -> AgentStatsResponse:
"""Return daily task counts for the last 30 days.
Public endpoint for sparkline charts on agent cards.
"""
since = datetime.now(timezone.utc) - timedelta(days=30)
date_col = func.date(Task.created_at).label("date")
stmt = (
select(
date_col,
func.count().label("count"),
)
.where(Task.provider_agent_id == agent_id)
.where(Task.created_at >= since)
.group_by(func.date(Task.created_at))
.order_by(func.date(Task.created_at))
)
result = await db.execute(stmt)
rows = result.all()
return AgentStatsResponse(
daily_tasks=[DailyTaskCount(date=str(r.date), count=r.count) for r in rows]
)
@router.post("/{agent_id}/verify", response_model=AgentResponse)
async def request_verification(
agent_id: UUID,
db: AsyncSession = Depends(get_db),
owner_id: UUID = Depends(resolve_db_user_id),
) -> AgentResponse:
"""Request verification review for an agent.
Requires authentication and ownership of the agent. Verification is
processed asynchronously and the agent status will be updated once
the review is complete.
"""
service = RegistryService(db)
await service.request_verification(
agent_id=agent_id,
owner_id=owner_id,
)
agent = await service.get_agent(agent_id=agent_id)
return agent
|