// Copyright (c) Microsoft. All rights reserved.
using Microsoft.Agents.AI.Workflows;
using Microsoft.Extensions.DependencyInjection;
namespace Microsoft.Agents.AI.Hosting;
///
/// Provides extension methods for to enable additional workflow configuration scenarios.
///
public static class HostedWorkflowBuilderExtensions
{
///
/// Registers the workflow as an AI agent in the dependency injection container.
///
/// The instance to extend.
/// An that can be used to further configure the agent.
public static IHostedAgentBuilder AddAsAIAgent(this IHostedWorkflowBuilder builder)
=> builder.AddAsAIAgent(name: null);
///
/// Registers the workflow as an AI agent in the dependency injection container.
///
/// The instance to extend.
/// The optional name for the AI agent. If not specified, the workflow name is used.
/// An that can be used to further configure the agent.
public static IHostedAgentBuilder AddAsAIAgent(this IHostedWorkflowBuilder builder, string? name)
{
var agentName = name ?? builder.Name;
return builder.HostApplicationBuilder.AddAIAgent(agentName, (sp, key) =>
{
var workflow = sp.GetRequiredKeyedService(key);
#pragma warning disable VSTHRD002 // Avoid problematic synchronous waits
return workflow.AsAgentAsync(name: key).AsTask().GetAwaiter().GetResult();
#pragma warning restore VSTHRD002 // Avoid problematic synchronous waits
});
}
}