-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Python: fix AG-UI pending tool history replay #5973
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
Closed
Yufeng He (he-yufeng)
wants to merge
1
commit into
microsoft:main
from
he-yufeng:fix/ag-ui-tool-history-pending-results
Closed
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,10 +32,36 @@ def _sanitize_tool_history(messages: list[Message]) -> list[Message]: | |
| pending_tool_call_ids: set[str] | None = None | ||
| pending_confirm_changes_id: str | None = None | ||
|
|
||
| def flush_pending_tool_results(result: str) -> None: | ||
| nonlocal pending_confirm_changes_id, pending_tool_call_ids | ||
|
|
||
| if not pending_tool_call_ids: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's address this one, please.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ping |
||
| pending_confirm_changes_id = None | ||
| return | ||
|
|
||
| for pending_call_id in sorted(pending_tool_call_ids): | ||
| logger.info(f"Injecting synthetic tool result for pending call_id={pending_call_id}") | ||
|
moonbox3 marked this conversation as resolved.
|
||
| sanitized.append( | ||
| Message( | ||
| role="tool", | ||
| contents=[Content.from_function_result(call_id=pending_call_id, result=result)], | ||
| ) | ||
| ) | ||
|
|
||
| pending_tool_call_ids = None | ||
| pending_confirm_changes_id = None | ||
|
|
||
| for msg in messages: | ||
| role_value = get_role_value(msg) | ||
|
|
||
| if role_value == "assistant": | ||
| if pending_tool_call_ids: | ||
| logger.info( | ||
| f"Assistant message arrived with {len(pending_tool_call_ids)} pending tool calls - " | ||
| "injecting synthetic results" | ||
| ) | ||
|
moonbox3 marked this conversation as resolved.
|
||
| flush_pending_tool_results("Tool execution skipped - assistant provided another message") | ||
|
|
||
| tool_ids = { | ||
| str(content.call_id) | ||
| for content in msg.contents or [] | ||
|
|
@@ -151,20 +177,7 @@ def _sanitize_tool_history(messages: list[Message]) -> list[Message]: | |
| f"User message arrived with {len(pending_tool_call_ids)} pending tool calls - " | ||
| "injecting synthetic results" | ||
| ) | ||
| for pending_call_id in pending_tool_call_ids: | ||
| logger.info(f"Injecting synthetic tool result for pending call_id={pending_call_id}") | ||
| synthetic_result = Message( | ||
| role="tool", | ||
| contents=[ | ||
| Content.from_function_result( | ||
| call_id=pending_call_id, | ||
| result="Tool execution skipped - user provided follow-up message", | ||
| ) | ||
| ], | ||
| ) | ||
| sanitized.append(synthetic_result) | ||
| pending_tool_call_ids = None | ||
| pending_confirm_changes_id = None | ||
| flush_pending_tool_results("Tool execution skipped - user provided follow-up message") | ||
|
|
||
| sanitized.append(msg) | ||
| pending_confirm_changes_id = None | ||
|
|
@@ -194,6 +207,13 @@ def _sanitize_tool_history(messages: list[Message]) -> list[Message]: | |
| pending_tool_call_ids = None | ||
| pending_confirm_changes_id = None | ||
|
|
||
| if pending_tool_call_ids: | ||
| logger.info( | ||
| f"Message history ended with {len(pending_tool_call_ids)} pending tool calls - " | ||
| "injecting synthetic results" | ||
|
moonbox3 marked this conversation as resolved.
|
||
| ) | ||
| flush_pending_tool_results("Tool execution skipped - missing tool result") | ||
|
|
||
| return sanitized | ||
|
|
||
|
|
||
|
|
||
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
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.