From f48b9257a32252659229f7ca2f4b6f76dca7712c Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Tue, 29 Sep 2026 17:41:17 +0330 Subject: [PATCH] last ... --- Extensions/XAiModelsExtensions.cs | 22 ++- Interfaces/IXAiDataProvider.cs | 2 + Providers/XAiDataProvider.cs | 270 ++++++++++++++++++++++++++++-- 3 files changed, 265 insertions(+), 29 deletions(-) diff --git a/Extensions/XAiModelsExtensions.cs b/Extensions/XAiModelsExtensions.cs index 2dfef6b..0be37c5 100644 --- a/Extensions/XAiModelsExtensions.cs +++ b/Extensions/XAiModelsExtensions.cs @@ -28,7 +28,6 @@ namespace xAiModels.Extensions var result = !source.IsNullOrDefault() && !source.OwnerId.IsNullOrEmpty() && - !source.Title.IsNullOrEmpty() && !source.Prompt.IsNullOrEmpty(); // @@ -40,7 +39,6 @@ namespace xAiModels.Extensions var result = !source.IsNullOrDefault() && !source.OwnerId.IsNullOrEmpty() && - !source.Title.IsNullOrEmpty() && !source.Prompt.IsNullOrEmpty(); // @@ -58,7 +56,6 @@ namespace xAiModels.Extensions var result = !source.IsNullOrDefault() && !source.OwnerId.IsNullOrEmpty() && - !source.Title.IsNullOrEmpty() && !source.ProjectId.IsNull() && !source.ProjectId.IsDefaultGuid(); @@ -71,7 +68,6 @@ namespace xAiModels.Extensions var result = !source.IsNullOrDefault() && !source.OwnerId.IsNullOrEmpty() && - !source.Title.IsNullOrEmpty() && !source.ProjectId.IsNull() && !source.ProjectId.IsDefaultGuid(); @@ -222,7 +218,7 @@ namespace xAiModels.Extensions .Contains(ownerId.ToNormalString()) && x.Title .ToNormalString() - .Contains(XAiModelsConstants.DefaultXAiProjectDescriptionResourceID) + .Contains(XAiModelsConstants.DefaultXAiProjectDescriptionResourceID.ToNormalString()) ); // @@ -287,7 +283,7 @@ namespace xAiModels.Extensions { // // Validate Args ... - if (source.HasChild() || + if (!source.HasChild() || ownerId.IsNullOrEmpty()) { XException.InvalidArgs.Throw(); @@ -301,7 +297,7 @@ namespace xAiModels.Extensions .Contains(ownerId.ToNormalString()) && x.Title .ToNormalString() - .Contains(XAiModelsConstants.DefaultXAiProjectDescriptionResourceID) + .Contains(XAiModelsConstants.XAiProjectTitleIdentifier.ToNormalString()) ); if (item.IsNullOrDefault()) { @@ -327,7 +323,8 @@ namespace xAiModels.Extensions { // // Validate Args ... - if (source.HasChild() || + if (!source.HasChild() || + ownerId.IsNullOrEmpty() || projectId.IsDefaultGuid()) { XException.InvalidArgs.Throw(); @@ -341,11 +338,12 @@ namespace xAiModels.Extensions .Contains(ownerId.ToNormalString()) && x.Title .ToNormalString() - .Contains(projectId.ToString().ToNormalString()) && - x.Title - .ToNormalString() - .Contains(XAiModelsConstants.DefaultXAiConversationTitleResourceID) + .Contains(XAiModelsConstants.XAiConversationTitleIdentifier.ToNormalString()) ); + if (item.IsNullOrDefault()) + { + XException.NotFound.Throw(); + } // var result = item.Id; diff --git a/Interfaces/IXAiDataProvider.cs b/Interfaces/IXAiDataProvider.cs index f8aa942..217374a 100644 --- a/Interfaces/IXAiDataProvider.cs +++ b/Interfaces/IXAiDataProvider.cs @@ -222,12 +222,14 @@ namespace xAiModels.Interfaces /// /// Create Conversation ... /// + /// /// /// /// /// /// Task CreateConversation( + string ownerId, Guid projectId, XAiConversationDto item = null, string connectionId = null, diff --git a/Providers/XAiDataProvider.cs b/Providers/XAiDataProvider.cs index 6dc24d3..e0c6cb3 100644 --- a/Providers/XAiDataProvider.cs +++ b/Providers/XAiDataProvider.cs @@ -397,6 +397,14 @@ namespace xAiModels.Providers // #region Creator/Adders ... + /// + /// Create Project ... + /// + /// + /// + /// + /// + /// public async Task CreateProject( string ownerId, XAiProjectDto item = null, @@ -420,6 +428,7 @@ namespace xAiModels.Providers if (hasProject) { // + item.OwnerId = ownerId; item.CreatedOn = DateTime.UtcNow; // @@ -432,18 +441,132 @@ namespace xAiModels.Providers // XAiProjectDto result = null; + var defaultLanguage = appConfiguration.DefaultLanguage; // // Create Provided Project ... if (hasProject) { // + // Add Item for ID Gneneration ... result = await projectRepositoryService.AddAsync( item: item, saveChanges: true, cancellationToken: cancellationToken ); + // + // Checking Resources ... + + // + // Titles ... + var titleResource = string.Empty; + if (!item.TitleResource.IsNullOrDefault() && + item.TitleResource.Locales.HasChild()) + { + // + // Adding Title Resource Locales ... + var isDefaultLanguageHandled = false; + foreach (var trl in item.TitleResource.Locales) + { + // + // Attaching Default Language ... + if (!isDefaultLanguageHandled && + trl.Language.IsNullOrEmpty()) + { + // + trl.Language = defaultLanguage; + isDefaultLanguageHandled = false; + } + + // + // Check Value Exists ... + if (trl.Value.IsNullOrEmpty()) + { + continue; + } + + // + var resource = await projectTitleResourceProvider.AddOrUpdateLocale( + locale: trl, + owner: ownerId, + identifier: result.Id, + connectionId: connectionId, + cancellationToken: cancellationToken + ); + if (!resource.IsNullOrDefault() && + titleResource.IsNullOrEmpty()) + { + titleResource = resource.Resource; + } + } + } + + // + // Descriptions ... + var descriptionResource = string.Empty; + if (!item.DescriptionResource.IsNullOrDefault() && + item.DescriptionResource.Locales.HasChild()) + { + // + // Adding Description Resource Locales .... + var isDefaultLanguageHandled = false; + foreach (var trl in item.DescriptionResource.Locales) + { + // + // Attaching Default Language ... + if (!isDefaultLanguageHandled && + trl.Language.IsNullOrEmpty()) + { + // + trl.Language = defaultLanguage; + isDefaultLanguageHandled = false; + } + + // + // Check Value Exists ... + if (trl.Value.IsNullOrEmpty()) + { + continue; + } + + // + var resource = await projectDescriptionResourceProvider.AddOrUpdateLocale( + locale: trl, + owner: ownerId, + identifier: result.Id, + connectionId: connectionId, + cancellationToken: cancellationToken + ); + if (!resource.IsNullOrDefault() && + descriptionResource.IsNullOrEmpty()) + { + descriptionResource = resource.Resource; + } + } + } + + // + // Update Data ... + result.Title = titleResource; + result.Description = descriptionResource; + result = await projectRepositoryService.UpdateAsync( + item: result, + id: result.Id, + saveChanges: true, + cancellationToken: cancellationToken + ); + if (!result.IsNullOrDefault()) + { + // + result = await projectEnricher.Enrich( + source: result, + fillConversations: false, + fillConversationMessages: false, + cancellationToken: cancellationToken + ); + } + // // Handling Send Push Notification ... if (!result.IsNullOrDefault() && @@ -493,11 +616,30 @@ namespace xAiModels.Providers XException.ActionFailed.Throw(); } + // + // Handle Enrichment ... + result = await Enrich( + source: result, + fillConversations: false, + fillConversationMessages: false, + cancellationToken: cancellationToken + ); + // return result; } + /// + /// Create an AI Conversation ... + /// + /// + /// + /// + /// + /// + /// public async Task CreateConversation( + string ownerId, Guid projectId, XAiConversationDto item = null, string connectionId = null, @@ -536,6 +678,7 @@ namespace xAiModels.Providers if (hasConversation) { // + item.OwnerId = ownerId; item.ProjectId = project.Id; item.CreatedOn = DateTime.UtcNow; @@ -549,12 +692,14 @@ namespace xAiModels.Providers // XAiConversationDto result = null; + var defaultLanguage = appConfiguration.DefaultLanguage; // // Create Provided Conversation ... if (hasConversation) { // + // Add Item for ID Gneneration ... result = await conversationRepositoryService.AddAsync( item: item, saveChanges: true, @@ -562,6 +707,72 @@ namespace xAiModels.Providers cancellationToken: cancellationToken ); + // + // Checking Resources ... + + // + // Titles ... + var titleResource = string.Empty; + if (!item.TitleResource.IsNullOrDefault() && + item.TitleResource.Locales.HasChild()) + { + // + // Adding Title Resource Locales ... + var isDefaultLanguageHandled = false; + foreach (var trl in item.TitleResource.Locales) + { + // + // Attaching Default Language ... + if (!isDefaultLanguageHandled && + trl.Language.IsNullOrEmpty()) + { + // + trl.Language = defaultLanguage; + isDefaultLanguageHandled = false; + } + + // + // Check Value Exists ... + if (trl.Value.IsNullOrEmpty()) + { + continue; + } + + // + var resource = await conversationTitleResourceProvider.AddOrUpdateLocale( + locale: trl, + owner: ownerId, + identifier: result.Id, + connectionId: connectionId, + cancellationToken: cancellationToken + ); + if (!resource.IsNullOrDefault() && + titleResource.IsNullOrEmpty()) + { + titleResource = resource.Resource; + } + } + } + + // + // Update Data ... + result.Title = titleResource; + result = await conversationRepositoryService.UpdateAsync( + item: result, + id: result.Id, + saveChanges: true, + cancellationToken: cancellationToken + ); + if (!result.IsNullOrDefault()) + { + // + result = await conversationEnricher.Enrich( + source: result, + fillMessages: false, + cancellationToken: cancellationToken + ); + } + // // Handling Send Push Notification ... if (!result.IsNullOrDefault() && @@ -613,10 +824,26 @@ namespace xAiModels.Providers XException.ActionFailed.Throw(); } + // + // Handle Enrichment ... + result = await Enrich( + source: result, + fillMessages: false, + cancellationToken: cancellationToken + ); + // return result; } + /// + /// Add Message ... + /// + /// + /// + /// + /// + /// public async Task AddMessage( Guid conversationId, XAiMessageDto item, @@ -653,6 +880,17 @@ namespace xAiModels.Providers cancellationToken: cancellationToken ); + // + // Handle Enrichment ... + if (!result.IsNullOrDefault()) + { + // + result = await Enrich( + source: result, + cancellationToken: cancellationToken + ); + } + // // Handling Send Push Notification ... if (!result.IsNullOrDefault() && @@ -714,15 +952,14 @@ namespace xAiModels.Providers cancellationToken: cancellationToken, predicate: x => x.ResourceTitle - // .ToNormalString() + .ToNormalString() .Contains(ownerId.ToNormalString()) && x.ResourceTitle - // .ToNormalString() - // .ToNormalString() - .Contains(XAiModelsConstants.XAiProjectTitleIdentifier) && + .ToNormalString() + .Contains(XAiModelsConstants.XAiProjectTitleIdentifier.ToNormalString()) && x.TranslatedValue - // .ToNormalString() - .Contains(XAiDataProviderConstants.XDefaultAiProjectSuffix) + .ToNormalString() + .Contains(XAiDataProviderConstants.XDefaultAiProjectSuffix.ToNormalString()) ); if (resource.IsNullOrDefault()) { @@ -1034,7 +1271,7 @@ namespace xAiModels.Providers cancellationToken: cancellationToken, predicate: x => x.ResourceTitle - .ToNormalString() + .ToNormalString() .Contains(ownerId.ToNormalString()) && x.ResourceTitle .ToNormalString() @@ -1101,7 +1338,7 @@ namespace xAiModels.Providers // Prepare Tile and Description Resource Ids ... var titleResource = await projectTitleResourceProvider.AddOrUpdateLocale( owner: ownerId, - identifier: item.Id, // $"{XAiDataProviderConstants.XDefaultAiProjectSuffix}_{item.Id}", + identifier: item.Id, locale: new XLocaleResourceDto { Language = defaultLang, @@ -1111,7 +1348,7 @@ namespace xAiModels.Providers ); var descriptionResource = await projectDescriptionResourceProvider.AddOrUpdateLocale( owner: ownerId, - identifier: item.Id, // $"{XAiDataProviderConstants.XDefaultAiProjectSuffix}_{item.Id}", + identifier: item.Id, new XLocaleResourceDto { Language = defaultLang, @@ -1209,15 +1446,14 @@ namespace xAiModels.Providers cancellationToken: cancellationToken, predicate: x => x.ResourceTitle - // .ToNormalString() - // .ToNormalString() - .Contains(projectId.ToString()) && + .ToNormalString() + .Contains(ownerId.ToString().ToNormalString()) && x.ResourceTitle - // .ToNormalString() - .Contains(XAiModelsConstants.XAiConversationTitleIdentifier) && + .ToNormalString() + .Contains(XAiModelsConstants.XAiConversationTitleIdentifier.ToNormalString()) && x.TranslatedValue - // .ToNormalString() - .Contains(XAiDataProviderConstants.XDefaultAiConversationSuffix) + .ToNormalString() + .Contains(XAiDataProviderConstants.XDefaultAiConversationSuffix.ToNormalString()) ); result = !resource.IsNullOrDefault(); if (!result) @@ -1324,7 +1560,7 @@ namespace xAiModels.Providers // Prepare Title Resource Ids ... var titleResource = await conversationTitleResourceProvider.AddOrUpdateLocale( owner: ownerId, - identifier: item.Id, // $"{XAiDataProviderConstants.XDefaultAiConversationSuffix}_{item.Id}", + identifier: item.Id, locale: new XLocaleResourceDto { Language = defaultLang,