(block: Any)
| 533 | |
| 534 | |
| 535 | def serialize_content_block(block: Any) -> Any: |
| 536 | if not _is_obj(block): |
| 537 | return {"type": "unknown", "value": safe_json_value(block)} |
| 538 | |
| 539 | b = _normalize_block(block) |
| 540 | raw_type = b.get("type") |
| 541 | block_type = raw_type if isinstance(raw_type, str) else "unknown" |
| 542 | |
| 543 | if block_type == "text": |
| 544 | text = b.get("text") |
| 545 | if not isinstance(text, str): |
| 546 | return {"type": "text", "text": ""} |
| 547 | return {"type": "text", "text": strip_top_level_internal_text(text)} |
| 548 | |
| 549 | if block_type == "tool_use": |
| 550 | result: dict[str, Any] = {"type": "tool_use"} |
| 551 | if b.get("id") is not None: |
| 552 | result["id"] = safe_json_value(b.get("id")) |
| 553 | if b.get("name") is not None: |
| 554 | result["name"] = str(b.get("name")) |
| 555 | if b.get("input") is not None: |
| 556 | result["input"] = safe_json_value(b.get("input")) |
| 557 | return result |
| 558 | |
| 559 | if block_type == "tool_result": |
| 560 | tool_use_id = b.get("tool_use_id") |
| 561 | if tool_use_id is None: |
| 562 | tool_use_id = b.get("toolUseId") |
| 563 | is_error = b.get("is_error") |
| 564 | if is_error is None: |
| 565 | is_error = b.get("isError") |
| 566 | result = {"type": "tool_result"} |
| 567 | if tool_use_id is not None: |
| 568 | result["toolUseId"] = str(tool_use_id) |
| 569 | if b.get("content") is not None: |
| 570 | result["content"] = safe_json_value(b.get("content")) |
| 571 | if is_error is not None: |
| 572 | result["isError"] = bool(is_error) |
| 573 | return result |
| 574 | |
| 575 | if block_type == "image": |
| 576 | result = {"type": "image"} |
| 577 | if b.get("source") is not None: |
| 578 | result["source"] = safe_json_value(b.get("source")) |
| 579 | return result |
| 580 | |
| 581 | if block_type in ("thinking", "redacted_thinking"): |
| 582 | return [] |
| 583 | |
| 584 | return {"type": block_type, "value": safe_json_value(b)} |
| 585 | |
| 586 | |
| 587 | def serialize_content_blocks(content: Any) -> List[Any]: |
no test coverage detected