A programming framework for agentic AI
Agentbase refactor (#3980)
Ryan Sweet0f4dd0cc6dd3
diff --git a/dotnet/samples/Hello/HelloAgent/README.md b/dotnet/samples/Hello/HelloAgent/README.mdindex 795eed072..2de64ebaa 100644--- a/dotnet/samples/Hello/HelloAgent/README.md+++ b/dotnet/samples/Hello/HelloAgent/README.md@@ -118,4 +118,4 @@ message ReadmeRequested {</ItemGroup>```-You can send messages using the [```Microsoft.AutoGen.Agents.AgentClient``` class](autogen/dotnet/src/Microsoft.AutoGen/Agents/AgentClient.cs). Messages are wrapped in [the CloudEvents specification](https://cloudevents.io) and sent to the event bus.+You can send messages using the [```Microsoft.AutoGen.Agents.AgentWorker``` class](autogen/dotnet/src/Microsoft.AutoGen/Agents/AgentWorker.cs). Messages are wrapped in [the CloudEvents specification](https://cloudevents.io) and sent to the event bus.diff --git a/dotnet/samples/Hello/HelloAgentState/README.md b/dotnet/samples/Hello/HelloAgentState/README.mdindex 06c488318..0079005d2 100644--- a/dotnet/samples/Hello/HelloAgentState/README.md+++ b/dotnet/samples/Hello/HelloAgentState/README.md@@ -117,7 +117,7 @@ message ReadmeRequested {</ItemGroup>```-You can send messages using the [```Microsoft.AutoGen.Agents``` class](autogen/dotnet/src/Microsoft.AutoGen/Agents/AgentClient.cs). Messages are wrapped in [the CloudEvents specification](https://cloudevents.io) and sent to the event bus.+You can send messages using the [```Microsoft.AutoGen.Agents.AgentWorker``` class](autogen/dotnet/src/Microsoft.AutoGen/Agents/AgentWorker.cs). Messages are wrapped in [the CloudEvents specification](https://cloudevents.io) and sent to the event bus.### Managing Statediff --git a/dotnet/samples/dev-team/DevTeam.Backend/Program.cs b/dotnet/samples/dev-team/DevTeam.Backend/Program.csindex d860f4050..9de188bdf 100644--- a/dotnet/samples/dev-team/DevTeam.Backend/Program.cs+++ b/dotnet/samples/dev-team/DevTeam.Backend/Program.cs@@ -23,7 +23,7 @@ builder.AddAgentWorker(builder.Configuration["AGENT_HOST"]!)//.AddAgent<Sandbox>(nameof(Sandbox)).AddAgent<Hubber>(nameof(Hubber));-builder.Services.AddSingleton<AgentClient>();+builder.Services.AddSingleton<AgentWorker>();builder.Services.AddSingleton<WebhookEventProcessor, GithubWebHookProcessor>();builder.Services.AddSingleton<GithubAuthService>();builder.Services.AddSingleton<IManageAzure, AzureService>();diff --git a/dotnet/samples/dev-team/DevTeam.Backend/Services/GithubWebHookProcessor.cs b/dotnet/samples/dev-team/DevTeam.Backend/Services/GithubWebHookProcessor.csindex 85b407c4e..e72b511e2 100644--- a/dotnet/samples/dev-team/DevTeam.Backend/Services/GithubWebHookProcessor.cs+++ b/dotnet/samples/dev-team/DevTeam.Backend/Services/GithubWebHookProcessor.cs@@ -10,10 +10,10 @@ using Octokit.Webhooks.Models;namespace DevTeam.Backend;-public sealed class GithubWebHookProcessor(ILogger<GithubWebHookProcessor> logger, AgentClient client) : WebhookEventProcessor+public sealed class GithubWebHookProcessor(ILogger<GithubWebHookProcessor> logger, AgentWorker client) : WebhookEventProcessor{private readonly ILogger<GithubWebHookProcessor> _logger = logger;- private readonly AgentClient _client = client;+ private readonly AgentWorker _client = client;protected override async Task ProcessIssuesWebhookAsync(WebhookHeaders headers, IssuesEvent issuesEvent, IssuesAction action){diff --git a/dotnet/src/Microsoft.AutoGen/Agents/AgentBase.cs b/dotnet/src/Microsoft.AutoGen/Agents/AgentBase.csindex 62779f836..d4a6ca0f4 100644--- a/dotnet/src/Microsoft.AutoGen/Agents/AgentBase.cs+++ b/dotnet/src/Microsoft.AutoGen/Agents/AgentBase.cs@@ -8,18 +8,17 @@ using Microsoft.Extensions.Logging;namespace Microsoft.AutoGen.Agents;-public abstract class AgentBase+public abstract class AgentBase : IAgentBase{public static readonly ActivitySource s_source = new("AutoGen.Agent");+ public AgentId AgentId => _context.AgentId;private readonly object _lock = new();private readonly Dictionary<string, TaskCompletionSource<RpcResponse>> _pendingRequests = [];private readonly Channel<object> _mailbox = Channel.CreateUnbounded<object>();private readonly IAgentContext _context;-- protected internal AgentId AgentId => _context.AgentId;protected internal ILogger Logger => _context.Logger;- protected internal IAgentContext Context => _context;+ public IAgentContext Context => _context;protected readonly EventTypes EventTypes;protected AgentBase(IAgentContext context, EventTypes eventTypes)@@ -54,7 +53,7 @@ public abstract class AgentBase}}- internal void ReceiveMessage(Message message) => _mailbox.Writer.TryWrite(message);+ public void ReceiveMessage(Message message) => _mailbox.Writer.TryWrite(message);private async Task RunMessagePump(){@@ -79,7 +78,7 @@ public abstract class AgentBase}}- private async Task HandleRpcMessage(Message msg)+ protected internal async Task HandleRpcMessage(Message msg){switch (msg.MessageCase){@@ -108,12 +107,12 @@ public abstract class AgentBasebreak;}}- protected async Task Store(AgentState state)+ public async Task Store(AgentState state){await _context.Store(state).ConfigureAwait(false);return;}- protected async Task<T> Read<T>(AgentId agentId) where T : IMessage, new()+ public async Task<T> Read<T>(AgentId agentId) where T : IMessage, new(){var agentstate = await _context.Read(agentId).ConfigureAwait(false);return agentstate.FromAgentState<T>();@@ -132,7 +131,6 @@ public abstract class AgentBasecompletion.SetResult(response);}-private async Task OnRequestCore(RpcRequest request){RpcResponse response;@@ -193,7 +191,7 @@ public abstract class AgentBasereturn await completion.Task.ConfigureAwait(false);}- protected async ValueTask PublishEvent(CloudEvent item)+ public async ValueTask PublishEvent(CloudEvent item){var activity = s_source.StartActivity($"PublishEvent '{item.Type}'", ActivityKind.Client, Activity.Current?.Context ?? default);activity?.SetTag("peer.service", $"{item.Type}/{item.Source}");@@ -225,5 +223,5 @@ public abstract class AgentBasereturn Task.CompletedTask;}- protected virtual Task<RpcResponse> HandleRequest(RpcRequest request) => Task.FromResult(new RpcResponse { Error = "Not implemented" });+ public virtual Task<RpcResponse> HandleRequest(RpcRequest request) => Task.FromResult(new RpcResponse { Error = "Not implemented" });}diff --git a/dotnet/src/Microsoft.AutoGen/Agents/AgentClient.cs b/dotnet/src/Microsoft.AutoGen/Agents/AgentClient.csdeleted file mode 100644index 369d717af..000000000--- a/dotnet/src/Microsoft.AutoGen/Agents/AgentClient.cs+++ /dev/null@@ -1,46 +0,0 @@-using System.Diagnostics;-using Google.Protobuf;-using Microsoft.AutoGen.Abstractions;-using Microsoft.Extensions.DependencyInjection;-using Microsoft.Extensions.Logging;--namespace Microsoft.AutoGen.Agents;-public sealed class AgentClient(ILogger<AgentClient> logger, AgentWorkerRuntime runtime, DistributedContextPropagator distributedContextPropagator,- [FromKeyedServices("EventTypes")] EventTypes eventTypes)- : AgentBase(new ClientContext(logger, runtime, distributedContextPropagator), eventTypes)-{- public async ValueTask PublishEventAsync(CloudEvent evt) => await PublishEvent(evt);- public async ValueTask<RpcResponse> SendRequestAsync(AgentId target, string method, Dictionary<string, string> parameters) => await RequestAsync(target, method, parameters);- public async ValueTask PublishEventAsync(string topic, IMessage evt)- {- await PublishEventAsync(evt.ToCloudEvent(topic)).ConfigureAwait(false);- }- private sealed class ClientContext(ILogger<AgentClient> logger, AgentWorkerRuntime runtime, DistributedContextPropagator distributedContextPropagator) : IAgentContext- {- public AgentId AgentId { get; } = new AgentId("client", Guid.NewGuid().ToString());- public AgentBase? AgentInstance { get; set; }- public ILogger Logger { get; } = logger;- public DistributedContextPropagator DistributedContextPropagator { get; } = distributedContextPropagator;- public async ValueTask PublishEventAsync(CloudEvent @event)- {- await runtime.PublishEvent(@event).ConfigureAwait(false);- }- public async ValueTask SendRequestAsync(AgentBase agent, RpcRequest request)- {- await runtime.SendRequest(AgentInstance!, request).ConfigureAwait(false);- }-- public async ValueTask SendResponseAsync(RpcRequest request, RpcResponse response)- {- await runtime.SendResponse(response).ConfigureAwait(false);- }- public ValueTask Store(AgentState value)- {- throw new NotImplementedException();- }- public ValueTask<AgentState> Read(AgentId agentId)- {- throw new NotImplementedException();- }- }-}diff --git a/dotnet/src/Microsoft.AutoGen/Agents/AgentContext.cs b/dotnet/src/Microsoft.AutoGen/Agents/AgentContext.csindex 779cc86a6..30be1406d 100644--- a/dotnet/src/Microsoft.AutoGen/Agents/AgentContext.cs+++ b/dotnet/src/Microsoft.AutoGen/Agents/AgentContext.cs@@ -4,9 +4,9 @@ using Microsoft.Extensions.Logging;namespace Microsoft.AutoGen.Agents;-internal sealed class AgentContext(AgentId agentId, AgentWorkerRuntime runtime, ILogger<AgentBase> logger, DistributedContextPropagator distributedContextPropagator) : IAgentContext+internal sealed class AgentContext(AgentId agentId, IAgentWorkerRuntime runtime, ILogger<AgentBase> logger, DistributedContextPropagator distributedContextPropagator) : IAgentContext{- private readonly AgentWorkerRuntime _runtime = runtime;+ private readonly IAgentWorkerRuntime _runtime = runtime;public AgentId AgentId { get; } = agentId;public ILogger Logger { get; } = logger;@@ -19,18 +19,18 @@ internal sealed class AgentContext(AgentId agentId, AgentWorkerRuntime runtime,}public async ValueTask SendRequestAsync(AgentBase agent, RpcRequest request){- await _runtime.SendRequest(agent, request);+ await _runtime.SendRequest(agent, request).ConfigureAwait(false);}public async ValueTask PublishEventAsync(CloudEvent @event){- await _runtime.PublishEvent(@event);+ await _runtime.PublishEvent(@event).ConfigureAwait(false);}public async ValueTask Store(AgentState value){- await _runtime.Store(value);+ await _runtime.Store(value).ConfigureAwait(false);}- public async ValueTask<AgentState> Read(AgentId agentId)+ public ValueTask<AgentState> Read(AgentId agentId){- return await _runtime.Read(agentId);+ return _runtime.Read(agentId);}}diff --git a/dotnet/src/Microsoft.AutoGen/Agents/AgentWorker.cs b/dotnet/src/Microsoft.AutoGen/Agents/AgentWorker.csnew file mode 100644index 000000000..0ba909ac6--- /dev/null+++ b/dotnet/src/Microsoft.AutoGen/Agents/AgentWorker.cs@@ -0,0 +1,18 @@+using System.Diagnostics;+using Google.Protobuf;+using Microsoft.AutoGen.Abstractions;+using Microsoft.Extensions.DependencyInjection;+using Microsoft.Extensions.Logging;++namespace Microsoft.AutoGen.Agents;+public sealed class AgentWorker(IAgentWorkerRuntime runtime, DistributedContextPropagator distributedContextPropagator,+ [FromKeyedServices("EventTypes")] EventTypes eventTypes, ILogger<AgentBase> logger)+ : AgentBase(new AgentContext(new AgentId("client", Guid.NewGuid().ToString()), runtime, logger, distributedContextPropagator), eventTypes)+{+ public async ValueTask PublishEventAsync(CloudEvent evt) => await PublishEvent(evt);++ public async ValueTask PublishEventAsync(string topic, IMessage evt)+ {+ await PublishEventAsync(evt.ToCloudEvent(topic)).ConfigureAwait(false);+ }+}diff --git a/dotnet/src/Microsoft.AutoGen/Agents/App.cs b/dotnet/src/Microsoft.AutoGen/Agents/App.csindex 00c487ede..be5da1ac5 100644--- a/dotnet/src/Microsoft.AutoGen/Agents/App.cs+++ b/dotnet/src/Microsoft.AutoGen/Agents/App.cs@@ -44,7 +44,7 @@ public static class AgentsApp{await StartAsync(builder, agents, local);}- var client = Host.Services.GetRequiredService<AgentClient>() ?? throw new InvalidOperationException("Host not started");+ var client = Host.Services.GetRequiredService<AgentWorker>() ?? throw new InvalidOperationException("Host not started");await client.PublishEventAsync(topic, message).ConfigureAwait(false);return Host;}diff --git a/dotnet/src/Microsoft.AutoGen/Agents/AgentWorkerRuntime.cs b/dotnet/src/Microsoft.AutoGen/Agents/GrpcAgentWorkerRuntime.cssimilarity index 92%rename from dotnet/src/Microsoft.AutoGen/Agents/AgentWorkerRuntime.csrename to dotnet/src/Microsoft.AutoGen/Agents/GrpcAgentWorkerRuntime.csindex f335881fc..b7168b9cb 100644--- a/dotnet/src/Microsoft.AutoGen/Agents/AgentWorkerRuntime.cs+++ b/dotnet/src/Microsoft.AutoGen/Agents/GrpcAgentWorkerRuntime.cs@@ -10,12 +10,12 @@ using Microsoft.Extensions.Logging;namespace Microsoft.AutoGen.Agents;-public sealed class AgentWorkerRuntime : IHostedService, IDisposable, IAgentWorkerRuntime+public sealed class GrpcAgentWorkerRuntime : IHostedService, IDisposable, IAgentWorkerRuntime{private readonly object _channelLock = new();private readonly ConcurrentDictionary<string, Type> _agentTypes = new();- private readonly ConcurrentDictionary<(string Type, string Key), AgentBase> _agents = new();- private readonly ConcurrentDictionary<string, (AgentBase Agent, string OriginalRequestId)> _pendingRequests = new();+ private readonly ConcurrentDictionary<(string Type, string Key), IAgentBase> _agents = new();+ private readonly ConcurrentDictionary<string, (IAgentBase Agent, string OriginalRequestId)> _pendingRequests = new();private readonly Channel<Message> _outboundMessagesChannel = Channel.CreateBounded<Message>(new BoundedChannelOptions(1024){AllowSynchronousContinuations = true,@@ -26,19 +26,19 @@ public sealed class AgentWorkerRuntime : IHostedService, IDisposable, IAgentWorkprivate readonly AgentRpc.AgentRpcClient _client;private readonly IServiceProvider _serviceProvider;private readonly IEnumerable<Tuple<string, Type>> _configuredAgentTypes;- private readonly ILogger<AgentWorkerRuntime> _logger;+ private readonly ILogger<GrpcAgentWorkerRuntime> _logger;private readonly DistributedContextPropagator _distributedContextPropagator;private readonly CancellationTokenSource _shutdownCts;private AsyncDuplexStreamingCall<Message, Message>? _channel;private Task? _readTask;private Task? _writeTask;- public AgentWorkerRuntime(+ public GrpcAgentWorkerRuntime(AgentRpc.AgentRpcClient client,IHostApplicationLifetime hostApplicationLifetime,IServiceProvider serviceProvider,[FromKeyedServices("AgentTypes")] IEnumerable<Tuple<string, Type>> configuredAgentTypes,- ILogger<AgentWorkerRuntime> logger,+ ILogger<GrpcAgentWorkerRuntime> logger,DistributedContextPropagator distributedContextPropagator){_client = client;@@ -83,6 +83,13 @@ public sealed class AgentWorkerRuntime : IHostedService, IDisposable, IAgentWorkmessage.Response.RequestId = request.OriginalRequestId;request.Agent.ReceiveMessage(message);break;+ case Message.MessageOneofCase.RegisterAgentTypeResponse:+ if (!message.RegisterAgentTypeResponse.Success)+ {+ throw new InvalidOperationException($"Failed to register agent: '{message.RegisterAgentTypeResponse.Error}'.");+ }+ break;+case Message.MessageOneofCase.CloudEvent:// HACK: Send the message to an instance of each agent type@@ -163,7 +170,7 @@ public sealed class AgentWorkerRuntime : IHostedService, IDisposable, IAgentWork}}- private AgentBase GetOrActivateAgent(AgentId agentId)+ private IAgentBase GetOrActivateAgent(AgentId agentId){if (!_agents.TryGetValue((agentId.Type, agentId.Key), out var agent)){@@ -197,6 +204,7 @@ public sealed class AgentWorkerRuntime : IHostedService, IDisposable, IAgentWorkRegisterAgentTypeRequest = new RegisterAgentTypeRequest{Type = type,+ RequestId = Guid.NewGuid().ToString(),//TopicTypes = { topicTypes },//StateType = state?.Name,//Events = { events }@@ -211,7 +219,7 @@ public sealed class AgentWorkerRuntime : IHostedService, IDisposable, IAgentWorkawait WriteChannelAsync(new Message { Response = response }).ConfigureAwait(false);}- public async ValueTask SendRequest(AgentBase agent, RpcRequest request)+ public async ValueTask SendRequest(IAgentBase agent, RpcRequest request){_logger.LogInformation("[{AgentId}] Sending request '{Request}'.", agent.AgentId, request);var requestId = Guid.NewGuid().ToString();@@ -322,10 +330,6 @@ public sealed class AgentWorkerRuntime : IHostedService, IDisposable, IAgentWork_channel?.Dispose();}}- public ValueTask SendRequest(RpcRequest request)- {- throw new NotImplementedException();- }public ValueTask Store(AgentState value){var agentId = value.AgentId ?? throw new InvalidOperationException("AgentId is required when saving AgentState.");diff --git a/dotnet/src/Microsoft.AutoGen/Agents/HostBuilderExtensions.cs b/dotnet/src/Microsoft.AutoGen/Agents/HostBuilderExtensions.csindex 0e29195bf..f13756f6d 100644--- a/dotnet/src/Microsoft.AutoGen/Agents/HostBuilderExtensions.cs+++ b/dotnet/src/Microsoft.AutoGen/Agents/HostBuilderExtensions.cs@@ -49,9 +49,9 @@ public static class HostBuilderExtensions});});builder.Services.TryAddSingleton(DistributedContextPropagator.Current);- builder.Services.AddSingleton<AgentClient>();- builder.Services.AddSingleton<AgentWorkerRuntime>();- builder.Services.AddSingleton<IHostedService>(sp => sp.GetRequiredService<AgentWorkerRuntime>());+ builder.Services.AddSingleton<IAgentWorkerRuntime, GrpcAgentWorkerRuntime>();+ builder.Services.AddSingleton<IHostedService>(sp => (IHostedService)sp.GetRequiredService<IAgentWorkerRuntime>());+ builder.Services.AddSingleton<AgentWorker>();builder.Services.AddKeyedSingleton("EventTypes", (sp, key) =>{var interfaceType = typeof(IMessage);@@ -111,7 +111,7 @@ public sealed class AgentTypes(Dictionary<string, Type> types).SelectMany(assembly => assembly.GetTypes()).Where(type => ReflectionHelper.IsSubclassOfGeneric(type, typeof(AgentBase))&& !type.IsAbstract- && !type.Name.Equals("AgentClient"))+ && !type.Name.Equals("AgentWorker")).ToDictionary(type => type.Name, type => type);return new AgentTypes(agents);diff --git a/dotnet/src/Microsoft.AutoGen/Agents/IAgentBase.cs b/dotnet/src/Microsoft.AutoGen/Agents/IAgentBase.csindex 122dff2c6..1e5a809ea 100644--- a/dotnet/src/Microsoft.AutoGen/Agents/IAgentBase.cs+++ b/dotnet/src/Microsoft.AutoGen/Agents/IAgentBase.cs@@ -1,22 +1,20 @@+using Google.Protobuf;using Microsoft.AutoGen.Abstractions;-using Microsoft.Extensions.Logging;namespace Microsoft.AutoGen.Agents{public interface IAgentBase{// Properties- string AgentId { get; }- ILogger Logger { get; }+ AgentId AgentId { get; }IAgentContext Context { get; }// MethodsTask CallHandler(CloudEvent item);Task<RpcResponse> HandleRequest(RpcRequest request);- Task Start();- Task ReceiveMessage(Message message);+ void ReceiveMessage(Message message);Task Store(AgentState state);- Task<T> Read<T>(AgentId agentId);- Task PublishEvent(CloudEvent item);+ Task<T> Read<T>(AgentId agentId) where T : IMessage, new();+ ValueTask PublishEvent(CloudEvent item);}}diff --git a/dotnet/src/Microsoft.AutoGen/Agents/IAgentWorkerRuntime.cs b/dotnet/src/Microsoft.AutoGen/Agents/IAgentWorkerRuntime.csindex ee1f6e4e2..5c2c7f486 100644--- a/dotnet/src/Microsoft.AutoGen/Agents/IAgentWorkerRuntime.cs+++ b/dotnet/src/Microsoft.AutoGen/Agents/IAgentWorkerRuntime.cs@@ -6,6 +6,8 @@ namespace Microsoft.AutoGen.Agents;public interface IAgentWorkerRuntime{ValueTask PublishEvent(CloudEvent evt);- ValueTask SendRequest(RpcRequest request);+ ValueTask SendRequest(IAgentBase agent, RpcRequest request);ValueTask SendResponse(RpcResponse response);+ ValueTask Store(AgentState value);+ ValueTask<AgentState> Read(AgentId agentId);}diff --git a/dotnet/src/Microsoft.AutoGen/Runtime/WorkerGateway.cs b/dotnet/src/Microsoft.AutoGen/Runtime/WorkerGateway.csindex 6d549ef72..424c5193a 100644--- a/dotnet/src/Microsoft.AutoGen/Runtime/WorkerGateway.cs+++ b/dotnet/src/Microsoft.AutoGen/Runtime/WorkerGateway.cs@@ -141,8 +141,22 @@ internal sealed class WorkerGateway : BackgroundService, IWorkerGateway{connection.AddSupportedType(msg.Type);_supportedAgentTypes.GetOrAdd(msg.Type, _ => []).Add(connection);+ var success = false;+ var error = String.Empty;- await _gatewayRegistry.RegisterAgentType(msg.Type, _reference);+ try+ {+ await _gatewayRegistry.RegisterAgentType(msg.Type, _reference);+ success = true;+ }+ catch (InvalidOperationException exception)+ {+ error = $"Error registering agent type '{msg.Type}'.";+ _logger.LogWarning(exception, error);+ }+ var request_id = msg.RequestId;+ var response = new RegisterAgentTypeResponse { RequestId = request_id, Success = success, Error = error };+ await connection.SendMessage(new Message { RegisterAgentTypeResponse = response });}private async ValueTask DispatchEventAsync(CloudEvent evt)