fix: 修复打字机吞吐异常

This commit is contained in:
huangzhe
2025-07-31 15:35:27 +08:00
parent e01ce128db
commit c60b454999
3 changed files with 30 additions and 36 deletions

View File

@@ -92,6 +92,7 @@ export default {
}, },
data() { data() {
return { return {
requestIndex: 1,
requestSingle: undefined, requestSingle: undefined,
// 管控单次请求,当 abort 之后, while 后面的会进行重复请求 // 管控单次请求,当 abort 之后, while 后面的会进行重复请求
single: false, single: false,
@@ -104,6 +105,8 @@ export default {
isVoiceMode: false, isVoiceMode: false,
answerMap: '', answerMap: '',
currentMessage: null, currentMessage: null,
// 随后生成的消息
genMessage: null,
messageInfo: { messageInfo: {
is_complete: false.toString(), is_complete: false.toString(),
information: '', information: '',
@@ -163,19 +166,6 @@ export default {
console.error(err) console.error(err)
} }
}, },
// hasTreasureBox() {
// chatProduct({ query: this.newMessage })
// .then((res) => {
// if (res) {
// this.messageStatus = 'stop'
// this.messages.push({ type: 'box', text: this.newMessage, detail: res.content })
// this.newMessage = ''
// }
// })
// .catch(() => {
// this.messageStatus = 'stop'
// })
// },
stopRecording() { stopRecording() {
if (this.mediaRecorder && this.isRecording) { if (this.mediaRecorder && this.isRecording) {
this.mediaRecorder.stop() this.mediaRecorder.stop()
@@ -230,7 +220,6 @@ export default {
message: JSON.stringify(this.messageInfo), message: JSON.stringify(this.messageInfo),
user: "gwcs-test", user: "gwcs-test",
inputs: {}, inputs: {},
// action: this.action
} }
this.currentMessage = { this.currentMessage = {
...this.messageInfo, ...this.messageInfo,
@@ -242,10 +231,9 @@ export default {
isLike: false, isLike: false,
isDisLike: false, isDisLike: false,
} }
if (this.single) { if (this.single) {
this.$set(this.messages, this.messages.length - 1, this.currentMessage) // this.$set(this.messages, this.messages.length - 1, { ...this.currentMessage })
this.$set(this.messages, this.messages.length, this.genMessage = { ...this.currentMessage })
} else { } else {
this.messages.push(this.currentMessage) this.messages.push(this.currentMessage)
} }
@@ -255,9 +243,6 @@ export default {
params[k] = this.chatData[k] params[k] = this.chatData[k]
} }
} }
// if (this.$route.query.compareId) {
// params.compareResult = JSON.parse(sessionStorage.getItem('results'))
// }
this.newMessage = '' this.newMessage = ''
fetch(gwcsChat(), { fetch(gwcsChat(), {
method: 'POST', method: 'POST',
@@ -267,7 +252,7 @@ export default {
timeout: 60000, timeout: 60000,
}) })
.then(async (res) => { .then(async (res) => {
await this.processStreamResponse(res) await this.processStreamResponse(res, this.requestIndex)
this.single = false this.single = false
}) })
.catch((err) => { .catch((err) => {
@@ -275,7 +260,7 @@ export default {
this.$emit('update:messageStatus', 'stop') this.$emit('update:messageStatus', 'stop')
}) })
}, },
async processStreamResponse(response) { async processStreamResponse(response, requestIndex) {
if (!response.ok) throw new Error(`HTTP错误: ${response.status}`) if (!response.ok) throw new Error(`HTTP错误: ${response.status}`)
if (!response.body) { if (!response.body) {
console.error('响应体不存在:', response) console.error('响应体不存在:', response)
@@ -284,8 +269,6 @@ export default {
const reader = response.body.getReader() const reader = response.body.getReader()
let buffer = '' let buffer = ''
while (true) { while (true) {
// if (this.single) break
try { try {
const { done, value } = await reader.read() const { done, value } = await reader.read()
if (done) break if (done) break
@@ -293,7 +276,7 @@ export default {
const lines = buffer.split('\n') const lines = buffer.split('\n')
lines.slice(0, -1).forEach((line) => { lines.slice(0, -1).forEach((line) => {
const parsed = this.parseStreamLine(line) const parsed = this.parseStreamLine(line)
if (parsed) this.updateMessageContent(parsed) if (parsed) this.updateMessageContent(parsed, requestIndex)
}) })
buffer = lines[lines.length - 1] || '' buffer = lines[lines.length - 1] || ''
} catch (error) { } catch (error) {
@@ -324,6 +307,7 @@ export default {
this.requestSingle.abort() this.requestSingle.abort()
this.single = true this.single = true
this.axiosGetAiChat() this.axiosGetAiChat()
this.requestIndex++
// this.typingQueue = [] // this.typingQueue = []
return null return null
} }
@@ -356,29 +340,25 @@ export default {
return data return data
}, },
updateMessageContent(parse) { updateMessageContent(parse, requestIndex) {
let { event, answer, isThink, message_id } = parse let { event, answer, isThink, message_id } = parse
this.currentMessageID = message_id this.currentMessageID = message_id
// 会导致发送按钮提前高亮展示
// if (event === 'message_end') {
// this.$emit('update:messageStatus', 'stop')
// }
if (!this.currentMessage || !answer) return if (!this.currentMessage || !answer) return
if (event !== 'message') return if (event !== 'message') return
// console.log(parse);
const mode = isThink ? 'think' : 'text' const mode = isThink ? 'think' : 'text'
const chars = { const chars = {
answer: answer, answer: answer,
isThink: isThink, isThink: isThink,
message_id message_id
} }
// debugger
this.typingQueue.push(chars) this.typingQueue.push(chars)
if (!this.isTyping) { if (!this.isTyping) {
this.startTypingAnimation(mode) this.startTypingAnimation(mode, requestIndex)
} }
}, },
startTypingAnimation() { startTypingAnimation(mode, requestIndex) {
this.isTyping = true this.isTyping = true
const typeNextChar = () => { const typeNextChar = () => {
if (this.typingQueue.length === 0) { if (this.typingQueue.length === 0) {
@@ -406,7 +386,17 @@ export default {
return return
} }
const char = chars.shift() || '' const char = chars.shift() || ''
this.$set(this.currentMessage, isThink ? 'think' : 'text', this.currentMessage[isThink ? 'think' : 'text'] + char) // this.$set(this.currentMessage, isThink ? 'think' : 'text', this.currentMessage[isThink ? 'think' : 'text'] + char)
if (requestIndex === 2) {
console.log('requestIndex === 2', char);
this.$set(this.genMessage, 'text', this.genMessage.text + char)
// this.messages.splice(this.messages.length - 1, 1, this.genMessage)
} if (requestIndex === 1) {
console.log('requestIndex === 1', char);
this.$set(this.currentMessage, 'text', this.currentMessage.text + char)
}
const delay = this.getTypingDelay(char) const delay = this.getTypingDelay(char)
setTimeout(outputChar, delay) setTimeout(outputChar, delay)
} }

View File

@@ -0,0 +1,3 @@
function generateMessageInfo() {
}

View File

@@ -20,7 +20,8 @@
<p v-html="md.render(message.think)" v-if="message.think && message.showThink" class="thinkText" /> <p v-html="md.render(message.think)" v-if="message.think && message.showThink" class="thinkText" />
</span> </span>
<div style="width: 100%"> <div style="width: 100%">
<!-- {{ (message) }} --> {{ (message) }}
<!-- {{ filterVisible(message) }} -->
<!-- <hr> --> <!-- <hr> -->
<p v-html="render(message)" class="render-container"></p> <p v-html="render(message)" class="render-container"></p>
<span class="speakLoadingToast pv10" v-if="!filterVisible(message)"> <span class="speakLoadingToast pv10" v-if="!filterVisible(message)">