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,196 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'tool'
|
|
4
|
+
require_relative '../admin'
|
|
5
|
+
|
|
6
|
+
class HTM
|
|
7
|
+
module MCP
|
|
8
|
+
# Helpers shared by the file tools
|
|
9
|
+
module FileSupport
|
|
10
|
+
private
|
|
11
|
+
|
|
12
|
+
def sync_state(source) = HTM::Admin::FileCommands.sync_state(source)
|
|
13
|
+
|
|
14
|
+
def find_source(path) = HTM::Admin::FileCommands.find_source(path)
|
|
15
|
+
|
|
16
|
+
def find_source!(path) = find_source(path) || raise(HTM::NotFoundError, "No file loaded from #{path}")
|
|
17
|
+
|
|
18
|
+
def source_summary(source)
|
|
19
|
+
{
|
|
20
|
+
id: source.id,
|
|
21
|
+
file_path: source.file_path,
|
|
22
|
+
file_size: source.file_size,
|
|
23
|
+
chunk_count: source.chunks_dataset.count,
|
|
24
|
+
sync_state: sync_state(source).to_s,
|
|
25
|
+
last_synced_at: source.last_synced_at&.iso8601
|
|
26
|
+
}
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# A file's chunk nodes in file order, as truncated node summaries with their position
|
|
30
|
+
def chunk_entries(source)
|
|
31
|
+
chunks = source.chunks_dataset.eager(:tags).order(:chunk_position).all
|
|
32
|
+
chunks.zip(node_summaries(chunks, content_limit: 200)).map { |chunk, summary| { **summary, position: chunk.chunk_position } }
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def load_result(result)
|
|
36
|
+
result.slice(:file_path, :file_source_id, :chunks_created, :chunks_updated,
|
|
37
|
+
:chunks_deleted, :skipped, :error).compact
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Tool: Load a markdown file into memory
|
|
42
|
+
class LoadFileTool < Tool
|
|
43
|
+
include FileSupport
|
|
44
|
+
|
|
45
|
+
description "Load a markdown file into long-term memory: chunk it, store each chunk as a memory " \
|
|
46
|
+
"and track the file for re-sync. Unchanged files are skipped unless force is true."
|
|
47
|
+
|
|
48
|
+
arguments do
|
|
49
|
+
required(:path).filled(:string).description("Path to the markdown file (relative to the server's working directory or absolute)")
|
|
50
|
+
optional(:force).filled(:bool).description("Reload even if the file has not changed (default: false)")
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def call(path:, force: false)
|
|
54
|
+
log_info "LoadFileTool called: path=#{path.inspect}, force=#{force}"
|
|
55
|
+
|
|
56
|
+
guarded { success(**load_result(session_htm.load_file(path, force:))) }
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# Tool: Load a directory of markdown files into memory
|
|
61
|
+
class LoadDirectoryTool < Tool
|
|
62
|
+
include FileSupport
|
|
63
|
+
|
|
64
|
+
description "Load every matching markdown file in a directory (recursively) into long-term memory"
|
|
65
|
+
|
|
66
|
+
arguments do
|
|
67
|
+
required(:path).filled(:string).description("Directory path")
|
|
68
|
+
optional(:pattern).filled(:string).description("Glob pattern for the files (default: '**/*.md')")
|
|
69
|
+
optional(:force).filled(:bool).description("Reload files even if unchanged (default: false)")
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def call(path:, pattern: '**/*.md', force: false)
|
|
73
|
+
log_info "LoadDirectoryTool called: path=#{path.inspect}, pattern=#{pattern.inspect}, force=#{force}"
|
|
74
|
+
|
|
75
|
+
guarded do
|
|
76
|
+
results = session_htm.load_directory(path, pattern:, force:).map { load_result(it) }
|
|
77
|
+
failed, loaded = results.partition { it[:error] }
|
|
78
|
+
|
|
79
|
+
success(path:, pattern:, totals: HTM::Admin::FileCommands.load_totals(loaded),
|
|
80
|
+
errors: failed.size, files: results)
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# Tool: List loaded files
|
|
86
|
+
class ListFilesTool < Tool
|
|
87
|
+
include FileSupport
|
|
88
|
+
|
|
89
|
+
read_only!
|
|
90
|
+
description "List the files loaded into memory with their chunk counts and sync state " \
|
|
91
|
+
"('synced', 'needs_sync' or 'missing')"
|
|
92
|
+
|
|
93
|
+
arguments do
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def call
|
|
97
|
+
log_info "ListFilesTool called"
|
|
98
|
+
|
|
99
|
+
guarded do
|
|
100
|
+
files = HTM::Models::FileSource.order(:file_path).all.map { source_summary(it) }
|
|
101
|
+
|
|
102
|
+
success(count: files.length, files:)
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# Tool: Details of one loaded file
|
|
108
|
+
class FileInfoTool < Tool
|
|
109
|
+
include FileSupport
|
|
110
|
+
|
|
111
|
+
read_only!
|
|
112
|
+
description "Show a loaded file's details: frontmatter, sync state and (optionally) its chunks"
|
|
113
|
+
|
|
114
|
+
arguments do
|
|
115
|
+
required(:path).filled(:string).description("Path the file was loaded from")
|
|
116
|
+
optional(:include_chunks).filled(:bool).description("Include the chunk nodes (default: true)")
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def call(path:, include_chunks: true)
|
|
120
|
+
log_info "FileInfoTool called: path=#{path.inspect}"
|
|
121
|
+
|
|
122
|
+
guarded do
|
|
123
|
+
source = find_source!(path)
|
|
124
|
+
chunks = include_chunks ? chunk_entries(source) : nil
|
|
125
|
+
|
|
126
|
+
success(file: { **source_summary(source), frontmatter: source.frontmatter_data,
|
|
127
|
+
frontmatter_tags: source.frontmatter_tags }, chunks:)
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
# Tool: Unload a file from memory
|
|
133
|
+
class UnloadFileTool < Tool
|
|
134
|
+
include FileSupport
|
|
135
|
+
|
|
136
|
+
destructive!
|
|
137
|
+
description "Unload a file: soft-delete its chunks and forget the file source"
|
|
138
|
+
|
|
139
|
+
arguments do
|
|
140
|
+
required(:path).filled(:string).description("Path the file was loaded from")
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
def call(path:)
|
|
144
|
+
log_info "UnloadFileTool called: path=#{path.inspect}"
|
|
145
|
+
|
|
146
|
+
guarded do
|
|
147
|
+
file_path = find_source!(path).file_path
|
|
148
|
+
|
|
149
|
+
success(file_path:, chunks_deleted: session_htm.unload_file(file_path))
|
|
150
|
+
end
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
# Tool: Reload changed files
|
|
155
|
+
class SyncFilesTool < Tool
|
|
156
|
+
include FileSupport
|
|
157
|
+
|
|
158
|
+
description "Reload every loaded file that changed on disk; reports synced, unchanged and missing files"
|
|
159
|
+
|
|
160
|
+
arguments do
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
def call
|
|
164
|
+
log_info "SyncFilesTool called"
|
|
165
|
+
|
|
166
|
+
guarded do
|
|
167
|
+
report = { synced: [], skipped: [], missing: [] }
|
|
168
|
+
HTM::Models::FileSource.order(:file_path).all.each { sync(it, report) }
|
|
169
|
+
|
|
170
|
+
success(**report.transform_values(&:length), files: report)
|
|
171
|
+
end
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
private
|
|
175
|
+
|
|
176
|
+
def sync(source, report)
|
|
177
|
+
path = source.file_path
|
|
178
|
+
case sync_state(source)
|
|
179
|
+
when :missing then report[:missing] << path
|
|
180
|
+
when :synced then report[:skipped] << path
|
|
181
|
+
else report[:synced] << load_result(session_htm.load_file(path))
|
|
182
|
+
end
|
|
183
|
+
end
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
# File loading tools for registration
|
|
187
|
+
FILE_TOOLS = [
|
|
188
|
+
LoadFileTool,
|
|
189
|
+
LoadDirectoryTool,
|
|
190
|
+
ListFilesTool,
|
|
191
|
+
FileInfoTool,
|
|
192
|
+
UnloadFileTool,
|
|
193
|
+
SyncFilesTool
|
|
194
|
+
].freeze
|
|
195
|
+
end
|
|
196
|
+
end
|
|
@@ -0,0 +1,466 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'tool'
|
|
4
|
+
|
|
5
|
+
class HTM
|
|
6
|
+
module MCP
|
|
7
|
+
# Session state for robot groups
|
|
8
|
+
module GroupSession
|
|
9
|
+
class << self
|
|
10
|
+
def groups
|
|
11
|
+
@groups ||= {}
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def get_group(name)
|
|
15
|
+
groups[name]
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def set_group(name, group)
|
|
19
|
+
groups[name] = group
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def remove_group(name)
|
|
23
|
+
group = groups.delete(name)
|
|
24
|
+
group&.shutdown
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def group_names
|
|
28
|
+
groups.keys
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# Tool: Create a new robot group
|
|
34
|
+
class CreateGroupTool < Tool
|
|
35
|
+
description "Create a new robot group for coordinating multiple robots with shared working memory"
|
|
36
|
+
|
|
37
|
+
arguments do
|
|
38
|
+
required(:name).filled(:string).description("Unique name for the robot group")
|
|
39
|
+
optional(:max_tokens).filled(:integer).description("Maximum token budget for shared working memory (default: 4000)")
|
|
40
|
+
optional(:join_as).filled(:string).description("Role to join as: 'active' or 'passive' (default: 'active')")
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def call(name:, max_tokens: 4000, join_as: 'active')
|
|
44
|
+
log_info "CreateGroupTool called: name=#{name.inspect}, max_tokens=#{max_tokens}"
|
|
45
|
+
|
|
46
|
+
return failure("Group '#{name}' already exists in this session") if find_group(name)
|
|
47
|
+
|
|
48
|
+
# Create the group with the current robot as initial member
|
|
49
|
+
robot_name = Session.robot_name
|
|
50
|
+
group = HTM::RobotGroup.new(name:, **initial_members(robot_name, join_as), max_tokens:)
|
|
51
|
+
GroupSession.set_group(name, group)
|
|
52
|
+
|
|
53
|
+
log_info "Group created: #{name}, robot=#{robot_name} joined as #{join_as}"
|
|
54
|
+
|
|
55
|
+
{
|
|
56
|
+
success: true,
|
|
57
|
+
group_name: name,
|
|
58
|
+
max_tokens:,
|
|
59
|
+
robot_name:,
|
|
60
|
+
role: join_as,
|
|
61
|
+
message: "Group '#{name}' created. Robot '#{robot_name}' joined as #{join_as}."
|
|
62
|
+
}.to_json
|
|
63
|
+
rescue StandardError => e
|
|
64
|
+
log_error "CreateGroupTool error: #{e.message}"
|
|
65
|
+
failure(e.message)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
private
|
|
69
|
+
|
|
70
|
+
# Active and passive member lists with the robot under its role;
|
|
71
|
+
# an unknown role leaves both lists empty
|
|
72
|
+
def initial_members(robot_name, role)
|
|
73
|
+
members = { active: [], passive: [] }
|
|
74
|
+
key = role.to_sym
|
|
75
|
+
members[key] = [robot_name] if members.key?(key)
|
|
76
|
+
members
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# Tool: List all robot groups
|
|
81
|
+
class ListGroupsTool < Tool
|
|
82
|
+
read_only!
|
|
83
|
+
description "List all robot groups in this session with their status"
|
|
84
|
+
|
|
85
|
+
arguments do
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def call
|
|
89
|
+
log_info "ListGroupsTool called"
|
|
90
|
+
|
|
91
|
+
groups = GroupSession.group_names.map { group_entry(it) }
|
|
92
|
+
count = groups.length
|
|
93
|
+
|
|
94
|
+
log_info "ListGroupsTool complete: #{count} groups"
|
|
95
|
+
|
|
96
|
+
{ success: true, count:, groups: }.to_json
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
private
|
|
100
|
+
|
|
101
|
+
# Summary of the named group's status
|
|
102
|
+
def group_entry(name)
|
|
103
|
+
status = GroupSession.get_group(name).status
|
|
104
|
+
{
|
|
105
|
+
name:,
|
|
106
|
+
active_robots: status[:active],
|
|
107
|
+
passive_robots: status[:passive],
|
|
108
|
+
**status.slice(:total_members, :in_sync, :token_utilization)
|
|
109
|
+
}
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
# Tool: Get detailed group status
|
|
114
|
+
class GetGroupStatusTool < Tool
|
|
115
|
+
read_only!
|
|
116
|
+
description "Get detailed status of a specific robot group"
|
|
117
|
+
|
|
118
|
+
arguments do
|
|
119
|
+
required(:name).filled(:string).description("Name of the robot group")
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def call(name:)
|
|
123
|
+
log_info "GetGroupStatusTool called: name=#{name.inspect}"
|
|
124
|
+
|
|
125
|
+
with_group(name) do |group|
|
|
126
|
+
status = group.status
|
|
127
|
+
sync_stats = group.sync_stats
|
|
128
|
+
|
|
129
|
+
log_info "GetGroupStatusTool complete: #{name}"
|
|
130
|
+
|
|
131
|
+
{ success: true, group_name: name, status:, sync_stats: }.to_json
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
# Tool: Join current robot to an existing group
|
|
137
|
+
class JoinGroupTool < Tool
|
|
138
|
+
description "Join the current robot to an existing robot group"
|
|
139
|
+
|
|
140
|
+
arguments do
|
|
141
|
+
required(:name).filled(:string).description("Name of the robot group to join")
|
|
142
|
+
optional(:role).filled(:string).description("Role to join as: 'active' or 'passive' (default: 'active')")
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
def call(name:, role: 'active')
|
|
146
|
+
log_info "JoinGroupTool called: name=#{name.inspect}, role=#{role}"
|
|
147
|
+
|
|
148
|
+
with_group(name) do |group|
|
|
149
|
+
robot_name = Session.robot_name
|
|
150
|
+
next failure("Robot '#{robot_name}' is already a member of group '#{name}'") if group.member?(robot_name)
|
|
151
|
+
|
|
152
|
+
robot_id = role == 'passive' ? group.add_passive(robot_name) : group.add_active(robot_name)
|
|
153
|
+
|
|
154
|
+
log_info "Robot #{robot_name} joined group #{name} as #{role}"
|
|
155
|
+
|
|
156
|
+
{
|
|
157
|
+
success: true,
|
|
158
|
+
group_name: name,
|
|
159
|
+
robot_name:,
|
|
160
|
+
robot_id:,
|
|
161
|
+
role:,
|
|
162
|
+
message: "Robot '#{robot_name}' joined group '#{name}' as #{role}"
|
|
163
|
+
}.to_json
|
|
164
|
+
end
|
|
165
|
+
rescue ArgumentError => e
|
|
166
|
+
failure(e.message)
|
|
167
|
+
end
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
# Tool: Leave a robot group
|
|
171
|
+
class LeaveGroupTool < Tool
|
|
172
|
+
description "Remove the current robot from a robot group"
|
|
173
|
+
|
|
174
|
+
arguments do
|
|
175
|
+
required(:name).filled(:string).description("Name of the robot group to leave")
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
def call(name:)
|
|
179
|
+
log_info "LeaveGroupTool called: name=#{name.inspect}"
|
|
180
|
+
|
|
181
|
+
with_group(name) do |group|
|
|
182
|
+
robot_name = Session.robot_name
|
|
183
|
+
next failure("Robot '#{robot_name}' is not a member of group '#{name}'") unless group.member?(robot_name)
|
|
184
|
+
|
|
185
|
+
group.remove(robot_name)
|
|
186
|
+
|
|
187
|
+
log_info "Robot #{robot_name} left group #{name}"
|
|
188
|
+
|
|
189
|
+
{
|
|
190
|
+
success: true,
|
|
191
|
+
group_name: name,
|
|
192
|
+
robot_name:,
|
|
193
|
+
message: "Robot '#{robot_name}' left group '#{name}'"
|
|
194
|
+
}.to_json
|
|
195
|
+
end
|
|
196
|
+
end
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
# Tool: Remember via group (syncs to all members)
|
|
200
|
+
class GroupRememberTool < Tool
|
|
201
|
+
description "Store information in a robot group's shared working memory (syncs to all members)"
|
|
202
|
+
|
|
203
|
+
arguments do
|
|
204
|
+
required(:group_name).filled(:string).description("Name of the robot group")
|
|
205
|
+
required(:content).filled(:string).description("The content to remember")
|
|
206
|
+
optional(:tags).array(:string).description("Optional tags for categorization")
|
|
207
|
+
optional(:metadata).hash.description("Optional metadata key-value pairs")
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
def call(group_name:, content:, tags: [], metadata: {})
|
|
211
|
+
log_info "GroupRememberTool called: group=#{group_name.inspect}, content=#{content[0..50].inspect}..."
|
|
212
|
+
|
|
213
|
+
with_group(group_name) do |group|
|
|
214
|
+
robot_name = Session.robot_name
|
|
215
|
+
node_id = group.remember(content, originator: robot_name, tags:, metadata:)
|
|
216
|
+
|
|
217
|
+
log_info "Group memory stored: node_id=#{node_id}, group=#{group_name}"
|
|
218
|
+
|
|
219
|
+
{
|
|
220
|
+
success: true,
|
|
221
|
+
node_id:,
|
|
222
|
+
group_name:,
|
|
223
|
+
originator: robot_name,
|
|
224
|
+
message: "Memory stored and synced to all group members"
|
|
225
|
+
}.to_json
|
|
226
|
+
end
|
|
227
|
+
rescue StandardError => e
|
|
228
|
+
log_error "GroupRememberTool error: #{e.message}"
|
|
229
|
+
failure(e.message)
|
|
230
|
+
end
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
# Tool: Recall from group's shared memory
|
|
234
|
+
class GroupRecallTool < Tool
|
|
235
|
+
read_only!
|
|
236
|
+
description "Search memories through a robot group's primary robot. Accepts the same " \
|
|
237
|
+
"timeframe, metadata, query_tags and with_relevance options as recall."
|
|
238
|
+
|
|
239
|
+
arguments do
|
|
240
|
+
required(:group_name).filled(:string).description("Name of the robot group")
|
|
241
|
+
required(:query).filled(:string).description("Search query")
|
|
242
|
+
optional(:limit).filled(:integer).description("Maximum number of results (default: 10)")
|
|
243
|
+
optional(:strategy).filled(:string).description("Search strategy: 'vector', 'fulltext', or 'hybrid' (default: 'hybrid')")
|
|
244
|
+
optional(:timeframe).filled(:string).description(
|
|
245
|
+
"Time filter: 'auto' (extract from the query), natural language ('last week'), or ISO8601 'start..end'"
|
|
246
|
+
)
|
|
247
|
+
optional(:metadata).hash.description("Only memories whose metadata contains these key-value pairs")
|
|
248
|
+
optional(:query_tags).array(:string).description("Tags to boost relevance scoring (with_relevance)")
|
|
249
|
+
optional(:with_relevance).filled(:bool).description("Rank by the composite relevance score (default: false)")
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
def call(group_name:, query:, limit: 10, strategy: 'hybrid', **)
|
|
253
|
+
log_info "GroupRecallTool called: group=#{group_name.inspect}, query=#{query.inspect}"
|
|
254
|
+
|
|
255
|
+
with_group(group_name) do |group|
|
|
256
|
+
guarded do
|
|
257
|
+
results = recall_results(group, query, "GroupRecallTool", limit:, strategy:, **)
|
|
258
|
+
|
|
259
|
+
success(group_name:, query:, strategy:, count: results.length, results:)
|
|
260
|
+
end
|
|
261
|
+
end
|
|
262
|
+
end
|
|
263
|
+
end
|
|
264
|
+
|
|
265
|
+
# Tool: Promote a passive robot to active
|
|
266
|
+
class PromoteRobotTool < Tool
|
|
267
|
+
description "Promote a passive robot to active status in a group"
|
|
268
|
+
|
|
269
|
+
arguments do
|
|
270
|
+
required(:group_name).filled(:string).description("Name of the robot group")
|
|
271
|
+
required(:robot_name).filled(:string).description("Name of the passive robot to promote")
|
|
272
|
+
end
|
|
273
|
+
|
|
274
|
+
def call(group_name:, robot_name:)
|
|
275
|
+
log_info "PromoteRobotTool called: group=#{group_name.inspect}, robot=#{robot_name.inspect}"
|
|
276
|
+
|
|
277
|
+
with_group(group_name) do |group|
|
|
278
|
+
group.promote(robot_name)
|
|
279
|
+
|
|
280
|
+
log_info "Robot #{robot_name} promoted to active in group #{group_name}"
|
|
281
|
+
|
|
282
|
+
{
|
|
283
|
+
success: true,
|
|
284
|
+
group_name:,
|
|
285
|
+
robot_name:,
|
|
286
|
+
message: "Robot '#{robot_name}' promoted to active"
|
|
287
|
+
}.to_json
|
|
288
|
+
end
|
|
289
|
+
rescue ArgumentError => e
|
|
290
|
+
failure(e.message)
|
|
291
|
+
end
|
|
292
|
+
end
|
|
293
|
+
|
|
294
|
+
# Tool: Trigger automatic failover
|
|
295
|
+
class FailoverTool < Tool
|
|
296
|
+
description "Trigger automatic failover - promotes first passive robot to active"
|
|
297
|
+
|
|
298
|
+
arguments do
|
|
299
|
+
required(:group_name).filled(:string).description("Name of the robot group")
|
|
300
|
+
end
|
|
301
|
+
|
|
302
|
+
def call(group_name:)
|
|
303
|
+
log_info "FailoverTool called: group=#{group_name.inspect}"
|
|
304
|
+
|
|
305
|
+
with_group(group_name) do |group|
|
|
306
|
+
promoted = group.failover!
|
|
307
|
+
|
|
308
|
+
log_info "Failover complete: #{promoted} promoted to active in group #{group_name}"
|
|
309
|
+
|
|
310
|
+
{
|
|
311
|
+
success: true,
|
|
312
|
+
group_name:,
|
|
313
|
+
promoted_robot: promoted,
|
|
314
|
+
message: "Failover complete. Robot '#{promoted}' is now active."
|
|
315
|
+
}.to_json
|
|
316
|
+
end
|
|
317
|
+
rescue RuntimeError => e
|
|
318
|
+
failure(e.message)
|
|
319
|
+
end
|
|
320
|
+
end
|
|
321
|
+
|
|
322
|
+
# Tool: Force sync all group members
|
|
323
|
+
class SyncGroupTool < Tool
|
|
324
|
+
description "Force synchronization of all group members' working memory"
|
|
325
|
+
|
|
326
|
+
arguments do
|
|
327
|
+
required(:group_name).filled(:string).description("Name of the robot group")
|
|
328
|
+
end
|
|
329
|
+
|
|
330
|
+
def call(group_name:)
|
|
331
|
+
log_info "SyncGroupTool called: group=#{group_name.inspect}"
|
|
332
|
+
|
|
333
|
+
with_group(group_name) do |group|
|
|
334
|
+
result = group.sync_all
|
|
335
|
+
synced_nodes = result[:synced_nodes]
|
|
336
|
+
members_updated = result[:members_updated]
|
|
337
|
+
summary = "#{synced_nodes} nodes to #{members_updated} members"
|
|
338
|
+
|
|
339
|
+
log_info "SyncGroupTool complete: synced #{summary}"
|
|
340
|
+
|
|
341
|
+
{
|
|
342
|
+
success: true,
|
|
343
|
+
group_name:,
|
|
344
|
+
synced_nodes:,
|
|
345
|
+
members_updated:,
|
|
346
|
+
in_sync: group.in_sync?,
|
|
347
|
+
message: "Synced #{summary}"
|
|
348
|
+
}.to_json
|
|
349
|
+
end
|
|
350
|
+
end
|
|
351
|
+
end
|
|
352
|
+
|
|
353
|
+
# Tool: Get group's shared working memory contents
|
|
354
|
+
class GetGroupWorkingMemoryTool < Tool
|
|
355
|
+
read_only!
|
|
356
|
+
description "Get all nodes in a group's shared working memory"
|
|
357
|
+
|
|
358
|
+
arguments do
|
|
359
|
+
required(:group_name).filled(:string).description("Name of the robot group")
|
|
360
|
+
end
|
|
361
|
+
|
|
362
|
+
def call(group_name:)
|
|
363
|
+
log_info "GetGroupWorkingMemoryTool called: group=#{group_name.inspect}"
|
|
364
|
+
|
|
365
|
+
with_group(group_name) do |group|
|
|
366
|
+
contents = node_summaries(group.working_memory_contents.eager(:tags).all)
|
|
367
|
+
count = contents.length
|
|
368
|
+
status = group.status
|
|
369
|
+
|
|
370
|
+
log_info "GetGroupWorkingMemoryTool complete: #{count} nodes"
|
|
371
|
+
|
|
372
|
+
{
|
|
373
|
+
success: true,
|
|
374
|
+
group_name:,
|
|
375
|
+
count:,
|
|
376
|
+
token_count: status[:working_memory_tokens],
|
|
377
|
+
token_utilization: status[:token_utilization],
|
|
378
|
+
contents:
|
|
379
|
+
}.to_json
|
|
380
|
+
end
|
|
381
|
+
end
|
|
382
|
+
end
|
|
383
|
+
|
|
384
|
+
# Tool: Clear a group's shared working memory
|
|
385
|
+
class ClearGroupWorkingMemoryTool < Tool
|
|
386
|
+
description "Clear a robot group's shared working memory for every member. The memories stay in long-term memory."
|
|
387
|
+
|
|
388
|
+
arguments do
|
|
389
|
+
required(:group_name).filled(:string).description("Name of the robot group")
|
|
390
|
+
end
|
|
391
|
+
|
|
392
|
+
def call(group_name:)
|
|
393
|
+
log_info "ClearGroupWorkingMemoryTool called: group=#{group_name.inspect}"
|
|
394
|
+
|
|
395
|
+
with_group(group_name) do |group|
|
|
396
|
+
guarded { success(group_name:, cleared: group.clear_working_memory) }
|
|
397
|
+
end
|
|
398
|
+
end
|
|
399
|
+
end
|
|
400
|
+
|
|
401
|
+
# Tool: Move working memory between group members
|
|
402
|
+
class TransferWorkingMemoryTool < Tool
|
|
403
|
+
description "Transfer working memory from one group member to another (e.g., hand off context " \
|
|
404
|
+
"before a robot leaves). Clears the source by default."
|
|
405
|
+
|
|
406
|
+
arguments do
|
|
407
|
+
required(:group_name).filled(:string).description("Name of the robot group")
|
|
408
|
+
required(:from_robot).filled(:string).description("Member whose working memory is transferred")
|
|
409
|
+
required(:to_robot).filled(:string).description("Member that receives it")
|
|
410
|
+
optional(:clear_source).filled(:bool).description("Clear the source robot's working memory afterwards (default: true)")
|
|
411
|
+
end
|
|
412
|
+
|
|
413
|
+
def call(group_name:, from_robot:, to_robot:, clear_source: true)
|
|
414
|
+
log_info "TransferWorkingMemoryTool called: group=#{group_name.inspect}, #{from_robot} -> #{to_robot}"
|
|
415
|
+
|
|
416
|
+
with_group(group_name) do |group|
|
|
417
|
+
guarded do
|
|
418
|
+
transferred = group.transfer_working_memory(from_robot, to_robot, clear_source:)
|
|
419
|
+
|
|
420
|
+
success(group_name:, from_robot:, to_robot:, clear_source:, transferred:)
|
|
421
|
+
end
|
|
422
|
+
end
|
|
423
|
+
end
|
|
424
|
+
end
|
|
425
|
+
|
|
426
|
+
# Tool: Shutdown a robot group
|
|
427
|
+
class ShutdownGroupTool < Tool
|
|
428
|
+
destructive!
|
|
429
|
+
description "Shutdown a robot group and release its resources"
|
|
430
|
+
|
|
431
|
+
arguments do
|
|
432
|
+
required(:name).filled(:string).description("Name of the robot group to shutdown")
|
|
433
|
+
end
|
|
434
|
+
|
|
435
|
+
def call(name:)
|
|
436
|
+
log_info "ShutdownGroupTool called: name=#{name.inspect}"
|
|
437
|
+
|
|
438
|
+
with_group(name) do
|
|
439
|
+
GroupSession.remove_group(name)
|
|
440
|
+
|
|
441
|
+
log_info "Group #{name} shutdown complete"
|
|
442
|
+
|
|
443
|
+
{ success: true, group_name: name, message: "Group '#{name}' has been shutdown" }.to_json
|
|
444
|
+
end
|
|
445
|
+
end
|
|
446
|
+
end
|
|
447
|
+
|
|
448
|
+
# All group tools for registration
|
|
449
|
+
GROUP_TOOLS = [
|
|
450
|
+
CreateGroupTool,
|
|
451
|
+
ListGroupsTool,
|
|
452
|
+
GetGroupStatusTool,
|
|
453
|
+
JoinGroupTool,
|
|
454
|
+
LeaveGroupTool,
|
|
455
|
+
GroupRememberTool,
|
|
456
|
+
GroupRecallTool,
|
|
457
|
+
GetGroupWorkingMemoryTool,
|
|
458
|
+
PromoteRobotTool,
|
|
459
|
+
FailoverTool,
|
|
460
|
+
SyncGroupTool,
|
|
461
|
+
ClearGroupWorkingMemoryTool,
|
|
462
|
+
TransferWorkingMemoryTool,
|
|
463
|
+
ShutdownGroupTool
|
|
464
|
+
].freeze
|
|
465
|
+
end
|
|
466
|
+
end
|