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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e63c592
 
 
 
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
import json
import os
from datetime import datetime
from pathlib import Path
from typing import Any, Dict, Generator, List, Optional, Tuple

import httpx
import streamlit as st

from services.backend_client import post_upload_text
from services.file_convert import convert_uploaded_file_to_text


def get_backend_base_url() -> str:
    """Prefer Streamlit secrets, then environment variable, then localhost."""
    if "BACKEND_BASE_URL" in st.secrets:
        base_url = st.secrets["BACKEND_BASE_URL"]
    else:
        base_url = os.getenv("BACKEND_BASE_URL", "http://localhost:8000")
    return str(base_url).rstrip("/")


def get_api_key() -> Optional[str]:
    """Read API key from Streamlit secrets or environment."""
    if "API_KEY" in st.secrets:
        return str(st.secrets["API_KEY"])
    return os.getenv("API_KEY")


def ping_health(base_url: str, api_key: Optional[str]) -> Dict[str, Any]:
    url = f"{base_url}/health"
    headers: Dict[str, str] = {}
    if api_key:
        headers["X-API-Key"] = api_key
    resp = httpx.get(url, headers=headers, timeout=10.0)
    resp.raise_for_status()
    return resp.json()


def call_chat(
    base_url: str,
    api_key: str,
    payload: Dict[str, Any],
) -> Dict[str, Any]:
    url = f"{base_url}/chat"
    headers: Dict[str, str] = {"Content-Type": "application/json", "X-API-Key": api_key}
    resp = httpx.post(url, json=payload, headers=headers, timeout=60.0)
    resp.raise_for_status()
    return resp.json()


def iter_chat_stream(
    base_url: str,
    api_key: str,
    payload: Dict[str, Any],
) -> Generator[Tuple[str, Optional[Dict[str, Any]]], None, None]:
    """Stream tokens from /chat/stream and yield (partial_answer, final_payload).

    The final_payload is None for intermediate updates and populated once
    when the terminating SSE event is received.
    """
    url = f"{base_url}/chat/stream"
    headers: Dict[str, str] = {"Content-Type": "application/json", "X-API-Key": api_key}

    full_answer = ""
    final_payload: Optional[Dict[str, Any]] = None
    current_event: Optional[str] = None

    with httpx.Client(timeout=60.0) as client:
        with client.stream("POST", url, json=payload, headers=headers) as resp:
            resp.raise_for_status()
            for line in resp.iter_lines():
                if not line:
                    continue

                if line.startswith("event:"):
                    current_event = line.split(":", 1)[1].strip()
                    continue

                if line.startswith("data:"):
                    data = line.split(":", 1)[1].lstrip()
                    if current_event == "end":
                        # Final payload with full JSON response.
                        try:
                            final_payload = json.loads(data)
                        except json.JSONDecodeError:
                            final_payload = None
                    else:
                        if data:
                            if full_answer:
                                full_answer += " "
                            full_answer += data
                            # Yield intermediate answer text.
                            yield full_answer, None

    # After stream ends, make sure we yield at least once with final payload.
    if final_payload is not None:
        # If the backend included the final answer in the JSON payload, prefer it.
        answer_text = str(final_payload.get("answer") or full_answer)
        yield answer_text, final_payload
    elif full_answer:
        yield full_answer, None


def init_session_state() -> None:
    if "messages" not in st.session_state:
        st.session_state.messages: List[Dict[str, Any]] = []
    if "show_sources" not in st.session_state:
        st.session_state.show_sources = True
    if "supports_stream" not in st.session_state:
        st.session_state.supports_stream = True
    # Namespace is fixed for now; default to "dev".
    if "namespace" not in st.session_state:
        st.session_state.namespace = "dev"
    if "recent_uploads" not in st.session_state:
        st.session_state.recent_uploads: List[Dict[str, Any]] = []
    if "chat_prefill" not in st.session_state:
        st.session_state.chat_prefill = None


