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; namespace BeWo.ViewModel.Controls.AI { public enum AiChatButtonState { Hidden, Normal, Loading, Warning, Disabled } public class AiChatButtonViewModel : AbstractBaseVM { 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 bool IsEnabled => BeWoApp.AppSettings.ShowAI && BeWoApp.HasLoggedOnUserRight(GetUserRightType()); 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)); } } 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 AiChatButtonViewModel(AiActionType aiActionType) { ActionType = aiActionType; if (!IsEnabled) return; canExecute = true; OpenConversationPopupCommand = new DelegateCommand(OpenConversationPopup); OpenAllPromptsPopupCommand = new DelegateCommand(OpenAllPromptsPopup); OpenFavoritePromptsPopupCommand = new DelegateCommand(OpenFavoritePromptsPopup); } public void CheckContextSize() { if (!IsEnabled) { return; } ButtonState = AiChatButtonState.Loading; var context = GetAiVisualContext(); if (context != null) { 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); } } private protected void OpenConversationPopup() { var context = GetAiVisualContext(); var chat_viewmodel = VMFactory.CreateAiConversationChatViewModel(ActionType, context.References, (long)context.Primary, true); BeWoApp.MainControl.WindowService.Show(chat_viewmodel); } private protected void OpenAllPromptsPopup() { BeWoApp.MainControl.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; } } }