Files
BeWoPlaner/BeWo/ViewModel/Controls/AI/AiChatButtonViewModel.cs

193 lines
5.0 KiB
C#
Raw Normal View History

2026-05-26 12:30:29 +02:00
using BeWo.ServiceProxy;
using BeWo.View.Controls.AI;
using BeWo.View.Detail.AI;
using BeWo.ViewModel.View.AI;
using BS.Shared;
using BS.Shared.DataContracts.Feature.AI;
using DevExpress.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
2026-05-26 12:30:29 +02:00
using System.Windows;
2026-05-26 14:57:10 +02:00
using System.Windows.Input;
namespace BeWo.ViewModel.Controls.AI
{
2026-05-26 12:30:29 +02:00
public enum AiChatButtonState
{
Hidden,
Normal,
Loading,
Warning,
Disabled
}
public class AiChatButtonViewModel : AbstractBaseVM
{
2026-05-26 14:57:10 +02:00
private bool isVisible;
private bool canExecute;
2026-05-26 12:30:29 +02:00
private AiChatButtonState buttonState;
2026-05-26 12:30:29 +02:00
public AiActionType ActionType { get; protected set; }
public bool IsFavoritenOpen { get; set; }
public List<AiMenuItemTemplate> AssistentCommands { get; protected set; }
public virtual bool IsHistoryEnabled => false;
2026-05-26 12:30:29 +02:00
public bool IsEnabled => BeWoApp.AppSettings.ShowAI && BeWoApp.HasLoggedOnUserRight(GetUserRightType());
2026-05-26 14:57:10 +02:00
public bool IsVisible
{
get => isVisible;
set => SetProperty(ref isVisible, value, nameof(IsVisible));
}
public bool CanExecute
{
get => canExecute;
set => SetProperty(ref canExecute, value, nameof(CanExecute));
}
2026-05-26 14:57:10 +02:00
2026-05-26 12:30:29 +02:00
public AiChatButtonState ButtonState
{
get => buttonState;
2026-05-26 14:04:31 +02:00
set {
if (!SetProperty(ref buttonState, value, nameof(ButtonState)))
return;
FirePropertyChanged(nameof(IsContextSizeTooBig));
2026-05-26 14:57:10 +02:00
FirePropertyChanged(nameof(CanSendNewMessage));
FirePropertyChanged(nameof(CanOpenConversation));
CommandManager.InvalidateRequerySuggested();
2026-05-26 14:04:31 +02:00
}
2026-05-26 12:30:29 +02:00
}
2026-05-26 12:30:29 +02:00
public Func<AiViewContextDC> GetAiVisualContext { get; set; }
public AiPromptbausteinSelectionComplexViewModel AllPromptsViewModel { get; set; }
public DelegateCommand OpenConversationPopupCommand { get; protected set; }
public DelegateCommand OpenAllPromptsPopupCommand { get; protected set; }
public DelegateCommand OpenFavoritePromptsPopupCommand { get; protected set; }
2026-05-26 14:04:31 +02:00
public bool IsContextSizeTooBig => ButtonState == AiChatButtonState.Warning;
2026-05-26 14:57:10 +02:00
public virtual bool CanSendNewMessage => ButtonState == AiChatButtonState.Normal;
public bool CanOpenConversation => ButtonState == AiChatButtonState.Normal || ButtonState == AiChatButtonState.Warning;
2026-05-26 14:04:31 +02:00
2026-05-26 12:30:29 +02:00
public AiChatButtonViewModel(AiActionType aiActionType)
{
2026-05-26 12:30:29 +02:00
ActionType = aiActionType;
if (!IsEnabled)
return;
2026-05-26 14:57:10 +02:00
canExecute = false;
isVisible = false;
2026-05-26 14:57:10 +02:00
OpenConversationPopupCommand = new DelegateCommand(OpenConversationPopup, () => CanOpenConversation);
OpenAllPromptsPopupCommand = new DelegateCommand(OpenAllPromptsPopup, () => CanSendNewMessage);
OpenFavoritePromptsPopupCommand = new DelegateCommand(OpenFavoritePromptsPopup, () => CanSendNewMessage);
}
2026-05-26 12:30:29 +02:00
public void CheckContextSize()
{
2026-05-26 12:30:29 +02:00
if (!IsEnabled)
{
return;
}
ButtonState = AiChatButtonState.Loading;
var context = GetAiVisualContext();
if (context != null)
{
2026-05-26 14:57:10 +02:00
if(context.Primary is null)
{
IsVisible = false;
ButtonState = AiChatButtonState.Disabled;
return;
}
IsVisible = true;
2026-05-26 12:30:29 +02:00
var context2 = context.References.ToDictionary(kv => kv.Key, kv => kv.Value.ToList());
ServiceFacade.DoAiEnhancedServiceAsnyc(x => x.CheckContextSize(ActionType, context2, null),
isValid =>
{
if (isValid)
ButtonState = AiChatButtonState.Normal;
else
ButtonState = AiChatButtonState.Warning;
},
(exp) => ButtonState = AiChatButtonState.Disabled);
}
}
2026-05-26 12:30:29 +02:00
2026-05-26 14:57:10 +02:00
private protected AiConversationChatViewModel GetConversationViewModel()
2026-05-26 12:30:29 +02:00
{
var context = GetAiVisualContext();
2026-05-26 14:57:10 +02:00
var chat_viewmodel = VMFactory.CreateAiConversationChatViewModel(ActionType, context.References, (long)context.Primary, CanSendNewMessage);
2026-05-26 12:30:29 +02:00
2026-05-26 14:57:10 +02:00
return chat_viewmodel;
}
private protected void OpenConversationPopup()
{
BeWoApp.MainControl.WindowService.Show(GetConversationViewModel());
2026-05-26 12:30:29 +02:00
}
2026-05-26 14:04:31 +02:00
private protected void OpenAllPromptsPopup()
{
BeWoApp.MainControl.WindowService.Show(AllPromptsViewModel);
}
2026-05-26 12:30:29 +02:00
2026-05-26 14:04:31 +02:00
private protected void OpenFavoritePromptsPopup()
{
IsFavoritenOpen = true;
FirePropertyChanged(nameof(IsFavoritenOpen));
}
2026-05-26 12:30:29 +02:00
private UserRightType GetUserRightType()
{
switch (ActionType)
{
case AiActionType.ServiceRecord:
break;
case AiActionType.ServiceRecordConversation:
return UserRightType.AiModuleServiceRecordChat;
case AiActionType.ServiceRecordDocumentation:
return UserRightType.AiModuleServiceRecordDocu;
default:
break;
}
throw new InvalidOperationException($"UserRightType '{ActionType}' not implemented");
}
2026-05-26 14:57:10 +02:00
public void ItemAccepted(AbstractBaseVM e)
{
if(e is AiPromptbausteinPromptVM prompt)
{
var vm = GetConversationViewModel();
vm.SendAfterLoaded = true;
vm.MessageInput = prompt.Prompt;
BeWoApp.MainControl.WindowService.Show(vm);
}
else if(e is AiPromptbausteinRoutineVM routine)
{
}
throw new NotImplementedException();
}
}
public class AiMenuItemTemplate
{
public string Icon { get; set; }
public string Name { get; set; }
public DelegateCommand<AiConversationMessageVM> Command { get; set; }
}
}