autogen/cli/Program.cs

93 lines
3.9 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");
pmReadmeCommand.SetHandler(async (file) => await CallFunction(nameof(PM), PM.Readme , file.FullName), fileOption);
var pmBootstrapCommand = new Command("bootstrap", "Bootstrap a project for a given input");
pmBootstrapCommand.SetHandler(async (file) => await CallFunction(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");
devleadPlanCommand.SetHandler(async (file) => await CallFunction(nameof(DevLead), DevLead.Plan, file.FullName), fileOption);
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");
devPlanCommand.SetHandler(async (file) => await CallFunction(nameof(Developer), Developer.Implement, file.FullName), fileOption);
devCommand.AddCommand(devPlanCommand);
rootCommand.AddCommand(pmCommand);
rootCommand.AddCommand(devleadCommand);
rootCommand.AddCommand(devCommand);
await rootCommand.InvokeAsync(args);
}
public static async Task CallFunction(string skillName, string functionName, string file)
{
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-01 10:58:29 -07:00
return;
}
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}");
return;
}
var responseJson = await response.Content.ReadAsStringAsync();
//if we have a successful response, we can deserialize the response body and write it to console
var responseBody = JsonSerializer.Deserialize<object>(responseJson);
// response body is a dictionary of key/value pairs and responseBody.response is not null, write it to console
if (responseBody is not null && responseBody is JsonElement jsonElement && jsonElement.TryGetProperty("response", out var responseValue))
{
Console.WriteLine(responseValue);
}
else
{
Console.WriteLine(responseJson);
}
return;
}
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"; }