refactor: use async methods instead of Task.Run in AIAssistant

Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
leo
2026-03-25 12:08:44 +08:00
parent 316cc26785
commit 917c3177fc
4 changed files with 45 additions and 39 deletions

View File

@@ -16,7 +16,7 @@ namespace SourceGit.AI
_service = service;
}
public async Task GenerateCommitMessage(string repo, string changeList, Action<string> onUpdate, CancellationToken cancellation)
public async Task GenerateCommitMessageAsync(string repo, string changeList, Action<string> onUpdate, CancellationToken cancellation)
{
var endPoint = new Uri(_service.Server);
var client = _service.Server.Contains("openai.azure.com/", StringComparison.Ordinal)

View File

@@ -3,9 +3,7 @@ using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Avalonia.Threading;
using CommunityToolkit.Mvvm.ComponentModel;
namespace SourceGit.ViewModels
@@ -34,16 +32,39 @@ namespace SourceGit.ViewModels
foreach (var c in changes)
SerializeChange(c, builder);
_changeList = builder.ToString();
Gen();
}
public void Regen()
public async Task GenAsync()
{
if (_cancel is { IsCancellationRequested: false })
_cancel.Cancel();
_cancel = new CancellationTokenSource();
Gen();
var agent = new AI.Agent(_service);
var builder = new StringBuilder();
builder.AppendLine("Asking AI to generate commit message...").AppendLine();
Text = builder.ToString();
IsGenerating = true;
try
{
await agent.GenerateCommitMessageAsync(_repo, _changeList, message =>
{
builder.AppendLine(message);
Dispatcher.UIThread.Post(() => Text = builder.ToString());
}, _cancel.Token);
}
catch (OperationCanceledException)
{
// Do nothing
}
catch (Exception e)
{
App.RaiseException(_repo, e.Message);
}
IsGenerating = false;
}
public void Cancel()
@@ -72,36 +93,6 @@ namespace SourceGit.ViewModels
builder.Append(c.Path).AppendLine();
}
private void Gen()
{
Text = string.Empty;
IsGenerating = true;
_cancel = new CancellationTokenSource();
Task.Run(async () =>
{
var agent = new AI.Agent(_service);
var builder = new StringBuilder();
builder.AppendLine("Asking AI to generate commit message...").AppendLine();
Dispatcher.UIThread.Post(() => Text = builder.ToString());
try
{
await agent.GenerateCommitMessage(_repo, _changeList, message =>
{
builder.AppendLine(message);
Dispatcher.UIThread.Post(() => Text = builder.ToString());
}, _cancel.Token).ConfigureAwait(false);
}
catch (Exception e)
{
App.RaiseException(_repo, e.Message);
}
Dispatcher.UIThread.Post(() => IsGenerating = false);
}, _cancel.Token);
}
private readonly string _repo = null;
private readonly AI.Service _service = null;
private readonly string _changeList = null;

View File

@@ -56,7 +56,7 @@
Padding="12,0"
Content="{DynamicResource Text.AIAssistant.Regen}"
IsEnabled="{Binding !IsGenerating}"
Command="{Binding Regen}"/>
Click="OnRegenClicked"/>
</StackPanel>
</Border>
</Grid>

View File

@@ -1,5 +1,4 @@
using System;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
@@ -134,10 +133,26 @@ namespace SourceGit.Views
InitializeComponent();
}
protected override async void OnOpened(EventArgs e)
{
base.OnOpened(e);
if (DataContext is ViewModels.AIAssistant vm)
await vm.GenAsync();
}
protected override void OnClosing(WindowClosingEventArgs e)
{
base.OnClosing(e);
(DataContext as ViewModels.AIAssistant)?.Cancel();
}
private async void OnRegenClicked(object sender, RoutedEventArgs e)
{
if (DataContext is ViewModels.AIAssistant vm)
await vm.GenAsync();
e.Handled = true;
}
}
}