Class: RobotLab::Message Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/robot_lab/message.rb

Overview

This class is abstract.

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

TextMessage, ToolCallMessage, ToolResultMessage

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, role:, content:, stop_reason: nil) ⇒ Message

Creates a new Message instance.

Parameters:

  • type (String, Symbol)

    the message type

  • role (String, Symbol)

    the message role

  • content (String, Hash, nil)

    the message content

  • stop_reason (String, Symbol, nil) (defaults to: nil)

    the stop reason

Raises:

  • (ArgumentError)

    if type, role, or stop_reason is invalid



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

#contentString, ... (readonly)

Returns the message content.

Returns:

  • (String, Hash, nil)

    the message content



27
# File 'lib/robot_lab/message.rb', line 27

attr_reader :type, :role, :content, :stop_reason

#roleString (readonly)

Returns the message role (system, user, assistant, or tool_result).

Returns:

  • (String)

    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_reasonObject (readonly)

Returns the value of attribute stop_reason.



27
# File 'lib/robot_lab/message.rb', line 27

attr_reader :type, :role, :content, :stop_reason

#typeString (readonly)

Returns the message type (text, tool_call, or tool_result).

Returns:

  • (String)

    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.

Parameters:

  • hash (Hash)

    the hash representation of a message

Returns:

  • (Message)

    the appropriate Message subclass instance



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.

Returns:

  • (Boolean)

    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.

Returns:

  • (Boolean)

    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.

Returns:

  • (Boolean)

    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.

Returns:

  • (Boolean)

    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_hHash

Converts the message to a hash representation.

Returns:

  • (Hash)

    a hash containing the message data



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.

Parameters:

  • args (Array)

    arguments passed to to_json

Returns:

  • (String)

    JSON representation of the message



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.

Returns:

  • (Boolean)

    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.

Returns:

  • (Boolean)

    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.

Returns:

  • (Boolean)

    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.

Returns:

  • (Boolean)

    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