"""Main chat interface page for ChemGraph.""" import asyncio import html import json import logging import os import pprint import queue import re import threading import uuid from datetime import datetime from pathlib import Path from typing import Any, Dict, Optional import pandas as pd import streamlit as st from ase.io import read as ase_read from chemgraph.agent.llm_agent import HumanInputRequired from chemgraph.memory.schemas import SessionMessage from chemgraph.memory.store import SessionStore from chemgraph.models.supported_models import supported_argo_models from chemgraph.schemas.ase_input import ( get_available_calculator_names, get_default_calculator_name, ) from chemgraph.utils.config_utils import ( get_argo_user_from_nested_config, get_base_url_for_model_from_nested_config, ) from chemgraph.utils.tool_mapping import ( format_validation_failure_message, validate_completion, ) from ui.agent_manager import initialize_agent from ui.branding import LOGO_IMAGES, first_existing_asset from ui.config import load_config from ui.endpoint import check_local_model_endpoint from ui.file_utils import ( extract_log_dir_from_messages, find_latest_xyz_file_in_dir, ) from ui.message_utils import ( extract_messages_from_result, extract_molecular_structure, extract_xyz_from_report_html, find_html_filename, find_structure_in_messages, has_structure_signal, is_infrared_requested, normalize_latex_delimiters, normalize_message_content, split_markdown_latex_blocks, strip_viewer_from_report_html, ) from ui.scientific_reminders import ( QUERY_SPECIFICATION_HINT, render_chat_scientific_reminder, ) from ui.session_utils import ( conversation_entry_to_messages, generate_session_id, messages_from_result, session_to_conversation_history, ) from ui.state import init_session_state from ui.visualization import ( STMOL_AVAILABLE, display_molecular_structure, visualize_trajectory, ) # Re-use the constants from the configuration page from ui._pages.configuration import normalize_workflow_name logger = logging.getLogger(__name__) _AGENT_RUN_LOCK = threading.Lock() # --------------------------------------------------------------------------- # Thin wrappers around config utilities # --------------------------------------------------------------------------- def _get_base_url_for_model(model_name: str, config: Dict[str, Any]) -> Optional[str]: """Resolve the configured base URL for a model. Parameters ---------- model_name : str Selected model identifier. config : dict[str, Any] Nested UI configuration. Returns ------- str or None Provider base URL, or ``None`` when not configured. """ return get_base_url_for_model_from_nested_config(model_name, config) def _initial_ui_log_root() -> str: """Return the root directory for per-chat UI artifacts.""" env_log_dir = os.environ.get("CHEMGRAPH_LOG_DIR") if env_log_dir: return str(_ui_log_root_from_path(Path(env_log_dir).expanduser())) return str((Path.cwd() / "cg_logs").resolve()) def _ui_log_root_from_path(path: Path) -> Path: """Return the chat-log root even when given a stale session/query path.""" current = path.resolve() while current.name.startswith(("query_", "session_", "ui_session_")): current = current.parent return current def _ensure_chat_log_dir() -> str: """Create and activate a log directory owned by the current chat.""" if st.session_state.get("ui_log_root"): st.session_state.ui_log_root = str( _ui_log_root_from_path(Path(st.session_state.ui_log_root).expanduser()) ) else: st.session_state.ui_log_root = _initial_ui_log_root() chat_log_dir = st.session_state.get("current_chat_log_dir") if chat_log_dir: chat_path = Path(chat_log_dir).expanduser().resolve() root_path = Path(st.session_state.ui_log_root).expanduser().resolve() if chat_path.parent != root_path: st.session_state.current_chat_log_dir = None chat_log_dir = None if not chat_log_dir: timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S") suffix = str(uuid.uuid4())[:8] chat_log_dir = str( Path(st.session_state.ui_log_root) / f"ui_session_{timestamp}_{suffix}" ) st.session_state.current_chat_log_dir = chat_log_dir os.makedirs(chat_log_dir, exist_ok=True) os.environ["CHEMGRAPH_LOG_DIR"] = chat_log_dir return chat_log_dir def _create_query_log_dir() -> str: """Create a per-query artifact directory under the active chat directory.""" chat_log_dir = _ensure_chat_log_dir() query_idx = len(st.session_state.get("conversation_history", [])) + 1 suffix = str(uuid.uuid4())[:8] query_log_dir = str(Path(chat_log_dir) / f"query_{query_idx:03d}_{suffix}") os.makedirs(query_log_dir, exist_ok=True) st.session_state.active_query_log_dir = query_log_dir return query_log_dir def _begin_agent_run(*, from_pending: bool = False) -> bool: """Mark an agent run as active if no other run is in progress.""" if st.session_state.get("agent_running") and not from_pending: return False if st.session_state.get("agent_run_lock_acquired"): return True if _AGENT_RUN_LOCK.locked(): return False acquired = _AGENT_RUN_LOCK.acquire(blocking=False) if not acquired: return False st.session_state.agent_running = True st.session_state.agent_run_lock_acquired = True return True def _agent_run_active() -> bool: """Return whether any Streamlit session has an active workflow run.""" lock_active = _AGENT_RUN_LOCK.locked() has_pending_submission = st.session_state.get("pending_agent_submission") is not None if ( st.session_state.get("agent_running") and not lock_active and not has_pending_submission ): # A browser refresh creates a fresh Streamlit session_state while the # process lock is the authoritative cross-session signal. st.session_state.agent_running = False st.session_state.agent_run_lock_acquired = False return bool(lock_active or st.session_state.get("agent_running")) def _end_agent_run() -> None: """Clear the active-run marker and release the process-local run lock.""" st.session_state.agent_running = False st.session_state.active_query_log_dir = None if st.session_state.get("agent_run_lock_acquired"): st.session_state.agent_run_lock_acquired = False try: _AGENT_RUN_LOCK.release() except RuntimeError: pass def _session_id_from_url() -> Optional[str]: """Read the session id from the URL query string when present.""" try: value = st.query_params.get("session") except Exception: return None if isinstance(value, list): return str(value[0]) if value else None return str(value) if value else None def _set_session_id_in_url(session_id: Optional[str]) -> None: """Keep the active session id in the browser URL for refresh recovery.""" try: if session_id: st.query_params["session"] = session_id else: st.query_params.pop("session", None) except Exception: pass def _restore_agent_log_context(agent, old_agent_log_dir, old_env_log_dir) -> None: """Restore agent/env log state after a per-query run.""" if agent is not None: agent.log_dir = old_agent_log_dir if st.session_state.get("current_chat_log_dir"): os.environ["CHEMGRAPH_LOG_DIR"] = st.session_state.current_chat_log_dir elif old_env_log_dir is not None: old_path = Path(old_env_log_dir).expanduser() if old_path.name.startswith("query_"): os.environ["CHEMGRAPH_LOG_DIR"] = str(_ui_log_root_from_path(old_path)) else: os.environ["CHEMGRAPH_LOG_DIR"] = old_env_log_dir else: os.environ.pop("CHEMGRAPH_LOG_DIR", None) def _resolve_structured_output_for_model( model_name: str, structured_output: bool ) -> tuple[bool, Optional[str]]: """Disable structured output for Argo models, including quick overrides. Parameters ---------- model_name : str Selected model identifier. structured_output : bool Requested structured-output setting. Returns ------- tuple[bool, str | None] Effective structured-output setting and optional warning message. """ if model_name in supported_argo_models and structured_output: return ( False, "Structured output is disabled for Argo models to avoid JSON parsing errors.", ) return structured_output, None # --------------------------------------------------------------------------- # Page entry point # --------------------------------------------------------------------------- def render() -> None: """Render the Main Interface page.""" init_session_state() _restore_session_from_url() config = st.session_state.config selected_model = config["general"]["model"] selected_workflow = normalize_workflow_name(config["general"]["workflow"]) selected_output = config["general"]["output"] structured_output = config["general"]["structured"] generate_report = config["general"]["report"] human_supervised = config["general"].get("human_supervised", False) thread_id = config["general"]["thread"] # ----- Header ----- logo_image = first_existing_asset(LOGO_IMAGES) if logo_image: st.image(logo_image, width=320) else: st.title("\U0001f9ea ChemGraph") st.markdown(""" ChemGraph enables you to perform various **computational chemistry** tasks with natural-language queries using AI agents. """) # ----- Calculator availability sidebar ----- _render_available_calculators_sidebar() _render_chat_controls() structured_output, ui_notice = _resolve_structured_output_for_model( selected_model, structured_output ) st.session_state.ui_notice = ui_notice st.session_state.active_model = selected_model st.session_state.active_workflow = selected_workflow if ui_notice: st.info(ui_notice) render_chat_scientific_reminder() selected_base_url = _get_base_url_for_model(selected_model, config) endpoint_status = check_local_model_endpoint(selected_base_url) # ----- Session management sidebar ----- _render_session_sidebar() # Reload config button if st.sidebar.button("\U0001f504 Reload Config", disabled=_agent_run_active()): st.session_state.config = load_config() st.success("\u2705 Configuration reloaded!") st.rerun() # ----- Auto-initialize agent ----- _auto_initialize_agent( config, selected_model, selected_workflow, structured_output, selected_output, generate_report, human_supervised, selected_base_url, ) # ----- Agent status sidebar ----- _render_agent_status(selected_model, selected_workflow, thread_id, endpoint_status) # ----- Restore pending clarification from the visible history ----- _restore_pending_interrupt_from_history() # ----- Conversation history ----- _render_conversation_history(thread_id) # ----- Pending interrupt display ----- _render_pending_interrupt() # ----- Example queries ----- _render_example_queries(config, selected_model) # ----- Chat input (handles both normal queries and interrupt responses) ----- is_interrupt = st.session_state.pending_human_question is not None agent_busy = _agent_run_active() if agent_busy: st.info("Agent is running. Please wait for the current task to finish.") prompt = st.chat_input( ( "Type your response..." if is_interrupt else "Ask: molecule/reaction + property + calculator + conditions..." ), disabled=agent_busy, ) # Check for example query submitted via button click if agent_busy: st.session_state.pop("_pending_example_query", None) prompt = None else: example_query = st.session_state.pop("_pending_example_query", None) if example_query: prompt = example_query if prompt: if is_interrupt: _handle_human_response(prompt, thread_id) else: _queue_query_submission( prompt, thread_id, endpoint_status, selected_base_url ) _execute_pending_agent_submission(thread_id, endpoint_status, selected_base_url) # --------------------------------------------------------------------------- # Internal renderers # --------------------------------------------------------------------------- def _render_markdown_with_math(text: str) -> None: """Render Markdown text, sending display math blocks through ``st.latex``. Parameters ---------- text : str Markdown text that may contain display math blocks. """ for block_type, content in split_markdown_latex_blocks(text): if block_type == "latex": st.latex(_prepare_latex_block(content)) else: st.markdown(content) def _prepare_latex_block(content: str) -> str: """Clean display math for Streamlit's KaTeX renderer. Parameters ---------- content : str Raw LaTeX block content. Returns ------- str KaTeX-compatible display math. """ lines = [line.strip() for line in content.splitlines() if line.strip()] if not lines: return "" if len(lines) == 1 or r"\begin{" in content: return " ".join(lines) return "\\begin{aligned}\n" + (r" \\" + "\n").join(lines) + "\n\\end{aligned}" def _format_calculator_label(calculator_name: str) -> str: """Format calculator class names for display. Parameters ---------- calculator_name : str Calculator class name or label. Returns ------- str Human-readable calculator label. """ label = calculator_name.removesuffix("Calc") if label == "TBLite": return "TBLite (xTB, GFN1-xTB, GFN2-xTB)" return label def _render_available_calculators_sidebar() -> None: """Render the calculators detected during ChemGraph initialization.""" available = get_available_calculator_names() default = get_default_calculator_name() with st.sidebar.expander("\U0001f9ee Available Calculators", expanded=True): st.caption("Detected during ChemGraph initialization.") for calculator_name in available: label = _format_calculator_label(calculator_name) if calculator_name == default: st.success(f"{label} (default)") else: st.markdown(f"- {label}") st.caption( "The agent uses this list when choosing calculators for ASE simulations. " "Availability does not imply suitability; verify calculator domain, " "charge/spin, and convergence settings for your system." ) def _start_new_chat() -> None: """Reset conversation state for a fresh chat session.""" if st.session_state.get("agent_run_lock_acquired"): try: _AGENT_RUN_LOCK.release() except RuntimeError: pass st.session_state.conversation_history.clear() st.session_state.current_session_id = None st.session_state.session_created = False st.session_state.query_input = "" st.session_state.last_run_error = None st.session_state.last_run_result = None st.session_state.last_run_query = None st.session_state.pop("_pending_example_query", None) st.session_state.agent = None st.session_state.last_config = None st.session_state.current_chat_log_dir = None st.session_state.active_query_log_dir = None st.session_state.active_graph_thread_id = None st.session_state.agent_running = False st.session_state.agent_run_lock_acquired = False st.session_state.pending_agent_submission = None _set_session_id_in_url(None) os.environ.pop("CHEMGRAPH_LOG_DIR", None) _clear_interrupt_state() def _ensure_graph_thread_id(thread_id: int) -> str: """Return a stable LangGraph thread ID for the current chat. The thread must persist across follow-up questions inside one chat so the agent can resolve references like "its dipole moment". It must also change when the user starts a new chat so old molecule/tool context does not leak into an unrelated task. """ active_id = st.session_state.get("active_graph_thread_id") if not active_id: active_id = f"{thread_id}-{uuid.uuid4().hex[:8]}" st.session_state.active_graph_thread_id = active_id return active_id def _render_chat_controls() -> None: """Render chat-level actions that must be available even without memory.""" if st.sidebar.button( "New Chat", key="new_chat_btn", use_container_width=True, disabled=_agent_run_active(), ): _start_new_chat() st.rerun() def _render_session_sidebar() -> None: """Render the session management panel in the sidebar.""" store: Optional[SessionStore] = st.session_state.get("session_store") if store is None: return with st.sidebar.expander("\U0001f4c2 Sessions", expanded=False): # Show current session info current_sid = st.session_state.get("current_session_id") if current_sid: st.caption(f"Active session: `{current_sid}`") # List recent sessions try: sessions = store.list_sessions(limit=10) except Exception: sessions = [] if not sessions: st.caption("No saved sessions yet.") return st.markdown("**Recent sessions:**") for s in sessions: # Highlight the active session is_active = current_sid and s.session_id == current_sid prefix = "\u25b6 " if is_active else "" label = s.title or "Untitled" if len(label) > 35: label = label[:32] + "..." col_load, col_del = st.columns([4, 1]) with col_load: if st.button( f"{prefix}{label}", key=f"load_session_{s.session_id}", use_container_width=True, help=( f"Model: {s.model_name} | " f"Queries: {s.query_count} | " f"{s.updated_at.strftime('%Y-%m-%d %H:%M')}" ), ): _load_session(s.session_id) st.rerun() with col_del: if st.button( "\U0001f5d1", key=f"del_session_{s.session_id}", help="Delete this session", ): try: store.delete_session(s.session_id) # If we just deleted the active session, reset if current_sid == s.session_id: _start_new_chat() except Exception as exc: logger.warning( "Failed to delete session %s: %s", s.session_id, exc ) st.rerun() def _load_session(session_id: str) -> None: """Load a stored session into the active conversation. Parameters ---------- session_id : str Session ID or prefix selected in the sidebar. """ store: Optional[SessionStore] = st.session_state.get("session_store") if store is None: return session = store.get_session(session_id) if session is None: st.sidebar.error(f"Session '{session_id}' not found.") return # Rebuild conversation_history from stored messages st.session_state.conversation_history = session_to_conversation_history(session) st.session_state.current_session_id = session.session_id st.session_state.session_created = True st.session_state.query_input = "" st.session_state.last_run_error = None st.session_state.last_run_result = None st.session_state.last_run_query = None st.session_state.current_chat_log_dir = session.log_dir st.session_state.active_graph_thread_id = f"session-{session.session_id}" _set_session_id_in_url(session.session_id) if session.log_dir: os.environ["CHEMGRAPH_LOG_DIR"] = session.log_dir def _restore_session_from_url() -> None: """Restore the visible chat after a browser refresh using URL state.""" if st.session_state.get("current_session_id"): return session_id = _session_id_from_url() if not session_id: return try: _load_session(session_id) except Exception as exc: logger.warning("Failed to restore session %s from URL: %s", session_id, exc) def _active_session_metadata() -> tuple[str, str]: """Return model/workflow metadata matching the active UI run.""" config = st.session_state.config model = ( st.session_state.get("pending_interrupt_model") or st.session_state.get("active_model") or config["general"]["model"] ) workflow = ( st.session_state.get("pending_interrupt_workflow") or st.session_state.get("active_workflow") or config["general"]["workflow"] ) return model, normalize_workflow_name(workflow) def _ensure_ui_session(query: str) -> Optional[str]: """Create the UI session-store row if needed and return its id.""" store: Optional[SessionStore] = st.session_state.get("session_store") if store is None: return None model, workflow = _active_session_metadata() if not st.session_state.session_created: sid = st.session_state.get("current_session_id") or generate_session_id() st.session_state.current_session_id = sid title = SessionStore.generate_title(query) store.create_session( session_id=sid, model_name=model, workflow_type=workflow, title=title, log_dir=st.session_state.get("current_chat_log_dir"), ) st.session_state.session_created = True _set_session_id_in_url(st.session_state.current_session_id) return st.session_state.current_session_id def _save_query_to_store(query: str) -> bool: """Persist the human query immediately so refresh does not hide it.""" store: Optional[SessionStore] = st.session_state.get("session_store") if store is None: return False try: session_id = _ensure_ui_session(query) if session_id: store.save_messages( session_id, [SessionMessage(role="human", content=query)], ) return True except Exception as exc: logger.warning("Failed to save pending query to session store: %s", exc) return False def _save_exchange_to_store( query: str, result: Any, *, include_query: bool = True, ) -> None: """Persist a single query/result exchange to the SessionStore. Creates the session DB row on the first call, then appends messages. Parameters ---------- query : str User query text. result : Any Agent result to persist as session messages. """ store: Optional[SessionStore] = st.session_state.get("session_store") if store is None: return try: _ensure_ui_session(query) # Build SessionMessage objects for this exchange if include_query: entry = {"query": query, "result": result} messages = conversation_entry_to_messages(entry) else: messages = [ msg for msg in messages_from_result(result) if msg.role != "human" ] if messages: store.save_messages(st.session_state.current_session_id, messages) except Exception as exc: # Best-effort persistence -- don't break the UI. logger.warning("Failed to save exchange to session store: %s", exc) def _render_agent_status( selected_model: str, selected_workflow: str, thread_id: int, endpoint_status: dict, ) -> None: """Render sidebar status for the active agent. Parameters ---------- selected_model : str Selected model name. selected_workflow : str Selected workflow name. thread_id : int Current LangGraph thread ID. endpoint_status : dict Local endpoint status dictionary. """ st.sidebar.header("Agent Status") if st.session_state.agent: st.sidebar.success("\u2705 Agents Ready") st.sidebar.info(f"\U0001f9e0 Model: {selected_model}") st.sidebar.info(f"\u2699\ufe0f Workflow: {selected_workflow}") st.sidebar.info(f"\U0001f517 Thread ID: {thread_id}") st.sidebar.info( f"\U0001f4ac Messages: {len(st.session_state.conversation_history)}" ) if endpoint_status["ok"]: st.sidebar.caption(f"LLM endpoint: {endpoint_status['message']}") else: st.sidebar.error(f"LLM endpoint issue: {endpoint_status['message']}") if st.session_state.pending_human_question is not None: st.sidebar.warning("Waiting for your input...") if st.session_state.last_run_error: st.sidebar.error("Last run error (see verbose info).") if st.sidebar.button("\U0001f504 Refresh Agents"): st.session_state.agent = None # Checkpoint is lost on re-init, so clear interrupt state _clear_interrupt_state() st.rerun() else: st.sidebar.error("\u274c Agents Not Ready") st.sidebar.info("Agents will initialize automatically...") if not endpoint_status["ok"]: st.sidebar.error(f"LLM endpoint issue: {endpoint_status['message']}") st.sidebar.markdown("---") st.sidebar.markdown("**\u2699\ufe0f Configuration**") st.sidebar.markdown( "Use the Configuration page to modify settings, API endpoints, and chemistry parameters." ) st.sidebar.markdown("Current config loaded from: `config.toml`") def _auto_initialize_agent( config: dict, selected_model: str, selected_workflow: str, structured_output: bool, selected_output: str, generate_report: bool, human_supervised: bool, selected_base_url: Optional[str], ) -> None: """Initialize or refresh the cached Streamlit agent when config changes. Parameters ---------- config : dict Nested UI configuration. selected_model : str Selected model name. selected_workflow : str Selected workflow name. structured_output : bool Effective structured-output setting. selected_output : str Agent return mode. generate_report : bool Whether report generation is enabled. human_supervised : bool Whether human-supervision tools are enabled. selected_base_url : str, optional Model endpoint URL. """ current_config = ( selected_model, selected_workflow, structured_output, selected_output, generate_report, human_supervised, config["general"]["recursion_limit"], selected_base_url, get_argo_user_from_nested_config(config), st.session_state.get("current_chat_log_dir"), ) if st.session_state.agent is None or st.session_state.last_config != current_config: with st.spinner("\U0001f680 Initializing ChemGraph agents..."): chat_log_dir = _ensure_chat_log_dir() agent = initialize_agent( selected_model, selected_workflow, structured_output, selected_output, generate_report, human_supervised, config["general"]["recursion_limit"], selected_base_url, get_argo_user_from_nested_config(config), log_dir=chat_log_dir, ) st.session_state.agent = agent if agent is not None: st.session_state.last_config = ( selected_model, selected_workflow, structured_output, selected_output, generate_report, human_supervised, config["general"]["recursion_limit"], selected_base_url, get_argo_user_from_nested_config(config), chat_log_dir, ) else: st.session_state.last_config = None def _render_conversation_history(thread_id: int) -> None: """Render all saved conversation exchanges. Parameters ---------- thread_id : int Current LangGraph thread ID. """ if not st.session_state.conversation_history: return pending = st.session_state.get("pending_agent_submission") pending_log_dir = pending.get("query_log_dir") if isinstance(pending, dict) else None pending_interrupt_log_dir = ( st.session_state.get("pending_interrupt_log_dir") if st.session_state.get("pending_human_question") is not None else None ) visible_idx = 1 for entry in st.session_state.conversation_history: if ( pending_log_dir and entry.get("status") == "running" and entry.get("log_dir") == pending_log_dir ): # The live streaming panel below renders this same submitted query. # Avoid showing a duplicate history bubble while the run is active. continue if ( pending_interrupt_log_dir and entry.get("status") == "waiting_for_input" and entry.get("log_dir") == pending_interrupt_log_dir ): # The pending interrupt panel renders the original query, prior # clarification exchanges, and the current question. continue _render_single_exchange(visible_idx, entry, thread_id) visible_idx += 1 def _render_single_exchange(idx: int, entry: dict, thread_id: int) -> None: """Render one user-query / agent-response exchange. Parameters ---------- idx : int One-based exchange index. entry : dict Conversation-history entry. thread_id : int Current LangGraph thread ID. """ # User message with st.chat_message("user"): st.markdown(entry["query"]) # Interrupt exchanges (if any occurred during this query) for exch in entry.get("interrupt_exchanges", []): with st.chat_message("assistant"): _render_markdown_with_math(exch["question"]) with st.chat_message("user"): st.markdown(exch["answer"]) messages = extract_messages_from_result(entry["result"]) if not messages: with st.chat_message("assistant"): status = entry.get("status") if status == "error": st.error(f"Processing error: {entry.get('error', 'unknown error')}") elif status == "waiting_for_input": st.info("The agent is waiting for your input.") elif status == "running" or _agent_run_active(): st.info("Agent is running. Please wait for the current task to finish.") else: st.info( "No assistant response was saved for this run. " "It may have been interrupted; please rerun the query." ) _render_workflow_process_trace(messages, entry) _render_verbose_info(idx, messages, entry) return # Prefer validated structured output over intermediate LLM prose. final_answer = _final_answer_for_entry(entry, messages) # Display the AI response with visualizations with st.chat_message("assistant"): if final_answer: _render_markdown_with_math(final_answer) _render_workflow_process_trace(messages, entry) # Structure visualisation html_filename = find_html_filename(messages) _render_structure_section(idx, messages, final_answer, entry, html_filename) # HTML report if html_filename: _render_html_report(idx, html_filename, messages, entry) # IR spectrum if is_infrared_requested(messages): _render_ir_spectrum(idx, messages, entry) # Debug expander _render_verbose_info(idx, messages, entry) def _extract_final_answer(messages: list) -> str: """Walk messages in reverse to find the last non-JSON AI message. Parameters ---------- messages : list Message-like objects or dictionaries. Returns ------- str Final displayable answer text, or an empty string. """ final_answer = "" for message in reversed(messages): if hasattr(message, "content") and hasattr(message, "type"): content = normalize_message_content(message.content).strip() if message.type == "ai" and content: if not ( content.startswith("{") and content.endswith("}") and "numbers" in content ): final_answer = content break elif isinstance(message, dict): content = normalize_message_content(message.get("content", "")).strip() if message.get("type") == "ai" and content: if not ( content.startswith("{") and content.endswith("}") and "numbers" in content ): final_answer = content break elif hasattr(message, "content"): content = normalize_message_content(getattr(message, "content", "")).strip() if content and not ( content.startswith("{") and content.endswith("}") and "numbers" in content ): final_answer = content break return final_answer def _is_response_formatter_payload(payload: Any) -> bool: """Return whether *payload* looks like ChemGraph structured final output.""" if not isinstance(payload, dict): return False formatter_fields = { "smiles", "scalar_answer", "scalar_answers", "dipole", "vibrational_answer", "ir_spectrum", "atoms_data", "_failure", } return any(field in payload for field in formatter_fields) def _parse_structured_output_text(text: str) -> Optional[dict]: """Parse a structured-output JSON string if it matches ResponseFormatter.""" stripped = normalize_message_content(text).strip() if not (stripped.startswith("{") and stripped.endswith("}")): return None try: payload = json.loads(stripped) except json.JSONDecodeError: return None if not _is_response_formatter_payload(payload): return None return payload def _structured_output_from_entry(entry: dict, messages: list) -> Optional[dict]: """Return the validated final output saved by the graph, if available.""" result = entry.get("result") if isinstance(entry, dict) else None if isinstance(result, dict): final_output = result.get("final_output") if _is_response_formatter_payload(final_output): return _augment_structured_output_from_messages(entry, final_output, messages) for message in reversed(messages): if hasattr(message, "content"): parsed = _parse_structured_output_text(getattr(message, "content", "")) elif isinstance(message, dict): parsed = _parse_structured_output_text(message.get("content", "")) else: parsed = None if parsed: return _augment_structured_output_from_messages(entry, parsed, messages) return None def _augment_structured_output_from_messages( entry: dict, structured: dict, messages: list, ) -> dict: """Fill derived fields from saved tool messages when older output lacks them.""" if structured.get("scalar_answers"): return structured query = str(entry.get("query") or "").strip() if not query or not messages: return structured try: validation = validate_completion(query, messages) except Exception: return structured candidate = ( validation.get("structured_output") if isinstance(validation, dict) else None ) if not isinstance(candidate, dict) or not candidate.get("scalar_answers"): return structured augmented = dict(structured) augmented["scalar_answers"] = candidate["scalar_answers"] return augmented def _workflow_status_from_result(result: dict) -> str: """Return scientific workflow status, not just graph stream status.""" if not isinstance(result, dict): return "complete" final_output = result.get("final_output") if isinstance(final_output, dict): failure = final_output.get("_failure") if isinstance(failure, dict): status = str(failure.get("status") or "").lower() if status in {"blocked", "failed", "error"}: return "failed" if status == "error" else status return "failed" ledger = result.get("scientific_ledger") if isinstance(ledger, dict): status = str(ledger.get("status") or "").lower() if status in {"complete", "blocked", "failed", "partial"}: return status validation = ( result.get("completion_validation") if isinstance(result.get("completion_validation"), dict) else result.get("validation") ) if isinstance(validation, dict) and "complete" in validation: return "complete" if validation.get("complete") else "blocked" return "complete" def _workflow_status_label(workflow_status: str) -> str: return { "complete": "Complete", "blocked": "Blocked", "failed": "Failed", "partial": "Partial", }.get(str(workflow_status or "").lower(), "Complete") def _streamlit_state_for_workflow_status(workflow_status: str) -> str: return "complete" if str(workflow_status or "").lower() == "complete" else "error" def _format_number_for_display(value: Any) -> str: """Format numeric values without excessive trailing precision.""" if isinstance(value, int): return str(value) if isinstance(value, float): return f"{value:.8g}" return str(value) def _plain_scientific_unit(unit: Any) -> str: """Return a readable unit string for Markdown text.""" text = normalize_latex_delimiters(str(unit or "")).strip() text = text.strip("$") text = re.sub(r"\\text\{([^{}]+)\}", r"\1", text) text = re.sub(r"\\mathrm\{([^{}]+)\}", r"\1", text) text = text.replace(r"\cdot", "·") text = text.replace(r"\AA", "Å") text = text.replace("Angstrom", "Å").replace("angstrom", "Å") text = re.sub(r"\s*\*\s*", "·", text) text = re.sub(r"\s*·\s*", "·", text) return text def _format_structured_output_answer(structured: Optional[dict]) -> str: """Create user-facing Markdown from validated structured output. The UI should prefer this over intermediate LLM prose because these fields come from the graph's completion validator or ResponseFormatter output. """ if not structured: return "" if isinstance(structured.get("_failure"), dict): return format_validation_failure_message(structured) lines: list[str] = [] smiles = structured.get("smiles") if smiles: values = smiles if isinstance(smiles, list) else [smiles] lines.append("**SMILES:** " + ", ".join(f"`{value}`" for value in values)) scalar_values = structured.get("scalar_answers") if isinstance(scalar_values, list): scalars = [ item for item in scalar_values if isinstance(item, dict) and item.get("value") is not None ] else: scalar = structured.get("scalar_answer") scalars = ( [scalar] if isinstance(scalar, dict) and scalar.get("value") is not None else [] ) for scalar in scalars: property_name = str(scalar.get("property") or "Scalar result").strip() value = _format_number_for_display(scalar.get("value")) unit = _plain_scientific_unit(scalar.get("unit")) suffix = f" {unit}" if unit else "" lines.append(f"**{property_name}:** {value}{suffix}") dipole = structured.get("dipole") if isinstance(dipole, dict) and dipole.get("value") is not None: values = dipole.get("value") if isinstance(values, list): vector = ", ".join(_format_number_for_display(value) for value in values) vector = f"[{vector}]" else: vector = _format_number_for_display(values) unit = _plain_scientific_unit(dipole.get("unit")) suffix = f" {unit}" if unit else "" lines.append(f"**Dipole moment:** {vector}{suffix}") vibrational = structured.get("vibrational_answer") if isinstance(vibrational, dict): frequencies = vibrational.get("frequency_cm1") or [] if frequencies: sample = ", ".join(str(freq) for freq in frequencies[:10]) extra = "" if len(frequencies) <= 10 else f", ... ({len(frequencies)} total)" lines.append(f"**Vibrational frequencies:** {sample}{extra} cm^-1") ir_spectrum = structured.get("ir_spectrum") if isinstance(ir_spectrum, dict): frequencies = ir_spectrum.get("frequency_cm1") or [] intensities = ir_spectrum.get("intensity") or [] if frequencies or intensities: lines.append( "**IR spectrum:** " f"{len(frequencies)} frequencies and {len(intensities)} intensities." ) plot = ir_spectrum.get("plot") if plot: lines.append(f"**IR plot:** `{plot}`") if structured.get("atoms_data"): lines.append("**Structure:** atomic geometry data is available below.") return "\n\n".join(lines) def _final_answer_for_entry(entry: dict, messages: list) -> str: """Return the final answer text for a UI exchange.""" structured = _structured_output_from_entry(entry, messages) structured_answer = _format_structured_output_answer(structured) if structured_answer: return structured_answer return _extract_final_answer(messages) def _render_structure_section( idx: int, messages: list, final_answer: str, entry: dict, html_filename: Optional[str], ) -> None: """Render molecular structure artifacts for an exchange. Parameters ---------- idx : int One-based exchange index. messages : list Message-like objects from the exchange. final_answer : str Final assistant answer text. entry : dict Conversation-history entry. html_filename : str, optional HTML report path/filename, if detected. """ structure = find_structure_in_messages(messages) if structure: display_molecular_structure( structure["atomic_numbers"], structure["positions"], title=f"Molecular Structure (Query {idx})", ) else: structure_from_text = extract_molecular_structure(final_answer) if structure_from_text: display_molecular_structure( structure_from_text["atomic_numbers"], structure_from_text["positions"], title=f"Structure from Response {idx}", ) elif not html_filename: if has_structure_signal(messages, entry.get("query", ""), final_answer): log_dir = _artifact_log_dir(messages, entry) if log_dir and os.path.isdir(log_dir): latest_xyz = find_latest_xyz_file_in_dir(log_dir) if latest_xyz: try: atoms = ase_read(latest_xyz) display_molecular_structure( atoms.get_atomic_numbers().tolist(), atoms.get_positions().tolist(), title=( f"Structure from {Path(latest_xyz).name} " f"(Query {idx})" ), ) except Exception as exc: st.warning(f"Failed to load XYZ structure: {exc}") def _render_html_report( idx: int, html_filename: str, messages: list, entry: dict ) -> None: """Render an HTML report expander and download button. Parameters ---------- idx : int One-based exchange index. html_filename : str HTML report path or filename. messages : list Message-like objects from the exchange. entry : dict Conversation-history entry. """ with st.expander("\U0001f4ca Report", expanded=False): try: resolved_html = _resolve_artifact_path( html_filename, _artifact_log_dir(messages, entry), ) with open(resolved_html, "r", encoding="utf-8") as f: html_content = f.read() report_structure = extract_xyz_from_report_html(html_content) if report_structure: display_molecular_structure( report_structure["atomic_numbers"], report_structure["positions"], title=f"Molecular Structure (Report {idx})", ) cleaned_html = strip_viewer_from_report_html(html_content) st.download_button( "Download HTML Report", data=html_content, file_name=Path(resolved_html).name, mime="text/html", key=f"download_report_{idx}", ) st.components.v1.html(cleaned_html, height=600, scrolling=True) except FileNotFoundError: st.warning(f"HTML file '{html_filename}' not found") except Exception as e: st.error(f"Error displaying HTML: {e}") def _artifact_log_dir(messages: list, entry: dict) -> Optional[str]: """Return the log directory tied to a specific conversation entry. Parameters ---------- messages : list Message-like objects from the exchange. entry : dict Conversation-history entry. Returns ------- str or None Artifact/log directory, if found. """ entry_log_dir = entry.get("log_dir") if entry_log_dir: return entry_log_dir return extract_log_dir_from_messages(messages) def _latest_artifact_path(directory: Optional[str], pattern: str) -> Optional[str]: """Return the newest shallow match for an output artifact pattern. Parameters ---------- directory : str, optional Directory to search. pattern : str Glob pattern to match. Returns ------- str or None Newest matching file path, or ``None``. """ if not directory or not os.path.isdir(directory): return None candidates: list[Path] = [] try: candidates.extend(path for path in Path(directory).glob(pattern) if path.is_file()) except OSError: return None if not candidates: return None return str(max(candidates, key=lambda path: path.stat().st_mtime)) def _resolve_artifact_path(filename: str, directory: Optional[str]) -> str: """Resolve an artifact path relative to its run directory when known. Parameters ---------- filename : str Absolute or relative artifact path. directory : str, optional Run artifact directory. Returns ------- str Resolved artifact path. """ if os.path.isabs(filename): return filename if directory: return str(Path(directory) / filename) return filename def _render_ir_spectrum(idx: int, messages: list, entry: dict) -> None: """Render IR spectrum plot, frequency table, and trajectory viewer. Parameters ---------- idx : int One-based exchange index. messages : list Message-like objects from the exchange. entry : dict Conversation-history entry. """ log_dir = _artifact_log_dir(messages, entry) ir_path = _latest_artifact_path(log_dir, "ir_spectrum*.png") freq_path = _latest_artifact_path(log_dir, "frequencies*.csv") if not ir_path and not freq_path: st.warning("IR spectrum not found.") return with st.expander("\U0001f50d IR Spectrum", expanded=True): col1, col2 = st.columns(2, border=True) with col1: if ir_path and os.path.exists(ir_path): st.image(ir_path) else: st.warning("IR spectrum plot not found.") with col2: if not freq_path or not os.path.exists(freq_path): st.warning("Frequencies file not found.") return df = pd.read_csv( freq_path, index_col=False, names=["filename", "frequency"], ) modes = df.iloc[6:] if len(df) > 6 else df if modes.empty: st.warning("No vibrational frequencies found.") return st.write("**Select a frequency to visualize:**") freq_options = {} for mode_idx, row in modes.iterrows(): freq_text = str(row["frequency"]).strip() suffix = "i" if freq_text.endswith("i") else "" try: freq_value = float(freq_text.rstrip("i")) label = f"Mode {mode_idx}: {freq_value:.2f}{suffix} cm\u207b\u00b9" except ValueError: label = f"Mode {mode_idx}: {freq_text} cm\u207b\u00b9" freq_options[label] = mode_idx selected_freq = st.selectbox( "Frequency", list(freq_options.keys()), index=0, key=f"ir_frequency_select_{idx}", ) traj_file = str(modes.loc[freq_options[selected_freq]]["filename"]) traj_path = _resolve_artifact_path(traj_file, log_dir) if not os.path.exists(traj_path): st.warning(f"Trajectory file '{traj_file}' not found.") elif not STMOL_AVAILABLE: st.info("3D viewer not available; install stmol to animate trajectories.") else: import stmol from ase.io.trajectory import Trajectory traj = Trajectory(traj_path) view = visualize_trajectory(traj) view.zoomTo() stmol.showmol(view, height=400, width=500) def _render_workflow_process_trace(messages: list, entry: dict) -> None: """Render the validated tool-path visualization for a saved exchange.""" ledger = _scientific_ledger_from_entry(entry) if isinstance(ledger, dict): trace = _workflow_trace_from_ledger(ledger) else: trace = entry.get("workflow_trace") rebuilt_trace = _workflow_trace_from_messages(messages) if messages else None if not isinstance(trace, dict): trace = rebuilt_trace elif _trace_has_incomplete_run_ase(trace) and isinstance(rebuilt_trace, dict): trace = rebuilt_trace completed = trace.get("completed", []) if isinstance(trace, dict) else [] active = trace.get("active", []) if isinstance(trace, dict) else [] if not completed and not active: return st.html(_workflow_trace_diagram_html(completed, active)) st.html(_workflow_tool_status_html(completed, active)) identity_html = _identity_provenance_html(_identity_records_from_messages(messages)) if identity_html: st.html(identity_html) def _scientific_ledger_from_entry(entry: dict) -> Optional[dict]: """Return saved scientific ledger for a UI exchange, when available.""" result = entry.get("result") if isinstance(entry, dict) else None if not isinstance(result, dict): return None ledger = result.get("scientific_ledger") return ledger if isinstance(ledger, dict) else None def _workflow_trace_from_ledger(ledger: dict) -> dict[str, list]: """Build UI trace labels from canonical requirement ledger rows.""" completed: list[str] = [] active: list[dict] = [] can_answer = bool(ledger.get("can_answer")) artifacts_by_id = { artifact.get("id"): artifact for artifact in ledger.get("artifacts", []) or [] if isinstance(artifact, dict) and artifact.get("id") } for req in ledger.get("requirements", []) or []: if not isinstance(req, dict): continue label = _ledger_requirement_label(req, artifacts_by_id) status = str(req.get("status") or "").lower() if status == "done": completed.append(label) elif status in {"failed", "blocked"}: completed.append(f"{label} ({status})") elif can_answer: continue else: completed.append(f"{label} (incomplete)") return {"completed": completed, "active": active} def _ledger_requirement_label(req: dict, artifacts_by_id: Optional[dict] = None) -> str: """Return a trace-compatible label for one ledger requirement.""" kind = str(req.get("kind") or "requirement") species = req.get("species") target = f" on {species}" if species else "" if kind == "identity": return f"molecule_name_to_smiles:{target}".replace(": on", ":") if kind == "xyz": return f"smiles_to_coordinate_file:{target}".replace(": on", ":") if kind == "aggregation": return "calculator: aggregate reaction property" if kind in {"energy", "dipole", "thermo", "vib", "ir"}: calculator = _ledger_requirement_calculator(req, artifacts_by_id or {}) calc_suffix = f"/{calculator}" if calculator else "" return f"run_ase: {kind}{calc_suffix}{target}" return f"{kind}:{target}".replace(": on", ":") def _ledger_requirement_calculator(req: dict, artifacts_by_id: dict) -> str: """Return calculator family from a requirement's satisfied artifacts.""" for artifact_id in req.get("satisfied_by", []) or []: artifact = artifacts_by_id.get(artifact_id) if not isinstance(artifact, dict): continue calculator = artifact.get("calculator") if calculator: return str(calculator) return "" def _trace_has_incomplete_run_ase(trace: dict) -> bool: """Return whether a saved workflow trace contains stale incomplete run_ase rows.""" labels = [] if isinstance(trace, dict): labels.extend(str(label) for label in trace.get("completed", []) or []) labels.extend(_trace_entry_label(item) for item in trace.get("active", []) or []) return any("run_ase" in label and "(incomplete)" in label for label in labels) def _render_verbose_info(idx: int, messages: list, entry: dict) -> None: """Render raw result/debug information for an exchange. Parameters ---------- idx : int One-based exchange index. messages : list Message-like objects from the exchange. entry : dict Conversation-history entry. """ structure = find_structure_in_messages(messages) with st.expander(f"\U0001f50d Verbose Info (Query {idx})", expanded=False): st.write(f"**Number of messages:** {len(messages)}") st.write(f"**Structure found:** {'Yes' if structure else 'No'}") raw_result = entry.get("result") if st.session_state.last_run_query == entry.get("query"): if st.session_state.last_run_error: st.write("**Last run error:**") st.code(str(st.session_state.last_run_error)) if st.session_state.last_run_result is not None: raw_result = st.session_state.last_run_result st.write("**Raw result:**") st.code(pprint.pformat(raw_result, width=1, compact=False), language="text") def _render_example_queries(config: dict, selected_model: str) -> None: """Show example queries that the user can click to submit directly. Parameters ---------- config : dict Nested UI configuration. selected_model : str Selected model name. """ # Hide after the first message or during an interrupt if ( st.session_state.conversation_history or st.session_state.pending_human_question is not None or _agent_run_active() ): return with st.expander("Example Queries", expanded=False): st.markdown("**Based on your current configuration:**") st.markdown(f"- Model: {selected_model}") st.markdown( f"- Default Calculator: {config['chemistry']['calculators']['default']}" ) st.caption(QUERY_SPECIFICATION_HINT) examples = [ "What is the SMILES string for caffeine?", f"Optimize the geometry of water molecule using {config['chemistry']['calculators']['default']}", "Calculate the infrared spectrum of methanol with xtb calculator", "What is the reaction enthalpy of methane combustion using mace_mp", ] for ex in examples: if st.button(ex, key=f"ex_{ex}"): st.session_state._pending_example_query = ex st.rerun() def _render_pending_interrupt() -> None: """Show the agent's pending question and any prior interrupt exchanges.""" question = st.session_state.pending_human_question if question is None: return # Show the original user query that triggered the interrupt original_query = st.session_state.pending_interrupt_query if original_query: with st.chat_message("user"): _render_markdown_with_math(original_query) # Show any prior interrupt exchanges in this chain for exch in st.session_state.interrupt_exchanges: with st.chat_message("assistant"): _render_markdown_with_math(exch["question"]) with st.chat_message("user"): _render_markdown_with_math(exch["answer"]) # Show the current pending question with st.chat_message("assistant"): st.info("The agent needs your input to continue.", icon="\u2753") _render_markdown_with_math(question) # Cancel button if st.button("Cancel", key="cancel_interrupt"): _clear_interrupt_state() st.rerun() def _restore_pending_interrupt_from_history() -> None: """Restore in-memory clarification state from a waiting history entry.""" if st.session_state.pending_human_question is not None: return for entry in reversed(st.session_state.get("conversation_history", [])): if entry.get("status") != "waiting_for_input": continue question = entry.get("pending_human_question") resume_config = entry.get("pending_interrupt_config") if not question or not isinstance(resume_config, dict): return st.session_state.pending_human_question = question st.session_state.pending_interrupt_config = dict(resume_config) st.session_state.pending_interrupt_query = entry.get( "pending_interrupt_query", entry.get("query") ) st.session_state.pending_interrupt_thread_id = entry.get( "pending_interrupt_thread_id", entry.get("thread_id") ) st.session_state.pending_interrupt_prev_msg_count = int( entry.get("pending_interrupt_prev_msg_count", 0) or 0 ) st.session_state.pending_interrupt_model = entry.get("pending_interrupt_model") st.session_state.pending_interrupt_workflow = entry.get( "pending_interrupt_workflow" ) st.session_state.pending_interrupt_log_dir = entry.get( "pending_interrupt_log_dir", entry.get("log_dir") ) st.session_state.interrupt_count = int(entry.get("interrupt_count", 1) or 1) st.session_state.interrupt_exchanges = list( entry.get("interrupt_exchanges") or [] ) return def _clear_interrupt_state() -> None: """Clear all interrupt-related session state.""" st.session_state.pending_human_question = None st.session_state.pending_interrupt_config = None st.session_state.pending_interrupt_query = None st.session_state.pending_interrupt_thread_id = None st.session_state.pending_interrupt_prev_msg_count = 0 st.session_state.pending_interrupt_model = None st.session_state.pending_interrupt_workflow = None st.session_state.pending_interrupt_log_dir = None st.session_state.interrupt_count = 0 st.session_state.interrupt_exchanges = [] def _classify_message(msg): """Classify a LangGraph message for UI display. Parameters ---------- msg : Any LangGraph/LangChain message to classify. Returns ------- tuple or None ``("tool_call", [tool_items])``, ``("tool_result", tool_item)``, or ``None`` when not relevant for display. """ if isinstance(msg, dict): tool_calls = msg.get("tool_calls") msg_type = msg.get("type") or msg.get("role") tool_name = msg.get("name") tool_call_id = msg.get("tool_call_id") content = msg.get("content") else: tool_calls = getattr(msg, "tool_calls", None) msg_type = getattr(msg, "type", None) tool_name = getattr(msg, "name", None) tool_call_id = getattr(msg, "tool_call_id", None) content = getattr(msg, "content", None) if tool_calls: items = [ _tool_call_display_item(tc) for tc in tool_calls if isinstance(tc, dict) ] if items: return ("tool_call", items) if msg_type == "tool": if tool_name: return ( "tool_result", { "id": tool_call_id, "name": tool_name, "label": _tool_result_label( tool_name, content, ), }, ) return None def _tool_call_display_item(tool_call: dict) -> dict: """Return a compact UI label for an LLM tool call.""" name = tool_call.get("name", "unknown") args = tool_call.get("args") or {} return { "id": tool_call.get("id"), "name": name, "label": _tool_call_label(name, args if isinstance(args, dict) else {}), } def _tool_call_label(name: str, args: dict) -> str: """Format a short in-progress label for a tool call.""" if name in {"molecule_name_to_smiles", "resolve_molecule_identity"}: molecule = args.get("name") return f"{name}: {molecule}" if molecule else name if name == "smiles_to_coordinate_file": smiles = args.get("smiles") output_file = args.get("output_file") if smiles and output_file: return f"{name}: {smiles} -> {Path(str(output_file)).name}" if smiles: return f"{name}: {smiles}" return name if name == "run_ase": params = args.get("params") if isinstance(args.get("params"), dict) else args driver = params.get("driver") structure = params.get("input_structure_file") calculator = _calculator_label(params.get("calculator")) parts = [part for part in [driver, calculator] if part] prefix = "/".join(parts) if parts else "ASE" if structure: return f"{name}: {prefix} on {Path(str(structure)).name}" return f"{name}: {prefix}" return name def _tool_result_label(name: str, content: Any) -> str: """Format a completed-tool label using the actual tool output when possible.""" data = _tool_result_content_to_data(content) if name in {"molecule_name_to_smiles", "resolve_molecule_identity"} and isinstance(data, dict): molecule = data.get("name") or data.get("input_name") smiles = data.get("smiles") details = [] if data.get("source"): details.append(str(data["source"])) if data.get("cid"): details.append(f"CID {data['cid']}") if data.get("credibility_score") is not None: details.append(f"score {data['credibility_score']}") if data.get("requires_clarification") or data.get("needs_clarification"): details.append("needs clarification") suffix = f" ({', '.join(details)})" if details else "" if molecule and smiles: return f"{name}: {molecule} -> {smiles}{suffix}" if molecule: return f"{name}: {molecule}{suffix}" if name == "smiles_to_coordinate_file" and isinstance(data, dict): smiles = data.get("smiles") path = data.get("path") or data.get("output_file") if smiles and path: return f"{name}: {smiles} -> {Path(str(path)).name}" if path: return f"{name}: {Path(str(path)).name}" if name == "run_ase" and isinstance(data, dict): status = data.get("status") result = data.get("result") if isinstance(data.get("result"), dict) else {} details = [] if "single_point_energy" in data: details.append("energy") if isinstance(result, dict) and result.get("thermochemistry"): details.append("thermo") if data.get("dipole_moment"): details.append("dipole") if isinstance(result, dict) and result.get("ir_spectrum"): details.append("ir") elif isinstance(result, dict) and result.get("vibrational_frequencies"): details.append("vib") detail = "/".join(details) if details else "ASE" structure_hint = _run_ase_result_structure_hint(data) if structure_hint: detail = f"{detail} on {structure_hint}" return f"{name}: {detail}{f' ({status})' if status else ''}" return name def _run_ase_result_structure_hint(data: dict) -> str: """Return a compact structure filename from a run_ase result payload.""" candidates: list[Any] = [] for key in ("input_structure_file", "structure_file"): if data.get(key): candidates.append(data.get(key)) simulation_input = data.get("simulation_input") if isinstance(simulation_input, dict) and simulation_input.get("input_structure_file"): candidates.append(simulation_input.get("input_structure_file")) message = data.get("message") if isinstance(message, str): match = re.search(r"saved to\s+(.+?\.json)\b", message) if match: candidates.append(match.group(1)) for candidate in candidates: hint = _structure_hint_from_path(candidate) if hint: return hint return "" def _structure_hint_from_path(path_value: Any) -> str: """Convert a result or structure path into a displayable XYZ-like basename.""" if not path_value: return "" name = Path(str(path_value)).name if name.endswith(".xyz"): return name if name.startswith("output_") and name.endswith(".json"): species = name.removeprefix("output_").removesuffix(".json") if species: return f"{species}.xyz" return "" def _tool_result_content_to_data(content: Any) -> Any: """Best-effort parse of a tool message payload for UI labels.""" if isinstance(content, (dict, list)): return content text = str(content or "").strip() if not text: return text try: return json.loads(text) except json.JSONDecodeError: return text def _workflow_trace_from_messages(messages: list) -> dict[str, list]: """Rebuild completed tool labels from saved LangGraph messages.""" completed: list[str] = [] active: list[dict] = [] for message in messages or []: classified = _classify_message(message) if not classified: continue event_type, event_data = classified if event_type == "tool_call": for item in event_data: active.append(item) elif event_type == "tool_result": item = event_data _pop_active_trace_match(active, item) label = str(item.get("label") or item.get("name") or "") if label and label not in completed: completed.append(label) for item in active: label = _trace_entry_label(item) incomplete = f"{label} (incomplete)" if label and incomplete not in completed: completed.append(incomplete) return {"completed": completed, "active": []} def _pop_active_trace_match(active: list[dict], result: Any) -> Optional[dict]: """Pop the active tool call matching a completed tool result.""" result_id = result.get("id") if isinstance(result, dict) else None result_name = result.get("name") if isinstance(result, dict) else str(result) result_label = _trace_entry_label(result) result_hint = _trace_structure_hint(result_label) if result_id: for index, item in enumerate(active): if isinstance(item, dict) and item.get("id") == result_id: return active.pop(index) specific_matches: list[int] = [] generic_matches: list[int] = [] for index, item in enumerate(active): if not isinstance(item, dict): continue item_name = item.get("name") item_label = _trace_entry_label(item) if item_name != result_name and item_label != result_name: continue generic_matches.append(index) item_hint = _trace_structure_hint(item_label) if result_hint and item_hint and result_hint == item_hint: specific_matches.append(index) if specific_matches: return active.pop(specific_matches[0]) if len(generic_matches) == 1: return active.pop(generic_matches[0]) return None def _identity_records_from_messages(messages: list) -> list[dict]: """Extract resolver provenance records from saved tool messages.""" records: list[dict] = [] for message in messages or []: classified = _classify_message(message) if not classified: continue event_type, event_data = classified if event_type != "tool_result": continue if event_data.get("name") not in { "molecule_name_to_smiles", "resolve_molecule_identity", }: continue content = message.get("content") if isinstance(message, dict) else getattr(message, "content", None) data = _tool_result_content_to_data(content) if isinstance(data, dict) and (data.get("smiles") or data.get("candidates")): records.append(data) return records def _identity_provenance_html(records: list[dict]) -> str: """Render resolver provenance as auditable UI, not model reasoning.""" if not records: return "" cards = [] for record in records[-6:]: name = record.get("input_name") or record.get("name") or "chemical" smiles = record.get("smiles") or "n/a" source = record.get("source") or "unknown" cid = record.get("cid") or "n/a" score = record.get("credibility_score") score_text = "n/a" if score is None else str(score) flags = ( record.get("identity_flags") if isinstance(record.get("identity_flags"), dict) else {} ) active_flags = [ key.replace("_", " ") for key, value in flags.items() if value is True ] if record.get("requires_clarification") or record.get("needs_clarification"): active_flags.append("needs clarification") warnings = record.get("warnings") or [] if isinstance(warnings, str): warnings = [warnings] warning = record.get("warning") if warning and warning not in warnings: warnings.append(warning) flags_text = ", ".join(active_flags) if active_flags else "none" warning_text = " ".join(str(item) for item in warnings if item) or "none" candidate_count = len(record.get("candidates") or []) cards.append( f"""
{html.escape(str(smiles))}
Source{html.escape(str(source))}
CID{html.escape(str(cid))}
Score{html.escape(score_text)}
Flags{html.escape(flags_text)}
Candidates{candidate_count}