Skip to content

Commit dd6881a

Browse files
moonbox3Copilot
andcommitted
Extract function_approval_response from workflow messages (microsoft#4546)
_extract_responses_from_messages now handles function_approval_response content in addition to function_result content. Previously, approval responses sent via the messages field were silently dropped because the function only checked for content.type == "function_result". The approval response is keyed by content.id and includes the approved status, id, and serialized function_call — consistent with how _coerce_content identifies approval response payloads. Co-authored-by: Copilot <[email protected]>
1 parent 1ca43f9 commit dd6881a

2 files changed

Lines changed: 89 additions & 4 deletions

File tree

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,18 @@ def _extract_responses_from_messages(messages: list[Message]) -> dict[str, Any]:
128128
responses: dict[str, Any] = {}
129129
for message in messages:
130130
for content in message.contents:
131-
if content.type != "function_result" or not content.call_id:
132-
continue
133-
value = _coerce_json_value(content.result)
134-
responses[str(content.call_id)] = value
131+
if content.type == "function_result" and content.call_id:
132+
value = _coerce_json_value(content.result)
133+
responses[str(content.call_id)] = value
134+
elif content.type == "function_approval_response" and getattr(content, "id", None):
135+
approval_value: dict[str, Any] = {
136+
"approved": getattr(content, "approved", False),
137+
"id": str(content.id), # type: ignore[union-attr]
138+
}
139+
func_call = getattr(content, "function_call", None)
140+
if func_call is not None:
141+
approval_value["function_call"] = make_json_safe(func_call.to_dict())
142+
responses[str(content.id)] = approval_value # type: ignore[union-attr]
135143
return responses
136144

137145

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

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
_custom_event_value,
3434
_details_code,
3535
_details_message,
36+
_extract_responses_from_messages,
3637
_interrupt_entry_for_request_event,
3738
_latest_assistant_contents,
3839
_latest_user_text,
@@ -1172,6 +1173,82 @@ def test_details_without_error_type(self):
11721173
assert _details_code(details) is None
11731174

11741175

1176+
class TestExtractResponsesFromMessages:
1177+
"""Tests for _extract_responses_from_messages helper."""
1178+
1179+
def test_function_result_extracted(self):
1180+
"""function_result content is extracted keyed by call_id."""
1181+
result = Content.from_function_result(call_id="call-1", result="ok")
1182+
messages = [Message(role="tool", contents=[result])]
1183+
responses = _extract_responses_from_messages(messages)
1184+
assert responses == {"call-1": "ok"}
1185+
1186+
def test_function_result_without_call_id_skipped(self):
1187+
"""function_result with no call_id is ignored."""
1188+
result = Content.from_function_result(call_id="", result="ok")
1189+
messages = [Message(role="tool", contents=[result])]
1190+
responses = _extract_responses_from_messages(messages)
1191+
assert responses == {}
1192+
1193+
def test_function_approval_response_extracted(self):
1194+
"""function_approval_response content is extracted keyed by id."""
1195+
func_call = Content.from_function_call(
1196+
call_id="call-1", name="do_action", arguments={"x": 1},
1197+
)
1198+
approval = Content.from_function_approval_response(
1199+
approved=True, id="approval-1", function_call=func_call,
1200+
)
1201+
messages = [Message(role="user", contents=[approval])]
1202+
responses = _extract_responses_from_messages(messages)
1203+
assert "approval-1" in responses
1204+
assert responses["approval-1"]["approved"] is True
1205+
assert responses["approval-1"]["id"] == "approval-1"
1206+
assert "function_call" in responses["approval-1"]
1207+
1208+
def test_denied_approval_response_extracted(self):
1209+
"""Denied function_approval_response is extracted with approved=False."""
1210+
func_call = Content.from_function_call(
1211+
call_id="call-2", name="delete_item", arguments={},
1212+
)
1213+
approval = Content.from_function_approval_response(
1214+
approved=False, id="approval-2", function_call=func_call,
1215+
)
1216+
messages = [Message(role="user", contents=[approval])]
1217+
responses = _extract_responses_from_messages(messages)
1218+
assert "approval-2" in responses
1219+
assert responses["approval-2"]["approved"] is False
1220+
1221+
def test_mixed_result_and_approval(self):
1222+
"""Both function_result and function_approval_response are extracted."""
1223+
result = Content.from_function_result(call_id="call-1", result="done")
1224+
func_call = Content.from_function_call(
1225+
call_id="call-2", name="submit", arguments={},
1226+
)
1227+
approval = Content.from_function_approval_response(
1228+
approved=True, id="approval-1", function_call=func_call,
1229+
)
1230+
messages = [
1231+
Message(role="tool", contents=[result]),
1232+
Message(role="user", contents=[approval]),
1233+
]
1234+
responses = _extract_responses_from_messages(messages)
1235+
assert "call-1" in responses
1236+
assert responses["call-1"] == "done"
1237+
assert "approval-1" in responses
1238+
assert responses["approval-1"]["approved"] is True
1239+
1240+
def test_text_content_skipped(self):
1241+
"""Non-result, non-approval content is ignored."""
1242+
text = Content.from_text(text="hello")
1243+
messages = [Message(role="user", contents=[text])]
1244+
responses = _extract_responses_from_messages(messages)
1245+
assert responses == {}
1246+
1247+
def test_empty_messages(self):
1248+
"""Empty message list returns empty responses."""
1249+
assert _extract_responses_from_messages([]) == {}
1250+
1251+
11751252
# ── Stream integration tests ──
11761253

11771254

0 commit comments

Comments
 (0)