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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
"""
Multi-tenant vector store for SimpleMem MCP Server
Uses LanceDB for vector storage with per-user table isolation
"""

import os
from typing import List, Optional, Dict, Any
from datetime import datetime

import lancedb
import pyarrow as pa

from ..auth.models import MemoryEntry


# LanceDB schema for memory entries
def get_memory_schema(embedding_dimension: int = 2560) -> pa.Schema:
    """Get PyArrow schema for memory entries"""
    return pa.schema([
        pa.field("entry_id", pa.string()),
        pa.field("lossless_restatement", pa.string()),
        pa.field("keywords", pa.list_(pa.string())),
        pa.field("timestamp", pa.string()),
        pa.field("location", pa.string()),
        pa.field("persons", pa.list_(pa.string())),
        pa.field("entities", pa.list_(pa.string())),
        pa.field("topic", pa.string()),
        pa.field("vector", pa.list_(pa.float32(), embedding_dimension)),
        pa.field("created_at", pa.string()),
    ])


class MultiTenantVectorStore:
    """
    Multi-tenant vector storage with per-user table isolation.
    Each user gets their own LanceDB table for complete data isolation.
    """

    def __init__(
        self,
        db_path: str = "./data/lancedb",
        embedding_dimension: int = 2560,
    ):
        self.db_path = db_path
        self.embedding_dimension = embedding_dimension
        os.makedirs(db_path, exist_ok=True)

        # Connect to LanceDB
        self.db = lancedb.connect(db_path)

        # Cache for opened tables
        self._tables: Dict[str, Any] = {}

    def _get_table(self, table_name: str) -> Any:
        """Get or create a user's table"""
        if table_name not in self._tables:
            if table_name in self.db.table_names():
                self._tables[table_name] = self.db.open_table(table_name)
            else:
                # Create new table with schema
                schema = get_memory_schema(self.embedding_dimension)
                self._tables[table_name] = self.db.create_table(
                    table_name,
                    schema=schema,
                )
        return self._tables[table_name]

    async def add_entries(
        self,
        table_name: str,
        entries: List[MemoryEntry],
        embeddings: List[List[float]],
    ) -> int:
        """
        Add memory entries to a user's table

        Args:
            table_name: User's table name
            entries: List of MemoryEntry objects
            embeddings: List of embedding vectors

        Returns:
            Number of entries added
        """
        if len(entries) != len(embeddings):
            raise ValueError("Number of entries must match number of embeddings")

        if not entries:
            return 0

        table = self._get_table(table_name)
        created_at = datetime.utcnow().isoformat()

        # Build records
        records = []
        for entry, embedding in zip(entries, embeddings):
            records.append({
                "entry_id": entry.entry_id,
                "lossless_restatement": entry.lossless_restatement,
                "keywords": entry.keywords or [],
                "timestamp": entry.timestamp or "",
                "location": entry.location or "",
                "persons": entry.persons or [],
                "entities": entry.entities or [],
                "topic": entry.topic or "",
                "vector": embedding,
                "created_at": created_at,
            })

        # Add to table
        table.add(records)
        return len(records)

    async def semantic_search(
        self,
        table_name: str,
        query_embedding: List[float],
        top_k: int = 25,
    ) -> List[MemoryEntry]:
        """
        Perform semantic search using vector similarity

        Args:
            table_name: User's table name
            query_embedding: Query embedding vector
            top_k: Number of results to return

        Returns:
            List of matching MemoryEntry objects
        """
        table = self._get_table(table_name)

        try:
            # Check if table has data
            if table.count_rows() == 0:
                return []

            results = (
                table.search(query_embedding)
                .limit(top_k)
                .to_pandas()
            )

            entries = []
            for _, row in results.iterrows():
                entries.append(MemoryEntry(
                    entry_id=row["entry_id"],
                    lossless_restatement=row["lossless_restatement"],
                    keywords=list(row["keywords"]) if row["keywords"] is not None else [],
                    timestamp=row["timestamp"] if row["timestamp"] else None,
                    location=row["location"] if row["location"] else None,
                    persons=list(row["persons"]) if row["persons"] is not None else [],
                    entities=list(row["entities"]) if row["entities"] is not None else [],
                    topic=row["topic"] if row["topic"] else None,
                ))

            return entries

        except Exception as e:
            print(f"Semantic search error: {e}")
            return []

    async def keyword_search(
        self,
        table_name: str,
        keywords: List[str],
        top_k: int = 5,
    ) -> List[MemoryEntry]:
        """
        Perform keyword-based search (BM25-style matching)

        Args:
            table_name: User's table name
            keywords: List of keywords to match
            top_k: Number of results to return

        Returns:
            List of matching MemoryEntry objects
        """
        table = self._get_table(table_name)

        try:
            if table.count_rows() == 0:
                return []

            # Load all entries for keyword matching
            df = table.to_pandas()

            # Score each entry
            scores = []
            for idx, row in df.iterrows():
                score = 0
                entry_keywords = set(k.lower() for k in (row["keywords"] or []))
                entry_text = row["lossless_restatement"].lower()

                for kw in keywords:
                    kw_lower = kw.lower()
                    # Keyword list match: 2 points
                    if kw_lower in entry_keywords:
                        score += 2
                    # Text match: 1 point
                    if kw_lower in entry_text:
                        score += 1

                scores.append((idx, score))

            # Sort by score and get top-k
            scores.sort(key=lambda x: x[1], reverse=True)
            top_indices = [idx for idx, score in scores[:top_k] if score > 0]

            entries = []
            for idx in top_indices:
                row = df.iloc[idx]
                entries.append(MemoryEntry(
                    entry_id=row["entry_id"],
                    lossless_restatement=row["lossless_restatement"],
                    keywords=list(row["keywords"]) if row["keywords"] is not None else [],
                    timestamp=row["timestamp"] if row["timestamp"] else None,
                    location=row["location"] if row["location"] else None,
                    persons=list(row["persons"]) if row["persons"] is not None else [],
                    entities=list(row["entities"]) if row["entities"] is not None else [],
                    topic=row["topic"] if row["topic"] else None,
                ))

            return entries

        except Exception as e:
            print(f"Keyword search error: {e}")
            return []

    async def structured_search(
        self,
        table_name: str,
        persons: Optional[List[str]] = None,
        location: Optional[str] = None,
        entities: Optional[List[str]] = None,
        timestamp_start: Optional[str] = None,
        timestamp_end: Optional[str] = None,
        top_k: int = 5,
    ) -> List[MemoryEntry]:
        """
        Perform structured/metadata-based search

        Args:
            table_name: User's table name
            persons: Filter by person names
            location: Filter by location
            entities: Filter by entities
            timestamp_start: Start of timestamp range
            timestamp_end: End of timestamp range
            top_k: Number of results to return

        Returns:
            List of matching MemoryEntry objects
        """
        table = self._get_table(table_name)

        try:
            if table.count_rows() == 0:
                return []

            df = table.to_pandas()

            # Apply filters
            mask = [True] * len(df)

            if persons:
                persons_lower = set(p.lower() for p in persons)
                for i, row in df.iterrows():
                    row_persons = set(p.lower() for p in (row["persons"] or []))
                    if not persons_lower.intersection(row_persons):
                        mask[i] = False

            if location:
                location_lower = location.lower()
                for i, row in df.iterrows():
                    if mask[i] and row["location"]:
                        if location_lower not in row["location"].lower():
                            mask[i] = False
                    elif mask[i]:
                        mask[i] = False

            if entities:
                entities_lower = set(e.lower() for e in entities)
                for i, row in df.iterrows():
                    if mask[i]:
                        row_entities = set(e.lower() for e in (row["entities"] or []))
                        if not entities_lower.intersection(row_entities):
                            mask[i] = False

            if timestamp_start:
                for i, row in df.iterrows():
                    if mask[i] and row["timestamp"]:
                        if row["timestamp"] < timestamp_start:
                            mask[i] = False

            if timestamp_end:
                for i, row in df.iterrows():
                    if mask[i] and row["timestamp"]:
                        if row["timestamp"] > timestamp_end:
                            mask[i] = False

            # Get filtered results
            filtered_df = df[[m for m in mask]][:top_k]

            entries = []
            for _, row in filtered_df.iterrows():
                entries.append(MemoryEntry(
                    entry_id=row["entry_id"],
                    lossless_restatement=row["lossless_restatement"],
                    keywords=list(row["keywords"]) if row["keywords"] is not None else [],
                    timestamp=row["timestamp"] if row["timestamp"] else None,
                    location=row["location"] if row["location"] else None,
                    persons=list(row["persons"]) if row["persons"] is not None else [],
                    entities=list(row["entities"]) if row["entities"] is not None else [],
                    topic=row["topic"] if row["topic"] else None,
                ))

            return entries

        except Exception as e:
            print(f"Structured search error: {e}")
            return []

    async def get_all_entries(self, table_name: str) -> List[MemoryEntry]:
        """Get all entries from a user's table"""
        table = self._get_table(table_name)

        try:
            if table.count_rows() == 0:
                return []

            df = table.to_pandas()
            entries = []

            for _, row in df.iterrows():
                entries.append(MemoryEntry(
                    entry_id=row["entry_id"],
                    lossless_restatement=row["lossless_restatement"],
                    keywords=list(row["keywords"]) if row["keywords"] is not None else [],
                    timestamp=row["timestamp"] if row["timestamp"] else None,
                    location=row["location"] if row["location"] else None,
                    persons=list(row["persons"]) if row["persons"] is not None else [],
                    entities=list(row["entities"]) if row["entities"] is not None else [],
                    topic=row["topic"] if row["topic"] else None,
                ))

            return entries

        except Exception as e:
            print(f"Get all entries error: {e}")
            return []

    async def count_entries(self, table_name: str) -> int:
        """Count entries in a user's table"""
        table = self._get_table(table_name)
        try:
            return table.count_rows()
        except Exception:
            return 0

    async def clear_table(self, table_name: str) -> bool:
        """Clear all entries from a user's table"""
        try:
            if table_name in self._tables:
                del self._tables[table_name]

            if table_name in self.db.table_names():
                self.db.drop_table(table_name)

            # Recreate empty table
            self._get_table(table_name)
            return True

        except Exception as e:
            print(f"Clear table error: {e}")
            return False

    async def delete_table(self, table_name: str) -> bool:
        """Completely delete a user's table"""
        try:
            if table_name in self._tables:
                del self._tables[table_name]

            if table_name in self.db.table_names():
                self.db.drop_table(table_name)

            return True

        except Exception as e:
            print(f"Delete table error: {e}")
            return False

    def get_stats(self, table_name: str) -> Dict[str, Any]:
        """Get statistics for a user's table"""
        try:
            table = self._get_table(table_name)
            count = table.count_rows()

            return {
                "table_name": table_name,
                "entry_count": count,
                "embedding_dimension": self.embedding_dimension,
            }
        except Exception as e:
            return {
                "table_name": table_name,
                "entry_count": 0,
                "embedding_dimension": self.embedding_dimension,
                "error": str(e),
            }