Commit 00f33a30 by sunqi02

feat: call update_skills_json on app startup

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent e4cabd99
import os
from pathlib import Path
from contextlib import asynccontextmanager
from fastapi import FastAPI from fastapi import FastAPI
from app.api.v1.router import api_router from app.api.v1.router import api_router
from app.config.loader import ConfigLoader from app.config.loader import ConfigLoader
from app.service.session_service import SessionService from app.service.session_service import SessionService
from app.service.im_service import IMService from app.service.im_service import IMService
from app.service.console_im_service import ConsoleIMService
from app.service.agent_hub import AgentHub from app.service.agent_hub import AgentHub
from app.infrastructure.database import SessionLocal from app.service.skill_scanner import update_skills_json
from app.infrastructure.database import SessionLocal, engine
app = FastAPI(title="Agent管理后台 API", version="1.0.0") from app.model.session import Base
app.include_router(api_router)
agent_hub_instance = None agent_hub_instance = None
@app.on_event("startup") @asynccontextmanager
async def startup_event(): async def lifespan(app: FastAPI):
global agent_hub_instance global agent_hub_instance
Base.metadata.create_all(bind=engine)
# 启动时扫描 skills 目录,生成 skills.json 索引缓存
skills_dir = Path(__file__).parent.parent / "skills"
config_dir = Path(__file__).parent.parent / "config"
update_skills_json(skills_dir, config_dir)
config_loader = ConfigLoader() config_loader = ConfigLoader()
db = SessionLocal() db = SessionLocal()
session_service = SessionService(db) session_service = SessionService(db)
if os.getenv("USE_CONSOLE_IM", "true").lower() == "true":
im_service = ConsoleIMService()
print("[startup] 使用 ConsoleIMService — hook 输出将打印到控制台")
else:
im_service = IMService( im_service = IMService(
base_url="http://localhost:8080", base_url=os.getenv("IM_BASE_URL", "http://localhost:8080"),
api_key="your-api-key" api_key=os.getenv("IM_API_KEY", "your-api-key")
) )
print(f"[startup] 使用 IMService → {os.getenv('IM_BASE_URL', 'http://localhost:8080')}")
agent_hub_instance = AgentHub( agent_hub_instance = AgentHub(
config_loader=config_loader, config_loader=config_loader,
...@@ -31,14 +46,21 @@ async def startup_event(): ...@@ -31,14 +46,21 @@ async def startup_event():
im_service=im_service im_service=im_service
) )
yield
@app.on_event("shutdown")
async def shutdown_event():
global agent_hub_instance
if agent_hub_instance: if agent_hub_instance:
await agent_hub_instance.close() await agent_hub_instance.close()
app = FastAPI(title="Agent管理后台 API", version="1.0.0", lifespan=lifespan)
app.include_router(api_router)
@app.get("/health") @app.get("/health")
def health_check(): def health_check():
return {"status": "ok"} return {"status": "ok"}
if __name__ == "__main__":
import uvicorn
uvicorn.run("app.main:app", host="0.0.0.0", port=8000, reload=True)
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!