A programming framework for agentic AI
Rysweet 4671 remove iagent base (#4673)
Ryan Sweet7d4bf9b957c0
diff --git a/dotnet/samples/dev-team/DevTeam.Backend/Agents/Sandbox.cs b/dotnet/samples/dev-team/DevTeam.Backend/Agents/Sandbox.csindex 306ebc945..19b0db005 100644--- a/dotnet/samples/dev-team/DevTeam.Backend/Agents/Sandbox.cs+++ b/dotnet/samples/dev-team/DevTeam.Backend/Agents/Sandbox.cs@@ -3,7 +3,7 @@// namespace DevTeam.Backend;-// public sealed class Sandbox : AgentBase+// public sealed class Sandbox : Agent// {// private const string ReminderName = "SandboxRunReminder";// private readonly IManageAzure _azService;diff --git a/dotnet/src/Microsoft.AutoGen/Abstractions/IAgentBase.cs b/dotnet/src/Microsoft.AutoGen/Abstractions/IAgentBase.csdeleted file mode 100644index ee7b9e745..000000000--- a/dotnet/src/Microsoft.AutoGen/Abstractions/IAgentBase.cs+++ /dev/null@@ -1,23 +0,0 @@-// Copyright (c) Microsoft Corporation. All rights reserved.-// IAgentBase.cs--using Google.Protobuf;--namespace Microsoft.AutoGen.Abstractions;--public interface IAgentBase-{- // Properties- AgentId AgentId { get; }- IAgentRuntime Context { get; }-- // Methods- Task CallHandler(CloudEvent item);- Task<RpcResponse> HandleRequest(RpcRequest request);- void ReceiveMessage(Message message);- Task StoreAsync(AgentState state, CancellationToken cancellationToken = default);- Task<T> ReadAsync<T>(AgentId agentId, CancellationToken cancellationToken = default) where T : IMessage, new();- ValueTask PublishEventAsync(CloudEvent item, CancellationToken cancellationToken = default);- ValueTask PublishEventAsync(string topic, IMessage evt, CancellationToken cancellationToken = default);- List<string> Subscribe(string topic);-}diff --git a/dotnet/src/Microsoft.AutoGen/Agents/Agent.cs b/dotnet/src/Microsoft.AutoGen/Agents/Agent.csindex 39ba48733..795e09ea1 100644--- a/dotnet/src/Microsoft.AutoGen/Agents/Agent.cs+++ b/dotnet/src/Microsoft.AutoGen/Agents/Agent.cs@@ -1,6 +1,7 @@// Copyright (c) Microsoft Corporation. All rights reserved.// Agent.cs+using System.Collections.Concurrent;using System.Diagnostics;using System.Reflection;using System.Text;@@ -12,16 +13,15 @@ using Microsoft.Extensions.Logging;namespace Microsoft.AutoGen.Agents;-public abstract class Agent : IAgentBase, IHandle+public abstract class Agent : IHandle{public static readonly ActivitySource s_source = new("AutoGen.Agent");public AgentId AgentId => _runtime.AgentId;private readonly object _lock = new();- private readonly Dictionary<string, TaskCompletionSource<RpcResponse>> _pendingRequests = [];+ private readonly ConcurrentDictionary<string, TaskCompletionSource<RpcResponse>> _pendingRequests = [];private readonly Channel<object> _mailbox = Channel.CreateUnbounded<object>();private readonly IAgentRuntime _runtime;- public string Route { get; set; } = "base";protected internal ILogger<Agent> _logger;public IAgentRuntime Context => _runtime;@@ -235,18 +235,15 @@ public abstract class Agent : IAgentBase, IHandleactivity?.SetTag("peer.service", target.ToString());var completion = new TaskCompletionSource<RpcResponse>(TaskCreationOptions.RunContinuationsAsynchronously);- _runtime.Update(request, activity);+ Context!.Update(request, activity);await this.InvokeWithActivityAsync(- static async ((Agent Agent, RpcRequest Request, TaskCompletionSource<RpcResponse>) state, CancellationToken ct) =>+ static async (state, ct) =>{var (self, request, completion) = state;- lock (self._lock)- {- self._pendingRequests[request.RequestId] = completion;- }+ self._pendingRequests.AddOrUpdate(request.RequestId, _ => completion, (_, __) => completion);- await state.Agent._runtime.SendRequestAsync(state.Agent, state.Request).ConfigureAwait(false);+ await state.Item1.Context!.SendRequestAsync(state.Item1, state.request, ct).ConfigureAwait(false);await completion.Task.ConfigureAwait(false);},diff --git a/dotnet/src/Microsoft.AutoGen/Agents/AgentRuntime.cs b/dotnet/src/Microsoft.AutoGen/Agents/AgentRuntime.csindex 8dbaf73cb..20295f8f9 100644--- a/dotnet/src/Microsoft.AutoGen/Agents/AgentRuntime.cs+++ b/dotnet/src/Microsoft.AutoGen/Agents/AgentRuntime.cs@@ -14,8 +14,8 @@ internal sealed class AgentRuntime(AgentId agentId, IAgentWorker worker, ILoggerprivate readonly IAgentWorker worker = worker;public AgentId AgentId { get; } = agentId;- public ILogger<Agent> Logger { get; } = logger;- public IAgentBase? AgentInstance { get; set; }+ private ILogger<Agent> Logger { get; } = logger;+ public Agent? AgentInstance { get; set; }private DistributedContextPropagator DistributedContextPropagator { get; } = distributedContextPropagator;public (string?, string?) GetTraceIdAndState(IDictionary<string, string> metadata){@@ -79,7 +79,7 @@ internal sealed class AgentRuntime(AgentId agentId, IAgentWorker worker, ILoggerresponse.RequestId = request.RequestId;await worker.SendResponseAsync(response, cancellationToken).ConfigureAwait(false);}- public async ValueTask SendRequestAsync(IAgentBase agent, RpcRequest request, CancellationToken cancellationToken = default)+ public async ValueTask SendRequestAsync(Agent agent, RpcRequest request, CancellationToken cancellationToken = default){await worker.SendRequestAsync(agent, request, cancellationToken).ConfigureAwait(false);}diff --git a/dotnet/src/Microsoft.AutoGen/Agents/Agents/AIAgent/InferenceAgent.cs b/dotnet/src/Microsoft.AutoGen/Agents/Agents/AIAgent/InferenceAgent.csindex c4c2163a0..ea37f0aba 100644--- a/dotnet/src/Microsoft.AutoGen/Agents/Agents/AIAgent/InferenceAgent.cs+++ b/dotnet/src/Microsoft.AutoGen/Agents/Agents/AIAgent/InferenceAgent.cs@@ -2,7 +2,6 @@// InferenceAgent.csusing Google.Protobuf;-using Microsoft.AutoGen.Abstractions;using Microsoft.Extensions.AI;namespace Microsoft.AutoGen.Agents;public abstract class InferenceAgent<T>(diff --git a/dotnet/src/Microsoft.AutoGen/Agents/Agents/AIAgent/SKAiAgent.cs b/dotnet/src/Microsoft.AutoGen/Agents/Agents/AIAgent/SKAiAgent.csindex 80dfa49f0..4d5c79d8f 100644--- a/dotnet/src/Microsoft.AutoGen/Agents/Agents/AIAgent/SKAiAgent.cs+++ b/dotnet/src/Microsoft.AutoGen/Agents/Agents/AIAgent/SKAiAgent.cs@@ -3,7 +3,6 @@using System.Globalization;using System.Text;-using Microsoft.AutoGen.Abstractions;using Microsoft.SemanticKernel;using Microsoft.SemanticKernel.Connectors.OpenAI;using Microsoft.SemanticKernel.Memory;diff --git a/dotnet/src/Microsoft.AutoGen/Agents/Agents/IOAgent/ConsoleAgent/IHandleConsole.cs b/dotnet/src/Microsoft.AutoGen/Agents/Agents/IOAgent/ConsoleAgent/IHandleConsole.csindex 60df58e92..0d7a68400 100644--- a/dotnet/src/Microsoft.AutoGen/Agents/Agents/IOAgent/ConsoleAgent/IHandleConsole.cs+++ b/dotnet/src/Microsoft.AutoGen/Agents/Agents/IOAgent/ConsoleAgent/IHandleConsole.cs@@ -8,7 +8,6 @@ namespace Microsoft.AutoGen.Agents;public interface IHandleConsole : IHandle<Output>, IHandle<Input>{- string Route { get; }AgentId AgentId { get; }ValueTask PublishMessageAsync<T>(T message, string? source = null, CancellationToken token = default) where T : IMessage;diff --git a/dotnet/src/Microsoft.AutoGen/Abstractions/IAgentRuntime.cs b/dotnet/src/Microsoft.AutoGen/Agents/IAgentRuntime.cssimilarity index 81%rename from dotnet/src/Microsoft.AutoGen/Abstractions/IAgentRuntime.csrename to dotnet/src/Microsoft.AutoGen/Agents/IAgentRuntime.csindex 6b3d4f98c..53da0713a 100644--- a/dotnet/src/Microsoft.AutoGen/Abstractions/IAgentRuntime.cs+++ b/dotnet/src/Microsoft.AutoGen/Agents/IAgentRuntime.cs@@ -2,17 +2,18 @@// IAgentRuntime.csusing System.Diagnostics;+using Microsoft.AutoGen.Abstractions;-namespace Microsoft.AutoGen.Abstractions;+namespace Microsoft.AutoGen.Agents;public interface IAgentRuntime{AgentId AgentId { get; }- IAgentBase? AgentInstance { get; set; }+ Agent? AgentInstance { get; set; }ValueTask StoreAsync(AgentState value, CancellationToken cancellationToken = default);ValueTask<AgentState> ReadAsync(AgentId agentId, CancellationToken cancellationToken = default);ValueTask SendResponseAsync(RpcRequest request, RpcResponse response, CancellationToken cancellationToken = default);- ValueTask SendRequestAsync(IAgentBase agent, RpcRequest request, CancellationToken cancellationToken = default);+ ValueTask SendRequestAsync(Agent agent, RpcRequest request, CancellationToken cancellationToken = default);ValueTask SendMessageAsync(Message message, CancellationToken cancellationToken = default);ValueTask PublishEventAsync(CloudEvent @event, CancellationToken cancellationToken = default);void Update(RpcRequest request, Activity? activity);diff --git a/dotnet/src/Microsoft.AutoGen/Abstractions/IAgentWorker.cs b/dotnet/src/Microsoft.AutoGen/Agents/IAgentWorker.cssimilarity index 76%rename from dotnet/src/Microsoft.AutoGen/Abstractions/IAgentWorker.csrename to dotnet/src/Microsoft.AutoGen/Agents/IAgentWorker.csindex 04517cd11..dbc3a737d 100644--- a/dotnet/src/Microsoft.AutoGen/Abstractions/IAgentWorker.cs+++ b/dotnet/src/Microsoft.AutoGen/Agents/IAgentWorker.cs@@ -1,11 +1,12 @@// Copyright (c) Microsoft Corporation. All rights reserved.// IAgentWorker.cs-namespace Microsoft.AutoGen.Abstractions;+using Microsoft.AutoGen.Abstractions;+namespace Microsoft.AutoGen.Agents;public interface IAgentWorker{ValueTask PublishEventAsync(CloudEvent evt, CancellationToken cancellationToken = default);- ValueTask SendRequestAsync(IAgentBase agent, RpcRequest request, CancellationToken cancellationToken = default);+ ValueTask SendRequestAsync(Agent agent, RpcRequest request, CancellationToken cancellationToken = default);ValueTask SendResponseAsync(RpcResponse response, CancellationToken cancellationToken = default);ValueTask SendMessageAsync(Message message, CancellationToken cancellationToken = default);ValueTask StoreAsync(AgentState value, CancellationToken cancellationToken = default);diff --git a/dotnet/src/Microsoft.AutoGen/Agents/Services/AgentWorker.cs b/dotnet/src/Microsoft.AutoGen/Agents/Services/AgentWorker.csindex ffa1aea1f..8a1f8229d 100644--- a/dotnet/src/Microsoft.AutoGen/Agents/Services/AgentWorker.cs+++ b/dotnet/src/Microsoft.AutoGen/Agents/Services/AgentWorker.cs@@ -16,11 +16,11 @@ public class AgentWorker :IAgentWorker{private readonly ConcurrentDictionary<string, Type> _agentTypes = new();- private readonly ConcurrentDictionary<(string Type, string Key), IAgentBase> _agents = new();+ private readonly ConcurrentDictionary<(string Type, string Key), Agent> _agents = new();private readonly ILogger<AgentWorker> _logger;private readonly Channel<object> _mailbox = Channel.CreateUnbounded<object>();private readonly ConcurrentDictionary<string, AgentState> _agentStates = new();- private readonly ConcurrentDictionary<string, (IAgentBase Agent, string OriginalRequestId)> _pendingClientRequests = new();+ private readonly ConcurrentDictionary<string, (Agent Agent, string OriginalRequestId)> _pendingClientRequests = new();private readonly CancellationTokenSource _shutdownCts;private readonly IServiceProvider _serviceProvider;private readonly IEnumerable<Tuple<string, Type>> _configuredAgentTypes;@@ -54,7 +54,7 @@ public class AgentWorker :agent.ReceiveMessage(new Message { CloudEvent = cloudEvent });}}- public async ValueTask SendRequestAsync(IAgentBase agent, RpcRequest request, CancellationToken cancellationToken = default)+ public async ValueTask SendRequestAsync(Agent agent, RpcRequest request, CancellationToken cancellationToken = default){var requestId = Guid.NewGuid().ToString();_pendingClientRequests[requestId] = (agent, request.RequestId);@@ -190,7 +190,7 @@ public class AgentWorker :{}}- private IAgentBase GetOrActivateAgent(AgentId agentId)+ private Agent GetOrActivateAgent(AgentId agentId){if (!_agents.TryGetValue((agentId.Type, agentId.Key), out var agent)){diff --git a/dotnet/src/Microsoft.AutoGen/Agents/Services/Grpc/GrpcAgentWorker.cs b/dotnet/src/Microsoft.AutoGen/Agents/Services/Grpc/GrpcAgentWorker.csindex 34ef328dd..23d349102 100644--- a/dotnet/src/Microsoft.AutoGen/Agents/Services/Grpc/GrpcAgentWorker.cs+++ b/dotnet/src/Microsoft.AutoGen/Agents/Services/Grpc/GrpcAgentWorker.cs@@ -25,8 +25,8 @@ public sealed class GrpcAgentWorker({private readonly object _channelLock = new();private readonly ConcurrentDictionary<string, Type> _agentTypes = new();- private readonly ConcurrentDictionary<(string Type, string Key), IAgentBase> _agents = new();- private readonly ConcurrentDictionary<string, (IAgentBase Agent, string OriginalRequestId)> _pendingRequests = new();+ private readonly ConcurrentDictionary<(string Type, string Key), Agent> _agents = new();+ private readonly ConcurrentDictionary<string, (Agent Agent, string OriginalRequestId)> _pendingRequests = new();private readonly Channel<(Message Message, TaskCompletionSource WriteCompletionSource)> _outboundMessagesChannel = Channel.CreateBounded<(Message, TaskCompletionSource)>(new BoundedChannelOptions(1024){AllowSynchronousContinuations = true,@@ -187,7 +187,7 @@ public sealed class GrpcAgentWorker(item.WriteCompletionSource.TrySetCanceled();}}- private IAgentBase GetOrActivateAgent(AgentId agentId)+ private Agent GetOrActivateAgent(AgentId agentId){if (!_agents.TryGetValue((agentId.Type, agentId.Key), out var agent)){@@ -275,7 +275,7 @@ public sealed class GrpcAgentWorker(await WriteChannelAsync(new Message { Response = response }, cancellationToken).ConfigureAwait(false);}// new is intentional- public new async ValueTask SendRequestAsync(IAgentBase agent, RpcRequest request, CancellationToken cancellationToken = default)+ public new async ValueTask SendRequestAsync(Agent agent, RpcRequest request, CancellationToken cancellationToken = default){var requestId = Guid.NewGuid().ToString();_pendingRequests[requestId] = (agent, request.RequestId);diff --git a/dotnet/src/Microsoft.AutoGen/Agents/Services/HostBuilderExtensions.cs b/dotnet/src/Microsoft.AutoGen/Agents/Services/HostBuilderExtensions.csindex 533349125..7bc87323a 100644--- a/dotnet/src/Microsoft.AutoGen/Agents/Services/HostBuilderExtensions.cs+++ b/dotnet/src/Microsoft.AutoGen/Agents/Services/HostBuilderExtensions.cs@@ -60,7 +60,8 @@ public static class HostBuilderExtensionsvar eventsMap = AppDomain.CurrentDomain.GetAssemblies().SelectMany(assembly => assembly.GetTypes())- .Where(type => type != null && ReflectionHelper.IsSubclassOfGeneric(type, typeof(Agent)) && !type.IsAbstract)+ .Where(type => ReflectionHelper.IsSubclassOfGeneric(type, typeof(Agent)) && !type.IsAbstract)+.Select(t => (t, t.GetInterfaces().Where(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IHandle<>)).Select(i => (GetMessageDescriptor(i.GetGenericArguments().First())?.FullName ?? "")).ToHashSet()))