|
| 1 | +"""Validated command catalog facade. |
| 2 | +
|
| 3 | +This module centralizes the rules that relate the public argparse |
| 4 | +command set to slash-visible builtins and interactive-local slash |
| 5 | +commands. It intentionally wraps the legacy ``BUILTIN_SLASH_COMMANDS`` |
| 6 | +constant instead of replacing it in one large rewrite. |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +from dataclasses import dataclass |
| 12 | +from typing import Iterable, Sequence |
| 13 | + |
| 14 | +from .slash_commands import BUILTIN_SLASH_COMMANDS, BuiltinSlashCommand |
| 15 | + |
| 16 | + |
| 17 | +SLASH_LOCAL_NAMES = frozenset({"help", "model", "reload", "quit"}) |
| 18 | +ARGPARSE_ONLY_NAMES = frozenset({"shell", "tui", "slash"}) |
| 19 | + |
| 20 | + |
| 21 | +@dataclass(frozen=True) |
| 22 | +class CommandCatalogEntry: |
| 23 | + name: str |
| 24 | + description: str |
| 25 | + slash_visible: bool |
| 26 | + argparse_registered: bool |
| 27 | + interactive_local: bool = False |
| 28 | + source: str = "builtin" |
| 29 | + |
| 30 | + def to_dict(self) -> dict[str, object]: |
| 31 | + return { |
| 32 | + "name": self.name, |
| 33 | + "description": self.description, |
| 34 | + "slash_visible": self.slash_visible, |
| 35 | + "argparse_registered": self.argparse_registered, |
| 36 | + "interactive_local": self.interactive_local, |
| 37 | + "source": self.source, |
| 38 | + } |
| 39 | + |
| 40 | + |
| 41 | +@dataclass(frozen=True) |
| 42 | +class CommandCatalogValidation: |
| 43 | + errors: tuple[str, ...] = () |
| 44 | + |
| 45 | + @property |
| 46 | + def ok(self) -> bool: |
| 47 | + return not self.errors |
| 48 | + |
| 49 | + |
| 50 | +def iter_builtin_slash_commands() -> tuple[BuiltinSlashCommand, ...]: |
| 51 | + """Return the slash-visible builtin command catalog.""" |
| 52 | + return BUILTIN_SLASH_COMMANDS |
| 53 | + |
| 54 | + |
| 55 | +def builtin_slash_by_name(name: str) -> BuiltinSlashCommand | None: |
| 56 | + for entry in iter_builtin_slash_commands(): |
| 57 | + if entry.name == name: |
| 58 | + return entry |
| 59 | + return None |
| 60 | + |
| 61 | + |
| 62 | +def build_command_catalog(handler_names: Iterable[str]) -> tuple[CommandCatalogEntry, ...]: |
| 63 | + """Build the top-level command catalog from handler names and slash data.""" |
| 64 | + handlers = set(handler_names) |
| 65 | + entries: list[CommandCatalogEntry] = [] |
| 66 | + for item in iter_builtin_slash_commands(): |
| 67 | + entries.append( |
| 68 | + CommandCatalogEntry( |
| 69 | + name=item.name, |
| 70 | + description=item.description, |
| 71 | + slash_visible=True, |
| 72 | + argparse_registered=item.name in handlers, |
| 73 | + interactive_local=item.name in SLASH_LOCAL_NAMES, |
| 74 | + ) |
| 75 | + ) |
| 76 | + descriptions = { |
| 77 | + "shell": "Open the interactive companion shell", |
| 78 | + "tui": "Open the Textual Terminal User Interface", |
| 79 | + "slash": "Inspect slash command catalog entries", |
| 80 | + } |
| 81 | + for name in sorted(ARGPARSE_ONLY_NAMES & handlers): |
| 82 | + entries.append( |
| 83 | + CommandCatalogEntry( |
| 84 | + name=name, |
| 85 | + description=descriptions.get(name, f"Mythic Vibe CLI subcommand: {name}"), |
| 86 | + slash_visible=False, |
| 87 | + argparse_registered=True, |
| 88 | + ) |
| 89 | + ) |
| 90 | + return tuple(entries) |
| 91 | + |
| 92 | + |
| 93 | +def validate_command_catalog( |
| 94 | + handler_names: Iterable[str], |
| 95 | + *, |
| 96 | + builtin_commands: Sequence[BuiltinSlashCommand] | None = None, |
| 97 | +) -> CommandCatalogValidation: |
| 98 | + """Validate command/slash catalog invariants. |
| 99 | +
|
| 100 | + Rules: |
| 101 | + - slash builtin names must be unique, |
| 102 | + - every non-local slash builtin must have an argparse handler, |
| 103 | + - every handler except argparse-only commands must have a slash entry, |
| 104 | + - interactive-local slash names must not also be argparse handlers. |
| 105 | + """ |
| 106 | + handlers = set(handler_names) |
| 107 | + builtins = tuple(BUILTIN_SLASH_COMMANDS if builtin_commands is None else builtin_commands) |
| 108 | + names = [entry.name for entry in builtins] |
| 109 | + builtin_names = set(names) |
| 110 | + errors: list[str] = [] |
| 111 | + |
| 112 | + duplicates = sorted({name for name in names if names.count(name) > 1}) |
| 113 | + if duplicates: |
| 114 | + errors.append(f"duplicate slash builtin names: {duplicates}") |
| 115 | + |
| 116 | + missing_handlers = sorted((builtin_names - SLASH_LOCAL_NAMES) - handlers) |
| 117 | + if missing_handlers: |
| 118 | + errors.append(f"slash builtins without argparse handlers: {missing_handlers}") |
| 119 | + |
| 120 | + missing_slash = sorted((handlers - ARGPARSE_ONLY_NAMES) - builtin_names) |
| 121 | + if missing_slash: |
| 122 | + errors.append(f"argparse handlers without slash builtins: {missing_slash}") |
| 123 | + |
| 124 | + locals_with_argparse = sorted(SLASH_LOCAL_NAMES & handlers) |
| 125 | + if locals_with_argparse: |
| 126 | + errors.append(f"interactive-local slash names registered in argparse: {locals_with_argparse}") |
| 127 | + |
| 128 | + return CommandCatalogValidation(errors=tuple(errors)) |
| 129 | + |
| 130 | + |
| 131 | +__all__ = [ |
| 132 | + "ARGPARSE_ONLY_NAMES", |
| 133 | + "CommandCatalogEntry", |
| 134 | + "CommandCatalogValidation", |
| 135 | + "SLASH_LOCAL_NAMES", |
| 136 | + "build_command_catalog", |
| 137 | + "builtin_slash_by_name", |
| 138 | + "iter_builtin_slash_commands", |
| 139 | + "validate_command_catalog", |
| 140 | +] |
0 commit comments