Skip to content

Commit 5169d88

Browse files
committed
Render content-block arrays fully
Use render_content_block for array items Add tests for image and tool_use arrays
1 parent 6ffbff3 commit 5169d88

5 files changed

Lines changed: 44 additions & 17 deletions

src/claude_code_transcripts/__init__.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -108,21 +108,7 @@ def render_content_block_array(blocks):
108108
"""
109109
parts = []
110110
for block in blocks:
111-
if not isinstance(block, dict):
112-
continue
113-
block_type = block.get("type", "")
114-
if block_type == "text":
115-
text = block.get("text", "")
116-
# Render as markdown
117-
parts.append(render_markdown_text(text))
118-
elif block_type == "thinking":
119-
thinking = block.get("thinking", "")
120-
parts.append(render_markdown_text(thinking))
121-
else:
122-
# For other types, just show as formatted text
123-
text = block.get("text", block.get("content", ""))
124-
if text:
125-
parts.append(f"<pre>{html.escape(str(text))}</pre>")
111+
parts.append(render_content_block(block))
126112
return "".join(parts) if parts else None
127113

128114

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
<div class="tool-result"><div class="truncatable"><div class="truncatable-content"><p>Here is the file content:</p>
1+
<div class="tool-result"><div class="truncatable"><div class="truncatable-content">
2+
<div class="assistant-text"><p>Here is the file content:</p>
23
<p>Line 1
3-
Line 2</p></div><button class="expand-btn">Show more</button></div></div>
4+
Line 2</p></div></div><button class="expand-btn">Show more</button></div></div>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<div class="tool-result"><div class="truncatable"><div class="truncatable-content">
2+
<div class="image-block"><img src="data:image/gif;base64,R0lGODlhAQABAIAAAAUEBA==" style="max-width: 100%"></div></div><button class="expand-btn">Show more</button></div></div>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<div class="tool-result"><div class="truncatable"><div class="truncatable-content">
2+
<div class="tool-use bash-tool" data-tool-id="toolu_123">
3+
<div class="tool-header"><span class="tool-icon">$</span> Bash</div>
4+
<div class="tool-description">List files</div><div class="truncatable"><div class="truncatable-content"><pre class="bash-command">ls -la</pre></div><button class="expand-btn">Show more</button></div>
5+
</div></div><button class="expand-btn">Show more</button></div></div>

tests/test_generate_html.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,39 @@ def test_tool_result_content_block_array(self, snapshot_html):
335335
assert '"type": "text"' not in result
336336
assert result == snapshot_html
337337

338+
def test_tool_result_content_block_array_with_image(self, snapshot_html):
339+
"""Test that image blocks inside tool_result arrays render correctly."""
340+
block = {
341+
"type": "tool_result",
342+
"content": (
343+
'[{"type": "image", "source": {"type": "base64",'
344+
' "media_type": "image/gif", "data": "R0lGODlhAQABAIAAAAUEBA=="}}]'
345+
),
346+
"is_error": False,
347+
}
348+
result = render_content_block(block)
349+
assert 'src="data:image/gif;base64,' in result
350+
assert "image-block" in result
351+
assert '"type": "image"' not in result
352+
assert result == snapshot_html
353+
354+
def test_tool_result_content_block_array_with_tool_use(self, snapshot_html):
355+
"""Test that tool_use blocks inside tool_result arrays render correctly."""
356+
block = {
357+
"type": "tool_result",
358+
"content": (
359+
'[{"type": "tool_use", "id": "toolu_123", "name": "Bash",'
360+
' "input": {"command": "ls -la", "description": "List files"}}]'
361+
),
362+
"is_error": False,
363+
}
364+
result = render_content_block(block)
365+
assert "tool-use" in result
366+
assert "bash-tool" in result
367+
assert "List files" in result
368+
assert '"type": "tool_use"' not in result
369+
assert result == snapshot_html
370+
338371

339372
class TestAnalyzeConversation:
340373
"""Tests for conversation analysis."""

0 commit comments

Comments
 (0)