ClaudeWrapper.StreamEvent (ClaudeWrapper v0.7.0)

Copy Markdown View Source

A single event from the Claude CLI's NDJSON streaming output.

When using --output-format stream-json, the CLI emits one JSON object per line. Each event has a type and associated data.

Event types

Common event types include:

  • "system" -- system initialization
  • "assistant" -- assistant message content
  • "tool_use" -- tool invocation
  • "tool_result" -- tool execution result
  • "result" -- final result with cost/session info
  • "error" -- error during execution

Summary

Functions

Extract cost in USD, if present.

Parse a single NDJSON line into a stream event.

Whether this is the final result event.

Extract the result text, if this is a result event.

Extract the session ID, if present.

Types

t()

@type t() :: %ClaudeWrapper.StreamEvent{
  data: map(),
  raw: String.t(),
  type: String.t() | nil
}

Functions

cost_usd(stream_event)

@spec cost_usd(t()) :: float() | nil

Extract cost in USD, if present.

parse(line)

@spec parse(String.t()) :: {:ok, t()} | {:error, term()}

Parse a single NDJSON line into a stream event.

result?(stream_event)

@spec result?(t()) :: boolean()

Whether this is the final result event.

result_text(stream_event)

@spec result_text(t()) :: String.t() | nil

Extract the result text, if this is a result event.

session_id(stream_event)

@spec session_id(t()) :: String.t() | nil

Extract the session ID, if present.