|
10 | 10 | import sys |
11 | 11 | import threading |
12 | 12 | import time |
13 | | -from collections.abc import Callable |
| 13 | +from collections.abc import Callable, Iterator |
14 | 14 | from dataclasses import dataclass |
15 | 15 | from functools import partial |
16 | 16 | from pathlib import Path |
@@ -307,6 +307,16 @@ def _tool_display(name: str, tool_input: dict[str, Any]) -> tuple[str, str, str] |
307 | 307 | return color, category, arg |
308 | 308 |
|
309 | 309 |
|
| 310 | +def _iter_content_blocks(raw: dict[str, Any]) -> Iterator[dict[str, Any]]: |
| 311 | + """Yield each dict block from ``raw["message"]["content"]``.""" |
| 312 | + content = raw.get("message", {}).get("content", []) |
| 313 | + if not isinstance(content, list): |
| 314 | + return |
| 315 | + for block in content: |
| 316 | + if isinstance(block, dict): |
| 317 | + yield block |
| 318 | + |
| 319 | + |
310 | 320 | class _LivePanelBase: |
311 | 321 | """Shared scroll-buffer state for iteration Live renderables. |
312 | 322 |
|
@@ -484,13 +494,7 @@ def _apply_assistant(self, raw: dict[str, Any]) -> None: |
484 | 494 | "cache_read_input_tokens", self._cache_read_tokens |
485 | 495 | ) |
486 | 496 |
|
487 | | - content = msg.get("content", []) |
488 | | - if not isinstance(content, list): |
489 | | - return |
490 | | - |
491 | | - for block in content: |
492 | | - if not isinstance(block, dict): |
493 | | - continue |
| 497 | + for block in _iter_content_blocks(raw): |
494 | 498 | block_type = block.get("type") |
495 | 499 |
|
496 | 500 | if block_type == "thinking": |
@@ -534,13 +538,7 @@ def _apply_assistant(self, raw: dict[str, Any]) -> None: |
534 | 538 | self.add_scroll_line(f"[bold {color}]{escape_markup(name)}[/]") |
535 | 539 |
|
536 | 540 | def _apply_user(self, raw: dict[str, Any]) -> None: |
537 | | - msg = raw.get("message", {}) |
538 | | - content = msg.get("content", []) |
539 | | - if not isinstance(content, list): |
540 | | - return |
541 | | - for block in content: |
542 | | - if not isinstance(block, dict): |
543 | | - continue |
| 541 | + for block in _iter_content_blocks(raw): |
544 | 542 | if block.get("type") == "tool_result" and block.get("is_error"): |
545 | 543 | snippet = _truncate(str(block.get("content", "")), _TRUNCATE_TOOL_ERROR) |
546 | 544 | self.add_scroll_line( |
|
0 commit comments