mirror of
https://github.com/sourcegit-scm/sourcegit.git
synced 2024-12-24 20:57: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 class WorkingCopy : ObservableObject
|
||||||
{
|
{
|
||||||
public string RepoPath
|
|
||||||
{
|
|
||||||
get => _repo.FullPath;
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool IncludeUntracked
|
public bool IncludeUntracked
|
||||||
{
|
{
|
||||||
get => _repo.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()
|
public void Commit()
|
||||||
{
|
{
|
||||||
DoCommit(false, false, false);
|
DoCommit(false, false, false);
|
||||||
|
|
|
@ -7,7 +7,6 @@
|
||||||
xmlns:c="using:SourceGit.Converters"
|
xmlns:c="using:SourceGit.Converters"
|
||||||
mc:Ignorable="d" d:DesignWidth="400" d:DesignHeight="120"
|
mc:Ignorable="d" d:DesignWidth="400" d:DesignHeight="120"
|
||||||
x:Class="SourceGit.Views.AIAssistant"
|
x:Class="SourceGit.Views.AIAssistant"
|
||||||
x:DataType="vm:WorkingCopy"
|
|
||||||
x:Name="ThisControl"
|
x:Name="ThisControl"
|
||||||
Icon="/App.ico"
|
Icon="/App.ico"
|
||||||
Title="{DynamicResource Text.AIAssistant}"
|
Title="{DynamicResource Text.AIAssistant}"
|
||||||
|
@ -55,6 +54,7 @@
|
||||||
Margin="16"
|
Margin="16"
|
||||||
FontSize="{Binding Source={x:Static vm:Preference.Instance}, Path=DefaultFontSize, Converter={x:Static c:DoubleConverters.Decrease}}"
|
FontSize="{Binding Source={x:Static vm:Preference.Instance}, Path=DefaultFontSize, Converter={x:Static c:DoubleConverters.Decrease}}"
|
||||||
HorizontalAlignment="Center"
|
HorizontalAlignment="Center"
|
||||||
|
Text="Generating commit message... Please wait!"
|
||||||
TextTrimming="CharacterEllipsis"/>
|
TextTrimming="CharacterEllipsis"/>
|
||||||
</Grid>
|
</Grid>
|
||||||
</v:ChromelessWindow>
|
</v:ChromelessWindow>
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
@ -13,28 +15,36 @@ namespace SourceGit.Views
|
||||||
{
|
{
|
||||||
_cancel = new CancellationTokenSource();
|
_cancel = new CancellationTokenSource();
|
||||||
InitializeComponent();
|
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();
|
_onDone?.Invoke(message);
|
||||||
if (_cancel.IsCancellationRequested)
|
Close();
|
||||||
return;
|
});
|
||||||
|
}, _cancel.Token);
|
||||||
Dispatcher.UIThread.Invoke(() =>
|
|
||||||
{
|
|
||||||
if (DataContext is ViewModels.WorkingCopy wc)
|
|
||||||
wc.CommitMessage = message;
|
|
||||||
|
|
||||||
Close();
|
|
||||||
});
|
|
||||||
}, _cancel.Token);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void OnClosing(WindowClosingEventArgs e)
|
protected override void OnClosing(WindowClosingEventArgs e)
|
||||||
|
@ -50,12 +60,12 @@ namespace SourceGit.Views
|
||||||
|
|
||||||
private void SetDescription(string message)
|
private void SetDescription(string message)
|
||||||
{
|
{
|
||||||
Dispatcher.UIThread.Invoke(() =>
|
Dispatcher.UIThread.Invoke(() => ProgressMessage.Text = message);
|
||||||
{
|
|
||||||
ProgressMessage.Text = message;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private string _repo;
|
||||||
|
private List<Models.Change> _changes;
|
||||||
|
private Action<string> _onDone;
|
||||||
private CancellationTokenSource _cancel;
|
private CancellationTokenSource _cancel;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -199,7 +199,7 @@
|
||||||
<Button Grid.Column="1"
|
<Button Grid.Column="1"
|
||||||
Classes="icon_button"
|
Classes="icon_button"
|
||||||
Margin="4,2,0,0"
|
Margin="4,2,0,0"
|
||||||
Click="OnOpenAIAssist"
|
Command="{Binding GenerateCommitMessageByAI}"
|
||||||
ToolTip.Tip="{DynamicResource Text.AIAssistant.Tip}"
|
ToolTip.Tip="{DynamicResource Text.AIAssistant.Tip}"
|
||||||
ToolTip.Placement="Top"
|
ToolTip.Placement="Top"
|
||||||
ToolTip.VerticalOffset="0">
|
ToolTip.VerticalOffset="0">
|
||||||
|
|
|
@ -120,31 +120,6 @@ namespace SourceGit.Views
|
||||||
e.Handled = true;
|
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)
|
private void OnOpenConventionalCommitHelper(object _, RoutedEventArgs e)
|
||||||
{
|
{
|
||||||
if (DataContext is ViewModels.WorkingCopy vm)
|
if (DataContext is ViewModels.WorkingCopy vm)
|
||||||
|
|
Loading…
Reference in a new issue