@@ -275,3 +275,134 @@ def web_search(query: str) -> str:
275275 assert result_full_history ["messages" ][17 ] == math_agent_messages [2 ]
276276 # final supervisor message
277277 assert result_full_history ["messages" ][- 1 ] == supervisor_messages [- 1 ]
278+
279+
280+ def test_supervisor_message_forwarding ():
281+ """Test that the supervisor forwards a message to a specific agent and receives the correct response."""
282+
283+ @tool
284+ def echo_tool (text : str ) -> str :
285+ """Echo the input text."""
286+ return text
287+
288+ # Agent that simply echoes the message
289+ echo_model = FakeChatModel (
290+ responses = [
291+ AIMessage (content = "Echo: test forwarding!" ),
292+ ]
293+ )
294+ echo_agent = create_react_agent (
295+ model = echo_model .bind_tools ([echo_tool ]),
296+ tools = [echo_tool ],
297+ name = "echo_agent" ,
298+ )
299+
300+ supervisor_messages = [
301+ AIMessage (
302+ content = "" ,
303+ tool_calls = [
304+ {
305+ "name" : "transfer_to_echo_agent" ,
306+ "args" : {},
307+ "id" : "call_gyQSgJQm5jJtPcF5ITe8GGGF" ,
308+ "type" : "tool_call" ,
309+ }
310+ ],
311+ ),
312+ AIMessage (
313+ content = "" ,
314+ tool_calls = [
315+ {
316+ "name" : "forward_message" ,
317+ "args" : {"from_agent" : "echo_agent" },
318+ "id" : "abcd123" ,
319+ "type" : "tool_call" ,
320+ }
321+ ],
322+ ),
323+ ]
324+
325+ workflow = create_supervisor (
326+ [echo_agent ],
327+ model = FakeChatModel (responses = supervisor_messages ),
328+ enable_forwarding = True ,
329+ )
330+ app = workflow .compile ()
331+
332+ result = app .invoke ({"messages" : [HumanMessage (content = "Scooby-dooby-doo" )]})
333+
334+ def get_tool_calls (msg ):
335+ tool_calls = getattr (msg , "tool_calls" , None )
336+ if tool_calls is None :
337+ return None
338+ return [
339+ {"name" : tc ["name" ], "args" : tc ["args" ]}
340+ for tc in tool_calls
341+ if tc ["type" ] == "tool_call"
342+ ]
343+
344+ received = [
345+ {
346+ "name" : msg .name ,
347+ "content" : msg .content ,
348+ "tool_calls" : get_tool_calls (msg ),
349+ "type" : msg .type ,
350+ }
351+ for msg in result ["messages" ]
352+ ]
353+
354+ expected = [
355+ {
356+ "name" : None ,
357+ "content" : "Scooby-dooby-doo" ,
358+ "tool_calls" : None ,
359+ "type" : "human" ,
360+ },
361+ {
362+ "name" : "supervisor" ,
363+ "content" : "" ,
364+ "tool_calls" : [
365+ {
366+ "name" : "transfer_to_echo_agent" ,
367+ "args" : {},
368+ }
369+ ],
370+ "type" : "ai" ,
371+ },
372+ {
373+ "name" : "transfer_to_echo_agent" ,
374+ "content" : "Successfully transferred to echo_agent" ,
375+ "tool_calls" : None ,
376+ "type" : "tool" ,
377+ },
378+ {
379+ "name" : "echo_agent" ,
380+ "content" : "Echo: test forwarding!" ,
381+ "tool_calls" : [],
382+ "type" : "ai" ,
383+ },
384+ {
385+ "name" : "echo_agent" ,
386+ "content" : "Transferring back to supervisor" ,
387+ "tool_calls" : [
388+ {
389+ "name" : "transfer_back_to_supervisor" ,
390+ "args" : {},
391+ }
392+ ],
393+ "type" : "ai" ,
394+ },
395+ {
396+ "name" : "transfer_back_to_supervisor" ,
397+ "content" : "Successfully transferred back to supervisor" ,
398+ "tool_calls" : None ,
399+ "type" : "tool" ,
400+ },
401+ {
402+ "name" : "supervisor" ,
403+ "content" : "Echo: test forwarding!" ,
404+ "tool_calls" : [],
405+ "type" : "ai" ,
406+ },
407+ ]
408+ assert received == expected
0 commit comments