记忆对时间戳排序,便于梳理前后关系

This commit is contained in:
玄凤科技
2025-03-04 13:58:14 +08:00
parent 83911d9ad1
commit 93a94dea45
+11 -7
View File
@@ -49,20 +49,24 @@ class MemoryProvider(MemoryProviderBase):
# Format each memory entry with its update time up to minutes # Format each memory entry with its update time up to minutes
memories = [] memories = []
for entry in results['results']: for entry in results['results']:
# Split timestamp and get date + time up to minutes timestamp = entry.get('updated_at', '')
timestamp = entry.get('updated_at', '').split('.')[0] # Remove milliseconds
if timestamp: if timestamp:
try: try:
# Parse and reformat the timestamp # Parse and reformat the timestamp
dt = timestamp.replace('T', ' ').split(':') # Split time components dt = timestamp.split('.')[0] # Remove milliseconds
formatted_time = f"{dt[0]}:{dt[1]}:{dt[2]}" # Keep only HH:MM:SS formatted_time = dt.replace('T', ' ')
except: except:
formatted_time = timestamp formatted_time = timestamp
memory = entry.get('memory', '') memory = entry.get('memory', '')
if timestamp and memory: if timestamp and memory:
memories.append(f"[{formatted_time}] {memory}") # Store tuple of (timestamp, formatted_string) for sorting
memories.append((timestamp, f"[{formatted_time}] {memory}"))
memories_str = "\n".join(f"- {memory}" for memory in memories)
# Sort by timestamp in descending order (newest first)
memories.sort(key=lambda x: x[0], reverse=True)
# Extract only the formatted strings
memories_str = "\n".join(f"- {memory[1]}" for memory in memories)
logger.bind(tag=TAG).debug(f"Query results: {memories_str}") logger.bind(tag=TAG).debug(f"Query results: {memories_str}")
return memories_str return memories_str
except Exception as e: except Exception as e: