// Copyright (c) Microsoft. All rights reserved. using Microsoft.Agents.AI.DurableTask.Workflows; using Microsoft.DurableTask; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; namespace Microsoft.Agents.AI.Hosting.AzureFunctions; /// /// A custom implementation that delegates workflow orchestration /// execution to the . /// internal sealed class WorkflowOrchestrator : ITaskOrchestrator { private readonly IServiceProvider _serviceProvider; /// /// Initializes a new instance of the class. /// /// The service provider used to resolve workflow dependencies. public WorkflowOrchestrator(IServiceProvider serviceProvider) { this._serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider)); } /// public Type InputType => typeof(DurableWorkflowInput); /// public Type OutputType => typeof(string); /// public async Task RunAsync(TaskOrchestrationContext context, object? input) { ArgumentNullException.ThrowIfNull(context); DurableWorkflowRunner runner = this._serviceProvider.GetRequiredService(); ILogger logger = context.CreateReplaySafeLogger(context.Name); DurableWorkflowInput workflowInput = input switch { DurableWorkflowInput existing => existing, _ => new DurableWorkflowInput { Input = input! } }; // ConfigureAwait(true) is required to preserve the orchestration context // across awaits, which the Durable Task framework uses for replay. return await runner.RunWorkflowOrchestrationAsync(context, workflowInput, logger).ConfigureAwait(true); } }