Files
X-Agents/agent/app/main.py
DESKTOP-72TV0V4\caoxiaozhu b2bc9988a9 feat: 重构前后端架构,添加Go后端和Python Agent服务
- 新增 Go 语言后端服务(server/),包含用户认证、Agent管理、数据库连接等API
- 新增 Python Agent 服务(agent/),实现Agent核心逻辑和工具集
- 前端从原生HTML迁移到Vue.js框架(web/src/)
- 添加 Docker Compose 支持(docker-compose.yml)
- 添加项目架构文档(docs/ARCHITECTURE.md)
- 添加环境变量示例(.env.example)和本地启动脚本(start-local.ps1)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-06 16:39:42 +08:00

85 lines
1.7 KiB
Python

"""
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"
}