autogen/cli/Program.cs

87 lines
3.7 KiB
C#
Raw Normal View History

2023-06-01 10:58:29 -07:00
using System;
2023-06-02 16:33:22 +02:00
using System.CommandLine;
2023-06-01 10:58:29 -07:00
using System.IO;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
2023-06-02 16:33:22 +02:00
var fileOption = new Option<FileInfo?>(
name: "--file",
description: "The file used for input to the skill function");
var rootCommand = new RootCommand("CLI tool for the AI Dev team");
rootCommand.AddGlobalOption(fileOption);
var pmCommand = new Command("pm", "Commands for the PM team");
var pmReadmeCommand = new Command("readme", "Produce a Readme for a given input");
2023-06-07 16:57:45 +02:00
pmReadmeCommand.SetHandler(async (file) => await CallFunction<string>(nameof(PM), PM.Readme , file.FullName), fileOption);
2023-06-02 16:33:22 +02:00
var pmBootstrapCommand = new Command("bootstrap", "Bootstrap a project for a given input");
2023-06-07 16:57:45 +02:00
pmBootstrapCommand.SetHandler(async (file) => await CallFunction<string>(nameof(PM), PM.BootstrapProject, file.FullName), fileOption);
2023-06-01 10:58:29 -07:00
2023-06-02 16:33:22 +02:00
pmCommand.AddCommand(pmReadmeCommand);
pmCommand.AddCommand(pmBootstrapCommand);
2023-06-01 10:58:29 -07:00
2023-06-02 16:33:22 +02:00
var devleadCommand = new Command("devlead", "Commands for the Dev Lead team");
var devleadPlanCommand = new Command("plan", "Plan the work for a given input");
2023-06-07 16:57:45 +02:00
devleadPlanCommand.SetHandler(async (file) => await CallFunction<DevLeadPlanResponse>(nameof(DevLead), DevLead.Plan, file.FullName), fileOption);
2023-06-02 16:33:22 +02:00
devleadCommand.AddCommand(devleadPlanCommand);
var devCommand = new Command("dev", "Commands for the Dev team");
var devPlanCommand = new Command("plan", "Implement the module for a given input");
2023-06-07 16:57:45 +02:00
devPlanCommand.SetHandler(async (file) => await CallFunction<string>(nameof(Developer), Developer.Implement, file.FullName), fileOption);
2023-06-02 16:33:22 +02:00
devCommand.AddCommand(devPlanCommand);
rootCommand.AddCommand(pmCommand);
rootCommand.AddCommand(devleadCommand);
rootCommand.AddCommand(devCommand);
await rootCommand.InvokeAsync(args);
}
2023-06-07 16:57:45 +02:00
public static async Task<T> CallFunction<T>(string skillName, string functionName, string file)
2023-06-02 16:33:22 +02:00
{
if (!File.Exists(file))
2023-06-01 10:58:29 -07:00
{
2023-06-02 16:33:22 +02:00
Console.WriteLine($"File not found: {file}");
2023-06-07 16:57:45 +02:00
return default;
2023-06-01 10:58:29 -07:00
}
var variables = new[]
{
2023-06-02 16:33:22 +02:00
new { key = "input", value = File.ReadAllText(file) }
2023-06-01 10:58:29 -07:00
};
var requestBody = new { variables };
var requestBodyJson = JsonSerializer.Serialize(requestBody);
2023-06-02 16:33:22 +02:00
Console.WriteLine($"Calling skill '{skillName}' function '{functionName}' with file '{file}'");
2023-06-01 10:58:29 -07:00
Console.WriteLine(requestBodyJson);
using var httpClient = new HttpClient();
var apiUrl = $"http://localhost:7071/api/skills/{skillName}/functions/{functionName}";
var response = await httpClient.PostAsync(apiUrl, new StringContent(requestBodyJson));
if (!response.IsSuccessStatusCode)
{
Console.WriteLine($"Error: {response.StatusCode} - {response.ReasonPhrase}");
2023-06-07 16:57:45 +02:00
return default;
2023-06-01 10:58:29 -07:00
}
var responseJson = await response.Content.ReadAsStringAsync();
2023-06-07 16:57:45 +02:00
var skillResponse = JsonSerializer.Deserialize<SkillsResponse>(responseJson);
var result = typeof(T) != typeof(string) ? JsonSerializer.Deserialize<T>(skillResponse.Response) : (T)(object)skillResponse.Response;
Console.WriteLine(responseJson);
return result;
2023-06-01 10:58:29 -07:00
}
2023-06-02 16:33:22 +02:00
}
2023-06-01 10:58:29 -07:00
2023-06-02 16:33:22 +02:00
public static class PM { public static string Readme = "Readme"; public static string BootstrapProject = "BootstrapProject"; }
public static class DevLead { public static string Plan="Plan"; }
public static class Developer { public static string Implement="Implement"; }