Class: RubynCode::Teams::Manager
- Inherits:
-
Object
- Object
- RubynCode::Teams::Manager
- Defined in:
- lib/rubyn_code/teams/manager.rb
Overview
CRUD manager for teammates backed by SQLite.
Provides lifecycle management for agent teammates: spawning, listing, status updates, removal, and parent-child discovery.
Instance Method Summary collapse
-
#active_teammates ⇒ Array<Teammate>
Returns all teammates with status “active”.
-
#agent_tree(root_id) ⇒ Hash?
Builds a full agent tree from a root agent ID.
-
#children_of(parent_id) ⇒ Array<Teammate>
Returns all direct children of the given parent agent.
-
#find_by_id(id) ⇒ Teammate?
Finds a teammate by ID.
-
#get(name) ⇒ Teammate?
Finds a teammate by name.
-
#initialize(db, mailbox:) ⇒ Manager
constructor
A new instance of Manager.
-
#list ⇒ Array<Teammate>
Returns all teammates.
-
#remove(name) ⇒ void
Removes a teammate by name.
-
#roots ⇒ Array<Teammate>
Returns all root agents (those with no parent).
-
#spawn(name:, role:, persona: nil, model: nil, parent_agent_id: nil) ⇒ Teammate
Creates a new teammate record.
-
#update_status(name, status) ⇒ void
Updates a teammate’s status.
Constructor Details
#initialize(db, mailbox:) ⇒ Manager
Returns a new instance of Manager.
16 17 18 19 20 |
# File 'lib/rubyn_code/teams/manager.rb', line 16 def initialize(db, mailbox:) @db = db @mailbox = mailbox ensure_table! end |
Instance Method Details
#active_teammates ⇒ Array<Teammate>
Returns all teammates with status “active”.
160 161 162 163 164 165 166 |
# File 'lib/rubyn_code/teams/manager.rb', line 160 def active_teammates rows = @db.query( 'SELECT * FROM teammates WHERE status = ? ORDER BY created_at ASC', ['active'] ).to_a rows.map { |row| row_to_teammate(row) } end |
#agent_tree(root_id) ⇒ Hash?
Builds a full agent tree from a root agent ID. Returns a nested hash: { agent: Teammate, children: [{ agent:, children: }, …] }
117 118 119 120 121 122 |
# File 'lib/rubyn_code/teams/manager.rb', line 117 def agent_tree(root_id) root = find_by_id(root_id) return nil unless root build_tree_node(root) end |
#children_of(parent_id) ⇒ Array<Teammate>
Returns all direct children of the given parent agent.
94 95 96 97 98 99 100 |
# File 'lib/rubyn_code/teams/manager.rb', line 94 def children_of(parent_id) rows = @db.query( 'SELECT * FROM teammates WHERE parent_agent_id = ? ORDER BY created_at ASC', [parent_id] ).to_a rows.map { |row| row_to_teammate(row) } end |
#find_by_id(id) ⇒ Teammate?
Finds a teammate by ID.
83 84 85 86 87 88 |
# File 'lib/rubyn_code/teams/manager.rb', line 83 def find_by_id(id) rows = @db.query('SELECT * FROM teammates WHERE id = ? LIMIT 1', [id]).to_a return nil if rows.empty? row_to_teammate(rows.first) end |
#get(name) ⇒ Teammate?
Finds a teammate by name.
72 73 74 75 76 77 |
# File 'lib/rubyn_code/teams/manager.rb', line 72 def get(name) rows = @db.query('SELECT * FROM teammates WHERE name = ? LIMIT 1', [name]).to_a return nil if rows.empty? row_to_teammate(rows.first) end |
#list ⇒ Array<Teammate>
Returns all teammates.
63 64 65 66 |
# File 'lib/rubyn_code/teams/manager.rb', line 63 def list rows = @db.query('SELECT * FROM teammates ORDER BY created_at ASC').to_a rows.map { |row| row_to_teammate(row) } end |
#remove(name) ⇒ void
This method returns an undefined value.
Removes a teammate by name.
150 151 152 153 154 155 |
# File 'lib/rubyn_code/teams/manager.rb', line 150 def remove(name) teammate = get(name) raise Error, "Teammate '#{name}' not found" unless teammate @db.execute('DELETE FROM teammates WHERE name = ?', [name]) end |
#roots ⇒ Array<Teammate>
Returns all root agents (those with no parent).
105 106 107 108 109 110 |
# File 'lib/rubyn_code/teams/manager.rb', line 105 def roots rows = @db.query( 'SELECT * FROM teammates WHERE parent_agent_id IS NULL ORDER BY created_at ASC' ).to_a rows.map { |row| row_to_teammate(row) } end |
#spawn(name:, role:, persona: nil, model: nil, parent_agent_id: nil) ⇒ Teammate
Creates a new teammate record.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/rubyn_code/teams/manager.rb', line 31 def spawn(name:, role:, persona: nil, model: nil, parent_agent_id: nil) existing = get(name) raise Error, "Teammate '#{name}' already exists" if existing id = SecureRandom.uuid now = Time.now.utc.iso8601 = JSON.generate({}) @db.execute( <<~SQL, INSERT INTO teammates (id, name, role, persona, model, status, parent_agent_id, metadata, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) SQL [id, name, role, persona, model, 'idle', parent_agent_id, , now] ) Teammate.new( id: id, name: name, role: role, persona: persona, model: model, status: 'idle', parent_agent_id: parent_agent_id, metadata: {}, created_at: now ) end |
#update_status(name, status) ⇒ void
This method returns an undefined value.
Updates a teammate’s status.
131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/rubyn_code/teams/manager.rb', line 131 def update_status(name, status) unless VALID_STATUSES.include?(status) raise ArgumentError, "Invalid status '#{status}'. Must be one of: #{VALID_STATUSES.join(', ')}" end teammate = get(name) raise Error, "Teammate '#{name}' not found" unless teammate @db.execute( 'UPDATE teammates SET status = ? WHERE name = ?', [status, name] ) end |