|
| 1 | +"""Shared content helpers for JSONL parsing and session peek.""" |
| 2 | + |
| 3 | +import re |
| 4 | +from typing import Any |
| 5 | + |
| 6 | +from models.session import MessageDict |
| 7 | + |
| 8 | + |
| 9 | +def entry_message(entry: dict[str, Any]) -> dict[str, Any]: |
| 10 | + m = entry.get("message") |
| 11 | + return m if isinstance(m, dict) else {} |
| 12 | + |
| 13 | + |
| 14 | +def normalize_content(content: Any) -> list[dict[str, Any]]: |
| 15 | + """Content can be a plain string, a list of strings, or a list of typed |
| 16 | + blocks. Normalize everything into [{type, text}, ...] form.""" |
| 17 | + if isinstance(content, str): |
| 18 | + return [{"type": "text", "text": content}] |
| 19 | + if isinstance(content, list): |
| 20 | + result = [] |
| 21 | + for part in content: |
| 22 | + if isinstance(part, str): |
| 23 | + result.append({"type": "text", "text": part}) |
| 24 | + elif isinstance(part, dict): |
| 25 | + result.append(part) |
| 26 | + return result |
| 27 | + return [] |
| 28 | + |
| 29 | + |
| 30 | +def extract_text(content_parts: Any) -> str: |
| 31 | + """Grab just the text blocks out of a content array, ignore tool_use/thinking.""" |
| 32 | + parts = normalize_content(content_parts) |
| 33 | + texts = [] |
| 34 | + for part in parts: |
| 35 | + if part.get("type") == "text": |
| 36 | + texts.append(part.get("text", "")) |
| 37 | + return "\n".join(texts) |
| 38 | + |
| 39 | + |
| 40 | +def extract_images(content_parts: Any) -> list[dict[str, Any]]: |
| 41 | + """Pull base64 image blocks out of a content array. |
| 42 | + Also looks inside nested tool_result content blocks.""" |
| 43 | + parts = normalize_content(content_parts) |
| 44 | + images = [] |
| 45 | + for part in parts: |
| 46 | + if part.get("type") == "image": |
| 47 | + source = part.get("source", {}) |
| 48 | + if source.get("type") == "base64" and source.get("data"): |
| 49 | + images.append({ |
| 50 | + "media_type": source.get("media_type", "image/png"), |
| 51 | + "data": source["data"], |
| 52 | + }) |
| 53 | + elif part.get("type") == "tool_result": |
| 54 | + # Nested content is usually a block list; string content is not normalized here. |
| 55 | + nested = part.get("content", []) |
| 56 | + if isinstance(nested, list): |
| 57 | + for sub in nested: |
| 58 | + if isinstance(sub, dict) and sub.get("type") == "image": |
| 59 | + source = sub.get("source", {}) |
| 60 | + if source.get("type") == "base64" and source.get("data"): |
| 61 | + images.append({ |
| 62 | + "media_type": source.get("media_type", "image/png"), |
| 63 | + "data": source["data"], |
| 64 | + }) |
| 65 | + return images |
| 66 | + |
| 67 | + |
| 68 | +def first_title_line(text: str, max_chars: int = 100) -> str: |
| 69 | + """First non-empty line after system-tag strip, truncated for session titles.""" |
| 70 | + return strip_system_tags(text).strip().split("\n")[0][:max_chars] |
| 71 | + |
| 72 | + |
| 73 | +def infer_title(messages: list[MessageDict]) -> str: |
| 74 | + """Use the first line of the first real user message as the session title.""" |
| 75 | + for msg in messages: |
| 76 | + if msg["role"] == "user" and msg.get("text"): |
| 77 | + first_line = first_title_line(msg["text"]) |
| 78 | + if first_line: |
| 79 | + return first_line |
| 80 | + return "Untitled Session" |
| 81 | + |
| 82 | + |
| 83 | +def strip_system_tags(text: str) -> str: |
| 84 | + """Strip out the internal XML tags Claude Code injects (system-reminder, |
| 85 | + ide_opened_file, etc.) so exported text is clean.""" |
| 86 | + # Remove block tags and their content |
| 87 | + for tag in ( |
| 88 | + "system-reminder", "ide_opened_file", "user-prompt-submit-hook", |
| 89 | + "claude_background_info", "fast_mode_info", "env", |
| 90 | + ): |
| 91 | + text = re.sub(rf"<{tag}>[\s\S]*?</{tag}>", "", text) |
| 92 | + # Strip remaining known opening/closing tags |
| 93 | + text = re.sub( |
| 94 | + r"</?(?:ide_selection|local-command-stdout|local-command-stderr|" |
| 95 | + r"command-name|antml:\w+|function_calls|example\w*)>", |
| 96 | + "", |
| 97 | + text, |
| 98 | + ) |
| 99 | + return text.strip() |
0 commit comments