import sys
import json
import re
import ast
import uuid
from pathlib import Path
import streamlit as st
sys.path.insert(0, str(Path(__file__).parent.parent))
from rag.pipeline import RAGProviderError, run_pipeline
from ingest.geo_metadata import geographic_metadata, normalize_country
sys.path.insert(0, str(Path(__file__).parent))
from sheets_logger import log_query
def _ensure_embedding_index_built():
"""Ensure precomputed_embeddings.json has texts+metadatas (needed by numpy retriever)."""
precomputed = Path("data/extracted/precomputed_embeddings.json")
needs_rebuild = True
if precomputed.exists():
import json as _json
pre = _json.loads(precomputed.read_text())
from ingest.embed_and_index import _content_hash, load_all_chunks
chunks = load_all_chunks()
texts = [c.get("text", _json.dumps(c)) for c in chunks]
ids = [f"{c.get('case_id', 'unknown')}_{c.get('chunk_index', i)}" for i, c in enumerate(chunks)]
from ingest.embed_and_index import build_metadatas
metadatas = build_metadatas(chunks)
content_hash = _content_hash(ids, texts, metadatas)
if (
pre.get("ids") == ids
and pre.get("texts") == texts
and pre.get("metadatas") == metadatas
and pre.get("content_hash") == content_hash
):
print(f"Embeddings ready ({len(pre['ids'])} chunks).")
needs_rebuild = False
if needs_rebuild:
print("Building embeddings index...")
from ingest.embed_and_index import embed_and_index
embed_and_index()
print("Embeddings index ready.")
QUESTIONS_PATH = Path("questionnaire/questions.json")
DATA_RAW = Path("data/raw")
HISTORY_DIR = Path(".streamlit_history")
MAX_HISTORY_ITEMS = 20
@st.cache_data
def load_case_meta() -> dict[str, dict]:
"""Load case metadata for all case studies with local case_meta.json files."""
meta = {}
if not DATA_RAW.exists():
return meta
for folder in DATA_RAW.iterdir():
m = __import__("re").match(r"^(CS\d+)", folder.name)
if m:
meta_path = folder / "case_meta.json"
if meta_path.exists():
cid = m.group(1)
meta[cid] = json.loads(meta_path.read_text())
return meta
def load_questions() -> list[dict]:
return json.loads(QUESTIONS_PATH.read_text())["questions"]
def _section_label(section: str) -> str:
label = section.replace("_", " ").title()
return label if len(label) < 60 else label[:57] + "..."
def _clean_excerpt(text: str, max_chars: int = 800) -> str:
text = re.sub(r'^Case study:.*?\n', '', text, flags=re.IGNORECASE)
text = re.sub(r'^Section:.*?\n', '', text, flags=re.IGNORECASE)
text = re.sub(r'^#{1,6}\s+', '', text, flags=re.MULTILINE)
text = re.sub(r'\*{1,2}([^*]+)\*{1,2}', r'\1', text)
text = re.sub(r'_{1,2}([^_]+)_{1,2}', r'\1', text)
text = re.sub(r'^[-*_]{3,}\s*$', '', text, flags=re.MULTILINE)
text = re.sub(r'\n{3,}', '\n\n', text).strip()
if len(text) > max_chars:
text = text[:max_chars].rsplit(' ', 1)[0] + ' ...'
return text
def _parse_source_links(raw) -> list[dict]:
if not raw:
return []
if isinstance(raw, list):
return [x for x in raw if isinstance(x, dict)]
if isinstance(raw, str):
for parser in (json.loads, ast.literal_eval):
try:
parsed = parser(raw)
except Exception:
continue
if isinstance(parsed, list):
return [x for x in parsed if isinstance(x, dict)]
return []
def _render_source_links(raw_links, limit: int = 5) -> None:
links = _parse_source_links(raw_links)
if not links:
return
st.markdown("**Paper/source links:**")
for link in links[:limit]:
title = link.get("title") or link.get("doi") or link.get("url") or "Source"
url = link.get("url") or (f"https://doi.org/{link['doi']}" if link.get("doi") else "")
if url:
st.markdown(f"- [{title}]({url})")
else:
st.markdown(f"- {title}")
if link.get("pdf_url"):
st.markdown(f" - [PDF]({link['pdf_url']})")
def _render_apa_source_links(raw_links, limit: int | None = None) -> None:
links = _parse_source_links(raw_links)
if not links:
st.caption("No source links available for this case yet.")
return
selected = links if limit is None else links[:limit]
for link in selected:
title = link.get("title") or link.get("doi") or link.get("url") or "Source"
url = link.get("url") or (f"https://doi.org/{link['doi']}" if link.get("doi") else "")
if url:
st.markdown(f"- [{title}]({url})")
else:
st.markdown(f"- {title}")
if link.get("pdf_url"):
st.markdown(f" - [PDF]({link['pdf_url']})")
def _render_navigation(current_page: str, conversation_id: str) -> None:
nav_items = [
("ask", "Ask"),
("how_to_use", "How to Use"),
("case_studies", "Case Studies"),
("previous_responses", "Previous Responses"),
("about", "About"),
]
st.markdown(
"""
""",
unsafe_allow_html=True,
)
cols = st.columns([3.9, 0.85, 1.25, 1.35, 1.75, 0.9])
cols[0].markdown('
RFR Knowledge Platform
', unsafe_allow_html=True)
for col, (page, label) in zip(cols[1:], nav_items):
if col.button(
label,
key=f"nav_{page}",
type="primary" if page == current_page else "secondary",
disabled=page == current_page,
):
_set_query_param("disclaimer_accepted", "true")
_set_query_param("conversation_id", conversation_id)
_set_query_param("page", page)
st.rerun()
st.markdown('', unsafe_allow_html=True)
def _case_sort_key(case_id: str) -> int:
m = re.search(r"\d+", case_id or "")
return int(m.group()) if m else 10_000
def _indexed_case_records() -> list[dict]:
extracted_dir = Path("data/extracted")
records = []
for f in sorted(extracted_dir.glob("CS*.json"), key=lambda p: _case_sort_key(p.stem)):
try:
chunks = json.loads(f.read_text())
except Exception:
continue
if not chunks:
continue
first = chunks[0]
cid = first.get("case_id", f.stem)
location = first.get("location", "")
raw_country = first.get("country", "")
geo = geographic_metadata(location, raw_country)
records.append({
"case_id": cid,
"name": load_case_meta().get(cid, {}).get("name", cid),
"location": location,
"country": normalize_country(raw_country),
"continent": first.get("continent") or geo["continent"],
"admin_area": first.get("admin_area") or geo["admin_area"],
})
return records
def _metadata_query_kind(query: str) -> str | None:
q = query.lower()
mentions_geo = re.search(
r"\b(continent|country|countries|region|area|province|state|location|europe|asia|africa|north america|south america|oceania)\b",
q,
)
mentions_cases = re.search(r"\bcase stud(?:y|ies)\b|\bcases\b", q)
asks_counts = re.search(r"\b(count|counts|frequency|frequencies|breakdown|how many|number of|table)\b", q)
asks_list = re.search(r"\b(list|show|which|what)\b", q)
asks_summarize = re.search(r"\b(summarize|summary|summaries)\b", q)
if mentions_cases and asks_counts and mentions_geo:
return "geo_counts"
if mentions_cases and asks_summarize and mentions_geo:
return "geo_summary"
if mentions_cases and asks_list and mentions_geo:
return "geo_list"
return None
def _markdown_table(headers: list[str], rows: list[list[str | int]]) -> str:
header = "| " + " | ".join(headers) + " |"
divider = "| " + " | ".join("---:" if h.lower() == "count" else "---" for h in headers) + " |"
body = ["| " + " | ".join(str(cell) for cell in row) + " |" for row in rows]
return "\n".join([header, divider, *body])
def _frequency_rows(records: list[dict], field: str) -> list[list[str | int]]:
counts = {}
for record in records:
value = record.get(field) or "Unknown"
counts[value] = counts.get(value, 0) + 1
return [[value, count] for value, count in sorted(counts.items(), key=lambda item: (-item[1], item[0]))]
def _requested_geo_value(query: str, records: list[dict]) -> tuple[str, str] | None:
q = query.lower()
fields = ("continent", "country", "admin_area")
for field in fields:
values = sorted({r.get(field, "") for r in records if r.get(field)}, key=len, reverse=True)
for value in values:
if value and value.lower() in q:
return field, value
return None
def _metadata_answer_markdown(query: str) -> str | None:
kind = _metadata_query_kind(query)
if not kind:
return None
records = _indexed_case_records()
if not records:
return "No indexed case studies were found."
q = query.lower()
wants_only_tables = "only include" in q and "table" in q
if kind == "geo_counts":
tables = []
if "continent" in q or "region" in q or "area" in q or "country" not in q:
tables.append(_markdown_table(["Continent", "Count"], _frequency_rows(records, "continent")))
if "country" in q or "countries" in q or "region" in q or "area" in q:
tables.append(_markdown_table(["Country", "Count"], _frequency_rows(records, "country")))
if "province" in q or "state" in q:
rows = [row for row in _frequency_rows(records, "admin_area") if row[0] != "Unknown"]
tables.append(_markdown_table(["State/Province/Area", "Count"], rows))
return "\n\n".join(tables)
requested = _requested_geo_value(query, records)
filtered = records
label = ""
if requested:
field, value = requested
filtered = [record for record in records if record.get(field) == value]
label = value
if kind == "geo_summary":
outside_count = len(records) - len(filtered) if requested else 0
rows = [
[record["case_id"], record["name"], record["location"], record["country"]]
for record in filtered
]
pieces = []
if not wants_only_tables:
pieces.append(f"### Case studies in {label}" if label else "### Matching case studies")
pieces.append(_markdown_table(["Case ID", "Name", "Location", "Country"], rows))
if requested:
pieces.append(_markdown_table([f"Outside {label}", "Count"], [[f"Outside {label}", outside_count]]))
return "\n\n".join(pieces)
if kind == "geo_list":
rows = [
[record["case_id"], record["name"], record["location"], record["country"], record["continent"]]
for record in filtered
]
return _markdown_table(["Case ID", "Name", "Location", "Country", "Continent"], rows)
return None
def _current_page() -> str:
page = _get_query_param("page") or "ask"
allowed = {"ask", "how_to_use", "case_studies", "previous_responses", "about"}
return page if page in allowed else "ask"
@st.cache_data
def _load_case_summary_chunks() -> list[dict]:
path = Path("data/extracted/case_summaries.json")
if not path.exists():
return []
try:
chunks = json.loads(path.read_text())
except Exception:
return []
return chunks if isinstance(chunks, list) else []
def _case_summary_records() -> list[dict]:
meta = load_case_meta()
records = []
for chunk in _load_case_summary_chunks():
cid = chunk.get("case_id", "")
case_meta = meta.get(cid, {})
records.append({
"case_id": cid,
"name": case_meta.get("name") or cid,
"location": chunk.get("location", ""),
"country": normalize_country(chunk.get("country", "")),
"continent": chunk.get("continent", ""),
"summary": _clean_excerpt(chunk.get("text", ""), max_chars=900),
"source_links": chunk.get("source_links"),
})
return sorted(records, key=lambda record: _case_sort_key(record["case_id"]))
def _render_case_studies_page() -> None:
st.title("Case Studies")
st.caption("Indexed managed retreat case studies with summary notes and paper/source links.")
records = _case_summary_records()
if not records:
st.warning("No case-study summaries were found. Run the ingest and summary chunk scripts first.")
return
countries = sorted({record["country"] for record in records if record["country"]})
continents = sorted({record["continent"] for record in records if record["continent"]})
filter_col, country_col = st.columns([1, 1])
with filter_col:
continent_filter = st.selectbox("Continent", ["All"] + continents)
with country_col:
country_filter = st.selectbox("Country", ["All"] + countries)
filtered = [
record for record in records
if (continent_filter == "All" or record["continent"] == continent_filter)
and (country_filter == "All" or record["country"] == country_filter)
]
st.caption(f"Showing {len(filtered)} of {len(records)} indexed case studies.")
for record in filtered:
with st.expander(f"{record['case_id']} — {record['name']} ({record['country']})", expanded=False):
st.markdown(f"**Location:** {record['location']}")
if record["continent"]:
st.markdown(f"**Continent:** {record['continent']}")
st.markdown("**Summary:**")
st.markdown(record["summary"])
st.markdown("**Sources:**")
_render_apa_source_links(record["source_links"], limit=None)
def _render_previous_responses_page(conversation_id: str, questions: list[dict]) -> None:
st.title("Previous Responses")
st.caption("Full responses saved for this browser conversation.")
history = _load_history(conversation_id)
if not history:
st.info("No previous responses are saved for this conversation yet.")
return
if st.button("Clear previous responses", type="secondary"):
st.session_state.history = []
_save_history(conversation_id, st.session_state.history)
st.rerun()
for i, entry in enumerate(history):
response_number = len(history) - i
st.markdown(f"## Response {response_number}")
st.markdown(f"**Question:** {entry.get('query', '')}")
context = entry.get("context") or {}
if context:
with st.expander("Context used", expanded=False):
for qid, val in context.items():
q_text = next((q["text"] for q in questions if q["id"] == qid), qid)
st.markdown(f"**{q_text}** {val}")
st.markdown(entry.get("answer", ""))
chunks = entry.get("chunks") or []
if chunks:
with st.expander("Case studies and sources used", expanded=False):
seen_ids = set()
for chunk in chunks:
metadata = chunk.get("metadata", {})
cid = metadata.get("case_id", "")
if cid in seen_ids:
continue
seen_ids.add(cid)
st.markdown(f"**{cid} — {metadata.get('source', '')}**")
_render_apa_source_links(metadata.get("source_links"), limit=5)
st.divider()
def _render_about_page() -> None:
st.title("About")
st.markdown(
"""
This AI-assisted knowledge platform supports exploration of managed retreat
case-study literature from the Retreat From Risk project by Anna Zhou from the University of Waterloo
The tool retrieves evidence from indexed case-study summaries and sections,
then generates a grounded response for planning, policy, and research use.
It is a research aid only and does not replace professional planning, legal,
engineering, or policy advice.
"""
)
def _render_how_to_use_page() -> None:
st.title("How to Use This Tool")
st.caption("Guidelines for asking stronger questions and reading the answers responsibly.")
st.markdown(
"""
### 1. Add context in the sidebar
The sidebar questions are optional, but they help tailor the answer to your situation.
Use them to describe who you are, the type of hazard you are thinking about, where your
community is in the managed retreat process, and what concerns matter most.
### 2. Ask one focused question at a time
Good questions usually name the topic you care about and the kind of answer you need.
**Examples:**
- What funding models have worked for floodplain buyout programs?
- How did communities maintain social cohesion after relocation?
- What barriers appeared in Indigenous or culturally distinct communities?
- Which cases involved strong public opposition, and how was it handled?
- What lessons from North American cases apply to municipal planners?
### 3. Use case IDs when you want a specific case
If you ask about a case ID, the tool retrieves that case directly.
**Examples:**
- Summarize CS42.
- What were the equity issues in CS41?
- Compare CS8 and CS28.
### 4. Use metadata questions for fast counts and lists
The tool answers geography and catalog questions directly from indexed metadata,
rather than asking the AI model to guess.
**Examples:**
- Give frequency tables by continent and country.
- Which case studies are in Europe?
- How many cases are outside North America?
- List all case studies in North Carolina.
### 5. Check the case studies and source links
Open the **Case Studies** tab to review every indexed case. Each case includes a short
summary and clickable paper/source links in APA-style labels where available.
### 6. Review previous answers
Open the **Previous Responses** tab to see the full answer history for your current
browser conversation. This is useful when comparing several questions or copying a
complete response with formatting.
### 7. Treat answers as research support
The responses are generated from managed retreat case-study literature and may contain
omissions or interpretation errors. Verify important claims against the cited papers or
source links before using an answer in planning, policy, legal, engineering, or public
engagement work.
"""
)
def _query_param_is_true(key: str) -> bool:
if hasattr(st, "query_params"):
value = st.query_params.get(key)
if isinstance(value, list):
value = value[0] if value else ""
return str(value).lower() in {"1", "true", "yes"}
values = st.experimental_get_query_params()
value = values.get(key, [""])
if isinstance(value, list):
value = value[0] if value else ""
return str(value).lower() in {"1", "true", "yes"}
def _set_query_param(key: str, value: str) -> None:
if hasattr(st, "query_params"):
st.query_params[key] = value
return
params = st.experimental_get_query_params()
params[key] = value
st.experimental_set_query_params(**params)
def _get_query_param(key: str) -> str:
if hasattr(st, "query_params"):
value = st.query_params.get(key, "")
if isinstance(value, list):
value = value[0] if value else ""
return str(value)
values = st.experimental_get_query_params()
value = values.get(key, [""])
if isinstance(value, list):
value = value[0] if value else ""
return str(value)
def _get_or_create_conversation_id() -> str:
existing = _get_query_param("conversation_id")
if re.fullmatch(r"[0-9a-f]{32}", existing or ""):
return existing
conversation_id = uuid.uuid4().hex
_set_query_param("conversation_id", conversation_id)
return conversation_id
def _history_path(conversation_id: str) -> Path:
HISTORY_DIR.mkdir(parents=True, exist_ok=True)
return HISTORY_DIR / f"{conversation_id}.json"
def _load_history(conversation_id: str) -> list[dict]:
path = _history_path(conversation_id)
if not path.exists():
return []
try:
history = json.loads(path.read_text())
except Exception:
return []
return history if isinstance(history, list) else []
def _save_history(conversation_id: str, history: list[dict]) -> None:
path = _history_path(conversation_id)
serializable_history = history[:MAX_HISTORY_ITEMS]
path.write_text(json.dumps(serializable_history, indent=2))
# ── Build embedding index before any Streamlit calls ──────────────────────────
_ensure_embedding_index_built()
# ── Page config ───────────────────────────────────────────────────────────────
st.set_page_config(page_title="AI-Assisted Knowledge Platform for Managed Retreat", layout="wide")
# ── Session state ─────────────────────────────────────────────────────────────
conversation_id = _get_or_create_conversation_id()
if "history" not in st.session_state:
st.session_state.history = _load_history(conversation_id)
if "disclaimer_accepted" not in st.session_state:
st.session_state.disclaimer_accepted = _query_param_is_true("disclaimer_accepted")
elif not st.session_state.disclaimer_accepted and _query_param_is_true("disclaimer_accepted"):
st.session_state.disclaimer_accepted = True
# ── Disclaimer modal ──────────────────────────────────────────────────────────
if not st.session_state.disclaimer_accepted:
st.markdown("""
AI-Assisted Knowledge Platform for Managed Retreat
Important Notice — Research Tool Disclaimer
This tool is intended to support informed decision-making by providing access to
synthesised insights from peer-reviewed managed retreat case study literature.
It is designed as a decision-support resource only.
This tool does not constitute professional planning, legal, engineering, or policy advice.
Responses are generated by an AI system and may contain errors, omissions, or
information that does not apply to your specific context. All outputs should be
critically evaluated and verified against primary sources before use in any
planning or policy process.
The responsibility for any decision remains entirely with the user and their organisation.
The researchers and institutions associated with the Retreat From Risk (RFR) project
accept no liability for decisions made on the basis of information provided by this tool.
By continuing, you acknowledge that you have read and understood this notice.
""", unsafe_allow_html=True)
if st.button("I understand — continue to the tool", type="primary"):
st.session_state.disclaimer_accepted = True
_set_query_param("disclaimer_accepted", "true")
st.rerun()
st.stop()
current_page = _current_page()
_render_navigation(current_page, conversation_id)
# ── Sidebar ───────────────────────────────────────────────────────────────────
with st.sidebar:
st.markdown('', unsafe_allow_html=True)
st.header("About You & Your Community")
st.caption("Optional context for tailoring answers. Skip any question that does not apply.")
questions = load_questions()
answers = {}
for q in questions:
answer = st.selectbox(q["text"], options=["(skip)"] + q["options"], key=q["id"])
if answer != "(skip)":
answers[q["id"]] = answer
st.divider()
answered = len(answers)
total = len(questions)
if answered == 0:
st.caption("No context provided — answers will be general.")
elif answered < total:
st.caption(f"{answered} of {total} questions answered.")
else:
st.caption("✓ Full context provided.")
model_choice = "claude"
if current_page == "how_to_use":
_render_how_to_use_page()
st.stop()
if current_page == "case_studies":
_render_case_studies_page()
st.stop()
if current_page == "previous_responses":
_render_previous_responses_page(conversation_id, questions)
st.stop()
if current_page == "about":
_render_about_page()
st.stop()
# ── Two-column layout ─────────────────────────────────────────────────────────
main_col, history_col = st.columns([2, 1], gap="large")
# ── Main panel ────────────────────────────────────────────────────────────────
with main_col:
st.title("AI-Assisted Knowledge Platform for Managed Retreat")
st.caption("Powered by RFR research — answers grounded in real managed retreat case studies from around the world.")
st.markdown('', unsafe_allow_html=True)
st.subheader("Ask a question")
st.markdown(
"Examples: *What funding models have worked for coastal buyout programs?* · "
"*How did communities maintain social cohesion after relocation?* · "
"*What are the biggest barriers to managed retreat in Indigenous communities?*"
)
st.caption(
"⚠️ This tool is a research aid only. Responses are AI-generated from case study literature "
"and may contain errors or omissions. They do not constitute professional advice. "
"All decisions remain the sole responsibility of the user."
)
query = st.text_area("Your question:", height=100, placeholder="Type your question about managed retreat here...")
if st.button("Get answer", type="primary"):
if not query.strip():
st.warning("Please enter a question.")
elif metadata_answer := _metadata_answer_markdown(query):
chunks = []
st.session_state.history.insert(0, {
"query": query,
"answer": metadata_answer,
"chunks": chunks,
"context": dict(answers),
})
st.session_state.history = st.session_state.history[:MAX_HISTORY_ITEMS]
_save_history(conversation_id, st.session_state.history)
st.markdown(metadata_answer)
elif re.search(r"\b(what|which|list|show|how many).{0,30}case stud", query, re.I):
# Meta question — list all case studies directly from extracted JSONs
cs_meta = load_case_meta()
extracted_dir = Path("data/extracted")
all_cases = []
for f in sorted(extracted_dir.glob("CS*.json"), key=lambda p: int(re.search(r'\d+', p.stem).group())):
cid = f.stem
if cid in cs_meta:
m = cs_meta[cid]
all_cases.append(f"**{cid}** — {m.get('name', cid)}, {m.get('location', '')}, {m.get('country', '')}")
else:
data = json.loads(f.read_text())
if data:
loc = data[0].get("location", "")
country = data[0].get("country", "")
all_cases.append(f"**{cid}** — {loc}, {country}" if loc else f"**{cid}**")
st.markdown("### Case Studies in the Knowledge Base")
for line in all_cases:
st.markdown(line)
st.caption(f"Total: {len(all_cases)} case studies indexed.")
chunks = []
else:
with st.spinner("Searching case studies and generating response..."):
try:
answer, chunks = run_pipeline(
query,
questionnaire_answers=answers if answers else None,
model=model_choice,
return_chunks=True,
)
except RAGProviderError as exc:
st.error(str(exc))
st.stop()
# Save to history
st.session_state.history.insert(0, {
"query": query,
"answer": answer,
"chunks": chunks,
"context": dict(answers),
})
st.session_state.history = st.session_state.history[:MAX_HISTORY_ITEMS]
_save_history(conversation_id, st.session_state.history)
# Log to Google Sheets
_log_err = log_query(query, answer, chunks, questionnaire_answers=answers, model=model_choice)
if _log_err:
st.caption(f"⚠️ Logging error (temporary debug): {_log_err}")
st.markdown("### Response")
st.caption(
"⚠️ The following response is AI-generated from managed retreat research literature. "
"It is intended to support — not replace — professional judgement. Verify findings "
"against primary sources before use in any planning or policy process."
)
st.markdown(answer.replace("$", r"\$"))
# ── Context used ──────────────────────────────────────────────
if answers:
with st.expander("Context used"):
for qid, val in answers.items():
q_text = next((q["text"] for q in questions if q["id"] == qid), qid)
st.markdown(f"**{q_text}** {val}")
# ── Case studies used ─────────────────────────────────────────
cs_meta = load_case_meta()
seen_ids: set[str] = set()
retrieved_cs = []
for c in chunks:
cid = c["metadata"].get("case_id", "")
if cid in cs_meta and cid not in seen_ids:
seen_ids.add(cid)
retrieved_cs.append(c)
mentioned_ids = {
f"CS{n}" for n in re.findall(r'\bCS(\d+)\b', answer)
}
if retrieved_cs:
st.markdown("---")
st.markdown('', unsafe_allow_html=True)
st.markdown("### Case Studies Used in This Answer")
st.caption(
"These are the case studies whose extracted content was retrieved "
"and sent to the model to generate the response above."
)
for chunk in retrieved_cs:
cid = chunk["metadata"]["case_id"]
meta = cs_meta[cid]
name = meta.get("name", "") or meta.get("location", cid)
country = meta.get("country", "")
section = _section_label(chunk["metadata"].get("section", ""))
cited = "✅ cited in response" if cid in mentioned_ids else "📄 used as context"
with st.expander(f"{cid} — {name}, {country} {cited}"):
st.markdown(f"**Section retrieved:** {section}")
st.markdown(f"**Source:** {chunk['metadata'].get('source', '')}")
_render_source_links(chunk["metadata"].get("source_links"))
excerpt = _clean_excerpt(chunk["text"])
if len(excerpt) > 40:
st.markdown("**Excerpt:**")
st.text(excerpt)
else:
st.caption("This section contains structural metadata only — no content preview available.")
# ── History panel (right column) ──────────────────────────────────────────────
with history_col:
st.markdown('', unsafe_allow_html=True)
st.subheader("Previous Responses")
if not st.session_state.history:
st.caption("Your previous answers will appear here.")
else:
if st.button("Clear history", type="secondary"):
st.session_state.history = []
_save_history(conversation_id, st.session_state.history)
st.rerun()
for i, entry in enumerate(st.session_state.history):
label = entry["query"][:60] + "..." if len(entry["query"]) > 60 else entry["query"]
with st.expander(f"Q{len(st.session_state.history) - i}: {label}"):
st.markdown(f"**Question:** {entry['query']}")
st.divider()
st.markdown(entry["answer"])
if entry["context"]:
st.divider()
st.caption("Context used:")
for qid, val in entry["context"].items():
q_text = next((q["text"] for q in questions if q["id"] == qid), qid)
st.caption(f"• {q_text}: {val}")