147 lines
4.2 KiB
Python
147 lines
4.2 KiB
Python
|
|
from typing import List, Optional
|
||
|
|
|
||
|
|
from fastapi import APIRouter, Depends, File, HTTPException, Request, UploadFile
|
||
|
|
from sqlmodel import Session
|
||
|
|
|
||
|
|
from core.database import get_session
|
||
|
|
from models.bot import BotInstance
|
||
|
|
from schemas.system import WorkspaceFileUpdateRequest
|
||
|
|
from services.workspace_service import (
|
||
|
|
get_workspace_tree_data,
|
||
|
|
read_workspace_text_file,
|
||
|
|
serve_workspace_file,
|
||
|
|
update_workspace_markdown_file,
|
||
|
|
upload_workspace_files_to_workspace,
|
||
|
|
)
|
||
|
|
|
||
|
|
router = APIRouter()
|
||
|
|
|
||
|
|
|
||
|
|
@router.get("/api/bots/{bot_id}/workspace/tree")
|
||
|
|
def get_workspace_tree(
|
||
|
|
bot_id: str,
|
||
|
|
path: Optional[str] = None,
|
||
|
|
recursive: bool = False,
|
||
|
|
session: Session = Depends(get_session),
|
||
|
|
):
|
||
|
|
bot = session.get(BotInstance, bot_id)
|
||
|
|
if not bot:
|
||
|
|
raise HTTPException(status_code=404, detail="Bot not found")
|
||
|
|
return get_workspace_tree_data(bot_id, path=path, recursive=recursive)
|
||
|
|
|
||
|
|
@router.get("/api/bots/{bot_id}/workspace/file")
|
||
|
|
def read_workspace_file(
|
||
|
|
bot_id: str,
|
||
|
|
path: str,
|
||
|
|
max_bytes: int = 200000,
|
||
|
|
session: Session = Depends(get_session),
|
||
|
|
):
|
||
|
|
bot = session.get(BotInstance, bot_id)
|
||
|
|
if not bot:
|
||
|
|
raise HTTPException(status_code=404, detail="Bot not found")
|
||
|
|
return read_workspace_text_file(bot_id, path=path, max_bytes=max_bytes)
|
||
|
|
|
||
|
|
@router.put("/api/bots/{bot_id}/workspace/file")
|
||
|
|
def update_workspace_file(
|
||
|
|
bot_id: str,
|
||
|
|
path: str,
|
||
|
|
payload: WorkspaceFileUpdateRequest,
|
||
|
|
session: Session = Depends(get_session),
|
||
|
|
):
|
||
|
|
bot = session.get(BotInstance, bot_id)
|
||
|
|
if not bot:
|
||
|
|
raise HTTPException(status_code=404, detail="Bot not found")
|
||
|
|
return update_workspace_markdown_file(bot_id, path=path, content=payload.content)
|
||
|
|
|
||
|
|
@router.get("/api/bots/{bot_id}/workspace/download")
|
||
|
|
def download_workspace_file(
|
||
|
|
bot_id: str,
|
||
|
|
path: str,
|
||
|
|
download: bool = False,
|
||
|
|
request: Request = None,
|
||
|
|
session: Session = Depends(get_session),
|
||
|
|
):
|
||
|
|
bot = session.get(BotInstance, bot_id)
|
||
|
|
if not bot:
|
||
|
|
raise HTTPException(status_code=404, detail="Bot not found")
|
||
|
|
return serve_workspace_file(
|
||
|
|
bot_id=bot_id,
|
||
|
|
path=path,
|
||
|
|
download=download,
|
||
|
|
request=request,
|
||
|
|
public=False,
|
||
|
|
redirect_html_to_raw=True,
|
||
|
|
)
|
||
|
|
|
||
|
|
@router.get("/public/bots/{bot_id}/workspace/download")
|
||
|
|
def public_download_workspace_file(
|
||
|
|
bot_id: str,
|
||
|
|
path: str,
|
||
|
|
download: bool = False,
|
||
|
|
request: Request = None,
|
||
|
|
session: Session = Depends(get_session),
|
||
|
|
):
|
||
|
|
bot = session.get(BotInstance, bot_id)
|
||
|
|
if not bot:
|
||
|
|
raise HTTPException(status_code=404, detail="Bot not found")
|
||
|
|
return serve_workspace_file(
|
||
|
|
bot_id=bot_id,
|
||
|
|
path=path,
|
||
|
|
download=download,
|
||
|
|
request=request,
|
||
|
|
public=True,
|
||
|
|
redirect_html_to_raw=True,
|
||
|
|
)
|
||
|
|
|
||
|
|
@router.get("/api/bots/{bot_id}/workspace/raw/{path:path}")
|
||
|
|
def raw_workspace_file(
|
||
|
|
bot_id: str,
|
||
|
|
path: str,
|
||
|
|
download: bool = False,
|
||
|
|
request: Request = None,
|
||
|
|
session: Session = Depends(get_session),
|
||
|
|
):
|
||
|
|
bot = session.get(BotInstance, bot_id)
|
||
|
|
if not bot:
|
||
|
|
raise HTTPException(status_code=404, detail="Bot not found")
|
||
|
|
return serve_workspace_file(
|
||
|
|
bot_id=bot_id,
|
||
|
|
path=path,
|
||
|
|
download=download,
|
||
|
|
request=request,
|
||
|
|
public=False,
|
||
|
|
redirect_html_to_raw=False,
|
||
|
|
)
|
||
|
|
|
||
|
|
@router.get("/public/bots/{bot_id}/workspace/raw/{path:path}")
|
||
|
|
def public_raw_workspace_file(
|
||
|
|
bot_id: str,
|
||
|
|
path: str,
|
||
|
|
download: bool = False,
|
||
|
|
request: Request = None,
|
||
|
|
session: Session = Depends(get_session),
|
||
|
|
):
|
||
|
|
bot = session.get(BotInstance, bot_id)
|
||
|
|
if not bot:
|
||
|
|
raise HTTPException(status_code=404, detail="Bot not found")
|
||
|
|
return serve_workspace_file(
|
||
|
|
bot_id=bot_id,
|
||
|
|
path=path,
|
||
|
|
download=download,
|
||
|
|
request=request,
|
||
|
|
public=True,
|
||
|
|
redirect_html_to_raw=False,
|
||
|
|
)
|
||
|
|
|
||
|
|
@router.post("/api/bots/{bot_id}/workspace/upload")
|
||
|
|
async def upload_workspace_files(
|
||
|
|
bot_id: str,
|
||
|
|
files: List[UploadFile] = File(...),
|
||
|
|
path: Optional[str] = None,
|
||
|
|
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_workspace_files_to_workspace(bot_id, files=files, path=path)
|