Class: Anthropic::Helpers::Tools::Runner Private
- Inherits:
-
Object
- Object
- Anthropic::Helpers::Tools::Runner
- Defined in:
- lib/anthropic/helpers/tools/runner.rb
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Instance Attribute Summary collapse
Instance Method Summary collapse
-
#add_tools(*tools) ⇒ void
Sends the tools' definitions in
tool_additionblocks with the next request, leavingparams[:tools]and the prompt cache alone. -
#compact_before_next_turn(compaction = nil) ⇒ void
private
Compact the conversation before the model's next turn.
- #each_message {|| ... } ⇒ void private
- #each_streaming {|| ... } ⇒ void private
- #feed_messages(*messages) ⇒ void private
- #finished? ⇒ Boolean private
-
#initialize(client, params:, max_iterations: nil, compaction_control: nil) ⇒ Runner
constructor
private
A new instance of Runner.
- #next_message ⇒ Anthropic::Models::BetaMessage? private
-
#remove_tools(*tools) ⇒ void
Sends
tool_removalblocks with the next request. - #run_until_finished ⇒ Array<Anthropic::Models::BetaMessage> private
Constructor Details
#initialize(client, params:, max_iterations: nil, compaction_control: nil) ⇒ Runner
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Runner.
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 |
# File 'lib/anthropic/helpers/tools/runner.rb', line 678 def initialize(client, params:, max_iterations: nil, compaction_control: nil) @client = client @params = params.to_h reject_compaction_param!(@params) @finished = false @max_iterations = max_iterations @iteration_count = 0 @compaction_control = compaction_control @compaction_warned = false @last_response = nil @pending_tool_changes = [] @tool_overrides = {} @pending_compaction = nil @compaction_phase = nil end |
Instance Attribute Details
#params ⇒ Anthropic::Models::Beta::MessageCreateParams
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
32 33 34 |
# File 'lib/anthropic/helpers/tools/runner.rb', line 32 def params @params end |
Instance Method Details
#add_tools(*tools) ⇒ void
Sends the tools' definitions in tool_addition blocks with the next request, leaving
params[:tools] and the prompt cache alone. A BaseTool is run under its name straight away,
replacing a same-name tool even for a call already in the message being handled; a raw
definition is never run here and stops a same-name tool from running. Needs the
inline-tools-2026-09-15 beta.
68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/anthropic/helpers/tools/runner.rb', line 68 def add_tools(*tools) # Converted in place, so the originals are kept apart to pair each with its definition. request = {tools: tools.dup} Anthropic::Helpers::Messages.distill_input_schema_models!(request, strict: nil) tools.zip(request.fetch(:tools)) do |tool, definition| block = {type: :tool_addition, tool: {type: :tool_definition, definition:}} runnable = tool if tool.is_a?(Anthropic::Helpers::Tools::BaseTool) name = changed_tool_name(block.fetch(:tool)) @tool_overrides.store(name, runnable) unless name.nil? @pending_tool_changes << block end end |
#compact_before_next_turn(compaction = nil) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Compact the conversation before the model's next turn. Once the current turn has finished,
including any tool calls, the runner asks the API for a summary and replaces its messages with
the compaction response, which you get like any other message. Requires the
compact-2026-09-04 beta.
112 113 114 115 116 117 |
# File 'lib/anthropic/helpers/tools/runner.rb', line 112 def compact_before_next_turn(compaction = nil) return unless @compaction_phase.nil? check_can_compact!(params) @pending_compaction = compaction || {type: :summarize} end |
#each_message {|| ... } ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/anthropic/helpers/tools/runner.rb', line 140 def (&blk) unless block_given? raise ArgumentError.new("A block must be given to ##{__method__}") end fold do = @client.beta..create(with_helper_header(_1, StainlessHelperHeader::BETA_TOOL_RUNNER)) blk.call() [false, ] end end |
#each_streaming {|| ... } ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/anthropic/helpers/tools/runner.rb', line 153 def each_streaming(&blk) unless block_given? raise ArgumentError.new("A block must be given to ##{__method__}") end fold do stream = @client.beta..stream(with_helper_header(_1, StainlessHelperHeader::BETA_TOOL_RUNNER)) blk.call(stream) [false, stream.] ensure stream&.close end end |
#feed_messages(*messages) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
55 56 57 |
# File 'lib/anthropic/helpers/tools/runner.rb', line 55 def (*) self.params = {**params.to_h, messages: params[:messages].to_a + } end |
#finished? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
52 |
# File 'lib/anthropic/helpers/tools/runner.rb', line 52 def finished? = @finished |
#next_message ⇒ Anthropic::Models::BetaMessage?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
123 124 125 126 127 128 129 130 |
# File 'lib/anthropic/helpers/tools/runner.rb', line 123 def = nil fold do = @client.beta..create(with_helper_header(_1, StainlessHelperHeader::BETA_TOOL_RUNNER)) [true, ] end end |
#remove_tools(*tools) ⇒ void
Sends tool_removal blocks with the next request. The tools stop being run straight away,
so a call to one gets the "not found" error result. Needs the inline-tools-2026-09-15 beta.
87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/anthropic/helpers/tools/runner.rb', line 87 def remove_tools(*tools) tools.each do |tool| name = case tool in Anthropic::Helpers::Tools::BaseTool Anthropic::Helpers::Messages.tool_api_name(tool) in String | Symbol tool.to_s end @tool_overrides.store(name, nil) @pending_tool_changes << {type: :tool_removal, tool: {type: :tool_reference, name:}} end end |
#run_until_finished ⇒ Array<Anthropic::Models::BetaMessage>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
133 134 135 136 137 |
# File 'lib/anthropic/helpers/tools/runner.rb', line 133 def run_until_finished = [] each_streaming { << _1. } end |