// Copyright (c) Microsoft. All rights reserved. using System; using Microsoft.Agents.AI.Workflows; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Shared.Diagnostics; namespace Microsoft.Agents.AI.Hosting; /// /// Provides extension methods for configuring AI workflows in a host application builder. /// public static class HostApplicationBuilderWorkflowExtensions { /// /// Registers a custom workflow using a factory delegate. /// /// The to configure. /// The unique name for the workflow. /// A factory function that creates the instance. The function receives the service provider and workflow name as parameters. /// The DI service lifetime for the workflow registration. Defaults to . /// An that can be used to further configure the workflow. /// Thrown when , , or is null. /// Thrown when is empty. /// /// Thrown when the factory delegate returns null or a workflow with a name that doesn't match the expected name. /// public static IHostedWorkflowBuilder AddWorkflow(this IHostApplicationBuilder builder, string name, Func createWorkflowDelegate, ServiceLifetime lifetime = ServiceLifetime.Singleton) { Throw.IfNull(builder); Throw.IfNull(name); Throw.IfNull(createWorkflowDelegate); builder.Services.AddKeyedService(name, (sp, key) => { Throw.IfNull(key); var keyString = key as string; Throw.IfNullOrEmpty(keyString); var workflow = createWorkflowDelegate(sp, keyString) ?? throw new InvalidOperationException($"The agent factory did not return a valid {nameof(Workflow)} instance for key '{keyString}'."); if (!string.Equals(workflow.Name, keyString, StringComparison.Ordinal)) { throw new InvalidOperationException($"The workflow factory returned workflow with name '{workflow.Name}', but the expected name is '{keyString}'."); } return workflow; }, lifetime); return new HostedWorkflowBuilder(name, builder); } }