Files
X-Agents/agent/app/main.py

85 lines
1.7 KiB
Python
Raw Normal View History

"""
X-Agents Python Agent Service
智能体引擎服务入口
"""
import os
from contextlib import asynccontextmanager
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from app.api import routes
from app.agent.core.agent import AgentManager
from app.security.audit import AuditLogger
# 全局组件
agent_manager: AgentManager = None
audit_logger: AuditLogger = None
@asynccontextmanager
async def lifespan(app: FastAPI):
"""应用生命周期管理"""
global agent_manager, audit_logger
# 启动时初始化
audit_logger = AuditLogger()
# 初始化 Agent 管理器
agent_manager = AgentManager(
llm_provider=os.getenv("LLM_PROVIDER", "openai"),
openai_api_key=os.getenv("OPENAI_API_KEY"),
anthropic_api_key=os.getenv("ANTHROPIC_API_KEY"),
)
# 加载 Agent 配置
await agent_manager.load_agents()
print("Agent service started successfully")
yield
# 关闭时清理
print("Agent service shutting down")
# 创建 FastAPI 应用
app = FastAPI(
title="X-Agents Agent Service",
description="AI Agent Engine for X-Agents Platform",
version="1.0.0",
lifespan=lifespan,
)
# CORS 中间件
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# 注册路由
app.include_router(routes.router, prefix="/agent", tags=["Agent"])
@app.get("/health")
async def health_check():
"""健康检查"""
return {
"status": "healthy",
"service": "agent",
"version": "1.0.0"
}
@app.get("/")
async def root():
"""根路径"""
return {
"message": "X-Agents Agent Service",
"docs": "/docs"
}