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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
from __future__ import annotations

import json
import sqlite3
import threading
from datetime import datetime
from typing import Any, Dict, List

from chatkit.store import NotFoundError, Store
from chatkit.types import Attachment, Page, Thread, ThreadItem, ThreadMetadata


class SQLiteStore(Store[dict[str, Any]]):
    """Persistent SQLite-backed store compatible with the ChatKit server interface."""

    def __init__(self, db_path: str = "chatkit_threads.db") -> None:
        self.db_path = db_path
        self.conn = sqlite3.connect(db_path, check_same_thread=False)
        self.conn.row_factory = sqlite3.Row
        
        # ๐Ÿš€ Performance optimizations
        self.conn.execute("PRAGMA journal_mode=WAL")           # Better concurrency
        self.conn.execute("PRAGMA synchronous=NORMAL")         # Faster writes
        self.conn.execute("PRAGMA cache_size=10000")           # Larger cache (10MB)
        self.conn.execute("PRAGMA temp_store=MEMORY")          # Temp tables in RAM
        self.conn.execute("PRAGMA mmap_size=268435456")        # 256MB memory-mapped I/O
        self.conn.execute("PRAGMA page_size=4096")             # Optimal page size
        
        self._lock = threading.RLock()  # Thread-safe locking
        self._init_db()

    def _init_db(self) -> None:
        """Initialize database tables."""
        with self._lock:
            cursor = self.conn.cursor()
            
            # Threads table
            cursor.execute("""

                CREATE TABLE IF NOT EXISTS threads (

                    thread_id TEXT PRIMARY KEY,

                    metadata TEXT NOT NULL,

                    created_at TEXT NOT NULL

                )

            """)
            
            # Thread items table with sequence number for guaranteed ordering
            cursor.execute("""

                CREATE TABLE IF NOT EXISTS thread_items (

                    item_id TEXT PRIMARY KEY,

                    thread_id TEXT NOT NULL,

                    item_data TEXT NOT NULL,

                    created_at TEXT NOT NULL,

                    sequence_num INTEGER NOT NULL,

                    FOREIGN KEY (thread_id) REFERENCES threads(thread_id) ON DELETE CASCADE

                )

            """)
            
            # Create optimized indexes for fast retrieval
            cursor.execute("""

                CREATE INDEX IF NOT EXISTS idx_thread_items_thread_id 

                ON thread_items(thread_id)

            """)
            
            # ๐Ÿš€ Composite index for ORDER BY queries - CRITICAL for performance!
            cursor.execute("""

                CREATE INDEX IF NOT EXISTS idx_thread_items_thread_sequence 

                ON thread_items(thread_id, sequence_num, created_at)

            """)
            
            # Index for created_at lookups
            cursor.execute("""

                CREATE INDEX IF NOT EXISTS idx_thread_items_created_at 

                ON thread_items(created_at)

            """)
            
            # Index for item_id lookups (primary key already indexed, but explicit is better)
            cursor.execute("""

                CREATE INDEX IF NOT EXISTS idx_thread_items_item_id 

                ON thread_items(item_id)

            """)
            
            # Analyze tables for query optimizer
            cursor.execute("ANALYZE")
            
            self.conn.commit()

    @staticmethod
    def _coerce_thread_metadata(thread: ThreadMetadata | Thread) -> ThreadMetadata:
        """Return thread metadata without any embedded items (openai-chatkit>=1.0)."""
        has_items = isinstance(thread, Thread) or "items" in getattr(
            thread, "model_fields_set", set()
        )
        if not has_items:
            return thread.model_copy(deep=True)

        data = thread.model_dump()
        data.pop("items", None)
        return ThreadMetadata(**data).model_copy(deep=True)

    # -- Thread metadata -------------------------------------------------
    async def load_thread(self, thread_id: str, context: dict[str, Any]) -> ThreadMetadata:
        with self._lock:
            cursor = self.conn.cursor()
            cursor.execute(
                "SELECT metadata FROM threads WHERE thread_id = ?",
                (thread_id,)
            )
            row = cursor.fetchone()
            if not row:
                raise NotFoundError(f"Thread {thread_id} not found")
            
            metadata_dict = json.loads(row["metadata"])
            return ThreadMetadata(**metadata_dict)

    async def save_thread(self, thread: ThreadMetadata, context: dict[str, Any]) -> None:
        with self._lock:
            metadata = self._coerce_thread_metadata(thread)
            metadata_json = metadata.model_dump_json()
            created_at = (metadata.created_at or datetime.utcnow()).isoformat()
            
            cursor = self.conn.cursor()
            cursor.execute(
                """

                INSERT INTO threads (thread_id, metadata, created_at)

                VALUES (?, ?, ?)

                ON CONFLICT(thread_id) DO UPDATE SET

                    metadata = excluded.metadata

                """,
                (thread.id, metadata_json, created_at)
            )
            self.conn.commit()

    async def load_threads(

        self,

        limit: int,

        after: str | None,

        order: str,

        context: dict[str, Any],

    ) -> Page[ThreadMetadata]:
        with self._lock:
            cursor = self.conn.cursor()
            
            order_clause = "DESC" if order == "desc" else "ASC"
            
            if after:
                # Get the created_at of the 'after' thread
                cursor.execute(
                    "SELECT created_at FROM threads WHERE thread_id = ?",
                    (after,)
                )
                after_row = cursor.fetchone()
                if after_row:
                    after_time = after_row["created_at"]
                    comparison = "<" if order == "desc" else ">"
                    cursor.execute(
                        f"""

                        SELECT metadata FROM threads

                        WHERE created_at {comparison} ?

                        ORDER BY created_at {order_clause}

                        LIMIT ?

                        """,
                        (after_time, limit + 1)
                    )
                else:
                    cursor.execute(
                        f"""

                        SELECT metadata FROM threads

                        ORDER BY created_at {order_clause}

                        LIMIT ?

                        """,
                        (limit + 1,)
                    )
            else:
                cursor.execute(
                    f"""

                    SELECT metadata FROM threads

                    ORDER BY created_at {order_clause}

                    LIMIT ?

                    """,
                    (limit + 1,)
                )
            
            rows = cursor.fetchall()
            threads = [ThreadMetadata(**json.loads(row["metadata"])) for row in rows]
            
            has_more = len(threads) > limit
            threads = threads[:limit]
            next_after = threads[-1].id if has_more and threads else None
            
            return Page(
                data=threads,
                has_more=has_more,
                after=next_after,
            )

    async def delete_thread(self, thread_id: str, context: dict[str, Any]) -> None:
        with self._lock:
            cursor = self.conn.cursor()
            cursor.execute("DELETE FROM threads WHERE thread_id = ?", (thread_id,))
            cursor.execute("DELETE FROM thread_items WHERE thread_id = ?", (thread_id,))
            self.conn.commit()

    # -- Thread items ----------------------------------------------------
    def _get_next_sequence_num(self, thread_id: str) -> int:
        """Get the next sequence number for a thread."""
        cursor = self.conn.cursor()
        cursor.execute(
            "SELECT MAX(sequence_num) as max_seq FROM thread_items WHERE thread_id = ?",
            (thread_id,)
        )
        row = cursor.fetchone()
        max_seq = row["max_seq"] if row and row["max_seq"] is not None else 0
        return max_seq + 1

    async def load_thread_items(

        self,

        thread_id: str,

        after: str | None,

        limit: int,

        order: str,

        context: dict[str, Any],

    ) -> Page[ThreadItem]:
        with self._lock:
            cursor = self.conn.cursor()
            
            # Use sequence_num for reliable ordering, with created_at as secondary
            order_clause = "DESC" if order == "desc" else "ASC"
            
            if after:
                # Get the sequence_num of the 'after' item
                cursor.execute(
                    "SELECT sequence_num FROM thread_items WHERE item_id = ?",
                    (after,)
                )
                after_row = cursor.fetchone()
                if after_row:
                    after_seq = after_row["sequence_num"]
                    comparison = "<" if order == "desc" else ">"
                    cursor.execute(
                        f"""

                        SELECT item_data FROM thread_items

                        WHERE thread_id = ? AND sequence_num {comparison} ?

                        ORDER BY sequence_num {order_clause}, created_at {order_clause}

                        LIMIT ?

                        """,
                        (thread_id, after_seq, limit + 1)
                    )
                else:
                    cursor.execute(
                        f"""

                        SELECT item_data FROM thread_items

                        WHERE thread_id = ?

                        ORDER BY sequence_num {order_clause}, created_at {order_clause}

                        LIMIT ?

                        """,
                        (thread_id, limit + 1)
                    )
            else:
                cursor.execute(
                    f"""

                    SELECT item_data FROM thread_items

                    WHERE thread_id = ?

                    ORDER BY sequence_num {order_clause}, created_at {order_clause}

                    LIMIT ?

                    """,
                    (thread_id, limit + 1)
                )
            
            rows = cursor.fetchall()
            items = []
            for row in rows:
                try:
                    item_dict = json.loads(row["item_data"])
                    item_type = item_dict.get("type")
                    
                    # Import all available message types
                    from chatkit.types import (
                        UserMessageItem,
                        AssistantMessageItem,
                        ClientToolCallItem,
                        WorkflowItem,
                        WidgetItem,
                        TaskItem,
                        HiddenContextItem,
                    )
                    
                    # Reconstruct based on type
                    if item_type == "user_message":
                        items.append(UserMessageItem(**item_dict))
                    elif item_type == "assistant_message":
                        items.append(AssistantMessageItem(**item_dict))
                    elif item_type == "client_tool_call":
                        items.append(ClientToolCallItem(**item_dict))
                    elif item_type == "workflow":
                        items.append(WorkflowItem(**item_dict))
                    elif item_type == "widget":
                        items.append(WidgetItem(**item_dict))
                    elif item_type == "task":
                        items.append(TaskItem(**item_dict))
                    elif item_type == "hidden_context_item":
                        items.append(HiddenContextItem(**item_dict))
                    else:
                        # Unknown type - log but continue
                        print(f"โš ๏ธ Skipping unknown item type: {item_type}")
                        continue
                        
                except (ImportError, TypeError, KeyError, ValueError, Exception) as e:
                    # If reconstruction fails, skip this item
                    print(f"โš ๏ธ Failed to reconstruct item: {e}")
                    continue
            
            has_more = len(items) > limit
            items = items[:limit]
            next_after = items[-1].id if has_more and items else None
            
            return Page(data=items, has_more=has_more, after=next_after)


    async def add_thread_item(

        self, thread_id: str, item: ThreadItem, context: dict[str, Any]

    ) -> None:
        with self._lock:
            # FIX: ChatKit/ChatCompletionsModel produces "__fake_id__" which causes collisions.
            # We enforce a unique ID only for items with actual content to ensure their persistence.
            if item.id == "__fake_id__":
                has_content = False
                # Check if message has meaningful text content
                if hasattr(item, 'content'):
                    for part in item.content:
                        if getattr(part, 'type', '') == 'output_text' and getattr(part, 'text', '').strip():
                            has_content = True
                            break
                
                if has_content:
                    import uuid
                    new_id = f"gen_{uuid.uuid4().hex[:12]}"
                    item = item.model_copy(update={"id": new_id})
                    print(f"๐Ÿ”„ Resolved fake_id (with content) to unique ID: {new_id}")
                else:
                    print(f"โš ๏ธ Keeping __fake_id__ for empty/tool placeholder")

            item_json = item.model_dump_json()
            created_at_val = getattr(item, "created_at", None)
            if created_at_val:
                if isinstance(created_at_val, str):
                    created_at = created_at_val
                else:
                    created_at = created_at_val.isoformat()
            else:
                created_at = datetime.utcnow().isoformat()
            
            cursor = self.conn.cursor()
            
            # Check if item already exists
            cursor.execute(
                "SELECT item_id FROM thread_items WHERE item_id = ?",
                (item.id,)
            )
            existing = cursor.fetchone()
            
            if existing:
                # Update existing item - keep original sequence_num
                cursor.execute(
                    """

                    UPDATE thread_items 

                    SET item_data = ?, created_at = ?

                    WHERE item_id = ?

                    """,
                    (item_json, created_at, item.id)
                )
                print(f"โœ… Updated existing item {item.id}")
            else:
                # Insert new item with next sequence number
                sequence_num = self._get_next_sequence_num(thread_id)
                cursor.execute(
                    """

                    INSERT INTO thread_items (item_id, thread_id, item_data, created_at, sequence_num)

                    VALUES (?, ?, ?, ?, ?)

                    """,
                    (item.id, thread_id, item_json, created_at, sequence_num)
                )
                print(f"โœ… Inserted new item {item.id} with sequence {sequence_num}")
            
            self.conn.commit()
            
            # Verify insertion
            cursor.execute(
                "SELECT COUNT(*) as count FROM thread_items WHERE thread_id = ?",
                (thread_id,)
            )
            count = cursor.fetchone()["count"]
            print(f"๐Ÿ” Total items in thread {thread_id}: {count}")

    async def save_item(self, thread_id: str, item: ThreadItem, context: dict[str, Any]) -> None:
        # Use add_thread_item which handles both insert and update
        await self.add_thread_item(thread_id, item, context)

    async def load_item(self, thread_id: str, item_id: str, context: dict[str, Any]) -> ThreadItem:
        with self._lock:
            cursor = self.conn.cursor()
            cursor.execute(
                "SELECT item_data FROM thread_items WHERE thread_id = ? AND item_id = ?",
                (thread_id, item_id)
            )
            row = cursor.fetchone()
            if not row:
                raise NotFoundError(f"Item {item_id} not found")
            
            item_dict = json.loads(row["item_data"])
            item_type = item_dict.get("type")
            
            try:
                from chatkit.types import (
                    UserMessageItem,
                    AssistantMessageItem,
                    ClientToolCallItem,
                    WorkflowItem,
                    WidgetItem,
                    TaskItem,
                    HiddenContextItem,
                )
                
                # Reconstruct based on type
                if item_type == "user_message":
                    return UserMessageItem(**item_dict)
                elif item_type == "assistant_message":
                    return AssistantMessageItem(**item_dict)
                elif item_type == "client_tool_call":
                    return ClientToolCallItem(**item_dict)
                elif item_type == "workflow":
                    return WorkflowItem(**item_dict)
                elif item_type == "widget":
                    return WidgetItem(**item_dict)
                elif item_type == "task":
                    return TaskItem(**item_dict)
                elif item_type == "hidden_context_item":
                    return HiddenContextItem(**item_dict)
                else:
                    raise NotFoundError(f"Item {item_id} has unknown type: {item_type}")
            except (ImportError, TypeError, KeyError, ValueError) as e:
                raise NotFoundError(f"Failed to load item {item_id}: {e}")

    async def delete_thread_item(

        self, thread_id: str, item_id: str, context: dict[str, Any]

    ) -> None:
        with self._lock:
            cursor = self.conn.cursor()
            cursor.execute(
                "DELETE FROM thread_items WHERE thread_id = ? AND item_id = ?",
                (thread_id, item_id)
            )
            self.conn.commit()

    # -- Files -----------------------------------------------------------
    async def save_attachment(

        self,

        attachment: Attachment,

        context: dict[str, Any],

    ) -> None:
        raise NotImplementedError(
            "SQLiteStore does not persist attachments. Provide a Store implementation "
            "that enforces authentication and authorization before enabling uploads."
        )

    async def load_attachment(

        self,

        attachment_id: str,

        context: dict[str, Any],

    ) -> Attachment:
        raise NotImplementedError(
            "SQLiteStore does not load attachments. Provide a Store implementation "
            "that enforces authentication and authorization before enabling uploads."
        )

    async def delete_attachment(self, attachment_id: str, context: dict[str, Any]) -> None:
        raise NotImplementedError(
            "SQLiteStore does not delete attachments because they are never stored."
        )
    
    # Helper method to get items (used by zendesk integration)
    def _items(self, thread_id: str) -> List[ThreadItem]:
        """Synchronous helper to get all items for a thread (for compatibility with existing code)."""
        with self._lock:
            cursor = self.conn.cursor()
            cursor.execute(
                """

                SELECT item_data FROM thread_items

                WHERE thread_id = ?

                ORDER BY sequence_num ASC, created_at ASC

                """,
                (thread_id,)
            )
            rows = cursor.fetchall()
            items = []
            for row in rows:
                try:
                    item_dict = json.loads(row["item_data"])
                    item_type = item_dict.get("type")
                    
                    from chatkit.types import (
                        UserMessageItem,
                        AssistantMessageItem,
                        ClientToolCallItem,
                        WorkflowItem,
                        WidgetItem,
                        TaskItem,
                        HiddenContextItem,
                    )
                    
                    # Reconstruct based on type
                    if item_type == "user_message":
                        items.append(UserMessageItem(**item_dict))
                    elif item_type == "assistant_message":
                        items.append(AssistantMessageItem(**item_dict))
                    elif item_type == "client_tool_call":
                        items.append(ClientToolCallItem(**item_dict))
                    elif item_type == "workflow":
                        items.append(WorkflowItem(**item_dict))
                    elif item_type == "widget":
                        items.append(WidgetItem(**item_dict))
                    elif item_type == "task":
                        items.append(TaskItem(**item_dict))
                    elif item_type == "hidden_context_item":
                        items.append(HiddenContextItem(**item_dict))
                    else:
                        print(f"โš ๏ธ Skipping unknown item type in _items: {item_type}")
                        continue
                except (ImportError, TypeError, KeyError, ValueError, Exception) as e:
                    print(f"โš ๏ธ Failed to reconstruct item in _items: {e}")
                    continue
            return items

    def close(self):
        """Close the database connection."""
        with self._lock:
            self.conn.close()