Merge branch 'refactor/backend-architecture' into development

This commit is contained in:
yangshilin
2026-03-18 17:18:23 +08:00
parent 2070a03d35
commit 48b70e1350
266 changed files with 12386 additions and 9690 deletions

View File

@@ -22,57 +22,32 @@ Life Echo API 是一个智能对话系统,通过 WebSocket 实时连接,使
```
api/
├── main.py # FastAPI 应用入口
├── requirements.txt # Python 依赖
├── Dockerfile # Docker 镜像构建文件
├── docker-compose.yml # 生产环境 Docker Compose
├── docker-compose.dev.yml # 开发环境 Docker Compose
├── agents/ # LangChain Agent
│ ├── conversation_agent.py # 对话引导 Agent
│ ├── memory_agent.py # 记忆整理 Agent
├── memoir_processor.py # 回忆录处理器
│ ├── state_schema.py # 状态模式定义
└── prompts/ # Agent 提示词
├── conversation_prompts.py
── memory_prompts.py
├── database/ # 数据库模块
│ ├── database.py # 数据库连接和初始化
└── models.py # SQLAlchemy 数据模型
├── routers/ # API 路由
│ ├── auth.py # 认证 API注册、登录、刷新令牌
│ ├── conversations.py # 对话管理 API
├── chapters.py # 章节管理 API
│ ├── books.py # 回忆录管理 API
│ ├── websocket.py # WebSocket 端点
── memoir_state.py # 回忆录状态 API
│ ├── user.py # 用户信息 API
│ ├── plans.py # 套餐管理 API
│ ├── orders.py # 订单管理 API
│ ├── quota.py # 配额管理 API
│ ├── tasks.py # 任务管理 API
│ ├── faqs.py # 常见问题 API
│ └── feedback.py # 反馈 API
├── middleware/ # 中间件
│ └── auth.py # 认证中间件
├── services/ # 业务服务
│ ├── auth_service.py # 认证服务密码哈希、JWT
│ ├── asr_service.py # 语音识别服务
│ ├── tts_service.py # 语音合成服务
│ ├── pdf_service.py # PDF 生成服务
│ ├── llm_service.py # LLM 服务DeepSeek/兼容 OpenAI 的 LLM
│ ├── memoir_state_service.py # 回忆录状态服务
│ ├── redis_service.py # Redis 服务
│ └── task_tracker.py # 任务追踪服务
├── main.py # 应用入口uvicorn 启动)
├── app/ # 应用主包
│ ├── main.py # FastAPI 应用定义
│ ├── core/ # 核心基础设施
│ │ ├── config.py # 配置pydantic-settings
├── db.py # 数据库连接
│ ├── redis.py # Redis 服务
│ ├── security.py # JWT、密码哈希
│ └── task_tracker.py # 任务状态追踪
│ ├── features/ # 功能模块(各模块含 router、service、repo
│ ├── auth/ # 认证(注册、登录、短信验证码)
├── user/ # 用户信息
── conversation/ # 对话、WebSocket
├── memory/ # 记忆检索
│ ├── memoir/ # 回忆录、章节、PDF、图像生成
│ ├── payment/ # 支付
├── plan/ # 套餐
│ ├── quota/ # 配额
│ ├── tasks/ # 任务状态 API
│ └── content/ # 内容TTS 等)
│ ├── adapters/ # 外部能力适配器ASR、TTS、LLM、短信、存储等
│ ├── ports/ # 能力契约Protocol
── agents/ # LangChain Agent
├── tasks/ # Celery 后台任务
│ ├── celery_app.py # Celery 应用配置
│ └── memoir_tasks.py # 回忆录处理任务
└── docs/ # 详细文档
├── README.md # 文档索引
├── 本地开发环境配置.md
├── WebSocket快速测试指南.md
├── WebSocket测试文档.md
├── 文字交流模式说明.md
└── 测试脚本使用说明.md
```
## 环境配置
@@ -126,13 +101,13 @@ DEEPSEEK_API_KEY=sk-xxxxxxxxxxxxx
DEEPSEEK_MODEL=deepseek-chat
```
### 3. 初始化数据库
### 3. 数据库迁移
数据库会在首次启动时自动初始化,或手动初始化
数据库 schema 由 Alembic 管理,首次或 schema 变更后执行
```python
from database import init_db
init_db()
```bash
cd api
uv run alembic upgrade head
```
## 快速启动
@@ -507,15 +482,16 @@ app.include_router(your_router.router)
### 添加新的数据库模型
1. `database/models.py` 中定义模型类
2. 继承 `Base`
3. 运行数据库迁移(或重新初始化)
1.对应 feature 的 `app/features/<feature_name>/models.py` 中定义模型类
2. 继承 `Base`(从 `app.core.db` 导入)
3. `app/main.py` 中 import 该 models 模块以注册到 Base.metadata
4. 运行 Alembic 迁移
### 添加新的服务
1.`services/` 目录创建服务文件
2. 实现服务类和方法
3.路由或其他服务中导入使用
1.`app/features/<feature_name>/service.py` 中实现业务逻辑
2. 通过 `deps.py` 提供依赖注入
3.对应 feature 的 router 中通过 `Depends(get_xxx_service)` 使用
## 安全注意事项