@@ -19,7 +19,7 @@ export class ChatService {
1919 async sendMessage (
2020 messages : ApiChatMessageData [ ] ,
2121 options : SettingsChatServiceOptions = { }
22- ) : Promise < string | void > {
22+ ) : Promise < string | void > {
2323 const {
2424 stream, onChunk, onComplete, onError,
2525 // Generation parameters
@@ -45,6 +45,7 @@ export class ChatService {
4545 role : msg . role ,
4646 content : msg . content
4747 } ) ) ,
48+ reasoning_format : 'auto' ,
4849 stream
4950 } ;
5051
@@ -124,7 +125,7 @@ export class ChatService {
124125 }
125126
126127 if ( stream ) {
127- return this . handleStreamResponse ( response , onChunk , onComplete , onError ) ;
128+ return this . handleStreamResponse ( response , onChunk , onComplete , onError , options . onReasoningChunk ) ;
128129 } else {
129130 return this . handleNonStreamResponse ( response , onComplete , onError ) ;
130131 }
@@ -166,14 +167,16 @@ export class ChatService {
166167 * @param onChunk - Optional callback invoked for each content chunk received
167168 * @param onComplete - Optional callback invoked when the stream is complete with full response
168169 * @param onError - Optional callback invoked if an error occurs during streaming
170+ * @param onReasoningChunk - Optional callback invoked for each reasoning content chunk
169171 * @returns {Promise<void> } Promise that resolves when streaming is complete
170172 * @throws {Error } if the stream cannot be read or parsed
171173 */
172174 private async handleStreamResponse (
173175 response : Response ,
174176 onChunk ?: ( chunk : string ) => void ,
175- onComplete ?: ( response : string ) => void ,
176- onError ?: ( error : Error ) => void
177+ onComplete ?: ( response : string , reasoningContent ?: string ) => void ,
178+ onError ?: ( error : Error ) => void ,
179+ onReasoningChunk ?: ( chunk : string ) => void
177180 ) : Promise < void > {
178181 const reader = response . body ?. getReader ( ) ;
179182
@@ -183,6 +186,7 @@ export class ChatService {
183186
184187 const decoder = new TextDecoder ( ) ;
185188 let fullResponse = '' ;
189+ let fullReasoningContent = '' ;
186190 let thinkContent = '' ;
187191 let regularContent = '' ;
188192 let insideThinkTag = false ;
@@ -208,13 +212,15 @@ export class ChatService {
208212 onError ?.( contextError ) ;
209213 return ;
210214 }
211- onComplete ?.( regularContent ) ;
215+ onComplete ?.( regularContent , fullReasoningContent || undefined ) ;
212216 return ;
213217 }
214218
215219 try {
216220 const parsed : ApiChatCompletionStreamChunk = JSON . parse ( data ) ;
217221 const content = parsed . choices [ 0 ] ?. delta ?. content ;
222+ const reasoningContent = parsed . choices [ 0 ] ?. delta ?. reasoning_content ;
223+
218224 if ( content ) {
219225 hasReceivedData = true ;
220226 fullResponse += content ;
@@ -240,6 +246,12 @@ export class ChatService {
240246 onChunk ?.( newRegularContent ) ;
241247 }
242248 }
249+
250+ if ( reasoningContent ) {
251+ hasReceivedData = true ;
252+ fullReasoningContent += reasoningContent ;
253+ onReasoningChunk ?.( reasoningContent ) ;
254+ }
243255 } catch ( e ) {
244256 console . error ( 'Error parsing JSON chunk:' , e ) ;
245257 }
@@ -272,14 +284,14 @@ export class ChatService {
272284 * @param response - The fetch Response object containing the JSON data
273285 * @param onComplete - Optional callback invoked when response is successfully parsed
274286 * @param onError - Optional callback invoked if an error occurs during parsing
275- * @returns {Promise<string> } Promise that resolves to the generated content string
287+ * @returns {Promise<string> } Promise that resolves to the generated content string
276288 * @throws {Error } if the response cannot be parsed or is malformed
277289 */
278290 private async handleNonStreamResponse (
279291 response : Response ,
280- onComplete ?: ( response : string ) => void ,
292+ onComplete ?: ( response : string , reasoningContent ?: string ) => void ,
281293 onError ?: ( error : Error ) => void
282- ) : Promise < string > {
294+ ) : Promise < string > {
283295 try {
284296 // Check if response body is empty
285297 const responseText = await response . text ( ) ;
@@ -293,6 +305,11 @@ export class ChatService {
293305
294306 const data : ApiChatCompletionResponse = JSON . parse ( responseText ) ;
295307 const content = data . choices [ 0 ] ?. message ?. content || '' ;
308+ const reasoningContent = data . choices [ 0 ] ?. message ?. reasoning_content ;
309+
310+ if ( reasoningContent ) {
311+ console . log ( 'Full reasoning content:' , reasoningContent ) ;
312+ }
296313
297314 // Check if content is empty even with valid JSON structure
298315 if ( ! content . trim ( ) ) {
@@ -302,7 +319,7 @@ export class ChatService {
302319 throw contextError ;
303320 }
304321
305- onComplete ?.( content ) ;
322+ onComplete ?.( content , reasoningContent ) ;
306323
307324 return content ;
308325 } catch ( error ) {
@@ -432,7 +449,8 @@ export class ChatService {
432449 temperature ?: number ;
433450 max_tokens ?: number ;
434451 onChunk ?: ( chunk : string ) => void ;
435- onComplete ?: ( response ?: string ) => void ;
452+ onReasoningChunk ?: ( chunk : string ) => void ;
453+ onComplete ?: ( response ?: string , reasoningContent ?: string ) => void ;
436454 onError ?: ( error : Error ) => void ;
437455 } = { }
438456 ) : Promise < string | void > {
0 commit comments