Spaces:
Sleeping
Sleeping
| from agent.dataset_sync import init_dataset, push_file | |
| from fastapi import FastAPI | |
| from fastapi.staticfiles import StaticFiles | |
| from fastapi.responses import FileResponse | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from pydantic import BaseModel | |
| import uvicorn | |
| import os | |
| from agent.fs import ( | |
| init_workspace, | |
| list_projects, | |
| create_project, | |
| read_file, | |
| write_file, | |
| tree | |
| ) | |
| # ----------- Schemas ----------- | |
| class ReadFileReq(BaseModel): | |
| path: str | |
| start: int = 0 | |
| limit: int = 2000 | |
| class WriteFileReq(BaseModel): | |
| path: str | |
| content: str | |
| class CreateProjectReq(BaseModel): | |
| name: str | |
| # ----------- App ----------- | |
| app = FastAPI() | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| # ----------- API ----------- | |
| def api_list_projects(): | |
| return list_projects() | |
| def api_create_project(req: CreateProjectReq): | |
| return create_project(req.name) | |
| def api_read_file(req: ReadFileReq): | |
| return { | |
| "content": read_file(req.path, req.start, req.limit) | |
| } | |
| def api_write_file(req: WriteFileReq): | |
| write_file(req.path, req.content) | |
| local_path = f"/workspace/agent/{req.path}" | |
| repo_path = f"agent_workspace/{req.path}" | |
| push_file(local_path, repo_path, "write file") | |
| return {"status": "ok"} | |
| def api_tree(path: str = "projects", depth: int = 2): | |
| return tree(path, depth) | |
| # ----------- UI ----------- | |
| app.mount("/static", StaticFiles(directory="static"), name="static") | |
| def root(): | |
| return FileResponse("static/index.html") | |
| def ping(): | |
| return {"status": "ok"} | |
| # ----------- HF ENTRY ----------- | |
| if __name__ == "__main__": | |
| init_dataset() # 🔥 pull permanent files | |
| init_workspace() | |
| uvicorn.run( | |
| "app:app", | |
| host="0.0.0.0", | |
| port=int(os.environ.get("PORT", 7860)) | |
| ) | |