2024-10-30 11:53:37 -07:00
|
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
// Program.cs
|
2024-10-16 20:09:39 -07:00
|
|
|
using Microsoft.AutoGen.Agents;
|
2024-12-12 19:43:26 -08:00
|
|
|
using Microsoft.AutoGen.Contracts;
|
2024-12-13 11:55:43 -08:00
|
|
|
using Microsoft.AutoGen.Core;
|
2024-11-12 11:04:59 -08:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
using Microsoft.Extensions.Hosting;
|
2024-10-02 09:14:54 -07:00
|
|
|
|
2024-12-03 08:09:02 -08:00
|
|
|
var local = true;
|
|
|
|
if (Environment.GetEnvironmentVariable("AGENT_HOST") != null) { local = false; }
|
2024-10-23 14:23:36 -07:00
|
|
|
var app = await AgentsApp.PublishMessageAsync("HelloAgents", new NewMessageReceived
|
2024-10-02 09:14:54 -07:00
|
|
|
{
|
|
|
|
Message = "World"
|
2024-12-03 08:09:02 -08:00
|
|
|
}, local: local).ConfigureAwait(false);
|
2024-10-02 09:14:54 -07:00
|
|
|
await app.WaitForShutdownAsync();
|
|
|
|
|
2024-10-09 11:31:12 -07:00
|
|
|
namespace Hello
|
2024-10-02 09:14:54 -07:00
|
|
|
{
|
2024-12-03 08:09:02 -08:00
|
|
|
[TopicSubscription("agents")]
|
2024-10-09 11:31:12 -07:00
|
|
|
public class HelloAgent(
|
2024-12-17 13:04:37 -08:00
|
|
|
IAgentWorker worker, IHostApplicationLifetime hostApplicationLifetime,
|
2024-12-12 12:43:57 -08:00
|
|
|
[FromKeyedServices("EventTypes")] EventTypes typeRegistry) : Agent(
|
2024-12-17 13:04:37 -08:00
|
|
|
worker,
|
2024-10-09 11:31:12 -07:00
|
|
|
typeRegistry),
|
|
|
|
ISayHello,
|
2024-10-30 09:51:01 -07:00
|
|
|
IHandleConsole,
|
2024-10-09 11:31:12 -07:00
|
|
|
IHandle<NewMessageReceived>,
|
2024-11-19 11:00:48 -08:00
|
|
|
IHandle<ConversationClosed>,
|
|
|
|
IHandle<Shutdown>
|
2024-10-02 09:14:54 -07:00
|
|
|
{
|
2024-10-09 11:31:12 -07:00
|
|
|
public async Task Handle(NewMessageReceived item)
|
2024-10-02 09:14:54 -07:00
|
|
|
{
|
2024-10-09 11:31:12 -07:00
|
|
|
var response = await SayHello(item.Message).ConfigureAwait(false);
|
2024-11-08 14:16:24 +00:00
|
|
|
var evt = new Output { Message = response };
|
|
|
|
await PublishMessageAsync(evt).ConfigureAwait(false);
|
2024-10-09 11:31:12 -07:00
|
|
|
var goodbye = new ConversationClosed
|
|
|
|
{
|
2024-12-17 13:04:37 -08:00
|
|
|
UserId = this.AgentId.Type,
|
2024-10-09 11:31:12 -07:00
|
|
|
UserMessage = "Goodbye"
|
2024-11-08 14:16:24 +00:00
|
|
|
};
|
|
|
|
await PublishMessageAsync(goodbye).ConfigureAwait(false);
|
2024-10-09 11:31:12 -07:00
|
|
|
}
|
|
|
|
public async Task Handle(ConversationClosed item)
|
2024-10-08 10:02:48 -07:00
|
|
|
{
|
2024-10-09 11:31:12 -07:00
|
|
|
var goodbye = $"********************* {item.UserId} said {item.UserMessage} ************************";
|
2024-11-19 11:00:48 -08:00
|
|
|
var evt = new Output { Message = goodbye };
|
|
|
|
await PublishMessageAsync(evt).ConfigureAwait(true);
|
2024-12-03 08:09:02 -08:00
|
|
|
if (Environment.GetEnvironmentVariable("STAY_ALIVE_ON_GOODBYE") != "true")
|
|
|
|
{
|
|
|
|
await PublishMessageAsync(new Shutdown()).ConfigureAwait(false);
|
|
|
|
}
|
2024-11-19 11:00:48 -08:00
|
|
|
}
|
2024-10-23 14:23:36 -07:00
|
|
|
|
2024-11-19 11:00:48 -08:00
|
|
|
public async Task Handle(Shutdown item)
|
|
|
|
{
|
|
|
|
Console.WriteLine("Shutting down...");
|
2024-11-01 15:43:20 -07:00
|
|
|
hostApplicationLifetime.StopApplication();
|
2024-10-09 11:31:12 -07:00
|
|
|
}
|
2024-11-01 15:43:20 -07:00
|
|
|
|
2024-10-09 11:31:12 -07:00
|
|
|
public async Task<string> SayHello(string ask)
|
2024-10-02 09:14:54 -07:00
|
|
|
{
|
2024-10-09 11:31:12 -07:00
|
|
|
var response = $"\n\n\n\n***************Hello {ask}**********************\n\n\n\n";
|
|
|
|
return response;
|
|
|
|
}
|
2024-10-02 09:14:54 -07:00
|
|
|
}
|
2024-10-09 11:31:12 -07:00
|
|
|
public interface ISayHello
|
2024-10-02 09:14:54 -07:00
|
|
|
{
|
2024-10-09 11:31:12 -07:00
|
|
|
public Task<string> SayHello(string ask);
|
2024-10-02 09:14:54 -07:00
|
|
|
}
|
|
|
|
}
|