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

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
memories = []
for entry in results['results']:
# Split timestamp and get date + time up to minutes
timestamp = entry.get('updated_at', '').split('.')[0] # Remove milliseconds
timestamp = entry.get('updated_at', '')
if timestamp:
try:
# Parse and reformat the timestamp
dt = timestamp.replace('T', ' ').split(':') # Split time components
formatted_time = f"{dt[0]}:{dt[1]}:{dt[2]}" # Keep only HH:MM:SS
dt = timestamp.split('.')[0] # Remove milliseconds
formatted_time = dt.replace('T', ' ')
except:
formatted_time = timestamp
memory = entry.get('memory', '')
if timestamp and memory:
memories.append(f"[{formatted_time}] {memory}")
memories_str = "\n".join(f"- {memory}" for memory in memories)
# Store tuple of (timestamp, formatted_string) for sorting
memories.append((timestamp, f"[{formatted_time}] {memory}"))
# 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}")
return memories_str
except Exception as e: