-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexport_api.py
More file actions
304 lines (266 loc) · 10.9 KB
/
Copy pathexport_api.py
File metadata and controls
304 lines (266 loc) · 10.9 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
"""Export endpoints -- bulk zip download and single-session md/json."""
import io
import json
import os
import zipfile
from datetime import datetime
from typing import Any
from flask import Blueprint, current_app, request, send_file
from api._flask_types import FlaskReturn, json_error, json_response
from models.export import ExportStateDict
from utils.export_state_store import (
EXPORT_STATE_FILE,
atomic_write_export_state,
export_state_lock,
load_export_state_from_disk,
)
from utils.session_path import (
get_claude_projects_dir,
list_projects,
list_sessions,
)
from utils.jsonl_parser import parse_session
from utils.session_stats import compute_stats
from utils.md_exporter import session_to_markdown
from utils.json_exporter import session_to_json
from utils.exclusion_rules import is_session_excluded
from utils.slugify import slugify
from utils.export_day_filter import collect_sessions_for_latest_activity_day
export_bp = Blueprint("export", __name__)
# Tests monkeypatch this path; keep in sync with utils.export_state_store.
_STATE_FILE = EXPORT_STATE_FILE
def _state_lock() -> Any:
return export_state_lock(_STATE_FILE)
def _load_state_from_disk() -> ExportStateDict:
return load_export_state_from_disk(_STATE_FILE)
def _atomic_write_state(state: ExportStateDict) -> None:
atomic_write_export_state(state, _STATE_FILE)
def _read_state() -> ExportStateDict:
with _state_lock():
return _load_state_from_disk()
def _write_state(sessions_map: dict[str, float], count: int) -> None:
"""Persist merge of *sessions_map* and update last-export metadata (*count* = this run only)."""
with _state_lock():
state = _load_state_from_disk()
state["lastExportTime"] = datetime.now().isoformat()
state["exportedCount"] = count
state.setdefault("sessions", {}).update(sessions_map)
_atomic_write_state(state)
@export_bp.route("/api/export/state")
def get_export_state() -> FlaskReturn:
state = _read_state()
n = state.get("exportedCount", 0)
return json_response(
{
"last_export_time": state.get("lastExportTime"),
# Sessions exported in the last completed bulk export (not a lifetime total).
"last_export_session_count": n,
"export_count": n,
}
)
@export_bp.route("/api/export", methods=["POST"])
def bulk_export() -> FlaskReturn:
body = request.get_json(silent=True)
if body is None:
body = {}
if not isinstance(body, dict):
return json_error("Invalid request body", 400)
since = body.get("since", "all")
if since not in ("all", "last", "incremental"):
return json_error({"error": "Invalid since mode", "since": since}, 400)
base = (
current_app.config.get("CLAUDE_PROJECTS_DIR")
or get_claude_projects_dir()
)
projects = list_projects(base)
rules = current_app.config.get("EXCLUSION_RULES") or []
state = _read_state()
last_export_sessions: dict[str, float] = (
state.get("sessions", {}) if since == "incremental" else {}
)
buf = io.BytesIO()
count = 0
manifest: list[dict[str, Any]] = []
new_sessions_map: dict[str, float] = {}
latest_day = None
with zipfile.ZipFile(buf, "w", zipfile.ZIP_DEFLATED) as zf:
if since == "last":
d, rows, _n = collect_sessions_for_latest_activity_day(
projects,
list_sessions=list_sessions,
parse_session=parse_session,
is_session_excluded=is_session_excluded,
rules=rules,
)
latest_day = d
for project, sess_info, session, _st, _en in rows:
sid = sess_info["id"]
try:
stats = compute_stats(session)
md = session_to_markdown(session, stats)
title_slug = slugify(session["title"], default="session")
short_id = sid[:8]
proj_slug = slugify(project["name"], default="project")
ts = session["metadata"].get("first_timestamp", "")
ts_file = (
ts[:19].replace(":", "-")
if ts
else "0000-00-00T00-00-00"
)
rel_path = (
f"{proj_slug}/{ts_file}__{title_slug}__{short_id}.md"
)
zf.writestr(rel_path, md)
manifest.append(
{
"session_id": sid,
"title": session["title"],
"project": project["name"],
"tokens": session["metadata"]["total_input_tokens"]
+ session["metadata"]["total_output_tokens"],
"tool_calls": session["metadata"][
"total_tool_calls"
],
"cost_estimate_usd": stats.get(
"cost_estimate_usd"
),
}
)
new_sessions_map[sid] = sess_info.get("modified", 0)
count += 1
except Exception as e:
current_app.logger.warning(
"Failed to export %s: %s", sid[:10], e
)
continue
else:
for project in projects:
sessions = list_sessions(project["path"])
for sess_info in sessions:
sid = sess_info["id"]
try:
if since == "incremental":
prev_mtime = last_export_sessions.get(sid, 0)
curr_mtime = sess_info.get("modified", 0)
if curr_mtime and curr_mtime <= prev_mtime:
continue
session = parse_session(sess_info["path"])
if session["title"] == "Untitled Session":
continue
if is_session_excluded(
rules,
session,
project.get("display_name") or project["name"],
):
continue
stats = compute_stats(session)
md = session_to_markdown(session, stats)
title_slug = slugify(
session["title"], default="session"
)
short_id = sid[:8]
proj_slug = slugify(project["name"], default="project")
ts = session["metadata"].get("first_timestamp", "")
ts_file = (
ts[:19].replace(":", "-")
if ts
else "0000-00-00T00-00-00"
)
rel_path = f"{proj_slug}/{ts_file}__{title_slug}__{short_id}.md"
zf.writestr(rel_path, md)
manifest.append(
{
"session_id": sid,
"title": session["title"],
"project": project["name"],
"tokens": session["metadata"][
"total_input_tokens"
]
+ session["metadata"]["total_output_tokens"],
"tool_calls": session["metadata"][
"total_tool_calls"
],
"cost_estimate_usd": stats.get(
"cost_estimate_usd"
),
}
)
new_sessions_map[sid] = sess_info.get("modified", 0)
count += 1
except Exception as e:
current_app.logger.warning(
"Failed to export %s: %s", sid[:10], e
)
continue
if manifest:
manifest_str = "\n".join(
json.dumps(e, default=str) for e in manifest
)
zf.writestr("manifest.jsonl", manifest_str)
if count > 0:
_write_state(new_sessions_map, count)
if count == 0:
return json_error({"error": "Nothing to export", "since": since}, 422)
buf.seek(0)
date_tag = datetime.now().strftime("%Y-%m-%d")
if since == "last":
if latest_day is not None:
suffix = f"-last-{latest_day.strftime('%m-%d')}"
else:
suffix = "-last"
elif since == "incremental":
suffix = "-incremental"
else:
suffix = ""
return send_file(
buf,
mimetype="application/zip",
as_attachment=True,
download_name=f"claude-code-export{suffix}-{date_tag}.zip", # type: ignore[call-arg]
)
@export_bp.route("/api/export/session/<path:project_name>/<session_id>")
def export_session(project_name: str, session_id: str) -> FlaskReturn:
import os
from utils.session_path import safe_join
base = (
current_app.config.get("CLAUDE_PROJECTS_DIR")
or get_claude_projects_dir()
)
try:
filepath = safe_join(base, project_name, f"{session_id}.jsonl")
except ValueError:
return json_error("Invalid path", 400)
if not os.path.isfile(filepath):
return json_error("Session not found", 404)
fmt = request.args.get("format", "md")
try:
session = parse_session(filepath)
rules = current_app.config.get("EXCLUSION_RULES") or []
if is_session_excluded(rules, session, project_name):
return json_error("Session not found", 404)
stats = compute_stats(session)
title_slug = slugify(session["title"], default="session")
if fmt == "json":
content = session_to_json(session, stats)
buf = io.BytesIO(content.encode("utf-8"))
buf.seek(0)
return send_file(
buf,
mimetype="application/json",
as_attachment=True,
download_name=f"{title_slug}.json", # type: ignore[call-arg]
)
md = session_to_markdown(session, stats)
buf = io.BytesIO(md.encode("utf-8"))
buf.seek(0)
return send_file(
buf,
mimetype="text/markdown",
as_attachment=True,
download_name=f"{title_slug}.md", # type: ignore[call-arg]
)
except Exception:
current_app.logger.exception(
"Failed to export session %s/%s", project_name, session_id
)
return json_error("Internal server error exporting session", 500)