File size: 20,277 Bytes
58b74a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
"""
web_app.py β€” FastAPI web UI + REST API for the Knowledge Agent
==============================================================
Run with:  uvicorn web_app:app --host 0.0.0.0 --port 8000 --reload

Endpoints:
  GET  /              β†’ Web UI (chat interface)
  POST /api/ask       β†’ JSON Q&A
  GET  /api/stream    β†’ SSE streaming answer
  POST /api/index     β†’ Trigger re-indexing
  POST /api/upload    β†’ Upload and index a new document
  GET  /api/docs-list β†’ List indexed documents
  GET  /api/stats     β†’ Knowledge base stats
  DELETE /api/doc/{filename} β†’ Remove a document
"""

import os
import shutil
import asyncio
from pathlib import Path
from typing import Optional

from fastapi import FastAPI, UploadFile, File, HTTPException, Query
from fastapi.responses import HTMLResponse, StreamingResponse, JSONResponse
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel

from knowledge_api import (
    query_knowledge,
    index_docs,
    index_single,
    get_knowledge_stats,
    list_indexed_docs,
    delete_doc,
    stream_knowledge_answer,
)

DOCS_DIR = os.getenv("DOCS_DIR", "./documents")

# ── App setup ─────────────────────────────────────────────────────────────────
app = FastAPI(
    title="Knowledge Agent",
    description="Personal RAG system over your documents",
    version="1.0.0",
)

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_methods=["*"],
    allow_headers=["*"],
)


# ── Request / Response models ─────────────────────────────────────────────────
class AskRequest(BaseModel):
    question: str
    top_k: int = 5

class IndexRequest(BaseModel):
    docs_dir: Optional[str] = None
    force: bool = False


# ── REST API endpoints ────────────────────────────────────────────────────────

@app.post("/api/ask")
async def ask(req: AskRequest):
    """Ask a question and get a grounded answer from your documents."""
    if not req.question.strip():
        raise HTTPException(400, "Question cannot be empty.")
    result = query_knowledge(req.question, top_k=req.top_k)
    return result


@app.get("/api/stream")
async def stream(
    question: str = Query(..., description="Your question"),
    top_k: int = Query(5, description="Number of chunks to retrieve"),
):
    """
    SSE endpoint β€” streams the answer token by token.
    Used by the web UI for a typing-cursor effect.
    """
    if not question.strip():
        raise HTTPException(400, "Question cannot be empty.")

    def event_generator():
        for token in stream_knowledge_answer(question, top_k=top_k):
            # SSE format: "data: <token>\n\n"
            yield f"data: {token}\n\n"
        yield "data: [DONE]\n\n"

    return StreamingResponse(
        event_generator(),
        media_type="text/event-stream",
        headers={
            "Cache-Control": "no-cache",
            "X-Accel-Buffering": "no",
        },
    )


@app.post("/api/index")
async def trigger_index(req: IndexRequest):
    """Trigger indexing of the documents directory."""
    docs_dir = req.docs_dir or DOCS_DIR
    # Run in a thread pool so we don't block the event loop
    loop = asyncio.get_event_loop()
    await loop.run_in_executor(
        None, lambda: index_docs(docs_dir=docs_dir, force=req.force)
    )
    stats = get_knowledge_stats()
    return {"message": "Indexing complete.", "stats": stats}


@app.post("/api/upload")
async def upload_document(file: UploadFile = File(...)):
    """
    Upload a document file; save it to the documents dir and index it.
    Supports: .pdf, .docx, .txt, .md
    """
    allowed = {".pdf", ".docx", ".doc", ".txt", ".md", ".markdown"}
    ext = Path(file.filename).suffix.lower()
    if ext not in allowed:
        raise HTTPException(400, f"Unsupported file type: {ext}. Allowed: {allowed}")

    os.makedirs(DOCS_DIR, exist_ok=True)
    dest = os.path.join(DOCS_DIR, file.filename)

    # Save file
    with open(dest, "wb") as f:
        shutil.copyfileobj(file.file, f)

    # Index it
    loop = asyncio.get_event_loop()
    await loop.run_in_executor(None, lambda: index_single(dest))

    return {
        "message": f"'{file.filename}' uploaded and indexed.",
        "path": dest,
    }


@app.get("/api/docs-list")
async def docs_list():
    """List all documents currently in the knowledge base."""
    return {"documents": list_indexed_docs()}


@app.get("/api/stats")
async def stats():
    """Return knowledge base statistics."""
    return get_knowledge_stats()


@app.delete("/api/doc/{filename}")
async def remove_doc(filename: str):
    """Remove a document and all its chunks from the index."""
    delete_doc(filename)
    return {"message": f"Deleted '{filename}' from the knowledge base."}


# ── Web UI ────────────────────────────────────────────────────────────────────

