-
Notifications
You must be signed in to change notification settings - Fork 1k
.NET: fix: Expose WorkflowErrorEvent as ErrorContent #2762
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
Merged
Changes from all commits
Commits
Show all changes
2 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
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
114 changes: 114 additions & 0 deletions
114
dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/WorkflowHostSmokeTests.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,114 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Runtime.CompilerServices; | ||
| using System.Text.Json; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using FluentAssertions; | ||
| using Microsoft.Extensions.AI; | ||
|
|
||
| namespace Microsoft.Agents.AI.Workflows.UnitTests; | ||
|
|
||
| public sealed class ExpectedException : Exception | ||
| { | ||
| public ExpectedException(string message) | ||
| : base(message) | ||
| { | ||
| } | ||
|
|
||
| public ExpectedException() : base() | ||
| { | ||
| } | ||
|
|
||
| public ExpectedException(string? message, Exception? innerException) : base(message, innerException) | ||
| { | ||
| } | ||
| } | ||
|
|
||
| public class WorkflowHostSmokeTests | ||
| { | ||
| private sealed class AlwaysFailsAIAgent(bool failByThrowing) : AIAgent | ||
| { | ||
| private sealed class Thread : InMemoryAgentThread | ||
| { | ||
| public Thread() { } | ||
|
|
||
| public Thread(JsonElement serializedThread, JsonSerializerOptions? jsonSerializerOptions = null) | ||
| : base(serializedThread, jsonSerializerOptions) | ||
| { } | ||
| } | ||
|
|
||
| public override AgentThread DeserializeThread(JsonElement serializedThread, JsonSerializerOptions? jsonSerializerOptions = null) | ||
| { | ||
| return new Thread(serializedThread, jsonSerializerOptions); | ||
| } | ||
|
|
||
| public override AgentThread GetNewThread() | ||
| { | ||
| return new Thread(); | ||
| } | ||
|
|
||
| protected override async Task<AgentRunResponse> RunCoreAsync(IEnumerable<ChatMessage> messages, AgentThread? thread = null, AgentRunOptions? options = null, CancellationToken cancellationToken = default) | ||
| { | ||
| return await this.RunStreamingAsync(messages, thread, options, cancellationToken) | ||
| .ToAgentRunResponseAsync(cancellationToken); | ||
| } | ||
|
|
||
| protected override async IAsyncEnumerable<AgentRunResponseUpdate> RunCoreStreamingAsync(IEnumerable<ChatMessage> messages, AgentThread? thread = null, AgentRunOptions? options = null, [EnumeratorCancellation] CancellationToken cancellationToken = default) | ||
| { | ||
| const string ErrorMessage = "Simulated agent failure."; | ||
| if (failByThrowing) | ||
| { | ||
| throw new ExpectedException(ErrorMessage); | ||
| } | ||
|
|
||
| yield return new AgentRunResponseUpdate(ChatRole.Assistant, [new ErrorContent(ErrorMessage)]); | ||
| } | ||
| } | ||
|
|
||
| private static Workflow CreateWorkflow(bool failByThrowing) | ||
lokitoth marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| ExecutorBinding agent = new AlwaysFailsAIAgent(failByThrowing).BindAsExecutor(emitEvents: true); | ||
|
|
||
| return new WorkflowBuilder(agent).Build(); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(true, true)] | ||
| [InlineData(true, false)] | ||
| [InlineData(false, true)] | ||
| [InlineData(false, false)] | ||
| public async Task Test_AsAgent_ErrorContentStreamedOutAsync(bool includeExceptionDetails, bool failByThrowing) | ||
| { | ||
| string expectedMessage = !failByThrowing || includeExceptionDetails | ||
| ? "Simulated agent failure." | ||
| : "An error occurred while executing the workflow."; | ||
|
|
||
| // Arrange is done by the caller. | ||
| Workflow workflow = CreateWorkflow(failByThrowing); | ||
|
|
||
| // Act | ||
| List<AgentRunResponseUpdate> updates = await workflow.AsAgent("WorkflowAgent", includeExceptionDetails: includeExceptionDetails) | ||
| .RunStreamingAsync(new ChatMessage(ChatRole.User, "Hello")) | ||
| .ToListAsync(); | ||
|
|
||
| // Assert | ||
| bool hadErrorContent = false; | ||
| foreach (AgentRunResponseUpdate update in updates) | ||
lokitoth marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| if (update.Contents.Any()) | ||
| { | ||
| // We should expect a single update which contains the error content. | ||
| update.Contents.Should().ContainSingle() | ||
| .Which.Should().BeOfType<ErrorContent>() | ||
| .Which.Message.Should().Be(expectedMessage); | ||
| hadErrorContent = true; | ||
| } | ||
| } | ||
|
|
||
| hadErrorContent.Should().BeTrue(); | ||
| } | ||
| } | ||
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.