Skip to content

Commit 12bb915

Browse files
committed
code_review: PR #596
- Add `ViewModels.Preference.PrepareOpenAIPrompt()` method to generate default prompt instead of a const fallback value. Therefore, it is more convenient for us to modify the default value in the Preference dialog. - Modify the default prompts. Rename `SubjectPrompt` to `GenerateSubjectPrompt`. Rename `SummaryPrompt` to `AnalyzeDiffPrompt`. - Rewrite the way to build OpenAI user content for subject generation Signed-off-by: leo <[email protected]>
1 parent 2f68aed commit 12bb915

File tree

5 files changed

+75
-94
lines changed

5 files changed

+75
-94
lines changed

src/Commands/GenerateCommitMessage.cs

Lines changed: 18 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,6 @@ namespace SourceGit.Commands
1010
/// </summary>
1111
public class GenerateCommitMessage
1212
{
13-
private const string DEFAULT_SUMMARY_PROMPT = """
14-
You are an expert developer specialist in creating commits.
15-
Provide a super concise one sentence overall changes summary of the user `git diff` output following strictly the next rules:
16-
- Do not use any code snippets, imports, file routes or bullets points.
17-
- Do not mention the route of file that has been change.
18-
- Simply describe the MAIN GOAL of the changes.
19-
- Output directly the summary in plain text.
20-
""";
21-
22-
private const string DEFAULT_SUBJECT_PROMPT = """
23-
You are an expert developer specialist in creating commits messages.
24-
Your only goal is to retrieve a single commit message.
25-
Based on the provided user changes, combine them in ONE SINGLE commit message retrieving the global idea, following strictly the next rules:
26-
- Assign the commit {type} according to the next conditions:
27-
feat: Only when adding a new feature.
28-
fix: When fixing a bug.
29-
docs: When updating documentation.
30-
style: When changing elements styles or design and/or making changes to the code style (formatting, missing semicolons, etc.) without changing the code logic.
31-
test: When adding or updating tests.
32-
chore: When making changes to the build process or auxiliary tools and libraries.
33-
revert: When undoing a previous commit.
34-
refactor: When restructuring code without changing its external behavior, or is any of the other refactor types.
35-
- Do not add any issues numeration, explain your output nor introduce your answer.
36-
- Output directly only one commit message in plain text with the next format: {type}: {commit_message}.
37-
- Be as concise as possible, keep the message under 50 characters.
38-
""";
39-
4013
public class GetDiffContent : Command
4114
{
4215
public GetDiffContent(string repo, Models.DiffOption opt)
@@ -59,31 +32,36 @@ public string Result()
5932
{
6033
try
6134
{
62-
var summaries = new List<string>();
35+
var summarybuilder = new StringBuilder();
36+
var bodyBuilder = new StringBuilder();
6337
foreach (var change in _changes)
6438
{
6539
if (_cancelToken.IsCancellationRequested)
6640
return "";
6741

6842
_onProgress?.Invoke($"Analyzing {change.Path}...");
43+
6944
var summary = GenerateChangeSummary(change);
70-
summaries.Add(summary);
45+
summarybuilder.Append("- ");
46+
summarybuilder.Append(summary);
47+
summarybuilder.Append("(file: ");
48+
summarybuilder.Append(change.Path);
49+
summarybuilder.Append(")");
50+
summarybuilder.AppendLine();
51+
52+
bodyBuilder.Append("- ");
53+
bodyBuilder.Append(summary);
54+
bodyBuilder.AppendLine();
7155
}
7256

7357
if (_cancelToken.IsCancellationRequested)
7458
return "";
7559

7660
_onProgress?.Invoke($"Generating commit message...");
77-
var builder = new StringBuilder();
78-
builder.Append(GenerateSubject(string.Join("", summaries)));
79-
builder.Append("\n");
80-
foreach (var summary in summaries)
81-
{
82-
builder.Append("\n- ");
83-
builder.Append(summary.Trim());
84-
}
8561

86-
return builder.ToString();
62+
var body = bodyBuilder.ToString();
63+
var subject = GenerateSubject(summarybuilder.ToString());
64+
return string.Format("{0}\n\n{1}", subject, body);
8765
}
8866
catch (Exception e)
8967
{
@@ -97,12 +75,7 @@ private string GenerateChangeSummary(Models.Change change)
9775
var rs = new GetDiffContent(_repo, new Models.DiffOption(change, false)).ReadToEnd();
9876
var diff = rs.IsSuccess ? rs.StdOut : "unknown change";
9977

100-
var prompt = string.IsNullOrWhiteSpace(Models.OpenAI.SummaryPrompt)
101-
? DEFAULT_SUMMARY_PROMPT
102-
: Models.OpenAI.SummaryPrompt;
103-
104-
var rsp = Models.OpenAI.Chat(prompt, $"Here is the `git diff` output: {diff}", _cancelToken);
105-
78+
var rsp = Models.OpenAI.Chat(Models.OpenAI.AnalyzeDiffPrompt, $"Here is the `git diff` output: {diff}", _cancelToken);
10679
if (rsp != null && rsp.Choices.Count > 0)
10780
return rsp.Choices[0].Message.Content;
10881

@@ -111,12 +84,7 @@ private string GenerateChangeSummary(Models.Change change)
11184

11285
private string GenerateSubject(string summary)
11386
{
114-
var prompt = string.IsNullOrWhiteSpace(Models.OpenAI.SubjectPrompt)
115-
? DEFAULT_SUBJECT_PROMPT
116-
: Models.OpenAI.SubjectPrompt;
117-
118-
var rsp = Models.OpenAI.Chat(prompt, $"Here are the summaries changes: {summary}", _cancelToken);
119-
87+
var rsp = Models.OpenAI.Chat(Models.OpenAI.GenerateSubjectPrompt, $"Here are the summaries changes:\n{summary}", _cancelToken);
12088
if (rsp != null && rsp.Choices.Count > 0)
12189
return rsp.Choices[0].Message.Content;
12290

src/Models/OpenAI.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,13 @@ public static string Model
9494
set;
9595
}
9696

97-
public static string SubjectPrompt
97+
public static string AnalyzeDiffPrompt
9898
{
9999
get;
100100
set;
101101
}
102102

103-
public static string SummaryPrompt
103+
public static string GenerateSubjectPrompt
104104
{
105105
get;
106106
set;

src/Resources/Locales/en_US.axaml

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -400,34 +400,11 @@
400400
<x:String x:Key="Text.Period.YearsAgo" xml:space="preserve">{0} years ago</x:String>
401401
<x:String x:Key="Text.Preference" xml:space="preserve">Preference</x:String>
402402
<x:String x:Key="Text.Preference.AI" xml:space="preserve">OPEN AI</x:String>
403-
<x:String x:Key="Text.Preference.AI.Server" xml:space="preserve">Server</x:String>
403+
<x:String x:Key="Text.Preference.AI.AnalyzeDiffPrompt" xml:space="preserve">Analyze Diff Prompt</x:String>
404404
<x:String x:Key="Text.Preference.AI.ApiKey" xml:space="preserve">API Key</x:String>
405+
<x:String x:Key="Text.Preference.AI.GenerateSubjectPrompt" xml:space="preserve">Generate Subject Prompt</x:String>
405406
<x:String x:Key="Text.Preference.AI.Model" xml:space="preserve">Model</x:String>
406-
<x:String x:Key="Text.Preference.AI.SummaryPrompt" xml:space="preserve">Summary Prompt</x:String>
407-
<x:String x:Key="Text.Preference.AI.SubjectPrompt" xml:space="preserve">Subject Prompt</x:String>
408-
<x:String x:Key="Text.Preference.AI.SummaryPromptHint" xml:space="preserve">You are an expert developer specialist in creating commits.
409-
Provide a super concise one sentence overall changes summary of the user `git diff` output following strictly the next rules:
410-
- Do not use any code snippets, imports, file routes or bullets points.
411-
- Do not mention the route of file that has been change.
412-
- Simply describe the MAIN GOAL of the changes.
413-
- Output directly the summary in plain text.
414-
</x:String>
415-
<x:String x:Key="Text.Preference.AI.SubjectPromptHint" xml:space="preserve">You are an expert developer specialist in creating commits messages.
416-
Your only goal is to retrieve a single commit message.
417-
Based on the provided user changes, combine them in ONE SINGLE commit message retrieving the global idea, following strictly the next rules:
418-
- Assign the commit {type} according to the next conditions:
419-
feat: Only when adding a new feature.
420-
fix: When fixing a bug.
421-
docs: When updating documentation.
422-
style: When changing elements styles or design and/or making changes to the code style (formatting, missing semicolons, etc.) without changing the code logic.
423-
test: When adding or updating tests.
424-
chore: When making changes to the build process or auxiliary tools and libraries.
425-
revert: When undoing a previous commit.
426-
refactor: When restructuring code without changing its external behavior, or is any of the other refactor types.
427-
- Do not add any issues numeration, explain your output nor introduce your answer.
428-
- Output directly only one commit message in plain text with the next format: {type}: {commit_message}.
429-
- Be as concise as possible, keep the message under 50 characters.
430-
</x:String>
407+
<x:String x:Key="Text.Preference.AI.Server" xml:space="preserve">Server</x:String>
431408
<x:String x:Key="Text.Preference.Appearance" xml:space="preserve">APPEARANCE</x:String>
432409
<x:String x:Key="Text.Preference.Appearance.DefaultFont" xml:space="preserve">Default Font</x:String>
433410
<x:String x:Key="Text.Preference.Appearance.DefaultFontSize" xml:space="preserve">Default Font Size</x:String>

src/ViewModels/Preference.cs

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public static Preference Instance
2525
_instance.PrepareGit();
2626
_instance.PrepareShellOrTerminal();
2727
_instance.PrepareWorkspaces();
28+
_instance.PrepareOpenAIPrompt();
2829

2930
return _instance;
3031
}
@@ -315,27 +316,27 @@ public string OpenAIModel
315316
}
316317
}
317318

318-
public string OpenAISubjectPrompt
319+
public string OpenAIAnalyzeDiffPrompt
319320
{
320-
get => Models.OpenAI.SubjectPrompt;
321+
get => Models.OpenAI.AnalyzeDiffPrompt;
321322
set
322323
{
323-
if (value != Models.OpenAI.SubjectPrompt)
324+
if (value != Models.OpenAI.AnalyzeDiffPrompt)
324325
{
325-
Models.OpenAI.SubjectPrompt = value;
326+
Models.OpenAI.AnalyzeDiffPrompt = value;
326327
OnPropertyChanged();
327328
}
328329
}
329330
}
330331

331-
public string OpenAISummaryPrompt
332+
public string OpenAIGenerateSubjectPrompt
332333
{
333-
get => Models.OpenAI.SummaryPrompt;
334+
get => Models.OpenAI.GenerateSubjectPrompt;
334335
set
335336
{
336-
if (value != Models.OpenAI.SummaryPrompt)
337+
if (value != Models.OpenAI.GenerateSubjectPrompt)
337338
{
338-
Models.OpenAI.SummaryPrompt = value;
339+
Models.OpenAI.GenerateSubjectPrompt = value;
339340
OnPropertyChanged();
340341
}
341342
}
@@ -553,6 +554,45 @@ private void PrepareWorkspaces()
553554
}
554555
}
555556

557+
private void PrepareOpenAIPrompt()
558+
{
559+
if (string.IsNullOrEmpty(Models.OpenAI.AnalyzeDiffPrompt))
560+
{
561+
Models.OpenAI.AnalyzeDiffPrompt = """
562+
You are an expert developer specialist in creating commits.
563+
Provide a super concise one sentence overall changes summary of the user `git diff` output following strictly the next rules:
564+
- Do not use any code snippets, imports, file routes or bullets points.
565+
- Do not mention the route of file that has been change.
566+
- Write clear, concise, and descriptive messages that explain the MAIN GOAL made of the changes.
567+
- Use the present tense and active voice in the message, for example, "Fix bug" instead of "Fixed bug.".
568+
- Use the imperative mood, which gives the message a sense of command, e.g. "Add feature" instead of "Added feature".
569+
- Avoid using general terms like "update" or "change", be specific about what was updated or changed.
570+
- Avoid using terms like "The main goal of", just output directly the summary in plain text
571+
""";
572+
}
573+
574+
if (string.IsNullOrEmpty(Models.OpenAI.GenerateSubjectPrompt))
575+
{
576+
Models.OpenAI.GenerateSubjectPrompt = """
577+
You are an expert developer specialist in creating commits messages.
578+
Your only goal is to retrieve a single commit message.
579+
Based on the provided user changes, combine them in ONE SINGLE commit message retrieving the global idea, following strictly the next rules:
580+
- Assign the commit {type} according to the next conditions:
581+
feat: Only when adding a new feature.
582+
fix: When fixing a bug.
583+
docs: When updating documentation.
584+
style: When changing elements styles or design and/or making changes to the code style (formatting, missing semicolons, etc.) without changing the code logic.
585+
test: When adding or updating tests.
586+
chore: When making changes to the build process or auxiliary tools and libraries.
587+
revert: When undoing a previous commit.
588+
refactor: When restructuring code without changing its external behavior, or is any of the other refactor types.
589+
- Do not add any issues numeration, explain your output nor introduce your answer.
590+
- Output directly only one commit message in plain text with the next format: {type}: {commit_message}.
591+
- Be as concise as possible, keep the message under 50 characters.
592+
""";
593+
}
594+
}
595+
556596
private RepositoryNode FindNodeRecursive(string id, List<RepositoryNode> collection)
557597
{
558598
foreach (var node in collection)

src/Views/Preference.axaml

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -499,31 +499,27 @@
499499
Text="{Binding OpenAIApiKey, Mode=TwoWay}"/>
500500

501501
<TextBlock Grid.Row="3" Grid.Column="0"
502-
Text="{DynamicResource Text.Preference.AI.SubjectPrompt}"
502+
Text="{DynamicResource Text.Preference.AI.AnalyzeDiffPrompt}"
503503
HorizontalAlignment="Right"
504504
Margin="0,0,16,0"/>
505-
506505
<TextBox Grid.Row="3" Grid.Column="1"
507506
Height="120"
508507
CornerRadius="3"
509508
VerticalContentAlignment="Top"
510-
Text="{Binding OpenAISubjectPrompt, Mode=TwoWay}"
509+
Text="{Binding OpenAIAnalyzeDiffPrompt, Mode=TwoWay}"
511510
AcceptsReturn="true"
512-
Watermark="{DynamicResource Text.Preference.AI.SubjectPromptHint}"
513511
TextWrapping="Wrap"/>
514512

515513
<TextBlock Grid.Row="4" Grid.Column="0"
516-
Text="{DynamicResource Text.Preference.AI.SummaryPrompt}"
514+
Text="{DynamicResource Text.Preference.AI.GenerateSubjectPrompt}"
517515
HorizontalAlignment="Right"
518516
Margin="0,0,16,0"/>
519-
520517
<TextBox Grid.Row="4" Grid.Column="1"
521518
Height="120"
522519
CornerRadius="3"
523520
VerticalContentAlignment="Top"
524-
Text="{Binding OpenAISummaryPrompt, Mode=TwoWay}"
521+
Text="{Binding OpenAIGenerateSubjectPrompt, Mode=TwoWay}"
525522
AcceptsReturn="true"
526-
Watermark="{DynamicResource Text.Preference.AI.SummaryPromptHint}"
527523
TextWrapping="Wrap"/>
528524
</Grid>
529525
</StackPanel>

0 commit comments

Comments
 (0)