Add AgentSessionService and refactor agent routes
Move session-related responsibilities into a new application-layer AgentSessionService (and AgentSessionFeedbackResult dataclass), provide a bootstrap factory (get_agent_session_service), and update agent API routes to call the service instead of accessing ConversationStore directly. Routes now translate ValueError into 404 responses and use service methods for get/list/history/delete/feedback. Also update package exports and docs/READMEs to declare the backend architecture authority, enforce api -> application -> domain ports -> infrastructure boundaries, and call out legacy services/workflows as migration-only. These changes centralize session logic in the application layer and tighten architecture guidance for future backend work.
This commit is contained in:
@@ -10,6 +10,31 @@
|
||||
- 本文档负责冻结目标模块边界、依赖规则和实现组织方式。
|
||||
- 后续任何代码重构、能力替换或底座升级,都应同时满足 RFC 与本文档。
|
||||
|
||||
## 1.1 Document Status And Authority
|
||||
|
||||
本文档不是仅供参考的“目标态草案”,而是当前 backend 持续开发的强制架构基线。
|
||||
|
||||
- 新增 backend 功能默认必须遵守本文档定义的模块边界与依赖方向。
|
||||
- 历史实现、迁移中代码和兼容 façade 的存在,不构成继续偏离本文档的理由。
|
||||
- 当现状与本文档冲突时,新增代码按本文档落位;旧代码按迁移计划逐步收口,但不允许继续扩大 legacy 边界。
|
||||
- 评审、重构验收和后续架构讨论,均以本文档作为 backend 内部结构的 authority。
|
||||
|
||||
## 1.2 Authoritative Scope
|
||||
|
||||
本文档约束的 backend 范围包括:
|
||||
|
||||
- `backend/app/api/*`
|
||||
- `backend/app/application/*`
|
||||
- `backend/app/domain/*`
|
||||
- `backend/app/infrastructure/*`
|
||||
- `backend/app/shared/*`
|
||||
|
||||
说明:
|
||||
|
||||
- `backend/app/services/*` 与 `backend/app/workflows/*` 当前属于迁移期 legacy 目录,不是新增业务逻辑的默认落点。
|
||||
- `backend/app/api/routes/docs.py` 与 `backend/app/api/routes/rag.py` 视为遗留或非主入口,除迁移、兼容或下线动作外,不应继续扩展。
|
||||
- `backend/app/api/routes/compliance.py` 当前仍对外暴露,但尚未完全满足本文档约束;在迁移到 application service 之前,应视为受控 legacy 入口,而不是新的架构样板。
|
||||
|
||||
## 2. Current-State Problems
|
||||
|
||||
基于当前代码,后端已经具备以下能力:
|
||||
@@ -22,6 +47,18 @@
|
||||
|
||||
但这些能力当前主要是“可运行”,还不是“结构清晰、便于替换、便于演进”的状态。核心问题如下。
|
||||
|
||||
### 2.0 Current-State Verdict
|
||||
|
||||
基于当前仓库,现状裁决如下:
|
||||
|
||||
- 已基本符合:`documents` 上传/查询主链路已经通过 `DocumentCommandService` 与 `DocumentQueryService` 收口。
|
||||
- 已基本符合:`knowledge` 检索已经通过 `KnowledgeRetrievalService` 统一对外暴露。
|
||||
- 已基本符合:`agent` 问答主链路已经通过 `AgentConversationService` 收口,`shared/bootstrap.py` 已承担 composition root 角色。
|
||||
- 部分符合:Agent session 详情、历史、删除、反馈等接口曾经直接访问 `ConversationStore`,需要继续收口到 application service。
|
||||
- 未完全符合:`compliance` 路由仍直接处理文件落盘、任务状态和 mock 结果,不符合 `api -> application -> domain ports -> infrastructure`。
|
||||
- 未完全符合:部分 `infrastructure` adapter 仍依赖 `services/*` 内的 legacy 实现,说明迁移尚未彻底完成。
|
||||
- 未完全符合:`api/main.py` 的生命周期预热逻辑仍直接依赖旧 LLM factory,尚未完全回到统一 wiring 边界。
|
||||
|
||||
### 2.1 `DocumentProcessor` 责任过载
|
||||
|
||||
现状判断:
|
||||
@@ -603,6 +640,7 @@ infrastructure -> external systems
|
||||
- `application` 只能依赖 `domain`、端口接口,以及通过 composition root 注入进来的实现实例
|
||||
- `domain` 不能依赖 `api` 或 `infrastructure`
|
||||
- `infrastructure` 可以依赖 `domain` 定义的端口和数据模型,但不能反向驱动 application 逻辑
|
||||
- `api/main.py` 这类应用入口可以保留轻量 startup/shutdown 生命周期代码,但不应长期直接依赖 legacy service factory;预热与装配逻辑应逐步收口到明确的 wiring 边界
|
||||
|
||||
说明:
|
||||
|
||||
@@ -739,6 +777,54 @@ infrastructure -> external systems
|
||||
- 内部 DTO / VO / domain object 收敛到 `application` 或 `domain`
|
||||
- 不允许 API model 直接渗透到 domain
|
||||
|
||||
### 10.10 应用入口与启动生命周期
|
||||
|
||||
当前:
|
||||
|
||||
- `backend/app/api/main.py`
|
||||
|
||||
目标:
|
||||
|
||||
- 保留 FastAPI app、middleware 和 lifespan 入口职责
|
||||
- 逐步去除对 legacy LLM factory 的直接依赖
|
||||
- 预热、清理和依赖装配应保持在明确的 wiring / bootstrap 边界内,而不是继续把旧 service factory 固化为应用入口依赖
|
||||
|
||||
### 10.11 Compliance 路由
|
||||
|
||||
当前:
|
||||
|
||||
- `backend/app/api/routes/compliance.py`
|
||||
|
||||
目标:
|
||||
|
||||
- 如继续保留该能力,应迁移到独立的 application service 与稳定端口
|
||||
- 在迁移完成前,该路由视为受控 legacy 入口,可修 bug,但不应继续扩展业务编排职责
|
||||
|
||||
### 10.12 遗留路由入口
|
||||
|
||||
当前:
|
||||
|
||||
- `backend/app/api/routes/docs.py`
|
||||
- `backend/app/api/routes/rag.py`
|
||||
|
||||
目标:
|
||||
|
||||
- 作为遗留或演示入口逐步归档、下线或迁移
|
||||
- 不再作为新增 backend 能力的开发入口
|
||||
|
||||
### 10.13 Legacy Workflow 与 Service 目录
|
||||
|
||||
当前:
|
||||
|
||||
- `backend/app/workflows/*`
|
||||
- `backend/app/services/*`
|
||||
|
||||
目标:
|
||||
|
||||
- 保留迁移期兼容价值,但不再承载新的长期业务编排
|
||||
- 若某个 legacy 实现仍被 `infrastructure` adapter 间接复用,应视为过渡依赖,后续逐步迁入 `infrastructure` 或更稳定的底层支撑模块
|
||||
- 任何新增 backend 业务能力,都不应再以这些目录作为默认落点
|
||||
|
||||
## 11. Technology Replacement Boundaries
|
||||
|
||||
### 11.1 本地解析 / MinerU -> 阿里云文档解析
|
||||
@@ -790,6 +876,10 @@ infrastructure -> external systems
|
||||
- 禁止新建第二个“大一统流程类”替代 `DocumentProcessor`
|
||||
- 禁止 `knowledge` 和 `agent` 各自维护独立检索实现
|
||||
- 禁止 parser、embedding、vector index、llm provider 的替换穿透到 API 层
|
||||
- 禁止新增 route 直接访问 `ConversationStore`
|
||||
- 禁止新增代码把 `backend/app/services/*` 或 `backend/app/workflows/*` 作为默认业务落点
|
||||
- 禁止新增 `infrastructure -> services/*` 的过渡依赖;已有依赖只允许在迁移窗口内逐步消除,不允许继续扩散
|
||||
- 禁止在 README、开发说明或评审结论中把 legacy 目录描述为当前 backend 的主结构
|
||||
|
||||
## 13. Architecture Review Checklist
|
||||
|
||||
@@ -807,3 +897,7 @@ infrastructure -> external systems
|
||||
10. 是否明确 `knowledge` 与 `agent` 共用同一 retrieval 底座。
|
||||
11. 是否明确 API 层只负责 transport concerns,不再直接承担业务编排。
|
||||
12. 是否保证后续替换方案时,上层 application service 与外部 API 契约不被迫变化。
|
||||
13. 是否仍存在 route 直接访问 `ConversationStore`、文件系统、对象存储或任务状态存储。
|
||||
14. 是否新增了 `infrastructure -> services/*` 依赖。
|
||||
15. 是否把新的 backend 业务逻辑写进了 `services/*` 或 `workflows/*`。
|
||||
16. README、backend README 与协作说明是否仍与当前 authoritative architecture 保持一致。
|
||||
|
||||
Reference in New Issue
Block a user