Class: ClaudeAgentServer::Services::SessionEntry
- Inherits:
-
Object
- Object
- ClaudeAgentServer::Services::SessionEntry
- Defined in:
- lib/claude_agent_server/services/session_manager.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#created_at ⇒ Object
readonly
Returns the value of attribute created_at.
-
#events ⇒ Object
readonly
Returns the value of attribute events.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#last_activity ⇒ Object
Returns the value of attribute last_activity.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Instance Method Summary collapse
- #broadcast(message) ⇒ Object
- #disconnect ⇒ Object
- #finish ⇒ Object
-
#get_events(offset: 0, limit: nil) ⇒ Object
Retrieve events by offset and limit (for polling).
-
#initialize(id:, client:) ⇒ SessionEntry
constructor
A new instance of SessionEntry.
- #message_count ⇒ Object
- #start_message_reader ⇒ Object
- #subscribe(offset: 0, &block) ⇒ Object
Constructor Details
#initialize(id:, client:) ⇒ SessionEntry
Returns a new instance of SessionEntry.
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/claude_agent_server/services/session_manager.rb', line 25 def initialize(id:, client:) @id = id @client = client @created_at = Time.now @last_activity = Time.now @status = :connected @events = [] @next_index = 0 @subscribers = [] @mutex = Mutex.new @reader_task = nil end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
22 23 24 |
# File 'lib/claude_agent_server/services/session_manager.rb', line 22 def client @client end |
#created_at ⇒ Object (readonly)
Returns the value of attribute created_at.
22 23 24 |
# File 'lib/claude_agent_server/services/session_manager.rb', line 22 def created_at @created_at end |
#events ⇒ Object (readonly)
Returns the value of attribute events.
22 23 24 |
# File 'lib/claude_agent_server/services/session_manager.rb', line 22 def events @events end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
22 23 24 |
# File 'lib/claude_agent_server/services/session_manager.rb', line 22 def id @id end |
#last_activity ⇒ Object
Returns the value of attribute last_activity.
23 24 25 |
# File 'lib/claude_agent_server/services/session_manager.rb', line 23 def last_activity @last_activity end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
22 23 24 |
# File 'lib/claude_agent_server/services/session_manager.rb', line 22 def status @status end |
Instance Method Details
#broadcast(message) ⇒ Object
71 72 73 74 75 76 77 78 79 |
# File 'lib/claude_agent_server/services/session_manager.rb', line 71 def broadcast() @mutex.synchronize do event = SessionEvent.new(index: @next_index, message: ) @next_index += 1 @events << event @last_activity = Time.now @subscribers.each { |q| q.enqueue(event) } end end |
#disconnect ⇒ Object
103 104 105 106 107 108 109 110 111 |
# File 'lib/claude_agent_server/services/session_manager.rb', line 103 def disconnect @status = :disconnected @reader_task&.kill @client.disconnect @mutex.synchronize do @subscribers.each { |q| q.enqueue(:done) } @subscribers.clear end end |
#finish ⇒ Object
81 82 83 84 85 86 |
# File 'lib/claude_agent_server/services/session_manager.rb', line 81 def finish @mutex.synchronize do @status = :finished @subscribers.each { |q| q.enqueue(:done) } end end |
#get_events(offset: 0, limit: nil) ⇒ Object
Retrieve events by offset and limit (for polling)
43 44 45 46 47 48 |
# File 'lib/claude_agent_server/services/session_manager.rb', line 43 def get_events(offset: 0, limit: nil) @mutex.synchronize do slice = @events[offset..] || [] limit ? slice.first(limit) : slice end end |
#message_count ⇒ Object
38 39 40 |
# File 'lib/claude_agent_server/services/session_manager.rb', line 38 def @events.size end |
#start_message_reader ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/claude_agent_server/services/session_manager.rb', line 88 def @reader_task = Thread.new do Async do client. do || broadcast() break if .is_a?(ClaudeAgentSDK::ResultMessage) end rescue StandardError # Reader ended (disconnect or error) ensure finish end end end |
#subscribe(offset: 0, &block) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/claude_agent_server/services/session_manager.rb', line 50 def subscribe(offset: 0, &block) queue = Async::Queue.new # Replay missed events from offset @mutex.synchronize do @subscribers << queue @events[offset..]&.each { |evt| queue.enqueue(evt) } end begin loop do event = queue.dequeue break if event == :done block.call(event) end ensure @mutex.synchronize { @subscribers.delete(queue) } end end |