htm 0.0.20 → 0.0.30

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.
Files changed (154) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +60 -0
  3. data/Rakefile +104 -18
  4. data/db/migrate/00001_enable_extensions.rb +9 -5
  5. data/db/migrate/00002_create_robots.rb +18 -6
  6. data/db/migrate/00003_create_file_sources.rb +30 -17
  7. data/db/migrate/00004_create_nodes.rb +60 -48
  8. data/db/migrate/00005_create_tags.rb +24 -12
  9. data/db/migrate/00006_create_node_tags.rb +28 -13
  10. data/db/migrate/00007_create_robot_nodes.rb +40 -26
  11. data/db/schema.sql +17 -1
  12. data/db/seeds.rb +33 -33
  13. data/docs/database/naming-convention.md +244 -0
  14. data/docs/database_rake_tasks.md +31 -0
  15. data/docs/development/rake-tasks.md +80 -35
  16. data/docs/guides/mcp-server.md +70 -1
  17. data/examples/.envrc +6 -0
  18. data/examples/.gitignore +2 -0
  19. data/examples/00_create_examples_db.rb +94 -0
  20. data/examples/{basic_usage.rb → 01_basic_usage.rb} +12 -16
  21. data/examples/{custom_llm_configuration.rb → 03_custom_llm_configuration.rb} +13 -3
  22. data/examples/{file_loader_usage.rb → 04_file_loader_usage.rb} +11 -14
  23. data/examples/{timeframe_demo.rb → 05_timeframe_demo.rb} +10 -3
  24. data/examples/{example_app → 06_example_app}/app.rb +15 -15
  25. data/examples/{cli_app → 07_cli_app}/htm_cli.rb +15 -22
  26. data/examples/08_sinatra_app/Gemfile.lock +241 -0
  27. data/examples/{sinatra_app → 08_sinatra_app}/app.rb +19 -18
  28. data/examples/{mcp_client.rb → 09_mcp_client.rb} +5 -8
  29. data/examples/{telemetry → 10_telemetry}/SETUP_README.md +1 -1
  30. data/examples/{telemetry → 10_telemetry}/demo.rb +14 -10
  31. data/examples/11_robot_groups/README.md +335 -0
  32. data/examples/{robot_groups → 11_robot_groups/lib}/robot_worker.rb +17 -3
  33. data/examples/{robot_groups → 11_robot_groups}/multi_process.rb +9 -9
  34. data/examples/{robot_groups → 11_robot_groups}/same_process.rb +9 -12
  35. data/examples/{rails_app → 12_rails_app}/Gemfile +3 -0
  36. data/examples/{rails_app → 12_rails_app}/Gemfile.lock +87 -58
  37. data/examples/{rails_app → 12_rails_app}/app/controllers/dashboard_controller.rb +10 -6
  38. data/examples/{rails_app → 12_rails_app}/app/controllers/files_controller.rb +5 -5
  39. data/examples/{rails_app → 12_rails_app}/app/controllers/memories_controller.rb +11 -7
  40. data/examples/{rails_app → 12_rails_app}/app/controllers/robots_controller.rb +8 -8
  41. data/examples/12_rails_app/app/controllers/tags_controller.rb +36 -0
  42. data/examples/{rails_app → 12_rails_app}/app/views/dashboard/index.html.erb +2 -2
  43. data/examples/{rails_app → 12_rails_app}/app/views/files/new.html.erb +5 -2
  44. data/examples/{rails_app → 12_rails_app}/app/views/memories/_memory_card.html.erb +3 -3
  45. data/examples/{rails_app → 12_rails_app}/app/views/memories/deleted.html.erb +3 -3
  46. data/examples/{rails_app → 12_rails_app}/app/views/memories/edit.html.erb +3 -3
  47. data/examples/{rails_app → 12_rails_app}/app/views/memories/show.html.erb +4 -4
  48. data/examples/{rails_app → 12_rails_app}/app/views/robots/index.html.erb +2 -2
  49. data/examples/{rails_app → 12_rails_app}/app/views/robots/show.html.erb +4 -4
  50. data/examples/{rails_app → 12_rails_app}/app/views/search/index.html.erb +1 -1
  51. data/examples/{rails_app → 12_rails_app}/app/views/tags/index.html.erb +2 -2
  52. data/examples/{rails_app → 12_rails_app}/app/views/tags/show.html.erb +1 -1
  53. data/examples/12_rails_app/config/initializers/htm.rb +7 -0
  54. data/examples/12_rails_app/config/initializers/rack.rb +5 -0
  55. data/examples/README.md +230 -211
  56. data/examples/examples_helper.rb +138 -0
  57. data/lib/htm/config/builder.rb +167 -0
  58. data/lib/htm/config/database.rb +317 -0
  59. data/lib/htm/config/defaults.yml +37 -9
  60. data/lib/htm/config/section.rb +74 -0
  61. data/lib/htm/config/validator.rb +83 -0
  62. data/lib/htm/config.rb +64 -360
  63. data/lib/htm/database.rb +85 -127
  64. data/lib/htm/errors.rb +14 -0
  65. data/lib/htm/integrations/sinatra.rb +13 -44
  66. data/lib/htm/jobs/generate_embedding_job.rb +3 -4
  67. data/lib/htm/jobs/generate_propositions_job.rb +4 -5
  68. data/lib/htm/jobs/generate_tags_job.rb +16 -15
  69. data/lib/htm/loaders/defaults_loader.rb +23 -0
  70. data/lib/htm/loaders/markdown_loader.rb +17 -15
  71. data/lib/htm/loaders/xdg_config_loader.rb +9 -9
  72. data/lib/htm/long_term_memory/fulltext_search.rb +14 -14
  73. data/lib/htm/long_term_memory/hybrid_search.rb +396 -229
  74. data/lib/htm/long_term_memory/node_operations.rb +24 -23
  75. data/lib/htm/long_term_memory/relevance_scorer.rb +23 -20
  76. data/lib/htm/long_term_memory/robot_operations.rb +4 -4
  77. data/lib/htm/long_term_memory/tag_operations.rb +91 -77
  78. data/lib/htm/long_term_memory/vector_search.rb +4 -5
  79. data/lib/htm/long_term_memory.rb +13 -13
  80. data/lib/htm/mcp/cli.rb +115 -8
  81. data/lib/htm/mcp/resources.rb +4 -3
  82. data/lib/htm/mcp/server.rb +5 -4
  83. data/lib/htm/mcp/tools.rb +37 -28
  84. data/lib/htm/migration.rb +72 -0
  85. data/lib/htm/models/file_source.rb +52 -31
  86. data/lib/htm/models/node.rb +224 -108
  87. data/lib/htm/models/node_tag.rb +49 -28
  88. data/lib/htm/models/robot.rb +38 -27
  89. data/lib/htm/models/robot_node.rb +63 -35
  90. data/lib/htm/models/tag.rb +126 -123
  91. data/lib/htm/observability.rb +45 -41
  92. data/lib/htm/proposition_service.rb +76 -7
  93. data/lib/htm/railtie.rb +2 -2
  94. data/lib/htm/robot_group.rb +30 -18
  95. data/lib/htm/sequel_config.rb +215 -0
  96. data/lib/htm/sql_builder.rb +14 -16
  97. data/lib/htm/tag_service.rb +78 -0
  98. data/lib/htm/tasks.rb +3 -0
  99. data/lib/htm/version.rb +1 -1
  100. data/lib/htm/workflows/remember_workflow.rb +6 -5
  101. data/lib/htm.rb +26 -22
  102. data/lib/tasks/db.rake +0 -2
  103. data/lib/tasks/doc.rake +2 -2
  104. data/lib/tasks/files.rake +11 -18
  105. data/lib/tasks/htm.rake +190 -62
  106. data/lib/tasks/jobs.rake +179 -54
  107. data/lib/tasks/tags.rake +8 -13
  108. data/scripts/backfill_parent_tags.rb +376 -0
  109. data/scripts/normalize_plural_tags.rb +335 -0
  110. metadata +109 -80
  111. data/examples/rails_app/app/controllers/tags_controller.rb +0 -30
  112. data/examples/sinatra_app/Gemfile.lock +0 -166
  113. data/lib/htm/active_record_config.rb +0 -104
  114. /data/examples/{config_file_example → 02_config_file_example}/README.md +0 -0
  115. /data/examples/{config_file_example → 02_config_file_example}/config/htm.local.yml +0 -0
  116. /data/examples/{config_file_example → 02_config_file_example}/custom_config.yml +0 -0
  117. /data/examples/{config_file_example → 02_config_file_example}/show_config.rb +0 -0
  118. /data/examples/{example_app → 06_example_app}/Rakefile +0 -0
  119. /data/examples/{cli_app → 07_cli_app}/README.md +0 -0
  120. /data/examples/{sinatra_app → 08_sinatra_app}/Gemfile +0 -0
  121. /data/examples/{telemetry → 10_telemetry}/README.md +0 -0
  122. /data/examples/{telemetry → 10_telemetry}/grafana/dashboards/htm-metrics.json +0 -0
  123. /data/examples/{rails_app → 12_rails_app}/.gitignore +0 -0
  124. /data/examples/{rails_app → 12_rails_app}/Procfile.dev +0 -0
  125. /data/examples/{rails_app → 12_rails_app}/README.md +0 -0
  126. /data/examples/{rails_app → 12_rails_app}/Rakefile +0 -0
  127. /data/examples/{rails_app → 12_rails_app}/app/assets/stylesheets/application.css +0 -0
  128. /data/examples/{rails_app → 12_rails_app}/app/assets/stylesheets/inter-font.css +0 -0
  129. /data/examples/{rails_app → 12_rails_app}/app/controllers/application_controller.rb +0 -0
  130. /data/examples/{rails_app → 12_rails_app}/app/controllers/search_controller.rb +0 -0
  131. /data/examples/{rails_app → 12_rails_app}/app/javascript/application.js +0 -0
  132. /data/examples/{rails_app → 12_rails_app}/app/javascript/controllers/application.js +0 -0
  133. /data/examples/{rails_app → 12_rails_app}/app/javascript/controllers/index.js +0 -0
  134. /data/examples/{rails_app → 12_rails_app}/app/views/files/index.html.erb +0 -0
  135. /data/examples/{rails_app → 12_rails_app}/app/views/files/show.html.erb +0 -0
  136. /data/examples/{rails_app → 12_rails_app}/app/views/layouts/application.html.erb +0 -0
  137. /data/examples/{rails_app → 12_rails_app}/app/views/memories/index.html.erb +0 -0
  138. /data/examples/{rails_app → 12_rails_app}/app/views/memories/new.html.erb +0 -0
  139. /data/examples/{rails_app → 12_rails_app}/app/views/robots/new.html.erb +0 -0
  140. /data/examples/{rails_app → 12_rails_app}/app/views/shared/_navbar.html.erb +0 -0
  141. /data/examples/{rails_app → 12_rails_app}/app/views/shared/_stat_card.html.erb +0 -0
  142. /data/examples/{rails_app → 12_rails_app}/bin/dev +0 -0
  143. /data/examples/{rails_app → 12_rails_app}/bin/rails +0 -0
  144. /data/examples/{rails_app → 12_rails_app}/bin/rake +0 -0
  145. /data/examples/{rails_app → 12_rails_app}/config/application.rb +0 -0
  146. /data/examples/{rails_app → 12_rails_app}/config/boot.rb +0 -0
  147. /data/examples/{rails_app → 12_rails_app}/config/database.yml +0 -0
  148. /data/examples/{rails_app → 12_rails_app}/config/environment.rb +0 -0
  149. /data/examples/{rails_app → 12_rails_app}/config/importmap.rb +0 -0
  150. /data/examples/{rails_app → 12_rails_app}/config/routes.rb +0 -0
  151. /data/examples/{rails_app → 12_rails_app}/config/tailwind.config.js +0 -0
  152. /data/examples/{rails_app → 12_rails_app}/config.ru +0 -0
  153. /data/examples/{rails_app → 12_rails_app}/log/.keep +0 -0
  154. /data/examples/{rails_app → 12_rails_app}/tmp/local_secret.txt +0 -0
data/examples/README.md CHANGED
@@ -2,20 +2,35 @@
2
2
 
3
3
  This directory contains example applications demonstrating various ways to use the HTM (Hierarchical Temporal Memory) gem.
4
4
 
5
- ## Prerequisites
5
+ ## Quick Start
6
+
7
+ All examples use the `htm_examples` database, isolated from development and production data.
8
+
9
+ ```bash
10
+ # One-time setup: create and configure examples database
11
+ rake examples:setup
12
+
13
+ # Run the basic example
14
+ rake examples:basic
15
+
16
+ # Run all standalone examples
17
+ rake examples:all
18
+
19
+ # Check examples database status
20
+ rake examples:status
21
+ ```
6
22
 
7
- All examples require:
23
+ ## Prerequisites
8
24
 
9
25
  1. **PostgreSQL Database** with pgvector extension:
10
26
  ```bash
11
- export HTM_DATABASE__URL="postgresql://user@localhost:5432/htm_development"
27
+ # Create the examples database (done automatically by rake examples:setup)
28
+ createdb htm_examples
29
+ psql htm_examples -c "CREATE EXTENSION IF NOT EXISTS vector; CREATE EXTENSION IF NOT EXISTS pg_trgm;"
12
30
  ```
13
31
 
14
- > **Note**: Database selection now respects `RAILS_ENV`. If `RAILS_ENV` is set,
15
- > HTM extracts the base name from `HTM_DATABASE__URL` and appends the environment suffix.
16
- > For example, with `HTM_DATABASE__URL=...htm_development` and `HTM_ENV=test`, HTM
17
- > connects to `htm_test`. When `RAILS_ENV` is unset (typical for examples),
18
- > behavior is unchanged.
32
+ > **Note**: All examples use `HTM_ENV=examples` which connects to `htm_examples` database.
33
+ > This isolation prevents examples from polluting development or production data.
19
34
 
