339 lines
10 KiB
C#
339 lines
10 KiB
C#
using System;
|
|
using System.Linq;
|
|
using xAiModels.Models;
|
|
using xCommons.Extensions;
|
|
using xAiModels.Constants;
|
|
using xAiModels.Models.Dtos;
|
|
using xExceptions.Constants;
|
|
using xAiModels.Models.Entities;
|
|
using System.Collections.Generic;
|
|
|
|
namespace xAiModels.Extensions
|
|
{
|
|
/// <summary>
|
|
/// Extensions Methods for XAiModels ...
|
|
/// </summary>
|
|
public static class XAiModelsExtensions
|
|
{
|
|
/// <summary>
|
|
/// Validate a Project for Add Or Update ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static bool IsValidForAddOrUpdate(this XAiProject source)
|
|
{
|
|
//
|
|
var result =
|
|
!source.IsNullOrDefault() &&
|
|
!source.OwnerId.IsNullOrEmpty() &&
|
|
!source.Title.IsNullOrEmpty() &&
|
|
!source.Prompt.IsNullOrEmpty();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
public static bool IsValidForAddOrUpdate(this XAiProjectDto source)
|
|
{
|
|
//
|
|
var result =
|
|
!source.IsNullOrDefault() &&
|
|
!source.OwnerId.IsNullOrEmpty() &&
|
|
!source.Title.IsNullOrEmpty() &&
|
|
!source.Prompt.IsNullOrEmpty();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Validate a Conversation for Add Or Update ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static bool IsValidForAddOrUpdate(this XAiConversation source)
|
|
{
|
|
//
|
|
var result =
|
|
!source.IsNullOrDefault() &&
|
|
!source.OwnerId.IsNullOrEmpty() &&
|
|
!source.Title.IsNullOrEmpty() &&
|
|
!source.ProjectId.IsNull() &&
|
|
!source.ProjectId.IsDefaultGuid();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
public static bool IsValidForAddOrUpdate(this XAiConversationDto source)
|
|
{
|
|
//
|
|
var result =
|
|
!source.IsNullOrDefault() &&
|
|
!source.OwnerId.IsNullOrEmpty() &&
|
|
!source.Title.IsNullOrEmpty() &&
|
|
!source.ProjectId.IsNull() &&
|
|
!source.ProjectId.IsDefaultGuid();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Validate a Message for Add Or Update ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Validate a Model Descriptor ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Validate Specified Param ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static bool IsValid(this XAiPromptParam source)
|
|
{
|
|
//
|
|
var result =
|
|
!source.IsNullOrDefault() &&
|
|
!source.Name.IsNullOrEmpty() &&
|
|
!source.Param.IsNullOrEmpty();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Validate Specified Prompt ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check a Collection Contains Default Project for Specified Owner or not ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="ownerId"></param>
|
|
/// <returns></returns>
|
|
public static bool HasDefaultProject(
|
|
this IEnumerable<XAiProjectDto> 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)
|
|
);
|
|
|
|
//
|
|
var result = !item.IsNullOrDefault();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check a Collection Contains Default Conversation for Specified Project or not ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="ownerId"></param>
|
|
/// <param name="projectId"></param>
|
|
/// <returns></returns>
|
|
public static bool HasDefaultConversation(
|
|
this IEnumerable<XAiConversationDto> 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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieve Default Project Id from a Collection ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="ownerId"></param>
|
|
/// <returns></returns>
|
|
public static Guid GetDafualtProject(
|
|
this IEnumerable<XAiProjectDto> 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)
|
|
);
|
|
if (item.IsNullOrDefault())
|
|
{
|
|
XException.NotFound.Throw();
|
|
}
|
|
|
|
//
|
|
return item.Id;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get Default Conversation Id from a Collection Contains ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="ownerId"></param>
|
|
/// <param name="projectId"></param>
|
|
/// <returns></returns>
|
|
public static Guid GetDefaultConversation(
|
|
this IEnumerable<XAiConversationDto> 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.Id;
|
|
|
|
//
|
|
return result;
|
|
}
|
|
}
|
|
} |