| 1 | using System.Text.Json; |
| 2 | using CascadeIDE.Features.IdeMcp.Execution; |
| 3 | using CascadeIDE.Services; |
| 4 | using CascadeIDE.ViewModels; |
| 5 | |
| 6 | namespace CascadeIDE.Features.IdeMcp.Application; |
| 7 | |
| 8 | /// <summary> |
| 9 | /// Реализация <see cref="IIdeMcpActions"/> вне <see cref="MainWindowViewModel"/> (Wave 2 Big Bang). |
| 10 | /// Доступ к UI-состоянию — через <see cref="Host"/>; маршалинг команд — на UI-поток. |
| 11 | /// </summary> |
| 12 | internal sealed partial class MainWindowIdeMcpHost : IIdeMcpActions |
| 13 | { |
| 14 | private readonly MainWindowViewModel _host; |
| 15 | private readonly IdeMcpCommandExecutor _executor; |
| 16 | |
| 17 | internal MainWindowViewModel Host => _host; |
| 18 | |
| 19 | internal IdeMcpCommandExecutor Executor => _executor; |
| 20 | |
| 21 | internal MainWindowIdeMcpHost(MainWindowViewModel host) |
| 22 | { |
| 23 | _host = host; |
| 24 | _executor = new IdeMcpCommandExecutor(host, this); // host: IMainWindowMcpHostContext |
| 25 | } |
| 26 | |
| 27 | public Task<string> ExecuteCommandAsync( |
| 28 | string commandId, |
| 29 | IReadOnlyDictionary<string, JsonElement>? args, |
| 30 | CancellationToken cancellationToken = default) |
| 31 | { |
| 32 | // AEE: не блокировать MCP на очереди UI (warm-up/HCI). Сервис потокобезопасен. |
| 33 | if (IsAgentEnvironmentCommand(commandId)) |
| 34 | return _executor.ExecuteAsync(commandId, args, cancellationToken); |
| 35 | |
| 36 | return UiScheduler.Default.InvokeAsync(() => _executor.ExecuteAsync(commandId, args, cancellationToken)); |
| 37 | } |
| 38 | |
| 39 | private static bool IsAgentEnvironmentCommand(string commandId) => |
| 40 | commandId is IdeCommands.IdeAgentVerify |
| 41 | or IdeCommands.IdeAgentVerifyBatch |
| 42 | or IdeCommands.IdeAgentCancel |
| 43 | or IdeCommands.IdeAgentStatus |
| 44 | or IdeCommands.IdeAgentLast |
| 45 | or IdeCommands.IdeAgentSandboxPrepare; |
| 46 | } |
| 47 | |