-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsessions.py
More file actions
61 lines (49 loc) · 2.19 KB
/
Copy pathsessions.py
File metadata and controls
61 lines (49 loc) · 2.19 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
"""Session detail and stats endpoints."""
import os
import traceback
from flask import Blueprint, current_app, jsonify, abort
from utils.session_path import get_claude_projects_dir, safe_join
from utils.jsonl_parser import parse_session
from utils.session_stats import compute_stats
from utils.exclusion_rules import is_session_excluded
sessions_bp = Blueprint("sessions", __name__)
@sessions_bp.route("/api/sessions/<path:project_name>/<session_id>")
def get_session(project_name, session_id):
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 jsonify({"error": "Invalid path"}), 400
if not os.path.isfile(filepath):
return jsonify({"error": f"Session {session_id} not found"}), 404
try:
session = parse_session(filepath)
rules = current_app.config.get("EXCLUSION_RULES") or []
if is_session_excluded(rules, session, project_name):
return jsonify({"error": "Session not found"}), 404
return jsonify(session)
except Exception as e:
tb = traceback.format_exc()
print(f"[ERROR] Failed to parse session {session_id}: {e}\n{tb}")
return jsonify({
"error": f"Failed to parse session: {type(e).__name__}: {e}",
}), 500
@sessions_bp.route("/api/sessions/<path:project_name>/<session_id>/stats")
def get_session_stats(project_name, session_id):
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 jsonify({"error": "Invalid path"}), 400
if not os.path.isfile(filepath):
return jsonify({"error": f"Session {session_id} not found"}), 404
try:
session = parse_session(filepath)
stats = compute_stats(session)
return jsonify(stats)
except Exception as e:
tb = traceback.format_exc()
print(f"[ERROR] Failed to compute stats for {session_id}: {e}\n{tb}")
return jsonify({
"error": f"Failed to compute stats: {type(e).__name__}: {e}",
}), 500