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

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