Class: RubyLLM::Agents::ToolContext

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_llm/agents/tool_context.rb

Overview

Read-only wrapper around Pipeline::Context for tool authors.

Exposes agent params and execution metadata to tools without leaking pipeline internals. Supports both method-style and hash-style access to agent params.

Examples:

Method-style access

context.container_id   # reads agent param
context.tenant_id      # fixed attribute

Hash-style access

context[:container_id]

Instance Method Summary collapse

Constructor Details

#initialize(pipeline_context) ⇒ ToolContext

Returns a new instance of ToolContext.



48
49
50
51
# File 'lib/ruby_llm/agents/tool_context.rb', line 48

def initialize(pipeline_context)
  @ctx = pipeline_context
  @agent_options = @ctx.agent_instance&.send(:options) || {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object (private)

Method-style access to agent params



56
57
58
59
60
61
62
63
# File 'lib/ruby_llm/agents/tool_context.rb', line 56

def method_missing(method_name, *args)
  key = method_name.to_sym
  if @agent_options.key?(key) || @agent_options.key?(key.to_s)
    self[key]
  else
    super
  end
end

Instance Method Details

#[](key) ⇒ Object?

Hash-style access to agent params

Parameters:

  • key (Symbol, String)

    The param key

Returns:

  • (Object, nil)


44
45
46
# File 'lib/ruby_llm/agents/tool_context.rb', line 44

def [](key)
  @agent_options[key.to_sym] || @agent_options[key.to_s]
end

#agent_typeString?

Agent class name

Returns:

  • (String, nil)


36
37
38
# File 'lib/ruby_llm/agents/tool_context.rb', line 36

def agent_type
  @ctx.agent_class&.name
end

#idInteger?

Execution record ID — links tool calls to the agent execution

Returns:

  • (Integer, nil)


22
23
24
# File 'lib/ruby_llm/agents/tool_context.rb', line 22

def id
  @ctx.execution_id
end

#tenant_idString?

Tenant ID from the pipeline context

Returns:

  • (String, nil)


29
30
31
# File 'lib/ruby_llm/agents/tool_context.rb', line 29

def tenant_id
  @ctx.tenant_id
end