Skip to content

Commit 576d5c9

Browse files
committed
fix: 初步修复推理输出丢失的问题
1 parent 6b93414 commit 576d5c9

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

src/deepseekApi.ts

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,37 @@ export async function processDeepSeekResponse(
9191
// 流式处理
9292
let chunkResponse = '';
9393
let finishReason: string | null = null;
94+
let thinking = false; // 新增 thinking 状态跟踪
9495

9596
for await (const chunk of options.response as AsyncIterable<OpenAI.Chat.Completions.ChatCompletionChunk>) {
9697
if (options.abortSignal?.aborted) {
9798
throw new Error(options.userStopException);
9899
}
99100

100-
const content = chunk.choices[0]?.delta?.content || '';
101+
const delta = chunk.choices[0]?.delta;
102+
const content = delta?.content || '';
103+
const thinkContent = (delta && 'reasoning_content' in delta)
104+
? (delta.reasoning_content as string)
105+
: "";
106+
107+
// Think 标签开始逻辑
108+
if (!thinking && chunkResponse === "" && thinkContent) {
109+
options.outputChannel?.append("<think>");
110+
thinking = true;
111+
}
112+
101113
chunkResponse += content;
102-
options.outputChannel?.append(content);
114+
115+
// 同时输出内容和思考内容
116+
if (options.outputChannel) {
117+
options.outputChannel.append(content + thinkContent);
118+
}
119+
120+
// Think 标签结束逻辑
121+
if (thinking && content) {
122+
options.outputChannel?.append("</think>");
123+
thinking = false;
124+
}
103125

104126
// 执行自定义 chunk 处理
105127
if (options.onChunk) {
@@ -112,7 +134,6 @@ export async function processDeepSeekResponse(
112134
return { chunkResponse, finishReason };
113135
}
114136

115-
116137
/**
117138
* 调用 DeepSeek API,支持 Function Calling
118139
* @param userContent 用户输入内容,可以是字符串或消息数组
@@ -175,14 +196,16 @@ export async function callDeepSeekApi(
175196
// 构造消息体
176197
let messages_body: OpenAI.ChatCompletionMessageParam[] = [];
177198
if (Array.isArray(userContent)) {
178-
if (systemPromot.role === 'user') { userContent[0].content = systemPromot.content + "\n\n" + userContent[0].content; }
179-
else{ messages_body.push(systemPromot); }
180-
181-
{
182-
const role = (userContent[0].role === 'user') ? 'user' : 'assistant';
183-
messages_body.push({ role, content: userContent[0].content });
199+
if (systemPromot.role === 'user') {
200+
userContent[0].content = systemPromot.content + "\n\n" + userContent[0].content;
201+
}
202+
else{
203+
messages_body.push(systemPromot);
184204
}
185205

206+
const role = (userContent[0].role === 'user') ? 'user' : 'assistant';
207+
messages_body.push({ role, content: userContent[0].content });
208+
186209
// 如果 userContent 是数组,按交替方式生成消息
187210
for (let i = 1; i < userContent.length; i++) {
188211
const role = (userContent[i].role === 'user') ? 'user' : 'assistant';

src/resources/chatPanel.css

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,13 @@ button {
159159
text-overflow: ellipsis;
160160
max-width: 200px;
161161
}
162+
163+
think {
164+
color: #9ccc9c; /* 柔和的绿色 */
165+
font-style: italic;
166+
opacity: 0.8;
167+
border-left: 3px solid #4d9375; /* 左侧装饰线 */
168+
padding-left: 0.8em;
169+
margin: 0.5em 0;
170+
display: block; /* 确保块级显示 */
171+
}

0 commit comments

Comments
 (0)