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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. // Program.cs
  3. using Microsoft.AutoGen.Abstractions;
  4. using Microsoft.AutoGen.Agents;
  5. // send a message to the agent
  6. var app = await AgentsApp.PublishMessageAsync("HelloAgents", new NewMessageReceived
  7. {
  8. Message = "World"
  9. }, local: false);
  10. await app.WaitForShutdownAsync();
  11. namespace Hello
  12. {
  13. [TopicSubscription("HelloAgents")]
  14. public class HelloAgent(
  15. IAgentContext context,
  16. [FromKeyedServices("EventTypes")] EventTypes typeRegistry) : ConsoleAgent(
  17. context,
  18. typeRegistry),
  19. ISayHello,
  20. IHandle<NewMessageReceived>,
  21. IHandle<ConversationClosed>
  22. {
  23. private AgentState? State { get; set; }
  24. public async Task Handle(NewMessageReceived item)
  25. {
  26. var response = await SayHello(item.Message).ConfigureAwait(false);
  27. var evt = new Output
  28. {
  29. Message = response
  30. }.ToCloudEvent(this.AgentId.Key);
  31. var entry = "We said hello to " + item.Message;
  32. await Store(new AgentState
  33. {
  34. AgentId = this.AgentId,
  35. TextData = entry
  36. }).ConfigureAwait(false);
  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. State = await Read<AgentState>(this.AgentId).ConfigureAwait(false);
  48. var read = State?.TextData ?? "No state data found";
  49. var goodbye = $"{read}\n********************* {item.UserId} said {item.UserMessage} ************************";
  50. var evt = new Output
  51. {
  52. Message = goodbye
  53. }.ToCloudEvent(this.AgentId.Key);
  54. await PublishEvent(evt).ConfigureAwait(false);
  55. //sleep
  56. await Task.Delay(10000).ConfigureAwait(false);
  57. await AgentsApp.ShutdownAsync().ConfigureAwait(false);
  58. }
  59. public async Task<string> SayHello(string ask)
  60. {
  61. var response = $"\n\n\n\n***************Hello {ask}**********************\n\n\n\n";
  62. return response;
  63. }
  64. }
  65. public interface ISayHello
  66. {
  67. public Task<string> SayHello(string ask);
  68. }
  69. }