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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
import os
import sys
import uuid
import time
import shutil
import tempfile
import functools
from pathlib import Path
from datetime import datetime
from flask import (
    Flask, request, jsonify, render_template,
    Response, send_from_directory, abort, make_response,
)
from flask_cors import CORS
from flask_compress import Compress

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

import config
config.validate_config()

from rag.document_loader import load_and_chunk_file
from rag.vector_store    import (
    add_documents, get_document_count, clear_store,
    get_unique_sources, delete_source, source_exists,
    get_source_chunk_count, get_source_preview,
)
from rag.retriever       import invalidate_bm25_cache
from orchestrator.graph  import run_chat
from utils.logger        import get_logger
from utils.knowledge_loader import download_knowledge_from_hf
from keep_alive          import start_keep_alive

logger = get_logger(__name__)

app = Flask(__name__, static_folder="../static", template_folder="../templates")
CORS(app)
Compress(app)

# ── Admin auth ───────────────────────────────────────────────
def require_admin(f):
    @functools.wraps(f)
    def decorated(*args, **kwargs):
        # Check cookie (set after /admin login) or header
        key = (
            request.cookies.get("admin_key")
            or request.headers.get("X-Admin-Key")
            or request.args.get("key")
        )
        if key != config.ADMIN_SECRET_KEY:
            abort(403)
        return f(*args, **kwargs)
    return decorated

# ── Startup: auto-index all knowledge & uploads ─────────────
def auto_index_knowledge() -> None:
    """Index all knowledge + uploaded files. Knowledge files are ALWAYS re-indexed
    (delete old + re-add) so edits are always picked up on restart."""
    knowledge_dir = Path(config.KNOWLEDGE_DIR)
    uploads_dir   = Path(config.UPLOADS_DIR)
    total_indexed = 0

    # ── Permanent knowledge files: FORCE re-index every startup ──
    knowledge_files = [
        f for f in knowledge_dir.iterdir()
        if f.is_file() and f.suffix.lower() in config.SUPPORTED_EXTENSIONS
        and not f.name.startswith(".")
    ]
    if knowledge_files:
        logger.info(f"Auto-index [permanent]: re-indexing {len(knowledge_files)} file(s) in {knowledge_dir}")
        for fpath in knowledge_files:
            try:
                # Delete old chunks first so updates are always picked up
                delete_source(fpath.name)
                chunks = load_and_chunk_file(str(fpath), source_name=fpath.name)
                if chunks:
                    added = add_documents(chunks)
                    logger.info(f"    βœ“ {fpath.name}: {added} chunks indexed.")
                    total_indexed += added
            except Exception as e:
                logger.error(f"  βœ— Failed to index {fpath.name}: {e}")
    else:
        logger.info(f"Auto-index [permanent]: no files in {knowledge_dir}")

    # ── Uploaded files: skip if already indexed ───────────────
    upload_files = [
        f for f in uploads_dir.iterdir()
        if f.is_file() and f.suffix.lower() in config.SUPPORTED_EXTENSIONS
        and not f.name.startswith(".")
    ]
    if upload_files:
        logger.info(f"Auto-index [uploaded]: scanning {len(upload_files)} file(s) in {uploads_dir}")
        for fpath in upload_files:
            if source_exists(fpath.name):
                logger.info(f"    β†’ {fpath.name}: already indexed, skipping.")
                continue
            try:
                chunks = load_and_chunk_file(str(fpath), source_name=fpath.name)
                if chunks:
                    added = add_documents(chunks)
                    logger.info(f"    βœ“ {fpath.name}: {added} chunks indexed.")
                    total_indexed += added
            except Exception as e:
                logger.error(f"  βœ— Failed to index {fpath.name}: {e}")
    else:
        logger.info(f"Auto-index [uploaded]: no files in {uploads_dir}")

    if total_indexed > 0:
        invalidate_bm25_cache()
        logger.info(f"Auto-index complete. {total_indexed} chunks added/updated.")
    else:
        logger.info("Auto-index complete. All files up to date.")

# Run at startup: download private knowledge (HF) then index
with app.app_context():
    download_knowledge_from_hf()   # ← pulls from private HF Dataset (no-op locally)
    auto_index_knowledge()

