@@ -10,18 +10,29 @@ export type ReasoningKey = (typeof KNOWN_REASONING_KEYS)[number];
1010
1111export const DEFAULT_REASONING_KEY : ReasoningKey = KNOWN_REASONING_KEYS [ 0 ] ;
1212
13+ export function extractReasoningStrings (
14+ source : unknown ,
15+ ) : { key : string ; value : string } [ ] {
16+ if ( typeof source !== 'object' || source === null ) return [ ] ;
17+ const record = source as Record < string , unknown > ;
18+ const found : { key : string ; value : string } [ ] = [ ] ;
19+ for ( const key of KNOWN_REASONING_KEYS ) {
20+ const value = record [ key ] ;
21+ if ( typeof value === 'string' ) found . push ( { key, value } ) ;
22+ }
23+ return found ;
24+ }
25+
1326export function extractReasoning (
1427 source : unknown ,
1528 explicitKey ?: string ,
1629) : { key : string ; value : string } | undefined {
17- if ( typeof source !== 'object' || source === null ) return undefined ;
18- const record = source as Record < string , unknown > ;
19- const keys : readonly string [ ] = explicitKey !== undefined ? [ explicitKey ] : KNOWN_REASONING_KEYS ;
20- for ( const key of keys ) {
21- const value = record [ key ] ;
22- if ( typeof value === 'string' ) return { key, value } ;
30+ if ( explicitKey !== undefined ) {
31+ if ( typeof source !== 'object' || source === null ) return undefined ;
32+ const value = ( source as Record < string , unknown > ) [ explicitKey ] ;
33+ return typeof value === 'string' ? { key : explicitKey , value } : undefined ;
2334 }
24- return undefined ;
35+ return extractReasoningStrings ( source ) [ 0 ] ;
2536}
2637
2738export class ReasoningKeyDialect {
@@ -32,7 +43,7 @@ export class ReasoningKeyDialect {
3243 observe ( source : unknown ) : string | undefined {
3344 const found = extractReasoning ( source , this . _explicitKey ) ;
3445 if ( found === undefined ) return undefined ;
35- if ( this . _explicitKey === undefined ) {
46+ if ( this . _explicitKey === undefined && this . _detected === undefined ) {
3647 this . _detected = found . key ;
3748 }
3849 return found . value ;
@@ -92,6 +103,7 @@ export function convertReasoningDetails(
92103 think : element . summary ,
93104 detailsIndex : element . index ,
94105 hidden : hiddenSummary ? true : undefined ,
106+ reasoningKey : REASONING_DETAILS_KEY ,
95107 } satisfies ThinkPart ) ;
96108 }
97109 if ( element . type !== 'summary' && element . encrypted !== undefined && element . encrypted . length > 0 ) {
@@ -100,6 +112,7 @@ export function convertReasoningDetails(
100112 think : '' ,
101113 encrypted : element . encrypted ,
102114 detailsIndex : element . index ,
115+ reasoningKey : REASONING_DETAILS_KEY ,
103116 } satisfies ThinkPart ) ;
104117 }
105118 }
0 commit comments