Ryan Sweet 458d273fc4
Refactoring the services and implementing an in-memory runtime for .NET (#4005)
closes #3950 closes #3702

What this is doing:

I am refactoring the services on the .NET runtime and attempting to clarify the naming and organization.
I added this doc to help capture the naming and concepts.
AgentRuntime / Worker should work similar to the python version and enables running the whole agent system in one process. For remote the system uses the versions of the services in the grpc folder.
lots of other bug fixes/threading cleanup - passing cancellation token throughout
Services update clarifies the naming and roles:

Worker: Hosts the Agents and is a client to the Gateway
Gateway:
-- RPC gateway for the other services APIs
-- Provides an RPC bridge between the workers and the Event Bus
Registry: keeps track of the agents in the system and which events they can handle
AgentState: persistent state for agents
2024-11-12 11:04:59 -08:00

97 lines
3.6 KiB
C#

// Copyright (c) Microsoft Corporation. All rights reserved.
// Program.cs
using System.Text.Json;
using Microsoft.AutoGen.Abstractions;
using Microsoft.AutoGen.Agents;
// send a message to the agent
var app = await AgentsApp.PublishMessageAsync("HelloAgents", new NewMessageReceived
{
Message = "World"
}, local: true);
await app.WaitForShutdownAsync();
namespace Hello
{
[TopicSubscription("HelloAgents")]
public class HelloAgent(
IAgentRuntime context,
IHostApplicationLifetime hostApplicationLifetime,
[FromKeyedServices("EventTypes")] EventTypes typeRegistry) : AgentBase(
context,
typeRegistry),
IHandleConsole,
IHandle<NewMessageReceived>,
IHandle<ConversationClosed>,
IHandle<Shutdown>
{
private AgentState? State { get; set; }
public async Task Handle(NewMessageReceived item)
{
var response = await SayHello(item.Message).ConfigureAwait(false);
var evt = new Output
{
Message = response
};
Dictionary<string, string> state = new()
{
{ "data", "We said hello to " + item.Message },
{ "workflow", "Active" }
};
await StoreAsync(new AgentState
{
AgentId = this.AgentId,
TextData = JsonSerializer.Serialize(state)
}).ConfigureAwait(false);
await PublishMessageAsync(evt).ConfigureAwait(false);
var goodbye = new ConversationClosed
{
UserId = this.AgentId.Key,
UserMessage = "Goodbye"
};
await PublishMessageAsync(goodbye).ConfigureAwait(false);
// send the shutdown message
await PublishMessageAsync(new Shutdown { Message = this.AgentId.Key }).ConfigureAwait(false);
}
public async Task Handle(ConversationClosed item)
{
State = await ReadAsync<AgentState>(this.AgentId).ConfigureAwait(false);
var state = JsonSerializer.Deserialize<Dictionary<string, string>>(State.TextData) ?? new Dictionary<string, string> { { "data", "No state data found" } };
var goodbye = $"\nState: {state}\n********************* {item.UserId} said {item.UserMessage} ************************";
var evt = new Output
{
Message = goodbye
};
await PublishMessageAsync(evt).ConfigureAwait(true);
state["workflow"] = "Complete";
await StoreAsync(new AgentState
{
AgentId = this.AgentId,
TextData = JsonSerializer.Serialize(state)
}).ConfigureAwait(false);
}
public async Task Handle(Shutdown item)
{
string? workflow = null;
// make sure the workflow is finished
while (workflow != "Complete")
{
State = await ReadAsync<AgentState>(this.AgentId).ConfigureAwait(true);
var state = JsonSerializer.Deserialize<Dictionary<string, string>>(State?.TextData ?? "{}") ?? new Dictionary<string, string>();
workflow = state["workflow"];
await Task.Delay(1000).ConfigureAwait(true);
}
// now we can shut down...
hostApplicationLifetime.StopApplication();
}
public async Task<string> SayHello(string ask)
{
var response = $"\n\n\n\n***************Hello {ask}**********************\n\n\n\n";
return response;
}
}
}