2024-10-30 11:53:37 -07:00
|
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
// Program.cs
|
2025-01-28 17:13:36 -05:00
|
|
|
|
2025-02-12 18:37:42 -05:00
|
|
|
using Microsoft.AutoGen.Agents;
|
2024-12-12 19:43:26 -08:00
|
|
|
using Microsoft.AutoGen.Contracts;
|
2024-12-13 11:55:43 -08:00
|
|
|
using Microsoft.AutoGen.Core;
|
2025-02-13 16:43:57 -08:00
|
|
|
using Microsoft.AutoGen.Core.Grpc;
|
2025-01-28 17:13:36 -05:00
|
|
|
using Samples;
|
2025-02-14 10:27:29 -05:00
|
|
|
|
|
|
|
string? hostAddress = null;
|
|
|
|
bool in_host_address = false;
|
|
|
|
bool sendHello = true;
|
|
|
|
foreach (string arg in args)
|
|
|
|
{
|
|
|
|
switch (arg)
|
|
|
|
{
|
|
|
|
case "--host":
|
|
|
|
in_host_address = true;
|
|
|
|
break;
|
|
|
|
case "--nosend":
|
|
|
|
sendHello = false;
|
|
|
|
break;
|
|
|
|
case "-h":
|
|
|
|
case "--help":
|
|
|
|
PrintHelp();
|
|
|
|
Environment.Exit(0);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (in_host_address)
|
|
|
|
{
|
|
|
|
hostAddress = arg;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hostAddress ??= Environment.GetEnvironmentVariable("AGENT_HOST");
|
2025-02-13 16:43:57 -08:00
|
|
|
var appBuilder = new AgentsAppBuilder(); // Create app builder
|
|
|
|
// if we are using distributed, we need the AGENT_HOST var defined and then we will use the grpc runtime
|
2025-02-14 10:27:29 -05:00
|
|
|
|
|
|
|
bool usingGrpc = false;
|
|
|
|
if (hostAddress is string agentHost)
|
2025-02-13 16:43:57 -08:00
|
|
|
{
|
2025-02-14 10:27:29 -05:00
|
|
|
usingGrpc = true;
|
|
|
|
Console.WriteLine($"connecting to {agentHost}");
|
2025-02-13 16:43:57 -08:00
|
|
|
appBuilder.AddGrpcAgentWorker(agentHost)
|
|
|
|
.AddAgent<HelloAgent>("HelloAgent");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Set up app builder for in-process runtime, allow message delivery to self, and add the Hello agent
|
|
|
|
appBuilder.UseInProcessRuntime(deliverToSelf: true).AddAgent<HelloAgent>("HelloAgent");
|
|
|
|
}
|
2025-01-28 17:13:36 -05:00
|
|
|
var app = await appBuilder.BuildAsync(); // Build the app
|
2025-02-14 10:27:29 -05:00
|
|
|
await app.StartAsync();
|
2025-01-28 17:13:36 -05:00
|
|
|
// Create a custom message type from proto and define message
|
2025-02-14 10:27:29 -05:00
|
|
|
|
|
|
|
if (sendHello)
|
|
|
|
{
|
|
|
|
var message = new NewMessageReceived { Message = "Hello World!" };
|
|
|
|
await app.PublishMessageAsync(message, new TopicId("HelloTopic")).ConfigureAwait(false); // Publish custom message (handler has been set in HelloAgent)
|
|
|
|
}
|
|
|
|
else if (!usingGrpc)
|
|
|
|
{
|
|
|
|
Console.Write("Warning: Using --nosend with the InProcessRuntime will hang. Terminating.");
|
|
|
|
Environment.Exit(-1);
|
|
|
|
}
|
|
|
|
|
2025-02-13 16:43:57 -08:00
|
|
|
await app.WaitForShutdownAsync().ConfigureAwait(false); // Wait for shutdown from agent
|
2025-02-14 10:27:29 -05:00
|
|
|
|
|
|
|
static void PrintHelp()
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
HelloAgent [--host <hostAddress>] [--nosend]
|
|
|
|
--host Use gRPC gateway at <hostAddress>; this can also be set using the AGENT_HOST Environment Variable
|
|
|
|
--nosend Do not send the starting message. Note: This means HelloAgent will wait until some other agent will send
|
|
|
|
that message. This will not work when using the InProcessRuntime.
|
|
|
|
*/
|
|
|
|
Console.WriteLine("HelloAgent [--host <hostAddress>] [--nosend]");
|
|
|
|
Console.WriteLine(" --host \tUse gRPC gateway at <hostAddress>; this can also be set using the AGENT_HOST Environment Variable");
|
|
|
|
Console.WriteLine(" --nosend \tDo not send the starting message. Note: This means HelloAgent will wait until some other agent will send");
|
|
|
|
}
|