From 8d4faa0f0ce0028c505ffdf4c5e5772e6dd6a58b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B8=D0=BA=D1=82=D0=BE=D1=80?= <78488229+viktor138irk@users.noreply.github.com> Date: Mon, 11 May 2026 23:27:17 +0900 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20workspace=20API=20=D0=B4=D0=BB=D1=8F=20=D1=84=D0=B0?= =?UTF-8?q?=D0=B9=D0=BB=D0=BE=D0=B2=D0=BE=D0=B3=D0=BE=20=D0=BC=D0=B5=D0=BD?= =?UTF-8?q?=D0=B5=D0=B4=D0=B6=D0=B5=D1=80=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/workspace_api.py | 62 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 backend/workspace_api.py diff --git a/backend/workspace_api.py b/backend/workspace_api.py new file mode 100644 index 0000000..ce735d3 --- /dev/null +++ b/backend/workspace_api.py @@ -0,0 +1,62 @@ +from fastapi import APIRouter, HTTPException +from pydantic import BaseModel + +from backend.file_workspace import ( + WorkspaceSecurityError, + build_tree, + read_file, + save_file, +) + +router = APIRouter(prefix='/api/files', tags=['workspace']) + + +class FilePathRequest(BaseModel): + path: str + + +class SaveFileRequest(BaseModel): + path: str + content: str + + +@router.post('/tree') +async def workspace_tree(payload: FilePathRequest): + try: + tree = build_tree(payload.path) + except WorkspaceSecurityError as exc: + raise HTTPException(status_code=400, detail=str(exc)) from exc + except Exception as exc: + raise HTTPException(status_code=500, detail=str(exc)) from exc + + return { + 'success': True, + 'tree': tree, + } + + +@router.post('/read') +async def workspace_read(payload: FilePathRequest): + try: + content = read_file(payload.path) + except WorkspaceSecurityError as exc: + raise HTTPException(status_code=400, detail=str(exc)) from exc + except Exception as exc: + raise HTTPException(status_code=500, detail=str(exc)) from exc + + return { + 'success': True, + 'content': content, + } + + +@router.post('/save') +async def workspace_save(payload: SaveFileRequest): + try: + result = save_file(payload.path, payload.content) + except WorkspaceSecurityError as exc: + raise HTTPException(status_code=400, detail=str(exc)) from exc + except Exception as exc: + raise HTTPException(status_code=500, detail=str(exc)) from exc + + return result