using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using xAiApi.AI.Extensions;
using xAiApi.AI.Interfaces;
using xAiApi.AI.Models.Entities;
using xAiModels.Models;
using xCommons.Extensions;
using xExceptions.Constants;
using xIdentityModels.Models;
using xIdentityService.Extensions;
using xIdentityService.Interfaces;
using xModels.Dtos;
namespace xAiApi.AI.Services
{
public class XAiProvider : IXAiProvider
{
//
public IXAIService AiService { get; }
private readonly IXIdentityProvider identityProvider;
public XAiProvider(
IXAIService aiService,
IXIdentityProvider identityProvider
)
{
//
AiService = aiService;
this.identityProvider = identityProvider;
}
//
#region Helpers ...
///
/// Converts Entity to Dto ...
///
///
///
///
public async Task ToDto(XAiProject entity)
{
//
XAiProjectDto result = null;
//
if (!entity.IsNullOrDefault())
{
//
result = entity.ToDto();
if (!entity.OwnerId.IsNullOrEmpty())
{
result.Owner = await GetPerson(entity.OwnerId);
}
//
result.Conversations = await ToDtos(entity.Conversations);
}
//
return result;
}
///
/// Converts Entity to Dto ...
///
///
///
///
public async Task ToDto(XAiMessage entity)
{
//
XAiMessageDto result = null;
//
if (!entity.IsNullOrDefault())
{
//
result = entity.ToDto();
if (!entity.OwnerId.IsNullOrEmpty())
{
result.Owner = await GetPerson(entity.OwnerId);
}
//
result.Conversation = await ToDto(entity.Conversation);
}
//
return result;
}
///
/// Converts Entity to Dto ...
///
///
///
///
public async Task ToDto(XAiConversation entity)
{
//
XAiConversationDto result = null;
//
if (!entity.IsNullOrDefault())
{
//
result = entity.ToDto();
if (!entity.OwnerId.IsNullOrEmpty())
{
result.Owner = await GetPerson(entity.OwnerId);
}
//
result.Project = await ToDto(entity.Project);
result.Messages = await ToDtos(entity.Messages);
}
//
return result;
}
///
/// Converts a List of Entities to Dtos ...
///
///
///
///
public async Task> ToDtos(IEnumerable entities)
{
//
IList list = new List();
//
var enumerable = entities.ToAsyncEnumerable();
await foreach (var entity in enumerable)
{
//
var dto = await ToDto(entity);
if (!dto.IsNullOrDefault())
{
list.Add(dto);
}
}
//
return list.AsEnumerable();
}
///
/// Converts a List of Entities to Dtos ...
///
///
///
///
public async Task> ToDtos(IEnumerable entities)
{
//
IList list = new List();
//
var enumerable = entities.ToAsyncEnumerable();
await foreach (var entity in enumerable)
{
//
var dto = await ToDto(entity);
if (!dto.IsNullOrDefault())
{
list.Add(dto);
}
}
//
return list.AsEnumerable();
}
///
/// Converts a List of Entities to Dtos ...
///
///
///
///
public async Task> ToDtos(IEnumerable entities)
{
//
IList list = new List();
//
var enumerable = entities.ToAsyncEnumerable();
await foreach (var entity in enumerable)
{
//
var dto = await ToDto(entity);
if (!dto.IsNullOrDefault())
{
list.Add(dto);
}
}
//
return list.AsEnumerable();
}
///
/// Converts a QueryResult of Entities to Dtos ...
///
///
///
///
public async Task> ToDtoQueryResult(XQueryResult queryResult)
{
//
var result = new XQueryResult();
//
if (!queryResult.IsNullOrDefault())
{
//
result = result.UpdateData(
updateWith: queryResult,
propertyBlackList: new List
{
nameof(XQueryResult.Items)
}
);
//
if (queryResult.Items.HasChild())
{
result.Items = await ToDtos(queryResult.Items);
}
}
//
return result;
}
///
/// Converts a QueryResult of Entities to Dtos ...
///
///
///
///
public async Task> ToDtoQueryResult(XQueryResult queryResult)
{
//
var result = new XQueryResult();
//
if (!queryResult.IsNullOrDefault())
{
//
result = result.UpdateData(
updateWith: queryResult,
propertyBlackList: new List
{
nameof(XQueryResult.Items)
}
);
//
if (queryResult.Items.HasChild())
{
result.Items = await ToDtos(queryResult.Items);
}
}
//
return result;
}
///
/// Converts a QueryResult of Entities to Dtos ...
///
///
///
///
public async Task> ToDtoQueryResult(XQueryResult queryResult)
{
//
var result = new XQueryResult();
//
if (!queryResult.IsNullOrDefault())
{
//
result = result.UpdateData(
updateWith: queryResult,
propertyBlackList: new List
{
nameof(XQueryResult.Items)
}
);
//
if (queryResult.Items.HasChild())
{
result.Items = await ToDtos(queryResult.Items);
}
}
//
return result;
}
///
/// Check Permission for Ai Actions ...
///
///
///
public async Task HasPermission(XUserClaimsInfoDto userInfo)
{
//
var result = true;
//
// TODO: Complete this ...
//
return await Task.FromResult(result);
}
///
/// Prepare Prompt ...
///
///
///
///
public async Task PreparePrompt(string prompt, XUserClaimsInfoDto userInfo)
{
//
var result = prompt;
//
// TODO: Complete this ...
//
return await Task.FromResult(result);
}
#endregion
///
/// Ask a Question ...
///
///
///
///
///
public Task Ask(
string prompt,
Guid conversationId,
XUserClaimsInfoDto userInfo
)
{
throw new NotImplementedException();
}
//
#region Private ...
///
/// Access Person class based on OwnerId ...
///
///
///
///
private async Task GetPerson(string ownerId)
{
//
XPersonDto result = null;
//
if (!ownerId.IsNullOrEmpty())
{
//
var device = identityProvider.GetDevice();
var userInfo = await identityProvider.GetUserInfo(
device: device,
userSelectByParam: ownerId
);
if (userInfo.IsNullOrDefault())
{
XException.NotFound.Throw();
}
//
result = userInfo.ToXPersonDto();
}
//
return result;
}
#endregion
}
}