20
35
  2. **Ollama** (recommended for local LLM):
21
36
  ```bash
@@ -28,18 +43,62 @@ All examples require:
28
43
  bundle install
29
44
  ```
30
45
 
46
+ ## Available Rake Tasks
47
+
48
+ ```bash
49
+ rake examples:setup # Set up examples database (create + setup schema)
50
+ rake examples:reset # Reset examples database (drop + create + setup)
51
+ rake examples:basic # Run basic_usage example
52
+ rake examples:all # Run all standalone examples
53
+ rake examples:status # Show examples database status
54
+ rake example # Alias for examples:basic
55
+ ```
56
+
31
57
  ---
32
58
 
33
- ## Standalone Scripts
59
+ ## Example Progression
60
+
61
+ Examples are numbered to indicate recommended learning order, from basic concepts to advanced features.
62
+
63
+ ### 00_create_examples_db.rb
64
+
65
+ **Database setup for examples.**
66
+
67
+ Creates and configures the `htm_examples` database. Run this first before other examples.
68
+
69
+ ```bash
70
+ ruby examples/00_create_examples_db.rb
71
+ ```
72
+
73
+ ---
74
+
75
+ ### 01_basic_usage.rb
76
+
77
+ **Getting started with HTM fundamentals.**
78
+
79
+ Demonstrates the core API: configuring HTM, registering a robot, and using the three primary methods (`remember`, `recall`, `forget`).
80
+
81
+ ```bash
82
+ ruby examples/01_basic_usage.rb
83
+ ```
84
+
85
+ **Features:**
86
+ - HTM configuration with Ollama provider
87
+ - Robot initialization
88
+ - Storing memories with `remember()`
89
+ - Retrieving memories with `recall()` using timeframes
90
+ - Understanding async embedding/tag generation
91
+
92
+ ---
34
93
 
