"""Case study viewer โ browse saved workflow conversations without API keys.""" import json from pathlib import Path import streamlit as st # Search paths for saved conversations and case-study-memory examples _APP_ROOT = Path(__file__).resolve().parent.parent # streamlit-client/ _PROJECT_ROOT = _APP_ROOT.parent # SciDER/ _CASE_STUDY_DIRS = [ _APP_ROOT / "saved_chats", # conversations saved by the app's own workflows _PROJECT_ROOT / "case-study-memory", # pre-bundled example case studies _APP_ROOT / "case-study-memory", ] def list_case_studies() -> list[tuple[Path, dict]]: """List all chat_history.json files in case-study-memory directories.""" results = [] seen = set() for base in _CASE_STUDY_DIRS: if not base.exists(): continue dirs = sorted( [d for d in base.iterdir() if d.is_dir() and (d / "chat_history.json").exists()], key=lambda x: x.stat().st_mtime, reverse=True, ) for memo_dir in dirs: chat_file = memo_dir / "chat_history.json" key = str(chat_file.resolve()) if key in seen: continue seen.add(key) try: with open(chat_file, encoding="utf-8") as f: data = json.load(f) ts = data.get("timestamp", "")[:19].replace("T", " ") wf = data.get("workflow_type", "unknown") query = data.get("metadata", {}).get("query", "") label = (query[:80] + "...") if len(query) > 83 else (query or memo_dir.name) results.append((chat_file, {"label": label, "timestamp": ts, "workflow_type": wf})) except Exception: results.append( ( chat_file, {"label": memo_dir.name, "timestamp": "", "workflow_type": "unknown"}, ) ) return results _WORKFLOW_LABELS = { "ideation": "Ideation", "data": "Data Analysis", "experiment": "Experiment", "full": "Full Pipeline", } def render_case_study_viewer(): """Render the case study browser UI.""" st.markdown( """
{query}
{wf_label} ยท {ts}