fix: parse error field in AssistantMessage to enable rate limit detection

Fixes #401

The SDK defines AssistantMessage.error (including "rate_limit"), but
the message parser was not extracting this field from the CLI response,
making it impossible to detect rate limits or implement retry logic.

Changes:
- Added error field extraction in message_parser.py
- AssistantMessage now properly includes the error field from the API
- Applications can now detect rate_limit errors and implement retry logic

Example usage:
```python
async for message in client.receive_response():
    if isinstance(message, AssistantMessage):
        if message.error == "rate_limit":
            # Implement retry logic
            await asyncio.sleep(60)
```

🤖 Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
majiayu000 2025-12-10 22:29:43 +08:00
parent 53482d8955
commit 51c636618a

View file

@ -120,6 +120,7 @@ def parse_message(data: dict[str, Any]) -> Message:
content=content_blocks,
model=data["message"]["model"],
parent_tool_use_id=data.get("parent_tool_use_id"),
error=data["message"].get("error"),
)
except KeyError as e:
raise MessageParseError(