101 lines
3.6 KiB
Python
101 lines
3.6 KiB
Python
from typing import Optional
|
|
|
|
from fastapi import APIRouter, Depends, File, Form, HTTPException, UploadFile
|
|
from sqlmodel import Session
|
|
|
|
from core.database import get_session
|
|
from models.bot import BotInstance
|
|
from services.skill_market_service import (
|
|
create_skill_market_item_record,
|
|
delete_skill_market_item_record,
|
|
install_skill_market_item_for_bot,
|
|
list_bot_skill_market_items,
|
|
list_skill_market_items,
|
|
update_skill_market_item_record,
|
|
)
|
|
from services.skill_service import (
|
|
delete_workspace_skill_entry,
|
|
list_bot_skills as list_workspace_bot_skills,
|
|
upload_bot_skill_zip_to_workspace,
|
|
)
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/api/platform/skills")
|
|
def list_skill_market(session: Session = Depends(get_session)):
|
|
return list_skill_market_items(session)
|
|
|
|
@router.post("/api/platform/skills")
|
|
async def create_skill_market_item(
|
|
skill_key: str = Form(""),
|
|
display_name: str = Form(""),
|
|
description: str = Form(""),
|
|
file: UploadFile = File(...),
|
|
session: Session = Depends(get_session),
|
|
):
|
|
return await create_skill_market_item_record(
|
|
session,
|
|
skill_key=skill_key,
|
|
display_name=display_name,
|
|
description=description,
|
|
upload=file,
|
|
)
|
|
|
|
@router.put("/api/platform/skills/{skill_id}")
|
|
async def update_skill_market_item(
|
|
skill_id: int,
|
|
skill_key: str = Form(""),
|
|
display_name: str = Form(""),
|
|
description: str = Form(""),
|
|
file: Optional[UploadFile] = File(None),
|
|
session: Session = Depends(get_session),
|
|
):
|
|
return await update_skill_market_item_record(
|
|
session,
|
|
skill_id=skill_id,
|
|
skill_key=skill_key,
|
|
display_name=display_name,
|
|
description=description,
|
|
upload=file,
|
|
)
|
|
|
|
@router.delete("/api/platform/skills/{skill_id}")
|
|
def delete_skill_market_item(skill_id: int, session: Session = Depends(get_session)):
|
|
return delete_skill_market_item_record(session, skill_id=skill_id)
|
|
|
|
@router.get("/api/bots/{bot_id}/skills")
|
|
def list_bot_skills(bot_id: str, session: Session = Depends(get_session)):
|
|
bot = session.get(BotInstance, bot_id)
|
|
if not bot:
|
|
raise HTTPException(status_code=404, detail="Bot not found")
|
|
return list_workspace_bot_skills(bot_id)
|
|
|
|
@router.get("/api/bots/{bot_id}/skill-market")
|
|
def list_bot_skill_market(bot_id: str, session: Session = Depends(get_session)):
|
|
bot = session.get(BotInstance, bot_id)
|
|
if not bot:
|
|
raise HTTPException(status_code=404, detail="Bot not found")
|
|
return list_bot_skill_market_items(session, bot_id=bot_id)
|
|
|
|
@router.post("/api/bots/{bot_id}/skill-market/{skill_id}/install")
|
|
def install_bot_skill_from_market(bot_id: str, skill_id: int, session: Session = Depends(get_session)):
|
|
bot = session.get(BotInstance, bot_id)
|
|
if not bot:
|
|
raise HTTPException(status_code=404, detail="Bot not found")
|
|
return install_skill_market_item_for_bot(session, bot_id=bot_id, skill_id=skill_id)
|
|
|
|
@router.post("/api/bots/{bot_id}/skills/upload")
|
|
async def upload_bot_skill_zip(bot_id: str, file: UploadFile = File(...), session: Session = Depends(get_session)):
|
|
bot = session.get(BotInstance, bot_id)
|
|
if not bot:
|
|
raise HTTPException(status_code=404, detail="Bot not found")
|
|
return await upload_bot_skill_zip_to_workspace(bot_id, upload=file)
|
|
|
|
@router.delete("/api/bots/{bot_id}/skills/{skill_name}")
|
|
def delete_bot_skill(bot_id: str, skill_name: str, session: Session = Depends(get_session)):
|
|
bot = session.get(BotInstance, bot_id)
|
|
if not bot:
|
|
raise HTTPException(status_code=404, detail="Bot not found")
|
|
return delete_workspace_skill_entry(bot_id, skill_name=skill_name)
|