A programming framework for agentic AI
feat: Flesh out App Builder APIs
Jacob Alber20a47ebd43ab
diff --git a/dotnet/src/Microsoft.AutoGen/Contracts/IAgentRuntime.cs b/dotnet/src/Microsoft.AutoGen/Contracts/IAgentRuntime.csindex bc461e74a..a344f90ef 100644--- a/dotnet/src/Microsoft.AutoGen/Contracts/IAgentRuntime.cs+++ b/dotnet/src/Microsoft.AutoGen/Contracts/IAgentRuntime.cs@@ -106,11 +106,10 @@ public interface IAgentRuntime : ISaveState<IAgentRuntime>/// Registers an agent factory with the runtime, associating it with a specific agent type./// The type must be unique./// </summary>- /// <typeparam name="TAgent">The agent type being registered.</typeparam>/// <param name="type">The agent type to associate with the factory.</param>/// <param name="factoryFunc">A function that asynchronously creates the agent instance.</param>/// <returns>A task representing the asynchronous operation, returning the registered <see cref="AgentType"/>.</returns>- public ValueTask<AgentType> RegisterAgentFactoryAsync<TAgent>(AgentType type, Func<AgentId, IAgentRuntime, ValueTask<TAgent>> factoryFunc) where TAgent : IHostableAgent;+ public ValueTask<AgentType> RegisterAgentFactoryAsync(AgentType type, Func<AgentId, IAgentRuntime, ValueTask<IHostableAgent>> factoryFunc);// TODO://public ValueTask<TAgent> TryGetUnderlyingAgentInstanceAsync<TAgent>(AgentId agentId) where TAgent : IHostableAgent;diff --git a/dotnet/src/Microsoft.AutoGen/Core/AgentRuntimeExtensions.cs b/dotnet/src/Microsoft.AutoGen/Core/AgentRuntimeExtensions.csindex bf75a6faa..5a79264c3 100644--- a/dotnet/src/Microsoft.AutoGen/Core/AgentRuntimeExtensions.cs+++ b/dotnet/src/Microsoft.AutoGen/Core/AgentRuntimeExtensions.cs@@ -15,20 +15,19 @@ public static class AgentRuntimeExtensions/// <summary>/// Instantiates and activates an agent asynchronously using dependency injection./// </summary>- /// <typeparam name="TAgent">The type of agent to activate. Must implement <see cref="IHostableAgent"/>.</typeparam>/// <param name="serviceProvider">The service provider used for dependency injection.</param>/// <param name="additionalArguments">Additional arguments to pass to the agent's constructor.</param>/// <returns>A <see cref="ValueTask{T}"/> representing the asynchronous activation of the agent.</returns>- internal static ValueTask<TAgent> ActivateAgentAsync<TAgent>(IServiceProvider serviceProvider, params object[] additionalArguments) where TAgent : IHostableAgent+ internal static ValueTask<IHostableAgent> ActivateAgentAsync(IServiceProvider serviceProvider, Type runtimeType, params object[] additionalArguments){try{- var agent = (TAgent)ActivatorUtilities.CreateInstance(serviceProvider, typeof(TAgent), additionalArguments);+ var agent = (IHostableAgent)ActivatorUtilities.CreateInstance(serviceProvider, runtimeType, additionalArguments);return ValueTask.FromResult(agent);}catch (Exception e){- return ValueTask.FromException<TAgent>(e);+ return ValueTask.FromException<IHostableAgent>(e);}}@@ -42,22 +41,28 @@ public static class AgentRuntimeExtensions/// <param name="additionalArguments">Additional arguments to pass to the agent's constructor.</param>/// <returns>A <see cref="ValueTask{AgentType}"/> representing the asynchronous operation of registering the agent.</returns>public static ValueTask<AgentType> RegisterAgentTypeAsync<TAgent>(this IAgentRuntime runtime, AgentType type, IServiceProvider serviceProvider, params IEnumerable<object> additionalArguments) where TAgent : IHostableAgent+ => RegisterAgentTypeAsync(runtime, type, typeof(TAgent), serviceProvider, additionalArguments);++ public static ValueTask<AgentType> RegisterAgentTypeAsync(this IAgentRuntime runtime, AgentType type, Type runtimeType, IServiceProvider serviceProvider, params IEnumerable<object> additionalArguments){- Func<AgentId, IAgentRuntime, ValueTask<TAgent>> factory = (id, runtime) => ActivateAgentAsync<TAgent>(serviceProvider, [id, runtime, ..additionalArguments]);+ Func<AgentId, IAgentRuntime, ValueTask<IHostableAgent>> factory = (id, runtime) => ActivateAgentAsync(serviceProvider, runtimeType, [id, runtime, .. additionalArguments]);return runtime.RegisterAgentFactoryAsync(type, factory);}private static ISubscriptionDefinition[] BindSubscriptionsForAgentType<T>(AgentType agentType, bool skipClassSubscriptions = false, bool skipDirectMessageSubscription = false)+ => BindSubscriptionsForAgentType(agentType, typeof(T), skipClassSubscriptions, skipDirectMessageSubscription);++ private static ISubscriptionDefinition[] BindSubscriptionsForAgentType(AgentType agentType, Type runtimeType, bool skipClassSubscriptions = false, bool skipDirectMessageSubscription = false){// var topicAttributes = this.GetType().GetCustomAttributes<TopicSubscriptionAttribute>().Select(t => t.Topic);var subscriptions = new List<ISubscriptionDefinition>();if (!skipClassSubscriptions){- var classSubscriptions = typeof(T).GetCustomAttributes<TypeSubscriptionAttribute>().Select(t => t.Bind(agentType));+ var classSubscriptions = runtimeType.GetCustomAttributes<TypeSubscriptionAttribute>().Select(t => t.Bind(agentType));subscriptions.AddRange(classSubscriptions);- var prefixSubscriptions = typeof(T).GetCustomAttributes<TopicPrefixSubscriptionAttribute>().Select(t => t.Bind(agentType));+ var prefixSubscriptions = runtimeType.GetCustomAttributes<TopicPrefixSubscriptionAttribute>().Select(t => t.Bind(agentType));subscriptions.AddRange(prefixSubscriptions);}@@ -70,8 +75,11 @@ public static class AgentRuntimeExtensions}public static async ValueTask RegisterImplicitAgentSubscriptionsAsync<TAgent>(this IAgentRuntime runtime, AgentType type, bool skipClassSubscriptions = false, bool skipDirectMessageSubscription = false) where TAgent : IHostableAgent+ => await RegisterImplicitAgentSubscriptionsAsync(runtime, type, typeof(TAgent), skipClassSubscriptions, skipDirectMessageSubscription);++ public static async ValueTask RegisterImplicitAgentSubscriptionsAsync(this IAgentRuntime runtime, AgentType type, Type runtimeType, bool skipClassSubscriptions = false, bool skipDirectMessageSubscription = false){- var subscriptions = BindSubscriptionsForAgentType<TAgent>(type);+ var subscriptions = BindSubscriptionsForAgentType(type, runtimeType);foreach (var subscription in subscriptions){await runtime.AddSubscriptionAsync(subscription);diff --git a/dotnet/src/Microsoft.AutoGen/Core/AgentsApp.cs b/dotnet/src/Microsoft.AutoGen/Core/AgentsApp.csindex 27f987ab2..8533fc1b4 100644--- a/dotnet/src/Microsoft.AutoGen/Core/AgentsApp.cs+++ b/dotnet/src/Microsoft.AutoGen/Core/AgentsApp.cs@@ -4,6 +4,7 @@using Microsoft.AutoGen.Contracts;using Microsoft.Extensions.DependencyInjection;using Microsoft.Extensions.Hosting;+using System.Reflection;namespace Microsoft.AutoGen.Core;@@ -18,15 +19,39 @@ public class AgentsAppBuilderthis.builder = baseBuilder ?? new HostApplicationBuilder();}- public void AddAgent<TAgent>(string name, bool skipClassSubscriptions = false, bool skipDirectMessageSubscription = false) where TAgent : IHostableAgent+ public void AddAgentsFromAssemblies(){- this.AgentTypeRegistrations.Add(async app => {- var agentType = await app.AgentRuntime.RegisterAgentTypeAsync<TAgent>(name, app.Services);- await app.AgentRuntime.RegisterImplicitAgentSubscriptionsAsync<TAgent>(name, skipClassSubscriptions, skipDirectMessageSubscription);+ this.AddAgentsFromAssemblies(AppDomain.CurrentDomain.GetAssemblies());+ }++ public void AddAgentsFromAssemblies(params Assembly[] assemblies)+ {+ IEnumerable<Type> agentTypes = assemblies.SelectMany(assembly => assembly.GetTypes())+ .Where(type => ReflectionHelper.IsSubclassOfGeneric(type, typeof(BaseAgent))+ && !type.IsAbstract+ // && !type.Name.Equals(nameof(Client))+ );++ foreach (Type agentType in agentTypes)+ {+ // TODO: Expose skipClassSubscriptions and skipDirectMessageSubscription as parameters?+ this.AddAgent(agentType.Name, agentType);+ }+ }++ private void AddAgent(AgentType agentType, Type runtimeType, bool skipClassSubscriptions = false, bool skipDirectMessageSubscription = false)+ {+ this.AgentTypeRegistrations.Add(async app =>+ {+ await app.AgentRuntime.RegisterAgentTypeAsync(agentType, runtimeType, app.Services);+ await app.AgentRuntime.RegisterImplicitAgentSubscriptionsAsync(agentType, runtimeType, skipClassSubscriptions, skipDirectMessageSubscription);return agentType;});}+ public void AddAgent<TAgent>(AgentType agentType, bool skipClassSubscriptions = false, bool skipDirectMessageSubscription = false) where TAgent : IHostableAgent+ => this.AddAgent(agentType, typeof(TAgent), skipClassSubscriptions, skipDirectMessageSubscription);+public async ValueTask<AgentsApp> BuildAsync(){IHost host = this.builder.Build();diff --git a/dotnet/src/Microsoft.AutoGen/Core/InProcessRuntime.cs b/dotnet/src/Microsoft.AutoGen/Core/InProcessRuntime.csindex 32920f686..dd8860d45 100644--- a/dotnet/src/Microsoft.AutoGen/Core/InProcessRuntime.cs+++ b/dotnet/src/Microsoft.AutoGen/Core/InProcessRuntime.cs@@ -176,6 +176,9 @@ public sealed class InProcessRuntime : IAgentRuntime}public ValueTask<AgentType> RegisterAgentFactoryAsync<TAgent>(AgentType type, Func<AgentId, IAgentRuntime, ValueTask<TAgent>> factoryFunc) where TAgent : IHostableAgent+ => this.RegisterAgentFactoryAsync(type, async (agentId, runtime) => await factoryFunc(agentId, runtime));++ public ValueTask<AgentType> RegisterAgentFactoryAsync(AgentType type, Func<AgentId, IAgentRuntime, ValueTask<IHostableAgent>> factoryFunc){if (this.agentFactories.ContainsKey(type)){diff --git a/dotnet/src/Microsoft.AutoGen/Core/ReflectionHelper.cs b/dotnet/src/Microsoft.AutoGen/Core/ReflectionHelper.csnew file mode 100644index 000000000..395162719--- /dev/null+++ b/dotnet/src/Microsoft.AutoGen/Core/ReflectionHelper.cs@@ -0,0 +1,24 @@+// Copyright (c) Microsoft Corporation. All rights reserved.+// ReflectionHelper.cs++namespace Microsoft.AutoGen.Core;++public sealed class ReflectionHelper+{+ public static bool IsSubclassOfGeneric(Type type, Type genericBaseType)+ {+ while (type != null && type != typeof(object))+ {+ if (genericBaseType == (type.IsGenericType ? type.GetGenericTypeDefinition() : type))+ {+ return true;+ }+ if (type.BaseType == null)+ {+ return false;+ }+ type = type.BaseType;+ }+ return false;+ }+}