- Create new skill_manager.py with list and upload endpoints - Add GET /api/v1/skill/list to retrieve official and user skills - Add POST /api/v1/skill/upload for skill file upload - Parse SKILL.md frontmatter to extract name and description - Move skill upload endpoint from files.py to skill_manager.py - Add SKILLS_DIR configuration to settings.py - Register skill_manager router in fastapi_app.py 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
98 lines
2.9 KiB
Python
98 lines
2.9 KiB
Python
import json
|
||
import os
|
||
import uuid
|
||
import time
|
||
import multiprocessing
|
||
import sys
|
||
from contextlib import asynccontextmanager
|
||
|
||
import uvicorn
|
||
from fastapi import FastAPI
|
||
from fastapi.staticfiles import StaticFiles
|
||
from fastapi.middleware.cors import CORSMiddleware
|
||
from routes.file_manager import router as file_manager_router
|
||
import logging
|
||
|
||
from utils.log_util.logger import init_with_fastapi
|
||
|
||
|
||
# Import route modules
|
||
from routes import chat, files, projects, system, skill_manager
|
||
|
||
|
||
@asynccontextmanager
|
||
async def lifespan(app: FastAPI):
|
||
"""FastAPI 应用生命周期管理"""
|
||
# 启动时初始化
|
||
logger.info("Starting up...")
|
||
from agent.checkpoint_manager import (
|
||
init_global_checkpointer,
|
||
get_checkpointer_manager,
|
||
close_global_checkpointer
|
||
)
|
||
from utils.settings import CHECKPOINT_CLEANUP_ENABLED
|
||
|
||
await init_global_checkpointer()
|
||
logger.info("Global checkpointer initialized")
|
||
|
||
# 启动 checkpoint 清理调度器
|
||
if CHECKPOINT_CLEANUP_ENABLED:
|
||
manager = get_checkpointer_manager()
|
||
# 启动时立即执行一次清理
|
||
try:
|
||
result = await manager.cleanup_old_dbs()
|
||
logger.info(f"Startup cleanup completed: {result}")
|
||
except Exception as e:
|
||
logger.warning(f"Startup cleanup failed (non-fatal): {e}")
|
||
# 启动定时清理调度器
|
||
manager.start_cleanup_scheduler()
|
||
logger.info("Checkpoint cleanup scheduler started")
|
||
|
||
yield
|
||
|
||
# 关闭时清理
|
||
logger.info("Shutting down...")
|
||
await close_global_checkpointer()
|
||
logger.info("Global checkpointer closed")
|
||
|
||
|
||
app = FastAPI(title="Database Assistant API", version="1.0.0", lifespan=lifespan)
|
||
|
||
init_with_fastapi(app)
|
||
|
||
logger = logging.getLogger('app')
|
||
|
||
|
||
# 挂载public文件夹为静态文件服务
|
||
app.mount("/public", StaticFiles(directory="public"), name="static")
|
||
|
||
# 添加CORS中间件,支持前端页面
|
||
app.add_middleware(
|
||
CORSMiddleware,
|
||
allow_origins=["*"], # 在生产环境中应该设置为具体的前端域名
|
||
allow_credentials=True,
|
||
allow_methods=["GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD", "PATCH"],
|
||
allow_headers=[
|
||
"Authorization", "Content-Type", "Accept", "Origin", "User-Agent",
|
||
"DNT", "Cache-Control", "Range", "X-Requested-With"
|
||
],
|
||
)
|
||
|
||
# Include all route modules
|
||
app.include_router(chat.router)
|
||
app.include_router(files.router)
|
||
app.include_router(projects.router)
|
||
app.include_router(system.router)
|
||
app.include_router(skill_manager.router)
|
||
|
||
# 注册文件管理API路由
|
||
app.include_router(file_manager_router)
|
||
|
||
|
||
if __name__ == "__main__":
|
||
# 启动 FastAPI 应用
|
||
logger.info("Starting FastAPI server...")
|
||
logger.info("File Manager API available at: http://localhost:8001/api/v1/files")
|
||
logger.info("Web Interface available at: http://localhost:8001/public/file-manager.html")
|
||
uvicorn.run(app, host="0.0.0.0", port=8001)
|