@@ -389,6 +389,14 @@ export function checkValidNumber(value: unknown): value is number {
389
389
return typeof value === 'number' && ! isNaN ( value ) && value > 0 ;
390
390
}
391
391
392
+ type ThinkingBlocks = {
393
+ thinking_blocks ?: Array < {
394
+ type : 'thinking' ;
395
+ thinking : string ;
396
+ signature : string ;
397
+ } > ;
398
+ } ;
399
+
392
400
export function createPruneMessages ( factoryParams : PruneMessagesFactoryParams ) {
393
401
const indexTokenCountMap = { ...factoryParams . indexTokenCountMap } ;
394
402
let lastTurnStartIndex = factoryParams . startIndex ;
@@ -402,6 +410,49 @@ export function createPruneMessages(factoryParams: PruneMessagesFactoryParams) {
402
410
context : BaseMessage [ ] ;
403
411
indexTokenCountMap : Record < string , number | undefined > ;
404
412
} {
413
+ if (
414
+ factoryParams . provider === Providers . OPENAI &&
415
+ factoryParams . thinkingEnabled === true
416
+ ) {
417
+ for ( let i = lastTurnStartIndex ; i < params . messages . length ; i ++ ) {
418
+ const m = params . messages [ i ] ;
419
+ if (
420
+ m . getType ( ) === 'ai' &&
421
+ typeof m . additional_kwargs . reasoning_content === 'string' &&
422
+ Array . isArray (
423
+ (
424
+ m . additional_kwargs . provider_specific_fields as
425
+ | ThinkingBlocks
426
+ | undefined
427
+ ) ?. thinking_blocks
428
+ ) &&
429
+ ( m as AIMessage ) . tool_calls &&
430
+ ( ( m as AIMessage ) . tool_calls ?. length ?? 0 ) > 0
431
+ ) {
432
+ const message = m as AIMessage ;
433
+ const thinkingBlocks = (
434
+ message . additional_kwargs . provider_specific_fields as ThinkingBlocks
435
+ ) . thinking_blocks ;
436
+ const signature =
437
+ thinkingBlocks ?. [ thinkingBlocks . length - 1 ] . signature ;
438
+ const thinkingBlock : ThinkingContentText = {
439
+ signature,
440
+ type : ContentTypes . THINKING ,
441
+ thinking : message . additional_kwargs . reasoning_content as string ,
442
+ } ;
443
+
444
+ params . messages [ i ] = new AIMessage ( {
445
+ ...message ,
446
+ content : [ thinkingBlock ] ,
447
+ additional_kwargs : {
448
+ ...message . additional_kwargs ,
449
+ reasoning_content : undefined ,
450
+ } ,
451
+ } ) ;
452
+ }
453
+ }
454
+ }
455
+
405
456
let currentUsage : UsageMetadata | undefined ;
406
457
if (
407
458
params . usageMetadata &&
0 commit comments