35
- ### config_file_example/
94
+ ### 02_config_file_example/
36
95
 
37
96
  **Configuration management with source tracing.**
38
97
 
39
98
  Demonstrates how HTM uses `anyway_config` for layered configuration from multiple sources, with source tracing to show where each value originated.
40
99
 
41
100
  ```bash
42
- cd examples/config_file_example
101
+ cd examples/02_config_file_example
43
102
  ruby show_config.rb
44
103
  ```
45
104
 
@@ -65,37 +124,18 @@ HTM_EMBEDDING__MODEL=mxbai-embed-large ruby show_config.rb
65
124
  HTM_ENV=production ruby show_config.rb
66
125
  ```
67
126
 
68
- See [config_file_example/README.md](config_file_example/README.md) for detailed documentation.
69
-
70
- ---
71
-
72
- ### basic_usage.rb
73
-
74
- **Getting started with HTM fundamentals.**
75
-
76
- Demonstrates the core API: configuring HTM, registering a robot, and using the three primary methods (`remember`, `recall`, `forget`).
77
-
78
- ```bash
79
- ruby examples/basic_usage.rb
80
- ```
81
-
82
- **Features:**
83
- - HTM configuration with Ollama provider
84
- - Robot initialization
85
- - Storing memories with `remember()`
86
- - Retrieving memories with `recall()` using timeframes
87
- - Understanding async embedding/tag generation
127
+ See [02_config_file_example/README.md](02_config_file_example/README.md) for detailed documentation.
88
128
 
89
129
  ---
90
130
 
91
- ### custom_llm_configuration.rb
131
+ ### 03_custom_llm_configuration.rb
92
132
 
93
133
  **Flexible LLM integration patterns.**
94
134
 
95
135
  Shows how to configure HTM with custom embedding and tag generation methods, supporting multiple LLM providers or custom infrastructure.
96
136
 
97
137
  ```bash
98
- ruby examples/custom_llm_configuration.rb
138
+ ruby examples/03_custom_llm_configuration.rb
99
139
  ```
100
140
 
101
141
  **Features:**
@@ -108,14 +148,14 @@ ruby examples/custom_llm_configuration.rb
108
148
 
109
149
  ---
110
150
 
111
- ### file_loader_usage.rb
151
+ ### 04_file_loader_usage.rb
112
152
 
113
153
  **Loading documents into long-term memory.**
114
154
 
115
155
  Demonstrates loading markdown files with automatic paragraph chunking, YAML frontmatter extraction, and source tracking for re-sync.
116
156
 
117
157
  ```bash
118
- ruby examples/file_loader_usage.rb
158
+ ruby examples/04_file_loader_usage.rb
119
159
  ```
120
160
 
121
161
  **Features:**
@@ -129,14 +169,14 @@ ruby examples/file_loader_usage.rb
129
169
 
130
170
  ---
131
171
 
132
- ### timeframe_demo.rb
172
+ ### 05_timeframe_demo.rb
133
173
 
134
174
  **Flexible time-based filtering for recall.**
135
175
 
136
176
  Comprehensive demonstration of all timeframe options supported by `recall()`, including natural language parsing.
137
177
 
138
178
  ```bash
