395 lines
11 KiB
C#
395 lines
11 KiB
C#
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 ...
|
|
/// <summary>
|
|
/// Converts Entity to Dto ...
|
|
/// <see cref="XAiProjectDto"/>
|
|
/// </summary>
|
|
/// <param name="entity"><see cref="XAiProject"/></param>
|
|
/// <returns></returns>
|
|
public async Task<XAiProjectDto> 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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts Entity to Dto ...
|
|
/// <see cref="XAiMessageDto"/>
|
|
/// </summary>
|
|
/// <param name="entity"><see cref="XAiMessage"/></param>
|
|
/// <returns></returns>
|
|
public async Task<XAiMessageDto> 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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts Entity to Dto ...
|
|
/// <see cref="XAiConversationDto"/>
|
|
/// </summary>
|
|
/// <param name="entity"><see cref="XAiConversation"/></param>
|
|
/// <returns></returns>
|
|
public async Task<XAiConversationDto> 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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts a List of Entities to Dtos ...
|
|
/// <see cref="XAiProjectDto"/>
|
|
/// </summary>
|
|
/// <param name="entities"><see cref="XAiProject"/></param>
|
|
/// <returns></returns>
|
|
public async Task<IEnumerable<XAiProjectDto>> ToDtos(IEnumerable<XAiProject> entities)
|
|
{
|
|
//
|
|
IList<XAiProjectDto> list = new List<XAiProjectDto>();
|
|
|
|
//
|
|
var enumerable = entities.ToAsyncEnumerable();
|
|
await foreach (var entity in enumerable)
|
|
{
|
|
//
|
|
var dto = await ToDto(entity);
|
|
if (!dto.IsNullOrDefault())
|
|
{
|
|
list.Add(dto);
|
|
}
|
|
}
|
|
|
|
//
|
|
return list.AsEnumerable();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts a List of Entities to Dtos ...
|
|
/// <see cref="XAiMessageDto"/>
|
|
/// </summary>
|
|
/// <param name="entities"><see cref="XAiMessage"/></param>
|
|
/// <returns></returns>
|
|
public async Task<IEnumerable<XAiMessageDto>> ToDtos(IEnumerable<XAiMessage> entities)
|
|
{
|
|
//
|
|
IList<XAiMessageDto> list = new List<XAiMessageDto>();
|
|
|
|
//
|
|
var enumerable = entities.ToAsyncEnumerable();
|
|
await foreach (var entity in enumerable)
|
|
{
|
|
//
|
|
var dto = await ToDto(entity);
|
|
if (!dto.IsNullOrDefault())
|
|
{
|
|
list.Add(dto);
|
|
}
|
|
}
|
|
|
|
//
|
|
return list.AsEnumerable();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts a List of Entities to Dtos ...
|
|
/// <see cref="XAiConversationDto"/>
|
|
/// </summary>
|
|
/// <param name="entities"><see cref="XAiConversation"/></param>
|
|
/// <returns></returns>
|
|
public async Task<IEnumerable<XAiConversationDto>> ToDtos(IEnumerable<XAiConversation> entities)
|
|
{
|
|
//
|
|
IList<XAiConversationDto> list = new List<XAiConversationDto>();
|
|
|
|
//
|
|
var enumerable = entities.ToAsyncEnumerable();
|
|
await foreach (var entity in enumerable)
|
|
{
|
|
//
|
|
var dto = await ToDto(entity);
|
|
if (!dto.IsNullOrDefault())
|
|
{
|
|
list.Add(dto);
|
|
}
|
|
}
|
|
|
|
//
|
|
return list.AsEnumerable();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts a QueryResult of Entities to Dtos ...
|
|
/// <see cref="XAiProjectDto"/>
|
|
/// </summary>
|
|
/// <param name="queryResult"><see cref="XAiProject"/></param>
|
|
/// <returns></returns>
|
|
public async Task<XQueryResult<XAiProjectDto>> ToDtoQueryResult(XQueryResult<XAiProject> queryResult)
|
|
{
|
|
//
|
|
var result = new XQueryResult<XAiProjectDto>();
|
|
|
|
//
|
|
if (!queryResult.IsNullOrDefault())
|
|
{
|
|
//
|
|
result = result.UpdateData(
|
|
updateWith: queryResult,
|
|
propertyBlackList: new List<string>
|
|
{
|
|
nameof(XQueryResult<XAiProject>.Items)
|
|
}
|
|
);
|
|
|
|
//
|
|
if (queryResult.Items.HasChild())
|
|
{
|
|
result.Items = await ToDtos(queryResult.Items);
|
|
}
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts a QueryResult of Entities to Dtos ...
|
|
/// <see cref="XAiMessageDto"/>
|
|
/// </summary>
|
|
/// <param name="queryResult"><see cref="XAiMessage"/></param>
|
|
/// <returns></returns>
|
|
public async Task<XQueryResult<XAiMessageDto>> ToDtoQueryResult(XQueryResult<XAiMessage> queryResult)
|
|
{
|
|
//
|
|
var result = new XQueryResult<XAiMessageDto>();
|
|
|
|
//
|
|
if (!queryResult.IsNullOrDefault())
|
|
{
|
|
//
|
|
result = result.UpdateData(
|
|
updateWith: queryResult,
|
|
propertyBlackList: new List<string>
|
|
{
|
|
nameof(XQueryResult<XAiMessage>.Items)
|
|
}
|
|
);
|
|
|
|
//
|
|
if (queryResult.Items.HasChild())
|
|
{
|
|
result.Items = await ToDtos(queryResult.Items);
|
|
}
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts a QueryResult of Entities to Dtos ...
|
|
/// <see cref="XAiConversationDto"/>
|
|
/// </summary>
|
|
/// <param name="queryResult"><see cref="XAiConversation"/></param>
|
|
/// <returns></returns>
|
|
public async Task<XQueryResult<XAiConversationDto>> ToDtoQueryResult(XQueryResult<XAiConversation> queryResult)
|
|
{
|
|
//
|
|
var result = new XQueryResult<XAiConversationDto>();
|
|
|
|
//
|
|
if (!queryResult.IsNullOrDefault())
|
|
{
|
|
//
|
|
result = result.UpdateData(
|
|
updateWith: queryResult,
|
|
propertyBlackList: new List<string>
|
|
{
|
|
nameof(XQueryResult<XAiConversation>.Items)
|
|
}
|
|
);
|
|
|
|
//
|
|
if (queryResult.Items.HasChild())
|
|
{
|
|
result.Items = await ToDtos(queryResult.Items);
|
|
}
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check Permission for Ai Actions ...
|
|
/// </summary>
|
|
/// <param name="userInfo"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> HasPermission(XUserClaimsInfoDto userInfo)
|
|
{
|
|
//
|
|
var result = true;
|
|
|
|
//
|
|
// TODO: Complete this ...
|
|
|
|
//
|
|
return await Task.FromResult(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Prepare Prompt ...
|
|
/// </summary>
|
|
/// <param name="prompt"></param>
|
|
/// <param name="userInfo"></param>
|
|
/// <returns></returns>
|
|
public async Task<string> PreparePrompt(string prompt, XUserClaimsInfoDto userInfo)
|
|
{
|
|
//
|
|
var result = prompt;
|
|
|
|
//
|
|
// TODO: Complete this ...
|
|
|
|
//
|
|
return await Task.FromResult(result);
|
|
}
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// Ask a Question ...
|
|
/// </summary>
|
|
/// <param name="prompt"></param>
|
|
/// <param name="conversationId"></param>
|
|
/// <param name="userInfo"></param>
|
|
/// <returns></returns>
|
|
public Task<XAiMessageDto> Ask(
|
|
string prompt,
|
|
Guid conversationId,
|
|
XUserClaimsInfoDto userInfo
|
|
)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
//
|
|
#region Private ...
|
|
/// <summary>
|
|
/// Access Person class based on OwnerId ...
|
|
/// <see cref="XPersonDto"/>
|
|
/// </summary>
|
|
/// <param name="ownerId"></param>
|
|
/// <returns></returns>
|
|
private async Task<XPersonDto> 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
|
|
}
|
|
} |