11// Copyright (c) Microsoft Corporation. All rights reserved.
2- // GrpcAgentWorker .cs
2+ // GrpcAgentRuntime .cs
33
44using System . Collections . Concurrent ;
55using System . Reflection ;
1212
1313namespace Microsoft . AutoGen . Core . Grpc ;
1414
15- public sealed class GrpcAgentWorker (
15+ public sealed class GrpcAgentRuntime (
1616 AgentRpc . AgentRpcClient client ,
1717 IHostApplicationLifetime hostApplicationLifetime ,
1818 IServiceProvider serviceProvider ,
1919 [ FromKeyedServices ( "AgentTypes" ) ] IEnumerable < Tuple < string , Type > > configuredAgentTypes ,
20- ILogger < GrpcAgentWorker > logger ) :
21- IHostedService , IDisposable , IAgentWorker
20+ ILogger < GrpcAgentRuntime > logger
21+ ) : AgentRuntime (
22+ hostApplicationLifetime ,
23+ serviceProvider ,
24+ configuredAgentTypes
25+ ) , IDisposable
2226{
2327 private readonly object _channelLock = new ( ) ;
2428 private readonly ConcurrentDictionary < string , Type > _agentTypes = new ( ) ;
@@ -35,13 +39,11 @@ public sealed class GrpcAgentWorker(
3539 private readonly AgentRpc . AgentRpcClient _client = client ;
3640 public readonly IServiceProvider ServiceProvider = serviceProvider ;
3741 private readonly IEnumerable < Tuple < string , Type > > _configuredAgentTypes = configuredAgentTypes ;
38- private readonly ILogger < GrpcAgentWorker > _logger = logger ;
42+ private readonly ILogger < GrpcAgentRuntime > _logger = logger ;
3943 private readonly CancellationTokenSource _shutdownCts = CancellationTokenSource . CreateLinkedTokenSource ( hostApplicationLifetime . ApplicationStopping ) ;
4044 private AsyncDuplexStreamingCall < Message , Message > ? _channel ;
4145 private Task ? _readTask ;
4246 private Task ? _writeTask ;
43-
44- IServiceProvider IAgentWorker . ServiceProvider => ServiceProvider ;
4547 public void Dispose ( )
4648 {
4749 _outboundMessagesChannel . Writer . TryComplete ( ) ;
@@ -289,25 +291,24 @@ await WriteChannelAsync(new Message
289291 }
290292 }
291293 // new is intentional
292- public async ValueTask SendResponseAsync ( RpcResponse response , CancellationToken cancellationToken = default )
294+ public new async ValueTask RuntimeSendResponseAsync ( RpcResponse response , CancellationToken cancellationToken = default )
293295 {
294296 await WriteChannelAsync ( new Message { Response = response } , cancellationToken ) . ConfigureAwait ( false ) ;
295297 }
296298
297- public async ValueTask SendRequestAsync ( Agent agent , RpcRequest request , CancellationToken cancellationToken = default )
299+ public new async ValueTask RuntimeSendRequestAsync ( Agent agent , RpcRequest request , CancellationToken cancellationToken = default )
298300 {
299301 var requestId = Guid . NewGuid ( ) . ToString ( ) ;
300302 _pendingRequests [ requestId ] = ( agent , request . RequestId ) ;
301303 request . RequestId = requestId ;
302304 await WriteChannelAsync ( new Message { Request = request } , cancellationToken ) . ConfigureAwait ( false ) ;
303305 }
304-
305- public async ValueTask SendMessageAsync ( Message message , CancellationToken cancellationToken = default )
306+ public new async ValueTask RuntimeWriteMessage ( Message message , CancellationToken cancellationToken = default )
306307 {
307308 await WriteChannelAsync ( message , cancellationToken ) . ConfigureAwait ( false ) ;
308309 }
309310
310- public async ValueTask PublishEventAsync ( CloudEvent @event , CancellationToken cancellationToken = default )
311+ public async ValueTask RuntimePublishEventAsync ( CloudEvent @event , CancellationToken cancellationToken = default )
311312 {
312313 await WriteChannelAsync ( new Message { CloudEvent = @event } , cancellationToken ) . ConfigureAwait ( false ) ;
313314 }
@@ -350,11 +351,10 @@ private AsyncDuplexStreamingCall<Message, Message> RecreateChannel(AsyncDuplexSt
350351
351352 return _channel ;
352353 }
353-
354- public async Task StartAsync ( CancellationToken cancellationToken )
354+ public new async Task StartAsync ( CancellationToken cancellationToken )
355355 {
356356 _channel = GetChannel ( ) ;
357- _logger . LogInformation ( "Starting GrpcAgentWorker, connecting to gRPC endpoint " + Environment . GetEnvironmentVariable ( "AGENT_HOST" ) ) ;
357+ _logger . LogInformation ( "Starting " + GetType ( ) . Name + ", connecting to gRPC endpoint " + Environment . GetEnvironmentVariable ( "AGENT_HOST" ) ) ;
358358
359359 StartCore ( ) ;
360360
@@ -389,8 +389,7 @@ void StartCore()
389389 }
390390 }
391391 }
392-
393- public async Task StopAsync ( CancellationToken cancellationToken )
392+ public new async Task StopAsync ( CancellationToken cancellationToken )
394393 {
395394 _shutdownCts . Cancel ( ) ;
396395
@@ -410,8 +409,7 @@ public async Task StopAsync(CancellationToken cancellationToken)
410409 _channel ? . Dispose ( ) ;
411410 }
412411 }
413-
414- public async ValueTask StoreAsync ( AgentState value , CancellationToken cancellationToken = default )
412+ public new async ValueTask SaveStateAsync ( AgentState value , CancellationToken cancellationToken = default )
415413 {
416414 var agentId = value . AgentId ?? throw new InvalidOperationException ( "AgentId is required when saving AgentState." ) ;
417415 var response = _client . SaveState ( value , null , null , cancellationToken ) ;
@@ -421,7 +419,7 @@ public async ValueTask StoreAsync(AgentState value, CancellationToken cancellati
421419 }
422420 }
423421
424- public async ValueTask < AgentState > ReadAsync ( AgentId agentId , CancellationToken cancellationToken = default )
422+ public new async ValueTask < AgentState > LoadStateAsync ( AgentId agentId , CancellationToken cancellationToken = default )
425423 {
426424 var response = await _client . GetStateAsync ( agentId ) . ConfigureAwait ( true ) ;
427425 // if (response.Success && response.AgentState.AgentId is not null) - why is success always false?
@@ -434,20 +432,20 @@ public async ValueTask<AgentState> ReadAsync(AgentId agentId, CancellationToken
434432 throw new KeyNotFoundException ( $ "Failed to read AgentState for { agentId } .") ;
435433 }
436434 }
437- public async ValueTask < List < Subscription > > GetSubscriptionsAsync ( GetSubscriptionsRequest request , CancellationToken cancellationToken = default )
435+ public new async ValueTask < List < Subscription > > GetSubscriptionsAsync ( GetSubscriptionsRequest request , CancellationToken cancellationToken = default )
438436 {
439437 var response = await _client . GetSubscriptionsAsync ( request , null , null , cancellationToken ) ;
440438 return response . Subscriptions . ToList ( ) ;
441439 }
442- public ValueTask < AddSubscriptionResponse > SubscribeAsync ( AddSubscriptionRequest request , CancellationToken cancellationToken = default )
440+ public new async Task < AddSubscriptionResponse > AddSubscriptionAsync ( AddSubscriptionRequest request , CancellationToken cancellationToken = default )
443441 {
444442 var response = _client . AddSubscription ( request , null , null , cancellationToken ) ;
445- return new ValueTask < AddSubscriptionResponse > ( response ) ;
443+ return response ;
446444 }
447- public ValueTask < RemoveSubscriptionResponse > UnsubscribeAsync ( RemoveSubscriptionRequest request , CancellationToken cancellationToken = default )
445+ public new async ValueTask < RemoveSubscriptionResponse > RemoveSubscriptionAsync ( RemoveSubscriptionRequest request , CancellationToken cancellationToken = default )
448446 {
449447 var response = _client . RemoveSubscription ( request , null , null , cancellationToken ) ;
450- return new ValueTask < RemoveSubscriptionResponse > ( response ) ;
448+ return response ;
451449 }
452450}
453451
0 commit comments