fix: 修复流式输出不显示问题,实时解析 XML 并更新 UI
This commit is contained in:
@@ -126,10 +126,10 @@ export const useAppStore = defineStore('app', () => {
|
||||
|
||||
console.log('Store: 开始 Ghostwriter Protocol 生成流程...')
|
||||
|
||||
// 流式接收并实时解析 XML 结构
|
||||
// 流式接收并实时解析 XML 结构,同时实时显示内容
|
||||
await api.generateContent(userMessage, (chunk) => {
|
||||
rawStreamBuffer.value += chunk
|
||||
const { section } = streamParser.process(chunk)
|
||||
const { section, buffer } = streamParser.process(chunk)
|
||||
|
||||
// 根据当前 section 更新 UI 状态
|
||||
if (section === 'thinking' && generationStage.value !== 'thinking') {
|
||||
@@ -140,9 +140,26 @@ export const useAppStore = defineStore('app', () => {
|
||||
generationStage.value = 'draft'
|
||||
console.log('Store: 进入草稿生成阶段')
|
||||
}
|
||||
|
||||
// 实时更新 UI 显示(流式输出)
|
||||
// 提取当前已接收的 thinking 和 draft 内容
|
||||
const thinkingMatch = buffer.match(/<thinking>([\s\S]*?)(?:<\/thinking>|$)/)
|
||||
const draftMatch = buffer.match(/<draft>([\s\S]*?)(?:<\/draft>|$)/)
|
||||
|
||||
if (thinkingMatch) {
|
||||
thinkingContent.value = thinkingMatch[1].trim()
|
||||
}
|
||||
if (draftMatch) {
|
||||
generatedContent.value = draftMatch[1].trim()
|
||||
}
|
||||
|
||||
// 如果 AI 没有按 XML 格式输出,直接显示原始内容
|
||||
if (!thinkingMatch && !draftMatch && buffer.length > 50) {
|
||||
generatedContent.value = buffer
|
||||
}
|
||||
})
|
||||
|
||||
// 流式完成后,解析最终结果
|
||||
// 流式完成后,最终解析确保内容完整
|
||||
const { thinking, draft, hasStructuredOutput } = streamParser.getResult()
|
||||
console.log('Store: XML 解析完成', { hasStructuredOutput, thinkingLength: thinking.length, draftLength: draft.length })
|
||||
|
||||
|
||||
Reference in New Issue
Block a user