典籍知识问答模块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了

相关推荐
Hilaku7 分钟前
为什么死磕 1px 的团队,用户体验反而更差?
前端·javascript·程序员
lichenyang45318 分钟前
鸿蒙元服务跳转 H5 太慢?从 Web 内核预热、网络预连接到骨架屏的完整优化方案
前端
aa小小27 分钟前
大屏的数据应该怎么做“实时更新“?大屏页面如果一直开着不关(比如挂了几天几夜),你觉得可能会出现什么问题,需要怎么预防?
前端·数据可视化
花间相见35 分钟前
【计算基础|网络04】—— HTTP接口实战(下):接口测试、鉴权与跨域排错
java·linux·人工智能·后端·python·计算机网络·postman
陈拉斯35 分钟前
给公司年会写了个大屏抽奖系统:动画在前端跑,凭什么说结果没被改?
javascript
BryceBorder36 分钟前
Agent Memory 不只是聊天记录:手搓三大记忆系统
后端·agent·面经·codex·claude code·agent memory
一条泥憨鱼44 分钟前
苍穹外卖【day11| 用户统计,订单统计,销量排名统计功能实现】
java·后端·苍穹外卖
她的男孩1 小时前
缓存明明命中了却报 ClassCastException:拆完多级缓存控制面,我挖出 5 个静默失效的坑
java·后端·架构