mirror of
https://github.com/sourcegit-scm/sourcegit
synced 2025-05-27 15:15:00 +00:00
refactor: rewrite OpenAI integration
- use `OpenAI` and `Azure.AI.OpenAI` - use `developer` role instead of `system` for OpenAI's `o1` series models - use streaming response - re-design `AIAssistant` Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
parent
cf90e51887
commit
a915708db3
12 changed files with 270 additions and 212 deletions
|
@ -10,7 +10,7 @@
|
|||
x:Name="ThisControl"
|
||||
Icon="/App.ico"
|
||||
Title="{DynamicResource Text.AIAssistant}"
|
||||
Width="400" SizeToContent="Height"
|
||||
Width="520" SizeToContent="Height"
|
||||
CanResize="False"
|
||||
WindowStartupLocation="CenterOwner">
|
||||
<Grid RowDefinitions="Auto,Auto,Auto">
|
||||
|
@ -36,18 +36,33 @@
|
|||
IsVisible="{OnPlatform True, macOS=False}"/>
|
||||
</Grid>
|
||||
|
||||
<!-- Animated Icon -->
|
||||
<v:LoadingIcon Grid.Row="1"
|
||||
Width="24" Height="24"
|
||||
Margin="0,16,0,0"/>
|
||||
<!-- AI response -->
|
||||
<v:AIResponseView Grid.Row="1"
|
||||
x:Name="TxtResponse"
|
||||
Margin="8"
|
||||
Height="320"
|
||||
BorderThickness="1"
|
||||
BorderBrush="{DynamicResource Brush.Border2}"
|
||||
Background="{DynamicResource Brush.Contents}"/>
|
||||
|
||||
<!-- Message -->
|
||||
<TextBlock Grid.Row="2"
|
||||
x:Name="ProgressMessage"
|
||||
Margin="16"
|
||||
FontSize="{Binding Source={x:Static vm:Preferences.Instance}, Path=DefaultFontSize, Converter={x:Static c:DoubleConverters.Decrease}}"
|
||||
HorizontalAlignment="Center"
|
||||
Text="Generating commit message... Please wait!"
|
||||
TextTrimming="CharacterEllipsis"/>
|
||||
<!-- Options -->
|
||||
<Border Grid.Row="2" Margin="0,0,0,8">
|
||||
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
|
||||
<v:LoadingIcon x:Name="IconInProgress" Width="14" Height="14" Margin="0,0,8,0"/>
|
||||
<Button Classes="flat"
|
||||
x:Name="BtnGenerateCommitMessage"
|
||||
Height="28"
|
||||
Margin="0,0,8,0"
|
||||
Padding="12,0"
|
||||
Content="{DynamicResource Text.AIAssistant.Use}"
|
||||
Click="OnGenerateCommitMessage"/>
|
||||
<Button Classes="flat"
|
||||
x:Name="BtnRegenerate"
|
||||
Height="28"
|
||||
Padding="12,0"
|
||||
Content="{DynamicResource Text.AIAssistant.Regen}"
|
||||
Click="OnRegen"/>
|
||||
</StackPanel>
|
||||
</Border>
|
||||
</Grid>
|
||||
</v:ChromelessWindow>
|
||||
|
|
|
@ -3,26 +3,112 @@ using System.Collections.Generic;
|
|||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Primitives;
|
||||
using Avalonia.Interactivity;
|
||||
using Avalonia.Media;
|
||||
using Avalonia.Threading;
|
||||
|
||||
using AvaloniaEdit.Document;
|
||||
using AvaloniaEdit.Editing;
|
||||
using AvaloniaEdit.TextMate;
|
||||
using AvaloniaEdit;
|
||||
|
||||
namespace SourceGit.Views
|
||||
{
|
||||
public class AIResponseView : TextEditor
|
||||
{
|
||||
protected override Type StyleKeyOverride => typeof(TextEditor);
|
||||
|
||||
public AIResponseView() : base(new TextArea(), new TextDocument())
|
||||
{
|
||||
IsReadOnly = true;
|
||||
ShowLineNumbers = false;
|
||||
WordWrap = true;
|
||||
HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled;
|
||||
VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
|
||||
|
||||
TextArea.TextView.Margin = new Thickness(4, 0);
|
||||
TextArea.TextView.Options.EnableHyperlinks = false;
|
||||
TextArea.TextView.Options.EnableEmailHyperlinks = false;
|
||||
}
|
||||
|
||||
protected override void OnLoaded(RoutedEventArgs e)
|
||||
{
|
||||
base.OnLoaded(e);
|
||||
|
||||
TextArea.TextView.ContextRequested += OnTextViewContextRequested;
|
||||
|
||||
if (_textMate == null)
|
||||
{
|
||||
_textMate = Models.TextMateHelper.CreateForEditor(this);
|
||||
Models.TextMateHelper.SetGrammarByFileName(_textMate, "README.md");
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnUnloaded(RoutedEventArgs e)
|
||||
{
|
||||
base.OnUnloaded(e);
|
||||
|
||||
TextArea.TextView.ContextRequested -= OnTextViewContextRequested;
|
||||
|
||||
if (_textMate != null)
|
||||
{
|
||||
_textMate.Dispose();
|
||||
_textMate = null;
|
||||
}
|
||||
|
||||
GC.Collect();
|
||||
}
|
||||
|
||||
private void OnTextViewContextRequested(object sender, ContextRequestedEventArgs e)
|
||||
{
|
||||
var selected = SelectedText;
|
||||
if (string.IsNullOrEmpty(selected))
|
||||
return;
|
||||
|
||||
var copy = new MenuItem() { Header = App.Text("Copy") };
|
||||
copy.Click += (_, ev) =>
|
||||
{
|
||||
App.CopyText(selected);
|
||||
ev.Handled = true;
|
||||
};
|
||||
|
||||
if (this.FindResource("Icons.Copy") is Geometry geo)
|
||||
{
|
||||
copy.Icon = new Avalonia.Controls.Shapes.Path()
|
||||
{
|
||||
Width = 10,
|
||||
Height = 10,
|
||||
Stretch = Stretch.Uniform,
|
||||
Data = geo,
|
||||
};
|
||||
}
|
||||
|
||||
var menu = new ContextMenu();
|
||||
menu.Items.Add(copy);
|
||||
menu.Open(TextArea.TextView);
|
||||
|
||||
e.Handled = true;
|
||||
}
|
||||
|
||||
private TextMate.Installation _textMate = null;
|
||||
}
|
||||
|
||||
public partial class AIAssistant : ChromelessWindow
|
||||
{
|
||||
public AIAssistant()
|
||||
{
|
||||
_cancel = new CancellationTokenSource();
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
public AIAssistant(Models.OpenAIService service, string repo, List<Models.Change> changes, Action<string> onDone)
|
||||
public AIAssistant(Models.OpenAIService service, string repo, ViewModels.WorkingCopy wc, List<Models.Change> changes)
|
||||
{
|
||||
_service = service;
|
||||
_repo = repo;
|
||||
_wc = wc;
|
||||
_changes = changes;
|
||||
_onDone = onDone;
|
||||
_cancel = new CancellationTokenSource();
|
||||
|
||||
InitializeComponent();
|
||||
}
|
||||
|
@ -30,39 +116,63 @@ namespace SourceGit.Views
|
|||
protected override void OnOpened(EventArgs e)
|
||||
{
|
||||
base.OnOpened(e);
|
||||
|
||||
if (string.IsNullOrEmpty(_repo))
|
||||
return;
|
||||
|
||||
Task.Run(() =>
|
||||
{
|
||||
var message = new Commands.GenerateCommitMessage(_service, _repo, _changes, _cancel.Token, SetDescription).Result();
|
||||
if (_cancel.IsCancellationRequested)
|
||||
return;
|
||||
|
||||
Dispatcher.UIThread.Invoke(() =>
|
||||
{
|
||||
_onDone?.Invoke(message);
|
||||
Close();
|
||||
});
|
||||
}, _cancel.Token);
|
||||
Generate();
|
||||
}
|
||||
|
||||
protected override void OnClosing(WindowClosingEventArgs e)
|
||||
{
|
||||
base.OnClosing(e);
|
||||
_cancel.Cancel();
|
||||
_cancel?.Cancel();
|
||||
}
|
||||
|
||||
private void SetDescription(string message)
|
||||
private void OnGenerateCommitMessage(object sender, RoutedEventArgs e)
|
||||
{
|
||||
Dispatcher.UIThread.Invoke(() => ProgressMessage.Text = message);
|
||||
if (_wc != null)
|
||||
_wc.CommitMessage = TxtResponse.Text;
|
||||
|
||||
Close();
|
||||
}
|
||||
|
||||
private void OnRegen(object sender, RoutedEventArgs e)
|
||||
{
|
||||
TxtResponse.Text = string.Empty;
|
||||
Generate();
|
||||
e.Handled = true;
|
||||
}
|
||||
|
||||
private void Generate()
|
||||
{
|
||||
if (_repo == null)
|
||||
return;
|
||||
|
||||
IconInProgress.IsVisible = true;
|
||||
BtnGenerateCommitMessage.IsEnabled = false;
|
||||
BtnRegenerate.IsEnabled = false;
|
||||
|
||||
_cancel = new CancellationTokenSource();
|
||||
Task.Run(() =>
|
||||
{
|
||||
new Commands.GenerateCommitMessage(_service, _repo, _changes, _cancel.Token, message =>
|
||||
{
|
||||
Dispatcher.UIThread.Invoke(() => TxtResponse.Text = message);
|
||||
}).Exec();
|
||||
|
||||
if (!_cancel.IsCancellationRequested)
|
||||
{
|
||||
Dispatcher.UIThread.Invoke(() =>
|
||||
{
|
||||
IconInProgress.IsVisible = false;
|
||||
BtnGenerateCommitMessage.IsEnabled = true;
|
||||
BtnRegenerate.IsEnabled = true;
|
||||
});
|
||||
}
|
||||
}, _cancel.Token);
|
||||
}
|
||||
|
||||
private Models.OpenAIService _service;
|
||||
private string _repo;
|
||||
private ViewModels.WorkingCopy _wc;
|
||||
private List<Models.Change> _changes;
|
||||
private Action<string> _onDone;
|
||||
private CancellationTokenSource _cancel;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue