Class: RailsConsoleAi::SubAgent

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_console_ai/sub_agent.rb

Constant Summary collapse

LOOP_WARN_THRESHOLD =
3
LOOP_BREAK_THRESHOLD =
5
LARGE_OUTPUT_THRESHOLD =
10_000
LARGE_OUTPUT_PREVIEW_CHARS =
8_000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(task:, agent_config:, binding_context:, parent_channel:, executor:, output_payload: nil, output_local_name: :output) ⇒ SubAgent

Returns a new instance of SubAgent.



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rails_console_ai/sub_agent.rb', line 15

def initialize(task:, agent_config:, binding_context:, parent_channel:, executor:,
               output_payload: nil, output_local_name: :output)
  @task = task
  @agent_config = agent_config || {}
  @binding_context = binding_context
  @parent_channel = parent_channel
  @parent_executor = executor
  @output_payload = output_payload
  @output_local_name = output_local_name
  @input_tokens = 0
  @output_tokens = 0
  @model_used = nil
end

Instance Attribute Details

#input_tokensObject (readonly)

Returns the value of attribute input_tokens.



13
14
15
# File 'lib/rails_console_ai/sub_agent.rb', line 13

def input_tokens
  @input_tokens
end

#model_usedObject (readonly)

Returns the value of attribute model_used.



13
14
15
# File 'lib/rails_console_ai/sub_agent.rb', line 13

def model_used
  @model_used
end

#output_tokensObject (readonly)

Returns the value of attribute output_tokens.



13
14
15
# File 'lib/rails_console_ai/sub_agent.rb', line 13

def output_tokens
  @output_tokens
end

Instance Method Details

#runObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rails_console_ai/sub_agent.rb', line 29

def run
  channel = Channel::SubAgent.new(
    parent_channel: @parent_channel,
    task_label: @agent_config['name']
  )

  effective_binding =
    if @output_payload
      b = @binding_context.eval("proc { binding }.call")
      b.local_variable_set(@output_local_name, @output_payload)
      b
    else
      @binding_context
    end

  executor = Executor.new(effective_binding, channel: channel)
  allowed_tools = @agent_config['tools'] ? Array(@agent_config['tools']) : nil
  tools = Tools::Registry.new(executor: executor, mode: :sub_agent, channel: channel, allowed_tools: allowed_tools)
  provider = build_provider
  system_prompt = build_system_prompt
  max_rounds = @agent_config['max_rounds'] || RailsConsoleAi.configuration.sub_agent_max_rounds

  messages = [{ role: :user, content: @task }]

  run_tool_loop(messages, system_prompt: system_prompt, tools: tools,
                provider: provider, channel: channel, executor: executor,
                max_rounds: max_rounds)
end