-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
263 lines (235 loc) · 8.35 KB
/
Program.cs
File metadata and controls
263 lines (235 loc) · 8.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
using System.CommandLine;
using Microsoft.Extensions.Logging;
using WriteCommit.Constants;
using WriteCommit.Services;
namespace WriteCommit;
class Program
{
static async Task<int> Main(string[] args)
{
var dryRunOption = new Option<bool>(
"--dry-run",
"Generate commit message without committing"
);
var verboseOption = new Option<bool>("--verbose", "Show detailed output");
var temperatureOption = new Option<int>(
"--temperature",
() => 1,
"Temperature setting for AI model (0-2)"
);
var topPOption = new Option<int>("--topp", () => 1, "Top-p setting for AI model (0-1)");
var presenceOption = new Option<int>(
"--presence",
() => 0,
"Presence penalty for AI model"
);
var frequencyOption = new Option<int>(
"--frequency",
() => 0,
"Frequency penalty for AI model"
);
var modelOption = new Option<string?>(
"--model",
description: "AI model to use (overrides setup)"
);
var setupOption = new Option<bool>("--setup", "Configure OpenAI or Azure OpenAI settings");
var rootCommand = new RootCommand(
"Generate AI-powered commit messages using OpenAI or Azure OpenAI"
)
{
dryRunOption,
verboseOption,
temperatureOption,
topPOption,
presenceOption,
frequencyOption,
modelOption,
setupOption,
};
rootCommand.SetHandler(
async (
bool dryRun,
bool verbose,
int temperature,
int topP,
int presence,
int frequency,
string? model
) =>
{
try
{
// Check if setup mode is requested
bool setupMode = args.Contains("--setup");
if (setupMode)
{
await RunSetupAsync(verbose);
return;
}
// Ensure patterns are installed before proceeding
var patternService = new PatternService(verbose);
await patternService.EnsurePatternsInstalledAsync();
await GenerateCommitMessage(
dryRun,
verbose,
temperature,
topP,
presence,
frequency,
model
);
}
catch (Exception ex)
{
Console.Error.WriteLine($"Error: {ex.Message}");
Environment.Exit(1);
}
},
dryRunOption,
verboseOption,
temperatureOption,
topPOption,
presenceOption,
frequencyOption,
modelOption
);
return await rootCommand.InvokeAsync(args);
}
static async Task GenerateCommitMessage(
bool dryRun,
bool verbose,
int temperature,
int topP,
int presence,
int frequency,
string? model
)
{
var gitService = new GitService();
// Get OpenAI API key
var configService = new ConfigurationService();
var apiKey = await configService.GetOpenAiApiKeyAsync();
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."
);
}
// Create OpenAI service with the API key
var endpoint = await configService.GetOpenAiEndpointAsync() ?? "https://api.openai.com/v1";
var defaultModel = await configService.GetDefaultModelAsync() ?? "gpt-4o-mini";
var useAzure = await configService.UseAzureOpenAIAsync();
var openAiService = new OpenAIService(apiKey, endpoint, useAzure);
// Check if we're in a git repository
if (!Directory.Exists(".git") && !await gitService.IsInGitRepositoryAsync())
{
throw new InvalidOperationException(
"Not in a git repository. Please run this command from within a git repository."
);
}
// Get staged changes
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 lineCount = stagedChanges.Split('\n').Length;
if (
fileCount <= DiffContextDefaults.SmallDiffFileThreshold
&& lineCount < DiffContextDefaults.SmallDiffLineThreshold
)
{
if (verbose)
{
Console.WriteLine("Small diff detected, gathering additional context...");
}
stagedChanges = await gitService.GetStagedChangesWithContextAsync(
DiffContextDefaults.ExtraContextLines,
verbose
);
}
if (string.IsNullOrWhiteSpace(stagedChanges))
{
Console.WriteLine(
"No staged changes found. Please stage your changes first using 'git add'."
);
return;
}
if (verbose)
{
Console.WriteLine(
"Staged changes detected. Analyzing and generating commit message..."
);
}
// Initialize semantic analyzer
using var loggerFactory = LoggerFactory.Create(builder =>
builder.AddConsole().SetMinimumLevel(verbose ? LogLevel.Information : LogLevel.Warning)
);
var logger = loggerFactory.CreateLogger<SemanticCoherenceAnalyzer>();
var analyzer = new SemanticCoherenceAnalyzer(logger);
// Chunk the diff if it's large
var chunks = analyzer.ChunkDiff(stagedChanges, verbose);
if (chunks.Count > 1 && verbose)
{
Console.WriteLine(
$"Large diff detected. Split into {chunks.Count} semantic chunks for processing."
);
}
// Generate commit message using OpenAI with chunking support
var finalModel = string.IsNullOrWhiteSpace(model) ? defaultModel : model;
var commitMessage = await openAiService.GenerateCommitMessageAsync(
chunks,
PatternNames.CommitPattern,
temperature,
topP,
presence,
frequency,
finalModel,
verbose
);
if (string.IsNullOrWhiteSpace(commitMessage))
{
throw new InvalidOperationException(
"Failed to generate commit message. Please ensure your OpenAI API key is valid."
);
}
// Display the generated commit message
Console.WriteLine("Generated commit message:");
Console.WriteLine(commitMessage);
Console.WriteLine();
if (dryRun)
{
Console.WriteLine("Dry run mode - not committing changes.");
return;
}
// Commit the changes
await gitService.CommitChangesAsync(commitMessage, verbose);
}
/// <summary>
/// Runs the setup process to configure the OpenAI API key
/// </summary>
static async Task RunSetupAsync(bool verbose)
{
var configService = new ConfigurationService();
bool success = await configService.SetupApiKeyAsync(verbose);
if (success)
{
Console.WriteLine();
Console.WriteLine("✅ Setup completed successfully.");
Console.WriteLine("You can now use WriteCommit to generate commit messages.");
}
else
{
Console.WriteLine();
Console.WriteLine("❌ Setup failed.");
Console.WriteLine(
"You can try again or set the OPENAI_API_KEY environment variable manually."
);
}
}
}