Skip to content

Commit 16dbe23

Browse files
committed
distributed sample WIP
1 parent 26bfd0e commit 16dbe23

File tree

4 files changed

+46
-23
lines changed

4 files changed

+46
-23
lines changed

dotnet/samples/Hello-distributed/Backend/Agents/HelloAgent.cs

+3-18
Original file line numberDiff line numberDiff line change
@@ -12,40 +12,25 @@ public class HelloAgent(
1212
[FromKeyedServices("EventTypes")] EventTypes typeRegistry) : AgentBase(
1313
context,
1414
typeRegistry),
15-
ISayHello,
1615
IHandleConsole,
17-
IHandle<AppNewMessageReceived>,
18-
IHandle<AppConversationClosed>
16+
IHandle<NewGreetingRequested>
1917
{
20-
public async Task Handle(AppNewMessageReceived item)
18+
public async Task Handle(NewGreetingRequested item)
2119
{
2220
var response = await SayHello(item.Message).ConfigureAwait(false);
2321
var evt = new Output { Message = response };
2422
await PublishMessageAsync(evt).ConfigureAwait(false);
25-
var goodbye = new AppConversationClosed
23+
var goodbye = new NewGreetingGenerated
2624
{
2725
UserId = AgentId.Key,
2826
UserMessage = "Goodbye"
2927
};
3028
await PublishMessageAsync(goodbye).ConfigureAwait(false);
3129
}
32-
public async Task Handle(AppConversationClosed item)
33-
{
34-
var goodbye = $"********************* {item.UserId} said {item.UserMessage} ************************";
35-
var evt = new AppOutput
36-
{
37-
Message = goodbye
38-
};
39-
await PublishMessageAsync(evt).ConfigureAwait(false);
40-
}
4130

4231
public async Task<string> SayHello(string ask)
4332
{
4433
var response = $"\n\n\n\n***************Hello {ask}**********************\n\n\n\n";
4534
return response;
4635
}
4736
}
48-
public interface ISayHello
49-
{
50-
public Task<string> SayHello(string ask);
51-
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// OutputAgent.cs
3+
4+
using Microsoft.AutoGen.Abstractions;
5+
using Microsoft.AutoGen.Agents;
6+
7+
namespace Backend.Agents;
8+
9+
[TopicSubscription("HelloAgents")]
10+
public class OutputAgent(
11+
IAgentRuntime context,
12+
[FromKeyedServices("EventTypes")] EventTypes typeRegistry) : AgentBase(
13+
context,
14+
typeRegistry),
15+
IHandleConsole,
16+
IHandle<NewGreetingGenerated>
17+
{
18+
public async Task Handle(NewGreetingGenerated item)
19+
{
20+
// TODO: store to memory
21+
22+
}
23+
}

dotnet/samples/Hello-distributed/Backend/Program.cs

+17
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Program.cs
33

4+
using Backend;
45
using Backend.Agents;
6+
using Microsoft.AspNetCore.Mvc;
7+
using Microsoft.AutoGen.Abstractions;
58
using Microsoft.AutoGen.Agents;
69
using Microsoft.AutoGen.Extensions.SemanticKernel;
710

@@ -23,6 +26,20 @@
2326
var app = builder.Build();
2427

2528
app.MapDefaultEndpoints();
29+
30+
app.MapPost("/sessions", async ([FromBody]string message, AgentWorker client) =>
31+
{
32+
var session = Guid.NewGuid().ToString();
33+
await client.PublishEventAsync(new NewGreetingRequested { Message = message }.ToCloudEvent(session));
34+
return session;
35+
});
36+
37+
app.MapGet("/sessions/{session}", async (string session) =>
38+
{
39+
40+
return session;
41+
});
42+
2643
app.UseRouting()
2744
.UseEndpoints(endpoints =>
2845
{

dotnet/samples/Hello-distributed/Backend/Protos/messages.proto

+3-5
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@ package helloWorld;
44

55
option csharp_namespace = "Backend";
66

7-
message AppOutput {
7+
message NewGreetingRequested {
88
string message = 1;
99
}
10-
message AppNewMessageReceived {
11-
string message = 1;
12-
}
13-
message AppConversationClosed {
10+
11+
message NewGreetingGenerated {
1412
string user_id = 1;
1513
string user_message = 2;
1614
}

0 commit comments

Comments
 (0)