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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. // Program.cs
  3. using Microsoft.AutoGen.Agents;
  4. using Microsoft.AutoGen.Contracts;
  5. using Microsoft.AutoGen.Core;
  6. using Microsoft.AutoGen.Core.Grpc;
  7. using Samples;
  8. string? hostAddress = null;
  9. bool in_host_address = false;
  10. bool sendHello = true;
  11. foreach (string arg in args)
  12. {
  13. switch (arg)
  14. {
  15. case "--host":
  16. in_host_address = true;
  17. break;
  18. case "--nosend":
  19. sendHello = false;
  20. break;
  21. case "-h":
  22. case "--help":
  23. PrintHelp();
  24. Environment.Exit(0);
  25. break;
  26. default:
  27. if (in_host_address)
  28. {
  29. hostAddress = arg;
  30. }
  31. break;
  32. }
  33. }
  34. hostAddress ??= Environment.GetEnvironmentVariable("AGENT_HOST");
  35. var appBuilder = new AgentsAppBuilder(); // Create app builder
  36. // if we are using distributed, we need the AGENT_HOST var defined and then we will use the grpc runtime
  37. bool usingGrpc = false;
  38. if (hostAddress is string agentHost)
  39. {
  40. usingGrpc = true;
  41. Console.WriteLine($"connecting to {agentHost}");
  42. appBuilder.AddGrpcAgentWorker(agentHost)
  43. .AddAgent<HelloAgent>("HelloAgent");
  44. }
  45. else
  46. {
  47. // Set up app builder for in-process runtime, allow message delivery to self, and add the Hello agent
  48. appBuilder.UseInProcessRuntime(deliverToSelf: true).AddAgent<HelloAgent>("HelloAgent");
  49. }
  50. var app = await appBuilder.BuildAsync(); // Build the app
  51. await app.StartAsync();
  52. // Create a custom message type from proto and define message
  53. if (sendHello)
  54. {
  55. var message = new NewMessageReceived { Message = "Hello World!" };
  56. await app.PublishMessageAsync(message, new TopicId("HelloTopic")).ConfigureAwait(false); // Publish custom message (handler has been set in HelloAgent)
  57. }
  58. else if (!usingGrpc)
  59. {
  60. Console.Write("Warning: Using --nosend with the InProcessRuntime will hang. Terminating.");
  61. Environment.Exit(-1);
  62. }
  63. await app.WaitForShutdownAsync().ConfigureAwait(false); // Wait for shutdown from agent
  64. static void PrintHelp()
  65. {
  66. /*
  67. HelloAgent [--host <hostAddress>] [--nosend]
  68. --host Use gRPC gateway at <hostAddress>; this can also be set using the AGENT_HOST Environment Variable
  69. --nosend Do not send the starting message. Note: This means HelloAgent will wait until some other agent will send
  70. that message. This will not work when using the InProcessRuntime.
  71. */
  72. Console.WriteLine("HelloAgent [--host <hostAddress>] [--nosend]");
  73. Console.WriteLine(" --host \tUse gRPC gateway at <hostAddress>; this can also be set using the AGENT_HOST Environment Variable");
  74. Console.WriteLine(" --nosend \tDo not send the starting message. Note: This means HelloAgent will wait until some other agent will send");
  75. }