ClaudeCode.Message (ClaudeCode v0.1.0)

View Source

Utilities for working with messages from the Claude CLI.

Messages can be system initialization, assistant responses, user tool results, or final result messages. This module provides functions to parse and work with any message type.

Summary

Functions

Checks if a value is any type of message.

Returns the type of a message.

Parses a message from JSON data based on its type.

Parses a list of messages.

Parses a newline-delimited JSON stream from the CLI.

Types

Functions

message?(arg1)

@spec message?(any()) :: boolean()

Checks if a value is any type of message.

message_type(arg1)

@spec message_type(t()) :: :system | :assistant | :user | :result

Returns the type of a message.

parse(data)

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

Parses a message from JSON data based on its type.

Examples

iex> Message.parse(%{"type" => "system", ...})
{:ok, %System{...}}

iex> Message.parse(%{"type" => "unknown"})
{:error, {:unknown_message_type, "unknown"}}

parse_all(messages)

@spec parse_all([map()]) :: {:ok, [t()]} | {:error, term()}

Parses a list of messages.

Returns {:ok, messages} if all messages parse successfully, or {:error, {:parse_error, index, error}} for the first failure.

parse_stream(stream)

@spec parse_stream(String.t()) :: {:ok, [t()]} | {:error, term()}

Parses a newline-delimited JSON stream from the CLI.

This is the format output by the CLI with --output-format stream-json.