-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjson_exporter.py
More file actions
53 lines (45 loc) · 1.76 KB
/
Copy pathjson_exporter.py
File metadata and controls
53 lines (45 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""JSON export format. Dumps everything -- no data loss compared to the raw
JSONL, but in a sane structure with computed stats included."""
import json
from datetime import datetime, timezone
from typing import Any
from models.session import MessageDict, SessionDict, SessionMetadataDict
from models.stats import SessionStatsDict
def session_to_json(
session: SessionDict,
stats: SessionStatsDict | None = None,
indent: int = 2,
) -> str:
"""Serialize a parsed session to a JSON string with schema versioning.
Pass indent=None if you want compact output for piping."""
output = {
"schema_version": "2.0",
"exported_at": datetime.now(timezone.utc).isoformat(),
"session_id": session["session_id"],
"title": session["title"],
"metadata": _serialize_metadata(session["metadata"]),
"stats": stats,
"messages": _serialize_messages(session["messages"]),
}
return json.dumps(output, indent=indent, default=str, ensure_ascii=False)
def _serialize_metadata(meta: SessionMetadataDict) -> dict[str, Any]:
"""json.dumps chokes on sets, so convert them to sorted lists."""
result: dict[str, Any] = {}
for key, val in meta.items():
if isinstance(val, set):
result[key] = sorted(val)
else:
result[key] = val
return result
def _serialize_messages(messages: list[MessageDict]) -> list[dict[str, Any]]:
"""Same set-to-list cleanup, but for each message dict."""
out: list[dict[str, Any]] = []
for msg in messages:
clean: dict[str, Any] = {}
for key, val in msg.items():
if isinstance(val, set):
clean[key] = sorted(val)
else:
clean[key] = val
out.append(clean)
return out