139
- ruby examples/timeframe_demo.rb
179
+ ruby examples/05_timeframe_demo.rb
140
180
  ```
141
181
 
142
182
  **Features:**
@@ -150,97 +190,101 @@ ruby examples/timeframe_demo.rb
150
190
 
151
191
  ---
152
192
 
153
- ### telemetry/
193
+ ### 06_example_app/
154
194
 
155
- **Live Grafana dashboard for HTM metrics.**
195
+ **Full-featured HTM demonstration with RubyLLM integration.**
156
196
 
157
- A complete telemetry demo using Homebrew-installed Prometheus and Grafana to visualize HTM metrics in real-time graphs.
197
+ A standalone application showing complete HTM workflow with database connection, Ollama integration, memory operations, and multiple search strategies.
158
198
 
159
199
  ```bash
160
- cd examples/telemetry
161
- ruby demo.rb
200
+ ruby examples/06_example_app/app.rb
162
201
  ```
163
202
 
164
- The demo automatically:
165
- - Checks/installs Prometheus and Grafana via Homebrew
166
- - Starts both services if not running
167
- - Configures Prometheus to scrape the demo's metrics
168
- - Cleans up previous demo data
169
- - Opens Grafana in your browser
170
-
171
203
  **Features:**
172
- - Live updating dashboard with job counts, latencies, cache hit rates
173
- - Pre-built Grafana dashboard JSON
174
- - No Docker required - uses native macOS services
204
+ - Database connection verification
205
+ - RubyLLM configuration for embeddings and tags
206
+ - Async embedding/tag generation with wait
207
+ - Comparison of search strategies (:fulltext, :vector, :hybrid)
208
+ - Detailed output of generated tags and embeddings
209
+
210
+ ---
211
+
212
+ ### 07_cli_app/
213
+
214
+ **Interactive command-line application.**
215
+
216
+ A REPL-style CLI demonstrating synchronous job execution with the `:inline` backend, ideal for CLI tools and scripts.
175
217
 
176
- **Dependencies:**
177
218
  ```bash
178
- brew install prometheus grafana
179
- gem install prometheus-client webrick
219
+ ruby examples/07_cli_app/htm_cli.rb
180
220
  ```
181
221
 
182
- See [telemetry/README.md](telemetry/README.md) for detailed setup instructions.
222
+ **Commands:**
223
+ - `remember <text>` - Store information (waits for embedding/tags)
224
+ - `recall <topic>` - Hybrid search with LLM-powered response generation
225
+ - `tags [prefix]` - List all tags with linked node content
226
+ - `stats` - Memory statistics
227
+ - `help` - Show help
228
+ - `exit` - Quit
183
229
 
184
- ---
230
+ **Features:**
231
+ - Synchronous job execution (`:inline` backend)
232
+ - Real-time progress feedback
233
+ - Tag extraction visibility during search
234
+ - Hybrid search with scoring (similarity, tag_boost, combined)
235
+ - RubyLLM chat integration for context-aware responses
236
+ - Response storage in long-term memory
185
237
 
186
- ### mcp_server.rb & mcp_client.rb
238
+ See [07_cli_app/README.md](07_cli_app/README.md) for detailed documentation.
187
239
 
188
- **Model Context Protocol (MCP) integration for AI assistants.**
240
+ ---
189
241
 
190
- A pair of examples demonstrating how to expose HTM as an MCP server and connect to it from a chat client. This enables AI assistants like Claude Desktop to use HTM's memory capabilities.
242
+ ### 08_sinatra_app/
191
243
 
192
- #### mcp_server.rb
244
+ **Web application with Sidekiq background processing.**
193
245
 
194
- An MCP server that exposes HTM's memory operations as tools:
246
+ A Sinatra-based web application demonstrating HTM in a multi-user web context with async job processing.
195
247
 
196
248
  ```bash
