feat(bot): refactor system prompt and enhance CLI command

- Replaced `DEFAULT_SYSTEM_PROMPT` with `BOT_SYSTEM_PROMPT` for clarity and modularity.
- Introduced a `build_system_prompt` function to dynamically compose prompts.
- Enhanced `call_claude` CLI with new tool options and appendable prompts.
This commit is contained in:
Harald Hoyer 2026-02-04 09:11:08 +01:00
parent f25aab2441
commit 9b42e808d3

View file

@ -101,7 +101,8 @@ def verify_signature(body: bytes, signature: str, random: Optional[str] = None)
return False return False
DEFAULT_SYSTEM_PROMPT = """Du bist Claude, ein KI-Assistent im Nextcloud Talk Chat. BOT_SYSTEM_PROMPT = """\
Du bist ein KI-Assistent im Nextcloud Talk Chat.
Deine Antworten werden direkt in den Chatraum gepostet. Deine Antworten werden direkt in den Chatraum gepostet.
Halte deine Antworten kurz und prägnant, da es ein Chat ist. Halte deine Antworten kurz und prägnant, da es ein Chat ist.
Nutze Markdown für Formatierung wenn sinnvoll. Nutze Markdown für Formatierung wenn sinnvoll.
@ -111,18 +112,16 @@ Du erhältst:
- <current_message>: Die aktuelle Nachricht, auf die du antworten sollst""" - <current_message>: Die aktuelle Nachricht, auf die du antworten sollst"""
def build_prompt(conversation_token: str, current_message: str, current_user: str) -> str: def build_system_prompt() -> str:
"""Build prompt with in-memory conversation history using XML structure.""" """Build the full system prompt from hardcoded + optional custom parts."""
parts = []
# Add system prompt (hardcoded + optional custom)
parts.append("<system>")
parts.append(DEFAULT_SYSTEM_PROMPT)
if SYSTEM_PROMPT: if SYSTEM_PROMPT:
parts.append("") return f"{BOT_SYSTEM_PROMPT}\n\n{SYSTEM_PROMPT.strip()}"
parts.append(SYSTEM_PROMPT.strip()) return BOT_SYSTEM_PROMPT
parts.append("</system>")
parts.append("")
def build_prompt(conversation_token: str, current_message: str, current_user: str) -> str:
"""Build user prompt with in-memory conversation history using XML structure."""
parts = []
# Add chat history if available # Add chat history if available
history = conversations.get(conversation_token, []) history = conversations.get(conversation_token, [])
@ -143,9 +142,14 @@ def build_prompt(conversation_token: str, current_message: str, current_user: st
async def call_claude(prompt: str) -> str: async def call_claude(prompt: str) -> str:
"""Call Claude CLI and return response.""" """Call Claude CLI and return response."""
cmd = [CLAUDE_PATH, "--print"] cmd = [
CLAUDE_PATH, "--print",
"--tools", "WebSearch,WebFetch",
"--allowedTools", "WebSearch,WebFetch",
"--append-system-prompt", build_system_prompt(),
]
log.info(f"Calling Claude: {' '.join(cmd)}") log.info(f"Calling Claude: {cmd[0]} --print --append-system-prompt ...")
try: try:
proc = await asyncio.create_subprocess_exec( proc = await asyncio.create_subprocess_exec(