You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Program.cs 2.1 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using Microsoft.AutoGen.Agents.Abstractions;
  2. using Microsoft.AutoGen.Agents.Client;
  3. using Microsoft.Extensions.DependencyInjection;
  4. using Microsoft.Extensions.Hosting;
  5. // send a message to the agent
  6. var app = await App.PublishMessageAsync("HelloAgents", new NewMessageReceived
  7. {
  8. Message = "World"
  9. }, local: true);
  10. await App.RuntimeApp!.WaitForShutdownAsync();
  11. await app.WaitForShutdownAsync();
  12. namespace Hello
  13. {
  14. [TopicSubscription("HelloAgents")]
  15. public class HelloAgent(
  16. IAgentContext context,
  17. [FromKeyedServices("EventTypes")] EventTypes typeRegistry) : ConsoleAgent(
  18. context,
  19. typeRegistry),
  20. ISayHello,
  21. IHandle<NewMessageReceived>,
  22. IHandle<ConversationClosed>
  23. {
  24. public async Task Handle(NewMessageReceived item)
  25. {
  26. var response = await SayHello(item.Message).ConfigureAwait(false);
  27. var evt = new Output
  28. {
  29. Message = response
  30. }.ToCloudEvent(this.AgentId.Key);
  31. await PublishEvent(evt).ConfigureAwait(false);
  32. var goodbye = new ConversationClosed
  33. {
  34. UserId = this.AgentId.Key,
  35. UserMessage = "Goodbye"
  36. }.ToCloudEvent(this.AgentId.Key);
  37. await PublishEvent(goodbye).ConfigureAwait(false);
  38. }
  39. public async Task Handle(ConversationClosed item)
  40. {
  41. var goodbye = $"********************* {item.UserId} said {item.UserMessage} ************************";
  42. var evt = new Output
  43. {
  44. Message = goodbye
  45. }.ToCloudEvent(this.AgentId.Key);
  46. await PublishEvent(evt).ConfigureAwait(false);
  47. await App.ShutdownAsync();
  48. }
  49. public async Task<string> SayHello(string ask)
  50. {
  51. var response = $"\n\n\n\n***************Hello {ask}**********************\n\n\n\n";
  52. return response;
  53. }
  54. }
  55. public interface ISayHello
  56. {
  57. public Task<string> SayHello(string ask);
  58. }
  59. }