class RubyLLM::ToolCall::Collection
The tool calls of a Message: an Enumerable of ToolCall objects in the order the provider returned them, also readable by call ID. Empty on a message without calls.
response.tool_calls.first response.tool_calls.map(&:name) # => ["search_issues"] response.tool_calls["call_1"]
Public Instance Methods
Source
# File lib/ruby_llm/tool_call/collection.rb, line 104 def ==(other) other.respond_to?(:to_hash) && @calls == other.to_hash end
Returns true when other is a Collection or Hash of the same calls under the same IDs.
Source
# File lib/ruby_llm/tool_call/collection.rb, line 43 def [](id) @calls[key_for(id)] end
Returns the ToolCall with the ID id, or nil.
Source
# File lib/ruby_llm/tool_call/collection.rb, line 115 def as_json(*) @calls.respond_to?(:as_json) ? @calls.as_json(*) : to_h end
Source
# File lib/ruby_llm/tool_call/collection.rb, line 25 def each(&) return enum_for(__method__) { size } unless block_given? @calls.each_value(&) self end
Yields each ToolCall, in provider order.
response.tool_calls.each { |call| puts call.name }
Source
# File lib/ruby_llm/tool_call/collection.rb, line 35 def each_key(&) return enum_for(__method__) { size } unless block_given? @calls.each_key(&) self end
Yields each key, in provider order: the call ID on a complete message, and on a streamed Chunk the stream index when the provider sends one.
Source
# File lib/ruby_llm/tool_call/collection.rb, line 64 def empty? @calls.empty? end
Returns true when there are no tool calls.
Source
# File lib/ruby_llm/tool_call/collection.rb, line 90 def except(*ids) self.class.new(@calls.except(*ids.map { |id| key_for(id) })) end
Returns a Collection without the calls with the given IDs.
Source
# File lib/ruby_llm/tool_call/collection.rb, line 53 def fetch(id, ...) @calls.fetch(key_for(id), ...) end
Returns the ToolCall with the ID id. Without a default or a block, raises KeyError when there is none.
Source
# File lib/ruby_llm/tool_call/collection.rb, line 79 def key?(id) @calls.key?(key_for(id)) end
Returns true when there is a tool call with the ID id.
Source
# File lib/ruby_llm/tool_call/collection.rb, line 74 def keys @calls.keys end
Returns the keys as an Array, as each_key yields them.
Source
# File lib/ruby_llm/tool_call/collection.rb, line 58 def size @calls.size end
Returns the number of tool calls.
Source
# File lib/ruby_llm/tool_call/collection.rb, line 97 def to_h(&block) block ? @calls.to_h(&block) : @calls.dup end
Returns the tool calls as a new Hash of ToolCall objects keyed by call ID. With a block, returns the Hash of the pairs it makes from each ID and ToolCall.
Source
# File lib/ruby_llm/tool_call/collection.rb, line 109 def to_json(*) @calls.to_json(*) end
Returns the JSON of to_h.
Source
# File lib/ruby_llm/tool_call/collection.rb, line 85 def transform_values(&) @calls.transform_values(&) end
Returns a Hash of what the block returns for each ToolCall, keyed by call ID.
Source
# File lib/ruby_llm/tool_call/collection.rb, line 69 def values @calls.values end
Returns the tool calls as an Array.