从字典反序列化
(data: dict)
| 113 | |
| 114 | @staticmethod |
| 115 | def from_dict(data: dict) -> "ContentBlock": |
| 116 | """从字典反序列化""" |
| 117 | block_type = data["type"] |
| 118 | if block_type == "text": |
| 119 | return ContentBlock(block_type="text", text=data["text"]) |
| 120 | elif block_type == "tool_use": |
| 121 | return ContentBlock( |
| 122 | block_type="tool_use", |
| 123 | tool_id=data["id"], |
| 124 | tool_name=data["name"], |
| 125 | tool_input=data["input"], |
| 126 | ) |
| 127 | elif block_type == "tool_result": |
| 128 | return ContentBlock( |
| 129 | block_type="tool_result", |
| 130 | tool_use_id=data["tool_use_id"], |
| 131 | tool_name=data["tool_name"], |
| 132 | output=data["output"], |
| 133 | is_error=data.get("is_error", False), |
| 134 | ) |
| 135 | raise ValueError(f"unknown block type: {block_type}") |
| 136 | |
| 137 | |
| 138 | @dataclass |
no test coverage detected