feat: Add dynamic language support in commit message generation with OpenAI

- Add support for dynamic language prompts in commit message generation.
- Add property to store selected language in OpenAI model.
- Add new icon definition for language selection in the icon resource file.
- Add localization string for the tip regarding the selected language in the OpenAI Assistant feature.
- Add OpenAI assistant strings for commit message generation in Brazilian Portuguese localization.
- Add SelectedLanguage property to manage language preference.
- Add method to create context menu for selecting language.
- Refactor tooltips and adjust button layouts for better alignment and additional functionality.
- Add a method to open a context menu for language selection in the WorkingCopy view.

Signed-off-by: Douglas Cunha <dougcunha@gmail.com>
This commit is contained in:
Douglas Cunha 2024-10-24 18:35:50 -03:00
parent 63f75dc589
commit c6d308be2c
No known key found for this signature in database
GPG key ID: 2D100AA81881B482
9 changed files with 103 additions and 10 deletions

View file

@ -70,12 +70,22 @@ namespace SourceGit.Commands
}
}
private static string GetSelectedLanguagePrompt()
{
var selectedLanguage = Models.OpenAI.SelectedLanguage == "English"
? string.Empty
: $"{Environment.NewLine}Always write in {Models.OpenAI.SelectedLanguage}";
return selectedLanguage;
}
private string GenerateChangeSummary(Models.Change change)
{
var rs = new GetDiffContent(_repo, new Models.DiffOption(change, false)).ReadToEnd();
var diff = rs.IsSuccess ? rs.StdOut : "unknown change";
var selectedLanguagePrompt = GetSelectedLanguagePrompt();
var rsp = Models.OpenAI.Chat(Models.OpenAI.AnalyzeDiffPrompt, $"Here is the `git diff` output: {diff}", _cancelToken);
var rsp = Models.OpenAI.Chat(Models.OpenAI.AnalyzeDiffPrompt + selectedLanguagePrompt, $"Here is the `git diff` output: {diff}", _cancelToken);
if (rsp != null && rsp.Choices.Count > 0)
return rsp.Choices[0].Message.Content;
@ -84,7 +94,8 @@ namespace SourceGit.Commands
private string GenerateSubject(string summary)
{
var rsp = Models.OpenAI.Chat(Models.OpenAI.GenerateSubjectPrompt, $"Here are the summaries changes:\n{summary}", _cancelToken);
var selectedLanguagePrompt = GetSelectedLanguagePrompt();
var rsp = Models.OpenAI.Chat(Models.OpenAI.GenerateSubjectPrompt + selectedLanguagePrompt, $"Here are the summaries changes:\n{summary}", _cancelToken);
if (rsp != null && rsp.Choices.Count > 0)
return rsp.Choices[0].Message.Content;

View file

@ -94,6 +94,12 @@ namespace SourceGit.Models
set;
}
public static string SelectedLanguage
{
get;
set;
}
public static string AnalyzeDiffPrompt
{
get;

View file

@ -61,6 +61,7 @@
<StreamGeometry x:Key="Icons.Init">M412 66C326 132 271 233 271 347c0 17 1 34 4 50-41-48-98-79-162-83a444 444 0 00-46 196c0 207 142 382 337 439h2c19 0 34 15 34 33 0 11-6 21-14 26l1 14C183 973 0 763 0 511 0 272 166 70 393 7A35 35 0 01414 0c19 0 34 15 34 33a33 33 0 01-36 33zm200 893c86-66 141-168 141-282 0-17-1-34-4-50 41 48 98 79 162 83a444 444 0 0046-196c0-207-142-382-337-439h-2a33 33 0 01-34-33c0-11 6-21 14-26L596 0C841 51 1024 261 1024 513c0 239-166 441-393 504A35 35 0 01610 1024a33 33 0 01-34-33 33 33 0 0136-33zM512 704a192 192 0 110-384 192 192 0 010 384z</StreamGeometry>
<StreamGeometry x:Key="Icons.InteractiveRebase">M512 64A447 447 0 0064 512c0 248 200 448 448 448s448-200 448-448S760 64 512 64zM218 295h31c54 0 105 19 145 55 13 12 13 31 3 43a35 35 0 01-22 10 36 36 0 01-21-7 155 155 0 00-103-39h-31a32 32 0 01-31-31c0-18 13-31 30-31zm31 433h-31a32 32 0 01-31-31c0-16 13-31 31-31h31A154 154 0 00403 512 217 217 0 01620 295h75l-93-67a33 33 0 01-7-43 33 33 0 0143-7l205 148-205 148a29 29 0 01-18 6 32 32 0 01-31-31c0-10 4-19 13-25l93-67H620a154 154 0 00-154 154c0 122-97 220-217 220zm390 118a29 29 0 01-18 6 32 32 0 01-31-31c0-10 4-19 13-25l93-67h-75c-52 0-103-19-143-54-12-12-13-31-1-43a30 30 0 0142-3 151 151 0 00102 39h75L602 599a33 33 0 01-7-43 33 33 0 0143-7l205 148-203 151z</StreamGeometry>
<StreamGeometry x:Key="Icons.Issue">M922 39H102A65 65 0 0039 106v609a65 65 0 0063 68h94v168a34 34 0 0019 31 30 30 0 0012 3 30 30 0 0022-10l182-192H922a65 65 0 0063-68V106A65 65 0 00922 39zM288 378h479a34 34 0 010 68H288a34 34 0 010-68zm0-135h479a34 34 0 010 68H288a34 34 0 010-68zm0 270h310a34 34 0 010 68H288a34 34 0 010-68z</StreamGeometry>
<StreamGeometry x:Key="Icons.LanguageSelection">M480-120q-74.31 0-140-28.42-65.69-28.43-114.42-77.16-48.73-48.73-77.16-114.42Q120-405.69 120-480q0-74.54 28.42-140.12 28.43-65.57 77.16-114.3 48.73-48.73 114.42-77.16Q405.69-840 480-840q74.54 0 140.12 28.42 65.57 28.43 114.3 77.16 48.73 48.73 77.16 114.3Q840-554.54 840-480q0 74.31-28.42 140-28.43 65.69-77.16 114.42-48.73 48.73-114.3 77.16Q554.54-120 480-120Zm0-39.69q35.23-45.23 58.08-88.85 22.84-43.61 37.15-97.61H384.77q15.85 57.07 37.92 100.69 22.08 43.61 57.31 85.77Zm-50.92-6q-28-33-51.12-81.58-23.11-48.58-34.42-98.88H190.15q34.39 74.61 97.5 122.38 63.12 47.77 141.43 58.08Zm101.84 0q78.31-10.31 141.43-58.08 63.11-47.77 97.5-122.38H616.46q-15.15 51.07-38.27 99.65-23.11 48.58-47.27 80.81ZM173.85-386.15h161.38q-4.54-24.62-6.42-47.97-1.89-23.34-1.89-45.88 0-22.54 1.89-45.88 1.88-23.35 6.42-47.97H173.85q-6.54 20.77-10.2 45.27Q160-504.08 160-480t3.65 48.58q3.66 24.5 10.2 45.27Zm201.38 0h209.54q4.54-24.62 6.42-47.2 1.89-22.57 1.89-46.65t-1.89-46.65q-1.88-22.58-6.42-47.2H375.23q-4.54 24.62-6.42 47.2-1.89 22.57-1.89 46.65t1.89 46.65q1.88 22.58 6.42 47.2Zm249.54 0h161.38q6.54-20.77 10.2-45.27Q800-455.92 800-480t-3.65-48.58q-3.66-24.5-10.2-45.27H624.77q4.54 24.62 6.42 47.97 1.89 23.34 1.89 45.88 0 22.54-1.89 45.88-1.88 23.35-6.42 47.97Zm-8.31-227.7h153.39Q734.69-690 673.5-736.23q-61.19-46.23-142.58-58.85 28 36.85 50.35 84.27 22.35 47.43 35.19 96.96Zm-231.69 0h190.46q-15.85-56.3-39.08-101.84-23.23-45.54-56.15-84.62-32.92 39.08-56.15 84.62-23.23 45.54-39.08 101.84Zm-194.62 0h153.39q12.84-49.53 35.19-96.96 22.35-47.42 50.35-84.27-82.16 12.62-142.96 59.23-60.81 46.62-95.97 122Z</StreamGeometry>
<StreamGeometry x:Key="Icons.LayoutHorizontal">M875 117H149C109 117 75 151 75 192v640c0 41 34 75 75 75h725c41 0 75-34 75-75V192c0-41-34-75-75-75zM139 832V192c0-6 4-11 11-11h331v661H149c-6 0-11-4-11-11zm747 0c0 6-4 11-11 11H544v-661H875c6 0 11 4 11 11v640z</StreamGeometry>
<StreamGeometry x:Key="Icons.LayoutVertical">M875 117H149C109 117 75 151 75 192v640c0 41 34 75 75 75h725c41 0 75-34 75-75V192c0-41-34-75-75-75zm-725 64h725c6 0 11 4 11 11v288h-747V192c0-6 4-11 11-11zm725 661H149c-6 0-11-4-11-11V544h747V832c0 6-4 11-11 11z</StreamGeometry>
<StreamGeometry x:Key="Icons.LFS">M40 9 15 23 15 31 9 28 9 20 34 5 24 0 0 14 0 34 25 48 25 28 49 14zM26 29 26 48 49 34 49 15z</StreamGeometry>

View file

@ -19,6 +19,7 @@
<x:String x:Key="Text.AddWorktree.Tracking" xml:space="preserve">Track Branch:</x:String>
<x:String x:Key="Text.AddWorktree.Tracking.Toggle" xml:space="preserve">Tracking remote branch</x:String>
<x:String x:Key="Text.AIAssistant" xml:space="preserve">OpenAI Assistant</x:String>
<x:String x:Key="Text.AIAssistant.SelectedLanguageTip" xml:space="preserve">Language used to generate commit message</x:String>
<x:String x:Key="Text.AIAssistant.Tip" xml:space="preserve">Use OpenAI to generate commit message</x:String>
<x:String x:Key="Text.Apply" xml:space="preserve">Patch</x:String>
<x:String x:Key="Text.Apply.Error" xml:space="preserve">Error</x:String>

View file

@ -20,6 +20,9 @@
<x:String x:Key="Text.AddWorktree.WhatToCheckout.Existing" xml:space="preserve">Branch Existente</x:String>
<x:String x:Key="Text.AddWorktree.WhatToCheckout" xml:space="preserve">O que Checar:</x:String>
<x:String x:Key="Text.AddWorktree" xml:space="preserve">Adicionar Worktree</x:String>
<x:String x:Key="Text.AIAssistant" xml:space="preserve">Assistente do OpenAI</x:String>
<x:String x:Key="Text.AIAssistant.SelectedLanguageTip" xml:space="preserve">Idioma usado a gerar a mensagem de commit</x:String>
<x:String x:Key="Text.AIAssistant.Tip" xml:space="preserve">Usar OpenAI para gerar mensagem de commit</x:String>
<x:String x:Key="Text.Apply.Error.Desc" xml:space="preserve">Erros levantados e se recusa a aplicar o patch</x:String>
<x:String x:Key="Text.Apply.Error" xml:space="preserve">Erro</x:String>
<x:String x:Key="Text.Apply.ErrorAll.Desc" xml:space="preserve">Semelhante a 'erro', mas mostra mais</x:String>

View file

@ -316,6 +316,19 @@ namespace SourceGit.ViewModels
}
}
public string SelectedLanguage
{
get => Models.OpenAI.SelectedLanguage;
set
{
if (value != Models.OpenAI.SelectedLanguage)
{
Models.OpenAI.SelectedLanguage = value;
OnPropertyChanged();
}
}
}
public string OpenAIAnalyzeDiffPrompt
{
get => Models.OpenAI.AnalyzeDiffPrompt;
@ -591,6 +604,9 @@ namespace SourceGit.ViewModels
- Be as concise as possible, keep the message under 50 characters.
""";
}
if (string.IsNullOrEmpty(Models.OpenAI.SelectedLanguage))
Models.OpenAI.SelectedLanguage = "English";
}
private RepositoryNode FindNodeRecursive(string id, List<RepositoryNode> collection)

View file

@ -8,6 +8,7 @@ using Avalonia.Platform.Storage;
using Avalonia.Threading;
using CommunityToolkit.Mvvm.ComponentModel;
using SourceGit.Models;
namespace SourceGit.ViewModels
{
@ -1147,6 +1148,37 @@ namespace SourceGit.ViewModels
return menu;
}
public ContextMenu CreateContextMenuForSelectedLanguage()
{
var menu = new ContextMenu();
var selectedLanguage = string.IsNullOrEmpty(Preference.Instance.SelectedLanguage)
? "English"
: Preference.Instance.SelectedLanguage;
foreach (var locale in Locale.Supported)
{
var language = locale.Name;
var item = new MenuItem
{
Header = language,
Icon = selectedLanguage.Equals(language, StringComparison.OrdinalIgnoreCase)
? App.CreateMenuIcon("Icons.Check")
: null
};
item.Click += (_, e) =>
{
Preference.Instance.SelectedLanguage = language;
e.Handled = true;
};
menu.Items.Add(item);
}
return menu;
}
public ContextMenu CreateContextMenuForCommitMessages()
{
var menu = new ContextMenu();

View file

@ -55,7 +55,7 @@
Classes="icon_button"
Width="26" Height="14"
Padding="0"
ToolTip.Tip="{DynamicResource Text.WorkingCopy.Unstaged.StageAll}"
ToolTip.Tip="{DynamicResource Text.WorkingCopy.Unstaged.StageAll}"
Command="{Binding StageAll}">
<Path Width="14" Height="14" Data="{StaticResource Icons.DoubleDown}"/>
</Button>
@ -99,7 +99,7 @@
<Button Grid.Column="6" Classes="icon_button" Width="26" Height="14" Padding="0" ToolTip.Tip="{DynamicResource Text.WorkingCopy.Staged.UnstageAll}" Command="{Binding UnstageAll}">
<Path Width="14" Height="14" Data="{StaticResource Icons.DoubleUp}"/>
</Button>
<v:ChangeViewModeSwitcher Grid.Column="7"
<v:ChangeViewModeSwitcher Grid.Column="7"
Width="26" Height="14"
Margin="0,1,0,0"
ViewMode="{Binding Source={x:Static vm:Preference.Instance}, Path=StagedChangeViewMode, Mode=TwoWay}"/>
@ -135,7 +135,7 @@
<RowDefinition Height="128" MinHeight="100"/>
<RowDefinition Height="36"/>
</Grid.RowDefinitions>
<!-- Select Change Detail -->
<Grid Grid.Row="0">
<Border BorderThickness="1" BorderBrush="{DynamicResource Brush.Border2}">
@ -165,7 +165,7 @@
<TextBlock Margin="0,16,0,8" FontSize="20" FontWeight="Bold" Text="{DynamicResource Text.WorkingCopy.Conflicts.Resolved}" Foreground="{DynamicResource Brush.FG2}" HorizontalAlignment="Center"/>
<TextBlock Text="{DynamicResource Text.WorkingCopy.CanStageTip}" Foreground="{DynamicResource Brush.FG2}" HorizontalAlignment="Center"/>
</StackPanel>
</Grid>
</Grid>
</Border>
</DataTemplate>
@ -185,7 +185,7 @@
<v:CommitMessageTextBox Grid.Row="2" Text="{Binding CommitMessage, Mode=TwoWay}"/>
<!-- Commit Options -->
<Grid Grid.Row="3" Margin="0,6,0,0" ColumnDefinitions="Auto,Auto,Auto,Auto,*,Auto,Auto,Auto,Auto">
<Grid Grid.Row="3" Margin="0,6,0,0" ColumnDefinitions="Auto,Auto,Auto,Auto,*,Auto,Auto,Auto,Auto,Auto">
<Button Grid.Column="0"
Classes="icon_button"
Margin="4,0,0,0" Padding="0"
@ -206,7 +206,19 @@
<Path Width="15" Height="15" Data="{StaticResource Icons.AIAssist}"/>
</Button>
<Button Grid.Column="2"
<Button Grid.Column="2"
Classes="icon_button"
Margin="0,2,0,0"
ToolTip.Tip="{DynamicResource Text.AIAssistant.SelectedLanguageTip}"
ToolTip.Placement="Top"
Click="OnOpenSelectLanguage"
ToolTip.VerticalOffset="0">
<Path Width="15" Height="15" Data="{StaticResource Icons.LanguageSelection}"/>
</Button>
<Button Grid.Column="3"
Classes="icon_button"
Margin="0,2,0,0"
Click="OnOpenConventionalCommitHelper"
@ -216,7 +228,7 @@
<Path Width="15" Height="15" Data="{StaticResource Icons.CommitMessageGenerator}"/>
</Button>
<CheckBox Grid.Column="3"
<CheckBox Grid.Column="4"
Height="24"
Margin="8,0,0,0"
HorizontalAlignment="Left"
@ -248,7 +260,7 @@
</StackPanel>
</ToolTip.Tip>
</Button>
<!-- Invisible button just to add another hotkey `Ctrl+Shift+Enter` to commit with auto-stage -->
<Button Grid.Column="7"
Width="0" Height="0"

View file

@ -22,6 +22,17 @@ namespace SourceGit.Views
}
}
public void OnOpenSelectLanguage(object sender, RoutedEventArgs e)
{
if (sender is Button button && DataContext is ViewModels.WorkingCopy vm)
{
var menu = vm.CreateContextMenuForSelectedLanguage();
menu.Placement = PlacementMode.TopEdgeAlignedLeft;
button.OpenContextMenu(menu);
e.Handled = true;
}
}
private void OnUnstagedContextRequested(object sender, ContextRequestedEventArgs e)
{
if (DataContext is ViewModels.WorkingCopy vm)