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

相关推荐
架构技术专栏1 小时前
Agent 工具调用:从工具声明到执行反馈
前端·人工智能·后端
别动我齐刘海2 小时前
C++ 工程化学习
java·linux·前端·c++·学习·机器学习·机器人
码事漫谈2 小时前
你让AI干的活,它可能只干了十分之一——而你根本看不出来
后端
Wx-bishekaifayuan2 小时前
express皖美特色农产品网售系统53118-计算机课程设计、毕业设计
人工智能·spring boot·后端·python·课程设计·express·旅游
志尊宝3 小时前
Vue3 零基础每日笔记(071):暗黑模式与主题切换——CSS 变量 + Element Plus 暗黑方案
前端·css·笔记·vue·html·前端开发·软件开发
Wx-bishekaifayuan3 小时前
springboot汉中旅游服务平台41442-计算机课程设计、毕业设计
spring boot·后端·python·django·课程设计·express·旅游
WeiXin_DZbishe3 小时前
基于springboot城市公交实时运营与调度系统-计算机毕设 附源码【课程设计】66173
java·vue.js·spring boot·后端·python·php·课程设计
刘发财3 小时前
前端如何生成10000页PDF文件?
前端·javascript·开源
WeiXin_DZbishe3 小时前
基于Springboot+AI的kuall音乐广场的设计与实现-计算机毕设 附源码【课程设计】12715
vue.js·人工智能·spring boot·后端·python·php·课程设计
A接拉起0074 小时前
2026 年 JavaScript 开发者需要了解的知识
前端·javascript·后端