feat: support language for commit message generation

- Extend the GenerateCommitMessage constructor to include a language parameter and use it in the selected language prompt logic.
- Add property for commit message language with default value "English".
- Add integration related configuration strings for commit message language.
- Add property for CommitMessageLanguage in RepositoryConfigure.
- Add language parameter to AIAssistant constructor and propagate it to GenerateCommitMessage.
- Add integration configuration options in the repository settings view.
This commit is contained in:
Douglas Cunha 2024-10-27 14:57:57 -03:00
parent ac543dbe07
commit 3a0ae1c6e0
No known key found for this signature in database
GPG key ID: 2D100AA81881B482
13 changed files with 69 additions and 95 deletions

View file

@ -20,9 +20,10 @@ namespace SourceGit.Commands
}
}
public GenerateCommitMessage(string repo, List<Models.Change> changes, CancellationToken cancelToken, Action<string> onProgress)
public GenerateCommitMessage(string repo, string language, List<Models.Change> changes, CancellationToken cancelToken, Action<string> onProgress)
{
_repo = repo;
_language = language ?? "English";
_changes = changes;
_cancelToken = cancelToken;
_onProgress = onProgress;
@ -70,11 +71,11 @@ namespace SourceGit.Commands
}
}
private static string GetSelectedLanguagePrompt()
private string GetSelectedLanguagePrompt()
{
var selectedLanguage = Models.OpenAI.SelectedLanguage == "English"
var selectedLanguage = _language == "English"
? string.Empty
: $"{Environment.NewLine}Always write in {Models.OpenAI.SelectedLanguage}";
: $"{Environment.NewLine}Always write in {_language}";
return selectedLanguage;
}
@ -106,5 +107,6 @@ namespace SourceGit.Commands
private List<Models.Change> _changes;
private CancellationToken _cancelToken;
private Action<string> _onProgress;
private readonly string _language;
}
}

View file

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

View file

