Class: OpenRouter::ToolCall
- Inherits:
-
Object
- Object
- OpenRouter::ToolCall
- Includes:
- ToolCallBase
- Defined in:
- lib/open_router/tool_call.rb
Overview
Represents a tool/function call from the Chat Completions API. Format: tool_calls[].function.name/arguments (nested under function key)
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
-
#build_result(result, error = nil) ⇒ Object
Build result for execute method (required by ToolCallBase).
-
#initialize(tool_call_data) ⇒ ToolCall
constructor
A new instance of ToolCall.
-
#name ⇒ Object
Get the function name.
- #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
Methods included from ToolCallBase
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
#build_result(result, error = nil) ⇒ Object
Build result for execute method (required by ToolCallBase)
27 28 29 |
# File 'lib/open_router/tool_call.rb', line 27 def build_result(result, error = nil) ToolResult.new(self, result, error) end |
#name ⇒ Object
Get the function name
22 23 24 |
# File 'lib/open_router/tool_call.rb', line 22 def name @function_name end |
#to_h ⇒ Object
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/open_router/tool_call.rb', line 55 def to_h { id: @id, type: @type, function: { name: @function_name, arguments: @arguments_string } } end |
#to_json(*args) ⇒ Object
66 67 68 |
# File 'lib/open_router/tool_call.rb', line 66 def to_json(*args) to_h.to_json(*args) end |
#to_message ⇒ Object
Convert this tool call to a message format for conversation continuation
32 33 34 35 36 37 38 |
# File 'lib/open_router/tool_call.rb', line 32 def { role: "assistant", content: nil, tool_calls: [to_h] } end |
#to_result_message(result) ⇒ Object
Convert a tool result to a tool message for the conversation
41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/open_router/tool_call.rb', line 41 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)
71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/open_router/tool_call.rb', line 71 def valid?(tools:) 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
84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/open_router/tool_call.rb', line 84 def validation_errors(tools:) 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 |