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,410 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# HTM memory commands: search, remember, inspect and forget memories
|
|
4
|
+
#
|
|
5
|
+
# htm recall "how do we store embeddings" --strategy hybrid --timeframe "last week"
|
|
6
|
+
# htm remember "We chose PostgreSQL" --tags database:postgresql decision
|
|
7
|
+
# htm show 42
|
|
8
|
+
# htm timeline "where does Alice live"
|
|
9
|
+
# htm first_mention "Alice moved to Berlin"
|
|
10
|
+
# htm confirm 88 --evidence 91
|
|
11
|
+
# htm forget 42 43 # soft delete; htm restore 42 43 brings them back
|
|
12
|
+
# htm recall travel --entity "Alice Chen"
|
|
13
|
+
|
|
14
|
+
require_relative "group"
|
|
15
|
+
require_relative "memory_command"
|
|
16
|
+
|
|
17
|
+
class HTM
|
|
18
|
+
# The memory commands at the root of the CLI (htm recall, htm remember,
|
|
19
|
+
# ...): search, store, inspect and forget memories as a robot
|
|
20
|
+
class CLI < Thor
|
|
21
|
+
include MemoryCommand
|
|
22
|
+
|
|
23
|
+
map "search" => :recall
|
|
24
|
+
|
|
25
|
+
# htm remember's event-time options, passed on to HTM#remember
|
|
26
|
+
EVENT_TIME_OPTIONS = %w[occurred_at occurred_until valid_from valid_until].freeze
|
|
27
|
+
|
|
28
|
+
# Epistemic statuses (HTM::Epistemics::STATUSES; a literal, the CLI loads before HTM)
|
|
29
|
+
STATUSES = %w[asserted extracted inferred hypothesis].freeze
|
|
30
|
+
|
|
31
|
+
desc "recall QUERY...", "Search memory by meaning, keywords or both (alias: search)"
|
|
32
|
+
long_desc <<~DESC, wrap: false
|
|
33
|
+
Search long-term memory. QUERY is every argument joined by spaces, or
|
|
34
|
+
stdin when it is omitted or "-". Found memories join the robot's working
|
|
35
|
+
memory, as they do through the MCP server. An option the command does
|
|
36
|
+
not have is an error; put -- before words that start with a dash
|
|
37
|
+
(htm recall -- -5 degrees).
|
|
38
|
+
|
|
39
|
+
--strategy: hybrid (default) combines vector similarity and full-text
|
|
40
|
+
ranking; vector finds memories by meaning (needs embeddings); fulltext
|
|
41
|
+
matches keywords and works without an LLM.
|
|
42
|
+
|
|
43
|
+
--timeframe: natural language ("today", "last week", "few days ago",
|
|
44
|
+
"last month"), an ISO8601 date (that whole day), an ISO8601 range
|
|
45
|
+
"2026-01-01..2026-01-31" (through the end day), or "auto" to take
|
|
46
|
+
it from the query
|
|
47
|
+
("what did we decide last week about postgres").
|
|
48
|
+
|
|
49
|
+
--metadata key:value ...: only memories whose metadata contains every
|
|
50
|
+
pair. Values that read as JSON (2, true, null) match that type.
|
|
51
|
+
|
|
52
|
+
--time-axis: what --timeframe reads. event (default) is when the memory
|
|
53
|
+
happened (a span overlapping the timeframe matches); recorded is when
|
|
54
|
+
HTM stored it.
|
|
55
|
+
|
|
56
|
+
--as-of TIME: only memories whose state was true at TIME (ISO8601 or
|
|
57
|
+
natural language): valid from (or occurred) by then and not yet
|
|
58
|
+
invalidated, e.g. what was true before a decision changed.
|
|
59
|
+
|
|
60
|
+
--relevance ranks by a composite score (semantic, tag, recency, access);
|
|
61
|
+
--boost TAG ... raises memories carrying those tags.
|
|
62
|
+
|
|
63
|
+
--history follows each result with the states it replaced (superseded,
|
|
64
|
+
merged or archived memories), each labelled with what changed and when.
|
|
65
|
+
Those earlier states do not join working memory.
|
|
66
|
+
|
|
67
|
+
--status STATUS ...: only memories known that way: asserted (stated
|
|
68
|
+
directly), extracted (copied from a source: propositions, file
|
|
69
|
+
chunks), inferred (concluded from other memories) or hypothesis.
|
|
70
|
+
--status asserted extracted keeps direct evidence only.
|
|
71
|
+
--min-confidence N keeps memories at least N confident (0 to 1; an
|
|
72
|
+
unrated memory counts as 1).
|
|
73
|
+
|
|
74
|
+
--entity NAME: only memories mentioning that entity (its name, an
|
|
75
|
+
alias or its id; see `htm entities list`). Repeat it for several
|
|
76
|
+
entities: memories mentioning any of them, or with --entity-match all
|
|
77
|
+
every one. An unknown name matches nothing. --entity-type TYPE keeps
|
|
78
|
+
memories mentioning an entity of that type (person, organization,
|
|
79
|
+
...; several as person,organization) and is where the names are
|
|
80
|
+
looked up. A hybrid search also ranks up the memories about entities
|
|
81
|
+
the query names.
|
|
82
|
+
|
|
83
|
+
Examples:
|
|
84
|
+
|
|
85
|
+
htm recall pgvector
|
|
86
|
+
htm search "how are embeddings stored" -s vector -l 5 --full
|
|
87
|
+
htm recall deployment -t "last week" -m environment:production
|
|
88
|
+
htm recall "what did we decide last month about caching" -t auto
|
|
89
|
+
htm recall deploys -t "last week" --time-axis recorded
|
|
90
|
+
htm recall "where does Alice live" --as-of 2025-01-01
|
|
91
|
+
htm recall "where does Alice live" --history
|
|
92
|
+
htm recall "Alice" --status asserted extracted --min-confidence 0.8
|
|
93
|
+
htm recall travel --entity "Alice Chen" --entity Globex --entity-match all
|
|
94
|
+
htm recall "what moved" --entity-type location -t "last month"
|
|
95
|
+
htm recall postgres --json | jq '.results[].id'
|
|
96
|
+
DESC
|
|
97
|
+
memory_options
|
|
98
|
+
full_option
|
|
99
|
+
limit_option 10
|
|
100
|
+
option :strategy, aliases: "-s", default: "hybrid", enum: %w[hybrid vector fulltext], desc: "Search strategy"
|
|
101
|
+
option :timeframe, aliases: "-t", desc: "Only memories from this time (see above)"
|
|
102
|
+
option :metadata, aliases: "-m", type: :hash, banner: "KEY:VALUE ...", desc: "Only memories with this metadata"
|
|
103
|
+
option :time_axis, default: "event", enum: %w[event recorded], desc: "Time --timeframe reads: when it happened or when it was stored"
|
|
104
|
+
option :as_of, banner: "TIME", desc: "Only memories whose state was true at TIME"
|
|
105
|
+
option :relevance, type: :boolean, desc: "Rank by composite relevance and show the score"
|
|
106
|
+
option :boost, type: :array, banner: "TAG ...", desc: "Tags that raise relevance (with --relevance)"
|
|
107
|
+
option :history, type: :boolean, desc: "Follow each result with the states it replaced"
|
|
108
|
+
option :status, type: :array, banner: "STATUS ...", desc: "Only memories known this way (#{STATUSES.join(', ')})"
|
|
109
|
+
option :min_confidence, type: :numeric, banner: "N", desc: "Only memories at least N confident (0-1)"
|
|
110
|
+
option :entity, repeatable: true, banner: "NAME", desc: "Only memories mentioning this entity (repeat for several)"
|
|
111
|
+
option :entity_match, default: "any", enum: %w[any all], desc: "With several --entity: any of them, or all"
|
|
112
|
+
option :entity_type, banner: "TYPE", desc: "Only memories mentioning an entity of this type"
|
|
113
|
+
def recall(*words)
|
|
114
|
+
memory_console(:Search) do |console|
|
|
115
|
+
console.recall(text_from(words), strategy: options[:strategy], limit: options[:limit],
|
|
116
|
+
timeframe: options[:timeframe], metadata: metadata_option,
|
|
117
|
+
query_tags: options[:boost], with_relevance: options[:relevance],
|
|
118
|
+
time_axis: options[:time_axis], as_of: options[:as_of],
|
|
119
|
+
include_history: options[:history] == true, status: options[:status],
|
|
120
|
+
min_confidence: options[:min_confidence], **entity_filters)
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
desc "remember [TEXT...]", "Store a memory (TEXT, or stdin when omitted or -)"
|
|
125
|
+
long_desc <<~DESC, wrap: false
|
|
126
|
+
Store TEXT in long-term memory and the robot's working memory. Its
|
|
127
|
+
embedding and tags (and propositions, when enabled) are generated by
|
|
128
|
+
the configured LLM before the command returns. An option the command
|
|
129
|
+
does not have is an error; put -- before text that starts with a dash
|
|
130
|
+
(htm remember -- -5 degrees outside).
|
|
131
|
+
|
|
132
|
+
When it happened: --occurred-at TIME (ISO8601 or natural language),
|
|
133
|
+
else a time the text states ("yesterday", "3 days ago", "March 3,
|
|
134
|
+
2025"), else now. --occurred-until ends a span. --valid-from and
|
|
135
|
+
--valid-until bound when a stated state was true.
|
|
136
|
+
|
|
137
|
+
--supersedes ID ... replaces memories that are no longer true: each is
|
|
138
|
+
retired (soft-deleted, its validity ended) in the same transaction and
|
|
139
|
+
stays in the new memory's timeline. --reason says why; --transition
|
|
140
|
+
says what kind of change it was: removed, restored or changed.
|
|
141
|
+
|
|
142
|
+
How it is known: --status asserted (default: stated directly),
|
|
143
|
+
extracted (copied from a source), inferred (concluded from other
|
|
144
|
+
memories) or hypothesis (unconfirmed). --confidence N rates it from 0
|
|
145
|
+
to 1. --evidence ID ... names the memories it rests on; each is linked
|
|
146
|
+
and shown in recall and working-memory context. Confirm a hypothesis
|
|
147
|
+
or inference later with `htm confirm`.
|
|
148
|
+
|
|
149
|
+
--entities NAME:TYPE ... names the entities it mentions, each as the
|
|
150
|
+
name, a colon and the type (person, organization, location, product,
|
|
151
|
+
event, ...); quote one with spaces. They are linked whether or not
|
|
152
|
+
entity.enabled is on; an unknown name becomes a new entity.
|
|
153
|
+
|
|
154
|
+
Examples:
|
|
155
|
+
|
|
156
|
+
htm remember "The API rate limit is 1000 requests a minute" -t api:rate-limiting
|
|
157
|
+
htm remember "User prefers dark mode" -m category:preference priority:high
|
|
158
|
+
git log -1 --format=%B | htm remember -t project:htm
|
|
159
|
+
htm remember "Deployed v2 to production" --occurred-at 2026-03-03
|
|
160
|
+
htm remember "Alice lives in Berlin" --valid-from 2025-06-01
|
|
161
|
+
htm remember "Alice lives in Berlin" --supersedes 41 --reason "moved" --transition changed
|
|
162
|
+
htm remember "Alice probably moved to Berlin" --status inferred --confidence 0.6 --evidence 12 15
|
|
163
|
+
htm remember "Alice joined Globex" --entities "Alice Chen:person" Globex:organization
|
|
164
|
+
DESC
|
|
165
|
+
memory_options
|
|
166
|
+
option :tags, aliases: "-t", type: :array, default: [], banner: "TAG ...", desc: "Tags to add (e.g. database:postgresql)"
|
|
167
|
+
option :metadata, aliases: "-m", type: :hash, banner: "KEY:VALUE ...", desc: "Metadata to store"
|
|
168
|
+
option :occurred_at, banner: "TIME", desc: "When it happened (default: a time the text states, else now)"
|
|
169
|
+
option :occurred_until, banner: "TIME", desc: "When it stopped happening (for a span)"
|
|
170
|
+
option :valid_from, banner: "TIME", desc: "When the stated state became true"
|
|
171
|
+
option :valid_until, banner: "TIME", desc: "When the stated state stopped being true"
|
|
172
|
+
option :supersedes, type: :array, banner: "ID ...", desc: "Memories this one replaces (kept in its timeline)"
|
|
173
|
+
option :reason, banner: "TEXT", desc: "Why the superseded memories are wrong now"
|
|
174
|
+
option :transition, enum: %w[removed restored changed], desc: "Kind of change (with --supersedes)"
|
|
175
|
+
option :status, default: "asserted", enum: STATUSES, desc: "How it is known"
|
|
176
|
+
option :confidence, type: :numeric, banner: "N", desc: "Confidence in it, 0 to 1"
|
|
177
|
+
option :evidence, type: :array, banner: "ID ...", desc: "Memories it rests on"
|
|
178
|
+
option :entities, type: :array, banner: "NAME:TYPE ...", desc: "Entities it mentions (e.g. \"Alice Chen:person\")"
|
|
179
|
+
def remember(*words)
|
|
180
|
+
supersedes = node_ids_option(:supersedes)
|
|
181
|
+
evidence = node_ids_option(:evidence)
|
|
182
|
+
memory_console(:Edit) do |console|
|
|
183
|
+
console.remember(text_from(words), tags: options[:tags], metadata: metadata_option,
|
|
184
|
+
supersedes:, reason: options[:reason], transition: options[:transition],
|
|
185
|
+
status: options[:status], confidence: options[:confidence], evidence:,
|
|
186
|
+
entities: HTM::Admin::EntityCommands.parse_known(options[:entities]),
|
|
187
|
+
**options.slice(*EVENT_TIME_OPTIONS).transform_keys(&:to_sym))
|
|
188
|
+
end
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
desc "confirm ID --evidence ID...", "Confirm a memory: mark it asserted on the strength of other memories"
|
|
192
|
+
long_desc <<~DESC, wrap: false
|
|
193
|
+
Upgrade an inferred memory or hypothesis (or an extracted fact) to
|
|
194
|
+
asserted. Each --evidence memory is linked as confirming it, keeping
|
|
195
|
+
the status it had before, so `htm show ID` explains the change.
|
|
196
|
+
--confidence N sets its new confidence (0 to 1).
|
|
197
|
+
|
|
198
|
+
Examples:
|
|
199
|
+
|
|
200
|
+
htm confirm 88 --evidence 91 --reason "Alice said so herself"
|
|
201
|
+
htm confirm 88 -e 91 92 --confidence 0.95
|
|
202
|
+
DESC
|
|
203
|
+
memory_options
|
|
204
|
+
option :evidence, aliases: "-e", type: :array, required: true, banner: "ID ...", desc: "Memories that confirm it"
|
|
205
|
+
option :reason, banner: "TEXT", desc: "Why the evidence settles it"
|
|
206
|
+
option :confidence, type: :numeric, banner: "N", desc: "Its new confidence, 0 to 1"
|
|
207
|
+
def confirm(id)
|
|
208
|
+
evidence = Array(options[:evidence]).map { node_id(it) }
|
|
209
|
+
memory_console(:Edit) do |console|
|
|
210
|
+
console.confirm(node_id(id), evidence:, reason: options[:reason], confidence: options[:confidence])
|
|
211
|
+
end
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
desc "first_mention QUERY...", "When was something first mentioned, and can memory establish it?"
|
|
215
|
+
long_desc <<~DESC, wrap: false
|
|
216
|
+
Search for QUERY (every argument joined by spaces, or stdin) and judge
|
|
217
|
+
the matches. Direct evidence is a memory stated (asserted) or
|
|
218
|
+
extracted from a source, with a known event time; inferred memories,
|
|
219
|
+
hypotheses and memories whose date was read from their text are
|
|
220
|
+
indirect. The first mention is established only when no indirect
|
|
221
|
+
evidence points earlier than the earliest direct match; otherwise the
|
|
222
|
+
earlier indirect matches are listed so you can see what is uncertain.
|
|
223
|
+
|
|
224
|
+
Examples:
|
|
225
|
+
|
|
226
|
+
htm first_mention "Alice moved to Berlin"
|
|
227
|
+
htm first_mention rate limit -s hybrid -t "last year" --json
|
|
228
|
+
DESC
|
|
229
|
+
memory_options
|
|
230
|
+
limit_option 50
|
|
231
|
+
option :strategy, aliases: "-s", default: "fulltext", enum: %w[hybrid vector fulltext], desc: "Search strategy"
|
|
232
|
+
option :timeframe, aliases: "-t", desc: "Only memories from this time (as for recall)"
|
|
233
|
+
def first_mention(*words)
|
|
234
|
+
memory_console(:Search) do |console|
|
|
235
|
+
console.first_mention(text_from(words), strategy: options[:strategy], limit: options[:limit],
|
|
236
|
+
timeframe: options[:timeframe])
|
|
237
|
+
end
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
desc "show ID", "Show one memory in full: content, tags, entities, metadata, status, how it is known, links, timestamps, source"
|
|
241
|
+
memory_options
|
|
242
|
+
def show(id) = memory_console(:Search) { it.show(node_id(id)) }
|
|
243
|
+
|
|
244
|
+
desc "timeline [QUERY...|ID]", "Show how a memory (or with --entity what is known of an entity) changed over time"
|
|
245
|
+
long_desc <<~DESC, wrap: false
|
|
246
|
+
Show the history of a memory: every state it went through (including
|
|
247
|
+
superseded, merged and archived ones, which recall leaves out) and the
|
|
248
|
+
change between each pair, with its kind (removed, restored, changed)
|
|
249
|
+
and reason, oldest first.
|
|
250
|
+
|
|
251
|
+
With a node ID (any state in the history, retired or not) shows that
|
|
252
|
+
history. Otherwise QUERY (every argument joined by spaces, or stdin)
|
|
253
|
+
is searched with --strategy and the histories of its top --limit
|
|
254
|
+
matches are shown, one per distinct history.
|
|
255
|
+
|
|
256
|
+
--entity NAME takes the place of QUERY: one history of the entity's
|
|
257
|
+
latest 200 memories and the states they replaced, in time order.
|
|
258
|
+
NAME is its name, an alias or its id (see `htm entities list`);
|
|
259
|
+
--entity-type TYPE picks one when several types share the name.
|
|
260
|
+
--strategy and --limit do not apply.
|
|
261
|
+
|
|
262
|
+
Examples:
|
|
263
|
+
|
|
264
|
+
htm timeline "where does Alice live"
|
|
265
|
+
htm timeline 42
|
|
266
|
+
htm timeline rate limit -s hybrid -l 3 --json
|
|
267
|
+
htm timeline --entity "Alice Chen"
|
|
268
|
+
DESC
|
|
269
|
+
memory_options
|
|
270
|
+
limit_option 5
|
|
271
|
+
option :strategy, aliases: "-s", default: "fulltext", enum: %w[hybrid vector fulltext], desc: "Search strategy for QUERY"
|
|
272
|
+
option :depth, type: :numeric, default: 25, desc: "Links to follow at most"
|
|
273
|
+
option :entity, banner: "NAME", desc: "Show the history of the memories mentioning this entity instead"
|
|
274
|
+
option :entity_type, banner: "TYPE", desc: "The entity's type, when several types share the name"
|
|
275
|
+
def timeline(*words)
|
|
276
|
+
memory_console(:Search) do |console|
|
|
277
|
+
depth = options[:depth].to_i
|
|
278
|
+
next console.entity_timeline(options[:entity], type: options[:entity_type], depth:) if entity_alone?(words)
|
|
279
|
+
|
|
280
|
+
console.timeline(text_from(words), strategy: options[:strategy], limit: options[:limit], depth:)
|
|
281
|
+
end
|
|
282
|
+
end
|
|
283
|
+
|
|
284
|
+
desc "forget [ID...]", "Forget memories by ID, with --matching by text, or with --entity all that mention an entity"
|
|
285
|
+
long_desc <<~DESC, wrap: false
|
|
286
|
+
Soft delete memories: they disappear from searches and `htm restore ID`
|
|
287
|
+
brings them back. --permanent deletes them for good (asks first unless
|
|
288
|
+
--yes). Name the memories by their IDs, with --matching TEXT or with
|
|
289
|
+
--entity NAME: one of the three, never two together.
|
|
290
|
+
|
|
291
|
+
--entity NAME forgets every memory mentioning that entity (its name,
|
|
292
|
+
an alias or its id; --entity-type TYPE picks one when several types
|
|
293
|
+
share the name). It shows how many memories that is and asks first
|
|
294
|
+
unless --yes. It is a soft delete: `htm restore ID` brings each
|
|
295
|
+
memory back, `htm deleted` lists them, and `htm purge` later removes
|
|
296
|
+
them for good. The entity itself stays known, mentioned by nothing.
|
|
297
|
+
`htm entities report NAME` shows beforehand what would go.
|
|
298
|
+
|
|
299
|
+
Examples:
|
|
300
|
+
|
|
301
|
+
htm forget 42 43
|
|
302
|
+
htm forget --matching "test data"
|
|
303
|
+
htm forget 42 --permanent --yes
|
|
304
|
+
htm forget --entity "Alice Chen"
|
|
305
|
+
DESC
|
|
306
|
+
memory_options
|
|
307
|
+
yes_option
|
|
308
|
+
option :matching, banner: "TEXT", desc: "Forget every memory whose content contains TEXT (case-insensitive)"
|
|
309
|
+
option :permanent, type: :boolean, desc: "Delete for good instead of soft deleting"
|
|
310
|
+
option :entity, banner: "NAME", desc: "Forget every memory mentioning this entity (soft delete; asks first)"
|
|
311
|
+
option :entity_type, banner: "TYPE", desc: "The entity's type, when several types share the name"
|
|
312
|
+
def forget(*ids)
|
|
313
|
+
permanent = options[:permanent]
|
|
314
|
+
text = options[:matching]
|
|
315
|
+
memory_console(:Edit) do |console|
|
|
316
|
+
one_selector!(ids)
|
|
317
|
+
next console.forget_entity(soft_entity, type: options[:entity_type]) if options[:entity]
|
|
318
|
+
|
|
319
|
+
text ? console.forget_matching(text, permanent:) : console.forget(node_ids(ids), permanent:)
|
|
320
|
+
end
|
|
321
|
+
end
|
|
322
|
+
|
|
323
|
+
desc "restore ID...", "Restore soft-deleted memories"
|
|
324
|
+
memory_options
|
|
325
|
+
def restore(*ids) = memory_console(:Edit) { it.restore(node_ids(ids)) }
|
|
326
|
+
|
|
327
|
+
desc "deleted", "List soft-deleted memories, newest deletions first"
|
|
328
|
+
memory_options
|
|
329
|
+
full_option
|
|
330
|
+
limit_option 20
|
|
331
|
+
def deleted = memory_console(:Edit) { it.deleted(limit: options[:limit]) }
|
|
332
|
+
|
|
333
|
+
desc "purge", "Permanently delete memories soft-deleted more than --days ago (asks first)"
|
|
334
|
+
memory_options
|
|
335
|
+
yes_option
|
|
336
|
+
option :days, type: :numeric, default: 30, desc: "Purge memories soft-deleted more than this many days ago"
|
|
337
|
+
option :before, banner: "TIME", desc: "Purge memories soft-deleted before this ISO8601 time (overrides --days)"
|
|
338
|
+
def purge
|
|
339
|
+
memory_console(:Edit) do |console|
|
|
340
|
+
console.purge(HTM::Admin::MemoryCommands.cutoff(days: options[:days], before: options[:before]))
|
|
341
|
+
end
|
|
342
|
+
end
|
|
343
|
+
|
|
344
|
+
desc "tag ID TAG...", "Add tags to a memory (a:b:c also adds a and a:b)"
|
|
345
|
+
memory_options
|
|
346
|
+
def tag(id, *tags) = memory_console(:Edit) { it.tag(node_id(id), tags) }
|
|
347
|
+
|
|
348
|
+
desc "untag ID TAG", "Remove one tag from a memory"
|
|
349
|
+
memory_options
|
|
350
|
+
def untag(id, tag) = memory_console(:Edit) { it.untag(node_id(id), tag) }
|
|
351
|
+
|
|
352
|
+
desc "related ID", "Memories linked to one by similar embeddings or shared entities, strongest link first"
|
|
353
|
+
memory_options
|
|
354
|
+
full_option
|
|
355
|
+
limit_option 10
|
|
356
|
+
option :min_weight, type: :numeric, default: 0.0, desc: "Minimum link weight, 0.0-1.0"
|
|
357
|
+
def related(id)
|
|
358
|
+
memory_console(:Search) { it.related(node_id(id), limit: options[:limit], min_weight: options[:min_weight]) }
|
|
359
|
+
end
|
|
360
|
+
|
|
361
|
+
desc "propositions [TEXT...]", "Break text (or stdin) into atomic factual propositions with the LLM"
|
|
362
|
+
memory_options
|
|
363
|
+
option :remember, type: :boolean, desc: "Store each proposition as a memory"
|
|
364
|
+
def propositions(*words)
|
|
365
|
+
memory_console(:Search) do |console|
|
|
366
|
+
text = text_from(words)
|
|
367
|
+
options[:remember] ? console.remember_propositions(text) : console.propositions(text)
|
|
368
|
+
end
|
|
369
|
+
end
|
|
370
|
+
|
|
371
|
+
desc "stats", "Memory statistics: nodes, enrichment coverage, tags, robots, files, relationships, entities"
|
|
372
|
+
memory_options
|
|
373
|
+
def stats = memory_console(:Search, &:stats)
|
|
374
|
+
|
|
375
|
+
private
|
|
376
|
+
|
|
377
|
+
# recall's entity filters, as HTM#recall names them
|
|
378
|
+
def entity_filters
|
|
379
|
+
{ entities: options[:entity], entity_match: options[:entity_match], entity_type: options[:entity_type] }
|
|
380
|
+
end
|
|
381
|
+
|
|
382
|
+
# Whether timeline is asked about --entity
|
|
383
|
+
#
|
|
384
|
+
# @raise [HTM::Admin::Error] when a QUERY comes with it
|
|
385
|
+
def entity_alone?(words)
|
|
386
|
+
return false unless options[:entity]
|
|
387
|
+
return true if words.empty?
|
|
388
|
+
|
|
389
|
+
raise HTM::Admin::Error, "Error: give a QUERY or --entity, not both"
|
|
390
|
+
end
|
|
391
|
+
|
|
392
|
+
# Checks that forget is told which memories one way only
|
|
393
|
+
#
|
|
394
|
+
# @raise [HTM::Admin::Error] for two or three of IDs, --matching and --entity
|
|
395
|
+
def one_selector!(ids)
|
|
396
|
+
return if [ids.any?, options[:matching], options[:entity]].count { it } < 2
|
|
397
|
+
|
|
398
|
+
raise HTM::Admin::Error, "Error: give node IDs, --matching TEXT or --entity NAME, not several of them"
|
|
399
|
+
end
|
|
400
|
+
|
|
401
|
+
# forget --entity's entity; refuses --permanent, which an entity's memories never get in one step
|
|
402
|
+
#
|
|
403
|
+
# @raise [HTM::Admin::Error]
|
|
404
|
+
def soft_entity
|
|
405
|
+
return options[:entity] unless options[:permanent]
|
|
406
|
+
|
|
407
|
+
raise HTM::Admin::Error, "Error: --entity soft deletes only; remove the memories for good later with `htm purge`"
|
|
408
|
+
end
|
|
409
|
+
end
|
|
410
|
+
end
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class HTM
|
|
4
|
+
class CLI < Thor
|
|
5
|
+
# Mixed into the command classes that work on memory (recall, remember,
|
|
6
|
+
# htm wm, htm tags search, ...): the shared --robot/--json/--verbose
|
|
7
|
+
# options and the console that runs each command.
|
|
8
|
+
module MemoryCommand
|
|
9
|
+
def self.included(base) = base.extend(ClassMethods)
|
|
10
|
+
|
|
11
|
+
# Option declarations shared by the memory commands
|
|
12
|
+
module ClassMethods
|
|
13
|
+
# --robot, --json and --verbose for the next command
|
|
14
|
+
def memory_options
|
|
15
|
+
option :robot, aliases: "-r", banner: "NAME", desc: "Robot to act as (default: $HTM_ROBOT_NAME or htm_cli)"
|
|
16
|
+
option :json, type: :boolean, desc: "Print JSON instead of text"
|
|
17
|
+
option :verbose, type: :boolean, desc: "Log HTM's activity to stderr"
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# --full for the next command, which lists memories
|
|
21
|
+
def full_option = option(:full, type: :boolean, desc: "Print whole contents instead of one-line previews")
|
|
22
|
+
|
|
23
|
+
# --yes for the next command, which asks before deleting for good
|
|
24
|
+
def yes_option = option(:yes, aliases: "-y", type: :boolean, desc: "Don't ask for confirmation")
|
|
25
|
+
|
|
26
|
+
# --limit for the next command
|
|
27
|
+
def limit_option(default) = option(:limit, aliases: "-l", type: :numeric, default:, desc: "Maximum results")
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
private
|
|
31
|
+
|
|
32
|
+
# Yield the named HTM::Admin::MemoryCommands console, set up from the
|
|
33
|
+
# command's options and any +settings+ of that console's own. An HTM
|
|
34
|
+
# error, or a bad argument value (an unknown strategy, an unparseable
|
|
35
|
+
# time), ends the command with its message.
|
|
36
|
+
def memory_console(name, **settings)
|
|
37
|
+
htm_task do |admin|
|
|
38
|
+
commands = admin::MemoryCommands
|
|
39
|
+
commands.log_to_stderr!(options[:verbose] ? Logger::DEBUG : Logger::WARN)
|
|
40
|
+
yield commands.const_get(name).new(robot: options[:robot], json: options[:json],
|
|
41
|
+
full: options[:full], assume_yes: options[:yes], **settings)
|
|
42
|
+
rescue ArgumentError, HTM::ValidationError => e
|
|
43
|
+
raise admin::Error, e.message
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Command-line ids as Integers
|
|
48
|
+
#
|
|
49
|
+
# @raise [HTM::Admin::Error] for one that isn't a positive integer
|
|
50
|
+
def node_ids(args)
|
|
51
|
+
raise HTM::Admin::Error, "Error: give at least one node ID" if args.empty?
|
|
52
|
+
|
|
53
|
+
args.map { node_id(it) }
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def node_id(arg)
|
|
57
|
+
id = Integer(arg, exception: false)
|
|
58
|
+
raise HTM::Admin::Error, "Error: not a node ID: #{arg}" unless id&.positive?
|
|
59
|
+
|
|
60
|
+
id
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# The text a command works on: its words, or stdin (see MemoryCommands.read_text)
|
|
64
|
+
def text_from(words) = HTM::Admin::MemoryCommands.read_text(words)
|
|
65
|
+
|
|
66
|
+
def metadata_option = HTM::Admin::MemoryCommands.parse_metadata(options[:metadata])
|
|
67
|
+
|
|
68
|
+
# The node ids an array option (--supersedes, --evidence) names; none when it is absent
|
|
69
|
+
def node_ids_option(name) = Array(options[name]).map { node_id(it) }
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# HTM robot team tasks: htm robots SUBCOMMAND
|
|
4
|
+
#
|
|
5
|
+
# The robot_lab teams that maintain long-term memory (see
|
|
6
|
+
# docs/guides/robot-teams.md). TEAMS are any of tag_curator, consolidator,
|
|
7
|
+
# contradiction_detector, archivist, entity_curator; none means robots.teams
|
|
8
|
+
# from the config.
|
|
9
|
+
#
|
|
10
|
+
# htm robots work # constant attention, until Ctrl-C
|
|
11
|
+
# htm robots once tag_curator # one batch per team, then exit
|
|
12
|
+
# htm robots status # cases waiting per team
|
|
13
|
+
# htm robots list # the robots sharing memory
|
|
14
|
+
|
|
15
|
+
require_relative "group"
|
|
16
|
+
require_relative "memory_command"
|
|
17
|
+
|
|
18
|
+
class HTM
|
|
19
|
+
class CLI < Thor
|
|
20
|
+
# The robot commands (htm robots SUBCOMMAND): list the robots sharing
|
|
21
|
+
# memory, and run and inspect the memory maintenance teams
|
|
22
|
+
class Robots < Thor
|
|
23
|
+
include Group
|
|
24
|
+
include MemoryCommand
|
|
25
|
+
|
|
26
|
+
# Shown as "1000+": status stops counting a team's cases here
|
|
27
|
+
STATUS_LIMIT = 1000
|
|
28
|
+
|
|
29
|
+
# What work, once and status say about their TEAMS arguments
|
|
30
|
+
TEAMS_HELP = <<~TEXT
|
|
31
|
+
TEAMS are any of tag_curator, consolidator, contradiction_detector,
|
|
32
|
+
archivist and entity_curator; none means robots.teams from the
|
|
33
|
+
configuration. See https://madbomber.github.io/htm/guides/robot-teams/
|
|
34
|
+
TEXT
|
|
35
|
+
|
|
36
|
+
desc "work [TEAMS...]", "Keep the robot teams working on memory until interrupted (Ctrl-C)"
|
|
37
|
+
long_desc TEAMS_HELP, wrap: false
|
|
38
|
+
def work(*teams) = htm_robots { work_until_interrupted(it, names(teams)) }
|
|
39
|
+
|
|
40
|
+
desc "once [TEAMS...]", "Give each robot team one batch, then exit"
|
|
41
|
+
long_desc TEAMS_HELP, wrap: false
|
|
42
|
+
def once(*teams) = htm_robots { puts "Handled #{it.supervisor(names(teams)).cycle} cases." }
|
|
43
|
+
|
|
44
|
+
desc "status [TEAMS...]", "Show how many cases wait for each robot team (SQL only, no LLM calls)"
|
|
45
|
+
long_desc TEAMS_HELP, wrap: false
|
|
46
|
+
def status(*teams) = htm_robots { print_status(it.status(names(teams), limit: STATUS_LIMIT)) }
|
|
47
|
+
|
|
48
|
+
desc "list", "List the robots sharing memory (the hive mind), most recently active first"
|
|
49
|
+
memory_options
|
|
50
|
+
limit_option 50
|
|
51
|
+
def list = memory_console(:Search) { it.robots(limit: options[:limit]) }
|
|
52
|
+
|
|
53
|
+
private
|
|
54
|
+
|
|
55
|
+
def names(teams) = teams.empty? ? nil : teams
|
|
56
|
+
|
|
57
|
+
# Connects HTM and yields HTM::Robots; an unknown team name ends the task cleanly
|
|
58
|
+
def htm_robots
|
|
59
|
+
htm_admin.connect!
|
|
60
|
+
yield HTM::Robots
|
|
61
|
+
rescue ArgumentError => e
|
|
62
|
+
abort e.message
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def work_until_interrupted(robots, team_names)
|
|
66
|
+
stopping = false
|
|
67
|
+
on_interrupt { stopping = true }
|
|
68
|
+
puts "Robot teams at work: #{robots.teams(team_names).map(&:slug).join(', ')} (Ctrl-C to stop)"
|
|
69
|
+
sup = robots.run_until(-> { stopping }, names: team_names)
|
|
70
|
+
puts "Stopped after #{sup.cycles} cycles."
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def on_interrupt(&) = %w[INT TERM].each { |sig| trap(sig, &) }
|
|
74
|
+
|
|
75
|
+
def print_status(counts)
|
|
76
|
+
counts.each do |slug, count|
|
|
77
|
+
puts format(" %<slug>-24s %<count>s", slug: slug, count: count >= STATUS_LIMIT ? "#{STATUS_LIMIT}+" : count)
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "thor"
|
|
4
|
+
|
|
5
|
+
class HTM
|
|
6
|
+
class CLI < Thor
|
|
7
|
+
# Helpers shared by the CLI's commands and the repository's own asgard
|
|
8
|
+
# tasks (examples.loki, doc.loki). Requiring this file loads nothing of
|
|
9
|
+
# HTM itself; the first command that needs HTM loads it.
|
|
10
|
+
module Support
|
|
11
|
+
private
|
|
12
|
+
|
|
13
|
+
# Load HTM and its admin commands
|
|
14
|
+
#
|
|
15
|
+
# @return [Module] HTM::Admin
|
|
16
|
+
#
|
|
17
|
+
def htm_admin
|
|
18
|
+
require_relative "../admin"
|
|
19
|
+
HTM::Admin
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# Yield HTM::Admin. An HTM::Admin::Error (a missing file, a failed
|
|
23
|
+
# connection) ends the command with its message and exit status 1
|
|
24
|
+
# instead of a backtrace; a database error also prints a fix-it hint.
|
|
25
|
+
#
|
|
26
|
+
# @return [Object] the block's value
|
|
27
|
+
#
|
|
28
|
+
def htm_task
|
|
29
|
+
yield htm_admin
|
|
30
|
+
rescue StandardError => e
|
|
31
|
+
raise unless reportable?(e)
|
|
32
|
+
|
|
33
|
+
# Not warn: it is silent once HTM sets $VERBOSE to nil
|
|
34
|
+
$stderr.puts e.message # rubocop:disable Style/StderrPuts
|
|
35
|
+
$stderr.puts "", *ErrorSuggestion.for_error(e.message) if database_error?(e) # rubocop:disable Style/StderrPuts
|
|
36
|
+
exit 1
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def reportable?(error) = (defined?(HTM::Error) && error.is_a?(HTM::Error)) || database_error?(error)
|
|
40
|
+
|
|
41
|
+
def database_error?(error) = defined?(Sequel::DatabaseError) && error.is_a?(Sequel::DatabaseError)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
require_relative "error_suggestion"
|