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
-
#arguments ⇒ Object
Parse the arguments JSON string into a Ruby hash.
-
#build_result(_result, _error = nil) ⇒ Object
Subclasses must implement this to return the appropriate result type.
-
#execute {|name, arguments| ... } ⇒ ToolResultBase
Execute the tool call with a provided block The block receives (name, arguments) and should return the result.
Instance Method Details
#arguments ⇒ Object
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.}" end end |
#build_result(_result, _error = nil) ⇒ Object
Subclasses must implement this to return the appropriate result type
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
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.) end end |