@@ -24,6 +24,7 @@ public sealed class GrpcGateway : BackgroundService, IGateway
24
24
private readonly ConcurrentDictionary < string , Subscription > _subscriptionsByAgentType = new ( ) ;
25
25
private readonly ConcurrentDictionary < string , List < string > > _subscriptionsByTopic = new ( ) ;
26
26
27
+ private readonly ConcurrentDictionary < string , HashSet < string > > _agentsToEventsMap = new ( ) ;
27
28
// The mapping from agent id to worker process.
28
29
private readonly ConcurrentDictionary < ( string Type , string Key ) , GrpcWorkerConnection > _agentDirectory = new ( ) ;
29
30
// RPC
@@ -42,10 +43,12 @@ public async ValueTask BroadcastEvent(CloudEvent evt)
42
43
{
43
44
// TODO: filter the workers that receive the event
44
45
var tasks = new List < Task > ( _workers . Count ) ;
45
- foreach ( var ( _ , connection ) in _supportedAgentTypes )
46
+ foreach ( var ( key , connection ) in _supportedAgentTypes )
46
47
{
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
+ }
49
52
}
50
53
await Task . WhenAll ( tasks ) . ConfigureAwait ( false ) ;
51
54
}
@@ -142,6 +145,7 @@ private async ValueTask RegisterAgentTypeAsync(GrpcWorkerConnection connection,
142
145
{
143
146
connection . AddSupportedType ( msg . Type ) ;
144
147
_supportedAgentTypes . GetOrAdd ( msg . Type , _ => [ ] ) . Add ( connection ) ;
148
+ _agentsToEventsMap . TryAdd ( msg . Type , new HashSet < string > ( msg . Events ) ) ;
145
149
146
150
await _gatewayRegistry . RegisterAgentType ( msg . Type , _reference ) . ConfigureAwait ( true ) ;
147
151
Message response = new ( )
@@ -153,22 +157,7 @@ private async ValueTask RegisterAgentTypeAsync(GrpcWorkerConnection connection,
153
157
Success = true
154
158
}
155
159
} ;
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
172
161
await connection . ResponseStream . WriteAsync ( response ) . ConfigureAwait ( false ) ;
173
162
}
174
163
private async ValueTask DispatchEventAsync ( CloudEvent evt )
0 commit comments