@@ -209,12 +209,16 @@ class AssistantTurnStoppedMessage:
209209 content. This is the aggregated transcript that is then used in the context.
210210
211211 Parameters:
212- content: The message content/text.
212+ content: The message content/text. May be empty if the LLM
213+ returned zero tokens (e.g. turn was interrupted before any tokens
214+ were received or pushed)
215+ interrupted: Whether the assistant turn was interrupted.
213216 timestamp: When the assistant turn started.
214217
215218 """
216219
217220 content : str
221+ interrupted : bool
218222 timestamp : str
219223
220224
@@ -1032,11 +1036,11 @@ async def _handle_llm_messages_transform(self, frame: LLMMessagesTransformFrame)
10321036 await self .push_context_frame (FrameDirection .UPSTREAM )
10331037
10341038 async def _handle_interruptions (self , frame : InterruptionFrame ):
1035- await self ._trigger_assistant_turn_stopped ()
1039+ await self ._trigger_assistant_turn_stopped (interrupted = True )
10361040 await self .reset ()
10371041
10381042 async def _handle_end_or_cancel (self , frame : Frame ):
1039- await self ._trigger_assistant_turn_stopped ()
1043+ await self ._trigger_assistant_turn_stopped (interrupted = isinstance ( frame , CancelFrame ) )
10401044 if self ._summarizer :
10411045 await self ._summarizer .cleanup ()
10421046
@@ -1394,17 +1398,23 @@ async def _trigger_assistant_turn_started(self):
13941398
13951399 await self ._call_event_handler ("on_assistant_turn_started" )
13961400
1397- async def _trigger_assistant_turn_stopped (self ):
1401+ async def _trigger_assistant_turn_stopped (self , * , interrupted : bool = False ):
1402+ if not self ._assistant_turn_start_timestamp :
1403+ return
1404+
13981405 aggregation = await self .push_aggregation ()
13991406 if aggregation :
14001407 # Strip turn completion markers from the transcript
1401- content = self ._maybe_strip_turn_completion_markers (aggregation )
1402- message = AssistantTurnStoppedMessage (
1403- content = content , timestamp = self ._assistant_turn_start_timestamp
1404- )
1405- await self ._call_event_handler ("on_assistant_turn_stopped" , message )
1408+ aggregation = self ._maybe_strip_turn_completion_markers (aggregation )
1409+
1410+ message = AssistantTurnStoppedMessage (
1411+ content = aggregation ,
1412+ interrupted = interrupted ,
1413+ timestamp = self ._assistant_turn_start_timestamp ,
1414+ )
1415+ await self ._call_event_handler ("on_assistant_turn_stopped" , message )
14061416
1407- self ._assistant_turn_start_timestamp = ""
1417+ self ._assistant_turn_start_timestamp = ""
14081418
14091419 def _maybe_strip_turn_completion_markers (self , text : str ) -> str :
14101420 """Strip turn completion markers from assistant transcript.
0 commit comments