| 1 | using CascadeIDE.ViewModels; |
| 2 | using CascadeIDE.Features.IdeMcp.Application; |
| 3 | using CascadeIDE.Features.UiChrome; |
| 4 | using CascadeIDE.Models; |
| 5 | using CascadeIDE.Services; |
| 6 | |
| 7 | namespace CascadeIDE.Features.IdeMcp.Application; |
| 8 | |
| 9 | internal sealed partial class MainWindowIdeMcpHost |
| 10 | { |
| 11 | |
| 12 | public string GetSolutionInfo() |
| 13 | { |
| 14 | var projects = McpSolutionTree.CollectProjectPaths(_host.Workspace.SolutionRoots).ToList(); |
| 15 | var solutionPath = _host.Workspace.SolutionPath; |
| 16 | if (string.IsNullOrWhiteSpace(solutionPath)) |
| 17 | solutionPath = _host.ChatPanel.ResolveAttachSolutionPath() ?? ""; |
| 18 | return IdeMcpWorkspaceOrchestrator.SerializeSolutionInfo( |
| 19 | solutionPath, |
| 20 | _host.CurrentFilePath, |
| 21 | projects, |
| 22 | _host.Workspace.SelectedSolutionItem?.FullPath); |
| 23 | } |
| 24 | |
| 25 | public string GetBuildOutput() |
| 26 | { |
| 27 | var (bg, fg) = Services.UiThemeSnapshot.GetBuildOutputTheme(); |
| 28 | return IdeMcpWorkspaceOrchestrator.SerializeBuildOutput(_host.BuildOutputPanel.BuildOutput, bg, fg); |
| 29 | } |
| 30 | |
| 31 | public Task<string> GetUiModesDiagnosticsAsync() => |
| 32 | Task.FromResult(UiModeCatalog.GetDiagnosticsJson()); |
| 33 | public async Task<string> SearchWorkspaceTextAsync( |
| 34 | string pattern, |
| 35 | string? subPath, |
| 36 | bool fixedString, |
| 37 | string? glob, |
| 38 | int maxMatches, |
| 39 | string? rgPath) |
| 40 | { |
| 41 | var solutionPath = await UiScheduler.Default.InvokeAsync(() => _host.Workspace.SolutionPath ?? ""); |
| 42 | if (!IdeMcpWorkspaceOrchestrator.TryResolveWorkspaceRootForRipgrep(solutionPath, out var root, out var err)) |
| 43 | return err; |
| 44 | |
| 45 | return await RipgrepWorkspaceSearchService.SearchAsync( |
| 46 | root, |
| 47 | pattern, |
| 48 | subPath, |
| 49 | fixedString, |
| 50 | glob, |
| 51 | maxMatches, |
| 52 | rgPath, |
| 53 | CancellationToken.None).ConfigureAwait(false); |
| 54 | } |
| 55 | public async Task<string> GetIdeStateAsync() |
| 56 | { |
| 57 | var diagnosticsJson = await this.GetCurrentFileDiagnosticsAsync().ConfigureAwait(false); |
| 58 | var diagnostics = IdeMcpWorkspaceOrchestrator.ParseDiagnosticsOrEmpty(diagnosticsJson); |
| 59 | var ui = await UiScheduler.Default.InvokeAsync(CaptureIdeMcpIdeStateUi); |
| 60 | var state = IdeMcpWorkspaceOrchestrator.BuildIdeStatePayload(ui, diagnostics); |
| 61 | return IdeMcpWorkspaceOrchestrator.SerializeIdeState(state); |
| 62 | } |
| 63 | |
| 64 | /// <summary>Снимок полей UI для MCP <c>get_ide_state</c>; вызывать только с UI-потока (через <see cref="UiScheduler"/>).</summary> |
| 65 | private IdeMcpIdeStateUiCapture CaptureIdeMcpIdeStateUi() |
| 66 | { |
| 67 | var buildText = IdeMcpWorkspaceOrchestrator.BuildTruncatedOutputPreview(_host.BuildOutputPanel.BuildOutput, 2000); |
| 68 | return new IdeMcpIdeStateUiCapture( |
| 69 | _host.Workspace.SolutionPath, |
| 70 | _host.CurrentFilePath, |
| 71 | _host.Workspace.SelectedSolutionItem?.FullPath, |
| 72 | (_host.EditorText ?? "").Length, |
| 73 | _host.EditorSelectionStart, |
| 74 | _host.EditorSelectionLength, |
| 75 | _host.AllBreakpointLinesInCurrentFile, |
| 76 | _host.DapDebug.GetSnapshot(), |
| 77 | _host.IsBuildOutputVisible, |
| 78 | buildText, |
| 79 | _host.McpLastBuildBinlogPath, |
| 80 | _host.IsTerminalVisible, |
| 81 | _host.UiMode, |
| 82 | _host.IsPfdRegionExpanded, |
| 83 | _host.IsMfdRegionExpanded, |
| 84 | _host.IsGitPanelVisible, |
| 85 | _host.IsInstrumentationDockVisible, |
| 86 | _host.SafetyLevel, |
| 87 | _host.EditorGroupCount, |
| 88 | _host.InstrumentationPanel.AgentTraceSteps.Count, |
| 89 | _host.Autonomous.IsAutonomousRunning, |
| 90 | _host.BuildCockpitSurfaceSnapshot()); |
| 91 | } |
| 92 | |
| 93 | public Task<string> GetCockpitSurfaceAsync() => |
| 94 | UiScheduler.Default.InvokeAsync(() => IdeMcpWorkspaceOrchestrator.SerializeCockpitSurface(_host.BuildCockpitSurfaceSnapshot())); |
| 95 | public async Task<string> GetCodeMetricsAsync(string? scope, string? path) |
| 96 | { |
| 97 | var files = await UiScheduler.Default.InvokeAsync(() => |
| 98 | McpCodeMetrics.ResolveMetricFilePaths(scope, path, _host.CurrentFilePath, _host.Workspace.SolutionRoots) |
| 99 | .Distinct(StringComparer.OrdinalIgnoreCase) |
| 100 | .ToList()); |
| 101 | return await McpCodeMetrics.ComputeMetricsJsonAsync(scope, files).ConfigureAwait(false); |
| 102 | } |
| 103 | |
| 104 | } |
| 105 | |