"""FastAPI 应用主文件""" import logging import asyncio from fastapi import FastAPI, Request from fastapi.middleware.cors import CORSMiddleware from pydantic import BaseModel from app.agents import orchestrate_agents from app.config import get_settings # 初始化日志 logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) # 初始化设置 settings = get_settings() # 创建 FastAPI 应用 app = FastAPI( title=settings.project_name, version=settings.project_version, debug=settings.fastapi_debug, ) # 添加 CORS 中间件 app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) class FullWorkflowResponse(BaseModel): """完整工作流响应""" status: str = "success" @app.post("/workflow/full", response_model=FullWorkflowResponse) async def full_workflow(request: Request): try: body_bytes = await request.body() message = body_bytes.decode('utf-8') logger.info(f"开始处理需求: {message}") # 调用编排函数,执行三个Agent的工作流 asyncio.create_task( orchestrate_agents(message)) # 构建响应 response = FullWorkflowResponse( status="success" ) return response except Exception as e: logger.error(f"处理需求时出错: {str(e)}", exc_info=True) return { "error": str(e), "status": "error" } if __name__ == "__main__": import uvicorn uvicorn.run( app, host=settings.fastapi_host, port=settings.fastapi_port, log_level="info", )