bruteSourceBruteMiddlewareCompletionBase

class Base

Shared machinery: option/env fallback, session append, and message conversions. Subclasses implement #complete(env) returning a RubyLLM::Message.

Definitions

def option(env, key)

Point-of-use option, falling back to env.

Implementation

def option(env, key)
  @options.fetch(key) { env[key] }
end

def message_from_openai(message_hash)

Build the session message from an OpenAI-style response message ("role" => "assistant", "content" => ..., "tool_calls" => [...]). Handles string and symbol keys.

Implementation

def message_from_openai(message_hash)
  message_hash ||= {}
  content    = field(message_hash, :content)
  tool_calls = Array(field(message_hash, :tool_calls)).each_with_object({}) do |tc, hash|
    id       = field(tc, :id)
    function = field(tc, :function) || {}
    hash[id] = ::RubyLLM::ToolCall.new(
      id:        id,
      name:      field(function, :name),
      arguments: parse_arguments(field(function, :arguments)),
    )
  end

  ::RubyLLM::Message.new(
    role:       :assistant,
    content:    content,
    tool_calls: tool_calls.empty? ? nil : tool_calls,
  )
end