class RubyLLM::Browser::Context

Trusted, host-supplied execution context.

Context is created by the host application — not the model and not page text — and carries identity, the source policy, the backend, stores, budgets and the data- sharing consent decision. Tools never invent any of it.

Attributes

actor_id[R]
approval_store[R]
backend[R]
budget[R]
logger[R]
metadata[R]
operation_store[R]
run_id[R]
source_policy[R]

Public Class Methods

new(actor_id:, run_id:, backend:, source_policy:, operation_store:, approval_store: nil, budget: nil, clock: nil, logger: nil, consent: nil, metadata: {}) click to toggle source

@param source_policy [Policy] the host’s URL/schema/action/egress policy @param backend [Backend] an egress-qualified backend @param operation_store [OperationStore] durable operation bookkeeping @param approval_store [ApprovalStore, nil] required only for Act @param budget [Budget, nil] defaults to the process default budget @param clock [#call, nil] returns the current UTC Time @param logger [Logger, nil] @param consent [Object, nil] host record of the remote-processing consent

# File lib/ruby_llm/browser/context.rb, line 22
def initialize(actor_id:, run_id:, backend:, source_policy:, operation_store:,
               approval_store: nil, budget: nil, clock: nil, logger: nil, consent: nil,
               metadata: {})
  @actor_id = presence!(actor_id, :actor_id)
  @run_id = presence!(run_id, :run_id)
  @backend = backend!(backend)
  @source_policy = policy!(source_policy)
  @operation_store = store!(operation_store)
  @approval_store = approval_store!(approval_store)
  @budget = budget.nil? ? RubyLLM::Browser.configuration.default_budget : budget!(budget)
  @clock = clock || RubyLLM::Browser.clock
  @logger = logger || RubyLLM::Browser.logger
  @consent = consent
  @metadata = Immutable.copy(metadata || {})
  freeze
end

Public Instance Methods

approval_store!(value) click to toggle source
# File lib/ruby_llm/browser/context.rb, line 101
def approval_store!(value)
  return nil if value.nil?
  unless value.is_a?(ApprovalStore)
    raise InvalidInput, "approval_store must be a RubyLLM::Browser::ApprovalStore"
  end

  value
end
backend!(value) click to toggle source
# File lib/ruby_llm/browser/context.rb, line 79
def backend!(value)
  unless value.is_a?(Backend)
    raise InvalidInput, "backend must be a RubyLLM::Browser::Backend"
  end

  value
end
budget!(value) click to toggle source
# File lib/ruby_llm/browser/context.rb, line 110
def budget!(value)
  raise InvalidInput, "budget must be a RubyLLM::Browser::Budget" unless value.is_a?(Budget)

  value
end
capabilities() click to toggle source
# File lib/ruby_llm/browser/context.rb, line 42
def capabilities
  backend.capabilities
end
inspect() click to toggle source
# File lib/ruby_llm/browser/context.rb, line 67
def inspect
  "#<#{self.class} actor=#{actor_id} run=#{run_id} backend=#{backend.class}>"
end
now() click to toggle source

Current UTC time from the context clock.

# File lib/ruby_llm/browser/context.rb, line 47
def now
  @clock.call
end
policy(= source_policy) click to toggle source

The host policy.

# File lib/ruby_llm/browser/context.rb, line 40
  def policy = source_policy

  def capabilities
    backend.capabilities
  end

  # Current UTC time from the context clock.
  def now
    @clock.call
  end

  # Exact secret values the backend wants scrubbed from page text and results.
  def secrets
    backend.respond_to?(:secrets) ? Array(backend.secrets) : []
  end

  # An audit-safe summary. Never includes backend credentials.
  def to_h
    {
      actor_id: actor_id,
      run_id: run_id,
      budget: budget.to_h,
      consent: !consent.nil?,
      capabilities: capabilities.respond_to?(:to_h) ? capabilities.to_h : capabilities
    }
  end

  def inspect
    "#<#{self.class} actor=#{actor_id} run=#{run_id} backend=#{backend.class}>"
  end

  private

  def presence!(value, name)
    raise InvalidInput, "#{name} must be a non-empty String" unless value.is_a?(String) && !value.empty?

    value.dup.freeze
  end

  def backend!(value)
    unless value.is_a?(Backend)
      raise InvalidInput, "backend must be a RubyLLM::Browser::Backend"
    end

    value
  end

  def policy!(value)
    raise InvalidInput, "source_policy must be a RubyLLM::Browser::Policy" unless value.is_a?(Policy)

    value
  end

  def store!(value)
    unless value.is_a?(OperationStore)
      raise InvalidInput, "operation_store must be a RubyLLM::Browser::OperationStore"
    end

    value
  end

  def approval_store!(value)
    return nil if value.nil?
    unless value.is_a?(ApprovalStore)
      raise InvalidInput, "approval_store must be a RubyLLM::Browser::ApprovalStore"
    end

    value
  end

  def budget!(value)
    raise InvalidInput, "budget must be a RubyLLM::Browser::Budget" unless value.is_a?(Budget)

    value
  end
end
policy!(value) click to toggle source
# File lib/ruby_llm/browser/context.rb, line 87
def policy!(value)
  raise InvalidInput, "source_policy must be a RubyLLM::Browser::Policy" unless value.is_a?(Policy)

  value
end
presence!(value, name) click to toggle source
# File lib/ruby_llm/browser/context.rb, line 73
def presence!(value, name)
  raise InvalidInput, "#{name} must be a non-empty String" unless value.is_a?(String) && !value.empty?

  value.dup.freeze
end
secrets() click to toggle source

Exact secret values the backend wants scrubbed from page text and results.

# File lib/ruby_llm/browser/context.rb, line 52
def secrets
  backend.respond_to?(:secrets) ? Array(backend.secrets) : []
end
store!(value) click to toggle source
# File lib/ruby_llm/browser/context.rb, line 93
def store!(value)
  unless value.is_a?(OperationStore)
    raise InvalidInput, "operation_store must be a RubyLLM::Browser::OperationStore"
  end

  value
end
to_h() click to toggle source

An audit-safe summary. Never includes backend credentials.

# File lib/ruby_llm/browser/context.rb, line 57
def to_h
  {
    actor_id: actor_id,
    run_id: run_id,
    budget: budget.to_h,
    consent: !consent.nil?,
    capabilities: capabilities.respond_to?(:to_h) ? capabilities.to_h : capabilities
  }
end