147 lines
4.1 KiB
Python
147 lines
4.1 KiB
Python
from typing import Any, Dict, List, Optional
|
|
|
|
from fastapi import HTTPException, Request, UploadFile
|
|
from sqlmodel import Session
|
|
|
|
from models.bot import BotInstance
|
|
from providers.selector import get_workspace_provider
|
|
|
|
|
|
class WorkspaceService:
|
|
def _require_bot(self, *, session: Session, bot_id: str) -> BotInstance:
|
|
bot = session.get(BotInstance, bot_id)
|
|
if not bot:
|
|
raise HTTPException(status_code=404, detail="Bot not found")
|
|
return bot
|
|
|
|
def list_tree(
|
|
self,
|
|
*,
|
|
app_state: Any,
|
|
bot: BotInstance,
|
|
path: Optional[str] = None,
|
|
recursive: bool = False,
|
|
) -> Dict[str, Any]:
|
|
return get_workspace_provider(app_state, bot).list_tree(bot_id=bot.id, path=path, recursive=recursive)
|
|
|
|
def read_file(
|
|
self,
|
|
*,
|
|
app_state: Any,
|
|
bot: BotInstance,
|
|
path: str,
|
|
max_bytes: int = 200000,
|
|
) -> Dict[str, Any]:
|
|
return get_workspace_provider(app_state, bot).read_file(bot_id=bot.id, path=path, max_bytes=max_bytes)
|
|
|
|
def write_markdown(
|
|
self,
|
|
*,
|
|
app_state: Any,
|
|
bot: BotInstance,
|
|
path: str,
|
|
content: str,
|
|
) -> Dict[str, Any]:
|
|
return get_workspace_provider(app_state, bot).write_markdown(bot_id=bot.id, path=path, content=content)
|
|
|
|
async def upload_files(
|
|
self,
|
|
*,
|
|
app_state: Any,
|
|
bot: BotInstance,
|
|
files: List[UploadFile],
|
|
path: Optional[str] = None,
|
|
) -> Dict[str, Any]:
|
|
return await get_workspace_provider(app_state, bot).upload_files(bot_id=bot.id, files=files, path=path)
|
|
|
|
def serve_file(
|
|
self,
|
|
*,
|
|
app_state: Any,
|
|
bot: BotInstance,
|
|
path: str,
|
|
download: bool,
|
|
request: Request,
|
|
public: bool = False,
|
|
redirect_html_to_raw: bool = False,
|
|
):
|
|
return get_workspace_provider(app_state, bot).serve_file(
|
|
bot_id=bot.id,
|
|
path=path,
|
|
download=download,
|
|
request=request,
|
|
public=public,
|
|
redirect_html_to_raw=redirect_html_to_raw,
|
|
)
|
|
|
|
def list_tree_for_bot(
|
|
self,
|
|
*,
|
|
app_state: Any,
|
|
session: Session,
|
|
bot_id: str,
|
|
path: Optional[str] = None,
|
|
recursive: bool = False,
|
|
) -> Dict[str, Any]:
|
|
bot = self._require_bot(session=session, bot_id=bot_id)
|
|
return self.list_tree(app_state=app_state, bot=bot, path=path, recursive=recursive)
|
|
|
|
def read_file_for_bot(
|
|
self,
|
|
*,
|
|
app_state: Any,
|
|
session: Session,
|
|
bot_id: str,
|
|
path: str,
|
|
max_bytes: int = 200000,
|
|
) -> Dict[str, Any]:
|
|
bot = self._require_bot(session=session, bot_id=bot_id)
|
|
return self.read_file(app_state=app_state, bot=bot, path=path, max_bytes=max_bytes)
|
|
|
|
def write_markdown_for_bot(
|
|
self,
|
|
*,
|
|
app_state: Any,
|
|
session: Session,
|
|
bot_id: str,
|
|
path: str,
|
|
content: str,
|
|
) -> Dict[str, Any]:
|
|
bot = self._require_bot(session=session, bot_id=bot_id)
|
|
return self.write_markdown(app_state=app_state, bot=bot, path=path, content=content)
|
|
|
|
def serve_file_for_bot(
|
|
self,
|
|
*,
|
|
app_state: Any,
|
|
session: Session,
|
|
bot_id: str,
|
|
path: str,
|
|
download: bool,
|
|
request: Request,
|
|
public: bool = False,
|
|
redirect_html_to_raw: bool = False,
|
|
):
|
|
bot = self._require_bot(session=session, bot_id=bot_id)
|
|
return self.serve_file(
|
|
app_state=app_state,
|
|
bot=bot,
|
|
path=path,
|
|
download=download,
|
|
request=request,
|
|
public=public,
|
|
redirect_html_to_raw=redirect_html_to_raw,
|
|
)
|
|
|
|
async def upload_files_for_bot(
|
|
self,
|
|
*,
|
|
app_state: Any,
|
|
session: Session,
|
|
bot_id: str,
|
|
files: List[UploadFile],
|
|
path: Optional[str] = None,
|
|
) -> Dict[str, Any]:
|
|
bot = self._require_bot(session=session, bot_id=bot_id)
|
|
return await self.upload_files(app_state=app_state, bot=bot, files=files, path=path)
|