Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
rysweet committed Oct 8, 2024
1 parent 5c86c20 commit 9e2315e
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 17 deletions.
25 changes: 9 additions & 16 deletions dotnet/samples/Hello/Program.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,29 @@
using Microsoft.Extensions.Hosting;
using Microsoft.AutoGen.Agents.Abstractions;
using Microsoft.AutoGen.Agents.Client;
using Microsoft.AutoGen.Agents.Runtime;
using Runtime = Microsoft.AutoGen.Agents.Runtime;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;

// start the server runtime
var builder = WebApplication.CreateBuilder(args);
builder.AddLocalAgentService();
var app = builder.Build();
app.MapAgentService();
await app.StartAsync().ConfigureAwait(false);

var app = await Runtime.Host.StartAsync(true);
// start the client worker
var clientBuilder = WebApplication.CreateBuilder(args);
clientBuilder.AddLocalAgentWorker().AddAgent<HelloAgent>("HelloAgent");
var clientApp = clientBuilder.Build();
await clientApp.StartAsync().ConfigureAwait(false);

var clientApp = await App.StartAsync<HelloAgent>("HelloAgent");
// get the client
var client = clientApp.Services.GetRequiredService<AgentClient>();

// 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
{
Message = "World"
}.ToCloudEvent("HelloAgents");
await client.PublishEventAsync(evt).ConfigureAwait(false);
await client.PublishEventAsync(evt);

await clientApp.WaitForShutdownAsync().ConfigureAwait(false);
await app.WaitForShutdownAsync().ConfigureAwait(false);
await clientApp.WaitForShutdownAsync();
await app.WaitForShutdownAsync();

[TopicSubscription("HelloAgents")]
public class HelloAgent(
Expand Down
7 changes: 6 additions & 1 deletion dotnet/src/Microsoft.AutoGen.Agents.Client/AgentClient.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System.Diagnostics;
using Google.Protobuf;
using Microsoft.AutoGen.Agents.Abstractions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace Microsoft.AutoGen.Agents.Client;

// TODO: Extract this to be part of the Client
public sealed class AgentClient(ILogger<AgentClient> logger, AgentWorkerRuntime runtime, DistributedContextPropagator distributedContextPropagator,
[FromKeyedServices("EventTypes")] EventTypes eventTypes)
: AgentBase(new ClientContext(logger, runtime, distributedContextPropagator), eventTypes)
Expand All @@ -25,6 +25,11 @@ 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
15 changes: 15 additions & 0 deletions dotnet/src/Microsoft.AutoGen.Agents.Client/App.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Microsoft.AspNetCore.Builder;

namespace Microsoft.AutoGen.Agents.Client;

public static class App
{
public static async Task<WebApplication> StartAsync<T>(string name) where T : AgentBase
{
var clientBuilder = WebApplication.CreateBuilder();
clientBuilder.AddLocalAgentWorker().AddAgent<T>(name);
var clientApp = clientBuilder.Build();
await clientApp.StartAsync().ConfigureAwait(false);
return clientApp;
}
}
23 changes: 23 additions & 0 deletions dotnet/src/Microsoft.AutoGen.Agents.Runtime/Host.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Microsoft.AspNetCore.Builder;

namespace Microsoft.AutoGen.Agents.Runtime;

public static class Host
{
public static async Task<WebApplication> StartAsync(bool local = false)
{
var builder = WebApplication.CreateBuilder();
if (local)
{
builder.AddLocalAgentService();
}
else
{
builder.AddAgentService();
}
var app = builder.Build();
app.MapAgentService();
await app.StartAsync().ConfigureAwait(false);
return app;
}
}

0 comments on commit 9e2315e

Please sign in to comment.