284 lines
9.2 KiB
C#
284 lines
9.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using xAiModels.Interfaces;
|
|
using xAiModels.Interfaces.Dtos;
|
|
using xAiModels.Interfaces.Enrichers;
|
|
using xAiModels.Models.Dtos;
|
|
using xCommons.Extensions;
|
|
using xIdentityService.Interfaces;
|
|
using xIdentityService.Extensions;
|
|
using xModels.Dtos;
|
|
using System.Linq;
|
|
|
|
namespace xAiModels.Providers.Enrichers
|
|
{
|
|
public class XAiConversationEnricher : IXAiConversationEnricher
|
|
{
|
|
private readonly IXIdentityProvider identityProvider;
|
|
private readonly IXAiMessageRepositoryService messageRepositoryService;
|
|
private readonly IXAiProjectRepositoryService projectRepositoryService;
|
|
private readonly IXAiConversationTitleResourceProvider conversationTitleResourceProvider;
|
|
|
|
public XAiConversationEnricher(
|
|
IXIdentityProvider identityProvider,
|
|
IXAiMessageRepositoryService messageRepositoryService,
|
|
IXAiProjectRepositoryService projectRepositoryService,
|
|
IXAiConversationTitleResourceProvider conversationTitleResourceProvider
|
|
)
|
|
{
|
|
this.identityProvider = identityProvider;
|
|
this.messageRepositoryService = messageRepositoryService;
|
|
this.projectRepositoryService = projectRepositoryService;
|
|
this.conversationTitleResourceProvider = conversationTitleResourceProvider;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts a Signle Entity to Dto ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="destination"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public async Task EnrichAsync(
|
|
XAiConversationDto source,
|
|
XAiConversationDto destination,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
await EnrichAsync(
|
|
source: source,
|
|
fillMessages: false,
|
|
destination: destination,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts a Collection of Entities to Dtos ...
|
|
/// </summary>
|
|
/// <param name="sources"></param>
|
|
/// <param name="destinations"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public async Task EnrichRangeAsync(
|
|
IEnumerable<XAiConversationDto> sources,
|
|
IEnumerable<XAiConversationDto> destinations,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
await EnrichRangeAsync(
|
|
sources: sources,
|
|
fillMessages: false,
|
|
destinations: destinations,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts a Signle Entity to Dto ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="destination"></param>
|
|
/// <param name="fillMessages"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public async Task EnrichAsync(
|
|
XAiConversationDto source,
|
|
XAiConversationDto destination,
|
|
bool fillMessages = false,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
// Run independent async operations concurrently
|
|
var ownerTask = ResolveOwnerAsync(source.OwnerId, cancellationToken);
|
|
var projectTask = ResolveProjectAsync(source.ProjectId, cancellationToken);
|
|
var titleLocalesTask = ResolveTitleLocalesAsync(source.Title, cancellationToken);
|
|
var messagesTask = ResolveMessagesAsync(source.Id, cancellationToken);
|
|
|
|
//
|
|
// wait until All Tasks Resolved ...
|
|
Task[] tasks = [
|
|
ownerTask,
|
|
projectTask,
|
|
titleLocalesTask
|
|
];
|
|
if (fillMessages)
|
|
{
|
|
tasks = [.. tasks, messagesTask];
|
|
}
|
|
await Task.WhenAll(tasks);
|
|
|
|
//
|
|
// Attached Resolved Info to Destination ...
|
|
destination.Owner = ownerTask.Result;
|
|
destination.TitleResource = titleLocalesTask.Result;
|
|
var project = projectTask.Result;
|
|
if (!project.IsNullOrDefault())
|
|
{
|
|
destination.ProjectTitle = project.Title;
|
|
destination.ProjectDescription = project.Description;
|
|
destination.ProjectTitleResource = project.TitleResource;
|
|
destination.ProjectDescriptionResource = project.DescriptionResource;
|
|
}
|
|
if (!messagesTask.Result.IsNull())
|
|
{
|
|
destination.Messages = [.. messagesTask.Result];
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts a Collection of Entities to Dtos ...
|
|
/// </summary>
|
|
/// <param name="sources"></param>
|
|
/// <param name="destinations"></param>
|
|
/// <param name="fillMessages"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public async Task EnrichRangeAsync(
|
|
IEnumerable<XAiConversationDto> sources,
|
|
IEnumerable<XAiConversationDto> destinations,
|
|
bool fillMessages = false,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
// Make Tasks Ready ...
|
|
var tasks = sources.Zip(destinations, (src, dst) =>
|
|
EnrichAsync(src, dst, cancellationToken)
|
|
);
|
|
|
|
//
|
|
await Task.WhenAll(tasks);
|
|
}
|
|
|
|
//
|
|
#region Private ...
|
|
/// <summary>
|
|
/// Reading Owner User Info ...
|
|
/// </summary>
|
|
/// <param name="ownerId"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
private async Task<XPersonDto> ResolveOwnerAsync(
|
|
string ownerId,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
XPersonDto result = null;
|
|
|
|
//
|
|
// Validate ...
|
|
if (ownerId.IsNullOrEmpty())
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Try to Retrieve User Info ...
|
|
var device = identityProvider.GetDevice();
|
|
var userInfo = await identityProvider.GetUserInfo(
|
|
device: device,
|
|
userSelectByParam: ownerId
|
|
);
|
|
result = userInfo.ToXPersonDto();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Reading Title Locales ...
|
|
/// </summary>
|
|
/// <param name="title"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
private async Task<XResourceDto> ResolveTitleLocalesAsync(
|
|
string title,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
XResourceDto result = null;
|
|
if (title.IsNullOrEmpty())
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
result = await conversationTitleResourceProvider.GetResource(
|
|
owner: null,
|
|
identifier: title,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieve Project ...
|
|
/// </summary>
|
|
/// <param name="projectId"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
private async Task<XAiProjectDto> ResolveProjectAsync(
|
|
Guid projectId,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
XAiProjectDto result = null;
|
|
if (projectId.IsNull() || projectId.IsDefaultGuid())
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
result = await projectRepositoryService.GetAsync(
|
|
id: projectId,
|
|
includeBuilder: null,
|
|
ignoreSoftDeleteds: true,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieve Specified Conversation Messages ...
|
|
/// </summary>
|
|
/// <param name="conversationId"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
private async Task<IEnumerable<XAiMessageDto>> ResolveMessagesAsync(
|
|
Guid conversationId,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
var result = new List<XAiMessageDto>();
|
|
|
|
//
|
|
if (conversationId.IsDefaultGuid())
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
result = [.. await messageRepositoryService.FindManyAsync(
|
|
cancellationToken: cancellationToken,
|
|
orderBuilder: x => x.OrderBy(y => y.CreatedOn),
|
|
predicate: x => x.ConversationId == conversationId
|
|
)];
|
|
|
|
//
|
|
return result;
|
|
}
|
|
#endregion
|
|
}
|
|
} |