Skip to content

Commit d2497d1

Browse files
committed
fix runtime broadcasting
1 parent f985f7d commit d2497d1

File tree

6 files changed

+40
-38
lines changed

6 files changed

+40
-38
lines changed

dotnet/src/Microsoft.AutoGen/Agents/AgentBase.cs

+9-13
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,6 @@ protected AgentBase(
3636
runtime.AgentInstance = this;
3737
this.EventTypes = eventTypes;
3838
_logger = logger ?? LoggerFactory.Create(builder => { }).CreateLogger<AgentBase>();
39-
var subscriptionRequest = new AddSubscriptionRequest
40-
{
41-
RequestId = Guid.NewGuid().ToString(),
42-
Subscription = new Subscription
43-
{
44-
TypeSubscription = new TypeSubscription
45-
{
46-
AgentType = this.AgentId.Type,
47-
TopicType = this.AgentId.Type + "/" + this.AgentId.Key
48-
}
49-
}
50-
};
51-
_runtime.SendMessageAsync(new Message { AddSubscriptionRequest = subscriptionRequest }).AsTask().Wait();
5239
Completion = Start();
5340
}
5441
internal Task Completion { get; }
@@ -125,6 +112,15 @@ await this.InvokeWithActivityAsync(
125112
case Message.MessageOneofCase.Response:
126113
OnResponseCore(msg.Response);
127114
break;
115+
case Message.MessageOneofCase.RegisterAgentTypeResponse:
116+
_logger.LogInformation($"Got {msg.MessageCase} with payload {msg.RegisterAgentTypeResponse}");
117+
break;
118+
case Message.MessageOneofCase.AddSubscriptionResponse:
119+
_logger.LogInformation($"Got {msg.MessageCase} with payload {msg.AddSubscriptionResponse}");
120+
break;
121+
default:
122+
_logger.LogInformation($"Got {msg.MessageCase}");
123+
break;
128124
}
129125
}
130126
public List<string> Subscribe(string topic)

dotnet/src/Microsoft.AutoGen/Agents/Services/Grpc/GrpcAgentWorker.cs

+18-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
using System.Diagnostics;
66
using System.Reflection;
77
using System.Threading.Channels;
8+
using Google.Protobuf;
9+
using Google.Protobuf.Reflection;
810
using Grpc.Core;
911
using Microsoft.AutoGen.Abstractions;
1012
using Microsoft.Extensions.DependencyInjection;
@@ -212,7 +214,7 @@ private async ValueTask RegisterAgentTypeAsync(string type, Type agentType, Canc
212214
{
213215
var events = agentType.GetInterfaces()
214216
.Where(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IHandle<>))
215-
.Select(i => i.GetGenericArguments().First().Name);
217+
.Select(i => GetDescriptorName(i.GetGenericArguments().First()));
216218
//var state = agentType.BaseType?.GetGenericArguments().First();
217219
var topicTypes = agentType.GetCustomAttributes<TopicSubscriptionAttribute>().Select(t => t.Topic);
218220

@@ -224,12 +226,26 @@ await WriteChannelAsync(new Message
224226
RequestId = Guid.NewGuid().ToString(),
225227
//TopicTypes = { topicTypes },
226228
//StateType = state?.Name,
227-
//Events = { events }
229+
Events = { events }
228230
}
229231
}, cancellationToken).ConfigureAwait(false);
230232
}
231233
}
232234

