Skip to content

Commit 3b5d26e

Browse files
committed
add subscription response
1 parent 458d273 commit 3b5d26e

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

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

+7
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ private async Task RunReadPump()
8484
throw new InvalidOperationException($"Failed to register agent: '{message.RegisterAgentTypeResponse.Error}'.");
8585
}
8686
break;
87+
88+
case Message.MessageOneofCase.AddSubscriptionResponse:
89+
if (!message.AddSubscriptionResponse.Success)
90+
{
91+
throw new InvalidOperationException($"Failed to add subscription: '{message.AddSubscriptionResponse.Error}'.");
92+
}
93+
break;
8794

8895
case Message.MessageOneofCase.CloudEvent:
8996

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

+14
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ public sealed class GrpcGateway : BackgroundService, IGateway
2020
// The agents supported by each worker process.
2121
private readonly ConcurrentDictionary<string, List<GrpcWorkerConnection>> _supportedAgentTypes = [];
2222
public readonly ConcurrentDictionary<IConnection, IConnection> _workers = new();
23+
private readonly ConcurrentDictionary<string, Subscription> _subscriptionsByAgentType = new();
24+
private readonly ConcurrentDictionary<string, List<string>> _subscriptionsByTopic = new();
2325

2426
// The mapping from agent id to worker process.
2527
private readonly ConcurrentDictionary<(string Type, string Key), GrpcWorkerConnection> _agentDirectory = new();
@@ -102,10 +104,22 @@ internal async Task OnReceivedMessageAsync(GrpcWorkerConnection connection, Mess
102104
case Message.MessageOneofCase.RegisterAgentTypeRequest:
103105
await RegisterAgentTypeAsync(connection, message.RegisterAgentTypeRequest);
104106
break;
107+
case Message.MessageOneofCase.AddSubscriptionRequest:
108+
await AddSubscriptionAsync(connection, message.AddSubscriptionRequest);
109+
break;
105110
default:
106111
throw new InvalidOperationException($"Unknown message type for message '{message}'.");
107112
};
108113
}
114+
private async ValueTask AddSubscriptionAsync(GrpcWorkerConnection connection, AddSubscriptionRequest request)
115+
{
116+
var topic = request.Subscription.TypeSubscription.TopicType;
117+
var agentType = request.Subscription.TypeSubscription.AgentType;
118+
_subscriptionsByAgentType[agentType] = request.Subscription;
119+
_subscriptionsByTopic[topic].Add(agentType);
120+
var response = new AddSubscriptionResponse { RequestId = request.RequestId, Error = "", Success = true };
121+
await connection.ResponseStream.WriteAsync(response).ConfigureAwait(false);
122+
}
109123
private async ValueTask RegisterAgentTypeAsync(GrpcWorkerConnection connection, RegisterAgentTypeRequest msg)
110124
{
111125
connection.AddSupportedType(msg.Type);

0 commit comments

Comments
 (0)