@@ -1737,13 +1737,13 @@ export function extractOutputItemsFromRunItems(
17371737 */
17381738export async function saveToSession (
17391739 session : Session | undefined ,
1740- originalInput : string | AgentInputItem [ ] | undefined ,
1740+ sessionInputItems : AgentInputItem [ ] | undefined ,
17411741 result : RunResult < any , any > ,
17421742) : Promise < void > {
17431743 if ( ! session ) {
17441744 return ;
17451745 }
1746- const inputItems = originalInput ? toInputItemList ( originalInput ) : [ ] ;
1746+ const inputItems = sessionInputItems ?? [ ] ;
17471747 const state = result . state ;
17481748 const alreadyPersisted = state . _currentTurnPersistedItemCount ?? 0 ;
17491749 // Persist only the portion of _generatedItems that has not yet been stored for this turn.
@@ -1771,19 +1771,15 @@ export async function saveToSession(
17711771 */
17721772export async function saveStreamInputToSession (
17731773 session : Session | undefined ,
1774- originalInput : string | AgentInputItem [ ] | undefined ,
1774+ sessionInputItems : AgentInputItem [ ] | undefined ,
17751775) : Promise < void > {
17761776 if ( ! session ) {
17771777 return ;
17781778 }
1779- if ( ! originalInput ) {
1779+ if ( ! sessionInputItems || sessionInputItems . length === 0 ) {
17801780 return ;
17811781 }
1782- const itemsToSave = toInputItemList ( originalInput ) ;
1783- if ( itemsToSave . length === 0 ) {
1784- return ;
1785- }
1786- await session . addItems ( itemsToSave ) ;
1782+ await session . addItems ( sessionInputItems ) ;
17871783}
17881784
17891785/**
@@ -1814,13 +1810,21 @@ export async function saveStreamResultToSession(
18141810 * @internal
18151811 * If a session is provided, expands the input with session history; otherwise returns the input.
18161812 */
1813+ export type PreparedInputWithSessionResult = {
1814+ preparedInput : string | AgentInputItem [ ] ;
1815+ sessionItems ?: AgentInputItem [ ] ;
1816+ } ;
1817+
18171818export async function prepareInputItemsWithSession (
18181819 input : string | AgentInputItem [ ] ,
18191820 session ?: Session ,
18201821 sessionInputCallback ?: SessionInputCallback ,
1821- ) : Promise < string | AgentInputItem [ ] > {
1822+ ) : Promise < PreparedInputWithSessionResult > {
18221823 if ( ! session ) {
1823- return input ;
1824+ return {
1825+ preparedInput : input ,
1826+ sessionItems : undefined ,
1827+ } ;
18241828 }
18251829
18261830 const history = await session . getItems ( ) ;
@@ -1839,7 +1843,10 @@ export async function prepareInputItemsWithSession(
18391843 }
18401844
18411845 if ( ! sessionInputCallback ) {
1842- return [ ...history , ...newInputItems ] ;
1846+ return {
1847+ preparedInput : [ ...history , ...newInputItems ] ,
1848+ sessionItems : newInputItems ,
1849+ } ;
18431850 }
18441851
18451852 // Delegate history reconciliation to the user-supplied callback. It must return a concrete list
@@ -1851,5 +1858,26 @@ export async function prepareInputItemsWithSession(
18511858 ) ;
18521859 }
18531860
1854- return combined ;
1861+ const historySet = new Set ( history ) ;
1862+ const newInputSet = new Set ( newInputItems ) ;
1863+ const appended : AgentInputItem [ ] = [ ] ;
1864+ for ( const item of combined ) {
1865+ if ( historySet . has ( item ) || ! newInputSet . has ( item ) ) {
1866+ if ( ! historySet . has ( item ) && ! newInputSet . has ( item ) ) {
1867+ appended . push ( item ) ;
1868+ }
1869+ continue ;
1870+ }
1871+ appended . push ( item ) ;
1872+ }
1873+
1874+ if ( appended . length === 0 && combined . length > history . length ) {
1875+ // When callbacks replace every new item with fresh objects, fall back to the tail slice.
1876+ appended . push ( ...combined . slice ( history . length ) ) ;
1877+ }
1878+
1879+ return {
1880+ preparedInput : combined ,
1881+ sessionItems : appended . length > 0 ? appended : [ ] ,
1882+ } ;
18551883}
0 commit comments