Skip to content

Commit 917c317

Browse files
committed
refactor: use async methods instead of Task.Run in AIAssistant
Signed-off-by: leo <longshuang@msn.cn>
1 parent 316cc26 commit 917c317

File tree

4 files changed

+45
-39
lines changed

4 files changed

+45
-39
lines changed

src/AI/Agent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public Agent(Service service)
1616
_service = service;
1717
}
1818

19-
public async Task GenerateCommitMessage(string repo, string changeList, Action<string> onUpdate, CancellationToken cancellation)
19+
public async Task GenerateCommitMessageAsync(string repo, string changeList, Action<string> onUpdate, CancellationToken cancellation)
2020
{
2121
var endPoint = new Uri(_service.Server);
2222
var client = _service.Server.Contains("openai.azure.com/", StringComparison.Ordinal)

src/ViewModels/AIAssistant.cs

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
using System.Text;
44
using System.Threading;
55
using System.Threading.Tasks;
6-
76
using Avalonia.Threading;
8-
97
using CommunityToolkit.Mvvm.ComponentModel;
108

119
namespace SourceGit.ViewModels
@@ -34,16 +32,39 @@ public AIAssistant(string repo, AI.Service service, List<Models.Change> changes)
3432
foreach (var c in changes)
3533
SerializeChange(c, builder);
3634
_changeList = builder.ToString();
37-
38-
Gen();
3935
}
4036

41-
public void Regen()
37+
public async Task GenAsync()
4238
{
4339
if (_cancel is { IsCancellationRequested: false })
4440
_cancel.Cancel();
41+
_cancel = new CancellationTokenSource();
42+
43+
var agent = new AI.Agent(_service);
44+
var builder = new StringBuilder();
45+
builder.AppendLine("Asking AI to generate commit message...").AppendLine();
46+
47+
Text = builder.ToString();
48+
IsGenerating = true;
49+
50+
try
51+
{
52+
await agent.GenerateCommitMessageAsync(_repo, _changeList, message =>
53+
{
54+
builder.AppendLine(message);
55+
Dispatcher.UIThread.Post(() => Text = builder.ToString());
56+
}, _cancel.Token);
57+
}
58+
catch (OperationCanceledException)
59+
{
60+
// Do nothing
61+
}
62+
catch (Exception e)
63+
{
64+
App.RaiseException(_repo, e.Message);
65+
}
4566

46-
Gen();
67+
IsGenerating = false;
4768
}
4869

4970
public void Cancel()
@@ -72,36 +93,6 @@ private void SerializeChange(Models.Change c, StringBuilder builder)
7293
builder.Append(c.Path).AppendLine();
7394
}
7495

75-
private void Gen()
76-
{
77-
Text = string.Empty;
78-
IsGenerating = true;
79-
80-
_cancel = new CancellationTokenSource();
81-
Task.Run(async () =>
82-
{
83-
var agent = new AI.Agent(_service);
84-
var builder = new StringBuilder();
85-
builder.AppendLine("Asking AI to generate commit message...").AppendLine();
86-
Dispatcher.UIThread.Post(() => Text = builder.ToString());
87-
88-
try
89-
{
90-
await agent.GenerateCommitMessage(_repo, _changeList, message =>
91-
{
92-
builder.AppendLine(message);
93-
Dispatcher.UIThread.Post(() => Text = builder.ToString());
94-
}, _cancel.Token).ConfigureAwait(false);
95-
}
96-
catch (Exception e)
97-
{
98-
App.RaiseException(_repo, e.Message);
99-
}
100-
101-
Dispatcher.UIThread.Post(() => IsGenerating = false);
102-
}, _cancel.Token);
103-
}
104-
10596
private readonly string _repo = null;
10697
private readonly AI.Service _service = null;
10798
private readonly string _changeList = null;

src/Views/AIAssistant.axaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
Padding="12,0"
5757
Content="{DynamicResource Text.AIAssistant.Regen}"
5858
IsEnabled="{Binding !IsGenerating}"
59-
Command="{Binding Regen}"/>
59+
Click="OnRegenClicked"/>
6060
</StackPanel>
6161
</Border>
6262
</Grid>

src/Views/AIAssistant.axaml.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
32
using Avalonia;
43
using Avalonia.Controls;
54
using Avalonia.Controls.Primitives;
@@ -134,10 +133,26 @@ public AIAssistant()
134133
InitializeComponent();
135134
}
136135

136+
protected override async void OnOpened(EventArgs e)
137+
{
138+
base.OnOpened(e);
139+
140+
if (DataContext is ViewModels.AIAssistant vm)
141+
await vm.GenAsync();
142+
}
143+
137144
protected override void OnClosing(WindowClosingEventArgs e)
138145
{
139146
base.OnClosing(e);
140147
(DataContext as ViewModels.AIAssistant)?.Cancel();
141148
}
149+
150+
private async void OnRegenClicked(object sender, RoutedEventArgs e)
151+
{
152+
if (DataContext is ViewModels.AIAssistant vm)
153+
await vm.GenAsync();
154+
155+
e.Handled = true;
156+
}
142157
}
143158
}

0 commit comments

Comments
 (0)