File size: 14,025 Bytes
1c47d23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
649a2e8
1c47d23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
649a2e8
 
1c47d23
 
6db335b
649a2e8
 
1c47d23
 
 
649a2e8
 
 
 
 
 
 
 
 
1c47d23
649a2e8
1c47d23
 
 
 
 
649a2e8
1c47d23
 
649a2e8
 
 
1c47d23
 
 
 
 
 
 
 
649a2e8
1c47d23
 
 
 
 
 
 
649a2e8
 
 
1c47d23
649a2e8
 
 
 
 
1c47d23
 
 
 
 
 
 
 
 
 
 
649a2e8
 
1c47d23
 
 
649a2e8
1c47d23
 
649a2e8
 
 
1c47d23
 
 
649a2e8
 
1c47d23
649a2e8
 
1c47d23
649a2e8
 
 
1c47d23
649a2e8
 
1c47d23
649a2e8
 
 
 
1c47d23
 
649a2e8
 
 
 
 
1c47d23
 
649a2e8
 
1c47d23
 
 
6db335b
649a2e8
 
 
 
1c47d23
649a2e8
 
 
 
 
 
 
1c47d23
649a2e8
 
 
1c47d23
 
 
 
649a2e8
 
1c47d23
 
649a2e8
1c47d23
649a2e8
 
1c47d23
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import secrets
import sqlite3
import logging
from datetime import datetime, timezone
from pathlib import Path

from db.init import get_conn

log = logging.getLogger("db.models")


def now_iso():
    return datetime.now(timezone.utc).isoformat()


# ============================================================
# USERS
# ============================================================
def get_user(user_id: int):
    with get_conn() as conn:
        row = conn.execute(
            "SELECT * FROM users WHERE user_id = ?", (user_id,)
        ).fetchone()
    return dict(row) if row else None


def upsert_user(user_id: int, username: str = None, first_name: str = None):
    with get_conn() as conn:
        existing = conn.execute(
            "SELECT user_id FROM users WHERE user_id = ?", (user_id,)
        ).fetchone()
        if existing:
            conn.execute(
                "UPDATE users SET username=?, first_name=?, last_seen=? WHERE user_id=?",
                (username, first_name, now_iso(), user_id),
            )
        else:
            conn.execute(
                """INSERT INTO users(user_id, username, first_name, created_at, last_seen)
                   VALUES (?, ?, ?, ?, ?)""",
                (user_id, username, first_name, now_iso(), now_iso()),
            )


def bind_token(user_id: int, token: str) -> bool:
    with get_conn() as conn:
        row = conn.execute(
            "SELECT * FROM tokens WHERE token = ?", (token,)
        ).fetchone()
        if not row:
            return False
        if not row["is_active"]:
            return False
        if row["assigned_to"] not in (None, user_id):
            return False

        conn.execute(
            "UPDATE tokens SET assigned_to=? WHERE token=?", (user_id, token)
        )
        conn.execute(
            "UPDATE users SET token=?, is_active=1 WHERE user_id=?",
            (token, user_id),
        )
        return True


def user_has_valid_token(user_id: int) -> bool:
    with get_conn() as conn:
        row = conn.execute(
            """SELECT u.is_active AS u_active, t.is_active AS t_active
               FROM users u LEFT JOIN tokens t ON u.token = t.token
               WHERE u.user_id = ?""",
            (user_id,),
        ).fetchone()
    if not row:
        return False
    return bool(row["u_active"]) and bool(row["t_active"])


def revoke_user(user_id: int):
    with get_conn() as conn:
        conn.execute("UPDATE users SET is_active=0 WHERE user_id=?", (user_id,))
        conn.execute(
            "UPDATE tokens SET is_active=0 WHERE assigned_to=?", (user_id,)
        )


def delete_user(user_id: int):
    with get_conn() as conn:
        conn.execute("DELETE FROM conversations WHERE user_id=?", (user_id,))
        conn.execute("DELETE FROM users WHERE user_id=?", (user_id,))
        conn.execute(
            "UPDATE tokens SET assigned_to=NULL, is_active=0 WHERE assigned_to=?",
            (user_id,),
        )


def increment_user_turns(user_id: int):
    with get_conn() as conn:
        conn.execute(
            "UPDATE users SET total_turns = total_turns + 1 WHERE user_id=?",
            (user_id,),
        )


# ============================================================
# TOKENS
# ============================================================
def generate_token(assigned_to: int = None) -> str:
    tok = secrets.token_urlsafe(24)
    with get_conn() as conn:
        conn.execute(
            """INSERT INTO tokens(token, assigned_to, created_at)
               VALUES (?, ?, ?)""",
            (tok, assigned_to, now_iso()),
        )
        if assigned_to is not None:
            conn.execute(
                "UPDATE users SET token=?, is_active=1 WHERE user_id=?",
                (tok, assigned_to),
            )
    return tok


