// Copyright (c) Microsoft. All rights reserved. using System; using System.Threading; using System.Threading.Tasks; namespace Microsoft.Agents.AI.Workflows; /// /// Executes a user-provided asynchronous function in response to workflow messages of the specified input type. /// /// The type of input message. /// A unique identifier for the executor. /// A delegate that defines the asynchronous function to execute for each input message. /// Configuration options for the executor. If null, default options will be used. public class FunctionExecutor(string id, Func handlerAsync, ExecutorOptions? options = null) : Executor(id, options) { internal static Func WrapAction(Action handlerSync) { return RunActionAsync; ValueTask RunActionAsync(TInput input, IWorkflowContext workflowContext, CancellationToken cancellationToken) { handlerSync(input, workflowContext, cancellationToken); return default; } } /// public override ValueTask HandleAsync(TInput message, IWorkflowContext context) => handlerAsync(message, context, default); /// /// Creates a new instance of the class. /// /// A unique identifier for the executor. /// A synchronous function to execute for each input message and workflow context. public FunctionExecutor(string id, Action handlerSync) : this(id, WrapAction(handlerSync)) { } } /// /// Executes a user-provided asynchronous function in response to workflow messages of the specified input type, /// /// The type of input message. /// The type of output message. /// A unique identifier for the executor. /// A delegate that defines the asynchronous function to execute for each input message. /// Configuration options for the executor. If null, default options will be used. public class FunctionExecutor(string id, Func> handlerAsync, ExecutorOptions? options = null) : Executor(id, options) { internal static Func> WrapFunc(Func handlerSync) { return RunFuncAsync; ValueTask RunFuncAsync(TInput input, IWorkflowContext workflowContext, CancellationToken cancellationToken) { TOutput result = handlerSync(input, workflowContext, cancellationToken); return new ValueTask(result); } } /// public override ValueTask HandleAsync(TInput message, IWorkflowContext context) => handlerAsync(message, context, default); /// /// Creates a new instance of the class. /// /// A unique identifier for the executor. /// A synchronous function to execute for each input message and workflow context. public FunctionExecutor(string id, Func handlerSync) : this(id, WrapFunc(handlerSync)) { } }