Files
BeWoPlaner/Service/ServiceImplementations/OperationsEnhancedServiceImp.cs
2025-04-08 12:58:12 +02:00

138 lines
4.1 KiB
C#

using BeWo.Data.Access;
using BeWo.Data.Entities;
using BeWo.Service.DCEntityMapper;
using BeWo.Service.ServiceContracts;
using BeWo.Service.ServiceProxy;
using BeWo.Service.ServiceUtils;
using BS.Shared.DataContracts;
using BS.Shared.DataContracts.Feature.AI;
using BS.Shared.DataContracts.MikePHPContracts;
using DevExpress.Entity.Model.Metadata;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace BeWo.Service.ServiceImplementations
{
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Single)]
public class OperationsEnhancedServiceImp : IOperationsEnhancedService
{
public IEnumerable<AiModelDC> GetAiModels()
{
var old_models = DAOFactory.GenericDAO.GetAll<AiModel>();
var new_models = ServiceFacade.OpenWebUI.GetAiModelle();
var unknown_models = new_models.ToList();
foreach (var old_model in old_models)
{
bool found = false;
foreach (var new_model in new_models)
{
if(old_model.ModelName == new_model.ModelName)
{
if (old_model.IsActive != BS.Shared.ActivationTypeId.Active)
DAOFactory.GenericDAO.SetActivationType(old_model, BS.Shared.ActivationTypeId.Active);
found = true;
unknown_models.Remove(new_model);
break;
}
}
if (!found)
DAOFactory.GenericDAO.Deactivate(old_model);
}
if (unknown_models.Count != 0)
{
foreach (var unknown_model in unknown_models)
{
DAOFactory.GenericDAO.Insert(unknown_model);
}
}
var models = DAOFactory.GenericDAO.GetAllActive<AiModel>();
return MapperFactory.AiModel.MapToNewDCs(models);
}
public IEnumerable<AiConversationDC> GetAiConversations(int uicontext)
{
// SearchDAO
var convs = DAOFactory.GenericDAO.GetAll<AiConversation>();
var filter = convs.Where(x => x.UIContext == uicontext);
var dcs = MapperFactory.AiConversation.MapToNewDCs(filter);
return dcs;
}
public AiConversationDC CreateAiConversation(int uicontext, long model, List<Tuple<long, long>> bewoobjects)
{
var system = AiSystemBuilder.CreateSystemInstructions(uicontext, bewoobjects);
var conv = new AiConversation();
conv.Created = DateTime.Now;
conv.ContextBeWoObjects = bewoobjects?.Select(x => new BeWoEntityReference() { Oid = x.Item1, Tid = x.Item2}).ToList();
conv.Messages = new List<AiConversationMessage>();
conv.Modell = DAOFactory.GenericDAO.LoadByID<AiModel>(model);
conv.UIContext = uicontext;
conv.Updated = DateTime.Now;
var msg = new AiConversationMessage();
msg.Message = system;
msg.AiConversation = conv;
msg.Role = BS.Shared.AiConversationMessageRole.System;
msg.Created = DateTime.Now;
conv.Messages.Add(msg);
DAOFactory.GenericDAO.Insert(conv);
var dc = MapperFactory.AiConversation.MapToNewDC(conv);
return dc;
}
public AiConversationMessageDC[] SendNewMessage(long conversation, string message)
{
var conv = DAOFactory.GenericDAO.LoadByID<AiConversation>(conversation);
var user_msg = new AiConversationMessage();
user_msg.Message = message;
user_msg.AiConversation = conv;
user_msg.Role = BS.Shared.AiConversationMessageRole.User;
user_msg.Created = DateTime.Now;
conv.Messages.Add(user_msg);
DAOFactory.GenericDAO.Update(conv);
var conv_dc = MapperFactory.AiConversation.MapToNewDC(conv);
var user_msg_dc = MapperFactory.AiConversationMessage.MapToNewDC(user_msg);
var result = ServiceFacade.OpenWebUI.SendAiMessageRequest(conv_dc, out var model, out var duration);
var assi_msg = new AiConversationMessage();
assi_msg.Message = result;
assi_msg.AiConversation = conv;
assi_msg.Role = BS.Shared.AiConversationMessageRole.Assistent;
assi_msg.Created = DateTime.Now;
assi_msg.Duration = duration ?? 0;
assi_msg.ModelName = model;
conv.Messages.Add(assi_msg);
DAOFactory.GenericDAO.Update(conv);
var assi_msg_dc = MapperFactory.AiConversationMessage.MapToNewDC(assi_msg);
return new AiConversationMessageDC[] { user_msg_dc, assi_msg_dc };
}
}
}