| 1 | using System.Text.Json; |
| 2 | |
| 3 | using CascadeIDE.Models.Editor; |
| 4 | |
| 5 | namespace CascadeIDE.Features.IdeMcp.Execution; |
| 6 | |
| 7 | /// <summary>MCP: состояние редактора, диапазон текста и текст открытого документа.</summary> |
| 8 | internal sealed partial class IdeMcpCommandExecutor |
| 9 | { |
| 10 | private void RegisterEditorStateAndContent(Action<string, Handler> add) |
| 11 | { |
| 12 | add(Services.IdeCommands.GetEditorState, async (args, _) => |
| 13 | { |
| 14 | var a = _actions; |
| 15 | return await a.GetEditorStateAsync(args is not null && args.TryGetValue("max_preview_chars", out var mpc) && mpc.TryGetInt32(out var maxPreview) ? maxPreview : null); |
| 16 | }); |
| 17 | add(Services.IdeCommands.GetEditorContentRange, async (args, _) => |
| 18 | { |
| 19 | var a = _actions; |
| 20 | if (!EditorContentLineRangeMcpArgs.TryParse(args, out var lines, out var errLines)) |
| 21 | return errLines; |
| 22 | return await a.GetEditorContentRangeAsync(lines.Start.Value, lines.End.Value); |
| 23 | }); |
| 24 | add(Services.IdeCommands.GetOpenDocumentText, async (args, _) => |
| 25 | { |
| 26 | var a = _actions; |
| 27 | int? maxCharsOpen = null; |
| 28 | if (args is not null && args.TryGetValue("max_chars", out var mco) && mco.ValueKind == JsonValueKind.Number && mco.TryGetInt32(out var mcOpen) && mcOpen > 0) |
| 29 | maxCharsOpen = mcOpen; |
| 30 | return await a.GetOpenDocumentTextAsync(McpCommandJsonArgs.String(args, "file_path"), maxCharsOpen); |
| 31 | }); |
| 32 | add(Services.IdeCommands.ReadWorkspaceFile, async (args, _) => |
| 33 | { |
| 34 | var a = _actions; |
| 35 | var path = McpCommandJsonArgs.String(args, "file_path"); |
| 36 | if (string.IsNullOrWhiteSpace(path)) |
| 37 | return """{"error":"no_path","message":"file_path is required."}"""; |
| 38 | int? offset = null; |
| 39 | int? limit = null; |
| 40 | int? maxChars = null; |
| 41 | if (args is not null) |
| 42 | { |
| 43 | if (args.TryGetValue("offset", out var off) && off.TryGetInt32(out var o) && o > 0) |
| 44 | offset = o; |
| 45 | if (args.TryGetValue("limit", out var lim) && lim.TryGetInt32(out var l) && l > 0) |
| 46 | limit = l; |
| 47 | if (args.TryGetValue("max_chars", out var mc) && mc.TryGetInt32(out var m) && m > 0) |
| 48 | maxChars = m; |
| 49 | } |
| 50 | |
| 51 | return await a.ReadWorkspaceFileAsync(path, offset, limit, maxChars); |
| 52 | }); |
| 53 | add(Services.IdeCommands.SaveDocument, async (args, _) => |
| 54 | await _actions.SaveDocumentAsync( |
| 55 | McpCommandJsonArgs.String(args, "file_path"), |
| 56 | McpCommandJsonArgs.String(args, "content"))); |
| 57 | } |
| 58 | } |
| 59 | |