Class: Brute::Message
- Inherits:
-
Data
- Object
- Data
- Brute::Message
- Defined in:
- lib/brute/messages.rb
Overview
Brute's canonical, framework-agnostic message. The rest of the stack (middleware, tool loop, persistence) never calls anything beyond #role, #content, #tool_calls, #tool_call_id and #to_h — so any object that duck-types those methods can ride in env too. This Data class is simply the canonical implementation.
Brute::Message.new(role: :user, content: "hi")
Brute::Message.new(role: :assistant, content: "", tool_calls: [
Brute::ToolCall.new(id: "tc1", name: "shell", arguments: { "command" => "ls" }),
])
Brute::Message.new(role: :tool, content: "result", tool_call_id: "tc1")
Instance Attribute Summary collapse
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#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.
Instance Method Summary collapse
-
#initialize(role:, content: nil, tool_calls: nil, tool_call_id: nil) ⇒ Message
constructor
A new instance of Message.
-
#to_h ⇒ Object
Plain, JSON-ready view (nils dropped, tool calls as hashes).
- #tool_call? ⇒ Boolean
Constructor Details
#initialize(role:, content: nil, tool_calls: nil, tool_call_id: nil) ⇒ Message
Returns a new instance of Message.
26 27 28 29 30 31 |
# File 'lib/brute/messages.rb', line 26 def initialize(role:, content: nil, tool_calls: nil, tool_call_id: nil) tool_calls = tool_calls&.map do |tc| tc.is_a?(ToolCall) ? tc : ToolCall.new(**tc.to_h.transform_keys(&:to_sym)) end super(role: role.to_sym, content: content, tool_calls: tool_calls, tool_call_id: tool_call_id) end |
Instance Attribute Details
#content ⇒ Object (readonly)
Returns the value of attribute content
25 26 27 |
# File 'lib/brute/messages.rb', line 25 def content @content end |
#role ⇒ Object (readonly)
Returns the value of attribute role
25 26 27 |
# File 'lib/brute/messages.rb', line 25 def role @role end |
#tool_call_id ⇒ Object (readonly)
Returns the value of attribute tool_call_id
25 26 27 |
# File 'lib/brute/messages.rb', line 25 def tool_call_id @tool_call_id end |
#tool_calls ⇒ Object (readonly)
Returns the value of attribute tool_calls
25 26 27 |
# File 'lib/brute/messages.rb', line 25 def tool_calls @tool_calls end |
Instance Method Details
#to_h ⇒ Object
Plain, JSON-ready view (nils dropped, tool calls as hashes).
38 39 40 41 42 |
# File 'lib/brute/messages.rb', line 38 def to_h h = super h[:tool_calls] = tool_calls.map(&:to_h) if tool_calls h.compact end |
#tool_call? ⇒ Boolean
33 34 35 |
# File 'lib/brute/messages.rb', line 33 def tool_call? !tool_calls.nil? && !tool_calls.empty? end |