Compare commits
19
Commits
c61f8aec98
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3a228439a5 | ||
|
|
13cd0745c6 | ||
|
|
5bccce3475 | ||
|
|
cddf2650f5 | ||
|
|
fba69b274c | ||
|
|
0077008105 | ||
|
|
3dc54332c2 | ||
|
|
dab384ea37 | ||
|
|
f6a916fa83 | ||
|
|
5656ec07d5 | ||
|
|
c24c14eb74 | ||
|
|
b58f1dd736 | ||
|
|
31add513bc | ||
|
|
16fb715926 | ||
|
|
5307fe4d2f | ||
|
|
ae7f5920c1 | ||
|
|
97acf0a123 | ||
|
|
c7efdd5525 | ||
|
|
937e30dd9d |
@@ -1,6 +1,7 @@
|
||||
#
|
||||
bin/*
|
||||
obj/*
|
||||
Logs
|
||||
|
||||
#
|
||||
Db/*
|
||||
|
||||
@@ -45,5 +45,11 @@ namespace xAiApi.Configurations
|
||||
/// RAG Configuration ...
|
||||
/// </summary>
|
||||
public XAiDocumentRAGDescriptor RAGDescriptor {get; set;} = new();
|
||||
|
||||
/// <summary>
|
||||
/// DateTime Configuration ...
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public XDateTimeDescriptor DateTime { get; set; } = new();
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,7 @@ namespace xAiApi.Constants
|
||||
public const string XAiApiContentExtractionPromptName = "ContentExtraction";
|
||||
|
||||
//
|
||||
public const string XAiDefaultModelName = "Gemma";
|
||||
public const string XAiDefaultModelName = "Arion";
|
||||
public const string XAiEmbeddingModelName = "Embed";
|
||||
public const string XAiDefaultVisionModelName = "Qwen";
|
||||
public const string XAiDefaultThinkingModelName = "Qwen";
|
||||
|
||||
@@ -28,7 +28,6 @@ namespace xAiApi.Controllers
|
||||
/// Flow: Ingest Files -> SessionId -> Ask / AskStream -> Delete Session
|
||||
/// </summary>
|
||||
[Authorize]
|
||||
[Route("[controller]")]
|
||||
public class ChatWithDocumentsController : XBaseIdentityApiV1Controller
|
||||
{
|
||||
/// <summary>
|
||||
|
||||
@@ -25,7 +25,6 @@ namespace xAiApi.Controllers
|
||||
/// Flow: Ingest Files -> Ask / AskStream (Retrieve from DB -> Generate) -> Documents / Delete
|
||||
/// </summary>
|
||||
[Authorize]
|
||||
[Route("[controller]")]
|
||||
public class KnowledgeController : XBaseIdentityApiV1Controller
|
||||
{
|
||||
/// <summary>
|
||||
|
||||
@@ -0,0 +1,501 @@
|
||||
using System;
|
||||
using xModels.Dtos;
|
||||
using xIdentityHelper;
|
||||
using System.Threading;
|
||||
using xAiApi.Interfaces;
|
||||
using xCommons.Providers;
|
||||
using xCommons.Extensions;
|
||||
using xAiModels.Extensions;
|
||||
using xAiModels.Models.Dtos;
|
||||
using xExceptions.Constants;
|
||||
using System.Threading.Tasks;
|
||||
using xCommons.Configurations;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Collections.Generic;
|
||||
using xIdentityService.Interfaces;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using xIdentityService.Controllers;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
|
||||
namespace xAiApi.Controllers
|
||||
{
|
||||
[Authorize]
|
||||
public class McpServersController : XBaseIdentityApiV1Controller
|
||||
{
|
||||
private readonly IXAiMcpServerProvider mcpServerProvider;
|
||||
|
||||
public McpServersController(
|
||||
XAppConfiguration appConfiguration,
|
||||
IXIdentityProvider identityProvider,
|
||||
ILogger<McpServersController> logger,
|
||||
XValidationProvider validationProvider,
|
||||
IXAiMcpServerProvider mcpServerProvider
|
||||
) : base(
|
||||
logger,
|
||||
appConfiguration,
|
||||
identityProvider,
|
||||
validationProvider
|
||||
)
|
||||
{
|
||||
this.mcpServerProvider = mcpServerProvider;
|
||||
}
|
||||
|
||||
//
|
||||
#region Provider ...
|
||||
/// <summary>
|
||||
/// Retrieve Specified MCP Server ...
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("{id}")]
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
public virtual async Task<ActionResult<XAiMcpServerDto>> GetMcpServer(
|
||||
[FromRoute] Guid id,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (!ModelState.IsValid || id.IsDefaultGuid())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var userInfo = await GetUserInfo();
|
||||
var connectionId = GetConnectionId();
|
||||
|
||||
//
|
||||
var result = await mcpServerProvider.GetMcpServer(
|
||||
id: id,
|
||||
userInfo: userInfo,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result.ToDynamicObject());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add MCP Server ...
|
||||
/// </summary>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
public virtual async Task<ActionResult<XAiMcpServerDto>> AddMcpServer(
|
||||
[FromBody] XAiMcpServerDto item,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (!ModelState.IsValid || item.IsNullOrDefault())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var userInfo = await GetUserInfo();
|
||||
var connectionId = GetConnectionId();
|
||||
|
||||
//
|
||||
var result = await mcpServerProvider.AddMcpServer(
|
||||
item: item,
|
||||
userInfo: userInfo,
|
||||
connectionId: connectionId,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result.ToDynamicObject());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update Specified MCP Server ...
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPut("{id}")]
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
public virtual async Task<ActionResult<XAiMcpServerDto>> UpdateMcpServer(
|
||||
[FromRoute] Guid id,
|
||||
[FromBody] XAiMcpServerDto item,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (id.IsDefaultGuid() ||
|
||||
!ModelState.IsValid ||
|
||||
!item.IsValidForAddOrUpdate())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var userInfo = await GetUserInfo();
|
||||
var connectionId = GetConnectionId();
|
||||
|
||||
//
|
||||
var result = await mcpServerProvider.UpdateMcpServer(
|
||||
id: id,
|
||||
item: item,
|
||||
userInfo: userInfo,
|
||||
connectionId: connectionId,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result.ToDynamicObject());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove Specified MCP Server ...
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[HttpDelete("{id}")]
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
public virtual async Task<ActionResult<XAiMcpServerDto>> RemoveMcpServer(
|
||||
[FromRoute] Guid id,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (!ModelState.IsValid || id.IsDefaultGuid())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var userInfo = await GetUserInfo();
|
||||
var connectionId = GetConnectionId();
|
||||
|
||||
//
|
||||
var result = await mcpServerProvider.RemoveMcpServer(
|
||||
id: id,
|
||||
userInfo: userInfo,
|
||||
connectionId: connectionId,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result.ToDynamicObject());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Enable Specified MCP Server ...
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost("{id}/Enable")]
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
public virtual async Task<ActionResult<bool>> EnableMcpServer(
|
||||
[FromRoute] Guid id,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (!ModelState.IsValid || id.IsDefaultGuid())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var userInfo = await GetUserInfo();
|
||||
var connectionId = GetConnectionId();
|
||||
|
||||
//
|
||||
var result = await mcpServerProvider.EnableMcpServer(
|
||||
id: id,
|
||||
userInfo: userInfo,
|
||||
connectionId: connectionId,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Disable Specified MCP Server ...
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost("{id}/Disable")]
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
public virtual async Task<ActionResult<bool>> DisableMcpServer(
|
||||
[FromRoute] Guid id,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (!ModelState.IsValid || id.IsDefaultGuid())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var userInfo = await GetUserInfo();
|
||||
var connectionId = GetConnectionId();
|
||||
|
||||
//
|
||||
var result = await mcpServerProvider.DisableMcpServer(
|
||||
id: id,
|
||||
userInfo: userInfo,
|
||||
connectionId: connectionId,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Toggle Specified MCP Server ...
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost("{id}/Toggle")]
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
public virtual async Task<ActionResult<bool>> ToggleMcpServer(
|
||||
[FromRoute] Guid id,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (!ModelState.IsValid || id.IsDefaultGuid())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var userInfo = await GetUserInfo();
|
||||
var connectionId = GetConnectionId();
|
||||
|
||||
//
|
||||
var result = await mcpServerProvider.ToggleMcpServer(
|
||||
id: id,
|
||||
userInfo: userInfo,
|
||||
connectionId: connectionId,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get all Registered MCP Servers by handling Ownership ...
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
public virtual async Task<ActionResult<IEnumerable<XAiMcpServerDto>>> GetMcpServers(
|
||||
[FromQuery] bool forceOwner = true,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var userInfo = await GetUserInfo();
|
||||
var connectionId = GetConnectionId();
|
||||
|
||||
//
|
||||
var result = await mcpServerProvider.GetAll(
|
||||
userInfo: userInfo,
|
||||
forceOwner: forceOwner,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result.ToDynamicObject());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Query Registered MCP Servers by handling Ownership ...
|
||||
/// </summary>
|
||||
/// <param name="query"></param>
|
||||
/// <param name="forceOwner"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("Query")]
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
public virtual async Task<ActionResult<XQueryResult<XAiMcpServerDto>>> QueryMcpServers(
|
||||
[FromQuery] XQuery query,
|
||||
[FromQuery] bool forceOwner = true,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
await ValidationProvider
|
||||
.GroupValidationBuilder()
|
||||
.AddNotNull(query)
|
||||
.ValidateGroupAsync();
|
||||
|
||||
//
|
||||
var userInfo = await GetUserInfo();
|
||||
var connectionId = GetConnectionId();
|
||||
|
||||
//
|
||||
var result = await mcpServerProvider.Query(
|
||||
query: query,
|
||||
userInfo: userInfo,
|
||||
forceOwner: forceOwner,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result.ToDynamicObject());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Refresh MCP Servers ...
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost("Refresh")]
|
||||
[Authorize(Policy = XPolicies.EnabledAdmin)]
|
||||
public async Task<ActionResult> RefreshMcpServers(
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
var userInfo = await GetUserInfo();
|
||||
|
||||
//
|
||||
await mcpServerProvider.RefreshAsync(
|
||||
userInfo: userInfo,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return Ok();
|
||||
} catch(Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
using xAiApi.Interfaces;
|
||||
using xCommons.Providers;
|
||||
using xCommons.Configurations;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using xIdentityService.Interfaces;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
@@ -9,7 +8,6 @@ using Microsoft.AspNetCore.Authorization;
|
||||
namespace xAiApi.Controllers
|
||||
{
|
||||
[Authorize]
|
||||
[Route("[controller]")]
|
||||
public class XAiController : XAiServiceControllerBase<IXAiService>
|
||||
{
|
||||
public XAiController(
|
||||
|
||||
@@ -24,7 +24,6 @@ using Microsoft.AspNetCore.Authorization;
|
||||
namespace xAiApi.Controllers
|
||||
{
|
||||
[Authorize]
|
||||
[Route("[controller]")]
|
||||
public abstract class XAiServiceControllerBase<TAiService> : XBaseIdentityApiV1Controller
|
||||
where TAiService : IXAiServiceBase
|
||||
{
|
||||
@@ -68,9 +67,13 @@ namespace xAiApi.Controllers
|
||||
// Validate ...
|
||||
ValidationProvider.NotEmpty(question);
|
||||
|
||||
//
|
||||
var userInfo = await GetUserInfo();
|
||||
|
||||
//
|
||||
var result = await aiService.AskTextAsync(
|
||||
prompt: question,
|
||||
userInfo: userInfo,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
@@ -113,9 +116,13 @@ namespace xAiApi.Controllers
|
||||
// Configure SSE Response ...
|
||||
Response.PrepareSseResponse();
|
||||
|
||||
//
|
||||
var userInfo = await GetUserInfo();
|
||||
|
||||
//
|
||||
var enumerable = aiService.AskTextAsEnumerable(
|
||||
prompt: question,
|
||||
userInfo: userInfo,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
@@ -325,7 +332,7 @@ namespace xAiApi.Controllers
|
||||
files: null,
|
||||
topK: request.TopK,
|
||||
userInfo: userInfo,
|
||||
type: request.Type,
|
||||
type: request.Type,
|
||||
prompt: request.Prompt,
|
||||
minScore: request.MinScore,
|
||||
connectionId: connectionId,
|
||||
@@ -382,7 +389,7 @@ namespace xAiApi.Controllers
|
||||
files: null,
|
||||
topK: request.TopK,
|
||||
userInfo: userInfo,
|
||||
type: request.Type,
|
||||
type: request.Type,
|
||||
prompt: request.Prompt,
|
||||
minScore: request.MinScore,
|
||||
connectionId: connectionId,
|
||||
@@ -467,7 +474,7 @@ namespace xAiApi.Controllers
|
||||
files: files,
|
||||
topK: request.TopK,
|
||||
userInfo: userInfo,
|
||||
type: request.Type,
|
||||
type: request.Type,
|
||||
prompt: request.Prompt,
|
||||
minScore: request.MinScore,
|
||||
connectionId: connectionId,
|
||||
@@ -525,10 +532,12 @@ namespace xAiApi.Controllers
|
||||
files: files,
|
||||
topK: request.TopK,
|
||||
userInfo: userInfo,
|
||||
type: request.Type,
|
||||
type: request.Type,
|
||||
prompt: request.Prompt,
|
||||
minScore: request.MinScore,
|
||||
connectionId: connectionId,
|
||||
extractFileContents: false,
|
||||
addFilesToKnowledge: false,
|
||||
projectId: request.ProjectId,
|
||||
cancellationToken: cancellationToken,
|
||||
conversationId: request.ConversationId
|
||||
@@ -627,7 +636,7 @@ namespace xAiApi.Controllers
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
[HttpGet("Projects/{projectId}/Conversations")]
|
||||
public async Task<ActionResult<IEnumerable<XAiConversationDto>>> LoadConversations(
|
||||
[FromRoute] Guid projectId,
|
||||
[FromRoute] Guid? projectId,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
@@ -635,12 +644,44 @@ namespace xAiApi.Controllers
|
||||
try
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (projectId.IsDefaultGuid())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
var userInfo = await GetUserInfo();
|
||||
var connectionId = GetConnectionId();
|
||||
|
||||
//
|
||||
var result = await aiService.LoadConversations(
|
||||
projectId: projectId,
|
||||
ownerId: userInfo.UserId,
|
||||
connectionId: connectionId,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve Specified AI Project's Conversations ...
|
||||
/// </summary>
|
||||
/// <param name="projectId"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
[HttpGet("Projects/Conversations")]
|
||||
public async Task<ActionResult<IEnumerable<XAiConversationDto>>> LoadProjectConversations(
|
||||
[FromQuery] Guid? projectId,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
var userInfo = await GetUserInfo();
|
||||
var connectionId = GetConnectionId();
|
||||
@@ -648,6 +689,7 @@ namespace xAiApi.Controllers
|
||||
//
|
||||
var result = await aiService.LoadConversations(
|
||||
projectId: projectId,
|
||||
ownerId: userInfo.UserId,
|
||||
connectionId: connectionId,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
@@ -672,7 +714,7 @@ namespace xAiApi.Controllers
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
[HttpGet("Conversations/{conversationId}/Messages")]
|
||||
public async Task<ActionResult<IEnumerable<XAiMessageDto>>> LoadMessages(
|
||||
[FromRoute] Guid conversationId,
|
||||
[FromRoute] Guid? conversationId,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
@@ -680,14 +722,52 @@ namespace xAiApi.Controllers
|
||||
try
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (conversationId.IsDefaultGuid())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
var userInfo = await GetUserInfo();
|
||||
var connectionId = GetConnectionId();
|
||||
|
||||
//
|
||||
var result = await aiService.LoadMessages(
|
||||
ownerId: userInfo.UserId,
|
||||
connectionId: connectionId,
|
||||
conversationId: conversationId,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve Specified AI Conversation's Messages ...
|
||||
/// </summary>
|
||||
/// <param name="conversationId"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
[HttpGet("Conversations/Messages")]
|
||||
public async Task<ActionResult<IEnumerable<XAiMessageDto>>> LoadConversationMessages(
|
||||
[FromQuery] Guid? conversationId,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
var userInfo = await GetUserInfo();
|
||||
var connectionId = GetConnectionId();
|
||||
|
||||
//
|
||||
var result = await aiService.LoadMessages(
|
||||
ownerId: userInfo.UserId,
|
||||
connectionId: connectionId,
|
||||
conversationId: conversationId,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
@@ -942,6 +1022,63 @@ namespace xAiApi.Controllers
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create an AI Conversation ...
|
||||
/// </summary>
|
||||
/// <param name="projectId"></param>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
[HttpPost("Projects/Conversations")]
|
||||
public async Task<ActionResult<XAiConversationDto>> CreateProjectConversation(
|
||||
[FromQuery] Guid? projectId,
|
||||
[FromBody] XAiConversationDto item,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (item.IsNullOrDefault())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var userInfo = await GetUserInfo();
|
||||
var connectionId = GetConnectionId();
|
||||
|
||||
//
|
||||
// Update Required Params ...
|
||||
item.OwnerId = userInfo.UserId;
|
||||
if (!item.IsValidForAddOrUpdate())
|
||||
{
|
||||
XException.InvalidData.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var result = await aiService.CreateConversation(
|
||||
item: item,
|
||||
projectId: projectId,
|
||||
ownerId: userInfo.UserId,
|
||||
connectionId: connectionId,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
//
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using xDataService.Db;
|
||||
using xAiApi.Constants;
|
||||
using xAiModels.Providers;
|
||||
using xDataService.Configuration;
|
||||
using xDataService.Db;
|
||||
using xTagService.Providers;
|
||||
using xDataService.Providers;
|
||||
using xFileService.Providers;
|
||||
using xStringService.Providers;
|
||||
using xTagService.Providers;
|
||||
using xDataService.Configuration;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace xAiApi.Database
|
||||
{
|
||||
@@ -41,6 +41,7 @@ namespace xAiApi.Database
|
||||
nameof(XAiVectorEntityRegisterar),
|
||||
nameof(XAiMessageEntityRegisterar),
|
||||
nameof(XAiProjectEntityRegisterar),
|
||||
nameof(XAiMcpServerEntityRegisterar),
|
||||
nameof(XAiConversationEntityRegisterar),
|
||||
]
|
||||
)
|
||||
|
||||
@@ -325,5 +325,27 @@ namespace xAiApi.Extensions
|
||||
return result;
|
||||
}
|
||||
|
||||
// public static IList<AITool> ToAITools(this IList<XAiMcpToolDefinition> source)
|
||||
// {
|
||||
// //
|
||||
// var result = new List<AITool>();
|
||||
|
||||
// //
|
||||
// foreach (var tool in source)
|
||||
// {
|
||||
// //
|
||||
// var declaration = AIFunctionFactory.CreateDeclaration(
|
||||
// name: tool.Name,
|
||||
// jsonSchema: tool.InputSchema,
|
||||
// description: tool.Description
|
||||
// );
|
||||
|
||||
// //
|
||||
// result.Add(declaration);
|
||||
// }
|
||||
|
||||
// //
|
||||
// return result;
|
||||
// }
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,11 @@
|
||||
using System.Linq;
|
||||
using xIdentityHelper;
|
||||
using xAiApi.Providers;
|
||||
using xAiApi.Interfaces;
|
||||
using xCommons.Extensions;
|
||||
using xCommons.Authorization;
|
||||
using System.Collections.Generic;
|
||||
using xIdentityService.Interfaces;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.HttpOverrides;
|
||||
@@ -11,6 +15,17 @@ namespace xAiApi.Extensions
|
||||
{
|
||||
public static class XProgramExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Register MCP Server Provider ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
public static void AddXAiMcpServerProvider(
|
||||
this IServiceCollection source
|
||||
)
|
||||
{
|
||||
source.AddScoped<IXAiMcpServerProvider, XAiMcpServerProvider>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Register Authorization Policies
|
||||
/// </summary>
|
||||
@@ -22,6 +37,7 @@ namespace xAiApi.Extensions
|
||||
)
|
||||
{
|
||||
//
|
||||
var policyHandler = services.GetRegisteredService<IXPolicyHandler>();
|
||||
services.AddAuthorization(options =>
|
||||
{
|
||||
//
|
||||
@@ -35,6 +51,26 @@ namespace xAiApi.Extensions
|
||||
{
|
||||
var policyDescriptor = policiesEnumerator.Current;
|
||||
options.AddPolicy(policyDescriptor.Key, policyDescriptor.Value);
|
||||
|
||||
//
|
||||
// Add Policy to Policy Handler ...
|
||||
if (!policyHandler.IsNull())
|
||||
{
|
||||
//
|
||||
// Check Policy is RequiredRole Policy ...
|
||||
var roleRequirements = policyDescriptor.Value.Requirements
|
||||
.OfType<RequiredRolesRequirement>()
|
||||
.FirstOrDefault();
|
||||
if (!roleRequirements.IsNull() &&
|
||||
roleRequirements.Roles.HasChild())
|
||||
{
|
||||
//
|
||||
policyHandler.AddPolicy(
|
||||
policy: policyDescriptor.Key.ToNormalString(),
|
||||
requiredRoles: roleRequirements.Roles.Select(r => r.ToNormalString())
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -49,8 +85,32 @@ namespace xAiApi.Extensions
|
||||
{
|
||||
while (policiesEnumerator.MoveNext())
|
||||
{
|
||||
//
|
||||
var policyDescriptor = policiesEnumerator.Current;
|
||||
|
||||
//
|
||||
// Add Policy to Authorization ...
|
||||
options.AddPolicy(policyDescriptor.Key, policyDescriptor.Value);
|
||||
|
||||
//
|
||||
// Add Policy to Policy Handler ...
|
||||
if (!policyHandler.IsNull())
|
||||
{
|
||||
//
|
||||
// Check Policy is RequiredRole Policy ...
|
||||
var roleRequirements = policyDescriptor.Value.Requirements
|
||||
.OfType<RequiredRolesRequirement>()
|
||||
.FirstOrDefault();
|
||||
if (!roleRequirements.IsNull() &&
|
||||
roleRequirements.Roles.HasChild())
|
||||
{
|
||||
//
|
||||
policyHandler.AddPolicy(
|
||||
policy: policyDescriptor.Key.ToNormalString(),
|
||||
requiredRoles: roleRequirements.Roles.Select(r => r.ToNormalString())
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,228 @@
|
||||
using System;
|
||||
using xModels.Dtos;
|
||||
using System.Threading;
|
||||
using xAiModels.Models.Dtos;
|
||||
using xIdentityModels.Models;
|
||||
using System.Threading.Tasks;
|
||||
using System.Linq.Expressions;
|
||||
using xAiModels.Models.Entities;
|
||||
using System.Collections.Generic;
|
||||
using ModelContextProtocol.Client;
|
||||
using Microsoft.Extensions.AI;
|
||||
|
||||
namespace xAiApi.Interfaces
|
||||
{
|
||||
/// <summary>
|
||||
/// MCP Manager / Provider ...
|
||||
/// </summary>
|
||||
public interface IXAiMcpServerProvider
|
||||
{
|
||||
//
|
||||
#region Manager ...
|
||||
/// <summary>
|
||||
/// Get a List Of Available Tools which Provided using registered Mcp Servers ...
|
||||
/// </summary>
|
||||
/// <param name="forceOwner"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<IList<AITool>> GetAvailableToolsAsync(
|
||||
bool forceOwner = false,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Test Specified Mcp Server ...
|
||||
/// </summary>
|
||||
/// <param name="server"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<string> TestServerAsync(
|
||||
XAiMcpServerDto server,
|
||||
string connectionId = null,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Force Refresh Mcp Servers ...
|
||||
/// </summary>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task RefreshAsync(
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
#endregion
|
||||
|
||||
//
|
||||
#region Actions ...
|
||||
/// <summary>
|
||||
/// Add an Mcp Server ...
|
||||
/// </summary>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<XAiMcpServerDto> AddMcpServer(
|
||||
XAiMcpServerDto item,
|
||||
string connectionId = null,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Get an Mcp Server ...
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<XAiMcpServerDto> GetMcpServer(
|
||||
Guid id,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Update Specified Mcp Server ...
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<XAiMcpServerDto> UpdateMcpServer(
|
||||
Guid id,
|
||||
XAiMcpServerDto item,
|
||||
string connectionId = null,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Remove Specified Mcp Server ...
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<XAiMcpServerDto> RemoveMcpServer(
|
||||
Guid id,
|
||||
string connectionId = null,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Disable Specified Mcp Server ...
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<bool> DisableMcpServer(
|
||||
Guid id,
|
||||
string connectionId = null,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Enable Specified Mcp Server ...
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<bool> EnableMcpServer(
|
||||
Guid id,
|
||||
string connectionId = null,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Toggle Specified Mcp Server ...
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<bool> ToggleMcpServer(
|
||||
Guid id,
|
||||
string connectionId = null,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve all Mcp Server ...
|
||||
/// </summary>
|
||||
/// <param name="forceOwner"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<IEnumerable<XAiMcpServerDto>> GetAll(
|
||||
bool forceOwner = false,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Find Specified Mcp Server ...
|
||||
/// </summary>
|
||||
/// <param name="predicate"></param>
|
||||
/// <param name="forceOwner"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<XAiMcpServerDto> FindOne(
|
||||
Expression<Func<XAiMcpServer, bool>> predicate,
|
||||
bool forceOwner = false,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Find a Collection of Specified Mcp Servers ...
|
||||
/// </summary>
|
||||
/// <param name="predicate"></param>
|
||||
/// <param name="forceOwner"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<IEnumerable<XAiMcpServerDto>> FindMany(
|
||||
Expression<Func<XAiMcpServer, bool>> predicate,
|
||||
bool forceOwner = false,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Query Registered Mcp Servers ...
|
||||
/// </summary>
|
||||
/// <param name="query"></param>
|
||||
/// <param name="forceOwner"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<XQueryResult<XAiMcpServerDto>> Query(
|
||||
XQuery query,
|
||||
bool forceOwner = false,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -32,10 +32,12 @@ namespace xAiApi.Interfaces
|
||||
/// Base Method for Communicate with LLM ...
|
||||
/// </summary>
|
||||
/// <param name="prompt"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<string> AskTextAsync(
|
||||
string prompt,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
|
||||
@@ -43,10 +45,12 @@ namespace xAiApi.Interfaces
|
||||
/// Base Method for Communicate with LLM ...
|
||||
/// </summary>
|
||||
/// <param name="prompt"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
IAsyncEnumerable<string> AskTextAsEnumerable(
|
||||
string prompt,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
|
||||
@@ -128,6 +132,8 @@ namespace xAiApi.Interfaces
|
||||
/// <param name="projectId"></param>
|
||||
/// <param name="conversationId"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <param name="extractFileContents"></param>
|
||||
/// <param name="addFilesToKnowledge"></param>
|
||||
/// <param name="files"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
@@ -140,6 +146,8 @@ namespace xAiApi.Interfaces
|
||||
Guid? projectId = null,
|
||||
Guid? conversationId = null,
|
||||
string connectionId = null,
|
||||
bool extractFileContents = false,
|
||||
bool addFilesToKnowledge = true,
|
||||
IFormFileCollection files = null,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
CancellationToken cancellationToken = default
|
||||
@@ -155,6 +163,8 @@ namespace xAiApi.Interfaces
|
||||
/// <param name="projectId"></param>
|
||||
/// <param name="conversationId"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <param name="extractFileContents"></param>
|
||||
/// <param name="addFilesToKnowledge"></param>
|
||||
/// <param name="files"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
@@ -167,6 +177,8 @@ namespace xAiApi.Interfaces
|
||||
Guid? projectId = null,
|
||||
Guid? conversationId = null,
|
||||
string connectionId = null,
|
||||
bool extractFileContents = false,
|
||||
bool addFilesToKnowledge = true,
|
||||
IFormFileCollection files = null,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
CancellationToken cancellationToken = default
|
||||
@@ -194,11 +206,13 @@ namespace xAiApi.Interfaces
|
||||
/// Load Conversations of Specified AI Project ...
|
||||
/// </summary>
|
||||
/// <param name="projectId"></param>
|
||||
/// <param name="ownerId"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<IEnumerable<XAiConversationDto>> LoadConversations(
|
||||
Guid projectId,
|
||||
Guid? projectId,
|
||||
string ownerId = null,
|
||||
string connectionId = null,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
@@ -207,10 +221,14 @@ namespace xAiApi.Interfaces
|
||||
/// Load Messages of Specified Conversation ...
|
||||
/// </summary>
|
||||
/// <param name="conversationId"></param>
|
||||
/// <param name="ownerId"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<IEnumerable<XAiMessageDto>> LoadMessages(
|
||||
Guid conversationId,
|
||||
Guid? conversationId,
|
||||
string ownerId = null,
|
||||
string connectionId = null,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
#endregion
|
||||
@@ -289,7 +307,7 @@ namespace xAiApi.Interfaces
|
||||
/// <returns></returns>
|
||||
Task<XAiConversationDto> CreateConversation(
|
||||
string ownerId,
|
||||
Guid projectId,
|
||||
Guid? projectId,
|
||||
XAiConversationDto item,
|
||||
string connectionId = null,
|
||||
CancellationToken cancellationToken = default
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
namespace xAiApi.Models
|
||||
{
|
||||
public class XDateTimeDescriptor
|
||||
{
|
||||
/// <summary>
|
||||
/// Adding Current DateTime to Prompt ...
|
||||
/// </summary>
|
||||
public bool InjectIntoSystemPrompt { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// Time Zone ...
|
||||
/// </summary>
|
||||
public string DefaultTimezone { get; set; } = "Asia/Tehran";
|
||||
|
||||
/// <summary>
|
||||
/// Georgian Date Format ...
|
||||
/// </summary>
|
||||
public string GregorianFormat { get; set; } = "yyyy-MM-dd HH:mm:ss";
|
||||
|
||||
/// <summary>
|
||||
/// Include Persian Calendar ...
|
||||
/// </summary>
|
||||
public bool IncludePersianCalendar { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// Include Day of Week ...
|
||||
/// </summary>
|
||||
public bool IncludeDayOfWeek { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// Include Unix Timestamp ...
|
||||
/// </summary>
|
||||
public bool IncludeUnixTimestamp { get; set; } = false;
|
||||
}
|
||||
}
|
||||
+5
-1
@@ -1,4 +1,5 @@
|
||||
using System.Text;
|
||||
using xLoggingService.DI;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
@@ -19,10 +20,13 @@ namespace xAiApi
|
||||
{
|
||||
//
|
||||
var result = Host.CreateDefaultBuilder(args)
|
||||
.ConfigureLogging(logging =>
|
||||
logging.AddXTelemetryLogger())
|
||||
.ConfigureWebHostDefaults(webBuilder =>
|
||||
{
|
||||
webBuilder.UseStartup<Startup>();
|
||||
});
|
||||
})
|
||||
.AddXTelemetry();
|
||||
|
||||
//
|
||||
return result;
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
"commandName": "Project",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "weatherforecast",
|
||||
"applicationUrl": "https://localhost:6001;http://localhost:6000",
|
||||
"applicationUrl": "https://localhost:6001;http://localhost:6000;https://0.0.0.0:6001;",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
|
||||
+366
-32
@@ -1,10 +1,12 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using xModels.Dtos;
|
||||
using xAiApi.Models;
|
||||
using System.Threading;
|
||||
using xAiApi.Constants;
|
||||
using xAiModels.Models;
|
||||
using TimeZoneConverter;
|
||||
using xAiApi.Interfaces;
|
||||
using xAiApi.Extensions;
|
||||
using xCommons.Providers;
|
||||
@@ -12,6 +14,7 @@ using xCommons.Extensions;
|
||||
using xAiModels.Constants;
|
||||
using xAiModels.Interfaces;
|
||||
using xAiModels.Extensions;
|
||||
using System.Globalization;
|
||||
using xAiApi.Configurations;
|
||||
using xExceptions.Constants;
|
||||
using xAiModels.Models.Dtos;
|
||||
@@ -24,6 +27,7 @@ using System.Collections.Generic;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using xAiApi.Interfaces.Extractors;
|
||||
using System.Runtime.CompilerServices;
|
||||
using MongoDB.Driver.Core.Connections;
|
||||
|
||||
namespace xAiApi.Providers
|
||||
{
|
||||
@@ -47,16 +51,22 @@ namespace xAiApi.Providers
|
||||
private readonly ILogger<XAIServiceBase> logger;
|
||||
private readonly XAiApiConfiguration configuration;
|
||||
private readonly XValidationProvider validationProvider;
|
||||
private readonly IXAiMcpServerProvider mcpServerProvider;
|
||||
private readonly IXFileContentExtractor fileContentExtractor;
|
||||
|
||||
//
|
||||
private readonly bool useProvidedMcpServers;
|
||||
|
||||
protected XAIServiceBase(
|
||||
IXAiRAGService ragProvider,
|
||||
IXAiDataProvider dataProvider,
|
||||
ILogger<XAIServiceBase> logger,
|
||||
XAiApiConfiguration configuration,
|
||||
XValidationProvider validationProvider,
|
||||
IXAiMcpServerProvider mcpServerProvider,
|
||||
IXFileContentExtractor fileContentExtractor,
|
||||
string model = null
|
||||
string model = null,
|
||||
bool useProvidedMcpServers = true
|
||||
)
|
||||
{
|
||||
//
|
||||
@@ -64,8 +74,10 @@ namespace xAiApi.Providers
|
||||
this.ragProvider = ragProvider;
|
||||
this.dataProvider = dataProvider;
|
||||
this.configuration = configuration;
|
||||
this.mcpServerProvider = mcpServerProvider;
|
||||
this.validationProvider = validationProvider;
|
||||
this.fileContentExtractor = fileContentExtractor;
|
||||
this.useProvidedMcpServers = useProvidedMcpServers;
|
||||
|
||||
//
|
||||
// Prepare Embedding Model Descriptor ...
|
||||
@@ -86,10 +98,12 @@ namespace xAiApi.Providers
|
||||
/// Base Method for Communicate with LLM ...
|
||||
/// </summary>
|
||||
/// <param name="prompt"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public virtual async Task<string> AskTextAsync(
|
||||
string prompt,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
@@ -103,6 +117,23 @@ namespace xAiApi.Providers
|
||||
//
|
||||
using var client = Descriptor.GetClient();
|
||||
var history = GetHistory(prompt);
|
||||
|
||||
//
|
||||
// Handle Provided MCP Servers ...
|
||||
if (useProvidedMcpServers)
|
||||
{
|
||||
//
|
||||
var availableTools = await mcpServerProvider
|
||||
.GetAvailableToolsAsync(
|
||||
userInfo: userInfo,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
Options.Tools = [.. availableTools];
|
||||
}
|
||||
|
||||
//
|
||||
var response = await client.GetResponseAsync(
|
||||
options: Options,
|
||||
messages: history,
|
||||
@@ -131,10 +162,12 @@ namespace xAiApi.Providers
|
||||
/// Base Method for Communicate with LLM ...
|
||||
/// </summary>
|
||||
/// <param name="prompt"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public virtual async IAsyncEnumerable<string> AskTextAsEnumerable(
|
||||
string prompt,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
[EnumeratorCancellation]
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
@@ -149,6 +182,23 @@ namespace xAiApi.Providers
|
||||
//
|
||||
using var client = Descriptor.GetClient();
|
||||
var history = GetHistory(prompt);
|
||||
|
||||
//
|
||||
// Handle Provided MCP Servers ...
|
||||
if (useProvidedMcpServers)
|
||||
{
|
||||
//
|
||||
var availableTools = await mcpServerProvider
|
||||
.GetAvailableToolsAsync(
|
||||
userInfo: userInfo,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
Options.Tools = [.. availableTools.Cast<AITool>()];
|
||||
}
|
||||
|
||||
//
|
||||
var enumerable = client.GetStreamingResponseAsync(
|
||||
options: Options,
|
||||
messages: history
|
||||
@@ -196,6 +246,23 @@ namespace xAiApi.Providers
|
||||
//
|
||||
using var client = Descriptor.GetClient();
|
||||
var history = GetHistory(prompt, context);
|
||||
|
||||
//
|
||||
// Handle Provided MCP Servers ...
|
||||
if (useProvidedMcpServers)
|
||||
{
|
||||
//
|
||||
var availableTools = await mcpServerProvider
|
||||
.GetAvailableToolsAsync(
|
||||
userInfo: userInfo,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
Options.Tools = [.. availableTools.Cast<AITool>()];
|
||||
}
|
||||
|
||||
//
|
||||
var response = await client.GetResponseAsync(
|
||||
options: Options,
|
||||
messages: history,
|
||||
@@ -245,6 +312,23 @@ namespace xAiApi.Providers
|
||||
//
|
||||
using var client = Descriptor.GetClient();
|
||||
var history = GetHistory(prompt, context);
|
||||
|
||||
//
|
||||
// Handle Provided MCP Servers ...
|
||||
if (useProvidedMcpServers)
|
||||
{
|
||||
//
|
||||
var availableTools = await mcpServerProvider
|
||||
.GetAvailableToolsAsync(
|
||||
userInfo: userInfo,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
Options.Tools = [.. availableTools.Cast<AITool>()];
|
||||
}
|
||||
|
||||
//
|
||||
var enumerable = client.GetStreamingResponseAsync(
|
||||
options: Options,
|
||||
messages: history,
|
||||
@@ -306,12 +390,32 @@ namespace xAiApi.Providers
|
||||
connectionId: null,
|
||||
conversationId: null,
|
||||
persistPrompt: false,
|
||||
additionalContents: null,
|
||||
addFilesToKnowledge: false,
|
||||
resolveConversation: false,
|
||||
extractFileContents: false,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
using var client = Descriptor.GetClient();
|
||||
|
||||
//
|
||||
// Handle Provided MCP Servers ...
|
||||
if (useProvidedMcpServers)
|
||||
{
|
||||
//
|
||||
var availableTools = await mcpServerProvider
|
||||
.GetAvailableToolsAsync(
|
||||
userInfo: userInfo,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
Options.Tools = [.. availableTools.Cast<AITool>()];
|
||||
}
|
||||
|
||||
//
|
||||
var response = await client
|
||||
.GetResponseAsync(
|
||||
options: Options,
|
||||
@@ -370,12 +474,32 @@ namespace xAiApi.Providers
|
||||
connectionId: null,
|
||||
conversationId: null,
|
||||
persistPrompt: false,
|
||||
additionalContents: null,
|
||||
resolveConversation: false,
|
||||
addFilesToKnowledge: false,
|
||||
extractFileContents: false,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
using var client = Descriptor.GetClient();
|
||||
|
||||
//
|
||||
// Handle Provided MCP Servers ...
|
||||
if (useProvidedMcpServers)
|
||||
{
|
||||
//
|
||||
var availableTools = await mcpServerProvider
|
||||
.GetAvailableToolsAsync(
|
||||
userInfo: userInfo,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
Options.Tools = [.. availableTools.Cast<AITool>()];
|
||||
}
|
||||
|
||||
//
|
||||
var stream = client
|
||||
.GetStreamingResponseAsync(
|
||||
options: Options,
|
||||
@@ -405,6 +529,8 @@ namespace xAiApi.Providers
|
||||
/// <param name="projectId"></param>
|
||||
/// <param name="conversationId"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <param name="extractFileContents"></param>
|
||||
/// <param name="addFilesToKnowledge"></param>
|
||||
/// <param name="files"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
@@ -417,6 +543,8 @@ namespace xAiApi.Providers
|
||||
Guid? projectId = null,
|
||||
Guid? conversationId = null,
|
||||
string connectionId = null,
|
||||
bool extractFileContents = false,
|
||||
bool addFilesToKnowledge = true,
|
||||
IFormFileCollection files = null,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
CancellationToken cancellationToken = default
|
||||
@@ -441,14 +569,34 @@ namespace xAiApi.Providers
|
||||
userInfo: userInfo,
|
||||
persistPrompt: true,
|
||||
projectId: projectId,
|
||||
additionalContents: null,
|
||||
resolveConversation: true,
|
||||
connectionId: connectionId,
|
||||
conversationId: conversationId,
|
||||
cancellationToken: cancellationToken
|
||||
cancellationToken: cancellationToken,
|
||||
extractFileContents: extractFileContents,
|
||||
addFilesToKnowledge: addFilesToKnowledge
|
||||
);
|
||||
|
||||
//
|
||||
using var client = Descriptor.GetClient();
|
||||
|
||||
//
|
||||
// Handle Provided MCP Servers ...
|
||||
if (useProvidedMcpServers)
|
||||
{
|
||||
//
|
||||
var availableTools = await mcpServerProvider
|
||||
.GetAvailableToolsAsync(
|
||||
userInfo: userInfo,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
Options.Tools = [.. availableTools.Cast<AITool>()];
|
||||
}
|
||||
|
||||
//
|
||||
var response = await client.GetResponseAsync(
|
||||
options: Options,
|
||||
messages: prep.History,
|
||||
@@ -491,6 +639,8 @@ namespace xAiApi.Providers
|
||||
/// <param name="projectId"></param>
|
||||
/// <param name="conversationId"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <param name="extractFileContents"></param>
|
||||
/// <param name="addFilesToKnowledge"></param>
|
||||
/// <param name="files"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
@@ -503,6 +653,8 @@ namespace xAiApi.Providers
|
||||
Guid? projectId = null,
|
||||
Guid? conversationId = null,
|
||||
string connectionId = null,
|
||||
bool extractFileContents = false,
|
||||
bool addFilesToKnowledge = true,
|
||||
IFormFileCollection files = null,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
[EnumeratorCancellation]
|
||||
@@ -528,14 +680,34 @@ namespace xAiApi.Providers
|
||||
userInfo: userInfo,
|
||||
persistPrompt: true,
|
||||
projectId: projectId,
|
||||
additionalContents: null,
|
||||
resolveConversation: true,
|
||||
connectionId: connectionId,
|
||||
conversationId: conversationId,
|
||||
cancellationToken: cancellationToken
|
||||
cancellationToken: cancellationToken,
|
||||
extractFileContents: extractFileContents,
|
||||
addFilesToKnowledge: addFilesToKnowledge
|
||||
);
|
||||
|
||||
//
|
||||
using var client = Descriptor.GetClient();
|
||||
|
||||
//
|
||||
// Handle Provided MCP Servers ...
|
||||
if (useProvidedMcpServers)
|
||||
{
|
||||
//
|
||||
var availableTools = await mcpServerProvider
|
||||
.GetAvailableToolsAsync(
|
||||
userInfo: userInfo,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
Options.Tools = [.. availableTools.Cast<AITool>()];
|
||||
}
|
||||
|
||||
//
|
||||
var stream = client
|
||||
.GetStreamingResponseAsync(
|
||||
options: Options,
|
||||
@@ -665,24 +837,20 @@ namespace xAiApi.Providers
|
||||
/// Load Conversations of Specified AI Project ...
|
||||
/// </summary>
|
||||
/// <param name="projectId"></param>
|
||||
/// <param name="ownerId"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public virtual async Task<IEnumerable<XAiConversationDto>> LoadConversations(
|
||||
Guid projectId,
|
||||
Guid? projectId,
|
||||
string ownerId = null,
|
||||
string connectionId = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate Args ...
|
||||
if (projectId.IsDefaultGuid())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var result = await dataProvider.LoadConversations(
|
||||
ownerId: ownerId,
|
||||
fillMessages: false,
|
||||
projectId: projectId,
|
||||
connectionId: connectionId,
|
||||
@@ -697,22 +865,21 @@ namespace xAiApi.Providers
|
||||
/// Load Messages of Specified Conversation ...
|
||||
/// </summary>
|
||||
/// <param name="conversationId"></param>
|
||||
/// <param name="ownerId"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public virtual async Task<IEnumerable<XAiMessageDto>> LoadMessages(
|
||||
Guid conversationId,
|
||||
Guid? conversationId,
|
||||
string ownerId = null,
|
||||
string connectionId = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate Args ...
|
||||
if (conversationId.IsDefaultGuid())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var result = await dataProvider.LoadMessages(
|
||||
ownerId: ownerId,
|
||||
connectionId: connectionId,
|
||||
conversationId: conversationId,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
@@ -905,7 +1072,7 @@ namespace xAiApi.Providers
|
||||
/// <returns></returns>
|
||||
public virtual async Task<XAiConversationDto> CreateConversation(
|
||||
string ownerId,
|
||||
Guid projectId,
|
||||
Guid? projectId,
|
||||
XAiConversationDto item,
|
||||
string connectionId = null,
|
||||
CancellationToken cancellationToken = default
|
||||
@@ -992,9 +1159,6 @@ namespace xAiApi.Providers
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
//
|
||||
// TODO: Implement File Save ...
|
||||
}
|
||||
|
||||
//
|
||||
@@ -1059,9 +1223,23 @@ namespace xAiApi.Providers
|
||||
[
|
||||
introductionPrompt,
|
||||
];
|
||||
|
||||
//
|
||||
// Prepare Datetime ...
|
||||
var datetimeContext = BuildDateTimeContext();
|
||||
if (!datetimeContext.IsNullOrEmpty())
|
||||
{
|
||||
//
|
||||
result.Add(new ChatMessage(
|
||||
ChatRole.Assistant,
|
||||
datetimeContext
|
||||
));
|
||||
}
|
||||
|
||||
//
|
||||
if (forcePrompt &&
|
||||
prompt is not null &&
|
||||
!string.IsNullOrWhiteSpace(prompt))
|
||||
!prompt.IsNullOrEmpty())
|
||||
{
|
||||
result.Add(promptMessage);
|
||||
}
|
||||
@@ -1091,6 +1269,7 @@ namespace xAiApi.Providers
|
||||
}
|
||||
|
||||
//
|
||||
var datetimeContext = BuildDateTimeContext();
|
||||
var contextBlock = BuildContextBlock(context);
|
||||
var systemText = introductionPrompt;
|
||||
if (context.HasChild() &&
|
||||
@@ -1099,7 +1278,17 @@ namespace xAiApi.Providers
|
||||
{
|
||||
//
|
||||
systemText =
|
||||
$"{introductionPrompt}{Environment.NewLine}{Environment.NewLine}" +
|
||||
$"{introductionPrompt}{Environment.NewLine}{Environment.NewLine}";
|
||||
|
||||
//
|
||||
if (!datetimeContext.IsNullOrEmpty())
|
||||
{
|
||||
//
|
||||
systemText += $"{datetimeContext}{Environment.NewLine}{Environment.NewLine}";
|
||||
}
|
||||
|
||||
//
|
||||
systemText +=
|
||||
$"{ragPrompt}{Environment.NewLine}{Environment.NewLine}" +
|
||||
$"Context:{Environment.NewLine}{contextBlock}";
|
||||
}
|
||||
@@ -1133,6 +1322,9 @@ namespace xAiApi.Providers
|
||||
//
|
||||
var result = new List<ChatMessage>();
|
||||
|
||||
//
|
||||
await Task.FromResult(true);
|
||||
|
||||
//
|
||||
// Retrieve Introduction Prompt ...
|
||||
var ragPrompt = configuration.GetPrompt(XAiApiConstants.XAiApiRagPromptName);
|
||||
@@ -1157,12 +1349,22 @@ namespace xAiApi.Providers
|
||||
$"Context:{Environment.NewLine}{contextBlock}";
|
||||
}
|
||||
|
||||
//
|
||||
// Prepare Datetime ...
|
||||
var datetimeContext = BuildDateTimeContext();
|
||||
if (!datetimeContext.IsNullOrEmpty())
|
||||
{
|
||||
//
|
||||
systemText +=
|
||||
$"{datetimeContext}{Environment.NewLine}{Environment.NewLine}";
|
||||
}
|
||||
|
||||
//
|
||||
// Apply Project Prompt as System Text ...
|
||||
if (!project.IsNullOrDefault() &&
|
||||
!project.Prompt.IsNullOrEmpty())
|
||||
{
|
||||
systemText += $"{project.Prompt}{Environment.NewLine}";
|
||||
systemText += $"{project.Prompt}{Environment.NewLine}{Environment.NewLine}";
|
||||
}
|
||||
|
||||
//
|
||||
@@ -1279,17 +1481,20 @@ namespace xAiApi.Providers
|
||||
/// <summary>
|
||||
/// Retrieve Prepared Chat Context ...
|
||||
/// </summary>
|
||||
/// <param name="prompt"></param>
|
||||
/// <param name="type"></param>
|
||||
/// <param name="topK"></param>
|
||||
/// <param name="type"></param>
|
||||
/// <param name="prompt"></param>
|
||||
/// <param name="minScore"></param>
|
||||
/// <param name="projectId"></param>
|
||||
/// <param name="conversationId"></param>
|
||||
/// <param name="persistPrompt"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <param name="conversationId"></param>
|
||||
/// <param name="resolveConversation"></param>
|
||||
/// <param name="files"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="resolveConversation"></param>
|
||||
/// <param name="persistPrompt"></param>
|
||||
/// <param name="extractFileContents"></param>
|
||||
/// <param name="addFilesToKnowledge"></param>
|
||||
/// <param name="additionalContents"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
private async Task<XPreparedChatContext> PrepareChatRequirementsAsync(
|
||||
@@ -1304,6 +1509,8 @@ namespace xAiApi.Providers
|
||||
bool resolveConversation,
|
||||
IFormFileCollection files,
|
||||
XUserClaimsInfoDto userInfo,
|
||||
bool extractFileContents = true,
|
||||
bool addFilesToKnowledge = false,
|
||||
IList<AIContent> additionalContents = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
@@ -1394,7 +1601,8 @@ namespace xAiApi.Providers
|
||||
// Ingest Files if Provided and also Resolve Conversation Flagged ...
|
||||
if (
|
||||
files.HasChild() &&
|
||||
resolveConversation
|
||||
resolveConversation &&
|
||||
addFilesToKnowledge
|
||||
)
|
||||
{
|
||||
//
|
||||
@@ -1437,6 +1645,59 @@ namespace xAiApi.Providers
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// If there isn't Files need to add Files to Knowledge ...
|
||||
if (files.HasChild() &&
|
||||
!addFilesToKnowledge
|
||||
)
|
||||
{
|
||||
//
|
||||
// Extract File Content and add them to Additional Contents ...
|
||||
foreach (var file in files)
|
||||
{
|
||||
//
|
||||
var mimeType = file.ContentType?.ToLowerInvariant() ?? string.Empty;
|
||||
if (mimeType.IsNullOrEmpty() ||
|
||||
!fileContentExtractor.CanExtract(mimeType))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
//
|
||||
using var stream = file.OpenReadStream();
|
||||
|
||||
//
|
||||
// Extract File Content if necessary ...
|
||||
if (extractFileContents)
|
||||
{
|
||||
//
|
||||
var content = await fileContentExtractor.ExtractAsync(
|
||||
fileStream: stream,
|
||||
mimeType: mimeType,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
if (!content.IsNullOrEmpty())
|
||||
{
|
||||
//
|
||||
var fileContent = new TextContent($"File:{Environment.NewLine}{file.FileName}{Environment.NewLine}{content}");
|
||||
additionalContents = [.. additionalContents, fileContent];
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//
|
||||
var fileContent = await DataContent.LoadFromAsync(stream, mimeType, cancellationToken);
|
||||
if (additionalContents.IsNull())
|
||||
{
|
||||
additionalContents = [];
|
||||
}
|
||||
|
||||
//
|
||||
additionalContents = [.. additionalContents, fileContent];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Request Knowledge ...
|
||||
var knowledgeRequest = new XAiKnowledgeAskRequest
|
||||
@@ -1467,6 +1728,8 @@ namespace xAiApi.Providers
|
||||
// Retrieve Conversation Message ...
|
||||
var messages = await dataProvider
|
||||
.LoadMessages(
|
||||
ownerId: userInfo.UserId,
|
||||
connectionId: connectionId,
|
||||
conversationId: conversationId.Value,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
@@ -1522,6 +1785,77 @@ namespace xAiApi.Providers
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
private string BuildDateTimeContext()
|
||||
{
|
||||
//
|
||||
var cfg = configuration.DateTime;
|
||||
if (cfg == null || !cfg.InjectIntoSystemPrompt)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
var tz = ResolveTimezone(cfg.DefaultTimezone);
|
||||
var nowUtc = DateTime.UtcNow;
|
||||
var nowLocal = TimeZoneInfo.ConvertTimeFromUtc(nowUtc, tz);
|
||||
|
||||
//
|
||||
var sb = new StringBuilder();
|
||||
sb.AppendLine("[CURRENT DATETIME — for your reference]");
|
||||
sb.AppendLine($"Now (local, {tz.Id}): {nowLocal.ToString(cfg.GregorianFormat)}");
|
||||
|
||||
//
|
||||
if (cfg.IncludeDayOfWeek)
|
||||
{
|
||||
sb.AppendLine($"Day of week: {nowLocal:dddd}");
|
||||
}
|
||||
|
||||
//
|
||||
if (cfg.IncludePersianCalendar)
|
||||
{
|
||||
//
|
||||
var pc = new PersianCalendar();
|
||||
sb.AppendLine($"Now (Persian): {pc.GetYear(nowLocal):D4}/{pc.GetMonth(nowLocal):D2}/{pc.GetDayOfMonth(nowLocal):D2} {nowLocal:HH:mm:ss}");
|
||||
}
|
||||
|
||||
//
|
||||
if (cfg.IncludeUnixTimestamp)
|
||||
{
|
||||
sb.AppendLine($"Unix timestamp: {new DateTimeOffset(nowUtc).ToUnixTimeSeconds()}");
|
||||
}
|
||||
|
||||
//
|
||||
sb.AppendLine("Use this information when the user asks about 'today', 'now', 'this week', relative dates, or any time-sensitive calculations.");
|
||||
return sb.ToString().TrimEnd();
|
||||
}
|
||||
catch
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
private static TimeZoneInfo ResolveTimezone(string id)
|
||||
{
|
||||
//
|
||||
if (id.IsNullOrEmpty())
|
||||
{
|
||||
return TimeZoneInfo.Utc;
|
||||
}
|
||||
|
||||
//
|
||||
try
|
||||
{
|
||||
return TZConvert.GetTimeZoneInfo(id);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return TimeZoneInfo.Utc;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -16,6 +16,7 @@ namespace xAiApi.Providers
|
||||
ILogger<XAiService> logger,
|
||||
XAiApiConfiguration configuration,
|
||||
XValidationProvider validationProvider,
|
||||
IXAiMcpServerProvider mcpServerProvider,
|
||||
IXFileContentExtractor fileContentExtractor
|
||||
) : base(
|
||||
ragProvider,
|
||||
@@ -23,6 +24,7 @@ namespace xAiApi.Providers
|
||||
logger,
|
||||
configuration,
|
||||
validationProvider,
|
||||
mcpServerProvider,
|
||||
fileContentExtractor,
|
||||
XAiApiConstants.XAiDefaultModelName
|
||||
)
|
||||
|
||||
@@ -17,6 +17,7 @@ namespace xAiApi.Providers
|
||||
ILogger<XAIServiceBase> logger,
|
||||
XAiApiConfiguration configuration,
|
||||
XValidationProvider validationProvider,
|
||||
IXAiMcpServerProvider mcpServerProvider,
|
||||
IXFileContentExtractor fileContentExtractor,
|
||||
ReasoningEffort effort = ReasoningEffort.Medium,
|
||||
ReasoningOutput output = ReasoningOutput.Full
|
||||
@@ -26,6 +27,7 @@ namespace xAiApi.Providers
|
||||
logger,
|
||||
configuration,
|
||||
validationProvider,
|
||||
mcpServerProvider,
|
||||
fileContentExtractor,
|
||||
XAiApiConstants.XAiDefaultThinkingModelName
|
||||
)
|
||||
|
||||
+33
-2
@@ -3,6 +3,7 @@ using System.IO;
|
||||
using xAiApi.DI;
|
||||
using System.Linq;
|
||||
using xAiModels.DI;
|
||||
using xAiService.DI;
|
||||
using xTagService.DI;
|
||||
using System.Net.Http;
|
||||
using xAiApi.Database;
|
||||
@@ -14,6 +15,7 @@ using Newtonsoft.Json;
|
||||
using System.Reflection;
|
||||
using xAiApi.Extensions;
|
||||
using xStringService.DI;
|
||||
using xLoggingService.DI;
|
||||
using xStorageService.DI;
|
||||
using xIdentityService.DI;
|
||||
using xDataService.Models;
|
||||
@@ -69,6 +71,10 @@ namespace xAiApi
|
||||
services.AddXAppConfiguration(Configuration);
|
||||
var appConfiguration = services.GetRegisteredService<XAppConfiguration>();
|
||||
|
||||
//
|
||||
// Register Logging Service ...
|
||||
services.AddXLoggingService(Configuration);
|
||||
|
||||
//
|
||||
// Register Allowed Origins ...
|
||||
services.AddXCors(appConfiguration.AllowedOrigins);
|
||||
@@ -184,10 +190,14 @@ namespace xAiApi
|
||||
// Register XAiApi Configuration ...
|
||||
services.AddXAiApiConfiguration(Configuration);
|
||||
|
||||
//
|
||||
// Register MCP Server Provider ...
|
||||
services.AddXAiMcpServerProvider();
|
||||
|
||||
//
|
||||
// Register File Content Extractor Services ...
|
||||
services.AddXAiFileContentExtractors();
|
||||
|
||||
|
||||
//
|
||||
// Register Text Chunker Service ...
|
||||
services.AddXAiTextChunker();
|
||||
@@ -196,6 +206,10 @@ namespace xAiApi
|
||||
// Register Chat With Documents Services (Text Chunker + In-Memory Vector Store) ...
|
||||
services.AddXAiChatWithDocuments();
|
||||
|
||||
//
|
||||
// Register XAiMcpClient ...
|
||||
services.AddXAiMcpClient();
|
||||
|
||||
//
|
||||
// Persistent RAG (Knowledge over XAiVector Table) ...
|
||||
services.AddXRagService();
|
||||
@@ -205,7 +219,10 @@ namespace xAiApi
|
||||
services.AddXAiServices();
|
||||
}
|
||||
|
||||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
||||
public void Configure(
|
||||
IApplicationBuilder app,
|
||||
IWebHostEnvironment env
|
||||
)
|
||||
{
|
||||
//
|
||||
// Configure the HTTP request pipeline.
|
||||
@@ -222,6 +239,10 @@ namespace xAiApi
|
||||
app.UseHsts();
|
||||
}
|
||||
|
||||
//
|
||||
// Setting Environment in App Confiuration ...
|
||||
app.AddEnvironmentToXAppConfiguration(env.EnvironmentName);
|
||||
|
||||
//
|
||||
// Force Https Redirection ...
|
||||
app.UseHttpsRedirection();
|
||||
@@ -250,6 +271,13 @@ namespace xAiApi
|
||||
// Using Routing ...
|
||||
app.UseRouting();
|
||||
|
||||
//
|
||||
// Using Request Logging ...
|
||||
if (!isDevelopmentEnvironment)
|
||||
{
|
||||
app.UseXRequestLogger();
|
||||
}
|
||||
|
||||
//
|
||||
// Use xIdentityService (Authentication) ...
|
||||
app.UseXIdentityService();
|
||||
@@ -306,3 +334,6 @@ namespace xAiApi
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
{
|
||||
"AllowedOrigins": [
|
||||
"http://127.0.0.1:5500",
|
||||
"http://localhost:5000",
|
||||
"https://localhost:5001",
|
||||
"http://api.saherelm.ir",
|
||||
"https://api.saherelm.ir",
|
||||
"http://api.saherelmhub.ir",
|
||||
"http://192.168.11.29:5173",
|
||||
"http://192.168.11.29:5174",
|
||||
"https://api.saherelmhub.ir"
|
||||
],
|
||||
"IdentityServiceConfiguration": {
|
||||
@@ -37,9 +40,16 @@
|
||||
},
|
||||
"AiApiConfiguration": {
|
||||
"Models": [
|
||||
{
|
||||
"Name": "Arion",
|
||||
"Provider": "OpenAI",
|
||||
"LLM": "claude-fable-5-1",
|
||||
"Url": "https://arionhub.pro/v1",
|
||||
"ApiKey": "sk-AKJGD8J9gOCrQgc5oWPGtRGUo25PnnQGnMoipzdliYHa3otZ"
|
||||
},
|
||||
{
|
||||
"ApiKey": "",
|
||||
"Name": "Gemma",
|
||||
"Name": "Gemma_BKP",
|
||||
"LLM": "gemma3:1b",
|
||||
"Provider": "OpenAI",
|
||||
"Url": "http://178.131.88.93:1112/v1"
|
||||
@@ -130,7 +140,15 @@
|
||||
"MetadataMimeType": "mimeType",
|
||||
"PromptName": "ChatWithDocuments",
|
||||
"MetadataChunkIndex": "chunkIndex"
|
||||
}
|
||||
},
|
||||
"DateTime": {
|
||||
"IncludeDayOfWeek": true,
|
||||
"IncludeUnixTimestamp": false,
|
||||
"InjectIntoSystemPrompt": true,
|
||||
"IncludePersianCalendar": true,
|
||||
"DefaultTimezone": "Asia/Tehran",
|
||||
"GregorianFormat": "yyyy-MM-dd HH:mm:ss"
|
||||
}
|
||||
},
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
|
||||
+37
-1
@@ -4,6 +4,7 @@
|
||||
"DefaultLanguage": "fa-IR",
|
||||
"XPoweredValue": "SaherElmITCenter",
|
||||
"WelcomeMessage": "Welcome to xSaherElm Project's AI API",
|
||||
"DefaultCacheTtlMinutes": 10,
|
||||
"AllowedOrigins": [
|
||||
"http://api.saherelm.ir",
|
||||
"https://api.saherelm.ir",
|
||||
@@ -18,6 +19,9 @@
|
||||
"Name": "Hadi Khazaee Asl",
|
||||
"Email": "hadi_khazaee_asl@yahoo.com",
|
||||
"Url": "https://www.saherelm.ir"
|
||||
},
|
||||
"DefaultValues": {
|
||||
"version": "1.0"
|
||||
}
|
||||
},
|
||||
"HttpServiceConfiguration": {
|
||||
@@ -48,6 +52,38 @@
|
||||
"BaseRoute": "hubs",
|
||||
"AddSupportMessageProtocol": true
|
||||
},
|
||||
"LoggingServiceConfiguration": {
|
||||
"Enabled": true,
|
||||
"MinimumLevel": "Information",
|
||||
"Queue": {
|
||||
"Enabled": true,
|
||||
"Capacity": 10000,
|
||||
"ShutdownTimeoutSeconds": 10
|
||||
},
|
||||
"File": {
|
||||
"Enabled": true,
|
||||
"Directory": "./Logs",
|
||||
"FileName": "xAiApi-.log",
|
||||
"Format": "Json",
|
||||
"MinimumLevel": "Information",
|
||||
"RetainedFileCountLimit": 30
|
||||
},
|
||||
"Database": {
|
||||
"Enabled": false,
|
||||
"TableName": "XLogs",
|
||||
"AutoCreateTable": true,
|
||||
"MinimumLevel": "Warning",
|
||||
"Provider": "Microsoft.Data.Sqlite",
|
||||
"ConnectionString": "Filename=./Db/XLogs.db"
|
||||
},
|
||||
"Event": {
|
||||
"Enabled": false,
|
||||
"Sender": "xAiApi",
|
||||
"MinimumLevel": "Error",
|
||||
"Topic": "application-logs",
|
||||
"Action": "ApplicationLogCreated"
|
||||
}
|
||||
},
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
@@ -55,4 +91,4 @@
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
}
|
||||
|
||||
+3
-2
@@ -7,7 +7,6 @@
|
||||
<Authors>Hadi Khazaee Asl</Authors>
|
||||
<Company>SaherElm IT Center</Company>
|
||||
<PackageId>xSaherElm.xAiApi</PackageId>
|
||||
<!-- <TargetFramework>net8.0</TargetFramework> -->
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
|
||||
<Description>
|
||||
@@ -18,8 +17,8 @@
|
||||
<PackageIcon>icon.png</PackageIcon>
|
||||
|
||||
<!-- Fix Duplicate TargetFramework Issue -->
|
||||
<GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>
|
||||
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
|
||||
<GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>
|
||||
</PropertyGroup>
|
||||
|
||||
<!-- Icon Handling -->
|
||||
@@ -41,6 +40,7 @@
|
||||
<ProjectReference Include="../Modules/xHttpService/xHttpService.csproj" />
|
||||
<ProjectReference Include="../Modules/xFileService/xFileService.csproj" />
|
||||
<ProjectReference Include="../Modules/xStorageService/xStorageService.csproj" />
|
||||
<ProjectReference Include="..\Modules\xLoggingService\xLoggingService.csproj" />
|
||||
<ProjectReference Include="../Modules/xIdentityHelper/xIdentityHelper.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -57,6 +57,7 @@
|
||||
<PackageReference Include="RtfPipe" Version="2.0.7677.4303" />
|
||||
<PackageReference Include="HtmlAgilityPack" Version="1.13.0" />
|
||||
<PackageReference Include="Whisper.net.Runtime" Version="1.9.1" />
|
||||
<PackageReference Include="ModelContextProtocol" Version="2.3.1" />
|
||||
<PackageReference Include="OfficeIMO.PowerPoint" Version="3.5.1" />
|
||||
<PackageReference Include="OfficeIMO.OpenDocument" Version="3.5.1" />
|
||||
<PackageReference Include="DocumentFormat.OpenXml" Version="3.5.1" />
|
||||
|
||||
Reference in New Issue
Block a user