This commit is contained in:
2026-04-24 13:42:14 +03:30
parent 5efd41754a
commit e49c273171
9 changed files with 643 additions and 171 deletions
@@ -1,4 +1,3 @@
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using xAiApi.AI.Models.Entities;
@@ -8,6 +7,13 @@ namespace xAiApi.AI.Configuration.Entities
public class XAiMessageConfiguration : IEntityTypeConfiguration<XAiMessage>
{
public void Configure(EntityTypeBuilder<XAiMessage> builder)
{ }
{
//
builder
.HasOne(x => x.Conversation)
.WithMany(x => x.Messages)
.HasForeignKey(x => x.ConversationId)
.OnDelete(DeleteBehavior.NoAction);
}
}
}
+85 -6
View File
@@ -1,21 +1,100 @@
using xCommons.Extensions;
using System.Collections.Generic;
using Microsoft.Extensions.AI;
using System.Linq;
using xAiApi.AI.Models.Entities;
using xAiModels.Models;
namespace xAiApi.AI.Extensions
{
/// <summary>
/// Extensions which used for AiModels ...
/// </summary>
public static class AiModelsExtensions
{
public static AIContent GetLast(IList<AIContent> source)
/// <summary>
/// Converts Entity to Dto regardsless of Owner and NavigationProperties ...
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static XAiMessageDto ToDto(this XAiMessage source)
{
//
AIContent result = null;
XAiMessageDto result = null;
//
if (source.HasChild())
if (!source.IsNullOrDefault())
{
result = source.ElementAt(source.Count - 1);
//
result = new XAiMessageDto();
result = result.UpdateData(
updateWith: source,
propertyBlackList: new List<string>
{
nameof(XAiMessage.Deleted),
nameof(XAiMessageDto.Owner),
nameof(XAiMessage.Conversation),
}
);
}
//
return result;
}
/// <summary>
/// Converts Entity to Dto regardsless of Owner and NavigationProperties ...
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static XAiProjectDto ToDto(this XAiProject source)
{
//
XAiProjectDto result = null;
//
if (!source.IsNullOrDefault())
{
//
result = new XAiProjectDto();
result = result.UpdateData(
updateWith: source,
propertyBlackList: new List<string>
{
nameof(XAiProject.Deleted),
nameof(XAiProjectDto.Owner),
nameof(XAiProject.Conversations),
}
);
}
//
return result;
}
/// <summary>
/// Converts Entity to Dto regardsless of Owner and NavigationProperties ...
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static XAiConversationDto ToDto(this XAiConversation source)
{
//
XAiConversationDto result = null;
//
if (!source.IsNullOrDefault())
{
//
result = new XAiConversationDto();
result = result.UpdateData(
updateWith: source,
propertyBlackList: new List<string>
{
nameof(XAiConversation.Deleted),
nameof(XAiConversation.Project),
nameof(XAiConversationDto.Owner),
nameof(XAiConversation.Messages),
}
);
}
//
-162
View File
@@ -1,162 +0,0 @@
using Microsoft.Extensions.AI;
using xAiApi.AI.Constants;
using xCommons.Extensions;
namespace xAiApi.AI.Extensions
{
/// <summary>
/// Extensions for ChatRole ...
/// </summary>
public static class ChatRoleExtensions
{
/// <summary>
/// Normalize an String to Propper Chat Role Value ...
/// <see cref="XAiChatRoles"/>>
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static string GetChatRoleTitle(this string source)
{
//
var result = XAiChatRoles.None;
//
if (source.ToNormalString() == XAiChatRoles.None.ToNormalString())
{
result = XAiChatRoles.None;
}
else if (source.ToNormalString() == XAiChatRoles.User.ToNormalString())
{
result = XAiChatRoles.User;
}
else if (source.ToNormalString() == XAiChatRoles.Tool.ToNormalString())
{
result = XAiChatRoles.Tool;
}
else if (source.ToNormalString() == XAiChatRoles.System.ToNormalString())
{
result = XAiChatRoles.System;
}
else if (source.ToNormalString() == XAiChatRoles.Assistant.ToNormalString())
{
result = XAiChatRoles.Assistant;
}
//
return result;
}
/// <summary>
/// Get Chat Role Title from Chat Role Class ...
/// <see cref="XAiChatRoles"/>
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static string GetRole(this ChatRole source)
{
//
var result = source.Value
.GetChatRoleTitle();
//
return result;
}
/// <summary>
/// Generate Chat Role Class Based on role Title ...
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static ChatRole ToChatRole(this string source)
{
//
var role = source.GetChatRoleTitle();
ChatRole result = new ChatRole(role);
//
return result;
}
/// <summary>
/// Extract Chat Role Enum from Chat Role ...
/// <see cref="ChatRole"/>
/// <see cref="XAiChatRole"/>
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static XAiChatRole ToChatRole(this ChatRole source)
{
//
var role = source
.GetRole()
.GetChatRoleTitle();
//
XAiChatRole result = XAiChatRole.User;
//
if (role == XAiChatRoles.User)
{
result = XAiChatRole.User;
}
else if (role == XAiChatRoles.Tool)
{
result = XAiChatRole.Tool;
}
else if (role == XAiChatRoles.System)
{
result = XAiChatRole.System;
}
else if (role == XAiChatRoles.Assistant)
{
result = XAiChatRole.Assistant;
}
//
return result;
}
/// <summary>
/// Validate an String as Chat Role ...
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static bool IsValid(this string source)
{
//
var result =
source != XAiChatRoles.None &&
source.ToNormalString() != XAiChatRoles.None.ToNormalString();
//
return result;
}
/// <summary>
/// Validate Role ...
/// <see cref="XAiChatRole"/>>
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static bool IsValid(this XAiChatRole source)
{
//
var result = source != XAiChatRole.None;
return result;
}
/// <summary>
/// Validate Role ...
/// <see cref="ChatRole"/>>
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static bool IsValid(this ChatRole source)
{
//
var role = source.ToChatRole();
var result = role.IsValid();
return result;
}
}
}
+120
View File
@@ -1,10 +1,130 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using xAiApi.AI.Models.Entities;
using xAiModels.Models;
using xIdentityModels.Models;
using xModels.Dtos;
namespace xAiApi.AI.Interfaces
{
/// <summary>
/// Provide Ai Capabilities Using AI Service ...
/// using User Data Info ...
/// </summary>
public interface IXAiProvider
{
/// <summary>
/// Ai Service for Getting Features ...
/// </summary>
IXAIService AiService { get; }
//
#region Helpers ...
/// <summary>
/// Converts Entity to Dto ...
/// <see cref="XAiProjectDto"/>
/// </summary>
/// <param name="entity"><see cref="XAiProject"/></param>
/// <returns></returns>
Task<XAiProjectDto> ToDto(XAiProject entity);
/// <summary>
/// Converts Entity to Dto ...
/// <see cref="XAiMessageDto"/>
/// </summary>
/// <param name="entity"><see cref="XAiMessage"/></param>
/// <returns></returns>
Task<XAiMessageDto> ToDto(XAiMessage entity);
/// <summary>
/// Converts Entity to Dto ...
/// <see cref="XAiConversationDto"/>
/// </summary>
/// <param name="entity"><see cref="XAiConversation"/></param>
/// <returns></returns>
Task<XAiConversationDto> ToDto(XAiConversation entity);
/// <summary>
/// Converts a List of Entities to Dtos ...
/// <see cref="XAiProjectDto"/>
/// </summary>
/// <param name="entities"><see cref="XAiProject"/></param>
/// <returns></returns>
Task<IEnumerable<XAiProjectDto>> ToDtos(IEnumerable<XAiProject> entities);
/// <summary>
/// Converts a List of Entities to Dtos ...
/// <see cref="XAiMessageDto"/>
/// </summary>
/// <param name="entities"><see cref="XAiMessage"/></param>
/// <returns></returns>
Task<IEnumerable<XAiMessageDto>> ToDtos(IEnumerable<XAiMessage> entities);
/// <summary>
/// Converts a List of Entities to Dtos ...
/// <see cref="XAiConversationDto"/>
/// </summary>
/// <param name="entities"><see cref="XAiConversation"/></param>
/// <returns></returns>
Task<IEnumerable<XAiConversationDto>> ToDtos(IEnumerable<XAiConversation> entities);
/// <summary>
/// Converts a QueryResult of Entities to Dtos ...
/// <see cref="XAiProjectDto"/>
/// </summary>
/// <param name="queryResult"><see cref="XAiProject"/></param>
/// <returns></returns>
Task<XQueryResult<XAiProjectDto>> ToDtoQueryResult(XQueryResult<XAiProject> queryResult);
/// <summary>
/// Converts a QueryResult of Entities to Dtos ...
/// <see cref="XAiMessageDto"/>
/// </summary>
/// <param name="queryResult"><see cref="XAiMessage"/></param>
/// <returns></returns>
Task<XQueryResult<XAiMessageDto>> ToDtoQueryResult(XQueryResult<XAiMessage> queryResult);
/// <summary>
/// Converts a QueryResult of Entities to Dtos ...
/// <see cref="XAiConversationDto"/>
/// </summary>
/// <param name="queryResult"><see cref="XAiConversation"/></param>
/// <returns></returns>
Task<XQueryResult<XAiConversationDto>> ToDtoQueryResult(XQueryResult<XAiConversation> queryResult);
/// <summary>
/// Check Permission for Ai Actions ...
/// </summary>
/// <param name="userInfo"></param>
/// <returns></returns>
Task<bool> HasPermission(
XUserClaimsInfoDto userInfo
);
/// <summary>
/// Prepare Prompt ...
/// </summary>
/// <param name="prompt"></param>
/// <param name="userInfo"></param>
/// <returns></returns>
Task<string> PreparePrompt(
string prompt,
XUserClaimsInfoDto userInfo
);
#endregion
/// <summary>
/// Ask a Question ...
/// </summary>
/// <param name="prompt"></param>
/// <param name="conversationId"></param>
/// <param name="userInfo"></param>
/// <returns></returns>
Task<XAiMessageDto> Ask(
string prompt,
Guid conversationId,
XUserClaimsInfoDto userInfo
);
}
}
+12
View File
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using xModels.Base;
namespace xAiApi.AI.Models.Entities
@@ -9,9 +10,19 @@ namespace xAiApi.AI.Models.Entities
/// </summary>
public class XAiConversation : XBaseGuidIDEntity
{
/// <summary>
/// User Identifier ...
/// </summary>
/// <value></value>
[Required]
[StringLength(255)]
public string OwnerId { get; set; }
/// <summary>
/// Title of Conversation ...
/// </summary>
[Required]
[StringLength(255)]
public string Title { get; set; }
/// <summary>
@@ -32,6 +43,7 @@ namespace xAiApi.AI.Models.Entities
/// <summary>
/// Project ID ...
/// </summary>
[Required]
public Guid ProjectId { get; set; }
/// <summary>
+11
View File
@@ -4,6 +4,7 @@ using System.ComponentModel.DataAnnotations.Schema;
using xModels.Base;
using xCommons.Extensions;
using xAiModels.Constants;
using System.ComponentModel.DataAnnotations;
namespace xAiApi.AI.Models.Entities
{
@@ -12,9 +13,18 @@ namespace xAiApi.AI.Models.Entities
/// </summary>
public class XAiMessage : XBaseGuidIDEntity
{
/// <summary>
/// User Identifier ...
/// </summary>
/// <value></value>
[Required]
[StringLength(255)]
public string OwnerId { get; set; }
/// <summary>
/// Message of Chat ...
/// </summary>
[Required]
public string Content { get; set; }
/// <summary>
@@ -35,6 +45,7 @@ namespace xAiApi.AI.Models.Entities
/// <summary>
/// Conversation Id ...
/// </summary>
[StringLength(255)]
public Guid ConversationId { get; set; }
/// <summary>
+12
View File
@@ -1,19 +1,31 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using xModels.Base;
namespace xAiApi.AI.Models.Entities
{
public class XAiProject : XBaseGuidIDEntity
{
/// <summary>
/// User Identifier ...
/// </summary>
/// <value></value>
[Required]
[StringLength(255)]
public string OwnerId { get; set; }
/// <summary>
/// Title of Project ...
/// </summary>
[Required]
[StringLength(255)]
public string Title { get; set; }
/// <summary>
/// Description of Project ...
/// </summary>
[StringLength(1000)]
public string Description { get; set; }
/// <summary>
+395
View File
@@ -0,0 +1,395 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using xAiApi.AI.Extensions;
using xAiApi.AI.Interfaces;
using xAiApi.AI.Models.Entities;
using xAiModels.Models;
using xCommons.Extensions;
using xExceptions.Constants;
using xIdentityModels.Models;
using xIdentityService.Extensions;
using xIdentityService.Interfaces;
using xModels.Dtos;
namespace xAiApi.AI.Services
{
public class XAiProvider : IXAiProvider
{
//
public IXAIService AiService { get; }
private readonly IXIdentityProvider identityProvider;
public XAiProvider(
IXAIService aiService,
IXIdentityProvider identityProvider
)
{
//
AiService = aiService;
this.identityProvider = identityProvider;
}
//
#region Helpers ...
/// <summary>
/// Converts Entity to Dto ...
/// <see cref="XAiProjectDto"/>
/// </summary>
/// <param name="entity"><see cref="XAiProject"/></param>
/// <returns></returns>
public async Task<XAiProjectDto> ToDto(XAiProject entity)
{
//
XAiProjectDto result = null;
//
if (!entity.IsNullOrDefault())
{
//
result = entity.ToDto();
if (!entity.OwnerId.IsNullOrEmpty())
{
result.Owner = await GetPerson(entity.OwnerId);
}
//
result.Conversations = await ToDtos(entity.Conversations);
}
//
return result;
}
/// <summary>
/// Converts Entity to Dto ...
/// <see cref="XAiMessageDto"/>
/// </summary>
/// <param name="entity"><see cref="XAiMessage"/></param>
/// <returns></returns>
public async Task<XAiMessageDto> ToDto(XAiMessage entity)
{
//
XAiMessageDto result = null;
//
if (!entity.IsNullOrDefault())
{
//
result = entity.ToDto();
if (!entity.OwnerId.IsNullOrEmpty())
{
result.Owner = await GetPerson(entity.OwnerId);
}
//
result.Conversation = await ToDto(entity.Conversation);
}
//
return result;
}
/// <summary>
/// Converts Entity to Dto ...
/// <see cref="XAiConversationDto"/>
/// </summary>
/// <param name="entity"><see cref="XAiConversation"/></param>
/// <returns></returns>
public async Task<XAiConversationDto> ToDto(XAiConversation entity)
{
//
XAiConversationDto result = null;
//
if (!entity.IsNullOrDefault())
{
//
result = entity.ToDto();
if (!entity.OwnerId.IsNullOrEmpty())
{
result.Owner = await GetPerson(entity.OwnerId);
}
//
result.Project = await ToDto(entity.Project);
result.Messages = await ToDtos(entity.Messages);
}
//
return result;
}
/// <summary>
/// Converts a List of Entities to Dtos ...
/// <see cref="XAiProjectDto"/>
/// </summary>
/// <param name="entities"><see cref="XAiProject"/></param>
/// <returns></returns>
public async Task<IEnumerable<XAiProjectDto>> ToDtos(IEnumerable<XAiProject> entities)
{
//
IList<XAiProjectDto> list = new List<XAiProjectDto>();
//
var enumerable = entities.ToAsyncEnumerable();
await foreach (var entity in enumerable)
{
//
var dto = await ToDto(entity);
if (!dto.IsNullOrDefault())
{
list.Add(dto);
}
}
//
return list.AsEnumerable();
}
/// <summary>
/// Converts a List of Entities to Dtos ...
/// <see cref="XAiMessageDto"/>
/// </summary>
/// <param name="entities"><see cref="XAiMessage"/></param>
/// <returns></returns>
public async Task<IEnumerable<XAiMessageDto>> ToDtos(IEnumerable<XAiMessage> entities)
{
//
IList<XAiMessageDto> list = new List<XAiMessageDto>();
//
var enumerable = entities.ToAsyncEnumerable();
await foreach (var entity in enumerable)
{
//
var dto = await ToDto(entity);
if (!dto.IsNullOrDefault())
{
list.Add(dto);
}
}
//
return list.AsEnumerable();
}
/// <summary>
/// Converts a List of Entities to Dtos ...
/// <see cref="XAiConversationDto"/>
/// </summary>
/// <param name="entities"><see cref="XAiConversation"/></param>
/// <returns></returns>
public async Task<IEnumerable<XAiConversationDto>> ToDtos(IEnumerable<XAiConversation> entities)
{
//
IList<XAiConversationDto> list = new List<XAiConversationDto>();
//
var enumerable = entities.ToAsyncEnumerable();
await foreach (var entity in enumerable)
{
//
var dto = await ToDto(entity);
if (!dto.IsNullOrDefault())
{
list.Add(dto);
}
}
//
return list.AsEnumerable();
}
/// <summary>
/// Converts a QueryResult of Entities to Dtos ...
/// <see cref="XAiProjectDto"/>
/// </summary>
/// <param name="queryResult"><see cref="XAiProject"/></param>
/// <returns></returns>
public async Task<XQueryResult<XAiProjectDto>> ToDtoQueryResult(XQueryResult<XAiProject> queryResult)
{
//
var result = new XQueryResult<XAiProjectDto>();
//
if (!queryResult.IsNullOrDefault())
{
//
result = result.UpdateData(
updateWith: queryResult,
propertyBlackList: new List<string>
{
nameof(XQueryResult<XAiProject>.Items)
}
);
//
if (queryResult.Items.HasChild())
{
result.Items = await ToDtos(queryResult.Items);
}
}
//
return result;
}
/// <summary>
/// Converts a QueryResult of Entities to Dtos ...
/// <see cref="XAiMessageDto"/>
/// </summary>
/// <param name="queryResult"><see cref="XAiMessage"/></param>
/// <returns></returns>
public async Task<XQueryResult<XAiMessageDto>> ToDtoQueryResult(XQueryResult<XAiMessage> queryResult)
{
//
var result = new XQueryResult<XAiMessageDto>();
//
if (!queryResult.IsNullOrDefault())
{
//
result = result.UpdateData(
updateWith: queryResult,
propertyBlackList: new List<string>
{
nameof(XQueryResult<XAiMessage>.Items)
}
);
//
if (queryResult.Items.HasChild())
{
result.Items = await ToDtos(queryResult.Items);
}
}
//
return result;
}
/// <summary>
/// Converts a QueryResult of Entities to Dtos ...
/// <see cref="XAiConversationDto"/>
/// </summary>
/// <param name="queryResult"><see cref="XAiConversation"/></param>
/// <returns></returns>
public async Task<XQueryResult<XAiConversationDto>> ToDtoQueryResult(XQueryResult<XAiConversation> queryResult)
{
//
var result = new XQueryResult<XAiConversationDto>();
//
if (!queryResult.IsNullOrDefault())
{
//
result = result.UpdateData(
updateWith: queryResult,
propertyBlackList: new List<string>
{
nameof(XQueryResult<XAiConversation>.Items)
}
);
//
if (queryResult.Items.HasChild())
{
result.Items = await ToDtos(queryResult.Items);
}
}
//
return result;
}
/// <summary>
/// Check Permission for Ai Actions ...
/// </summary>
/// <param name="userInfo"></param>
/// <returns></returns>
public async Task<bool> HasPermission(XUserClaimsInfoDto userInfo)
{
//
var result = true;
//
// TODO: Complete this ...
//
return await Task.FromResult(result);
}
/// <summary>
/// Prepare Prompt ...
/// </summary>
/// <param name="prompt"></param>
/// <param name="userInfo"></param>
/// <returns></returns>
public async Task<string> PreparePrompt(string prompt, XUserClaimsInfoDto userInfo)
{
//
var result = prompt;
//
// TODO: Complete this ...
//
return await Task.FromResult(result);
}
#endregion
/// <summary>
/// Ask a Question ...
/// </summary>
/// <param name="prompt"></param>
/// <param name="conversationId"></param>
/// <param name="userInfo"></param>
/// <returns></returns>
public Task<XAiMessageDto> Ask(
string prompt,
Guid conversationId,
XUserClaimsInfoDto userInfo
)
{
throw new NotImplementedException();
}
//
#region Private ...
/// <summary>
/// Access Person class based on OwnerId ...
/// <see cref="XPersonDto"/>
/// </summary>
/// <param name="ownerId"></param>
/// <returns></returns>
private async Task<XPersonDto> GetPerson(string ownerId)
{
//
XPersonDto result = null;
//
if (!ownerId.IsNullOrEmpty())
{
//
var device = identityProvider.GetDevice();
var userInfo = await identityProvider.GetUserInfo(
device: device,
userSelectByParam: ownerId
);
if (userInfo.IsNullOrDefault())
{
XException.NotFound.Throw();
}
//
result = userInfo.ToXPersonDto();
}
//
return result;
}
#endregion
}
}
-1
View File
@@ -32,7 +32,6 @@
<!-- Dependencies -->
<ItemGroup>
<PackageReference Include="OllamaSharp" Version="5.4.25" />
<PackageReference Include="Microsoft.Extensions.AI" Version="10.4.1" />
<PackageReference Include="Microsoft.SemanticKernel" Version="1.65.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="5.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.11" />