htm-cli 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +11 -0
- data/COMMITS.md +196 -0
- data/LICENSE.txt +21 -0
- data/README.md +99 -0
- data/Rakefile +23 -0
- data/bin/htm +18 -0
- data/lib/htm/admin/database_commands.rb +489 -0
- data/lib/htm/admin/enrich_commands.rb +275 -0
- data/lib/htm/admin/entity_commands.rb +203 -0
- data/lib/htm/admin/entity_graph.rb +313 -0
- data/lib/htm/admin/file_commands.rb +323 -0
- data/lib/htm/admin/memory_commands/edit.rb +175 -0
- data/lib/htm/admin/memory_commands/entities.rb +189 -0
- data/lib/htm/admin/memory_commands/entity_lines.rb +111 -0
- data/lib/htm/admin/memory_commands/search.rb +205 -0
- data/lib/htm/admin/memory_commands/tag_search.rb +98 -0
- data/lib/htm/admin/memory_commands/text.rb +166 -0
- data/lib/htm/admin/memory_commands/working_memory.rb +71 -0
- data/lib/htm/admin/memory_commands.rb +196 -0
- data/lib/htm/admin/tag_commands.rb +352 -0
- data/lib/htm/admin/tag_graph.rb +345 -0
- data/lib/htm/admin/templates/tag_graph.html +319 -0
- data/lib/htm/admin.rb +266 -0
- data/lib/htm/cli/config_file.rb +73 -0
- data/lib/htm/cli/db.rb +80 -0
- data/lib/htm/cli/enrich.rb +52 -0
- data/lib/htm/cli/entities.rb +268 -0
- data/lib/htm/cli/environment.rb +74 -0
- data/lib/htm/cli/error_suggestion.rb +97 -0
- data/lib/htm/cli/files.rb +41 -0
- data/lib/htm/cli/group.rb +46 -0
- data/lib/htm/cli/memory.rb +410 -0
- data/lib/htm/cli/memory_command.rb +72 -0
- data/lib/htm/cli/robots.rb +82 -0
- data/lib/htm/cli/support.rb +46 -0
- data/lib/htm/cli/tags.rb +103 -0
- data/lib/htm/cli/version.rb +11 -0
- data/lib/htm/cli/working_memory.rb +41 -0
- data/lib/htm/cli.rb +96 -0
- data/lib/htm/mcp/entity_tools.rb +334 -0
- data/lib/htm/mcp/file_tools.rb +196 -0
- data/lib/htm/mcp/group_tools.rb +466 -0
- data/lib/htm/mcp/node_tools.rb +358 -0
- data/lib/htm/mcp/resources.rb +192 -0
- data/lib/htm/mcp/robot_team_tools.rb +129 -0
- data/lib/htm/mcp/server.rb +111 -0
- data/lib/htm/mcp/tag_tools.rb +130 -0
- data/lib/htm/mcp/tool.rb +40 -0
- data/lib/htm/mcp/tool_support.rb +108 -0
- data/lib/htm/mcp/tools.rb +461 -0
- metadata +181 -0
|
@@ -0,0 +1,489 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class HTM
|
|
4
|
+
module Admin
|
|
5
|
+
# The `htm db ...` commands that are more than one HTM::Database call
|
|
6
|
+
module DatabaseCommands
|
|
7
|
+
DROP_PROMPT = "Are you sure you want to drop all tables? This cannot be undone! (yes/no): "
|
|
8
|
+
|
|
9
|
+
module_function
|
|
10
|
+
|
|
11
|
+
# Open a PG connection from an HTM database config
|
|
12
|
+
#
|
|
13
|
+
# @param config [Hash] HTM::SequelConfig.load_database_config
|
|
14
|
+
# @param dbname [String] Database to connect to
|
|
15
|
+
# @return [PG::Connection]
|
|
16
|
+
#
|
|
17
|
+
def pg_connect(config, dbname: config[:database])
|
|
18
|
+
require 'pg'
|
|
19
|
+
PG.connect(host: config[:host], port: config[:port], dbname:,
|
|
20
|
+
user: config[:username], password: config[:password])
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# Create the configured database (with pgvector and pg_trgm) unless it exists
|
|
24
|
+
#
|
|
25
|
+
# @return [Symbol] :created or :exists
|
|
26
|
+
# @raise [HTM::Admin::Error] when PostgreSQL refuses
|
|
27
|
+
#
|
|
28
|
+
def create(config = HTM::SequelConfig.load_database_config, out: $stdout)
|
|
29
|
+
name = config[:database]
|
|
30
|
+
out.puts "Creating database: #{name} (#{HTM.env})"
|
|
31
|
+
unless create_database(config)
|
|
32
|
+
out.puts "✓ Database already exists: #{name}"
|
|
33
|
+
return :exists
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
out.puts "✓ Database created: #{name}"
|
|
37
|
+
enable_extensions(config)
|
|
38
|
+
out.puts "✓ Extensions enabled (pgvector, pg_trgm)"
|
|
39
|
+
:created
|
|
40
|
+
rescue PG::Error => e
|
|
41
|
+
raise Admin::Error, "✗ Error: #{e.message}"
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# Create the configured database unless the server already has it
|
|
45
|
+
#
|
|
46
|
+
# @param config [Hash] HTM::SequelConfig.load_database_config
|
|
47
|
+
# @return [Boolean] true when created, false when it already existed
|
|
48
|
+
#
|
|
49
|
+
def create_database(config)
|
|
50
|
+
name = config[:database]
|
|
51
|
+
admin = pg_connect(config, dbname: 'postgres')
|
|
52
|
+
return false if database_exists?(admin, name)
|
|
53
|
+
|
|
54
|
+
admin.exec("CREATE DATABASE #{PG::Connection.quote_ident(name)}")
|
|
55
|
+
true
|
|
56
|
+
ensure
|
|
57
|
+
admin&.close
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# @return [Boolean] whether the server behind +conn+ has database +name+
|
|
61
|
+
def database_exists?(conn, name)
|
|
62
|
+
conn.exec_params("SELECT 1 FROM pg_database WHERE datname = $1", [name]).ntuples.positive?
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# Enable the extensions HTM needs in the configured database
|
|
66
|
+
def enable_extensions(config, extensions: %w[vector pg_trgm])
|
|
67
|
+
conn = pg_connect(config)
|
|
68
|
+
extensions.each { conn.exec("CREATE EXTENSION IF NOT EXISTS #{it}") }
|
|
69
|
+
ensure
|
|
70
|
+
conn&.close
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# Connect to the configured database and report pgvector
|
|
74
|
+
#
|
|
75
|
+
# @return [true]
|
|
76
|
+
# @raise [HTM::Admin::Error] when the connection fails
|
|
77
|
+
#
|
|
78
|
+
def verify(config = HTM::SequelConfig.load_database_config, out: $stdout)
|
|
79
|
+
Admin.print_counts("Verifying HTM database connection (#{HTM.env})...", {
|
|
80
|
+
'Host' => config[:host],
|
|
81
|
+
'Port' => config[:port],
|
|
82
|
+
'Database' => config[:database],
|
|
83
|
+
'User' => config[:username]
|
|
84
|
+
}, out:)
|
|
85
|
+
|
|
86
|
+
conn = pg_connect(config)
|
|
87
|
+
version = pgvector_version(conn)
|
|
88
|
+
conn.close
|
|
89
|
+
out.puts version ? " ✓ pgvector version: #{version}" : " ⚠ Warning: pgvector extension not found"
|
|
90
|
+
out.puts "✓ Connection successful!"
|
|
91
|
+
true
|
|
92
|
+
rescue PG::Error => e
|
|
93
|
+
raise Admin::Error, "✗ Connection failed: #{e.message}"
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# @return [String, nil] the installed pgvector version, nil when absent
|
|
97
|
+
def pgvector_version(conn)
|
|
98
|
+
conn.exec("SELECT extversion FROM pg_extension WHERE extname='vector'").first&.fetch('extversion')
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# @return [Array<String>] the psql command line for the configured database
|
|
102
|
+
def console_command(config = HTM::SequelConfig.load_database_config)
|
|
103
|
+
["psql", "-h", config[:host].to_s, "-p", config[:port].to_s, "-U", config[:username].to_s, "-d", config[:database].to_s]
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# Drop all HTM tables, asking first unless +confirm+
|
|
107
|
+
#
|
|
108
|
+
# @return [Symbol, nil] :dropped, nil when cancelled
|
|
109
|
+
#
|
|
110
|
+
def drop(confirm: nil) = Console.new(assume_yes: confirm).drop
|
|
111
|
+
|
|
112
|
+
# Drop all HTM tables and set the schema up again, asking first unless +confirm+
|
|
113
|
+
#
|
|
114
|
+
# @return [Symbol, nil] :reset, nil when cancelled
|
|
115
|
+
#
|
|
116
|
+
def reset(confirm: nil) = Console.new(assume_yes: confirm).reset
|
|
117
|
+
|
|
118
|
+
# Report what would be purged, ask, then purge (see Console#purge_all)
|
|
119
|
+
def purge_all = Console.new.purge_all
|
|
120
|
+
|
|
121
|
+
# Clear every embedding and regenerate them all, after confirmation (see Console#rebuild_embeddings)
|
|
122
|
+
def rebuild_embeddings = Console.new.rebuild_embeddings
|
|
123
|
+
|
|
124
|
+
# Delete every proposition node and extract them all again, after confirmation
|
|
125
|
+
# (see Console#rebuild_propositions)
|
|
126
|
+
def rebuild_propositions = Console.new.rebuild_propositions
|
|
127
|
+
|
|
128
|
+
# Delete the edges GenerateRelationshipsJob wrote and compute them again
|
|
129
|
+
# for every active node (see Console#rebuild_relationships)
|
|
130
|
+
def rebuild_relationships = Console.new.rebuild_relationships
|
|
131
|
+
|
|
132
|
+
# Row counts per HTM table, with a note for nodes
|
|
133
|
+
#
|
|
134
|
+
# @return [Array<Array(String, Integer, String)>] [table, count, note]
|
|
135
|
+
#
|
|
136
|
+
def table_counts
|
|
137
|
+
nodes = HTM::Models::Node
|
|
138
|
+
active = nodes.count
|
|
139
|
+
[
|
|
140
|
+
['robots', HTM::Models::Robot.count, ''],
|
|
141
|
+
['nodes', active,
|
|
142
|
+
" (active: #{active}, deleted: #{nodes.deleted.count}, " \
|
|
143
|
+
"with embeddings: #{nodes.exclude(embedding: nil).count})"],
|
|
144
|
+
['tags', HTM::Models::Tag.count, ''],
|
|
145
|
+
['nodes_tags', HTM::Models::NodeTag.count, ''],
|
|
146
|
+
['file_sources', HTM::Models::FileSource.count, ''],
|
|
147
|
+
*entity_table_counts(EntityCommands.counts)
|
|
148
|
+
]
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
# @param counts [Hash] EntityCommands.counts
|
|
152
|
+
# @return [Array<Array(String, Integer, String)>] [table, count, note] for the entity tables
|
|
153
|
+
def entity_table_counts(counts)
|
|
154
|
+
standing, merged = counts.values_at(:total, :merged)
|
|
155
|
+
[
|
|
156
|
+
['entities', standing + merged, " (standing: #{standing}, merged: #{merged})"],
|
|
157
|
+
['entity_aliases', counts[:aliases], ''],
|
|
158
|
+
['node_entities', counts[:mentions], ' (mentions on active nodes)']
|
|
159
|
+
]
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
# @return [Array<Array(String, Integer)>] robots with the most active nodes
|
|
163
|
+
def top_robots(limit = 5)
|
|
164
|
+
HTM::Models::RobotNode
|
|
165
|
+
.join(:nodes, id: :node_id)
|
|
166
|
+
.where(Sequel[:nodes][:deleted_at] => nil)
|
|
167
|
+
.group_and_count(:robot_id)
|
|
168
|
+
.all
|
|
169
|
+
.map { [robot_label(it[:robot_id]), it[:count]] }
|
|
170
|
+
.sort_by { -it.last }
|
|
171
|
+
.first(limit)
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
# @return [String] the name of robot +id+, or "Unknown (id)" when it is gone
|
|
175
|
+
def robot_label(id) = HTM::Models::Robot[id]&.name || "Unknown (#{id})"
|
|
176
|
+
|
|
177
|
+
# @return [Array<Array(String, Integer)>] root tag names with the most tags under them
|
|
178
|
+
def top_root_tags(limit = 5)
|
|
179
|
+
root = Sequel.lit("split_part(name, ':', 1)")
|
|
180
|
+
HTM.db[:tags]
|
|
181
|
+
.select(Sequel.as(root, :root), Sequel.function(:count, Sequel.lit('*')).as(:cnt))
|
|
182
|
+
.group(root)
|
|
183
|
+
.order(Sequel.desc(:cnt))
|
|
184
|
+
.limit(limit)
|
|
185
|
+
.all
|
|
186
|
+
.map { [it[:root], it[:cnt]] }
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
# Print record counts for every HTM table, top robots, top root tags and entities by type
|
|
190
|
+
def stats(out: $stdout)
|
|
191
|
+
Admin.connect!
|
|
192
|
+
out.puts "\nHTM Database Statistics"
|
|
193
|
+
out.puts "=" * 50
|
|
194
|
+
print_table_counts(table_counts, out)
|
|
195
|
+
out.puts "\nAdditional Statistics:"
|
|
196
|
+
print_ranking("Top robots by node count:", top_robots, out)
|
|
197
|
+
print_ranking("Top root tag categories:", top_root_tags, out)
|
|
198
|
+
print_ranking("Entities by type:", EntityCommands.counts[:by_type].to_a, out)
|
|
199
|
+
out.puts
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
def print_table_counts(rows, out)
|
|
203
|
+
width = rows.map { it.first.length }.max
|
|
204
|
+
rows.each { |name, count, note| out.puts " #{count_line(name, count, width)}#{note}" }
|
|
205
|
+
out.puts "-" * 50
|
|
206
|
+
out.puts " #{count_line('Total', rows.sum { it[1] }, width)}"
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
# @return [String] +label+ padded to +width+ and +count+ right-aligned in 8 columns
|
|
210
|
+
def count_line(label, count, width, gap: 2) = "#{label.to_s.ljust(width)}#{' ' * gap}#{count.to_s.rjust(8)}"
|
|
211
|
+
|
|
212
|
+
def print_ranking(title, rows, out)
|
|
213
|
+
return if rows.empty?
|
|
214
|
+
|
|
215
|
+
out.puts " #{title}"
|
|
216
|
+
rows.each { |name, count| out.puts " #{name}: #{count}" }
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
# Proposition nodes (active or soft-deleted) with no derived_from link:
|
|
220
|
+
# their source node was purged, which cascades away the link
|
|
221
|
+
#
|
|
222
|
+
# @return [Sequel::Dataset]
|
|
223
|
+
#
|
|
224
|
+
def orphaned_propositions
|
|
225
|
+
linked = HTM::Models::NodeLink.of_kind(:derived_from).where(from_node_id: Sequel[:nodes][:id]).select(1)
|
|
226
|
+
HTM::Models::Node.with_deleted.propositions.exclude(linked.exists)
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
# @return [Sequel::Dataset] rows of +model+ (node_tags, robot_nodes) pointing to missing nodes
|
|
230
|
+
def orphaned_links(model)
|
|
231
|
+
rows = model.with_deleted
|
|
232
|
+
ids = rows.left_join(:nodes, id: :node_id).where(Sequel[:nodes][:id] => nil).select_map(Sequel[model.table_name][:id])
|
|
233
|
+
rows.where(id: ids)
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
# @return [Sequel::Dataset] robots with no memory nodes
|
|
237
|
+
def orphaned_robots
|
|
238
|
+
linked = HTM::Models::RobotNode.where(Sequel[:robot_nodes][:robot_id] => Sequel[:robots][:id]).select(1)
|
|
239
|
+
HTM::Models::Robot.where(Sequel.~(Sequel.exists(linked)))
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
# What purge_all would delete
|
|
243
|
+
#
|
|
244
|
+
# @return [Hash{Symbol => Integer}]
|
|
245
|
+
#
|
|
246
|
+
def purge_counts
|
|
247
|
+
{
|
|
248
|
+
deleted_nodes: HTM::Models::Node.deleted.count,
|
|
249
|
+
deleted_node_tags: HTM::Models::NodeTag.deleted.count,
|
|
250
|
+
deleted_robot_nodes: HTM::Models::RobotNode.deleted.count,
|
|
251
|
+
orphaned_propositions: orphaned_propositions.count,
|
|
252
|
+
orphaned_node_tags: orphaned_links(HTM::Models::NodeTag).count,
|
|
253
|
+
orphaned_robot_nodes: orphaned_links(HTM::Models::RobotNode).count,
|
|
254
|
+
orphaned_robots: orphaned_robots.count
|
|
255
|
+
}
|
|
256
|
+
end
|
|
257
|
+
|
|
258
|
+
# Permanently delete soft-deleted and orphaned records, children first
|
|
259
|
+
#
|
|
260
|
+
# @return [Hash{Symbol => Integer}] rows deleted per kind
|
|
261
|
+
#
|
|
262
|
+
def purge!
|
|
263
|
+
purged = {}
|
|
264
|
+
# Orphaned propositions first: deleting them orphans their join rows
|
|
265
|
+
purged[:orphaned_propositions] = orphaned_propositions.delete
|
|
266
|
+
purged[:orphaned_node_tags] = orphaned_links(HTM::Models::NodeTag).delete
|
|
267
|
+
purged[:deleted_node_tags] = HTM::Models::NodeTag.deleted.delete
|
|
268
|
+
purged[:orphaned_robot_nodes] = orphaned_links(HTM::Models::RobotNode).delete
|
|
269
|
+
purged[:deleted_robot_nodes] = HTM::Models::RobotNode.deleted.delete
|
|
270
|
+
purged[:deleted_nodes] = HTM::Models::Node.deleted.delete
|
|
271
|
+
purged[:orphaned_robots] = orphaned_robots.delete
|
|
272
|
+
purged
|
|
273
|
+
end
|
|
274
|
+
|
|
275
|
+
# Labels, purge_counts keys and notes of the soft-deleted rows purge_all reports
|
|
276
|
+
SOFT_DELETED_ROWS = [
|
|
277
|
+
['nodes', :deleted_nodes, ''],
|
|
278
|
+
['node_tags', :deleted_node_tags, ''],
|
|
279
|
+
['robot_nodes', :deleted_robot_nodes, '']
|
|
280
|
+
].freeze
|
|
281
|
+
|
|
282
|
+
# Labels, purge_counts keys and notes of the orphaned rows purge_all reports
|
|
283
|
+
ORPHANED_ROWS = [
|
|
284
|
+
['propositions', :orphaned_propositions, ' (source node no longer exists)'],
|
|
285
|
+
['node_tags', :orphaned_node_tags, ' (pointing to missing nodes)'],
|
|
286
|
+
['robot_nodes', :orphaned_robot_nodes, ' (pointing to missing nodes)'],
|
|
287
|
+
['robots', :orphaned_robots, ' (no associated memory nodes)']
|
|
288
|
+
].freeze
|
|
289
|
+
|
|
290
|
+
def print_purge_counts(counts, out)
|
|
291
|
+
out.puts "\nSoft-deleted records by table:"
|
|
292
|
+
print_purge_rows(SOFT_DELETED_ROWS, counts, out)
|
|
293
|
+
out.puts "\nOrphaned records:"
|
|
294
|
+
print_purge_rows(ORPHANED_ROWS, counts, out)
|
|
295
|
+
out.puts " #{'-' * 40}"
|
|
296
|
+
out.puts " #{count_line('Total', counts.values.sum, 20, gap: 1)}"
|
|
297
|
+
end
|
|
298
|
+
|
|
299
|
+
# Print one line per [label, key, note] of +specs+ with its count from +counts+
|
|
300
|
+
def print_purge_rows(specs, counts, out)
|
|
301
|
+
specs.each { |label, key, note| out.puts " #{count_line(label, counts[key], 20, gap: 1)}#{note}" }
|
|
302
|
+
end
|
|
303
|
+
|
|
304
|
+
def print_purged(purged, out)
|
|
305
|
+
out.puts "\nPurge complete!"
|
|
306
|
+
purged.each { |kind, count| out.puts " #{"#{kind.to_s.tr('_', ' ').capitalize} purged:".ljust(30)} #{count}" }
|
|
307
|
+
out.puts " #{'-' * 40}"
|
|
308
|
+
out.puts " #{'Total records purged:'.ljust(30)} #{purged.values.sum}"
|
|
309
|
+
end
|
|
310
|
+
|
|
311
|
+
# Extract propositions for every node of +sources+ under a progress bar
|
|
312
|
+
#
|
|
313
|
+
# @return [Hash] { ok:, failed:, created: }
|
|
314
|
+
#
|
|
315
|
+
def extract_all_propositions(sources, robot, out)
|
|
316
|
+
created = 0
|
|
317
|
+
counts = Admin.each_with_progress(sources, 'Extracting', out:) do |node, bar|
|
|
318
|
+
created += Admin.extract_propositions!(node, robot, warn: bar.method(:log))
|
|
319
|
+
end
|
|
320
|
+
counts.merge(created:)
|
|
321
|
+
end
|
|
322
|
+
|
|
323
|
+
# The `htm db ...` commands that ask before they destroy anything
|
|
324
|
+
#
|
|
325
|
+
# @example
|
|
326
|
+
# HTM::Admin::DatabaseCommands::Console.new(input: StringIO.new("yes\n")).purge_all
|
|
327
|
+
#
|
|
328
|
+
class Console < Admin::Console
|
|
329
|
+
LONG_RUNNING = "This operation may take a long time depending on your LLM provider."
|
|
330
|
+
|
|
331
|
+
# Drop all HTM tables, asking first
|
|
332
|
+
#
|
|
333
|
+
# @return [Symbol, nil] :dropped, nil when cancelled
|
|
334
|
+
#
|
|
335
|
+
def drop
|
|
336
|
+
return cancelled unless confirmed?(DROP_PROMPT)
|
|
337
|
+
|
|
338
|
+
HTM::Database.drop
|
|
339
|
+
:dropped
|
|
340
|
+
end
|
|
341
|
+
|
|
342
|
+
# Drop all HTM tables and set the schema up again, asking first
|
|
343
|
+
#
|
|
344
|
+
# @return [Symbol, nil] :reset, nil when cancelled
|
|
345
|
+
#
|
|
346
|
+
def reset
|
|
347
|
+
return cancelled unless confirmed?(DROP_PROMPT)
|
|
348
|
+
|
|
349
|
+
HTM::Database.drop
|
|
350
|
+
HTM::Database.setup(dump_schema: true)
|
|
351
|
+
:reset
|
|
352
|
+
end
|
|
353
|
+
|
|
354
|
+
# Report what would be purged, ask, then purge
|
|
355
|
+
#
|
|
356
|
+
# @return [Hash, nil] rows deleted per kind, nil when nothing was purged
|
|
357
|
+
#
|
|
358
|
+
def purge_all
|
|
359
|
+
Admin.connect!
|
|
360
|
+
heading("HTM Purge All Soft-Deleted Records", 60)
|
|
361
|
+
counts = DatabaseCommands.purge_counts
|
|
362
|
+
DatabaseCommands.print_purge_counts(counts, out)
|
|
363
|
+
return say("\nNo records to purge.") if counts.values.sum.zero?
|
|
364
|
+
|
|
365
|
+
out.puts "\nWARNING: This permanently deletes records and cannot be undone!"
|
|
366
|
+
return cancelled unless confirmed?("Type 'yes' to continue with hard delete: ")
|
|
367
|
+
|
|
368
|
+
purge
|
|
369
|
+
end
|
|
370
|
+
|
|
371
|
+
# Clear every embedding and regenerate them all, after confirmation,
|
|
372
|
+
# then rebuild the relationship edges the new embeddings give
|
|
373
|
+
#
|
|
374
|
+
# @return [Hash, nil] { ok:, failed: } or nil when aborted
|
|
375
|
+
#
|
|
376
|
+
def rebuild_embeddings
|
|
377
|
+
Admin.connect!
|
|
378
|
+
total = HTM::Models::Node.count
|
|
379
|
+
embedded = HTM::Models::Node.exclude(embedding: nil)
|
|
380
|
+
announce_embeddings_rebuild(total, embedded.count)
|
|
381
|
+
return aborted unless proceed?
|
|
382
|
+
|
|
383
|
+
out.puts "\nClearing existing embeddings..."
|
|
384
|
+
out.puts " Cleared #{embedded.update(embedding: nil)} embeddings"
|
|
385
|
+
regenerate_embeddings(total, embedded).tap { rebuild_relationships }
|
|
386
|
+
end
|
|
387
|
+
|
|
388
|
+
# Delete every proposition node and extract them all again, after confirmation
|
|
389
|
+
#
|
|
390
|
+
# @return [Hash, nil] { ok:, failed:, created: } or nil when aborted
|
|
391
|
+
#
|
|
392
|
+
def rebuild_propositions
|
|
393
|
+
Admin.connect!
|
|
394
|
+
sources = HTM::Models::Node.non_propositions
|
|
395
|
+
propositions = HTM::Models::Node.propositions
|
|
396
|
+
source_count = sources.count
|
|
397
|
+
existing = propositions.count
|
|
398
|
+
announce_propositions_rebuild(source_count, existing)
|
|
399
|
+
return aborted unless proceed?
|
|
400
|
+
|
|
401
|
+
delete_propositions(propositions, existing)
|
|
402
|
+
regenerate_propositions(sources, source_count, propositions)
|
|
403
|
+
end
|
|
404
|
+
|
|
405
|
+
# Delete the edges GenerateRelationshipsJob wrote and compute them
|
|
406
|
+
# again for every active node. No LLM is called (the edges come from
|
|
407
|
+
# the stored embeddings and entity mentions), so it does not ask first.
|
|
408
|
+
#
|
|
409
|
+
# @return [Hash] { ok:, failed: } nodes processed
|
|
410
|
+
#
|
|
411
|
+
def rebuild_relationships
|
|
412
|
+
Admin.connect!
|
|
413
|
+
job = HTM::Jobs::GenerateRelationshipsJob
|
|
414
|
+
edges = HTM::Models::NodeRelationship.where(origin: job::ORIGINS)
|
|
415
|
+
heading("HTM Relationships Rebuild")
|
|
416
|
+
out.puts "Deleted #{edges.delete} edges"
|
|
417
|
+
counts = each_with_progress(HTM::Models::Node.dataset, 'Relationships') { |node, _bar| job.relate(node.id) }
|
|
418
|
+
print_counts("\nRebuild complete!", { 'Nodes processed' => counts[:ok], 'Errors' => counts[:failed], 'Edges' => edges.count })
|
|
419
|
+
counts
|
|
420
|
+
end
|
|
421
|
+
|
|
422
|
+
private
|
|
423
|
+
|
|
424
|
+
def purge
|
|
425
|
+
out.puts "\nPurging records..."
|
|
426
|
+
purged = DatabaseCommands.purge!
|
|
427
|
+
DatabaseCommands.print_purged(purged, out)
|
|
428
|
+
purged
|
|
429
|
+
end
|
|
430
|
+
|
|
431
|
+
def announce_embeddings_rebuild(total, with_embeddings)
|
|
432
|
+
heading("HTM Embeddings Rebuild")
|
|
433
|
+
print_counts("Current state:", {
|
|
434
|
+
'Total active nodes' => total,
|
|
435
|
+
'With embeddings' => with_embeddings,
|
|
436
|
+
'Without embeddings' => total - with_embeddings
|
|
437
|
+
})
|
|
438
|
+
out.puts "\nThis will regenerate embeddings for ALL #{total} nodes."
|
|
439
|
+
out.puts LONG_RUNNING
|
|
440
|
+
end
|
|
441
|
+
|
|
442
|
+
# @param embedded [Sequel::Dataset] the nodes with an embedding
|
|
443
|
+
# @return [Hash] { ok:, failed: }
|
|
444
|
+
def regenerate_embeddings(total, embedded)
|
|
445
|
+
out.puts "\nGenerating embeddings for #{total} nodes..."
|
|
446
|
+
counts = each_with_progress(HTM::Models::Node.dataset, 'Embedding') { |node, _bar| Admin.embed!(node) }
|
|
447
|
+
print_counts("\nRebuild complete!", {
|
|
448
|
+
'Nodes processed' => total,
|
|
449
|
+
'Successful' => counts[:ok],
|
|
450
|
+
'Errors' => counts[:failed],
|
|
451
|
+
'Nodes with embeddings' => embedded.count
|
|
452
|
+
})
|
|
453
|
+
counts
|
|
454
|
+
end
|
|
455
|
+
|
|
456
|
+
def announce_propositions_rebuild(source_count, existing)
|
|
457
|
+
heading("HTM Propositions Rebuild")
|
|
458
|
+
print_counts("Current state:", {
|
|
459
|
+
'Source nodes (non-propositions)' => source_count,
|
|
460
|
+
'Existing proposition nodes' => existing
|
|
461
|
+
})
|
|
462
|
+
out.puts "\nThis will extract propositions from ALL #{source_count} source nodes."
|
|
463
|
+
out.puts "Existing proposition nodes will be deleted and regenerated."
|
|
464
|
+
out.puts LONG_RUNNING
|
|
465
|
+
end
|
|
466
|
+
|
|
467
|
+
def delete_propositions(propositions, existing)
|
|
468
|
+
return unless existing.positive?
|
|
469
|
+
|
|
470
|
+
out.puts "\nDeleting #{existing} existing proposition nodes..."
|
|
471
|
+
out.puts " Deleted #{propositions.delete} proposition nodes"
|
|
472
|
+
end
|
|
473
|
+
|
|
474
|
+
# @return [Hash] { ok:, failed:, created: }
|
|
475
|
+
def regenerate_propositions(sources, source_count, propositions)
|
|
476
|
+
out.puts "\nExtracting propositions from #{source_count} nodes..."
|
|
477
|
+
counts = DatabaseCommands.extract_all_propositions(sources, Admin.proposition_robot('proposition_rebuilder'), out)
|
|
478
|
+
print_counts("\nRebuild complete!", {
|
|
479
|
+
'Source nodes processed' => counts[:ok],
|
|
480
|
+
'Propositions created' => counts[:created],
|
|
481
|
+
'Errors' => counts[:failed],
|
|
482
|
+
'Total proposition nodes' => propositions.count
|
|
483
|
+
})
|
|
484
|
+
counts
|
|
485
|
+
end
|
|
486
|
+
end
|
|
487
|
+
end
|
|
488
|
+
end
|
|
489
|
+
end
|