This commit is contained in:
2026-09-30 15:27:39 +03:30
parent 383e42a8d2
commit efaf71d962
4 changed files with 459 additions and 75 deletions
+222 -70
View File
@@ -2,6 +2,7 @@ using System;
using OpenAI;
using OllamaSharp;
using System.Linq;
using xModels.Dtos;
using System.Net.Http;
using System.Threading;
using xAiApi.Constants;
@@ -352,11 +353,13 @@ namespace xAiApi.Providers
/// </summary>
/// <param name="ownerId"></param>
/// <param name="connectionId"></param>
/// <param name="includeDefaults"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<IEnumerable<XAiProjectDto>> LoadProjects(
string ownerId,
string connectionId = null,
bool includeDefaults = false,
CancellationToken cancellationToken = default
)
{
@@ -377,42 +380,50 @@ namespace xAiApi.Providers
);
//
// Here we have to fill Conversations of Default Project ...
var defaultProjectId = result.GetDafualtProject(ownerId);
var defaultProject = result.FirstOrDefault(x => x.Id == defaultProjectId);
result = result.Where(x => x.Id != defaultProjectId);
// Handle Defaults Including ...
if (includeDefaults)
{
//
// Here we have to fill Conversations of Default Project ...
var defaultProjectId = result.GetDafualtProject(ownerId);
var defaultProject = result.FirstOrDefault(x => x.Id == defaultProjectId);
result = result.Where(x => x.Id != defaultProjectId);
//
// Reading Conversations of Specified Default Project ...
var defaultProjectConversations = await dataProvider.LoadConversations(
fillMessages: false,
connectionId: connectionId,
projectId: defaultProjectId,
cancellationToken: cancellationToken
);
var defaultConversationId = defaultProjectConversations.GetDefaultConversation(
ownerId: ownerId,
projectId: defaultProjectId
);
var defaultConversation = defaultProjectConversations.First(x => x.Id == defaultConversationId);
defaultProjectConversations = defaultProjectConversations.Where(x => x.Id != defaultConversationId);
//
// Reading Conversations of Specified Default Project ...
var defaultProjectConversations = await dataProvider.LoadConversations(
fillMessages: false,
connectionId: connectionId,
projectId: defaultProjectId,
cancellationToken: cancellationToken
);
var defaultConversationId = defaultProjectConversations.GetDefaultConversation(
ownerId: ownerId,
projectId: defaultProjectId
);
var defaultConversation = defaultProjectConversations.First(x => x.Id == defaultConversationId);
defaultProjectConversations = defaultProjectConversations.Where(x => x.Id != defaultConversationId);
//
// Reading Default Conversations Messages ...
var defaultConversationMessages = await dataProvider.LoadMessages(
cancellationToken: cancellationToken,
conversationId: defaultConversationId
);
//
// Reading Default Conversations Messages ...
var defaultConversationMessages = await dataProvider.LoadMessages(
cancellationToken: cancellationToken,
conversationId: defaultConversationId
);
//
defaultConversation.Messages = [.. defaultConversationMessages];
defaultProjectConversations = [defaultConversation, .. defaultProjectConversations];
result = [defaultProject, .. result];
//
defaultConversation.Messages = [.. defaultConversationMessages.OrderByDescending(m => m.CreatedOn)];
defaultProjectConversations = [defaultConversation, .. defaultProjectConversations];
defaultProject.Conversations = [.. defaultProjectConversations];
result = [defaultProject, .. result];
}
//
return result;
}
/// <summary>
/// Load Conversations of Specified AI Project ...
/// </summary>
@@ -470,7 +481,7 @@ namespace xAiApi.Providers
);
//
return result;
return result;
}
/// <summary>
@@ -647,6 +658,7 @@ namespace xAiApi.Providers
Role = XAiChatRole.User,
CreatedOn = DateTime.UtcNow,
ConversationId = conversationId,
ConversationTitle = conversation.Title
};
promptMessage = await dataProvider.AddMessage(
item: promptMessage,
@@ -678,9 +690,10 @@ namespace xAiApi.Providers
{
Content = answer,
OwnerId = ownerId,
Role = XAiChatRole.System,
CreatedOn = DateTime.UtcNow,
ConversationId = conversationId
Role = XAiChatRole.Assistant,
ConversationId = conversationId,
ConversationTitle = conversation.Title
};
result = await dataProvider.AddMessage(
item: result,
@@ -891,6 +904,149 @@ namespace xAiApi.Providers
}
#endregion
//
#region Query ...
/// <summary>
/// Query Projects ...
/// </summary>
/// <param name="ownerId"></param>
/// <param name="query"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<XQueryResult<XAiProjectDto>> QueryProjects(
string ownerId,
XQuery query,
CancellationToken cancellationToken = default
)
{
//
// Validate ...
if (ownerId.IsNullOrEmpty())
{
XException.InvalidArgs.Throw();
}
//
var result = await dataProvider.QueryProjects(
query: query,
ownerId: ownerId,
cancellationToken: cancellationToken
);
//
return result;
}
/// <summary>
/// Query Conversations ...
/// </summary>
/// <param name="ownerId"></param>
/// <param name="projectId"></param>
/// <param name="query"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<XQueryResult<XAiConversationDto>> QueryConversations(
string ownerId,
Guid projectId,
XQuery query,
CancellationToken cancellationToken = default
)
{
//
// Validate ...
if (ownerId.IsNullOrEmpty() ||
projectId.IsDefaultGuid())
{
XException.InvalidArgs.Throw();
}
//
// Retrieve Project ...
var project = await dataProvider.GetProject(
id: projectId,
fillConversations: false,
fillConversationMessages: false,
cancellationToken: cancellationToken
);
if (project.IsNullOrDefault())
{
XException.NotFound.Throw();
}
//
// Validate Owner ...
if (ownerId != project.OwnerId)
{
XException.NotAllowed.Throw();
}
//
var result = await dataProvider.QueryConversations(
query: query,
ownerId: ownerId,
projectId: projectId,
cancellationToken: cancellationToken
);
//
return result;
}
/// <summary>
/// Query Messages ...
/// </summary>
/// <param name="ownerId"></param>
/// <param name="conversationId"></param>
/// <param name="query"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<XQueryResult<XAiMessageDto>> QueryMessages(
string ownerId,
Guid conversationId,
XQuery query,
CancellationToken cancellationToken = default
)
{
//
// Validate ...
if (ownerId.IsNullOrEmpty() ||
conversationId.IsDefaultGuid())
{
XException.InvalidArgs.Throw();
}
//
// Retrieve Conversation ...
var conversation = await dataProvider.GetConversation(
id: conversationId,
fillMessages: false,
cancellationToken: cancellationToken
);
if (conversation.IsNullOrDefault())
{
XException.NotFound.Throw();
}
//
// Validate Owner ...
if (ownerId != conversation.OwnerId)
{
XException.NotAllowed.Throw();
}
//
var result = await dataProvider.QueryMessages(
query: query,
ownerId: ownerId,
conversationId: conversationId,
cancellationToken: cancellationToken
);
//
return result;
}
#endregion
/// <summary>
/// Base Dispose Action ...
/// </summary>
@@ -916,49 +1072,45 @@ namespace xAiApi.Providers
//
var result = new List<ChatMessage>();
//
// Retrieve Introduction Prompt ...
var introductionPrompt = configuration.GetPrompt(XAiApiConstants.XAiApiIntroductionPromptName);
if (introductionPrompt.IsNullOrEmpty())
{
XException.InvalidData.Throw();
}
//
var introductionMessage = new ChatMessage(
role: ChatRole.System,
content: introductionPrompt
);
//
result.Add(introductionMessage);
//
if (!project.IsNullOrDefault() &&
!project.Prompt.IsNullOrEmpty())
{
//
// TODO: Handle Project Files Here ...
var projectMessage = new ChatMessage(
role: ChatRole.System,
contents: [new TextContent(project.Prompt)]
);
result.Add(projectMessage);
}
//
if (messages.HasChild())
{
//
// if Messages Has Child, this means
// Default Messages Handled before ...
result = [.. messages.Select(m => m.ToChatMessages())];
}
else
{
//
// If Message Has not any Childs, this means
// First Asking Questions ...
//
// Retrieve Introduction Prompt ...
var introductionPrompt = configuration.GetPrompt(XAiApiConstants.XAiApiIntroductionPromptName);
if (introductionPrompt.IsNullOrEmpty())
{
XException.InvalidData.Throw();
}
//
var introductionMessage = new ChatMessage(
role: ChatRole.System,
content: introductionPrompt
);
//
result.Add(introductionMessage);
//
if (!project.IsNullOrDefault() &&
!project.Prompt.IsNullOrEmpty())
{
//
// TODO: Handle Project Files Here ...
var projectMessage = new ChatMessage(
role: ChatRole.System,
contents: [new TextContent(project.Prompt)]
);
result.Add(projectMessage);
}
// TODO: Here we can Implement Memory Management based on
// Last nth of Messages, or Summarization of Messages, etc ...
result = [..result, ..messages
.OrderBy(m => m.CreatedOn)
.Select(m => m.ToChatMessages())];
}
//