using System.Text.Json; namespace Tango.Portal.Chat.Web.Services { public sealed class SchemaRegistry { private readonly IWebHostEnvironment _env; private readonly ILogger _log; private readonly AIInstructionService _instructionService; private string? _cached; public SchemaRegistry(IWebHostEnvironment env, ILogger log, AIInstructionService instructionService) { _env = env; _log = log; _instructionService = instructionService; } public string GetSchemaJson() { if (!string.IsNullOrEmpty(_cached)) return _cached!; var path = Path.Combine(_env.ContentRootPath, "Data", "schema.json"); if (!File.Exists(path)) { _log.LogWarning("Schema file not found at {Path}. Returning empty schema.", path); _cached = "{\"tables\":{}}"; return _cached!; } _cached = File.ReadAllText(path); // Basic sanity check JsonDocument.Parse(_cached); return _cached!; } public async Task GetPlannerPromptAsync() { var path = Path.Combine(_env.ContentRootPath, "Data", "planner_prompt.txt"); if (!File.Exists(path)) { _log.LogWarning("Planner prompt file not found at {Path}. Returning empty prompt.", path); return string.Empty; } var basePrompt = File.ReadAllText(path); var aiInstructions = await _instructionService.GetInstructionsTextAsync(); if (!string.IsNullOrWhiteSpace(aiInstructions)) { return $"{basePrompt}\n\nAdditional Instructions:\n{aiInstructions}"; } return basePrompt; } public string GetPlannerPrompt() { // Keep synchronous version for backward compatibility var path = Path.Combine(_env.ContentRootPath, "Data", "planner_prompt.txt"); if (!File.Exists(path)) { _log.LogWarning("Planner prompt file not found at {Path}. Returning empty prompt.", path); return string.Empty; } return File.ReadAllText(path); } public string GetPlotySample() { var path = Path.Combine(_env.ContentRootPath, "Data", "ploty_sample.txt"); if (!File.Exists(path)) { _log.LogWarning("Ploty sample file not found at {Path}. Returning empty prompt.", path); return string.Empty; } return File.ReadAllText(path); } } }