Skip to content

Class HTM::Models::Node

Inherits Object
Defined in lib/htm/models/node.rb

Node model - represents a memory node (conversation message)

Nodes are globally unique by content (via content_hash) and can be linked to multiple robots through the robot_nodes join table.

Public Class Methods

find_by_content(content)

Find a node by content hash, or return nil - @param content [String] The content to search for - @return [Node, nil] The existing node or nil

generate_content_hash(content)

Generate SHA-256 hash for content - @param content [String] Content to hash - @return [String] 64-character hex hash

purge_deleted(older_than:)

Permanently delete all soft-deleted nodes older than the specified time - @param older_than [Time] Delete nodes soft-deleted before this time - @return [Integer] Number of nodes permanently deleted

Public Instance Methods

add_tags(tag_names)

Add tags to this node (creates tags and all parent tags if they don't exist) - @param tag_names [Array, String] Tag name(s) to add - @return [void]

before_create()

Not documented.

before_save()

Not documented.

before_validation()

Hooks

deleted?()

Check if node is soft-deleted - @return [Boolean] true if deleted_at is set

embedding()

Override embedding getter to return Array instead of String pgvector stores as string format "[0.1,0.2,...]" and we need Array

embedding_array()

Get embedding as an Array (handles both String and Array storage) Note: The embedding getter already returns Array, this is an alias for compatibility - @return [Array, nil] The embedding vector as an array

nearest_neighbors(limit: 10, distance: "cosine")

Find nearest neighbors to this node's embedding - @param limit [Integer] number of neighbors to return (default: 10) - @param distance [String] distance metric (default: "cosine") - @return [Array] ordered by distance (closest first)

neighbor_distance()

Accessor for neighbor_distance from nearest_neighbors query Works with both: * Instance method (stores in @neighbor_distance) * Dataset method (stores in values hash from SELECT)

proposition?()

Check if node is a proposition (extracted atomic fact) - @return [Boolean] true if metadata['is_proposition'] is true

remove_tag(tag_name)

Remove a tag from this node - @param tag_name [String] Tag name to remove - @return [void]

restore!()

Restore a soft-deleted node - @return [Boolean] true if restored successfully

similarity_to(other)

Calculate cosine similarity to another embedding or node - @param other [Array, Node] query embedding vector or another Node - @return [Float] similarity score (0.0 to 1.0, higher is more similar)

soft_delete!()

Soft delete - mark node as deleted without removing from database - @return [Boolean] true if soft deleted successfully

tag_names()

Get all tag names associated with this node - @return [Array] Array of hierarchical tag names

to_hash()

Convert to hash (for compatibility with existing code) - @return [Hash] Hash representation of the node

validate()

Validations