File size: 8,709 Bytes
24f95f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
"""
SQLite Memory Graph — Persistent memory for Janus.
Stores queries, entities, insights, and connections in SQLite.
"""

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

logger = logging.getLogger(__name__)

try:
    from app.config import DATA_DIR as BASE_DATA_DIR
except ImportError:
    BASE_DATA_DIR = Path(__file__).resolve().parent.parent / "data"

DATA_DIR = Path(BASE_DATA_DIR) / "memory_graph"
DATA_DIR.mkdir(parents=True, exist_ok=True)

DB_PATH = DATA_DIR / "memory_graph.db"


class MemoryGraph:
    def __init__(self, db_path: Path = DB_PATH):
        self.db_path = db_path
        self._init_db()

    def _get_conn(self):
        conn = sqlite3.connect(str(self.db_path))
        conn.row_factory = sqlite3.Row
        return conn

    def _init_db(self):
        """Initialize database schema."""
        conn = self._get_conn()
        cursor = conn.cursor()

        cursor.executescript("""
            CREATE TABLE IF NOT EXISTS queries (
                id TEXT PRIMARY KEY,
                text TEXT NOT NULL,
                type TEXT DEFAULT 'unknown',
                domain TEXT DEFAULT 'general',
                timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
                confidence REAL DEFAULT 0.5,
                response_summary TEXT
            );
            
            CREATE TABLE IF NOT EXISTS entities (
                id TEXT PRIMARY KEY,
                name TEXT NOT NULL,
                type TEXT DEFAULT 'unknown',
                first_seen DATETIME DEFAULT CURRENT_TIMESTAMP,
                mention_count INTEGER DEFAULT 1
            );
            
            CREATE TABLE IF NOT EXISTS insights (
                id TEXT PRIMARY KEY,
                content TEXT NOT NULL,
                source_query TEXT,
                confidence REAL DEFAULT 0.5,
                timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
            );
            
            CREATE TABLE IF NOT EXISTS query_entity (
                query_id TEXT,
                entity_id TEXT,
                relevance REAL DEFAULT 0.5,
                PRIMARY KEY (query_id, entity_id)
            );
            
            CREATE TABLE IF NOT EXISTS query_query (
                source_id TEXT,
                target_id TEXT,
                relationship TEXT DEFAULT 'related',
                strength REAL DEFAULT 0.5,
                PRIMARY KEY (source_id, target_id)
            );
            
            CREATE TABLE IF NOT EXISTS entity_entity (
                source_id TEXT,
                target_id TEXT,
                relationship TEXT DEFAULT 'related',
                strength REAL DEFAULT 0.5,
                PRIMARY KEY (source_id, target_id)
            );
            
            CREATE INDEX IF NOT EXISTS idx_queries_timestamp ON queries(timestamp);
            CREATE INDEX IF NOT EXISTS idx_entities_name ON entities(name);
            CREATE INDEX IF NOT EXISTS idx_insights_timestamp ON insights(timestamp);
        """)

        conn.commit()
        conn.close()

    def add_query(
        self,
        query_id: str,
        text: str,
        type: str = "unknown",
        domain: str = "general",
        confidence: float = 0.5,
        response_summary: str = None,
    ):
        """Record a query."""
        conn = self._get_conn()
        conn.execute(
            """
            INSERT OR REPLACE INTO queries (id, text, type, domain, confidence, response_summary)
            VALUES (?, ?, ?, ?, ?, ?)
        """,
            (query_id, text, type, domain, confidence, response_summary),
        )
        conn.commit()
        conn.close()

    def add_entity(self, entity_id: str, name: str, type: str = "unknown"):
        """Add or update an entity."""
        conn = self._get_conn()
        conn.execute(
            """
            INSERT INTO entities (id, name, type) VALUES (?, ?, ?)
            ON CONFLICT(id) DO UPDATE SET mention_count = mention_count + 1
        """,
            (entity_id, name, type),
        )
        conn.commit()
        conn.close()

    def add_insight(
        self,
        insight_id: str,
        content: str,
        source_query: str = None,
        confidence: float = 0.5,
    ):
        """Add an insight."""
        conn = self._get_conn()
        conn.execute(
            """
            INSERT INTO insights (id, content, source_query, confidence)
            VALUES (?, ?, ?, ?)
        """,
            (insight_id, content, source_query, confidence),
        )
        conn.commit()
        conn.close()

    def link_query_entity(self, query_id: str, entity_id: str, relevance: float = 0.5):
        """Link a query to an entity."""
        conn = self._get_conn()
        conn.execute(
            """
            INSERT OR REPLACE INTO query_entity (query_id, entity_id, relevance)
            VALUES (?, ?, ?)
        """,
            (query_id, entity_id, relevance),
        )
        conn.commit()
        conn.close()

    def link_query_query(
        self,
        source_id: str,
        target_id: str,
        relationship: str = "related",
        strength: float = 0.5,
    ):
        """Link two queries."""
        conn = self._get_conn()
        conn.execute(
            """
            INSERT OR REPLACE INTO query_query (source_id, target_id, relationship, strength)
            VALUES (?, ?, ?, ?)
        """,
            (source_id, target_id, relationship, strength),
        )
        conn.commit()
        conn.close()

    def link_entity_entity(
        self,
        source_id: str,
        target_id: str,
        relationship: str = "related",
        strength: float = 0.5,
    ):
        """Link two entities."""
        conn = self._get_conn()
        conn.execute(
            """
            INSERT OR REPLACE INTO entity_entity (source_id, target_id, relationship, strength)
            VALUES (?, ?, ?, ?)
        """,
            (source_id, target_id, relationship, strength),
        )
        conn.commit()
        conn.close()

    def get_related_queries(self, query_id: str, limit: int = 5) -> List[Dict]:
        """Get queries related to a given query."""
        conn = self._get_conn()
        rows = conn.execute(
            """
            SELECT q.* FROM queries q
            JOIN query_query qq ON q.id = qq.target_id
            WHERE qq.source_id = ?
            ORDER BY qq.strength DESC
            LIMIT ?
        """,
            (query_id, limit),
        ).fetchall()
        conn.close()
        return [dict(r) for r in rows]

    def get_queries_by_domain(self, domain: str, limit: int = 10) -> List[Dict]:
        """Get queries by domain."""
        conn = self._get_conn()
        rows = conn.execute(
            """
            SELECT * FROM queries WHERE domain = ?
            ORDER BY timestamp DESC
            LIMIT ?
        """,
            (domain, limit),
        ).fetchall()
        conn.close()
        return [dict(r) for r in rows]

    def get_entity_connections(self, entity_id: str) -> List[Dict]:
        """Get all connections for an entity."""
        conn = self._get_conn()
        rows = conn.execute(
            """
            SELECT e.name, e.type, ee.relationship, ee.strength
            FROM entities e
            JOIN entity_entity ee ON e.id = ee.target_id
            WHERE ee.source_id = ?
            UNION
            SELECT e.name, e.type, ee.relationship, ee.strength
            FROM entities e
            JOIN entity_entity ee ON e.id = ee.source_id
            WHERE ee.target_id = ?
        """,
            (entity_id, entity_id),
        ).fetchall()
        conn.close()
        return [dict(r) for r in rows]

    def get_stats(self) -> Dict:
        """Get memory graph statistics."""
        conn = self._get_conn()
        query_count = conn.execute("SELECT COUNT(*) FROM queries").fetchone()[0]
        entity_count = conn.execute("SELECT COUNT(*) FROM entities").fetchone()[0]
        insight_count = conn.execute("SELECT COUNT(*) FROM insights").fetchone()[0]
        conn.close()

        return {
            "queries": query_count,
            "entities": entity_count,
            "insights": insight_count,
        }

    def search_queries(self, text: str, limit: int = 10) -> List[Dict]:
        """Search queries by text."""
        conn = self._get_conn()
        rows = conn.execute(
            """
            SELECT * FROM queries WHERE text LIKE ?
            ORDER BY timestamp DESC
            LIMIT ?
        """,
            (f"%{text}%", limit),
        ).fetchall()
        conn.close()
        return [dict(r) for r in rows]