| 1 | using System.Text.Json; |
| 2 | |
| 3 | namespace CascadeIDE.Services; |
| 4 | |
| 5 | /// <summary>Чтение примитивов из словаря аргументов MCP-команд IDE (JSON).</summary> |
| 6 | public static class McpCommandJsonArgs |
| 7 | { |
| 8 | public static string? String(IReadOnlyDictionary<string, JsonElement>? args, string key) => |
| 9 | args is not null && args.TryGetValue(key, out var e) ? e.GetString() : null; |
| 10 | |
| 11 | /// <summary>Primary knowledge root override (MCP 2.0 / IDE parity). Accepts legacy <c>canon_path</c> alias.</summary> |
| 12 | public static string? KnowledgePath(IReadOnlyDictionary<string, JsonElement>? args) => |
| 13 | String(args, "knowledge_path") ?? String(args, "canon_path"); |
| 14 | |
| 15 | /// <summary>Knowledge root id from TOML <c>[knowledge.roots]</c> / <c>[[knowledge.read_only]]</c> (MCP 2.1 / ADR 015). Mutually exclusive with <see cref="KnowledgePath"/>.</summary> |
| 16 | public static string? KnowledgeRootId(IReadOnlyDictionary<string, JsonElement>? args) => |
| 17 | String(args, "knowledge_root_id"); |
| 18 | |
| 19 | public static int Int(IReadOnlyDictionary<string, JsonElement>? args, string key, int defaultValue = 0) |
| 20 | { |
| 21 | if (args is null || !args.TryGetValue(key, out var e) || e.ValueKind != JsonValueKind.Number) |
| 22 | return defaultValue; |
| 23 | return e.TryGetInt32(out var v) ? v : defaultValue; |
| 24 | } |
| 25 | |
| 26 | public static int? OptionalInt32(IReadOnlyDictionary<string, JsonElement>? args, string key) => |
| 27 | args is not null && args.TryGetValue(key, out var e) && e.ValueKind == JsonValueKind.Number && e.TryGetInt32(out var v) |
| 28 | ? v |
| 29 | : null; |
| 30 | |
| 31 | public static bool Bool(IReadOnlyDictionary<string, JsonElement>? args, string key, bool defaultValue = false) => |
| 32 | args is not null && args.TryGetValue(key, out var e) && (e.ValueKind is JsonValueKind.True or JsonValueKind.False) |
| 33 | ? e.GetBoolean() |
| 34 | : defaultValue; |
| 35 | |
| 36 | public static bool? OptionalBool(IReadOnlyDictionary<string, JsonElement>? args, string key) |
| 37 | { |
| 38 | if (args is null || !args.TryGetValue(key, out var e)) |
| 39 | return null; |
| 40 | return e.ValueKind switch |
| 41 | { |
| 42 | JsonValueKind.True => true, |
| 43 | JsonValueKind.False => false, |
| 44 | _ => null, |
| 45 | }; |
| 46 | } |
| 47 | |
| 48 | public static double? OptionalDouble(IReadOnlyDictionary<string, JsonElement>? args, string key) |
| 49 | { |
| 50 | if (args is null || !args.TryGetValue(key, out var e)) |
| 51 | return null; |
| 52 | return e.ValueKind == JsonValueKind.Number && e.TryGetDouble(out var d) |
| 53 | ? d |
| 54 | : null; |
| 55 | } |
| 56 | |
| 57 | public static long? OptionalInt64(IReadOnlyDictionary<string, JsonElement>? args, string key) |
| 58 | { |
| 59 | if (args is null || !args.TryGetValue(key, out var e)) |
| 60 | return null; |
| 61 | return e.ValueKind == JsonValueKind.Number && e.TryGetInt64(out var v) |
| 62 | ? v |
| 63 | : null; |
| 64 | } |
| 65 | |
| 66 | public static List<string>? StringList(IReadOnlyDictionary<string, JsonElement>? args, string key) |
| 67 | { |
| 68 | if (args is null || !args.TryGetValue(key, out var e) || e.ValueKind != JsonValueKind.Array) |
| 69 | return null; |
| 70 | var values = new List<string>(); |
| 71 | foreach (var item in e.EnumerateArray()) |
| 72 | { |
| 73 | var value = item.GetString(); |
| 74 | if (!string.IsNullOrWhiteSpace(value)) |
| 75 | values.Add(value); |
| 76 | } |
| 77 | return values; |
| 78 | } |
| 79 | } |
| 80 | |