refactor(memory): streamline memory extraction by utilizing extract_text_content utility

This commit is contained in:
Anish Sarkar 2026-05-02 16:10:30 +05:30
parent 451a98936e
commit 9975e085aa
5 changed files with 106 additions and 25 deletions

View file

@ -16,6 +16,7 @@ from sqlalchemy import select
from app.agents.new_chat.tools.update_memory import _save_memory
from app.db import SearchSpace, User, shielded_async_session
from app.utils.content_utils import extract_text_content
logger = logging.getLogger(__name__)
@ -144,11 +145,7 @@ async def extract_and_save_memory(
[HumanMessage(content=prompt)],
config={"tags": ["surfsense:internal", "memory-extraction"]},
)
text = (
response.content
if isinstance(response.content, str)
else str(response.content)
).strip()
text = extract_text_content(response.content).strip()
if text == "NO_UPDATE" or not text:
logger.debug("Memory extraction: no update needed (user %s)", uid)
@ -207,11 +204,7 @@ async def extract_and_save_team_memory(
[HumanMessage(content=prompt)],
config={"tags": ["surfsense:internal", "team-memory-extraction"]},
)
text = (
response.content
if isinstance(response.content, str)
else str(response.content)
).strip()
text = extract_text_content(response.content).strip()
if text == "NO_UPDATE" or not text:
logger.debug(

View file

@ -27,6 +27,7 @@ from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from app.db import SearchSpace, User
from app.utils.content_utils import extract_text_content
logger = logging.getLogger(__name__)
@ -188,11 +189,7 @@ async def _forced_rewrite(content: str, llm: Any) -> str | None:
[HumanMessage(content=prompt)],
config={"tags": ["surfsense:internal"]},
)
text = (
response.content
if isinstance(response.content, str)
else str(response.content)
)
text = extract_text_content(response.content)
return text.strip()
except Exception:
logger.exception("Forced rewrite LLM call failed")
@ -235,6 +232,16 @@ async def _save_memory(
label : str
Human label for log messages (e.g. "user memory", "team memory").
"""
if not isinstance(updated_memory, str):
logger.warning(
"Refusing non-string memory payload (type=%s)",
type(updated_memory).__name__,
)
return {
"status": "error",
"message": "Internal error: memory payload must be a string.",
}
content = updated_memory
# --- forced rewrite if over the hard limit ---