def list_tokens():
    with get_conn() as conn:
        rows = conn.execute(
            "SELECT * FROM tokens ORDER BY created_at DESC"
        ).fetchall()
    return [dict(r) for r in rows]


def revoke_token(token: str):
    with get_conn() as conn:
        conn.execute("UPDATE tokens SET is_active=0 WHERE token=?", (token,))
        conn.execute(
            "UPDATE users SET is_active=0 WHERE token=?", (token,)
        )


def reassign_token(token: str, new_user_id: int):
    with get_conn() as conn:
        conn.execute(
            "UPDATE tokens SET assigned_to=? WHERE token=?", (new_user_id, token)
        )
        conn.execute(
            "UPDATE users SET token=?, is_active=1 WHERE user_id=?",
            (token, new_user_id),
        )


def bump_token_use(token: str):
    with get_conn() as conn:
        conn.execute(
            """UPDATE tokens SET turns_used = turns_used + 1,
                                  last_used = ?
               WHERE token = ?""",
            (now_iso(), token),
        )


# ============================================================
# CONVERSATIONS (live context only — cleared by /reset)
# ============================================================
def add_message(user_id: int, role: str, content: str,
                latency_ms: int = None, gpu_time_ms: int = None):
    with get_conn() as conn:
        conn.execute(
            """INSERT INTO conversations
               (user_id, role, content, timestamp, latency_ms, gpu_time_ms)
               VALUES (?, ?, ?, ?, ?, ?)""",
            (user_id, role, content, now_iso(), latency_ms, gpu_time_ms),
        )


def get_history(user_id: int, limit_turns: int = 6):
    """Return last N turns (2*N messages) in chronological order."""
    with get_conn() as conn:
        rows = conn.execute(
            """SELECT role, content FROM conversations
               WHERE user_id = ?
               ORDER BY id DESC LIMIT ?""",
            (user_id, limit_turns * 2),
        ).fetchall()
    rows = list(reversed(rows))
    return [{"role": r["role"], "content": r["content"]} for r in rows]


def clear_history(user_id: int):
    with get_conn() as conn:
        conn.execute("DELETE FROM conversations WHERE user_id=?", (user_id,))


def list_users():
    with get_conn() as conn:
        rows = conn.execute(
            "SELECT * FROM users ORDER BY created_at DESC"
        ).fetchall()
    return [dict(r) for r in rows]


def list_conversations(user_id: int = None, limit: int = 500):
    with get_conn() as conn:
        if user_id:
            rows = conn.execute(
                """SELECT * FROM conversations WHERE user_id=?
                   ORDER BY id DESC LIMIT ?""",
                (user_id, limit),
            ).fetchall()
        else:
            rows = conn.execute(
                "SELECT * FROM conversations ORDER BY id DESC LIMIT ?",
                (limit,),
            ).fetchall()
    return [dict(r) for r in rows]


def stats():
    with get_conn() as conn:
        total_users = conn.execute("SELECT COUNT(*) c FROM users").fetchone()["c"]
        active_users = conn.execute(
            "SELECT COUNT(*) c FROM users WHERE is_active=1"
        ).fetchone()["c"]
        total_msgs = conn.execute(
            "SELECT COUNT(*) c FROM conversations"
        ).fetchone()["c"]
        today = datetime.now(timezone.utc).date().isoformat()
        today_msgs = conn.execute(
            "SELECT COUNT(*) c FROM conversations WHERE timestamp LIKE ?",
            (today + "%",),
        ).fetchone()["c"]
    return {
        "total_users": total_users,
        "active_users": active_users,
        "total_messages": total_msgs,
        "messages_today": today_msgs,
    }


# ============================================================
# SETTINGS
# ============================================================
def get_setting(key: str, default=None):
    with get_conn() as conn:
        row = conn.execute(
            "SELECT value FROM settings WHERE key=?", (key,)
        ).fetchone()
    return row["value"] if row else default


def set_setting(key: str, value: str):
    with get_conn() as conn:
        conn.execute(
            "INSERT INTO settings(key, value) VALUES (?, ?) "
            "ON CONFLICT(key) DO UPDATE SET value=excluded.value",
            (key, str(value)),
        )


def all_settings():
    with get_conn() as conn:
        rows = conn.execute("SELECT key, value FROM settings").fetchall()
    return {r["key"]: r["value"] for r in rows}


# ============================================================
# GPU LOG
# ============================================================
def log_gpu(user_id: int, duration_ms: int, status: str):
    with get_conn() as conn:
        conn.execute(
            """INSERT INTO gpu_log(user_id, timestamp, duration_ms, status)
               VALUES (?, ?, ?, ?)""",
            (user_id, now_iso(), duration_ms, status),
        )


def recent_gpu_log(limit: int = 50):
    with get_conn() as conn:
        rows = conn.execute(
            "SELECT * FROM gpu_log ORDER BY id DESC LIMIT ?", (limit,)
        ).fetchall()
    return [dict(r) for r in rows]


# ============================================================
# ARCHIVE — one markdown file PER SESSION
# Path: /data/chat_logs/{user_id}/{timestamp}.md
# A new session starts whenever the previous one was closed by /reset
# (or when the user hasn't chatted before).
# ============================================================
import os
import re
from datetime import datetime as _dt

ARCHIVE_DIR = os.environ.get("ARCHIVE_DIR", "/data/chat_logs")

# In-process tracker: user_id -> Path of the currently-open session file.
# Reset when /reset is called, so the next message opens a fresh file.
_active_session = {}


def _user_dir(user_id: int) -> Path:
    d = Path(ARCHIVE_DIR) / str(user_id)
    d.mkdir(parents=True, exist_ok=True)
    return d


def _new_session_path(user_id: int) -> Path:
    """Generate a fresh session file path with a UTC timestamp."""
    d = _user_dir(user_id)
    stamp = _dt.now(timezone.utc).strftime("%Y%m%dT%H%M%S")
    p = d / f"{stamp}.md"
    # Extremely unlikely, but avoid clobbering if two messages land in the
    # same second.
    n = 1
    while p.exists():
        p = d / f"{stamp}_{n}.md"
        n += 1
    return p


def archive_append(user_id: int, role: str, content: str,
                   latency_ms: int = None):
    """
    Append a message to the user's *current* session markdown file.
    If no session is open, start a new one.
    """
    p = _active_session.get(user_id)
    if p is None or not p.exists():
        p = _new_session_path(user_id)
        _active_session[user_id] = p
        # Write the file header
        p.write_text(
            f"# Transcript — user {user_id}\n\n"
            f"_Session started: {now_iso()}_\n\n---\n\n",
            encoding="utf-8",
        )

    header = "🧑 **User**" if role == "user" else "🤖 **Assistant**"
    with p.open("a", encoding="utf-8") as f:
        f.write(f"### {header} — `{now_iso()}`\n\n")
        if role == "assistant" and latency_ms is not None:
            f.write(f"_(latency: {latency_ms} ms)_\n\n")
        f.write(content.strip() + "\n\n")


def archive_close_session(user_id: int):
    """
    Close the current session. The next message will open a new file.
    Called by /reset.
    """
    p = _active_session.pop(user_id, None)
    if p and p.exists():
        with p.open("a", encoding="utf-8") as f:
            f.write(f"\n---\n\n_Session closed: {now_iso()}_\n")


def archive_list(user_id: int = None):
    """
    List archived session files.
    If user_id is None, list all users' files.
    Returns list of dicts: {user_id, filename, path, size, modified}
    """
    root = Path(ARCHIVE_DIR)
    if not root.exists():
        return []

    out = []
    user_dirs = [root / str(user_id)] if user_id else [d for d in root.iterdir() if d.is_dir()]
    for d in user_dirs:
        if not d.exists():
            continue
        try:
            uid = int(d.name)
        except ValueError:
            continue
        for f in sorted(d.glob("*.md"), reverse=True):
            stat = f.stat()
            out.append({
                "user_id": uid,
                "filename": f.name,
                "path": str(f),
                "size": stat.st_size,
                "modified": _dt.fromtimestamp(stat.st_mtime, timezone.utc).isoformat(),
            })
    return out


def archive_read(user_id: int, filename: str) -> str:
    p = Path(ARCHIVE_DIR) / str(user_id) / filename
    if not p.exists():
        return ""
    return p.read_text(encoding="utf-8")


def archive_delete(user_id: int, filename: str) -> bool:
    p = Path(ARCHIVE_DIR) / str(user_id) / filename
    if p.exists():
        p.unlink()
        # Close in-process session if we just deleted the active file
        if _active_session.get(user_id) == p:
            _active_session.pop(user_id, None)
        return True
    return False


def archive_delete_user(user_id: int) -> int:
    """Delete ALL session files for one user. Returns count deleted."""
    d = Path(ARCHIVE_DIR) / str(user_id)
    if not d.exists():
        return 0
    n = 0
    for f in d.glob("*.md"):
        f.unlink()
        n += 1
    _active_session.pop(user_id, None)
    return n


def archive_delete_all():
    """Delete every transcript for every user. Returns count deleted."""
    root = Path(ARCHIVE_DIR)
    if not root.exists():
        return 0
    n = 0
    for d in root.iterdir():
        if not d.is_dir():
            continue
        for f in d.glob("*.md"):
            f.unlink()
            n += 1
    _active_session.clear()
    return n


def archive_export_user(user_id: int) -> str:
    """Concatenate all of a user's sessions into one big markdown blob."""
    sessions = archive_list(user_id)
    # Oldest first
    sessions = sorted(sessions, key=lambda x: x["filename"])
    parts = [f"# Full transcript — user {user_id}\n"]
    for s in sessions:
        parts.append(f"\n\n## Session: {s['filename']}\n")
        parts.append(archive_read(user_id, s["filename"]))
    return "".join(parts)