4.1 KiB
4.1 KiB
重roll功能调试指南
问题描述
重roll时整个前端页面被流式输出内容取代,而不是只在目标消息中更新。
可能的原因
- 消息ID匹配失败:
newMessageId与实际消息的 ID 不匹配 - 状态更新错误:使用了
set({ messages: [...] })而不是set((state) => ({ messages: ... })) - 字段丢失:更新时没有使用
...msg保留原有字段
已实施的修复
1. 确保 nextFloor 变量定义
let userFloor, assistantFloor, nextFloor; // ✅ 在外部声明
if (isReroll) {
assistantFloor = targetFloor;
nextFloor = targetFloor; // ✅ 设置 nextFloor
} else {
nextFloor = get().getNextFloor(messages);
// ...
}
2. 添加详细的调试日志
console.log('[ChatBoxStore] 🔄 重roll模式,更新消息:', {
id: newMessageId,
floor: assistantFloor,
currentMes: targetMessage.mes.substring(0, 50) + '...',
hasSwipes: !!targetMessage.swipes,
swipesCount: targetMessage.swipes?.length || 0,
swipeId: targetMessage.swipe_id
});
3. 验证消息匹配
set((state) => {
const targetMsg = state.messages.find(msg => msg.id === newMessageId);
if (!targetMsg && chunkCount === 1) {
console.error('[ChatBoxStore] ❌ 找不到目标消息!', {
newMessageId,
availableIds: state.messages.map(m => ({ id: m.id, floor: m.floor }))
});
}
return {
messages: state.messages.map((msg) => {
if (msg.id === newMessageId) {
return {
...msg, // ✅ 保留所有原有字段
mes: assistantMessage
};
}
return msg;
})
};
});
测试步骤
1. 打开浏览器控制台
按 F12 打开开发者工具,切换到 Console 标签。
2. 触发重roll
在任意 AI 消息上右键点击,选择"🔄 重roll"。
3. 观察日志输出
应该看到以下日志:
[ChatBoxStore] 🔄 重roll模式,更新消息: {
id: "ai_1234567890_abc123",
floor: 2,
currentMes: "这是AI的回复内容...",
hasSwipes: true,
swipesCount: 2,
swipeId: 1
}
[WebSocket] 📡 连接已建立
[WebSocket] 📤 发送消息:
- Floor: 2
- Mode: 🔄 Reroll
- ...
[WebSocket] 📊 已接收 10 个 chunks
[WebSocket] 📊 已接收 20 个 chunks
...
[ChatBoxStore] 🔄 重roll完成,添加新的 swipe 版本
[ChatBoxStore] 📊 Swipes 更新: {
oldCount: 2,
newCount: 3,
newSwipeIndex: 2
}
4. 检查是否有错误
如果看到以下错误,说明消息ID匹配失败:
[ChatBoxStore] ❌ 找不到目标消息! {
newMessageId: "ai_1234567890_abc123",
availableIds: [
{ id: "ai_1111111111_xyz", floor: 1 },
{ id: "ai_2222222222_def", floor: 2 }
]
}
这意味着:newMessageId 与消息列表中的任何 ID 都不匹配!
5. 验证页面表现
✅ 正确的表现:
- 只有目标 AI 消息的内容在流式更新
- 其他消息保持不变
- 用户消息、其他 AI 消息不受影响
- 侧边栏、顶部栏等UI组件正常显示
❌ 错误的表现(修复前):
- 整个页面被流式文本覆盖
- 其他消息消失或被替换
- UI 组件异常
常见问题排查
Q1: 如果看到"找不到目标消息"错误
原因:消息ID不匹配
解决:
- 检查
targetMessage.id是否正确获取 - 确认消息列表中确实存在该 ID
- 检查是否有其他地方修改了消息ID
Q2: 如果页面仍然被覆盖
原因:可能是 React 渲染问题
解决:
- 检查 ChatBox.jsx 的 renderMessage 函数
- 确认使用了正确的 key(应该是
message.id) - 检查是否有条件渲染导致其他消息被隐藏
Q3: Swipe 按钮消失
原因:swipes 数组丢失
解决:
- 确认更新时使用了
...msg保留所有字段 - 检查 complete 事件中是否正确更新了 swipes 数组
- 验证
msg.swipes在更新后仍然存在
预期结果
修复后,重roll应该:
- ✅ 只更新目标 AI 消息
- ✅ 保留所有其他消息
- ✅ 保留 swipes 数组和 swipe_id
- ✅ 流式显示新生成的内容
- ✅ 完成后自动切换到新版本
- ✅ 不影响页面其他部分