On this page
NDJSON protocol layer that reads raw Claude Code stream-json output lines and decodes them into typed Event objects for consumption.
#claudestream._protocol
#claudestream._protocol
NDJSON protocol layer that reads raw Claude Code stream-json output lines and decodes them into typed Event objects for consumption.
#parse_content_block
def parse_content_block(raw: dict) -> ContentBlockParse a single content block dict into the correct typed block.
#parse_usage
def parse_usage(raw: dict | None) -> Usage | NoneParse a usage dict into Usage, or return None.
#parse_event
def parse_event(raw: dict) -> EventMap a raw JSON dict to the correct typed Event Struct.
#_resolve_path
def _resolve_path(path: str, cwd: str | None) -> strResolve a file path to absolute, using cwd if the path is relative.
#_derive_file_events
def _derive_file_events(block: ToolUseBlock, event: AssistantMessage, cwd: str | None) -> list[Event]Derive FileWrite/FileEdit events from a file-modifying ToolUseBlock.
#flatten_event
def flatten_event(event: Event, cwd: str | None=None) -> list[Event]Expand an event into convenience events (one per content block).
Args:
event: The event to flatten.cwd: Working directory for resolving relative paths in file-tracking events.
#read_events
async def read_events(stream: asyncio.StreamReader) -> AsyncIterator[Event]Async generator that reads NDJSON lines and yields parsed Events.
#write_message
async def write_message(stream: asyncio.StreamWriter, msg: Writable) -> NoneSerialize a message to NDJSON and write it to the stream.