| 1 | namespace AgentForge.Abstractions; |
| 2 | |
| 3 | /// <summary>Plugins register DOI commands during host startup (Option B — imperative).</summary> |
| 4 | public sealed class ForgeCommandRegistry |
| 5 | { |
| 6 | private readonly List<ForgeCommandDescriptor> _commands = []; |
| 7 | private readonly HashSet<string> _doiKeys = new(StringComparer.OrdinalIgnoreCase); |
| 8 | |
| 9 | public IReadOnlyList<ForgeCommandDescriptor> Commands => _commands; |
| 10 | |
| 11 | public void Add(ForgeCommandDescriptor descriptor) |
| 12 | { |
| 13 | ArgumentNullException.ThrowIfNull(descriptor); |
| 14 | Validate(descriptor); |
| 15 | |
| 16 | var doiKey = $"{descriptor.Object.Trim()}.{descriptor.Intent.Trim()}"; |
| 17 | if (!_doiKeys.Add(doiKey)) |
| 18 | throw new InvalidOperationException( |
| 19 | $"Duplicate command DOI '{descriptor.Object}.{descriptor.Intent}' in plugin registration."); |
| 20 | |
| 21 | _commands.Add(descriptor); |
| 22 | } |
| 23 | |
| 24 | private static void Validate(ForgeCommandDescriptor descriptor) |
| 25 | { |
| 26 | if (string.IsNullOrWhiteSpace(descriptor.Object)) |
| 27 | throw new ArgumentException("Command object is required.", nameof(descriptor)); |
| 28 | if (string.IsNullOrWhiteSpace(descriptor.Intent)) |
| 29 | throw new ArgumentException("Command intent is required.", nameof(descriptor)); |
| 30 | if (string.IsNullOrWhiteSpace(descriptor.CommandId)) |
| 31 | throw new ArgumentException("CommandId is required.", nameof(descriptor)); |
| 32 | if (string.IsNullOrWhiteSpace(descriptor.Path)) |
| 33 | throw new ArgumentException("Command path is required.", nameof(descriptor)); |
| 34 | } |
| 35 | } |
| 36 | |