feat(memory): add session-based chat history and user metadata retrieval

- Add ChatSessionCache to manage chat history per session
- Add SEARCH_USER_METADATA cypher query for retrieving user entity metadata
- Add "str" mode support to StructResponse for raw text extraction
- Add content_str field to MemorySearchResult for pre-formatted content
- Fix sandbox URL by removing hardcoded port
- Add description field to entity search results
- Remove history from UserInput schema, use session_id instead
This commit is contained in:
Eternity
2026-05-06 16:24:53 +08:00
parent 6b68ee9fc8
commit 3f9740412a
19 changed files with 387 additions and 117 deletions

View File

@@ -1296,6 +1296,7 @@ RETURN e.id AS id,
e.name AS name,
e.end_user_id AS end_user_id,
e.entity_type AS entity_type,
e.description AS description,
COALESCE(e.activation_value, e.importance_score, 0.5) AS activation_value,
COALESCE(e.importance_score, 0.5) AS importance_score,
e.last_access_time AS last_access_time,
@@ -1479,6 +1480,21 @@ ORDER BY score DESC
LIMIT $limit
"""
SEARCH_USER_METADATA = """
MATCH (n:ExtractedEntity)
WHERE (n.end_user_id = $end_user_id AND n.entity_type ='用户')
RETURN n.description AS description,
n.aliases AS aliases,
n.anchors AS anchors,
n.beliefs_or_stances AS beliefs_or_stances,
n.core_facts AS core_facts,
n.events AS events,
n.goals AS goals,
n.interests AS interests,
n.relations AS relations,
n.traits AS traits
"""
FULLTEXT_QUERY_CYPHER_MAPPING = {
Neo4jNodeType.STATEMENT: SEARCH_STATEMENTS_BY_KEYWORD,
Neo4jNodeType.EXTRACTEDENTITY: SEARCH_ENTITIES_BY_NAME_OR_ALIAS,

View File

@@ -27,9 +27,9 @@ from app.repositories.neo4j.cypher_queries import (
SEARCH_PERCEPTUAL_BY_USER_ID,
FULLTEXT_QUERY_CYPHER_MAPPING,
USER_ID_QUERY_CYPHER_MAPPING,
NODE_ID_QUERY_CYPHER_MAPPING
NODE_ID_QUERY_CYPHER_MAPPING,
SEARCH_USER_METADATA
)
from app.repositories.neo4j.neo4j_connector import Neo4jConnector
logger = logging.getLogger(__name__)
@@ -513,7 +513,7 @@ async def search_graph_by_embedding(
task_keys = []
for node_type in include:
tasks.append(search_by_embedding(connector, node_type, end_user_id, embedding, limit*2))
tasks.append(search_by_embedding(connector, node_type, end_user_id, embedding, limit * 2))
task_keys.append(node_type.value)
task_results = await asyncio.gather(*tasks, return_exceptions=True)
@@ -557,6 +557,17 @@ async def search_graph_by_embedding(
return results
async def search_user_metadata(
connector: Neo4jConnector,
end_user_id: str
) -> dict:
user_info = await connector.execute_query(
SEARCH_USER_METADATA,
end_user_id=end_user_id
)
return user_info[0] if user_info else {}
async def get_dedup_candidates_for_entities( # 适配新版查询:使用全文索引按名称检索候选实体
connector: Neo4jConnector,
end_user_id: str,