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.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. // Program.cs
  3. using Microsoft.AutoGen.Abstractions;
  4. using Microsoft.AutoGen.Agents;
  5. // step 1: create in-memory agent runtime
  6. // step 2: register HelloAgent to that agent runtime
  7. // step 3: start the agent runtime
  8. // step 4: send a message to the agent
  9. // step 5: wait for the agent runtime to shutdown
  10. var app = await AgentsApp.PublishMessageAsync("HelloAgents", new NewMessageReceived
  11. {
  12. Message = "World"
  13. }, local: false);
  14. await app.WaitForShutdownAsync();
  15. namespace Hello
  16. {
  17. [TopicSubscription("HelloAgents")]
  18. public class HelloAgent(
  19. IAgentContext context,
  20. [FromKeyedServices("EventTypes")] EventTypes typeRegistry,
  21. IHostApplicationLifetime hostApplicationLifetime) : AgentBase(
  22. context,
  23. typeRegistry),
  24. ISayHello,
  25. IHandleConsole,
  26. IHandle<NewMessageReceived>,
  27. IHandle<ConversationClosed>
  28. {
  29. public async Task Handle(NewMessageReceived item)
  30. {
  31. var response = await SayHello(item.Message).ConfigureAwait(false);
  32. var evt = new Output
  33. {
  34. Message = response
  35. }.ToCloudEvent(this.AgentId.Key);
  36. await PublishEvent(evt).ConfigureAwait(false);
  37. var goodbye = new ConversationClosed
  38. {
  39. UserId = this.AgentId.Key,
  40. UserMessage = "Goodbye"
  41. }.ToCloudEvent(this.AgentId.Key);
  42. await PublishEvent(goodbye).ConfigureAwait(false);
  43. }
  44. public async Task Handle(ConversationClosed item)
  45. {
  46. var goodbye = $"********************* {item.UserId} said {item.UserMessage} ************************";
  47. var evt = new Output
  48. {
  49. Message = goodbye
  50. }.ToCloudEvent(this.AgentId.Key);
  51. await PublishEvent(evt).ConfigureAwait(false);
  52. // Signal shutdown.
  53. hostApplicationLifetime.StopApplication();
  54. }
  55. public async Task<string> SayHello(string ask)
  56. {
  57. var response = $"\n\n\n\n***************Hello {ask}**********************\n\n\n\n";
  58. return response;
  59. }
  60. }
  61. public interface ISayHello
  62. {
  63. public Task<string> SayHello(string ask);
  64. }
  65. }