42 lines
1.0 KiB
Python
42 lines
1.0 KiB
Python
|
|
from dataclasses import dataclass
|
||
|
|
from enum import StrEnum
|
||
|
|
|
||
|
|
from yuxi.channels.models import ChannelMessage
|
||
|
|
|
||
|
|
|
||
|
|
class ContextCommand(StrEnum):
|
||
|
|
RESET = "reset"
|
||
|
|
HISTORY = "history"
|
||
|
|
CONTEXT = "context"
|
||
|
|
SUMMARY = "summary"
|
||
|
|
|
||
|
|
|
||
|
|
@dataclass
|
||
|
|
class ContextCommandResult:
|
||
|
|
handled: bool
|
||
|
|
command: ContextCommand | None = None
|
||
|
|
args: str = ""
|
||
|
|
is_unknown_command: bool = False
|
||
|
|
|
||
|
|
|
||
|
|
class ContextPolicy:
|
||
|
|
COMMAND_PREFIX = "/"
|
||
|
|
|
||
|
|
def parse(self, message: ChannelMessage) -> ContextCommandResult:
|
||
|
|
content = message.content.strip()
|
||
|
|
if not content.startswith(self.COMMAND_PREFIX):
|
||
|
|
return ContextCommandResult(handled=False)
|
||
|
|
|
||
|
|
parts = content[1:].split(maxsplit=1)
|
||
|
|
cmd = parts[0].lower()
|
||
|
|
args = parts[1] if len(parts) > 1 else ""
|
||
|
|
|
||
|
|
try:
|
||
|
|
return ContextCommandResult(
|
||
|
|
handled=True,
|
||
|
|
command=ContextCommand(cmd),
|
||
|
|
args=args,
|
||
|
|
)
|
||
|
|
except ValueError:
|
||
|
|
return ContextCommandResult(handled=False, is_unknown_command=True)
|