-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsessions.py
More file actions
109 lines (94 loc) · 3.45 KB
/
sessions.py
File metadata and controls
109 lines (94 loc) · 3.45 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
"""Session detail and stats endpoints."""
import json
import os
from flask import Blueprint, current_app
from api._flask_types import FlaskReturn, json_response
from api.error_codes import ErrorCode, error_response
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__)
_PARSE_ERRORS = (
json.JSONDecodeError,
KeyError,
ValueError,
OSError,
FileNotFoundError,
)
@sessions_bp.route("/api/sessions/<path:project_name>/<session_id>")
def get_session(project_name: str, session_id: str) -> FlaskReturn:
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 error_response(ErrorCode.INVALID_PATH, "Invalid path", 400)
if not os.path.isfile(filepath):
return error_response(
ErrorCode.SESSION_NOT_FOUND,
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 error_response(
ErrorCode.SESSION_NOT_FOUND,
"Session not found",
404,
)
return json_response(session)
except _PARSE_ERRORS:
current_app.logger.exception("Failed to parse session %s", session_id)
return error_response(
ErrorCode.PARSE_ERROR,
"Failed to parse session",
500,
)
@sessions_bp.route("/api/sessions/<path:project_name>/<session_id>/stats")
def get_session_stats(project_name: str, session_id: str) -> FlaskReturn:
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 error_response(ErrorCode.INVALID_PATH, "Invalid path", 400)
if not os.path.isfile(filepath):
return error_response(
ErrorCode.SESSION_NOT_FOUND,
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 error_response(
ErrorCode.SESSION_NOT_FOUND,
"Session not found",
404,
)
except _PARSE_ERRORS:
current_app.logger.exception("Failed to parse session %s", session_id)
return error_response(
ErrorCode.PARSE_ERROR,
"Failed to parse session",
500,
)
rules = current_app.config.get("EXCLUSION_RULES") or []
if is_session_excluded(rules, session, project_name):
return error_response(
ErrorCode.SESSION_NOT_FOUND,
"Session not found",
404,
)
try:
stats = compute_stats(session)
return json_response(stats)
except _PARSE_ERRORS:
current_app.logger.exception("Failed to compute stats for %s", session_id)
return error_response(
ErrorCode.INTERNAL_ERROR,
"Failed to compute session stats",
500,
)