-
Notifications
You must be signed in to change notification settings - Fork 1.6k
.NET: fix: Duplicate CallIds cause Handoff Message Filtering to fail #5359
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
+170
−89
Merged
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
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
115 changes: 115 additions & 0 deletions
115
dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/HandoffMessageFilterTests.cs
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,115 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using FluentAssertions; | ||
| using Microsoft.Agents.AI.Workflows.Specialized; | ||
| using Microsoft.Extensions.AI; | ||
|
|
||
| namespace Microsoft.Agents.AI.Workflows.UnitTests; | ||
|
|
||
| public class HandoffMessageFilterTests | ||
| { | ||
| private List<ChatMessage> CreateTestMessages(bool firstAgentUsesCallId, bool secondAgentUsesCallId, HandoffToolCallFilteringBehavior filter = HandoffToolCallFilteringBehavior.None) | ||
| { | ||
| FunctionCallContent handoffRequest1 = CreateHandoffCall(1, firstAgentUsesCallId); | ||
| FunctionResultContent handoffResponse1 = CreateHandoffResponse(handoffRequest1); | ||
|
|
||
| FunctionCallContent toolCall = CreateToolCall(secondAgentUsesCallId); | ||
| FunctionResultContent toolResponse = CreateToolResponse(toolCall); | ||
|
|
||
| // Approvals come from the function call middleware over ChatClient, so we can expect there to be a RequestId (not that we | ||
| // care, because we do not filter approval content) | ||
| ToolApprovalRequestContent toolApproval = new(Guid.NewGuid().ToString("N"), toolCall); | ||
| ToolApprovalResponseContent toolApprovalResponse = new(toolApproval.RequestId, true, toolCall); | ||
|
|
||
| FunctionCallContent handoffRequest2 = CreateHandoffCall(1, secondAgentUsesCallId); | ||
| FunctionResultContent handoffResponse2 = CreateHandoffResponse(handoffRequest2); | ||
|
|
||
| List<ChatMessage> result = [new(ChatRole.User, "Hello")]; | ||
|
|
||
| // Agent 1 turn | ||
| result.Add(new(ChatRole.Assistant, "Hello! What do you want help with today?")); | ||
| result.Add(new(ChatRole.User, "Please explain temperature")); | ||
|
|
||
| // Unless we are filtering none, we expect the handoff call to be filtered out, so we add it conditionally | ||
| if (filter == HandoffToolCallFilteringBehavior.None) | ||
| { | ||
| result.Add(new(ChatRole.Assistant, [handoffRequest1])); | ||
| result.Add(new(ChatRole.Tool, [handoffResponse1])); | ||
| } | ||
|
|
||
| // Agent 2 turn | ||
|
|
||
| // Tool approvals are never filtered, so we add them unconditionally | ||
| result.Add(new(ChatRole.Assistant, [toolApproval])); | ||
| result.Add(new(ChatRole.User, [toolApprovalResponse])); | ||
|
|
||
| // Unless we are filtering all, we expect the tool call to be retained, so we add it conditionally | ||
| if (filter != HandoffToolCallFilteringBehavior.All) | ||
| { | ||
| result.Add(new(ChatRole.Assistant, [toolCall])); | ||
| result.Add(new(ChatRole.Tool, [toolResponse])); | ||
| } | ||
|
|
||
| result.Add(new(ChatRole.Assistant, "Temperature is a measure of the average kinetic energy of the particles in a substance.")); | ||
|
|
||
| if (filter == HandoffToolCallFilteringBehavior.None) | ||
| { | ||
| result.Add(new(ChatRole.Assistant, [handoffRequest2])); | ||
| result.Add(new(ChatRole.Tool, [handoffResponse2])); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| private static FunctionCallContent CreateHandoffCall(int id, bool useCallId) | ||
| { | ||
| string callName = $"{HandoffWorkflowBuilder.FunctionPrefix}{id}"; | ||
| string callId = useCallId ? Guid.NewGuid().ToString("N") : callName; | ||
|
|
||
| return new FunctionCallContent(callId, callName); | ||
| } | ||
|
|
||
| private static FunctionResultContent CreateHandoffResponse(FunctionCallContent call) | ||
| => HandoffAgentExecutor.CreateHandoffResult(call.CallId); | ||
|
|
||
| private static FunctionCallContent CreateToolCall(bool useCallId) | ||
| { | ||
| const string CallName = "ToolFunction"; | ||
| string callId = useCallId ? Guid.NewGuid().ToString("N") : CallName; | ||
|
|
||
| return new FunctionCallContent(callId, CallName); | ||
| } | ||
|
|
||
| private static FunctionResultContent CreateToolResponse(FunctionCallContent call) | ||
| => new(call.CallId, new object()); | ||
|
lokitoth marked this conversation as resolved.
|
||
|
|
||
| [Theory] | ||
| [InlineData(true, true, HandoffToolCallFilteringBehavior.None)] | ||
| [InlineData(true, false, HandoffToolCallFilteringBehavior.None)] | ||
| [InlineData(false, true, HandoffToolCallFilteringBehavior.None)] | ||
| [InlineData(false, false, HandoffToolCallFilteringBehavior.None)] | ||
| [InlineData(true, true, HandoffToolCallFilteringBehavior.HandoffOnly)] | ||
| [InlineData(true, false, HandoffToolCallFilteringBehavior.HandoffOnly)] | ||
| [InlineData(false, true, HandoffToolCallFilteringBehavior.HandoffOnly)] | ||
| [InlineData(false, false, HandoffToolCallFilteringBehavior.HandoffOnly)] | ||
| [InlineData(true, true, HandoffToolCallFilteringBehavior.All)] | ||
| [InlineData(true, false, HandoffToolCallFilteringBehavior.All)] | ||
| [InlineData(false, true, HandoffToolCallFilteringBehavior.All)] | ||
| [InlineData(false, false, HandoffToolCallFilteringBehavior.All)] | ||
| public void Test_HandoffMessageFilter_FiltersOnlyExpectedMessages(bool firstAgentUsesCallId, bool secondAgentUsesCallId, HandoffToolCallFilteringBehavior behavior) | ||
| { | ||
| // Arrange | ||
| List<ChatMessage> messages = this.CreateTestMessages(firstAgentUsesCallId, secondAgentUsesCallId); | ||
| List<ChatMessage> expected = this.CreateTestMessages(firstAgentUsesCallId, secondAgentUsesCallId, behavior); | ||
|
|
||
|
lokitoth marked this conversation as resolved.
|
||
| HandoffMessagesFilter filter = new(behavior); | ||
|
|
||
| // Act | ||
| IEnumerable<ChatMessage> filteredMessages = filter.FilterMessages(messages); | ||
|
|
||
| // Assert | ||
| filteredMessages.Should().BeEquivalentTo(expected); | ||
| } | ||
| } | ||
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.