using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text.Json; using System.Text.Json.Serialization; using xAiService.Models.Dtos; using xCommons.Extensions; namespace xAiService.Helpers { public static class XPromptHelper { /// /// Holds Initialized Prompts ... /// private static XPromptsDto prompts; /// /// Initialize Prompt Helper ... /// /// /// /// public static void Initialize(string path = "prompts.json") { // // Create full Path and Check File Exists ... var fullPath = Path.Combine(AppContext.BaseDirectory, path); if (!File.Exists(fullPath)) { throw new Exception($"Prompts Resources not found: {fullPath}"); } // // Read File as Json Text ... var json = File.ReadAllText(fullPath); var options = new JsonSerializerOptions { AllowTrailingCommas = true, PropertyNameCaseInsensitive = true, ReadCommentHandling = JsonCommentHandling.Skip, Converters = { new JsonStringEnumConverter( namingPolicy: null, allowIntegerValues: false ) } }; // // Serialized Root Object ... var root = JsonSerializer.Deserialize(json, options); if (!root.IsNullOrDefault()) { prompts = new XPromptsDto { Prompts = root.Prompts }; } } /// /// Retrieve all Allowed Prompts as Enumerable ... /// /// public static IEnumerable GetPrompts() { return prompts!.Prompts.AsEnumerable(); } /// /// Get Specified Prompt ... /// /// /// /// public static XPromptDto GetPrompt(string name) { // return GetPrompts() .FirstOrDefault(p => p.Name == name); } } }