Class: RobotLab::Message Abstract
- Inherits:
-
Object
- Object
- RobotLab::Message
- Defined in:
- lib/robot_lab/message.rb
Overview
Subclass and implement specific message types
Base class for all message types in RobotLab
Messages represent the communication between users, assistants, and tools in a conversation. This mirrors the TypeScript Message union type.
Direct Known Subclasses
Constant Summary collapse
- VALID_TYPES =
Valid message types
%w[text tool_call tool_result].freeze
- VALID_ROLES =
Valid message roles
%w[system user assistant tool_result].freeze
- VALID_STOP_REASONS =
Valid stop reasons
%w[tool stop].freeze
Instance Attribute Summary collapse
-
#content ⇒ String, ...
readonly
The message content.
-
#role ⇒ String
readonly
The message role (system, user, assistant, or tool_result).
-
#stop_reason ⇒ Object
readonly
Returns the value of attribute stop_reason.
-
#type ⇒ String
readonly
The message type (text, tool_call, or tool_result).
Class Method Summary collapse
-
.from_hash(hash) ⇒ Message
Creates a Message instance from a hash.
Instance Method Summary collapse
-
#assistant? ⇒ Boolean
True if this is an assistant message.
-
#initialize(type:, role:, content:, stop_reason: nil) ⇒ Message
constructor
Creates a new Message instance.
-
#stopped? ⇒ Boolean
True if the conversation stopped naturally.
-
#system? ⇒ Boolean
True if this is a system message.
-
#text? ⇒ Boolean
True if this is a text message.
-
#to_h ⇒ Hash
Converts the message to a hash representation.
-
#to_json(*args) ⇒ String
Converts the message to JSON.
-
#tool_call? ⇒ Boolean
True if this is a tool call message.
-
#tool_result? ⇒ Boolean
True if this is a tool result message.
-
#tool_stop? ⇒ Boolean
True if the conversation stopped for a tool call.
-
#user? ⇒ Boolean
True if this is a user message.
Constructor Details
#initialize(type:, role:, content:, stop_reason: nil) ⇒ Message
Creates a new Message instance.
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/robot_lab/message.rb', line 36 def initialize(type:, role:, content:, stop_reason: nil) validate_type!(type) validate_role!(role) validate_stop_reason!(stop_reason) if stop_reason @type = type.to_s @role = role.to_s @content = content @stop_reason = stop_reason&.to_s end |
Instance Attribute Details
#content ⇒ String, ... (readonly)
Returns the message content.
27 |
# File 'lib/robot_lab/message.rb', line 27 attr_reader :type, :role, :content, :stop_reason |
#role ⇒ String (readonly)
Returns the message role (system, user, assistant, or tool_result).
27 |
# File 'lib/robot_lab/message.rb', line 27 attr_reader :type, :role, :content, :stop_reason |
#stop_reason ⇒ Object (readonly)
Returns the value of attribute stop_reason.
27 |
# File 'lib/robot_lab/message.rb', line 27 attr_reader :type, :role, :content, :stop_reason |
#type ⇒ String (readonly)
Returns the message type (text, tool_call, or tool_result).
27 28 29 |
# File 'lib/robot_lab/message.rb', line 27 def type @type end |
Class Method Details
.from_hash(hash) ⇒ Message
Creates a Message instance from a hash.
Automatically determines the appropriate subclass based on the type.
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/robot_lab/message.rb', line 92 def self.from_hash(hash) hash = hash.transform_keys(&:to_sym) case hash[:type]&.to_s when "text" TextMessage.new(**hash.slice(:role, :content, :stop_reason)) when "tool_call" ToolCallMessage.new( role: hash[:role], tools: hash[:tools], stop_reason: hash[:stop_reason] ) when "tool_result" ToolResultMessage.new( tool: hash[:tool], content: hash[:content], stop_reason: hash[:stop_reason] ) else new(**hash) end end |
Instance Method Details
#assistant? ⇒ Boolean
Returns true if this is an assistant message.
59 |
# File 'lib/robot_lab/message.rb', line 59 def assistant? = role == "assistant" |
#stopped? ⇒ Boolean
Returns true if the conversation stopped naturally.
62 63 |
# File 'lib/robot_lab/message.rb', line 62 def stopped? = stop_reason == "stop" # @return [Boolean] true if the conversation stopped for a tool call |
#system? ⇒ Boolean
Returns true if this is a system message.
55 56 |
# File 'lib/robot_lab/message.rb', line 55 def system? = role == "system" # @return [Boolean] true if this is a user message |
#text? ⇒ Boolean
Returns true if this is a text message.
48 49 |
# File 'lib/robot_lab/message.rb', line 48 def text? = type == "text" # @return [Boolean] true if this is a tool call message |
#to_h ⇒ Hash
Converts the message to a hash representation.
69 70 71 72 73 74 75 76 |
# File 'lib/robot_lab/message.rb', line 69 def to_h { type: type, role: role, content: content, stop_reason: stop_reason }.compact end |
#to_json(*args) ⇒ String
Converts the message to JSON.
82 83 84 |
# File 'lib/robot_lab/message.rb', line 82 def to_json(*args) to_h.to_json(*args) end |
#tool_call? ⇒ Boolean
Returns true if this is a tool call message.
50 51 |
# File 'lib/robot_lab/message.rb', line 50 def tool_call? = type == "tool_call" # @return [Boolean] true if this is a tool result message |
#tool_result? ⇒ Boolean
Returns true if this is a tool result message.
52 |
# File 'lib/robot_lab/message.rb', line 52 def tool_result? = type == "tool_result" |
#tool_stop? ⇒ Boolean
Returns true if the conversation stopped for a tool call.
64 |
# File 'lib/robot_lab/message.rb', line 64 def tool_stop? = stop_reason == "tool" |
#user? ⇒ Boolean
Returns true if this is a user message.
57 58 |
# File 'lib/robot_lab/message.rb', line 57 def user? = role == "user" # @return [Boolean] true if this is an assistant message |