@ -112,6 +112,12 @@ namespace SourceGit.Models
set;
} = false;
public string CommitMessageLanguage
{
get;
set;
} = "English";
public void PushCommitMessage(string message)
{
var existIdx = CommitMessages.IndexOf(message);

View file

@ -61,7 +61,6 @@
<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,7 +19,6 @@
<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>
@ -145,6 +144,8 @@
<x:String x:Key="Text.Configure.Git.AutoFetchIntervalSuffix" xml:space="preserve">Minute(s)</x:String>
<x:String x:Key="Text.Configure.Git.DefaultRemote" xml:space="preserve">Default Remote</x:String>
<x:String x:Key="Text.Configure.Git.EnableSignOff" xml:space="preserve">Enable --signoff for commit</x:String>
<x:String x:Key="Text.Configure.Integration" xml:space="preserve">INTEGRATION</x:String>
<x:String x:Key="Text.Configure.Integration.Language" xml:space="preserve">Commit Message Language</x:String>
<x:String x:Key="Text.Configure.IssueTracker" xml:space="preserve">ISSUE TRACKER</x:String>
<x:String x:Key="Text.Configure.IssueTracker.AddSampleGithub" xml:space="preserve">Add Sample Github Rule</x:String>
<x:String x:Key="Text.Configure.IssueTracker.AddSampleJira" xml:space="preserve">Add Sample Jira Rule</x:String>

View file

@ -21,7 +21,6 @@
<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>
@ -136,6 +135,8 @@
<x:String x:Key="Text.Configure.Git.AutoFetch" xml:space="preserve">Buscar remotos automaticamente</x:String>
<x:String x:Key="Text.Configure.Git.AutoFetchIntervalSuffix" xml:space="preserve">Minuto(s)</x:String>
<x:String x:Key="Text.Configure.Git" xml:space="preserve">GIT</x:String>
<x:String x:Key="Text.Configure.Integration" xml:space="preserve">INTEGRAÇÃO</x:String>
<x:String x:Key="Text.Configure.Integration.Language" xml:space="preserve">Idioma para mensagem de commit</x:String>
<x:String x:Key="Text.Configure.IssueTracker.AddSampleGithub" xml:space="preserve">Adicionar Regra de Exemplo do Github</x:String>
<x:String x:Key="Text.Configure.IssueTracker.AddSampleJira" xml:space="preserve">Adicionar Regra de Exemplo do Jira</x:String>
<x:String x:Key="Text.Configure.IssueTracker.NewRule" xml:space="preserve">Nova Regra</x:String>

View file

@ -316,19 +316,6 @@ 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;
@ -604,9 +591,6 @@ 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

@ -86,6 +86,12 @@ namespace SourceGit.ViewModels
}
}
public string CommitMessageLanguage
{
get => _repo.Settings.CommitMessageLanguage;
set => _repo.Settings.CommitMessageLanguage = value;
}
public AvaloniaList<Models.CommitTemplate> CommitTemplates
{
get => _repo.Settings.CommitTemplates;

View file

@ -414,7 +414,7 @@ namespace SourceGit.ViewModels
if (_staged is { Count: > 0 })
{
var dialog = new Views.AIAssistant(_repo.FullPath, _staged, generated => CommitMessage = generated);
var dialog = new Views.AIAssistant(_repo.FullPath, _repo.Settings.CommitMessageLanguage, _staged, generated => CommitMessage = generated);
App.OpenDialog(dialog);
}
else
@ -1148,38 +1148,7 @@ 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()
public ContextMenu CreateContextMenuForCommitMessages()
{
var menu = new ContextMenu();

View file

@ -17,9 +17,10 @@ namespace SourceGit.Views
InitializeComponent();
}
public AIAssistant(string repo, List<Models.Change> changes, Action<string> onDone)
public AIAssistant(string repo, string language, List<Models.Change> changes, Action<string> onDone)
{
_repo = repo;
_language = language;
_changes = changes;
_onDone = onDone;
_cancel = new CancellationTokenSource();
@ -35,7 +36,7 @@ namespace SourceGit.Views
Task.Run(() =>
{
var message = new Commands.GenerateCommitMessage(_repo, _changes, _cancel.Token, SetDescription).Result();
var message = new Commands.GenerateCommitMessage(_repo, _language, _changes, _cancel.Token, SetDescription).Result();
if (_cancel.IsCancellationRequested)
return;
@ -67,5 +68,6 @@ namespace SourceGit.Views
private List<Models.Change> _changes;
private Action<string> _onDone;
private CancellationTokenSource _cancel;
private readonly string _language;
}
}

View file

@ -5,6 +5,7 @@
xmlns:m="using:SourceGit.Models"
xmlns:vm="using:SourceGit.ViewModels"
xmlns:v="using:SourceGit.Views"
xmlns:c="using:SourceGit.Converters"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="SourceGit.Views.RepositoryConfigure"
x:DataType="vm:RepositoryConfigure"
@ -340,6 +341,38 @@
</ContentControl>
</Grid>
</TabItem>
<TabItem>
<TabItem.Header>
<TextBlock Classes="tab_header" Text="{DynamicResource Text.Configure.Integration}"/>
</TabItem.Header>
<StackPanel Margin="0" MaxWidth="580" Orientation="Vertical" Grid.IsSharedSizeScope="True">
<StackPanel Orientation="Horizontal" Margin="0,24,0,0">
<Path Width="12" Height="12" Data="{StaticResource Icons.AIAssist}"/>
<TextBlock Classes="bold" Margin="4,0,0,0" Text="{DynamicResource Text.Preference.AI}"/>
</StackPanel>
<Rectangle Margin="0,8" Fill="{DynamicResource Brush.Border2}" Height=".6" HorizontalAlignment="Stretch"/>
<Grid Margin="8,0,0,12" RowDefinitions="32,Auto">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="IntegrationLabel"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0"
Text="{DynamicResource Text.Configure.Integration.Language}"
HorizontalAlignment="Right"
Margin="0,0,16,0"/>
<ComboBox Grid.Row="0" Grid.Column="1"
MinHeight="28"
Padding="8,0"
HorizontalAlignment="Stretch"
ItemsSource="{Binding Source={x:Static m:Locale.Supported}}"
DisplayMemberBinding="{Binding Name, x:DataType=m:Locale}"
SelectedItem="{Binding CommitMessageLanguage, Mode=TwoWay, Converter={x:Static c:StringConverters.ToLocale}}"/>
</Grid>
</StackPanel>
</TabItem>
</TabControl>
</Grid>
</v:ChromelessWindow>

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,Auto">
<Grid Grid.Row="3" Margin="0,6,0,0" ColumnDefinitions="Auto,Auto,Auto,Auto,*,Auto,Auto,Auto,Auto">
<Button Grid.Column="0"
Classes="icon_button"
Margin="4,0,0,0" Padding="0"
@ -206,19 +206,7 @@
<Path Width="15" Height="15" Data="{StaticResource Icons.AIAssist}"/>
</Button>
<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"
<Button Grid.Column="2"
Classes="icon_button"
Margin="0,2,0,0"
Click="OnOpenConventionalCommitHelper"
@ -228,7 +216,7 @@
<Path Width="15" Height="15" Data="{StaticResource Icons.CommitMessageGenerator}"/>
</Button>
<CheckBox Grid.Column="4"
<CheckBox Grid.Column="3"
Height="24"
Margin="8,0,0,0"
HorizontalAlignment="Left"
@ -260,7 +248,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,17 +22,6 @@ 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)