Skip to content

Commit 193429d

Browse files
moonbox3Copilot
andcommitted
Fix microsoft#4546: Update docstring and add integration tests for message-based approvals
- Update _extract_responses_from_messages docstring to reflect that it now handles function_approval_response content in addition to function_result content. - Add integration tests for run_workflow_stream across two turns with approval responses provided via messages (function_approvals) rather than resume.interrupts, covering both approved and denied scenarios. Co-authored-by: Copilot <[email protected]>
1 parent 2095d79 commit 193429d

2 files changed

Lines changed: 143 additions & 1 deletion

File tree

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,13 @@ def _request_payload_from_request_event(request_event: Any) -> dict[str, Any] |
124124

125125

126126
def _extract_responses_from_messages(messages: list[Message]) -> dict[str, Any]:
127-
"""Extract request-info responses from incoming tool/function-result messages."""
127+
"""Extract request-info responses from incoming messages.
128+
129+
Handles both ``function_result`` content (keyed by ``call_id``) and
130+
``function_approval_response`` content (keyed by ``id``), so that
131+
approval decisions sent via messages are forwarded into the workflow
132+
responses map.
133+
"""
128134
responses: dict[str, Any] = {}
129135
for message in messages:
130136
for content in message.contents:

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

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,6 +1264,142 @@ def test_empty_messages(self):
12641264
# ── Stream integration tests ──
12651265

12661266

1267+
async def test_workflow_run_approval_via_messages_approved() -> None:
1268+
"""Approval response sent via messages (function_approvals) should satisfy the pending request."""
1269+
1270+
class ApprovalExecutor(Executor):
1271+
def __init__(self) -> None:
1272+
super().__init__(id="approval_executor")
1273+
1274+
@handler
1275+
async def start(self, message: Any, ctx: WorkflowContext) -> None:
1276+
del message
1277+
function_call = Content.from_function_call(
1278+
call_id="refund-call",
1279+
name="submit_refund",
1280+
arguments={"order_id": "12345", "amount": "$89.99"},
1281+
)
1282+
approval_request = Content.from_function_approval_request(id="approval-1", function_call=function_call)
1283+
await ctx.request_info(approval_request, Content, request_id="approval-1")
1284+
1285+
@response_handler
1286+
async def handle_approval(self, original_request: Content, response: Content, ctx: WorkflowContext) -> None:
1287+
del original_request
1288+
status = "approved" if bool(response.approved) else "rejected"
1289+
await ctx.yield_output(f"Refund {status}.")
1290+
1291+
workflow = WorkflowBuilder(start_executor=ApprovalExecutor()).build()
1292+
first_events = [
1293+
event async for event in run_workflow_stream({"messages": [{"role": "user", "content": "go"}]}, workflow)
1294+
]
1295+
first_finished = [event for event in first_events if event.type == "RUN_FINISHED"][0].model_dump()
1296+
interrupt_payload = cast(list[dict[str, Any]], first_finished.get("interrupt"))
1297+
assert isinstance(interrupt_payload, list) and len(interrupt_payload) == 1
1298+
1299+
# Second turn: send approval via function_approvals on a message (not resume.interrupts)
1300+
resumed_events = [
1301+
event
1302+
async for event in run_workflow_stream(
1303+
{
1304+
"messages": [
1305+
{
1306+
"role": "user",
1307+
"content": "",
1308+
"function_approvals": [
1309+
{
1310+
"approved": True,
1311+
"id": "approval-1",
1312+
"call_id": "refund-call",
1313+
"name": "submit_refund",
1314+
"arguments": {"order_id": "12345", "amount": "$89.99"},
1315+
}
1316+
],
1317+
}
1318+
],
1319+
},
1320+
workflow,
1321+
)
1322+
]
1323+
1324+
resumed_types = [event.type for event in resumed_events]
1325+
assert "RUN_STARTED" in resumed_types
1326+
assert "RUN_FINISHED" in resumed_types
1327+
assert "RUN_ERROR" not in resumed_types
1328+
assert "TEXT_MESSAGE_CONTENT" in resumed_types
1329+
text_deltas = [event.delta for event in resumed_events if event.type == "TEXT_MESSAGE_CONTENT"]
1330+
assert any("approved" in delta for delta in text_deltas)
1331+
resumed_finished = [event for event in resumed_events if event.type == "RUN_FINISHED"][0].model_dump()
1332+
assert "interrupt" not in resumed_finished
1333+
1334+
1335+
async def test_workflow_run_approval_via_messages_denied() -> None:
1336+
"""Denied approval response sent via messages (function_approvals) should satisfy the pending request."""
1337+
1338+
class ApprovalExecutor(Executor):
1339+
def __init__(self) -> None:
1340+
super().__init__(id="approval_executor")
1341+
1342+
@handler
1343+
async def start(self, message: Any, ctx: WorkflowContext) -> None:
1344+
del message
1345+
function_call = Content.from_function_call(
1346+
call_id="delete-call",
1347+
name="delete_record",
1348+
arguments={"record_id": "abc"},
1349+
)
1350+
approval_request = Content.from_function_approval_request(id="deny-1", function_call=function_call)
1351+
await ctx.request_info(approval_request, Content, request_id="deny-1")
1352+
1353+
@response_handler
1354+
async def handle_approval(self, original_request: Content, response: Content, ctx: WorkflowContext) -> None:
1355+
del original_request
1356+
status = "approved" if bool(response.approved) else "rejected"
1357+
await ctx.yield_output(f"Delete {status}.")
1358+
1359+
workflow = WorkflowBuilder(start_executor=ApprovalExecutor()).build()
1360+
first_events = [
1361+
event async for event in run_workflow_stream({"messages": [{"role": "user", "content": "go"}]}, workflow)
1362+
]
1363+
first_finished = [event for event in first_events if event.type == "RUN_FINISHED"][0].model_dump()
1364+
interrupt_payload = cast(list[dict[str, Any]], first_finished.get("interrupt"))
1365+
assert isinstance(interrupt_payload, list) and len(interrupt_payload) == 1
1366+
1367+
# Second turn: send denial via function_approvals on a message (not resume.interrupts)
1368+
resumed_events = [
1369+
event
1370+
async for event in run_workflow_stream(
1371+
{
1372+
"messages": [
1373+
{
1374+
"role": "user",
1375+
"content": "",
1376+
"function_approvals": [
1377+
{
1378+
"approved": False,
1379+
"id": "deny-1",
1380+
"call_id": "delete-call",
1381+
"name": "delete_record",
1382+
"arguments": {"record_id": "abc"},
1383+
}
1384+
],
1385+
}
1386+
],
1387+
},
1388+
workflow,
1389+
)
1390+
]
1391+
1392+
resumed_types = [event.type for event in resumed_events]
1393+
assert "RUN_STARTED" in resumed_types
1394+
assert "RUN_FINISHED" in resumed_types
1395+
assert "RUN_ERROR" not in resumed_types
1396+
assert "TEXT_MESSAGE_CONTENT" in resumed_types
1397+
text_deltas = [event.delta for event in resumed_events if event.type == "TEXT_MESSAGE_CONTENT"]
1398+
assert any("rejected" in delta for delta in text_deltas)
1399+
resumed_finished = [event for event in resumed_events if event.type == "RUN_FINISHED"][0].model_dump()
1400+
assert "interrupt" not in resumed_finished
1401+
1402+
12671403
async def test_workflow_run_available_interrupts_logged():
12681404
"""available_interrupts in input data should be logged without errors."""
12691405

0 commit comments

Comments
 (0)