@@ -48,6 +48,12 @@ fn unsettled(message: &SessionMessageInfo) -> bool {
4848#[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash , PartialOrd , Ord ) ]
4949pub struct MessageKey ( pub u32 ) ;
5050
51+ #[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash ) ]
52+ pub ( crate ) enum StreamPart {
53+ Text ( i64 ) ,
54+ Reasoning ( i64 ) ,
55+ }
56+
5157/// One session's loaded messages, oldest first, found by ID or by key.
5258#[ derive( Debug , Clone , Default ) ]
5359pub struct Transcript {
@@ -57,6 +63,8 @@ pub struct Transcript {
5763 /// The position of each key's item, by key; `u32::MAX` once it left. The next key is its length.
5864 positions : Vec < u32 > ,
5965 index : HashMap < String , usize > ,
66+ /// Parts whose start we saw while their assistant was loaded.
67+ streaming : HashSet < ( MessageKey , StreamPart ) > ,
6068}
6169
6270impl Transcript {
@@ -144,6 +152,7 @@ impl Transcript {
144152 }
145153 self . keys . retain ( |key| self . positions [ key. 0 as usize ] != u32:: MAX ) ;
146154 self . items . retain ( keep) ;
155+ self . streaming . retain ( |( key, _) | self . positions [ key. 0 as usize ] != u32:: MAX ) ;
147156 self . index = HashMap :: new ( ) ;
148157 self . reindex ( 0 ) ;
149158 self . items . shrink_to_fit ( ) ;
@@ -166,7 +175,10 @@ impl Transcript {
166175 /// Replaces the item with the same ID, or appends it.
167176 pub ( crate ) fn upsert ( & mut self , item : SessionMessageInfo ) {
168177 match self . index . get ( item. id ( ) ) {
169- Some ( & position) => self . items [ position] = item,
178+ Some ( & position) => {
179+ self . streaming . retain ( |( key, _) | * key != self . keys [ position] ) ;
180+ self . items [ position] = item;
181+ }
170182 None => {
171183 self . append ( item) ;
172184 }
@@ -178,6 +190,7 @@ impl Transcript {
178190 let position = self . index . remove ( id) ?;
179191 self . items . remove ( position) ;
180192 let key = self . keys . remove ( position) ;
193+ self . streaming . retain ( |( stream, _) | * stream != key) ;
181194 self . positions [ key. 0 as usize ] = u32:: MAX ;
182195 self . reindex ( position) ;
183196 Some ( position)
@@ -199,6 +212,7 @@ impl Transcript {
199212 for key in self . keys . drain ( position..) {
200213 self . positions [ key. 0 as usize ] = u32:: MAX ;
201214 }
215+ self . streaming . retain ( |( key, _) | self . positions [ key. 0 as usize ] != u32:: MAX ) ;
202216 for item in self . items . drain ( position..) {
203217 self . index . remove ( item. id ( ) ) ;
204218 }
@@ -593,6 +607,8 @@ impl State {
593607 for event in & read. edits {
594608 self . apply ( event, & mut ignored) ;
595609 }
610+ // Even a replayed start may precede the snapshot: the empty part it left has no known prefix.
611+ self . transcript_mut ( session) . streaming . clear ( ) ;
596612 unknown
597613 }
598614
@@ -602,12 +618,21 @@ impl State {
602618 session : & ( impl AsRef < str > + ?Sized ) ,
603619 message : & ( impl AsRef < str > + ?Sized ) ,
604620 f : impl FnOnce ( & mut SessionMessageAssistant ) ,
621+ ) {
622+ self . edit_parts ( session, message, |assistant, _, _| f ( assistant) ) ;
623+ }
624+
625+ fn edit_parts (
626+ & mut self ,
627+ session : & ( impl AsRef < str > + ?Sized ) ,
628+ message : & ( impl AsRef < str > + ?Sized ) ,
629+ f : impl FnOnce ( & mut SessionMessageAssistant , MessageKey , & mut HashSet < ( MessageKey , StreamPart ) > ) ,
605630 ) {
606631 let Some ( ( key, position) ) = self . edited ( session. as_ref ( ) , message. as_ref ( ) ) else { return } ;
607632 let Some ( transcript) = & mut self . sessions [ key] . transcript else { return } ;
608- let SessionMessageInfo :: Assistant ( assistant) = & mut transcript. items_mut ( ) [ position] else { return } ;
609- f ( assistant ) ;
610- let message = transcript. keys ( ) [ position ] ;
633+ let SessionMessageInfo :: Assistant ( assistant) = & mut transcript. items [ position] else { return } ;
634+ let message = transcript . keys [ position ] ;
635+ f ( assistant , message, & mut transcript. streaming ) ;
611636 self . touch_message ( key, message) ;
612637 }
613638
@@ -647,46 +672,6 @@ impl State {
647672 } ) ;
648673 }
649674
650- /// Edits the latest text part (`editText`).
651- fn edit_text (
652- & mut self ,
653- session : & ( impl AsRef < str > + ?Sized ) ,
654- message : & ( impl AsRef < str > + ?Sized ) ,
655- f : impl FnOnce ( & mut SessionMessageAssistantText ) ,
656- ) {
657- self . edit_assistant ( session, message, |assistant| {
658- let part = assistant. content . iter_mut ( ) . rev ( ) . find_map ( |part| match part {
659- SessionMessageAssistantContentItem :: Text ( part) => Some ( part) ,
660- _ => None ,
661- } ) ;
662- if let Some ( part) = part {
663- f ( part) ;
664- }
665- } ) ;
666- }
667-
668- /// Edits the latest reasoning part that has not completed (`editReasoning`).
669- fn edit_reasoning (
670- & mut self ,
671- session : & ( impl AsRef < str > + ?Sized ) ,
672- message : & ( impl AsRef < str > + ?Sized ) ,
673- f : impl FnOnce ( & mut SessionMessageAssistantReasoning ) ,
674- ) {
675- self . edit_assistant ( session, message, |assistant| {
676- let part = assistant. content . iter_mut ( ) . rev ( ) . find_map ( |part| match part {
677- SessionMessageAssistantContentItem :: Reasoning ( part)
678- if part. time . as_ref ( ) . is_none_or ( |time| time. completed . is_none ( ) ) =>
679- {
680- Some ( part)
681- }
682- _ => None ,
683- } ) ;
684- if let Some ( part) = part {
685- f ( part) ;
686- }
687- } ) ;
688- }
689-
690675 pub ( crate ) fn on_step_started ( & mut self , event : & SessionStepStarted ) {
691676 let data = & event. data ;
692677 let session = & data. session_id ;
@@ -714,11 +699,11 @@ impl State {
714699 return ;
715700 }
716701 if let Some ( current) = transcript. active_assistant ( ) {
717- current. retry = None ;
718702 current. time . completed = Some ( event. created ) ;
719703 let id = current. id . clone ( ) ;
720704 self . message_changed ( session, & id) ;
721705 }
706+ self . clear_retry ( session) ;
722707 let assistant = SessionMessageAssistant {
723708 id : data. assistant_message_id . clone ( ) ,
724709 r#type : tag:: Assistant ,
@@ -783,7 +768,8 @@ impl State {
783768 /// A start the snapshot already holds (its `ordinal` counts the message's text parts) is skipped, so a replay
784769 /// over a fresh page does not show the part twice.
785770 pub ( crate ) fn on_text_started ( & mut self , session : & SessionID , message : & SessionMessageID , ordinal : i64 ) {
786- self . edit_assistant ( session, message, |assistant| {
771+ self . edit_parts ( session, message, |assistant, key, streaming| {
772+ streaming. insert ( ( key, StreamPart :: Text ( ordinal) ) ) ;
787773 let held = assistant
788774 . content
789775 . iter ( )
@@ -800,12 +786,18 @@ impl State {
800786 }
801787
802788 pub ( crate ) fn on_text_ended ( & mut self , data : & SessionTextEndedData ) {
803- self . edit_text ( & data. session_id , & data. assistant_message_id , |text| text. text . clone_from ( & data. text ) ) ;
789+ self . edit_parts ( & data. session_id , & data. assistant_message_id , |assistant, key, streaming| {
790+ streaming. remove ( & ( key, StreamPart :: Text ( data. ordinal ) ) ) ;
791+ if let Some ( part) = last_text ( assistant) {
792+ part. text . clone_from ( & data. text ) ;
793+ }
794+ } ) ;
804795 }
805796
806797 pub ( crate ) fn on_reasoning_started ( & mut self , event : & SessionReasoningStarted ) {
807798 let data = & event. data ;
808- self . edit_assistant ( & data. session_id , & data. assistant_message_id , |assistant| {
799+ self . edit_parts ( & data. session_id , & data. assistant_message_id , |assistant, key, streaming| {
800+ streaming. insert ( ( key, StreamPart :: Reasoning ( data. ordinal ) ) ) ;
809801 let held = assistant
810802 . content
811803 . iter ( )
@@ -825,7 +817,9 @@ impl State {
825817
826818 pub ( crate ) fn on_reasoning_ended ( & mut self , event : & SessionReasoningEnded ) {
827819 let data = & event. data ;
828- self . edit_reasoning ( & data. session_id , & data. assistant_message_id , |reasoning| {
820+ self . edit_parts ( & data. session_id , & data. assistant_message_id , |assistant, key, streaming| {
821+ streaming. remove ( & ( key, StreamPart :: Reasoning ( data. ordinal ) ) ) ;
822+ let Some ( reasoning) = last_reasoning ( assistant) else { return } ;
829823 reasoning. text . clone_from ( & data. text ) ;
830824 reasoning. time = Some ( SessionMessageShellTime {
831825 created : reasoning. time . as_ref ( ) . map_or ( event. created , |time| time. created ) ,
@@ -958,9 +952,14 @@ impl State {
958952 } ) ;
959953 }
960954
961- /// Clears the retry notice of the active assistant message.
955+ /// A failed stream can retry after its assistant message has already completed .
962956 pub ( crate ) fn clear_retry ( & mut self , session : & SessionID ) {
963- let Some ( current) = self . transcript_mut ( session) . active_assistant ( ) else { return } ;
957+ let Some ( current) = self . transcript_mut ( session) . items . iter_mut ( ) . rev ( ) . find_map ( |message| match message {
958+ SessionMessageInfo :: Assistant ( assistant) => Some ( assistant) ,
959+ _ => None ,
960+ } ) else {
961+ return ;
962+ } ;
964963 if current. retry . take ( ) . is_some ( ) {
965964 let id = current. id . clone ( ) ;
966965 self . message_changed ( session, & id) ;
@@ -1040,11 +1039,18 @@ impl State {
10401039 pub ( crate ) fn apply_delta ( & mut self , delta : Delta < ' _ > ) {
10411040 self . event_seq = None ;
10421041 match delta {
1043- Delta :: Text { session, message, delta } => {
1044- self . edit_text ( session, message, |text| text. text . push_str ( delta) )
1045- }
1046- Delta :: Reasoning { session, message, delta } => {
1047- self . edit_reasoning ( session, message, |reasoning| reasoning. text . push_str ( delta) )
1042+ Delta :: Fragment { session, message, part, delta } => {
1043+ self . edit_parts ( session, message, |assistant, key, streaming| {
1044+ let text = match part {
1045+ StreamPart :: Text ( _) => last_text ( assistant) . map ( |p| & mut p. text ) ,
1046+ StreamPart :: Reasoning ( _) => last_reasoning ( assistant) . map ( |p| & mut p. text ) ,
1047+ } ;
1048+ if let Some ( text) = text
1049+ && ( !text. is_empty ( ) || streaming. contains ( & ( key, part) ) )
1050+ {
1051+ text. push_str ( delta) ;
1052+ }
1053+ } ) ;
10481054 }
10491055 Delta :: ToolInput { session, message, tool, delta } => self . edit_tool ( session, message, tool, |tool| {
10501056 if let SessionMessageAssistantToolState :: Streaming ( state) = & mut tool. state {
@@ -1146,6 +1152,24 @@ fn running_compaction(transcript: &Transcript) -> Option<usize> {
11461152 . rposition ( |item| matches ! ( item, SessionMessageInfo :: Compaction ( SessionMessageCompaction :: Running ( _) ) ) )
11471153}
11481154
1155+ fn last_text ( assistant : & mut SessionMessageAssistant ) -> Option < & mut SessionMessageAssistantText > {
1156+ assistant. content . iter_mut ( ) . rev ( ) . find_map ( |part| match part {
1157+ SessionMessageAssistantContentItem :: Text ( part) => Some ( part) ,
1158+ _ => None ,
1159+ } )
1160+ }
1161+
1162+ fn last_reasoning ( assistant : & mut SessionMessageAssistant ) -> Option < & mut SessionMessageAssistantReasoning > {
1163+ assistant. content . iter_mut ( ) . rev ( ) . find_map ( |part| match part {
1164+ SessionMessageAssistantContentItem :: Reasoning ( part)
1165+ if part. time . as_ref ( ) . is_none_or ( |t| t. completed . is_none ( ) ) =>
1166+ {
1167+ Some ( part)
1168+ }
1169+ _ => None ,
1170+ } )
1171+ }
1172+
11491173fn merge_native ( target : & mut Option < SessionMessageNative > , next : & Option < SessionMessageNative > ) {
11501174 if let Some ( next) = next {
11511175 target. get_or_insert_default ( ) . extend ( next. iter ( ) . map ( |( key, value) | ( key. clone ( ) , value. clone ( ) ) ) ) ;
0 commit comments