2024-10-30 11:53:37 -07:00
|
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
// Program.cs
|
2024-10-23 14:23:36 -07:00
|
|
|
|
2024-10-16 20:09:39 -07:00
|
|
|
using Microsoft.AutoGen.Abstractions;
|
|
|
|
using Microsoft.AutoGen.Agents;
|
2024-10-02 09:14:54 -07:00
|
|
|
|
2024-10-30 11:53:37 -07:00
|
|
|
// step 1: create in-memory agent runtime
|
|
|
|
|
|
|
|
// step 2: register HelloAgent to that agent runtime
|
|
|
|
|
|
|
|
// step 3: start the agent runtime
|
|
|
|
|
|
|
|
// step 4: send a message to the agent
|
|
|
|
|
|
|
|
// step 5: wait for the agent runtime to shutdown
|
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-10-23 14:23:36 -07:00
|
|
|
}, local: false);
|
2024-10-08 10:02:48 -07:00
|
|
|
|
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-10-09 11:31:12 -07:00
|
|
|
[TopicSubscription("HelloAgents")]
|
|
|
|
public class HelloAgent(
|
|
|
|
IAgentContext context,
|
2024-11-01 15:43:20 -07:00
|
|
|
[FromKeyedServices("EventTypes")] EventTypes typeRegistry,
|
|
|
|
IHostApplicationLifetime hostApplicationLifetime) : AgentBase(
|
2024-10-09 11:31:12 -07:00
|
|
|
context,
|
|
|
|
typeRegistry),
|
|
|
|
ISayHello,
|
2024-10-30 09:51:01 -07:00
|
|
|
IHandleConsole,
|
2024-10-09 11:31:12 -07:00
|
|
|
IHandle<NewMessageReceived>,
|
|
|
|
IHandle<ConversationClosed>
|
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);
|
|
|
|
var evt = new Output
|
|
|
|
{
|
|
|
|
Message = response
|
|
|
|
}.ToCloudEvent(this.AgentId.Key);
|
|
|
|
await PublishEvent(evt).ConfigureAwait(false);
|
|
|
|
var goodbye = new ConversationClosed
|
|
|
|
{
|
|
|
|
UserId = this.AgentId.Key,
|
|
|
|
UserMessage = "Goodbye"
|
|
|
|
}.ToCloudEvent(this.AgentId.Key);
|
|
|
|
await PublishEvent(goodbye).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
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} ************************";
|
|
|
|
var evt = new Output
|
|
|
|
{
|
|
|
|
Message = goodbye
|
|
|
|
}.ToCloudEvent(this.AgentId.Key);
|
|
|
|
await PublishEvent(evt).ConfigureAwait(false);
|
2024-10-23 14:23:36 -07:00
|
|
|
|
2024-11-01 15:43:20 -07:00
|
|
|
// Signal shutdown.
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|