refactor: rewrite OpenAI integration

Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
leo 2024-10-23 14:05:40 +08:00
parent f6e1e65a53
commit d21a8f2449
No known key found for this signature in database
5 changed files with 52 additions and 53 deletions

View file

@ -33,11 +33,6 @@ namespace SourceGit.ViewModels
public class WorkingCopy : ObservableObject
{
public string RepoPath
{
get => _repo.FullPath;
}
public bool IncludeUntracked
{
get => _repo.IncludeUntracked;
@ -408,6 +403,25 @@ namespace SourceGit.ViewModels
}
}
public void GenerateCommitMessageByAI()
{
if (!Models.OpenAI.IsValid)
{
App.RaiseException(_repo.FullPath, "Bad configuration for OpenAI");
return;
}
if (_staged is { Count: > 0 })
{
var dialog = new Views.AIAssistant(_repo.FullPath, _staged, generated => CommitMessage = generated);
App.OpenDialog(dialog);
}
else
{
App.RaiseException(_repo.FullPath, "No files added to commit!");
}
}
public void Commit()
{
DoCommit(false, false, false);

View file

@ -7,7 +7,6 @@
xmlns:c="using:SourceGit.Converters"
mc:Ignorable="d" d:DesignWidth="400" d:DesignHeight="120"
x:Class="SourceGit.Views.AIAssistant"
x:DataType="vm:WorkingCopy"
x:Name="ThisControl"
Icon="/App.ico"
Title="{DynamicResource Text.AIAssistant}"
@ -55,6 +54,7 @@
Margin="16"
FontSize="{Binding Source={x:Static vm:Preference.Instance}, Path=DefaultFontSize, Converter={x:Static c:DoubleConverters.Decrease}}"
HorizontalAlignment="Center"
Text="Generating commit message... Please wait!"
TextTrimming="CharacterEllipsis"/>
</Grid>
</v:ChromelessWindow>

View file

@ -1,3 +1,5 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
@ -13,29 +15,37 @@ namespace SourceGit.Views
{
_cancel = new CancellationTokenSource();
InitializeComponent();
ProgressMessage.Text = "Generating commit message... Please wait!";
}
public void GenerateCommitMessage()
public AIAssistant(string repo, List<Models.Change> changes, Action<string> onDone)
{
if (DataContext is ViewModels.WorkingCopy vm)
_repo = repo;
_changes = changes;
_onDone = onDone;
_cancel = new CancellationTokenSource();
InitializeComponent();
}
protected override void OnOpened(EventArgs e)
{
base.OnOpened(e);
if (string.IsNullOrEmpty(_repo))
return;
Task.Run(() =>
{
var message = new Commands.GenerateCommitMessage(vm.RepoPath, vm.Staged, _cancel.Token, SetDescription).Result();
var message = new Commands.GenerateCommitMessage(_repo, _changes, _cancel.Token, SetDescription).Result();
if (_cancel.IsCancellationRequested)
return;
Dispatcher.UIThread.Invoke(() =>
{
if (DataContext is ViewModels.WorkingCopy wc)
wc.CommitMessage = message;
_onDone?.Invoke(message);
Close();
});
}, _cancel.Token);
}
}
protected override void OnClosing(WindowClosingEventArgs e)
{
@ -50,12 +60,12 @@ namespace SourceGit.Views
private void SetDescription(string message)
{
Dispatcher.UIThread.Invoke(() =>
{
ProgressMessage.Text = message;
});
Dispatcher.UIThread.Invoke(() => ProgressMessage.Text = message);
}
private string _repo;
private List<Models.Change> _changes;
private Action<string> _onDone;
private CancellationTokenSource _cancel;
}
}

View file

@ -199,7 +199,7 @@
<Button Grid.Column="1"
Classes="icon_button"
Margin="4,2,0,0"
Click="OnOpenAIAssist"
Command="{Binding GenerateCommitMessageByAI}"
ToolTip.Tip="{DynamicResource Text.AIAssistant.Tip}"
ToolTip.Placement="Top"
ToolTip.VerticalOffset="0">

View file

@ -120,31 +120,6 @@ namespace SourceGit.Views
e.Handled = true;
}
private void OnOpenAIAssist(object _, RoutedEventArgs e)
{
if (!Models.OpenAI.IsValid)
{
App.RaiseException(null, "Bad configuration for OpenAI");
return;
}
if (DataContext is ViewModels.WorkingCopy vm)
{
if (vm.Staged is { Count: > 0 })
{
var dialog = new AIAssistant() { DataContext = vm };
dialog.GenerateCommitMessage();
App.OpenDialog(dialog);
}
else
{
App.RaiseException(null, "No files added to commit!");
}
}
e.Handled = true;
}
private void OnOpenConventionalCommitHelper(object _, RoutedEventArgs e)
{
if (DataContext is ViewModels.WorkingCopy vm)