M7-13 P0 | loadContextByScope: global 走全库检索 + folder 无独立分支 #101

Closed
opened 2026-06-06 17:12:45 +08:00 by wangdl · 1 comment
Owner

发现位置

rag-chat.service.ts L333-381 loadContextByScope 方法。

问题 1: global scope 错误检索知识库

switch (scopeType) {
  case 'material': ...
  case 'knowledge_item': ...
  case 'knowledge_base':
  default:  // ← global 和 folder 都走这里!
    items = await this.prisma.knowledgeItem.findMany({
      where: { knowledgeBaseId: kbId, deletedAt: null },
    });
}

global scope 的正确行为是不检索任何知识库内容(纯模型回答),但现在走了 default 分支,如果 kbId 不为 null,会错误检索整个知识库。

问题 2: folder scope 无独立检索逻辑

folder scope 在 VALID_SCOPE_TYPES 中已定义,但 loadContextByScope 没有 case 'folder'。走了 default 分支后用了全库检索,而非只检索该 folder 及其子节点的内容。

修复方案

  1. 增加 case 'global': → 返回空上下文 { text: '', citations: [], isEmpty: true }
  2. 增加 case 'folder': → 先查 folder 的 KnowledgeItem(itemType=folder),获取其下所有子 item,再按 item IDs 检索
    • 一期简化实现:查询 parentId = scopeId 的所有 item
  3. default 分支改为 fallback 到 knowledge_base 行为 + warn 日志

涉及文件

文件 变更
src/modules/rag-chat/rag-chat.service.ts loadContextByScope 增加 global + folder case
## 发现位置 `rag-chat.service.ts` L333-381 `loadContextByScope` 方法。 ### 问题 1: global scope 错误检索知识库 ```typescript switch (scopeType) { case 'material': ... case 'knowledge_item': ... case 'knowledge_base': default: // ← global 和 folder 都走这里! items = await this.prisma.knowledgeItem.findMany({ where: { knowledgeBaseId: kbId, deletedAt: null }, }); } ``` `global` scope 的正确行为是**不检索任何知识库内容**(纯模型回答),但现在走了 `default` 分支,如果 kbId 不为 null,会错误检索整个知识库。 ### 问题 2: folder scope 无独立检索逻辑 `folder` scope 在 `VALID_SCOPE_TYPES` 中已定义,但 `loadContextByScope` 没有 `case 'folder'`。走了 `default` 分支后用了全库检索,而非只检索该 folder 及其子节点的内容。 ## 修复方案 1. 增加 `case 'global':` → 返回空上下文 `{ text: '', citations: [], isEmpty: true }` 2. 增加 `case 'folder':` → 先查 folder 的 `KnowledgeItem`(itemType=folder),获取其下所有子 item,再按 item IDs 检索 - 一期简化实现:查询 `parentId = scopeId` 的所有 item 3. `default` 分支改为 fallback 到 knowledge_base 行为 + warn 日志 ## 涉及文件 | 文件 | 变更 | |------|------| | src/modules/rag-chat/rag-chat.service.ts | loadContextByScope 增加 global + folder case |
wangdl added this to the M7:ChatScope 会话系统 — 学习对象绑定的上下文会话 milestone 2026-06-06 17:12:45 +08:00
Author
Owner

修复

  • case global → 直接返回空上下文
  • case folder → WHERE parentId = scopeId
  • default → warn 日志 + 回退到 knowledge_base

涉及: rag-chat.service.ts

## 修复 - case global → 直接返回空上下文 - case folder → WHERE parentId = scopeId - default → warn 日志 + 回退到 knowledge_base 涉及: rag-chat.service.ts
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: wangdl/api-server#101
No description provided.