Class: ActiveAgent::Delegation::Runner Private

Inherits:
Object
  • Object
show all
Defined in:
lib/active_agent/delegation/runner.rb

Overview

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.

Executes one delegated call: budget check, sub-agent generation, result validation, accounting.

The runner is what makes a sub-agent a tool rather than a method call. It answers to the calling model in the model's own terms — a refused call comes back as a structured result the model can reason about, not as an exception that ends the conversation.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definition, owner:) ⇒ 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.

Parameters:



24
25
26
27
# File 'lib/active_agent/delegation/runner.rb', line 24

def initialize(definition, owner:)
  @definition = definition
  @owner      = owner
end

Instance Attribute Details

#definitionDefinition (readonly)

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:



18
19
20
# File 'lib/active_agent/delegation/runner.rb', line 18

def definition
  @definition
end

#ownerActiveAgent::Base (readonly)

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 the delegating agent instance.

Returns:



20
21
22
# File 'lib/active_agent/delegation/runner.rb', line 20

def owner
  @owner
end

Instance Method Details

#call(**arguments) ⇒ Object

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.

Runs the delegated call.

Parameters:

  • arguments (Hash)

    arguments supplied by the calling model

Returns:

  • (Object)

    the sub-agent's result, or a structured error the calling model can act on

Raises:



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/active_agent/delegation/runner.rb', line 36

def call(**arguments)
  if (violation = budget_violation)
    return refuse(violation)
  end

  arguments = coerce_arguments(arguments)

  instrument(arguments) do |payload|
    started  = clock
    response = nil

    begin
      response = with_timeout { generate(arguments) }
    rescue Timeout::Error
      duration = clock - started
      record(duration: duration)
      payload[:status]      = :timed_out
      payload[:duration_ms] = (duration * 1_000).round

      next timed_out(duration)
    end

    duration = clock - started
    usage    = response.usage
    model    = response.model.presence || generation_model(response)
    cost     = Pricing.cost_for(usage: usage, model: model, rates: budget.rates)

    record(tokens: usage&.total_tokens, cost: cost, duration: duration)

    payload[:status]      = :ok
    payload[:model]       = model
    payload[:duration_ms] = (duration * 1_000).round
    payload[:usage]       = usage
    payload[:cost]        = cost
    payload[:ledger]      = ledgers.last.to_h

    result(response, payload)
  end
end