rysweet-unsubscribe-and-agent-tests-4744 (#4920)

* add tests for core functionality and client/server
* add remove subscription, get subscriptions
* fix LOTS of bugs
* add grpc tuning
* adapt to latest agreed agents_worker proto changes.
This commit is contained in:
Ryan Sweet
2025-01-24 19:24:00 -08:00
committed by GitHub
parent 55e929db98
commit b6597fdd24
132 changed files with 3799 additions and 1472 deletions

View File

@@ -12,6 +12,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../../../src/Microsoft.AutoGen/Core.Grpc/Microsoft.AutoGen.Core.Grpc.csproj" />
<ProjectReference Include="..\..\..\src\Microsoft.AutoGen\Agents\Microsoft.AutoGen.Agents.csproj" />
<ProjectReference Include="..\..\..\src\Microsoft.AutoGen\Contracts\Microsoft.AutoGen.Contracts.csproj" />
<ProjectReference Include="..\..\..\src\Microsoft.AutoGen\Core\Microsoft.AutoGen.Core.csproj" />

View File

@@ -7,21 +7,21 @@ using Microsoft.AutoGen.Contracts;
using Microsoft.AutoGen.Core;
// send a message to the agent
var app = await AgentsApp.PublishMessageAsync("HelloAgents", new NewMessageReceived
var local = true;
if (Environment.GetEnvironmentVariable("AGENT_HOST") != null) { local = false; }
var app = await Microsoft.AutoGen.Core.Grpc.AgentsApp.PublishMessageAsync("HelloAgents", new NewMessageReceived
{
Message = "World"
}, local: false);
}, local: local).ConfigureAwait(false);
await app.WaitForShutdownAsync();
namespace Hello
{
[TopicSubscription("agents")]
[TopicSubscription("HelloAgents")]
public class HelloAgent(
IAgentWorker worker,
IHostApplicationLifetime hostApplicationLifetime,
[FromKeyedServices("EventTypes")] EventTypes typeRegistry) : Agent(
worker,
[FromKeyedServices("AgentsMetadata")] AgentsMetadata typeRegistry) : Agent(
typeRegistry),
IHandleConsole,
IHandle<NewMessageReceived>,
@@ -29,7 +29,7 @@ namespace Hello
IHandle<Shutdown>
{
private AgentState? State { get; set; }
public async Task Handle(NewMessageReceived item)
public async Task Handle(NewMessageReceived item, CancellationToken cancellationToken = default)
{
var response = await SayHello(item.Message).ConfigureAwait(false);
var evt = new Output
@@ -57,7 +57,7 @@ namespace Hello
await PublishMessageAsync(new Shutdown { Message = this.AgentId.Key }).ConfigureAwait(false);
}
public async Task Handle(ConversationClosed item)
public async Task Handle(ConversationClosed item, CancellationToken cancellationToken = default)
{
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" } };
@@ -74,7 +74,7 @@ namespace Hello
TextData = JsonSerializer.Serialize(state)
}).ConfigureAwait(false);
}
public async Task Handle(Shutdown item)
public async Task Handle(Shutdown item, CancellationToken cancellationToken = default)
{
string? workflow = null;
// make sure the workflow is finished

View File

@@ -25,10 +25,10 @@ Flow Diagram:
```mermaid
%%{init: {'theme':'forest'}}%%
graph LR;
A[Main] --> |"PublishEventAsync(NewMessage('World'))"| B{"Handle(NewMessageReceived item)"}
A[Main] --> |"PublishEventAsync(NewMessage('World'))"| B{"Handle(NewMessageReceived item, CancellationToken cancellationToken = default)"}
B --> |"PublishEventAsync(Output('***Hello, World***'))"| C[ConsoleAgent]
C --> D{"WriteConsole()"}
B --> |"PublishEventAsync(ConversationClosed('Goodbye'))"| E{"Handle(ConversationClosed item)"}
B --> |"PublishEventAsync(ConversationClosed('Goodbye'))"| E{"Handle(ConversationClosed item, CancellationToken cancellationToken = default)"}
B --> |"PublishEventAsync(Output('***Goodbye***'))"| C
E --> F{"Shutdown()"}
@@ -44,14 +44,14 @@ Within that event handler you may optionally *emit* new events, which are then s
TopicSubscription("HelloAgents")]
public class HelloAgent(
iAgentWorker worker,
[FromKeyedServices("EventTypes")] EventTypes typeRegistry) : ConsoleAgent(
[FromKeyedServices("AgentsMetadata")] AgentsMetadata typeRegistry) : ConsoleAgent(
worker,
typeRegistry),
ISayHello,
IHandle<NewMessageReceived>,
IHandle<ConversationClosed>
{
public async Task Handle(NewMessageReceived item)
public async Task Handle(NewMessageReceived item, CancellationToken cancellationToken = default)
{
var response = await SayHello(item.Message).ConfigureAwait(false);
var evt = new Output