| 1 | using CascadeIDE.ViewModels; |
| 2 | using CascadeIDE.Features.HybridIndex.McpParity; |
| 3 | using CascadeIDE.Features.IdeMcp.Application; |
| 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 | private bool TryResolveHybridIndexScopeForCodebaseIndexCalls( |
| 13 | string? argWorkspacePath, |
| 14 | string? argSolutionPath, |
| 15 | out string hciWorkspaceRoot, |
| 16 | out string? hciSolutionPath, |
| 17 | out string? errorJson) => |
| 18 | IdeMcpHybridIndexScope.TryResolveForCodebaseIndexCommand( |
| 19 | argWorkspacePath, |
| 20 | argSolutionPath, |
| 21 | _host.McpSettings.HybridIndex.ScopeMode, |
| 22 | _host.Workspace.SolutionPath, |
| 23 | WorkspaceDirectoryFromSolutionPath.Resolve, |
| 24 | out hciWorkspaceRoot, |
| 25 | out hciSolutionPath, |
| 26 | out errorJson); |
| 27 | |
| 28 | public Task<string> CodebaseIndexStatusAsync(string? workspacePath, string? solutionPath, CancellationToken cancellationToken) |
| 29 | { |
| 30 | if (!TryResolveHybridIndexScopeForCodebaseIndexCalls(workspacePath, solutionPath, out var ws, out var sln, out var errJson)) |
| 31 | return Task.FromResult(errJson!); |
| 32 | |
| 33 | return Task.Run(async () => |
| 34 | { |
| 35 | var st = await _host.McpHybridIndex.GetIndexStatusAsync(ws, sln, cancellationToken).ConfigureAwait(false); |
| 36 | return CodebaseIndexIdeJsonResponses.SerializeStatus(st); |
| 37 | }, cancellationToken); |
| 38 | } |
| 39 | |
| 40 | public Task<string> CodebaseIndexSearchAsync( |
| 41 | string? workspacePath, |
| 42 | string? solutionPath, |
| 43 | string query, |
| 44 | int topN, |
| 45 | string? pathPrefix, |
| 46 | IReadOnlyList<string>? excludePathPrefixes, |
| 47 | IReadOnlyList<string>? extensions, |
| 48 | bool semantic, |
| 49 | double alpha, |
| 50 | double beta, |
| 51 | int vecTopK, |
| 52 | CancellationToken cancellationToken) |
| 53 | { |
| 54 | if (string.IsNullOrWhiteSpace(query)) |
| 55 | return Task.FromResult(IdeMcpHybridCodebaseIndexOrchestrator.MissingQueryJson()); |
| 56 | |
| 57 | if (!TryResolveHybridIndexScopeForCodebaseIndexCalls(workspacePath, solutionPath, out var ws, out var sln, out var errJson)) |
| 58 | return Task.FromResult(errJson!); |
| 59 | |
| 60 | return Task.Run(async () => |
| 61 | { |
| 62 | var (response, searchErr) = await _host.McpHybridIndex.SearchHybridAsync( |
| 63 | ws, |
| 64 | sln, |
| 65 | query.Trim(), |
| 66 | topN, |
| 67 | pathPrefix, |
| 68 | excludePathPrefixes, |
| 69 | extensions, |
| 70 | semantic, |
| 71 | alpha, |
| 72 | beta, |
| 73 | vecTopK, |
| 74 | cancellationToken) |
| 75 | .ConfigureAwait(false); |
| 76 | return CodebaseIndexIdeJsonResponses.SerializeSearch(response, searchErr); |
| 77 | }, cancellationToken); |
| 78 | } |
| 79 | |
| 80 | public Task<string> CodebaseIndexExplainAsync(string? workspacePath, string? solutionPath, long hitId, CancellationToken cancellationToken) |
| 81 | { |
| 82 | if (hitId <= 0) |
| 83 | return Task.FromResult(IdeMcpHybridCodebaseIndexOrchestrator.InvalidHitIdJson()); |
| 84 | |
| 85 | if (!TryResolveHybridIndexScopeForCodebaseIndexCalls(workspacePath, solutionPath, out var ws, out var sln, out var errJson)) |
| 86 | return Task.FromResult(errJson!); |
| 87 | |
| 88 | return Task.Run(async () => |
| 89 | { |
| 90 | var resp = await _host.McpHybridIndex.ExplainHitAsync(ws, sln, hitId, cancellationToken).ConfigureAwait(false); |
| 91 | return CodebaseIndexIdeJsonResponses.SerializeExplain(resp); |
| 92 | }, cancellationToken); |
| 93 | } |
| 94 | |
| 95 | public Task<string> CodebaseIndexReindexAsync(string? workspacePath, string? solutionPath, bool fullRebuild, CancellationToken cancellationToken) |
| 96 | { |
| 97 | if (!TryResolveHybridIndexScopeForCodebaseIndexCalls(workspacePath, solutionPath, out var ws, out var sln, out var errJson)) |
| 98 | return Task.FromResult(errJson!); |
| 99 | |
| 100 | return Task.Run(async () => |
| 101 | { |
| 102 | try |
| 103 | { |
| 104 | var summary = await _host.McpHybridIndex.RunReindexWithPublishAsync(ws, sln, fullRebuild, cancellationToken).ConfigureAwait(false); |
| 105 | return CodebaseIndexIdeJsonResponses.SerializeReindex(summary); |
| 106 | } |
| 107 | catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) |
| 108 | { |
| 109 | throw; |
| 110 | } |
| 111 | catch (Exception ex) |
| 112 | { |
| 113 | return IdeMcpHybridCodebaseIndexOrchestrator.SerializeReindexFailed(ex.Message); |
| 114 | } |
| 115 | }, cancellationToken); |
| 116 | } |
| 117 | |
| 118 | } |
| 119 | |