|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// AgentsAppBuilderExtensions.cs |
| 3 | + |
| 4 | +using System.Diagnostics; |
| 5 | +using Grpc.Core; |
| 6 | +using Grpc.Net.Client.Configuration; |
| 7 | +using Microsoft.AutoGen.Contracts; |
| 8 | +using Microsoft.AutoGen.Protobuf; |
| 9 | +using Microsoft.Extensions.Configuration; |
| 10 | +using Microsoft.Extensions.DependencyInjection; |
| 11 | +using Microsoft.Extensions.DependencyInjection.Extensions; |
| 12 | +using Microsoft.Extensions.Logging; |
| 13 | +namespace Microsoft.AutoGen.Core.Grpc; |
| 14 | + |
| 15 | +public static class AgentsAppBuilderExtensions |
| 16 | +{ |
| 17 | + private const string _defaultAgentServiceAddress = "http://localhost:53071"; |
| 18 | + |
| 19 | + // TODO: How do we ensure AddGrpcAgentWorker and UseInProcessRuntime are mutually exclusive? |
| 20 | + public static AgentsAppBuilder AddGrpcAgentWorker(this AgentsAppBuilder builder, string? agentServiceAddress = null) |
| 21 | + { |
| 22 | + builder.Services.AddGrpcClient<AgentRpc.AgentRpcClient>(options => |
| 23 | + { |
| 24 | + options.Address = new Uri(agentServiceAddress ?? builder.Configuration.GetValue("AGENT_HOST", _defaultAgentServiceAddress)); |
| 25 | + options.ChannelOptionsActions.Add(channelOptions => |
| 26 | + { |
| 27 | + var loggerFactory = new LoggerFactory(); |
| 28 | + if (Debugger.IsAttached) |
| 29 | + { |
| 30 | + channelOptions.HttpHandler = new SocketsHttpHandler |
| 31 | + { |
| 32 | + EnableMultipleHttp2Connections = false, |
| 33 | + KeepAlivePingDelay = TimeSpan.FromSeconds(200), |
| 34 | + KeepAlivePingTimeout = TimeSpan.FromSeconds(100), |
| 35 | + KeepAlivePingPolicy = HttpKeepAlivePingPolicy.Always |
| 36 | + }; |
| 37 | + } |
| 38 | + else |
| 39 | + { |
| 40 | + channelOptions.HttpHandler = new SocketsHttpHandler |
| 41 | + { |
| 42 | + EnableMultipleHttp2Connections = true, |
| 43 | + KeepAlivePingDelay = TimeSpan.FromSeconds(20), |
| 44 | + KeepAlivePingTimeout = TimeSpan.FromSeconds(10), |
| 45 | + KeepAlivePingPolicy = HttpKeepAlivePingPolicy.WithActiveRequests |
| 46 | + }; |
| 47 | + } |
| 48 | + |
| 49 | + var methodConfig = new MethodConfig |
| 50 | + { |
| 51 | + Names = { MethodName.Default }, |
| 52 | + RetryPolicy = new RetryPolicy |
| 53 | + { |
| 54 | + MaxAttempts = 5, |
| 55 | + InitialBackoff = TimeSpan.FromSeconds(1), |
| 56 | + MaxBackoff = TimeSpan.FromSeconds(5), |
| 57 | + BackoffMultiplier = 1.5, |
| 58 | + RetryableStatusCodes = { StatusCode.Unavailable } |
| 59 | + } |
| 60 | + }; |
| 61 | + |
| 62 | + channelOptions.ServiceConfig = new() { MethodConfigs = { methodConfig } }; |
| 63 | + channelOptions.ThrowOperationCanceledOnCancellation = true; |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + builder.Services.TryAddSingleton(DistributedContextPropagator.Current); |
| 68 | + builder.Services.AddSingleton<IAgentRuntime, GrpcAgentRuntime>(); |
| 69 | + builder.Services.AddHostedService<GrpcAgentRuntime>(services => |
| 70 | + { |
| 71 | + return (services.GetRequiredService<IAgentRuntime>() as GrpcAgentRuntime)!; |
| 72 | + }); |
| 73 | + |
| 74 | + return builder; |
| 75 | + } |
| 76 | +} |
0 commit comments