| 1 | using System.Text.Json; |
| 2 | using System.Text.Json.Serialization; |
| 3 | |
| 4 | namespace AgentNotes.Core; |
| 5 | |
| 6 | /// <summary>Дефолтные относительные пути для workspace → scope и алиасов: embedded JSON (<see cref="BundledAgentNotesContent"/> + <c>Resources/mcp-resolve-paths-defaults.json</c>). Литералы в коде — только если ресурс не читается или JSON не парсится / не проходит валидацию путей.</summary> |
| 7 | internal static class McpResolvePathsDefaults |
| 8 | { |
| 9 | private static readonly Lazy<(string WorkspaceScopeMapRelative, string ScopeAliasMapRelative)> Pair = new(Load); |
| 10 | |
| 11 | public static (string WorkspaceScopeMapRelative, string ScopeAliasMapRelative) DefaultsPair => Pair.Value; |
| 12 | |
| 13 | private static readonly JsonSerializerOptions JsonOptions = new() |
| 14 | { |
| 15 | PropertyNameCaseInsensitive = true, |
| 16 | ReadCommentHandling = JsonCommentHandling.Skip, |
| 17 | AllowTrailingCommas = true |
| 18 | }; |
| 19 | |
| 20 | private static (string, string) Load() |
| 21 | { |
| 22 | if (!BundledAgentNotesContent.TryReadEmbeddedText("mcp-resolve-paths-defaults.json", out var text)) |
| 23 | return HardcodedFallback.Pair; |
| 24 | |
| 25 | try |
| 26 | { |
| 27 | var dto = JsonSerializer.Deserialize<McpResolvePathsConfigModel>(text, JsonOptions); |
| 28 | if (dto is not null |
| 29 | && TryValidateKnowledgePath(dto.WorkspaceScopeMap, out var ws) |
| 30 | && TryValidateKnowledgePath(dto.ScopeAliasMap, out var al)) |
| 31 | { |
| 32 | return (ws, al); |
| 33 | } |
| 34 | } |
| 35 | catch |
| 36 | { |
| 37 | // fall through to hardcoded fallback |
| 38 | } |
| 39 | |
| 40 | return HardcodedFallback.Pair; |
| 41 | } |
| 42 | |
| 43 | /// <summary>Same rules as <c>read_knowledge_file</c> paths: no <c>..</c>, not rooted; trimmed forward slashes.</summary> |
| 44 | private static bool TryValidateKnowledgePath(string? filePath, out string normalized) |
| 45 | { |
| 46 | normalized = ""; |
| 47 | if (string.IsNullOrWhiteSpace(filePath)) |
| 48 | return false; |
| 49 | |
| 50 | normalized = filePath.Replace('\\', '/').TrimStart('/'); |
| 51 | if (string.IsNullOrWhiteSpace(normalized) || normalized.Contains("..", StringComparison.Ordinal) || Path.IsPathRooted(normalized)) |
| 52 | { |
| 53 | normalized = ""; |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | private static class HardcodedFallback |
| 61 | { |
| 62 | internal static readonly (string WorkspaceScopeMapRelative, string ScopeAliasMapRelative) Pair = ( |
| 63 | "work/local/workspace-scope-map-v1.md", |
| 64 | "work/local/scope-alias-map-v1.md"); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /// <summary>JSON shape shared by embedded defaults and optional <c>knowledge/META/mcp-resolve-paths-v1.json</c> on disk.</summary> |
| 69 | internal sealed class McpResolvePathsConfigModel |
| 70 | { |
| 71 | [JsonPropertyName("workspace_scope_map")] |
| 72 | public string? WorkspaceScopeMap { get; set; } |
| 73 | |
| 74 | [JsonPropertyName("scope_alias_map")] |
| 75 | public string? ScopeAliasMap { get; set; } |
| 76 | } |
| 77 | |