Class: RobotLab::UserMessage
- Inherits:
-
Object
- Object
- RobotLab::UserMessage
- Defined in:
- lib/robot_lab/user_message.rb
Overview
Enhanced user message with metadata and system prompt augmentation
UserMessage wraps the user’s input with additional context like thread ID, system prompt additions, and other metadata that can influence robot behavior.
Instance Attribute Summary collapse
-
#content ⇒ String
readonly
The message content.
-
#created_at ⇒ Object
readonly
Returns the value of attribute created_at.
-
#id ⇒ String
readonly
Unique message identifier.
-
#metadata ⇒ Hash
readonly
Additional metadata.
-
#session_id ⇒ String?
readonly
The conversation thread identifier.
-
#system_prompt ⇒ String?
readonly
Additional system prompt to inject.
Class Method Summary collapse
-
.from(input) ⇒ UserMessage
Create from string or hash.
Instance Method Summary collapse
-
#initialize(content, session_id: nil, system_prompt: nil, metadata: nil, id: nil) ⇒ UserMessage
constructor
Creates a new UserMessage instance.
-
#to_h ⇒ Hash
Converts the message to a hash representation.
-
#to_json(*args) ⇒ String
Converts the message to JSON.
-
#to_message ⇒ TextMessage
Convert to a simple text message for the conversation.
-
#to_s ⇒ String
Get the string content (for compatibility with String inputs).
Constructor Details
#initialize(content, session_id: nil, system_prompt: nil, metadata: nil, id: nil) ⇒ UserMessage
Creates a new UserMessage instance.
44 45 46 47 48 49 50 51 |
# File 'lib/robot_lab/user_message.rb', line 44 def initialize(content, session_id: nil, system_prompt: nil, metadata: nil, id: nil) @content = content.to_s @session_id = session_id @system_prompt = system_prompt @metadata = || {} @id = id || SecureRandom.uuid @created_at = Time.now end |
Instance Attribute Details
#content ⇒ String (readonly)
Returns the message content.
35 36 37 |
# File 'lib/robot_lab/user_message.rb', line 35 def content @content end |
#created_at ⇒ Object (readonly)
Returns the value of attribute created_at.
35 |
# File 'lib/robot_lab/user_message.rb', line 35 attr_reader :content, :session_id, :system_prompt, :metadata, :id, :created_at |
#id ⇒ String (readonly)
Returns unique message identifier.
35 |
# File 'lib/robot_lab/user_message.rb', line 35 attr_reader :content, :session_id, :system_prompt, :metadata, :id, :created_at |
#metadata ⇒ Hash (readonly)
Returns additional metadata.
35 |
# File 'lib/robot_lab/user_message.rb', line 35 attr_reader :content, :session_id, :system_prompt, :metadata, :id, :created_at |
#session_id ⇒ String? (readonly)
Returns the conversation thread identifier.
35 |
# File 'lib/robot_lab/user_message.rb', line 35 attr_reader :content, :session_id, :system_prompt, :metadata, :id, :created_at |
#system_prompt ⇒ String? (readonly)
Returns additional system prompt to inject.
35 |
# File 'lib/robot_lab/user_message.rb', line 35 attr_reader :content, :session_id, :system_prompt, :metadata, :id, :created_at |
Class Method Details
.from(input) ⇒ UserMessage
Create from string or hash
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/robot_lab/user_message.rb', line 96 def self.from(input) case input when UserMessage input when String new(input) when Hash input = input.transform_keys(&:to_sym) new( input[:content], session_id: input[:session_id], system_prompt: input[:system_prompt], metadata: input[:metadata], id: input[:id] ) when TextMessage new(input.content) else new(input.to_s) end end |
Instance Method Details
#to_h ⇒ Hash
Converts the message to a hash representation.
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/robot_lab/user_message.rb', line 72 def to_h { content: content, session_id: session_id, system_prompt: system_prompt, metadata: , id: id, created_at: created_at.iso8601 }.compact end |
#to_json(*args) ⇒ String
Converts the message to JSON.
87 88 89 |
# File 'lib/robot_lab/user_message.rb', line 87 def to_json(*args) to_h.to_json(*args) end |
#to_message ⇒ TextMessage
Convert to a simple text message for the conversation
57 58 59 |
# File 'lib/robot_lab/user_message.rb', line 57 def RobotLab::TextMessage.new(role: "user", content: content) end |
#to_s ⇒ String
Get the string content (for compatibility with String inputs)
65 66 67 |
# File 'lib/robot_lab/user_message.rb', line 65 def to_s content end |