94 lines
2.8 KiB
C#
94 lines
2.8 KiB
C#
using MAF1.Agents.FileCity;
|
|||
|
|
using MAF1.Agents.Weather;
|
||
|
|
using MAF1.Orchestration;
|
||
|
|
using MAF1.Tools;
|
||
|
|
using MAF1.Utils;
|
||
|
|
using MAF1.Workflows;
|
||
|
|
using Microsoft.Agents.AI;
|
||
|
|
using Microsoft.Agents.AI.Workflows;
|
||
|
|
using Microsoft.Extensions.Configuration;
|
||
|
|
|
||
|
|
namespace MAF1;
|
||
|
|
|
||
|
|
internal static class CliHost
|
||
|
|
{
|
||
|
|
public static async Task RunAsync(string[] args)
|
||
|
|
{
|
||
|
|
IConfiguration config = new ConfigurationBuilder()
|
||
|
|
.SetBasePath(AppContext.BaseDirectory)
|
||
|
|
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: false)
|
||
|
|
.AddEnvironmentVariables()
|
||
|
|
.Build();
|
||
|
|
|
||
|
|
AgentFactory factory = new(AgentFactory.Load(config));
|
||
|
|
WeatherOptions weatherOptions = config.GetSection("Weather").Get<WeatherOptions>() ?? new WeatherOptions();
|
||
|
|
using HttpClient http = WeatherTools.CreateHttpClient();
|
||
|
|
WeatherTools weatherTools = new(weatherOptions, http);
|
||
|
|
AIAgent fileCityAgent = FileCityAgent.Create(factory);
|
||
|
|
AIAgent weatherAgent = WeatherAgent.Create(factory, weatherTools);
|
||
|
|
|
||
|
|
if (!TryParseArgs(args, out string mode, out string filePath))
|
||
|
|
{
|
||
|
|
PrintUsage();
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
Console.WriteLine($"天气数据源: {weatherOptions.Provider}");
|
||
|
|
Console.WriteLine($"目标文件: {filePath}");
|
||
|
|
Console.WriteLine();
|
||
|
|
|
||
|
|
Workflow workflow = mode == "edge"
|
||
|
|
? CityWeatherWorkflow.BuildEdgeCondition(fileCityAgent, weatherAgent)
|
||
|
|
: CityWeatherWorkflow.BuildNodeCondition(fileCityAgent, weatherAgent);
|
||
|
|
string label = mode == "edge"
|
||
|
|
? "模式 edge:判断写在边上"
|
||
|
|
: "模式 node:判断写在节点里";
|
||
|
|
await WorkflowOrchestration.RunAsync(workflow, label, filePath);
|
||
|
|
}
|
||
|
|
|
||
|
|
private static bool TryParseArgs(string[] args, out string mode, out string filePath)
|
||
|
|
{
|
||
|
|
mode = "node";
|
||
|
|
filePath = Path.Combine("Data", "cities.txt");
|
||
|
|
IEnumerable<string> rest = args[0] is "--cli" ? args.Skip(1) : args;
|
||
|
|
string[] list = rest.ToArray();
|
||
|
|
if (list.Length == 0)
|
||
|
|
{
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
string first = list[0];
|
||
|
|
if (first is "node" or "edge")
|
||
|
|
{
|
||
|
|
mode = first;
|
||
|
|
if (list.Length > 1)
|
||
|
|
{
|
||
|
|
filePath = list[1];
|
||
|
|
}
|
||
|
|
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (first is "-h" or "--help")
|
||
|
|
{
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
filePath = first;
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
private static void PrintUsage()
|
||
|
|
{
|
||
|
|
Console.WriteLine(
|
||
|
|
"""
|
||
|
|
可视化编排(默认):
|
||
|
|
dotnet run
|
||
|
|
|
||
|
|
命令行工作流对比:
|
||
|
|
dotnet run -- node Data/cities.txt
|
||
|
|
dotnet run -- edge Data/cities.txt
|
||
|
|
""");
|
||
|
|
}
|
||
|
|
}
|