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,352 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class HTM
|
|
4
|
+
module Admin
|
|
5
|
+
# The `htm tags ...` commands: show, export, rebuild, clean up and
|
|
6
|
+
# repair the tag hierarchy
|
|
7
|
+
#
|
|
8
|
+
# The module functions are the task entry points and the helpers that
|
|
9
|
+
# print nothing; Console runs the commands against a given terminal.
|
|
10
|
+
module TagCommands
|
|
11
|
+
# Singular forms the skip-list rule must not pluralize back: each is a
|
|
12
|
+
# word of its own (async-http is not a mangled async-https)
|
|
13
|
+
KEEP_SINGULAR = %w[http io new].freeze
|
|
14
|
+
|
|
15
|
+
module_function
|
|
16
|
+
|
|
17
|
+
# Print the tag tree (see Console#tree)
|
|
18
|
+
def tree(prefix = nil) = Console.new.tree(prefix)
|
|
19
|
+
|
|
20
|
+
# Write the tags as a Mermaid flowchart to tags.md (see Console#mermaid)
|
|
21
|
+
def mermaid(prefix = nil) = Console.new.mermaid(prefix)
|
|
22
|
+
|
|
23
|
+
# Write the tags as an SVG diagram to tags.svg (see Console#svg)
|
|
24
|
+
def svg(prefix = nil) = Console.new.svg(prefix)
|
|
25
|
+
|
|
26
|
+
# Write tags.txt, tags.md and tags.svg (see Console#export)
|
|
27
|
+
def export(prefix = nil) = Console.new.export(prefix)
|
|
28
|
+
|
|
29
|
+
# Delete every tag and extract them again for every node, after confirmation (see Console#rebuild)
|
|
30
|
+
def rebuild = Console.new.rebuild
|
|
31
|
+
|
|
32
|
+
# List, or with +apply+ rename, the tags the old normalizer mangled (see Console#restore_plurals)
|
|
33
|
+
def restore_plurals(prefix = nil, apply: nil) = Console.new.restore_plurals(prefix, apply:)
|
|
34
|
+
|
|
35
|
+
# Soft delete stale node_tags rows and orphaned tags, after confirmation (see Console#cleanup)
|
|
36
|
+
def cleanup = Console.new.cleanup
|
|
37
|
+
|
|
38
|
+
# @return [Sequel::Dataset] tags under +prefix+, or every tag when nil
|
|
39
|
+
def scope(prefix = nil) = prefix ? HTM::Models::Tag.with_prefix(prefix) : HTM::Models::Tag.dataset
|
|
40
|
+
|
|
41
|
+
# @return [String] " (prefix: x)" or ""
|
|
42
|
+
def prefix_note(prefix) = prefix ? " (prefix: #{prefix})" : ''
|
|
43
|
+
|
|
44
|
+
# @return [String] the title for an SVG of +prefix+'s tags
|
|
45
|
+
def title(prefix) = prefix ? "HTM Tags: #{prefix}*" : 'HTM Tag Hierarchy'
|
|
46
|
+
|
|
47
|
+
# @return [String] the tag tree as text, with a heading and a total
|
|
48
|
+
def tree_text(tags, prefix, count)
|
|
49
|
+
"HTM Tags Tree#{prefix_note(prefix)}\n#{'=' * 40}\n#{HTM::Models::Tag.tree_string(tags)}\nTotal tags: #{count}\n"
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# The files export writes
|
|
53
|
+
#
|
|
54
|
+
# @return [Hash{String => Array(String, String)}] file name => [content, label]
|
|
55
|
+
#
|
|
56
|
+
def export_files(tags, prefix, count)
|
|
57
|
+
{
|
|
58
|
+
'tags.txt' => [tree_text(tags, prefix, count), "Text tree"],
|
|
59
|
+
'tags.md' => [HTM::Models::Tag.tree_mermaid(dataset: tags), "Mermaid flowchart"],
|
|
60
|
+
'tags.svg' => [HTM::Models::Tag.tree_svg(title: title(prefix), dataset: tags), "SVG visualization"]
|
|
61
|
+
}
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# What rebuild will do, for the confirmation prompt
|
|
65
|
+
#
|
|
66
|
+
# @return [Array<String>]
|
|
67
|
+
#
|
|
68
|
+
def rebuild_plan(node_count)
|
|
69
|
+
[
|
|
70
|
+
"This will:",
|
|
71
|
+
" - Delete #{HTM::Models::NodeTag.count} node-tag associations",
|
|
72
|
+
" - Delete #{HTM::Models::Tag.count} tags",
|
|
73
|
+
" - Regenerate tags for #{node_count} nodes using LLM",
|
|
74
|
+
"\nThis operation cannot be undone."
|
|
75
|
+
]
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# Delete every node-tag association and every tag
|
|
79
|
+
#
|
|
80
|
+
# @return [Array(Integer, Integer)] node_tags and tags rows deleted
|
|
81
|
+
#
|
|
82
|
+
def clear_tags = [HTM::Models::NodeTag.dataset.delete, HTM::Models::Tag.dataset.delete]
|
|
83
|
+
|
|
84
|
+
# The name +name+ had before the old normalizer trimmed a trailing "s"
|
|
85
|
+
# from a level that is a name or a fixed plural. A level is restored
|
|
86
|
+
# when its plural is:
|
|
87
|
+
# - a name the tag's own nodes spell as one (AlchemyCMS, state_machines)
|
|
88
|
+
# - something with an ending no plural has (cms, js, aas)
|
|
89
|
+
# - a skip-list word as the last part of a hyphenated level
|
|
90
|
+
# (ruby-on-rails, civil-rights), or an OS or *ops name (chromeos, mlops)
|
|
91
|
+
#
|
|
92
|
+
# @param name [String] tag name
|
|
93
|
+
# @param contents [Array<String>] content of the nodes carrying the tag
|
|
94
|
+
# @return [String] the restored name, or +name+ when nothing was trimmed
|
|
95
|
+
#
|
|
96
|
+
def restored_plural_name(name, contents)
|
|
97
|
+
keep = contents.flat_map { HTM::TagService.proper_plurals(it) }.uniq
|
|
98
|
+
name.split(':').map { restored_plural_level(it, keep) }.join(':')
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# @return [String] +level+ with its "s" back, or +level+ unchanged
|
|
102
|
+
def restored_plural_level(level, keep)
|
|
103
|
+
plural = "#{level}s"
|
|
104
|
+
return level if HTM::TagService.singularize_level(plural, keep:) != plural
|
|
105
|
+
|
|
106
|
+
restore = keep.include?(plural.delete('-')) ||
|
|
107
|
+
(plural.end_with?(*HTM::TagService::NEVER_PLURAL_SUFFIXES) &&
|
|
108
|
+
!level.end_with?(*HTM::TagService::NEVER_PLURAL_SUFFIXES)) ||
|
|
109
|
+
skip_list_plural?(level) ||
|
|
110
|
+
plural.end_with?('sis') # necropsi -> necropsis
|
|
111
|
+
restore ? plural : level
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
# @return [Boolean] whether +level+ is a skip-list word with its "s" trimmed
|
|
115
|
+
def skip_list_plural?(level)
|
|
116
|
+
last = level.split('-').last
|
|
117
|
+
return false if KEEP_SINGULAR.include?(last)
|
|
118
|
+
return false unless HTM::TagService::SINGULARIZE_SKIP_LIST.include?("#{last}s")
|
|
119
|
+
|
|
120
|
+
level.include?('-') || last.end_with?('o', 'op')
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# Every active tag whose name the old normalizer mangled, shortest
|
|
124
|
+
# first so a parent is renamed (taking its subtree) before its children
|
|
125
|
+
#
|
|
126
|
+
# @param prefix [String, nil] only tags under this prefix
|
|
127
|
+
# @return [Array<Array(String, String)>] [old name, restored name] pairs
|
|
128
|
+
#
|
|
129
|
+
def plural_repairs(prefix = nil)
|
|
130
|
+
repairs = scope(prefix).eager(:nodes).all.filter_map do |tag|
|
|
131
|
+
name = tag.name
|
|
132
|
+
restored = restored_plural_name(name, tag.nodes.map(&:content))
|
|
133
|
+
[name, restored] unless restored == name
|
|
134
|
+
end
|
|
135
|
+
repairs.sort_by { |from, _| [from.length, from] }
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
# Rename tag +from+ (subtree included) to +into+, merging into it if it
|
|
139
|
+
# already exists. A rename is a soft delete plus relinks, as merge_tags.
|
|
140
|
+
#
|
|
141
|
+
# @return [Boolean] whether +from+ was renamed; false when it is gone
|
|
142
|
+
# (moved already with its parent)
|
|
143
|
+
#
|
|
144
|
+
def restore_plural(from, into)
|
|
145
|
+
return false unless HTM::Models::Tag.where(name: from).any?
|
|
146
|
+
|
|
147
|
+
HTM::Robots::Actions.merge_tags(from:, into:)
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
# @return [Sequel::Dataset] active node_tags rows whose node is soft-deleted or missing
|
|
151
|
+
def stale_node_tags
|
|
152
|
+
HTM::Models::NodeTag.left_join(:nodes, id: :node_id).where(Sequel.lit("nodes.id IS NULL OR nodes.deleted_at IS NOT NULL"))
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
# Soft delete the rows of each dataset of +targets+ that +counts+ says has any
|
|
156
|
+
#
|
|
157
|
+
# @param targets [Hash{Symbol => Sequel::Dataset}]
|
|
158
|
+
# @param counts [Hash{Symbol => Integer}] rows per target
|
|
159
|
+
# @param now [Time] the deleted_at stamp
|
|
160
|
+
# @return [Hash{Symbol => Integer}] rows soft-deleted per target
|
|
161
|
+
#
|
|
162
|
+
def soft_delete_all(targets, counts, now)
|
|
163
|
+
targets.to_h { |kind, dataset| [kind, counts[kind].positive? ? dataset.update(deleted_at: now) : 0] }
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
# The `htm tags ...` commands, reporting to +out+
|
|
167
|
+
#
|
|
168
|
+
# @example
|
|
169
|
+
# HTM::Admin::TagCommands::Console.new(out: StringIO.new).tree("ai")
|
|
170
|
+
#
|
|
171
|
+
class Console < Admin::Console
|
|
172
|
+
# restore_plurals' step by its apply: flag; anything else lists the repairs only
|
|
173
|
+
RESTORE_STEPS = { true => :apply_repairs }.freeze
|
|
174
|
+
|
|
175
|
+
# Print the tag tree
|
|
176
|
+
#
|
|
177
|
+
# @return [Integer, nil] tags printed, nil when there are none
|
|
178
|
+
#
|
|
179
|
+
def tree(prefix = nil)
|
|
180
|
+
with_tags(prefix) do |tags, count|
|
|
181
|
+
out.print "\n#{TagCommands.tree_text(tags, prefix, count)}"
|
|
182
|
+
count
|
|
183
|
+
end
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
# Write the tags as a Mermaid flowchart to +path+
|
|
187
|
+
def mermaid(prefix = nil, path: 'tags.md')
|
|
188
|
+
with_tags(prefix) do |tags, count|
|
|
189
|
+
File.write(path, HTM::Models::Tag.tree_mermaid(dataset: tags))
|
|
190
|
+
out.puts "Mermaid flowchart written to: #{path}"
|
|
191
|
+
out.puts "Tags exported: #{count}#{TagCommands.prefix_note(prefix)}"
|
|
192
|
+
path
|
|
193
|
+
end
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
# Write the tags as an SVG diagram to +path+
|
|
197
|
+
def svg(prefix = nil, path: 'tags.svg')
|
|
198
|
+
with_tags(prefix) do |tags, count|
|
|
199
|
+
File.write(path, HTM::Models::Tag.tree_svg(title: TagCommands.title(prefix), dataset: tags))
|
|
200
|
+
out.puts "SVG visualization written to: #{path}"
|
|
201
|
+
out.puts "Tags exported: #{count}#{TagCommands.prefix_note(prefix)}"
|
|
202
|
+
path
|
|
203
|
+
end
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
# Write tags.txt, tags.md and tags.svg into +dir+
|
|
207
|
+
#
|
|
208
|
+
# @return [Array<String>, nil] the files written
|
|
209
|
+
#
|
|
210
|
+
def export(prefix = nil, dir: '.')
|
|
211
|
+
with_tags(prefix) do |tags, count|
|
|
212
|
+
files = write_files(TagCommands.export_files(tags, prefix, count), dir)
|
|
213
|
+
out.puts "\nTags exported: #{count}#{TagCommands.prefix_note(prefix)}"
|
|
214
|
+
files
|
|
215
|
+
end
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
# Delete every tag and extract them again for every node, after confirmation
|
|
219
|
+
#
|
|
220
|
+
# @return [Hash, nil] { ok:, failed: }, nil when aborted
|
|
221
|
+
#
|
|
222
|
+
def rebuild
|
|
223
|
+
Admin.connect!
|
|
224
|
+
node_count = HTM::Models::Node.count
|
|
225
|
+
heading("HTM Tags Rebuild", 40)
|
|
226
|
+
out.puts TagCommands.rebuild_plan(node_count)
|
|
227
|
+
return aborted unless proceed?
|
|
228
|
+
|
|
229
|
+
out.puts "\nClearing existing tags..."
|
|
230
|
+
node_tags, tags = TagCommands.clear_tags
|
|
231
|
+
out.puts " Deleted #{node_tags} node-tag associations", " Deleted #{tags} tags"
|
|
232
|
+
regenerate_tags(node_count)
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
# List the tags the old normalizer mangled; with +apply+, rename each
|
|
236
|
+
# (subtree included) to its restored name, merging into it if it
|
|
237
|
+
# already exists. Renames are soft deletes plus relinks, as merge_tags.
|
|
238
|
+
#
|
|
239
|
+
# @param prefix [String, nil] only tags under this prefix
|
|
240
|
+
# @param apply [Boolean, nil] rename the tags instead of only listing them
|
|
241
|
+
# @return [Array<Array(String, String)>] the repairs found
|
|
242
|
+
#
|
|
243
|
+
def restore_plurals(prefix = nil, apply: nil)
|
|
244
|
+
Admin.connect!
|
|
245
|
+
repairs = TagCommands.plural_repairs(prefix)
|
|
246
|
+
if repairs.empty?
|
|
247
|
+
out.puts "No mangled tag names found."
|
|
248
|
+
return repairs
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
repairs.each { |from, into| out.puts " #{from} -> #{into}" }
|
|
252
|
+
send(RESTORE_STEPS.fetch(apply, :suggest_apply), repairs)
|
|
253
|
+
repairs
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
# Soft delete stale node_tags rows and orphaned tags, after confirmation
|
|
257
|
+
#
|
|
258
|
+
# @return [Hash, nil] { node_tags:, tags: } soft-deleted, nil when nothing was done
|
|
259
|
+
#
|
|
260
|
+
def cleanup(now: Time.now)
|
|
261
|
+
Admin.connect!
|
|
262
|
+
heading("HTM Tag Cleanup")
|
|
263
|
+
targets = { node_tags: TagCommands.stale_node_tags, tags: HTM::Models::Tag.orphaned }
|
|
264
|
+
counts = targets.transform_values(&:count)
|
|
265
|
+
print_cleanup_counts(counts, targets[:tags])
|
|
266
|
+
return if counts.values.sum.zero?
|
|
267
|
+
return cancelled unless confirmed?("\nSoft delete these entries? (yes/no): ")
|
|
268
|
+
|
|
269
|
+
result = TagCommands.soft_delete_all(targets, counts, now)
|
|
270
|
+
out.puts "\nSoft deleted #{result[:node_tags]} stale node_tags entries.",
|
|
271
|
+
"Soft deleted #{result[:tags]} orphaned tags.",
|
|
272
|
+
"\nCleanup complete (soft delete)."
|
|
273
|
+
result
|
|
274
|
+
end
|
|
275
|
+
|
|
276
|
+
private
|
|
277
|
+
|
|
278
|
+
# Yield the tags under +prefix+ and their count, or report that there are none
|
|
279
|
+
#
|
|
280
|
+
# @return [Object, nil] the block's value, nil when there are no tags
|
|
281
|
+
#
|
|
282
|
+
def with_tags(prefix)
|
|
283
|
+
Admin.connect!
|
|
284
|
+
tags = TagCommands.scope(prefix)
|
|
285
|
+
count = tags.count
|
|
286
|
+
if count.zero?
|
|
287
|
+
out.puts prefix ? "No tags found with prefix '#{prefix}'." : "No tags found in database."
|
|
288
|
+
return
|
|
289
|
+
end
|
|
290
|
+
|
|
291
|
+
yield tags, count
|
|
292
|
+
end
|
|
293
|
+
|
|
294
|
+
# Write each of +files+ (name => [content, label]) into +dir+
|
|
295
|
+
#
|
|
296
|
+
# @return [Array<String>] the paths written
|
|
297
|
+
#
|
|
298
|
+
def write_files(files, dir)
|
|
299
|
+
files.map do |name, (content, label)|
|
|
300
|
+
path = File.join(dir, name)
|
|
301
|
+
File.write(path, content)
|
|
302
|
+
out.puts "#{label} written to: #{path}"
|
|
303
|
+
path
|
|
304
|
+
end
|
|
305
|
+
end
|
|
306
|
+
|
|
307
|
+
# @return [Hash] { ok:, failed: }
|
|
308
|
+
def regenerate_tags(node_count)
|
|
309
|
+
out.puts "\nRegenerating tags for #{node_count} nodes..."
|
|
310
|
+
counts = each_with_progress(HTM::Models::Node.dataset, 'Processing') do |node, _bar|
|
|
311
|
+
HTM::Jobs::GenerateTagsJob.perform(node_id: node.id)
|
|
312
|
+
end
|
|
313
|
+
print_counts("\nRebuild complete!", {
|
|
314
|
+
'Nodes processed' => node_count,
|
|
315
|
+
'Errors' => counts[:failed],
|
|
316
|
+
'Tags created' => HTM::Models::Tag.count,
|
|
317
|
+
'Node-tag associations' => HTM::Models::NodeTag.count
|
|
318
|
+
})
|
|
319
|
+
counts
|
|
320
|
+
end
|
|
321
|
+
|
|
322
|
+
def apply_repairs(repairs)
|
|
323
|
+
applied = repairs.count { |from, into| TagCommands.restore_plural(from, into) }
|
|
324
|
+
out.puts "\nRestored #{applied} tags (children moved with their parents)."
|
|
325
|
+
end
|
|
326
|
+
|
|
327
|
+
def suggest_apply(repairs)
|
|
328
|
+
out.puts "\n#{repairs.size} tags to restore. Run again with --apply to rename them."
|
|
329
|
+
end
|
|
330
|
+
|
|
331
|
+
def print_cleanup_counts(counts, orphaned)
|
|
332
|
+
stale_count, orphan_count = counts.values_at(:node_tags, :tags)
|
|
333
|
+
if (stale_count + orphan_count).zero?
|
|
334
|
+
out.puts "No cleanup needed.", " Stale node_tags entries: 0", " Orphaned tags: 0"
|
|
335
|
+
return
|
|
336
|
+
end
|
|
337
|
+
|
|
338
|
+
out.puts "Found:",
|
|
339
|
+
" Stale node_tags entries: #{stale_count} (pointing to deleted/missing nodes)",
|
|
340
|
+
" Orphaned tags: #{orphan_count} (no active nodes)"
|
|
341
|
+
print_orphaned(orphaned, orphan_count) if orphan_count.positive?
|
|
342
|
+
end
|
|
343
|
+
|
|
344
|
+
def print_orphaned(orphaned, count)
|
|
345
|
+
out.puts "\nOrphaned tags:"
|
|
346
|
+
orphaned.limit(20).select_map(:name).each { out.puts " - #{it}" }
|
|
347
|
+
out.puts " ... and #{count - 20} more" if count > 20
|
|
348
|
+
end
|
|
349
|
+
end
|
|
350
|
+
end
|
|
351
|
+
end
|
|
352
|
+
end
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'cgi'
|
|
4
|
+
require 'json'
|
|
5
|
+
|
|
6
|
+
class HTM
|
|
7
|
+
module Admin
|
|
8
|
+
# `htm tags graph`: write an interactive force-directed graph of the
|
|
9
|
+
# nodes under a tag prefix and the tag co-occurrence links between them
|
|
10
|
+
# (node_relationships, written by GenerateRelationshipsJob) as one HTML
|
|
11
|
+
# file. The data and d3 are embedded, so the file opens from disk.
|
|
12
|
+
#
|
|
13
|
+
# Reading the database (graph_data) is kept apart from shaping the data
|
|
14
|
+
# (build, branch_of, source_of, source_info) and writing the page
|
|
15
|
+
# (render), so each can be tested on its own.
|
|
16
|
+
#
|
|
17
|
+
# @example
|
|
18
|
+
# HTM::Admin::TagGraph.write("medicine", path: "medicine.html")
|
|
19
|
+
#
|
|
20
|
+
module TagGraph
|
|
21
|
+
TEMPLATE = File.expand_path("templates/tag_graph.html", __dir__)
|
|
22
|
+
DEFAULT_PATH = 'tags_graph.html'
|
|
23
|
+
D3_URL = 'https://cdnjs.cloudflare.com/ajax/libs/d3/7.9.0/d3.min.js'
|
|
24
|
+
CONTENT_CHARS = 600
|
|
25
|
+
FALLBACK_BRANCH = 'general'
|
|
26
|
+
|
|
27
|
+
module_function
|
|
28
|
+
|
|
29
|
+
# Write the graph page for the nodes under +prefix+ (every tagged node when nil)
|
|
30
|
+
#
|
|
31
|
+
# @param prefix [String, nil] Tag prefix, e.g. "medicine"
|
|
32
|
+
# @param path [String] Output file
|
|
33
|
+
# @param title [String, nil] Page title; defaults to one naming the prefix
|
|
34
|
+
# @param d3_js [String, nil, Symbol] d3 source to embed; :fetch downloads it,
|
|
35
|
+
# nil links the CDN copy instead (the page then needs a connection)
|
|
36
|
+
# @param out [IO]
|
|
37
|
+
# @return [String, nil] the path written, nil when no node matched
|
|
38
|
+
#
|
|
39
|
+
def write(prefix = nil, path: DEFAULT_PATH, title: nil, d3_js: :fetch, out: $stdout)
|
|
40
|
+
Admin.connect!
|
|
41
|
+
data = build(graph_data(prefix), prefix:)
|
|
42
|
+
return Admin.say(out, prefix ? "No nodes tagged under '#{prefix}'." : "No tagged nodes found.") if data[:nodes].empty?
|
|
43
|
+
|
|
44
|
+
title ||= default_title(prefix)
|
|
45
|
+
d3_js = download_d3_library(out:) if d3_js == :fetch
|
|
46
|
+
File.write(path, render(data, title:, database: database_name, d3_js:))
|
|
47
|
+
out.puts written_summary(path, data)
|
|
48
|
+
path
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# @return [Array<String>] the lines write reports once +data+ is written to +path+
|
|
52
|
+
def written_summary(path, data)
|
|
53
|
+
["Tag graph written to: #{path}",
|
|
54
|
+
"Nodes: #{data[:nodes].size}, links: #{data[:edges].size}, sources: #{data[:sources].size}"]
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# @return [String] "HTM memory graph: medicine" or "HTM memory graph"
|
|
58
|
+
def default_title(prefix) = prefix ? "HTM memory graph: #{prefix}" : 'HTM memory graph'
|
|
59
|
+
|
|
60
|
+
# @return [String] the connected database's name
|
|
61
|
+
def database_name = HTM.db.get(Sequel.function(:current_database)).to_s
|
|
62
|
+
|
|
63
|
+
# Download d3 so the page works offline
|
|
64
|
+
#
|
|
65
|
+
# @return [String, nil] the library source, nil when it cannot be fetched
|
|
66
|
+
#
|
|
67
|
+
def download_d3_library(out: $stdout)
|
|
68
|
+
require 'net/http'
|
|
69
|
+
response = Net::HTTP.get_response(URI(D3_URL))
|
|
70
|
+
return response.body if response.is_a?(Net::HTTPSuccess)
|
|
71
|
+
|
|
72
|
+
out.puts "Could not download d3 (HTTP #{response.code}); the page will load it from the CDN."
|
|
73
|
+
nil
|
|
74
|
+
rescue StandardError => e
|
|
75
|
+
out.puts "Could not download d3 (#{e.message}); the page will load it from the CDN."
|
|
76
|
+
nil
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# Read the nodes under +prefix+, their tags, their sources and the links between them
|
|
80
|
+
#
|
|
81
|
+
# @return [Hash] { nodes:, tags:, edges:, sources:, parents: } as plain rows
|
|
82
|
+
#
|
|
83
|
+
def graph_data(prefix)
|
|
84
|
+
db = HTM.db
|
|
85
|
+
node_ids = db[:node_tags].where(deleted_at: nil, tag_id: tag_scope(prefix).select(:id)).select(:node_id)
|
|
86
|
+
nodes = db[:nodes].where(id: node_ids, deleted_at: nil).order(:id)
|
|
87
|
+
.select(:id, :content, :epistemic_status, :source_id).all
|
|
88
|
+
ids = nodes.map { it[:id] }
|
|
89
|
+
|
|
90
|
+
{
|
|
91
|
+
nodes:,
|
|
92
|
+
tags: node_tags(ids),
|
|
93
|
+
edges: db[:node_relationships].where(source_id: ids, target_id: ids)
|
|
94
|
+
.where(Sequel[:source_id] < Sequel[:target_id])
|
|
95
|
+
.select_map(%i[source_id target_id weight]),
|
|
96
|
+
parents: parent_rows(nodes),
|
|
97
|
+
sources: db[:file_sources].select(:id, :file_path, :frontmatter).all
|
|
98
|
+
}
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# @return [Sequel::Dataset] live tags named +prefix+ or under it (all live tags when nil)
|
|
102
|
+
def tag_scope(prefix)
|
|
103
|
+
tags = HTM.db[:tags].where(deleted_at: nil)
|
|
104
|
+
return tags unless prefix
|
|
105
|
+
|
|
106
|
+
tags.where(Sequel.|({ name: prefix }, Sequel.like(:name, "#{HTM::SqlBuilder.sanitize_like_pattern(prefix)}:%")))
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# @return [Hash{Integer => Array<String>}] live tag names per node id
|
|
110
|
+
def node_tags(ids)
|
|
111
|
+
links = Sequel[:node_tags]
|
|
112
|
+
tags = Sequel[:tags]
|
|
113
|
+
node_id = links[:node_id]
|
|
114
|
+
pairs = HTM.db[:node_tags].join(:tags, id: :tag_id)
|
|
115
|
+
.where(node_id => ids, links[:deleted_at] => nil, tags[:deleted_at] => nil)
|
|
116
|
+
.select_map([node_id, tags[:name]])
|
|
117
|
+
pairs.group_by(&:first).transform_values { |rows| rows.map(&:last).sort }
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
# The source_id and parent of each node a proposition descends from
|
|
121
|
+
# that is not itself in the graph, so propositions find their file
|
|
122
|
+
#
|
|
123
|
+
# @param nodes [Array<Hash>] rows with :id, :source_id
|
|
124
|
+
# @param max_depth [Integer] generations followed; proposition chains
|
|
125
|
+
# are one level deep, so the cap only guards against cycles
|
|
126
|
+
# @return [Hash{Integer => Hash}] id => { source_id:, parent: }
|
|
127
|
+
#
|
|
128
|
+
def parent_rows(nodes, max_depth: 5)
|
|
129
|
+
known = lineage(nodes)
|
|
130
|
+
wanted = missing_parents(known.values, known)
|
|
131
|
+
|
|
132
|
+
max_depth.times do
|
|
133
|
+
break if wanted.empty?
|
|
134
|
+
|
|
135
|
+
found = lineage(HTM.db[:nodes].where(id: wanted).select(:id, :source_id).all)
|
|
136
|
+
known.merge!(found)
|
|
137
|
+
wanted = missing_parents(found.values, known)
|
|
138
|
+
end
|
|
139
|
+
known
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
# @param rows [Array<Hash>] node rows with :id, :source_id
|
|
143
|
+
# @return [Hash{Integer => Hash}] id => { source_id:, parent: } for each row
|
|
144
|
+
def lineage(rows)
|
|
145
|
+
parents = source_node_ids(rows.map { it[:id] })
|
|
146
|
+
rows.to_h do |row|
|
|
147
|
+
id = row[:id]
|
|
148
|
+
[id, { source_id: row[:source_id], parent: parents[id] }]
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
# @param links [Array<Hash>] { source_id:, parent: } values of lineage
|
|
153
|
+
# @param known [Hash{Integer => Hash}] the lineage read so far
|
|
154
|
+
# @return [Array<Integer>] the parents +links+ name that +known+ lacks
|
|
155
|
+
def missing_parents(links, known) = links.filter_map { it[:parent] }.uniq - known.keys
|
|
156
|
+
|
|
157
|
+
# @param ids [Array<Integer>] node ids
|
|
158
|
+
# @return [Hash{Integer => Integer}] node id => the node it was derived from
|
|
159
|
+
def source_node_ids(ids)
|
|
160
|
+
HTM::Models::NodeLink.of_kind(:derived_from).where(from_node_id: ids).select_hash(:from_node_id, :to_node_id)
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
# Shape the rows graph_data reads into the page's data
|
|
164
|
+
#
|
|
165
|
+
# @param rows [Hash] graph_data's rows:
|
|
166
|
+
# - nodes [Array<Hash>] rows with :id, :content, :source_id
|
|
167
|
+
# - tags [Hash{Integer => Array<String>}]
|
|
168
|
+
# - edges [Array<Array>] [source_id, target_id, weight]
|
|
169
|
+
# - parents [Hash{Integer => Hash}] see parent_rows
|
|
170
|
+
# - sources [Array<Hash>] file_sources rows
|
|
171
|
+
# @param prefix [String, nil]
|
|
172
|
+
# @return [Hash] { meta:, branches:, sources:, nodes:, edges: }
|
|
173
|
+
#
|
|
174
|
+
def build(rows, prefix: nil)
|
|
175
|
+
source_rows = rows[:sources].to_h { [it[:id], it] }
|
|
176
|
+
graph_nodes = rows[:nodes].map { graph_node(it, rows, source_rows, prefix) }
|
|
177
|
+
|
|
178
|
+
{
|
|
179
|
+
meta: { prefix: },
|
|
180
|
+
branches: ranked(graph_nodes.map { it[:b] }) - [FALLBACK_BRANCH],
|
|
181
|
+
sources: ranked(graph_nodes.filter_map { it[:s] }).map { source_info(source_rows.fetch(it.to_i)) },
|
|
182
|
+
nodes: graph_nodes,
|
|
183
|
+
edges: rows[:edges].map { |from, to, weight| [from, to, weight.to_f.round(3)] }
|
|
184
|
+
}
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
# One node as the page draws it
|
|
188
|
+
#
|
|
189
|
+
# @param row [Hash] nodes row with :id, :content, :epistemic_status, :source_id
|
|
190
|
+
# @param rows [Hash] graph_data's rows (for :tags and :parents)
|
|
191
|
+
# @param source_rows [Hash{Integer => Hash}] file_sources rows by id
|
|
192
|
+
# @param prefix [String, nil]
|
|
193
|
+
# @return [Hash] { id:, c: content, p: proposition?, s: source id, b: branch, t: tags }
|
|
194
|
+
#
|
|
195
|
+
def graph_node(row, rows, source_rows, prefix)
|
|
196
|
+
id = row[:id]
|
|
197
|
+
tags = rows[:tags].fetch(id, [])
|
|
198
|
+
source = source_of(id, rows[:parents])
|
|
199
|
+
{
|
|
200
|
+
id:, c: row[:content].to_s[0, CONTENT_CHARS], p: proposition?(row),
|
|
201
|
+
s: source_rows.key?(source) ? source.to_s : nil, b: branch_of(tags, prefix), t: tags
|
|
202
|
+
}
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
# @return [Array<String>] the distinct values, most frequent first (ties by name)
|
|
206
|
+
def ranked(values) = values.tally.sort_by { |value, count| [-count, value] }.map(&:first)
|
|
207
|
+
|
|
208
|
+
# @return [Boolean] whether a nodes row is a proposition (extracted, not a file chunk)
|
|
209
|
+
def proposition?(row) = row[:epistemic_status] == 'extracted' && row[:source_id].nil?
|
|
210
|
+
|
|
211
|
+
# The branch a node sits in: the level just below +prefix+ (the root
|
|
212
|
+
# level when nil) that most of its tags pass through
|
|
213
|
+
#
|
|
214
|
+
# @example
|
|
215
|
+
# branch_of(%w[medicine medicine:genetics medicine:genetics:gata1], "medicine") # => "genetics"
|
|
216
|
+
#
|
|
217
|
+
# @param tags [Array<String>]
|
|
218
|
+
# @param prefix [String, nil]
|
|
219
|
+
# @return [String] the branch, or "general" when no tag goes below +prefix+
|
|
220
|
+
#
|
|
221
|
+
def branch_of(tags, prefix)
|
|
222
|
+
depth = prefix ? prefix.count(':') + 1 : 0
|
|
223
|
+
under = prefix ? tags.select { it.start_with?("#{prefix}:") } : tags
|
|
224
|
+
levels = under.filter_map { it.split(':')[depth] }
|
|
225
|
+
levels.empty? ? FALLBACK_BRANCH : ranked(levels).first
|
|
226
|
+
end
|
|
227
|
+
|
|
228
|
+
# The file a node came from: its own source, or the source of the
|
|
229
|
+
# node it was extracted from (propositions)
|
|
230
|
+
#
|
|
231
|
+
# @param id [Integer]
|
|
232
|
+
# @param parents [Hash{Integer => Hash}] id => { source_id:, parent: }
|
|
233
|
+
# @return [Integer, nil] file_sources id
|
|
234
|
+
#
|
|
235
|
+
def source_of(id, parents)
|
|
236
|
+
seen = []
|
|
237
|
+
while (row = parents[id]) && !seen.include?(id)
|
|
238
|
+
source = row[:source_id]
|
|
239
|
+
return source if source
|
|
240
|
+
|
|
241
|
+
seen << id
|
|
242
|
+
id = row[:parent]
|
|
243
|
+
end
|
|
244
|
+
nil
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
# How the page names and links a file source, from its frontmatter
|
|
248
|
+
#
|
|
249
|
+
# @param row [Hash] file_sources row with :id, :file_path, :frontmatter
|
|
250
|
+
# @return [Hash] { id:, title:, url:, detail: }
|
|
251
|
+
#
|
|
252
|
+
def source_info(row)
|
|
253
|
+
fm = (row[:frontmatter] || {}).to_h.transform_keys(&:to_s)
|
|
254
|
+
doi = fm['doi']
|
|
255
|
+
{
|
|
256
|
+
id: row[:id].to_s,
|
|
257
|
+
title: fm['title'].to_s.strip.delete_suffix('.').then { it.empty? ? File.basename(row[:file_path].to_s) : it },
|
|
258
|
+
url: fm['url'] || (doi && "https://doi.org/#{doi}"),
|
|
259
|
+
detail: [fm['journal'] || fm['source'], fm['year'] || fm['date']].compact.join(' ')
|
|
260
|
+
}
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
# Fill the page template
|
|
264
|
+
#
|
|
265
|
+
# @param data [Hash] from build
|
|
266
|
+
# @param title [String]
|
|
267
|
+
# @param database [String]
|
|
268
|
+
# @param d3_js [String, nil] d3 source to embed; nil links the CDN copy
|
|
269
|
+
# @param generated [Time]
|
|
270
|
+
# @return [String] the HTML page
|
|
271
|
+
#
|
|
272
|
+
def render(data, title:, database:, d3_js: nil, generated: Time.now)
|
|
273
|
+
d3_tag = if d3_js
|
|
274
|
+
# Net::HTTP hands back a binary string; the library is UTF-8 text
|
|
275
|
+
"<script>#{d3_js.dup.force_encoding(Encoding::UTF_8).gsub('</script', '<\/script')}</script>"
|
|
276
|
+
else
|
|
277
|
+
%(<script src="#{D3_URL}"></script>)
|
|
278
|
+
end
|
|
279
|
+
values = {
|
|
280
|
+
'TITLE' => CGI.escapeHTML(title),
|
|
281
|
+
'DATABASE' => CGI.escapeHTML(database),
|
|
282
|
+
'GENERATED' => generated.strftime('%-d %b %Y %H:%M'),
|
|
283
|
+
'DATA' => JSON.generate(data).gsub('</', '<\/'),
|
|
284
|
+
'D3' => d3_tag
|
|
285
|
+
}
|
|
286
|
+
File.read(TEMPLATE).gsub(/\{\{(TITLE|DATABASE|GENERATED|DATA|D3)\}\}/) { values.fetch(Regexp.last_match(1)) }
|
|
287
|
+
end
|
|
288
|
+
end
|
|
289
|
+
end
|
|
290
|
+
end
|