refactor: refactor of backend
This commit is contained in:
1 parent
d4cb8c5def
commit
14e5af0c9c
55 files changed
+2251
-1805
No files matched your search
@@ -15,12 +15,13 @@
|
||||
| 新增 API / 业务功能 | §2 目标目录总览 → §3 分层 → §6 新功能清单 |
|
||||
| 修改现有路由 / service | §4 设计原则 → §5 各域速查 → 对照同域已有代码 |
|
||||
| AI Agent 写代码 | §4.1 依赖红线 + §7 禁止项 + §8 import 规范 |
|
||||
| 拆 god 文件 | §2.2 映射关系 + §9.2 API 迁包步骤 |
|
||||
| 后台异步任务 | §5.8 jobs 域 + §9 Wave 2 |
|
||||
| 拆 god 文件 | §2.3 映射关系 + §9.6 API 迁包步骤 |
|
||||
| 后台异步任务 | §5.2 jobs 域 + §9.3 Wave 2 |
|
||||
| 各 Wave 任务清单 | §9.1–§9.5 |
|
||||
|
||||
**合并前自检**:`pytest backend/tests` 通过;改表结构须配套 Alembic 迁移;对外 URL 保持不变。
|
||||
|
||||
**当前进度**:Wave 1–3 已完成;Wave 4 已完成(排程迁出 Web;`integrations/` 真迁移;根路径兼容 shim 已删除;`FILE_CHECK_RUN` / `WECOM_DIRECTORY_SYNC` 由 schedule worker 执行;`services/` 不再直接 `import flask.request`,HTTP 读取收口到 `core/http_context.py`)。残留:固资快照线程仍在 Web 进程(另行治理)。
|
||||
**当前进度**:Wave 1–4 已完成;固资快照已迁出 Web(`hxops-fixed-assets-worker`);Tool API 真实现收口到 `api/tools/`;其余扁平 API 已迁为同名包;`before_request` 配置热更新改为进程内 TTL(`APP_SETTINGS_REFRESH_SECONDS`,默认 30s),不再每请求全量读 `app_settings`。
|
||||
|
||||
---
|
||||
|
||||
@@ -57,7 +58,7 @@
|
||||
| 问题 | 典型文件 | 约行数 |
|
||||
| --- | --- | --- |
|
||||
| API god 文件 | `api/chat.py`、`api/user.py`、`api/admin.py` 均已拆包 | 原单体 1400–2200 |
|
||||
| Service monolith | `services/text2sql.py`、`shipping_notice/tools.py` | 1200–2000 |
|
||||
| Service monolith | `services/integrations/text2sql.py`、`shipping_notice/tools.py` | 1200–2000 |
|
||||
| 单文件模型 | `models.py` | 796(30+ 模型) |
|
||||
| 错误响应不统一 | API 混用 `AppError` 与 `jsonify({'error':...})` | 300+ 处 |
|
||||
| 后台执行分散 | scheduler 线程、API 内 `threading`、`worker_kb.py` | — |
|
||||
@@ -156,22 +157,33 @@ backend/
|
||||
│ │ │ ├── upload.py
|
||||
│ │ │ └── jobs.py # 触发清洗、查 job 状态
|
||||
│ │ │
|
||||
│ │ ├── agents/ # ← 现有 agents.py(体量小,可单文件)
|
||||
│ │ ├── integration/ # ← 现有 integration.py
|
||||
│ │ ├── agents/ # ← 已迁包
|
||||
│ │ ├── integration/ # ← 已迁包
|
||||
│ │ ├── maintenance/
|
||||
│ │ ├── speech/
|
||||
│ │ ├── sso/
|
||||
│ │ ├── linked_services/
|
||||
│ │ ├── training/
|
||||
│ │ ├── contract_payment/
|
||||
│ │ ├── in_transit_transfer/
|
||||
│ │ ├── integration_connector/
|
||||
│ │ │
|
||||
│ │ ├── workspace/ # Wave 0 骨架已建
|
||||
│ │ ├── workspace/ # 已迁包
|
||||
│ │ │ ├── schedule.py
|
||||
│ │ │ ├── shipping_notice.py
|
||||
│ │ │ └── file_check.py
|
||||
│ │ │
|
||||
│ │ ├── tools/ # Dify HTTP Tool API;Wave 0 骨架已建
|
||||
│ │ ├── tools/ # Dify HTTP Tool API(真实现已收口)
|
||||
│ │ │ ├── shipping_notice.py # /api/shipping-notice
|
||||
│ │ │ └── file_check.py # /api/file-check
|
||||
│ │ │ ├── file_check.py # /api/file-check
|
||||
│ │ │ ├── maintenance.py # /api/maintenance/tools
|
||||
│ │ │ ├── agent_runtime_context.py
|
||||
│ │ │ └── procurement_contract.py
|
||||
│ │ │
|
||||
│ │ ├── internal/
|
||||
│ │ │ └── jobs.py # /api/internal/jobs
|
||||
│ │ │
|
||||
│ │ └── speech/ # ← 现有 speech.py
|
||||
│ │ └── files/
|
||||
│ │
|
||||
│ ├── services/ # 业务逻辑层(禁止 flask.request)
|
||||
│ │ ├── chat/
|
||||
@@ -261,7 +273,7 @@ Python 3 中 **模块文件与同名包不能并存**,否则包覆盖模块、
|
||||
| --- | --- |
|
||||
| `models.py` 与 `models/` | Wave 1:拆入 `models/*.py` 后**删除** `models.py` |
|
||||
| `api/chat.py` 与 `api/chat/` | 先 rename 为 `chat/_legacy.py`,再建包 |
|
||||
| 同理 `user.py`、`knowledge_base.py` | 标准步骤见 §9.2(`admin.py` 已删除,仅保留 `api/admin/` 包) |
|
||||
| 同理 `user.py`、`knowledge_base.py` | 标准步骤见 §9.6(`admin.py` 已删除,仅保留 `api/admin/` 包) |
|
||||
|
||||
**Wave 0 已建、无冲突的分包**:`api/schedule/`、`api/workspace/`、`api/tools/`、`api/internal/`、`core/`、`jobs/`、`schemas/`、`services/{chat,user,admin,...}/`。
|
||||
|
||||
@@ -405,7 +417,7 @@ jobs/handlers/kb_clean.py # worker 执行 → services/kb → edkb_system
|
||||
| **kb** | `/api/kb` | `api/kb/` | `services/kb/*` | `knowledge_base.py`;异步清洗 |
|
||||
| **agents** | `/api/agents` | `api/agents/` | 薄,可保留单文件 | ~118 行 |
|
||||
| **workspace** | `/api/workspace` | `api/workspace/` | 各域 query_service | 骨架已建 |
|
||||
| **tools** | `/api/shipping-notice`、`/api/file-check` | `api/tools/` | `shipping_notice/`、`file_check_tools/` | 已有 OpenAPI |
|
||||
| **tools** | `/api/shipping-notice`、`/api/file-check`、`/api/maintenance`、`/api/agent-runtime-context`、`/api/procurement-contract` | `api/tools/` | `shipping_notice/`、`file_check_tools/`、`maintenance_*` | 真实现已在 `api/tools/`;根路径 `*_tools.py` 为兼容 shim |
|
||||
| **integration** | `/api/integration` | `api/integration/` | `integration_api.py` | 源文件反查 |
|
||||
| **speech** | `/api/speech` | `api/speech/` | `speech/asr.py` | 体量小 |
|
||||
| **internal** | `/api/internal/jobs` | `api/internal/` | jobs 回调 | 骨架已建 |
|
||||
@@ -511,26 +523,80 @@ from app.models import User, Agent # Wave 1 后仍保持此入口
|
||||
|
||||
## 9. 分阶段迁移
|
||||
|
||||
主线 Wave 0–4 **均已完成**。下表为总览;各 Wave 细项见 §9.1–§9.5。迁包操作规程见 §9.6;完成定义见 §9.7。
|
||||
|
||||
```text
|
||||
Wave 0 骨架建立 ✅ 目录 + 占位(行为不变)
|
||||
Wave 1 基础设施 ✅ register_blueprints / extensions / core / AppError / models
|
||||
Wave 2 任务层 ✅ jobs/ 接 KB / shipping / schedule 等 worker
|
||||
Wave 3 拆 god 文件 admin ✅ → user ✅ → chat ✅ → kb ✅
|
||||
Wave 4 集成收敛 ✅ scheduler 迁出 web;integrations/ 真迁移;根路径 shim 已删除;file-check / 企微通讯录入队
|
||||
Wave 3 拆 god 文件 ✅ admin → user → chat → kb 迁包
|
||||
Wave 4 集成收敛 ✅ scheduler 迁出 web;integrations/ 真迁移;file-check / 企微入队
|
||||
后续 深化收口 ✅ 固资 worker / Tool API / 扁平 API 包 / 配置 TTL(非原 Wave 编号)
|
||||
```
|
||||
|
||||
### 9.1 Wave 1 任务清单
|
||||
### 9.1 Wave 0 — 骨架建立
|
||||
|
||||
| # | 任务 | 风险 | 状态 |
|
||||
| --- | --- | --- | --- |
|
||||
| 1 | 建立 `core/`、`jobs/`、`schemas/`、`api/{schedule,workspace,tools,internal}/` 目录与占位 | 低 | ✅ |
|
||||
| 2 | 建立 `services/{chat,user,admin,auth,kb,integrations,...}/` 骨架(多为 re-export) | 低 | ✅ |
|
||||
| 3 | 增加 `worker.py` / `workers/` 兼容入口骨架 | 低 | ✅ |
|
||||
| 4 | **不改** `create_app` 行为(零行为变更) | — | ✅ |
|
||||
|
||||
### 9.2 Wave 1 — 基础设施
|
||||
|
||||
| # | 任务 | 风险 | 状态 |
|
||||
| --- | --- | --- | --- |
|
||||
| 1 | `create_app` 改用 `register_blueprints(app)` | 低 | ✅ |
|
||||
| 2 | 修复 `deploy/gunicorn_config.py` workers 未赋值 | 低 | ✅ |
|
||||
| 3 | `extensions.py` 抽离 `db` / `migrate` | 低 | ✅ |
|
||||
| 4 | `core/*` 迁入 + 删除根 shim | 低 | ✅ |
|
||||
| 5 | `AppError` 统一 + 契约测试 | 中 | ✅ |
|
||||
| 6 | `models/` 按域拆分 | 中 | ✅ |
|
||||
| 4 | `core/*` 迁入 + 删除根 shim(`errors` / `auth_jwt` 等) | 低 | ✅ |
|
||||
| 5 | `AppError` 统一 + 契约测试(Tool API 保留例外格式) | 中 | ✅ |
|
||||
| 6 | `models/` 按域拆分,删除单体 `models.py` | 中 | ✅ |
|
||||
|
||||
### 9.2 API 单体迁包标准步骤
|
||||
### 9.3 Wave 2 — 统一任务层
|
||||
|
||||
| # | 任务 | 风险 | 状态 |
|
||||
| --- | --- | --- | --- |
|
||||
| 1 | 落地 `jobs/{types,registry,queue,store,runner,context}` | 中 | ✅ |
|
||||
| 2 | KB 清洗 / Dify sync 经 `jobs` + `worker.py` / `worker_kb.py` | 中 | ✅ |
|
||||
| 3 | shipping / transfer 等业务 handler 入 `jobs/handlers` | 中 | ✅ |
|
||||
| 4 | Compose / 本地脚本接 kb / shipping / transfer worker | 低 | ✅ |
|
||||
|
||||
### 9.4 Wave 3 — 拆 god 文件(API 迁包)
|
||||
|
||||
| # | 任务 | 风险 | 状态 |
|
||||
| --- | --- | --- | --- |
|
||||
| 1 | `api/admin.py` → `api/admin/*` + `services/admin/*` | 中 | ✅ |
|
||||
| 2 | `api/user.py` → `api/user/*`(local / Casdoor / 企微 / 密码重置)+ `auth_support` | 高 | ✅ |
|
||||
| 3 | `api/chat.py` → `api/chat/*`(SSE / 会话 / 预览 / 上传 / 反馈) | 高 | ✅ |
|
||||
| 4 | `knowledge_base.py` → `api/kb/*` + `services/kb/*` | 中 | ✅ |
|
||||
| 5 | 删除被包遮蔽的残留单体(如旧 `chat.py`) | 低 | ✅ |
|
||||
|
||||
迁包操作步骤见 §9.6。
|
||||
|
||||
### 9.5 Wave 4 — 集成收敛
|
||||
|
||||
| # | 任务 | 风险 | 状态 |
|
||||
| --- | --- | --- | --- |
|
||||
| 1 | 排程迁出 Web:`hxops-schedule-worker`;无 in-process scheduler 线程 | 中 | ✅ |
|
||||
| 2 | `FILE_CHECK_RUN` / `WECOM_DIRECTORY_SYNC` handler 落地,去掉 API 内 `threading` | 中 | ✅ |
|
||||
| 3 | `services/integrations/` 真迁移(dify / oa / plm / wecom / minio / text2sql 等) | 高 | ✅ |
|
||||
| 4 | 调用方 import 翻转到 `app.services.integrations.*`;根路径 shim 已删除 | 中 | ✅ |
|
||||
| 5 | `services/` 去 `flask.request`;HTTP 读口收到 `core/http_context.py` | 中 | ✅ |
|
||||
|
||||
### 9.5.1 Wave 4 之后的深化(已做,非原 Wave 编号)
|
||||
|
||||
| # | 任务 | 状态 |
|
||||
| --- | --- | --- |
|
||||
| 1 | 固资快照迁出 Web → `workers/worker_fixed_assets.py` / `hxops-fixed-assets-worker` | ✅ |
|
||||
| 2 | Tool API 真实现收口到 `api/tools/`(根路径 `*_tools.py` 为兼容 shim) | ✅ |
|
||||
| 3 | 其余扁平 API 迁同名包(agents / training / integration / contract_payment 等) | ✅ |
|
||||
| 4 | `before_request` 配置热更新:进程内 TTL(`APP_SETTINGS_REFRESH_SECONDS`,默认 30s) | ✅ |
|
||||
|
||||
仍开放:业务大文件持续拆分(>800 行,尤其 `shipping_notice/`),见 §9.7。
|
||||
|
||||
### 9.6 API 单体迁包标准步骤
|
||||
|
||||
```text
|
||||
1. app/api/user.py → app/api/user/_legacy.py
|
||||
@@ -539,15 +605,17 @@ Wave 4 集成收敛 ✅ scheduler 迁出 web;integrations/ 真迁移
|
||||
4. _legacy 归零后删除
|
||||
```
|
||||
|
||||
较小平坦模块也可直接 `git mv api/foo.py → api/foo/__init__.py`(import 路径不变)。
|
||||
|
||||
`monkeypatch('app.api.user._get_current_user')` 须在 `__init__.py` 显式 re-export 保持可用。
|
||||
|
||||
### 9.3 完成定义
|
||||
### 9.7 完成定义
|
||||
|
||||
- [ ] 无 >800 行业务源文件(`edkb_system/` 除外)
|
||||
- [ ] 无 >800 行业务源文件(`edkb_system/` 除外)— **未完成**,域内持续拆分
|
||||
- [x] API 统一 `AppError`(Tool API 除外,见 `docs/convention/error_handling.md` §2.5)
|
||||
- [x] `services/` 无 `flask.request`(HTTP 头/Host 经 `app.core.http_context`)
|
||||
- [x] 后台任务统一经 `jobs/`(file-check、企微通讯录同步已入队;固资快照线程另行治理)
|
||||
- [x] web 进程无 scheduler thread(排程由 `hxops-schedule-worker` 执行;固资快照线程另行治理)
|
||||
- [x] 后台任务统一经 `jobs/`(file-check、企微通讯录同步已入队;固资快照为独立常驻 worker,非 Web 线程)
|
||||
- [x] web 进程无 scheduler / 固资同步线程(排程 → `hxops-schedule-worker`;固资快照 → `hxops-fixed-assets-worker`)
|
||||
|
||||
---
|
||||
|
||||
|
||||
Reference in new issue
Block a user