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

相关推荐
怕浪猫几秒前
React从入门到出门 第五章 React Router 配置与原理初探
前端·javascript·react.js
jinmo_C++1 分钟前
从零开始学前端 · HTML 基础篇(一):认识 HTML 与页面结构
前端·html·状态模式
鹏多多7 分钟前
前端2025年终总结:借着AI做大做强再创辉煌
前端·javascript
Coder_Boy_8 分钟前
Spring Boot 事务回滚异常 UnexpectedRollbackException 详解(常见问题集合)
java·spring boot·后端
风象南9 分钟前
SpringBoot 实现网络限速
后端
哈__10 分钟前
React Native 鸿蒙跨平台开发:Vibration 实现鸿蒙端设备的震动反馈
javascript·react native·react.js
WebGISer_白茶乌龙桃13 分钟前
Cesium实现“悬浮岛”式,三维立体的行政区划
javascript·vue.js·3d·web3·html5·webgl
源代码•宸14 分钟前
Golang语法进阶(定时器)
开发语言·经验分享·后端·算法·golang·timer·ticker
小Tomkk16 分钟前
⭐️ StarRocks Web 使用介绍与实战指南
前端·ffmpeg
计算机学姐18 分钟前
基于SpringBoot的汽车租赁系统【个性化推荐算法+数据可视化统计】
java·vue.js·spring boot·后端·spring·汽车·推荐算法