A programming framework for agentic AI
Rysweet 5201 refactor runtime interface (#5204)
Ryan Sweet10e40b5a1f91
diff --git a/dotnet/samples/dev-team/DevTeam.Backend/Program.cs b/dotnet/samples/dev-team/DevTeam.Backend/Program.csindex ee75f056c..bf1424892 100644--- a/dotnet/samples/dev-team/DevTeam.Backend/Program.cs+++ b/dotnet/samples/dev-team/DevTeam.Backend/Program.cs@@ -32,7 +32,7 @@ builder.AddGrpcAgentWorker(builder.Configuration["AGENT_HOST"]!).AddAgent<ProductManager>(nameof(ProductManager)).AddAgent<DeveloperLead>(nameof(DeveloperLead));-builder.Services.AddSingleton<AgentWorker>();+builder.Services.AddSingleton<AgentRuntime>();builder.Services.AddSingleton<WebhookEventProcessor, GithubWebHookProcessor>();builder.Services.AddSingleton<GithubAuthService>();builder.Services.AddSingleton<IManageAzure, AzureService>();diff --git a/dotnet/src/Microsoft.AutoGen/Agents/IOAgent/WebAPIAgent/WebAPIAgent.cs b/dotnet/src/Microsoft.AutoGen/Agents/IOAgent/WebAPIAgent/WebAPIAgent.csindex a94d8d181..610b1db11 100644--- a/dotnet/src/Microsoft.AutoGen/Agents/IOAgent/WebAPIAgent/WebAPIAgent.cs+++ b/dotnet/src/Microsoft.AutoGen/Agents/IOAgent/WebAPIAgent/WebAPIAgent.cs@@ -17,7 +17,7 @@ public abstract class WebAPIAgent : IOAgent,private readonly string _url = "/agents/webio";public WebAPIAgent(- IAgentWorker worker,+ IAgentRuntime worker,[FromKeyedServices("AgentsMetadata")] AgentsMetadata typeRegistry,ILogger<WebAPIAgent> logger,string url = "/agents/webio") : base(diff --git a/dotnet/src/Microsoft.AutoGen/Core.Grpc/App.cs b/dotnet/src/Microsoft.AutoGen/Core.Grpc/App.csindex f5c0dabe8..af7488209 100644--- a/dotnet/src/Microsoft.AutoGen/Core.Grpc/App.cs+++ b/dotnet/src/Microsoft.AutoGen/Core.Grpc/App.cs@@ -46,7 +46,7 @@ public static class AgentsAppawait StartAsync(builder, agents, local);}var client = Host.Services.GetRequiredService<Client>() ?? throw new InvalidOperationException("Host not started");- await client.PublishEventAsync(topic, message, new CancellationToken()).ConfigureAwait(true);+ await client.PublishMessageAsync(message, topic, token: new CancellationToken()).ConfigureAwait(true);return Host;}public static async ValueTask ShutdownAsync()diff --git a/dotnet/src/Microsoft.AutoGen/Core.Grpc/GrpcAgentWorker.cs b/dotnet/src/Microsoft.AutoGen/Core.Grpc/GrpcAgentRuntime.cssimilarity index 90%rename from dotnet/src/Microsoft.AutoGen/Core.Grpc/GrpcAgentWorker.csrename to dotnet/src/Microsoft.AutoGen/Core.Grpc/GrpcAgentRuntime.csindex c40ded0ab..85d070f36 100644--- a/dotnet/src/Microsoft.AutoGen/Core.Grpc/GrpcAgentWorker.cs+++ b/dotnet/src/Microsoft.AutoGen/Core.Grpc/GrpcAgentRuntime.cs@@ -1,5 +1,5 @@// Copyright (c) Microsoft Corporation. All rights reserved.-// GrpcAgentWorker.cs+// GrpcAgentRuntime.csusing System.Collections.Concurrent;using System.Reflection;@@ -12,13 +12,17 @@ using Microsoft.Extensions.Logging;namespace Microsoft.AutoGen.Core.Grpc;-public sealed class GrpcAgentWorker(+public sealed class GrpcAgentRuntime(AgentRpc.AgentRpcClient client,IHostApplicationLifetime hostApplicationLifetime,IServiceProvider serviceProvider,[FromKeyedServices("AgentTypes")] IEnumerable<Tuple<string, Type>> configuredAgentTypes,- ILogger<GrpcAgentWorker> logger) :- IHostedService, IDisposable, IAgentWorker+ ILogger<GrpcAgentRuntime> logger+ ) : AgentRuntime(+ hostApplicationLifetime,+ serviceProvider,+ configuredAgentTypes+ ), IDisposable{private readonly object _channelLock = new();private readonly ConcurrentDictionary<string, Type> _agentTypes = new();@@ -35,13 +39,11 @@ public sealed class GrpcAgentWorker(private readonly AgentRpc.AgentRpcClient _client = client;public readonly IServiceProvider ServiceProvider = serviceProvider;private readonly IEnumerable<Tuple<string, Type>> _configuredAgentTypes = configuredAgentTypes;- private readonly ILogger<GrpcAgentWorker> _logger = logger;+ private readonly ILogger<GrpcAgentRuntime> _logger = logger;private readonly CancellationTokenSource _shutdownCts = CancellationTokenSource.CreateLinkedTokenSource(hostApplicationLifetime.ApplicationStopping);private AsyncDuplexStreamingCall<Message, Message>? _channel;private Task? _readTask;private Task? _writeTask;-- IServiceProvider IAgentWorker.ServiceProvider => ServiceProvider;public void Dispose(){_outboundMessagesChannel.Writer.TryComplete();@@ -289,25 +291,24 @@ public sealed class GrpcAgentWorker(}}// new is intentional- public async ValueTask SendResponseAsync(RpcResponse response, CancellationToken cancellationToken = default)+ public new async ValueTask RuntimeSendResponseAsync(RpcResponse response, CancellationToken cancellationToken = default){await WriteChannelAsync(new Message { Response = response }, cancellationToken).ConfigureAwait(false);}- public async ValueTask SendRequestAsync(Agent agent, RpcRequest request, CancellationToken cancellationToken = default)+ public new async ValueTask RuntimeSendRequestAsync(Agent agent, RpcRequest request, CancellationToken cancellationToken = default){var requestId = Guid.NewGuid().ToString();_pendingRequests[requestId] = (agent, request.RequestId);request.RequestId = requestId;await WriteChannelAsync(new Message { Request = request }, cancellationToken).ConfigureAwait(false);}-- public async ValueTask SendMessageAsync(Message message, CancellationToken cancellationToken = default)+ public new async ValueTask RuntimeWriteMessage(Message message, CancellationToken cancellationToken = default){await WriteChannelAsync(message, cancellationToken).ConfigureAwait(false);}- public async ValueTask PublishEventAsync(CloudEvent @event, CancellationToken cancellationToken = default)+ public async ValueTask RuntimePublishEventAsync(CloudEvent @event, CancellationToken cancellationToken = default){await WriteChannelAsync(new Message { CloudEvent = @event }, cancellationToken).ConfigureAwait(false);}@@ -350,11 +351,10 @@ public sealed class GrpcAgentWorker(return _channel;}-- public async Task StartAsync(CancellationToken cancellationToken)+ public new async Task StartAsync(CancellationToken cancellationToken){_channel = GetChannel();- _logger.LogInformation("Starting GrpcAgentWorker, connecting to gRPC endpoint " + Environment.GetEnvironmentVariable("AGENT_HOST"));+ _logger.LogInformation("Starting " + GetType().Name + ",connecting to gRPC endpoint " + Environment.GetEnvironmentVariable("AGENT_HOST"));StartCore();@@ -389,8 +389,7 @@ public sealed class GrpcAgentWorker(}}}-- public async Task StopAsync(CancellationToken cancellationToken)+ public new async Task StopAsync(CancellationToken cancellationToken){_shutdownCts.Cancel();@@ -410,8 +409,7 @@ public sealed class GrpcAgentWorker(_channel?.Dispose();}}-- public async ValueTask StoreAsync(AgentState value, CancellationToken cancellationToken = default)+ public new async ValueTask SaveStateAsync(AgentState value, CancellationToken cancellationToken = default){var agentId = value.AgentId ?? throw new InvalidOperationException("AgentId is required when saving AgentState.");var response = _client.SaveState(value, null, null, cancellationToken);@@ -421,7 +419,7 @@ public sealed class GrpcAgentWorker(}}- public async ValueTask<AgentState> ReadAsync(AgentId agentId, CancellationToken cancellationToken = default)+ public new async ValueTask<AgentState> LoadStateAsync(AgentId agentId, CancellationToken cancellationToken = default){var response = await _client.GetStateAsync(agentId).ConfigureAwait(true);// if (response.Success && response.AgentState.AgentId is not null) - why is success always false?@@ -434,20 +432,20 @@ public sealed class GrpcAgentWorker(throw new KeyNotFoundException($"Failed to read AgentState for {agentId}.");}}- public async ValueTask<List<Subscription>> GetSubscriptionsAsync(GetSubscriptionsRequest request, CancellationToken cancellationToken = default)+ public new async ValueTask<List<Subscription>> GetSubscriptionsAsync(GetSubscriptionsRequest request, CancellationToken cancellationToken = default){var response = await _client.GetSubscriptionsAsync(request, null, null, cancellationToken);return response.Subscriptions.ToList();}- public ValueTask<AddSubscriptionResponse> SubscribeAsync(AddSubscriptionRequest request, CancellationToken cancellationToken = default)+ public new async Task<AddSubscriptionResponse> AddSubscriptionAsync(AddSubscriptionRequest request, CancellationToken cancellationToken = default){var response = _client.AddSubscription(request, null, null, cancellationToken);- return new ValueTask<AddSubscriptionResponse>(response);+ return response;}- public ValueTask<RemoveSubscriptionResponse> UnsubscribeAsync(RemoveSubscriptionRequest request, CancellationToken cancellationToken = default)+ public new async ValueTask<RemoveSubscriptionResponse> RemoveSubscriptionAsync(RemoveSubscriptionRequest request, CancellationToken cancellationToken = default){var response = _client.RemoveSubscription(request, null, null, cancellationToken);- return new ValueTask<RemoveSubscriptionResponse>(response);+ return response;}}diff --git a/dotnet/src/Microsoft.AutoGen/Core.Grpc/GrpcAgentWorkerHostBuilderExtension.cs b/dotnet/src/Microsoft.AutoGen/Core.Grpc/GrpcAgentWorkerHostBuilderExtension.csindex 91e2beacb..2331d1702 100644--- a/dotnet/src/Microsoft.AutoGen/Core.Grpc/GrpcAgentWorkerHostBuilderExtension.cs+++ b/dotnet/src/Microsoft.AutoGen/Core.Grpc/GrpcAgentWorkerHostBuilderExtension.cs@@ -63,15 +63,15 @@ public static class GrpcAgentWorkerHostBuilderExtensions});var assemblies = AppDomain.CurrentDomain.GetAssemblies();builder.Services.TryAddSingleton(DistributedContextPropagator.Current);- builder.Services.AddSingleton<IAgentWorker, GrpcAgentWorker>();- builder.Services.AddSingleton<IHostedService>(sp => (IHostedService)sp.GetRequiredService<IAgentWorker>());+ builder.Services.AddSingleton<IAgentRuntime, GrpcAgentRuntime>();+ builder.Services.AddSingleton<IHostedService>(sp => (IHostedService)sp.GetRequiredService<IAgentRuntime>());builder.Services.AddKeyedSingleton("AgentsMetadata", (sp, key) =>{return ReflectionHelper.GetAgentsMetadata(assemblies);});builder.Services.AddSingleton((s) =>{- var worker = s.GetRequiredService<IAgentWorker>();+ var worker = s.GetRequiredService<IAgentRuntime>();var client = ActivatorUtilities.CreateInstance<Client>(s);Agent.Initialize(worker, client);return client;diff --git a/dotnet/src/Microsoft.AutoGen/Core/Agent.cs b/dotnet/src/Microsoft.AutoGen/Core/Agent.csindex fe6103e70..865ff8593 100644--- a/dotnet/src/Microsoft.AutoGen/Core/Agent.cs+++ b/dotnet/src/Microsoft.AutoGen/Core/Agent.cs@@ -32,7 +32,7 @@ public abstract class Agentpublic AgentId AgentId { get; private set; }private readonly Channel<object> _mailbox = Channel.CreateUnbounded<object>();protected internal ILogger<Agent> _logger;- public IAgentWorker Worker { get; private set; }+ public IAgentRuntime Worker { get; private set; }private readonly ConcurrentDictionary<Type, MethodInfo> _handlersByMessageType;protected readonly AgentsMetadata EventTypes;@@ -46,7 +46,7 @@ public abstract class Agent_handlersByMessageType = new(GetType().GetHandlersLookupTable());Worker = new UninitializedAgentWorker();}- public static void Initialize(IAgentWorker worker, Agent agent)+ public static void Initialize(IAgentRuntime worker, Agent agent){agent.Worker = worker;agent.Start();@@ -76,7 +76,7 @@ public abstract class Agent}};// explicitly wait for this to complete- Worker.SubscribeAsync(subscriptionRequest).AsTask().Wait();+ Worker.AddSubscriptionAsync(subscriptionRequest).AsTask().Wait();}// using reflection, find all methods that Handle<T> and subscribe to the topic T@@ -192,7 +192,7 @@ public abstract class Agent}}};- var subscriptionResponse = await Worker.SubscribeAsync(subscriptionRequest).ConfigureAwait(true);+ var subscriptionResponse = await Worker.AddSubscriptionAsync(subscriptionRequest).ConfigureAwait(true);if (!subscriptionResponse.Success){_logger.LogError($"{GetType}{AgentId.Key}: Failed to unsubscribe from topic {topic}");@@ -205,7 +205,7 @@ public abstract class Agent{Id = id.ToString()};- var subscriptionResponse = await Worker.UnsubscribeAsync(subscriptionRequest).ConfigureAwait(true);+ var subscriptionResponse = await Worker.RemoveSubscriptionAsync(subscriptionRequest).ConfigureAwait(true);if (!subscriptionResponse.Success){_logger.LogError($"{GetType}{AgentId.Key}: Failed to unsubscribe from Subscription {id}");@@ -227,12 +227,12 @@ public abstract class Agent}public async Task StoreAsync(AgentState state, CancellationToken cancellationToken = default){- await Worker.StoreAsync(state, cancellationToken).ConfigureAwait(false);+ await Worker.SaveStateAsync(state, cancellationToken).ConfigureAwait(false);return;}public async Task<T> ReadAsync<T>(AgentId agentId, CancellationToken cancellationToken = default) where T : IMessage, new(){- var agentstate = await Worker.ReadAsync(agentId, cancellationToken).ConfigureAwait(false);+ var agentstate = await Worker.LoadStateAsync(agentId, cancellationToken).ConfigureAwait(false);return agentstate.FromAgentState<T>();}private void OnResponseCore(RpcResponse response)@@ -263,7 +263,7 @@ public abstract class Agent}response.RequestId = request.RequestId;- await Worker.SendResponseAsync(response, cancellationToken).ConfigureAwait(false);+ await Worker.RuntimeSendResponseAsync(response, cancellationToken).ConfigureAwait(false);}protected async Task<RpcResponse> RequestAsync(AgentId target, string method, Dictionary<string, string> parameters)@@ -287,7 +287,7 @@ public abstract class Agentactivity?.SetTag("peer.service", target.ToString());var completion = new TaskCompletionSource<RpcResponse>(TaskCreationOptions.RunContinuationsAsynchronously);- IAgentWorkerExtensions.Update(this.Worker, request, activity);+ IAgentRuntimeExtensions.Update(this.Worker, request, activity);await this.InvokeWithActivityAsync(static async (state, ct) =>{@@ -295,7 +295,7 @@ public abstract class Agentself._pendingRequests.AddOrUpdate(request.RequestId, _ => completion, (_, __) => completion);- await state.Item1.Worker!.SendRequestAsync(state.Item1, state.request, ct).ConfigureAwait(false);+ await state.Item1.Worker!.RuntimeSendRequestAsync(state.Item1, state.request, ct).ConfigureAwait(false);await completion.Task.ConfigureAwait(false);},@@ -321,81 +321,68 @@ public abstract class Agent}/// <summary>- /// Publishes a message asynchronously.+ /// Publishes a message to the agent runtime./// </summary>- /// <typeparam name="T">The type of the message.</typeparam>+ /// <param name="message">The message to publish.</param>+ /// <param name="topic">The topic to which we publish the message</param>+ /// <param name="source">The source of the message.</param>+ /// <param name="key">The key of the message.</param>/// <param name="token">A token to cancel the operation.</param>/// <returns>A task representing the asynchronous operation.</returns>- public async ValueTask PublishMessageAsync<T>(T message, string topic, string source, string key, CancellationToken token = default) where T : IMessage+ public async ValueTask PublishMessageAsync(IMessage message, string topic, string source, string key, CancellationToken token = default){+ var taskList = new List<Task>();+ var topicTypes = new List<string>();// if there are no topic types, use the agent's default topic subscription attribute and the agent's type and keyif (string.IsNullOrWhiteSpace(topic)){- if (string.IsNullOrWhiteSpace(topic))+ topic = this.AgentId.Type;+ var topicAttributes = this.GetType().GetCustomAttributes<TopicSubscriptionAttribute>().Select(t => t.Topic);+ if (topicAttributes.Any()){- topic = this.AgentId.Type + "." + this.AgentId.Key;+ topicTypes.AddRange(topicAttributes);}- else+ if (topicTypes.Count == 0){- topic = topic + "." + source + "." + key;- }-- var topicTypes = this.GetType().GetCustomAttributes<TopicSubscriptionAttribute>().Select(t => t.Topic);- if (!topicTypes.Any())- {- topicTypes = topicTypes.Append(string.IsNullOrWhiteSpace(source) ? this.AgentId.Type + "." + this.AgentId.Key : source);- }- topicTypes = topicTypes.Append(SetTopic(topic, source, key));- foreach (var t in topicTypes)- {- await PublishEventAsync(t, message, token).ConfigureAwait(false);+ topicTypes.Add(+ string.IsNullOrWhiteSpace(source) ? this.AgentId.Type + "." + (string.IsNullOrEmpty(key) ? this.AgentId.Key : key) : source+ );}+ topicTypes.Add(SetTopic(topic, source, key));}- else+ if (string.IsNullOrWhiteSpace(source) && string.IsNullOrWhiteSpace(key)) { source = this.AgentId.Key; }+ if (!String.IsNullOrWhiteSpace(key)) { source = source + "." + key; }+ var topicIds = new List<TopicId>();+ foreach (var t in topicTypes) { topicIds.Add(new TopicId() { Type = t, Source = source }); }+ topicIds.Add(new TopicId() { Type = topic, Source = source });+ foreach (var topicId in topicIds){- await PublishEventAsync(topic, message, token).ConfigureAwait(false);+ taskList.Add(Task.Run(async () =>+ {+ await Worker.PublishMessageAsync(message, topicId, this, token);+ }));}+ await Task.WhenAll(taskList).ConfigureAwait(false);}public async ValueTask PublishMessageAsync<T>(T message, string topic, string source, CancellationToken token = default) where T : IMessage{- string key = this.AgentId.Key;+ var key = this.AgentId.Key;await PublishMessageAsync(message, topic, source, key, token).ConfigureAwait(false);}public async ValueTask PublishMessageAsync<T>(T message, string topic, CancellationToken token = default) where T : IMessage{- string source = this.AgentId.Type;- string key = this.AgentId.Key;+ var source = this.AgentId.Type;+ var key = this.AgentId.Key;await PublishMessageAsync(message, topic, source, key, token).ConfigureAwait(false);}public async ValueTask PublishMessageAsync<T>(T message, CancellationToken token = default) where T : IMessage{- string topic = "";- string source = this.AgentId.Type;- string key = this.AgentId.Key;+ var topic = "";+ var source = this.AgentId.Type;+ var key = this.AgentId.Key;await PublishMessageAsync(message, topic, source, key, token).ConfigureAwait(false);}- public async ValueTask PublishEventAsync(string topic, IMessage message, CancellationToken cancellationToken = default)- {- await PublishEventAsync(message.ToCloudEvent(key: GetType().Name, topic: topic), cancellationToken).ConfigureAwait(false);- }- public async ValueTask PublishEventAsync(CloudEvent item, CancellationToken cancellationToken = default)- {- var activity = s_source.StartActivity($"PublishEventAsync '{item.Type}'", ActivityKind.Client, Activity.Current?.Context ?? default);- activity?.SetTag("peer.service", $"{item.Type}/{item.Source}");-- // TODO: fix activity- IAgentWorkerExtensions.Update(this.Worker, item, activity);- await this.InvokeWithActivityAsync(- static async ((Agent Agent, CloudEvent Event) state, CancellationToken ct) =>- {- await state.Agent.Worker.PublishEventAsync(state.Event).ConfigureAwait(false);- },- (this, item),- activity,- item.Type, cancellationToken).ConfigureAwait(false);- }-- public Task CallHandlerAsync(CloudEvent item, CancellationToken cancellationToken = default)+ private Task CallHandlerAsync(CloudEvent item, CancellationToken cancellationToken = default){// Only send the event to the handler if the agent type is handling that typeif (EventTypes.CheckIfTypeHandles(GetType(), eventName: item.Type))diff --git a/dotnet/src/Microsoft.AutoGen/Core/AgentExtensions.cs b/dotnet/src/Microsoft.AutoGen/Core/AgentExtensions.csindex 50d25b20a..d2fc801ac 100644--- a/dotnet/src/Microsoft.AutoGen/Core/AgentExtensions.cs+++ b/dotnet/src/Microsoft.AutoGen/Core/AgentExtensions.cs@@ -22,7 +22,7 @@ public static class AgentExtensionspublic static Activity? ExtractActivity(this Agent agent, string activityName, IDictionary<string, string> metadata){Activity? activity;- (string? traceParent, string? traceState) = IAgentWorkerExtensions.GetTraceIdAndState(agent.Worker, metadata);+ (string? traceParent, string? traceState) = IAgentRuntimeExtensions.GetTraceIdAndState(agent.Worker, metadata);if (!string.IsNullOrEmpty(traceParent)){if (ActivityContext.TryParse(traceParent, traceState, isRemote: true, out var parentContext))@@ -43,7 +43,7 @@ public static class AgentExtensionsactivity.TraceStateString = traceState;}- var baggage = IAgentWorkerExtensions.ExtractMetadata(agent.Worker, metadata);+ var baggage = IAgentRuntimeExtensions.ExtractMetadata(agent.Worker, metadata);foreach (var baggageItem in baggage){diff --git a/dotnet/src/Microsoft.AutoGen/Core/AgentRuntime.cs b/dotnet/src/Microsoft.AutoGen/Core/AgentRuntime.csnew file mode 100644index 000000000..9e18d15fa--- /dev/null+++ b/dotnet/src/Microsoft.AutoGen/Core/AgentRuntime.cs@@ -0,0 +1,155 @@+// Copyright (c) Microsoft Corporation. All rights reserved.+// AgentRuntime.cs+using System.Collections.Concurrent;+using Microsoft.AutoGen.Contracts;+using Microsoft.Extensions.DependencyInjection;+using Microsoft.Extensions.Hosting;++namespace Microsoft.AutoGen.Core;++/// <summary>+/// Represents a worker that manages agents and handles messages.+/// </summary>+/// <remarks>+/// Initializes a new instance of the <see cref="AgentRuntime"/> class.+/// </remarks>+/// <param name="hostApplicationLifetime">The application lifetime.</param>+/// <param name="serviceProvider">The service provider.</param>+/// <param name="configuredAgentTypes">The configured agent types.</param>+public class AgentRuntime(+ IHostApplicationLifetime hostApplicationLifetime,+ IServiceProvider serviceProvider,+ [FromKeyedServices("AgentTypes")] IEnumerable<Tuple<string, Type>> configuredAgentTypes) :+ AgentRuntimeBase(+ hostApplicationLifetime,+ serviceProvider,+ configuredAgentTypes)+{+ private readonly ConcurrentDictionary<string, AgentState> _agentStates = new();+ private readonly ConcurrentDictionary<string, List<Subscription>> _subscriptionsByAgentType = new();+ private readonly ConcurrentDictionary<string, List<string>> _subscriptionsByTopic = new();+ private readonly ConcurrentDictionary<Guid, IDictionary<string, string>> _subscriptionsByGuid = new();++ /// <inheritdoc />+ public override ValueTask SaveStateAsync(AgentState value, CancellationToken cancellationToken = default)+ {+ var agentId = value.AgentId ?? throw new InvalidOperationException("AgentId is required when saving AgentState.");+ var response = _agentStates.AddOrUpdate(agentId.ToString(), value, (key, oldValue) => value);+ return ValueTask.CompletedTask;+ }++ /// <inheritdoc />+ public override ValueTask<AgentState> LoadStateAsync(AgentId agentId, CancellationToken cancellationToken = default)+ {+ _agentStates.TryGetValue(agentId.ToString(), out var state);+ if (state is not null && state.AgentId is not null)+ {+ return new ValueTask<AgentState>(state);+ }+ else+ {+ throw new KeyNotFoundException($"Failed to read AgentState for {agentId}.");+ }+ }+ /// <inheritdoc />+ public new async ValueTask RuntimeSendRequestAsync(Agent agent, RpcRequest request, CancellationToken cancellationToken = default)+ {+ var requestId = Guid.NewGuid().ToString();+ _pendingClientRequests[requestId] = (agent, request.RequestId);+ request.RequestId = requestId;+ await _mailbox.Writer.WriteAsync(request, cancellationToken).ConfigureAwait(false);+ }++ /// <inheritdoc />+ public new ValueTask RuntimeSendResponseAsync(RpcResponse response, CancellationToken cancellationToken = default)+ {+ return _mailbox.Writer.WriteAsync(new Message { Response = response }, cancellationToken);+ }+ /// <inheritdoc />+ public ValueTask RuntimeWriteMessage(Message message, CancellationToken cancellationToken = default)+ {+ return _mailbox.Writer.WriteAsync(message, cancellationToken);+ }+ public override async ValueTask<AddSubscriptionResponse> AddSubscriptionAsync(AddSubscriptionRequest subscription, CancellationToken cancellationToken = default)+ {+ var topic = subscription.Subscription.TypeSubscription.TopicType;+ var agentType = subscription.Subscription.TypeSubscription.AgentType;+ var id = Guid.NewGuid();+ subscription.Subscription.Id = id.ToString();+ var sub = new Dictionary<string, string> { { topic, agentType } };+ _subscriptionsByGuid.GetOrAdd(id, static _ => new Dictionary<string, string>()).Add(topic, agentType);+ _subscriptionsByAgentType.GetOrAdd(key: agentType, _ => []).Add(subscription.Subscription);+ _subscriptionsByTopic.GetOrAdd(topic, _ => []).Add(agentType);+ var response = new AddSubscriptionResponse+ {+ RequestId = subscription.RequestId,+ Error = "",+ Success = true+ };+ return response;+ }+ public override async ValueTask<RemoveSubscriptionResponse> RemoveSubscriptionAsync(RemoveSubscriptionRequest request, CancellationToken cancellationToken = default)+ {+ if (!Guid.TryParse(request.Id, out var id))+ {+ var removeSubscriptionResponse = new RemoveSubscriptionResponse+ {+ Error = "Invalid subscription ID",+ Success = false+ };+ return removeSubscriptionResponse;+ }+ if (_subscriptionsByGuid.TryGetValue(id, out var sub))+ {+ foreach (var (topic, agentType) in sub)+ {+ if (_subscriptionsByTopic.TryGetValue(topic, out var innerAgentTypes))+ {+ while (innerAgentTypes.Remove(agentType))+ {+ //ensures all instances are removed+ }+ _subscriptionsByTopic.AddOrUpdate(topic, innerAgentTypes, (_, _) => innerAgentTypes);+ }+ var toRemove = new List<Subscription>();+ if (_subscriptionsByAgentType.TryGetValue(agentType, out var innerSubscriptions))+ {+ foreach (var subscription in innerSubscriptions)+ {+ if (subscription.Id == id.ToString())+ {+ toRemove.Add(subscription);+ }+ }+ foreach (var subscription in toRemove) { innerSubscriptions.Remove(subscription); }+ _subscriptionsByAgentType.AddOrUpdate(agentType, innerSubscriptions, (_, _) => innerSubscriptions);+ }+ }+ _subscriptionsByGuid.TryRemove(id, out _);+ }+ var response = new RemoveSubscriptionResponse+ {+ Error = "",+ Success = true+ };+ return response;+ }++ public ValueTask<List<Subscription>> GetSubscriptionsAsync(Type type)+ {+ if (_subscriptionsByAgentType.TryGetValue(type.Name, out var subscriptions))+ {+ return new ValueTask<List<Subscription>>(subscriptions);+ }+ return new ValueTask<List<Subscription>>([]);+ }+ public override ValueTask<List<Subscription>> GetSubscriptionsAsync(GetSubscriptionsRequest request, CancellationToken cancellationToken = default)+ {+ var subscriptions = new List<Subscription>();+ foreach (var (_, value) in _subscriptionsByAgentType)+ {+ subscriptions.AddRange(value);+ }+ return new ValueTask<List<Subscription>>(subscriptions);+ }+}diff --git a/dotnet/src/Microsoft.AutoGen/Core/AgentRuntimeBase.cs b/dotnet/src/Microsoft.AutoGen/Core/AgentRuntimeBase.csnew file mode 100644index 000000000..7f9b772e0--- /dev/null+++ b/dotnet/src/Microsoft.AutoGen/Core/AgentRuntimeBase.cs@@ -0,0 +1,215 @@+// Copyright (c) Microsoft Corporation. All rights reserved.+// AgentRuntimeBase.cs++using System.Collections.Concurrent;+using System.Diagnostics;+using System.Threading.Channels;+using Google.Protobuf;+using Microsoft.AutoGen.Contracts;+using Microsoft.Extensions.DependencyInjection;+using Microsoft.Extensions.Hosting;++namespace Microsoft.AutoGen.Core;+/// <summary>+/// Base class for agent runtime.+/// </summary>+/// <param name="hostApplicationLifetime">used for shutdown</param>+/// <param name="serviceProvider">the service host for DI container</param>+/// <param name="configuredAgentTypes">the agent types this runtime can host</param>+public abstract class AgentRuntimeBase(+ IHostApplicationLifetime hostApplicationLifetime,+ IServiceProvider serviceProvider,+ [FromKeyedServices("AgentTypes")] IEnumerable<Tuple<string, Type>> configuredAgentTypes) : IHostedService, IAgentRuntime+{+ public IServiceProvider RuntimeServiceProvider { get; } = serviceProvider;+ protected readonly ConcurrentDictionary<string, (Agent Agent, string OriginalRequestId)> _pendingClientRequests = new();+ protected readonly Channel<object> _mailbox = Channel.CreateUnbounded<object>();+ private readonly ConcurrentDictionary<(string Type, string Key), Agent> _agents = new();+ private readonly ConcurrentDictionary<string, Type> _agentTypes = new();+ private readonly CancellationTokenSource _shutdownCts = CancellationTokenSource.CreateLinkedTokenSource(hostApplicationLifetime.ApplicationStopping);+ private readonly CancellationTokenSource _shutdownCancellationToken = new();+ private readonly IEnumerable<Tuple<string, Type>> _configuredAgentTypes = configuredAgentTypes;+ private Task? _mailboxTask;+ private readonly object _channelLock = new();++ /// <summary>+ /// Starts the agent runtime.+ /// Required to implement IHostedService+ /// </summary>+ /// <param name="cancellationToken"></param>+ /// <returns>Task</returns>+ public async Task StartAsync(CancellationToken cancellationToken)+ {+ StartCore();++ foreach (var (typeName, type) in _configuredAgentTypes)+ {+ _agentTypes.TryAdd(typeName, type);+ }+ void StartCore()+ {+ var didSuppress = false;+ if (!ExecutionContext.IsFlowSuppressed())+ {+ didSuppress = true;+ ExecutionContext.SuppressFlow();+ }++ try+ {+ _mailboxTask = Task.Run(RunMessagePump, CancellationToken.None);+ }+ finally+ {+ if (didSuppress)+ {+ ExecutionContext.RestoreFlow();+ }+ }+ }+ }+ /// <summary>+ /// Stops the agent runtime.+ /// Required to implement IHostedService.+ /// </summary>+ /// <param name="cancellationToken"></param>+ /// <returns>Task</returns>+ public async Task StopAsync(CancellationToken cancellationToken)+ {+ _shutdownCts.Cancel();++ _mailbox.Writer.TryComplete();++ if (_mailboxTask is { } readTask)+ {+ await readTask.ConfigureAwait(false);+ }+ lock (_channelLock)+ {+ }+ }++ /// <summary>+ /// Runs the message pump.+ /// </summary>+ public async Task RunMessagePump()+ {+ await Task.CompletedTask.ConfigureAwait(ConfigureAwaitOptions.ForceYielding);+ await foreach (var message in _mailbox.Reader.ReadAllAsync())+ {+ try+ {+ if (message == null) { continue; }+ switch (message)+ {+ case Message msg when msg.CloudEvent != null:++ var item = msg.CloudEvent;++ foreach (var (typeName, _) in _agentTypes)+ {+ var agentToInvoke = GetOrActivateAgent(new AgentId(typeName, item.Source));+ agentToInvoke.ReceiveMessage(msg);+ }+ break;+ case Message msg when msg.AddSubscriptionRequest != null:+ await AddSubscriptionAsync(msg.AddSubscriptionRequest).ConfigureAwait(true);+ break;+ case Message msg when msg.AddSubscriptionResponse != null:+ break;+ case Message msg when msg.RegisterAgentTypeResponse != null:+ break;+ default:+ throw new InvalidOperationException($"Unexpected message '{message}'.");+ }+ }+ catch (OperationCanceledException)+ {+ }+ finally+ {+ _shutdownCancellationToken.Cancel();+ }+ }+ }+ public async ValueTask PublishMessageAsync(IMessage message, TopicId topic, Agent? sender, CancellationToken? cancellationToken = default)+ {+ var topicString = topic.Type + "." + topic.Source;+ sender ??= RuntimeServiceProvider.GetRequiredService<Client>();+ await PublishEventAsync(message.ToCloudEvent(key: sender.GetType().Name, topic: topicString), sender, cancellationToken.GetValueOrDefault()).ConfigureAwait(false);+ }+ public abstract ValueTask SaveStateAsync(AgentState value, CancellationToken cancellationToken = default);+ public abstract ValueTask<AgentState> LoadStateAsync(AgentId agentId, CancellationToken cancellationToken = default);+ public abstract ValueTask<AddSubscriptionResponse> AddSubscriptionAsync(AddSubscriptionRequest request, CancellationToken cancellationToken = default);+ public abstract ValueTask<RemoveSubscriptionResponse> RemoveSubscriptionAsync(RemoveSubscriptionRequest request, CancellationToken cancellationToken = default);+ public abstract ValueTask<List<Subscription>> GetSubscriptionsAsync(GetSubscriptionsRequest request, CancellationToken cancellationToken = default);++ private Agent GetOrActivateAgent(AgentId agentId)+ {+ if (!_agents.TryGetValue((agentId.Type, agentId.Key), out var agent))+ {+ if (_agentTypes.TryGetValue(agentId.Type, out var agentType))+ {+ using (var scope = RuntimeServiceProvider.CreateScope())+ {+ var scopedProvider = scope.ServiceProvider;+ agent = (Agent)ActivatorUtilities.CreateInstance(scopedProvider, agentType);+ Agent.Initialize(this, agent);+ _agents.TryAdd((agentId.Type, agentId.Key), agent);+ }+ }+ else+ {+ throw new InvalidOperationException($"Agent type '{agentId.Type}' is unknown.");+ }+ }+ return agent;+ }+ private async ValueTask PublishEventAsync(CloudEvent item, Agent sender, CancellationToken cancellationToken = default)+ {+ var activity = Agent.s_source.StartActivity($"PublishEventAsync '{item.Type}'", ActivityKind.Client, Activity.Current?.Context ?? default);+ activity?.SetTag("peer.service", $"{item.Type}/{item.Source}");++ IAgentRuntimeExtensions.Update(this, item, activity);+ await sender.InvokeWithActivityAsync<(IAgentRuntime Worker, CloudEvent Event)>(+ async (state, ct) =>+ {+ await DispatchEventsToAgentsAsync(state.Event, cancellationToken).ConfigureAwait(false);+ },+ (this, item),+ activity,+ item.Type, cancellationToken).ConfigureAwait(false);+ }++ /// <summary>+ /// DispatchEventsToAgentsAsync+ /// writes CloudEvent to Agents+ /// </summary>+ /// <param name="cloudEvent">The CloudEvent to dispatch.</param>+ /// <param name="cancellationToken">The cancellation token.</param>+ private async ValueTask DispatchEventsToAgentsAsync(CloudEvent cloudEvent, CancellationToken cancellationToken = default)+ {+ var taskList = new List<Task>(capacity: _agentTypes.Count);+ foreach (var (typeName, _) in _agentTypes)+ {+ if (typeName == nameof(Client)) { continue; }+ var task = Task.Run(async () =>+ {+ var agent = GetOrActivateAgent(new AgentId { Type = typeName, Key = cloudEvent.GetSubject() });+ agent.ReceiveMessage(new Message { CloudEvent = cloudEvent });+ }, cancellationToken);+ taskList.Add(task);+ }+ await Task.WhenAll(taskList).ConfigureAwait(false);+ }++ public ValueTask RuntimeSendRequestAsync(Agent agent, RpcRequest request, CancellationToken cancellationToken = default)+ {+ throw new NotImplementedException();+ }++ public ValueTask RuntimeSendResponseAsync(RpcResponse response, CancellationToken cancellationToken = default)+ {+ throw new NotImplementedException();+ }+}diff --git a/dotnet/src/Microsoft.AutoGen/Core/AgentWorker.cs b/dotnet/src/Microsoft.AutoGen/Core/AgentWorker.csdeleted file mode 100644index b2d94d247..000000000--- a/dotnet/src/Microsoft.AutoGen/Core/AgentWorker.cs+++ /dev/null@@ -1,294 +0,0 @@-// Copyright (c) Microsoft Corporation. All rights reserved.-// AgentWorker.cs--using System.Collections.Concurrent;-using System.Threading.Channels;-using Microsoft.AutoGen.Contracts;-using Microsoft.Extensions.DependencyInjection;-using Microsoft.Extensions.Hosting;--namespace Microsoft.AutoGen.Core;--/// <summary>-/// Represents a worker that manages agents and handles messages.-/// </summary>-/// <remarks>-/// Initializes a new instance of the <see cref="AgentWorker"/> class.-/// </remarks>-/// <param name="hostApplicationLifetime">The application lifetime.</param>-/// <param name="serviceProvider">The service provider.</param>-/// <param name="configuredAgentTypes">The configured agent types.</param>-public class AgentWorker(- IHostApplicationLifetime hostApplicationLifetime,- IServiceProvider serviceProvider,- [FromKeyedServices("AgentTypes")] IEnumerable<Tuple<string, Type>> configuredAgentTypes) : IHostedService, IAgentWorker-{- private readonly ConcurrentDictionary<string, Type> _agentTypes = new();- private readonly ConcurrentDictionary<(string Type, string Key), Agent> _agents = new();- private readonly Channel<object> _mailbox = Channel.CreateUnbounded<object>();- private readonly ConcurrentDictionary<string, AgentState> _agentStates = new();- private readonly ConcurrentDictionary<string, (Agent Agent, string OriginalRequestId)> _pendingClientRequests = new();- private readonly CancellationTokenSource _shutdownCts = CancellationTokenSource.CreateLinkedTokenSource(hostApplicationLifetime.ApplicationStopping);- public IServiceProvider ServiceProvider { get; } = serviceProvider;- private readonly IEnumerable<Tuple<string, Type>> _configuredAgentTypes = configuredAgentTypes;- private readonly ConcurrentDictionary<string, List<Subscription>> _subscriptionsByAgentType = new();- private readonly ConcurrentDictionary<string, List<string>> _subscriptionsByTopic = new();- private readonly ConcurrentDictionary<Guid, IDictionary<string, string>> _subscriptionsByGuid = new();- private readonly CancellationTokenSource _shutdownCancellationToken = new();- private Task? _mailboxTask;- private readonly object _channelLock = new();-- /// <inheritdoc />- public async ValueTask PublishEventAsync(CloudEvent cloudEvent, CancellationToken cancellationToken = default)- {- foreach (var (typeName, _) in _agentTypes)- {- if (typeName == nameof(Client)) { continue; }- var agent = GetOrActivateAgent(new AgentId { Type = typeName, Key = cloudEvent.GetSubject() });- agent.ReceiveMessage(new Message { CloudEvent = cloudEvent });- }- }-- /// <inheritdoc />- public async ValueTask SendRequestAsync(Agent agent, RpcRequest request, CancellationToken cancellationToken = default)- {- var requestId = Guid.NewGuid().ToString();- _pendingClientRequests[requestId] = (agent, request.RequestId);- request.RequestId = requestId;- await _mailbox.Writer.WriteAsync(request, cancellationToken).ConfigureAwait(false);- }-- /// <inheritdoc />- public ValueTask SendResponseAsync(RpcResponse response, CancellationToken cancellationToken = default)- {- return _mailbox.Writer.WriteAsync(new Message { Response = response }, cancellationToken);- }-- /// <inheritdoc />- public ValueTask SendMessageAsync(Message message, CancellationToken cancellationToken = default)- {- return _mailbox.Writer.WriteAsync(message, cancellationToken);- }-- /// <inheritdoc />- public ValueTask StoreAsync(AgentState value, CancellationToken cancellationToken = default)- {- var agentId = value.AgentId ?? throw new InvalidOperationException("AgentId is required when saving AgentState.");- var response = _agentStates.AddOrUpdate(agentId.ToString(), value, (key, oldValue) => value);- return ValueTask.CompletedTask;- }-- /// <inheritdoc />- public ValueTask<AgentState> ReadAsync(AgentId agentId, CancellationToken cancellationToken = default)- {- _agentStates.TryGetValue(agentId.ToString(), out var state);- if (state is not null && state.AgentId is not null)- {- return new ValueTask<AgentState>(state);- }- else- {- throw new KeyNotFoundException($"Failed to read AgentState for {agentId}.");- }- }-- /// <summary>- /// Runs the message pump.- /// </summary>- public async Task RunMessagePump()- {- await Task.CompletedTask.ConfigureAwait(ConfigureAwaitOptions.ForceYielding);- await foreach (var message in _mailbox.Reader.ReadAllAsync())- {- try- {- if (message == null) { continue; }- switch (message)- {- case Message msg when msg.CloudEvent != null:-- var item = msg.CloudEvent;-- foreach (var (typeName, _) in _agentTypes)- {- var agentToInvoke = GetOrActivateAgent(new AgentId(typeName, item.Source));- agentToInvoke.ReceiveMessage(msg);- }- break;- case Message msg when msg.AddSubscriptionRequest != null:- await SubscribeAsync(msg.AddSubscriptionRequest).ConfigureAwait(true);- break;- case Message msg when msg.AddSubscriptionResponse != null:- break;- case Message msg when msg.RegisterAgentTypeResponse != null:- break;- default:- throw new InvalidOperationException($"Unexpected message '{message}'.");- }- }- catch (OperationCanceledException)- {- }- finally- {- _shutdownCancellationToken.Cancel();- }- }- }- public async ValueTask<AddSubscriptionResponse> SubscribeAsync(AddSubscriptionRequest subscription, CancellationToken cancellationToken = default)- {- var topic = subscription.Subscription.TypeSubscription.TopicType;- var agentType = subscription.Subscription.TypeSubscription.AgentType;- var id = Guid.NewGuid();- subscription.Subscription.Id = id.ToString();- var sub = new Dictionary<string, string> { { topic, agentType } };- _subscriptionsByGuid.GetOrAdd(id, static _ => new Dictionary<string, string>()).Add(topic, agentType);- _subscriptionsByAgentType.GetOrAdd(key: agentType, _ => []).Add(subscription.Subscription);- _subscriptionsByTopic.GetOrAdd(topic, _ => []).Add(agentType);- var response = new AddSubscriptionResponse- {- RequestId = subscription.RequestId,- Error = "",- Success = true- };- return response;- }- public async ValueTask<RemoveSubscriptionResponse> UnsubscribeAsync(RemoveSubscriptionRequest request, CancellationToken cancellationToken = default)- {- if (!Guid.TryParse(request.Id, out var id))- {- var removeSubscriptionResponse = new RemoveSubscriptionResponse- {- Error = "Invalid subscription ID",- Success = false- };- return removeSubscriptionResponse;- }- if (_subscriptionsByGuid.TryGetValue(id, out var sub))- {- foreach (var (topic, agentType) in sub)- {- if (_subscriptionsByTopic.TryGetValue(topic, out var innerAgentTypes))- {- while (innerAgentTypes.Remove(agentType))- {- //ensures all instances are removed- }- _subscriptionsByTopic.AddOrUpdate(topic, innerAgentTypes, (_, _) => innerAgentTypes);- }- var toRemove = new List<Subscription>();- if (_subscriptionsByAgentType.TryGetValue(agentType, out var innerSubscriptions))- {- foreach (var subscription in innerSubscriptions)- {- if (subscription.Id == id.ToString())- {- toRemove.Add(subscription);- }- }- foreach (var subscription in toRemove) { innerSubscriptions.Remove(subscription); }- _subscriptionsByAgentType.AddOrUpdate(agentType, innerSubscriptions, (_, _) => innerSubscriptions);- }- }- _subscriptionsByGuid.TryRemove(id, out _);- }- var response = new RemoveSubscriptionResponse- {- Error = "",- Success = true- };- return response;- }- public async Task StartAsync(CancellationToken cancellationToken)- {- StartCore();-- foreach (var (typeName, type) in _configuredAgentTypes)- {- _agentTypes.TryAdd(typeName, type);- }- void StartCore()- {- var didSuppress = false;- if (!ExecutionContext.IsFlowSuppressed())- {- didSuppress = true;- ExecutionContext.SuppressFlow();- }-- try- {- _mailboxTask = Task.Run(RunMessagePump, CancellationToken.None);- }- finally- {- if (didSuppress)- {- ExecutionContext.RestoreFlow();- }- }- }- }-- /// <inheritdoc />- public async Task StopAsync(CancellationToken cancellationToken)- {- _shutdownCts.Cancel();-- _mailbox.Writer.TryComplete();-- if (_mailboxTask is { } readTask)- {- await readTask.ConfigureAwait(false);- }- lock (_channelLock)- {- }- }-- /// <summary>- /// Gets or activates an agent.- /// </summary>- /// <param name="agentId">The agent ID.</param>- /// <returns>The activated agent.</returns>-- private Agent GetOrActivateAgent(AgentId agentId)- {- if (!_agents.TryGetValue((agentId.Type, agentId.Key), out var agent))- {- if (_agentTypes.TryGetValue(agentId.Type, out var agentType))- {- using (var scope = ServiceProvider.CreateScope())- {- var scopedProvider = scope.ServiceProvider;- agent = (Agent)ActivatorUtilities.CreateInstance(scopedProvider, agentType);- Agent.Initialize(this, agent);- _agents.TryAdd((agentId.Type, agentId.Key), agent);- }- }- else- {- throw new InvalidOperationException($"Agent type '{agentId.Type}' is unknown.");- }- }-- return agent;- }- public ValueTask<List<Subscription>> GetSubscriptionsAsync(Type type)- {- if (_subscriptionsByAgentType.TryGetValue(type.Name, out var subscriptions))- {- return new ValueTask<List<Subscription>>(subscriptions);- }- return new ValueTask<List<Subscription>>([]);- }- public ValueTask<List<Subscription>> GetSubscriptionsAsync(GetSubscriptionsRequest request, CancellationToken cancellationToken = default)- {- var subscriptions = new List<Subscription>();- foreach (var (_, value) in _subscriptionsByAgentType)- {- subscriptions.AddRange(value);- }- return new ValueTask<List<Subscription>>(subscriptions);- }-}diff --git a/dotnet/src/Microsoft.AutoGen/Core/App.cs b/dotnet/src/Microsoft.AutoGen/Core/App.csindex e8d84789f..d13bd5a5f 100644--- a/dotnet/src/Microsoft.AutoGen/Core/App.cs+++ b/dotnet/src/Microsoft.AutoGen/Core/App.cs@@ -39,7 +39,7 @@ public static class AgentsAppawait StartAsync(builder, agents).ConfigureAwait(false);}var client = Host.Services.GetRequiredService<Client>() ?? throw new InvalidOperationException("Host not started");- await client.PublishEventAsync(topic, message, new CancellationToken()).ConfigureAwait(true);+ await client.PublishMessageAsync(message, topic, token: new CancellationToken()).ConfigureAwait(true);return Host;}public static async ValueTask ShutdownAsync()diff --git a/dotnet/src/Microsoft.AutoGen/Core/HostBuilderExtensions.cs b/dotnet/src/Microsoft.AutoGen/Core/HostBuilderExtensions.csindex 339ea6fd1..4cf1fe3ec 100644--- a/dotnet/src/Microsoft.AutoGen/Core/HostBuilderExtensions.cs+++ b/dotnet/src/Microsoft.AutoGen/Core/HostBuilderExtensions.cs@@ -29,15 +29,15 @@ public static class HostBuilderExtensions{var assemblies = AppDomain.CurrentDomain.GetAssemblies();builder.Services.TryAddSingleton(DistributedContextPropagator.Current);- builder.Services.AddSingleton<IAgentWorker, AgentWorker>();- builder.Services.AddSingleton<IHostedService>(sp => (IHostedService)sp.GetRequiredService<IAgentWorker>());+ builder.Services.AddSingleton<IAgentRuntime, AgentRuntime>();+ builder.Services.AddSingleton<IHostedService>(sp => (IHostedService)sp.GetRequiredService<IAgentRuntime>());builder.Services.AddKeyedSingleton("AgentsMetadata", (sp, key) =>{return ReflectionHelper.GetAgentsMetadata(assemblies);});builder.Services.AddSingleton((s) =>{- var worker = s.GetRequiredService<IAgentWorker>();+ var worker = s.GetRequiredService<IAgentRuntime>();var client = ActivatorUtilities.CreateInstance<Client>(s);Agent.Initialize(worker, client);return client;diff --git a/dotnet/src/Microsoft.AutoGen/Core/IAgentRuntime.cs b/dotnet/src/Microsoft.AutoGen/Core/IAgentRuntime.csnew file mode 100644index 000000000..b2e9b4597--- /dev/null+++ b/dotnet/src/Microsoft.AutoGen/Core/IAgentRuntime.cs@@ -0,0 +1,19 @@+// Copyright (c) Microsoft Corporation. All rights reserved.+// IAgentRuntime.cs++using Google.Protobuf;+using Microsoft.AutoGen.Contracts;+namespace Microsoft.AutoGen.Core;++public interface IAgentRuntime+{+ IServiceProvider RuntimeServiceProvider { get; }+ ValueTask RuntimeSendRequestAsync(Agent agent, RpcRequest request, CancellationToken cancellationToken = default);+ ValueTask RuntimeSendResponseAsync(RpcResponse response, CancellationToken cancellationToken = default);+ ValueTask PublishMessageAsync(IMessage message, TopicId topic, Agent? sender, CancellationToken? cancellationToken = default);+ ValueTask SaveStateAsync(AgentState value, CancellationToken cancellationToken = default);+ ValueTask<AgentState> LoadStateAsync(AgentId agentId, CancellationToken cancellationToken = default);+ ValueTask<AddSubscriptionResponse> AddSubscriptionAsync(AddSubscriptionRequest request, CancellationToken cancellationToken = default);+ ValueTask<RemoveSubscriptionResponse> RemoveSubscriptionAsync(RemoveSubscriptionRequest request, CancellationToken cancellationToken = default);+ ValueTask<List<Subscription>> GetSubscriptionsAsync(GetSubscriptionsRequest request, CancellationToken cancellationToken = default);+}diff --git a/dotnet/src/Microsoft.AutoGen/Core/IAgentWorkerExtensions.cs b/dotnet/src/Microsoft.AutoGen/Core/IAgentRuntimeExtensions.cssimilarity index 73%rename from dotnet/src/Microsoft.AutoGen/Core/IAgentWorkerExtensions.csrename to dotnet/src/Microsoft.AutoGen/Core/IAgentRuntimeExtensions.csindex 53dd8890e..479ba06f5 100644--- a/dotnet/src/Microsoft.AutoGen/Core/IAgentWorkerExtensions.cs+++ b/dotnet/src/Microsoft.AutoGen/Core/IAgentRuntimeExtensions.cs@@ -1,5 +1,5 @@// Copyright (c) Microsoft Corporation. All rights reserved.-// IAgentWorkerExtensions.cs+// IAgentRuntimeExtensions.csusing System.Diagnostics;using Google.Protobuf.Collections;@@ -9,11 +9,11 @@ using static Microsoft.AutoGen.Contracts.CloudEvent.Types;namespace Microsoft.AutoGen.Core;-public static class IAgentWorkerExtensions+public static class IAgentRuntimeExtensions{- public static (string?, string?) GetTraceIdAndState(IAgentWorker worker, IDictionary<string, string> metadata)+ public static (string?, string?) GetTraceIdAndState(IAgentRuntime worker, IDictionary<string, string> metadata){- var dcp = worker.ServiceProvider.GetRequiredService<DistributedContextPropagator>();+ var dcp = worker.RuntimeServiceProvider.GetRequiredService<DistributedContextPropagator>();dcp.ExtractTraceIdAndState(metadata,static (object? carrier, string fieldName, out string? fieldValue, out IEnumerable<string>? fieldValues) =>{@@ -25,9 +25,9 @@ public static class IAgentWorkerExtensionsout var traceState);return (traceParent, traceState);}- public static (string?, string?) GetTraceIdAndState(IAgentWorker worker, MapField<string, CloudEventAttributeValue> metadata)+ public static (string?, string?) GetTraceIdAndState(IAgentRuntime worker, MapField<string, CloudEventAttributeValue> metadata){- var dcp = worker.ServiceProvider.GetRequiredService<DistributedContextPropagator>();+ var dcp = worker.RuntimeServiceProvider.GetRequiredService<DistributedContextPropagator>();dcp.ExtractTraceIdAndState(metadata,static (object? carrier, string fieldName, out string? fieldValue, out IEnumerable<string>? fieldValues) =>{@@ -40,9 +40,9 @@ public static class IAgentWorkerExtensionsout var traceState);return (traceParent, traceState);}- public static void Update(IAgentWorker worker, RpcRequest request, Activity? activity = null)+ public static void Update(IAgentRuntime worker, RpcRequest request, Activity? activity = null){- var dcp = worker.ServiceProvider.GetRequiredService<DistributedContextPropagator>();+ var dcp = worker.RuntimeServiceProvider.GetRequiredService<DistributedContextPropagator>();dcp.Inject(activity, request.Metadata, static (carrier, key, value) =>{var metadata = (IDictionary<string, string>)carrier!;@@ -56,9 +56,9 @@ public static class IAgentWorkerExtensions}});}- public static void Update(IAgentWorker worker, CloudEvent cloudEvent, Activity? activity = null)+ public static void Update(IAgentRuntime worker, CloudEvent cloudEvent, Activity? activity = null){- var dcp = worker.ServiceProvider.GetRequiredService<DistributedContextPropagator>();+ var dcp = worker.RuntimeServiceProvider.GetRequiredService<DistributedContextPropagator>();dcp.Inject(activity, cloudEvent.Attributes, static (carrier, key, value) =>{var mapField = (MapField<string, CloudEventAttributeValue>)carrier!;@@ -73,9 +73,9 @@ public static class IAgentWorkerExtensions});}- public static IDictionary<string, string> ExtractMetadata(IAgentWorker worker, IDictionary<string, string> metadata)+ public static IDictionary<string, string> ExtractMetadata(IAgentRuntime worker, IDictionary<string, string> metadata){- var dcp = worker.ServiceProvider.GetRequiredService<DistributedContextPropagator>();+ var dcp = worker.RuntimeServiceProvider.GetRequiredService<DistributedContextPropagator>();var baggage = dcp.ExtractBaggage(metadata, static (object? carrier, string fieldName, out string? fieldValue, out IEnumerable<string>? fieldValues) =>{var metadata = (IDictionary<string, string>)carrier!;@@ -85,9 +85,9 @@ public static class IAgentWorkerExtensionsreturn baggage as IDictionary<string, string> ?? new Dictionary<string, string>();}- public static IDictionary<string, string> ExtractMetadata(IAgentWorker worker, MapField<string, CloudEventAttributeValue> metadata)+ public static IDictionary<string, string> ExtractMetadata(IAgentRuntime worker, MapField<string, CloudEventAttributeValue> metadata){- var dcp = worker.ServiceProvider.GetRequiredService<DistributedContextPropagator>();+ var dcp = worker.RuntimeServiceProvider.GetRequiredService<DistributedContextPropagator>();var baggage = dcp.ExtractBaggage(metadata, static (object? carrier, string fieldName, out string? fieldValue, out IEnumerable<string>? fieldValues) =>{var metadata = (MapField<string, CloudEventAttributeValue>)carrier!;diff --git a/dotnet/src/Microsoft.AutoGen/Core/IAgentWorker.cs b/dotnet/src/Microsoft.AutoGen/Core/IAgentWorker.csdeleted file mode 100644index f3e8bc730..000000000--- a/dotnet/src/Microsoft.AutoGen/Core/IAgentWorker.cs+++ /dev/null@@ -1,18 +0,0 @@-// Copyright (c) Microsoft Corporation. All rights reserved.-// IAgentWorker.cs-using Microsoft.AutoGen.Contracts;-namespace Microsoft.AutoGen.Core;--public interface IAgentWorker-{- IServiceProvider ServiceProvider { get; }- ValueTask PublishEventAsync(CloudEvent evt, 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);- ValueTask<AgentState> ReadAsync(AgentId agentId, CancellationToken cancellationToken = default);- ValueTask<AddSubscriptionResponse> SubscribeAsync(AddSubscriptionRequest request, CancellationToken cancellationToken = default);- ValueTask<RemoveSubscriptionResponse> UnsubscribeAsync(RemoveSubscriptionRequest request, CancellationToken cancellationToken = default);- ValueTask<List<Subscription>> GetSubscriptionsAsync(GetSubscriptionsRequest request, CancellationToken cancellationToken = default);-}diff --git a/dotnet/src/Microsoft.AutoGen/Core/UninitializedAgentWorker.cs b/dotnet/src/Microsoft.AutoGen/Core/UninitializedAgentWorker.csindex 4aa14ac22..d8a8f6386 100644--- a/dotnet/src/Microsoft.AutoGen/Core/UninitializedAgentWorker.cs+++ b/dotnet/src/Microsoft.AutoGen/Core/UninitializedAgentWorker.cs@@ -1,23 +1,22 @@// Copyright (c) Microsoft Corporation. All rights reserved.// UninitializedAgentWorker.cs+using Google.Protobuf;using Microsoft.AutoGen.Contracts;namespace Microsoft.AutoGen.Core;-public class UninitializedAgentWorker() : IAgentWorker+public class UninitializedAgentWorker() : IAgentRuntime{- public IServiceProvider ServiceProvider => throw new AgentInitalizedIncorrectlyException(AgentNotInitializedMessage);+ public IServiceProvider RuntimeServiceProvider => throw new AgentInitalizedIncorrectlyException(AgentNotInitializedMessage);internal const string AgentNotInitializedMessage = "Agent not initialized correctly. An Agent should never be directly intialized - it is always started by the AgentWorker from the Runtime (using the static Initialize() method).";- public ValueTask PublishEventAsync(CloudEvent evt, CancellationToken cancellationToken = default) => throw new AgentInitalizedIncorrectlyException(AgentNotInitializedMessage);- public ValueTask<AgentState> ReadAsync(AgentId agentId, CancellationToken cancellationToken = default) => throw new AgentInitalizedIncorrectlyException(AgentNotInitializedMessage);- public ValueTask SendMessageAsync(Message message, CancellationToken cancellationToken = default) => throw new AgentInitalizedIncorrectlyException(AgentNotInitializedMessage);- public ValueTask SendRequestAsync(Agent agent, RpcRequest request, CancellationToken cancellationToken = default) => throw new AgentInitalizedIncorrectlyException(AgentNotInitializedMessage);- public ValueTask SendResponseAsync(RpcResponse response, CancellationToken cancellationToken = default) => throw new AgentInitalizedIncorrectlyException(AgentNotInitializedMessage);- public ValueTask StoreAsync(AgentState value, CancellationToken cancellationToken = default) => throw new AgentInitalizedIncorrectlyException(AgentNotInitializedMessage);- public ValueTask<List<Subscription>> GetSubscriptionsAsync(Type type) => throw new AgentInitalizedIncorrectlyException(AgentNotInitializedMessage);+ public ValueTask<AgentState> LoadStateAsync(AgentId agentId, CancellationToken cancellationToken = default) => throw new AgentInitalizedIncorrectlyException(AgentNotInitializedMessage);+ public ValueTask RuntimeSendRequestAsync(Agent agent, RpcRequest request, CancellationToken cancellationToken = default) => throw new AgentInitalizedIncorrectlyException(AgentNotInitializedMessage);+ public ValueTask RuntimeSendResponseAsync(RpcResponse response, CancellationToken cancellationToken = default) => throw new AgentInitalizedIncorrectlyException(AgentNotInitializedMessage);+ public ValueTask SaveStateAsync(AgentState value, CancellationToken cancellationToken = default) => throw new AgentInitalizedIncorrectlyException(AgentNotInitializedMessage);public ValueTask<List<Subscription>> GetSubscriptionsAsync(GetSubscriptionsRequest request, CancellationToken cancellationToken = default) => throw new AgentInitalizedIncorrectlyException(AgentNotInitializedMessage);- public ValueTask<AddSubscriptionResponse> SubscribeAsync(AddSubscriptionRequest request, CancellationToken cancellationToken = default) => throw new AgentInitalizedIncorrectlyException(AgentNotInitializedMessage);- public ValueTask<RemoveSubscriptionResponse> UnsubscribeAsync(RemoveSubscriptionRequest request, CancellationToken cancellationToken = default) => throw new AgentInitalizedIncorrectlyException(AgentNotInitializedMessage);+ public ValueTask<AddSubscriptionResponse> AddSubscriptionAsync(AddSubscriptionRequest request, CancellationToken cancellationToken = default) => throw new AgentInitalizedIncorrectlyException(AgentNotInitializedMessage);+ public ValueTask<RemoveSubscriptionResponse> RemoveSubscriptionAsync(RemoveSubscriptionRequest request, CancellationToken cancellationToken = default) => throw new AgentInitalizedIncorrectlyException(AgentNotInitializedMessage);+ public ValueTask PublishMessageAsync(IMessage message, TopicId topic, Agent? sender, CancellationToken? cancellationToken = null) => throw new AgentInitalizedIncorrectlyException(AgentNotInitializedMessage);public class AgentInitalizedIncorrectlyException(string message) : Exception(message){}diff --git a/dotnet/test/Microsoft.AutoGen.Core.Grpc.Tests/AgentGrpcTests.cs b/dotnet/test/Microsoft.AutoGen.Core.Grpc.Tests/AgentGrpcTests.csindex 3594abad8..96b6df153 100644--- a/dotnet/test/Microsoft.AutoGen.Core.Grpc.Tests/AgentGrpcTests.cs+++ b/dotnet/test/Microsoft.AutoGen.Core.Grpc.Tests/AgentGrpcTests.cs@@ -41,7 +41,7 @@ public class AgentGrpcTests{using var runtime = new GrpcRuntime();var (worker, agent) = runtime.Start();- Assert.Equal("GrpcAgentWorker", worker.GetType().Name);+ Assert.Equal(nameof(GrpcAgentRuntime), worker.GetType().Name);await Task.Delay(5000);var subscriptions = await agent.GetSubscriptionsAsync();Assert.Equal(2, subscriptions.Count);@@ -222,7 +222,7 @@ public sealed class GrpcRuntime : IDisposable/// <summary>/// Start - gets a new port and starts fresh instances/// </summary>- public (IAgentWorker, TestAgent) Start(bool initialize = true)+ public (IAgentRuntime, TestAgent) Start(bool initialize = true){int port = GetAvailablePort(); // Get a new port per test run@@ -234,7 +234,7 @@ public sealed class GrpcRuntime : IDisposableClient = StartClientAsync().GetAwaiter().GetResult();var agent = ActivatorUtilities.CreateInstance<TestAgent>(Client.Services);- var worker = Client.Services.GetRequiredService<IAgentWorker>();+ var worker = Client.Services.GetRequiredService<IAgentRuntime>();if (initialize){Agent.Initialize(worker, agent);diff --git a/dotnet/test/Microsoft.AutoGen.Core.Tests/AgentTests.cs b/dotnet/test/Microsoft.AutoGen.Core.Tests/AgentTests.csindex aa0cf91d9..33d2a8eed 100644--- a/dotnet/test/Microsoft.AutoGen.Core.Tests/AgentTests.cs+++ b/dotnet/test/Microsoft.AutoGen.Core.Tests/AgentTests.cs@@ -45,7 +45,7 @@ public class AgentTests(){var runtime = new InMemoryAgentRuntimeFixture();var (worker, agent) = runtime.Start();- Assert.Equal("AgentWorker", worker.GetType().Name);+ Assert.Equal(nameof(AgentRuntime), worker.GetType().Name);var subscriptions = await agent.GetSubscriptionsAsync();Assert.Equal(2, subscriptions.Count);runtime.Stop();@@ -232,10 +232,10 @@ public sealed class InMemoryAgentRuntimeFixture : IDisposable/// Start - starts the agent/// </summary>/// <returns>IAgentWorker, TestAgent</returns>- public (IAgentWorker, TestAgent) Start()+ public (IAgentRuntime, TestAgent) Start(){var agent = ActivatorUtilities.CreateInstance<TestAgent>(AppHost.Services);- var worker = AppHost.Services.GetRequiredService<IAgentWorker>();+ var worker = AppHost.Services.GetRequiredService<IAgentRuntime>();Agent.Initialize(worker, agent);return (worker, agent);}