@@ -342,8 +342,11 @@ export async function prepareInputItemsWithSession(
342342 const newInputItems = toAgentInputList ( input ) ;
343343
344344 if ( ! sessionInputCallback ) {
345+ const historyForModelInput = history . map ( ( item ) =>
346+ prepareHistoryItemForModelInput ( session , item ) ,
347+ ) ;
345348 const preparedInput = includeHistoryInPreparedInput
346- ? dropOrphanToolCalls ( [ ...history , ...newInputItems ] , {
349+ ? dropOrphanToolCalls ( [ ...historyForModelInput , ...newInputItems ] , {
347350 pruningIndexes : new Set ( history . map ( ( _ , index ) => index ) ) ,
348351 } )
349352 : newInputItems ;
@@ -363,37 +366,41 @@ export async function prepareInputItemsWithSession(
363366 ) ;
364367 }
365368
366- const historyCounts = buildItemFrequencyMap ( historySnapshot ) ;
369+ const historyCounts = buildItemFrequencyMap ( historySnapshot , {
370+ session,
371+ prepareForModelInput : true ,
372+ } ) ;
367373 const newInputCounts = buildItemFrequencyMap ( newInputSnapshot ) ;
368374 const historyRefs = buildAgentInputPool ( historySnapshot ) ;
369375 const newInputRefs = buildAgentInputPool ( newInputSnapshot ) ;
370376 const historyIndexes = new Set < number > ( ) ;
371377
372378 const appended : AgentInputItem [ ] = [ ] ;
373379 for ( const [ index , item ] of combined . entries ( ) ) {
374- const key = getAgentInputItemKey ( item ) ;
380+ const historyKey = getHistoryItemModelInputKey ( session , item ) ;
381+ const newInputKey = getAgentInputItemKey ( item ) ;
375382 if ( removeAgentInputFromPool ( newInputRefs , item ) ) {
376- decrementCount ( newInputCounts , key ) ;
383+ decrementCount ( newInputCounts , newInputKey ) ;
377384 appended . push ( item ) ;
378385 continue ;
379386 }
380387
381388 if ( removeAgentInputFromPool ( historyRefs , item ) ) {
382- decrementCount ( historyCounts , key ) ;
389+ decrementCount ( historyCounts , historyKey ) ;
383390 historyIndexes . add ( index ) ;
384391 continue ;
385392 }
386393
387- const historyRemaining = historyCounts . get ( key ) ?? 0 ;
394+ const historyRemaining = historyCounts . get ( historyKey ) ?? 0 ;
388395 if ( historyRemaining > 0 ) {
389- historyCounts . set ( key , historyRemaining - 1 ) ;
396+ historyCounts . set ( historyKey , historyRemaining - 1 ) ;
390397 historyIndexes . add ( index ) ;
391398 continue ;
392399 }
393400
394- const newRemaining = newInputCounts . get ( key ) ?? 0 ;
401+ const newRemaining = newInputCounts . get ( newInputKey ) ?? 0 ;
395402 if ( newRemaining > 0 ) {
396- newInputCounts . set ( key , newRemaining - 1 ) ;
403+ newInputCounts . set ( newInputKey , newRemaining - 1 ) ;
397404 appended . push ( item ) ;
398405 continue ;
399406 }
@@ -421,7 +428,14 @@ export async function prepareInputItemsWithSession(
421428 }
422429
423430 const prunedPreparedItems = includeHistoryInPreparedInput
424- ? dropOrphanToolCalls ( preparedItems , { pruningIndexes : historyIndexes } )
431+ ? dropOrphanToolCalls (
432+ prepareHistoryItemsForModelInput (
433+ session ,
434+ preparedItems ,
435+ historyIndexes ,
436+ ) ,
437+ { pruningIndexes : historyIndexes } ,
438+ )
425439 : preparedItems ;
426440
427441 return {
@@ -430,6 +444,35 @@ export async function prepareInputItemsWithSession(
430444 } ;
431445}
432446
447+ function prepareHistoryItemsForModelInput (
448+ session : Session ,
449+ items : AgentInputItem [ ] ,
450+ historyIndexes : Set < number > ,
451+ ) : AgentInputItem [ ] {
452+ if ( ! session . prepareHistoryItemForModelInput || historyIndexes . size === 0 ) {
453+ return items ;
454+ }
455+ return items . map ( ( item , index ) =>
456+ historyIndexes . has ( index )
457+ ? prepareHistoryItemForModelInput ( session , item )
458+ : item ,
459+ ) ;
460+ }
461+
462+ function prepareHistoryItemForModelInput (
463+ session : Session ,
464+ item : AgentInputItem ,
465+ ) : AgentInputItem {
466+ return session . prepareHistoryItemForModelInput ?.( item ) ?? item ;
467+ }
468+
469+ function getHistoryItemModelInputKey (
470+ session : Session ,
471+ item : AgentInputItem ,
472+ ) : string {
473+ return getAgentInputItemKey ( prepareHistoryItemForModelInput ( session , item ) ) ;
474+ }
475+
433476function normalizeItemsForSessionPersistence (
434477 items : AgentInputItem [ ] ,
435478) : AgentInputItem [ ] {
@@ -635,10 +678,16 @@ async function runCompactionOnSession(
635678 ) ;
636679}
637680
638- function buildItemFrequencyMap ( items : AgentInputItem [ ] ) : Map < string , number > {
681+ function buildItemFrequencyMap (
682+ items : AgentInputItem [ ] ,
683+ options ?: { session ?: Session ; prepareForModelInput ?: boolean } ,
684+ ) : Map < string , number > {
639685 const counts = new Map < string , number > ( ) ;
640686 for ( const item of items ) {
641- const key = getAgentInputItemKey ( item ) ;
687+ const key =
688+ options ?. prepareForModelInput && options . session
689+ ? getHistoryItemModelInputKey ( options . session , item )
690+ : getAgentInputItemKey ( item ) ;
642691 counts . set ( key , ( counts . get ( key ) ?? 0 ) + 1 ) ;
643692 }
644693 return counts ;
0 commit comments