Skip to content

Commit

Permalink
Merge pull request #34 from andresharpe/fix/cute-chat-bugs-01
Browse files Browse the repository at this point in the history
Fixed a number of small bugs (chat)
  • Loading branch information
andresharpe authored Sep 29, 2024
2 parents bce55fc + 80e4b8d commit 3961f7f
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 16 deletions.
7 changes: 7 additions & 0 deletions source/Cute.Lib/Contentful/GraphQL/AutoGraphQlQueryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,17 @@ private static void ExtractScribanVariables(Template template, List<string[]> va
TraverseScriptNode(template.Page, variableList);
}

private static string[] functionPrefixes = [
"cute", "array", "date", "html", "math", "object",
"regex", "string", "timespan", "json", "collection"
];

private static void TraverseScriptNode(ScriptNode node, List<string[]> variableList)
{
if (node is ScriptMemberExpression globalVariable)
{
if (functionPrefixes.Contains(globalVariable.Target.ToString())) return;

variableList.Add(globalVariable.ToString().Split('.'));
return;
}
Expand Down
51 changes: 37 additions & 14 deletions source/Cute/Commands/Chat/ChatCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,14 +260,17 @@ private static bool UserWantsToLeave(string input)
{
return input.Equals("exit", StringComparison.OrdinalIgnoreCase)
|| input.StartsWith("bye", StringComparison.OrdinalIgnoreCase)
|| input.StartsWith("goodbye", StringComparison.OrdinalIgnoreCase)
|| input.StartsWith("quit", StringComparison.OrdinalIgnoreCase);
}

private async Task HandleExecution(IEnumerable<Locale> locales, BotResponse botResponse)
{
_console.WriteBlankLine();
_console.WriteRuler("Solution");
_console.WriteBlankLine();
_console.WriteAlertAccent(botResponse.QueryOrCommand);
_console.WriteBlankLine();
_console.WriteRuler();
_console.WriteBlankLine();

Expand Down Expand Up @@ -325,13 +328,24 @@ await AnsiConsole.Status()

private async Task RunSelectedCommand(BotResponse botResponse)
{
var parameters = botResponse.QueryOrCommand.Split(' ').ToList();
if (parameters[0] == "cute") parameters.RemoveAt(0);
parameters.Add("--no-banner");
var args = parameters.ToArray();
var command = new CommandAppBuilder(args).Build();
await command.RunAsync(args);
_console.WriteBlankLine();
try
{
var splitter = System.CommandLine.Parsing.CommandLineStringSplitter.Instance;
var parameters = splitter.Split(botResponse.QueryOrCommand).ToList();
if (parameters[0] == "cute") parameters.RemoveAt(0);
parameters.Add("--no-banner");
var args = parameters.ToArray();
var command = new CommandAppBuilder(args).Build();
await command.RunAsync(args);
_console.WriteBlankLine();
}
catch (Exception ex)
{
_console.WriteBlankLine();
_console.WriteAlert($"Oops! Something went wrong executing the query...");
_console.WriteAlert(ex.Message);
_console.WriteBlankLine();
}
}

private static ChatCompletionOptions CreateChatCompletionOptions(Settings settings)
Expand Down Expand Up @@ -408,7 +422,6 @@ Do not use "contentful" in the query structure.
# THE CUTE CLI

When asked about CUTE CLI command usage, list ALL command options including common options.
Also explain why the CLI is great for the specific command usage when responding.
The quoted text contains cute cli commands, options and usage:

"""
Expand All @@ -420,12 +433,16 @@ All Scriban expressions must be enclosed in double curly braces.
Variables are always preceded with the contentType and followed by a field name.
If a fiels is a Link to another entry then the child entry fields are valid to use.

When generating CLI commands with parameters, the use of single quotes (') to delimit a string is NOT supported.
Always use doube quotes (") to delimit strings for commands and escape any double quotes with a slash (\")
Never escape double quotes unless they are inside unescaped quotes on the command line
Regex expressions are currently NOT supported in edit or find or replace expressions. Don't suggest them
"""";
}

private static string BuildContentTypesPromptInfo(IEnumerable<ContentType> contentTypes)
{
string[] excludePrefix = ["ux", "ui", "cute", "test", "meta"];
string[] excludePrefix = ["ux", "ui", "cute", "meta"];

var sbContentTypesInfo = new StringBuilder();
foreach (var contentType in contentTypes.OrderBy(ct => ct.Id()))
Expand All @@ -434,7 +451,7 @@ private static string BuildContentTypesPromptInfo(IEnumerable<ContentType> conte

sbContentTypesInfo.AppendLine();

sbContentTypesInfo.AppendLine($"Content type: {contentType.Name}");
sbContentTypesInfo.AppendLine($"CONTENT TYPE: {contentType.Name}");
foreach (var field in contentType.Fields)
{
sbContentTypesInfo.Append($" - {field.Id} ({field.Type})");
Expand Down Expand Up @@ -533,15 +550,19 @@ internal static string GetCliDocs()
var rootCommand = CliCommandInfoXmlParser.FromXml(xml,
"The cute cli is a command line interface for interacting with Contentful.");

ConvertToDocs(rootCommand, sb);
var nameStack = new Stack<string>();

ConvertToDocs(rootCommand, sb, nameStack);

return sb.ToString();
}

private static void ConvertToDocs(CliCommandInfo commandInfo, StringBuilder sb)
private static void ConvertToDocs(CliCommandInfo commandInfo, StringBuilder sb, Stack<string> nameStack)
{
nameStack.Push(commandInfo.Name == "" ? Globals.AppName : commandInfo.Name);

sb.AppendLine();
sb.AppendLine($"COMMAND: {Globals.AppName} {commandInfo.Name}");
sb.AppendLine($"COMMAND: {string.Join(' ', nameStack.Reverse())}");
sb.AppendLine($" - DESCRIPTION: {commandInfo.Description}");

var allOptions = commandInfo.GetAllOptions();
Expand All @@ -565,8 +586,10 @@ private static void ConvertToDocs(CliCommandInfo commandInfo, StringBuilder sb)

foreach (var subCommand in commandInfo.SubCommands)
{
ConvertToDocs(subCommand, sb);
ConvertToDocs(subCommand, sb, nameStack);
}

nameStack.Pop();
}

public BotResponse DeserializeBotResponse(string json)
Expand Down
5 changes: 3 additions & 2 deletions source/Cute/Commands/Content/ContentReplaceCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
namespace Cute.Commands.Content;

public class ContentReplaceCommand(IConsoleWriter console, ILogger<ContentReplaceCommand> logger,
AppSettings appSettings, HttpClient httpClient)
AppSettings appSettings, HttpClient httpClient, ContentfulConnection contentfulConnection)
: BaseLoggedInCommand<Settings>(console, logger, appSettings)
{
private readonly HttpClient _httpClient = httpClient;
private readonly ContentfulConnection _contentfulConnection = contentfulConnection;

public class Settings : LoggedInSettings
{
Expand Down Expand Up @@ -48,7 +49,7 @@ public class Settings : LoggedInSettings

public override ValidationResult Validate(CommandContext context, Settings settings)
{
var defaultLocale = ContentfulConnection.GetDefaultLocaleAsync().Result;
var defaultLocale = _contentfulConnection.GetDefaultLocaleAsync().Result;

var defaultLocaleCode = defaultLocale.Code;

Expand Down
1 change: 1 addition & 0 deletions source/Cute/Cute.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
<PackageReference Include="ReadLine" Version="2.0.1" />
<PackageReference Include="Spectre.Console" Version="0.49.1" />
<PackageReference Include="Spectre.Console.Cli" Version="0.49.1" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
<PackageReference Include="System.Formats.Asn1" Version="8.0.1" />
</ItemGroup>
<ItemGroup>
Expand Down

0 comments on commit 3961f7f

Please sign in to comment.