ClaudeCode.Message.AssistantMessage (ClaudeCode v0.36.3)

View Source

Represents an assistant message from the Claude CLI.

Assistant messages contain Claude's responses, which can include text, tool use requests, or a combination of both.

Matches the official SDK schema:

{
  type: "assistant",
  uuid: string,
  message: { ... },  # Anthropic SDK Message type
  session_id: string,
  parent_tool_use_id?: string | null,
  error?: "authentication_failed" | "billing_error" | "rate_limit"
          | "invalid_request" | "server_error" | "max_output_tokens"
          | "unknown" | null
}

Summary

Functions

Creates a new AssistantMessage from JSON data.

Parses the inner API message (BetaMessage) from a JSON map.

Types

assistant_message_error()

@type assistant_message_error() ::
  :authentication_failed
  | :billing_error
  | :rate_limit
  | :invalid_request
  | :server_error
  | :max_output_tokens
  | :unknown
  | String.t()

message()

@type message() :: %{
  id: String.t(),
  type: :message | String.t() | nil,
  role: ClaudeCode.Message.role(),
  content: [ClaudeCode.Content.t()],
  model: String.t(),
  stop_reason: ClaudeCode.Message.stop_reason() | nil,
  stop_sequence: String.t() | nil,
  usage: ClaudeCode.Usage.t(),
  context_management: map() | nil
}

t()

@type t() :: %ClaudeCode.Message.AssistantMessage{
  error: assistant_message_error() | nil,
  message: message(),
  parent_tool_use_id: String.t() | nil,
  session_id: String.t(),
  type: :assistant,
  uuid: String.t() | nil
}

Functions

new(json)

@spec new(map()) :: {:ok, t()} | {:error, atom() | tuple()}

Creates a new AssistantMessage from JSON data.

Examples

iex> AssistantMessage.new(%{"type" => "assistant", "message" => %{...}})
{:ok, %AssistantMessage{...}}

iex> AssistantMessage.new(%{"type" => "user"})
{:error, :invalid_message_type}

parse_api_message(data)

@spec parse_api_message(map()) :: {:ok, message()} | {:error, term()}

Parses the inner API message (BetaMessage) from a JSON map.

Used by both AssistantMessage.new/1 (full messages) and PartialAssistantMessage (message_start events) since both contain the same Anthropic API Message structure.