典籍知识问答模块AI问答bug修改
一、修改流式数据处理问题
1.问题描述:由于传来的数据形式如下:
event:START
data:350
data:<
data:t
data:h
data:i
data:n
data:k
data:>
data:
data:
data:
data:
data:嗯
data:,
导致需要修改获取正常的当前信息id并更新的逻辑
2.修改代码如下:
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) {
if (line.startsWith('event:')) {
currentEvent = line.replace('event:', '').trim();
continue;
}
if (line.startsWith('data:')) {
const data = line.replace('data:', '').trim();
// 处理 START 事件
if (currentEvent === 'START') {
serverAIId = data;
currentMessages.value.push({
id: serverAIId,
role: 'assistant',
content: '',
createdAt: new Date().toISOString(),
streaming: true
});
currentEvent = null; // 重置事件状态
//console.log('START event received, serverAIId:', serverAIId);
continue;
}
// 处理普通内容(MESSAGE 或未指定 event)
if (currentEvent === null) {
aiContent += data;
currentMessages.value = currentMessages.value.map(msg =>
msg.id === serverAIId ? { ...msg, content: aiContent } : msg
);
}
// 处理 COMPLETE 事件(结束)
if (currentEvent === 'COMPLETE') {
currentMessages.value = currentMessages.value.map(msg =>
msg.id === serverAIId ? { ...msg, streaming: false } : msg
);
}
}
}
}
currentMessages.value = currentMessages.value.map(msg =>
msg.id === serverAIId ? { ...msg, streaming: false } : msg
);
await fetchSessions(classic.value.id);
二、重新编辑问题时出现的问题
1.问题描述:由于发送信息后的userId也没能及时更新到前端,导致在使用修改问题获取msgid时无法获取原始id的信息.
2.解决方法:后端返回时加上问题的id信息,具体返回如下:
event:USER_MSG
data:347
event:START
data:348
data:<
data:t
因此就可以正常更新问题id了