|
| 1 | +"""Chat session management for persistence across restarts.""" |
| 2 | + |
| 3 | +import json |
| 4 | +import uuid |
| 5 | +from dataclasses import dataclass, field, asdict |
| 6 | +from datetime import datetime |
| 7 | +from pathlib import Path |
| 8 | +from typing import Any |
| 9 | + |
| 10 | +from ..llm.base import Message |
| 11 | + |
| 12 | + |
| 13 | +@dataclass |
| 14 | +class ChatSession: |
| 15 | + """Represents a saved chat session.""" |
| 16 | + id: str |
| 17 | + title: str |
| 18 | + created_at: str # ISO format |
| 19 | + last_modified: str # ISO format |
| 20 | + messages: list[Message] = field(default_factory=list) |
| 21 | + is_compacted: bool = False |
| 22 | + |
| 23 | + def to_dict(self) -> dict[str, Any]: |
| 24 | + """Convert session to dictionary for JSON serialization.""" |
| 25 | + return { |
| 26 | + "id": self.id, |
| 27 | + "title": self.title, |
| 28 | + "created_at": self.created_at, |
| 29 | + "last_modified": self.last_modified, |
| 30 | + "is_compacted": self.is_compacted, |
| 31 | + "messages": [ |
| 32 | + {"role": msg.role, "content": msg.content} |
| 33 | + for msg in self.messages |
| 34 | + ] |
| 35 | + } |
| 36 | + |
| 37 | + @classmethod |
| 38 | + def from_dict(cls, data: dict[str, Any]) -> "ChatSession": |
| 39 | + """Create session from dictionary.""" |
| 40 | + messages = [ |
| 41 | + Message(role=m["role"], content=m["content"]) |
| 42 | + for m in data.get("messages", []) |
| 43 | + ] |
| 44 | + return cls( |
| 45 | + id=data["id"], |
| 46 | + title=data.get("title", "Untitled"), |
| 47 | + created_at=data.get("created_at", ""), |
| 48 | + last_modified=data.get("last_modified", ""), |
| 49 | + messages=messages, |
| 50 | + is_compacted=data.get("is_compacted", False) |
| 51 | + ) |
| 52 | + |
| 53 | + |
| 54 | +class SessionManager: |
| 55 | + """Manages chat session persistence. |
| 56 | + |
| 57 | + Sessions are stored as JSON files in .supercoder/sessions/ directory. |
| 58 | + Maximum of 10 sessions are kept; oldest sessions are automatically deleted. |
| 59 | + """ |
| 60 | + |
| 61 | + MAX_SESSIONS = 10 |
| 62 | + SESSIONS_DIR = "sessions" |
| 63 | + |
| 64 | + def __init__(self, project_root: Path): |
| 65 | + self.project_root = Path(project_root) |
| 66 | + self.sessions_dir = self.project_root / ".supercoder" / self.SESSIONS_DIR |
| 67 | + self._ensure_sessions_dir() |
| 68 | + |
| 69 | + def _ensure_sessions_dir(self) -> None: |
| 70 | + """Create sessions directory if it doesn't exist.""" |
| 71 | + self.sessions_dir.mkdir(parents=True, exist_ok=True) |
| 72 | + |
| 73 | + def _get_session_path(self, session_id: str) -> Path: |
| 74 | + """Get path to session file.""" |
| 75 | + return self.sessions_dir / f"{session_id}.json" |
| 76 | + |
| 77 | + def create_new_session(self) -> ChatSession: |
| 78 | + """Create a new empty session.""" |
| 79 | + now = datetime.now().isoformat() |
| 80 | + session = ChatSession( |
| 81 | + id=str(uuid.uuid4())[:8], # Short UUID for readability |
| 82 | + title="New Session", |
| 83 | + created_at=now, |
| 84 | + last_modified=now, |
| 85 | + messages=[], |
| 86 | + is_compacted=False |
| 87 | + ) |
| 88 | + return session |
| 89 | + |
| 90 | + def save_session(self, session: ChatSession) -> None: |
| 91 | + """Save session to JSON file. |
| 92 | + |
| 93 | + Also triggers cleanup if MAX_SESSIONS is exceeded. |
| 94 | + """ |
| 95 | + session.last_modified = datetime.now().isoformat() |
| 96 | + |
| 97 | + # Update title from last user message |
| 98 | + user_messages = [m for m in session.messages if m.role == "user" and not m.content.startswith("<@TOOL_RESULT>")] |
| 99 | + if user_messages: |
| 100 | + last_msg = user_messages[-1].content |
| 101 | + # Truncate and clean title |
| 102 | + session.title = (last_msg[:50] + "...") if len(last_msg) > 50 else last_msg |
| 103 | + session.title = session.title.replace("\n", " ").strip() |
| 104 | + |
| 105 | + # Save to file |
| 106 | + session_path = self._get_session_path(session.id) |
| 107 | + with open(session_path, "w", encoding="utf-8") as f: |
| 108 | + json.dump(session.to_dict(), f, ensure_ascii=False, indent=2) |
| 109 | + |
| 110 | + # Cleanup old sessions |
| 111 | + self._cleanup_old_sessions() |
| 112 | + |
| 113 | + def load_session(self, session_id: str) -> ChatSession | None: |
| 114 | + """Load session from JSON file.""" |
| 115 | + session_path = self._get_session_path(session_id) |
| 116 | + |
| 117 | + if not session_path.exists(): |
| 118 | + return None |
| 119 | + |
| 120 | + try: |
| 121 | + with open(session_path, "r", encoding="utf-8") as f: |
| 122 | + data = json.load(f) |
| 123 | + return ChatSession.from_dict(data) |
| 124 | + except (json.JSONDecodeError, KeyError) as e: |
| 125 | + # Corrupted session file |
| 126 | + return None |
| 127 | + |
| 128 | + def list_sessions(self) -> list[dict[str, Any]]: |
| 129 | + """List all available sessions with metadata. |
| 130 | + |
| 131 | + Returns list of dicts with id, title, last_modified, is_compacted. |
| 132 | + Sorted by last_modified (newest first). |
| 133 | + """ |
| 134 | + sessions = [] |
| 135 | + |
| 136 | + for session_file in self.sessions_dir.glob("*.json"): |
| 137 | + try: |
| 138 | + with open(session_file, "r", encoding="utf-8") as f: |
| 139 | + data = json.load(f) |
| 140 | + sessions.append({ |
| 141 | + "id": data.get("id", session_file.stem), |
| 142 | + "title": data.get("title", "Untitled"), |
| 143 | + "created_at": data.get("created_at", ""), |
| 144 | + "last_modified": data.get("last_modified", ""), |
| 145 | + "is_compacted": data.get("is_compacted", False), |
| 146 | + "message_count": len(data.get("messages", [])) |
| 147 | + }) |
| 148 | + except (json.JSONDecodeError, KeyError): |
| 149 | + # Skip corrupted files |
| 150 | + continue |
| 151 | + |
| 152 | + # Sort by last_modified (newest first) |
| 153 | + sessions.sort(key=lambda s: s.get("last_modified", ""), reverse=True) |
| 154 | + |
| 155 | + return sessions |
| 156 | + |
| 157 | + def delete_session(self, session_id: str) -> bool: |
| 158 | + """Delete a session file.""" |
| 159 | + session_path = self._get_session_path(session_id) |
| 160 | + |
| 161 | + if session_path.exists(): |
| 162 | + session_path.unlink() |
| 163 | + return True |
| 164 | + return False |
| 165 | + |
| 166 | + def update_session_after_compact(self, session: ChatSession, summary: str) -> None: |
| 167 | + """Update session after context compaction. |
| 168 | + |
| 169 | + Replaces all messages with the summary and marks as compacted. |
| 170 | + """ |
| 171 | + session.is_compacted = True |
| 172 | + session.messages = [Message("user", f"[Previous Context Summary]\\n\\n{summary}")] |
| 173 | + session.last_modified = datetime.now().isoformat() |
| 174 | + |
| 175 | + # Save updated session |
| 176 | + session_path = self._get_session_path(session.id) |
| 177 | + with open(session_path, "w", encoding="utf-8") as f: |
| 178 | + json.dump(session.to_dict(), f, ensure_ascii=False, indent=2) |
| 179 | + |
| 180 | + def _cleanup_old_sessions(self) -> None: |
| 181 | + """Remove oldest sessions if we exceed MAX_SESSIONS.""" |
| 182 | + sessions = self.list_sessions() |
| 183 | + |
| 184 | + if len(sessions) <= self.MAX_SESSIONS: |
| 185 | + return |
| 186 | + |
| 187 | + # Sessions are sorted newest first, so remove from the end |
| 188 | + sessions_to_delete = sessions[self.MAX_SESSIONS:] |
| 189 | + |
| 190 | + for session in sessions_to_delete: |
| 191 | + self.delete_session(session["id"]) |
0 commit comments