fix(commands): keep slash-prefixed paths in normal chat
This commit is contained in:
1 parent
3fb9417daf
commit
7e00d6a944
7 files changed
+159
-76
No files matched your search
+27
-33
@@ -5,7 +5,6 @@ from __future__ import annotations
|
||||
import re
|
||||
from contextlib import AbstractContextManager
|
||||
from dataclasses import dataclass, field
|
||||
from difflib import get_close_matches
|
||||
from typing import TYPE_CHECKING, Any, Awaitable, Callable
|
||||
|
||||
from nanobot.bus.events import InboundMessage, OutboundMessage
|
||||
@@ -86,12 +85,21 @@ class CommandRouter:
|
||||
def is_priority(self, text: str) -> bool:
|
||||
return normalize_command_text(text).lower() in self._priority
|
||||
|
||||
def command_name(self, text: str) -> str | None:
|
||||
"""Return the registered command name, or None for ordinary chat text.
|
||||
|
||||
A leading slash alone is not command intent: absolute paths and other
|
||||
slash-prefixed text must remain available to the agent.
|
||||
"""
|
||||
parts = normalize_command_text(text).split(maxsplit=1)
|
||||
return self._registered_commands().get(parts[0].lower()) if parts else None
|
||||
|
||||
def is_dispatchable_command(self, text: str) -> bool:
|
||||
"""Check whether *text* should be handled by non-priority dispatch.
|
||||
|
||||
Exact priority commands are handled separately. Recognized non-priority
|
||||
commands and invalid slash commands are dispatched here so malformed
|
||||
commands can be rejected instead of reaching the LLM.
|
||||
commands and malformed uses of registered commands are dispatched here
|
||||
so argument errors can be rejected instead of reaching the LLM.
|
||||
"""
|
||||
cmd = normalize_command_text(text).lower()
|
||||
if cmd in self._priority:
|
||||
@@ -101,7 +109,7 @@ class CommandRouter:
|
||||
for pfx, _ in self._prefix:
|
||||
if cmd.startswith(pfx):
|
||||
return True
|
||||
return cmd.startswith("/")
|
||||
return self.command_name(text) is not None
|
||||
|
||||
async def dispatch_priority(self, ctx: CommandContext) -> InboundMessage | OutboundMessage | None:
|
||||
"""Dispatch a priority command. Called from run() without the lock."""
|
||||
@@ -112,7 +120,7 @@ class CommandRouter:
|
||||
return None
|
||||
|
||||
async def dispatch(self, ctx: CommandContext) -> InboundMessage | OutboundMessage | None:
|
||||
"""Try exact and prefix handlers, then reject invalid slash commands."""
|
||||
"""Try exact and prefix handlers, then validate registered commands."""
|
||||
if ctx.msg.metadata.get("original_command"):
|
||||
return None
|
||||
ctx.raw = normalize_command_text(ctx.raw)
|
||||
@@ -129,39 +137,25 @@ class CommandRouter:
|
||||
return self._invalid_command_response(ctx)
|
||||
|
||||
def _invalid_command_response(self, ctx: CommandContext) -> OutboundMessage | None:
|
||||
if not ctx.raw.startswith("/"):
|
||||
canonical = self.command_name(ctx.raw)
|
||||
if canonical is None:
|
||||
return None
|
||||
|
||||
entered = ctx.raw.split(maxsplit=1)[0]
|
||||
commands = self._registered_commands()
|
||||
canonical = commands.get(entered.lower())
|
||||
if canonical is not None:
|
||||
accepts_args = any(
|
||||
pfx.rstrip().lower() == entered.lower()
|
||||
for pfx, _ in self._prefix
|
||||
accepts_args = any(
|
||||
pfx.rstrip().lower() == entered.lower()
|
||||
for pfx, _ in self._prefix
|
||||
)
|
||||
if accepts_args:
|
||||
content = (
|
||||
f'Invalid command "{entered}". '
|
||||
'Use "/help" to list available commands.'
|
||||
)
|
||||
if accepts_args:
|
||||
content = (
|
||||
f'Invalid command "{entered}". '
|
||||
'Use "/help" to list available commands.'
|
||||
)
|
||||
else:
|
||||
content = (
|
||||
f'Command "{canonical}" does not accept arguments. '
|
||||
f'Did you mean "{canonical}"?'
|
||||
)
|
||||
else:
|
||||
matches = get_close_matches(entered.lower(), commands, n=1, cutoff=0.6)
|
||||
if matches:
|
||||
content = (
|
||||
f'Unknown command "{entered}". '
|
||||
f'Did you mean "{commands[matches[0]]}"?'
|
||||
)
|
||||
else:
|
||||
content = (
|
||||
f'Unknown command "{entered}". '
|
||||
'Use "/help" to list available commands.'
|
||||
)
|
||||
content = (
|
||||
f'Command "{canonical}" does not accept arguments. '
|
||||
f'Did you mean "{canonical}"?'
|
||||
)
|
||||
|
||||
return OutboundMessage(
|
||||
channel=ctx.msg.channel,
|
||||
|
||||
Reference in new issue
Block a user