-
Notifications
You must be signed in to change notification settings - Fork 891
.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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| // 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(); | ||
| } | ||
|
|
||
| public override async Task<AgentRunResponse> RunAsync(IEnumerable<ChatMessage> messages, AgentThread? thread = null, AgentRunOptions? options = null, CancellationToken cancellationToken = default) | ||
| { | ||
| return await this.RunStreamingAsync(messages, thread, options, cancellationToken) | ||
| .ToAgentRunResponseAsync(cancellationToken); | ||
| } | ||
|
|
||
| public override async IAsyncEnumerable<AgentRunResponseUpdate> RunStreamingAsync(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) | ||
| { | ||
| ExecutorBinding agent = new AlwaysFailsAIAgent(failByThrowing).BindAsExecutor(emitEvents: true); | ||
|
|
||
| return new WorkflowBuilder(agent).Build(); | ||
| } | ||
|
|
||
| private async static Task InvokeAsAgentAndProcessResponseAsync(Workflow workflow) | ||
|
||
| { | ||
| List<AgentRunResponseUpdate> updates = await workflow.AsAgent("WorkflowAgent") | ||
|
||
| .RunStreamingAsync(new ChatMessage(ChatRole.User, "Hello")) | ||
| .ToListAsync(); | ||
|
|
||
| bool hadErrorContent = false; | ||
| foreach (AgentRunResponseUpdate update in updates) | ||
|
||
| { | ||
| 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("Simulated agent failure."); | ||
| hadErrorContent = true; | ||
| } | ||
| } | ||
|
|
||
| hadErrorContent.Should().BeTrue(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public Task Test_AsAgent_ErrorContentStreamedOutAsync() | ||
| => InvokeAsAgentAndProcessResponseAsync(CreateWorkflow(failByThrowing: false)); | ||
|
|
||
| [Fact] | ||
| public Task Test_AsAgent_ExceptionToErrorContentAsync() | ||
| => InvokeAsAgentAndProcessResponseAsync(CreateWorkflow(failByThrowing: true)); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to the C# coding guidelines, prefer defining variables using types rather than var to help users understand the types involved.