Files
BeWoPlaner/BeWo/ViewModel/Controls/AI/AiChatButtonWithHistoryViewModel.cs
2026-05-26 14:04:31 +02:00

134 lines
3.0 KiB
C#

using BS.Shared;
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 string currentValue;
public int CurrentIndex { get; set; }
public List<string> History { get; set; }
public string CurrentValue
{
get => currentValue;
set => SetProperty(ref currentValue, value, nameof(CurrentValue));
}
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) : 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);
Func<bool> canExecute = () => !string.IsNullOrEmpty(CurrentValue);
OpenAllPromptsPopupCommand = new DelegateCommand(OpenAllPromptsPopup, canExecute);
OpenConversationPopupCommand = new DelegateCommand(OpenConversationPopup, canExecute);
OpenFavoritePromptsPopupCommand = new DelegateCommand(OpenFavoritePromptsPopup, canExecute);
}
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);
}
}
}