235+
public static string GetDescriptorName(Type messageType)
236+
{
237+
if (typeof(IMessage).IsAssignableFrom(messageType))
238+
{
239+
var descriptorProperty = messageType.GetProperty("Descriptor", BindingFlags.Public | BindingFlags.Static);
240+
if (descriptorProperty != null)
241+
{
242+
var descriptor = descriptorProperty.GetValue(null) as MessageDescriptor;
243+
return descriptor?.FullName??messageType.Name;
244+
}
245+
}
246+
return messageType.Name;
247+
}
248+
233249
// new is intentional
234250
public new async ValueTask SendResponseAsync(RpcResponse response, CancellationToken cancellationToken = default)
235251
{

dotnet/src/Microsoft.AutoGen/Agents/Services/Grpc/GrpcAgentWorkerHostBuilderExtension.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace Microsoft.AutoGen.Agents;
1111

1212
public static class GrpcAgentWorkerHostBuilderExtensions
1313
{
14-
private const string _defaultAgentServiceAddress = "https://localhost:53071";
14+
private const string _defaultAgentServiceAddress = "https://localhost:5001";
1515
public static IHostApplicationBuilder AddGrpcAgentWorker(this IHostApplicationBuilder builder, string? agentServiceAddress = null)
1616
{
1717
builder.Services.AddGrpcClient<AgentRpc.AgentRpcClient>(options =>

dotnet/src/Microsoft.AutoGen/Agents/Services/Grpc/GrpcGateway.cs

+8-19
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public sealed class GrpcGateway : BackgroundService, IGateway
2424
private readonly ConcurrentDictionary<string, Subscription> _subscriptionsByAgentType = new();
2525
private readonly ConcurrentDictionary<string, List<string>> _subscriptionsByTopic = new();
2626

27+
private readonly ConcurrentDictionary<string, HashSet<string>> _agentsToEventsMap = new();
2728
// The mapping from agent id to worker process.
2829
private readonly ConcurrentDictionary<(string Type, string Key), GrpcWorkerConnection> _agentDirectory = new();
2930
// RPC
@@ -42,10 +43,12 @@ public async ValueTask BroadcastEvent(CloudEvent evt)
4243
{
4344
// TODO: filter the workers that receive the event
4445
var tasks = new List<Task>(_workers.Count);
45-
foreach (var (_, connection) in _supportedAgentTypes)
46+
foreach (var (key, connection) in _supportedAgentTypes)
4647
{
47-
48-
tasks.Add(this.SendMessageAsync((IConnection)connection[0], evt, default));
48+
if (_agentsToEventsMap.TryGetValue(key, out var events) && events.Contains(evt.Type))
49+
{
50+
tasks.Add(SendMessageAsync(connection[0], evt, default));
51+
}
4952
}
5053
await Task.WhenAll(tasks).ConfigureAwait(false);
5154
}
@@ -142,6 +145,7 @@ private async ValueTask RegisterAgentTypeAsync(GrpcWorkerConnection connection,
142145
{
143146
connection.AddSupportedType(msg.Type);
144147
_supportedAgentTypes.GetOrAdd(msg.Type, _ => []).Add(connection);
148+
_agentsToEventsMap.TryAdd(msg.Type, new HashSet<string>(msg.Events));
145149

146150
await _gatewayRegistry.RegisterAgentType(msg.Type, _reference).ConfigureAwait(true);
147151
Message response = new()
@@ -153,22 +157,7 @@ private async ValueTask RegisterAgentTypeAsync(GrpcWorkerConnection connection,
153157
Success = true
154158
}
155159
};
156-
// add a default subscription for the agent type
157-
//TODO: we should consider having constraints on the namespace or at least migrate all our examples to use well typed namesspaces like com.microsoft.autogen/hello/HelloAgents etc
158-
var subscriptionRequest = new AddSubscriptionRequest
159-
{
160-
RequestId = Guid.NewGuid().ToString(),
161-
Subscription = new Subscription
162-
{
163-
TypeSubscription = new TypeSubscription
164-
{
165-
AgentType = msg.Type,
166-
TopicType = msg.Type
167-
}
168-
}
169-
};
170-
await AddSubscriptionAsync(connection, subscriptionRequest).ConfigureAwait(true);
171-
160+
// TODO: add Topics from the registration message
172161
await connection.ResponseStream.WriteAsync(response).ConfigureAwait(false);
173162
}
174163
private async ValueTask DispatchEventAsync(CloudEvent evt)

dotnet/src/Microsoft.AutoGen/Agents/Services/Orleans/OrleansRuntimeHostingExtenions.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ namespace Microsoft.AutoGen.Agents;
1313

1414
public static class OrleansRuntimeHostingExtenions
1515
{
16-
public static WebApplicationBuilder AddOrleans(this WebApplicationBuilder builder, bool local = false)
16+
public static IHostApplicationBuilder AddOrleans(this WebApplicationBuilder builder, bool local = false)
1717
{
18-
return builder.AddOrleans(local);
18+
return builder.AddOrleansImpl(local);
1919
}
2020

21-
public static IHostApplicationBuilder AddOrleans(this IHostApplicationBuilder builder, bool local = false)
21+
private static IHostApplicationBuilder AddOrleansImpl(this IHostApplicationBuilder builder, bool local = false)
2222
{
2323
builder.Services.AddSerializer(serializer => serializer.AddProtobufSerializer());
2424
builder.Services.AddSingleton<IRegistryGrain, RegistryGrain>();

protos/agent_worker.proto

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ message Event {
5050
message RegisterAgentTypeRequest {
5151
string request_id = 1;
5252
string type = 2;
53+
repeated string events = 3;
5354
}
5455

5556
message RegisterAgentTypeResponse {

0 commit comments

Comments
 (0)