refactor(commands): own trusted Shell completion outside agent loop

This commit is contained in:
chengyongru authored and chengyongru committed 2026-11-05 16:39:09 +08:00
1 parent e6127aa324
commit 2b44088413
4 files changed
+140 -83

No files matched your search

+2 -25
View File
@@ -11,10 +11,11 @@ from typing import TYPE_CHECKING, Any, cast
from loguru import logger
from nanobot import __version__
from nanobot.bus.events import INBOUND_META_USER_SHELL, OutboundMessage
from nanobot.bus.events import OutboundMessage
from nanobot.command.router import CommandContext, CommandRouter
from nanobot.command.spec import BuiltinCommandSpec
from nanobot.command.spec import CommandLifecycle as CommandLifecycle
from nanobot.command.user_shell import cmd_user_shell
from nanobot.providers.base import LLMUsage
from nanobot.utils.cancellation import shield_and_drain
from nanobot.utils.helpers import build_status_content
@@ -924,30 +925,6 @@ async def cmd_help(ctx: CommandContext) -> OutboundMessage:
)
async def cmd_user_shell(ctx: CommandContext) -> OutboundMessage:
"""Run a trusted local ``!command`` through nanobot's exec policy."""
metadata = dict(ctx.msg.metadata or {})
if (
ctx.msg.channel != "websocket"
or metadata.get("webui") is not True
or metadata.get(INBOUND_META_USER_SHELL) is not True
):
return OutboundMessage(
channel=ctx.msg.channel,
chat_id=ctx.msg.chat_id,
content="Shell commands are only available from a trusted local client.",
metadata={**metadata, "render_as": "text"},
)
if not ctx.args.strip():
return OutboundMessage(
channel=ctx.msg.channel,
chat_id=ctx.msg.chat_id,
content="Type a command after `!`, for example `!pwd`.",
metadata={**metadata, "render_as": "text"},
)
return await ctx.loop.execute_user_shell_command(ctx)
def build_help_text(specs: tuple[BuiltinCommandSpec, ...] = CORE_COMMAND_SPECS) -> str:
"""Build help text for the commands registered in one application."""
lines = ["🐈 nanobot commands:"]