Skip to content

Commit

Permalink
adding local runtime and client
Browse files Browse the repository at this point in the history
  • Loading branch information
rysweet committed Oct 8, 2024
1 parent 79b6f11 commit 5c86c20
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 32 deletions.
46 changes: 14 additions & 32 deletions dotnet/samples/Hello/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,32 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Server.Kestrel.Core;

// start the server runtime
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.ConfigureKestrel(serverOptions =>
{
serverOptions.ListenLocalhost(5001, listenOptions =>
{
listenOptions.Protocols = HttpProtocols.Http2;
listenOptions.UseHttps();
});
});
builder.AddAgentService();
builder.UseOrleans(siloBuilder =>
{
siloBuilder.UseLocalhostClustering(); ;
});
builder.AddLocalAgentService();
var app = builder.Build();
app.MapAgentService();
await app.StartAsync();
await app.StartAsync().ConfigureAwait(false);

// start the client worker
var clientBuilder = WebApplication.CreateBuilder(args);
clientBuilder.Services.AddHostedService<AgentWorkerRuntime>();
clientBuilder.Services.AddSingleton<AgentClient>();
var agentBuilder = clientBuilder.AddAgentWorker("https://localhost:5001").AddAgent<HelloAgent>("HelloAgent");
clientBuilder.AddLocalAgentWorker().AddAgent<HelloAgent>("HelloAgent");
var clientApp = clientBuilder.Build();
await clientApp.StartAsync();
await clientApp.StartAsync().ConfigureAwait(false);

// get the client and send a message
AgentClient client = clientApp.Services.GetRequiredService<AgentClient>();
// get the client
var client = clientApp.Services.GetRequiredService<AgentClient>();

//send our hello message event via cloud events
var evt = new NewMessageReceived
{
Message = "World"
}.ToCloudEvent("HelloAgents");
await client.PublishEventAsync(evt);
await client.PublishEventAsync(evt).ConfigureAwait(false);

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

[TopicSubscription("HelloAgents")]
public class HelloAgent(
Expand All @@ -59,32 +44,29 @@ public class HelloAgent(
{
public async Task Handle(NewMessageReceived item)
{
var response = await SayHello(item.Message);
var response = await SayHello(item.Message).ConfigureAwait(false);
var evt = new Output
{
Message = response
}.ToCloudEvent(this.AgentId.Key);
await PublishEvent(evt);
await PublishEvent(evt).ConfigureAwait(false);
var goodbye = new ConversationClosed
{
UserId = this.AgentId.Key,
UserMessage = "Goodbye"
}.ToCloudEvent(this.AgentId.Key);
await PublishEvent(goodbye);

await PublishEvent(goodbye).ConfigureAwait(false);
}

public async Task Handle(ConversationClosed item)
{
var goodbye = $"********************* {item.UserId} said {item.UserMessage} ************************";
var evt = new Output
{
Message = goodbye
}.ToCloudEvent(this.AgentId.Key);
await PublishEvent(evt);
throw new Exception("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^Conversation Closed");
await PublishEvent(evt).ConfigureAwait(false);
throw new NotImplementedException("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^Conversation Closed");
}

public async Task<string> SayHello(string ask)
{
var response = $"\n\n\n\n***************Hello {ask}**********************\n\n\n\n";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ namespace Microsoft.AutoGen.Agents.Client;

public static class HostBuilderExtensions
{
public static AgentApplicationBuilder AddLocalAgentWorker(this IHostApplicationBuilder clientBuilder)
{
clientBuilder.Services.AddHostedService<AgentWorkerRuntime>();
clientBuilder.Services.AddSingleton<AgentClient>();
clientBuilder.AddAgentWorker("https://localhost:5001");
return new AgentApplicationBuilder(clientBuilder);
}
public static AgentApplicationBuilder AddAgentWorker(this IHostApplicationBuilder builder, string agentServiceAddress)
{
builder.Services.AddGrpcClient<AgentRpc.AgentRpcClient>(options =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Diagnostics;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
Expand All @@ -23,6 +25,24 @@ public static IHostApplicationBuilder AddAgentService(this IHostApplicationBuild
return builder;
}

public static WebApplicationBuilder AddLocalAgentService(this WebApplicationBuilder builder)
{
builder.WebHost.ConfigureKestrel(serverOptions =>
{
serverOptions.ListenLocalhost(5001, listenOptions =>
{
listenOptions.Protocols = HttpProtocols.Http2;
listenOptions.UseHttps();
});
});
builder.AddAgentService();
builder.UseOrleans(siloBuilder =>
{
siloBuilder.UseLocalhostClustering(); ;
});
return builder;
}

public static WebApplication MapAgentService(this WebApplication app)
{
app.MapGrpcService<WorkerGatewayService>();
Expand Down

0 comments on commit 5c86c20

Please sign in to comment.