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 3.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. // Program.cs
  3. using System.Text.Json;
  4. using Microsoft.AutoGen.Abstractions;
  5. using Microsoft.AutoGen.Agents;
  6. // send a message to the agent
  7. var app = await AgentsApp.PublishMessageAsync("HelloAgents", new NewMessageReceived
  8. {
  9. Message = "World"
  10. }, local: true);
  11. await app.WaitForShutdownAsync();
  12. namespace Hello
  13. {
  14. [TopicSubscription("HelloAgents")]
  15. public class HelloAgent(
  16. IAgentRuntime context,
  17. IHostApplicationLifetime hostApplicationLifetime,
  18. [FromKeyedServices("EventTypes")] EventTypes typeRegistry) : AgentBase(
  19. context,
  20. typeRegistry),
  21. IHandleConsole,
  22. IHandle<NewMessageReceived>,
  23. IHandle<ConversationClosed>,
  24. IHandle<Shutdown>
  25. {
  26. private AgentState? State { get; set; }
  27. public async Task Handle(NewMessageReceived item)
  28. {
  29. var response = await SayHello(item.Message).ConfigureAwait(false);
  30. var evt = new Output
  31. {
  32. Message = response
  33. };
  34. Dictionary<string, string> state = new()
  35. {
  36. { "data", "We said hello to " + item.Message },
  37. { "workflow", "Active" }
  38. };
  39. await StoreAsync(new AgentState
  40. {
  41. AgentId = this.AgentId,
  42. TextData = JsonSerializer.Serialize(state)
  43. }).ConfigureAwait(false);
  44. await PublishMessageAsync(evt).ConfigureAwait(false);
  45. var goodbye = new ConversationClosed
  46. {
  47. UserId = this.AgentId.Key,
  48. UserMessage = "Goodbye"
  49. };
  50. await PublishMessageAsync(goodbye).ConfigureAwait(false);
  51. // send the shutdown message
  52. await PublishMessageAsync(new Shutdown { Message = this.AgentId.Key }).ConfigureAwait(false);
  53. }
  54. public async Task Handle(ConversationClosed item)
  55. {
  56. State = await ReadAsync<AgentState>(this.AgentId).ConfigureAwait(false);
  57. var state = JsonSerializer.Deserialize<Dictionary<string, string>>(State.TextData) ?? new Dictionary<string, string> { { "data", "No state data found" } };
  58. var goodbye = $"\nState: {state}\n********************* {item.UserId} said {item.UserMessage} ************************";
  59. var evt = new Output
  60. {
  61. Message = goodbye
  62. };
  63. await PublishMessageAsync(evt).ConfigureAwait(true);
  64. state["workflow"] = "Complete";
  65. await StoreAsync(new AgentState
  66. {
  67. AgentId = this.AgentId,
  68. TextData = JsonSerializer.Serialize(state)
  69. }).ConfigureAwait(false);
  70. }
  71. public async Task Handle(Shutdown item)
  72. {
  73. string? workflow = null;
  74. // make sure the workflow is finished
  75. while (workflow != "Complete")
  76. {
  77. State = await ReadAsync<AgentState>(this.AgentId).ConfigureAwait(true);
  78. var state = JsonSerializer.Deserialize<Dictionary<string, string>>(State?.TextData ?? "{}") ?? new Dictionary<string, string>();
  79. workflow = state["workflow"];
  80. await Task.Delay(1000).ConfigureAwait(true);
  81. }
  82. // now we can shut down...
  83. hostApplicationLifetime.StopApplication();
  84. }
  85. public async Task<string> SayHello(string ask)
  86. {
  87. var response = $"\n\n\n\n***************Hello {ask}**********************\n\n\n\n";
  88. return response;
  89. }
  90. }
  91. }