fix: Text bot now reads Confluence pages and includes room docs in LLM context
Three issues fixed: 1. Confluence URLs were detected but content never fetched - now reads the actual page via API so the LLM can work with it 2. Room document context (PDFs, Confluence, images) was stored but never passed to the text LLM - now included as system message 3. Conversation history increased from 10 to 30 messages for better context in collaborative sessions Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
40
bot.py
40
bot.py
@@ -857,11 +857,25 @@ class Bot:
|
|||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
logger.warning("Confluence short link resolution failed: %s", exc)
|
logger.warning("Confluence short link resolution failed: %s", exc)
|
||||||
if confluence_page_id:
|
if confluence_page_id:
|
||||||
|
# Fetch actual page content so the text bot can work with it
|
||||||
|
conf_text = f"confluence_page_id:{confluence_page_id}"
|
||||||
|
conf_title = f"Confluence page {confluence_page_id}"
|
||||||
|
if CONFLUENCE_URL and CONFLUENCE_USER and CONFLUENCE_TOKEN:
|
||||||
|
try:
|
||||||
|
from voice import _confluence_read_page
|
||||||
|
title, plain, _ver = await _confluence_read_page(confluence_page_id)
|
||||||
|
conf_title = title
|
||||||
|
conf_text = f"confluence_page_id:{confluence_page_id}\n\nTitle: {title}\n\n{plain}"
|
||||||
|
logger.info("Fetched Confluence page %s: %s (%d chars)",
|
||||||
|
confluence_page_id, title, len(plain))
|
||||||
|
except Exception as exc:
|
||||||
|
logger.warning("Confluence page fetch failed for %s: %s",
|
||||||
|
confluence_page_id, exc)
|
||||||
docs = self._room_document_context.setdefault(room.room_id, [])
|
docs = self._room_document_context.setdefault(room.room_id, [])
|
||||||
docs.append({
|
docs.append({
|
||||||
"type": "confluence",
|
"type": "confluence",
|
||||||
"filename": f"Confluence page {confluence_page_id}",
|
"filename": conf_title,
|
||||||
"text": f"confluence_page_id:{confluence_page_id}",
|
"text": conf_text,
|
||||||
"timestamp": time.time(),
|
"timestamp": time.time(),
|
||||||
})
|
})
|
||||||
logger.info("Confluence page %s detected in room %s",
|
logger.info("Confluence page %s detected in room %s",
|
||||||
@@ -1467,7 +1481,7 @@ class Bot:
|
|||||||
history = []
|
history = []
|
||||||
try:
|
try:
|
||||||
resp = await self.client.room_messages(
|
resp = await self.client.room_messages(
|
||||||
room.room_id, start=self.client.next_batch or "", limit=10
|
room.room_id, start=self.client.next_batch or "", limit=30
|
||||||
)
|
)
|
||||||
if hasattr(resp, "chunk"):
|
if hasattr(resp, "chunk"):
|
||||||
for evt in reversed(resp.chunk):
|
for evt in reversed(resp.chunk):
|
||||||
@@ -1494,12 +1508,32 @@ class Bot:
|
|||||||
memories = await self.memory.query(sender, user_message, top_k=10) if sender else []
|
memories = await self.memory.query(sender, user_message, top_k=10) if sender else []
|
||||||
memory_context = self._format_memories(memories)
|
memory_context = self._format_memories(memories)
|
||||||
|
|
||||||
|
# Include room document context (PDFs, Confluence pages, images uploaded to room)
|
||||||
|
room_doc_context = ""
|
||||||
|
room_docs = [e for e in self._room_document_context.get(room.room_id, [])
|
||||||
|
if time.time() - e["timestamp"] < 3600]
|
||||||
|
if room_docs:
|
||||||
|
parts = []
|
||||||
|
for e in room_docs:
|
||||||
|
label = {"pdf": "PDF", "image": "Image", "confluence": "Confluence",
|
||||||
|
"text": "File", "docx": "Word"}.get(e["type"], "Document")
|
||||||
|
text = e["text"][:20000] if e["type"] != "image" else e["text"][:2000]
|
||||||
|
parts.append(f"[{label}: {e['filename']}]\n{text}")
|
||||||
|
room_doc_context = (
|
||||||
|
"Documents available in this room (uploaded or linked by the user):\n\n"
|
||||||
|
+ "\n\n---\n\n".join(parts)
|
||||||
|
+ "\n\nUse these documents to answer questions. "
|
||||||
|
"You CAN access and read these documents — never say you cannot."
|
||||||
|
)
|
||||||
|
|
||||||
# Build conversation context
|
# Build conversation context
|
||||||
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
||||||
if memory_context:
|
if memory_context:
|
||||||
messages.append({"role": "system", "content": memory_context})
|
messages.append({"role": "system", "content": memory_context})
|
||||||
if doc_context:
|
if doc_context:
|
||||||
messages.append({"role": "system", "content": doc_context})
|
messages.append({"role": "system", "content": doc_context})
|
||||||
|
if room_doc_context:
|
||||||
|
messages.append({"role": "system", "content": room_doc_context})
|
||||||
messages.extend(history)
|
messages.extend(history)
|
||||||
|
|
||||||
# Add current user message (multimodal if image provided)
|
# Add current user message (multimodal if image provided)
|
||||||
|
|||||||
Reference in New Issue
Block a user