Completer for chat/interactive slash-commands using unified command system.
| 3 | |
| 4 | |
| 5 | class ChatCommandCompleter(Completer): |
| 6 | """Completer for chat/interactive slash-commands using unified command system.""" |
| 7 | |
| 8 | def __init__(self, context): |
| 9 | self.context = context |
| 10 | |
| 11 | def get_completions(self, document, complete_event): |
| 12 | """Get completions from unified command registry.""" |
| 13 | from mcp_cli.commands.registry import UnifiedCommandRegistry |
| 14 | from mcp_cli.commands.base import CommandMode |
| 15 | from mcp_cli.commands import register_all_commands |
| 16 | |
| 17 | # Ensure commands are registered |
| 18 | register_all_commands() |
| 19 | |
| 20 | # Get text before cursor |
| 21 | text = document.text_before_cursor.lstrip() |
| 22 | |
| 23 | # Only complete if we're typing a command (starts with /) |
| 24 | if not text or not text.startswith("/"): |
| 25 | return |
| 26 | |
| 27 | # Get unified commands |
| 28 | registry = UnifiedCommandRegistry() |
| 29 | commands = registry.list_commands(mode=CommandMode.CHAT) |
| 30 | |
| 31 | # Calculate start position - how far back to replace |
| 32 | start_pos = -len(text) |
| 33 | |
| 34 | # Show all matching commands |
| 35 | for cmd in commands: |
| 36 | cmd_text = f"/{cmd.name}" |
| 37 | # Check if this command matches what's been typed |
| 38 | if cmd_text.startswith(text): |
| 39 | yield Completion( |
| 40 | cmd_text, |
| 41 | start_position=start_pos, |
| 42 | display=cmd_text, |
| 43 | display_meta=cmd.description, |
| 44 | ) |
| 45 | |
| 46 | # Also check aliases |
| 47 | for alias in cmd.aliases: |
| 48 | alias_text = f"/{alias}" |
| 49 | if alias_text.startswith(text) and alias != cmd.name: |
| 50 | yield Completion( |
| 51 | alias_text, |
| 52 | start_position=start_pos, |
| 53 | display=alias_text, |
| 54 | display_meta=f"→ /{cmd.name}", |
| 55 | ) |
no outgoing calls