Class: RobotLab::Web::StreamHook

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

Overview

Taps robot_lab's hook system and turns each lifecycle moment into a RobotLab::Web::Event, delivered to the current EventSink and recorded in the ActivityLog. This is the producer half of the on_event -> SSE bridge that drives the browser console.

Register per-run so it only fires for web-driven runs:

robot.run(message, hooks: [RobotLab::Web::StreamHook])

...inside an EventSink.capture { } block.

Class Method Summary collapse

Class Method Details

.after_run(ctx) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/robot_lab/web/stream_hook.rb', line 25

def after_run(ctx)
  if ctx.error
    emit_error(ctx, ctx.error)
  else
    emit(ctx, role: :robot, content: final_text(ctx.response))
  end
end

.after_tool_call(ctx) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/robot_lab/web/stream_hook.rb', line 37

def after_tool_call(ctx)
  content =
    if ctx.tool_error
      { name: ctx.tool_name, error: ctx.tool_error.message }
    else
      { name: ctx.tool_name, result: ctx.tool_result }
    end
  emit(ctx, role: :tool_result, content: content)
end

.before_run(ctx) ⇒ Object



21
22
23
# File 'lib/robot_lab/web/stream_hook.rb', line 21

def before_run(ctx)
  emit(ctx, role: :user, content: ctx.request.to_s)
end

.before_tool_call(ctx) ⇒ Object



33
34
35
# File 'lib/robot_lab/web/stream_hook.rb', line 33

def before_tool_call(ctx)
  emit(ctx, role: :tool_call, content: { name: ctx.tool_name, args: ctx.tool_args })
end

.on_error(ctx) ⇒ Object



47
48
49
# File 'lib/robot_lab/web/stream_hook.rb', line 47

def on_error(ctx)
  emit_error(ctx, ctx.error)
end