197
- ruby examples/mcp_server.rb
249
+ cd examples/08_sinatra_app
250
+ bundle install
251
+ bundle exec ruby app.rb
198
252
  ```
199
253
 
200
- **Tools exposed:**
201
- - `SetRobotTool` - Set the robot identity for this session (call first)
202
- - `GetRobotTool` - Get current robot information
203
- - `GetWorkingMemoryTool` - Get working memory contents for session restore
204
- - `RememberTool` - Store information with optional tags and metadata
205
- - `RecallTool` - Search memories using vector, fulltext, or hybrid strategies
206
- - `ForgetTool` - Soft-delete a memory (recoverable)
207
- - `RestoreTool` - Restore a soft-deleted memory
208
- - `ListTagsTool` - List tags with optional prefix filtering
209
- - `StatsTool` - Get memory usage statistics
210
-
211
- **Resources exposed:**
212
- - `htm://statistics` - Memory statistics as JSON
213
- - `htm://tags/hierarchy` - Tag hierarchy as text tree
214
- - `htm://memories/recent` - Last 20 memories
215
-
216
- **Claude Desktop configuration** (`~/.config/claude/claude_desktop_config.json`):
217
- ```json
218
- {
219
- "mcpServers": {
220
- "htm-memory": {
221
- "command": "ruby",
222
- "args": ["/path/to/htm/examples/mcp_server.rb"],
223
- "env": {
224
- "HTM_DATABASE__URL": "postgresql://user@localhost:5432/htm_development"
225
- }
226
- }
227
- }
228
- }
229
- ```
254
+ **Features:**
255
+ - Sidekiq integration for background jobs
256
+ - Session-based robot identification
257
+ - RESTful API endpoints:
258
+ - `POST /api/remember` - Store information
259
+ - `GET /api/recall` - Search memories with timeframe filtering
260
+ - `GET /api/stats` - Memory statistics
261
+ - `GET /api/tags` - Tag tree structure
262
+ - `GET /api/health` - Health check
263
+ - Interactive HTML UI with hybrid search scoring display
264
+ - Tag tree visualization
230
265
 
