feat: 后端统一消息类型分类 + 更新 API 文档

msg_type 现在由后端在所有消息路径上自动填充,前端无需解析内容猜测类型。
- chat_handler: SendSystemMessage → system_info, parseMultiMessage 返回 proactiveSegment{msgType}
- protocol: MultiMessageItem 增加 MsgType 字段
- useWebSocket: 所有 handler 直接读取 msg_type,移除前端类型推断
- docs/api/gateway-api.md: 文档化 msg_type 分类机制,移除已删除的每日简报章节

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-05-26 12:35:13 +08:00
parent 6f4056eefb
commit 2574f60823
5 changed files with 41 additions and 76 deletions
+6 -7
View File
@@ -234,6 +234,7 @@ function handleServerMessage(msg: WSServerMessage) {
content: msg.content,
timestamp: msg.timestamp,
isStreaming: true,
msgType: 'chat',
});
setTyping(false);
} else {
@@ -248,11 +249,8 @@ function handleServerMessage(msg: WSServerMessage) {
case 'history_response':
// 防御性检查:仅当当前消息为空时才加载 WebSocket 历史响应
// 避免 WebSocket 的 history_response (可能来自后端空缓存) 覆盖 HTTP loadMessagesFromServer 已加载的消息
// 注意:空数组 [] 在 JS 中是 truthy,必须显式检查 length > 0
if (msg.messages && msg.messages.length > 0) {
const sessionState = useSessionStore.getState();
// 如果 sessionStore 或 chatStore 中已有消息,说明 HTTP 已加载完成,忽略 WS 的历史响应
if (sessionState.messages.length > 0 || chatState.messages.length > 0) {
console.log(
'[WS] 忽略 history_response:消息已由 HTTP 加载',
@@ -264,8 +262,7 @@ function handleServerMessage(msg: WSServerMessage) {
const msgsWithIds: Message[] = msg.messages.map((m, i) => ({
...m,
id: m.id || `hist_${i}_${Date.now()}`,
// 规范化 msg_type → msgType 以保持与 HTTP 加载路径一致
msgType: (m as any).msg_type || m.msgType,
msgType: m.msgType || 'chat',
}));
setMessages(msgsWithIds);
useChatStore.getState().setMessages(msgsWithIds);
@@ -294,15 +291,17 @@ function handleServerMessage(msg: WSServerMessage) {
break;
case 'multi_message':
// 多段消息 — 仅在后端未发送 response 时作为兜底
// 多段消息 — 后端已分类 msg_type,前端直接使用
if (msg.multi_messages && msg.multi_messages.length > 0) {
for (const item of msg.multi_messages) {
const itemMsgType = (item.msg_type as MessageDisplayType) || 'chat';
addMessage({
id: `multi_${Date.now()}_${item.index}`,
role: 'assistant',
role: itemMsgType === 'action' ? 'action' : 'assistant',
content: item.content,
timestamp: msg.timestamp || Date.now(),
isStreaming: false,
msgType: itemMsgType,
});
}
}
+1
View File
@@ -91,6 +91,7 @@ export interface IoTDeviceUpdate {
export interface MultiMessageItem {
index: number;
content: string;
msg_type?: string; // chat | action | system_info (由后端分类)
}
/** 流式片段 (子会话架构 stream_segments 类型) */