last works ...
This commit is contained in:
@@ -17,16 +17,19 @@ 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;
|
||||
}
|
||||
@@ -43,16 +46,70 @@ namespace xAiModels.Providers.Enrichers
|
||||
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 ...
|
||||
await Task.WhenAll(ownerTask, titleLocalesTask, projectTask);
|
||||
Task[] tasks = [
|
||||
ownerTask,
|
||||
projectTask,
|
||||
titleLocalesTask
|
||||
];
|
||||
if (fillMessages)
|
||||
{
|
||||
tasks = [.. tasks, messagesTask];
|
||||
}
|
||||
await Task.WhenAll(tasks);
|
||||
|
||||
//
|
||||
// Attached Resolved Info to Destination ...
|
||||
@@ -66,6 +123,10 @@ namespace xAiModels.Providers.Enrichers
|
||||
destination.ProjectTitleResource = project.TitleResource;
|
||||
destination.ProjectDescriptionResource = project.DescriptionResource;
|
||||
}
|
||||
if (!messagesTask.Result.IsNull())
|
||||
{
|
||||
destination.Messages = [.. messagesTask.Result];
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -73,11 +134,13 @@ namespace xAiModels.Providers.Enrichers
|
||||
/// </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
|
||||
)
|
||||
{
|
||||
@@ -185,6 +248,37 @@ namespace xAiModels.Providers.Enrichers
|
||||
//
|
||||
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
|
||||
}
|
||||
}
|
||||
@@ -52,14 +52,19 @@ namespace xAiModels.Providers.Enrichers
|
||||
XAiProjectDto source,
|
||||
XAiProjectDto destination,
|
||||
bool fillConversations = true,
|
||||
bool fillConversationMessages = true,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Run independent async operations concurrently
|
||||
var conversationsTask = ResolveConversationsAsync(
|
||||
source.Id,
|
||||
fillConversationMessages,
|
||||
cancellationToken
|
||||
);
|
||||
var titleTask = ResolveTitleAsync(source.Title, cancellationToken);
|
||||
var ownerTask = ResolveOwnerAsync(source.OwnerId, cancellationToken);
|
||||
var conversationsTask = ResolveConversationsAsync(source.Id, cancellationToken);
|
||||
var descriptionTask = ResolveDescriptionAsync(source.Description, cancellationToken);
|
||||
|
||||
//
|
||||
@@ -107,13 +112,20 @@ namespace xAiModels.Providers.Enrichers
|
||||
IEnumerable<XAiProjectDto> sources,
|
||||
IEnumerable<XAiProjectDto> destinations,
|
||||
bool fillConversations = true,
|
||||
bool fillConversationMessages = true,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Make Tasks Ready ...
|
||||
var tasks = sources.Zip(destinations, (src, dst) =>
|
||||
EnrichAsync(src, dst, fillConversations, cancellationToken)
|
||||
EnrichAsync(
|
||||
source: src,
|
||||
destination: dst,
|
||||
fillConversations: fillConversations,
|
||||
cancellationToken: cancellationToken,
|
||||
fillConversationMessages: fillConversationMessages
|
||||
)
|
||||
);
|
||||
|
||||
//
|
||||
@@ -224,10 +236,12 @@ namespace xAiModels.Providers.Enrichers
|
||||
/// Retrieve Project Conversations ...
|
||||
/// </summary>
|
||||
/// <param name="projectId"></param>
|
||||
/// <param name="fillMessages"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
private async Task<List<XAiConversationDto>> ResolveConversationsAsync(
|
||||
Guid projectId,
|
||||
bool fillMessages = false,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
@@ -248,6 +262,7 @@ namespace xAiModels.Providers.Enrichers
|
||||
orderBuilder: null,
|
||||
includeBuilder: null,
|
||||
ignoreSoftDeleteds: true,
|
||||
fillMessages: fillMessages,
|
||||
predicate: x => x.Id == projectId,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user