|
| 1 | +import sqlite3 |
| 2 | +import fcntl |
| 3 | +import os |
| 4 | +import sys |
| 5 | + |
| 6 | +def fullpath(file): |
| 7 | + xdg_root = os.getenv("XDG_ROOT") |
| 8 | + if xdg_root: |
| 9 | + return os.path.join(xdg_root, file) |
| 10 | + else: |
| 11 | + home_dir = os.path.expanduser("~") |
| 12 | + return os.path.join(home_dir, file) |
| 13 | + |
| 14 | +lock_fd = None |
| 15 | + |
| 16 | +def check_single_instance(lock_file): |
| 17 | + global lock_fd |
| 18 | + |
| 19 | + try: |
| 20 | + lock_fd = os.open(lock_file, os.O_CREAT | os.O_TRUNC | os.O_EXLOCK | os.O_NONBLOCK) |
| 21 | + except BlockingIOError: |
| 22 | + # Another instance is already running, so terminate |
| 23 | + print("Another instance is already running. Exiting.") |
| 24 | + sys.exit(1) |
| 25 | + |
| 26 | +class ChatDB: |
| 27 | + def __init__(self, db_file=".chatgpt.db"): |
| 28 | + db_path = fullpath(db_file) |
| 29 | + check_single_instance(db_path + ".lock") |
| 30 | + self.conn = sqlite3.connect(db_path) |
| 31 | + self.create_schema() |
| 32 | + |
| 33 | + def create_schema(self): |
| 34 | + query = """ |
| 35 | + CREATE TABLE IF NOT EXISTS chats ( |
| 36 | + id INTEGER PRIMARY KEY AUTOINCREMENT, |
| 37 | + name TEXT, |
| 38 | + last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP |
| 39 | + ) |
| 40 | + """ |
| 41 | + self.conn.execute(query) |
| 42 | + |
| 43 | + query = """ |
| 44 | + CREATE TABLE IF NOT EXISTS messages ( |
| 45 | + id INTEGER PRIMARY KEY AUTOINCREMENT, |
| 46 | + chat_id INTEGER, |
| 47 | + role TEXT, |
| 48 | + content TEXT, |
| 49 | + time TIMESTAMP DEFAULT CURRENT_TIMESTAMP, |
| 50 | + deleted INTEGER DEFAULT 0, |
| 51 | + FOREIGN KEY (chat_id) REFERENCES chats (id) |
| 52 | + ) |
| 53 | + """ |
| 54 | + self.conn.execute(query) |
| 55 | + |
| 56 | + query = """ |
| 57 | + CREATE VIRTUAL TABLE IF NOT EXISTS messages_fts USING FTS5 ( |
| 58 | + message_id UNINDEXED, |
| 59 | + content, |
| 60 | + content_rowid, |
| 61 | + ) |
| 62 | + """ |
| 63 | + self.conn.execute(query) |
| 64 | + |
| 65 | + def create_chat(self, name=None): |
| 66 | + cursor = self.conn.cursor() # Create a cursor |
| 67 | + query = "INSERT INTO chats (name) VALUES (?)" |
| 68 | + cursor.execute(query, (name,)) |
| 69 | + chat_id = cursor.lastrowid # Access lastrowid from the cursor |
| 70 | + self.conn.commit() |
| 71 | + return chat_id |
| 72 | + |
| 73 | + def add_message(self, chat_id: int, role: str, content: str): |
| 74 | + cursor = self.conn.cursor() # Create a cursor |
| 75 | + query = "INSERT INTO messages (chat_id, role, content) VALUES (?, ?, ?)" |
| 76 | + cursor.execute(query, (chat_id, role, content)) |
| 77 | + self.conn.commit() |
| 78 | + last_message_id = cursor.lastrowid |
| 79 | + |
| 80 | + fts_query = "INSERT INTO messages_fts (message_id, content) VALUES (?, ?)" |
| 81 | + self.conn.execute(fts_query, (last_message_id, content.lower())) |
| 82 | + self.conn.commit() |
| 83 | + |
| 84 | + query = f"UPDATE chats SET last_update = CURRENT_TIMESTAMP WHERE id = ?" |
| 85 | + cursor.execute(query, (chat_id, )) |
| 86 | + |
| 87 | + return last_message_id |
| 88 | + |
| 89 | + def num_messages(self, chat_id: int) -> int: |
| 90 | + query = "SELECT COUNT(*) FROM messages WHERE chat_id = ?" |
| 91 | + result = self.conn.execute(query, (chat_id,)).fetchone() |
| 92 | + return result[0] |
| 93 | + |
| 94 | + def get_message_by_id(self, message_id: int): |
| 95 | + query = "SELECT role, content, time, id, deleted FROM messages WHERE id = ?" |
| 96 | + result = self.conn.execute(query, (message_id,)).fetchone() |
| 97 | + if result: |
| 98 | + return result |
| 99 | + else: |
| 100 | + raise IndexError("Index out of range") |
| 101 | + |
| 102 | + def get_message_by_index(self, chat_id: int, index: int): |
| 103 | + query = "SELECT role, content, time, id, deleted FROM messages WHERE chat_id = ? ORDER BY id LIMIT 1 OFFSET ?" |
| 104 | + result = self.conn.execute(query, (chat_id, index)).fetchone() |
| 105 | + if result: |
| 106 | + return result |
| 107 | + else: |
| 108 | + raise IndexError("Index out of range") |
| 109 | + |
| 110 | + def list_chats(self): |
| 111 | + query = "SELECT id, name, last_update FROM chats ORDER BY id DESC" |
| 112 | + result = self.conn.execute(query).fetchall() |
| 113 | + return result |
| 114 | + |
| 115 | + def set_chat_name(self, chat_id: int, name: str): |
| 116 | + query = "UPDATE chats SET name = ? WHERE id = ?" |
| 117 | + self.conn.execute(query, (name, chat_id)) |
| 118 | + self.conn.commit() |
| 119 | + |
| 120 | + def get_chat_name(self, chat_id): |
| 121 | + query = "SELECT name FROM chats WHERE id = ?" |
| 122 | + cursor = self.conn.execute(query, (chat_id,)) |
| 123 | + result = cursor.fetchone() |
| 124 | + if result: |
| 125 | + return result[0] |
| 126 | + else: |
| 127 | + return None |
| 128 | + |
| 129 | + def delete_message(self, message_id: int): |
| 130 | + query = """ |
| 131 | + UPDATE messages |
| 132 | + SET deleted = 1 |
| 133 | + WHERE id = ? |
| 134 | + """ |
| 135 | + self.conn.execute(query, (message_id,)) |
| 136 | + self.conn.commit() |
| 137 | + |
| 138 | + def search_messages(self, query: str, pagination_token: int, limit: int): |
| 139 | + fts_query = """ |
| 140 | + SELECT m.id, m.chat_id, snippet(messages_fts, 1, '\ue000', '\ue001', '...', 16) AS snippet |
| 141 | + FROM messages_fts |
| 142 | + JOIN messages m ON messages_fts.message_id = m.id |
| 143 | + WHERE messages_fts.content MATCH ? |
| 144 | + LIMIT ? |
| 145 | + OFFSET ? |
| 146 | + """ |
| 147 | + |
| 148 | + if pagination_token is None: |
| 149 | + offset = 0 |
| 150 | + else: |
| 151 | + offset = pagination_token |
| 152 | + |
| 153 | + parameters = (query.lower(), limit, offset) |
| 154 | + |
| 155 | + result = self.conn.execute(fts_query, parameters) |
| 156 | + |
| 157 | + message_ids = [] |
| 158 | + chat_ids = [] |
| 159 | + snippets = {} |
| 160 | + for row in result: |
| 161 | + message_ids.append(int(row[0])) |
| 162 | + snippets[int(row[0])] = row[2] |
| 163 | + chat_ids.append(int(row[1])) |
| 164 | + |
| 165 | + # Sort chat IDs |
| 166 | + sorted_chat_ids = sorted(set(chat_ids)) |
| 167 | + |
| 168 | + messages_by_chat = [] |
| 169 | + for chat_id in sorted_chat_ids: |
| 170 | + message_ids_for_chat = [(message_ids[i], snippets[message_ids[i]]) for i in range(len(message_ids)) if chat_ids[i] == chat_id] |
| 171 | + messages_by_chat.append((chat_id, message_ids_for_chat)) |
| 172 | + |
| 173 | + # Determine the pagination token |
| 174 | + next_offset = offset + limit |
| 175 | + has_more_results = len(message_ids) > next_offset |
| 176 | + pagination_token = next_offset if has_more_results else None |
| 177 | + |
| 178 | + return messages_by_chat, offset + len(message_ids) |
| 179 | + |
0 commit comments