|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System.CommandLine; |
| 5 | +using System.CommandLine.Parsing; |
| 6 | +using System.Diagnostics.CodeAnalysis; |
| 7 | +using System.Reflection; |
| 8 | +using System.Text.Json; |
| 9 | +using AzureMcp.CloudArchitect.Models; |
| 10 | +using AzureMcp.CloudArchitect.Options; |
| 11 | +using AzureMcp.Core.Commands; |
| 12 | +using AzureMcp.Core.Helpers; |
| 13 | +using AzureMcp.Core.Models; |
| 14 | +using Microsoft.Extensions.Logging; |
| 15 | + |
| 16 | +namespace AzureMcp.CloudArchitect.Commands.Design; |
| 17 | + |
| 18 | +public sealed class DesignCommand(ILogger<DesignCommand> logger) : GlobalCommand<ArchitectureDesignToolOptions> |
| 19 | +{ |
| 20 | + private const string CommandTitle = "Design Azure cloud architectures through guided questions"; |
| 21 | + private readonly ILogger<DesignCommand> _logger = logger; |
| 22 | + |
| 23 | + private readonly Option<string> _questionOption = CloudArchitectOptionDefinitions.Question; |
| 24 | + private readonly Option<int> _questionNumberOption = CloudArchitectOptionDefinitions.QuestionNumber; |
| 25 | + private readonly Option<int> _questionTotalQuestions = CloudArchitectOptionDefinitions.TotalQuestions; |
| 26 | + private readonly Option<string> _answerOption = CloudArchitectOptionDefinitions.Answer; |
| 27 | + private readonly Option<bool> _nextQuestionNeededOption = CloudArchitectOptionDefinitions.NextQuestionNeeded; |
| 28 | + private readonly Option<double> _confidenceScoreOption = CloudArchitectOptionDefinitions.ConfidenceScore; |
| 29 | + |
| 30 | + private readonly Option<string> _architectureDesignToolState = CloudArchitectOptionDefinitions.State; |
| 31 | + |
| 32 | + private static readonly string s_designArchitectureText = LoadArchitectureDesignText(); |
| 33 | + |
| 34 | + private static string GetArchitectureDesignText() => s_designArchitectureText; |
| 35 | + |
| 36 | + public override string Name => "design"; |
| 37 | + |
| 38 | + public override string Description => """ |
| 39 | + Azure architecture design tool that gathers requirements through guided questions and recommends optimal solutions. |
| 40 | +
|
| 41 | + Key parameters: question, questionNumber, confidenceScore (0.0-1.0, present architecture when ≥0.7), totalQuestions, answer, nextQuestionNeeded, architectureComponent, architectureTier, state. |
| 42 | +
|
| 43 | + Process: |
| 44 | + 1. Ask about user role, business goals (1-2 questions at a time) |
| 45 | + 2. Track confidence and update requirements (explicit/implicit/assumed) |
| 46 | + 3. When confident enough, present architecture with table format, visual organization, ASCII diagrams |
| 47 | + 4. Follow Azure Well-Architected Framework principles |
| 48 | + 5. Cover all tiers: infrastructure, platform, application, data, security, operations |
| 49 | + 6. Provide actionable advice and high-level overview |
| 50 | +
|
| 51 | + State tracks components, requirements by category, and confidence factors. Be conservative with suggestions. |
| 52 | + """; |
| 53 | + |
| 54 | + public override string Title => CommandTitle; |
| 55 | + |
| 56 | + public override ToolMetadata Metadata => new() |
| 57 | + { |
| 58 | + Destructive = false, |
| 59 | + ReadOnly = true |
| 60 | + }; |
| 61 | + |
| 62 | + private static string LoadArchitectureDesignText() |
| 63 | + { |
| 64 | + Assembly assembly = typeof(DesignCommand).Assembly; |
| 65 | + string resourceName = EmbeddedResourceHelper.FindEmbeddedResource(assembly, "azure-architecture-design.txt"); |
| 66 | + return EmbeddedResourceHelper.ReadEmbeddedResource(assembly, resourceName); |
| 67 | + } |
| 68 | + |
| 69 | + protected override void RegisterOptions(Command command) |
| 70 | + { |
| 71 | + base.RegisterOptions(command); |
| 72 | + command.AddOption(_questionOption); |
| 73 | + command.AddOption(_questionNumberOption); |
| 74 | + command.AddOption(_questionTotalQuestions); |
| 75 | + command.AddOption(_answerOption); |
| 76 | + command.AddOption(_nextQuestionNeededOption); |
| 77 | + command.AddOption(_confidenceScoreOption); |
| 78 | + command.AddOption(_architectureDesignToolState); |
| 79 | + |
| 80 | + command.AddValidator(result => |
| 81 | + { |
| 82 | + // Validate confidence score is between 0.0 and 1.0 |
| 83 | + var confidenceScore = result.GetValueForOption(_confidenceScoreOption); |
| 84 | + if (confidenceScore < 0.0 || confidenceScore > 1.0) |
| 85 | + { |
| 86 | + result.ErrorMessage = "Confidence score must be between 0.0 and 1.0"; |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + // Validate question number is not negative |
| 91 | + var questionNumber = result.GetValueForOption(_questionNumberOption); |
| 92 | + if (questionNumber < 0) |
| 93 | + { |
| 94 | + result.ErrorMessage = "Question number cannot be negative"; |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + // Validate total questions is not negative |
| 99 | + var totalQuestions = result.GetValueForOption(_questionTotalQuestions); |
| 100 | + if (totalQuestions < 0) |
| 101 | + { |
| 102 | + result.ErrorMessage = "Total questions cannot be negative"; |
| 103 | + return; |
| 104 | + } |
| 105 | + }); |
| 106 | + } |
| 107 | + |
| 108 | + protected override ArchitectureDesignToolOptions BindOptions(ParseResult parseResult) |
| 109 | + { |
| 110 | + var options = base.BindOptions(parseResult); |
| 111 | + options.Question = parseResult.GetValueForOption(_questionOption) ?? string.Empty; |
| 112 | + options.QuestionNumber = parseResult.GetValueForOption(_questionNumberOption); |
| 113 | + options.TotalQuestions = parseResult.GetValueForOption(_questionTotalQuestions); |
| 114 | + options.Answer = parseResult.GetValueForOption(_answerOption); |
| 115 | + options.NextQuestionNeeded = parseResult.GetValueForOption(_nextQuestionNeededOption); |
| 116 | + options.ConfidenceScore = parseResult.GetValueForOption(_confidenceScoreOption); |
| 117 | + options.State = DeserializeState(parseResult.GetValueForOption(_architectureDesignToolState)); |
| 118 | + return options; |
| 119 | + } |
| 120 | + |
| 121 | + private static ArchitectureDesignToolState DeserializeState(string? stateJson) |
| 122 | + { |
| 123 | + if (string.IsNullOrEmpty(stateJson)) |
| 124 | + { |
| 125 | + return new ArchitectureDesignToolState(); |
| 126 | + } |
| 127 | + |
| 128 | + try |
| 129 | + { |
| 130 | + var state = JsonSerializer.Deserialize<ArchitectureDesignToolState>(stateJson, CloudArchitectJsonContext.Default.ArchitectureDesignToolState); |
| 131 | + return state ?? new ArchitectureDesignToolState(); |
| 132 | + } |
| 133 | + catch (JsonException ex) |
| 134 | + { |
| 135 | + throw new InvalidOperationException($"Failed to deserialize state JSON: {ex.Message}", ex); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + public override Task<CommandResponse> ExecuteAsync(CommandContext context, ParseResult parseResult) |
| 140 | + { |
| 141 | + try |
| 142 | + { |
| 143 | + var options = BindOptions(parseResult); |
| 144 | + |
| 145 | + if (!Validate(parseResult.CommandResult, context.Response).IsValid) |
| 146 | + { |
| 147 | + return Task.FromResult(context.Response); |
| 148 | + } |
| 149 | + |
| 150 | + var designArchitecture = GetArchitectureDesignText(); |
| 151 | + var responseObject = new CloudArchitectResponseObject |
| 152 | + { |
| 153 | + DisplayText = options.Question, |
| 154 | + DisplayThought = options.State.Thought, |
| 155 | + DisplayHint = options.State.SuggestedHint, |
| 156 | + QuestionNumber = options.QuestionNumber, |
| 157 | + TotalQuestions = options.TotalQuestions, |
| 158 | + NextQuestionNeeded = options.NextQuestionNeeded, |
| 159 | + State = options.State |
| 160 | + }; |
| 161 | + |
| 162 | + var result = new CloudArchitectDesignResponse |
| 163 | + { |
| 164 | + DesignArchitecture = designArchitecture, |
| 165 | + ResponseObject = responseObject |
| 166 | + }; |
| 167 | + |
| 168 | + context.Response.Status = 200; |
| 169 | + context.Response.Results = ResponseResult.Create(result, CloudArchitectJsonContext.Default.CloudArchitectDesignResponse); |
| 170 | + context.Response.Message = string.Empty; |
| 171 | + } |
| 172 | + catch (Exception ex) |
| 173 | + { |
| 174 | + _logger.LogError(ex, "An exception occurred in cloud architect design command"); |
| 175 | + HandleException(context, ex); |
| 176 | + } |
| 177 | + return Task.FromResult(context.Response); |
| 178 | + } |
| 179 | +} |
0 commit comments