Skip to content

Commit d21a8f2

Browse files
committed
refactor: rewrite OpenAI integration
Signed-off-by: leo <[email protected]>
1 parent f6e1e65 commit d21a8f2

File tree

5 files changed

+52
-53
lines changed

5 files changed

+52
-53
lines changed

src/ViewModels/WorkingCopy.cs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@ public ConflictContext(string repo, Models.Change change)
3333

3434
public class WorkingCopy : ObservableObject
3535
{
36-
public string RepoPath
37-
{
38-
get => _repo.FullPath;
39-
}
40-
4136
public bool IncludeUntracked
4237
{
4338
get => _repo.IncludeUntracked;
@@ -408,6 +403,25 @@ public void Discard(List<Models.Change> changes)
408403
}
409404
}
410405

406+
public void GenerateCommitMessageByAI()
407+
{
408+
if (!Models.OpenAI.IsValid)
409+
{
410+
App.RaiseException(_repo.FullPath, "Bad configuration for OpenAI");
411+
return;
412+
}
413+
414+
if (_staged is { Count: > 0 })
415+
{
416+
var dialog = new Views.AIAssistant(_repo.FullPath, _staged, generated => CommitMessage = generated);
417+
App.OpenDialog(dialog);
418+
}
419+
else
420+
{
421+
App.RaiseException(_repo.FullPath, "No files added to commit!");
422+
}
423+
}
424+
411425
public void Commit()
412426
{
413427
DoCommit(false, false, false);

src/Views/AIAssistant.axaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
xmlns:c="using:SourceGit.Converters"
88
mc:Ignorable="d" d:DesignWidth="400" d:DesignHeight="120"
99
x:Class="SourceGit.Views.AIAssistant"
10-
x:DataType="vm:WorkingCopy"
1110
x:Name="ThisControl"
1211
Icon="/App.ico"
1312
Title="{DynamicResource Text.AIAssistant}"
@@ -55,6 +54,7 @@
5554
Margin="16"
5655
FontSize="{Binding Source={x:Static vm:Preference.Instance}, Path=DefaultFontSize, Converter={x:Static c:DoubleConverters.Decrease}}"
5756
HorizontalAlignment="Center"
57+
Text="Generating commit message... Please wait!"
5858
TextTrimming="CharacterEllipsis"/>
5959
</Grid>
6060
</v:ChromelessWindow>

src/Views/AIAssistant.axaml.cs

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System;
2+
using System.Collections.Generic;
13
using System.Threading;
24
using System.Threading.Tasks;
35

@@ -13,28 +15,36 @@ public AIAssistant()
1315
{
1416
_cancel = new CancellationTokenSource();
1517
InitializeComponent();
16-
ProgressMessage.Text = "Generating commit message... Please wait!";
1718
}
1819

19-
public void GenerateCommitMessage()
20+
public AIAssistant(string repo, List<Models.Change> changes, Action<string> onDone)
2021
{
21-
if (DataContext is ViewModels.WorkingCopy vm)
22+
_repo = repo;
23+
_changes = changes;
24+
_onDone = onDone;
25+
_cancel = new CancellationTokenSource();
26+
InitializeComponent();
27+
}
28+
29+
protected override void OnOpened(EventArgs e)
30+
{
31+
base.OnOpened(e);
32+
33+
if (string.IsNullOrEmpty(_repo))
34+
return;
35+
36+
Task.Run(() =>
2237
{
23-
Task.Run(() =>
38+
var message = new Commands.GenerateCommitMessage(_repo, _changes, _cancel.Token, SetDescription).Result();
39+
if (_cancel.IsCancellationRequested)
40+
return;
41+
42+
Dispatcher.UIThread.Invoke(() =>
2443
{
25-
var message = new Commands.GenerateCommitMessage(vm.RepoPath, vm.Staged, _cancel.Token, SetDescription).Result();
26-
if (_cancel.IsCancellationRequested)
27-
return;
28-
29-
Dispatcher.UIThread.Invoke(() =>
30-
{
31-
if (DataContext is ViewModels.WorkingCopy wc)
32-
wc.CommitMessage = message;
33-
34-
Close();
35-
});
36-
}, _cancel.Token);
37-
}
44+
_onDone?.Invoke(message);
45+
Close();
46+
});
47+
}, _cancel.Token);
3848
}
3949

4050
protected override void OnClosing(WindowClosingEventArgs e)
@@ -50,12 +60,12 @@ private void BeginMoveWindow(object _, PointerPressedEventArgs e)
5060

5161
private void SetDescription(string message)
5262
{
53-
Dispatcher.UIThread.Invoke(() =>
54-
{
55-
ProgressMessage.Text = message;
56-
});
63+
Dispatcher.UIThread.Invoke(() => ProgressMessage.Text = message);
5764
}
5865

66+
private string _repo;
67+
private List<Models.Change> _changes;
68+
private Action<string> _onDone;
5969
private CancellationTokenSource _cancel;
6070
}
6171
}

src/Views/WorkingCopy.axaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@
199199
<Button Grid.Column="1"
200200
Classes="icon_button"
201201
Margin="4,2,0,0"
202-
Click="OnOpenAIAssist"
202+
Command="{Binding GenerateCommitMessageByAI}"
203203
ToolTip.Tip="{DynamicResource Text.AIAssistant.Tip}"
204204
ToolTip.Placement="Top"
205205
ToolTip.VerticalOffset="0">

src/Views/WorkingCopy.axaml.cs

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -120,31 +120,6 @@ private void OnUnstageSelectedButtonClicked(object _, RoutedEventArgs e)
120120
e.Handled = true;
121121
}
122122

123-
private void OnOpenAIAssist(object _, RoutedEventArgs e)
124-
{
125-
if (!Models.OpenAI.IsValid)
126-
{
127-
App.RaiseException(null, "Bad configuration for OpenAI");
128-
return;
129-
}
130-
131-
if (DataContext is ViewModels.WorkingCopy vm)
132-
{
133-
if (vm.Staged is { Count: > 0 })
134-
{
135-
var dialog = new AIAssistant() { DataContext = vm };
136-
dialog.GenerateCommitMessage();
137-
App.OpenDialog(dialog);
138-
}
139-
else
140-
{
141-
App.RaiseException(null, "No files added to commit!");
142-
}
143-
}
144-
145-
e.Handled = true;
146-
}
147-
148123
private void OnOpenConventionalCommitHelper(object _, RoutedEventArgs e)
149124
{
150125
if (DataContext is ViewModels.WorkingCopy vm)

0 commit comments

Comments
 (0)