156 lines
3.5 KiB
C#
156 lines
3.5 KiB
C#
using BS.Shared;
|
|
using BS.Shared.Interface;
|
|
using DevExpress.Mvvm;
|
|
using DevExpress.XtraPrinting;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows;
|
|
using System.Windows.Media;
|
|
|
|
namespace BeWo.ViewModel.Controls.AI
|
|
{
|
|
public class AiChatButtonWithHistoryViewModel : AiChatButtonViewModel
|
|
{
|
|
private readonly ITextProvider _provider;
|
|
private readonly string _key;
|
|
private int _currentIndex;
|
|
|
|
public List<string> History { get; set; }
|
|
|
|
public string CurrentValue
|
|
{
|
|
get => _provider.GetText(_key);
|
|
set => _provider.SetText(_key, value);
|
|
}
|
|
|
|
public int CurrentIndex
|
|
{
|
|
get => _currentIndex;
|
|
set {
|
|
if(SetProperty(ref _currentIndex, value, nameof(CurrentIndex)))
|
|
{
|
|
FirePropertyChanged(nameof(IsUndoButtonVisible));
|
|
FirePropertyChanged(nameof(IsRedoButtonVisible));
|
|
}
|
|
}
|
|
}
|
|
|
|
public override bool IsHistoryEnabled => true;
|
|
public override bool IsHistoryVisible => History.Count > 1;
|
|
|
|
public bool IsUndoButtonVisible => CurrentIndex > 0;
|
|
public bool IsRedoButtonVisible => CurrentIndex < History.Count - 1;
|
|
|
|
public DelegateCommand UndoAiActionCommand { get; protected set; }
|
|
public DelegateCommand RedoAiActionCommand { get; protected set; }
|
|
|
|
public AiChatButtonWithHistoryViewModel(AiActionType aiActionType, ITextProvider provider, string key) : base(aiActionType)
|
|
{
|
|
History = new List<string>();
|
|
CurrentIndex = 0;
|
|
AssistentCommands = new List<AiMenuItemTemplate>()
|
|
{
|
|
new AiMenuItemTemplate()
|
|
{
|
|
Command = new DelegateCommand<AiConversationMessageVM>(x => AddValue(x.Message)),
|
|
Icon = Application.Current.FindResource("Phosphor.RowsPlusTop") as Geometry,
|
|
Name = "Anfügen"
|
|
},
|
|
new AiMenuItemTemplate()
|
|
{
|
|
Command = new DelegateCommand<AiConversationMessageVM>(x => ReplaceValue(x.Message)),
|
|
Icon = Application.Current.FindResource("Phosphor.Swap") as Geometry,
|
|
Name = "Ersetzen"
|
|
},
|
|
};
|
|
UndoAiActionCommand = new DelegateCommand(UndoAiAction);
|
|
RedoAiActionCommand = new DelegateCommand(RedoAiAction);
|
|
|
|
_provider = provider;
|
|
_key = key;
|
|
}
|
|
|
|
public override bool CanSendNewMessage => base.CanSendNewMessage && !string.IsNullOrEmpty(CurrentValue);
|
|
|
|
public void ClearHistory()
|
|
{
|
|
History.Clear();
|
|
History.Add(CurrentValue);
|
|
CurrentIndex = 0;
|
|
|
|
FirePropertyChanged(nameof(IsHistoryVisible));
|
|
}
|
|
|
|
public void UndoAiAction()
|
|
{
|
|
if (CurrentIndex <= 0)
|
|
return;
|
|
|
|
CurrentIndex--;
|
|
CurrentValue = History[CurrentIndex];
|
|
}
|
|
|
|
public void RedoAiAction()
|
|
{
|
|
if (CurrentIndex >= History.Count - 1)
|
|
return;
|
|
|
|
CurrentIndex++;
|
|
CurrentValue = History[CurrentIndex];
|
|
}
|
|
|
|
public void AddValue(string s)
|
|
{
|
|
var sb = new StringBuilder();
|
|
sb.AppendLine("Antwort:");
|
|
sb.AppendLine(s);
|
|
|
|
sb.AppendLine();
|
|
sb.AppendLine("Original:");
|
|
sb.AppendLine(CurrentValue);
|
|
|
|
var str = sb.ToString();
|
|
|
|
accept(str);
|
|
}
|
|
|
|
public void ReplaceValue(string str)
|
|
{
|
|
accept(str);
|
|
}
|
|
|
|
private void accept(string str)
|
|
{
|
|
var idx = CurrentIndex;
|
|
var count = History.Count;
|
|
|
|
var last_idx = count - 1;
|
|
|
|
if (last_idx > idx)
|
|
{
|
|
History.RemoveRange(idx + 1, last_idx - idx);
|
|
}
|
|
|
|
if (History.Count == 0)
|
|
{
|
|
History.Add(CurrentValue);
|
|
}
|
|
else if (CurrentValue != History[CurrentIndex])
|
|
{
|
|
History.Add(CurrentValue);
|
|
CurrentIndex++;
|
|
}
|
|
|
|
History.Add(str);
|
|
CurrentIndex++;
|
|
|
|
CurrentValue = str;
|
|
|
|
FirePropertyChanged(nameof(IsHistoryVisible));
|
|
}
|
|
}
|
|
}
|