Compare commits

..
11 Commits
Author SHA1 Message Date
saherelm 3a228439a5 add config for cache ttl ... 2026-11-15 21:26:41 +03:30
saherelm 13cd0745c6 last ... 2026-11-14 08:34:10 +03:30
saherelm 5bccce3475 apply fix on Create Conversation Validations ... 2026-11-11 11:56:11 +03:30
saherelm cddf2650f5 fix Create Conversation to Handle Default Project Id ... 2026-11-11 11:42:55 +03:30
saherelm fba69b274c adding allowed origin for Live Server ... 2026-11-10 18:35:11 +03:30
saherelm 0077008105 last ... 2026-11-10 17:29:59 +03:30
saherelm 3dc54332c2 fix ask with files ... 2026-11-10 11:56:55 +03:30
saherelm dab384ea37 adding cors origins ... 2026-11-10 10:40:00 +03:30
saherelm f6a916fa83 make it use global ip ... 2026-11-09 15:41:23 +03:30
saherelm 5656ec07d5 - .gitignore: +Logs
- Program.cs: +xLoggingService، +AddXTelemetryLogger، +AddXTelemetry
- Startup.cs: +UseXRequestLogger (non-development)
- xAiApi.csproj: +xLoggingService reference
2026-11-09 12:50:22 +03:30
saherelm c24c14eb74 add using XLoggingService ... 2026-11-08 22:55:56 +03:30
10 changed files with 242 additions and 75 deletions
+1
View File
@@ -1,6 +1,7 @@
#
bin/*
obj/*
Logs
#
Db/*
+143 -13
View File
@@ -536,6 +536,8 @@ namespace xAiApi.Controllers
prompt: request.Prompt,
minScore: request.MinScore,
connectionId: connectionId,
extractFileContents: false,
addFilesToKnowledge: false,
projectId: request.ProjectId,
cancellationToken: cancellationToken,
conversationId: request.ConversationId
@@ -634,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
)
{
@@ -642,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();
@@ -655,6 +689,7 @@ namespace xAiApi.Controllers
//
var result = await aiService.LoadConversations(
projectId: projectId,
ownerId: userInfo.UserId,
connectionId: connectionId,
cancellationToken: cancellationToken
);
@@ -679,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
)
{
@@ -687,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
);
@@ -949,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
//
+9 -3
View File
@@ -206,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
);
@@ -219,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
@@ -301,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
+5 -1
View File
@@ -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;
+1 -1
View File
@@ -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"
}
+21 -17
View File
@@ -27,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
{
@@ -836,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,
@@ -868,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
);
@@ -1076,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
@@ -1691,6 +1687,12 @@ namespace xAiApi.Providers
{
//
var fileContent = await DataContent.LoadFromAsync(stream, mimeType, cancellationToken);
if (additionalContents.IsNull())
{
additionalContents = [];
}
//
additionalContents = [.. additionalContents, fileContent];
}
}
@@ -1726,6 +1728,8 @@ namespace xAiApi.Providers
// Retrieve Conversation Message ...
var messages = await dataProvider
.LoadMessages(
ownerId: userInfo.UserId,
connectionId: connectionId,
conversationId: conversationId.Value,
cancellationToken: cancellationToken
);
+12 -2
View File
@@ -1,4 +1,3 @@
using xLoggingService.DI;
using System;
using System.IO;
using xAiApi.DI;
@@ -16,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;
@@ -62,7 +62,6 @@ namespace xAiApi
public void ConfigureServices(IServiceCollection services)
{
services.AddXLoggingService(Configuration);
//
// Register Validation Provider and all XCommons Module Services ...
services.AddXCommons(Configuration);
@@ -72,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);
@@ -268,6 +271,13 @@ namespace xAiApi
// Using Routing ...
app.UseRouting();
//
// Using Request Logging ...
if (!isDevelopmentEnvironment)
{
app.UseXRequestLogger();
}
//
// Use xIdentityService (Authentication) ...
app.UseXIdentityService();
+3
View File
@@ -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": {
+33 -34
View File
@@ -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",
@@ -51,44 +52,42 @@
"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",
"Microsoft.AspNetCore": "Warning"
},
"XLogger": {
"Enabled": true,
"ApplicationName": "xAiApi",
"EnvironmentName": "Production",
"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,
"Provider": "Microsoft.Data.Sqlite",
"ConnectionString": "Filename=./Db/XLogs.db",
"TableName": "XLogs",
"AutoCreateTable": true,
"MinimumLevel": "Warning"
},
"Event": {
"Enabled": false,
"Topic": "application-logs",
"Action": "ApplicationLogCreated",
"Sender": "xAiApi",
"MinimumLevel": "Error"
}
}
},
"AllowedHosts": "*"
+14 -4
View File
@@ -1,4 +1,5 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<!-- Runtime Definition -->
<PropertyGroup>
<Version>1.0.0</Version>
@@ -7,22 +8,28 @@
<Company>SaherElm IT Center</Company>
<PackageId>xSaherElm.xAiApi</PackageId>
<TargetFramework>netcoreapp3.1</TargetFramework>
<Description>
a WebAPI Project which contains all Business Logic of xSaherElm Project's AI.
</Description>
<!-- Icon Definition -->
<PackageIcon>icon.png</PackageIcon>
<!-- Fix Duplicate TargetFramework Issue -->
<GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>
</PropertyGroup>
<!-- Icon Handling -->
<ItemGroup>
<None Include="../../Resources/Images/favicon.png" Link="icon.png" Pack="true" PackagePath="\icon.png" />
</ItemGroup>
<!-- Local Modules -->
<ItemGroup>
</ItemGroup>
<!-- Local Dependencies -->
<ItemGroup>
<ProjectReference Include="../Modules/xCommons/xCommons.csproj" />
@@ -33,8 +40,10 @@
<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>
<!-- Dependencies -->
<ItemGroup>
<PackageReference Include="Tesseract" Version="5.2.0" />
@@ -58,12 +67,14 @@
<PackageReference Include="IdentityModel.AspNetCore.OAuth2Introspection" Version="5.0.0" />
<PackageReference Include="SkiaSharp.NativeAssets.Linux.NoDependencies" Version="3.116.1" />
</ItemGroup>
<!-- For XML Documentation Support -->
<PropertyGroup>
<NoWarn>$(NoWarn);1591</NoWarn>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
</PropertyGroup>
<!-- Ef Core -->
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.5">
@@ -71,11 +82,10 @@
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<!-- Create Required Folders -->
<Target Name="CreateRequiredFolder" AfterTargets="AfterPublish">
<MakeDir Directories="$(PublishDir)wwwroot" Condition="!Exists('$(PublishDir)wwwroot')" />
</Target>
<ItemGroup>
<ProjectReference Include="..\Modules\xLoggingService\xLoggingService.csproj" />
</ItemGroup>
</Project>