summaryrefslogtreecommitdiff
path: root/src/agentux/session_store.py
blob: 0269dba9d92a072d06ff28f0a6761399c4eea2cc (plain)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
"""Session persistence and resumption engine for Agentux.

Allows saving and restoring complete conversational and working sessions
across terminal closures, including full message histories, tool state,
working directories, and metadata.
"""

from __future__ import annotations

import json
import os
import time
from typing import Any, Dict, List, Optional
from .models import Message


def _default_session_dir(working_dir: Optional[str] = None) -> str:
    """Session directory preference: project .agentux/sessions if exists, else user config."""
    if working_dir:
        project_dir = os.path.join(working_dir, ".agentux", "sessions")
        if os.path.isdir(os.path.join(working_dir, ".agentux")):
            os.makedirs(project_dir, exist_ok=True)
            return project_dir
    global_dir = os.path.expanduser("~/.config/agentux/sessions")
    os.makedirs(global_dir, exist_ok=True)
    return global_dir


class SessionStore:
    """Saves, lists, and restores Agentux sessions."""

    def __init__(self, directory: Optional[str] = None) -> None:
        self.directory = directory or _default_session_dir()
        os.makedirs(self.directory, exist_ok=True)

    def _file_path(self, session_id: str) -> str:
        safe_id = "".join(c for c in session_id if c.isalnum() or c in ("-", "_"))
        return os.path.join(self.directory, f"{safe_id}.json")

    def save(
        self,
        session_id: str,
        history: List[Message],
        model: str,
        working_dir: str,
        title: Optional[str] = None,
        extra: Optional[Dict[str, Any]] = None,
    ) -> str:
        """Persist a conversation session to disk."""
        path = self._file_path(session_id)
        first_user_prompt = ""
        for m in history:
            if m.role == "user" and m.content:
                first_user_prompt = m.content[:80].strip()
                break

        data: Dict[str, Any] = {
            "id": session_id,
            "title": title or first_user_prompt or f"Session {session_id}",
            "created_at": time.time(),
            "updated_at": time.time(),
            "model": model,
            "working_dir": working_dir,
            "turn_count": len([m for m in history if m.role == "user"]),
            "messages": [m.to_dict() for m in history],
            "extra": extra or {},
        }

        with open(path, "w", encoding="utf-8") as f:
            json.dump(data, f, indent=2)
        return path

    def load(self, session_id: str) -> Optional[Dict[str, Any]]:
        """Load session data by ID or filename."""
        path = self._file_path(session_id)
        if not os.path.isfile(path):
            # Check if session_id is a partial match
            for f in os.listdir(self.directory):
                if f.endswith(".json") and session_id in f:
                    path = os.path.join(self.directory, f)
                    break
        if not os.path.isfile(path):
            return None

        try:
            with open(path, "r", encoding="utf-8") as f:
                data = json.load(f)
            # Reconstruct Message instances
            messages = [Message.from_dict(m) for m in data.get("messages", [])]
            data["messages"] = messages
            return data
        except Exception:
            return None

    def list_sessions(self) -> List[Dict[str, Any]]:
        """List all saved sessions sorted by most recently modified."""
        sessions: List[Dict[str, Any]] = []
        if not os.path.isdir(self.directory):
            return sessions

        for filename in sorted(os.listdir(self.directory), reverse=True):
            if not filename.endswith(".json"):
                continue
            path = os.path.join(self.directory, filename)
            try:
                with open(path, "r", encoding="utf-8") as f:
                    meta = json.load(f)
                sessions.append({
                    "id": meta.get("id", filename[:-5]),
                    "title": meta.get("title", "Untitled"),
                    "model": meta.get("model", ""),
                    "updated_at": meta.get("updated_at", os.path.getmtime(path)),
                    "turn_count": meta.get("turn_count", 0),
                    "working_dir": meta.get("working_dir", ""),
                })
            except Exception:
                continue

        sessions.sort(key=lambda s: s["updated_at"], reverse=True)
        return sessions

    def delete(self, session_id: str) -> bool:
        path = self._file_path(session_id)
        if os.path.isfile(path):
            try:
                os.remove(path)
                return True
            except OSError:
                return False
        return False