(block: Any)
| 20 | |
| 21 | |
| 22 | def _flatten_block(block: Any) -> str: |
| 23 | if not isinstance(block, dict): |
| 24 | return "" |
| 25 | btype = block.get("type", "") |
| 26 | if btype == "text": |
| 27 | return str(block.get("text", "")) |
| 28 | if btype == "tool_use": |
| 29 | name = block.get("name", "") |
| 30 | args = block.get("input", block.get("arguments", {})) |
| 31 | canonical = json.dumps(args, sort_keys=True, separators=(",", ":"), ensure_ascii=False) |
| 32 | return f"[tool_use:{name}({canonical})]" |
| 33 | if btype == "tool_result": |
| 34 | inner = block.get("content", "") |
| 35 | if not inner: |
| 36 | return "" |
| 37 | if isinstance(inner, str): |
| 38 | return inner |
| 39 | if isinstance(inner, list): |
| 40 | return " ".join(_flatten_block(b) for b in inner) |
| 41 | return "" |
| 42 | if btype in ("image", "input_image"): |
| 43 | return "[image]" |
| 44 | return "" |
| 45 | |
| 46 | |
| 47 | def normalize_message_text(message: dict[str, Any]) -> str: |
no test coverage detected