@@ -327,12 +327,8 @@ export async function* fetchStream(
327327 return
328328 }
329329 } else {
330- const reasoning = getChoiceProp ( json , 'reasoning' ) || getChoiceProp ( json , 'thought' )
331- const token : string =
332- getChoiceProp ( json , 'content' ) ||
333- getChoiceProp ( json , 'text' ) ||
334- json . token ||
335- json . response
330+ const reasoning = getNextThoughts ( json ) || json . reasoning_content
331+ const token : string = getNextTokens ( json ) || json . token || json . response
336332
337333 const index = + ( getChoiceProp < string > ( json , 'index' ) || '0' )
338334
@@ -401,7 +397,7 @@ export async function* fetchStream(
401397 yield { token : suffix + token }
402398 }
403399
404- if ( DEBUG ) {
400+ if ( DEBUG || true ) {
405401 const choice = json . choices ?. [ 0 ]
406402 if ( choice ) console . log ( `#${ type } ` , inline ( choice ) )
407403 else console . log ( `#${ type } ` , inline ( json ) )
@@ -521,6 +517,53 @@ function getChoiceProp<T = any>(json: any, prop: string, assign?: any) {
521517 return value as T
522518}
523519
520+ function getNextTokens ( json : any ) {
521+ const props = [ 'content' , 'text' ]
522+
523+ const choice = json ?. choices ?. [ 0 ]
524+ let value : any = undefined
525+
526+ for ( const prop of props ) {
527+ const match = choice ?. delta ?. [ prop ] || choice ?. [ prop ] || json ?. [ prop ]
528+ if ( ! match ) continue
529+
530+ value = match
531+ break
532+ }
533+
534+ return value
535+ }
536+
537+ function getNextThoughts ( json : any ) {
538+ const props = [ 'reasoning' , 'thought' , 'reasoning_content' ]
539+ const choice = json ?. choices ?. [ 0 ]
540+
541+ let value : any = undefined
542+ for ( const prop of props ) {
543+ const match = choice ?. delta ?. [ prop ] || choice ?. [ prop ] || json ?. [ prop ]
544+ if ( ! match ) continue
545+
546+ value = match
547+ break
548+ }
549+
550+ if ( ! value ) return
551+
552+ if ( typeof value === 'string' ) return value
553+
554+ // Mistral returns thoughts in an array for some reason string
555+ if ( Array . isArray ( value ) ) {
556+ const first = value [ 0 ]
557+ if ( ! first ) return
558+
559+ if ( first . type !== 'thinking' ) return
560+ if ( ! first . thinking ?. [ 0 ] ) return
561+ return first . thinking ?. [ 0 ] ?. text
562+ }
563+
564+ return
565+ }
566+
524567function tryParse ( value : any ) {
525568 try {
526569 const obj = JSON . parse ( value )
0 commit comments