-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcommand_parser.py
More file actions
62 lines (48 loc) · 1.75 KB
/
command_parser.py
File metadata and controls
62 lines (48 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""Slash command parsing for /command syntax."""
from typing import Optional, List, Callable
from dataclasses import dataclass
@dataclass
class SlashCommand:
"""A parsed slash command."""
name: str # Command name without /
args: str # Arguments after command name
raw: str # Original input
@dataclass
class CommandDef:
"""Definition of a slash command."""
name: str
description: str
handler: Optional[Callable] = None # Local handler, or None to send to bridge
class CommandParser:
"""Parses input for /command patterns."""
BUILTIN_COMMANDS = {
"clear": "Clear conversation history (local)",
"compact": "Summarize conversation to reduce context",
"context": "Show pending context items",
}
@staticmethod
def parse(text: str) -> Optional[SlashCommand]:
"""Parse text for slash command.
Returns SlashCommand if text starts with /, None otherwise.
"""
text = text.strip()
if not text.startswith("/"):
return None
# Split into command and args
parts = text[1:].split(None, 1) # Split on whitespace, max 2 parts
if not parts:
return None
name = parts[0].lower()
args = parts[1] if len(parts) > 1 else ""
return SlashCommand(name=name, args=args, raw=text)
@staticmethod
def is_builtin(name: str) -> bool:
"""Check if command is a builtin."""
return name in CommandParser.BUILTIN_COMMANDS
@staticmethod
def get_completions() -> List[CommandDef]:
"""Get list of available commands for autocomplete."""
return [
CommandDef(name=name, description=desc)
for name, desc in CommandParser.BUILTIN_COMMANDS.items()
]