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

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