From c5d6059e5269b4f4b4f9e06d180102712fbac1d5 Mon Sep 17 00:00:00 2001 From: Patrick Ruddiman <86851465+PatrickRuddiman@users.noreply.github.com> Date: Sun, 20 Jul 2025 15:31:17 +0000 Subject: [PATCH] Format Files --- Program.cs | 35 ++++++++++++++--------- Services/ConfigurationService.cs | 21 ++++++++++---- Services/GitService.cs | 9 ++++-- Services/OpenAIService.cs | 49 +++++++++++++++++++++++++------- 4 files changed, 83 insertions(+), 31 deletions(-) diff --git a/Program.cs b/Program.cs index 7132734..ce7a363 100644 --- a/Program.cs +++ b/Program.cs @@ -20,7 +20,11 @@ static async Task Main(string[] args) "Temperature setting for AI model (0-2)" ); var topPOption = new Option("--topp", () => 1, "Top-p setting for AI model (0-1)"); - var presenceOption = new Option("--presence", () => 0, "Presence penalty for AI model"); + var presenceOption = new Option( + "--presence", + () => 0, + "Presence penalty for AI model" + ); var frequencyOption = new Option( "--frequency", () => 0, @@ -31,12 +35,11 @@ static async Task Main(string[] args) description: "AI model to use (overrides setup)" ); - var setupOption = new Option( - "--setup", - "Configure OpenAI or Azure OpenAI settings" - ); + var setupOption = new Option("--setup", "Configure OpenAI or Azure OpenAI settings"); - var rootCommand = new RootCommand("Generate AI-powered commit messages using OpenAI or Azure OpenAI") + var rootCommand = new RootCommand( + "Generate AI-powered commit messages using OpenAI or Azure OpenAI" + ) { dryRunOption, verboseOption, @@ -120,8 +123,8 @@ static async Task GenerateCommitMessage( if (string.IsNullOrEmpty(apiKey)) { throw new InvalidOperationException( - "OpenAI API key not found. Please run 'WriteCommit --setup' to configure your API key " + - "or set the OPENAI_API_KEY environment variable." + "OpenAI API key not found. Please run 'WriteCommit --setup' to configure your API key " + + "or set the OPENAI_API_KEY environment variable." ); } @@ -144,11 +147,13 @@ static async Task GenerateCommitMessage( var stagedChanges = await gitService.GetStagedChangesAsync(verbose); // If the diff is very small, grab a few extra lines of context - var fileCount = System.Text.RegularExpressions.Regex.Matches( - stagedChanges, - "^diff --git", - System.Text.RegularExpressions.RegexOptions.Multiline - ).Count; + var fileCount = System + .Text.RegularExpressions.Regex.Matches( + stagedChanges, + "^diff --git", + System.Text.RegularExpressions.RegexOptions.Multiline + ) + .Count; var lineCount = stagedChanges.Split('\n').Length; if ( @@ -250,7 +255,9 @@ static async Task RunSetupAsync(bool verbose) { Console.WriteLine(); Console.WriteLine("❌ Setup failed."); - Console.WriteLine("You can try again or set the OPENAI_API_KEY environment variable manually."); + Console.WriteLine( + "You can try again or set the OPENAI_API_KEY environment variable manually." + ); } } } diff --git a/Services/ConfigurationService.cs b/Services/ConfigurationService.cs index 53f263b..d3ab0c8 100644 --- a/Services/ConfigurationService.cs +++ b/Services/ConfigurationService.cs @@ -1,7 +1,7 @@ +using System.ClientModel; using System.Text.Json; -using OpenAI.Chat; using Azure.AI.OpenAI; -using System.ClientModel; +using OpenAI.Chat; using WriteCommit.Constants; using WriteCommit.Models; @@ -114,7 +114,9 @@ public async Task SetupApiKeyAsync(bool verbose = false) Console.WriteLine("================="); Console.WriteLine(); Console.WriteLine("Please enter your OpenAI API key (or Azure OpenAI key)."); - Console.WriteLine("You can get one from: https://platform.openai.com/api-keys or your Azure portal"); + Console.WriteLine( + "You can get one from: https://platform.openai.com/api-keys or your Azure portal" + ); Console.WriteLine(); Console.Write("API Key (leave blank if not required): "); @@ -196,7 +198,13 @@ public async Task SetupApiKeyAsync(bool verbose = false) /// /// Tests if the API key is valid by making a simple request /// - private async Task TestApiKeyAsync(string? apiKey, bool useAzure, string endpoint, string model, bool verbose) + private async Task TestApiKeyAsync( + string? apiKey, + bool useAzure, + string endpoint, + string model, + bool verbose + ) { Console.WriteLine("Testing API key..."); @@ -205,7 +213,10 @@ private async Task TestApiKeyAsync(string? apiKey, bool useAzure, string e ChatClient testClient; if (useAzure) { - var azureClient = new Azure.AI.OpenAI.AzureOpenAIClient(new Uri(endpoint), new ApiKeyCredential(apiKey)); + var azureClient = new Azure.AI.OpenAI.AzureOpenAIClient( + new Uri(endpoint), + new ApiKeyCredential(apiKey) + ); testClient = azureClient.GetChatClient(model); } else diff --git a/Services/GitService.cs b/Services/GitService.cs index 3b134bd..9f8a613 100644 --- a/Services/GitService.cs +++ b/Services/GitService.cs @@ -28,13 +28,18 @@ public async Task GetStagedChangesAsync(bool verbose = false) return result.Output; } - public async Task GetStagedChangesWithContextAsync(int contextLines, bool verbose = false) + public async Task GetStagedChangesWithContextAsync( + int contextLines, + bool verbose = false + ) { var args = $"--no-pager diff --staged --unified={contextLines}"; var result = await RunCommandAsync("git", args, verbose); if (result.ExitCode != 0) { - throw new InvalidOperationException($"Failed to get staged changes with context: {result.Error}"); + throw new InvalidOperationException( + $"Failed to get staged changes with context: {result.Error}" + ); } return result.Output; } diff --git a/Services/OpenAIService.cs b/Services/OpenAIService.cs index 10b5d2c..0e00a52 100644 --- a/Services/OpenAIService.cs +++ b/Services/OpenAIService.cs @@ -1,7 +1,7 @@ -using System.Text; -using OpenAI.Chat; using System.ClientModel; +using System.Text; using Azure.AI.OpenAI; +using OpenAI.Chat; using WriteCommit.Constants; using WriteCommit.Models; @@ -23,9 +23,7 @@ public OpenAIService(string apiKey, string? endpoint = null, bool useAzure = fal } _apiKey = apiKey; - _endpoint = string.IsNullOrWhiteSpace(endpoint) - ? "https://api.openai.com/v1" - : endpoint; + _endpoint = string.IsNullOrWhiteSpace(endpoint) ? "https://api.openai.com/v1" : endpoint; _useAzure = useAzure; _patternsDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "patterns"); } @@ -34,7 +32,10 @@ private ChatClient CreateChatClient(string model) { if (_useAzure) { - var azureClient = new Azure.AI.OpenAI.AzureOpenAIClient(new Uri(_endpoint), new ApiKeyCredential(_apiKey)); + var azureClient = new Azure.AI.OpenAI.AzureOpenAIClient( + new Uri(_endpoint), + new ApiKeyCredential(_apiKey) + ); return azureClient.GetChatClient(model); } @@ -249,7 +250,8 @@ bool verbose } var combinedContent = string.Join("\n\n", chunkMessages); - var estimatedTokens = EstimateTokenCount(systemPrompt) + EstimateTokenCount(combinedContent); + var estimatedTokens = + EstimateTokenCount(systemPrompt) + EstimateTokenCount(combinedContent); if (estimatedTokens > MaxContextTokens && chunkMessages.Count > 1) { @@ -267,7 +269,16 @@ bool verbose var msgTokens = EstimateTokenCount(msg); if (currentTokens + msgTokens > MaxContextTokens / 2 && currentGroup.Count > 0) { - var summary = await CombineChunkMessagesAsync(currentGroup, pattern, temperature, topP, presence, frequency, model, verbose); + var summary = await CombineChunkMessagesAsync( + currentGroup, + pattern, + temperature, + topP, + presence, + frequency, + model, + verbose + ); groupedSummaries.Add(summary); currentGroup.Clear(); currentTokens = EstimateTokenCount(systemPrompt); @@ -279,11 +290,29 @@ bool verbose if (currentGroup.Count > 0) { - var summary = await CombineChunkMessagesAsync(currentGroup, pattern, temperature, topP, presence, frequency, model, verbose); + var summary = await CombineChunkMessagesAsync( + currentGroup, + pattern, + temperature, + topP, + presence, + frequency, + model, + verbose + ); groupedSummaries.Add(summary); } - return await CombineChunkMessagesAsync(groupedSummaries, pattern, temperature, topP, presence, frequency, model, verbose); + return await CombineChunkMessagesAsync( + groupedSummaries, + pattern, + temperature, + topP, + presence, + frequency, + model, + verbose + ); } // Create a client for this specific model