Skip to content

Commit 1065060

Browse files
CopilotCopilot
andcommitted
fix(ag-ui): address PR review feedback for microsoft#4589
1. _resolve_approval_responses now returns only approved results (not rejections) so TOOL_CALL_RESULT events are emitted only for executed tools. Rejection results are still written into message history. 2. Emit resolved TOOL_CALL_RESULT events in the no-updates fallback RUN_STARTED path so approval results are never lost. 3. Rewrite tests to use real FunctionTool with func and approval_mode='always_require' via StubAgent default_options, verifying actual tool execution output in TOOL_CALL_RESULT content. Added test for rejection not emitting TOOL_CALL_RESULT. Co-authored-by: Copilot <[email protected]>
1 parent d727c12 commit 1065060

2 files changed

Lines changed: 106 additions & 40 deletions

File tree

‎python/packages/ag-ui/agent_framework_ag_ui/_agent_run.py‎

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,10 @@ async def _resolve_approval_responses(
410410
thread_id: The conversation thread ID used to scope registry keys.
411411
412412
Returns:
413-
List of resolved function_result Content objects (empty if no approvals).
413+
List of approved function_result Content objects only (empty if no
414+
approvals). Rejection results are written into the message history
415+
but are *not* included in the return value because they should not
416+
be emitted as TOOL_CALL_RESULT events.
414417
"""
415418
fcc_todo = _collect_approval_responses(messages)
416419
if not fcc_todo:
@@ -497,39 +500,41 @@ async def _resolve_approval_responses(
497500
logger.exception("Failed to execute approved tool calls; injecting error results: %s", e)
498501
approved_function_results = []
499502

500-
# Build normalized results for approved responses
501-
normalized_results: list[Content] = []
503+
# Build results for approved responses (used for TOOL_CALL_RESULT event emission)
504+
approved_results: list[Content] = []
502505
for idx, approval in enumerate(approved_responses):
503506
if (
504507
idx < len(approved_function_results)
505508
and getattr(approved_function_results[idx], "type", None) == "function_result"
506509
):
507-
normalized_results.append(approved_function_results[idx])
510+
approved_results.append(approved_function_results[idx])
508511
continue
509512
# Get call_id from function_call if present, otherwise use approval.id
510513
func_call = approval.function_call
511514
call_id = (func_call.call_id if func_call else None) or approval.id or ""
512-
normalized_results.append(
515+
approved_results.append(
513516
Content.from_function_result(call_id=call_id, result="Error: Tool call invocation failed.")
514517
)
515518

516-
# Build rejection results
519+
# Build rejection results (included in message history but not emitted as events)
520+
rejection_results: list[Content] = []
517521
for rejection in rejected_responses:
518522
func_call = rejection.function_call
519523
call_id = (func_call.call_id if func_call else None) or rejection.id or ""
520-
normalized_results.append(
524+
rejection_results.append(
521525
Content.from_function_result(call_id=call_id, result="Error: Tool call invocation was rejected by user.")
522526
)
523527

524-
_replace_approval_contents_with_results(messages, fcc_todo, normalized_results) # type: ignore
528+
all_results = approved_results + rejection_results
529+
_replace_approval_contents_with_results(messages, fcc_todo, all_results) # type: ignore
525530

526531
# Post-process: Convert user messages with function_result content to proper tool messages.
527532
# After _replace_approval_contents_with_results, approved tool calls have their results
528533
# placed in user messages. OpenAI requires tool results to be in role="tool" messages.
529534
# This transformation ensures the message history is valid for the LLM provider.
530535
_convert_approval_results_to_tool_messages(messages)
531536

532-
return normalized_results
537+
return approved_results
533538

534539

535540
def _convert_approval_results_to_tool_messages(messages: list[Message]) -> None:
@@ -925,7 +930,17 @@ async def run_agent_stream(
925930
if state_schema and flow.current_state:
926931
yield StateSnapshotEvent(snapshot=flow.current_state)
927932

928-
# Process structured output if response_format is set
933+
# Emit TOOL_CALL_RESULT events for tools executed during approval resolution
934+
for resolved in resolved_approval_results:
935+
if resolved.call_id:
936+
raw = resolved.result if resolved.result is not None else ""
937+
result_str = raw if isinstance(raw, str) else json.dumps(make_json_safe(raw))
938+
yield ToolCallResultEvent(
939+
message_id=generate_event_id(),
940+
tool_call_id=resolved.call_id,
941+
content=result_str,
942+
role="tool",
943+
)
929944
if response_format is not None and all_updates:
930945
from agent_framework import AgentResponse
931946
from pydantic import BaseModel

‎python/packages/ag-ui/tests/ag_ui/test_approval_result_event.py‎

Lines changed: 81 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,27 @@
77
import json
88
from typing import Any
99

10-
from agent_framework import AgentResponseUpdate, Content
10+
from agent_framework import AgentResponseUpdate, Content, FunctionTool
1111
from conftest import StubAgent
1212

1313
from agent_framework_ag_ui._agent import AgentConfig
1414
from 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+
1731
async 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

96103
async 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

158158
async 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

Comments
 (0)