Module: Rubino::LLM::ToolBridge

Defined in:
lib/rubino/llm/tool_bridge.rb

Overview

Wraps a Rubino::Tools::Base instance into a RubyLLM::Tool subclass so that ruby_llm can register it, serialize its schema to the LLM, and dispatch tool calls through our full execution pipeline.

When a ToolExecutor is provided (always the case in production), tool execution goes through:

ApprovalPolicy → tool.call() → truncation → ToolCallRepository.record

This ensures identical behavior regardless of LLM provider — there is now a single tool-execution path in the entire application.

Class Method Summary collapse

Class Method Details

.bridge_class_for(tool_name) ⇒ Object



27
28
29
30
# File 'lib/rubino/llm/tool_bridge.rb', line 27

def self.bridge_class_for(tool_name)
  @cache ||= {}
  @cache[tool_name] ||= build_class(tool_name)
end

.build_class(tool_name) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rubino/llm/tool_bridge.rb', line 32

def self.build_class(tool_name)
  klass = Class.new(::RubyLLM::Tool) do
    define_method(:name) { tool_name }

    define_method(:initialize) do |agent_tool, ui:, event_bus:, tool_executor:|
      @agent_tool    = agent_tool
      @ui            = ui
      @event_bus     = event_bus
      @tool_executor = tool_executor
    end

    define_method(:description) { @agent_tool.description }
    define_method(:params_schema) { @agent_tool.input_schema }

    define_method(:execute) do |**kwargs|
      name = @agent_tool.name
      args = kwargs.transform_keys(&:to_s)

      if @tool_executor
        # Full pipeline: approval check → tool.call → truncation → audit record
        result = @tool_executor.execute(
          name: name,
          arguments: args,
          call_id: nil
        )
        result.output
      else
        # Fallback: direct call (tests / one-shot mode without full Lifecycle)
        @event_bus&.emit(Rubino::Interaction::Events::TOOL_STARTED, name: name)
        @ui&.tool_started(name, arguments: args)

        begin
          output = @agent_tool.call(args)
          result = Rubino::Tools::Result.success(
            name: name, call_id: nil, output: output.to_s
          )
          @event_bus&.emit(Rubino::Interaction::Events::TOOL_FINISHED, name: name)
          @ui&.tool_finished(name, result: result)
          result.output
        rescue StandardError => e
          @event_bus&.emit(Rubino::Interaction::Events::TOOL_FINISHED, name: name)
          @ui&.tool_finished(name)
          "Error: #{e.message}"
        end
      end
    end
  end

  const_name = "Bridge_#{tool_name.gsub(/[^a-zA-Z0-9]/, "_")}"
  unless Rubino::LLM::ToolBridge.const_defined?(const_name, false)
    Rubino::LLM::ToolBridge.const_set(const_name, klass)
  end

  klass
end

.for(agent_tool, ui: nil, event_bus: nil, tool_executor: nil) ⇒ Object

Returns a RubyLLM::Tool instance wrapping agent_tool.



19
20
21
22
23
24
25
# File 'lib/rubino/llm/tool_bridge.rb', line 19

def self.for(agent_tool, ui: nil, event_bus: nil, tool_executor: nil)
  klass = bridge_class_for(agent_tool.name)
  klass.new(agent_tool,
            ui: ui || Rubino.ui,
            event_bus: event_bus || Rubino.event_bus,
            tool_executor: tool_executor)
end