// Copyright (c) Microsoft Corporation. All rights reserved. // AgentRuntimeExtensions.cs using Microsoft.Extensions.DependencyInjection; namespace Microsoft.AutoGen.Contracts.Python; /// /// Provides extension methods for managing and registering agents within an . /// public static class AgentRuntimeExtensions { /// /// Instantiates and activates an agent asynchronously using dependency injection. /// /// The type of agent to activate. Must implement . /// The service provider used for dependency injection. /// Additional arguments to pass to the agent's constructor. /// A representing the asynchronous activation of the agent. internal static ValueTask ActivateAgentAsync(IServiceProvider serviceProvider, params object[] additionalArguments) where TAgent : IHostableAgent { try { var agent = (TAgent)ActivatorUtilities.CreateInstance(serviceProvider, typeof(TAgent), additionalArguments); return ValueTask.FromResult(agent); } catch (Exception e) { return ValueTask.FromException(e); } } /// /// Registers an agent type with the runtime, providing a factory function to create instances of the agent. /// /// The type of agent being registered. Must implement . /// The where the agent will be registered. /// The representing the type of agent. /// The service provider used for dependency injection. /// Additional arguments to pass to the agent's constructor. /// A representing the asynchronous operation of registering the agent. public static ValueTask RegisterAgentTypeAsync(this IAgentRuntime runtime, AgentType type, IServiceProvider serviceProvider, params object[] additionalArguments) where TAgent : IHostableAgent { Func> factory = () => ActivateAgentAsync(serviceProvider, additionalArguments); return runtime.RegisterAgentFactoryAsync(type, factory); } }