feat: 范式分析结果改为流式输出
- 修改API analyzeContent方法使用fetch实现流式请求 - 更新AnalysisPanel实时处理流式响应 - 分析过程中实时显示结果,提升用户体验
This commit is contained in:
@@ -209,7 +209,12 @@ const analyzeArticle = async () => {
|
||||
}
|
||||
|
||||
isAnalyzing.value = true
|
||||
appStore.analysisResult = null
|
||||
appStore.analysisResult = {
|
||||
paradigm: '分析中...',
|
||||
paradigmType: null,
|
||||
analysis: '',
|
||||
timestamp: new Date()
|
||||
}
|
||||
|
||||
try {
|
||||
const api = new DeepSeekAPI({
|
||||
@@ -217,21 +222,55 @@ const analyzeArticle = async () => {
|
||||
key: appStore.apiKey
|
||||
})
|
||||
|
||||
const result = await api.analyzeContent(analysisText.value)
|
||||
const response = await api.analyzeContent(analysisText.value)
|
||||
const reader = response.body.getReader()
|
||||
const decoder = new TextDecoder()
|
||||
|
||||
// 解析分析结果,提取范式类型
|
||||
const analysisContent = result.choices[0].message.content
|
||||
const detectedParadigm = detectParadigm(analysisContent)
|
||||
let fullContent = ''
|
||||
|
||||
while (true) {
|
||||
const { done, value } = await reader.read()
|
||||
if (done) break
|
||||
|
||||
const chunk = decoder.decode(value)
|
||||
const lines = chunk.split('\n').filter(line => line.trim())
|
||||
|
||||
for (const line of lines) {
|
||||
if (line.startsWith('data: ')) {
|
||||
const data = line.slice(6)
|
||||
if (data === '[DONE]') continue
|
||||
|
||||
try {
|
||||
const parsed = JSON.parse(data)
|
||||
const content = parsed.choices?.[0]?.delta?.content || ''
|
||||
if (content) {
|
||||
fullContent += content
|
||||
// 实时更新分析结果
|
||||
appStore.analysisResult = {
|
||||
paradigm: '分析中...',
|
||||
paradigmType: null,
|
||||
analysis: fullContent,
|
||||
timestamp: new Date()
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
// 忽略解析错误
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 分析完成后检测范式类型
|
||||
const detectedParadigm = detectParadigm(fullContent)
|
||||
appStore.analysisResult = {
|
||||
paradigm: detectedParadigm.name,
|
||||
paradigmType: detectedParadigm.type,
|
||||
analysis: analysisContent,
|
||||
analysis: fullContent,
|
||||
timestamp: new Date()
|
||||
}
|
||||
|
||||
// 添加到历史记录
|
||||
addToHistory(detectedParadigm.name, analysisText.value, analysisContent)
|
||||
addToHistory(detectedParadigm.name, analysisText.value, fullContent)
|
||||
} catch (error) {
|
||||
appStore.analysisResult = {
|
||||
error: true,
|
||||
|
||||
Reference in New Issue
Block a user