# ── Health check & keep-alive ping ──────────────────────────
@app.route("/ping")
def ping():
    return jsonify({"status": "alive", "service": "Kuldeep AI"}), 200

# ── Public routes ────────────────────────────────────────────
@app.route("/")
def index():
    return render_template("index.html")

@app.route("/static/<path:path>")
def send_static(path):
    return send_from_directory("../static", path)

@app.route("/api/stats", methods=["GET"])
def get_stats():
    try:
        chunks = get_document_count()
    except Exception:
        chunks = 0
    return jsonify({"total_chunks": chunks})

@app.route("/api/chat", methods=["POST"])
def chat():
    data       = request.json
    query      = data.get("message")
    session_id = data.get("session_id", str(uuid.uuid4())[:8])

    if not query:
        return jsonify({"error": "Message is required"}), 400

    def generate():
        try:
            start   = time.perf_counter()
            result  = run_chat(query=query, session_id=session_id)
            elapsed = round(time.perf_counter() - start, 2)

            # Log routing decision visibly in console
            agent_used = result.get("agent_used", "unknown")
            route      = result.get("route", "unknown")
            logger.info(f"━━━ [{session_id}] Route: {route} β†’ {agent_used} ({elapsed}s) ━━━")

            # Stream words β€” agent info hidden from public response
            words = result["response"].split(" ")
            for i, w in enumerate(words):
                yield w + (" " if i < len(words) - 1 else "")
                time.sleep(0.005)  # 5ms β€” 3x faster than before

        except Exception as e:
            logger.error(f"[/api/chat] Error: {e}", exc_info=True)
            yield f"⚠️ Server error: {str(e)}"

    return Response(generate(), mimetype="text/event-stream")


# ── Admin page ────────────────────────────────────────────────
@app.route("/admin")
def admin():
    key = request.args.get("key", "")
    if key != config.ADMIN_SECRET_KEY:
        return render_template("admin_denied.html"), 403
    resp = make_response(render_template("admin.html"))
    # Set session cookie (1 day)
    resp.set_cookie(
        "admin_key", config.ADMIN_SECRET_KEY,
        max_age=86400, httponly=True, samesite="Lax",
    )
    return resp

# ── Admin API: force reindex all ─────────────────────────────
@app.route("/api/admin/reindex-all", methods=["POST"])
@require_admin
def reindex_all():
    try:
        logger.info("Manual reindex triggered via admin API.")
        auto_index_knowledge()
        chunks = get_document_count()
        return jsonify({"status": "ok", "total_chunks": chunks})
    except Exception as e:
        logger.error(f"Reindex failed: {e}")
        return jsonify({"error": str(e)}), 500

# ── Admin API: stats ──────────────────────────────────────────
@app.route("/api/admin/stats", methods=["GET"])
@require_admin
def admin_stats():
    try:
        chunks = get_document_count()
        sources = get_unique_sources()

        # Classify sources into knowledge vs uploads
        knowledge_files = [
            f.name for f in Path(config.KNOWLEDGE_DIR).iterdir()
            if f.is_file() and f.suffix.lower() in config.SUPPORTED_EXTENSIONS
        ]
        upload_files = [
            f.name for f in Path(config.UPLOADS_DIR).iterdir()
            if f.is_file() and f.suffix.lower() in config.SUPPORTED_EXTENSIONS
        ]

        return jsonify({
            "total_chunks":    chunks,
            "total_docs":      len(sources),
            "knowledge_count": len(knowledge_files),
            "upload_count":    len(upload_files),
        })
    except Exception as e:
        return jsonify({"error": str(e)}), 500

