Module: RobotLab::Robot::Hooking

Defined in:
lib/robot_lab/robot/hooking.rb

Instance Method Summary collapse

Instance Method Details

#on(handler_class, context: nil) ⇒ Object



45
46
47
# File 'lib/robot_lab/robot/hooking.rb', line 45

def on(handler_class, context: nil)
  @hooks.on(handler_class, context: context)
end

#run(message = nil, network: nil, task: nil, network_memory: nil, network_config: nil, memory: nil, mcp: :none, tools: :none, hooks: nil, **kwargs, &block) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/robot_lab/robot/hooking.rb', line 6

def run(message = nil, network: nil, task: nil, network_memory: nil, network_config: nil,
        memory: nil, mcp: :none, tools: :none, hooks: nil, **kwargs, &block)
  run_memory = resolve_run_memory(memory, network: network, network_memory: network_memory)
  previous_writer = run_memory.current_writer
  run_memory.current_writer = @name
  context = RunHookContext.new(
    robot: self,
    network: network,
    task: task,
    memory: run_memory,
    config: @config,
    request: message
  )

  registries = hook_registries(network)

  begin
    RobotLab.with_hook_scope(registries, hooks) do
      RobotLab::Hooks.run(:run, context, registries: registries, per_run_hooks: hooks) do
        run_context = kwargs.except(:with)
        prepare_tools(message: context.request, mcp: mcp, tools: tools,
                      network: network, network_config: network_config)
        rerender_template(run_context) if @template && run_context.any?
        reservation = reserve_budget!
        response = invoke_ask(context: context, kwargs: kwargs, hooks: hooks, block: block)
        result = build_result(response, run_memory)
        reconcile_budget!(reservation, response: response, result: result)
        enforce_token_budget!
        enforce_cost_budget!
        result
      end
    end
  ensure
    remove_doom_loop_detection
    restore_tool_call_callback if @config.max_tool_rounds
    run_memory.current_writer = previous_writer
  end
end