-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_session_handlers.py
More file actions
94 lines (79 loc) · 2.78 KB
/
Copy path_session_handlers.py
File metadata and controls
94 lines (79 loc) · 2.78 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
"""Shared resolve/load/exclude/error helpers for session API handlers."""
from __future__ import annotations
import os
from collections.abc import Callable
from dataclasses import dataclass
from flask import current_app
from api._flask_types import FlaskReturn
from api.error_codes import ErrorCode, error_response
from models.session import SessionDict
from models.stats import SessionStatsDict
from utils.exclusion_rules import is_session_excluded
from utils.session_cache import get_cached_session
from utils.session_errors import SESSION_LOAD_ERRORS
from utils.session_path import get_claude_projects_dir, safe_join
from utils.session_stats import compute_stats
__all__ = [
"SESSION_LOAD_ERRORS",
"LoadedSession",
"resolve_loaded_session",
"compute_stats_or_error",
]
@dataclass(frozen=True)
class LoadedSession:
session: SessionDict
filepath: str
def resolve_loaded_session(
project_name: str,
session_id: str,
*,
missing_file_message: str | Callable[[str], str],
parse_log_action: str = "Failed to parse session %s",
) -> LoadedSession | FlaskReturn:
"""Resolve path, load session, and apply exclusion rules.
Returns ``LoadedSession`` on success or an ``error_response`` tuple/Response.
"""
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):
msg = (
missing_file_message(session_id)
if callable(missing_file_message)
else missing_file_message
)
return error_response(ErrorCode.SESSION_NOT_FOUND, msg, 404)
try:
session = get_cached_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 SESSION_LOAD_ERRORS:
current_app.logger.exception(parse_log_action, session_id)
return error_response(
ErrorCode.PARSE_ERROR,
"Failed to parse session",
500,
)
return LoadedSession(session=session, filepath=filepath)
def compute_stats_or_error(
session: SessionDict,
session_id: str,
*,
log_action: str,
) -> SessionStatsDict | FlaskReturn:
try:
return compute_stats(session)
except SESSION_LOAD_ERRORS:
current_app.logger.exception(log_action, session_id)
return error_response(
ErrorCode.INTERNAL_ERROR,
"Failed to compute session stats",
500,
)