htm 0.0.32 → 0.1.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 +4 -4
- data/.architecture/decisions/adrs/005-rag-based-retrieval-with-hybrid-search.md +4 -0
- data/.architecture/decisions/adrs/017-bitemporal-event-time.md +78 -0
- data/.architecture/decisions/adrs/018-memory-timelines-and-node-links.md +64 -0
- data/.architecture/decisions/adrs/019-epistemic-status.md +66 -0
- data/.claude.json +11 -0
- data/.envrc +9 -7
- data/.irbrc +14 -14
- data/.reek.yml +104 -0
- data/.rubocop.yml +0 -11
- data/.ruby-version +1 -0
- data/CHANGELOG.md +160 -98
- data/README.md +22 -17
- data/SETUP.md +49 -46
- data/_typos.toml +17 -0
- data/bin/htm +18 -0
- data/db/migrate/00010_convert_embedding_to_sparsevec.rb +47 -0
- data/db/migrate/00011_strip_frontmatter_from_chunks.rb +66 -0
- data/db/migrate/00012_add_event_time_to_nodes.rb +110 -0
- data/db/migrate/00013_create_node_links.rb +158 -0
- data/db/migrate/00014_add_epistemic_status.rb +110 -0
- data/db/schema.sql +238 -7
- data/db/seed_data/README.md +11 -11
- data/db/seeds.rb +11 -11
- data/docs/api/database.md +8 -8
- data/docs/api/embedding-service.md +138 -262
- data/docs/api/htm.md +128 -12
- data/docs/api/index.md +8 -8
- data/docs/api/long-term-memory.md +8 -19
- data/docs/api/working-memory.md +34 -46
- data/docs/api/yard/HTM/Admin/Console.md +53 -0
- data/docs/api/yard/HTM/Admin/DatabaseCommands/Console.md +38 -0
- data/docs/api/yard/HTM/Admin/DatabaseCommands.md +127 -0
- data/docs/api/yard/HTM/Admin/EnrichCommands.md +85 -0
- data/docs/api/yard/HTM/Admin/Error.md +9 -0
- data/docs/api/yard/HTM/Admin/FileCommands/Console.md +50 -0
- data/docs/api/yard/HTM/Admin/FileCommands.md +83 -0
- data/docs/api/yard/HTM/Admin/MemoryCommands/Base.md +17 -0
- data/docs/api/yard/HTM/Admin/MemoryCommands/Edit.md +74 -0
- data/docs/api/yard/HTM/Admin/MemoryCommands/Search.md +77 -0
- data/docs/api/yard/HTM/Admin/MemoryCommands/TagSearch.md +46 -0
- data/docs/api/yard/HTM/Admin/MemoryCommands/WorkingMemory.md +36 -0
- data/docs/api/yard/HTM/Admin/MemoryCommands.md +67 -0
- data/docs/api/yard/HTM/Admin/TagCommands/Console.md +48 -0
- data/docs/api/yard/HTM/Admin/TagCommands.md +105 -0
- data/docs/api/yard/HTM/Admin/TagGraph.md +149 -0
- data/docs/api/yard/HTM/Admin/YardDocs.md +60 -0
- data/docs/api/yard/HTM/Admin.md +102 -0
- data/docs/api/yard/HTM/AuthorizationError.md +6 -6
- data/docs/api/yard/HTM/CLI/ConfigFile.md +40 -0
- data/docs/api/yard/HTM/CLI/Db.md +63 -0
- data/docs/api/yard/HTM/CLI/Enrich.md +39 -0
- data/docs/api/yard/HTM/CLI/ErrorSuggestion.md +56 -0
- data/docs/api/yard/HTM/CLI/Files.md +36 -0
- data/docs/api/yard/HTM/CLI/Group/ClassMethods.md +11 -0
- data/docs/api/yard/HTM/CLI/Group.md +13 -0
- data/docs/api/yard/HTM/CLI/MemoryCommand/ClassMethods.md +20 -0
- data/docs/api/yard/HTM/CLI/MemoryCommand.md +9 -0
- data/docs/api/yard/HTM/CLI/Robots.md +31 -0
- data/docs/api/yard/HTM/CLI/Support.md +9 -0
- data/docs/api/yard/HTM/CLI/Tags.md +57 -0
- data/docs/api/yard/HTM/CLI/WorkingMemory.md +24 -0
- data/docs/api/yard/HTM/CLI.md +84 -0
- data/docs/api/yard/HTM/CircuitBreaker/Guarded.md +29 -0
- data/docs/api/yard/HTM/CircuitBreaker/Settings.md +21 -0
- data/docs/api/yard/HTM/CircuitBreaker/Status.md +42 -0
- data/docs/api/yard/HTM/CircuitBreaker.md +54 -49
- data/docs/api/yard/HTM/CircuitBreakerOpenError.md +9 -9
- data/docs/api/yard/HTM/Config/Builder.md +87 -0
- data/docs/api/yard/HTM/Config/Coercion.md +59 -0
- data/docs/api/yard/HTM/Config/Database.md +102 -0
- data/docs/api/yard/HTM/Config/Readers.md +30 -0
- data/docs/api/yard/HTM/Config/RubyLlmSettings.md +32 -0
- data/docs/api/yard/HTM/Config/Validator.md +59 -0
- data/docs/api/yard/HTM/Config.md +224 -105
- data/docs/api/yard/HTM/ConfigurationError.md +20 -0
- data/docs/api/yard/HTM/Confirmation.md +22 -0
- data/docs/api/yard/HTM/Database/Connection.md +26 -0
- data/docs/api/yard/HTM/Database/Migrations.md +32 -0
- data/docs/api/yard/HTM/Database/Report.md +26 -0
- data/docs/api/yard/HTM/Database/SchemaFile.md +52 -0
- data/docs/api/yard/HTM/Database/Teardown.md +15 -0
- data/docs/api/yard/HTM/Database.md +58 -70
- data/docs/api/yard/HTM/DatabaseDocs.md +67 -0
- data/docs/api/yard/HTM/DatabaseError.md +6 -6
- data/docs/api/yard/HTM/Deletion.md +32 -0
- data/docs/api/yard/HTM/EmbeddingError.md +6 -6
- data/docs/api/yard/HTM/EmbeddingService.md +37 -38
- data/docs/api/yard/HTM/Epistemics.md +135 -0
- data/docs/api/yard/HTM/Error.md +6 -6
- data/docs/api/yard/HTM/EventTime.md +110 -0
- data/docs/api/yard/HTM/FileMemory.md +76 -0
- data/docs/api/yard/HTM/InputValidator.md +82 -0
- data/docs/api/yard/HTM/JobAdapter/Job.md +26 -0
- data/docs/api/yard/HTM/JobAdapter.md +35 -16
- data/docs/api/yard/HTM/Jobs/FailureLogging.md +14 -0
- data/docs/api/yard/HTM/Jobs/GenerateEmbeddingJob.md +16 -0
- data/docs/api/yard/HTM/Jobs/GeneratePropositionsJob.md +17 -0
- data/docs/api/yard/HTM/Jobs/GenerateRelationshipsJob.md +26 -0
- data/docs/api/yard/HTM/Jobs/GenerateTagsJob.md +17 -0
- data/docs/api/yard/HTM/Jobs/LlmCallTimer.md +58 -0
- data/docs/api/yard/HTM/Jobs/NodeLookup.md +14 -0
- data/docs/api/yard/HTM/Jobs.md +13 -0
- data/docs/api/yard/HTM/Loaders/ChunkSync/Chunk.md +22 -0
- data/docs/api/yard/HTM/Loaders/ChunkSync.md +50 -0
- data/docs/api/yard/HTM/Loaders/MarkdownChunker/Coalescer.md +24 -0
- data/docs/api/yard/HTM/Loaders/MarkdownChunker/Packer.md +20 -0
- data/docs/api/yard/HTM/Loaders/MarkdownChunker/Splitter.md +18 -0
- data/docs/api/yard/HTM/Loaders/MarkdownChunker.md +83 -0
- data/docs/api/yard/HTM/Loaders/MarkdownLoader/Document.md +43 -0
- data/docs/api/yard/HTM/Loaders/MarkdownLoader.md +62 -0
- data/docs/api/yard/HTM/Loaders.md +5 -0
- data/docs/api/yard/HTM/LongTermMemory/FulltextSearch.md +36 -0
- data/docs/api/yard/HTM/LongTermMemory/HybridSearch/RrfMerger.md +25 -0
- data/docs/api/yard/HTM/LongTermMemory/HybridSearch/RrfRows.md +30 -0
- data/docs/api/yard/HTM/LongTermMemory/HybridSearch/RrfSql/Query.md +79 -0
- data/docs/api/yard/HTM/LongTermMemory/HybridSearch/RrfSql/Source.md +45 -0
- data/docs/api/yard/HTM/LongTermMemory/HybridSearch/RrfSql.md +11 -0
- data/docs/api/yard/HTM/LongTermMemory/HybridSearch/TagDepth.md +52 -0
- data/docs/api/yard/HTM/LongTermMemory/HybridSearch.md +52 -0
- data/docs/api/yard/HTM/LongTermMemory/NodeOperations.md +95 -0
- data/docs/api/yard/HTM/LongTermMemory/RelevanceScorer/HierarchicalJaccard.md +58 -0
- data/docs/api/yard/HTM/LongTermMemory/RelevanceScorer/TagSearch.md +27 -0
- data/docs/api/yard/HTM/LongTermMemory/RelevanceScorer.md +81 -0
- data/docs/api/yard/HTM/LongTermMemory/RobotOperations.md +22 -0
- data/docs/api/yard/HTM/LongTermMemory/TagOperations/ExpiringValue.md +24 -0
- data/docs/api/yard/HTM/LongTermMemory/TagOperations/TagMatchSql.md +40 -0
- data/docs/api/yard/HTM/LongTermMemory/TagOperations.md +129 -0
- data/docs/api/yard/HTM/LongTermMemory/VectorSearch.md +30 -0
- data/docs/api/yard/HTM/LongTermMemory.md +279 -31
- data/docs/api/yard/HTM/MCP/AssembleContextTool.md +16 -0
- data/docs/api/yard/HTM/MCP/ClearGroupWorkingMemoryTool.md +12 -0
- data/docs/api/yard/HTM/MCP/ClearWorkingMemoryTool.md +12 -0
- data/docs/api/yard/HTM/MCP/ConfigResource.md +12 -0
- data/docs/api/yard/HTM/MCP/ConfirmTool.md +12 -0
- data/docs/api/yard/HTM/MCP/CreateGroupTool.md +12 -0
- data/docs/api/yard/HTM/MCP/EnrichPendingTool.md +16 -0
- data/docs/api/yard/HTM/MCP/ExtractPropositionsTool.md +12 -0
- data/docs/api/yard/HTM/MCP/FailoverTool.md +12 -0
- data/docs/api/yard/HTM/MCP/FileInfoTool.md +13 -0
- data/docs/api/yard/HTM/MCP/FileSourcesResource.md +12 -0
- data/docs/api/yard/HTM/MCP/FileSupport.md +7 -0
- data/docs/api/yard/HTM/MCP/FindByTopicTool.md +12 -0
- data/docs/api/yard/HTM/MCP/FirstMentionTool.md +12 -0
- data/docs/api/yard/HTM/MCP/ForgetContentTool.md +12 -0
- data/docs/api/yard/HTM/MCP/ForgetTool.md +12 -0
- data/docs/api/yard/HTM/MCP/GetGroupStatusTool.md +12 -0
- data/docs/api/yard/HTM/MCP/GetGroupWorkingMemoryTool.md +12 -0
- data/docs/api/yard/HTM/MCP/GetNodeTool.md +12 -0
- data/docs/api/yard/HTM/MCP/GetRobotTool.md +12 -0
- data/docs/api/yard/HTM/MCP/GetWorkingMemoryTool.md +12 -0
- data/docs/api/yard/HTM/MCP/GroupRecallTool.md +12 -0
- data/docs/api/yard/HTM/MCP/GroupRememberTool.md +12 -0
- data/docs/api/yard/HTM/MCP/GroupSession.md +23 -0
- data/docs/api/yard/HTM/MCP/JoinGroupTool.md +12 -0
- data/docs/api/yard/HTM/MCP/LeaveGroupTool.md +12 -0
- data/docs/api/yard/HTM/MCP/ListDeletedTool.md +12 -0
- data/docs/api/yard/HTM/MCP/ListFilesTool.md +13 -0
- data/docs/api/yard/HTM/MCP/ListGroupsTool.md +12 -0
- data/docs/api/yard/HTM/MCP/ListRobotsTool.md +12 -0
- data/docs/api/yard/HTM/MCP/ListTagsTool.md +12 -0
- data/docs/api/yard/HTM/MCP/LoadDirectoryTool.md +13 -0
- data/docs/api/yard/HTM/MCP/LoadFileTool.md +13 -0
- data/docs/api/yard/HTM/MCP/MemoryStatsResource.md +13 -0
- data/docs/api/yard/HTM/MCP/PopularTagsTool.md +12 -0
- data/docs/api/yard/HTM/MCP/PromoteRobotTool.md +12 -0
- data/docs/api/yard/HTM/MCP/PurgeDeletedTool.md +12 -0
- data/docs/api/yard/HTM/MCP/RecallTool.md +16 -0
- data/docs/api/yard/HTM/MCP/RecentMemoriesResource.md +13 -0
- data/docs/api/yard/HTM/MCP/RelatedNodesTool.md +12 -0
- data/docs/api/yard/HTM/MCP/RememberTool.md +12 -0
- data/docs/api/yard/HTM/MCP/RestoreTool.md +12 -0
- data/docs/api/yard/HTM/MCP/RobotGroupsResource.md +13 -0
- data/docs/api/yard/HTM/MCP/RobotTeamsStatusTool.md +13 -0
- data/docs/api/yard/HTM/MCP/RunRobotTeamsTool.md +13 -0
- data/docs/api/yard/HTM/MCP/SearchByTagsTool.md +16 -0
- data/docs/api/yard/HTM/MCP/SearchTagsTool.md +12 -0
- data/docs/api/yard/HTM/MCP/Server.md +37 -0
- data/docs/api/yard/HTM/MCP/Session.md +32 -0
- data/docs/api/yard/HTM/MCP/SetRobotTool.md +12 -0
- data/docs/api/yard/HTM/MCP/ShutdownGroupTool.md +12 -0
- data/docs/api/yard/HTM/MCP/StartupPromptResource.md +16 -0
- data/docs/api/yard/HTM/MCP/StatsTool.md +12 -0
- data/docs/api/yard/HTM/MCP/SyncFilesTool.md +13 -0
- data/docs/api/yard/HTM/MCP/SyncGroupTool.md +12 -0
- data/docs/api/yard/HTM/MCP/TagHierarchyResource.md +16 -0
- data/docs/api/yard/HTM/MCP/TagNodeTool.md +12 -0
- data/docs/api/yard/HTM/MCP/TagTreeTool.md +22 -0
- data/docs/api/yard/HTM/MCP/TeamNames.md +7 -0
- data/docs/api/yard/HTM/MCP/TimelineTool.md +12 -0
- data/docs/api/yard/HTM/MCP/Tool.md +24 -0
- data/docs/api/yard/HTM/MCP/ToolSupport.md +17 -0
- data/docs/api/yard/HTM/MCP/TopicRelationshipsTool.md +12 -0
- data/docs/api/yard/HTM/MCP/TransferWorkingMemoryTool.md +12 -0
- data/docs/api/yard/HTM/MCP/UnloadFileTool.md +13 -0
- data/docs/api/yard/HTM/MCP/UntagNodeTool.md +12 -0
- data/docs/api/yard/HTM/MCP.md +30 -0
- data/docs/api/yard/HTM/Migration.md +44 -0
- data/docs/api/yard/HTM/Models/FileSource.md +66 -0
- data/docs/api/yard/HTM/Models/Node/Scopes.md +74 -0
- data/docs/api/yard/HTM/Models/Node/SoftDeletion.md +22 -0
- data/docs/api/yard/HTM/Models/Node/VectorSimilarity.md +28 -0
- data/docs/api/yard/HTM/Models/Node.md +113 -0
- data/docs/api/yard/HTM/Models/NodeLink.md +65 -0
- data/docs/api/yard/HTM/Models/NodeRelationship.md +31 -0
- data/docs/api/yard/HTM/Models/NodeTag.md +38 -0
- data/docs/api/yard/HTM/Models/Robot.md +50 -0
- data/docs/api/yard/HTM/Models/RobotNode.md +35 -0
- data/docs/api/yard/HTM/Models/Tag/Scopes.md +32 -0
- data/docs/api/yard/HTM/Models/Tag.md +112 -0
- data/docs/api/yard/HTM/Models.md +5 -0
- data/docs/api/yard/HTM/NodeEnrichment.md +36 -0
- data/docs/api/yard/HTM/NodeView.md +113 -0
- data/docs/api/yard/HTM/NotFoundError.md +6 -5
- data/docs/api/yard/HTM/Observability.md +58 -40
- data/docs/api/yard/HTM/PropositionError.md +6 -6
- data/docs/api/yard/HTM/PropositionService.md +63 -29
- data/docs/api/yard/HTM/QueryCache/KeyNormalizer.md +25 -0
- data/docs/api/yard/HTM/QueryCache/NullStore.md +19 -0
- data/docs/api/yard/HTM/QueryCache/Store.md +43 -0
- data/docs/api/yard/HTM/QueryCache.md +41 -46
- data/docs/api/yard/HTM/QueryTimeoutError.md +9 -8
- data/docs/api/yard/HTM/Railtie.md +11 -7
- data/docs/api/yard/HTM/RecallQuery.md +111 -0
- data/docs/api/yard/HTM/ResourceExhaustedError.md +6 -6
- data/docs/api/yard/HTM/RobotGroup/CacheSync.md +35 -0
- data/docs/api/yard/HTM/RobotGroup/Membership.md +96 -0
- data/docs/api/yard/HTM/RobotGroup/TransferOptions.md +14 -0
- data/docs/api/yard/HTM/RobotGroup/WorkingMemoryLedger.md +65 -0
- data/docs/api/yard/HTM/RobotGroup.md +116 -360
- data/docs/api/yard/HTM/Robots/Actions/TagMove.md +31 -0
- data/docs/api/yard/HTM/Robots/Actions.md +153 -0
- data/docs/api/yard/HTM/Robots/Neighbors.md +20 -0
- data/docs/api/yard/HTM/Robots/Supervisor/Idler.md +26 -0
- data/docs/api/yard/HTM/Robots/Supervisor/Roster.md +26 -0
- data/docs/api/yard/HTM/Robots/Supervisor.md +42 -0
- data/docs/api/yard/HTM/Robots/Team.md +60 -0
- data/docs/api/yard/HTM/Robots/Teams/Archivist.md +43 -0
- data/docs/api/yard/HTM/Robots/Teams/Consolidator.md +29 -0
- data/docs/api/yard/HTM/Robots/Teams/ContradictionDetector.md +33 -0
- data/docs/api/yard/HTM/Robots/Teams/TagCurator.md +37 -0
- data/docs/api/yard/HTM/Robots/Teams.md +5 -0
- data/docs/api/yard/HTM/Robots/Tools/ArchiveNodes.md +12 -0
- data/docs/api/yard/HTM/Robots/Tools/Base.md +16 -0
- data/docs/api/yard/HTM/Robots/Tools/MergeNodes.md +12 -0
- data/docs/api/yard/HTM/Robots/Tools/MergeTags.md +12 -0
- data/docs/api/yard/HTM/Robots/Tools/Pass.md +12 -0
- data/docs/api/yard/HTM/Robots/Tools/RecordConflict.md +12 -0
- data/docs/api/yard/HTM/Robots/Tools/SupersedeNode.md +12 -0
- data/docs/api/yard/HTM/Robots/Tools.md +14 -0
- data/docs/api/yard/HTM/Robots.md +72 -0
- data/docs/api/yard/HTM/SearchFilters.md +82 -0
- data/docs/api/yard/HTM/SequelConfig.md +97 -0
- data/docs/api/yard/HTM/Settings.md +39 -0
- data/docs/api/yard/HTM/Sinatra/Helpers.md +37 -0
- data/docs/api/yard/HTM/Sinatra/Middleware.md +29 -0
- data/docs/api/yard/HTM/Sinatra.md +44 -0
- data/docs/api/yard/HTM/SqlBuilder/Sparsevec.md +69 -0
- data/docs/api/yard/HTM/SqlBuilder.md +62 -75
- data/docs/api/yard/HTM/StartupPrompt.md +45 -0
- data/docs/api/yard/HTM/TagError.md +6 -6
- data/docs/api/yard/HTM/TagService.md +100 -37
- data/docs/api/yard/HTM/TagTree/Branch.md +43 -0
- data/docs/api/yard/HTM/TagTree/SvgRenderer/Layout.md +51 -0
- data/docs/api/yard/HTM/TagTree/SvgRenderer.md +25 -0
- data/docs/api/yard/HTM/TagTree.md +65 -0
- data/docs/api/yard/HTM/Telemetry/NullInstrument.md +12 -9
- data/docs/api/yard/HTM/Telemetry/NullMeter.md +17 -9
- data/docs/api/yard/HTM/Telemetry.md +53 -50
- data/docs/api/yard/HTM/Timeframe/Result.md +13 -16
- data/docs/api/yard/HTM/Timeframe.md +17 -20
- data/docs/api/yard/HTM/TimeframeExtractor/Patterns.md +18 -0
- data/docs/api/yard/HTM/TimeframeExtractor/Result.md +13 -16
- data/docs/api/yard/HTM/TimeframeExtractor.md +27 -16
- data/docs/api/yard/HTM/Timeline.md +113 -0
- data/docs/api/yard/HTM/ValidationError.md +6 -5
- data/docs/api/yard/HTM/Workflows/RememberWorkflow/NodeState.md +73 -0
- data/docs/api/yard/HTM/Workflows/RememberWorkflow.md +80 -0
- data/docs/api/yard/HTM/Workflows.md +5 -0
- data/docs/api/yard/HTM/WorkingMemory/Entry.md +67 -0
- data/docs/api/yard/HTM/WorkingMemory/Store.md +54 -0
- data/docs/api/yard/HTM/WorkingMemory.md +113 -87
- data/docs/api/yard/HTM/WorkingMemoryChannel/Subscribers.md +28 -0
- data/docs/api/yard/HTM/WorkingMemoryChannel.md +108 -102
- data/docs/api/yard/HTM.md +483 -5
- data/docs/api/yard/README.md +211 -0
- data/docs/api/yard/Sinatra/Base.md +30 -0
- data/docs/api/yard/Sinatra.md +7 -0
- data/docs/api/yard/index.csv +1834 -181
- data/docs/api/yard-reference.md +228 -9
- data/docs/architecture/adrs/005-rag-retrieval.md +3 -0
- data/docs/architecture/adrs/index.md +1 -1
- data/docs/architecture/index.md +2 -2
- data/docs/architecture/overview.md +3 -3
- data/docs/assets/images/project-structure.svg +2 -2
- data/docs/database/INDEX.md +113 -0
- data/docs/database/README.md +60 -176
- data/docs/database/file_sources.md +51 -0
- data/docs/database/file_sources.svg +120 -0
- data/docs/database/naming-convention.md +12 -12
- data/docs/database/node_links.md +65 -0
- data/docs/database/node_links.svg +171 -0
- data/docs/database/node_relationships.md +62 -0
- data/docs/database/node_relationships.svg +124 -0
- data/docs/database/node_tags.md +50 -0
- data/docs/database/node_tags.svg +125 -0
- data/docs/database/nodes.md +120 -0
- data/docs/database/nodes.svg +394 -0
- data/docs/database/robot_nodes.md +57 -0
- data/docs/database/robot_nodes.svg +155 -0
- data/docs/database/robots.md +40 -0
- data/docs/database/robots.svg +131 -0
- data/docs/database/schema.svg +384 -275
- data/docs/database/schema_migrations.md +19 -0
- data/docs/database/schema_migrations.svg +31 -0
- data/docs/database/tags.md +47 -0
- data/docs/database/tags.svg +90 -0
- data/docs/development/asgard-tasks.md +114 -0
- data/docs/development/contributing.md +6 -6
- data/docs/development/index.md +15 -17
- data/docs/development/schema.md +43 -14
- data/docs/development/setup.md +60 -70
- data/docs/development/testing.md +57 -65
- data/docs/examples/basic-usage.md +9 -9
- data/docs/examples/config-files.md +8 -8
- data/docs/examples/file-loading.md +12 -12
- data/docs/examples/index.md +15 -8
- data/docs/examples/llm-configuration.md +13 -9
- data/docs/examples/mcp-client.md +61 -59
- data/docs/examples/rails-integration.md +5 -4
- data/docs/examples/sinatra-integration.md +5 -4
- data/docs/examples/standalone-app.md +17 -16
- data/docs/getting-started/index.md +3 -3
- data/docs/getting-started/installation.md +76 -83
- data/docs/getting-started/quick-start.md +24 -22
- data/docs/guides/adding-memories.md +14 -3
- data/docs/guides/cli.md +458 -0
- data/docs/guides/configuration.md +72 -37
- data/docs/guides/epistemic-status.md +138 -0
- data/docs/guides/event-time.md +106 -0
- data/docs/guides/file-loading.md +26 -22
- data/docs/guides/getting-started.md +21 -17
- data/docs/guides/long-term-memory.md +1 -1
- data/docs/guides/mcp-server.md +416 -173
- data/docs/guides/propositions.md +18 -29
- data/docs/guides/recalling-memories.md +22 -7
- data/docs/guides/robot-teams.md +96 -0
- data/docs/guides/search-strategies.md +10 -7
- data/docs/guides/tags.md +19 -19
- data/docs/guides/timeline.md +106 -0
- data/docs/index.md +7 -7
- data/docs/llms.txt +35 -0
- data/docs/multi_framework_support.md +13 -13
- data/docs/robots/robot-groups.md +9 -9
- data/docs/robots/two-tier-memory.md +13 -26
- data/docs/setup_local_database.md +33 -31
- data/examples/00_create_examples_db.rb +20 -7
- data/examples/01_basic_usage.rb +20 -18
- data/examples/02_config_file_example/README.md +13 -12
- data/examples/02_config_file_example/config/htm.local.yml +8 -8
- data/examples/02_config_file_example/show_config.rb +3 -4
- data/examples/03_custom_llm_configuration.rb +12 -12
- data/examples/04_file_loader_usage.rb +15 -21
- data/examples/05_timeframe_demo.rb +282 -234
- data/examples/06_example_app/.loki +36 -0
- data/examples/06_example_app/app.rb +21 -33
- data/examples/07_cli_app/README.md +18 -18
- data/examples/07_cli_app/htm_cli.rb +25 -19
- data/examples/08_sinatra_app/Gemfile +1 -0
- data/examples/08_sinatra_app/Gemfile.lock +101 -67
- data/examples/08_sinatra_app/app.rb +64 -62
- data/examples/09_mcp_client.rb +92 -119
- data/examples/10_telemetry/README.md +3 -3
- data/examples/10_telemetry/SETUP_README.md +2 -2
- data/examples/10_telemetry/demo.rb +5 -5
- data/examples/11_robot_groups/README.md +5 -4
- data/examples/11_robot_groups/lib/robot_worker.rb +0 -5
- data/examples/11_robot_groups/multi_process.rb +5 -4
- data/examples/11_robot_groups/same_process.rb +18 -17
- data/examples/12_rails_app/Gemfile +1 -1
- data/examples/12_rails_app/Gemfile.lock +146 -97
- data/examples/12_rails_app/README.md +21 -17
- data/examples/12_rails_app/app/controllers/chats_controller.rb +33 -63
- data/examples/12_rails_app/app/controllers/memories_controller.rb +7 -1
- data/examples/12_rails_app/app/controllers/messages_controller.rb +21 -58
- data/examples/12_rails_app/app/controllers/search_controller.rb +7 -2
- data/examples/12_rails_app/app/models/chat.rb +0 -33
- data/examples/12_rails_app/app/models/message.rb +0 -2
- data/examples/12_rails_app/app/views/chats/show.html.erb +2 -2
- data/examples/12_rails_app/app/views/files/new.html.erb +1 -1
- data/examples/12_rails_app/app/views/memories/_memory_card.html.erb +1 -1
- data/examples/12_rails_app/app/views/memories/new.html.erb +12 -0
- data/examples/12_rails_app/app/views/memories/show.html.erb +13 -0
- data/examples/12_rails_app/app/views/search/index.html.erb +18 -2
- data/examples/12_rails_app/config/initializers/ruby_llm.rb +11 -15
- data/examples/12_rails_app/db/migrate/20261205061932_create_ruby_llm_records.rb +160 -0
- data/examples/12_rails_app/db/migrate/20261205061933_create_chats.rb +13 -0
- data/examples/12_rails_app/db/migrate/20261205061934_create_messages.rb +23 -0
- data/examples/12_rails_app/db/schema.rb +163 -20
- data/examples/README.md +53 -44
- data/examples/examples_helper.rb +29 -5
- data/htm.gemspec +68 -0
- data/lib/htm/admin/database_commands.rb +454 -0
- data/lib/htm/admin/enrich_commands.rb +209 -0
- data/lib/htm/admin/file_commands.rb +323 -0
- data/lib/htm/admin/memory_commands/edit.rb +157 -0
- data/lib/htm/admin/memory_commands/search.rb +178 -0
- data/lib/htm/admin/memory_commands/tag_search.rb +98 -0
- data/lib/htm/admin/memory_commands/text.rb +164 -0
- data/lib/htm/admin/memory_commands/working_memory.rb +71 -0
- data/lib/htm/admin/memory_commands.rb +179 -0
- data/lib/htm/admin/tag_commands.rb +352 -0
- data/lib/htm/admin/tag_graph.rb +290 -0
- data/lib/htm/admin/templates/tag_graph.html +295 -0
- data/lib/htm/admin/yard_docs.rb +206 -0
- data/lib/htm/admin.rb +253 -0
- data/lib/htm/circuit_breaker.rb +142 -68
- data/lib/htm/class_methods.rb +136 -0
- data/lib/htm/cli/config_file.rb +86 -0
- data/lib/htm/cli/db.rb +77 -0
- data/lib/htm/cli/enrich.rb +44 -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 +32 -0
- data/lib/htm/cli/memory.rb +320 -0
- data/lib/htm/cli/memory_command.rb +71 -0
- data/lib/htm/cli/robots.rb +71 -0
- data/lib/htm/cli/support.rb +46 -0
- data/lib/htm/cli/tags.rb +103 -0
- data/lib/htm/cli/working_memory.rb +41 -0
- data/lib/htm/cli.rb +75 -0
- data/lib/htm/config/builder.rb +99 -68
- data/lib/htm/config/coercion.rb +91 -0
- data/lib/htm/config/database.rb +81 -52
- data/lib/htm/config/defaults.yml +52 -16
- data/lib/htm/config/readers.rb +85 -0
- data/lib/htm/config/ruby_llm_settings.rb +58 -0
- data/lib/htm/config/validator.rb +45 -26
- data/lib/htm/config.rb +112 -297
- data/lib/htm/database.rb +305 -396
- data/lib/htm/database_docs.rb +149 -0
- data/lib/htm/embedding_service.rb +26 -66
- data/lib/htm/epistemics.rb +254 -0
- data/lib/htm/errors.rb +4 -4
- data/lib/htm/event_time.rb +201 -0
- data/lib/htm/file_memory.rb +106 -0
- data/lib/htm/input_validator.rb +158 -0
- data/lib/htm/integrations/sinatra.rb +43 -31
- data/lib/htm/job_adapter.rb +78 -83
- data/lib/htm/jobs/failure_logging.rb +49 -0
- data/lib/htm/jobs/generate_embedding_job.rb +21 -28
- data/lib/htm/jobs/generate_propositions_job.rb +56 -28
- data/lib/htm/jobs/generate_relationships_job.rb +28 -24
- data/lib/htm/jobs/generate_tags_job.rb +32 -37
- data/lib/htm/jobs/llm_call_timer.rb +53 -0
- data/lib/htm/jobs/node_lookup.rb +27 -0
- data/lib/htm/knowledge_memory.rb +153 -0
- data/lib/htm/loaders/chunk_sync.rb +168 -0
- data/lib/htm/loaders/markdown_chunker.rb +207 -36
- data/lib/htm/loaders/markdown_loader.rb +76 -120
- data/lib/htm/long_term_memory/fulltext_search.rb +15 -36
- data/lib/htm/long_term_memory/hybrid_search.rb +471 -544
- data/lib/htm/long_term_memory/node_operations.rb +110 -81
- data/lib/htm/long_term_memory/relevance_scorer.rb +232 -237
- data/lib/htm/long_term_memory/tag_operations.rb +201 -130
- data/lib/htm/long_term_memory/vector_search.rb +33 -61
- data/lib/htm/long_term_memory.rb +5 -17
- data/lib/htm/mcp/file_tools.rb +190 -0
- data/lib/htm/mcp/group_tools.rb +223 -233
- data/lib/htm/mcp/node_tools.rb +350 -0
- data/lib/htm/mcp/resources.rb +102 -28
- data/lib/htm/mcp/robot_team_tools.rb +119 -0
- data/lib/htm/mcp/server.rb +26 -14
- data/lib/htm/mcp/tag_tools.rb +130 -0
- data/lib/htm/mcp/tool.rb +40 -0
- data/lib/htm/mcp/tool_support.rb +88 -0
- data/lib/htm/mcp/tools.rb +232 -283
- data/lib/htm/models/file_source.rb +35 -20
- data/lib/htm/models/node.rb +287 -193
- data/lib/htm/models/node_link.rb +97 -0
- data/lib/htm/models/node_relationship.rb +3 -2
- data/lib/htm/models/node_tag.rb +22 -2
- data/lib/htm/models/robot.rb +4 -8
- data/lib/htm/models/robot_node.rb +5 -2
- data/lib/htm/models/tag.rb +53 -207
- data/lib/htm/node_enrichment.rb +75 -0
- data/lib/htm/node_view.rb +295 -0
- data/lib/htm/observability.rb +114 -98
- data/lib/htm/proposition_service.rb +38 -74
- data/lib/htm/query_cache.rb +145 -91
- data/lib/htm/railtie.rb +3 -7
- data/lib/htm/request_options.rb +149 -0
- data/lib/htm/robot_group/cache_sync.rb +69 -0
- data/lib/htm/robot_group/membership.rb +147 -0
- data/lib/htm/robot_group/working_memory_ledger.rb +111 -0
- data/lib/htm/robot_group.rb +128 -521
- data/lib/htm/robots/actions.rb +371 -0
- data/lib/htm/robots/neighbors.rb +63 -0
- data/lib/htm/robots/node_brief.rb +49 -0
- data/lib/htm/robots/supervisor.rb +140 -0
- data/lib/htm/robots/team.rb +124 -0
- data/lib/htm/robots/teams/archivist.rb +88 -0
- data/lib/htm/robots/teams/consolidator.rb +52 -0
- data/lib/htm/robots/teams/contradiction_detector.rb +67 -0
- data/lib/htm/robots/teams/tag_curator.rb +75 -0
- data/lib/htm/robots/tools.rb +127 -0
- data/lib/htm/robots.rb +125 -0
- data/lib/htm/search_filters.rb +116 -0
- data/lib/htm/sequel_config.rb +95 -59
- data/lib/htm/sql_builder.rb +162 -53
- data/lib/htm/startup_prompt.rb +73 -0
- data/lib/htm/tag_service.rb +104 -109
- data/lib/htm/tag_tree.rb +206 -0
- data/lib/htm/telemetry.rb +15 -16
- data/lib/htm/timeframe.rb +27 -9
- data/lib/htm/timeframe_extractor.rb +77 -55
- data/lib/htm/timeline.rb +230 -0
- data/lib/htm/version.rb +1 -1
- data/lib/htm/workflows/remember_workflow.rb +145 -136
- data/lib/htm/working_memory/entry.rb +53 -0
- data/lib/htm/working_memory/store.rb +97 -0
- data/lib/htm/working_memory.rb +137 -139
- data/lib/htm/working_memory_channel.rb +90 -40
- data/lib/htm.rb +177 -501
- data/mkdocs.yml +100 -23
- data/scripts/install_local_database.sh +7 -6
- metadata +1032 -594
- data/.tbls.yml +0 -31
- data/Rakefile +0 -240
- data/bin/htm_mcp +0 -27
- data/docs/api/yard/HTM/ActiveRecordConfig.md +0 -29
- data/docs/api/yard/HTM/ConfigSection.md +0 -28
- data/docs/database/public.file_sources.md +0 -42
- data/docs/database/public.file_sources.svg +0 -219
- data/docs/database/public.node_stats.md +0 -48
- data/docs/database/public.node_stats.svg +0 -41
- data/docs/database/public.node_tags.md +0 -41
- data/docs/database/public.node_tags.svg +0 -251
- data/docs/database/public.nodes.md +0 -61
- data/docs/database/public.nodes.svg +0 -283
- data/docs/database/public.nodes_tags.md +0 -39
- data/docs/database/public.nodes_tags.svg +0 -112
- data/docs/database/public.ontology_structure.md +0 -48
- data/docs/database/public.ontology_structure.svg +0 -38
- data/docs/database/public.operations_log.md +0 -42
- data/docs/database/public.operations_log.svg +0 -130
- data/docs/database/public.relationships.md +0 -39
- data/docs/database/public.relationships.svg +0 -41
- data/docs/database/public.robot_activity.md +0 -46
- data/docs/database/public.robot_activity.svg +0 -35
- data/docs/database/public.robot_nodes.md +0 -48
- data/docs/database/public.robot_nodes.svg +0 -251
- data/docs/database/public.robots.md +0 -34
- data/docs/database/public.robots.svg +0 -165
- data/docs/database/public.schema_migrations.md +0 -29
- data/docs/database/public.schema_migrations.svg +0 -26
- data/docs/database/public.tags.md +0 -38
- data/docs/database/public.tags.svg +0 -147
- data/docs/database/public.topic_relationships.md +0 -45
- data/docs/database/public.topic_relationships.svg +0 -32
- data/docs/database/schema.json +0 -1814
- data/docs/database_rake_tasks.md +0 -345
- data/docs/development/rake-tasks.md +0 -1113
- data/docs/using_rake_tasks_in_your_app.md +0 -383
- data/examples/06_example_app/Rakefile +0 -71
- data/examples/12_rails_app/app/models/model.rb +0 -5
- data/examples/12_rails_app/app/models/tool_call.rb +0 -5
- data/examples/12_rails_app/db/migrate/20250124000001_create_ruby_llm_tables.rb +0 -34
- data/examples/12_rails_app/db/migrate/20250124000002_create_models_table.rb +0 -28
- data/lib/htm/mcp/cli.rb +0 -583
- data/lib/htm/tasks.rb +0 -79
- data/lib/tasks/db.rake +0 -15
- data/lib/tasks/doc.rake +0 -301
- data/lib/tasks/files.rake +0 -285
- data/lib/tasks/htm.rake +0 -703
- data/lib/tasks/jobs.rake +0 -429
- data/lib/tasks/tags.rake +0 -184
- data/scripts/backfill_parent_tags.rb +0 -376
- data/scripts/normalize_plural_tags.rb +0 -335
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class HTM
|
|
4
|
+
# SearchFilters' fields (the class is below)
|
|
5
|
+
SearchFilters = Data.define(:timeframe, :metadata, :time_axis, :as_of, :statuses, :min_confidence)
|
|
6
|
+
|
|
7
|
+
# The filters every search applies, beyond its query: a timeframe, a
|
|
8
|
+
# metadata containment, which time axis the timeframe reads, and a point
|
|
9
|
+
# in time the memories must have been valid at.
|
|
10
|
+
#
|
|
11
|
+
# Time axes:
|
|
12
|
+
# - :event (default) - when the memory happened. A node matches a range
|
|
13
|
+
# when its span [occurred_at, occurred_until] overlaps it, so a digest
|
|
14
|
+
# covering March is found by a search for mid-March.
|
|
15
|
+
# - :recorded - when HTM stored the node (created_at)
|
|
16
|
+
#
|
|
17
|
+
# as_of keeps only nodes whose state was true at that moment:
|
|
18
|
+
# COALESCE(valid_from, occurred_at) <= as_of, and valid_until NULL or later.
|
|
19
|
+
#
|
|
20
|
+
# statuses keeps only nodes with one of those epistemic statuses;
|
|
21
|
+
# min_confidence keeps nodes rated at least that confident (an unrated
|
|
22
|
+
# node counts as 1.0).
|
|
23
|
+
#
|
|
24
|
+
# @example
|
|
25
|
+
# filters = HTM::SearchFilters.build(timeframe: range, as_of: Time.utc(2026, 3, 1))
|
|
26
|
+
# filters.conditions(table_alias: 'n') # => ["(...)", "(...)"]
|
|
27
|
+
#
|
|
28
|
+
class SearchFilters
|
|
29
|
+
# @return [SearchFilters] filters that keep every active node
|
|
30
|
+
def self.none = build
|
|
31
|
+
|
|
32
|
+
# build's options and their defaults
|
|
33
|
+
DEFAULTS = { timeframe: nil, metadata: {}.freeze, time_axis: :event, as_of: nil, statuses: nil,
|
|
34
|
+
min_confidence: nil }.freeze
|
|
35
|
+
|
|
36
|
+
# @param options [Hash] any of:
|
|
37
|
+
# @param timeframe [nil, Range, Array<Range>] normalized time range(s)
|
|
38
|
+
# @param metadata [Hash, nil] metadata the node must contain
|
|
39
|
+
# @param time_axis [Symbol, String] :event or :recorded
|
|
40
|
+
# @param as_of [Time, nil] moment the node's state must have been valid
|
|
41
|
+
# @param statuses [Array<String>, nil] epistemic statuses to keep (nil keeps all)
|
|
42
|
+
# @param min_confidence [Float, nil] lowest confidence to keep
|
|
43
|
+
# @return [SearchFilters]
|
|
44
|
+
# @raise [ArgumentError] for an unknown time axis
|
|
45
|
+
def self.build(**options)
|
|
46
|
+
given = DEFAULTS.merge(options)
|
|
47
|
+
new(**given, metadata: given[:metadata] || {}, time_axis: HTM::EventTime.axis!(given[:time_axis]),
|
|
48
|
+
statuses: given[:statuses]&.map(&:to_s))
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# SQL conditions to AND into a WHERE clause
|
|
52
|
+
#
|
|
53
|
+
# @param table_alias [String, nil] alias of the nodes table
|
|
54
|
+
# @return [Array<String>]
|
|
55
|
+
def conditions(table_alias: nil)
|
|
56
|
+
[
|
|
57
|
+
timeframe_condition(table_alias),
|
|
58
|
+
HTM::SqlBuilder.metadata_condition(metadata, table_alias:),
|
|
59
|
+
as_of_condition(table_alias),
|
|
60
|
+
status_condition(table_alias),
|
|
61
|
+
confidence_condition(table_alias)
|
|
62
|
+
].compact
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# @return [String] the conditions as an "AND ..." suffix; "" without any
|
|
66
|
+
def and_sql(table_alias: nil) = conditions(table_alias:).map { "AND #{it}" }.join(' ')
|
|
67
|
+
|
|
68
|
+
# @return [String] the column results are ordered and aged by
|
|
69
|
+
def time_column = time_axis == :recorded ? 'created_at' : 'occurred_at'
|
|
70
|
+
|
|
71
|
+
# @return [Array] values identifying these filters in a cache key
|
|
72
|
+
def cache_key = [timeframe, metadata, time_axis, as_of, statuses, min_confidence]
|
|
73
|
+
|
|
74
|
+
private
|
|
75
|
+
|
|
76
|
+
def column(table_alias, name) = [table_alias, name].compact.join('.')
|
|
77
|
+
|
|
78
|
+
def timeframe_condition(table_alias)
|
|
79
|
+
return if timeframe.nil?
|
|
80
|
+
|
|
81
|
+
return HTM::SqlBuilder.timeframe_condition(timeframe, table_alias:, column: 'created_at') if time_axis == :recorded
|
|
82
|
+
|
|
83
|
+
ranges = timeframe.is_a?(Range) ? [timeframe] : timeframe
|
|
84
|
+
"(#{ranges.map { span_overlap(table_alias, it) }.join(' OR ')})"
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# The node's event span overlaps +range+
|
|
88
|
+
def span_overlap(table_alias, range)
|
|
89
|
+
db = HTM.db
|
|
90
|
+
start = column(table_alias, 'occurred_at')
|
|
91
|
+
stop = "COALESCE(#{column(table_alias, 'occurred_until')}, #{start})"
|
|
92
|
+
"(#{start} <= #{db.literal(range.end.iso8601)} AND #{stop} >= #{db.literal(range.begin.iso8601)})"
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def as_of_condition(table_alias)
|
|
96
|
+
return if as_of.nil?
|
|
97
|
+
|
|
98
|
+
moment = HTM.db.literal(as_of.iso8601)
|
|
99
|
+
from = "COALESCE(#{column(table_alias, 'valid_from')}, #{column(table_alias, 'occurred_at')})"
|
|
100
|
+
until_ = column(table_alias, 'valid_until')
|
|
101
|
+
"(#{from} <= #{moment} AND (#{until_} IS NULL OR #{until_} > #{moment}))"
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def status_condition(table_alias)
|
|
105
|
+
return if statuses.nil? || statuses.empty?
|
|
106
|
+
|
|
107
|
+
"#{column(table_alias, 'epistemic_status')} IN (#{statuses.map { HTM.db.literal(it) }.join(', ')})"
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def confidence_condition(table_alias)
|
|
111
|
+
return if min_confidence.nil?
|
|
112
|
+
|
|
113
|
+
"COALESCE(#{column(table_alias, 'confidence')}, 1.0) >= #{HTM.db.literal(min_confidence.to_f)}"
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
end
|
data/lib/htm/sequel_config.rb
CHANGED
|
@@ -2,6 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
require 'sequel'
|
|
4
4
|
|
|
5
|
+
# Key connection checkout by fiber, not thread: jobs on the default :fiber
|
|
6
|
+
# backend run concurrently in one thread, and fibers sharing a connection
|
|
7
|
+
# read each other's query results (or deadlock)
|
|
8
|
+
Sequel.extension :fiber_concurrency
|
|
9
|
+
# HTM (Hierarchical Temporal Memory): memory management for LLM robots (see
|
|
10
|
+
# lib/htm.rb). This file adds its Sequel connection.
|
|
5
11
|
class HTM
|
|
6
12
|
# Sequel database configuration and model loading
|
|
7
13
|
#
|
|
@@ -9,10 +15,21 @@ class HTM
|
|
|
9
15
|
# - Environment variables (HTM_DATABASE__URL, HTM_DATABASE__HOST, etc.)
|
|
10
16
|
# - Programmatic configuration via HTM.configure
|
|
11
17
|
#
|
|
12
|
-
#
|
|
13
|
-
#
|
|
18
|
+
# Each fiber checks out its own pooled connection (the fiber_concurrency
|
|
19
|
+
# extension), so the fiber job backend can run jobs concurrently.
|
|
14
20
|
#
|
|
15
|
-
|
|
21
|
+
module SequelConfig
|
|
22
|
+
# What establish_connection! does with the models once connected, by its
|
|
23
|
+
# load_models: value. false leaves them unloaded (migrations on a fresh
|
|
24
|
+
# database); any other value loads them once their tables exist.
|
|
25
|
+
MODEL_LOADING = { false => :leave_models_unloaded }.freeze
|
|
26
|
+
|
|
27
|
+
# Statement timeout (ms) when database.statement_timeout is not set
|
|
28
|
+
DEFAULT_STATEMENT_TIMEOUT = 30_000
|
|
29
|
+
|
|
30
|
+
# Wait (ms) for a free pooled connection when database.timeout is not set
|
|
31
|
+
DEFAULT_POOL_TIMEOUT = 5_000
|
|
32
|
+
|
|
16
33
|
class << self
|
|
17
34
|
# Database connection instance
|
|
18
35
|
# @return [Sequel::Database, nil]
|
|
@@ -20,37 +37,16 @@ class HTM
|
|
|
20
37
|
|
|
21
38
|
# Establish database connection from HTM::Config
|
|
22
39
|
#
|
|
23
|
-
# @param load_models [Boolean]
|
|
24
|
-
#
|
|
40
|
+
# @param load_models [Boolean, nil] false to skip loading the models
|
|
41
|
+
# after connecting (when running migrations on a fresh database);
|
|
42
|
+
# loaded otherwise
|
|
25
43
|
# @return [Sequel::Database] The database connection
|
|
26
44
|
#
|
|
27
|
-
def establish_connection!(load_models:
|
|
45
|
+
def establish_connection!(load_models: nil)
|
|
28
46
|
return @db if @db
|
|
29
47
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
# Configure Sequel with fiber-safe settings
|
|
34
|
-
@db = Sequel.connect(connection_string, {
|
|
35
|
-
max_connections: config[:pool] || 5,
|
|
36
|
-
pool_timeout: (config[:checkout_timeout] || 5).to_i,
|
|
37
|
-
# Fiber-safe settings - important for async gem compatibility
|
|
38
|
-
preconnect: :concurrently,
|
|
39
|
-
# Use threaded mode which works well with fibers
|
|
40
|
-
single_threaded: false
|
|
41
|
-
})
|
|
42
|
-
|
|
43
|
-
# Load PostgreSQL-specific extensions for JSONB and array handling
|
|
44
|
-
@db.extension :pg_json
|
|
45
|
-
@db.extension :pg_array
|
|
46
|
-
|
|
47
|
-
# Set search path and statement timeout
|
|
48
|
-
@db.run("SET search_path TO public")
|
|
49
|
-
@db.run("SET statement_timeout = #{config[:statement_timeout] || 30_000}")
|
|
50
|
-
|
|
51
|
-
# Load models after connection is established (unless disabled for migrations)
|
|
52
|
-
require_models if load_models && models_loadable?
|
|
53
|
-
|
|
48
|
+
@db = connect(load_database_config)
|
|
49
|
+
send(MODEL_LOADING.fetch(load_models, :load_models_if_ready))
|
|
54
50
|
@db
|
|
55
51
|
end
|
|
56
52
|
|
|
@@ -59,8 +55,7 @@ class HTM
|
|
|
59
55
|
# @return [Boolean]
|
|
60
56
|
#
|
|
61
57
|
def models_loadable?
|
|
62
|
-
|
|
63
|
-
@db.table_exists?(:robots)
|
|
58
|
+
@db&.table_exists?(:robots) || false
|
|
64
59
|
rescue Sequel::DatabaseError
|
|
65
60
|
false
|
|
66
61
|
end
|
|
@@ -70,9 +65,7 @@ class HTM
|
|
|
70
65
|
# Call this after migrations to ensure models are available
|
|
71
66
|
# @return [void]
|
|
72
67
|
#
|
|
73
|
-
def ensure_models_loaded!
|
|
74
|
-
require_models unless @models_loaded
|
|
75
|
-
end
|
|
68
|
+
def ensure_models_loaded! = require_models
|
|
76
69
|
|
|
77
70
|
# Load database configuration from HTM::Config
|
|
78
71
|
#
|
|
@@ -88,24 +81,13 @@ class HTM
|
|
|
88
81
|
# @return [String] PostgreSQL connection string
|
|
89
82
|
#
|
|
90
83
|
def build_connection_string(config)
|
|
91
|
-
|
|
92
|
-
if
|
|
93
|
-
return config[:url]
|
|
94
|
-
end
|
|
84
|
+
url = config[:url]
|
|
85
|
+
return url if url
|
|
95
86
|
|
|
96
87
|
user = config[:username] || config[:user]
|
|
97
|
-
password = config[:password]
|
|
98
88
|
host = config[:host] || 'localhost'
|
|
99
89
|
port = config[:port] || 5432
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
if password && !password.empty?
|
|
103
|
-
"postgres://#{user}:#{password}@#{host}:#{port}/#{database}"
|
|
104
|
-
elsif user
|
|
105
|
-
"postgres://#{user}@#{host}:#{port}/#{database}"
|
|
106
|
-
else
|
|
107
|
-
"postgres://#{host}:#{port}/#{database}"
|
|
108
|
-
end
|
|
90
|
+
"postgres://#{userinfo(user, config[:password])}#{host}:#{port}/#{config[:database]}"
|
|
109
91
|
end
|
|
110
92
|
|
|
111
93
|
# Check if connection is established and active
|
|
@@ -113,9 +95,7 @@ class HTM
|
|
|
113
95
|
# @return [Boolean]
|
|
114
96
|
#
|
|
115
97
|
def connected?
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
@db.test_connection
|
|
98
|
+
@db&.test_connection || false
|
|
119
99
|
rescue Sequel::DatabaseError
|
|
120
100
|
false
|
|
121
101
|
end
|
|
@@ -153,20 +133,36 @@ class HTM
|
|
|
153
133
|
true
|
|
154
134
|
end
|
|
155
135
|
|
|
136
|
+
# Session settings every pooled connection runs as it opens. Sequel
|
|
137
|
+
# runs connect_sqls on each new connection, so the whole pool shares
|
|
138
|
+
# them; a SET through Database#run reaches only one connection.
|
|
139
|
+
#
|
|
140
|
+
# @param config [Hash] Database configuration (statement_timeout: in ms)
|
|
141
|
+
# @return [Array<String>] SQL statements
|
|
142
|
+
#
|
|
143
|
+
def session_sqls(config)
|
|
144
|
+
timeout = Integer(config[:statement_timeout] || DEFAULT_STATEMENT_TIMEOUT)
|
|
145
|
+
["SET search_path TO public", "SET statement_timeout = #{timeout}"]
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
# Seconds to wait for a free pooled connection
|
|
149
|
+
#
|
|
150
|
+
# @param config [Hash] Database configuration (timeout: in ms)
|
|
151
|
+
# @return [Float]
|
|
152
|
+
#
|
|
153
|
+
def pool_timeout(config) = (config[:timeout] || DEFAULT_POOL_TIMEOUT) / 1000.0
|
|
154
|
+
|
|
156
155
|
# Get connection pool statistics
|
|
157
156
|
#
|
|
158
157
|
# @return [Hash] Pool statistics
|
|
159
158
|
#
|
|
160
159
|
def connection_stats
|
|
161
160
|
pool = @db.pool
|
|
162
|
-
{
|
|
163
|
-
|
|
164
|
-
available: pool.available_connections.size,
|
|
165
|
-
allocated: pool.allocated.size
|
|
166
|
-
}
|
|
161
|
+
size = { size: pool.max_size }
|
|
162
|
+
size.merge(available: pool.available_connections.size, allocated: pool.allocated.size)
|
|
167
163
|
rescue NoMethodError
|
|
168
164
|
# Fallback for connection pools that don't support these methods
|
|
169
|
-
|
|
165
|
+
size or raise
|
|
170
166
|
end
|
|
171
167
|
|
|
172
168
|
# Run raw SQL
|
|
@@ -189,7 +185,46 @@ class HTM
|
|
|
189
185
|
|
|
190
186
|
private
|
|
191
187
|
|
|
192
|
-
#
|
|
188
|
+
# Open a connection pool with HTM's extensions and session settings
|
|
189
|
+
#
|
|
190
|
+
# @param config [Hash] Database configuration
|
|
191
|
+
# @return [Sequel::Database]
|
|
192
|
+
#
|
|
193
|
+
def connect(config)
|
|
194
|
+
db = Sequel.connect(build_connection_string(config), {
|
|
195
|
+
max_connections: config[:pool] || 5,
|
|
196
|
+
pool_timeout: pool_timeout(config),
|
|
197
|
+
connect_sqls: session_sqls(config),
|
|
198
|
+
preconnect: :concurrently,
|
|
199
|
+
single_threaded: false
|
|
200
|
+
})
|
|
201
|
+
|
|
202
|
+
# PostgreSQL-specific extensions for JSONB and array handling
|
|
203
|
+
db.extension :pg_json
|
|
204
|
+
db.extension :pg_array
|
|
205
|
+
db
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
# The userinfo part of a connection string, both parts percent-encoded
|
|
209
|
+
#
|
|
210
|
+
# @param user [String, nil]
|
|
211
|
+
# @param password [String, nil]
|
|
212
|
+
# @return [String] "user:password@", "user@" or ""
|
|
213
|
+
#
|
|
214
|
+
def userinfo(user, password)
|
|
215
|
+
user &&= URI.encode_uri_component(user)
|
|
216
|
+
return "#{user}:#{URI.encode_uri_component(password)}@" if password && !password.empty?
|
|
217
|
+
|
|
218
|
+
user ? "#{user}@" : ''
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
def load_models_if_ready
|
|
222
|
+
require_models if models_loadable?
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
def leave_models_unloaded = nil
|
|
226
|
+
|
|
227
|
+
# Require all model files (once)
|
|
193
228
|
def require_models
|
|
194
229
|
return if @models_loaded
|
|
195
230
|
|
|
@@ -199,6 +234,7 @@ class HTM
|
|
|
199
234
|
require_relative 'models/tag'
|
|
200
235
|
require_relative 'models/node_tag'
|
|
201
236
|
require_relative 'models/node_relationship'
|
|
237
|
+
require_relative 'models/node_link'
|
|
202
238
|
require_relative 'models/file_source'
|
|
203
239
|
|
|
204
240
|
@models_loaded = true
|
data/lib/htm/sql_builder.rb
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "forwardable"
|
|
4
|
+
|
|
3
5
|
class HTM
|
|
4
6
|
# SQL building utilities for constructing safe, parameterized queries
|
|
5
7
|
#
|
|
6
8
|
# Provides class methods for building SQL conditions for:
|
|
7
9
|
# - Timeframe filtering (single range or multiple ranges)
|
|
8
10
|
# - Metadata filtering (JSONB containment)
|
|
9
|
-
# - Embedding
|
|
11
|
+
# - Embedding conversion to pgvector sparsevec literals (SQL injection prevention)
|
|
10
12
|
# - LIKE pattern sanitization (wildcard injection prevention)
|
|
11
13
|
#
|
|
12
14
|
# All methods use proper escaping and parameterization to prevent SQL injection.
|
|
@@ -21,65 +23,157 @@ class HTM
|
|
|
21
23
|
#
|
|
22
24
|
# @example Sanitize an embedding
|
|
23
25
|
# HTM::SqlBuilder.sanitize_embedding([0.1, 0.2, 0.3])
|
|
24
|
-
# # => "
|
|
26
|
+
# # => "{1:0.1,2:0.2,3:0.3}/2000"
|
|
25
27
|
#
|
|
26
28
|
# @example Sanitize a LIKE pattern
|
|
27
29
|
# HTM::SqlBuilder.sanitize_like_pattern("test%pattern")
|
|
28
30
|
# # => "test\\%pattern"
|
|
29
31
|
#
|
|
30
32
|
class SqlBuilder
|
|
31
|
-
#
|
|
33
|
+
# Declared dimension of the nodes.embedding sparsevec column
|
|
32
34
|
MAX_EMBEDDING_DIMENSION = 2000
|
|
33
35
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
36
|
+
# pgvector's HNSW index on sparsevec accepts at most this many non-zero elements
|
|
37
|
+
MAX_SPARSE_NONZERO = 1000
|
|
38
|
+
|
|
39
|
+
extend SingleForwardable
|
|
40
|
+
|
|
41
|
+
# Embedding conversions are part of SqlBuilder's API; Sparsevec does the work
|
|
42
|
+
def_delegators "HTM::SqlBuilder::Sparsevec",
|
|
43
|
+
:sanitize_embedding, :parse_embedding, :finite_number?,
|
|
44
|
+
:validate_embedding_shape!, :validate_embedding_values!, :validate_embedding_sparsity!
|
|
45
|
+
|
|
46
|
+
# Conversion between dense embedding Arrays and pgvector sparsevec literals,
|
|
47
|
+
# with the validation that keeps anything but finite numbers out of the SQL
|
|
48
|
+
module Sparsevec
|
|
49
|
+
class << self
|
|
50
|
+
# Convert an embedding to a pgvector sparsevec literal
|
|
51
|
+
#
|
|
52
|
+
# Validates that all values are finite numbers, drops zeros, and writes the
|
|
53
|
+
# remaining values with 1-based positions. Only numbers reach the SQL, so
|
|
54
|
+
# the result is safe to bind or quote.
|
|
55
|
+
#
|
|
56
|
+
# @param embedding [Array<Numeric>] Dense embedding vector
|
|
57
|
+
# @param dimension [Integer] Declared sparsevec dimension
|
|
58
|
+
# @return [String] Sparsevec literal (e.g., "{1:0.1,3:0.3}/2000")
|
|
59
|
+
# @raise [ArgumentError] If the embedding is empty, too long, or not numeric
|
|
60
|
+
#
|
|
61
|
+
def sanitize_embedding(embedding, dimension: MAX_EMBEDDING_DIMENSION)
|
|
62
|
+
validate_embedding_shape!(embedding, dimension)
|
|
63
|
+
validate_embedding_values!(embedding)
|
|
64
|
+
validate_embedding_sparsity!(embedding)
|
|
65
|
+
|
|
66
|
+
"{#{entries(embedding).join(',')}}/#{dimension}"
|
|
47
67
|
end
|
|
48
68
|
|
|
49
|
-
|
|
50
|
-
|
|
69
|
+
# The "position:value" entries of a sparsevec literal: 1-based positions,
|
|
70
|
+
# zeros dropped
|
|
71
|
+
#
|
|
72
|
+
# @param embedding [Array<Numeric>] Dense embedding of finite numbers
|
|
73
|
+
# @return [Array<String>] e.g. ["1:0.1", "3:0.3"]
|
|
74
|
+
#
|
|
75
|
+
def entries(embedding)
|
|
76
|
+
embedding.each_with_index.filter_map do |value, index|
|
|
77
|
+
number = value.to_f
|
|
78
|
+
"#{index + 1}:#{number}" unless number.zero?
|
|
79
|
+
end
|
|
51
80
|
end
|
|
52
81
|
|
|
53
|
-
#
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
82
|
+
# Parse a pgvector sparsevec literal into a dense Array
|
|
83
|
+
#
|
|
84
|
+
# @param raw [String, Array, nil] Sparsevec text (e.g., "{1:0.1,3:0.3}/2000")
|
|
85
|
+
# @param length [Integer, nil] Length of the returned Array; defaults to the
|
|
86
|
+
# highest non-zero position
|
|
87
|
+
# @return [Array<Float>, nil] Dense embedding
|
|
88
|
+
#
|
|
89
|
+
def parse_embedding(raw, length: nil)
|
|
90
|
+
return nil if raw.nil?
|
|
91
|
+
return raw.map(&:to_f) if raw.is_a?(Array)
|
|
92
|
+
|
|
93
|
+
densify(pairs(raw.to_s), *length)
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# The [index, value] pairs of a sparsevec literal, with 0-based indexes
|
|
97
|
+
#
|
|
98
|
+
# @param text [String] Sparsevec text (e.g., "{1:0.1,3:0.3}/2000")
|
|
99
|
+
# @return [Array<Array(Integer, Float)>]
|
|
100
|
+
# @raise [ArgumentError] If +text+ is not a sparsevec literal
|
|
101
|
+
#
|
|
102
|
+
def pairs(text)
|
|
103
|
+
body = text[/\{([^}]*)\}/, 1] or raise ArgumentError, "Not a sparsevec literal: #{text[0, 40]}"
|
|
104
|
+
body.split(',').map do |pair|
|
|
105
|
+
index, value = pair.split(':')
|
|
106
|
+
[index.to_i - 1, value.to_f]
|
|
58
107
|
end
|
|
59
108
|
end
|
|
60
109
|
|
|
61
|
-
|
|
62
|
-
|
|
110
|
+
# @param pairs [Array<Array(Integer, Float)>] 0-based [index, value] pairs
|
|
111
|
+
# @return [Integer] Length that reaches the highest index (0 when empty)
|
|
112
|
+
#
|
|
113
|
+
def dense_length(pairs) = (pairs.map(&:first).max || -1) + 1
|
|
114
|
+
|
|
115
|
+
# +size+ zeros with +pairs+ filled in; pairs past +size+ are dropped
|
|
116
|
+
#
|
|
117
|
+
# @param pairs [Array<Array(Integer, Float)>] 0-based [index, value] pairs
|
|
118
|
+
# @param size [Integer] Length of the result (default: up to the highest index)
|
|
119
|
+
# @return [Array<Float>]
|
|
120
|
+
#
|
|
121
|
+
def densify(pairs, size = dense_length(pairs))
|
|
122
|
+
dense = Array.new(size, 0.0)
|
|
123
|
+
pairs.each { |index, value| dense[index] = value if index < size }
|
|
124
|
+
dense
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
# Raise unless +embedding+ is a non-empty Array no longer than +dimension+
|
|
128
|
+
#
|
|
129
|
+
# @param embedding [Object] Candidate embedding
|
|
130
|
+
# @param dimension [Integer] Declared sparsevec dimension
|
|
131
|
+
# @raise [ArgumentError] If the shape is wrong
|
|
132
|
+
#
|
|
133
|
+
def validate_embedding_shape!(embedding, dimension)
|
|
134
|
+
raise ArgumentError, "Embedding must be an Array, got #{embedding.class}" unless embedding.is_a?(Array)
|
|
135
|
+
raise ArgumentError, "Embedding cannot be empty" if embedding.empty?
|
|
136
|
+
size = embedding.length
|
|
137
|
+
return if size <= dimension
|
|
138
|
+
|
|
139
|
+
raise ArgumentError, "Embedding has #{size} dimensions, more than #{dimension}"
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
# Raise unless every value in +embedding+ is a finite number
|
|
143
|
+
#
|
|
144
|
+
# @param embedding [Array] Candidate embedding
|
|
145
|
+
# @raise [ArgumentError] Naming up to five offending indexes
|
|
146
|
+
#
|
|
147
|
+
def validate_embedding_values!(embedding)
|
|
148
|
+
invalid = embedding.each_with_index.reject { |value, _index| finite_number?(value) }
|
|
149
|
+
return if invalid.empty?
|
|
150
|
+
|
|
151
|
+
sample = invalid.first(5).map { |value, index| "index #{index}: #{value.inspect}" }.join(", ")
|
|
63
152
|
raise ArgumentError, "Embedding contains invalid values at #{sample}"
|
|
64
153
|
end
|
|
65
154
|
|
|
66
|
-
|
|
67
|
-
|
|
155
|
+
# Raise if +embedding+ has more non-zero values than the HNSW index allows
|
|
156
|
+
#
|
|
157
|
+
# @param embedding [Array<Numeric>] Embedding of finite numbers
|
|
158
|
+
# @raise [ArgumentError] If there are more than MAX_SPARSE_NONZERO non-zero values
|
|
159
|
+
#
|
|
160
|
+
def validate_embedding_sparsity!(embedding)
|
|
161
|
+
nonzero = embedding.count { !it.to_f.zero? }
|
|
162
|
+
return if nonzero <= MAX_SPARSE_NONZERO
|
|
68
163
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
# Pads embedding with zeros to reach the target dimension for pgvector compatibility.
|
|
72
|
-
#
|
|
73
|
-
# @param embedding [Array<Numeric>] Embedding vector
|
|
74
|
-
# @param target_dimension [Integer] Target dimension (default: MAX_EMBEDDING_DIMENSION)
|
|
75
|
-
# @return [Array<Numeric>] Padded embedding
|
|
76
|
-
#
|
|
77
|
-
def pad_embedding(embedding, target_dimension: MAX_EMBEDDING_DIMENSION)
|
|
78
|
-
return embedding if embedding.length >= target_dimension
|
|
164
|
+
raise ArgumentError, "Embedding has #{nonzero} non-zero values; the sparsevec HNSW index allows #{MAX_SPARSE_NONZERO}"
|
|
165
|
+
end
|
|
79
166
|
|
|
80
|
-
|
|
167
|
+
# @param value [Object]
|
|
168
|
+
# @return [Boolean] true if +value+ is a finite Numeric
|
|
169
|
+
#
|
|
170
|
+
def finite_number?(value)
|
|
171
|
+
value.is_a?(Numeric) && value.finite?
|
|
172
|
+
end
|
|
81
173
|
end
|
|
174
|
+
end
|
|
82
175
|
|
|
176
|
+
class << self
|
|
83
177
|
# Sanitize a string for use in SQL LIKE patterns
|
|
84
178
|
#
|
|
85
179
|
# Escapes SQL LIKE wildcards (% and _) to prevent pattern injection.
|
|
@@ -103,24 +197,25 @@ class HTM
|
|
|
103
197
|
def timeframe_condition(timeframe, table_alias: nil, column: "created_at")
|
|
104
198
|
return nil if timeframe.nil?
|
|
105
199
|
|
|
106
|
-
|
|
107
|
-
full_column = "#{prefix}#{column}"
|
|
200
|
+
full_column = [table_alias, column].compact.join(".")
|
|
108
201
|
|
|
109
202
|
case timeframe
|
|
110
203
|
when Range
|
|
111
|
-
|
|
112
|
-
end_quoted = HTM.db.literal(timeframe.end.iso8601)
|
|
113
|
-
"(#{full_column} BETWEEN #{begin_quoted} AND #{end_quoted})"
|
|
204
|
+
between_condition(full_column, timeframe)
|
|
114
205
|
when Array
|
|
115
|
-
|
|
116
|
-
begin_quoted = HTM.db.literal(range.begin.iso8601)
|
|
117
|
-
end_quoted = HTM.db.literal(range.end.iso8601)
|
|
118
|
-
"(#{full_column} BETWEEN #{begin_quoted} AND #{end_quoted})"
|
|
119
|
-
end
|
|
120
|
-
"(#{conditions.join(' OR ')})"
|
|
206
|
+
"(#{timeframe.map { between_condition(full_column, it) }.join(' OR ')})"
|
|
121
207
|
end
|
|
122
208
|
end
|
|
123
209
|
|
|
210
|
+
# @param column [String] Column name
|
|
211
|
+
# @param range [Range<Time>] Time range
|
|
212
|
+
# @return [String] "(column BETWEEN 'begin' AND 'end')" with quoted ISO 8601 times
|
|
213
|
+
#
|
|
214
|
+
def between_condition(column, range)
|
|
215
|
+
db = HTM.db
|
|
216
|
+
"(#{column} BETWEEN #{db.literal(range.begin.iso8601)} AND #{db.literal(range.end.iso8601)})"
|
|
217
|
+
end
|
|
218
|
+
|
|
124
219
|
# Apply timeframe filter to Sequel dataset
|
|
125
220
|
#
|
|
126
221
|
# @param scope [Sequel::Dataset] Base dataset
|
|
@@ -142,6 +237,23 @@ class HTM
|
|
|
142
237
|
end
|
|
143
238
|
end
|
|
144
239
|
|
|
240
|
+
# SQL for a nearest-neighbor query; binds the embedding twice, then the limit
|
|
241
|
+
#
|
|
242
|
+
# @param filters [Array<String>] extra conditions to AND into the WHERE clause
|
|
243
|
+
# @return [String]
|
|
244
|
+
#
|
|
245
|
+
def vector_search_sql(filters = [])
|
|
246
|
+
conditions = ["embedding IS NOT NULL", "deleted_at IS NULL", *filters]
|
|
247
|
+
<<~SQL
|
|
248
|
+
SELECT id, content, access_count, created_at, occurred_at, token_count,
|
|
249
|
+
1 - (embedding <=> ?::sparsevec) as similarity
|
|
250
|
+
FROM nodes
|
|
251
|
+
WHERE #{conditions.join(' AND ')}
|
|
252
|
+
ORDER BY embedding <=> ?::sparsevec
|
|
253
|
+
LIMIT ?
|
|
254
|
+
SQL
|
|
255
|
+
end
|
|
256
|
+
|
|
145
257
|
# Build SQL condition for metadata filtering (JSONB containment)
|
|
146
258
|
#
|
|
147
259
|
# @param metadata [Hash] Metadata to filter by
|
|
@@ -152,11 +264,8 @@ class HTM
|
|
|
152
264
|
def metadata_condition(metadata, table_alias: nil, column: "metadata")
|
|
153
265
|
return nil if metadata.nil? || metadata.empty?
|
|
154
266
|
|
|
155
|
-
prefix = table_alias ? "#{table_alias}." : ""
|
|
156
|
-
full_column = "#{prefix}#{column}"
|
|
157
|
-
|
|
158
267
|
quoted_metadata = HTM.db.literal(metadata.to_json)
|
|
159
|
-
"(#{
|
|
268
|
+
"(#{[table_alias, column].compact.join('.')} @> #{quoted_metadata}::jsonb)"
|
|
160
269
|
end
|
|
161
270
|
|
|
162
271
|
# Apply metadata filter to Sequel dataset
|