WIP. runs all executors/agents in workflow sequantially.

This commit is contained in:
Shyju Krishnankutty
2026-01-14 12:33:54 -08:00
Unverified
parent 4340f37e97
commit 7f22a87a24
9 changed files with 288 additions and 18 deletions
@@ -27,7 +27,10 @@ internal sealed class BuiltInFunctionExecutor : IFunctionExecutor
if (context.FunctionDefinition.EntryPoint == BuiltInFunctions.InvokeWorkflowActivityFunctionEntryPoint)
{
context.GetInvocationResult().Value = await BuiltInFunctions.InvokeWorkflowActivityAsync("aa", context);
var binding = context.FunctionDefinition.InputBindings.Values.FirstOrDefault(a => a.Name == "input");
var input = await context.BindInputAsync<string>(binding!);
var val = input.Value;
context.GetInvocationResult().Value = await BuiltInFunctions.InvokeWorkflowActivityAsync(val!, context);
return;
}
@@ -152,10 +152,37 @@ internal sealed class DurableWorkflowRunner
this._logger.LogExecutingActivity(executorPair.Key, executorPair.Value.ExecutorType.TypeName);
const string result = "Many types are internal.";
// Attempt to invoke the executor using Executor.ExecuteAsync
// This allows the executor to handle its own execution logic
try
{
// Create the executor instance
Executor executor = await workflow.CreateExecutorInstanceAsync(
executorPair.Key,
"activity-run",
CancellationToken.None).ConfigureAwait(false);
this._logger.LogActivityExecuted(executorPair.Key, result);
// Create a minimal workflow context for the executor
MinimalActivityContext context = new(executorPair.Key);
return result;
// Execute the executor with the input
// The executor handles its own routing logic internally
object? result = await executor.ExecuteAsync(
input,
new TypeId(typeof(string)),
context,
CancellationToken.None).ConfigureAwait(false);
// Convert result to string
string resultString = result?.ToString() ?? string.Empty;
this._logger.LogActivityExecuted(executorPair.Key, resultString);
return resultString;
}
catch (Exception ex)
{
this._logger.LogError(ex, "Error executing executor '{ExecutorId}' in activity", executorPair.Key);
throw;
}
}
}
@@ -0,0 +1,90 @@
// Copyright (c) Microsoft. All rights reserved.
using Microsoft.Agents.AI.Workflows;
namespace Microsoft.Agents.AI.Hosting.AzureFunctions;
/// <summary>
/// A minimal implementation of <see cref="IWorkflowContext"/> for use in Azure Functions activities.
/// This provides basic context support for simple executors that don't require full workflow infrastructure.
/// </summary>
internal sealed class MinimalActivityContext : IWorkflowContext
{
public MinimalActivityContext(string executorId)
{
// executorId is provided but not stored since this minimal context doesn't use it
_ = executorId;
}
/// <inheritdoc/>
public ValueTask AddEventAsync(WorkflowEvent workflowEvent, CancellationToken cancellationToken = default)
{
// In activity context, events are not propagated to the workflow
// They would need to be returned as part of the activity result
return default;
}
/// <inheritdoc/>
public ValueTask SendMessageAsync(object message, string? targetId = null, CancellationToken cancellationToken = default)
{
// In activity context, messages cannot be routed to other executors
// The orchestration handles message routing between executors
return default;
}
/// <inheritdoc/>
public ValueTask YieldOutputAsync(object output, CancellationToken cancellationToken = default)
{
// In activity context, outputs are not yielded to the workflow
// They would need to be returned as part of the activity result
return default;
}
/// <inheritdoc/>
public ValueTask RequestHaltAsync()
{
// Halt requests are not supported in activity context
return default;
}
/// <inheritdoc/>
public ValueTask<T?> ReadStateAsync<T>(string key, string? scopeName = null, CancellationToken cancellationToken = default)
{
// No state available in activity context
return new ValueTask<T?>(default(T));
}
/// <inheritdoc/>
public ValueTask<T> ReadOrInitStateAsync<T>(string key, Func<T> initialStateFactory, string? scopeName = null, CancellationToken cancellationToken = default)
{
// Initialize with factory value since no state is available
return new ValueTask<T>(initialStateFactory());
}
/// <inheritdoc/>
public ValueTask<HashSet<string>> ReadStateKeysAsync(string? scopeName = null, CancellationToken cancellationToken = default)
{
// No state keys in activity context
return new ValueTask<HashSet<string>>([]);
}
/// <inheritdoc/>
public ValueTask QueueStateUpdateAsync<T>(string key, T? value, string? scopeName = null, CancellationToken cancellationToken = default)
{
// State updates are not persisted in activity context
return default;
}
/// <inheritdoc/>
public ValueTask QueueClearScopeAsync(string? scopeName = null, CancellationToken cancellationToken = default)
{
// No state to clear in activity context
return default;
}
/// <inheritdoc/>
public IReadOnlyDictionary<string, string>? TraceContext => null;
/// <inheritdoc/>
public bool ConcurrentRunsEnabled => false;
}
@@ -224,4 +224,25 @@ public class Workflow
}
}
}
/// <summary>
/// Creates an instance of the specified executor.
/// </summary>
/// <param name="executorId">The identifier of the executor to create.</param>
/// <param name="runId">A unique identifier for the run context.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests.</param>
/// <returns>A <see cref="ValueTask{Executor}"/> representing the asynchronous operation.</returns>
/// <remarks>
/// This method is useful for Azure Functions scenarios where you need to create executor instances
/// outside of the normal workflow execution flow.
/// </remarks>
public async ValueTask<Executor> CreateExecutorInstanceAsync(string executorId, string runId, CancellationToken cancellationToken = default)
{
if (!this.ExecutorBindings.TryGetValue(executorId, out ExecutorBinding? binding))
{
throw new InvalidOperationException($"Executor '{executorId}' not found in workflow.");
}
return await binding.CreateInstanceAsync(runId).ConfigureAwait(false);
}
}