@@ -1597,13 +1597,30 @@ export const useCopilotStore = create<CopilotStore>()(
15971597 ( chat : CopilotChat ) => chat . id === currentChat . id
15981598 )
15991599 if ( updatedCurrentChat ) {
1600- set ( {
1601- currentChat : updatedCurrentChat ,
1602- messages : ensureToolCallDisplayNames ( updatedCurrentChat . messages || [ ] ) ,
1603- } )
1604- logger . info (
1605- `Preserved current chat selection: ${ updatedCurrentChat . title || 'Untitled' } (${ updatedCurrentChat . messages ?. length || 0 } messages)`
1606- )
1600+ const { isSendingMessage } = get ( )
1601+
1602+ // If we're currently streaming, preserve the current messages state
1603+ // to avoid overwriting streaming content with stale database state
1604+ if ( isSendingMessage ) {
1605+ set ( {
1606+ currentChat : {
1607+ ...updatedCurrentChat ,
1608+ messages : get ( ) . messages , // Preserve current streaming messages
1609+ } ,
1610+ } )
1611+ logger . info (
1612+ `Preserved current chat and streaming messages during active stream: ${ updatedCurrentChat . title || 'Untitled' } `
1613+ )
1614+ } else {
1615+ // Safe to update messages when not streaming
1616+ set ( {
1617+ currentChat : updatedCurrentChat ,
1618+ messages : ensureToolCallDisplayNames ( updatedCurrentChat . messages || [ ] ) ,
1619+ } )
1620+ logger . info (
1621+ `Updated current chat with latest database state: ${ updatedCurrentChat . title || 'Untitled' } (${ updatedCurrentChat . messages ?. length || 0 } messages)`
1622+ )
1623+ }
16071624
16081625 // Load checkpoints for the preserved chat
16091626 try {
@@ -1614,22 +1631,31 @@ export const useCopilotStore = create<CopilotStore>()(
16141631 }
16151632 } else {
16161633 // Only auto-select most recent chat if no current chat or current chat is stale
1617- const mostRecentChat = data . chats [ 0 ]
1618- set ( {
1619- currentChat : mostRecentChat ,
1620- messages : ensureToolCallDisplayNames ( mostRecentChat . messages || [ ] ) ,
1621- } )
1622- logger . info (
1623- `Auto-selected most recent chat for workflow ${ workflowId } : ${ mostRecentChat . title || 'Untitled' } `
1624- )
1634+ // But don't auto-select during streaming to avoid disrupting the conversation
1635+ const { isSendingMessage } = get ( )
16251636
1626- // Load checkpoints for the auto-selected chat
1627- try {
1628- await get ( ) . loadMessageCheckpoints ( mostRecentChat . id )
1629- } catch ( checkpointError ) {
1630- logger . error (
1631- 'Failed to load checkpoints for auto-selected chat:' ,
1632- checkpointError
1637+ if ( ! isSendingMessage ) {
1638+ const mostRecentChat = data . chats [ 0 ]
1639+ set ( {
1640+ currentChat : mostRecentChat ,
1641+ messages : ensureToolCallDisplayNames ( mostRecentChat . messages || [ ] ) ,
1642+ } )
1643+ logger . info (
1644+ `Auto-selected most recent chat for workflow ${ workflowId } : ${ mostRecentChat . title || 'Untitled' } `
1645+ )
1646+
1647+ // Load checkpoints for the auto-selected chat
1648+ try {
1649+ await get ( ) . loadMessageCheckpoints ( mostRecentChat . id )
1650+ } catch ( checkpointError ) {
1651+ logger . error (
1652+ 'Failed to load checkpoints for auto-selected chat:' ,
1653+ checkpointError
1654+ )
1655+ }
1656+ } else {
1657+ logger . info (
1658+ `Skipped auto-selecting chat during active stream for workflow ${ workflowId } `
16331659 )
16341660 }
16351661 }
0 commit comments