@@ -35,6 +35,8 @@ export const useMessageHandling = () => {
3535 const streamingMessageIndexRef = useRef < number | null > ( null ) ;
3636 // Track the index of the current aggregated thinking message
3737 const thinkingMessageIndexRef = useRef < number | null > ( null ) ;
38+ // Preserve one stable timestamp for all message segments in the same turn.
39+ const currentStreamTimestampRef = useRef < number | null > ( null ) ;
3840
3941 /**
4042 * Add message
@@ -54,6 +56,9 @@ export const useMessageHandling = () => {
5456 * Start streaming response
5557 */
5658 const startStreaming = useCallback ( ( timestamp ?: number ) => {
59+ const resolvedTimestamp =
60+ typeof timestamp === 'number' ? timestamp : Date . now ( ) ;
61+ currentStreamTimestampRef . current = resolvedTimestamp ;
5762 // Create an assistant placeholder message immediately so tool calls won't jump before it
5863 setMessages ( ( prev ) => {
5964 // Record index of the placeholder to update on chunks
@@ -63,8 +68,8 @@ export const useMessageHandling = () => {
6368 {
6469 role : 'assistant' ,
6570 content : '' ,
66- // Use provided timestamp (from extension) to keep ordering stable
67- timestamp : typeof timestamp === 'number' ? timestamp : Date . now ( ) ,
71+ // Use one stable turn timestamp so later split segments sort correctly.
72+ timestamp : resolvedTimestamp ,
6873 } ,
6974 ] ;
7075 } ) ;
@@ -89,7 +94,11 @@ export const useMessageHandling = () => {
8994 if ( idx === null ) {
9095 idx = next . length ;
9196 streamingMessageIndexRef . current = idx ;
92- next . push ( { role : 'assistant' , content : '' , timestamp : Date . now ( ) } ) ;
97+ next . push ( {
98+ role : 'assistant' ,
99+ content : '' ,
100+ timestamp : currentStreamTimestampRef . current ?? Date . now ( ) ,
101+ } ) ;
93102 }
94103
95104 if ( idx < 0 || idx >= next . length ) {
@@ -122,6 +131,7 @@ export const useMessageHandling = () => {
122131 setIsStreaming ( false ) ;
123132 streamingMessageIndexRef . current = null ;
124133 thinkingMessageIndexRef . current = null ;
134+ currentStreamTimestampRef . current = null ;
125135 } , [ ] ) ;
126136
127137 /**
@@ -173,7 +183,7 @@ export const useMessageHandling = () => {
173183 assistantIdx >= 0 &&
174184 assistantIdx < next . length
175185 ? next [ assistantIdx ] . timestamp
176- : Date . now ( ) ;
186+ : ( currentStreamTimestampRef . current ?? Date . now ( ) ) ;
177187 next . push ( {
178188 role : 'thinking' ,
179189 content : '' ,
0 commit comments