fix: 修改流处理

This commit is contained in:
shaohuzhang1 2023-12-15 17:43:49 +08:00
parent 2c9ef25754
commit d3409d9845

View File

@ -263,7 +263,7 @@ function chatMessage() {
vote_status: '-1' vote_status: '-1'
}) })
inputValue.value = '' inputValue.value = ''
applicationApi.postChatMessage(chartOpenId.value, problem_text).then(async (response) => { applicationApi.postChatMessage(chartOpenId.value, problem_text).then((response) => {
const row = chatList.value.find((item) => item.id === id) const row = chatList.value.find((item) => item.id === id)
if (row) { if (row) {
@ -271,27 +271,33 @@ function chatMessage() {
ChatManagement.write(id) ChatManagement.write(id)
const reader = response.body.getReader() const reader = response.body.getReader()
/*eslint no-constant-condition: ["error", { "checkLoops": false }]*/ /*eslint no-constant-condition: ["error", { "checkLoops": false }]*/
while (true) { const write = ({ done, value }: { done: boolean; value: any }) => {
const { done, value } = await reader.read()
if (done) {
ChatManagement.close(id)
break
}
try { try {
if (done) {
ChatManagement.close(id)
return
}
const decoder = new TextDecoder('utf-8') const decoder = new TextDecoder('utf-8')
const str = decoder.decode(value, { stream: true }) const str = decoder.decode(value, { stream: true })
if (str && str.startsWith('data:')) { if (str && str.startsWith('data:')) {
row.record_id = JSON?.parse(str.replace('data:', '')).id const split = str.match(/data:.*}\n\n/g)
const content = JSON?.parse(str.replace('data:', ''))?.content if (split) {
if (content) { split.forEach((item_str) => {
ChatManagement.append(id, content) row.record_id = JSON?.parse(item_str.replace('data:', '')).id
const content = JSON?.parse(item_str.replace('data:', ''))?.content
if (content) {
ChatManagement.append(id, content)
}
})
} }
} }
} catch (e) { } catch (e) {
console.log(e)
// console // console
} }
return reader.read().then(write)
} }
reader.read().then(write)
} }
}) })
} }