using System.Collections.Generic; using System.IO; using System.Text.Json; namespace StellaOps.AdvisoryAI.Hosting; internal static class GuardrailPhraseLoader { public static IReadOnlyCollection Load(string path) { if (string.IsNullOrWhiteSpace(path)) { throw new ArgumentException("Guardrail phrase file path must be provided.", nameof(path)); } using var stream = File.OpenRead(path); using var document = JsonDocument.Parse(stream); var root = document.RootElement; return root.ValueKind switch { JsonValueKind.Array => ExtractValues(root), JsonValueKind.Object when root.TryGetProperty("phrases", out var phrases) => ExtractValues(phrases), _ => throw new InvalidDataException($"Guardrail phrase file {path} must be a JSON array or object with a phrases array."), }; } private static IReadOnlyCollection ExtractValues(JsonElement element) { var phrases = new List(); foreach (var item in element.EnumerateArray()) { if (item.ValueKind == JsonValueKind.String) { var value = item.GetString(); if (!string.IsNullOrWhiteSpace(value)) { phrases.Add(value.Trim()); } } } return phrases; } }