Agent Zero AI framework
Scope Telegram integration commands
Anmol Malikabf0cef8e8d7
diff --git a/helpers/integration_commands.py b/helpers/integration_commands.pyindex e4a09b32b..24a78e60f 100644--- a/helpers/integration_commands.py+++ b/helpers/integration_commands.py@@ -25,6 +25,7 @@ class IntegrationCommandDef:aliases: tuple[str, ...] = ()args_hint: str = ""menu: bool = True+ integrations: tuple[str, ...] = ()COMMAND_REGISTRY: tuple[IntegrationCommandDef, ...] = (@@ -40,6 +41,7 @@ COMMAND_REGISTRY: tuple[IntegrationCommandDef, ...] = ("Show or switch recent chat sessions.","Session",aliases=("session",),+ integrations=("telegram",),),IntegrationCommandDef("clear", "Reset the current chat context.", "Session", aliases=("reset",)),IntegrationCommandDef(@@ -63,12 +65,14 @@ COMMAND_REGISTRY: tuple[IntegrationCommandDef, ...] = ("Enable or disable Telegram response streaming.","Configuration",args_hint="[on|off]",+ integrations=("telegram",),),IntegrationCommandDef("tools","Show or hide Telegram tool progress.","Configuration",args_hint="[on|off]",+ integrations=("telegram",),),IntegrationCommandDef("project",@@ -109,46 +113,55 @@ def extract_command_line(text: str) -> str:return ""-def parse_command(text: str) -> tuple[str, str] | None:+def parse_command(text: str, *, integration: str | None = None) -> tuple[str, str] | None:line = extract_command_line(text)if not line.startswith("/"):return Nonecommand, _, args = line.partition(" ")command = _normalize_command_token(command)- resolved = resolve_command(command)+ resolved = resolve_command(command, integration=integration)if not resolved:return Nonereturn f"/{resolved.name}", args.strip()-def resolve_command(command: str) -> IntegrationCommandDef | None:+def resolve_command(command: str, *, integration: str | None = None) -> IntegrationCommandDef | None:normalized = _normalize_command_token(command)if not normalized.startswith("/"):normalized = f"/{normalized}"- return _COMMAND_LOOKUP.get(normalized)+ command_def = _COMMAND_LOOKUP.get(normalized)+ if not command_def or not _is_command_available(command_def, integration):+ return None+ return command_defdef telegram_menu_commands() -> list[tuple[str, str]]:return [(command.name, _telegram_description(command))for command in COMMAND_REGISTRY- if command.menu+ if command.menu and _is_command_available(command, "telegram")]-def command_names(include_aliases: bool = True) -> list[str]:+def command_names(include_aliases: bool = True, *, integration: str | None = None) -> list[str]:names: list[str] = []for command in COMMAND_REGISTRY:+ if not _is_command_available(command, integration):+ continuenames.append(command.name)if include_aliases:names.extend(command.aliases)return names-def help_text(*, full: bool = False) -> str:- commands = COMMAND_REGISTRY if full else tuple(c for c in COMMAND_REGISTRY if c.menu)+def help_text(*, full: bool = False, integration: str | None = None) -> str:+ commands = tuple(+ command+ for command in COMMAND_REGISTRY+ if _is_command_available(command, integration) and (full or command.menu)+ )lines = ["Available commands:"]for command in commands:args = f" {command.args_hint}" if command.args_hint else ""@@ -159,19 +172,24 @@ def help_text(*, full: bool = False) -> str:return "\n".join(lines)-def unknown_command_text(command: str) -> str:+def unknown_command_text(command: str, *, integration: str | None = None) -> str:token = _normalize_command_token(command).split(" ", 1)[0]- return f"Unknown command: {token}\n\n{help_text(full=True)}"+ return f"Unknown command: {token}\n\n{help_text(full=True, integration=integration)}"-def try_handle_command(context: "AgentContext", text: str) -> str | None:- parsed = parse_command(text)+def try_handle_command(+ context: "AgentContext",+ text: str,+ *,+ integration: str | None = None,+) -> str | None:+ parsed = parse_command(text, integration=integration)if not parsed:return Nonecommand, args = parsedif command == "/commands":- return help_text(full=True)+ return help_text(full=True, integration=integration)if command == "/status":return _handle_status(context)if command == "/sessions":@@ -213,6 +231,14 @@ def _normalize_command_token(command: str) -> str:return f"{token} {rest[0]}".strip() if rest else token+def _is_command_available(command: IntegrationCommandDef, integration: str | None) -> bool:+ if not command.integrations:+ return True+ if not integration:+ return False+ return integration.lower() in command.integrations++def _handle_queue(context: "AgentContext", args: str) -> str:queue = mq.get_queue(context)count = len(queue)diff --git a/plugins/_telegram_integration/extensions/python/system_prompt/_20_telegram_context.py b/plugins/_telegram_integration/extensions/python/system_prompt/_20_telegram_context.pyindex 9d337cf8d..3d10d594b 100644--- a/plugins/_telegram_integration/extensions/python/system_prompt/_20_telegram_context.py+++ b/plugins/_telegram_integration/extensions/python/system_prompt/_20_telegram_context.py@@ -40,8 +40,8 @@ def _telegram_commands_prompt() -> str:"If the user asks what commands exist, refer them to /commands.","Current integration commands:",]- for name in integration_commands.command_names(include_aliases=False):- definition = integration_commands.resolve_command(name)+ for name in integration_commands.command_names(include_aliases=False, integration="telegram"):+ definition = integration_commands.resolve_command(name, integration="telegram")if definition:args = f" {definition.args_hint}" if definition.args_hint else ""lines.append(f"- /{definition.name}{args}: {definition.description}")diff --git a/plugins/_telegram_integration/helpers/bot_manager.py b/plugins/_telegram_integration/helpers/bot_manager.pyindex 757804c21..1eec11440 100644--- a/plugins/_telegram_integration/helpers/bot_manager.py+++ b/plugins/_telegram_integration/helpers/bot_manager.py@@ -59,7 +59,7 @@ def create_bot(if on_command_control:router.message.register(on_command_control,- Command(commands=integration_commands.command_names()),+ Command(commands=integration_commands.command_names(integration="telegram")),)if on_callback_query:diff --git a/plugins/_telegram_integration/helpers/command_ui.py b/plugins/_telegram_integration/helpers/command_ui.pyindex a0f108d0f..b31dc0501 100644--- a/plugins/_telegram_integration/helpers/command_ui.py+++ b/plugins/_telegram_integration/helpers/command_ui.py@@ -47,7 +47,7 @@ async def handle_command(reply_to_message_id: int | None,text: str,) -> bool:- parsed = integration_commands.parse_command(text or "")+ parsed = integration_commands.parse_command(text or "", integration="telegram")if not parsed:return Falsecommand, args = parseddiff --git a/plugins/_telegram_integration/helpers/handler.py b/plugins/_telegram_integration/helpers/handler.pyindex 3fd0f8c82..5adad0e12 100644--- a/plugins/_telegram_integration/helpers/handler.py+++ b/plugins/_telegram_integration/helpers/handler.py@@ -186,7 +186,7 @@ async def handle_message(message: TgMessage, bot_name: str, bot_cfg: dict):):return- command_reply = integration_commands.try_handle_command(context, text)+ command_reply = integration_commands.try_handle_command(context, text, integration="telegram")if command_reply is not None:await _send_with_temp_bot(instance.bot.token,@@ -201,7 +201,7 @@ async def handle_message(message: TgMessage, bot_name: str, bot_cfg: dict):await _send_with_temp_bot(instance.bot.token,message.chat.id,- integration_commands.unknown_command_text(command),+ integration_commands.unknown_command_text(command, integration="telegram"),parse_mode=None,reply_to_message_id=message.message_id,)@@ -303,7 +303,7 @@ async def handle_callback_query(query: CallbackQuery, bot_name: str, bot_cfg: diif text.startswith("tg:"):return- command_reply = integration_commands.try_handle_command(context, text)+ command_reply = integration_commands.try_handle_command(context, text, integration="telegram")if command_reply is not None:if instance:await _send_with_temp_bot(