File size: 6,996 Bytes
71b378e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
"""
Database utilities for SQLite persistence
Simple key-value storage with JSON serialization
"""

import json
import sqlite3
from pathlib import Path
from typing import Optional, Any, List, Dict
from datetime import datetime

from src.config import config


class Database:
    """Simple SQLite database for campaign data"""

    def __init__(self, db_path: Optional[Path] = None):
        """Initialize database connection"""
        self.db_path = db_path or config.database.db_path
        self.db_path.parent.mkdir(parents=True, exist_ok=True)
        self.conn: Optional[sqlite3.Connection] = None
        self._initialize()

    def _initialize(self):
        """Initialize database and create tables"""
        self.conn = sqlite3.connect(str(self.db_path), check_same_thread=False)
        self.conn.row_factory = sqlite3.Row

        # Create tables
        self._create_tables()

    def _create_tables(self):
        """Create database tables"""
        cursor = self.conn.cursor()

        # Generic key-value store with type
        cursor.execute("""
            CREATE TABLE IF NOT EXISTS entities (
                id TEXT PRIMARY KEY,
                type TEXT NOT NULL,
                data TEXT NOT NULL,
                created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
            )
        """)

        # Index for faster lookups
        cursor.execute("""
            CREATE INDEX IF NOT EXISTS idx_entities_type ON entities(type)
        """)

        # Campaign events for memory
        cursor.execute("""
            CREATE TABLE IF NOT EXISTS campaign_events (
                id TEXT PRIMARY KEY,
                campaign_id TEXT NOT NULL,
                session_number INTEGER NOT NULL,
                event_type TEXT NOT NULL,
                data TEXT NOT NULL,
                importance INTEGER DEFAULT 3,
                timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                FOREIGN KEY (campaign_id) REFERENCES entities(id)
            )
        """)

        cursor.execute("""
            CREATE INDEX IF NOT EXISTS idx_events_campaign ON campaign_events(campaign_id)
        """)

        self.conn.commit()

    def save(self, entity_id: str, entity_type: str, data: Dict[str, Any]):
        """Save entity to database"""
        cursor = self.conn.cursor()

        # Serialize data to JSON
        json_data = json.dumps(data, default=str)

        cursor.execute("""
            INSERT OR REPLACE INTO entities (id, type, data, updated_at)
            VALUES (?, ?, ?, CURRENT_TIMESTAMP)
        """, (entity_id, entity_type, json_data))

        self.conn.commit()

    def load(self, entity_id: str, entity_type: Optional[str] = None) -> Optional[Dict[str, Any]]:
        """Load entity from database"""
        cursor = self.conn.cursor()

        if entity_type:
            cursor.execute("""
                SELECT data FROM entities WHERE id = ? AND type = ?
            """, (entity_id, entity_type))
        else:
            cursor.execute("""
                SELECT data FROM entities WHERE id = ?
            """, (entity_id,))

        row = cursor.fetchone()
        if row:
            return json.loads(row['data'])
        return None

    def load_all(self, entity_type: str) -> List[Dict[str, Any]]:
        """Load all entities of a specific type"""
        cursor = self.conn.cursor()

        cursor.execute("""
            SELECT data FROM entities WHERE type = ?
            ORDER BY updated_at DESC
        """, (entity_type,))

        return [json.loads(row['data']) for row in cursor.fetchall()]

    def delete(self, entity_id: str):
        """Delete entity from database"""
        cursor = self.conn.cursor()

        cursor.execute("""
            DELETE FROM entities WHERE id = ?
        """, (entity_id,))

        self.conn.commit()

    def search(self, entity_type: str, query: str, limit: int = 10) -> List[Dict[str, Any]]:
        """Simple text search in entity data"""
        cursor = self.conn.cursor()

        cursor.execute("""
            SELECT data FROM entities
            WHERE type = ? AND data LIKE ?
            ORDER BY updated_at DESC
            LIMIT ?
        """, (entity_type, f"%{query}%", limit))

        return [json.loads(row['data']) for row in cursor.fetchall()]

    # Campaign Events specific methods
    def save_campaign_event(self, event_data: Dict[str, Any]):
        """Save campaign event for memory"""
        cursor = self.conn.cursor()

        cursor.execute("""
            INSERT INTO campaign_events
            (id, campaign_id, session_number, event_type, data, importance)
            VALUES (?, ?, ?, ?, ?, ?)
        """, (
            event_data['id'],
            event_data['campaign_id'],
            event_data['session_number'],
            event_data['event_type'],
            json.dumps(event_data, default=str),
            event_data.get('importance', 3)
        ))

        self.conn.commit()

    def load_campaign_events(
        self,
        campaign_id: str,
        session_number: Optional[int] = None,
        limit: Optional[int] = None
    ) -> List[Dict[str, Any]]:
        """Load campaign events for memory context"""
        cursor = self.conn.cursor()

        query = """
            SELECT data FROM campaign_events
            WHERE campaign_id = ?
        """
        params = [campaign_id]

        if session_number:
            query += " AND session_number = ?"
            params.append(session_number)

        query += " ORDER BY timestamp DESC"

        if limit:
            query += " LIMIT ?"
            params.append(limit)

        cursor.execute(query, params)

        return [json.loads(row['data']) for row in cursor.fetchall()]

    def get_campaign_context(self, campaign_id: str, max_events: int = 50) -> str:
        """Get formatted campaign context for AI"""
        events = self.load_campaign_events(campaign_id, limit=max_events)

        if not events:
            return "No campaign history yet."

        context_parts = ["# Campaign History\n"]

        for event in reversed(events):  # Chronological order
            context_parts.append(f"## Session {event['session_number']}: {event['title']}")
            context_parts.append(f"**Type:** {event['event_type']}")
            context_parts.append(f"{event['description']}\n")

        return "\n".join(context_parts)

    def close(self):
        """Close database connection"""
        if self.conn:
            self.conn.close()

    def __enter__(self):
        """Context manager entry"""
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        """Context manager exit"""
        self.close()


# Global database instance
_database: Optional[Database] = None


def get_database() -> Database:
    """Get or create global database instance"""
    global _database
    if _database is None:
        _database = Database()
    return _database