Class: RubyLLM::Agents::Tool
- Inherits:
-
Tool
- Object
- Tool
- RubyLLM::Agents::Tool
- Defined in:
- lib/ruby_llm/agents/tool.rb
Overview
Base class for tools that need access to the agent’s execution context.
Inherits from RubyLLM::Tool and adds:
-
‘context` accessor: read agent params, tenant, execution ID
-
‘timeout` DSL: per-tool timeout in seconds
-
Error handling: exceptions become error strings for the LLM
-
Tool execution tracking: records each tool call in the database
Users implement ‘execute()` — the standard RubyLLM convention. This class overrides `call()` to wrap execution with its features.
Instance Attribute Summary collapse
-
#context ⇒ ToolContext?
readonly
The execution context, set before each call.
Class Method Summary collapse
-
.timeout(value = nil) ⇒ Integer?
Sets or gets the per-tool timeout in seconds.
Instance Method Summary collapse
-
#call(args) ⇒ String, Tool::Halt
Wraps RubyLLM’s call() with context, timeout, tracking, and error handling.
Instance Attribute Details
#context ⇒ ToolContext? (readonly)
The execution context, set before each call. Provides access to agent params, tenant, execution ID.
44 45 46 |
# File 'lib/ruby_llm/agents/tool.rb', line 44 def context @context end |
Class Method Details
.timeout(value = nil) ⇒ Integer?
Sets or gets the per-tool timeout in seconds.
51 52 53 54 55 56 57 |
# File 'lib/ruby_llm/agents/tool.rb', line 51 def timeout(value = nil) if value @timeout = value else @timeout end end |
Instance Method Details
#call(args) ⇒ String, Tool::Halt
Wraps RubyLLM’s call() with context, timeout, tracking, and error handling.
RubyLLM’s Chat calls tool.call(args) during the tool loop. We set up context, create a tracking record, apply timeout, then delegate to super (which validates args and calls execute).
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/ruby_llm/agents/tool.rb', line 68 def call(args) pipeline_context = Thread.current[:ruby_llm_agents_caller_context] @context = pipeline_context ? ToolContext.new(pipeline_context) : nil record = start_tool_tracking(pipeline_context, args) check_cancelled!(pipeline_context) timeout_seconds = self.class.timeout timeout_seconds ||= RubyLLM::Agents.configuration.default_tool_timeout result = if timeout_seconds Timeout.timeout(timeout_seconds) { super } else super end complete_tool_tracking(record, result, status: "success") result rescue Timeout::Error complete_tool_tracking(record, nil, status: "timed_out", error: "Timed out after #{timeout_seconds}s") "TIMEOUT: Tool did not complete within #{timeout_seconds}s." rescue RubyLLM::Agents::CancelledError complete_tool_tracking(record, nil, status: "cancelled") raise # Let cancellation propagate to BaseAgent rescue => e complete_tool_tracking(record, nil, status: "error", error: e.) "ERROR (#{e.class}): #{e.}" end |