From 5abe903657d3f48ff8730f72a10aeb4fd4c9c3d3 Mon Sep 17 00:00:00 2001 From: Ryan Sweet Date: Tue, 8 Oct 2024 05:53:17 -0700 Subject: [PATCH] enhance app and simlify hello --- dotnet/samples/Hello/Program.cs | 20 +++-------- .../AgentClient.cs | 9 +++-- .../Microsoft.AutoGen.Agents.Client/App.cs | 33 ++++++++++++++----- 3 files changed, 33 insertions(+), 29 deletions(-) diff --git a/dotnet/samples/Hello/Program.cs b/dotnet/samples/Hello/Program.cs index 94a7d0111118..b1dfb1d5d3f2 100644 --- a/dotnet/samples/Hello/Program.cs +++ b/dotnet/samples/Hello/Program.cs @@ -1,25 +1,15 @@ -using Microsoft.Extensions.Hosting; using Microsoft.AutoGen.Agents.Abstractions; using Microsoft.AutoGen.Agents.Client; using Microsoft.Extensions.DependencyInjection; -using Microsoft.AspNetCore.Hosting; - -// start the client worker -var clientApp = await App.StartAsync(local: true); -// get the client -var client = clientApp.Services.GetRequiredService(); +using Microsoft.Extensions.Hosting; -// why doesn't this work? -//await client.PublishEventAsync("HelloAgents", new NewMessageReceived{ Message = "World" }) -// instead we have to do this -//send our hello message event via cloud events -var evt = new NewMessageReceived +// send a message to the agent +var app = await App.PublishMessageAsync("HelloAgents", new NewMessageReceived { Message = "World" -}.ToCloudEvent("HelloAgents"); -await client.PublishEventAsync(evt); +}, local: true); -await clientApp.WaitForShutdownAsync(); +await app.WaitForShutdownAsync(); [TopicSubscription("HelloAgents")] public class HelloAgent( diff --git a/dotnet/src/Microsoft.AutoGen.Agents.Client/AgentClient.cs b/dotnet/src/Microsoft.AutoGen.Agents.Client/AgentClient.cs index 6c4f96edff67..00518d3ce28f 100644 --- a/dotnet/src/Microsoft.AutoGen.Agents.Client/AgentClient.cs +++ b/dotnet/src/Microsoft.AutoGen.Agents.Client/AgentClient.cs @@ -13,6 +13,10 @@ public sealed class AgentClient(ILogger logger, AgentWorkerRuntime public async ValueTask PublishEventAsync(CloudEvent evt) => await PublishEvent(evt); public async ValueTask SendRequestAsync(AgentId target, string method, Dictionary parameters) => await RequestAsync(target, method, parameters); + public async ValueTask PublishEventAsync(string topic, IMessage evt) + { + await PublishEventAsync(evt.ToCloudEvent(topic)).ConfigureAwait(false); + } private sealed class ClientContext(ILogger logger, AgentWorkerRuntime runtime, DistributedContextPropagator distributedContextPropagator) : IAgentContext { public AgentId AgentId { get; } = new AgentId("client", Guid.NewGuid().ToString()); @@ -25,11 +29,6 @@ public async ValueTask PublishEventAsync(CloudEvent @event) await runtime.PublishEvent(@event).ConfigureAwait(false); } - public async ValueTask PublishEventAsync(string topic, IMessage evt) - { - await PublishEventAsync(evt.ToCloudEvent(topic)).ConfigureAwait(false); - } - public async ValueTask SendRequestAsync(AgentBase agent, RpcRequest request) { await runtime.SendRequest(AgentInstance!, request).ConfigureAwait(false); diff --git a/dotnet/src/Microsoft.AutoGen.Agents.Client/App.cs b/dotnet/src/Microsoft.AutoGen.Agents.Client/App.cs index 3635318cf8c8..cdff0e8cb500 100644 --- a/dotnet/src/Microsoft.AutoGen.Agents.Client/App.cs +++ b/dotnet/src/Microsoft.AutoGen.Agents.Client/App.cs @@ -1,4 +1,6 @@ +using Google.Protobuf; using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.DependencyInjection; namespace Microsoft.AutoGen.Agents.Client; @@ -6,13 +8,11 @@ public static class App { // need a variable to store the runtime instance public static WebApplication? RuntimeApp { get; set; } - public static async Task StartAsync(AgentTypes? agentTypes = null, bool local = false) + public static WebApplication? ClientApp { get; set; } + public static async ValueTask StartAsync(AgentTypes? agentTypes = null, bool local = false) { - if (RuntimeApp == null) - { - // start the server runtime - RuntimeApp = await Runtime.Host.StartAsync(local); - } + // start the server runtime + RuntimeApp ??= await Runtime.Host.StartAsync(local); var clientBuilder = WebApplication.CreateBuilder(); var appBuilder = clientBuilder.AddAgentWorker(); agentTypes ??= AgentTypes.GetAgentTypesFromAssembly() @@ -21,8 +21,23 @@ public static async Task StartAsync(AgentTypes? agentTypes = nul { appBuilder.AddAgent(type.Key, type.Value); } - var clientApp = clientBuilder.Build(); - await clientApp.StartAsync().ConfigureAwait(false); - return clientApp; + ClientApp = clientBuilder.Build(); + await ClientApp.StartAsync().ConfigureAwait(false); + return ClientApp; + } + + public static async ValueTask PublishMessageAsync( + string topic, + IMessage message, + AgentTypes? agentTypes = null, + bool local = false) + { + if (ClientApp == null) + { + ClientApp = await App.StartAsync(agentTypes, local); + } + var client = ClientApp.Services.GetRequiredService() ?? throw new InvalidOperationException("Client not started"); + await client.PublishEventAsync(topic, message).ConfigureAwait(false); + return ClientApp; } } \ No newline at end of file