-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Python: Correlate AG-UI confirm_changes snapshots by call id #7411
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
eavanvalkenburg
merged 3 commits into
microsoft:main
from
eavanvalkenburg:python-agui-confirm-changes-snapshot
Jul 30, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
157 changes: 157 additions & 0 deletions
157
python/packages/ag-ui/tests/ag_ui/test_confirm_changes_snapshot.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| # Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| from typing import Any | ||
|
|
||
| from agent_framework import Content, Message | ||
|
|
||
| from agent_framework_ag_ui._agent_run import ( | ||
| _clean_resolved_approvals_from_snapshot, | ||
| _confirm_changes_target_call_id, | ||
| ) | ||
|
|
||
|
|
||
| def _confirm_snapshot( | ||
| *, | ||
| original_call_id: str, | ||
| confirm_call_id: str, | ||
| accepted: bool, | ||
| ) -> list[dict[str, Any]]: | ||
| return [ | ||
| { | ||
| "role": "assistant", | ||
| "content": "", | ||
| "tool_calls": [ | ||
| { | ||
| "id": original_call_id, | ||
| "type": "function", | ||
| "function": {"name": "apply_changes", "arguments": "{}"}, | ||
| }, | ||
| { | ||
| "id": confirm_call_id, | ||
| "type": "function", | ||
| "function": { | ||
| "name": "confirm_changes", | ||
| "arguments": json.dumps({"function_call_id": original_call_id}), | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| "role": "tool", | ||
| "toolCallId": confirm_call_id, | ||
| "content": json.dumps({"accepted": accepted, "steps": []}), | ||
| }, | ||
| ] | ||
|
|
||
|
|
||
| def test_confirm_changes_target_ignores_non_list_tool_calls() -> None: | ||
| snapshot_messages: list[dict[str, Any]] = [ | ||
| { | ||
| "role": "assistant", | ||
| "tool_calls": {"id": "call_confirm"}, | ||
| } | ||
| ] | ||
|
|
||
| target_call_id = _confirm_changes_target_call_id(snapshot_messages, "call_confirm", {}) | ||
|
|
||
| assert target_call_id is None | ||
|
|
||
|
|
||
| def test_confirm_changes_target_rejects_malformed_arguments_json() -> None: | ||
| snapshot_messages: list[dict[str, Any]] = [ | ||
| { | ||
| "role": "assistant", | ||
| "tool_calls": [ | ||
| { | ||
| "id": "call_confirm", | ||
| "type": "function", | ||
| "function": { | ||
| "name": "confirm_changes", | ||
| "arguments": "{not-json", | ||
| }, | ||
| } | ||
| ], | ||
| } | ||
| ] | ||
|
|
||
| target_call_id = _confirm_changes_target_call_id(snapshot_messages, "call_confirm", {}) | ||
|
|
||
| assert target_call_id is None | ||
|
|
||
|
|
||
| def test_confirm_changes_snapshot_uses_original_call_id_with_multiple_results() -> None: | ||
| snapshot_messages = _confirm_snapshot( | ||
| original_call_id="call_target", | ||
| confirm_call_id="call_confirm", | ||
| accepted=True, | ||
| ) | ||
| resolved_messages = [ | ||
| Message( | ||
| role="tool", | ||
| contents=[ | ||
| Content.from_function_result(call_id="call_old", result="old result"), | ||
| Content.from_function_result(call_id="call_target", result="target result"), | ||
| ], | ||
| ) | ||
| ] | ||
|
|
||
| _clean_resolved_approvals_from_snapshot(snapshot_messages, resolved_messages) | ||
|
|
||
| assert snapshot_messages[1]["content"] == "target result" | ||
|
|
||
|
|
||
| def test_confirm_changes_snapshot_accepts_explicit_payload_call_id() -> None: | ||
| snapshot_messages: list[dict[str, Any]] = [ | ||
| { | ||
| "role": "tool", | ||
| "toolCallId": "call_confirm", | ||
| "content": json.dumps({"accepted": True, "function_call_id": "call_target"}), | ||
| } | ||
| ] | ||
| resolved_messages = [ | ||
| Message( | ||
| role="tool", | ||
| contents=[Content.from_function_result(call_id="call_target", result="target result")], | ||
| ) | ||
| ] | ||
|
|
||
| _clean_resolved_approvals_from_snapshot(snapshot_messages, resolved_messages) | ||
|
|
||
| assert snapshot_messages[0]["content"] == "target result" | ||
|
|
||
|
|
||
| def test_confirm_changes_snapshot_cleans_rejection_without_results() -> None: | ||
| snapshot_messages = _confirm_snapshot( | ||
| original_call_id="call_target", | ||
| confirm_call_id="call_confirm", | ||
| accepted=False, | ||
| ) | ||
|
|
||
| _clean_resolved_approvals_from_snapshot(snapshot_messages, []) | ||
|
|
||
| assert snapshot_messages[1]["content"] == "Changes declined." | ||
|
|
||
|
|
||
| def test_confirm_changes_snapshot_keeps_accepted_payload_when_target_result_is_missing() -> None: | ||
| snapshot_messages = _confirm_snapshot( | ||
| original_call_id="call_target", | ||
| confirm_call_id="call_confirm", | ||
| accepted=True, | ||
| ) | ||
| original_content = snapshot_messages[1]["content"] | ||
| resolved_messages = [ | ||
| Message( | ||
| role="tool", | ||
| contents=[ | ||
| Content.from_function_result(call_id="call_old_1", result="old result 1"), | ||
| Content.from_function_result(call_id="call_old_2", result="old result 2"), | ||
| ], | ||
| ) | ||
| ] | ||
|
|
||
| _clean_resolved_approvals_from_snapshot(snapshot_messages, resolved_messages) | ||
|
|
||
| assert snapshot_messages[1]["content"] == original_content |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.