Module: RubyLLM::RedCandle::Tools

Defined in:
lib/ruby_llm/red_candle/tools.rb

Overview

Tool calling support for Red Candle provider. Bridges between RubyLLM::Tool and Candle::Tool formats.

Class Method Summary collapse

Class Method Details

.candle_tool_for(tool) ⇒ Object

Convert a RubyLLM::Tool to a Candle::Tool (without a callable block —RubyLLM manages tool execution itself)



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ruby_llm/red_candle/tools.rb', line 14

def candle_tool_for(tool)
  parameters = tool.params_schema ||
               RubyLLM::Tool::SchemaDefinition.from_parameters(tool.parameters)&.json_schema ||
               { type: "object", properties: {}, required: [] }

  ::Candle::Tool.new(
    name: tool.name,
    description: tool.description || "",
    parameters: parameters
  ) { |_args| nil } # No-op block — RubyLLM handles execution
end

.format_tool_call(msg) ⇒ Object

Format a tool call message (assistant message with tool_calls) for sending back to the model. Injects tool calls into the content.



45
46
47
48
49
50
51
# File 'lib/ruby_llm/red_candle/tools.rb', line 45

def format_tool_call(msg)
  content = msg.content.to_s
  msg.tool_calls&.each_value do |tc|
    content += "\n<tool_call>\n#{JSON.generate({ name: tc.name, arguments: tc.arguments })}\n</tool_call>"
  end
  { role: "assistant", content: content }
end

.format_tool_result(msg) ⇒ Object

Format a tool result message for sending back to the model



54
55
56
# File 'lib/ruby_llm/red_candle/tools.rb', line 54

def format_tool_result(msg)
  { role: "tool", content: msg.content.to_s }
end

.parse_tool_calls(candle_tool_calls) ⇒ Object

Convert Candle::ToolCall objects to RubyLLM tool_calls hash format RubyLLM expects: { “call_id” => RubyLLM::ToolCall, … }



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ruby_llm/red_candle/tools.rb', line 28

def parse_tool_calls(candle_tool_calls)
  return nil if candle_tool_calls.nil? || candle_tool_calls.empty?

  tool_calls = {}
  candle_tool_calls.each do |tc|
    call_id = "call_#{SecureRandom.hex(12)}"
    tool_calls[call_id] = RubyLLM::ToolCall.new(
      id: call_id,
      name: tc.name,
      arguments: tc.arguments
    )
  end
  tool_calls
end