M7-14 P1 | sendMessageStream aiGateway 非空断言导致 null 时 crash #102

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

发现位置

rag-chat.service.ts L233 sendMessageStream 方法。

问题

for await (const chunk of this.aiGateway!.generateStream({
//                                ^ 非空断言

aiGateway@Optional() 注入,可能为 null。使用 ! 非空断言会导致运行时 crash。

对比同步方法 sendMessage (L151-177) 有正确的 fallback:

if (this.aiGateway && context.text) {
  // ...
} else {
  reply = this.fallbackReply(context.isEmpty);
}

sendMessageStreamcontext.text 为空时做了 fallback,但在 aiGateway 为 null 时没有保护。

修复方案

context.text 非空且 this.aiGateway 存在时才调用 generateStream,否则 yield fallback:

if (!this.aiGateway || !context.text) {
  yield { type: 'content', content: this.fallbackReply(!context.text) };
} else {
  for await (...) { ... }
}

涉及文件

文件 变更
src/modules/rag-chat/rag-chat.service.ts sendMessageStream aiGateway null check
## 发现位置 `rag-chat.service.ts` L233 `sendMessageStream` 方法。 ### 问题 ```typescript for await (const chunk of this.aiGateway!.generateStream({ // ^ 非空断言 ``` `aiGateway` 是 `@Optional()` 注入,可能为 `null`。使用 `!` 非空断言会导致运行时 crash。 对比同步方法 `sendMessage` (L151-177) 有正确的 fallback: ```typescript if (this.aiGateway && context.text) { // ... } else { reply = this.fallbackReply(context.isEmpty); } ``` 但 `sendMessageStream` 在 `context.text` 为空时做了 fallback,但在 `aiGateway` 为 null 时没有保护。 ### 修复方案 在 `context.text` 非空且 `this.aiGateway` 存在时才调用 `generateStream`,否则 yield fallback: ```typescript if (!this.aiGateway || !context.text) { yield { type: 'content', content: this.fallbackReply(!context.text) }; } else { for await (...) { ... } } ``` ## 涉及文件 | 文件 | 变更 | |------|------| | src/modules/rag-chat/rag-chat.service.ts | sendMessageStream aiGateway null check |
wangdl added this to the M7:ChatScope 会话系统 — 学习对象绑定的上下文会话 milestone 2026-06-06 17:12:46 +08:00
Author
Owner

修复

  • sendMessageStream 增加 aiGateway null 检查
  • this.aiGateway!.generateStream → this.aiGateway.generateStream(安全访问)
  • aiGateway 为 null 或无上下文 → yield fallback 回复

涉及: rag-chat.service.ts

## 修复 - sendMessageStream 增加 aiGateway null 检查 - this.aiGateway!.generateStream → this.aiGateway.generateStream(安全访问) - aiGateway 为 null 或无上下文 → yield fallback 回复 涉及: 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#102
No description provided.