feat: Gateway 消息排队机制 — 同会话串行化处理

同一 session 的消息按顺序处理:当前回复未完成时新消息进入队列,
完成后自动消费下一条。避免并发请求导致上下文竞争和响应交错。
客户端收到 type:"queued" 时可显示排队状态。

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-05-29 21:06:59 +08:00
parent 61284c9c6a
commit 22d7b91cb1
2 changed files with 86 additions and 2 deletions
+22
View File
@@ -256,6 +256,7 @@ ws://<gateway>/ws/chat?token=<jwt>&session_id=<optional>&client_id=<optional>&de
| `pong` | 响应 ping |
| `device_update` | IoT 设备状态更新 |
| `background_thinking` | 后台思考状态变更 |
| `queued` | 消息已加入处理队列(会话繁忙时) |
> **广播机制**:服务端推送分为两类:
> - **用户消息回显**`type: "response"`, `role: "user"`):通过 `SendToUserExcept` 广播,排除发送者自身(发送者本地已渲染),仅同步到同用户的其他设备。
@@ -301,6 +302,27 @@ Client Gateway AI-Core
|<-- {type:"stream_end"} | (含 full text) |
```
### 消息排队机制
同一会话的消息处理为串行化队列。若上一轮 AI 回复尚未完成时用户发送新消息,新消息会加入等待队列,并在当前处理完成后自动消费。
```
Client Gateway
| |
|-- msg1 (type:"message") --> |
|<-- {type:"stream_start"} | → AI-Core 处理中...
| |
|-- msg2 (type:"message") --> |
|<-- {type:"queued"} | → 加入队列等待
| |
|<-- {type:"stream_end"} (msg1) | → msg1 完成
|<-- {type:"stream_start"} (msg2) | → 自动取出 msg2 处理
|<-- ... |
|<-- {type:"stream_end"} (msg2) |
```
> `queued` 消息表明用户消息已接收但尚未开始处理,客户端可据此显示"排队中"状态。
---
### 语音输入流程