Class: HTM::Models::Robot

Inherits:
Object
  • Object
show all
Defined in:
lib/htm/models/robot.rb

Overview

Robot model - represents an LLM agent using the HTM system

Robots can share memories through the many-to-many relationship with nodes. When a robot is deleted, only the robot_nodes links are removed; shared nodes remain in the database for other robots.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_or_create_by_name(robot_name) ⇒ Robot

Find or create a robot by name

Parameters:

  • robot_name (String)

    Name of the robot

Returns:

  • (Robot)

    The found or created robot



52
53
54
# File 'lib/htm/models/robot.rb', line 52

def self.find_or_create_by_name(robot_name)
  find_or_create(name: robot_name)
end

Instance Method Details

#before_createObject

Hooks



40
41
42
43
# File 'lib/htm/models/robot.rb', line 40

def before_create
  self.created_at ||= Time.now
  super
end

#memory_summaryHash

Get a summary of this robot’s memory state

Returns:

  • (Hash)

    Summary including:

    • :total_nodes [Integer] Total nodes associated with this robot

    • :in_working_memory [Integer] Nodes currently in working memory

    • :with_embeddings [Integer] Nodes that have embeddings generated



103
104
105
106
107
108
109
# File 'lib/htm/models/robot.rb', line 103

def memory_summary
  {
    total_nodes: nodes_dataset.count,
    in_working_memory: robot_nodes_dataset.where(working_memory: true).count,
    with_embeddings: nodes_dataset.exclude(embedding: nil).count
  }
end

#node_countInteger

Get the total number of nodes associated with this robot

Returns:

  • (Integer)

    Number of nodes



62
63
64
# File 'lib/htm/models/robot.rb', line 62

def node_count
  nodes_dataset.count
end

#nodes_with_metadata(limit = 10) ⇒ Array<Hash>

Get nodes with their remember metadata for this robot

Parameters:

  • limit (Integer) (defaults to: 10)

    Max nodes to return

Returns:

  • (Array<Hash>)

    Nodes with remember_count, first/last_remembered_at



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/htm/models/robot.rb', line 80

def (limit = 10)
  robot_nodes_dataset
    .eager(:node)
    .order(Sequel.desc(:last_remembered_at))
    .limit(limit)
    .all
    .map do |rn|
      {
        node: rn.node,
        remember_count: rn.remember_count,
        first_remembered_at: rn.first_remembered_at,
        last_remembered_at: rn.last_remembered_at
      }
    end
end

#recent_nodes(limit = 10) ⇒ Array<Node>

Get the most recent nodes for this robot

Parameters:

  • limit (Integer) (defaults to: 10)

    Maximum number of nodes to return (default: 10)

Returns:

  • (Array<Node>)

    Recent nodes ordered by created_at desc



71
72
73
# File 'lib/htm/models/robot.rb', line 71

def recent_nodes(limit = 10)
  nodes_dataset.order(Sequel.desc(:created_at)).limit(limit).all
end

#validateObject

Validations



23
24
25
26
# File 'lib/htm/models/robot.rb', line 23

def validate
  super
  validates_presence :name
end