Skip to content

A programming framework for agentic AI

fix: Unhandled Messages should not error when no deserializer is found (#5549)

Jacob Alber8029572f3f8a
diff --git a/dotnet/samples/Hello/HelloAgent/Program.cs b/dotnet/samples/Hello/HelloAgent/Program.cs
index ef15c2059..2e4b9de69 100644
--- a/dotnet/samples/Hello/HelloAgent/Program.cs
+++ b/dotnet/samples/Hello/HelloAgent/Program.cs
@@ -6,10 +6,43 @@ using Microsoft.AutoGen.Contracts;
using Microsoft.AutoGen.Core;
using Microsoft.AutoGen.Core.Grpc;
using Samples;
+
+string? hostAddress = null;
+bool in_host_address = false;
+bool sendHello = true;
+foreach (string arg in args)
+{
+ switch (arg)
+ {
+ case "--host":
+ in_host_address = true;
+ break;
+ case "--nosend":
+ sendHello = false;
+ break;
+ case "-h":
+ case "--help":
+ PrintHelp();
+ Environment.Exit(0);
+ break;
+ default:
+ if (in_host_address)
+ {
+ hostAddress = arg;
+ }
+ break;
+ }
+}
+
+hostAddress ??= Environment.GetEnvironmentVariable("AGENT_HOST");
var appBuilder = new AgentsAppBuilder(); // Create app builder
// if we are using distributed, we need the AGENT_HOST var defined and then we will use the grpc runtime
-if (Environment.GetEnvironmentVariable("AGENT_HOST") is string agentHost)
+
+bool usingGrpc = false;
+if (hostAddress is string agentHost)
{
+ usingGrpc = true;
+ Console.WriteLine($"connecting to {agentHost}");
appBuilder.AddGrpcAgentWorker(agentHost)
.AddAgent<HelloAgent>("HelloAgent");
}
@@ -19,7 +52,31 @@ else
appBuilder.UseInProcessRuntime(deliverToSelf: true).AddAgent<HelloAgent>("HelloAgent");
}
var app = await appBuilder.BuildAsync(); // Build the app
+await app.StartAsync();
// Create a custom message type from proto and define message
-var message = new NewMessageReceived { Message = "Hello World!" };
-await app.PublishMessageAsync(message, new TopicId("HelloTopic")).ConfigureAwait(false); // Publish custom message (handler has been set in HelloAgent)
+
+if (sendHello)
+{
+ var message = new NewMessageReceived { Message = "Hello World!" };
+ await app.PublishMessageAsync(message, new TopicId("HelloTopic")).ConfigureAwait(false); // Publish custom message (handler has been set in HelloAgent)
+}
+else if (!usingGrpc)
+{
+ Console.Write("Warning: Using --nosend with the InProcessRuntime will hang. Terminating.");
+ Environment.Exit(-1);
+}
+
await app.WaitForShutdownAsync().ConfigureAwait(false); // Wait for shutdown from agent
+
+static void PrintHelp()
+{
+ /*
+ HelloAgent [--host <hostAddress>] [--nosend]
+ --host Use gRPC gateway at <hostAddress>; this can also be set using the AGENT_HOST Environment Variable
+ --nosend Do not send the starting message. Note: This means HelloAgent will wait until some other agent will send
+ that message. This will not work when using the InProcessRuntime.
+ */
+ Console.WriteLine("HelloAgent [--host <hostAddress>] [--nosend]");
+ Console.WriteLine(" --host \tUse gRPC gateway at <hostAddress>; this can also be set using the AGENT_HOST Environment Variable");
+ Console.WriteLine(" --nosend \tDo not send the starting message. Note: This means HelloAgent will wait until some other agent will send");
+}
diff --git a/dotnet/src/Microsoft.AutoGen/Core.Grpc/AgentsAppBuilderExtensions.cs b/dotnet/src/Microsoft.AutoGen/Core.Grpc/AgentsAppBuilderExtensions.cs
index 7cf66d4ce..9a11aa06b 100644
--- a/dotnet/src/Microsoft.AutoGen/Core.Grpc/AgentsAppBuilderExtensions.cs
+++ b/dotnet/src/Microsoft.AutoGen/Core.Grpc/AgentsAppBuilderExtensions.cs
@@ -9,6 +9,7 @@ using Microsoft.AutoGen.Protobuf;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
+using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace Microsoft.AutoGen.Core.Grpc;
@@ -17,7 +18,7 @@ public static class AgentsAppBuilderExtensions
private const string _defaultAgentServiceAddress = "http://localhost:53071";
// TODO: How do we ensure AddGrpcAgentWorker and UseInProcessRuntime are mutually exclusive?
- public static AgentsAppBuilder AddGrpcAgentWorker(this AgentsAppBuilder builder, string? agentServiceAddress = null)
+ public static AgentsAppBuilder AddGrpcAgentWorker(this AgentsAppBuilder builder, string? agentServiceAddress = null, bool useStrictDeserialiation = false)
{
builder.Services.AddGrpcClient<AgentRpc.AgentRpcClient>(options =>
{
@@ -65,7 +66,16 @@ public static class AgentsAppBuilderExtensions
});
builder.Services.TryAddSingleton(DistributedContextPropagator.Current);
- builder.Services.AddSingleton<IAgentRuntime, GrpcAgentRuntime>();
+ builder.Services.AddSingleton<IAgentRuntime, GrpcAgentRuntime>(
+ (services) =>
+ {
+ return new GrpcAgentRuntime(
+ services.GetRequiredService<AgentRpc.AgentRpcClient>(),
+ services.GetRequiredService<IHostApplicationLifetime>(),
+ services,
+ services.GetRequiredService<ILogger<GrpcAgentRuntime>>(),
+ useStrictDeserialiation);
+ });
builder.Services.AddHostedService<GrpcAgentRuntime>(services =>
{
return (services.GetRequiredService<IAgentRuntime>() as GrpcAgentRuntime)!;
diff --git a/dotnet/src/Microsoft.AutoGen/Core.Grpc/GrpcAgentRuntime.cs b/dotnet/src/Microsoft.AutoGen/Core.Grpc/GrpcAgentRuntime.cs
index c3fce75b7..5667c1984 100644
--- a/dotnet/src/Microsoft.AutoGen/Core.Grpc/GrpcAgentRuntime.cs
+++ b/dotnet/src/Microsoft.AutoGen/Core.Grpc/GrpcAgentRuntime.cs
@@ -90,7 +90,8 @@ public sealed class GrpcAgentRuntime : IHostedService, IAgentRuntime, IMessageSi
public GrpcAgentRuntime(AgentRpc.AgentRpcClient client,
IHostApplicationLifetime hostApplicationLifetime,
IServiceProvider serviceProvider,
- ILogger<GrpcAgentRuntime> logger)
+ ILogger<GrpcAgentRuntime> logger,
+ bool strictMessageDeserialization = false)
{
this._client = client;
this._logger = logger;
@@ -98,6 +99,7 @@ public sealed class GrpcAgentRuntime : IHostedService, IAgentRuntime, IMessageSi
this._messageRouter = new GrpcMessageRouter(client, this, _clientId, logger, this._shutdownCts.Token);
this._agentsContainer = new AgentsContainer(this, this.SerializationRegistry);
+ this._strictMessageDeserialization = strictMessageDeserialization;
this.ServiceProvider = serviceProvider;
}
@@ -128,6 +130,7 @@ public sealed class GrpcAgentRuntime : IHostedService, IAgentRuntime, IMessageSi
}
}
+ private readonly bool _strictMessageDeserialization;
public IProtoSerializationRegistry SerializationRegistry { get; } = new ProtobufSerializationRegistry();
public void Dispose()
@@ -257,10 +260,22 @@ public sealed class GrpcAgentRuntime : IHostedService, IAgentRuntime, IMessageSi
var agent = await this._agentsContainer.EnsureAgentAsync(recipient);
// give the serializer a second chance to have been registered
- serializer ??= SerializationRegistry.GetSerializer(typeName) ?? throw new Exception($"Could not find a serializer for message of type {typeName}");
- message ??= serializer.Deserialize(evt.ProtoData);
+ serializer ??= SerializationRegistry.GetSerializer(typeName);
- await agent.OnMessageAsync(message, messageContext);
+ if (serializer != null)
+ {
+ message ??= serializer.Deserialize(evt.ProtoData);
+ await agent.OnMessageAsync(message, messageContext);
+ }
+ else if (_strictMessageDeserialization)
+ {
+ throw new Exception($"Could not find a serializer for message of type {typeName}");
+
+ }
+ else
+ {
+ _logger.LogWarning($"Could not find a serializer for message of type {typeName}; this is likely due there not yet being an instantiated agent with a contract for it.");
+ }
}
}
}