Spaces:
Running
Running
Download src/api/agents.py from arimatch1/crewhub-staging: direct link, hf CLI and curl.
- Browser
- Download file 6.95 kB
-
https://huggingface.co/spaces/arimatch1/crewhub-staging/resolve/main/src/api/agents.py
- Command line
-
hf download hf://spaces/arimatch1/crewhub-staging/src/api/agents.py
-
curl -L -o agents.py https://huggingface.co/spaces/arimatch1/crewhub-staging/resolve/main/src/api/agents.py
6.95 kB
| # 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"]) | |
| 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 | |
| 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) | |
| 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 | |
| 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 | |
| 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 | |
| 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) | |
| 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 | |
| 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, | |
| } | |
| 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] | |
| ) | |
| 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 | |