def render_sidebar(backend_base_url: str, api_key: Optional[str]) -> Dict[str, Any]:
    with st.sidebar:
        st.header("Backend")

        st.markdown(f"**Backend URL:** `{backend_base_url}`")
        if api_key:
            st.markdown("**API key:** configured in Streamlit secrets or environment.")
        else:
            st.warning(
                "API_KEY is not configured. The backend is expected to be protected; "
                "chat will be disabled until an API key is set."
            )

        if st.button("Ping /health"):
            try:
                health = ping_health(backend_base_url, api_key)
                st.success("Backend reachable.")
                st.json(health)
            except Exception as exc:  # noqa: BLE001
                st.error(f"Health check failed: {exc}")

        st.markdown("---")
        st.subheader("Chat settings")

        top_k = st.slider("Top K", min_value=1, max_value=20, value=5, step=1)
        min_score = st.slider(
            "Minimum relevance score",
            min_value=0.0,
            max_value=1.0,
            value=0.25,
            step=0.05,
        )
        use_web_fallback = st.checkbox(
            "Use web fallback (Tavily)",
            value=True,
            help="When enabled, /chat may call Tavily if retrieval is weak.",
        )

        st.session_state.show_sources = st.checkbox(
            "Show sources", value=st.session_state.show_sources
        )

        if st.button("Clear chat"):
            st.session_state.messages = []

        st.markdown("---")
        st.subheader("Recent uploads")
        recent = st.session_state.get("recent_uploads", [])
        if not recent:
            st.caption("No documents uploaded yet.")
        else:
            for idx, item in enumerate(recent):
                title = item.get("title") or "Untitled"
                ns = item.get("namespace") or st.session_state.get("namespace", "dev")
                ts = item.get("timestamp", "")
                st.markdown(f"- **{title}**  \n  Namespace: `{ns}`  \n  Uploaded: {ts}")
                if st.button("Search this document", key=f"search_upload_{idx}"):
                    st.session_state.chat_prefill = f"Summarize: {title}"

    return {
        "top_k": top_k,
        "min_score": float(min_score),
        "use_web_fallback": bool(use_web_fallback),
    }


def render_chat_history(show_sources: bool) -> None:
    for message in st.session_state.messages:
        role = message.get("role", "user")
        content = message.get("content", "")
        with st.chat_message("assistant" if role == "assistant" else "user"):
            st.markdown(content)
            if role == "assistant" and show_sources:
                sources = message.get("sources") or []
                if sources:
                    with st.expander("Sources", expanded=False):
                        for idx, src in enumerate(sources, start=1):
                            title = src.get("title") or f"Source {idx}"
                            url = src.get("url") or ""
                            score = src.get("score", 0.0)
                            st.markdown(f"**[{idx}] {title}** (score={score:.3f})")
                            if url:
                                st.markdown(f"- URL: {url}")
                            chunk_text = src.get("chunk_text") or ""
                            if chunk_text:
                                st.write(chunk_text[:1000] + ("..." if len(chunk_text) > 1000 else ""))


@st.dialog("Upload document")
def upload_dialog(backend_base_url: str, api_key: Optional[str]) -> None:
    """Modal dialog for uploading and ingesting a document via /documents/upload-text."""
    st.write("Upload a document to ingest it into the RAG backend.")

    with st.form("upload_form"):
        uploaded_file = st.file_uploader(
            "Choose a file",
            type=["pdf", "md", "txt", "docx", "pptx", "xlsx", "html", "htm"],
            accept_multiple_files=False,
        )

        default_title = ""
        if uploaded_file is not None:
            default_title = Path(uploaded_file.name).stem

        title = st.text_input("Title", value=default_title)
        namespace = st.text_input(
            "Namespace",
            value=st.session_state.get("namespace", "dev"),
            help="Target Pinecone namespace.",
        )
        source = st.text_input("Source label", value="ui-upload")
        tags = st.text_input("Tags (comma separated)", value="")
        notes = st.text_area("Notes", value="", height=80)

        upload_anyway = st.checkbox(
            "Upload even if extracted text is very short",
            value=False,
            help="Enable to upload even when the extracted text is shorter than 200 characters.",
        )

        submit = st.form_submit_button("Upload")
    if not submit:
        return

    if uploaded_file is None:
        st.error("Please select a file to upload.")
        return

    if not title.strip():
        st.error("Please provide a title.")
        return

    if not api_key:
        st.error("API_KEY is not configured; cannot upload to a protected backend.")
        return

    with st.spinner("Converting and uploading document..."):
        try:
            uploaded_file.seek(0)
            text, conv_meta = convert_uploaded_file_to_text(uploaded_file)
        except Exception as exc:  # noqa: BLE001
            st.error(f"Error converting file: {exc}")
            return

        if len(text.strip()) < 200 and not upload_anyway:
            st.warning(
                "Extracted text is very short (< 200 characters). "
                "Check the file or enable the checkbox to upload anyway."
            )
            return

        meta: Dict[str, Any] = {
            **conv_meta,
            "tags": [t.strip() for t in tags.split(",") if t.strip()],
            "notes": notes,
        }

        payload = {
            "title": title.strip(),
            "source": source.strip() or "ui-upload",
            "text": text,
            "namespace": namespace.strip() or st.session_state.get("namespace", "dev"),
            "metadata": meta,
        }

        try:
            response = post_upload_text(backend_base_url, api_key, payload)
        except httpx.HTTPStatusError as exc:
            if exc.response is not None:
                detail = exc.response.text
                status_code = exc.response.status_code
            else:
                detail = str(exc)
                status_code = "error"
            st.error(f"Upload failed ({status_code}): {detail}")
            return
        except Exception as exc:  # noqa: BLE001
            st.error(f"Upload failed: {exc}")
            return

        # Record recent upload and suggest a follow-up chat action.
        rec = {
            "title": title.strip(),
            "namespace": payload["namespace"],
            "timestamp": datetime.utcnow().isoformat() + "Z",
            "response": response,
        }
        recent = st.session_state.get("recent_uploads", [])
        recent.append(rec)
        st.session_state.recent_uploads = recent[-5:]

        st.success(f"Uploaded and indexed: {title.strip()}")
        st.rerun()


