@@ -4923,10 +4923,19 @@ def delete_node_by_prams(
49234923 return deleted_count
49244924
49254925 @timed
4926- def get_user_names_by_memory_ids (self , memory_ids : list [str ]) -> list [str ]:
4927- """Get user names by memory ids."""
4926+ def get_user_names_by_memory_ids (self , memory_ids : list [str ]) -> dict [str , list [str ]]:
4927+ """Get user names by memory ids.
4928+
4929+ Args:
4930+ memory_ids: List of memory node IDs to query.
4931+
4932+ Returns:
4933+ dict[str, list[str]]: Dictionary with one key:
4934+ - 'no_exist_memory_ids': List of memory_ids that do not exist (if any are missing)
4935+ - 'exist_user_names': List of distinct user names (if all memory_ids exist)
4936+ """
49284937 if not memory_ids :
4929- return []
4938+ return { "exist_user_names" : []}
49304939
49314940 # Build OR conditions for each memory_id
49324941 id_conditions = []
@@ -4937,27 +4946,62 @@ def get_user_names_by_memory_ids(self, memory_ids: list[str]) -> list[str]:
49374946
49384947 where_clause = f"({ ' OR ' .join (id_conditions )} )"
49394948
4940- query = f"""
4941- SELECT DISTINCT ag_catalog.agtype_access_operator(properties, '\" user_name\" '::agtype)::text
4949+ # Query to check which memory_ids exist
4950+ check_query = f"""
4951+ SELECT ag_catalog.agtype_access_operator(properties, '\" id\" '::agtype)::text
49424952 FROM "{ self .db_name } _graph"."Memory"
49434953 WHERE { where_clause }
49444954 """
4945- logger .info (f"[get_user_names_by_memory_ids] query: { query } " )
4955+
4956+ logger .info (f"[get_user_names_by_memory_ids] check_query: { check_query } " )
49464957 conn = None
4947- user_names = []
49484958 try :
49494959 conn = self ._get_connection ()
49504960 with conn .cursor () as cursor :
4951- cursor .execute (query )
4961+ # Check which memory_ids exist
4962+ cursor .execute (check_query )
4963+ check_results = cursor .fetchall ()
4964+ existing_ids = set ()
4965+ for row in check_results :
4966+ node_id = row [0 ]
4967+ # Remove quotes if present
4968+ if isinstance (node_id , str ):
4969+ node_id = node_id .strip ('"' ).strip ("'" )
4970+ existing_ids .add (node_id )
4971+
4972+ # Check if any memory_ids are missing
4973+ no_exist_list = [mid for mid in memory_ids if mid not in existing_ids ]
4974+
4975+ # If any memory_ids are missing, return no_exist_memory_ids
4976+ if no_exist_list :
4977+ logger .info (
4978+ f"[get_user_names_by_memory_ids] Found { len (no_exist_list )} non-existing memory_ids: { no_exist_list } "
4979+ )
4980+ return {"no_exist_memory_ids" : no_exist_list }
4981+
4982+ # All memory_ids exist, query user_names
4983+ user_names_query = f"""
4984+ SELECT DISTINCT ag_catalog.agtype_access_operator(properties, '\" user_name\" '::agtype)::text
4985+ FROM "{ self .db_name } _graph"."Memory"
4986+ WHERE { where_clause }
4987+ """
4988+ logger .info (f"[get_user_names_by_memory_ids] user_names_query: { user_names_query } " )
4989+
4990+ cursor .execute (user_names_query )
49524991 results = cursor .fetchall ()
4953- # Extract user_name values and clean them
4992+ user_names = []
49544993 for row in results :
49554994 user_name = row [0 ]
49564995 # Remove quotes if present
49574996 if isinstance (user_name , str ):
49584997 user_name = user_name .strip ('"' ).strip ("'" )
49594998 user_names .append (user_name )
4960- return user_names
4999+
5000+ logger .info (
5001+ f"[get_user_names_by_memory_ids] All memory_ids exist, found { len (user_names )} distinct user_names"
5002+ )
5003+
5004+ return {"exist_user_names" : user_names }
49615005 except Exception as e :
49625006 logger .error (
49635007 f"[get_user_names_by_memory_ids] Failed to get user names: { e } " , exc_info = True
0 commit comments