From 03e4007149c5757c1caac47c7d2f449278832538 Mon Sep 17 00:00:00 2001 From: empty Date: Thu, 8 Jan 2026 10:32:37 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=86=99=E4=BD=9C?= =?UTF-8?q?=E5=B7=A5=E5=9D=8A=E6=B5=81=E5=BC=8F=E8=BE=93=E5=87=BA=E5=A4=B1?= =?UTF-8?q?=E6=95=88=E9=97=AE=E9=A2=98=EF=BC=8C=E4=BC=98=E5=8C=96=E6=B5=81?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E8=A7=A3=E6=9E=90=E7=A8=B3=E5=AE=9A=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/deepseek.js | 41 ++++++++++++++++------------ src/components/WriterPanel.vue | 49 ++++++++++++++++------------------ 2 files changed, 47 insertions(+), 43 deletions(-) diff --git a/src/api/deepseek.js b/src/api/deepseek.js index b8f5d9d..dc8060b 100644 --- a/src/api/deepseek.js +++ b/src/api/deepseek.js @@ -14,27 +14,34 @@ class DeepSeekAPI { } async generateContent(prompt, options = {}) { - const response = await this.client.post('', { - model: 'deepseek-chat', - messages: [ - { - role: 'system', - content: '你是一个资深的专业写作助手,请严格按照用户的要求进行创作。' - }, - { - role: 'user', - content: prompt - } - ], - stream: true, - temperature: 0.7, - ...options - }, { + const response = await fetch(this.baseURL, { + method: 'POST', headers: { + 'Content-Type': 'application/json', 'Authorization': `Bearer ${this.apiKey}` - } + }, + body: JSON.stringify({ + model: 'deepseek-chat', + messages: [ + { + role: 'system', + content: '你是一个资深的专业写作助手,请严格按照用户的要求进行创作。' + }, + { + role: 'user', + content: prompt + } + ], + stream: true, + temperature: 0.7, + ...options + }) }) + if (!response.ok) { + throw new Error(`API请求失败: ${response.status}`) + } + return response } diff --git a/src/components/WriterPanel.vue b/src/components/WriterPanel.vue index e9b5073..f73f83a 100644 --- a/src/components/WriterPanel.vue +++ b/src/components/WriterPanel.vue @@ -197,42 +197,39 @@ const generateContent = async () => { const response = await api.generateContent(prompt) const reader = response.body.getReader() const decoder = new TextDecoder() + let buffer = '' while (true) { const { done, value } = await reader.read() if (done) break - const chunk = decoder.decode(value) - const content = parseStreamResponse(chunk) - generatedContent.value += content + buffer += decoder.decode(value, { stream: true }) + const lines = buffer.split('\n') + buffer = lines.pop() // 最后一行可能不完整,存入 buffer + + for (const line of lines) { + const trimmedLine = line.trim() + if (!trimmedLine || !trimmedLine.startsWith('data: ')) continue + + const data = trimmedLine.slice(6) + if (data === '[DONE]') break + + try { + const parsed = JSON.parse(data) + const content = parsed.choices?.[0]?.delta?.content || '' + if (content) { + generatedContent.value += content + } + } catch (e) { + console.warn('解析流数据失败:', e, data) + } + } } } catch (error) { + console.error('生成内容失败:', error) generatedContent.value = `## 错误\n\n${error.message}\n\n请检查 API 配置是否正确。` } finally { isGenerating.value = false } } - -// 解析流式响应 -const parseStreamResponse = (chunk) => { - const lines = chunk.split('\n').filter(line => line.trim()) - const contents = [] - - 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) contents.push(content) - } catch (e) { - // 忽略解析错误 - } - } - } - - return contents.join('') -}