From 9b271d15725deb547d40f0de46749880ce499e26 Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Thu, 9 Apr 2026 17:01:45 +0330 Subject: [PATCH] last ... --- Configurations/XAIServiceConfiguration.cs | 20 +++ Constants/ConfigurationNodeNames.cs | 7 + DI/XDIHelperExtension.cs | 151 ++++++++++++++++++ Extensions/ModelExtensions.cs | 42 +++++ Models/XAILLMDescriptor.cs | 13 ++ .../netstandard2.0/xAiService.AssemblyInfo.cs | 2 +- .../xAiService.AssemblyInfoInputs.cache | 2 +- 7 files changed, 235 insertions(+), 2 deletions(-) create mode 100644 Configurations/XAIServiceConfiguration.cs create mode 100644 Constants/ConfigurationNodeNames.cs create mode 100644 DI/XDIHelperExtension.cs create mode 100644 Extensions/ModelExtensions.cs create mode 100644 Models/XAILLMDescriptor.cs diff --git a/Configurations/XAIServiceConfiguration.cs b/Configurations/XAIServiceConfiguration.cs new file mode 100644 index 0000000..6dadfc1 --- /dev/null +++ b/Configurations/XAIServiceConfiguration.cs @@ -0,0 +1,20 @@ +using System.Collections.Generic; +using xAiService.Models; + +namespace xAiService.Configurations +{ + public partial class XAIServiceConfiguration + { + /// + /// List of Text Generator Models ... + /// + /// + public Dictionary TextGenerators { get; set; } + + /// + /// List of Image Generator Models ... + /// + /// + public Dictionary ImageGenerators { get; set; } + } +} \ No newline at end of file diff --git a/Constants/ConfigurationNodeNames.cs b/Constants/ConfigurationNodeNames.cs new file mode 100644 index 0000000..578ba1f --- /dev/null +++ b/Constants/ConfigurationNodeNames.cs @@ -0,0 +1,7 @@ +namespace xAiService.Constants +{ + public partial struct ConfigurationNodeNames + { + public const string AI_SERVICE_NODE = "AIServiceConfiguration"; + } +} \ No newline at end of file diff --git a/DI/XDIHelperExtension.cs b/DI/XDIHelperExtension.cs new file mode 100644 index 0000000..a3a3a1d --- /dev/null +++ b/DI/XDIHelperExtension.cs @@ -0,0 +1,151 @@ +using System; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using xAiService.Configurations; +using xAiService.Constants; +using xAiService.Extensions; +using xCommons.Extensions; +using xExceptions.Constants; + +namespace xAiService.DI +{ + public static partial class XDIHelperExtension + { + /// + /// Extract AI Service Configuration ... + /// + /// + /// + public static XAIServiceConfiguration GetXAIServiceConfiguration(this IConfiguration source) + { + // + XAIServiceConfiguration result = null; + + // + var section = source.GetSection(ConfigurationNodeNames.AI_SERVICE_NODE); + if (!section.IsNull()) + { + result = section.Get(); + } + + // + return result; + } + + /// + /// Add AI Service Configuration ... + /// + /// + /// + public static void AddXAIServiceConfiguration( + this IServiceCollection source, + XAIServiceConfiguration configuration + ) + { + source.AddSingleton(configuration); + } + + public static void AddXAIService( + this IServiceCollection services, + IConfiguration configuration, + ServiceLifetime lifeTime = ServiceLifetime.Singleton + ) + { + // + var config = configuration.GetXAIServiceConfiguration(); + AddAIService( + services, + config + ); + } + + public static void AddXAIService( + this IServiceCollection services, + XAIServiceConfiguration configuration, + ServiceLifetime lifeTime = ServiceLifetime.Singleton + ) + { + // + AddAIService( + services, + configuration + ); + } + + // + #region Private ... + /// + /// a LogTag for Service ... + /// + private static string XLogTag = "XFileService"; + + /// + /// print a log in Console ... + /// + /// + private static void Log(string message) + { + Console.WriteLine($"{XLogTag} => {message}"); + } + + private static void AddAIService( + this IServiceCollection services, + XAIServiceConfiguration configuration, + ServiceLifetime lifeTime = ServiceLifetime.Singleton + ) + { + // + var exception = XException.InvalidArgs.ToException(); ; + + // + // Services ... + var isServicesExists = !services.IsNull(); + if (!isServicesExists) + { + // + Log("AddAIService failed, services not provided ..."); + throw exception; + } + + // + // Lifetime ... + var isLifetimeExists = !lifeTime.IsNull(); + if (!isLifetimeExists) + { + // + Log("AddAIService failed, lifetime not provided ..."); + throw exception; + } + + // + // Check Dependencies Exists ... + var isDependenciesPassed = true; + if (!isDependenciesPassed) + { + // + Log("AddAIService failed due Dependencies Issues, not provided ..."); + throw exception; + } + + // + // Validate Configurations ... + var isConfigurationExists = + !configuration.IsNull() && + configuration.IsValid(); + if (!isConfigurationExists) + { + // + Log("AddAIService failed due InValid Service Configuration or not provided ..."); + throw exception; + } + + // + // Register Configurations ... + services.AddXAIServiceConfiguration(configuration); + + // + // Register Services ... + } + #endregion + } +} \ No newline at end of file diff --git a/Extensions/ModelExtensions.cs b/Extensions/ModelExtensions.cs new file mode 100644 index 0000000..d6cac59 --- /dev/null +++ b/Extensions/ModelExtensions.cs @@ -0,0 +1,42 @@ +using xAiService.Configurations; +using xAiService.Models; +using xCommons.Extensions; + +namespace xAiService.Extensions +{ + public static class ModelExtensions + { + /// + /// Validating LLM Descriptor ... + /// + /// + /// + public static bool IsValid(this XAILLMDescriptor source) + { + // + var result = + !source.IsNull() && + !source.Url.IsNullOrEmpty() && + !source.Client.IsNullOrEmpty(); + + // + return result; + } + + /// + /// Validate XAIServiceConfiguration ... + /// + /// + /// + public static bool IsValid(this XAIServiceConfiguration source) + { + // + var result = + !source.IsNull() && + (source.TextGenerators.Count > 0); + + // + return result; + } + } +} \ No newline at end of file diff --git a/Models/XAILLMDescriptor.cs b/Models/XAILLMDescriptor.cs new file mode 100644 index 0000000..d94649f --- /dev/null +++ b/Models/XAILLMDescriptor.cs @@ -0,0 +1,13 @@ +using AutoMapper.Configuration.Conventions; + +namespace xAiService.Models +{ + public class XAILLMDescriptor + { + public string Url { get; set; } + public string Port { get; set; } + public string Name { get; set; } + public string Client { get; set; } + public string Descriptor { get; set; } + } +} \ No newline at end of file diff --git a/obj/Debug/netstandard2.0/xAiService.AssemblyInfo.cs b/obj/Debug/netstandard2.0/xAiService.AssemblyInfo.cs index 7d63984..65517be 100644 --- a/obj/Debug/netstandard2.0/xAiService.AssemblyInfo.cs +++ b/obj/Debug/netstandard2.0/xAiService.AssemblyInfo.cs @@ -15,7 +15,7 @@ using System.Reflection; [assembly: System.Reflection.AssemblyDescriptionAttribute(("\r\n it is a Part of xSaherElm project which Provides Services to use xSaherEl" + "m AI Features ...\r\n "))] [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] -[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+bbaa05905fc5dfb9bc9282f094c2d3248b9e486d")] +[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+c538f9555ab8bfc86ea124ed5bfbd2d63c27f7f9")] [assembly: System.Reflection.AssemblyProductAttribute("xAiService")] [assembly: System.Reflection.AssemblyTitleAttribute("xAiService")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] diff --git a/obj/Debug/netstandard2.0/xAiService.AssemblyInfoInputs.cache b/obj/Debug/netstandard2.0/xAiService.AssemblyInfoInputs.cache index 28698b5..5626683 100644 --- a/obj/Debug/netstandard2.0/xAiService.AssemblyInfoInputs.cache +++ b/obj/Debug/netstandard2.0/xAiService.AssemblyInfoInputs.cache @@ -1 +1 @@ -17bf4170f79b796f881d5d7ca98bdf22aaf9999471d839ed8109b7226a063919 +67816ea562b25f672b311e2c73d380d615ccf404089ec562f84575f6211c1c89