-
Notifications
You must be signed in to change notification settings - Fork 1
Extract JSONL parser monolith into focused modules #57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e3ab28b
Extract JSONL parser monolith into focused modules
clean6378-max-it b0b712b
fix(session_peek): scan full file when size ≤10KB
clean6378-max-it ddefa46
refactor(jsonl): address parser-split review nits
clean6378-max-it 942fb0a
fix(jsonl): address parser-split review follow-ups
clean6378-max-it e7d7dfc
fix(session_peek): restore quick_session_info parity with monolith
clean6378-max-it File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| """Shared content helpers for JSONL parsing and session peek.""" | ||
|
|
||
| import re | ||
| from typing import Any | ||
|
|
||
| from models.session import MessageDict | ||
|
|
||
|
|
||
| def entry_message(entry: dict[str, Any]) -> dict[str, Any]: | ||
| m = entry.get("message") | ||
| return m if isinstance(m, dict) else {} | ||
|
|
||
|
|
||
| def normalize_content(content: Any) -> list[dict[str, Any]]: | ||
| """Content can be a plain string, a list of strings, or a list of typed | ||
| blocks. Normalize everything into [{type, text}, ...] form.""" | ||
| if isinstance(content, str): | ||
| return [{"type": "text", "text": content}] | ||
| if isinstance(content, list): | ||
| result = [] | ||
| for part in content: | ||
| if isinstance(part, str): | ||
| result.append({"type": "text", "text": part}) | ||
| elif isinstance(part, dict): | ||
| result.append(part) | ||
| return result | ||
| return [] | ||
|
|
||
|
|
||
| def extract_text(content_parts: Any) -> str: | ||
| """Grab just the text blocks out of a content array, ignore tool_use/thinking.""" | ||
| parts = normalize_content(content_parts) | ||
| texts = [] | ||
| for part in parts: | ||
| if part.get("type") == "text": | ||
| texts.append(part.get("text", "")) | ||
| return "\n".join(texts) | ||
|
|
||
|
|
||
| def extract_images(content_parts: Any) -> list[dict[str, Any]]: | ||
| """Pull base64 image blocks out of a content array. | ||
| Also looks inside nested tool_result content blocks.""" | ||
| parts = normalize_content(content_parts) | ||
| images = [] | ||
| for part in parts: | ||
| if part.get("type") == "image": | ||
| source = part.get("source", {}) | ||
| if source.get("type") == "base64" and source.get("data"): | ||
| images.append({ | ||
| "media_type": source.get("media_type", "image/png"), | ||
| "data": source["data"], | ||
| }) | ||
| elif part.get("type") == "tool_result": | ||
| # Nested content is usually a block list; string content is not normalized here. | ||
| nested = part.get("content", []) | ||
| if isinstance(nested, list): | ||
| for sub in nested: | ||
| if isinstance(sub, dict) and sub.get("type") == "image": | ||
| source = sub.get("source", {}) | ||
| if source.get("type") == "base64" and source.get("data"): | ||
| images.append({ | ||
| "media_type": source.get("media_type", "image/png"), | ||
| "data": source["data"], | ||
| }) | ||
| return images | ||
|
|
||
|
|
||
| def first_title_line(text: str, max_chars: int = 100) -> str: | ||
| """First non-empty line after system-tag strip, truncated for session titles.""" | ||
| return strip_system_tags(text).strip().split("\n")[0][:max_chars] | ||
|
clean6378-max-it marked this conversation as resolved.
|
||
|
|
||
|
|
||
| def infer_title(messages: list[MessageDict]) -> str: | ||
| """Use the first line of the first real user message as the session title.""" | ||
| for msg in messages: | ||
| if msg["role"] == "user" and msg.get("text"): | ||
| first_line = first_title_line(msg["text"]) | ||
| if first_line: | ||
| return first_line | ||
| return "Untitled Session" | ||
|
|
||
|
|
||
| def strip_system_tags(text: str) -> str: | ||
| """Strip out the internal XML tags Claude Code injects (system-reminder, | ||
| ide_opened_file, etc.) so exported text is clean.""" | ||
| # Remove block tags and their content | ||
| for tag in ( | ||
| "system-reminder", "ide_opened_file", "user-prompt-submit-hook", | ||
| "claude_background_info", "fast_mode_info", "env", | ||
| ): | ||
| text = re.sub(rf"<{tag}>[\s\S]*?</{tag}>", "", text) | ||
| # Strip remaining known opening/closing tags | ||
| text = re.sub( | ||
| r"</?(?:ide_selection|local-command-stdout|local-command-stderr|" | ||
| r"command-name|antml:\w+|function_calls|example\w*)>", | ||
| "", | ||
| text, | ||
| ) | ||
| return text.strip() | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.