Class: Ask::Message
- Inherits:
-
Object
- Object
- Ask::Message
- Defined in:
- lib/ask/conversation.rb
Overview
A single message in a conversation. Immutable after creation. Valid roles are: :system, :user, :assistant, :tool.
Constant Summary collapse
- VALID_ROLES =
%i[system user assistant tool].freeze
Instance Attribute Summary collapse
-
#content ⇒ String?
readonly
Message text content.
-
#metadata ⇒ Hash
readonly
Arbitrary metadata attached to this message.
-
#name ⇒ String?
readonly
Optional participant name (for multi-agent scenarios).
-
#role ⇒ Symbol
readonly
Message role (:system, :user, :assistant, :tool).
-
#tool_call_id ⇒ String?
readonly
Tool call ID this message is responding to.
-
#tool_calls ⇒ Array<Hash>?
readonly
Tool calls included in this message.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
True if role, content, name, and tool metadata all match.
-
#assistant? ⇒ Boolean
True if role is :assistant.
- #hash ⇒ Object
-
#initialize(role:, content: nil, name: nil, tool_call_id: nil, tool_calls: nil, metadata: {}) ⇒ Message
constructor
A new instance of Message.
- #inspect ⇒ String
-
#system? ⇒ Boolean
True if role is :system.
-
#to_h ⇒ Hash
Convert to a hash suitable for provider wire format serialization.
-
#tool? ⇒ Boolean
True if role is :tool.
-
#tool_call? ⇒ Boolean
True if this message contains tool calls.
-
#tool_result? ⇒ Boolean
True if this is a tool result message.
-
#user? ⇒ Boolean
True if role is :user.
Constructor Details
#initialize(role:, content: nil, name: nil, tool_call_id: nil, tool_calls: nil, metadata: {}) ⇒ Message
Returns a new instance of Message.
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/ask/conversation.rb', line 27 def initialize(role:, content: nil, name: nil, tool_call_id: nil, tool_calls: nil, metadata: {}) @role = normalize_role!(role) @content = content @name = normalize_name(name) @tool_call_id = tool_call_id @tool_calls = tool_calls @metadata = .dup.freeze validate! freeze end |
Instance Attribute Details
#content ⇒ String? (readonly)
Returns message text content.
13 14 15 |
# File 'lib/ask/conversation.rb', line 13 def content @content end |
#metadata ⇒ Hash (readonly)
Returns arbitrary metadata attached to this message.
25 26 27 |
# File 'lib/ask/conversation.rb', line 25 def @metadata end |
#name ⇒ String? (readonly)
Returns optional participant name (for multi-agent scenarios).
16 17 18 |
# File 'lib/ask/conversation.rb', line 16 def name @name end |
#role ⇒ Symbol (readonly)
Returns message role (:system, :user, :assistant, :tool).
10 11 12 |
# File 'lib/ask/conversation.rb', line 10 def role @role end |
#tool_call_id ⇒ String? (readonly)
Returns tool call ID this message is responding to.
19 20 21 |
# File 'lib/ask/conversation.rb', line 19 def tool_call_id @tool_call_id end |
#tool_calls ⇒ Array<Hash>? (readonly)
Returns tool calls included in this message.
22 23 24 |
# File 'lib/ask/conversation.rb', line 22 def tool_calls @tool_calls end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
Returns true if role, content, name, and tool metadata all match.
69 70 71 72 73 74 75 |
# File 'lib/ask/conversation.rb', line 69 def ==(other) return false unless other.is_a?(Message) @role == other.role && @content == other.content && @name == other.name && @tool_call_id == other.tool_call_id && @tool_calls == other.tool_calls end |
#assistant? ⇒ Boolean
Returns true if role is :assistant.
51 |
# File 'lib/ask/conversation.rb', line 51 def assistant? = @role == :assistant |
#hash ⇒ Object
78 79 80 |
# File 'lib/ask/conversation.rb', line 78 def hash [@role, @content, @name, @tool_call_id, @tool_calls].hash end |
#inspect ⇒ String
83 84 85 |
# File 'lib/ask/conversation.rb', line 83 def inspect "#<Ask::Message role=#{@role.inspect} content=#{@content && @content.length > 57 ? @content[0,57].inspect + "..." : @content.inspect}>" end |
#system? ⇒ Boolean
Returns true if role is :system.
45 |
# File 'lib/ask/conversation.rb', line 45 def system? = @role == :system |
#to_h ⇒ Hash
Convert to a hash suitable for provider wire format serialization. Omits nil-valued keys.
59 60 61 62 63 64 65 66 |
# File 'lib/ask/conversation.rb', line 59 def to_h base = { role: @role } base[:content] = @content if @content base[:name] = @name if @name base[:tool_call_id] = @tool_call_id if @tool_call_id base[:tool_calls] = @tool_calls if @tool_calls base end |
#tool? ⇒ Boolean
Returns true if role is :tool.
54 |
# File 'lib/ask/conversation.rb', line 54 def tool? = @role == :tool |
#tool_call? ⇒ Boolean
Returns true if this message contains tool calls.
39 |
# File 'lib/ask/conversation.rb', line 39 def tool_call? = @tool_calls&.any? == true |
#tool_result? ⇒ Boolean
Returns true if this is a tool result message.
42 |
# File 'lib/ask/conversation.rb', line 42 def tool_result? = !@tool_call_id.nil? |
#user? ⇒ Boolean
Returns true if role is :user.
48 |
# File 'lib/ask/conversation.rb', line 48 def user? = @role == :user |