典籍知识问答重新生成和消息修改Bug修改
1.Bug:
{messageId: "temp-user-1747051925999",…} messageId : "temp-user-1747051925999" newContent : "关于选中的内容:\"The ancient people lived long by following natural laws\",我的问题是:翻译成法文是什么意思\n" 负载是这个
{ "timestamp": "2025-05-12T12:17:39.268+00:00", "status": 400, "error": "Bad Request", "path": "/api/qa/stream-edit" } 响应是这个
2.错误原因
用户消息的ID在创建时是临时的,但在流式响应中未正确更新为持久化ID。
3.修改代码
// 修改流式响应处理部分
while (true) {
const { done, value } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
const lines = buffer.split('\n');
buffer = lines.pop() || '';
for (const line of lines) {
// 捕获持久化ID事件
if (line.startsWith('event: START')) {
const persistedId = line.replace('event: START\ndata:', '').trim();
// 更新临时消息ID
const tempMsg = currentMessages.value.find(m => m.id === aiMsg.id);
if (tempMsg) {
tempMsg.id = persistedId; // 更新为真实ID
tempMsg.temp = false; // 移除临时标记
currentMessages.value = [...currentMessages.value]; // 触发响应式更新
}
}
// 原有内容
}
}
// 修改编辑消息方法
const saveEdit = async (msgId) => {
if (isSavingEdit.value) return;
// 新增ID有效性检查
if (msgId.startsWith('temp-')) {
alert('请等待消息生成完成后再操作');
return;
}
isSavingEdit.value = true;
try {
await qastore.editMessage(msgId, editingStates.value[msgId].editedContent);
editingStates.value[msgId].editing = false;
} finally {
isSavingEdit.value = false;
}
}
//原有内容