@@ -106,7 +106,6 @@ async def _handle_stream_events(self, input: RunAgentInput) -> AsyncGenerator[st
106
106
"thinking_process" : None ,
107
107
}
108
108
109
- messages = input .messages or []
110
109
forwarded_props = input .forwarded_props
111
110
node_name_input = forwarded_props .get ('node_name' , None ) if forwarded_props else None
112
111
@@ -120,30 +119,13 @@ async def _handle_stream_events(self, input: RunAgentInput) -> AsyncGenerator[st
120
119
121
120
agent_state = await self .graph .aget_state (config )
122
121
self .active_run ["mode" ] = "continue" if thread_id and self .active_run .get ("node_name" ) != "__end__" and self .active_run .get ("node_name" ) else "start"
122
+
123
123
prepared_stream_response = await self .prepare_stream (input = input , agent_state = agent_state , config = config )
124
124
125
125
yield self ._dispatch_event (
126
126
RunStartedEvent (type = EventType .RUN_STARTED , thread_id = thread_id , run_id = self .active_run ["id" ])
127
127
)
128
128
129
- langchain_messages = agui_messages_to_langchain (messages )
130
- non_system_messages = [msg for msg in langchain_messages if not isinstance (msg , SystemMessage )]
131
-
132
- if len (agent_state .values .get ("messages" , [])) > len (non_system_messages ):
133
- # Find the last user message by working backwards from the last message
134
- last_user_message = None
135
- for i in range (len (langchain_messages ) - 1 , - 1 , - 1 ):
136
- if isinstance (langchain_messages [i ], HumanMessage ):
137
- last_user_message = langchain_messages [i ]
138
- break
139
-
140
- if last_user_message :
141
- prepared_stream_response = await self .prepare_regenerate_stream (
142
- input = input ,
143
- message_checkpoint = last_user_message ,
144
- config = config
145
- )
146
-
147
129
state = prepared_stream_response ["state" ]
148
130
stream = prepared_stream_response ["stream" ]
149
131
config = prepared_stream_response ["config" ]
@@ -274,14 +256,31 @@ async def prepare_stream(self, input: RunAgentInput, agent_state: State, config:
274
256
thread_id = input .thread_id
275
257
276
258
state_input ["messages" ] = agent_state .values .get ("messages" , [])
277
- self .active_run ["current_graph_state" ] = agent_state .values
259
+ self .active_run ["current_graph_state" ] = agent_state .values . copy ()
278
260
langchain_messages = agui_messages_to_langchain (messages )
279
261
state = self .langgraph_default_merge_state (state_input , langchain_messages , tools )
280
262
self .active_run ["current_graph_state" ].update (state )
281
263
config ["configurable" ]["thread_id" ] = thread_id
282
264
interrupts = agent_state .tasks [0 ].interrupts if agent_state .tasks and len (agent_state .tasks ) > 0 else []
283
265
has_active_interrupts = len (interrupts ) > 0
284
266
resume_input = forwarded_props .get ('command' , {}).get ('resume' , None )
267
+ self .active_run ["schema_keys" ] = self .get_schema_keys (config )
268
+
269
+ non_system_messages = [msg for msg in langchain_messages if not isinstance (msg , SystemMessage )]
270
+ if len (agent_state .values .get ("messages" , [])) > len (non_system_messages ):
271
+ # Find the last user message by working backwards from the last message
272
+ last_user_message = None
273
+ for i in range (len (langchain_messages ) - 1 , - 1 , - 1 ):
274
+ if isinstance (langchain_messages [i ], HumanMessage ):
275
+ last_user_message = langchain_messages [i ]
276
+ break
277
+
278
+ if last_user_message :
279
+ return await self .prepare_regenerate_stream (
280
+ input = input ,
281
+ message_checkpoint = last_user_message ,
282
+ config = config
283
+ )
285
284
286
285
events_to_dispatch = []
287
286
if has_active_interrupts and not resume_input :
@@ -312,8 +311,6 @@ async def prepare_stream(self, input: RunAgentInput, agent_state: State, config:
312
311
if self .active_run ["mode" ] == "continue" :
313
312
await self .graph .aupdate_state (config , state , as_node = self .active_run .get ("node_name" ))
314
313
315
- self .active_run ["schema_keys" ] = self .get_schema_keys (config )
316
-
317
314
if resume_input :
318
315
stream_input = Command (resume = resume_input )
319
316
else :
0 commit comments