实现了知识库管理的应用层服务,包括命令定义、命令处理器和数据传输对象(DTOs)。遵循 CQRS 模式和依赖注入原则。
定义了 7 个命令类,用于表示知识库管理的各种操作:
CreateKnowledgeBaseCommand: 创建知识库
UpdateKnowledgeBaseCommand: 更新知识库
DeleteKnowledgeBaseCommand: 删除知识库
AddDocumentToKnowledgeBaseCommand: 添加文档到知识库
RemoveDocumentFromKnowledgeBaseCommand: 从知识库移除文档
AddTagToKnowledgeBaseCommand: 添加标签到知识库
RemoveTagFromKnowledgeBaseCommand: 从知识库移除标签
特点:
__post_init__ 中进行参数验证has_name_update(), has_tags_update())定义了 2 个数据传输对象:
KnowledgeBaseDTO: 知识库数据传输对象
from_entity(): 从领域实体创建 DTOto_entity(): 转换为领域实体to_dict(): 转换为字典(JSON 序列化)from_dict(): 从字典创建 DTO(JSON 反序列化)has_documents(), has_tags(), has_document(), has_tag(): 辅助查询方法PromptDimensionDTO: 提示词维度数据传输对象
has_variables(), has_variable()特点:
实现了 7 个命令处理器,对应 7 个命令:
CreateKnowledgeBaseHandler: 处理创建知识库命令
UpdateKnowledgeBaseHandler: 处理更新知识库命令
DeleteKnowledgeBaseHandler: 处理删除知识库命令
AddDocumentToKnowledgeBaseHandler: 处理添加文档命令
add_document() 方法RemoveDocumentFromKnowledgeBaseHandler: 处理移除文档命令
remove_document() 方法AddTagToKnowledgeBaseHandler: 处理添加标签命令
add_tag() 方法RemoveTagFromKnowledgeBaseHandler: 处理移除标签命令
remove_tag() 方法处理器特点:
DomainException → ValidationExceptionResourceNotFoundExceptionApplicationException导出所有公共接口:
详细的模块文档,包括:
Depends 机制领域层 (src/domain/knowledge_base/):
KnowledgeBase 和 PromptDimension 实体KnowledgeBaseRepository 接口EntityId 和 Timestamp 值对象DomainException 异常应用层共享 (src/application/shared/):
ApplicationException, ResourceNotFoundException, ValidationExceptionRequirement 1.4: 应用层协调领域对象完成用例
Requirement 8.4: 知识库相关代码组织到独立模块
src/application/knowledge_base/ 目录Requirement 3.2: 使用依赖注入机制管理组件依赖关系
Requirement 6.3: 使用结构化日志记录
实现与现有的 vector_search 和 document_parsing 应用服务保持一致:
__post_init__ 中验证from_entity(), to_entity(), to_dict(), from_dict() 方法单元测试 (Task 5.11):
集成测试:
表现层集成:
查询处理器 (可选):
成功实现了知识库应用服务的完整功能,包括:
实现遵循了 CQRS 模式、依赖注入原则和分层架构设计,为后续的表现层实现和测试奠定了坚实的基础。