194 lines
5.0 KiB
C#
194 lines
5.0 KiB
C#
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<AiMenuItemTemplate> AssistentCommands { get; protected set; }
|
|
|
|
public virtual bool IsHistoryEnabled => 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<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; }
|
|
|
|
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);
|
|
}
|
|
|
|
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;
|
|
|
|
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 AiConversationChatViewModel GetConversationViewModel()
|
|
{
|
|
var context = GetAiVisualContext();
|
|
|
|
var chat_viewmodel = VMFactory.CreateAiConversationChatViewModel(ActionType, context.References, (long)context.Primary, CanSendNewMessage);
|
|
|
|
return chat_viewmodel;
|
|
}
|
|
|
|
private protected void OpenConversationPopup()
|
|
{
|
|
BeWoApp.MainControl.WindowService.Show(GetConversationViewModel());
|
|
}
|
|
|
|
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 void ItemAccepted(AbstractBaseVM e)
|
|
{
|
|
if(e is AiPromptbausteinPromptVM prompt)
|
|
{
|
|
var vm = GetConversationViewModel();
|
|
vm.SendAfterLoaded = true;
|
|
vm.MessageInput = prompt.Prompt;
|
|
BeWoApp.MainControl.WindowService.Show(vm);
|
|
return;
|
|
}
|
|
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; }
|
|
}
|
|
}
|