refactor(command): declare session queue policy at registration (#6170)

* refactor(command): distinguish handled commands without replies

* refactor(command): declare session queue scheduling at registration
This commit is contained in:
chengyongru authored and GitHub committed 2026-10-12 13:38:14 +08:00
1 parent 61fa3aa83c
commit 67742b2880
4 files changed
+39 -27

No files matched your search

+11 -1
View File
@@ -71,12 +71,17 @@ class CommandRouter:
self._priority: dict[str, Handler] = {}
self._exact: dict[str, Handler] = {}
self._prefix: list[tuple[str, Handler]] = []
self._session_commands: set[str] = set()
def priority(self, cmd: str, handler: Handler) -> None:
self._priority[cmd] = handler
def exact(self, cmd: str, handler: Handler) -> None:
def exact(self, cmd: str, handler: Handler, *, session_queue: bool = False) -> None:
self._exact[cmd] = handler
if session_queue:
self._session_commands.add(cmd)
else:
self._session_commands.discard(cmd)
def prefix(self, pfx: str, handler: Handler) -> None:
self._prefix.append((pfx, handler))
@@ -85,6 +90,11 @@ class CommandRouter:
def is_priority(self, text: str) -> bool:
return normalize_command_text(text).lower() in self._priority
def requires_session_queue(self, text: str) -> bool:
"""Whether an exact command must run on the session worker."""
cmd = normalize_command_text(text).lower()
return cmd in self._session_commands and cmd not in self._priority
def command_name(self, text: str) -> str | None:
"""Return the registered command name, or None for ordinary chat text.