import uuid SESSIONS = {} USER_FEEDBACK_LOG = [] def create_session(query: str) -> str: sid = uuid.uuid4().hex SESSIONS[sid] = { "original_query": query, "round": 0, "history_candidates": [], "user_feedback": [], "rejected": [], "selected_prompt": None, "chat_history": [], "model_name": None } return sid def get_session(sid: str): return SESSIONS.get(sid) def update_session_add_candidates(sid: str, candidates: list): s = SESSIONS[sid] s["round"] += 1 s["history_candidates"].extend(candidates) def log_user_choice(sid: str, choice: str): SESSIONS[sid]["user_feedback"].append( {"round": SESSIONS[sid]["round"], "choice": choice} ) USER_FEEDBACK_LOG.append({ "session_id": sid, "round": SESSIONS[sid]["round"], "choice": choice }) def log_user_reject(sid: str, candidate: str, reason: str | None = None): SESSIONS[sid]["rejected"].append(candidate) USER_FEEDBACK_LOG.append({ "session_id": sid, "round": SESSIONS[sid]["round"], "reject": candidate, "reason": reason or "" }) def set_selected_prompt(sid: str, prompt: str): SESSIONS[sid]["selected_prompt"] = prompt def log_chat_message(sid: str, role: str, content: str): SESSIONS[sid]["chat_history"].append({"role": role, "content": content}) def set_session_model(sid: str, model_name: str | None): s = SESSIONS.get(sid) if s is not None: s["model_name"] = model_name