Class: Smith::Tool

Inherits:
RubyLLM::Tool
  • Object
show all
Extended by:
CaptureConfiguration, ScopedContext
Includes:
BudgetEnforcement, Capture, ExecutionAuthorization, ExecutionLifecycle, Policy
Defined in:
lib/smith/tool.rb,
lib/smith/tool/policy.rb,
lib/smith/tool/capture.rb,
lib/smith/tool/call_batch.rb,
lib/smith/tool/invocation.rb,
lib/smith/tool/call_budget.rb,
lib/smith/tool/compatibility.rb,
lib/smith/tool/call_admission.rb,
lib/smith/tool/call_allowance.rb,
lib/smith/tool/scoped_context.rb,
lib/smith/tool/execution_batch.rb,
lib/smith/tool/call_reservation.rb,
lib/smith/tool/argument_snapshot.rb,
lib/smith/tool/execution_tracker.rb,
lib/smith/tool/budget_enforcement.rb,
lib/smith/tool/capability_builder.rb,
lib/smith/tool/execution_dispatch.rb,
lib/smith/tool/invocation_request.rb,
lib/smith/tool/execution_authority.rb,
lib/smith/tool/execution_lifecycle.rb,
lib/smith/tool/graceful_completion.rb,
lib/smith/tool/invocation_sequence.rb,
lib/smith/tool/fail_fast_completion.rb,
lib/smith/tool/capture_configuration.rb,
lib/smith/tool/execution_batch_state.rb,
lib/smith/tool/legacy_call_allowance.rb,
lib/smith/tool/call_allowance_counter.rb,
lib/smith/tool/chat_execution_context.rb,
lib/smith/tool/execution_authorization.rb,
lib/smith/tool/execution_batch_builder.rb,
lib/smith/tool/execution_batch_sources.rb,
lib/smith/tool/argument_scalar_snapshot.rb,
lib/smith/tool/argument_snapshot_result.rb,
lib/smith/tool/bounded_completion_guard.rb,
lib/smith/tool/bounded_completion_state.rb,
lib/smith/tool/chat_execution_callbacks.rb,
lib/smith/tool/execution_batch_registry.rb,
lib/smith/tool/execution_batch_requests.rb,
lib/smith/tool/argument_container_reader.rb,
lib/smith/tool/execution_batch_admission.rb,
lib/smith/tool/execution_batch_lifecycle.rb,
lib/smith/tool/bounded_completion_context.rb,
lib/smith/tool/execution_batch_collection.rb,
lib/smith/tool/execution_failure_handling.rb,
lib/smith/tool/argument_snapshot_traversal.rb,
lib/smith/tool/bounded_completion_controls.rb,
lib/smith/tool/execution_batch_invocations.rb,
lib/smith/tool/execution_batch_source_call.rb,
lib/smith/tool/argument_snapshot_accounting.rb,
lib/smith/tool/bounded_completion_installation.rb,
lib/smith/tool/execution_batch_source_metadata.rb

Defined Under Namespace

Modules: BoundedCompletionContext, BoundedCompletionControls, BoundedCompletionInstallation, BudgetEnforcement, Capture, CaptureConfiguration, ChatExecutionCallbacks, ChatExecutionContext, Compatibility, ExecutionAuthorization, ExecutionBatchLifecycle, ExecutionDispatch, ExecutionFailureHandling, ExecutionLifecycle, FailFastCompletion, GracefulCompletion, Policy, ScopedContext Classes: ArgumentContainerReader, ArgumentScalarSnapshot, ArgumentSnapshot, ArgumentSnapshotAccounting, ArgumentSnapshotResult, ArgumentSnapshotTraversal, BoundedCompletionGuard, BoundedCompletionState, CallAdmission, CallAllowance, CallAllowanceCounter, CallBatch, CallBudget, CallReservation, CapabilityBuilder, ExecutionBatch, ExecutionBatchAdmission, ExecutionBatchBuilder, ExecutionBatchCollection, ExecutionBatchInvocations, ExecutionBatchRegistry, ExecutionBatchRequests, ExecutionBatchSourceCall, ExecutionBatchSourceMetadata, ExecutionBatchSources, ExecutionBatchState, ExecutionTracker, Invocation, InvocationRequest, InvocationSequence, LegacyCallAllowance

Constant Summary

Constants included from ScopedContext

ScopedContext::CONTEXT_KEYS

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CaptureConfiguration

capture_result, capture_result_strict?

Methods included from ScopedContext

around, capture, with_call_budget, with_invocation_context

Class Attribute Details

.compatible_with_specObject (readonly)

Returns the value of attribute compatible_with_spec.



93
94
95
# File 'lib/smith/tool.rb', line 93

def compatible_with_spec
  @compatible_with_spec
end

Class Method Details

.authorize(&block) ⇒ Object



109
110
111
112
113
# File 'lib/smith/tool.rb', line 109

def authorize(&block)
  return @authorize unless block_given?

  @authorize = block
end

.before_execute(&block) ⇒ Object



115
116
117
118
119
# File 'lib/smith/tool.rb', line 115

def before_execute(&block)
  return @before_execute unless block_given?

  @before_execute = block
end

.capabilitiesObject



101
102
103
104
105
106
107
# File 'lib/smith/tool.rb', line 101

def capabilities(&)
  return @capabilities unless block_given?

  builder = CapabilityBuilder.new
  builder.instance_eval(&)
  @capabilities = builder.to_h
end

.category(value = nil) ⇒ Object



95
96
97
98
99
# File 'lib/smith/tool.rb', line 95

def category(value = nil)
  return @category if value.nil?

  @category = value
end

.compatible_with(*providers, except: nil, **provider_endpoints) ⇒ Object

Declarative compatibility DSL. Examples:

compatible_with :anthropic, :gemini
compatible_with :anthropic, :gemini, openai: :responses
compatible_with except: { openai: :chat_completions }

Tools that NEVER declare compatible_with are universally compatible. Consumed by Smith::Models::Normalizer.drop_incompatible_tools when the resolved model rejects the (tools + thinking) combo and no routing fallback (e.g., openai_api_mode :auto) is available.



89
90
91
# File 'lib/smith/tool.rb', line 89

def compatible_with(*providers, except: nil, **provider_endpoints)
  @compatible_with_spec = Compatibility.parse(providers, except: except, **provider_endpoints)
end

.inherited(subclass) ⇒ Object

Tool subclasses inherit the parent's compatible_with spec by reference (the spec is a frozen Hash; immutability makes shared references safe). Subclasses can override by calling compatible_with again — assigns a NEW frozen Hash to its own @compatible_with_spec, leaving the parent untouched.



75
76
77
78
# File 'lib/smith/tool.rb', line 75

def inherited(subclass)
  super
  subclass.instance_variable_set(:@compatible_with_spec, @compatible_with_spec)
end

Instance Method Details

#execute(**kwargs) ⇒ Object



122
123
124
125
126
127
128
129
130
131
# File 'lib/smith/tool.rb', line 122

def execute(**kwargs)
  authorize_tool_execution!
  kwargs.freeze
  prepare_tool_execution!(kwargs)
  result, duration = perform_with_duration(kwargs)

  emit_tool_trace(kwargs, result, duration)
  capture_result_if_configured(kwargs, result)
  result
end