2024-10-22 23:05:40 -07:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2024-09-11 03:22:05 -07:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
using Avalonia.Controls;
|
|
|
|
using Avalonia.Threading;
|
|
|
|
|
|
|
|
namespace SourceGit.Views
|
|
|
|
{
|
|
|
|
public partial class AIAssistant : ChromelessWindow
|
|
|
|
{
|
|
|
|
public AIAssistant()
|
|
|
|
{
|
|
|
|
_cancel = new CancellationTokenSource();
|
|
|
|
InitializeComponent();
|
|
|
|
}
|
|
|
|
|
2024-10-27 20:00:11 -07:00
|
|
|
public AIAssistant(Models.OpenAIService service, string repo, List<Models.Change> changes, Action<string> onDone)
|
2024-09-11 03:22:05 -07:00
|
|
|
{
|
2024-10-27 20:00:11 -07:00
|
|
|
_service = service;
|
2024-10-22 23:05:40 -07:00
|
|
|
_repo = repo;
|
|
|
|
_changes = changes;
|
|
|
|
_onDone = onDone;
|
|
|
|
_cancel = new CancellationTokenSource();
|
2024-10-27 20:00:11 -07:00
|
|
|
|
2024-10-22 23:05:40 -07:00
|
|
|
InitializeComponent();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnOpened(EventArgs e)
|
|
|
|
{
|
|
|
|
base.OnOpened(e);
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(_repo))
|
|
|
|
return;
|
|
|
|
|
|
|
|
Task.Run(() =>
|
2024-09-11 03:22:05 -07:00
|
|
|
{
|
2024-10-27 20:00:11 -07:00
|
|
|
var message = new Commands.GenerateCommitMessage(_service, _repo, _changes, _cancel.Token, SetDescription).Result();
|
2024-10-22 23:05:40 -07:00
|
|
|
if (_cancel.IsCancellationRequested)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Dispatcher.UIThread.Invoke(() =>
|
2024-09-11 03:22:05 -07:00
|
|
|
{
|
2024-10-22 23:05:40 -07:00
|
|
|
_onDone?.Invoke(message);
|
|
|
|
Close();
|
|
|
|
});
|
|
|
|
}, _cancel.Token);
|
2024-09-11 03:22:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnClosing(WindowClosingEventArgs e)
|
|
|
|
{
|
|
|
|
base.OnClosing(e);
|
|
|
|
_cancel.Cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void SetDescription(string message)
|
|
|
|
{
|
2024-10-22 23:05:40 -07:00
|
|
|
Dispatcher.UIThread.Invoke(() => ProgressMessage.Text = message);
|
2024-09-11 03:22:05 -07:00
|
|
|
}
|
|
|
|
|
2024-10-27 20:00:11 -07:00
|
|
|
private Models.OpenAIService _service;
|
2024-10-22 23:05:40 -07:00
|
|
|
private string _repo;
|
|
|
|
private List<Models.Change> _changes;
|
|
|
|
private Action<string> _onDone;
|
2024-09-11 03:22:05 -07:00
|
|
|
private CancellationTokenSource _cancel;
|
|
|
|
}
|
|
|
|
}
|