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,461 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'tool'
|
|
4
|
+
|
|
5
|
+
class HTM
|
|
6
|
+
module MCP
|
|
7
|
+
# Session state for the current robot
|
|
8
|
+
# Each MCP client spawns its own server process, so this is naturally isolated
|
|
9
|
+
module Session
|
|
10
|
+
DEFAULT_ROBOT_NAME = "mcp_default"
|
|
11
|
+
|
|
12
|
+
class << self
|
|
13
|
+
attr_reader :logger
|
|
14
|
+
|
|
15
|
+
# Use the given logger for all MCP session logging
|
|
16
|
+
def attach_logger(logger)
|
|
17
|
+
@logger = logger
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def htm_instance
|
|
21
|
+
@htm_instance ||= build(DEFAULT_ROBOT_NAME)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def set_robot(name)
|
|
25
|
+
@robot_name = name
|
|
26
|
+
@htm_instance = build(name)
|
|
27
|
+
logger&.info "Robot set: #{name} (id=#{@htm_instance.robot_id})"
|
|
28
|
+
@htm_instance
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def robot_name
|
|
32
|
+
@robot_name || DEFAULT_ROBOT_NAME
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def robot_initialized?
|
|
36
|
+
@robot_name != nil
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
private
|
|
40
|
+
|
|
41
|
+
# An HTM instance for the robot, its working memory refilled from the
|
|
42
|
+
# database so context survives a server restart
|
|
43
|
+
def build(name)
|
|
44
|
+
HTM.new(robot_name: name).tap { it.working_memory.restore(it.robot_id) }
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# Tool: Set the robot identity for this session
|
|
50
|
+
class SetRobotTool < Tool
|
|
51
|
+
description "Set the robot identity for this session. Call this first to establish your robot name. " \
|
|
52
|
+
"The robot's working memory from earlier sessions is restored."
|
|
53
|
+
|
|
54
|
+
arguments do
|
|
55
|
+
required(:name).filled(:string).description("The robot name (will be created if it doesn't exist)")
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def call(name:)
|
|
59
|
+
log_info "SetRobotTool called: name=#{name.inspect}"
|
|
60
|
+
|
|
61
|
+
htm = Session.set_robot(name)
|
|
62
|
+
|
|
63
|
+
{
|
|
64
|
+
success: true,
|
|
65
|
+
**robot_identity(htm),
|
|
66
|
+
node_count: robot_record(htm).node_count,
|
|
67
|
+
working_memory_nodes: htm.working_memory.node_count,
|
|
68
|
+
message: "Robot '#{name}' is now active for this session"
|
|
69
|
+
}.to_json
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# Tool: Get current robot info
|
|
74
|
+
class GetRobotTool < Tool
|
|
75
|
+
read_only!
|
|
76
|
+
description "Get information about the current robot for this session"
|
|
77
|
+
|
|
78
|
+
arguments do
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def call
|
|
82
|
+
log_info "GetRobotTool called"
|
|
83
|
+
|
|
84
|
+
htm = session_htm
|
|
85
|
+
|
|
86
|
+
{
|
|
87
|
+
success: true,
|
|
88
|
+
**robot_identity(htm),
|
|
89
|
+
initialized: Session.robot_initialized?,
|
|
90
|
+
memory_summary: robot_record(htm).memory_summary
|
|
91
|
+
}.to_json
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# Tool: Get working memory contents for session restore
|
|
96
|
+
class GetWorkingMemoryTool < Tool
|
|
97
|
+
read_only!
|
|
98
|
+
description "Get all working memory contents for the current robot. Use this to restore a previous session."
|
|
99
|
+
|
|
100
|
+
arguments do
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def call
|
|
104
|
+
htm = session_htm
|
|
105
|
+
log_info "GetWorkingMemoryTool called for robot=#{htm.robot_name}"
|
|
106
|
+
|
|
107
|
+
nodes = working_memory_entries(robot_record(htm))
|
|
108
|
+
count = nodes.length
|
|
109
|
+
|
|
110
|
+
log_info "GetWorkingMemoryTool complete: #{count} nodes in working memory"
|
|
111
|
+
{ success: true, **robot_identity(htm), count:, working_memory: nodes }.to_json
|
|
112
|
+
rescue StandardError => e
|
|
113
|
+
log_error "GetWorkingMemoryTool error: #{e.message}"
|
|
114
|
+
{ success: false, error: e.message, count: 0, working_memory: [] }.to_json
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
# Tool: Remember information
|
|
119
|
+
class RememberTool < Tool
|
|
120
|
+
OCCURRED_AT = "When it happened (ISO8601 or 'yesterday'). Leave it out when the content says when ('last Friday', " \
|
|
121
|
+
"'March 3, 2025'): HTM reads that against today's date, else uses now"
|
|
122
|
+
SUPERSEDES = "IDs of memories this one replaces. They are retired (soft-deleted, validity ended) in the same " \
|
|
123
|
+
"transaction and stay in this memory's timeline"
|
|
124
|
+
ENTITIES = "Entities (people, organizations, locations, products, events) the content is about. They are " \
|
|
125
|
+
"linked at once; entities found by extraction, when enabled, are added in the background"
|
|
126
|
+
SURFACE = "The entity as the content writes it, when not by its name (e.g., 'Dr. Chen')"
|
|
127
|
+
|
|
128
|
+
description "Store information in HTM long-term memory with optional tags, metadata and entities. " \
|
|
129
|
+
"Embeddings, tags (and propositions and entities, when enabled) are added in the background."
|
|
130
|
+
|
|
131
|
+
arguments do
|
|
132
|
+
required(:content).filled(:string).description("The content to remember")
|
|
133
|
+
optional(:tags).array(:string).description("Optional lowercase tags, levels split by colons (e.g., ['database:postgresql'])")
|
|
134
|
+
optional(:metadata).hash.description("Optional metadata key-value pairs")
|
|
135
|
+
optional(:entities).array(:hash) do
|
|
136
|
+
required(:name).filled(:string).description("The entity's name (e.g., 'Alice Chen')")
|
|
137
|
+
required(:type).filled(:string).description("Its type: one of the configured types (e.g., 'person', 'organization')")
|
|
138
|
+
optional(:surface_text).filled(:string).description(SURFACE)
|
|
139
|
+
end.description(ENTITIES)
|
|
140
|
+
optional(:occurred_at).filled(:string).description(OCCURRED_AT)
|
|
141
|
+
optional(:occurred_until).filled(:string).description("When it stopped happening, for an event span")
|
|
142
|
+
optional(:valid_from).filled(:string).description("When the stated state became true (default: occurred_at)")
|
|
143
|
+
optional(:valid_until).filled(:string).description("When the stated state stopped being true")
|
|
144
|
+
optional(:supersedes).array(:integer).description(SUPERSEDES)
|
|
145
|
+
optional(:reason).filled(:string).description("Why the superseded memories are wrong now")
|
|
146
|
+
optional(:transition).filled(:string).description("Kind of change: 'removed', 'restored' or 'changed'")
|
|
147
|
+
optional(:status).filled(:string).description(
|
|
148
|
+
"How it is known: 'asserted' (default; stated directly), 'extracted' (copied from a source), " \
|
|
149
|
+
"'inferred' (concluded from other memories) or 'hypothesis' (unconfirmed)"
|
|
150
|
+
)
|
|
151
|
+
optional(:confidence).filled(:float).description("Confidence in it, 0.0-1.0 (default: unrated)")
|
|
152
|
+
optional(:evidence).array(:integer).description(
|
|
153
|
+
"IDs of memories it rests on (give them for inferred memories and hypotheses)"
|
|
154
|
+
)
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
def call(content:, tags: [], metadata: {}, entities: [], supersedes: [], status: 'asserted', confidence: nil,
|
|
158
|
+
evidence: [], **event_time)
|
|
159
|
+
log_info "RememberTool called: content=#{content[0..50].inspect}..."
|
|
160
|
+
|
|
161
|
+
guarded do
|
|
162
|
+
htm = session_htm
|
|
163
|
+
node_id = htm.remember(content, tags:, metadata:, entities:, supersedes:, status:, confidence:, evidence:,
|
|
164
|
+
**event_time)
|
|
165
|
+
stored = stored_view(HTM::Models::Node.eager(:tags).first!(id: node_id))
|
|
166
|
+
|
|
167
|
+
log_info "Memory stored: node_id=#{node_id}, robot=#{htm.robot_name}, tags=#{stored[:tags]}"
|
|
168
|
+
|
|
169
|
+
{ success: true, node_id:, **robot_identity(htm), superseded: supersedes, **stored }.to_json
|
|
170
|
+
end
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
private
|
|
174
|
+
|
|
175
|
+
# What remember reports about the node it stored
|
|
176
|
+
def stored_view(node)
|
|
177
|
+
{
|
|
178
|
+
content: node.content,
|
|
179
|
+
**knowledge(node).except(:conflicts),
|
|
180
|
+
tags: tag_names(node),
|
|
181
|
+
entities: entities_of(node.id),
|
|
182
|
+
occurred_at: node.occurred_at&.iso8601,
|
|
183
|
+
time_source: node.time_source,
|
|
184
|
+
created_at: node.created_at.iso8601
|
|
185
|
+
}
|
|
186
|
+
end
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
# Tool: Recall memories
|
|
190
|
+
class RecallTool < Tool
|
|
191
|
+
STATUS_FILTER = "Only memories known these ways: any of 'asserted', 'extracted', 'inferred', 'hypothesis' " \
|
|
192
|
+
"(['asserted', 'extracted'] keeps direct evidence only). Each result carries its " \
|
|
193
|
+
'epistemic_status, confidence, evidence and conflicts'
|
|
194
|
+
ENTITY_FILTER = "Only memories mentioning this entity: its name, one of its aliases, or its id. " \
|
|
195
|
+
'Each result carries the entities it mentions'
|
|
196
|
+
TYPE_FILTER = "Only memories mentioning an entity of this type, or of one of several separated by commas " \
|
|
197
|
+
"(e.g., 'person' or 'person,organization')"
|
|
198
|
+
TIMEFRAME = "Time filter: natural language ('today', 'yesterday', 'last week', 'few days ago', 'last month'), " \
|
|
199
|
+
"'auto' to extract it from the query, an ISO8601 date (that day), or a range 'start..end' " \
|
|
200
|
+
'(through the end day)'
|
|
201
|
+
|
|
202
|
+
read_only!
|
|
203
|
+
description "Search and retrieve memories from HTM using semantic (vector), full-text, or hybrid search, " \
|
|
204
|
+
"optionally filtered by time, metadata and the entities they mention. Found memories join " \
|
|
205
|
+
"the robot's working memory."
|
|
206
|
+
|
|
207
|
+
arguments do
|
|
208
|
+
required(:query).filled(:string).description("Search query - can be natural language or keywords")
|
|
209
|
+
optional(:limit).filled(:integer).description("Maximum number of results (default: 10)")
|
|
210
|
+
optional(:strategy).filled(:string).description("Search strategy: 'vector', 'fulltext', or 'hybrid' (default: 'hybrid')")
|
|
211
|
+
optional(:timeframe).filled(:string).description(TIMEFRAME)
|
|
212
|
+
optional(:metadata).hash.description("Only memories whose metadata contains these key-value pairs")
|
|
213
|
+
optional(:time_axis).filled(:string).description(
|
|
214
|
+
"What timeframe filters on: 'event' (default; when the memory happened, any overlap with its span) " \
|
|
215
|
+
"or 'recorded' (when HTM stored it)"
|
|
216
|
+
)
|
|
217
|
+
optional(:as_of).filled(:string).description("Only memories whose state was true at this moment (ISO8601 or natural language)")
|
|
218
|
+
optional(:query_tags).array(:string).description("Tags that boost relevance (used with with_relevance)")
|
|
219
|
+
optional(:with_relevance).filled(:bool).description(
|
|
220
|
+
"Rank by a composite relevance score (semantic, tag, recency, access) and return it (default: false)"
|
|
221
|
+
)
|
|
222
|
+
optional(:include_history).filled(:bool).description(
|
|
223
|
+
"Follow each result with the earlier states it replaced (superseded, merged or archived), each with " \
|
|
224
|
+
"a status and a history_note saying what changed and when (default: false)"
|
|
225
|
+
)
|
|
226
|
+
optional(:status).array(:string).description(STATUS_FILTER)
|
|
227
|
+
optional(:min_confidence).filled(:float).description("Only memories at least this confident, 0.0-1.0 (unrated = 1.0)")
|
|
228
|
+
optional(:entity).filled(:string).description(ENTITY_FILTER)
|
|
229
|
+
optional(:entities).array(:string).description("Only memories mentioning these entities (names, aliases or ids)")
|
|
230
|
+
optional(:entity_match).filled(:string).description("'any' (at least one of the entities, default) or 'all' (every one)")
|
|
231
|
+
optional(:entity_type).filled(:string).description(TYPE_FILTER)
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
def call(query:, limit: 10, strategy: 'hybrid', **filters)
|
|
235
|
+
htm = session_htm
|
|
236
|
+
robot_name = htm.robot_name
|
|
237
|
+
log_info "RecallTool called: query=#{query.inspect}, strategy=#{strategy}, limit=#{limit}, robot=#{robot_name}"
|
|
238
|
+
|
|
239
|
+
guarded do
|
|
240
|
+
entity_type!(filters[:entity_type])
|
|
241
|
+
results = recall_results(htm, query, "Recall", limit:, strategy:, **filters)
|
|
242
|
+
|
|
243
|
+
{ success: true, query:, strategy:, robot_name:, count: results.length, results: }.to_json
|
|
244
|
+
end
|
|
245
|
+
end
|
|
246
|
+
end
|
|
247
|
+
|
|
248
|
+
# Tool: Forget a memory
|
|
249
|
+
class ForgetTool < Tool
|
|
250
|
+
destructive!
|
|
251
|
+
description "Forget a memory. Soft-deletes by default (recoverable with restore); " \
|
|
252
|
+
"permanent: true deletes it for good and requires confirm: 'confirmed'."
|
|
253
|
+
|
|
254
|
+
arguments do
|
|
255
|
+
required(:node_id).filled(:integer).description("The ID of the node to forget")
|
|
256
|
+
optional(:permanent).filled(:bool).description("Delete permanently instead of soft-deleting (default: false)")
|
|
257
|
+
optional(:confirm).filled(:string).description("Must be 'confirmed' when permanent is true")
|
|
258
|
+
end
|
|
259
|
+
|
|
260
|
+
def call(node_id:, permanent: false, confirm: nil)
|
|
261
|
+
htm = session_htm
|
|
262
|
+
robot_name = htm.robot_name
|
|
263
|
+
log_info "ForgetTool called: node_id=#{node_id}, permanent=#{permanent}, robot=#{robot_name}"
|
|
264
|
+
|
|
265
|
+
guarded do
|
|
266
|
+
htm.forget(node_id, soft: !permanent, confirm: htm_confirmation(confirm))
|
|
267
|
+
|
|
268
|
+
log_info "Memory #{permanent ? 'permanently deleted' : 'soft-deleted'}: node_id=#{node_id}"
|
|
269
|
+
|
|
270
|
+
message = permanent ? "Memory permanently deleted." : "Memory soft-deleted. Use restore to recover."
|
|
271
|
+
{ success: true, node_id:, robot_name:, permanent:, message: }.to_json
|
|
272
|
+
end
|
|
273
|
+
end
|
|
274
|
+
end
|
|
275
|
+
|
|
276
|
+
# Tool: Restore a forgotten memory
|
|
277
|
+
class RestoreTool < Tool
|
|
278
|
+
description "Restore a soft-deleted memory"
|
|
279
|
+
|
|
280
|
+
arguments do
|
|
281
|
+
required(:node_id).filled(:integer).description("The ID of the node to restore")
|
|
282
|
+
end
|
|
283
|
+
|
|
284
|
+
def call(node_id:)
|
|
285
|
+
htm = session_htm
|
|
286
|
+
robot_name = htm.robot_name
|
|
287
|
+
log_info "RestoreTool called: node_id=#{node_id}, robot=#{robot_name}"
|
|
288
|
+
|
|
289
|
+
guarded do
|
|
290
|
+
htm.restore(node_id)
|
|
291
|
+
|
|
292
|
+
log_info "Memory restored: node_id=#{node_id}"
|
|
293
|
+
|
|
294
|
+
{ success: true, node_id:, robot_name:, message: "Memory restored successfully" }.to_json
|
|
295
|
+
end
|
|
296
|
+
end
|
|
297
|
+
end
|
|
298
|
+
|
|
299
|
+
# Tool: List tags
|
|
300
|
+
class ListTagsTool < Tool
|
|
301
|
+
read_only!
|
|
302
|
+
description "List all tags in HTM, optionally filtered by prefix"
|
|
303
|
+
|
|
304
|
+
arguments do
|
|
305
|
+
optional(:prefix).filled(:string).description("Filter tags by prefix (e.g., 'database' returns 'database:postgresql', etc.)")
|
|
306
|
+
end
|
|
307
|
+
|
|
308
|
+
def call(prefix: nil)
|
|
309
|
+
log_info "ListTagsTool called: prefix=#{prefix.inspect}"
|
|
310
|
+
|
|
311
|
+
tags = tags_dataset(prefix).map { { name: it.name, node_count: it.nodes.count } }
|
|
312
|
+
count = tags.length
|
|
313
|
+
|
|
314
|
+
log_info "ListTagsTool complete: found #{count} tags"
|
|
315
|
+
|
|
316
|
+
{ success: true, prefix:, count:, tags: }.to_json
|
|
317
|
+
end
|
|
318
|
+
|
|
319
|
+
private
|
|
320
|
+
|
|
321
|
+
# All tags ordered by name, narrowed to a prefix when one is given
|
|
322
|
+
def tags_dataset(prefix)
|
|
323
|
+
dataset = HTM::Models::Tag.order(:name)
|
|
324
|
+
prefix ? dataset.with_prefix(prefix) : dataset
|
|
325
|
+
end
|
|
326
|
+
end
|
|
327
|
+
|
|
328
|
+
# Tool: Search tags with fuzzy matching
|
|
329
|
+
class SearchTagsTool < Tool
|
|
330
|
+
read_only!
|
|
331
|
+
description "Search for tags using fuzzy matching (typo-tolerant). Use this when you're unsure of exact tag names."
|
|
332
|
+
|
|
333
|
+
arguments do
|
|
334
|
+
required(:query).filled(:string).description("Search query - can contain typos (e.g., 'postgrsql' finds 'database:postgresql')")
|
|
335
|
+
optional(:limit).filled(:integer).description("Maximum number of results (default: 20)")
|
|
336
|
+
optional(:min_similarity).filled(:float).description("Minimum similarity threshold 0.0-1.0 (default: 0.3, lower = more fuzzy)")
|
|
337
|
+
end
|
|
338
|
+
|
|
339
|
+
def call(query:, limit: 20, min_similarity: 0.3)
|
|
340
|
+
log_info "SearchTagsTool called: query=#{query.inspect}, limit=#{limit}, min_similarity=#{min_similarity}"
|
|
341
|
+
|
|
342
|
+
results = session_htm.long_term_memory.search_tags(query, limit:, min_similarity:)
|
|
343
|
+
tags = results.map { tag_match(it) }
|
|
344
|
+
count = tags.length
|
|
345
|
+
|
|
346
|
+
log_info "SearchTagsTool complete: found #{count} tags"
|
|
347
|
+
|
|
348
|
+
{ success: true, query:, min_similarity:, count:, tags: }.to_json
|
|
349
|
+
end
|
|
350
|
+
|
|
351
|
+
private
|
|
352
|
+
|
|
353
|
+
# A search_tags row enriched with the tag's node count
|
|
354
|
+
def tag_match(result)
|
|
355
|
+
name = result[:name]
|
|
356
|
+
tag = HTM::Models::Tag.first(name:)
|
|
357
|
+
{
|
|
358
|
+
name:,
|
|
359
|
+
similarity: result[:similarity].round(3),
|
|
360
|
+
node_count: tag&.nodes&.count || 0
|
|
361
|
+
}
|
|
362
|
+
end
|
|
363
|
+
end
|
|
364
|
+
|
|
365
|
+
# Tool: Find nodes by topic with fuzzy option
|
|
366
|
+
class FindByTopicTool < Tool
|
|
367
|
+
read_only!
|
|
368
|
+
description "Find memory nodes by topic/tag with optional fuzzy matching for typo tolerance"
|
|
369
|
+
|
|
370
|
+
arguments do
|
|
371
|
+
required(:topic).filled(:string).description("Topic or tag to search for (e.g., 'database:postgresql' or 'postgrsql' with fuzzy)")
|
|
372
|
+
optional(:fuzzy).filled(:bool).description("Enable fuzzy matching for typo tolerance (default: false)")
|
|
373
|
+
optional(:exact).filled(:bool).description("Require exact tag match (default: false, uses prefix matching)")
|
|
374
|
+
optional(:limit).filled(:integer).description("Maximum number of results (default: 20)")
|
|
375
|
+
optional(:min_similarity).filled(:float).description("Minimum similarity for fuzzy mode (default: 0.3)")
|
|
376
|
+
end
|
|
377
|
+
|
|
378
|
+
# fuzzy and exact are optional flags; an omitted flag means false
|
|
379
|
+
def call(topic:, fuzzy: nil, exact: nil, limit: 20, min_similarity: 0.3)
|
|
380
|
+
flags = { fuzzy: fuzzy == true, exact: exact == true }
|
|
381
|
+
log_info "FindByTopicTool called: topic=#{topic.inspect}, fuzzy=#{flags[:fuzzy]}, exact=#{flags[:exact]}"
|
|
382
|
+
|
|
383
|
+
nodes = session_htm.long_term_memory.nodes_by_topic(topic, **flags, min_similarity:, limit:)
|
|
384
|
+
results = topic_results(nodes.map { it['id'] })
|
|
385
|
+
count = results.length
|
|
386
|
+
|
|
387
|
+
log_info "FindByTopicTool complete: found #{count} nodes"
|
|
388
|
+
|
|
389
|
+
{ success: true, topic:, **flags, count:, results: }.to_json
|
|
390
|
+
end
|
|
391
|
+
|
|
392
|
+
private
|
|
393
|
+
|
|
394
|
+
# The nodes with these ids as truncated node summaries, in the order given; nodes that are gone are left out
|
|
395
|
+
def topic_results(ids)
|
|
396
|
+
return [] if ids.empty?
|
|
397
|
+
|
|
398
|
+
nodes = HTM::Models::Node.where(id: ids).eager(:tags).all.to_h { [it.id, it] }
|
|
399
|
+
node_summaries(nodes.values_at(*ids).compact, content_limit: 200)
|
|
400
|
+
end
|
|
401
|
+
end
|
|
402
|
+
|
|
403
|
+
# Tool: Get memory statistics
|
|
404
|
+
class StatsTool < Tool
|
|
405
|
+
read_only!
|
|
406
|
+
description "Get statistics about HTM memory: nodes, enrichment coverage (embeddings, tags, propositions), " \
|
|
407
|
+
"tags, robots, loaded files, relationships, and this session's working memory"
|
|
408
|
+
|
|
409
|
+
arguments do
|
|
410
|
+
end
|
|
411
|
+
|
|
412
|
+
def call
|
|
413
|
+
htm = session_htm
|
|
414
|
+
log_info "StatsTool called for robot=#{htm.robot_name}"
|
|
415
|
+
|
|
416
|
+
robot = current_robot(htm)
|
|
417
|
+
statistics = memory_statistics
|
|
418
|
+
|
|
419
|
+
log_info "StatsTool complete: #{statistics.dig(:nodes, :active)} active nodes, #{statistics.dig(:tags, :total)} tags"
|
|
420
|
+
|
|
421
|
+
{ success: true, current_robot: robot, statistics: }.to_json
|
|
422
|
+
rescue StandardError => e
|
|
423
|
+
log_error "StatsTool error: #{e.message}"
|
|
424
|
+
failure(e.message)
|
|
425
|
+
end
|
|
426
|
+
|
|
427
|
+
private
|
|
428
|
+
|
|
429
|
+
def current_robot(htm)
|
|
430
|
+
id = htm.robot_id
|
|
431
|
+
working_memory = htm.working_memory
|
|
432
|
+
{
|
|
433
|
+
name: htm.robot_name,
|
|
434
|
+
id:,
|
|
435
|
+
memory_summary: HTM::Models::Robot[id].memory_summary,
|
|
436
|
+
working_memory: {
|
|
437
|
+
nodes: working_memory.node_count,
|
|
438
|
+
tokens: working_memory.token_count,
|
|
439
|
+
max_tokens: working_memory.max_tokens,
|
|
440
|
+
utilization: working_memory.utilization_percentage
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
end
|
|
444
|
+
end
|
|
445
|
+
|
|
446
|
+
# All individual tools for registration
|
|
447
|
+
TOOLS = [
|
|
448
|
+
SetRobotTool,
|
|
449
|
+
GetRobotTool,
|
|
450
|
+
GetWorkingMemoryTool,
|
|
451
|
+
RememberTool,
|
|
452
|
+
RecallTool,
|
|
453
|
+
ForgetTool,
|
|
454
|
+
RestoreTool,
|
|
455
|
+
ListTagsTool,
|
|
456
|
+
SearchTagsTool,
|
|
457
|
+
FindByTopicTool,
|
|
458
|
+
StatsTool
|
|
459
|
+
].freeze
|
|
460
|
+
end
|
|
461
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: htm-cli
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.2.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Dewayne VanHoozer
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: htm
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
-
|
|
17
|
+
- ~>
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: 0.2.0
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
-
|
|
25
|
+
- ~>
|
|
26
|
+
- !ruby/object:Gem::Version
|
|
27
|
+
version: 0.2.0
|
|
28
|
+
- !ruby/object:Gem::Dependency
|
|
29
|
+
name: fast-mcp
|
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
|
31
|
+
requirements:
|
|
32
|
+
-
|
|
33
|
+
- ">="
|
|
34
|
+
- !ruby/object:Gem::Version
|
|
35
|
+
version: "0"
|
|
36
|
+
type: :runtime
|
|
37
|
+
prerelease: false
|
|
38
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
39
|
+
requirements:
|
|
40
|
+
-
|
|
41
|
+
- ">="
|
|
42
|
+
- !ruby/object:Gem::Version
|
|
43
|
+
version: "0"
|
|
44
|
+
- !ruby/object:Gem::Dependency
|
|
45
|
+
name: logger
|
|
46
|
+
requirement: !ruby/object:Gem::Requirement
|
|
47
|
+
requirements:
|
|
48
|
+
-
|
|
49
|
+
- ">="
|
|
50
|
+
- !ruby/object:Gem::Version
|
|
51
|
+
version: "0"
|
|
52
|
+
type: :runtime
|
|
53
|
+
prerelease: false
|
|
54
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
55
|
+
requirements:
|
|
56
|
+
-
|
|
57
|
+
- ">="
|
|
58
|
+
- !ruby/object:Gem::Version
|
|
59
|
+
version: "0"
|
|
60
|
+
- !ruby/object:Gem::Dependency
|
|
61
|
+
name: ruby-progressbar
|
|
62
|
+
requirement: !ruby/object:Gem::Requirement
|
|
63
|
+
requirements:
|
|
64
|
+
-
|
|
65
|
+
- ">="
|
|
66
|
+
- !ruby/object:Gem::Version
|
|
67
|
+
version: "0"
|
|
68
|
+
type: :runtime
|
|
69
|
+
prerelease: false
|
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
71
|
+
requirements:
|
|
72
|
+
-
|
|
73
|
+
- ">="
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: "0"
|
|
76
|
+
- !ruby/object:Gem::Dependency
|
|
77
|
+
name: thor
|
|
78
|
+
requirement: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
-
|
|
81
|
+
- ~>
|
|
82
|
+
- !ruby/object:Gem::Version
|
|
83
|
+
version: "1.3"
|
|
84
|
+
type: :runtime
|
|
85
|
+
prerelease: false
|
|
86
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
87
|
+
requirements:
|
|
88
|
+
-
|
|
89
|
+
- ~>
|
|
90
|
+
- !ruby/object:Gem::Version
|
|
91
|
+
version: "1.3"
|
|
92
|
+
description: |
|
|
93
|
+
The htm command: database setup and maintenance, file loading, tag, entity
|
|
94
|
+
and memory commands for an HTM (Hierarchical Temporal Memory) store, and
|
|
95
|
+
`htm mcp`, an MCP server that gives MCP clients HTM's long-term memory.
|
|
96
|
+
The memory itself is the htm gem; this gem is the application around it.
|
|
97
|
+
email:
|
|
98
|
+
- dvanhoozer@gmail.com
|
|
99
|
+
executables:
|
|
100
|
+
- htm
|
|
101
|
+
extensions: []
|
|
102
|
+
extra_rdoc_files: []
|
|
103
|
+
files:
|
|
104
|
+
- CHANGELOG.md
|
|
105
|
+
- COMMITS.md
|
|
106
|
+
- LICENSE.txt
|
|
107
|
+
- README.md
|
|
108
|
+
- Rakefile
|
|
109
|
+
- bin/htm
|
|
110
|
+
- lib/htm/admin.rb
|
|
111
|
+
- lib/htm/admin/database_commands.rb
|
|
112
|
+
- lib/htm/admin/enrich_commands.rb
|
|
113
|
+
- lib/htm/admin/entity_commands.rb
|
|
114
|
+
- lib/htm/admin/entity_graph.rb
|
|
115
|
+
- lib/htm/admin/file_commands.rb
|
|
116
|
+
- lib/htm/admin/memory_commands.rb
|
|
117
|
+
- lib/htm/admin/memory_commands/edit.rb
|
|
118
|
+
- lib/htm/admin/memory_commands/entities.rb
|
|
119
|
+
- lib/htm/admin/memory_commands/entity_lines.rb
|
|
120
|
+
- lib/htm/admin/memory_commands/search.rb
|
|
121
|
+
- lib/htm/admin/memory_commands/tag_search.rb
|
|
122
|
+
- lib/htm/admin/memory_commands/text.rb
|
|
123
|
+
- lib/htm/admin/memory_commands/working_memory.rb
|
|
124
|
+
- lib/htm/admin/tag_commands.rb
|
|
125
|
+
- lib/htm/admin/tag_graph.rb
|
|
126
|
+
- lib/htm/admin/templates/tag_graph.html
|
|
127
|
+
- lib/htm/cli.rb
|
|
128
|
+
- lib/htm/cli/config_file.rb
|
|
129
|
+
- lib/htm/cli/db.rb
|
|
130
|
+
- lib/htm/cli/enrich.rb
|
|
131
|
+
- lib/htm/cli/entities.rb
|
|
132
|
+
- lib/htm/cli/environment.rb
|
|
133
|
+
- lib/htm/cli/error_suggestion.rb
|
|
134
|
+
- lib/htm/cli/files.rb
|
|
135
|
+
- lib/htm/cli/group.rb
|
|
136
|
+
- lib/htm/cli/memory.rb
|
|
137
|
+
- lib/htm/cli/memory_command.rb
|
|
138
|
+
- lib/htm/cli/robots.rb
|
|
139
|
+
- lib/htm/cli/support.rb
|
|
140
|
+
- lib/htm/cli/tags.rb
|
|
141
|
+
- lib/htm/cli/version.rb
|
|
142
|
+
- lib/htm/cli/working_memory.rb
|
|
143
|
+
- lib/htm/mcp/entity_tools.rb
|
|
144
|
+
- lib/htm/mcp/file_tools.rb
|
|
145
|
+
- lib/htm/mcp/group_tools.rb
|
|
146
|
+
- lib/htm/mcp/node_tools.rb
|
|
147
|
+
- lib/htm/mcp/resources.rb
|
|
148
|
+
- lib/htm/mcp/robot_team_tools.rb
|
|
149
|
+
- lib/htm/mcp/server.rb
|
|
150
|
+
- lib/htm/mcp/tag_tools.rb
|
|
151
|
+
- lib/htm/mcp/tool.rb
|
|
152
|
+
- lib/htm/mcp/tool_support.rb
|
|
153
|
+
- lib/htm/mcp/tools.rb
|
|
154
|
+
homepage: "https://github.com/madbomber/htm-cli"
|
|
155
|
+
licenses:
|
|
156
|
+
- MIT
|
|
157
|
+
metadata:
|
|
158
|
+
homepage_uri: "https://github.com/madbomber/htm-cli"
|
|
159
|
+
source_code_uri: "https://github.com/madbomber/htm-cli"
|
|
160
|
+
changelog_uri: "https://github.com/madbomber/htm-cli/blob/main/CHANGELOG.md"
|
|
161
|
+
rubygems_mfa_required: "true"
|
|
162
|
+
rdoc_options: []
|
|
163
|
+
require_paths:
|
|
164
|
+
- lib
|
|
165
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
166
|
+
requirements:
|
|
167
|
+
-
|
|
168
|
+
- ">="
|
|
169
|
+
- !ruby/object:Gem::Version
|
|
170
|
+
version: "4.0"
|
|
171
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
172
|
+
requirements:
|
|
173
|
+
-
|
|
174
|
+
- ">="
|
|
175
|
+
- !ruby/object:Gem::Version
|
|
176
|
+
version: "0"
|
|
177
|
+
requirements: []
|
|
178
|
+
rubygems_version: 4.1.1
|
|
179
|
+
specification_version: 4
|
|
180
|
+
summary: The htm command line and MCP server for HTM (Hierarchical Temporal Memory)
|
|
181
|
+
test_files: []
|