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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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: false);
  16. await app.WaitForShutdownAsync();
  17. namespace Hello
  18. {
  19. [TopicSubscription("HelloAgents")]
  20. public class HelloAgent(
  21. IAgentContext context,
  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
  34. {
  35. Message = response
  36. }.ToCloudEvent(this.AgentId.Key);
  37. await PublishEvent(evt).ConfigureAwait(false);
  38. var goodbye = new ConversationClosed
  39. {
  40. UserId = this.AgentId.Key,
  41. UserMessage = "Goodbye"
  42. }.ToCloudEvent(this.AgentId.Key);
  43. await PublishEvent(goodbye).ConfigureAwait(false);
  44. }
  45. public async Task Handle(ConversationClosed item)
  46. {
  47. var goodbye = $"********************* {item.UserId} said {item.UserMessage} ************************";
  48. var evt = new Output
  49. {
  50. Message = goodbye
  51. }.ToCloudEvent(this.AgentId.Key);
  52. await PublishEvent(evt).ConfigureAwait(false);
  53. //sleep
  54. await Task.Delay(10000).ConfigureAwait(false);
  55. await AgentsApp.ShutdownAsync().ConfigureAwait(false);
  56. }
  57. public async Task<string> SayHello(string ask)
  58. {
  59. var response = $"\n\n\n\n***************Hello {ask}**********************\n\n\n\n";
  60. return response;
  61. }
  62. }
  63. public interface ISayHello
  64. {
  65. public Task<string> SayHello(string ask);
  66. }
  67. }