Skip to content

Commit

Permalink
distributed sample WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
kostapetan committed Nov 16, 2024
1 parent 26bfd0e commit 16dbe23
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 23 deletions.
21 changes: 3 additions & 18 deletions dotnet/samples/Hello-distributed/Backend/Agents/HelloAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,25 @@ public class HelloAgent(
[FromKeyedServices("EventTypes")] EventTypes typeRegistry) : AgentBase(
context,
typeRegistry),
ISayHello,
IHandleConsole,
IHandle<AppNewMessageReceived>,
IHandle<AppConversationClosed>
IHandle<NewGreetingRequested>
{
public async Task Handle(AppNewMessageReceived item)
public async Task Handle(NewGreetingRequested item)
{
var response = await SayHello(item.Message).ConfigureAwait(false);
var evt = new Output { Message = response };
await PublishMessageAsync(evt).ConfigureAwait(false);
var goodbye = new AppConversationClosed
var goodbye = new NewGreetingGenerated
{
UserId = AgentId.Key,
UserMessage = "Goodbye"
};
await PublishMessageAsync(goodbye).ConfigureAwait(false);
}
public async Task Handle(AppConversationClosed item)
{
var goodbye = $"********************* {item.UserId} said {item.UserMessage} ************************";
var evt = new AppOutput
{
Message = goodbye
};
await PublishMessageAsync(evt).ConfigureAwait(false);
}

public async Task<string> SayHello(string ask)
{
var response = $"\n\n\n\n***************Hello {ask}**********************\n\n\n\n";
return response;
}
}
public interface ISayHello
{
public Task<string> SayHello(string ask);
}
23 changes: 23 additions & 0 deletions dotnet/samples/Hello-distributed/Backend/Agents/OutputAgent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// OutputAgent.cs

using Microsoft.AutoGen.Abstractions;
using Microsoft.AutoGen.Agents;

namespace Backend.Agents;

[TopicSubscription("HelloAgents")]
public class OutputAgent(
IAgentRuntime context,
[FromKeyedServices("EventTypes")] EventTypes typeRegistry) : AgentBase(
context,
typeRegistry),
IHandleConsole,
IHandle<NewGreetingGenerated>
{
public async Task Handle(NewGreetingGenerated item)
{
// TODO: store to memory

}
}
17 changes: 17 additions & 0 deletions dotnet/samples/Hello-distributed/Backend/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Program.cs

using Backend;
using Backend.Agents;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AutoGen.Abstractions;
using Microsoft.AutoGen.Agents;
using Microsoft.AutoGen.Extensions.SemanticKernel;

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

app.MapDefaultEndpoints();

app.MapPost("/sessions", async ([FromBody]string message, AgentWorker client) =>
{
var session = Guid.NewGuid().ToString();
await client.PublishEventAsync(new NewGreetingRequested { Message = message }.ToCloudEvent(session));
return session;
});

app.MapGet("/sessions/{session}", async (string session) =>
{
return session;
});

app.UseRouting()
.UseEndpoints(endpoints =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ package helloWorld;

option csharp_namespace = "Backend";

message AppOutput {
message NewGreetingRequested {
string message = 1;
}
message AppNewMessageReceived {
string message = 1;
}
message AppConversationClosed {

message NewGreetingGenerated {
string user_id = 1;
string user_message = 2;
}
Expand Down

0 comments on commit 16dbe23

Please sign in to comment.