| 1 | using System.Text.Json; |
| 2 | using CascadeIDE.Contracts; |
| 3 | using CascadeIDE.Services; |
| 4 | |
| 5 | namespace CascadeIDE.Features.IdeMcp.Application; |
| 6 | |
| 7 | /// <summary> |
| 8 | /// Application-level helpers for IDE MCP debug actions. |
| 9 | /// Keeps snapshot payload shaping out of MainWindowViewModel. |
| 10 | /// </summary> |
| 11 | [ApplicationOrchestrator] |
| 12 | public static class IdeMcpDebugOrchestrator |
| 13 | { |
| 14 | /// <summary>План для UI после DAP-снимка; без <c>File.Exists</c> на Application-слое (CASCOPE031).</summary> |
| 15 | public static IdeMcpDapSnapshotUiPlan BuildDapSnapshotUiPlan(DebugSessionSnapshot s, bool mfdDebugPagePrimedForCurrentStop) |
| 16 | { |
| 17 | bool mfdNext; |
| 18 | bool activateDock; |
| 19 | if (!s.IsExecutionStopped) |
| 20 | { |
| 21 | mfdNext = false; |
| 22 | activateDock = false; |
| 23 | } |
| 24 | else if (!mfdDebugPagePrimedForCurrentStop) |
| 25 | { |
| 26 | mfdNext = true; |
| 27 | activateDock = true; |
| 28 | } |
| 29 | else |
| 30 | { |
| 31 | mfdNext = true; |
| 32 | activateDock = false; |
| 33 | } |
| 34 | |
| 35 | var positionFile = !string.IsNullOrEmpty(s.StoppedFile) |
| 36 | ? CanonicalFilePath.Normalize(s.StoppedFile) |
| 37 | : null; |
| 38 | |
| 39 | var attemptOpen = s.IsExecutionStopped && !string.IsNullOrEmpty(s.StoppedFile); |
| 40 | |
| 41 | var stackIndex = s is { IsExecutionStopped: true, StackFrames.Count: > 0 } |
| 42 | ? s.VariablesFrameIndex |
| 43 | : -1; |
| 44 | |
| 45 | return new IdeMcpDapSnapshotUiPlan( |
| 46 | mfdNext, |
| 47 | activateDock, |
| 48 | positionFile, |
| 49 | s.StoppedLine, |
| 50 | attemptOpen, |
| 51 | attemptOpen ? s.StoppedFile : null, |
| 52 | stackIndex); |
| 53 | } |
| 54 | |
| 55 | public static string SerializeDebugSnapshot(DebugSessionSnapshot snapshot) => |
| 56 | JsonSerializer.Serialize(new |
| 57 | { |
| 58 | snapshot.HasActiveSession, |
| 59 | snapshot.IsExecutionStopped, |
| 60 | position = new { file = snapshot.StoppedFile, line = snapshot.StoppedLine }, |
| 61 | exception = snapshot.ExceptionText, |
| 62 | breakpoints = snapshot.Breakpoints |
| 63 | .Take(500) |
| 64 | .Select(b => new { file = b.File, line = b.Line, condition = b.Condition }) |
| 65 | .ToList(), |
| 66 | stack_frames = snapshot.StackFrames |
| 67 | .Select(frame => new { frame.Name, frame.File, frame.Line }) |
| 68 | .ToList(), |
| 69 | variables_frame_index = snapshot.VariablesFrameIndex, |
| 70 | variable_root_scopes = snapshot.VariableRootScopes |
| 71 | .Take(50) |
| 72 | .Select(g => new |
| 73 | { |
| 74 | scope = g.ScopeName, |
| 75 | roots = g.Roots |
| 76 | .Take(200) |
| 77 | .Select(v => new |
| 78 | { |
| 79 | v.Name, |
| 80 | v.Value, |
| 81 | v.Type, |
| 82 | variables_reference = v.VariablesReference, |
| 83 | v.NamedVariables, |
| 84 | v.IndexedVariables |
| 85 | }) |
| 86 | .ToList() |
| 87 | }) |
| 88 | .ToList() |
| 89 | }); |
| 90 | } |
| 91 | |