def main() -> None:
    st.set_page_config(page_title="RAG Agent Workbench", layout="wide")
    st.title("RAG Agent Workbench – Chatbot")

    init_session_state()

    backend_base_url = get_backend_base_url()
    api_key = get_api_key()

    # Upload button near the top-level chat UI.
    if st.button("📄 Upload Document"):
        upload_dialog(backend_base_url, api_key)

    settings = render_sidebar(backend_base_url, api_key)
    render_chat_history(show_sources=st.session_state.show_sources)

    if not api_key:
        st.info(
            "Configure `API_KEY` in Streamlit secrets (and on the backend) to start chatting."
        )
        return

    # Pre-fill chat input if a suggestion was set (e.g. from recent uploads).
    prefill = st.session_state.get("chat_prefill")
    if prefill and "chat_input" not in st.session_state:
        st.session_state.chat_input = prefill

    user_message = st.chat_input(
        "Ask a question about your documents...", key="chat_input"
    )
    if not user_message:
        return

    # Clear any prefill once the user has sent a message.
    st.session_state.chat_prefill = None

    # Record and display user message
    st.session_state.messages.append({"role": "user", "content": user_message})
    with st.chat_message("user"):
        st.markdown(user_message)

    # Prepare payload for backend
    chat_history = [
        {"role": msg["role"], "content": msg["content"]}
        for msg in st.session_state.messages
        if msg.get("role") in ("user", "assistant")
    ]
    payload: Dict[str, Any] = {
        "query": user_message,
        "namespace": st.session_state.namespace,
        "top_k": int(settings["top_k"]),
        "use_web_fallback": settings["use_web_fallback"],
        "min_score": float(settings["min_score"]),
        "max_web_results": 5,
        "chat_history": chat_history,
    }

    # Call backend and stream / display assistant response
    with st.chat_message("assistant"):
        placeholder = st.empty()
        placeholder.markdown("_Thinking..._")

        response: Optional[Dict[str, Any]] = None

        try:
            if st.session_state.get("supports_stream", True):
                try:
                    # Attempt to use streaming endpoint first.
                    for partial_answer, final_payload in iter_chat_stream(
                        backend_base_url,
                        api_key,
                        payload,
                    ):
                        if partial_answer:
                            placeholder.markdown(partial_answer)
                        if final_payload is not None:
                            response = final_payload
                            break
                except httpx.HTTPStatusError as exc:
                    # If /chat/stream is not available, fall back to /chat.
                    if exc.response is not None and exc.response.status_code == 404:
                        st.session_state.supports_stream = False
                    else:
                        raise

            if response is None:
                # Fallback to non-streaming /chat.
                response = call_chat(backend_base_url, api_key, payload)
                answer_text = str(response.get("answer") or "")
                if answer_text:
                    placeholder.markdown(answer_text)
                else:
                    placeholder.markdown("_No answer returned._")

        except Exception as exc:  # noqa: BLE001
            placeholder.markdown("")
            st.error(f"Error calling backend: {exc}")
            return

        if not response:
            return

        answer = str(response.get("answer") or "")
        sources = response.get("sources") or []
        timings = response.get("timings") or {}

        # Optionally render sources for this assistant turn.
        if st.session_state.show_sources and sources:
            with st.expander("Sources", expanded=False):
                for idx, src in enumerate(sources, start=1):
                    title = src.get("title") or f"Source {idx}"
                    url = src.get("url") or ""
                    score = src.get("score", 0.0)
                    st.markdown(f"**[{idx}] {title}** (score={score:.3f})")
                    if url:
                        st.markdown(f"- URL: {url}")
                    chunk_text = src.get("chunk_text") or ""
                    if chunk_text:
                        st.write(chunk_text[:1000] + ("..." if len(chunk_text) > 1000 else ""))

        # Persist assistant message with metadata.
        st.session_state.messages.append(
            {
                "role": "assistant",
                "content": answer,
                "sources": sources,
                "timings": timings,
            }
        )


if __name__ == "__main__":
    main()