@@ -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' ;
0 commit comments