Files
agent-framework/dotnet/tests/Microsoft.Agents.Workflows.UnitTests/TestingExecutor.cs
T
Jacob Alber 39e071c430 .NET: Update Workflow Input/Output Redesign (#881)
* feat: Make Executor id field mandatory

When checkpointing is involved, it is critical to keep executor ids consistent between runs, even when recreating a new object tree for the workflow.

The default id-setting mechanism generated a guid for part of the id, making it not work when restoring from a checkpoint.

This change prevents this situation from arising.

* feat: Enable running untyped Workflows

With the change to enable delay-instantiation of executors and support for async Executor factory methods, we must instantiate the starting executor to know what are the valid input types for the workflow.

To avoid forcing instantiation every time, and to better support workflows with multiple input types, we enable support for build and interacting with the base Workflow type without type annotations, and remove the requirement to know a valid input type when initiating a run.

* feat: Support Output from any executor and multiple outputs.
2025-09-25 02:03:22 +00:00

81 lines
2.5 KiB
C#

// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.Agents.Workflows.UnitTests;
internal abstract class TestingExecutor<TIn, TOut> : Executor, IDisposable
{
private readonly bool _loop;
private readonly Func<TIn, IWorkflowContext, CancellationToken, ValueTask<TOut>>[] _actions;
private readonly HashSet<CancellationToken> _linkedTokens = [];
private CancellationTokenSource _internalCts = new();
protected TestingExecutor(string id, bool loop = false, params Func<TIn, IWorkflowContext, CancellationToken, ValueTask<TOut>>[] actions) : base(id)
{
this._loop = loop;
this._actions = actions;
}
public void UnlinkCancellation(CancellationToken token) =>
this._linkedTokens.Remove(token);
public void LinkCancellation(CancellationToken token)
{
this._linkedTokens.Add(token);
CancellationTokenSource tokenSource = CancellationTokenSource.CreateLinkedTokenSource(this._linkedTokens.ToArray());
tokenSource = Interlocked.Exchange(ref this._internalCts, tokenSource);
tokenSource.Dispose();
}
public void SetCancel() =>
Volatile.Read(ref this._internalCts).Cancel();
protected sealed override RouteBuilder ConfigureRoutes(RouteBuilder routeBuilder) =>
routeBuilder.AddHandler<TIn, TOut>(this.RouteToActionsAsync);
private int _nextActionIndex;
private ValueTask<TOut> RouteToActionsAsync(TIn message, IWorkflowContext context)
{
if (this._nextActionIndex >= this._actions.Length)
{
if (this._loop)
{
this._nextActionIndex = 0;
}
else
{
throw new InvalidOperationException("No more actions to execute and looping is disabled.");
}
}
try
{
Func<TIn, IWorkflowContext, CancellationToken, ValueTask<TOut>> action = this._actions[this._nextActionIndex];
return action(message, context, Volatile.Read(ref this._internalCts).Token);
}
finally
{
this._nextActionIndex++;
}
}
~TestingExecutor()
{
this.Dispose(false);
}
protected virtual void Dispose(bool disposing) =>
this._internalCts.Dispose();
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
}