From f25aab2441282479a2f47a9b2c4e3c4e4ccd7e18 Mon Sep 17 00:00:00 2001 From: Harald Hoyer Date: Tue, 3 Feb 2026 17:39:31 +0100 Subject: [PATCH] feat(bot): improve prompt-building and help command handling - Added a default system prompt and adjusted the structure to use XML for clarity. - Improved help command handling by simplifying triggers and updating responses. - Enhanced NixOS configuration with support for optional custom instructions. --- .../mx/nextcloud-claude-bot/bot.py | 41 ++++++++++++++----- .../mx/nextcloud-claude-bot/default.nix | 2 + 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/systems/x86_64-linux/mx/nextcloud-claude-bot/bot.py b/systems/x86_64-linux/mx/nextcloud-claude-bot/bot.py index 2f1f162..6962f5c 100644 --- a/systems/x86_64-linux/mx/nextcloud-claude-bot/bot.py +++ b/systems/x86_64-linux/mx/nextcloud-claude-bot/bot.py @@ -101,22 +101,44 @@ def verify_signature(body: bytes, signature: str, random: Optional[str] = None) return False +DEFAULT_SYSTEM_PROMPT = """Du bist Claude, ein KI-Assistent im Nextcloud Talk Chat. +Deine Antworten werden direkt in den Chatraum gepostet. +Halte deine Antworten kurz und prägnant, da es ein Chat ist. +Nutze Markdown für Formatierung wenn sinnvoll. + +Du erhältst: +- : Die letzten Nachrichten im Chatraum (User und deine Antworten) +- : Die aktuelle Nachricht, auf die du antworten sollst""" + + def build_prompt(conversation_token: str, current_message: str, current_user: str) -> str: - """Build prompt with in-memory conversation history.""" + """Build prompt with in-memory conversation history using XML structure.""" parts = [] + # Add system prompt (hardcoded + optional custom) + parts.append("") + parts.append(DEFAULT_SYSTEM_PROMPT) if SYSTEM_PROMPT: - parts.append(f"System: {SYSTEM_PROMPT}\n") + parts.append("") + parts.append(SYSTEM_PROMPT.strip()) + parts.append("") + parts.append("") - # Add recent history from memory + # Add chat history if available history = conversations.get(conversation_token, []) - for role, msg in history[-MAX_HISTORY:]: - parts.append(f"{role}: {msg}") + if history: + parts.append("") + for role, msg in history[-MAX_HISTORY:]: + parts.append(f"{role}: {msg}") + parts.append("") + parts.append("") # Add current message - parts.append(f"User ({current_user}): {current_message}") + parts.append(f"") + parts.append(current_message) + parts.append("") - return "\n\n".join(parts) + return "\n".join(parts) async def call_claude(prompt: str) -> str: @@ -277,17 +299,16 @@ async def handle_webhook( log.info(f"Processing message from {actor_id}: {message_text[:100]}") - if message_text.strip().lower() in ("/help", "/hilfe"): + if message_text.strip().lower() in ("hilfe", "help", "?"): help_text = """🤖 **Claude Bot Hilfe** Schreib mir einfach eine Nachricht und ich antworte dir. **Nutzung:** -• In Direktnachrichten: Einfach schreiben • In Gruppenchats: @Claude gefolgt von deiner Frage **Befehle:** -• `/help` oder `/hilfe` – Diese Hilfe anzeigen +• `hilfe` oder `?` – Diese Hilfe anzeigen Der Bot merkt sich die letzten Nachrichten pro Raum (bis zum Neustart).""" await send_reply(conversation_token, help_text, reply_to=message_id) diff --git a/systems/x86_64-linux/mx/nextcloud-claude-bot/default.nix b/systems/x86_64-linux/mx/nextcloud-claude-bot/default.nix index c74f2ed..9bda0c7 100644 --- a/systems/x86_64-linux/mx/nextcloud-claude-bot/default.nix +++ b/systems/x86_64-linux/mx/nextcloud-claude-bot/default.nix @@ -7,6 +7,8 @@ nextcloudUrl = "https://nc.hoyer.xyz"; botSecretFile = config.sops.secrets."nextcloud-claude-bot/secret".path; allowedUsers = []; # Allow all registered users + # Optional extra instructions (base prompt is hardcoded in bot.py) + # systemPrompt = "Additional custom instructions here"; }; sops.secrets."nextcloud-claude-bot/secret" = {