@@ -223,3 +223,232 @@ async def test_rejection_does_not_emit_tool_call_result() -> None:
223223 assert len (tool_result_events ) == 0 , (
224224 f"Expected no TOOL_CALL_RESULT for rejected tool, got { len (tool_result_events )} "
225225 )
226+
227+
228+ def _make_temperature_tool () -> FunctionTool :
229+ """Create a real executable temperature tool with approval_mode='always_require'."""
230+
231+ def get_temperature (city : str ) -> str :
232+ return f"72F in { city } "
233+
234+ return FunctionTool (
235+ name = "get_temperature" ,
236+ description = "Get the temperature for a city" ,
237+ func = get_temperature ,
238+ approval_mode = "always_require" ,
239+ )
240+
241+
242+ async def test_mixed_approve_reject_emits_only_approved_tool_result () -> None :
243+ """When one tool call is approved and another rejected, only the approved one produces a TOOL_CALL_RESULT event."""
244+ weather_tool = _make_weather_tool ()
245+ temperature_tool = _make_temperature_tool ()
246+ approved_call_id = "call_approved"
247+ rejected_call_id = "call_rejected"
248+
249+ agent = StubAgent (
250+ updates = [AgentResponseUpdate (contents = [Content .from_text (text = "Here are the results." )], role = "assistant" )],
251+ default_options = {"tools" : [weather_tool , temperature_tool ]},
252+ )
253+ config = AgentConfig ()
254+
255+ resume_messages : list [dict [str , Any ]] = [
256+ {"role" : "user" , "content" : "Weather and temperature in Seattle?" },
257+ {
258+ "role" : "assistant" ,
259+ "content" : "" ,
260+ "tool_calls" : [
261+ {
262+ "id" : approved_call_id ,
263+ "type" : "function" ,
264+ "function" : {
265+ "name" : "get_weather" ,
266+ "arguments" : json .dumps ({"city" : "Seattle" }),
267+ },
268+ },
269+ {
270+ "id" : rejected_call_id ,
271+ "type" : "function" ,
272+ "function" : {
273+ "name" : "get_temperature" ,
274+ "arguments" : json .dumps ({"city" : "Seattle" }),
275+ },
276+ },
277+ ],
278+ },
279+ {
280+ "role" : "tool" ,
281+ "content" : json .dumps ({"accepted" : True }),
282+ "toolCallId" : approved_call_id ,
283+ },
284+ {
285+ "role" : "tool" ,
286+ "content" : json .dumps ({"accepted" : False }),
287+ "toolCallId" : rejected_call_id ,
288+ },
289+ ]
290+
291+ input_data : dict [str , Any ] = {
292+ "thread_id" : "thread-mixed" ,
293+ "run_id" : "run-mixed" ,
294+ "messages" : resume_messages ,
295+ }
296+
297+ events : list [Any ] = []
298+ async for event in run_agent_stream (input_data , agent , config ):
299+ events .append (event )
300+
301+ tool_result_events = [e for e in events if getattr (e , "type" , None ) == "TOOL_CALL_RESULT" ]
302+
303+ # Only the approved tool call should produce a TOOL_CALL_RESULT event
304+ assert len (tool_result_events ) == 1 , (
305+ f"Expected exactly 1 TOOL_CALL_RESULT (approved only), got { len (tool_result_events )} "
306+ )
307+ assert tool_result_events [0 ].tool_call_id == approved_call_id
308+ assert tool_result_events [0 ].content == "Sunny in Seattle"
309+
310+
311+ async def test_approval_resume_zero_updates_emits_tool_result () -> None :
312+ """When the agent produces zero updates, TOOL_CALL_RESULT events should still be emitted via the fallback path."""
313+ tool_name = "get_weather"
314+ call_id = "call_zero_updates"
315+ weather_tool = _make_weather_tool ()
316+
317+ agent = StubAgent (
318+ updates = [],
319+ default_options = {"tools" : [weather_tool ]},
320+ )
321+ config = AgentConfig ()
322+
323+ resume_messages : list [dict [str , Any ]] = [
324+ {"role" : "user" , "content" : "What's the weather?" },
325+ {
326+ "role" : "assistant" ,
327+ "content" : "" ,
328+ "tool_calls" : [
329+ {
330+ "id" : call_id ,
331+ "type" : "function" ,
332+ "function" : {
333+ "name" : tool_name ,
334+ "arguments" : json .dumps ({"city" : "Boston" }),
335+ },
336+ }
337+ ],
338+ },
339+ {
340+ "role" : "tool" ,
341+ "content" : json .dumps ({"accepted" : True }),
342+ "toolCallId" : call_id ,
343+ },
344+ ]
345+
346+ input_data : dict [str , Any ] = {
347+ "thread_id" : "thread-zero-updates" ,
348+ "run_id" : "run-zero-updates" ,
349+ "messages" : resume_messages ,
350+ }
351+
352+ events : list [Any ] = []
353+ async for event in run_agent_stream (input_data , agent , config ):
354+ events .append (event )
355+
356+ event_types = [getattr (e , "type" , None ) for e in events ]
357+ assert "RUN_STARTED" in event_types
358+
359+ tool_result_events = [e for e in events if getattr (e , "type" , None ) == "TOOL_CALL_RESULT" ]
360+ assert len (tool_result_events ) == 1 , (
361+ f"Expected 1 TOOL_CALL_RESULT in zero-updates fallback path, got { len (tool_result_events )} "
362+ )
363+ assert tool_result_events [0 ].tool_call_id == call_id
364+ assert tool_result_events [0 ].content == "Sunny in Boston"
365+
366+
367+ async def test_resolve_approval_responses_returns_only_approved () -> None :
368+ """_resolve_approval_responses should return only approved results; rejection results go into messages only."""
369+ from agent_framework import Message
370+ from agent_framework_ag_ui ._agent_run import _resolve_approval_responses
371+
372+ weather_tool = _make_weather_tool ()
373+ temperature_tool = _make_temperature_tool ()
374+ approved_call_id = "call_a"
375+ rejected_call_id = "call_r"
376+
377+ messages : list [Any ] = [
378+ Message (role = "user" , contents = [Content .from_text (text = "Hi" )]),
379+ Message (
380+ role = "assistant" ,
381+ contents = [
382+ Content (
383+ type = "function_approval_request" ,
384+ id = approved_call_id ,
385+ function_call = Content (
386+ type = "function_call" ,
387+ name = "get_weather" ,
388+ call_id = approved_call_id ,
389+ arguments = '{"city": "NYC"}' ,
390+ ),
391+ ),
392+ Content (
393+ type = "function_approval_request" ,
394+ id = rejected_call_id ,
395+ function_call = Content (
396+ type = "function_call" ,
397+ name = "get_temperature" ,
398+ call_id = rejected_call_id ,
399+ arguments = '{"city": "NYC"}' ,
400+ ),
401+ ),
402+ ],
403+ ),
404+ Message (
405+ role = "user" ,
406+ contents = [
407+ Content (
408+ type = "function_approval_response" ,
409+ id = approved_call_id ,
410+ approved = True ,
411+ function_call = Content (
412+ type = "function_call" ,
413+ name = "get_weather" ,
414+ call_id = approved_call_id ,
415+ arguments = '{"city": "NYC"}' ,
416+ ),
417+ ),
418+ Content (
419+ type = "function_approval_response" ,
420+ id = rejected_call_id ,
421+ approved = False ,
422+ function_call = Content (
423+ type = "function_call" ,
424+ name = "get_temperature" ,
425+ call_id = rejected_call_id ,
426+ arguments = '{"city": "NYC"}' ,
427+ ),
428+ ),
429+ ],
430+ ),
431+ ]
432+
433+ agent = StubAgent (
434+ updates = [],
435+ default_options = {"tools" : [weather_tool , temperature_tool ]},
436+ )
437+
438+ results = await _resolve_approval_responses (
439+ messages , [weather_tool , temperature_tool ], agent , {}
440+ )
441+
442+ # Return value should only contain approved results
443+ assert len (results ) == 1
444+ assert results [0 ].call_id == approved_call_id
445+ assert results [0 ].type == "function_result"
446+
447+ # Rejection result should be written into messages (by _replace_approval_contents_with_results)
448+ all_contents = [c for msg in messages for c in msg .contents ]
449+ rejection_results = [
450+ c for c in all_contents
451+ if c .type == "function_result" and c .call_id == rejected_call_id
452+ ]
453+ assert len (rejection_results ) == 1
454+ assert "rejected" in str (rejection_results [0 ].result ).lower ()
0 commit comments