Skip to content

知识图谱服务 Architecture

定位

知识图谱服务 KnowledgeGraphService 位于 src/services/storage/KnowledgeGraphService.js, 提供实体/关系的 CRUD、版本历史、子图/路径查询与作用域共享。 HTTP 出口见知识图谱接口,模型侧工具见 内置工具知识图谱类别,抽取侧由 KnowledgeGraphExtractor(同目录)完成。

数据模型

SQLite 表(DatabaseService 初始化):

用途
kg_entities实体(entity_identity_typenamescope_idpropertiesversion
kg_relationships关系(relationship_idfrom_entity_idto_entity_idrelation_typescope_idpropertiesversion
kg_entity_history实体历史版本(回滚依据)
kg_relationship_history关系历史版本
kg_scope_sharing作用域共享(全局/继承)

实体读回字段为 entityType(创建请求用 type)。实体/关系标识符为 <scopeId>:entity:<uuid> 形式,作用域支持 globaluser:<id>group:<id>group:<gid>:user:<uid>

服务能力

方法组说明
实体createEntity / getEntity / listEntities / searchEntities / countEntities / updateEntity / deleteEntity
关系createRelationship / getRelationship / updateRelationship / deleteRelationship / getEntityRelationships
版本getEntityHistory / hasEntityVersion / rollbackEntity;关系侧同名方法
图查询querySubgraph(深度/节点/边上限)、pathQuery(最短路径、relationTypes 过滤)、getKnowledgeContext(用户上下文)
作用域listScopes / getScopeStats
导入导出exportGraph / importGraph(校验 schemaVersion / truncated / counts 一致性)、iterateScopeEntities / iterateScopeRelationships(流式导出用)

关键语义

  • 同名合并:同一作用域内同 entity_type + name 保存时合并更新,不产生重复实体。
  • 删除保留历史:删除实体会先清理其关系,再标记不存在;历史版本保留, 通过 POST /api/graph/entities/:id/rollback 恢复为新的当前版本(版本号递增)。
  • 回滚实现:目标快照作为一次新变更写回,而不是把版本号回退。
  • 领域错误:服务层抛 KnowledgeGraphError(message, statusCode), 路由按 statusCode 映射 HTTP 状态(400/404),未知异常统一 500 且不泄露内部细节。
  • 可视化摘要getVisualization(scopeId, { limit, focusEntityId }) 返回带 totalEntities / totalRelationships / truncated 的限幅图谱。

抽取侧

KnowledgeGraphExtractor 使用 LLM 从对话中抽取结构化知识,实体类型白名单 person / thing / place / concept / event,与模型侧 kg_* 工具 (src/mcp/tools/knowledgeGraph.js,12 个工具)及 /api/graph 共享同一服务。

基于 MIT 许可发布