refactor: 优化流式输出与状态管理
- 重构 DeepSeekAPI.js,实现稳健的 SSE 流式解析 - 将核心业务逻辑(生成、分析)移入 appStore.js - 优化 WriterPanel 和 AnalysisPanel 组件,移除冗余逻辑 - 更新文档,补充架构演进说明
This commit is contained in:
@@ -203,81 +203,12 @@ const applyParadigm = (paradigm) => {
|
||||
|
||||
// 分析文章
|
||||
const analyzeArticle = async () => {
|
||||
if (!analysisText.value.trim()) {
|
||||
alert('请输入要分析的文章内容')
|
||||
return
|
||||
}
|
||||
|
||||
isAnalyzing.value = true
|
||||
appStore.analysisResult = {
|
||||
paradigm: '分析中...',
|
||||
paradigmType: null,
|
||||
analysis: '',
|
||||
timestamp: new Date()
|
||||
}
|
||||
|
||||
try {
|
||||
const api = new DeepSeekAPI({
|
||||
url: appStore.apiUrl,
|
||||
key: appStore.apiKey
|
||||
})
|
||||
|
||||
const response = await api.analyzeContent(analysisText.value)
|
||||
const reader = response.body.getReader()
|
||||
const decoder = new TextDecoder()
|
||||
|
||||
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: fullContent,
|
||||
timestamp: new Date()
|
||||
}
|
||||
|
||||
const result = await appStore.analyzeArticleAction(analysisText.value, detectParadigm)
|
||||
// 添加到历史记录
|
||||
addToHistory(detectedParadigm.name, analysisText.value, fullContent)
|
||||
addToHistory(result.paradigm, analysisText.value, result.content)
|
||||
} catch (error) {
|
||||
appStore.analysisResult = {
|
||||
error: true,
|
||||
message: error.message
|
||||
}
|
||||
} finally {
|
||||
isAnalyzing.value = false
|
||||
alert(error.message)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user