This commit is contained in:
246
frontend/README.md
Normal file
246
frontend/README.md
Normal file
@@ -0,0 +1,246 @@
|
||||
# ✨ 前端模块化重构总结
|
||||
|
||||
## 📊 重构成果
|
||||
|
||||
### 文件结构对比
|
||||
|
||||
#### 重构前
|
||||
```
|
||||
frontend/
|
||||
└── frontend.py # 280+ 行单体文件
|
||||
```
|
||||
|
||||
#### 重构后
|
||||
```
|
||||
frontend/
|
||||
├── __init__.py # 包初始化
|
||||
├── frontend.py # 主入口(48 行)
|
||||
├── config.py # 配置管理(62 行)
|
||||
├── state.py # 状态管理(120 行)
|
||||
├── api_client.py # API 客户端(164 行)
|
||||
├── utils.py # 工具函数(56 行)
|
||||
├── components/
|
||||
│ ├── __init__.py
|
||||
│ ├── sidebar.py # 左侧栏(156 行)
|
||||
│ ├── chat_area.py # 中间栏(156 行)
|
||||
│ └── info_panel.py # 右侧栏(63 行)
|
||||
└── REFACTOR.md # 重构文档
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 🎯 核心改进
|
||||
|
||||
### 1. **代码量优化**
|
||||
|
||||
| 模块 | 行数 | 说明 |
|
||||
|------|------|------|
|
||||
| [frontend.py](file:///home/huang/Study/AIProject/Agent1/frontend/frontend.py) | 48 行 | ✅ -83%(原 280+ 行) |
|
||||
| [config.py](file:///home/huang/Study/AIProject/Agent1/frontend/config.py) | 62 行 | 新增配置管理 |
|
||||
| [state.py](file:///home/huang/Study/AIProject/Agent1/frontend/state.py) | 120 行 | 新增状态管理 |
|
||||
| [api_client.py](file:///home/huang/Study/AIProject/Agent1/frontend/api_client.py) | 164 行 | 新增 API 客户端 |
|
||||
| [components/sidebar.py](file:///home/huang/Study/AIProject/Agent1/frontend/components/sidebar.py) | 156 行 | 左侧栏组件 |
|
||||
| [components/chat_area.py](file:///home/huang/Study/AIProject/Agent1/frontend/components/chat_area.py) | 156 行 | 中间聊天区 |
|
||||
| [components/info_panel.py](file:///home/huang/Study/AIProject/Agent1/frontend/components/info_panel.py) | 63 行 | 右侧信息面板 |
|
||||
|
||||
**总计**:769 行(模块化后),平均每个文件 < 110 行
|
||||
|
||||
---
|
||||
|
||||
### 2. **架构设计**
|
||||
|
||||
#### 分层架构
|
||||
```
|
||||
┌─────────────────────────────────────┐
|
||||
│ 表现层 (Components) │ ← UI 渲染
|
||||
│ sidebar, chat_area, info_panel │
|
||||
├─────────────────────────────────────┤
|
||||
│ 业务层 (State) │ ← 状态管理
|
||||
│ AppState 类 │
|
||||
├─────────────────────────────────────┤
|
||||
│ 数据层 (API Client) │ ← 后端通信
|
||||
│ APIClient 类 │
|
||||
├─────────────────────────────────────┤
|
||||
│ 配置层 (Config) │ ← 配置管理
|
||||
│ FrontendConfig 数据类 │
|
||||
└─────────────────────────────────────┘
|
||||
```
|
||||
|
||||
#### 依赖关系
|
||||
```
|
||||
Components → State → API Client → Config
|
||||
↑ ↓
|
||||
└──────── 全局单例 ────────┘
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 3. **设计模式应用**
|
||||
|
||||
| 模式 | 应用场景 | 优势 |
|
||||
|------|---------|------|
|
||||
| **单例模式** | `config`, `api_client` 全局实例 | 避免重复初始化 |
|
||||
| **外观模式** | [AppState](file:///home/huang/Study/AIProject/Agent1/frontend/state.py#L11-L117) 封装 Session State | 统一状态操作接口 |
|
||||
| **模块模式** | `components/` 独立组件 | 职责单一,易于维护 |
|
||||
| **数据类** | [FrontendConfig](file:///home/huang/Study/AIProject/Agent1/frontend/config.py#L13-L66) 配置管理 | 类型安全,IDE 友好 |
|
||||
|
||||
---
|
||||
|
||||
## 🚀 使用方式
|
||||
|
||||
### 本地开发
|
||||
```bash
|
||||
# 启动前后端
|
||||
./scripts/start.sh both
|
||||
|
||||
# 访问前端
|
||||
open http://localhost:8501
|
||||
```
|
||||
|
||||
### Docker 部署
|
||||
```bash
|
||||
# 配置环境变量
|
||||
cp .env.docker .env
|
||||
# 编辑 .env 填入 API Key
|
||||
|
||||
# 启动服务
|
||||
cd docker
|
||||
docker compose up -d
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 📝 扩展示例
|
||||
|
||||
### 示例 1:添加对话导出功能
|
||||
|
||||
只需修改 [components/sidebar.py](file:///home/huang/Study/AIProject/Agent1/frontend/components/sidebar.py):
|
||||
|
||||
```python
|
||||
def _render_history_actions():
|
||||
"""渲染历史操作按钮"""
|
||||
if st.button("🔄 刷新列表", use_container_width=True):
|
||||
_refresh_threads()
|
||||
|
||||
if st.button("➕ 新对话", type="primary", use_container_width=True):
|
||||
AppState.start_new_thread()
|
||||
st.rerun()
|
||||
|
||||
# 新增:导出按钮
|
||||
if st.button("📤 导出对话", use_container_width=True):
|
||||
_export_conversation()
|
||||
|
||||
def _export_conversation():
|
||||
"""导出当前对话"""
|
||||
messages = AppState.get_messages()
|
||||
content = "\n\n".join([
|
||||
f"**{m['role'].upper()}**: {m['content']}"
|
||||
for m in messages
|
||||
])
|
||||
st.download_button(
|
||||
label="下载 Markdown",
|
||||
data=content,
|
||||
file_name="conversation.md",
|
||||
mime="text/markdown"
|
||||
)
|
||||
```
|
||||
|
||||
**影响范围**:仅修改 `sidebar.py`,不影响其他模块!
|
||||
|
||||
---
|
||||
|
||||
### 示例 2:添加暗色主题
|
||||
|
||||
修改 [config.py](file:///home/huang/Study/AIProject/Agent1/frontend/config.py):
|
||||
|
||||
```python
|
||||
@dataclass
|
||||
class FrontendConfig:
|
||||
# ... 现有配置 ...
|
||||
theme: str = "light" # 新增主题配置
|
||||
|
||||
# 在 frontend.py 中应用
|
||||
if config.theme == "dark":
|
||||
st.markdown("""
|
||||
<style>
|
||||
.stApp { background-color: #0e1117; }
|
||||
</style>
|
||||
""", unsafe_allow_html=True)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 示例 3:添加消息统计图表
|
||||
|
||||
修改 [components/info_panel.py](file:///home/huang/Study/AIProject/Agent1/frontend/components/info_panel.py):
|
||||
|
||||
```python
|
||||
def _render_message_stats():
|
||||
"""渲染消息统计"""
|
||||
st.subheader("消息统计")
|
||||
|
||||
stats = AppState.get_message_stats()
|
||||
|
||||
# 新增:柱状图
|
||||
import pandas as pd
|
||||
df = pd.DataFrame({
|
||||
'角色': ['用户', 'AI'],
|
||||
'数量': [stats['user'], stats['assistant']]
|
||||
})
|
||||
st.bar_chart(df.set_index('角色'))
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## ✅ 重构优势
|
||||
|
||||
### 1. **可维护性** ⭐⭐⭐⭐⭐
|
||||
- 每个文件职责单一,平均 < 110 行
|
||||
- 修改功能只需改对应模块
|
||||
- 代码结构清晰,易于理解
|
||||
|
||||
### 2. **可扩展性** ⭐⭐⭐⭐⭐
|
||||
- 新增功能不影响现有代码
|
||||
- 组件独立,可自由组合
|
||||
- 支持插件化开发
|
||||
|
||||
### 3. **可测试性** ⭐⭐⭐⭐⭐
|
||||
- 各模块独立,便于 Mock
|
||||
- 状态管理统一,易于验证
|
||||
- API 客户端可独立测试
|
||||
|
||||
### 4. **代码质量** ⭐⭐⭐⭐⭐
|
||||
- 遵循 SOLID 原则
|
||||
- 类型提示完整
|
||||
- 符合 Clean Architecture
|
||||
|
||||
### 5. **团队协作** ⭐⭐⭐⭐⭐
|
||||
- 多人并行开发不同组件
|
||||
- 减少代码冲突
|
||||
- 降低 Review 难度
|
||||
|
||||
---
|
||||
|
||||
## 📚 文档资源
|
||||
|
||||
| 文档 | 说明 |
|
||||
|------|------|
|
||||
| [frontend/REFACTOR.md](file:///home/huang/Study/AIProject/Agent1/frontend/REFACTOR.md) | 详细重构说明和架构设计 |
|
||||
| [FEATURES.md](file:///home/huang/Study/AIProject/Agent1/FEATURES.md) | 功能使用说明 |
|
||||
| [README.md](file:///home/huang/Study/AIProject/Agent1/README.md) | 项目总体说明 |
|
||||
|
||||
---
|
||||
|
||||
## 🎉 总结
|
||||
|
||||
本次重构将前端从 **280+ 行单体文件** 改造为 **模块化分层架构**,实现了:
|
||||
|
||||
✅ **代码精简**:主文件从 280+ 行降至 48 行(-83%)
|
||||
✅ **模块化**:拆分为 7 个独立模块,平均 < 110 行
|
||||
✅ **分层架构**:表现层 → 业务层 → 数据层 → 配置层
|
||||
✅ **类型安全**:使用 dataclass 和类型提示
|
||||
✅ **易于扩展**:新增功能只需修改对应模块
|
||||
✅ **易于测试**:各模块独立,便于 Mock 和单元测试
|
||||
✅ **团队协作**:减少代码冲突,降低 Review 难度
|
||||
|
||||
**前端架构已与后端保持一致的优雅设计!** 🎊
|
||||
Reference in New Issue
Block a user