Class: OpenRouter::ToolCall
- Inherits:
-
Object
- Object
- OpenRouter::ToolCall
- Defined in:
- lib/open_router/tool_call.rb
Instance Attribute Summary collapse
-
#arguments_string ⇒ Object
readonly
Returns the value of attribute arguments_string.
-
#function_name ⇒ Object
readonly
Returns the value of attribute function_name.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#arguments ⇒ Object
Parse the arguments JSON string into a Ruby hash.
-
#execute(&block) ⇒ Object
Execute the tool call with a provided block The block should accept (name, arguments) and return the result.
-
#initialize(tool_call_data) ⇒ ToolCall
constructor
A new instance of ToolCall.
-
#name ⇒ Object
Get the function name (alias for consistency).
- #to_h ⇒ Object
- #to_json(*args) ⇒ Object
-
#to_message ⇒ Object
Convert this tool call to a message format for conversation continuation.
-
#to_result_message(result) ⇒ Object
Convert a tool result to a tool message for the conversation.
-
#valid?(tools:) ⇒ Boolean
Validate against a provided array of tools (Tool instances or hashes).
- #validation_errors(tools:) ⇒ Object
Constructor Details
#initialize(tool_call_data) ⇒ ToolCall
Returns a new instance of ToolCall.
11 12 13 14 15 16 17 18 19 |
# File 'lib/open_router/tool_call.rb', line 11 def initialize(tool_call_data) @id = tool_call_data["id"] @type = tool_call_data["type"] raise ToolCallError, "Invalid tool call data: missing function" unless tool_call_data["function"] @function_name = tool_call_data["function"]["name"] @arguments_string = tool_call_data["function"]["arguments"] end |
Instance Attribute Details
#arguments_string ⇒ Object (readonly)
Returns the value of attribute arguments_string.
9 10 11 |
# File 'lib/open_router/tool_call.rb', line 9 def arguments_string @arguments_string end |
#function_name ⇒ Object (readonly)
Returns the value of attribute function_name.
9 10 11 |
# File 'lib/open_router/tool_call.rb', line 9 def function_name @function_name end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
9 10 11 |
# File 'lib/open_router/tool_call.rb', line 9 def id @id end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
9 10 11 |
# File 'lib/open_router/tool_call.rb', line 9 def type @type end |
Instance Method Details
#arguments ⇒ Object
Parse the arguments JSON string into a Ruby hash
22 23 24 25 26 27 28 |
# File 'lib/open_router/tool_call.rb', line 22 def arguments @arguments ||= begin JSON.parse(@arguments_string) rescue JSON::ParserError => e raise ToolCallError, "Failed to parse tool call arguments: #{e.}" end end |
#execute(&block) ⇒ Object
Execute the tool call with a provided block The block should accept (name, arguments) and return the result
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/open_router/tool_call.rb', line 37 def execute(&block) raise ArgumentError, "Block required for tool execution" unless block_given? begin result = block.call(@function_name, arguments) ToolResult.new(self, result) rescue StandardError => e ToolResult.new(self, nil, e.) end end |
#name ⇒ Object
Get the function name (alias for consistency)
31 32 33 |
# File 'lib/open_router/tool_call.rb', line 31 def name @function_name end |
#to_h ⇒ Object
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/open_router/tool_call.rb', line 81 def to_h { id: @id, type: @type, function: { name: @function_name, arguments: @arguments_string } } end |
#to_json(*args) ⇒ Object
92 93 94 |
# File 'lib/open_router/tool_call.rb', line 92 def to_json(*args) to_h.to_json(*args) end |
#to_message ⇒ Object
Convert this tool call to a message format for conversation continuation
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/open_router/tool_call.rb', line 49 def { role: "assistant", content: nil, tool_calls: [ { id: @id, type: @type, function: { name: @function_name, arguments: @arguments_string } } ] } end |
#to_result_message(result) ⇒ Object
Convert a tool result to a tool message for the conversation
67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/open_router/tool_call.rb', line 67 def (result) content = case result when String then result when nil then "" else result.to_json end { role: "tool", tool_call_id: @id, content: content } end |
#valid?(tools:) ⇒ Boolean
Validate against a provided array of tools (Tool instances or hashes)
97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/open_router/tool_call.rb', line 97 def valid?(tools:) # tools is now a required keyword argument schema = find_schema_for_call(tools) return true unless schema # No validation if tool not found return JSON::Validator.validate(schema, arguments) if validation_available? # Fallback: shallow required check required = Array(schema[:required]).map(&:to_s) required.all? { |k| arguments.key?(k) } rescue StandardError false end |
#validation_errors(tools:) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/open_router/tool_call.rb', line 112 def validation_errors(tools:) # tools is now a required keyword argument schema = find_schema_for_call(tools) return [] unless schema # No errors if tool not found return JSON::Validator.fully_validate(schema, arguments) if validation_available? # Fallback: check required fields required = Array(schema[:required]).map(&:to_s) missing = required.reject { |k| arguments.key?(k) } missing.any? ? ["Missing required keys: #{missing.join(", ")}"] : [] rescue StandardError => e ["Validation error: #{e.}"] end |