Class: Pikuri::Agent::History::Message
- Inherits:
-
Data
- Object
- Data
- Pikuri::Agent::History::Message
- Defined in:
- lib/pikuri/agent/history.rb
Overview
One message. id and kind are pikuri's own and never reach a
provider; everything else is what the model saw.
content is always a String (empty on a pure tool-call turn, which is
what ruby_llm stores); anything the model was shown beyond text is an
Attachment in attachments.
Instance Attribute Summary collapse
-
#attachments ⇒ Object
readonly
Returns the value of attribute attachments.
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#kind ⇒ Object
readonly
Returns the value of attribute kind.
-
#model_id ⇒ Object
readonly
Returns the value of attribute model_id.
-
#role ⇒ Object
readonly
Returns the value of attribute role.
-
#thinking ⇒ Object
readonly
Returns the value of attribute thinking.
-
#tokens ⇒ Object
readonly
Returns the value of attribute tokens.
-
#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
-
.from_ruby_llm(msg, id:, kind: 'user') ⇒ Message
Snapshot a live
RubyLLM::Message.
Instance Method Summary collapse
-
#initialize(id:, role:, content:, kind: 'user', attachments: [], tool_calls: [], tool_call_id: nil, model_id: nil, thinking: nil, tokens: nil) ⇒ Message
constructor
A new instance of Message.
-
#to_ruby_llm(keep_thinking: true) ⇒ RubyLLM::Message
Rebuild the
RubyLLM::Messagethis was taken from. -
#tool_call? ⇒ Boolean
Whether this message asked for tools.
Constructor Details
#initialize(id:, role:, content:, kind: 'user', attachments: [], tool_calls: [], tool_call_id: nil, model_id: nil, thinking: nil, tokens: nil) ⇒ Message
Returns a new instance of Message.
158 159 160 161 |
# File 'lib/pikuri/agent/history.rb', line 158 def initialize(id:, role:, content:, kind: 'user', attachments: [], tool_calls: [], tool_call_id: nil, model_id: nil, thinking: nil, tokens: nil) super end |
Instance Attribute Details
#attachments ⇒ Object (readonly)
Returns the value of attribute attachments
143 144 145 |
# File 'lib/pikuri/agent/history.rb', line 143 def @attachments end |
#content ⇒ Object (readonly)
Returns the value of attribute content
143 144 145 |
# File 'lib/pikuri/agent/history.rb', line 143 def content @content end |
#id ⇒ Object (readonly)
Returns the value of attribute id
143 144 145 |
# File 'lib/pikuri/agent/history.rb', line 143 def id @id end |
#kind ⇒ Object (readonly)
Returns the value of attribute kind
143 144 145 |
# File 'lib/pikuri/agent/history.rb', line 143 def kind @kind end |
#model_id ⇒ Object (readonly)
Returns the value of attribute model_id
143 144 145 |
# File 'lib/pikuri/agent/history.rb', line 143 def model_id @model_id end |
#role ⇒ Object (readonly)
Returns the value of attribute role
143 144 145 |
# File 'lib/pikuri/agent/history.rb', line 143 def role @role end |
#thinking ⇒ Object (readonly)
Returns the value of attribute thinking
143 144 145 |
# File 'lib/pikuri/agent/history.rb', line 143 def thinking @thinking end |
#tokens ⇒ Object (readonly)
Returns the value of attribute tokens
143 144 145 |
# File 'lib/pikuri/agent/history.rb', line 143 def tokens @tokens end |
#tool_call_id ⇒ Object (readonly)
Returns the value of attribute tool_call_id
143 144 145 |
# File 'lib/pikuri/agent/history.rb', line 143 def tool_call_id @tool_call_id end |
#tool_calls ⇒ Object (readonly)
Returns the value of attribute tool_calls
143 144 145 |
# File 'lib/pikuri/agent/history.rb', line 143 def tool_calls @tool_calls end |
Class Method Details
.from_ruby_llm(msg, id:, kind: 'user') ⇒ Message
Snapshot a live RubyLLM::Message. Attachment bytes are read now
— the file a tool showed the model may be gone or changed by the
time anyone loads this back.
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/pikuri/agent/history.rb', line 174 def self.from_ruby_llm(msg, id:, kind: 'user') text, = split_content(msg.content) new( id: id, role: msg.role.to_s, kind: kind, content: text, attachments: , tool_calls: (msg.tool_calls || {}).values.map do |c| ToolCall.new(id: c.id, name: c.name, arguments: c.arguments || {}) end, tool_call_id: msg.tool_call_id, model_id: msg.model_id, thinking: msg.thinking && Thinking.new(text: msg.thinking.text, signature: msg.thinking.signature), tokens: msg.tokens && Tokens.new( input: msg.tokens.input, output: msg.tokens.output, cached: msg.tokens.cached, cache_creation: msg.tokens.cache_creation, thinking: msg.tokens.thinking ) ) end |
Instance Method Details
#to_ruby_llm(keep_thinking: true) ⇒ RubyLLM::Message
Rebuild the RubyLLM::Message this was taken from.
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/pikuri/agent/history.rb', line 213 def to_ruby_llm(keep_thinking: true) calls = tool_calls.to_h do |c| [c.id, RubyLLM::ToolCall.new(id: c.id, name: c.name, arguments: c.arguments)] end replayed = thinking if keep_thinking RubyLLM::Message.new( role: role.to_sym, content: ruby_llm_content, tool_calls: calls.empty? ? nil : calls, tool_call_id: tool_call_id, model_id: model_id, thinking: replayed && RubyLLM::Thinking.build( text: replayed.text, signature: replayed.signature ), tokens: tokens && RubyLLM::Tokens.build(**tokens.to_h) ) end |
#tool_call? ⇒ Boolean
Returns whether this message asked for tools.
164 |
# File 'lib/pikuri/agent/history.rb', line 164 def tool_call? = !tool_calls.empty? |