feat(markdown): thinking blocks between hidden tool calls collapse to the first one

This commit is contained in:
hh
2026-09-02 01:44:02 +02:00
parent 38ae967233
commit c3b0864e3d
2 changed files with 24 additions and 2 deletions
@@ -48,14 +48,19 @@ def render_assistant_text(text: str) -> str:
def render_assistant_message(message: Message) -> str:
"""Render an assistant ``Message`` into a turn block.
Tool-use blocks render to nothing; only text and thinking content reach
the file (see :mod:`.parser` for how the reverse strip works).
Tool-use blocks render to nothing; of the thinking blocks between two
texts only the first one reaches the file.
"""
parts: list[str] = ["### Assistant:", ""]
after_thinking = False
for block in message.content:
thinking = isinstance(block, ThinkingBlock)
if thinking and after_thinking:
continue
lines = list(_render_block(block))
if not lines:
continue
after_thinking = thinking
parts.extend(lines)
parts.append("")
return "\n".join(parts).rstrip() + "\n"
+17
View File
@@ -39,6 +39,23 @@ def test_thinking_renders_as_collapsed_callout() -> None:
)
def test_thinking_between_hidden_tool_calls_collapses_to_the_first() -> None:
message = _message(
ThinkingBlock(type="thinking", thinking="one", signature="s"),
ToolUseBlock(type="tool_use", id="t1", name="Read", input={}),
ThinkingBlock(type="thinking", thinking="two", signature="s"),
ToolUseBlock(type="tool_use", id="t2", name="Read", input={}),
ThinkingBlock(type="thinking", thinking="three", signature="s"),
TextBlock(type="text", text="answer"),
ThinkingBlock(type="thinking", thinking="four", signature="s"),
TextBlock(type="text", text="more"),
)
assert renderer.render_assistant_message(message) == (
"### Assistant:\n\n> [!thinking]-\n> one\n\nanswer\n\n"
"> [!thinking]-\n> four\n\nmore\n"
)
def test_user_scaffold_matches_rendered_user_turn_shape() -> None:
body = renderer.append_to_body("### Assistant:\n\nhi\n", renderer.USER_SCAFFOLD)
assert body.endswith("\n\n---\n\n### User:\n\n")