Class: RobotLab::Narrator

Inherits:
Hook
  • Object
show all
Defined in:
lib/robot_lab/narrator.rb

Overview

Opt-in live observability hook: narrates what a robot is doing as it happens, so a run is never silent between events. Complements RobotLab::Audit (which records to a persistent store) with a human-facing console feed.

Enable it globally (applies to every robot run, including networks):

RobotLab::Narrator.enable!            # narrate to $stderr
RobotLab::Narrator.enable!(output: $stdout)

or register like any hook for finer scope:

robot.on(RobotLab::Narrator)
network.on(RobotLab::Narrator)

Output goes to $stderr by default and uses IO#puts (not Kernel#warn, which is silenced when Ruby warnings are disabled — i.e. $VERBOSE is nil, common under bundle exec).

Constant Summary collapse

MAX =
80

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.outputIO

Returns where narration is written (default $stderr).

Returns:

  • (IO)

    where narration is written (default $stderr)



30
31
32
# File 'lib/robot_lab/narrator.rb', line 30

def output
  @output ||= $stderr
end

Class Method Details

.after_tool_call(ctx) ⇒ Object



59
60
61
62
63
# File 'lib/robot_lab/narrator.rb', line 59

def after_tool_call(ctx)
  say "#{clip(ctx.tool_error.message)}" if ctx.tool_error
rescue StandardError
  nil
end

.before_llm_generation(ctx) ⇒ Object

--- hooks ---------------------------------------------------------------



46
47
48
49
50
51
# File 'lib/robot_lab/narrator.rb', line 46

def before_llm_generation(ctx)
  who = ctx.respond_to?(:robot) ? ctx.robot&.name : nil
  say "#{"#{who}: " if who}thinking…"
rescue StandardError
  nil
end

.before_tool_call(ctx) ⇒ Object



53
54
55
56
57
# File 'lib/robot_lab/narrator.rb', line 53

def before_tool_call(ctx)
  say "#{ctx.tool_name}#{summarize(ctx.tool_args)}"
rescue StandardError
  nil
end

.enable!(output: $stderr) ⇒ Class

Register the narrator globally and set its output.

Parameters:

  • output (IO) (defaults to: $stderr)

Returns:

  • (Class)

    self



38
39
40
41
42
# File 'lib/robot_lab/narrator.rb', line 38

def enable!(output: $stderr)
  self.output = output
  RobotLab.on(self)
  self
end