Skip to content

Commit

Permalink
enhance app and simlify hello
Browse files Browse the repository at this point in the history
  • Loading branch information
rysweet committed Oct 8, 2024
1 parent 48856d5 commit 5abe903
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 29 deletions.
20 changes: 5 additions & 15 deletions dotnet/samples/Hello/Program.cs
Original file line number Diff line number Diff line change
@@ -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<AgentClient>();
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(
Expand Down
9 changes: 4 additions & 5 deletions dotnet/src/Microsoft.AutoGen.Agents.Client/AgentClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ public sealed class AgentClient(ILogger<AgentClient> logger, AgentWorkerRuntime
public async ValueTask PublishEventAsync(CloudEvent evt) => await PublishEvent(evt);
public async ValueTask<RpcResponse> SendRequestAsync(AgentId target, string method, Dictionary<string, string> 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<AgentClient> logger, AgentWorkerRuntime runtime, DistributedContextPropagator distributedContextPropagator) : IAgentContext
{
public AgentId AgentId { get; } = new AgentId("client", Guid.NewGuid().ToString());
Expand All @@ -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);
Expand Down
33 changes: 24 additions & 9 deletions dotnet/src/Microsoft.AutoGen.Agents.Client/App.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
using Google.Protobuf;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;

namespace Microsoft.AutoGen.Agents.Client;

public static class App
{
// need a variable to store the runtime instance
public static WebApplication? RuntimeApp { get; set; }
public static async Task<WebApplication> StartAsync(AgentTypes? agentTypes = null, bool local = false)
public static WebApplication? ClientApp { get; set; }
public static async ValueTask<WebApplication> 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()
Expand All @@ -21,8 +21,23 @@ public static async Task<WebApplication> 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<WebApplication> 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<AgentClient>() ?? throw new InvalidOperationException("Client not started");
await client.PublishEventAsync(topic, message).ConfigureAwait(false);
return ClientApp;
}
}

0 comments on commit 5abe903

Please sign in to comment.