diff --git a/backend/app/backend.py b/backend/app/backend.py index c8ea409..05825d4 100644 --- a/backend/app/backend.py +++ b/backend/app/backend.py @@ -4,7 +4,8 @@ FastAPI 后端 - 支持动态模型切换,使用 PostgreSQL 持久化记忆 """ import os -from app.config import DB_URI, BACKEND_PORT +# from app.config import DB_URI, BACKEND_PORT +from app.config import BACKEND_PORT import uuid import json from contextlib import asynccontextmanager @@ -14,7 +15,7 @@ from fastapi import FastAPI, HTTPException, WebSocket, WebSocketDisconnect, Depe from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import StreamingResponse from pydantic import BaseModel -from langgraph.checkpoint.postgres.aio import AsyncPostgresSaver +from langgraph.checkpoint.memory import MemorySaver from .agent.service import AIAgentService from .agent.history import ThreadHistoryService from app.core.human_review import ( @@ -26,56 +27,42 @@ from app.core.human_review import ( from app.subgraphs.contact.api_client import ContactAPIClient from app.subgraphs.dictionary.api_client import DictionaryAPIClient from app.subgraphs.news_analysis.api_client import NewsAPIClient -from .db.init_db import init_subgraph_tables -from .db.models import ContactRepository, DictionaryRepository, NewsRepository +# from .db.init_db import init_subgraph_tables +# from .db.models import ContactRepository, DictionaryRepository, NewsRepository from app.logger import info, error @asynccontextmanager async def lifespan(app: FastAPI): - """应用生命周期管理:创建并注入全局服务""" - # 1. 创建数据库连接池并初始化表(仅 checkpointer) - async with AsyncPostgresSaver.from_conn_string(DB_URI) as checkpointer: - await checkpointer.setup() - - # 1.5 初始化子图表 - await init_subgraph_tables(checkpointer.conn) - - # 2. 构建 AI Agent 服务 - agent_service = AIAgentService(checkpointer) - await agent_service.initialize() - - # 3. 创建历史查询服务(保持原有的 checkpointer 参数) - history_service = ThreadHistoryService(checkpointer) - - # 3.5 创建子图 Repositories - contact_repo = ContactRepository(checkpointer.conn) - dictionary_repo = DictionaryRepository(checkpointer.conn) - news_repo = NewsRepository(checkpointer.conn) - - # 3.6 创建子图 API 客户端(真实数据库模式) - contact_api = ContactAPIClient(checkpointer.conn) - dictionary_api = DictionaryAPIClient(word_repository=dictionary_repo) - news_api = NewsAPIClient(news_repository=news_repo) - - # 4. 创建审核管理器 - review_manager = ReviewManager(InMemoryReviewStore()) - - # 5. 将服务实例存入 app.state - app.state.agent_service = agent_service - app.state.history_service = history_service - app.state.review_manager = review_manager - app.state.contact_api = contact_api - app.state.dictionary_api = dictionary_api - app.state.news_api = news_api - app.state.contact_repo = contact_repo - app.state.dictionary_repo = dictionary_repo - app.state.news_repo = news_repo - - # 应用运行中... - yield - - # 6. 关闭时自动清理数据库连接(async with 负责) - info("🛑 应用关闭,数据库连接池已释放") + """应用生命周期管理:创建并注入全局服务(临时用内存 checkpoint)""" + # 1. 创建内存 checkpointer(临时测试) + checkpointer = MemorySaver() + + # 2. 构建 AI Agent 服务 + agent_service = AIAgentService(checkpointer) + await agent_service.initialize() + + # 3. 创建历史查询服务(保持原有的 checkpointer 参数) + history_service = ThreadHistoryService(checkpointer) + + # 4. 创建审核管理器 + review_manager = ReviewManager(InMemoryReviewStore()) + + # 5. 将服务实例存入 app.state + app.state.agent_service = agent_service + app.state.history_service = history_service + app.state.review_manager = review_manager + app.state.contact_api = ContactAPIClient() + app.state.dictionary_api = DictionaryAPIClient() + app.state.news_api = NewsAPIClient() + app.state.contact_repo = None + app.state.dictionary_repo = None + app.state.news_repo = None + + # 应用运行中... + yield + + # 6. 关闭时清理 + info("🛑 应用关闭") app = FastAPI(lifespan=lifespan) diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index 8c343a0..b40ccc0 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -15,7 +15,7 @@ services: # ========================================================================= # PostgreSQL 数据库配置 # ========================================================================= - - DB_HOST=115.190.121.151 + - DB_HOST=ai-postgres - DB_PORT=5432 - DB_USER=postgres - DB_PASSWORD=${DB_PASSWORD:?请配置 DB_PASSWORD(本地:.env 文件 | CI/CD:Secrets)} # ⭐ 敏感密钥配置