伪流式

main
wangyunfei 1 month ago
parent eb453493fc
commit a7edbcf865

@ -110,21 +110,36 @@ export const callLocalChatAPI = async (prompt, onStreamData) => {
// 按行处理响应
const lines = responseText.split(/\r?\n/)
console.log('总行数:', lines.length)
for (const line of lines) {
for (let i = 0; i < lines.length; i++) {
const line = lines[i]
if (line.startsWith('event:')) {
currentEvent = line.substring(6).trim()
console.log(`${i}行 - 事件类型:`, currentEvent)
} else if (line.startsWith('data:')) {
const data = line.substring(5)
console.log(`${i}行 - 数据类型:${currentEvent}, 数据长度:${data.length}, 数据内容:${data.substring(0, 50)}...`)
if (currentEvent === 'thought') {
thoughtContent += data
} else if (currentEvent === 'message') {
// 累加所有 message 数据,而不是只处理第一个
messageContent += data
console.log(`messageContent 当前长度: ${messageContent.length}`)
}
}
}
// 调试:打印解析结果
console.log('解析结果:', {
thoughtLength: thoughtContent.length,
messageLength: messageContent.length,
thought: thoughtContent.substring(0, 100) + '...',
message: messageContent.substring(0, 100) + '...'
})
// 返回最终结果
console.log('API returning:3333', { thought: thoughtContent, message: messageContent }) // 调试信息

@ -136,8 +136,10 @@ const [currentStreamingMessageId, setCurrentStreamingMessageId] = useState(null)
// 模拟流式效果
const simulateStreamingEffect = (fullContent, messageId) => {
console.log('开始流式效果:', { fullContent: fullContent.substring(0, 100) + '...', messageId, length: fullContent.length })
setIsStreaming(true)
setStreamingContent('')
setCurrentStreamingMessageId(messageId)
let currentIndex = 0
const chunkSize = 3 // 每次显示3个字符
@ -148,6 +150,8 @@ const [currentStreamingMessageId, setCurrentStreamingMessageId] = useState(null)
const nextIndex = Math.min(currentIndex + chunkSize, fullContent.length)
const currentContent = fullContent.substring(0, nextIndex)
console.log('流式进度:', { currentIndex, nextIndex, currentContent: currentContent.substring(0, 50) + '...' })
setStreamingContent(currentContent)
conversationStore.updateMessage(messageId, currentContent)
@ -155,6 +159,7 @@ const [currentStreamingMessageId, setCurrentStreamingMessageId] = useState(null)
setTimeout(streamNext, delay)
} else {
// 流式效果完成
console.log('流式效果完成')
setIsStreaming(false)
setStreamingContent('')
setCurrentStreamingMessageId(null)
@ -394,11 +399,67 @@ const [currentStreamingMessageId, setCurrentStreamingMessageId] = useState(null)
{/* 回答框 */}
<div className='ds-message-content'>
{displayContent ? (
<ReactMarkdown
remarkPlugins={[remarkGfm]}
>
{displayContent}
</ReactMarkdown>
<div>
{deepThinkingEnabled && displayContent.includes('\n\n') ? (
// 当开启深度思考且有思考内容时,分别渲染
(() => {
const parts = displayContent.split('\n\n')
const thought = parts[0]
const message = parts.slice(1).join('\n\n')
return (
<>
{/* 思考过程 */}
<div
style={{
fontSize: '14px',
color: '#666',
padding: '5px',
backgroundColor: '#f5f5f5',
borderRadius: '4px',
marginBottom: '10px',
// fontStyle: 'italic',
borderLeft: '3px solid #d9d9d9'
}}
>
<strong>思考过程</strong>
<ReactMarkdown
remarkPlugins={[remarkGfm]}
components={{
p: ({children}) => <span style={{fontSize: '14px'}}>{children}</span>,
strong: ({children}) => <strong style={{fontSize: '14px'}}>{children}</strong>,
em: ({children}) => <em style={{fontSize: '14px'}}>{children}</em>,
code: ({children}) => <code style={{fontSize: '14px'}}>{children}</code>,
pre: ({children}) => <pre style={{fontSize: '14px'}}>{children}</pre>
}}
>
{thought}
</ReactMarkdown>
</div>
{/* 正式回答 */}
{message && (
<div>
{/* <strong style={{fontSize: '12px', color: '#333', marginBottom: '4px', display: 'block'}}>回答:</strong> */}
<ReactMarkdown
remarkPlugins={[remarkGfm]}
>
{message}
</ReactMarkdown>
</div>
)}
</>
)
})()
) : (
// 普通渲染
<ReactMarkdown
remarkPlugins={[remarkGfm]}
>
{displayContent}
</ReactMarkdown>
)}
</div>
) : (
<div style={{ color: '#999', fontStyle: 'italic' }}>
暂无内容

@ -126,15 +126,21 @@ class PageConversationStore {
// 更新指定消息的内容
updateMessage(messageId, newContent) {
console.log('updateMessage 调用:', { messageId, newContent: newContent.substring(0, 50) + '...', length: newContent.length })
const currentConversation = this.getCurrentConversation()
if (currentConversation) {
const message = currentConversation.messages.find(msg => msg.id === messageId)
if (message) {
console.log('找到消息,更新内容:', { oldContent: message.content.substring(0, 50) + '...', newContent: newContent.substring(0, 50) + '...' })
message.content = newContent
currentConversation.lastUpdate = new Date()
this.saveToStorage()
this.notifySubscribers()
} else {
console.log('未找到消息:', messageId)
}
} else {
console.log('未找到当前对话')
}
}

Loading…
Cancel
Save