Class: ClaudeAgentSDK::SessionMessage
- Inherits:
-
Object
- Object
- ClaudeAgentSDK::SessionMessage
- 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
-
#message ⇒ Object
Returns the value of attribute message.
-
#parent_agent_id ⇒ Object
Returns the value of attribute parent_agent_id.
-
#parent_tool_use_id ⇒ Object
Returns the value of attribute parent_tool_use_id.
-
#session_id ⇒ Object
Returns the value of attribute session_id.
-
#type ⇒ Object
Returns the value of attribute type.
-
#uuid ⇒ Object
Returns the value of attribute uuid.
Instance Method Summary collapse
-
#content_blocks ⇒ Object
Typed content blocks for this message.
-
#initialize(type:, uuid:, session_id:, message:, parent_tool_use_id: nil, parent_agent_id: nil) ⇒ SessionMessage
constructor
A new instance of SessionMessage.
-
#text ⇒ Object
(also: #to_s)
Concatenated text across every TextBlock in this message.
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 = @parent_tool_use_id = parent_tool_use_id @parent_agent_id = parent_agent_id end |
Instance Attribute Details
#message ⇒ Object
Returns the value of attribute message.
42 43 44 |
# File 'lib/claude_agent_sdk/sessions.rb', line 42 def @message end |
#parent_agent_id ⇒ Object
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_id ⇒ Object
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_id ⇒ Object
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 |
#type ⇒ Object
Returns the value of attribute type.
42 43 44 |
# File 'lib/claude_agent_sdk/sessions.rb', line 42 def type @type end |
#uuid ⇒ Object
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_blocks ⇒ Object
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 |
#text ⇒ Object 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 |