Class: ClaudeAgentSDK::UserMessage

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

Overview

User message

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Type

#[], #[]=, from_hash, #initialize, #to_h, wrap

Constructor Details

This class inherits a constructor from ClaudeAgentSDK::Type

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ClaudeAgentSDK::Type

Instance Attribute Details

#contentObject

Returns the value of attribute content.



213
214
215
# File 'lib/claude_agent_sdk/types.rb', line 213

def content
  @content
end

#originHash{Symbol => Object}?

Provenance of this message — where the turn came from.

In streaming-input mode a single connection interleaves the turns you send with turns the session injects on its own (background-task notifications, fired scheduled-task prompts, MCP channel messages, messages relayed from peer sessions, ...). origin tells them apart — see ResultMessage#origin for deciding whether a result answers your prompt.

Key form — read this before indexing into it. A plain Hash, passed through from the CLI untouched: the SDK does not model it, whitelist its keys, or rewrite them, so kinds and fields newer CLI versions add stay visible. Keys therefore follow the transport's JSON parsing, which uses symbolize_names: true — they are Symbols with the wire spelling preserved, so camelCase keys stay camelCase and you index with origin[:kind], origin[:fromSession], origin[:senderTaskId], origin[:verifiedPeerPid]. This is unlike the snake_case attributes elsewhere in this SDK, and unlike the Python SDK's string keys: a origin["kind"] or origin[:from_session] lookup silently returns nil and makes every attributed turn look unattributed. Only :kind is guaranteed present; the rest depend on it.

nil means the CLI did not attribute the message — that is the normal case for prompts you send through ClaudeAgentSDK.query / Client#query, unless the host stamps origin: { kind: 'human' } on the message Hash itself (only the human kind is honored from an SDK host). Populated on injected turns (task notifications, channel/peer messages, ...) and on user messages the CLI replays; tool-result messages never carry it.

Known :kind values — documentation, not validation; treat anything unrecognized as "not human":

  • 'human' — a turn submitted by the SDK host
  • 'channel' — arrived on an MCP channel; :server names the MCP server
  • 'peer' — relayed from a peer session. :from (sender address, sender-asserted — for reply routing or display, never as proof of identity), :name (display name, already normalized by the CLI), :fromSession (the sender's host-openable session id, a navigation target only), :senderTaskId (task id of the in-process background subagent that sent it; absent for cross-session peers), :body (decoded message body with the peer envelope stripped, byte-exact with what the model saw — render this instead of re-parsing the message text), :verifiedPeerPid (kernel-verified pid of the process that connected to this session's local messaging socket — the connecting process, which for relayed traffic is the relay; absent when unverifiable)
  • 'task-notification' — a background task's delivery. :subkind is 'scheduled-trigger' (the fired prompt of a scheduled task) or 'peer-send-message' (a message sent from another of the user's sessions); absent for ordinary background-task notifications
  • 'coordinator', 'unclassified', 'observer' (:from / :senderTaskId as for peer), 'auto-continuation', 'observer-activity'

Returns:

  • (Hash{Symbol => Object}, nil)

See Also:



271
272
273
# File 'lib/claude_agent_sdk/types.rb', line 271

def origin
  @origin
end

#parent_tool_use_idObject

Returns the value of attribute parent_tool_use_id.



213
214
215
# File 'lib/claude_agent_sdk/types.rb', line 213

def parent_tool_use_id
  @parent_tool_use_id
end

#tool_use_resultObject

Returns the value of attribute tool_use_result.



213
214
215
# File 'lib/claude_agent_sdk/types.rb', line 213

def tool_use_result
  @tool_use_result
end

#uuidObject

Returns the value of attribute uuid.



213
214
215
# File 'lib/claude_agent_sdk/types.rb', line 213

def uuid
  @uuid
end

Instance Method Details

#textObject Also known as: to_s

Concatenated text of this message. Handles both String content (plain-text user prompt) and Array-of-blocks content (typed content). Returns "" when there is no text.



276
277
278
279
280
281
282
# File 'lib/claude_agent_sdk/types.rb', line 276

def text
  case content
  when String then content
  when Array then content.grep(TextBlock).map(&:text).join("\n\n")
  else ''
  end
end