Class: Brute::Message

Inherits:
Data
  • Object
show all
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

Instance Method Summary collapse

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

#contentObject (readonly)

Returns the value of attribute content

Returns:

  • (Object)

    the current value of content



25
26
27
# File 'lib/brute/messages.rb', line 25

def content
  @content
end

#roleObject (readonly)

Returns the value of attribute role

Returns:

  • (Object)

    the current value of role



25
26
27
# File 'lib/brute/messages.rb', line 25

def role
  @role
end

#tool_call_idObject (readonly)

Returns the value of attribute tool_call_id

Returns:

  • (Object)

    the current value of tool_call_id



25
26
27
# File 'lib/brute/messages.rb', line 25

def tool_call_id
  @tool_call_id
end

#tool_callsObject (readonly)

Returns the value of attribute tool_calls

Returns:

  • (Object)

    the current value of tool_calls



25
26
27
# File 'lib/brute/messages.rb', line 25

def tool_calls
  @tool_calls
end

Instance Method Details

#to_hObject

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

Returns:

  • (Boolean)


33
34
35
# File 'lib/brute/messages.rb', line 33

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