// Copyright (c) Microsoft. All rights reserved. using Microsoft.Agents.AI.DurableTask; namespace Microsoft.Agents.AI.Hosting.AzureFunctions; /// /// Provides extension methods for registering and configuring AI agents in the context of the Azure Functions hosting environment. /// public static class DurableAgentsOptionsExtensions { // Registry of agent options. private static readonly Dictionary s_agentOptions = new(StringComparer.OrdinalIgnoreCase); /// /// Adds an AI agent to the specified DurableAgentsOptions instance and optionally configures agent-specific /// options. /// /// The DurableAgentsOptions instance to which the AI agent will be added. /// The AI agent to add. The agent's Name property must not be null or empty. /// An optional delegate to configure agent-specific options. If null, default options are used. /// The updated instance containing the added AI agent. public static DurableAgentsOptions AddAIAgent( this DurableAgentsOptions options, AIAgent agent, Action? configure) { ArgumentNullException.ThrowIfNull(options); ArgumentNullException.ThrowIfNull(agent); ArgumentException.ThrowIfNullOrEmpty(agent.Name); // Initialize with default behavior (HTTP trigger enabled) FunctionsAgentOptions agentOptions = new() { HttpTrigger = { IsEnabled = true } }; configure?.Invoke(agentOptions); options.AddAIAgent(agent); s_agentOptions[agent.Name] = agentOptions; return options; } /// /// Adds an AI agent to the specified options and configures trigger support for HTTP and MCP tool invocations. /// /// If an agent with the same name already exists in the options, its configuration will be /// updated. Both triggers can be enabled independently. This method supports method chaining by returning the /// provided options instance. /// The options collection to which the AI agent will be added. Cannot be null. /// The AI agent to add. The agent's Name property must not be null or empty. /// true to enable an HTTP trigger for the agent; otherwise, false. /// true to enable an MCP tool trigger for the agent; otherwise, false. /// The updated instance with the specified AI agent and trigger configuration applied. public static DurableAgentsOptions AddAIAgent( this DurableAgentsOptions options, AIAgent agent, bool enableHttpTrigger, bool enableMcpToolTrigger) { ArgumentNullException.ThrowIfNull(options); ArgumentNullException.ThrowIfNull(agent); ArgumentException.ThrowIfNullOrEmpty(agent.Name); FunctionsAgentOptions agentOptions = new(); agentOptions.HttpTrigger.IsEnabled = enableHttpTrigger; agentOptions.McpToolTrigger.IsEnabled = enableMcpToolTrigger; options.AddAIAgent(agent); s_agentOptions[agent.Name] = agentOptions; return options; } /// /// Registers an AI agent factory with the specified name and optional configuration in the provided /// DurableAgentsOptions instance. /// /// If an agent factory with the same name already exists, its configuration will be replaced. /// This method enables custom agent registration and configuration for use in durable agent scenarios. /// The DurableAgentsOptions instance to which the AI agent factory will be added. Cannot be null. /// The unique name used to identify the AI agent factory. Cannot be null. /// A delegate that creates an AIAgent instance using the provided IServiceProvider. Cannot be null. /// An optional action to configure FunctionsAgentOptions for the agent factory. If null, default options are used. /// The updated DurableAgentsOptions instance containing the registered AI agent factory. public static DurableAgentsOptions AddAIAgentFactory( this DurableAgentsOptions options, string name, Func factory, Action? configure) { ArgumentNullException.ThrowIfNull(options); ArgumentNullException.ThrowIfNull(name); ArgumentNullException.ThrowIfNull(factory); // Initialize with default behavior (HTTP trigger enabled) FunctionsAgentOptions agentOptions = new() { HttpTrigger = { IsEnabled = true } }; configure?.Invoke(agentOptions); options.AddAIAgentFactory(name, factory); s_agentOptions[name] = agentOptions; return options; } /// /// Registers an AI agent factory with the specified name and configures trigger options for the agent. /// /// If both triggers are disabled, the agent will not be accessible via HTTP or MCP tool /// endpoints. This method can be used to register multiple agent factories with different configurations. /// The options object to which the AI agent factory will be added. Cannot be null. /// The unique name used to identify the AI agent factory. Cannot be null. /// A delegate that creates an instance of the AI agent using the provided service provider. Cannot be null. /// true to enable the HTTP trigger for the agent; otherwise, false. /// true to enable the MCP tool trigger for the agent; otherwise, false. /// The same DurableAgentsOptions instance, allowing for method chaining. public static DurableAgentsOptions AddAIAgentFactory( this DurableAgentsOptions options, string name, Func factory, bool enableHttpTrigger, bool enableMcpToolTrigger) { ArgumentNullException.ThrowIfNull(options); ArgumentNullException.ThrowIfNull(name); ArgumentNullException.ThrowIfNull(factory); FunctionsAgentOptions agentOptions = new(); agentOptions.HttpTrigger.IsEnabled = enableHttpTrigger; agentOptions.McpToolTrigger.IsEnabled = enableMcpToolTrigger; options.AddAIAgentFactory(name, factory); s_agentOptions[name] = agentOptions; return options; } /// /// Builds the agentOptions used for dependency injection (read-only copy). /// internal static IReadOnlyDictionary GetAgentOptionsSnapshot() { return new Dictionary(s_agentOptions, StringComparer.OrdinalIgnoreCase); } }