mirror of
https://github.com/sourcegit-scm/sourcegit.git
synced 2024-12-22 20:37:19 -08:00
refactor: rewrite OpenAI integration
Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
parent
f6e1e65a53
commit
d21a8f2449
5 changed files with 52 additions and 53 deletions
|
@ -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);
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
@ -13,28 +15,36 @@ 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(() =>
|
||||
{
|
||||
Task.Run(() =>
|
||||
var message = new Commands.GenerateCommitMessage(_repo, _changes, _cancel.Token, SetDescription).Result();
|
||||
if (_cancel.IsCancellationRequested)
|
||||
return;
|
||||
|
||||
Dispatcher.UIThread.Invoke(() =>
|
||||
{
|
||||
var message = new Commands.GenerateCommitMessage(vm.RepoPath, vm.Staged, _cancel.Token, SetDescription).Result();
|
||||
if (_cancel.IsCancellationRequested)
|
||||
return;
|
||||
|
||||
Dispatcher.UIThread.Invoke(() =>
|
||||
{
|
||||
if (DataContext is ViewModels.WorkingCopy wc)
|
||||
wc.CommitMessage = message;
|
||||
|
||||
Close();
|
||||
});
|
||||
}, _cancel.Token);
|
||||
}
|
||||
_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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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">
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue