Skip to content

Commit a39ecc9

Browse files
committed
feat: Add a way to show/suppress exception information
1 parent f97ea1b commit a39ecc9

3 files changed

Lines changed: 18 additions & 7 deletions

File tree

dotnet/src/Microsoft.Agents.AI.Workflows/WorkflowHostAgent.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,20 @@ internal sealed class WorkflowHostAgent : AIAgent
1818
private readonly string? _id;
1919
private readonly CheckpointManager? _checkpointManager;
2020
private readonly IWorkflowExecutionEnvironment _executionEnvironment;
21+
private readonly bool _includeExceptionDetails;
2122
private readonly Task<ProtocolDescriptor> _describeTask;
2223

2324
private readonly ConcurrentDictionary<string, string> _assignedRunIds = [];
2425

25-
public WorkflowHostAgent(Workflow workflow, string? id = null, string? name = null, string? description = null, CheckpointManager? checkpointManager = null, IWorkflowExecutionEnvironment? executionEnvironment = null)
26+
public WorkflowHostAgent(Workflow workflow, string? id = null, string? name = null, string? description = null, CheckpointManager? checkpointManager = null, IWorkflowExecutionEnvironment? executionEnvironment = null, bool includeExceptionDetails = false)
2627
{
2728
this._workflow = Throw.IfNull(workflow);
2829

2930
this._executionEnvironment = executionEnvironment ?? (workflow.AllowConcurrent
3031
? InProcessExecution.Concurrent
3132
: InProcessExecution.OffThread);
3233
this._checkpointManager = checkpointManager;
34+
this._includeExceptionDetails = includeExceptionDetails;
3335

3436
this._id = id;
3537
this.Name = name;
@@ -61,10 +63,10 @@ private async ValueTask ValidateWorkflowAsync()
6163
protocol.ThrowIfNotChatProtocol();
6264
}
6365

64-
public override AgentThread GetNewThread() => new WorkflowThread(this._workflow, this.GenerateNewId(), this._executionEnvironment, this._checkpointManager);
66+
public override AgentThread GetNewThread() => new WorkflowThread(this._workflow, this.GenerateNewId(), this._executionEnvironment, this._checkpointManager, this._includeExceptionDetails);
6567

6668
public override AgentThread DeserializeThread(JsonElement serializedThread, JsonSerializerOptions? jsonSerializerOptions = null)
67-
=> new WorkflowThread(this._workflow, serializedThread, this._executionEnvironment, this._checkpointManager, jsonSerializerOptions);
69+
=> new WorkflowThread(this._workflow, serializedThread, this._executionEnvironment, this._checkpointManager, this._includeExceptionDetails, jsonSerializerOptions);
6870

6971
private ValueTask<WorkflowThread> UpdateThreadAsync(IEnumerable<ChatMessage> messages, AgentThread? thread = null, CancellationToken cancellationToken = default)
7072
{

dotnet/src/Microsoft.Agents.AI.Workflows/WorkflowHostingExtensions.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,17 @@ public static class WorkflowHostingExtensions
2121
/// <param name="executionEnvironment">Specify the execution environment to use when running the workflows. See
2222
/// <see cref="InProcessExecution.OffThread"/>, <see cref="InProcessExecution.Concurrent"/> and
2323
/// <see cref="InProcessExecution.Lockstep"/> for the in-process environments.</param>
24+
/// <param name="includeExceptionDetails">If <see langword="true"/>, will include <see cref="System.Exception.Message"/>
25+
/// in the <see cref="ErrorContent"/> representing the workflow error.</param>
2426
/// <returns></returns>
2527
public static AIAgent AsAgent(
2628
this Workflow workflow,
2729
string? id = null,
2830
string? name = null,
2931
string? description = null,
3032
CheckpointManager? checkpointManager = null,
31-
IWorkflowExecutionEnvironment? executionEnvironment = null)
33+
IWorkflowExecutionEnvironment? executionEnvironment = null,
34+
bool includeExceptionDetails = false)
3235
{
3336
return new WorkflowHostAgent(workflow, id, name, description, checkpointManager, executionEnvironment);
3437
}

dotnet/src/Microsoft.Agents.AI.Workflows/WorkflowThread.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@ internal sealed class WorkflowThread : AgentThread
1818
{
1919
private readonly Workflow _workflow;
2020
private readonly IWorkflowExecutionEnvironment _executionEnvironment;
21+
private readonly bool _includeExceptionDetails;
2122

2223
private readonly CheckpointManager _checkpointManager;
2324
private readonly InMemoryCheckpointManager? _inMemoryCheckpointManager;
2425

25-
public WorkflowThread(Workflow workflow, string runId, IWorkflowExecutionEnvironment executionEnvironment, CheckpointManager? checkpointManager = null)
26+
public WorkflowThread(Workflow workflow, string runId, IWorkflowExecutionEnvironment executionEnvironment, CheckpointManager? checkpointManager = null, bool includeExceptionDetails = false)
2627
{
2728
this._workflow = Throw.IfNull(workflow);
2829
this._executionEnvironment = Throw.IfNull(executionEnvironment);
30+
this._includeExceptionDetails = includeExceptionDetails;
2931

3032
// If the user provided an external checkpoint manager, use that, otherwise rely on an in-memory one.
3133
// TODO: Implement persist-only-last functionality for in-memory checkpoint manager, to avoid unbounded
@@ -36,7 +38,7 @@ public WorkflowThread(Workflow workflow, string runId, IWorkflowExecutionEnviron
3638
this.MessageStore = new WorkflowMessageStore();
3739
}
3840

39-
public WorkflowThread(Workflow workflow, JsonElement serializedThread, IWorkflowExecutionEnvironment executionEnvironment, CheckpointManager? checkpointManager = null, JsonSerializerOptions? jsonSerializerOptions = null)
41+
public WorkflowThread(Workflow workflow, JsonElement serializedThread, IWorkflowExecutionEnvironment executionEnvironment, CheckpointManager? checkpointManager = null, bool includeExceptionDetails = false, JsonSerializerOptions? jsonSerializerOptions = null)
4042
{
4143
this._workflow = Throw.IfNull(workflow);
4244
this._executionEnvironment = Throw.IfNull(executionEnvironment);
@@ -168,7 +170,11 @@ IAsyncEnumerable<AgentRunResponseUpdate> InvokeStageAsync(
168170

169171
if (exception != null)
170172
{
171-
ErrorContent errorContent = new(exception.Message);
173+
string message = this._includeExceptionDetails
174+
? exception.Message
175+
: "An error occurred while executing the workflow.";
176+
177+
ErrorContent errorContent = new(message);
172178
yield return this.CreateUpdate(this.LastResponseId, evt, errorContent);
173179
}
174180

0 commit comments

Comments
 (0)