Class: RubynCode::Teams::Manager

Inherits:
Object
  • Object
show all
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, and removal.

Instance Method Summary collapse

Constructor Details

#initialize(db, mailbox:) ⇒ Manager

Returns a new instance of Manager.

Parameters:

  • db (DB::Connection)

    the database connection

  • mailbox (Mailbox)

    the team mailbox for inter-agent messaging



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_teammatesArray<Teammate>

Returns all teammates with status “active”.

Returns:



113
114
115
116
117
118
119
# File 'lib/rubyn_code/teams/manager.rb', line 113

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

#get(name) ⇒ Teammate?

Finds a teammate by name.

Parameters:

  • name (String)

Returns:



70
71
72
73
74
75
# File 'lib/rubyn_code/teams/manager.rb', line 70

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

#listArray<Teammate>

Returns all teammates.

Returns:



61
62
63
64
# File 'lib/rubyn_code/teams/manager.rb', line 61

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.

Parameters:

  • name (String)

Raises:

  • (Error)

    if the teammate is not found



103
104
105
106
107
108
# File 'lib/rubyn_code/teams/manager.rb', line 103

def remove(name)
  teammate = get(name)
  raise Error, "Teammate '#{name}' not found" unless teammate

  @db.execute('DELETE FROM teammates WHERE name = ?', [name])
end

#spawn(name:, role:, persona: nil, model: nil) ⇒ Teammate

Creates a new teammate record.

Parameters:

  • name (String)

    unique teammate name

  • role (String)

    the teammate’s role description

  • persona (String, nil) (defaults to: nil)

    optional persona prompt

  • model (String, nil) (defaults to: nil)

    optional LLM model override

Returns:

  • (Teammate)

    the newly created teammate

Raises:

  • (Error)

    if a teammate with the given name already exists



30
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
# File 'lib/rubyn_code/teams/manager.rb', line 30

def spawn(name:, role:, persona: nil, model: 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, metadata, created_at)
      VALUES (?, ?, ?, ?, ?, ?, ?, ?)
    SQL
    [id, name, role, persona, model, 'idle', , now]
  )

  Teammate.new(
    id: id,
    name: name,
    role: role,
    persona: persona,
    model: model,
    status: 'idle',
    metadata: {},
    created_at: now
  )
end

#update_status(name, status) ⇒ void

This method returns an undefined value.

Updates a teammate’s status.

Parameters:

  • name (String)
  • status (String)

    one of “idle”, “active”, “offline”

Raises:

  • (ArgumentError)

    if the status is invalid

  • (Error)

    if the teammate is not found



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/rubyn_code/teams/manager.rb', line 84

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