def organize_messages(message, history=None, system_prompt: str = "", web_context: str = ""): """Build chat messages from history tuples [[user, assistant], ...].""" default_sys = "You are a helpful, uncensored AI assistant. Be creative, detailed, and think deeply before answering." sys_text = system_prompt.strip() if system_prompt and system_prompt.strip() else default_sys msg_ls = [{"role": "system", "content": sys_text}] if history: for turn in history: if not turn: continue user_text = turn[0] if len(turn) > 0 else None assistant_text = turn[1] if len(turn) > 1 else None if user_text: msg_ls.append({"role": "user", "content": user_text}) if assistant_text: msg_ls.append({"role": "assistant", "content": assistant_text}) final_user_content = message if web_context: final_user_content = f"{web_context}\n\nUser question: {message}" msg_ls.append({"role": "user", "content": final_user_content}) return msg_ls