Class: ClaudeAgentSDK::SessionMessage

Inherits:
Object
  • Object
show all
Defined in:
lib/claude_agent_sdk/sessions.rb

Overview

A single message from a session transcript.

parent_tool_use_id / parent_agent_id are only populated for messages returned by get_subagent_messages / get_subagent_messages_from_store: respectively the id of the Agent tool_use block in the parent session that spawned the subagent, and (for nested subagents) the agent id of the subagent that spawned it. Both are nil when the subagent's metadata is unavailable, and always nil for top-level session messages.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, uuid:, session_id:, message:, parent_tool_use_id: nil, parent_agent_id: nil) ⇒ SessionMessage

Returns a new instance of SessionMessage.



44
45
46
47
48
49
50
51
# File 'lib/claude_agent_sdk/sessions.rb', line 44

def initialize(type:, uuid:, session_id:, message:, parent_tool_use_id: nil, parent_agent_id: nil)
  @type = type
  @uuid = uuid
  @session_id = session_id
  @message = message
  @parent_tool_use_id = parent_tool_use_id
  @parent_agent_id = parent_agent_id
end

Instance Attribute Details

#messageObject

Returns the value of attribute message.



42
43
44
# File 'lib/claude_agent_sdk/sessions.rb', line 42

def message
  @message
end

#parent_agent_idObject

Returns the value of attribute parent_agent_id.



42
43
44
# File 'lib/claude_agent_sdk/sessions.rb', line 42

def parent_agent_id
  @parent_agent_id
end

#parent_tool_use_idObject

Returns the value of attribute parent_tool_use_id.



42
43
44
# File 'lib/claude_agent_sdk/sessions.rb', line 42

def parent_tool_use_id
  @parent_tool_use_id
end

#session_idObject

Returns the value of attribute session_id.



42
43
44
# File 'lib/claude_agent_sdk/sessions.rb', line 42

def session_id
  @session_id
end

#typeObject

Returns the value of attribute type.



42
43
44
# File 'lib/claude_agent_sdk/sessions.rb', line 42

def type
  @type
end

#uuidObject

Returns the value of attribute uuid.



42
43
44
# File 'lib/claude_agent_sdk/sessions.rb', line 42

def uuid
  @uuid
end

Instance Method Details

#content_blocksObject

Typed content blocks for this message. Each entry is one of TextBlock, ThinkingBlock, ToolUseBlock, ToolResultBlock, or UnknownBlock (for forward-compatibility with newer CLI block types). Returns [] when the message has no array-of-blocks content (nil message, non-Hash message, String content, missing content).



72
73
74
75
76
77
78
79
80
81
# File 'lib/claude_agent_sdk/sessions.rb', line 72

def content_blocks
  return [] unless @message.is_a?(Hash)

  raw = @message['content'] || @message[:content]
  return [] unless raw.is_a?(Array)

  raw.filter_map do |block|
    MessageParser.parse_content_block(block) if block.is_a?(Hash)
  end
end

#textObject Also known as: to_s

Concatenated text across every TextBlock in this message. Returns "" when the message has no text content (nil message, non-Hash message, empty content, or only non-text blocks).



56
57
58
59
60
61
62
63
# File 'lib/claude_agent_sdk/sessions.rb', line 56

def text
  raw = @message.is_a?(Hash) ? (@message['content'] || @message[:content]) : nil
  case raw
  when String then raw
  when Array  then content_blocks.grep(TextBlock).map(&:text).join("\n\n")
  else ''
  end
end