# ── Admin API: list documents ─────────────────────────────────
@app.route("/api/documents", methods=["GET"])
@require_admin
def get_documents():
    try:
        sources    = get_unique_sources()
        knowledge_names = {
            f.name for f in Path(config.KNOWLEDGE_DIR).iterdir()
            if f.is_file() and f.suffix.lower() in config.SUPPORTED_EXTENSIONS
        }
        upload_names = {
            f.name for f in Path(config.UPLOADS_DIR).iterdir()
            if f.is_file() and f.suffix.lower() in config.SUPPORTED_EXTENSIONS
        }

        docs = []
        for src in sources:
            # Determine disk path
            if src in knowledge_names:
                disk_path = Path(config.KNOWLEDGE_DIR) / src
                origin = "knowledge"
            elif src in upload_names:
                disk_path = Path(config.UPLOADS_DIR) / src
                origin = "uploaded"
            else:
                disk_path = None
                origin = "unknown"

            size_bytes = disk_path.stat().st_size if disk_path and disk_path.exists() else 0
            chunk_count = get_source_chunk_count(src)

            docs.append({
                "name":        src,
                "origin":      origin,        # "knowledge" | "uploaded" | "unknown"
                "size_bytes":  size_bytes,
                "chunk_count": chunk_count,
                "file_type":   Path(src).suffix.lower().lstrip("."),
            })

        return jsonify({"documents": docs})
    except Exception as e:
        logger.error(f"Error listing documents: {e}")
        return jsonify({"error": str(e)}), 500

# ── Admin API: view document content ─────────────────────────
@app.route("/api/document/view", methods=["GET"])
@require_admin
def view_document():
    source_name = request.args.get("filename")
    if not source_name:
        return jsonify({"error": "filename is required"}), 400

    # Try to read actual file first (most accurate)
    for folder in [config.KNOWLEDGE_DIR, config.UPLOADS_DIR]:
        fpath = Path(folder) / source_name
        if fpath.exists() and fpath.suffix.lower() == ".txt":
            return jsonify({
                "filename": source_name,
                "content":  fpath.read_text(encoding="utf-8", errors="ignore"),
                "source":   "file",
            })
        elif fpath.exists() and fpath.suffix.lower() == ".pdf":
            preview = get_source_preview(source_name)
            return jsonify({
                "filename": source_name,
                "content":  preview,
                "source":   "chunks",
                "note":     "PDF β€” showing indexed text preview",
            })

    # Fallback: reconstruct from ChromaDB chunks
    preview = get_source_preview(source_name)
    return jsonify({
        "filename": source_name,
        "content":  preview or "No content found.",
        "source":   "chunks",
    })

# ── Admin API: update / edit document ─────────────────────────
@app.route("/api/document/update", methods=["POST"])
@require_admin
def update_document():
    """
    Two modes:
      1. TXT inline edit: JSON body with {filename, content}
      2. File re-upload:  multipart form with file
    """
    # Mode 1: inline text update
    if request.is_json:
        data    = request.json
        filename = data.get("filename")
        content  = data.get("content")
        if not filename or content is None:
            return jsonify({"error": "filename and content required"}), 400

        # Find existing file location
        target = None
        for folder in [config.KNOWLEDGE_DIR, config.UPLOADS_DIR]:
            fpath = Path(folder) / filename
            if fpath.exists():
                target = fpath
                break

        if not target:
            # Default to uploads dir
            target = Path(config.UPLOADS_DIR) / filename

        # Write updated content
        target.write_text(content, encoding="utf-8")

        # Re-index: delete old chunks β†’ re-index file
        delete_source(filename)
        chunks = load_and_chunk_file(str(target), source_name=filename)
        count  = add_documents(chunks)
        invalidate_bm25_cache()

        logger.info(f"Document '{filename}' updated inline. {count} chunks re-indexed.")
        return jsonify({
            "message":      f"'{filename}' updated and re-indexed successfully.",
            "chunk_count":  count,
        })

    # Mode 2: file re-upload
    if "file" not in request.files:
        return jsonify({"error": "No file provided"}), 400

    f        = request.files["file"]
    filename = f.filename
    suffix   = Path(filename).suffix.lower()

    if suffix not in config.SUPPORTED_EXTENSIONS:
        return jsonify({"error": f"Unsupported file type: {suffix}"}), 400

    # Save to uploads dir (replaces old file)
    save_path = Path(config.UPLOADS_DIR) / filename
    f.save(str(save_path))

    # Re-index
    delete_source(filename)
    chunks = load_and_chunk_file(str(save_path), source_name=filename)
    count  = add_documents(chunks)
    invalidate_bm25_cache()

    logger.info(f"Document '{filename}' re-uploaded. {count} chunks re-indexed.")
    return jsonify({
        "message":     f"'{filename}' updated and re-indexed.",
        "chunk_count": count,
    })

