77import json
88from typing import Any
99
10- from agent_framework import AgentResponseUpdate , Content
10+ from agent_framework import AgentResponseUpdate , Content , FunctionTool
1111from conftest import StubAgent
1212
1313from agent_framework_ag_ui ._agent import AgentConfig
1414from agent_framework_ag_ui ._agent_run import run_agent_stream
1515
1616
17+ def _make_weather_tool () -> FunctionTool :
18+ """Create a real executable weather tool with approval_mode='always_require'."""
19+
20+ def get_weather (city : str ) -> str :
21+ return f"Sunny in { city } "
22+
23+ return FunctionTool (
24+ name = "get_weather" ,
25+ description = "Get the weather for a city" ,
26+ func = get_weather ,
27+ approval_mode = "always_require" ,
28+ )
29+
30+
1731async def test_approval_resume_emits_tool_call_result () -> None :
1832 """After approving a tool call, the resume stream should contain a TOOL_CALL_RESULT event.
1933
@@ -23,9 +37,11 @@ async def test_approval_resume_emits_tool_call_result() -> None:
2337 """
2438 tool_name = "get_weather"
2539 call_id = "call_abc123"
40+ weather_tool = _make_weather_tool ()
2641
2742 agent = StubAgent (
28- updates = [AgentResponseUpdate (contents = [Content .from_text (text = "The weather is sunny." )], role = "assistant" )]
43+ updates = [AgentResponseUpdate (contents = [Content .from_text (text = "The weather is sunny." )], role = "assistant" )],
44+ default_options = {"tools" : [weather_tool ]},
2945 )
3046 config = AgentConfig ()
3147
@@ -53,21 +69,10 @@ async def test_approval_resume_emits_tool_call_result() -> None:
5369 },
5470 ]
5571
56- input_data = {
72+ input_data : dict [ str , Any ] = {
5773 "thread_id" : "thread-approval-result" ,
5874 "run_id" : "run-resume" ,
5975 "messages" : resume_messages ,
60- "tools" : [
61- {
62- "name" : tool_name ,
63- "description" : "Get the weather for a city" ,
64- "parameters" : {
65- "type" : "object" ,
66- "properties" : {"city" : {"type" : "string" }},
67- "required" : ["city" ],
68- },
69- }
70- ],
7176 }
7277
7378 events : list [Any ] = []
@@ -91,14 +96,20 @@ async def test_approval_resume_emits_tool_call_result() -> None:
9196 assert result_event .tool_call_id == call_id , (
9297 f"Expected TOOL_CALL_RESULT with tool_call_id={ call_id } , got tool_call_id={ result_event .tool_call_id } "
9398 )
99+ # Verify the result contains the actual tool execution output
100+ assert result_event .content == "Sunny in Seattle"
94101
95102
96103async def test_approval_resume_result_has_content () -> None :
97104 """TOOL_CALL_RESULT event from an approved tool should contain the execution result."""
98105 tool_name = "get_weather"
99106 call_id = "call_content_check"
107+ weather_tool = _make_weather_tool ()
100108
101- agent = StubAgent (updates = [AgentResponseUpdate (contents = [Content .from_text (text = "Done." )], role = "assistant" )])
109+ agent = StubAgent (
110+ updates = [AgentResponseUpdate (contents = [Content .from_text (text = "Done." )], role = "assistant" )],
111+ default_options = {"tools" : [weather_tool ]},
112+ )
102113 config = AgentConfig ()
103114
104115 resume_messages : list [dict [str , Any ]] = [
@@ -124,21 +135,10 @@ async def test_approval_resume_result_has_content() -> None:
124135 },
125136 ]
126137
127- input_data = {
138+ input_data : dict [ str , Any ] = {
128139 "thread_id" : "thread-result-content" ,
129140 "run_id" : "run-resume-2" ,
130141 "messages" : resume_messages ,
131- "tools" : [
132- {
133- "name" : tool_name ,
134- "description" : "Get the weather for a city" ,
135- "parameters" : {
136- "type" : "object" ,
137- "properties" : {"city" : {"type" : "string" }},
138- "required" : ["city" ],
139- },
140- }
141- ],
142142 }
143143
144144 events : list [Any ] = []
@@ -151,16 +151,16 @@ async def test_approval_resume_result_has_content() -> None:
151151 result_event = tool_result_events [0 ]
152152 assert result_event .tool_call_id == call_id
153153 assert result_event .role == "tool"
154- # The result content should be a non-empty string (actual content depends on tool execution )
155- assert isinstance ( result_event .content , str )
154+ # Verify the result contains the actual tool execution output (string returned directly )
155+ assert result_event .content == "Sunny in Portland"
156156
157157
158158async def test_no_approval_no_extra_tool_result () -> None :
159159 """When no approval response is present, no extra TOOL_CALL_RESULT events should be emitted."""
160160 agent = StubAgent (updates = [AgentResponseUpdate (contents = [Content .from_text (text = "Hello." )], role = "assistant" )])
161161 config = AgentConfig ()
162162
163- input_data = {
163+ input_data : dict [ str , Any ] = {
164164 "thread_id" : "thread-no-approval" ,
165165 "run_id" : "run-normal" ,
166166 "messages" : [{"role" : "user" , "content" : "Hi" }],
@@ -172,3 +172,54 @@ async def test_no_approval_no_extra_tool_result() -> None:
172172
173173 tool_result_events = [e for e in events if getattr (e , "type" , None ) == "TOOL_CALL_RESULT" ]
174174 assert len (tool_result_events ) == 0 , f"Unexpected TOOL_CALL_RESULT events: { tool_result_events } "
175+
176+
177+ async def test_rejection_does_not_emit_tool_call_result () -> None :
178+ """Rejected tool calls should not produce TOOL_CALL_RESULT events."""
179+ tool_name = "get_weather"
180+ call_id = "call_rejected"
181+ weather_tool = _make_weather_tool ()
182+
183+ agent = StubAgent (
184+ updates = [AgentResponseUpdate (contents = [Content .from_text (text = "OK, I won't check." )], role = "assistant" )],
185+ default_options = {"tools" : [weather_tool ]},
186+ )
187+ config = AgentConfig ()
188+
189+ resume_messages : list [dict [str , Any ]] = [
190+ {"role" : "user" , "content" : "What's the weather?" },
191+ {
192+ "role" : "assistant" ,
193+ "content" : "" ,
194+ "tool_calls" : [
195+ {
196+ "id" : call_id ,
197+ "type" : "function" ,
198+ "function" : {
199+ "name" : tool_name ,
200+ "arguments" : json .dumps ({"city" : "Denver" }),
201+ },
202+ }
203+ ],
204+ },
205+ {
206+ "role" : "tool" ,
207+ "content" : json .dumps ({"accepted" : False }),
208+ "toolCallId" : call_id ,
209+ },
210+ ]
211+
212+ input_data : dict [str , Any ] = {
213+ "thread_id" : "thread-rejection" ,
214+ "run_id" : "run-rejected" ,
215+ "messages" : resume_messages ,
216+ }
217+
218+ events : list [Any ] = []
219+ async for event in run_agent_stream (input_data , agent , config ):
220+ events .append (event )
221+
222+ tool_result_events = [e for e in events if getattr (e , "type" , None ) == "TOOL_CALL_RESULT" ]
223+ assert len (tool_result_events ) == 0 , (
224+ f"Expected no TOOL_CALL_RESULT for rejected tool, got { len (tool_result_events )} "
225+ )
0 commit comments