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; using System.Windows; using System.Windows.Input; namespace BeWo.ViewModel.Controls.AI { public enum AiChatButtonState { Hidden, Normal, Loading, Warning, Disabled } public class AiChatButtonViewModel : AbstractBaseVM { private bool isVisible; private bool canExecute; private AiChatButtonState buttonState; public AiActionType ActionType { get; protected set; } public bool IsFavoritenOpen { get; set; } public List AssistentCommands { get; protected set; } public virtual bool IsHistoryEnabled => false; public virtual bool IsHistoryVisible => false; public bool IsEnabled => BeWoApp.AppSettings.ShowAI && BeWoApp.HasLoggedOnUserRight(GetUserRightType()); public bool IsVisible { get => isVisible; set => SetProperty(ref isVisible, value, nameof(IsVisible)); } public bool CanExecute { get => canExecute; set => SetProperty(ref canExecute, value, nameof(CanExecute)); } public AiChatButtonState ButtonState { get => buttonState; set { if (!SetProperty(ref buttonState, value, nameof(ButtonState))) return; FirePropertyChanged(nameof(IsContextSizeTooBig)); FirePropertyChanged(nameof(CanSendNewMessage)); FirePropertyChanged(nameof(CanOpenConversation)); CommandManager.InvalidateRequerySuggested(); } } public Func 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; } public bool IsContextSizeTooBig => ButtonState == AiChatButtonState.Warning; public virtual bool CanSendNewMessage => ButtonState == AiChatButtonState.Normal; public bool CanOpenConversation => ButtonState == AiChatButtonState.Normal || ButtonState == AiChatButtonState.Warning; public AiChatButtonViewModel(AiActionType aiActionType) { ActionType = aiActionType; if (!IsEnabled) return; canExecute = false; isVisible = false; OpenConversationPopupCommand = new DelegateCommand(OpenConversationPopup, () => CanOpenConversation); OpenAllPromptsPopupCommand = new DelegateCommand(OpenAllPromptsPopup, () => CanSendNewMessage); OpenFavoritePromptsPopupCommand = new DelegateCommand(OpenFavoritePromptsPopup, () => CanSendNewMessage); ButtonState = AiChatButtonState.Loading; } public void CheckContextSize() { if (!IsEnabled) return; ButtonState = AiChatButtonState.Loading; var context = GetAiVisualContext(); if (context != null) { if (context.Primary is null) { IsVisible = false; ButtonState = AiChatButtonState.Disabled; return; } IsVisible = true; ServiceFacade.DoAiEnhancedServiceAsnyc(x => x.CheckContextSize(ActionType, context), isValid => { if (isValid) ButtonState = AiChatButtonState.Normal; else ButtonState = AiChatButtonState.Warning; }, (exp) => ButtonState = AiChatButtonState.Disabled); } } public void ReloadData(bool value, Action> initPromptbausteine) { if (!IsEnabled) return; var actionType = ActionType; ServiceFacade.DoAiEnhancedServiceAsnyc(x => x.GetAiPromptbausteinFolder(actionType), folders => initPromptbausteine(actionType, folders), null, true); } public void ItemAccepted(AbstractBaseVM e) { if (e is AiPromptbausteinPromptVM prompt) { var vm = GetConversationViewModel(); vm.InputItem = prompt; BeWoApp.WindowService.Show(vm); return; } else if (e is AiPromptbausteinRoutineVM routine) { var vm = GetConversationViewModel(); vm.InputItem = routine; BeWoApp.WindowService.Show(vm); return; } throw new NotImplementedException(); } private protected AiConversationChatViewModel GetConversationViewModel() { var context = GetAiVisualContext(); var chat_viewmodel = VMFactory.CreateAiConversationChatViewModel(ActionType, context, AssistentCommands, CanSendNewMessage); return chat_viewmodel; } private protected void OpenConversationPopup() { BeWoApp.WindowService.Show(GetConversationViewModel()); } private protected void OpenAllPromptsPopup() { BeWoApp.WindowService.Show(AllPromptsViewModel); } private protected void OpenFavoritePromptsPopup() { IsFavoritenOpen = true; FirePropertyChanged(nameof(IsFavoritenOpen)); } 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"); } } public class AiMenuItemTemplate { public string Icon { get; set; } public string Name { get; set; } public DelegateCommand Command { get; set; } } }