-
Notifications
You must be signed in to change notification settings - Fork 55
Add text, markdown, and chat output formats (#43) #44
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
Open
golergka
wants to merge
2
commits into
daaain:main
Choose a base branch
from
golergka:feat/text-output-format
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,179 @@ | ||
| #!/usr/bin/env python3 | ||
| """Extract data from ContentItem objects without formatting. | ||
|
|
||
| This module provides shared content extraction logic used by both HTML and text renderers. | ||
| It separates data extraction from presentation formatting. | ||
| """ | ||
|
|
||
| import json | ||
| from typing import Any, Dict, List, Union, Optional | ||
| from dataclasses import dataclass | ||
|
|
||
| from .models import ( | ||
| ContentItem, | ||
| TextContent, | ||
| ToolUseContent, | ||
| ToolResultContent, | ||
| ThinkingContent, | ||
| ImageContent, | ||
| ) | ||
|
|
||
|
|
||
| @dataclass | ||
| class ExtractedText: | ||
| """Extracted text content.""" | ||
|
|
||
| text: str | ||
|
|
||
|
|
||
| @dataclass | ||
| class ExtractedThinking: | ||
| """Extracted thinking content.""" | ||
|
|
||
| thinking: str | ||
| signature: Optional[str] = None | ||
|
|
||
|
|
||
| @dataclass | ||
| class ExtractedToolUse: | ||
| """Extracted tool use content.""" | ||
|
|
||
| name: str | ||
| id: str | ||
| input: Dict[str, Any] | ||
|
|
||
|
|
||
| @dataclass | ||
| class ExtractedToolResult: | ||
| """Extracted tool result content.""" | ||
|
|
||
| tool_use_id: str | ||
| is_error: bool | ||
| content: Union[str, List[Dict[str, Any]]] | ||
|
|
||
|
|
||
| @dataclass | ||
| class ExtractedImage: | ||
| """Extracted image content.""" | ||
|
|
||
| media_type: str | ||
| data: str | ||
|
|
||
|
|
||
| # Union type for all extracted content | ||
| ExtractedContent = Union[ | ||
| ExtractedText, | ||
| ExtractedThinking, | ||
| ExtractedToolUse, | ||
| ExtractedToolResult, | ||
| ExtractedImage, | ||
| ] | ||
|
|
||
|
|
||
| def extract_content_data(content: ContentItem) -> Optional[ExtractedContent]: | ||
| """Extract raw data from ContentItem without any formatting. | ||
|
|
||
| Args: | ||
| content: A ContentItem object (TextContent, ToolUseContent, etc.) | ||
|
|
||
| Returns: | ||
| Extracted data as a dataclass, or None if content type is unknown | ||
| """ | ||
| # Handle TextContent | ||
| if isinstance(content, TextContent) or ( | ||
| hasattr(content, "type") and getattr(content, "type") == "text" | ||
| ): | ||
| text = getattr(content, "text", str(content)) | ||
| return ExtractedText(text=text) | ||
|
|
||
| # Handle ThinkingContent | ||
| elif isinstance(content, ThinkingContent) or ( | ||
| hasattr(content, "type") and getattr(content, "type") == "thinking" | ||
| ): | ||
| thinking_text = getattr(content, "thinking", "") | ||
| signature = getattr(content, "signature", None) | ||
| return ExtractedThinking(thinking=thinking_text, signature=signature) | ||
|
|
||
| # Handle ToolUseContent | ||
| elif isinstance(content, ToolUseContent) or ( | ||
| hasattr(content, "type") and getattr(content, "type") == "tool_use" | ||
| ): | ||
| tool_name = getattr(content, "name", "unknown") | ||
| tool_id = getattr(content, "id", "") | ||
| tool_input = getattr(content, "input", {}) | ||
| return ExtractedToolUse(name=tool_name, id=tool_id, input=tool_input) | ||
|
|
||
| # Handle ToolResultContent | ||
| elif isinstance(content, ToolResultContent) or ( | ||
| hasattr(content, "type") and getattr(content, "type") == "tool_result" | ||
| ): | ||
| tool_use_id = getattr(content, "tool_use_id", "") | ||
| is_error = getattr(content, "is_error", False) | ||
| content_data = getattr(content, "content", "") | ||
| return ExtractedToolResult( | ||
| tool_use_id=tool_use_id, is_error=is_error, content=content_data | ||
| ) | ||
|
|
||
| # Handle ImageContent | ||
| elif isinstance(content, ImageContent) or ( | ||
| hasattr(content, "type") and getattr(content, "type") == "image" | ||
| ): | ||
| source = getattr(content, "source", {}) | ||
| media_type = ( | ||
| getattr(source, "media_type", "unknown") | ||
| if hasattr(source, "media_type") | ||
| else "unknown" | ||
| ) | ||
| data = getattr(source, "data", "") if hasattr(source, "data") else "" | ||
| return ExtractedImage(media_type=media_type, data=data) | ||
|
|
||
| # Unknown content type | ||
| return None | ||
|
|
||
|
|
||
| def format_tool_input_json(tool_input: Dict[str, Any], indent: int = 2) -> str: | ||
| """Format tool input as indented JSON string. | ||
|
|
||
| Args: | ||
| tool_input: Tool input dictionary | ||
| indent: Number of spaces for JSON indentation | ||
|
|
||
| Returns: | ||
| Formatted JSON string | ||
| """ | ||
| return json.dumps(tool_input, indent=indent) | ||
|
|
||
|
|
||
| def is_text_content(content: ContentItem) -> bool: | ||
| """Check if content is TextContent.""" | ||
| return isinstance(content, TextContent) or ( | ||
| hasattr(content, "type") and getattr(content, "type") == "text" | ||
| ) | ||
|
|
||
|
|
||
| def is_thinking_content(content: ContentItem) -> bool: | ||
| """Check if content is ThinkingContent.""" | ||
| return isinstance(content, ThinkingContent) or ( | ||
| hasattr(content, "type") and getattr(content, "type") == "thinking" | ||
| ) | ||
|
|
||
|
|
||
| def is_tool_use_content(content: ContentItem) -> bool: | ||
| """Check if content is ToolUseContent.""" | ||
| return isinstance(content, ToolUseContent) or ( | ||
| hasattr(content, "type") and getattr(content, "type") == "tool_use" | ||
| ) | ||
|
|
||
|
|
||
| def is_tool_result_content(content: ContentItem) -> bool: | ||
| """Check if content is ToolResultContent.""" | ||
| return isinstance(content, ToolResultContent) or ( | ||
| hasattr(content, "type") and getattr(content, "type") == "tool_result" | ||
| ) | ||
|
|
||
|
|
||
| def is_image_content(content: ContentItem) -> bool: | ||
| """Check if content is ImageContent.""" | ||
| return isinstance(content, ImageContent) or ( | ||
| hasattr(content, "type") and getattr(content, "type") == "image" | ||
| ) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Non-HTML default run still uses all-projects HTML path
Right now the all-projects guard for non-HTML formats only checks the explicit
--all-projectsflag:But later, when
input_pathisNone, you implicitly set:This means
claude-code-log --format text(noinput_path) will still go throughprocess_projects_hierarchy(...)and generate HTML index files, ignoring the requested text format.To align behavior with the intended restriction (“--all-projects only works with HTML format”), re-validate after you default
all_projects:This preserves the early guard for explicit
--all-projectsand also covers the implicit default case.Also applies to: 529-575