import re from typing import Dict, List, Any, Optional def parse_conversation(raw: str) -> Dict[str, Any]: """ Parse a single conversation payload. - Everything before the `` tag becomes the system prompt. - Inside , lines starting with `User:` or `Assistant:` open a new turn. - Lines starting with `` are stored as alternatives on the *current* turn, under key "alt". - blocks are embedded into the current turn as a fenced code block (```). Returns: { "system": str, "messages": [ {"role": "user"|"assistant", "content": str, "alt": [str, ...]?}, ... ] } """ # 1) Split system vs dialogue start = raw.find("") if start == -1: raise ValueError("No tag found.") end = raw.find("", start) if end == -1: # If no closing tag, assume dialogue runs to the end end = len(raw) system_prompt = raw[:start].strip() dialogue = raw[start + len(""):end].strip("\n") # 2) Helpers for building messages messages: List[Dict[str, Any]] = [] current_role: Optional[str] = None current_lines: List[str] = [] current_alts: List[str] = [] def flush(): nonlocal current_role, current_lines, current_alts if current_role is not None: msg: Dict[str, Any] = { "role": current_role, "content": "\n".join(current_lines).strip() } if current_alts: msg["alt"] = current_alts[:] messages.append(msg) current_role = None current_lines = [] current_alts = [] # 3) Line-by-line parse of dialogue i = 0 lines = dialogue.splitlines() while i < len(lines): line = lines[i] # New speaker? m_user = re.match(r'^\s*User:\s*(.*)$', line) m_assistant = re.match(r'^\s*Assistant:\s*(.*)$', line) if m_user or m_assistant: # close previous turn flush() current_role = "user" if m_user else "assistant" first_text = (m_user or m_assistant).group(1) current_lines.append(first_text) i += 1 continue # Alternative for current turn? m_alt = re.match(r'^\s*\s*(.*)$', line) if m_alt: # If there's no current role yet, ignore dangling if current_role is not None: current_alts.append(m_alt.group(1)) i += 1 continue # Code block? if "" in line: # Collect until code_content_lines: List[str] = [] # If there is inline content after , capture only what's after the tag after_open = line.split("", 1)[1] # If the close is on the same line if "" in after_open: inside, after = after_open.split("", 1) code_content_lines.append(inside) code_text = "\n".join(code_content_lines).strip("\n") current_lines.append("```python\n" + code_text + "\n```") # If trailing text after on same line, keep it if after.strip(): current_lines.append(after.strip()) i += 1 continue else: # Multi-line code code_content_lines.append(after_open) i += 1 found_close = False while i < len(lines): if "" in lines[i]: before_close, after = lines[i].split("", 1) code_content_lines.append(before_close) code_text = "\n".join(code_content_lines).strip("\n") current_lines.append("```python\n" + code_text + "\n```") if after.strip(): current_lines.append(after.strip()) found_close = True i += 1 break else: code_content_lines.append(lines[i]) i += 1 if not found_close: # No closing tag found; still append what we have as code code_text = "\n".join(code_content_lines).strip("\n") current_lines.append("```python\n" + code_text + "\n```") continue # Regular content line: attach to current turn if any if current_role is not None: current_lines.append(line) # else ignore blank/dangling lines i += 1 # flush last flush() return { "system": system_prompt, "messages": messages }