Module: RobotLab::Robot::BusMessaging
- Included in:
- RobotLab::Robot
- Defined in:
- lib/robot_lab/robot/bus_messaging.rb
Overview
Inter-robot communication via TypedBus.
Expects the including class to provide:
@bus, @bus_poller, @bus_poller_group, @message_counter,
@outbox, @message_handler, @bus_subscriber_id, @name
and the `run` instance method.
Delivery Serialization
TypedBus delivers messages in concurrent Async fibers. Robots enqueue deliveries into a BusPoller rather than handling them inline. The BusPoller drains each group’s queue sequentially on a dedicated OS thread, so robot.run() calls never interleave.
Instance Method Summary collapse
-
#assign_bus_poller(poller, group: :default) ⇒ void
Assign a shared BusPoller from a Network.
-
#on_message {|message| ... } ⇒ self
Register a custom handler for incoming bus messages.
-
#send_message(to:, content:) ⇒ RobotMessage
Send a message to another robot via the bus.
-
#send_reply(to:, content:, in_reply_to:) ⇒ RobotMessage
Send a reply to a specific message via the bus.
-
#spawn(name: "robot", system_prompt: nil, template: nil, local_tools: [], **options) ⇒ Robot
Spawn a new robot on a shared bus.
-
#with_bus(bus = nil) ⇒ self
Connect this robot to a message bus.
Instance Method Details
#assign_bus_poller(poller, group: :default) ⇒ void
This method returns an undefined value.
Assign a shared BusPoller from a Network.
Stops any private poller this robot auto-created, then adopts the network’s shared poller for the given group.
123 124 125 126 127 128 |
# File 'lib/robot_lab/robot/bus_messaging.rb', line 123 def assign_bus_poller(poller, group: :default) @private_bus_poller&.stop @private_bus_poller = nil @bus_poller = poller @bus_poller_group = group end |
#on_message {|message| ... } ⇒ self
Register a custom handler for incoming bus messages.
Block arity controls delivery handling:
-
1 argument ‘|message|`: auto-acks before calling, auto-nacks on exception
-
2 arguments ‘|delivery, message|`: manual mode, you call ack!/nack!
62 63 64 65 |
# File 'lib/robot_lab/robot/bus_messaging.rb', line 62 def (&block) @message_handler = block self end |
#send_message(to:, content:) ⇒ RobotMessage
Send a message to another robot via the bus.
26 27 28 29 30 31 32 33 34 |
# File 'lib/robot_lab/robot/bus_messaging.rb', line 26 def (to:, content:) raise BusError, "No bus configured on robot '#{@name}'" unless @bus @message_counter += 1 = RobotMessage.build(id: @message_counter, from: @name, content: content) @outbox[.key] = { message: , status: :sent, replies: [] } publish_to_bus(to.to_sym, ) end |
#send_reply(to:, content:, in_reply_to:) ⇒ RobotMessage
Send a reply to a specific message via the bus.
44 45 46 47 48 49 50 51 |
# File 'lib/robot_lab/robot/bus_messaging.rb', line 44 def send_reply(to:, content:, in_reply_to:) raise BusError, "No bus configured on robot '#{@name}'" unless @bus @message_counter += 1 reply = RobotMessage.build(id: @message_counter, from: @name, content: content, in_reply_to: in_reply_to) publish_to_bus(to.to_sym, reply) reply end |
#spawn(name: "robot", system_prompt: nil, template: nil, local_tools: [], **options) ⇒ Robot
Spawn a new robot on a shared bus.
Creates a new Robot instance that shares this robot’s bus, allowing it to immediately send and receive messages with all other robots on the bus. If no bus exists yet, one is created automatically and the parent robot is connected to it.
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/robot_lab/robot/bus_messaging.rb', line 82 def spawn(name: "robot", system_prompt: nil, template: nil, local_tools: [], **) ensure_bus RobotLab.build( name: name, system_prompt: system_prompt, template: template, local_tools: local_tools, bus: @bus, ** ) end |
#with_bus(bus = nil) ⇒ self
Connect this robot to a message bus.
If a bus is provided, the robot joins it. If no bus is provided and the robot doesn’t already have one, a new bus is created. No-op if the robot is already on the given bus.
105 106 107 108 109 110 111 112 |
# File 'lib/robot_lab/robot/bus_messaging.rb', line 105 def with_bus(bus = nil) return self if bus && @bus == bus teardown_bus_channel if @bus @bus = bus || @bus || TypedBus::MessageBus.new setup_bus_channel self end |