Class: ActiveMatrix::Agent

Inherits:
ApplicationRecord show all
Defined in:
app/models/active_matrix/agent.rb,
app/models/active_matrix/agent/jobs/memory_reaper.rb

Defined Under Namespace

Modules: Jobs

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#passwordObject

Password handling



153
154
155
# File 'app/models/active_matrix/agent.rb', line 153

def password
  @password
end

Instance Method Details

#authenticate(password) ⇒ Object



155
156
157
158
159
# File 'app/models/active_matrix/agent.rb', line 155

def authenticate(password)
  return false if encrypted_password.blank?

  BCrypt::Password.new(encrypted_password) == password
end

#bot_instanceObject

Instance methods



110
111
112
# File 'app/models/active_matrix/agent.rb', line 110

def bot_instance
  @bot_instance ||= bot_class.constantize.new(client) if running?
end

#clientObject

Returns a Matrix client using resolved connection config Resolution order:

1. matrix_connection → lookup from config/active_matrix.yml
2. Inline credentials (homeserver, access_token) → user-uploaded bots


118
119
120
# File 'app/models/active_matrix/agent.rb', line 118

def client
  @client ||= build_client
end

#connection_configHash

Returns the resolved connection configuration

Returns:

  • (Hash)

    with :homeserver_url and :access_token keys



124
125
126
127
128
129
130
131
132
133
134
# File 'app/models/active_matrix/agent.rb', line 124

def connection_config
  if matrix_connection.present?
    ActiveMatrix.connection(matrix_connection)
  else
    {
      homeserver_url: homeserver,
      access_token: access_token,
      username: username
    }.compact
  end
end

#increment_messages_handled!Object



144
145
146
# File 'app/models/active_matrix/agent.rb', line 144

def increment_messages_handled!
  update!(messages_handled: messages_handled + 1)
end

#memoryObject



140
141
142
# File 'app/models/active_matrix/agent.rb', line 140

def memory
  @memory ||= ActiveMatrix::Memory::AgentMemory.new(self)
end

#running?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'app/models/active_matrix/agent.rb', line 136

def running?
  %i[online_idle online_busy].include?(state.to_sym)
end

#update_activity!Object



148
149
150
# File 'app/models/active_matrix/agent.rb', line 148

def update_activity!
  update(last_active_at: Time.current)
end