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
32
33
34
35
36
37
# File 'lib/brute/messages.rb', line 26

def initialize(role:, content: nil, tool_calls: nil, tool_call_id: nil)
  formatted_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: formatted_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

Clean, JSON-ready hash export dropping nil values



43
44
45
46
47
# File 'lib/brute/messages.rb', line 43

def to_h(...)
  hash = super
  hash[:tool_calls] = tool_calls.map(&:to_h) if tool_calls
  hash.compact
end

#tool_call?Boolean Also known as: has_tool_calls?

Returns:

  • (Boolean)


39
# File 'lib/brute/messages.rb', line 39

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