Class: RubyLLM::MessageTransport

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_llm/message_transport.rb

Overview

Transports the result of an LLM call into Brute's message format.

Calling an LLM is trivial — RubyLLM.chat.ask "..." — so Brute has no "completion middleware". The terminal run of an agent pipeline is an inline proc that makes the LLM call itself. The only wrinkle: RubyLLM hands back its own objects (a Message, an array, a whole Chat transcript), while the rest of the stack — the turn manager, event handlers, tool loop and SessionLog persistence — works off env[:messages] (a plain message log; see Brute.log).

This makes NO LLM call and does NO appending. It just wraps what the proc got back and yields each message in Brute's format; the proc appends:

response = provider.complete(env[:messages], ...)          # one Message
RubyLLM::MessageTransport.new(response).wrap_each do |message|
env[:messages] << message
end

before = chat.messages.length                              # a Chat loop
chat.complete
RubyLLM::MessageTransport.new(chat.messages[before..]).wrap_each do |message|
env[:messages] << message
end

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(result) ⇒ MessageTransport

Returns a new instance of MessageTransport.



36
37
38
# File 'lib/ruby_llm/message_transport.rb', line 36

def initialize(result)
  @result = result
end

Class Method Details

.wrap_each(result, &block) ⇒ Object

Convenience: RubyLLM::MessageTransport.wrap_each(result) { |m| ... }



32
33
34
# File 'lib/ruby_llm/message_transport.rb', line 32

def self.wrap_each(result, &block)
  new(result).wrap_each(&block)
end

Instance Method Details

#messagesObject

The result normalized to a flat list of messages. A single Message, an array, or anything transcript-shaped (a Chat responds to #messages).



51
52
53
54
55
56
57
# File 'lib/ruby_llm/message_transport.rb', line 51

def messages
  case @result
  when ::RubyLLM::Message then [@result]
  when Array              then @result.compact
  else @result.respond_to?(:messages) ? @result.messages : Array(@result)
  end
end

#wrap_eachObject

Yield each result message in Brute's format. Without a block, returns an Enumerator. The caller decides what to do with each (typically append to env).



43
44
45
46
47
# File 'lib/ruby_llm/message_transport.rb', line 43

def wrap_each
  return enum_for(:wrap_each) unless block_given?

  messages.each { |message| yield wrap(message) }
end