-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsessions.py
More file actions
70 lines (58 loc) · 2.91 KB
/
Copy pathsessions.py
File metadata and controls
70 lines (58 loc) · 2.91 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
"""Session detail and stats endpoints."""
import os
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 build_searchable_text, is_excluded_by_rules
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 rules:
meta = session["metadata"]
text_parts = [msg.get("text") or "" for msg in session.get("messages", []) if msg.get("text")]
searchable = build_searchable_text(
project_name=project_name,
session_title=session["title"],
model_names=list(meta.get("models_used") or []),
content_snippet="\n\n".join(text_parts),
)
if is_excluded_by_rules(rules, searchable):
return jsonify({"error": "Session not found"}), 404
return jsonify(session)
except Exception:
# Full traceback (class name, message, stack) goes to the server log
# via logger.exception. The HTTP body returns a stable, generic
# message — never the class name or `e` itself, which would leak
# internal field names, file paths, and user values to any client
# (issue #25).
current_app.logger.exception("Failed to parse session %s", session_id)
return jsonify({"error": "Failed to parse session"}), 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:
# Same pattern as get_session above — full detail to the server log,
# generic message in the HTTP body (issue #25).
current_app.logger.exception("Failed to compute stats for %s", session_id)
return jsonify({"error": "Failed to compute session stats"}), 500