-
Notifications
You must be signed in to change notification settings - Fork 204
/
Copy pathAIAssistant.cs
80 lines (66 loc) · 2.08 KB
/
AIAssistant.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Avalonia.Threading;
using CommunityToolkit.Mvvm.ComponentModel;
namespace SourceGit.ViewModels
{
public class AIAssistant : ObservableObject
{
public bool IsGenerating
{
get => _isGenerating;
private set => SetProperty(ref _isGenerating, value);
}
public string Text
{
get => _text;
private set => SetProperty(ref _text, value);
}
public AIAssistant(Repository repo, Models.OpenAIService service, List<Models.Change> changes, Action<string> onApply)
{
_repo = repo;
_service = service;
_changes = changes;
_onApply = onApply;
_cancel = new CancellationTokenSource();
Gen();
}
public void Regen()
{
if (_cancel is { IsCancellationRequested: false })
_cancel.Cancel();
Gen();
}
public void Apply()
{
_onApply?.Invoke(Text);
}
public void Cancel()
{
_cancel?.Cancel();
}
private void Gen()
{
Text = string.Empty;
IsGenerating = true;
_cancel = new CancellationTokenSource();
Task.Run(() =>
{
new Commands.GenerateCommitMessage(_service, _repo.FullPath, _changes, _cancel.Token, message =>
{
Dispatcher.UIThread.Invoke(() => Text = message);
}).Exec();
Dispatcher.UIThread.Invoke(() => IsGenerating = false);
}, _cancel.Token);
}
private readonly Repository _repo = null;
private Models.OpenAIService _service = null;
private List<Models.Change> _changes = null;
private Action<string> _onApply = null;
private CancellationTokenSource _cancel = null;
private bool _isGenerating = false;
private string _text = string.Empty;
}
}