@app.get("/", response_class=HTMLResponse)
async def ui():
    """Serve the single-page chat interface."""
    return HTMLResponse(content=WEB_UI_HTML)


# ── HTML for the web UI (self-contained, no build step needed) ────────────────
WEB_UI_HTML = """<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Knowledge Agent</title>
<link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;500&family=IBM+Plex+Sans:wght@300;400;600&display=swap" rel="stylesheet">
<style>
  :root {
    --bg:       #0d0f14;
    --surface:  #161921;
    --border:   #2a2f3d;
    --accent:   #5b8def;
    --accent2:  #7ecfb0;
    --text:     #d4dae8;
    --muted:    #5c6480;
    --danger:   #e05c7a;
    --mono:     'IBM Plex Mono', monospace;
    --sans:     'IBM Plex Sans', sans-serif;
  }
  * { box-sizing: border-box; margin: 0; padding: 0; }
  body {
    background: var(--bg);
    color: var(--text);
    font-family: var(--sans);
    height: 100vh;
    display: flex;
    flex-direction: column;
  }

  /* ── Header ── */
  header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 24px;
    border-bottom: 1px solid var(--border);
    background: var(--surface);
    flex-shrink: 0;
  }
  .logo {
    font-family: var(--mono);
    font-size: 1rem;
    color: var(--accent);
    letter-spacing: .04em;
  }
  .logo span { color: var(--accent2); }
  .header-actions { display: flex; gap: 10px; }
  .btn {
    font-family: var(--mono);
    font-size: .75rem;
    padding: 6px 14px;
    border: 1px solid var(--border);
    border-radius: 4px;
    background: transparent;
    color: var(--text);
    cursor: pointer;
    transition: all .15s;
  }
  .btn:hover { border-color: var(--accent); color: var(--accent); }
  .btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
  .btn.primary:hover { opacity: .85; }
  .btn.danger { border-color: var(--danger); color: var(--danger); }

  /* ── Layout ── */
  .main { display: flex; flex: 1; overflow: hidden; }

  /* ── Sidebar ── */
  .sidebar {
    width: 260px;
    border-right: 1px solid var(--border);
    background: var(--surface);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
    overflow: hidden;
  }
  .sidebar-section { padding: 16px; border-bottom: 1px solid var(--border); }
  .sidebar-title {
    font-family: var(--mono);
    font-size: .65rem;
    text-transform: uppercase;
    letter-spacing: .12em;
    color: var(--muted);
    margin-bottom: 10px;
  }
  #stats-panel { font-size: .78rem; line-height: 1.9; color: var(--muted); }
  #stats-panel strong { color: var(--text); }
  .doc-list { flex: 1; overflow-y: auto; padding: 8px 0; }
  .doc-item {
    padding: 8px 16px;
    font-size: .78rem;
    color: var(--muted);
    display: flex;
    align-items: center;
    gap: 8px;
    cursor: default;
  }
  .doc-item .dot { width: 6px; height: 6px; border-radius: 50%; background: var(--accent2); flex-shrink: 0; }
  .doc-item .del {
    margin-left: auto;
    color: var(--danger);
    cursor: pointer;
    font-size: .9rem;
    opacity: 0;
    transition: opacity .15s;
  }
  .doc-item:hover .del { opacity: 1; }

  /* Upload zone */
  .upload-zone {
    margin: 0 16px 16px;
    border: 1px dashed var(--border);
    border-radius: 6px;
    padding: 12px;
    text-align: center;
    font-size: .75rem;
    color: var(--muted);
    cursor: pointer;
    transition: border-color .15s;
  }
  .upload-zone:hover { border-color: var(--accent); color: var(--accent); }
  #file-input { display: none; }

  /* ── Chat area ── */
  .chat-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
  }
  .messages {
    flex: 1;
    overflow-y: auto;
    padding: 28px 32px;
    display: flex;
    flex-direction: column;
    gap: 24px;
  }
  .msg { display: flex; gap: 14px; }
  .msg-avatar {
    width: 30px; height: 30px;
    border-radius: 4px;
    display: flex; align-items: center; justify-content: center;
    font-family: var(--mono);
    font-size: .65rem;
    font-weight: 500;
    flex-shrink: 0;
    margin-top: 2px;
  }
  .msg.user .msg-avatar  { background: #2b3354; color: var(--accent); }
  .msg.agent .msg-avatar { background: #1e3330; color: var(--accent2); }
  .msg-body { flex: 1; }
  .msg-label {
    font-size: .68rem;
    font-family: var(--mono);
    color: var(--muted);
    margin-bottom: 5px;
    text-transform: uppercase;
    letter-spacing: .08em;
  }
  .msg-text {
    font-size: .875rem;
    line-height: 1.75;
    color: var(--text);
    white-space: pre-wrap;
    word-break: break-word;
  }
  .msg.user .msg-text { color: #aebcdc; }
  .sources-bar {
    margin-top: 10px;
    font-size: .7rem;
    font-family: var(--mono);
    color: var(--muted);
  }
  .sources-bar span {
    display: inline-block;
    background: #1a2238;
    border: 1px solid var(--border);
    border-radius: 3px;
    padding: 2px 8px;
    margin: 2px 3px 0 0;
    color: var(--accent);
  }

  /* ── Input row ── */
  .input-row {
    padding: 18px 32px;
    border-top: 1px solid var(--border);
    display: flex;
    gap: 10px;
    background: var(--surface);
    flex-shrink: 0;
  }
  #question-input {
    flex: 1;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 12px 16px;
    font-family: var(--sans);
    font-size: .875rem;
    color: var(--text);
    resize: none;
    outline: none;
    transition: border-color .15s;
    line-height: 1.5;
    min-height: 48px;
    max-height: 160px;
  }
  #question-input:focus { border-color: var(--accent); }
  #question-input::placeholder { color: var(--muted); }
  #send-btn {
    align-self: flex-end;
    height: 48px;
    padding: 0 22px;
  }

  /* ── Status bar ── */
  #status-bar {
    padding: 6px 32px;
    font-size: .7rem;
    font-family: var(--mono);
    color: var(--muted);
    background: var(--bg);
    border-top: 1px solid var(--border);
    flex-shrink: 0;
  }

  /* Scrollbar */
  ::-webkit-scrollbar { width: 6px; }
  ::-webkit-scrollbar-track { background: transparent; }
  ::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

  /* Loading dots */
  .typing { display: flex; gap: 5px; align-items: center; padding: 4px 0; }
  .typing .dot { width: 6px; height: 6px; border-radius: 50%; background: var(--accent); animation: blink 1.2s infinite; }
  .typing .dot:nth-child(2) { animation-delay: .2s; }
  .typing .dot:nth-child(3) { animation-delay: .4s; }
  @keyframes blink { 0%,80%,100%{opacity:.2} 40%{opacity:1} }

  .empty-state {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    color: var(--muted);
    font-size: .875rem;
    gap: 10px;
    text-align: center;
  }
  .empty-state .icon { font-size: 2.5rem; margin-bottom: 4px; }
</style>
</head>
<body>

<header>
  <div class="logo">knowledge<span>_agent</span></div>
  <div class="header-actions">
    <button class="btn" onclick="loadStats()">↻ Refresh</button>
    <button class="btn primary" onclick="triggerIndex()">⚑ Re-index</button>
  </div>
</header>

<div class="main">
  <!-- Sidebar -->
  <div class="sidebar">
    <div class="sidebar-section">
      <div class="sidebar-title">Knowledge Base</div>
      <div id="stats-panel">Loading…</div>
    </div>
    <div class="sidebar-section" style="flex:1;overflow:hidden;display:flex;flex-direction:column;padding-bottom:0">
      <div class="sidebar-title">Indexed Documents</div>
      <div class="doc-list" id="doc-list"></div>
    </div>
    <div class="upload-zone" onclick="document.getElementById('file-input').click()">
      οΌ‹ Upload document
    </div>
    <input type="file" id="file-input" accept=".pdf,.docx,.txt,.md,.markdown"
           onchange="uploadFile(this.files[0])">
  </div>

  <!-- Chat -->
  <div class="chat-area">
    <div class="messages" id="messages">
      <div class="empty-state" id="empty-state">
        <div class="icon">🧠</div>
        <strong>Ask anything about your documents</strong>
        <span>Add files via the sidebar, then start asking questions.</span>
      </div>
    </div>

    <div class="input-row">
      <textarea id="question-input"
                placeholder="Ask a question about your documents…"
                rows="1"
                onkeydown="handleKey(event)"></textarea>
      <button class="btn primary" id="send-btn" onclick="sendQuestion()">Ask β†’</button>
    </div>
    <div id="status-bar">Ready</div>
  </div>
</div>

<script>
// ── State ──────────────────────────────────────────────────────────────────
let isLoading = false;

// ── On load ────────────────────────────────────────────────────────────────
window.addEventListener('DOMContentLoaded', () => {
  loadStats();
  loadDocs();
  autoResize(document.getElementById('question-input'));
});

// ── Stats & docs ───────────────────────────────────────────────────────────
async function loadStats() {
  try {
    const r = await fetch('/api/stats');
    const s = await r.json();
    document.getElementById('stats-panel').innerHTML =
      `<strong>${s.total_chunks}</strong> chunks indexed<br>
       <strong>${s.indexed_files}</strong> documents<br>
       <span style="font-size:.7rem">${s.embed_model}</span>`;
  } catch(e) {
    document.getElementById('stats-panel').textContent = 'Error loading stats.';
  }
}

async function loadDocs() {
  try {
    const r = await fetch('/api/docs-list');
    const data = await r.json();
    const list = document.getElementById('doc-list');
    list.innerHTML = '';
    if (!data.documents.length) {
      list.innerHTML = '<div class="doc-item" style="font-style:italic">No documents yet</div>';
      return;
    }
    data.documents.forEach(doc => {
      const div = document.createElement('div');
      div.className = 'doc-item';
      div.innerHTML = `
        <span class="dot"></span>
        <span style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap"
              title="${doc.path}">${doc.name}</span>
        <span class="del" onclick="removeDoc('${doc.name}', this)" title="Remove">βœ•</span>`;
      list.appendChild(div);
    });
  } catch(e) {}
}

async function removeDoc(name, el) {
  if (!confirm(`Remove "${name}" from the knowledge base?`)) return;
  await fetch(`/api/doc/${encodeURIComponent(name)}`, { method: 'DELETE' });
  el.closest('.doc-item').remove();
  loadStats();
}

async function triggerIndex() {
  setStatus('Indexing documents…');
  try {
    const r = await fetch('/api/index', { method: 'POST',
      headers: {'Content-Type':'application/json'},
      body: JSON.stringify({ force: false })
    });
    const d = await r.json();
    setStatus(`Indexing complete β€” ${d.stats.total_chunks} chunks.`);
    loadStats(); loadDocs();
  } catch(e) { setStatus('Indexing failed.'); }
}

async function uploadFile(file) {
  if (!file) return;
  setStatus(`Uploading ${file.name}…`);
  const fd = new FormData();
  fd.append('file', file);
  try {
    const r = await fetch('/api/upload', { method: 'POST', body: fd });
    const d = await r.json();
    setStatus(d.message);
    loadStats(); loadDocs();
  } catch(e) { setStatus('Upload failed.'); }
  document.getElementById('file-input').value = '';
}

// ── Chat ───────────────────────────────────────────────────────────────────
function handleKey(e) {
  if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); sendQuestion(); }
  setTimeout(() => autoResize(e.target), 0);
}

function autoResize(el) {
  el.style.height = 'auto';
  el.style.height = Math.min(el.scrollHeight, 160) + 'px';
}

async function sendQuestion() {
  const input = document.getElementById('question-input');
  const question = input.value.trim();
  if (!question || isLoading) return;

  isLoading = true;
  input.value = '';
  input.style.height = '48px';
  document.getElementById('send-btn').disabled = true;
  document.getElementById('empty-state')?.remove();

  addMessage('user', question);
  const agentMsg = addMessage('agent', '', true);
  setStatus('Searching knowledge base…');

  // SSE streaming
  try {
    const url = `/api/stream?question=${encodeURIComponent(question)}&top_k=5`;
    const es = new EventSource(url);
    let buffer = '';

    es.onmessage = (e) => {
      if (e.data === '[DONE]') {
        es.close();
        finishMessage(agentMsg, buffer);
        isLoading = false;
        document.getElementById('send-btn').disabled = false;
        setStatus('Ready');
        return;
      }
      buffer += e.data;
      agentMsg.querySelector('.msg-text').textContent = buffer;
      scrollToBottom();
    };
    es.onerror = () => {
      es.close();
      if (!buffer) {
        agentMsg.querySelector('.msg-text').textContent =
          'Error: Could not reach the server.';
      }
      isLoading = false;
      document.getElementById('send-btn').disabled = false;
      setStatus('Error');
    };
  } catch(e) {
    agentMsg.querySelector('.msg-text').textContent = 'Unexpected error.';
    isLoading = false;
    document.getElementById('send-btn').disabled = false;
    setStatus('Error');
  }
}

function addMessage(role, text, loading = false) {
  const container = document.getElementById('messages');
  const div = document.createElement('div');
  div.className = `msg ${role}`;
  const label = role === 'user' ? 'You' : 'Agent';
  const avatarText = role === 'user' ? 'YOU' : 'KB';
  div.innerHTML = `
    <div class="msg-avatar">${avatarText}</div>
    <div class="msg-body">
      <div class="msg-label">${label}</div>
      <div class="msg-text">${loading ? '<div class="typing"><div class="dot"></div><div class="dot"></div><div class="dot"></div></div>' : escapeHtml(text)}</div>
    </div>`;
  container.appendChild(div);
  scrollToBottom();
  return div;
}

function finishMessage(div, text) {
  div.querySelector('.msg-text').textContent = text;
  scrollToBottom();
}

function scrollToBottom() {
  const c = document.getElementById('messages');
  c.scrollTop = c.scrollHeight;
}

function setStatus(msg) {
  document.getElementById('status-bar').textContent = msg;
}

function escapeHtml(s) {
  return s.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
}
</script>
</body>
</html>"""