| 1 | using System.Text.Json; |
| 2 | using CascadeIDE.ViewModels; |
| 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 | public Task<string> RequestConfirmationAsync(string message, CancellationToken cancellationToken) |
| 13 | { |
| 14 | var request = _host.RequestConfirmation; |
| 15 | if (request is null) |
| 16 | return Task.FromResult(ConfirmationResponses.Ok); |
| 17 | return request(message ?? "", cancellationToken); |
| 18 | } |
| 19 | |
| 20 | public string GetUiTheme() => UiThemeSnapshot.GetJson(); |
| 21 | public async Task<string> SetUiThemeAsync(string themeJson) => |
| 22 | await UiThemeApply.ApplyOnUiThreadAsync(themeJson ?? ""); |
| 23 | public async Task<string> GetUiLayoutAsync() => |
| 24 | await IdeMcpUiAutomationOrchestrator.InvokeJsonProviderOrDefaultAsync(UiScheduler.Default, _host.GetUiLayoutProvider); |
| 25 | public async Task<string> GetColorsUnderCursorAsync() => |
| 26 | await IdeMcpUiAutomationOrchestrator.InvokeJsonProviderOrDefaultAsync(UiScheduler.Default, _host.GetColorsUnderCursorProvider); |
| 27 | public async Task<string> GetControlAppearanceAsync(string? name) => |
| 28 | await IdeMcpUiAutomationOrchestrator.InvokeJsonAppearanceProviderAsync( |
| 29 | UiScheduler.Default, |
| 30 | _host.GetControlAppearanceProvider, |
| 31 | name); |
| 32 | public async Task<string> SetControlLayoutAsync(string controlName, string layoutJson) => |
| 33 | await IdeMcpUiAutomationOrchestrator.InvokeProviderOrMessageAsync( |
| 34 | UiScheduler.Default, |
| 35 | _host.SetControlLayoutProvider, |
| 36 | controlName, |
| 37 | IdeMcpUiAutomationOrchestrator.NormalizeJsonInput(layoutJson), |
| 38 | IdeMcpUiAutomationOrchestrator.NoLayoutProviderMessage()); |
| 39 | public async Task<string> AddControlAsync(string parentName, string controlType, string? content, string? name) => |
| 40 | await IdeMcpUiAutomationOrchestrator.InvokeProviderOrMessageAsync( |
| 41 | UiScheduler.Default, |
| 42 | _host.AddControlProvider, |
| 43 | parentName, |
| 44 | controlType ?? "", |
| 45 | content, |
| 46 | name, |
| 47 | IdeMcpUiAutomationOrchestrator.AddControlDisabledMessage()); |
| 48 | public async Task<string> SetControlTextAsync(string controlName, string text) => |
| 49 | await IdeMcpUiAutomationOrchestrator.InvokeProviderOrMessageAsync( |
| 50 | UiScheduler.Default, |
| 51 | _host.SetControlTextProvider, |
| 52 | controlName, |
| 53 | IdeMcpUiAutomationOrchestrator.NormalizeTextInput(text), |
| 54 | IdeMcpUiAutomationOrchestrator.DefaultProviderMissingMessage()); |
| 55 | public async Task<string> ClickControlAsync(string? controlName) => |
| 56 | await IdeMcpUiAutomationOrchestrator.InvokeProviderOrMessageAsync( |
| 57 | UiScheduler.Default, |
| 58 | _host.ClickControlProvider, |
| 59 | controlName, |
| 60 | IdeMcpUiAutomationOrchestrator.DefaultProviderMissingMessage()); |
| 61 | public async Task<string> SendKeysAsync(string? controlName, string keys) => |
| 62 | await IdeMcpUiAutomationOrchestrator.InvokeSendKeysProviderOrMessageAsync( |
| 63 | UiScheduler.Default, |
| 64 | _host.SendKeysProvider, |
| 65 | controlName, |
| 66 | IdeMcpUiAutomationOrchestrator.NormalizeTextInput(keys), |
| 67 | IdeMcpUiAutomationOrchestrator.DefaultProviderMissingMessage()); |
| 68 | public async Task<string> SelectChatMessageAsync(int index) => |
| 69 | await IdeMcpUiAutomationOrchestrator.InvokeStringResultOnUiAsync( |
| 70 | UiScheduler.Default, |
| 71 | () => _host.ChatPanel.SelectMessageByIndex(index)); |
| 72 | public async Task<string> SelectChatMessageByOrdinalAsync(int ordinal, int endOrdinal) => |
| 73 | await IdeMcpUiAutomationOrchestrator.InvokeStringResultOnUiAsync( |
| 74 | UiScheduler.Default, |
| 75 | () => _host.ChatPanel.SelectMessageByOrdinalRangeInDetailLane(ordinal, endOrdinal)); |
| 76 | public async Task<string> GetSelectedChatMessageAsync() => |
| 77 | await IdeMcpUiAutomationOrchestrator.InvokeStringResultOnUiAsync( |
| 78 | UiScheduler.Default, |
| 79 | _host.ChatPanel.GetSelectedMessageJson); |
| 80 | |
| 81 | public async Task<string> FindIntercomMessagesForCodeAsync( |
| 82 | IReadOnlyDictionary<string, JsonElement>? args) => |
| 83 | await IdeMcpUiAutomationOrchestrator.InvokeStringResultOnUiAsync( |
| 84 | UiScheduler.Default, |
| 85 | () => _host.ChatPanel.FindMessagesForCodeRefFromMcp(args)); |
| 86 | |
| 87 | public async Task<string> RelateIntercomMessageRangeToCodeAsync( |
| 88 | IReadOnlyDictionary<string, JsonElement>? args) => |
| 89 | await IdeMcpUiAutomationOrchestrator.InvokeStringResultOnUiAsync( |
| 90 | UiScheduler.Default, |
| 91 | () => _host.ChatPanel.RelateMessageRangeToCodeRefFromMcp(args)); |
| 92 | public async Task<string> EditChatAssistantMessageAsync(string messageId, string newContent, string? reason) => |
| 93 | await IdeMcpUiAutomationOrchestrator.EditChatAssistantMessageOnUiAsync( |
| 94 | UiScheduler.Default, |
| 95 | messageId, |
| 96 | newContent, |
| 97 | reason, |
| 98 | _host.ChatPanel.EditAssistantMessageById); |
| 99 | public async Task<string> ExportChatReadableAsync(bool writeFile, string? fileName) => |
| 100 | await IdeMcpUiAutomationOrchestrator.InvokeStringResultOnUiAsync( |
| 101 | UiScheduler.Default, |
| 102 | () => _host.ChatPanel.ExportReadableMarkdown(writeFile, fileName)); |
| 103 | public async Task<string> SetFocusAsync(string? controlName) => |
| 104 | await IdeMcpUiAutomationOrchestrator.InvokeProviderOrMessageAsync( |
| 105 | UiScheduler.Default, |
| 106 | _host.SetFocusProvider, |
| 107 | controlName, |
| 108 | IdeMcpUiAutomationOrchestrator.DefaultProviderMissingMessage()); |
| 109 | public async Task<string> HighlightControlAsync(string? controlName) => |
| 110 | await IdeMcpUiAutomationOrchestrator.InvokeProviderOrMessageAsync( |
| 111 | UiScheduler.Default, |
| 112 | _host.HighlightControlProvider, |
| 113 | controlName, |
| 114 | IdeMcpUiAutomationOrchestrator.DefaultProviderMissingMessage()); |
| 115 | public async Task<string> SetPanelSizeAsync(string panel, double? width, double? height) => |
| 116 | await IdeMcpUiAutomationOrchestrator.InvokeProviderOrMessageAsync( |
| 117 | UiScheduler.Default, |
| 118 | _host.SetPanelSizeProvider, |
| 119 | panel, |
| 120 | width, |
| 121 | height, |
| 122 | IdeMcpUiAutomationOrchestrator.DefaultProviderMissingMessage()); |
| 123 | |
| 124 | public string GetSupportedEditorLanguages() => EditorLanguageSupport.GetJson(); |
| 125 | |
| 126 | } |
| 127 | |