router: add tool manager runtime and memory retrieval updates
This commit is contained in:
@@ -18,6 +18,7 @@ Collections per agent:
|
||||
import os
|
||||
import json
|
||||
import logging
|
||||
import re
|
||||
from typing import Optional, Dict, Any, List
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import datetime
|
||||
@@ -453,7 +454,7 @@ class MemoryRetrieval:
|
||||
# Higher threshold for messages; even higher when user asks about docs to avoid pulling old chatter.
|
||||
msg_thresh = 0.5 if is_doc_query else 0.4
|
||||
if r.score > msg_thresh:
|
||||
text = r.payload.get("text", r.payload.get("content", ""))
|
||||
text = self._extract_message_text(r.payload)
|
||||
# Skip very short or system messages
|
||||
if len(text) > 20 and not text.startswith("<"):
|
||||
if is_doc_query and topic_keywords:
|
||||
@@ -501,7 +502,7 @@ class MemoryRetrieval:
|
||||
seen_texts = set()
|
||||
unique_results = []
|
||||
for r in all_results:
|
||||
text_key = r.get("text", "")[:50].lower()
|
||||
text_key = self._canonical_text_key(r.get("text", ""))
|
||||
if text_key not in seen_texts:
|
||||
seen_texts.add(text_key)
|
||||
unique_results.append(r)
|
||||
@@ -511,6 +512,40 @@ class MemoryRetrieval:
|
||||
except Exception as e:
|
||||
logger.warning(f"Memory search failed for {agent_id}: {e}")
|
||||
return []
|
||||
|
||||
@staticmethod
|
||||
def _extract_message_text(payload: Dict[str, Any]) -> str:
|
||||
"""
|
||||
Normalize text across both payload schemas:
|
||||
- memory-service: content/text (+ role/channel_id)
|
||||
- router: user_message + assistant_response (+ chat_id)
|
||||
"""
|
||||
if not payload:
|
||||
return ""
|
||||
|
||||
text = (payload.get("text") or payload.get("content") or "").strip()
|
||||
if text:
|
||||
lower = text.lower()
|
||||
marker = "\n\nassistant:"
|
||||
idx = lower.rfind(marker)
|
||||
if lower.startswith("user:") and idx != -1:
|
||||
assistant_text = text[idx + len(marker):].strip()
|
||||
if assistant_text:
|
||||
return assistant_text
|
||||
return text
|
||||
|
||||
user_text = (payload.get("user_message") or "").strip()
|
||||
assistant_text = (payload.get("assistant_response") or "").strip()
|
||||
if user_text and assistant_text:
|
||||
return f"User: {user_text}\n\nAssistant: {assistant_text}"
|
||||
return user_text or assistant_text
|
||||
|
||||
@staticmethod
|
||||
def _canonical_text_key(text: str) -> str:
|
||||
if not text:
|
||||
return ""
|
||||
normalized = re.sub(r"\s+", " ", text.strip().lower())
|
||||
return normalized[:220]
|
||||
|
||||
async def get_user_graph_context(
|
||||
self,
|
||||
@@ -619,6 +654,8 @@ class MemoryRetrieval:
|
||||
query=message,
|
||||
agent_id=agent_id,
|
||||
platform_user_id=identity.platform_user_id,
|
||||
chat_id=chat_id,
|
||||
user_id=user_id,
|
||||
limit=5
|
||||
)
|
||||
brief.relevant_memories = memories
|
||||
|
||||
Reference in New Issue
Block a user