Files
BeWoPlaner/BeWo/ViewModel/Controls/AI/AiChatButtonWithHistoryViewModel.cs
2026-05-26 15:22:54 +02:00

136 lines
2.9 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;
namespace BeWo.ViewModel.Controls.AI
{
public class AiChatButtonWithHistoryViewModel : AiChatButtonViewModel
{
private readonly ITextProvider _provider;
private readonly string _key;
public int CurrentIndex { get; set; }
public List<string> History { get; set; }
public string CurrentValue
{
get => _provider.GetText(_key);
set => _provider.SetText(_key, value);
}
public override bool IsHistoryEnabled => true;
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 = null,
Name = "Anfügen"
},
new AiMenuItemTemplate()
{
Command = new DelegateCommand<AiConversationMessageVM>(x => ReplaceValue(x.Message)),
Icon = null,
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(IsRedoButtonVisible));
FirePropertyChanged(nameof(IsUndoButtonVisible));
}
public 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 (CurrentValue != History[CurrentIndex])
{
History.Add(CurrentValue);
CurrentIndex++;
}
History.Add(str);
CurrentIndex++;
CurrentValue = str;
}
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);
}
}
}