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
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 app = await AgentsApp.PublishMessageAsync("HelloAgents", new NewMessageReceived
  9. {
  10. Message = "World"
  11. }, local: false);
  12. await app.WaitForShutdownAsync();
  13. namespace Hello
  14. {
  15. [TopicSubscription("agents")]
  16. public class HelloAgent(
  17. IAgentWorker worker,
  18. IHostApplicationLifetime hostApplicationLifetime,
  19. [FromKeyedServices("EventTypes")] EventTypes typeRegistry) : Agent(
  20. worker,
  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)
  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)
  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)
  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. }