231
- #### mcp_client.rb
266
+ **Environment Variables:**
267
+ - `HTM_DATABASE__URL` - PostgreSQL connection (required)
268
+ - `REDIS_URL` - Redis for Sidekiq (default: redis://localhost:6379/0)
269
+ - `SESSION_SECRET` - Session encryption key
232
270
 
233
- An interactive chat client that connects to the MCP server via STDIO and uses a local Ollama model (gpt-oss) for conversation:
271
+ ---
272
+
273
+ ### 09_mcp_client.rb
274
+
275
+ **Model Context Protocol (MCP) client for AI assistants.**
276
+
277
+ An interactive chat client that connects to an MCP server via STDIO and uses a local Ollama model for conversation. This enables AI assistants like Claude Desktop to use HTM's memory capabilities.
234
278
 
235
279
  ```bash
236
- ruby examples/mcp_client.rb
280
+ ruby examples/09_mcp_client.rb
237
281
  ```
238
282
 
239
283
  **Features:**
240
284
  - Prompts for robot name on startup (or uses `HTM_ROBOT_NAME` env var)
241
285
  - Calls `SetRobotTool` to establish robot identity with the server
242
286
  - Offers to restore previous session from working memory
243
- - Connects to `mcp_server.rb` automatically via STDIO transport
287
+ - Connects to MCP server automatically via STDIO transport
244
288
  - Interactive chat loop with tool calling
245
289
  - LLM decides when to remember/recall information
246
290
  - Logs tool calls and results for visibility
@@ -254,7 +298,7 @@ ruby examples/mcp_client.rb
254
298
 
255
299
  **Example startup and conversation:**
256
300
  ```
257
- $ ruby examples/mcp_client.rb
301
+ $ ruby examples/09_mcp_client.rb
258
302
  Connecting to HTM MCP server...
259
303
  [✓] Connected to HTM MCP server
260
304
  [✓] Found 9 tools:
@@ -304,89 +348,40 @@ ollama pull gpt-oss # Or your preferred model
304
348
 
305
349
  ---
306
350
 
307
- ## Application Examples
351
+ ### 10_telemetry/
308
352
 
309
- ### example_app/
310
-
311
- **Full-featured HTM demonstration with RubyLLM integration.**
353
+ **Live Grafana dashboard for HTM metrics.**
312
354
 
313
- A standalone application showing complete HTM workflow with database connection, Ollama integration, memory operations, and multiple search strategies.
355
+ A complete telemetry demo using Homebrew-installed Prometheus and Grafana to visualize HTM metrics in real-time graphs.
314
356
 
315
357
  ```bash
316
- ruby examples/example_app/app.rb
358
+ cd examples/10_telemetry
359
+ ruby demo.rb
317
360
  ```
318
361
 
319
- **Features:**
320
- - Database connection verification
321
- - RubyLLM configuration for embeddings and tags
322
- - Async embedding/tag generation with wait
323
- - Comparison of search strategies (:fulltext, :vector, :hybrid)
324
- - Detailed output of generated tags and embeddings
325
-
326
- ---
327
-
328
- ### sinatra_app/
329
-
330
- **Web application with Sidekiq background processing.**
331
-
332
- A Sinatra-based web application demonstrating HTM in a multi-user web context with async job processing.
333
-
334
- ```bash
335
- cd examples/sinatra_app
336
- bundle install
337
- bundle exec ruby app.rb
338
- ```
362
+ The demo automatically:
363
+ - Checks/installs Prometheus and Grafana via Homebrew
364
+ - Starts both services if not running
365
+ - Configures Prometheus to scrape the demo's metrics
366
+ - Cleans up previous demo data
367
+ - Opens Grafana in your browser
339
368
 
340
369
  **Features:**
341
- - Sidekiq integration for background jobs
342
- - Session-based robot identification
343
- - RESTful API endpoints:
344
- - `POST /api/remember` - Store information
345
- - `GET /api/recall` - Search memories with timeframe filtering
346
- - `GET /api/stats` - Memory statistics
347
- - `GET /api/tags` - Tag tree structure
348
- - `GET /api/health` - Health check
349
- - Interactive HTML UI with hybrid search scoring display
350
- - Tag tree visualization
351
-
352
- **Environment Variables:**
353
- - `HTM_DATABASE__URL` - PostgreSQL connection (required)
354
- - `REDIS_URL` - Redis for Sidekiq (default: redis://localhost:6379/0)
355
- - `SESSION_SECRET` - Session encryption key
356
-
357
- ---
358
-
359
- ### cli_app/
360
-
361
- **Interactive command-line application.**
362
-
363
- A REPL-style CLI demonstrating synchronous job execution with the `:inline` backend, ideal for CLI tools and scripts.
370
+ - Live updating dashboard with job counts, latencies, cache hit rates
371
+ - Pre-built Grafana dashboard JSON
372
+ - No Docker required - uses native macOS services
364
373
 
374
+ **Dependencies:**
365
375
  ```bash
366
- ruby examples/cli_app/htm_cli.rb
376
+ brew install prometheus grafana
377
+ gem install prometheus-client webrick
367
378
  ```
368
379
 
369
- **Commands:**
370
- - `remember <text>` - Store information (waits for embedding/tags)
371
- - `recall <topic>` - Hybrid search with LLM-powered response generation
372
- - `tags [prefix]` - List all tags with linked node content
373
- - `stats` - Memory statistics
374
- - `help` - Show help
375
- - `exit` - Quit
376
-
377
- **Features:**
378
- - Synchronous job execution (`:inline` backend)
379
- - Real-time progress feedback
380
- - Tag extraction visibility during search
381
- - Hybrid search with scoring (similarity, tag_boost, combined)
382
- - RubyLLM chat integration for context-aware responses
383
- - Response storage in long-term memory
384
-
385
- See [cli_app/README.md](cli_app/README.md) for detailed documentation.
380
+ See [10_telemetry/README.md](10_telemetry/README.md) for detailed setup instructions.
386
381
 
387
382
  ---
388
383
 
389
- ### robot_groups/
384
+ ### 11_robot_groups/
390
385
 
391
386
  **Multi-robot coordination with shared working memory.**
392
387
 
@@ -397,7 +392,7 @@ Demonstrates high-availability patterns with shared working memory, failover, an
397
392
  Single-process demonstration of robot groups:
398
393
 
399
394
  ```bash
400
- ruby examples/robot_groups/same_process.rb
395
+ ruby examples/11_robot_groups/same_process.rb
401
396
  ```
402
397
 
403
398
  **Scenarios demonstrated:**
@@ -415,7 +410,7 @@ ruby examples/robot_groups/same_process.rb
415
410
  Cross-process demonstration with separate Ruby processes:
416
411
 
417
412
  ```bash
418
- ruby examples/robot_groups/multi_process.rb
413
+ ruby examples/11_robot_groups/multi_process.rb
419
414
  ```
420
415
 
421
416
  **Scenarios demonstrated:**
@@ -433,45 +428,68 @@ ruby examples/robot_groups/multi_process.rb
433
428
 
434
429
  ---
435
430
 
431
+ ### 12_rails_app/
432
+
433
+ **Rails integration example.**
434
+
435
+ A complete Rails application demonstrating HTM integration with Rails controllers, views, and background jobs.
436
+
437
+ ```bash
438
+ cd examples/12_rails_app
439
+ bundle install
440
+ bin/rails db:create db:migrate
441
+ bin/dev
442
+ ```
443
+
444
+ See [12_rails_app/README.md](12_rails_app/README.md) for detailed documentation.
445
+
446
+ ---
447
+
436
448
  ## Directory Structure
437
449
 
438
450
  ```
439
451
  examples/
440
- ├── README.md # This file
441
- ├── basic_usage.rb # Core API demonstration
442
- ├── custom_llm_configuration.rb # LLM integration patterns
443
- ├── file_loader_usage.rb # Document loading
444
- ├── timeframe_demo.rb # Time-based filtering
445
- ├── config_file_example/
446
- │ ├── show_config.rb # Config source tracing demo
447
- │ ├── custom_config.yml # Example for HTM_CONF
448
- │ ├── README.md # Configuration documentation
452
+ ├── README.md # This file
453
+ ├── examples_helper.rb # Shared setup for all examples
454
+ ├── 00_create_examples_db.rb # Database setup
455
+ ├── 01_basic_usage.rb # Core API demonstration
456
+ ├── 02_config_file_example/
457
+ ├── show_config.rb # Config source tracing demo
458
+ │ ├── custom_config.yml # Example for HTM_CONF
459
+ │ ├── README.md # Configuration documentation
449
460
  │ └── config/
450
- │ └── htm.local.yml # Auto-loaded local overrides
451
- ├── telemetry/
452
- ├── demo.rb # Live Grafana metrics dashboard
453
- ├── README.md
454
- ├── SETUP_README.md
455
- └── grafana/dashboards/htm-metrics.json
456
- ├── mcp_server.rb # MCP server exposing HTM tools
457
- ├── mcp_client.rb # MCP client with chat interface
458
- ├── example_app/
459
- │ ├── app.rb # Full-featured demo app
461
+ │ └── htm.local.yml # Auto-loaded local overrides
462
+ ├── 03_custom_llm_configuration.rb # LLM integration patterns
463
+ ├── 04_file_loader_usage.rb # Document loading
464
+ ├── 05_timeframe_demo.rb # Time-based filtering
465
+ ├── 06_example_app/
466
+ ├── app.rb # Full-featured demo app
460
467
  │ └── Rakefile
461
- ├── sinatra_app/
462
- │ ├── app.rb # Sinatra web application
468
+ ├── 07_cli_app/
469
+ │ ├── htm_cli.rb # Interactive CLI
470
+ │ └── README.md # Detailed CLI documentation
471
+ ├── 08_sinatra_app/
472
+ │ ├── app.rb # Sinatra web application
463
473
  │ ├── Gemfile
464
474
  │ └── Gemfile.lock
465
- ├── cli_app/
466
- ├── htm_cli.rb # Interactive CLI
467
- └── README.md # Detailed CLI documentation
468
- └── robot_groups/
469
- ├── same_process.rb # Single-process robot groups
470
- ├── multi_process.rb # Multi-process coordination
471
- ├── robot_worker.rb # Worker process for multi_process.rb
472
- └── lib/
473
- ├── robot_group.rb # RobotGroup coordination class
474
- └── working_memory_channel.rb # PostgreSQL pub/sub
475
+ ├── 09_mcp_client.rb # MCP client with chat interface
476
+ ├── 10_telemetry/
477
+ ├── demo.rb # Live Grafana metrics dashboard
478
+ │ ├── README.md
479
+ ├── SETUP_README.md
480
+ │ └── grafana/dashboards/htm-metrics.json
481
+ ├── 11_robot_groups/
482
+ │ ├── same_process.rb # Single-process robot groups
483
+ ├── multi_process.rb # Multi-process coordination
484
+ │ ├── robot_worker.rb # Worker process for multi_process.rb
485
+ │ └── lib/
486
+ │ ├── robot_group.rb # RobotGroup coordination class
487
+ │ └── working_memory_channel.rb # PostgreSQL pub/sub
488
+ └── 12_rails_app/
489
+ ├── app/ # Rails application files
490
+ ├── config/ # Rails configuration
491
+ ├── Gemfile
492
+ └── README.md
475
493
  ```
476
494
 
477
495
  ---
@@ -480,15 +498,16 @@ examples/
480
498
 
481
499
  | Use Case | Example |
482
500
  |----------|---------|
483
- | Learning HTM basics | `basic_usage.rb` |
484
- | Configuration management | `config_file_example/` |
485
- | Custom LLM integration | `custom_llm_configuration.rb` |
486
- | Loading documents/files | `file_loader_usage.rb` |
487
- | Time-based queries | `timeframe_demo.rb` |
488
- | Production observability | `telemetry/` |
489
- | MCP server for AI assistants | `mcp_server.rb` |
490
- | MCP client with chat interface | `mcp_client.rb` |
491
- | Web application | `sinatra_app/` |
492
- | CLI tool | `cli_app/` |
493
- | Multi-robot coordination | `robot_groups/` |
494
- | High availability | `robot_groups/` |
501
+ | Database setup | `00_create_examples_db.rb` |
502
+ | Learning HTM basics | `01_basic_usage.rb` |
503
+ | Configuration management | `02_config_file_example/` |
504
+ | Custom LLM integration | `03_custom_llm_configuration.rb` |
505
+ | Loading documents/files | `04_file_loader_usage.rb` |
506
+ | Time-based queries | `05_timeframe_demo.rb` |
507
+ | Full-featured demo | `06_example_app/` |
508
+ | CLI tool | `07_cli_app/` |
509
+ | Web application | `08_sinatra_app/` |
510
+ | MCP client with chat | `09_mcp_client.rb` |
511
+ | Production observability | `10_telemetry/` |
512
+ | Multi-robot coordination | `11_robot_groups/` |
513
+ | Rails integration | `12_rails_app/` |