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

.net changes to re-enable xlang support, add subscription apis (#4159) * add subscription response * fix send subscription response * add register agent type response * adding a test * working on shaping up a test * appsettins update for backend * another appsettings * fixup aspire hosting * enable AGENT_HOST var from aspire * add SendMessageAsync * remove broken test * test compiles and runs but is not (yet) correct * subscriptions grain wireup. * temp assert true. * remove DI for SubscriptionGrain * add xlang python code * add subscription response * rebond * Update to .NET 9.0 * Fix Backend project SDK * Package updates * get RegisterAgentTypeRequest working * fix exceptions * add error handling for requests * whoops * send cloud event message type * processing cloudevents * trying tosend proto data - doesn't work * trying to pack proto_data * fix (#4238) * pack the Message from agents_events * format - not sure why these? * format * cleanup, error handling, xlang sample publishes messages that can be heard by .NET and vice versa * format * sdk version * sdk vers * net8 * back to net8 * remove netstandard2 * fix used * remove unused * more cleanup * remove unneeded package * I'm terrible at writing tests * deserialize the cloud events and sent them as events * comment * cleanup * await * Delete dotnet/samples/Hello/Backend/Backend.csproj unneeded change * whoops * merge main python back into here * revert back to local * revert some of the helloAgents changes. * [.NET] Add happy path test for in-memory agent && Simplify HelloAgent example && some clean-up in extension APIs (#4227) * add happy path test * remove unnecessary namespace * fix build error * Update AgentBaseTests.cs * revert changes --------- * fix busted merge from main * addressing review comments * make internal * case sensitive rename step 1 * case sensitive rename step 2 * remove! --------- Co-authored-by: Peter Chang <petchang@microsoft.com> Co-authored-by: Reuben Bond <reuben.bond@gmail.com> Co-authored-by: Eric Zhu <ekzhu@users.noreply.github.com> Co-authored-by: Xiaoyun Zhang <bigmiao.zhang@gmail.com>
1 year ago
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: false);
  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. }