Class: PromptObjects::MessageBus
- Inherits:
-
Object
- Object
- PromptObjects::MessageBus
- Defined in:
- lib/prompt_objects/message_bus.rb
Overview
Message bus for routing and logging all inter-capability communication. This makes the semantic binding visible - you can see natural language being transformed into capability calls.
Each entry stores the full message content and a truncated summary. Use :summary for compact log displays, :message for full inspection.
Instance Attribute Summary collapse
-
#log ⇒ Object
readonly
Returns the value of attribute log.
Instance Method Summary collapse
-
#clear ⇒ Object
Clear the log.
-
#format_log(count = 20) ⇒ String
Format log entries for compact display.
-
#initialize(session_store: nil) ⇒ MessageBus
constructor
A new instance of MessageBus.
-
#publish(from:, to:, message:, session_id: nil) ⇒ Hash
Log a message between capabilities.
-
#recent(count = 20) ⇒ Array<Hash>
Get recent log entries.
-
#subscribe {|Hash| ... } ⇒ Object
Subscribe to message events.
-
#unsubscribe(block) ⇒ Object
Unsubscribe from message events.
Constructor Details
#initialize(session_store: nil) ⇒ MessageBus
Returns a new instance of MessageBus.
14 15 16 17 18 |
# File 'lib/prompt_objects/message_bus.rb', line 14 def initialize(session_store: nil) @log = [] @subscribers = [] @store = session_store end |
Instance Attribute Details
#log ⇒ Object (readonly)
Returns the value of attribute log.
11 12 13 |
# File 'lib/prompt_objects/message_bus.rb', line 11 def log @log end |
Instance Method Details
#clear ⇒ Object
Clear the log.
61 62 63 |
# File 'lib/prompt_objects/message_bus.rb', line 61 def clear @log.clear end |
#format_log(count = 20) ⇒ String
Format log entries for compact display.
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/prompt_objects/message_bus.rb', line 68 def format_log(count = 20) recent(count).map do |entry| time = entry[:timestamp].strftime("%H:%M:%S") from = entry[:from] to = entry[:to] msg = entry[:summary] "#{time} #{from} → #{to}: #{msg}" end.join("\n") end |
#publish(from:, to:, message:, session_id: nil) ⇒ Hash
Log a message between capabilities.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/prompt_objects/message_bus.rb', line 26 def publish(from:, to:, message:, session_id: nil) entry = { timestamp: Time.now, from: from, to: to, message: , summary: summarize() } @log << entry persist_event(entry, session_id: session_id) notify_subscribers(entry) entry end |
#recent(count = 20) ⇒ Array<Hash>
Get recent log entries.
56 57 58 |
# File 'lib/prompt_objects/message_bus.rb', line 56 def recent(count = 20) @log.last(count) end |
#subscribe {|Hash| ... } ⇒ Object
Subscribe to message events.
43 44 45 |
# File 'lib/prompt_objects/message_bus.rb', line 43 def subscribe(&block) @subscribers << block end |
#unsubscribe(block) ⇒ Object
Unsubscribe from message events.
49 50 51 |
# File 'lib/prompt_objects/message_bus.rb', line 49 def unsubscribe(block) @subscribers.delete(block) end |