Hausmeister: Fix History-Poisoning + Message-Logging-Reihenfolge + Einzelmessage-Fallback

This commit is contained in:
root 2026-03-15 12:32:45 +07:00
parent 339a087ac4
commit 5eea29f284
3 changed files with 22 additions and 11 deletions

View file

@ -391,14 +391,13 @@ def ask_with_tools(question: str, tool_handlers: dict, session_id: str = None) -
if session_id:
try:
import memory_client
history = memory_client.get_session_messages(session_id, limit=20)
history = memory_client.get_session_messages(session_id, limit=10)
for msg in history:
if msg.get("role") in ("user", "assistant") and msg.get("content"):
messages.append({"role": msg["role"], "content": msg["content"]})
except Exception:
pass
if not any(m.get("content") == question for m in messages):
messages.append({"role": "user", "content": question})
try:

View file

@ -212,25 +212,38 @@ def get_session_summary(session_id: str, limit: int = 20, topic: str = None) ->
return "Keine Themen in dieser Session."
if topic:
matching = []
matching_pairs = []
matching_singles = []
other_topics = []
for q, a in exchanges:
combined = q + " " + (a or "")
if _topic_matches(combined, topic):
matching.append((q, a))
matching_pairs.append((q, a))
else:
other_topics.append(q[:80])
# Fallback: einzelne Messages pruefen (falls Pairing unvollstaendig)
if not matching_pairs:
for msg in messages:
content = (msg.get("content") or "").strip()
if content and _topic_matches(content, topic):
role = msg.get("role", "?")
matching_singles.append((role, content[:200]))
lines = []
if matching:
lines.append("Zum Thema '" + topic + "' (" + str(len(matching)) + " Treffer):")
for i, (q, a) in enumerate(matching, 1):
if matching_pairs:
lines.append("Zum Thema '" + topic + "' (" + str(len(matching_pairs)) + " Treffer):")
for i, (q, a) in enumerate(matching_pairs, 1):
line = str(i) + ". Frage: " + q
if a:
line += "\n Antwort: " + a
lines.append(line)
elif matching_singles:
lines.append("Zum Thema '" + topic + "' (" + str(len(matching_singles)) + " relevante Messages):")
for role, content in matching_singles:
lines.append(" [" + role + "] " + content)
else:
lines.append("Zum Thema '" + topic + "' wurde in dieser Session nichts direkt besprochen.")
lines.append("Zum Thema '" + topic + "' wurde in dieser Session nichts besprochen.")
if other_topics:
lines.append("\nSonstige Themen der Session: " + ", ".join(other_topics))

View file

@ -308,14 +308,13 @@ async def handle_message(update: Update, ctx: ContextTypes.DEFAULT_TYPE):
channel_key = str(update.effective_chat.id)
session_id = memory_client.get_or_create_session(channel_key, source="telegram")
if session_id:
memory_client.log_message(session_id, "user", text)
await update.message.reply_text("🤔 Denke nach...")
try:
handlers = context.get_tool_handlers(session_id=session_id)
answer = llm.ask_with_tools(text, handlers, session_id=session_id)
if session_id:
memory_client.log_message(session_id, "user", text)
memory_client.log_message(session_id, "assistant", answer)
await update.message.reply_text(answer[:4000], reply_markup=KEYBOARD)
except Exception as e: