Class: Ragents::Message
- Inherits:
-
Object
- Object
- Ragents::Message
- Defined in:
- lib/ragents/message.rb
Overview
Immutable message value object for conversation history. Messages are Ractor-shareable when frozen.
Constant Summary collapse
- ROLES =
%i[system user assistant tool].freeze
Instance Attribute Summary collapse
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#role ⇒ Object
readonly
Returns the value of attribute role.
-
#tool_call_id ⇒ Object
readonly
Returns the value of attribute tool_call_id.
-
#tool_calls ⇒ Object
readonly
Returns the value of attribute tool_calls.
Class Method Summary collapse
-
.assistant(content, tool_calls: nil, **metadata) ⇒ Object
Create an assistant message.
-
.make_shareable(message) ⇒ Object
Make Ractor-shareable.
-
.system(content, **metadata) ⇒ Object
Create a system message.
-
.tool(content, tool_call_id:, name: nil, **metadata) ⇒ Object
Create a tool result message.
-
.user(content, **metadata) ⇒ Object
Create a user message.
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #assistant? ⇒ Boolean
- #has_tool_calls? ⇒ Boolean
- #hash ⇒ Object
-
#initialize(role:, content: nil, tool_calls: nil, tool_call_id: nil, name: nil, metadata: {}) ⇒ Message
constructor
A new instance of Message.
- #system? ⇒ Boolean
-
#to_h ⇒ Object
Convert to hash for serialization.
- #tool? ⇒ Boolean
- #user? ⇒ Boolean
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
#content ⇒ Object (readonly)
Returns the value of attribute content.
15 16 17 |
# File 'lib/ragents/message.rb', line 15 def content @content end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
15 16 17 |
# File 'lib/ragents/message.rb', line 15 def @metadata end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
15 16 17 |
# File 'lib/ragents/message.rb', line 15 def name @name end |
#role ⇒ Object (readonly)
Returns the value of attribute role.
15 16 17 |
# File 'lib/ragents/message.rb', line 15 def role @role end |
#tool_call_id ⇒ Object (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_calls ⇒ Object (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() Ractor.make_shareable() 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
56 57 58 |
# File 'lib/ragents/message.rb', line 56 def assistant? role == :assistant end |
#has_tool_calls? ⇒ Boolean
64 65 66 |
# File 'lib/ragents/message.rb', line 64 def has_tool_calls? !tool_calls.nil? && !tool_calls.empty? end |
#hash ⇒ Object
91 92 93 |
# File 'lib/ragents/message.rb', line 91 def hash [role, content, tool_calls, tool_call_id, name].hash end |
#system? ⇒ Boolean
48 49 50 |
# File 'lib/ragents/message.rb', line 48 def system? role == :system end |
#to_h ⇒ Object
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
60 61 62 |
# File 'lib/ragents/message.rb', line 60 def tool? role == :tool end |
#user? ⇒ Boolean
52 53 54 |
# File 'lib/ragents/message.rb', line 52 def user? role == :user end |