// Copyright (c) Microsoft Corporation. All rights reserved. // AgentRuntimeExtensions.cs using Microsoft.AutoGen.Core.Python; using Microsoft.Extensions.DependencyInjection; using System.Reflection; 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 IEnumerable additionalArguments) where TAgent : IHostableAgent { Func> factory = (id, runtime) => ActivateAgentAsync(serviceProvider, [id, runtime, ..additionalArguments]); return runtime.RegisterAgentFactoryAsync(type, factory); } private static ISubscriptionDefinition[] BindSubscriptionsForAgentType(AgentType agentType, bool skipClassSubscriptions = false, bool skipDirectMessageSubscription = false) { // var topicAttributes = this.GetType().GetCustomAttributes().Select(t => t.Topic); var subscriptions = new List(); if (!skipClassSubscriptions) { var classSubscriptions = typeof(T).GetCustomAttributes().Select(t => t.Bind(agentType)); subscriptions.AddRange(classSubscriptions); var prefixSubscriptions = typeof(T).GetCustomAttributes().Select(t => t.Bind(agentType)); subscriptions.AddRange(prefixSubscriptions); } if (!skipDirectMessageSubscription) { subscriptions.Add(new TypePrefixSubscription(agentType.Name + ":", agentType)); } return subscriptions.ToArray(); } public static async ValueTask RegisterImplicitAgentSubscriptionsAsync(this IAgentRuntime runtime, AgentType type, bool skipClassSubscriptions = false, bool skipDirectMessageSubscription = false) where TAgent : IHostableAgent { var subscriptions = BindSubscriptionsForAgentType(type); foreach (var subscription in subscriptions) { await runtime.AddSubscriptionAsync(subscription); } } }