using System; using System.Linq; using xAiModels.Models; using xModels.Extensions; using xCommons.Extensions; using xAiModels.Constants; using xAiModels.Models.Dtos; using xExceptions.Constants; using Microsoft.Extensions.AI; using xAiModels.Models.Entities; using System.Collections.Generic; namespace xAiModels.Extensions { /// /// Extensions Methods for XAiModels ... /// public static class XAiModelsExtensions { /// /// Validate a Project for Add Or Update ... /// /// /// public static bool IsValidForAddOrUpdate(this XAiProject source) { // var result = !source.IsNullOrDefault() && !source.OwnerId.IsNullOrEmpty() && !source.Prompt.IsNullOrEmpty(); // return result; } public static bool IsValidForAddOrUpdate(this XAiProjectDto source) { // var result = !source.IsNullOrDefault() && !source.OwnerId.IsNullOrEmpty() && !source.Prompt.IsNullOrEmpty(); // return result; } /// /// Validate a Conversation for Add Or Update ... /// /// /// public static bool IsValidForAddOrUpdate(this XAiConversation source) { // var result = !source.IsNullOrDefault() && !source.OwnerId.IsNullOrEmpty() && !source.ProjectId.IsNull() && !source.ProjectId.IsDefaultGuid(); // return result; } public static bool IsValidForAddOrUpdate(this XAiConversationDto source) { // var result = !source.IsNullOrDefault() && !source.OwnerId.IsNullOrEmpty() && !source.ProjectId.IsNull() && !source.ProjectId.IsDefaultGuid(); // return result; } /// /// Validate a Message for Add Or Update ... /// /// /// public static bool IsValidForAddOrUpdate(this XAiMessage source) { // var result = !source.IsNullOrDefault() && !source.OwnerId.IsNullOrEmpty() && source.Role.IsValid() && !source.ConversationId.IsNull() && !source.ConversationId.IsDefaultGuid() && !source.Content.IsNullOrEmpty(); // return result; } public static bool IsValidForAddOrUpdate(this XAiMessageDto source) { // var result = !source.IsNullOrDefault() && !source.OwnerId.IsNullOrEmpty() && source.Role.IsValid() && !source.ConversationId.IsNull() && !source.ConversationId.IsDefaultGuid() && !source.Content.IsNullOrEmpty(); // return result; } /// /// Validate a Model Descriptor ... /// /// /// public static bool IsValid(this XAiModelDescriptor source) { // var result = !source.IsNullOrDefault() && !source.Name.IsNullOrEmpty() && !source.Url.IsNullOrEmpty() && source.Url.IsValidUrl() && !source.LLM.IsNullOrEmpty() && source.Provider != Constants.XAiModelProviderType.None; // return result; } /// /// Validate Specified Param ... /// /// /// public static bool IsValid(this XAiPromptParam source) { // var result = !source.IsNullOrDefault() && !source.Name.IsNullOrEmpty() && !source.Param.IsNullOrEmpty(); // return result; } /// /// Validate Specified Prompt ... /// /// /// public static bool IsValid(this XAiPromptDescriptor source) { // var result = !source.IsNullOrDefault() && !source.Name.IsNullOrEmpty() && !source.Template.IsNullOrEmpty(); if (!result) { return result; } // if (source.Params.HasChild()) { result = result && source.Params.All(x => x.IsValid()); } // return result; } /// /// Validate a Request for Retrieve Response in Controllers ... /// /// /// public static bool IsValid(this XAiResponseRequest source) { // var result = !source.IsNullOrDefault() && !source.Prompt.IsNullOrEmpty(); // return result; } /// /// Check a Collection Contains Default Project for Specified Owner or not ... /// /// /// /// public static bool HasDefaultProject( this IEnumerable source, string ownerId ) { // // Validate Args ... if (source.HasChild() || ownerId.IsNullOrEmpty()) { XException.InvalidArgs.Throw(); } // var item = source .FirstOrDefault(x => x.Title .ToNormalString() .Contains(ownerId.ToNormalString()) && x.Title .ToNormalString() .Contains(XAiModelsConstants.DefaultXAiProjectDescriptionResourceID.ToNormalString()) ); // var result = !item.IsNullOrDefault(); // return result; } /// /// Check a Collection Contains Default Conversation for Specified Project or not ... /// /// /// /// /// public static bool HasDefaultConversation( this IEnumerable source, string ownerId, Guid projectId ) { // // Validate Args ... if (source.HasChild() || projectId.IsDefaultGuid()) { XException.InvalidArgs.Throw(); } // var item = source .FirstOrDefault(x => x.Title .ToNormalString() .Contains(ownerId.ToNormalString()) && x.Title .ToNormalString() .Contains(projectId.ToString().ToNormalString()) && x.Title .ToNormalString() .Contains(XAiModelsConstants.DefaultXAiConversationTitleResourceID) ); // var result = !item.IsNullOrDefault(); // return result; } /// /// Retrieve Default Project Id from a Collection ... /// /// /// /// public static Guid GetDafualtProject( this IEnumerable source, string ownerId ) { // // Validate Args ... if (!source.HasChild() || ownerId.IsNullOrEmpty()) { XException.InvalidArgs.Throw(); } // var item = source .FirstOrDefault(x => x.Title .ToNormalString() .Contains(ownerId.ToNormalString()) && x.Title .ToNormalString() .Contains(XAiModelsConstants.XAiProjectTitleIdentifier.ToNormalString()) ); if (item.IsNullOrDefault()) { XException.NotFound.Throw(); } // return item.Id; } /// /// Get Default Conversation Id from a Collection Contains ... /// /// /// /// /// public static Guid GetDefaultConversation( this IEnumerable source, string ownerId, Guid projectId ) { // // Validate Args ... if (!source.HasChild() || ownerId.IsNullOrEmpty() || projectId.IsDefaultGuid()) { XException.InvalidArgs.Throw(); } // var item = source .FirstOrDefault(x => x.Title .ToNormalString() .Contains(ownerId.ToNormalString()) && x.Title .ToNormalString() .Contains(XAiModelsConstants.XAiConversationTitleIdentifier.ToNormalString()) ); if (item.IsNullOrDefault()) { XException.NotFound.Throw(); } // var result = item.Id; // return result; } /// /// Converts a Message Dto to ChatMessage ... /// /// /// /// public static ChatMessage ToChatMessages( this XAiMessageDto source, IList additionalContents = null ) { // ChatMessage result = null; // if (!source.IsNullOrDefault()) { // var contents = new List { // // Add Source Content ... new TextContent(source.Content) }; // // Add Additional Content ... if (additionalContents.HasChild()) { contents.AddRange(additionalContents); } // result = new ChatMessage { Contents = contents, AuthorName = source.Role == XAiChatRole.User && !source.Owner.IsNullOrDefault() ? source.Owner.GetFullname() : string.Empty, Role = source.Role.ToChatRole(), MessageId = source.Id.ToString(), }; } // return result; } } }