feat: initial XCClaw基础架构
- 基于 FastAPI 的 Web API 服务 - OpenCode API 客户端封装 - 会话管理器(同步/异步任务执行) - APScheduler 定时任务调度 - 完整的 REST API 端点
This commit is contained in:
1
app/api/__init__.py
Normal file
1
app/api/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from app.api.routes import router
|
||||
72
app/api/routes.py
Normal file
72
app/api/routes.py
Normal file
@@ -0,0 +1,72 @@
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from app.models.session import CreateSessionRequest, SessionType
|
||||
from app.services.session_manager import session_manager
|
||||
from app.services.scheduler import scheduler_service, ScheduleTask
|
||||
from app.services.opencode_client import opencode_client
|
||||
|
||||
|
||||
router = APIRouter(prefix="/api/xcclaw", tags=["xcclaw"])
|
||||
|
||||
|
||||
@router.get("/health")
|
||||
async def health_check():
|
||||
try:
|
||||
opencode_status = await opencode_client.health_check()
|
||||
return {"status": "ok", "opencode": opencode_status}
|
||||
except Exception as e:
|
||||
return {"status": "error", "message": str(e)}
|
||||
|
||||
|
||||
@router.post("/task")
|
||||
async def create_task(request: CreateSessionRequest):
|
||||
task = await session_manager.create_task(request)
|
||||
return task
|
||||
|
||||
|
||||
@router.post("/task/{task_id}/execute")
|
||||
async def execute_task(task_id: str):
|
||||
result = await session_manager.execute_task(task_id)
|
||||
return result
|
||||
|
||||
|
||||
@router.post("/task/{task_id}/execute_async")
|
||||
async def execute_task_async(task_id: str):
|
||||
await session_manager.execute_task_async(task_id)
|
||||
return {"status": "started", "task_id": task_id}
|
||||
|
||||
|
||||
@router.post("/task/{task_id}/abort")
|
||||
async def abort_task(task_id: str):
|
||||
result = await session_manager.abort_task(task_id)
|
||||
return {"aborted": result}
|
||||
|
||||
|
||||
@router.get("/task/{task_id}")
|
||||
async def get_task(task_id: str):
|
||||
task = await session_manager.get_task(task_id)
|
||||
if not task:
|
||||
raise HTTPException(status_code=404, detail="Task not found")
|
||||
return task
|
||||
|
||||
|
||||
@router.get("/task")
|
||||
async def list_tasks():
|
||||
return await session_manager.list_tasks()
|
||||
|
||||
|
||||
@router.post("/schedule")
|
||||
async def create_schedule(task: ScheduleTask):
|
||||
return await scheduler_service.add_schedule(task)
|
||||
|
||||
|
||||
@router.get("/schedule")
|
||||
async def list_schedules():
|
||||
return await scheduler_service.list_schedules()
|
||||
|
||||
|
||||
@router.delete("/schedule/{schedule_id}")
|
||||
async def delete_schedule(schedule_id: str):
|
||||
result = await scheduler_service.remove_schedule(schedule_id)
|
||||
if not result:
|
||||
raise HTTPException(status_code=404, detail="Schedule not found")
|
||||
return {"deleted": True}
|
||||
Reference in New Issue
Block a user