Spaces:
Sleeping
Sleeping
Create rag_timeline.py
Browse files- rag_timeline.py +40 -0
rag_timeline.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datetime import datetime
|
| 2 |
+
from rag_data import threads, messages
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
def build_timeline(thread_id: str) -> str:
|
| 6 |
+
"""
|
| 7 |
+
Build a simple markdown timeline for a thread:
|
| 8 |
+
- one line per message
|
| 9 |
+
- sorted by date
|
| 10 |
+
- with [msg: <id>] citations
|
| 11 |
+
"""
|
| 12 |
+
msg_ids = threads.get(thread_id, [])
|
| 13 |
+
if not msg_ids:
|
| 14 |
+
return f"No messages found for thread {thread_id}."
|
| 15 |
+
|
| 16 |
+
entries = []
|
| 17 |
+
for mid in msg_ids:
|
| 18 |
+
m = messages.get(mid)
|
| 19 |
+
if not m:
|
| 20 |
+
continue
|
| 21 |
+
date_str = m.get("date") or ""
|
| 22 |
+
sender = m.get("from") or "(unknown)"
|
| 23 |
+
subject = m.get("subject") or "(no subject)"
|
| 24 |
+
|
| 25 |
+
# Try to format date nicely
|
| 26 |
+
try:
|
| 27 |
+
dt = datetime.fromisoformat(date_str.replace("Z", "+00:00"))
|
| 28 |
+
date_fmt = dt.strftime("%Y-%m-%d %H:%M")
|
| 29 |
+
except Exception:
|
| 30 |
+
date_fmt = date_str
|
| 31 |
+
|
| 32 |
+
line = f"- **{date_fmt}** — **{sender}** — _{subject}_ [msg: {mid}]"
|
| 33 |
+
entries.append((date_str, line))
|
| 34 |
+
|
| 35 |
+
# Sort by raw date string; not perfect but fine for this dataset
|
| 36 |
+
entries.sort(key=lambda x: x[0])
|
| 37 |
+
lines = [f"### Timeline for thread {thread_id}", ""]
|
| 38 |
+
lines.extend(line for _, line in entries)
|
| 39 |
+
|
| 40 |
+
return "\n".join(lines)
|