|
| 1 | +import logging |
| 2 | +from http import HTTPStatus |
| 3 | +from typing import Optional |
| 4 | + |
| 5 | +from fastapi import APIRouter, Body, Header, HTTPException, Query |
| 6 | +from starlette.responses import JSONResponse |
| 7 | + |
| 8 | +from consts.exceptions import SkillDuplicateError, UnauthorizedError |
| 9 | +from services.agent_repository_service import ( |
| 10 | + create_agent_repository_listing_impl, |
| 11 | + import_agent_from_repository_impl, |
| 12 | + list_agent_repository_listings_impl, |
| 13 | + update_agent_repository_status_impl, |
| 14 | +) |
| 15 | +from utils.auth_utils import get_current_user_id |
| 16 | + |
| 17 | +agent_repository_router = APIRouter(prefix="/repository/agent") |
| 18 | +logger = logging.getLogger("agent_repository_app") |
| 19 | + |
| 20 | + |
| 21 | +@agent_repository_router.get("") |
| 22 | +async def list_agent_repository_listings_api( |
| 23 | + status: Optional[str] = Query(None, description="Filter by listing status"), |
| 24 | + authorization: str = Header(None), |
| 25 | +): |
| 26 | + """List all marketplace repository listings with optional status filter.""" |
| 27 | + try: |
| 28 | + get_current_user_id(authorization) |
| 29 | + result = list_agent_repository_listings_impl(status=status) |
| 30 | + return JSONResponse(status_code=HTTPStatus.OK, content=result) |
| 31 | + except UnauthorizedError as e: |
| 32 | + raise HTTPException(status_code=HTTPStatus.UNAUTHORIZED, detail=str(e)) |
| 33 | + except ValueError as e: |
| 34 | + raise HTTPException(status_code=HTTPStatus.BAD_REQUEST, detail=str(e)) |
| 35 | + except Exception as e: |
| 36 | + logger.error(f"List agent repository listings error: {str(e)}") |
| 37 | + raise HTTPException( |
| 38 | + status_code=HTTPStatus.INTERNAL_SERVER_ERROR, |
| 39 | + detail="List agent repository listings error.", |
| 40 | + ) |
| 41 | + |
| 42 | + |
| 43 | +@agent_repository_router.patch("/{agent_repository_id}/status") |
| 44 | +async def update_agent_repository_status_api( |
| 45 | + agent_repository_id: int, |
| 46 | + status: str = Body( |
| 47 | + ..., |
| 48 | + embed=True, |
| 49 | + description=( |
| 50 | + "New status: NOT_SHARED (未共享) / PENDING_REVIEW (待审核) / " |
| 51 | + "REJECTED (审核驳回) / SHARED (已共享)" |
| 52 | + ), |
| 53 | + ), |
| 54 | + authorization: str = Header(None), |
| 55 | +): |
| 56 | + """Update marketplace repository listing status (share, unshare, approve, reject).""" |
| 57 | + try: |
| 58 | + user_id, _ = get_current_user_id(authorization) |
| 59 | + result = update_agent_repository_status_impl( |
| 60 | + agent_repository_id=agent_repository_id, |
| 61 | + status=status, |
| 62 | + user_id=user_id, |
| 63 | + ) |
| 64 | + return JSONResponse(status_code=HTTPStatus.OK, content=result) |
| 65 | + except UnauthorizedError as e: |
| 66 | + raise HTTPException(status_code=HTTPStatus.UNAUTHORIZED, detail=str(e)) |
| 67 | + except ValueError as e: |
| 68 | + raise HTTPException(status_code=HTTPStatus.BAD_REQUEST, detail=str(e)) |
| 69 | + except Exception as e: |
| 70 | + logger.error(f"Update agent repository status error: {str(e)}") |
| 71 | + raise HTTPException( |
| 72 | + status_code=HTTPStatus.INTERNAL_SERVER_ERROR, |
| 73 | + detail="Update agent repository status error.", |
| 74 | + ) |
| 75 | + |
| 76 | + |
| 77 | +@agent_repository_router.post("/{agent_id}/versions/{version_no}") |
| 78 | +async def create_agent_repository_listing_api( |
| 79 | + agent_id: int, |
| 80 | + version_no: int, |
| 81 | + authorization: str = Header(None), |
| 82 | +): |
| 83 | + """Create or update a marketplace repository listing from an agent version snapshot.""" |
| 84 | + try: |
| 85 | + user_id, tenant_id = get_current_user_id(authorization) |
| 86 | + result = await create_agent_repository_listing_impl( |
| 87 | + agent_id=agent_id, |
| 88 | + tenant_id=tenant_id, |
| 89 | + user_id=user_id, |
| 90 | + version_no=version_no, |
| 91 | + ) |
| 92 | + return JSONResponse(status_code=HTTPStatus.OK, content=result) |
| 93 | + except UnauthorizedError as e: |
| 94 | + raise HTTPException(status_code=HTTPStatus.UNAUTHORIZED, detail=str(e)) |
| 95 | + except ValueError as e: |
| 96 | + raise HTTPException(status_code=HTTPStatus.BAD_REQUEST, detail=str(e)) |
| 97 | + except Exception as e: |
| 98 | + logger.error(f"Create agent repository listing error: {str(e)}") |
| 99 | + raise HTTPException( |
| 100 | + status_code=HTTPStatus.INTERNAL_SERVER_ERROR, |
| 101 | + detail="Create agent repository listing error.", |
| 102 | + ) |
| 103 | + |
| 104 | + |
| 105 | +@agent_repository_router.post("/{agent_repository_id}/import") |
| 106 | +async def import_agent_from_repository_api( |
| 107 | + agent_repository_id: int, |
| 108 | + authorization: Optional[str] = Header(None), |
| 109 | +): |
| 110 | + """Import an agent tree from a marketplace repository listing into the current tenant.""" |
| 111 | + try: |
| 112 | + await import_agent_from_repository_impl( |
| 113 | + agent_repository_id=agent_repository_id, |
| 114 | + authorization=authorization, |
| 115 | + ) |
| 116 | + return JSONResponse(status_code=HTTPStatus.OK, content={}) |
| 117 | + except UnauthorizedError as e: |
| 118 | + raise HTTPException(status_code=HTTPStatus.UNAUTHORIZED, detail=str(e)) |
| 119 | + except SkillDuplicateError as exc: |
| 120 | + raise HTTPException( |
| 121 | + status_code=HTTPStatus.CONFLICT, |
| 122 | + detail={ |
| 123 | + "type": "skill_duplicate", |
| 124 | + "duplicate_skills": exc.duplicate_names, |
| 125 | + }, |
| 126 | + ) |
| 127 | + except ValueError as e: |
| 128 | + raise HTTPException(status_code=HTTPStatus.NOT_FOUND, detail=str(e)) |
| 129 | + except Exception as e: |
| 130 | + logger.error(f"Import agent from repository error: {str(e)}") |
| 131 | + raise HTTPException( |
| 132 | + status_code=HTTPStatus.INTERNAL_SERVER_ERROR, |
| 133 | + detail="Import agent from repository error.", |
| 134 | + ) |
0 commit comments