Forge
csharp4405de34
1using System.Text.Json;
2
3namespace CascadeIDE.Services;
4
5/// <summary>
6/// Разбор JSON-аргументов MCP для панели отладки (breakpoints / stack / variables).
7/// Чистая логика для план B и unit-тестов без IDE command executor.
8/// </summary>
9public static class McpDebugPayloadParsing
10{
11 public const string MissingBreakpointsMessage = "Missing breakpoints (array of { file_path, line })";
12
13 /// <summary>
14 /// Парсит массив <c>breakpoints</c>: объекты с <c>file_path</c> и <c>line</c>.
15 /// </summary>
16 public static bool TryParseBreakpoints(
17 IReadOnlyDictionary<string, JsonElement>? args,
18 out List<(string FilePath, int Line)> breakpoints,
19 out string errorMessage)
20 {
21 breakpoints = [];
22 if (args is null || !args.TryGetValue("breakpoints", out var arr) || arr.ValueKind != JsonValueKind.Array)
23 {
24 errorMessage = MissingBreakpointsMessage;
25 return false;
26 }
27
28 foreach (var item in arr.EnumerateArray())
29 {
30 if (!item.TryGetProperty("file_path", out var fp) || !item.TryGetProperty("line", out var ln))
31 continue;
32 var path = fp.GetString();
33 if (string.IsNullOrEmpty(path))
34 continue;
35 breakpoints.Add((path, ln.GetInt32()));
36 }
37
38 errorMessage = "";
39 return true;
40 }
41
42 /// <summary>
43 /// Опциональные <c>stack_frames</c> и <c>variables</c>; при отсутствии — пустые списки.
44 /// </summary>
45 public static void ParseDebugState(
46 IReadOnlyDictionary<string, JsonElement>? args,
47 out List<(string Name, string? File, int Line)> stackFrames,
48 out List<(string Name, string Value, string? Type)> variables)
49 {
50 stackFrames = [];
51 variables = [];
52 if (args is null)
53 return;
54
55 if (args.TryGetValue("stack_frames", out var sf) && sf.ValueKind == JsonValueKind.Array)
56 {
57 foreach (var item in sf.EnumerateArray())
58 {
59 stackFrames.Add((
60 item.TryGetProperty("name", out var n) ? n.GetString() ?? "" : "",
61 item.TryGetProperty("file", out var f) ? f.GetString() : null,
62 item.TryGetProperty("line", out var l) ? l.GetInt32() : 0));
63 }
64 }
65
66 if (args.TryGetValue("variables", out var v) && v.ValueKind == JsonValueKind.Array)
67 {
68 foreach (var item in v.EnumerateArray())
69 {
70 if (!item.TryGetProperty("name", out var vn) || !item.TryGetProperty("value", out var vv))
71 continue;
72 var t = item.TryGetProperty("type", out var vt) ? vt.GetString() : null;
73 variables.Add((vn.GetString() ?? "", vv.GetString() ?? "", t));
74 }
75 }
76 }
77}
78
View only · write via MCP/CIDE