| 1 | #nullable enable |
| 2 | |
| 3 | using System.Text.Json; |
| 4 | using CascadeIDE.Features.Agent.Environment; |
| 5 | using CascadeIDE.Models; |
| 6 | using CascadeIDE.Services; |
| 7 | |
| 8 | namespace CascadeIDE.Features.IdeMcp.Application; |
| 9 | |
| 10 | internal sealed partial class MainWindowIdeMcpHost |
| 11 | { |
| 12 | public Task<string> IdeAgentVerifyAsync( |
| 13 | string? policy, |
| 14 | string? sandboxProfile, |
| 15 | string? solutionPath, |
| 16 | CancellationToken cancellationToken = default) |
| 17 | { |
| 18 | _ = cancellationToken; |
| 19 | var path = ResolveSolutionPath(solutionPath); |
| 20 | if (string.IsNullOrWhiteSpace(path)) |
| 21 | return Task.FromResult("{\"error\":\"solution_path required\"}"); |
| 22 | |
| 23 | var p = ParsePolicy(policy); |
| 24 | var sandbox = ParseSandbox(sandboxProfile); |
| 25 | var result = _host.AgentEnvironment.StartVerify(path, p, sandbox); |
| 26 | return Task.FromResult(JsonSerializer.Serialize(result)); |
| 27 | } |
| 28 | |
| 29 | public Task<string> IdeAgentVerifyBatchAsync( |
| 30 | string? policy, |
| 31 | string? sandboxProfile, |
| 32 | string? solutionPath, |
| 33 | bool useWorktree, |
| 34 | CancellationToken cancellationToken = default) |
| 35 | { |
| 36 | _ = cancellationToken; |
| 37 | var path = ResolveSolutionPath(solutionPath); |
| 38 | if (string.IsNullOrWhiteSpace(path)) |
| 39 | return Task.FromResult("{\"error\":\"solution_path required\"}"); |
| 40 | |
| 41 | var request = new AgentVerifyBatchRequest( |
| 42 | ParsePolicy(policy), |
| 43 | ParseSandboxOrDefault(sandboxProfile), |
| 44 | path, |
| 45 | useWorktree); |
| 46 | var result = _host.AgentEnvironment.StartVerifyBatch(request); |
| 47 | return Task.FromResult(JsonSerializer.Serialize(result)); |
| 48 | } |
| 49 | |
| 50 | public Task<string> IdeAgentCancelAsync(CancellationToken cancellationToken = default) |
| 51 | { |
| 52 | _ = cancellationToken; |
| 53 | var ok = _host.AgentEnvironment.CancelActive(); |
| 54 | return Task.FromResult(JsonSerializer.Serialize(new { cancelled = ok })); |
| 55 | } |
| 56 | |
| 57 | public Task<string> IdeAgentStatusAsync(CancellationToken cancellationToken = default) |
| 58 | { |
| 59 | _ = cancellationToken; |
| 60 | var aee = _host.AgentEnvironment.GetStatus(); |
| 61 | var epoch = _host.AgentEnvironment.EpochTracker; |
| 62 | var harness = _host.ChatPanel.Harness.GetTelemetry(); |
| 63 | return Task.FromResult(System.Text.Json.JsonSerializer.Serialize(new |
| 64 | { |
| 65 | active = aee.IsActive, |
| 66 | run_id = aee.RunId, |
| 67 | verify_snapshot_id = aee.VerifySnapshotId, |
| 68 | policy = aee.Policy, |
| 69 | sandbox_profile = aee.SandboxProfile, |
| 70 | sandbox_run_directory = aee.SandboxRunDirectory, |
| 71 | execution_channel = aee.ExecutionChannel, |
| 72 | writes_invalidated_verify_epoch = aee.WritesInvalidatedVerifyEpoch || epoch.WritesInvalidatedVerifyEpoch, |
| 73 | verify_epoch_ui_stale = epoch.IsUiStale, |
| 74 | solution_path = aee.SolutionPath, |
| 75 | harness_session_user_turn_count = harness.SessionUserTurnCount, |
| 76 | harness_checkpoint_due = harness.CheckpointDue, |
| 77 | harness_next_checkpoint_at_turn = harness.NextCheckpointAtTurn, |
| 78 | harness_hot_context_loaded = harness.HotContextLoaded, |
| 79 | harness_hot_context_scope = harness.HotContextScope, |
| 80 | })); |
| 81 | } |
| 82 | |
| 83 | public Task<string> IdeAgentLastAsync(CancellationToken cancellationToken = default) |
| 84 | { |
| 85 | _ = cancellationToken; |
| 86 | var last = _host.AgentEnvironment.GetLastRun(); |
| 87 | return Task.FromResult(last is null ? "{}" : JsonSerializer.Serialize(last)); |
| 88 | } |
| 89 | |
| 90 | public Task<string> IdeAgentSandboxPrepareAsync( |
| 91 | string? profile, |
| 92 | string? workspaceRoot, |
| 93 | CancellationToken cancellationToken = default) |
| 94 | { |
| 95 | _ = cancellationToken; |
| 96 | var result = _host.AgentEnvironment.PrepareSandbox( |
| 97 | ParseSandboxOrDefault(profile), |
| 98 | string.IsNullOrWhiteSpace(workspaceRoot) ? _host.GetWorkspacePath() : workspaceRoot); |
| 99 | return Task.FromResult(JsonSerializer.Serialize(result)); |
| 100 | } |
| 101 | |
| 102 | private string? ResolveSolutionPath(string? solutionPath) => |
| 103 | string.IsNullOrWhiteSpace(solutionPath) ? _host.Workspace.SolutionPath : solutionPath.Trim(); |
| 104 | |
| 105 | private static AgentVerifyPolicy ParsePolicy(string? policy) => |
| 106 | string.IsNullOrWhiteSpace(policy) |
| 107 | ? AgentVerifyPolicy.Standard |
| 108 | : Enum.TryParse<AgentVerifyPolicy>(policy, ignoreCase: true, out var p) |
| 109 | ? p |
| 110 | : AgentVerifyPolicy.Standard; |
| 111 | |
| 112 | private static AgentSandboxProfile? ParseSandbox(string? profile) |
| 113 | { |
| 114 | if (string.IsNullOrWhiteSpace(profile)) |
| 115 | return null; |
| 116 | |
| 117 | return Enum.TryParse<AgentSandboxProfile>(profile, ignoreCase: true, out var p) ? p : null; |
| 118 | } |
| 119 | |
| 120 | private static AgentSandboxProfile ParseSandboxOrDefault(string? profile) => |
| 121 | ParseSandbox(profile) ?? AgentSandboxProfile.AgentEphemeral; |
| 122 | } |
| 123 | |