3434from nemoguardrails .actions .actions import ActionResult , action
3535from nemoguardrails .actions .llm .utils import (
3636 flow_to_colang ,
37+ get_and_clear_reasoning_trace_contextvar ,
3738 get_first_nonempty_line ,
3839 get_last_bot_intent_event ,
3940 get_last_user_intent_event ,
5152 generation_options_var ,
5253 llm_call_info_var ,
5354 raw_llm_request ,
54- reasoning_trace_var ,
5555 streaming_handler_var ,
5656)
5757from nemoguardrails .embeddings .index import EmbeddingsIndex , IndexItem
@@ -519,6 +519,7 @@ async def generate_user_intent(
519519 )
520520 else :
521521 output_events = []
522+ context_updates = {}
522523
523524 # If we are in passthrough mode, we just use the input for prompting
524525 if self .config .passthrough :
@@ -642,6 +643,13 @@ async def generate_user_intent(
642643 if streaming_handler :
643644 await streaming_handler .push_chunk (text )
644645
646+ reasoning_trace = get_and_clear_reasoning_trace_contextvar ()
647+ if reasoning_trace :
648+ context_updates ["bot_thinking" ] = reasoning_trace
649+ output_events .append (
650+ new_event_dict ("BotThinking" , content = reasoning_trace )
651+ )
652+
645653 if self .config .passthrough :
646654 from nemoguardrails .actions .llm .utils import (
647655 get_and_clear_tool_calls_contextvar ,
@@ -658,7 +666,7 @@ async def generate_user_intent(
658666 else :
659667 output_events .append (new_event_dict ("BotMessage" , text = text ))
660668
661- return ActionResult (events = output_events )
669+ return ActionResult (events = output_events , context_updates = context_updates )
662670
663671 async def _search_flows_index (self , text , max_results ):
664672 """Search the index of flows."""
@@ -949,16 +957,37 @@ async def generate_bot_message(
949957 '"\n ' ,
950958 ]
951959 text = await _streaming_handler .wait ()
952- return ActionResult (
953- events = [new_event_dict ("BotMessage" , text = text )]
960+
961+ output_events = []
962+ reasoning_trace = get_and_clear_reasoning_trace_contextvar ()
963+ if reasoning_trace :
964+ output_events .append (
965+ new_event_dict (
966+ "BotThinking" , content = reasoning_trace
967+ )
968+ )
969+ output_events .append (
970+ new_event_dict ("BotMessage" , text = text )
954971 )
972+
973+ return ActionResult (events = output_events )
955974 else :
956975 if streaming_handler :
957976 await streaming_handler .push_chunk (
958977 bot_message_event ["text" ]
959978 )
960979
961- return ActionResult (events = [bot_message_event ])
980+ output_events = []
981+ reasoning_trace = get_and_clear_reasoning_trace_contextvar ()
982+ if reasoning_trace :
983+ output_events .append (
984+ new_event_dict (
985+ "BotThinking" , content = reasoning_trace
986+ )
987+ )
988+ output_events .append (bot_message_event )
989+
990+ return ActionResult (events = output_events )
962991
963992 # If we are in passthrough mode, we just use the input for prompting
964993 if self .config .passthrough :
@@ -1117,8 +1146,17 @@ async def generate_bot_message(
11171146 if streaming_handler :
11181147 await streaming_handler .push_chunk (bot_utterance )
11191148
1149+ output_events = []
1150+ reasoning_trace = get_and_clear_reasoning_trace_contextvar ()
1151+ if reasoning_trace :
1152+ context_updates ["bot_thinking" ] = reasoning_trace
1153+ output_events .append (
1154+ new_event_dict ("BotThinking" , content = reasoning_trace )
1155+ )
1156+ output_events .append (new_event_dict ("BotMessage" , text = bot_utterance ))
1157+
11201158 return ActionResult (
1121- events = [ new_event_dict ( "BotMessage" , text = bot_utterance )] ,
1159+ events = output_events ,
11221160 context_updates = context_updates ,
11231161 )
11241162 else :
@@ -1127,8 +1165,17 @@ async def generate_bot_message(
11271165 if streaming_handler :
11281166 await streaming_handler .push_chunk (bot_utterance )
11291167
1168+ output_events = []
1169+ reasoning_trace = get_and_clear_reasoning_trace_contextvar ()
1170+ if reasoning_trace :
1171+ context_updates ["bot_thinking" ] = reasoning_trace
1172+ output_events .append (
1173+ new_event_dict ("BotThinking" , content = reasoning_trace )
1174+ )
1175+ output_events .append (new_event_dict ("BotMessage" , text = bot_utterance ))
1176+
11301177 return ActionResult (
1131- events = [ new_event_dict ( "BotMessage" , text = bot_utterance )] ,
1178+ events = output_events ,
11321179 context_updates = context_updates ,
11331180 )
11341181
0 commit comments