Class: Ragents::Message

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

Overview

Immutable message value object for conversation history. Messages are Ractor-shareable when frozen.

Examples:

message = Ragents::Message.new(role: :user, content: "Hello")
message.role     # => :user
message.content  # => "Hello"

Constant Summary collapse

ROLES =
%i[system user assistant tool].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(role:, content: nil, tool_calls: nil, tool_call_id: nil, name: nil, metadata: {}) ⇒ Message

Returns a new instance of Message.



17
18
19
20
21
22
23
24
25
26
# File 'lib/ragents/message.rb', line 17

def initialize(role:, content: nil, tool_calls: nil, tool_call_id: nil, name: nil, metadata: {})
  @role = validate_role!(role)
  @content = content&.freeze
  @tool_calls = tool_calls&.map(&:freeze)&.freeze
  @tool_call_id = tool_call_id&.freeze
  @name = name&.freeze
  @metadata = .freeze

  freeze
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



15
16
17
# File 'lib/ragents/message.rb', line 15

def content
  @content
end

#metadataObject (readonly)

Returns the value of attribute metadata.



15
16
17
# File 'lib/ragents/message.rb', line 15

def 
  @metadata
end

#nameObject (readonly)

Returns the value of attribute name.



15
16
17
# File 'lib/ragents/message.rb', line 15

def name
  @name
end

#roleObject (readonly)

Returns the value of attribute role.



15
16
17
# File 'lib/ragents/message.rb', line 15

def role
  @role
end

#tool_call_idObject (readonly)

Returns the value of attribute tool_call_id.



15
16
17
# File 'lib/ragents/message.rb', line 15

def tool_call_id
  @tool_call_id
end

#tool_callsObject (readonly)

Returns the value of attribute tool_calls.



15
16
17
# File 'lib/ragents/message.rb', line 15

def tool_calls
  @tool_calls
end

Class Method Details

.assistant(content, tool_calls: nil, **metadata) ⇒ Object

Create an assistant message



39
40
41
# File 'lib/ragents/message.rb', line 39

def self.assistant(content, tool_calls: nil, **)
  new(role: :assistant, content: content, tool_calls: tool_calls, metadata: )
end

.make_shareable(message) ⇒ Object

Make Ractor-shareable



96
97
98
# File 'lib/ragents/message.rb', line 96

def self.make_shareable(message)
  Ractor.make_shareable(message)
end

.system(content, **metadata) ⇒ Object

Create a system message



29
30
31
# File 'lib/ragents/message.rb', line 29

def self.system(content, **)
  new(role: :system, content: content, metadata: )
end

.tool(content, tool_call_id:, name: nil, **metadata) ⇒ Object

Create a tool result message



44
45
46
# File 'lib/ragents/message.rb', line 44

def self.tool(content, tool_call_id:, name: nil, **)
  new(role: :tool, content: content, tool_call_id: tool_call_id, name: name, metadata: )
end

.user(content, **metadata) ⇒ Object

Create a user message



34
35
36
# File 'lib/ragents/message.rb', line 34

def self.user(content, **)
  new(role: :user, content: content, metadata: )
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



79
80
81
82
83
84
85
86
87
# File 'lib/ragents/message.rb', line 79

def ==(other)
  return false unless other.is_a?(Message)

  role == other.role &&
    content == other.content &&
    tool_calls == other.tool_calls &&
    tool_call_id == other.tool_call_id &&
    name == other.name
end

#assistant?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/ragents/message.rb', line 56

def assistant?
  role == :assistant
end

#has_tool_calls?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/ragents/message.rb', line 64

def has_tool_calls?
  !tool_calls.nil? && !tool_calls.empty?
end

#hashObject



91
92
93
# File 'lib/ragents/message.rb', line 91

def hash
  [role, content, tool_calls, tool_call_id, name].hash
end

#system?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/ragents/message.rb', line 48

def system?
  role == :system
end

#to_hObject

Convert to hash for serialization



69
70
71
72
73
74
75
76
77
# File 'lib/ragents/message.rb', line 69

def to_h
  hash = { role: role }
  hash[:content] = content if content
  hash[:tool_calls] = tool_calls if tool_calls
  hash[:tool_call_id] = tool_call_id if tool_call_id
  hash[:name] = name if name
  hash[:metadata] =  unless .empty?
  hash
end

#tool?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/ragents/message.rb', line 60

def tool?
  role == :tool
end

#user?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/ragents/message.rb', line 52

def user?
  role == :user
end