Class: RobotLab::A2A::RobotAdapter

Inherits:
A2A::Server::AgentExecutor
  • Object
show all
Defined in:
lib/robot_lab/a2a/robot_adapter.rb

Overview

Wraps a RobotLab::Robot as an A2A AgentExecutor.

Pass an instance as the executor: when building an A2A server:

adapter = RobotAdapter.new(robot, interactive: :none)
A2A.server(agent_card: card, executor: adapter).run

interactive modes:

:none      — robot runs sync; AskUser would block stdin (not recommended)
:a2a_tool  — A2A::AskUserTool replaces AskUser; Thread+Queue bridge
:io_bridge — IoBridge replaces robot.input/output; Thread+Queue bridge

Interactive modes keep the robot thread alive between A2A INPUT_REQUIRED and resume. Only works with in-process (Memory) storage.

Constant Summary collapse

VALID_MODES =
%i[none a2a_tool io_bridge].freeze

Instance Method Summary collapse

Constructor Details

#initialize(robot, interactive: :none) ⇒ RobotAdapter

Returns a new instance of RobotAdapter.



22
23
24
25
26
27
28
29
30
# File 'lib/robot_lab/a2a/robot_adapter.rb', line 22

def initialize(robot, interactive: :none)
  super()
  unless VALID_MODES.include?(interactive)
    raise ArgumentError, "interactive must be one of #{VALID_MODES.inspect}"
  end

  @robot       = robot
  @interactive = interactive
end

Instance Method Details

#call(context) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/robot_lab/a2a/robot_adapter.rb', line 32

def call(context)
  input_text = context.message.text_content
  task_id    = context.task.id

  case @interactive
  when :none
    run_simple(context, input_text)
  when :a2a_tool, :io_bridge
    run_interactive(context, input_text, task_id)
  end
end

#cancel(context) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/robot_lab/a2a/robot_adapter.rb', line 44

def cancel(context)
  task_id = context.task.id
  entry   = Registry.fetch(task_id)
  if entry
    entry.thread.kill
    Registry.delete(task_id)
  end
  super
end