// Copyright (c) Microsoft. All rights reserved. using System; using Microsoft.Extensions.AI; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Shared.Diagnostics; namespace Microsoft.Agents.AI.Hosting; /// /// Provides extension methods for configuring AI agents in a host application builder. /// public static class HostApplicationBuilderAgentExtensions { /// /// Adds an AI agent to the host application builder with the specified name and instructions. /// /// The host application builder to configure. /// The name of the agent. /// The instructions for the agent. /// The DI service lifetime for the agent registration. Defaults to . /// The configured host application builder. /// Thrown when , , or is null. public static IHostedAgentBuilder AddAIAgent(this IHostApplicationBuilder builder, string name, string? instructions, ServiceLifetime lifetime = ServiceLifetime.Singleton) { Throw.IfNull(builder); return builder.Services.AddAIAgent(name, instructions, lifetime); } /// /// Adds an AI agent to the host application builder with the specified name, instructions, and chat client key. /// /// The host application builder to configure. /// The name of the agent. /// The instructions for the agent. /// The chat client which the agent will use for inference. /// The DI service lifetime for the agent registration. Defaults to . /// The configured host application builder. /// Thrown when , , or is null. public static IHostedAgentBuilder AddAIAgent(this IHostApplicationBuilder builder, string name, string? instructions, IChatClient chatClient, ServiceLifetime lifetime = ServiceLifetime.Singleton) { Throw.IfNull(builder); Throw.IfNullOrEmpty(name); return builder.Services.AddAIAgent(name, instructions, chatClient, lifetime); } /// /// Adds an AI agent to the host application builder with the specified name, instructions, and chat client key. /// /// The host application builder to configure. /// The name of the agent. /// The instructions for the agent. /// A description of the agent. /// The key to use when resolving the chat client from the service provider. If null, a non-keyed service will be resolved. /// The DI service lifetime for the agent registration. Defaults to . /// The configured host application builder. /// Thrown when , , or is null. public static IHostedAgentBuilder AddAIAgent(this IHostApplicationBuilder builder, string name, string? instructions, string? description, object? chatClientServiceKey, ServiceLifetime lifetime = ServiceLifetime.Singleton) { Throw.IfNull(builder); Throw.IfNullOrEmpty(name); return builder.Services.AddAIAgent(name, instructions, description, chatClientServiceKey, lifetime); } /// /// Adds an AI agent to the host application builder with the specified name, instructions, and chat client key. /// /// The host application builder to configure. /// The name of the agent. /// The instructions for the agent. /// The key to use when resolving the chat client from the service provider. If null, a non-keyed service will be resolved. /// The DI service lifetime for the agent registration. Defaults to . /// The configured host application builder. /// Thrown when , , or is null. public static IHostedAgentBuilder AddAIAgent(this IHostApplicationBuilder builder, string name, string? instructions, object? chatClientServiceKey, ServiceLifetime lifetime = ServiceLifetime.Singleton) { Throw.IfNull(builder); return builder.Services.AddAIAgent(name, instructions, chatClientServiceKey, lifetime); } /// /// Adds an AI agent to the host application builder using a custom factory delegate. /// /// The host application builder to configure. /// The name of the agent. /// A factory delegate that creates the AI agent instance. The delegate receives the service provider and agent key as parameters. /// The DI service lifetime for the agent registration. Defaults to . /// The configured host application builder. /// Thrown when , , or is null. /// Thrown when the agent factory delegate returns null or an invalid AI agent instance. public static IHostedAgentBuilder AddAIAgent(this IHostApplicationBuilder builder, string name, Func createAgentDelegate, ServiceLifetime lifetime = ServiceLifetime.Singleton) { Throw.IfNull(builder); return builder.Services.AddAIAgent(name, createAgentDelegate, lifetime); } }