144 lines
3.8 KiB
C#
144 lines
3.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.Linq;
|
|
using System.Runtime.Serialization;
|
|
using BeWo.ServiceProxy;
|
|
using BeWo.ViewModel.ListViewModel;
|
|
using BS.Shared;
|
|
using BS.Shared.DataContracts;
|
|
using BS.Shared.DataContracts.Feature.AI;
|
|
using DevExpress.Mvvm;
|
|
using DevExpress.Xpf.Core.Native;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace BeWo.ViewModel
|
|
{
|
|
[JsonObject(MemberSerialization.OptIn)]
|
|
public class AiConversationVM : AbstractDCMapperVM<AiConversationDC>
|
|
{
|
|
private bool _IsEditing = false;
|
|
private string _Displayname;
|
|
private DateTime _Updated;
|
|
private AiConversationMessageListVM _Messages;
|
|
|
|
public AiConversationVM() : this(new AiConversationDC()) { }
|
|
public AiConversationVM(AiConversationDC dc) : base(dc, dc.Oid is null)
|
|
{
|
|
|
|
}
|
|
|
|
public string Json
|
|
{
|
|
get => JsonConvert.SerializeObject(this, Formatting.Indented);
|
|
}
|
|
|
|
[JsonProperty]
|
|
public string Displayname
|
|
{
|
|
get { return _Displayname; }
|
|
set => SetProperty(ref _Displayname, value, nameof(Displayname), () => DataContract.Displayname);
|
|
}
|
|
|
|
[JsonProperty]
|
|
public DateTime Updated
|
|
{
|
|
get { return _Updated; }
|
|
set => SetProperty(ref _Updated, value, nameof(Updated), () => DataContract.Updated);
|
|
}
|
|
|
|
public AiConversationMessageListVM Messages
|
|
{
|
|
get { return _Messages ?? (_Messages = new AiConversationMessageListVM()); }
|
|
set
|
|
{
|
|
if (!AreDifferent(_Messages, value))
|
|
return;
|
|
|
|
_Messages = value;
|
|
StoreDirtyInformation(AreDifferent(DataContract.Messages, value), nameof(Messages));
|
|
FirePropertyChanged(nameof(Messages));
|
|
}
|
|
}
|
|
|
|
public bool IsEditing
|
|
{
|
|
get => _IsEditing;
|
|
set => SetProperty(ref _IsEditing, value, nameof(IsEditing));
|
|
}
|
|
|
|
public long Oid => DataContract.Oid.Value;
|
|
public long Version => DataContract.Version.Value;
|
|
public DateTime Created => DataContract.Created;
|
|
public AiModelDC Modell => DataContract.Modell;
|
|
public string Context => DataContract.Context;
|
|
public Dictionary<TableID, long[]> ContextBeWoObjects => DataContract.ContextBeWoObjects;
|
|
public AiDataContextType Context_Type => DataContract.ContextType;
|
|
public UIContext UIContext => (UIContext)DataContract.UIContext;
|
|
|
|
protected override void InitByDataContract(AiConversationDC pDataContract)
|
|
{
|
|
_Displayname = pDataContract.Displayname;
|
|
_Updated = pDataContract.Updated;
|
|
|
|
if (pDataContract.Messages is object)
|
|
_Messages = getFilter(pDataContract.Messages);
|
|
}
|
|
|
|
protected override AiConversationDC MapToDataContract(AiConversationDC pDataContract, bool doCommit)
|
|
{
|
|
pDataContract.Displayname = _Displayname;
|
|
pDataContract.Updated = _Updated;
|
|
pDataContract.Messages = _Messages?.CopyToDCList(doCommit);
|
|
|
|
return pDataContract;
|
|
}
|
|
|
|
public override void UpdateByDataContract(AiConversationDC pDataContract)
|
|
{
|
|
DataContract = pDataContract;
|
|
|
|
Displayname = pDataContract.Displayname;
|
|
Updated = pDataContract.Updated;
|
|
|
|
if (pDataContract.Messages is object)
|
|
Messages = getFilter(pDataContract.Messages);
|
|
}
|
|
|
|
private AiConversationMessageListVM getFilter(List<AiConversationMessageDC> messages)
|
|
{
|
|
if(!BeWoApp.IsInDeveloperMode && messages is List<AiConversationMessageDC> && messages.Any())
|
|
{
|
|
messages.RemoveAll(m => m.Role == AiConversationMessageRole.System);
|
|
}
|
|
return new AiConversationMessageListVM(messages);
|
|
}
|
|
|
|
public void ResetDisplayname()
|
|
{
|
|
Displayname = DataContract.Displayname;
|
|
}
|
|
|
|
public void SaveDisplayname()
|
|
{
|
|
if (!IsEditing)
|
|
return;
|
|
|
|
IsEditing = false;
|
|
|
|
if (DataContract.Displayname == Displayname)
|
|
return;
|
|
|
|
ServiceFacade.DoAiEnhancedServiceAsnyc(x => x.UpdateAiConversationDisplayname(Oid, Version, Displayname),
|
|
callback =>
|
|
{
|
|
DataContract.Version = callback;
|
|
DataContract.Displayname = Displayname;
|
|
},
|
|
(ex) =>
|
|
{
|
|
ResetDisplayname();
|
|
});
|
|
}
|
|
}
|
|
} |