M7-08 P0 | open-or-create 会话逻辑(同一 scope 继续/新建) #76

Closed
opened 2026-06-06 16:21:28 +08:00 by wangdl · 1 comment
Owner

目标

新增接口或前端逻辑:同一 scopeType + scopeId 查到最近会话就打开,没有就创建。

方案(后端实现)

POST /rag-chat/sessions/open-or-create

请求 body 同 createSession。后端逻辑:

  1. 查 scopeType + scopeId + userId 最近会话
  2. 有 → 返回已有 session(isNew: false)
  3. 没有 → 创建新 session(isNew: true)

方案(前端实现)

  1. listSessions(scopeType, scopeId)
  2. 有 → openSession(first.id)
  3. 没有 → createSession(scope)
## 目标 新增接口或前端逻辑:同一 scopeType + scopeId 查到最近会话就打开,没有就创建。 ## 方案(后端实现) ```http POST /rag-chat/sessions/open-or-create ``` 请求 body 同 createSession。后端逻辑: 1. 查 scopeType + scopeId + userId 最近会话 2. 有 → 返回已有 session(isNew: false) 3. 没有 → 创建新 session(isNew: true) ## 方案(前端实现) 1. listSessions(scopeType, scopeId) 2. 有 → openSession(first.id) 3. 没有 → createSession(scope)
wangdl added this to the M7:ChatScope 会话系统 — 学习对象绑定的上下文会话 milestone 2026-06-06 16:21:28 +08:00
wangdl changed title from M7-08 P1 | open-or-create 会话逻辑 to M7-08 P0 | open-or-create 会话逻辑(同一 scope 继续/新建) 2026-06-06 16:27:24 +08:00
Author
Owner

完成情况

交付物

Service 层createSession 加入 open-or-create 逻辑:

async createSession(userId: string, params: CreateSessionParams): Promise<CreateSessionResult> {
  // 1. 校验 scopeType 合法性
  // 2. 校验 non-global scope 必须有 scopeId
  // 3. 对于非 global scope,先查找已有活跃会话
  // 4. 已存在 → 返回 { session, isNew: false }
  // 5. 不存在 → 创建新会话 → 返回 { session, isNew: true }
}

匹配规则

SELECT * FROM "ChatSession"
WHERE "userId" = ?
  AND "scopeType" = ?
  AND "scopeId" = ?
  AND "isDeleted" = false
ORDER BY "updatedAt" DESC
LIMIT 1;

关键行为

场景 行为
scope=material/MA 首次调用 创建新会话 (201)
scope=material/MA 再次调用 返回已有会话 (200)
scope=global 每次调用 始终创建新会话 (201)
已有会话 isDeleted=true 被忽略,创建新会话 (201)

Controller 层

  • @Res({ passthrough: true }) 动态设置状态码
  • 已有会话 → HTTP 200
  • 新建会话 → HTTP 201
  • 响应 body 为 ChatSession 对象

涉及文件

文件 变更
src/modules/rag-chat/rag-chat.service.ts createSession 改为 open-or-create + CreateSessionResult
src/modules/rag-chat/rag-chat.controller.ts createSession 使用 @Res passthrough 设置 200/201
## 完成情况 ### 交付物 **Service 层** — `createSession` 加入 open-or-create 逻辑: ```typescript async createSession(userId: string, params: CreateSessionParams): Promise<CreateSessionResult> { // 1. 校验 scopeType 合法性 // 2. 校验 non-global scope 必须有 scopeId // 3. 对于非 global scope,先查找已有活跃会话 // 4. 已存在 → 返回 { session, isNew: false } // 5. 不存在 → 创建新会话 → 返回 { session, isNew: true } } ``` ### 匹配规则 ```sql SELECT * FROM "ChatSession" WHERE "userId" = ? AND "scopeType" = ? AND "scopeId" = ? AND "isDeleted" = false ORDER BY "updatedAt" DESC LIMIT 1; ``` ### 关键行为 | 场景 | 行为 | |------|------| | `scope=material/MA` 首次调用 | 创建新会话 (201) | | `scope=material/MA` 再次调用 | 返回已有会话 (200) | | `scope=global` 每次调用 | 始终创建新会话 (201) | | 已有会话 isDeleted=true | 被忽略,创建新会话 (201) | ### Controller 层 - `@Res({ passthrough: true })` 动态设置状态码 - 已有会话 → HTTP 200 - 新建会话 → HTTP 201 - 响应 body 为 ChatSession 对象 ### 涉及文件 | 文件 | 变更 | |------|------| | src/modules/rag-chat/rag-chat.service.ts | createSession 改为 open-or-create + CreateSessionResult | | src/modules/rag-chat/rag-chat.controller.ts | createSession 使用 @Res passthrough 设置 200/201 |
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#76
No description provided.