# ── Admin API: upload new document(s) ────────────────────────
@app.route("/api/upload", methods=["POST"])
@require_admin
def upload_files():
    if "files" not in request.files:
        return jsonify({"error": "No files provided"}), 400

    files         = request.files.getlist("files")
    results       = []
    total_chunks  = 0

    for f in files:
        filename = f.filename
        suffix   = Path(filename).suffix.lower()

        if suffix not in config.SUPPORTED_EXTENSIONS:
            results.append({"file": filename, "status": "skipped", "reason": f"Unsupported type {suffix}"})
            continue

        # ── Save permanently to uploads/ ──
        save_path = Path(config.UPLOADS_DIR) / filename
        f.save(str(save_path))

        # ── If already indexed, re-index (update) ──
        if source_exists(filename):
            logger.info(f"  '{filename}' already indexed β€” re-indexing (update).")
            delete_source(filename)

        try:
            chunks = load_and_chunk_file(str(save_path), source_name=filename)
            count  = add_documents(chunks)
            invalidate_bm25_cache()
            total_chunks += count
            results.append({"file": filename, "status": "indexed", "chunks": count})
            logger.info(f"Uploaded & indexed: {filename} ({count} chunks)")
        except Exception as e:
            logger.error(f"Error indexing {filename}: {e}")
            results.append({"file": filename, "status": "error", "reason": str(e)})

    return jsonify({
        "message":      f"Processed {len(files)} file(s).",
        "total_chunks": total_chunks,
        "results":      results,
    })

# ── Admin API: delete document ────────────────────────────────
@app.route("/api/delete_document", methods=["DELETE"])
@require_admin
def delete_document():
    data        = request.json or {}
    source_name = data.get("filename")
    if not source_name:
        return jsonify({"error": "filename is required"}), 400

    # Remove from ChromaDB
    ok = delete_source(source_name)
    if ok:
        invalidate_bm25_cache()

    # Remove from disk (uploads only β€” don't delete permanent knowledge files)
    disk_path = Path(config.UPLOADS_DIR) / source_name
    if disk_path.exists():
        disk_path.unlink()
        logger.info(f"Disk file deleted: {disk_path}")
    else:
        # Check if it's a knowledge file (can delete from ChromaDB but not disk)
        knowledge_path = Path(config.KNOWLEDGE_DIR) / source_name
        if knowledge_path.exists():
            logger.warning(
                f"'{source_name}' is a permanent knowledge file β€” "
                f"removed from ChromaDB but NOT from disk. It will re-index on restart."
            )
            return jsonify({
                "message": (
                    f"'{source_name}' removed from active knowledge base. "
                    f"Note: this is a permanent file β€” it will be re-indexed on next server restart."
                )
            })

    return jsonify({"message": f"'{source_name}' deleted successfully."})

# ── Admin API: clear all uploads ──────────────────────────────
@app.route("/api/clear", methods=["POST"])
@require_admin
def clear_database():
    data          = request.json or {}
    clear_uploads_only = data.get("uploads_only", False)

    try:
        if clear_uploads_only:
            # Only clear uploaded docs, keep permanent knowledge
            upload_files = [
                f.name for f in Path(config.UPLOADS_DIR).iterdir()
                if f.is_file() and f.suffix.lower() in config.SUPPORTED_EXTENSIONS
            ]
            for fname in upload_files:
                delete_source(fname)
                fpath = Path(config.UPLOADS_DIR) / fname
                if fpath.exists():
                    fpath.unlink()
            invalidate_bm25_cache()
            # Re-index permanent knowledge
            auto_index_knowledge()
            logger.info(f"Cleared {len(upload_files)} uploaded file(s).")
            return jsonify({
                "message": f"Cleared {len(upload_files)} uploaded file(s). Permanent knowledge preserved.",
                "total_chunks": get_document_count(),
            })
        else:
            clear_store()
            invalidate_bm25_cache()
            logger.info("Full knowledge base cleared.")
            return jsonify({"message": "Knowledge base cleared.", "total_chunks": 0})
    except Exception as e:
        logger.error(f"Error clearing KB: {e}")
        return jsonify({"error": str(e)}), 500

if __name__ == "__main__":
    app.run(debug=True, port=5000)