Module: OpenRouter::ToolCallBase

Included in:
ResponsesToolCall, ToolCall
Defined in:
lib/open_router/tool_call_base.rb

Overview

Shared behavior for tool call parsing across different API formats. Include this module and define ‘name` and `arguments_string` accessors.

Instance Method Summary collapse

Instance Method Details

#argumentsObject

Parse the arguments JSON string into a Ruby hash



12
13
14
15
16
17
18
# File 'lib/open_router/tool_call_base.rb', line 12

def arguments
  @arguments ||= begin
    JSON.parse(arguments_string)
  rescue JSON::ParserError => e
    raise ToolCallError, "Failed to parse tool call arguments: #{e.message}"
  end
end

#build_result(_result, _error = nil) ⇒ Object

Subclasses must implement this to return the appropriate result type

Raises:

  • (NotImplementedError)


37
38
39
# File 'lib/open_router/tool_call_base.rb', line 37

def build_result(_result, _error = nil)
  raise NotImplementedError, "Subclasses must implement build_result"
end

#execute {|name, arguments| ... } ⇒ ToolResultBase

Execute the tool call with a provided block The block receives (name, arguments) and should return the result

Yields:

  • (name, arguments)

    Block to execute the tool

Returns:

Raises:

  • (ArgumentError)


25
26
27
28
29
30
31
32
33
34
# File 'lib/open_router/tool_call_base.rb', line 25

def execute(&block)
  raise ArgumentError, "Block required for tool execution" unless block_given?

  begin
    result = block.call(name, arguments)
    build_result(result)
  rescue StandardError => e
    build_result(nil, e.message)
  end
end