mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
67a8147151
* Python: Add Scaffolding for Durable AzureFunctions package to Agent Framework (#1823) * Add scafolding * update readme * add code owners and label * update owners * .NET: Durable extension: initial src and unit tests (#1900) * Python: Add Durable Agent Wrapper code (#1913) * add initial changes * Move code and add single sample * Update logger * Remove unused code * address PR comments * cleanup code and address comments --------- Co-authored-by: Dmytro Struk <13853051+dmytrostruk@users.noreply.github.com> * Azure Functions .NET samples (#1939) * Python: Add Unit tests for Azurefunctions package (#1976) * Add Unit tests for Azurefunctions * remove duplicate import * .NET: [Feature Branch] Migrate state schema updates and support for agents as MCP tools (#1979) * Python: Add more samples for Azure Functions (#1980) * Move all samples * fix comments * remove dead lines * Make samples simpler * .NET: [Feature Branch] Durable Task extension integration tests (#2017) * .NET: [Feature Branch] Update OpenAI config for integration tests (#2063) * Python: Add Integration tests for AzureFunctions (#2020) * Add Integration tests * Remove DTS extension * Apply suggestions from code review Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Add pyi file for type safety * Add samples in readme * Updated all readme instructions * Address comments * Update readmes * Fix requirements * Address comments --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * .NET: [Feature Branch] Update dotnet-build-and-test.yml to support integration tests (#2070) Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Fix DTS startup issue and improve logging (#2103) * .NET: [Feature Branch] Introduce Azure OpenAI config for .NET pipeline (#2106) Also fixes an issue where we were trying to start docker containers for integration tests on Windows, which doesn't work. Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Fix uv.lock after merge * Python: Add README for Azure Functions samples setup (#2100) * Add README for Azure Functions samples setup Added setup instructions for Azure Functions samples, including environment setup, virtual environment creation, and running samples. * Update python/samples/getting_started/azure_functions/README.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Apply suggestion from @Copilot Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Laveesh Rohra <larohra@microsoft.com> * Fix or remove broken markdown file links (#2115) * .NET: [Feature Branch] Update HTTP API to be consistent across languages (#2118) * Python: Fix AzureFunctions Integration Tests (#2116) * Add Identity Auth to samples * Update python/samples/getting_started/azure_functions/README.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update python/samples/getting_started/azure_functions/01_single_agent/function_app.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update python/samples/getting_started/azure_functions/02_multi_agent/function_app.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update python/samples/getting_started/azure_functions/06_multi_agent_orchestration_conditionals/README.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Python: Fix Http Schema (#2112) * Rename to threadid * Respond in plain text * Make snake-case * Add http prefix * rename to wait-for-response * Add query param check * address comments * .NET: Remove IsPackable=false in preparation for nuget release (#2142) * Python: Move `azurefunctions` to `azure` for import (#2141) * Move import to Azure * fix mypy * Update python/packages/azurefunctions/README.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Add missing types * Address comments --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update python/packages/azurefunctions/pyproject.toml Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update python/packages/azurefunctions/agent_framework_azurefunctions/__init__.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Fix imports * Address PR feedback from westey-m (#2150) - Adds a link from the /dotnet/samples/README.md to /dotnet/samples/AzureFunctions - Make DurableAgentThread deserialization internal for future-proofing - Update JSON serialization logic to address recently discovered issues with source generator serialization * Address comments (#2160) --------- Co-authored-by: Laveesh Rohra <larohra@microsoft.com> Co-authored-by: Chris Gillum <cgillum@microsoft.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Anirudh Garg <anirudhg@microsoft.com>
138 lines
7.2 KiB
C#
138 lines
7.2 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
using Microsoft.Agents.AI.DurableTask;
|
|
|
|
namespace Microsoft.Agents.AI.Hosting.AzureFunctions;
|
|
|
|
/// <summary>
|
|
/// Provides extension methods for registering and configuring AI agents in the context of the Azure Functions hosting environment.
|
|
/// </summary>
|
|
public static class DurableAgentsOptionsExtensions
|
|
{
|
|
// Registry of agent options.
|
|
private static readonly Dictionary<string, FunctionsAgentOptions> s_agentOptions = new(StringComparer.OrdinalIgnoreCase);
|
|
|
|
/// <summary>
|
|
/// Adds an AI agent to the specified DurableAgentsOptions instance and optionally configures agent-specific
|
|
/// options.
|
|
/// </summary>
|
|
/// <param name="options">The DurableAgentsOptions instance to which the AI agent will be added.</param>
|
|
/// <param name="agent">The AI agent to add. The agent's Name property must not be null or empty.</param>
|
|
/// <param name="configure">An optional delegate to configure agent-specific options. If null, default options are used.</param>
|
|
/// <returns>The updated <see cref="DurableAgentsOptions"/> instance containing the added AI agent.</returns>
|
|
public static DurableAgentsOptions AddAIAgent(
|
|
this DurableAgentsOptions options,
|
|
AIAgent agent,
|
|
Action<FunctionsAgentOptions>? 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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds an AI agent to the specified options and configures trigger support for HTTP and MCP tool invocations.
|
|
/// </summary>
|
|
/// <remarks>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.</remarks>
|
|
/// <param name="options">The options collection to which the AI agent will be added. Cannot be null.</param>
|
|
/// <param name="agent">The AI agent to add. The agent's Name property must not be null or empty.</param>
|
|
/// <param name="enableHttpTrigger">true to enable an HTTP trigger for the agent; otherwise, false.</param>
|
|
/// <param name="enableMcpToolTrigger">true to enable an MCP tool trigger for the agent; otherwise, false.</param>
|
|
/// <returns>The updated <see cref="DurableAgentsOptions"/> instance with the specified AI agent and trigger configuration applied.</returns>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Registers an AI agent factory with the specified name and optional configuration in the provided
|
|
/// DurableAgentsOptions instance.
|
|
/// </summary>
|
|
/// <remarks>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.</remarks>
|
|
/// <param name="options">The DurableAgentsOptions instance to which the AI agent factory will be added. Cannot be null.</param>
|
|
/// <param name="name">The unique name used to identify the AI agent factory. Cannot be null.</param>
|
|
/// <param name="factory">A delegate that creates an AIAgent instance using the provided IServiceProvider. Cannot be null.</param>
|
|
/// <param name="configure">An optional action to configure FunctionsAgentOptions for the agent factory. If null, default options are used.</param>
|
|
/// <returns>The updated DurableAgentsOptions instance containing the registered AI agent factory.</returns>
|
|
public static DurableAgentsOptions AddAIAgentFactory(
|
|
this DurableAgentsOptions options,
|
|
string name,
|
|
Func<IServiceProvider, AIAgent> factory,
|
|
Action<FunctionsAgentOptions>? 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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Registers an AI agent factory with the specified name and configures trigger options for the agent.
|
|
/// </summary>
|
|
/// <remarks>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.</remarks>
|
|
/// <param name="options">The options object to which the AI agent factory will be added. Cannot be null.</param>
|
|
/// <param name="name">The unique name used to identify the AI agent factory. Cannot be null.</param>
|
|
/// <param name="factory">A delegate that creates an instance of the AI agent using the provided service provider. Cannot be null.</param>
|
|
/// <param name="enableHttpTrigger">true to enable the HTTP trigger for the agent; otherwise, false.</param>
|
|
/// <param name="enableMcpToolTrigger">true to enable the MCP tool trigger for the agent; otherwise, false.</param>
|
|
/// <returns>The same DurableAgentsOptions instance, allowing for method chaining.</returns>
|
|
public static DurableAgentsOptions AddAIAgentFactory(
|
|
this DurableAgentsOptions options,
|
|
string name,
|
|
Func<IServiceProvider, AIAgent> 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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Builds the agentOptions used for dependency injection (read-only copy).
|
|
/// </summary>
|
|
internal static IReadOnlyDictionary<string, FunctionsAgentOptions> GetAgentOptionsSnapshot()
|
|
{
|
|
return new Dictionary<string, FunctionsAgentOptions>(s_agentOptions, StringComparer.OrdinalIgnoreCase);
|
|
}
|
|
}
|