#!/bin/bash # BlackRoad Memory Indexer System # Creates searchable indexes for ultra-fast memory queries MEMORY_DIR="$HOME/.blackroad/memory" JOURNAL_FILE="$MEMORY_DIR/journals/master-journal.jsonl" INDEX_DIR="$MEMORY_DIR/indexes" INDEX_DB="$INDEX_DIR/indexes.db" # Colors GREEN='\033[0;32m' YELLOW='\033[1;33m' RED='\033[0;31m' CYAN='\033[0;36m' PURPLE='\033[0;35m' NC='\033[0m' # Initialize indexing system init() { echo -e "${PURPLE}πŸ” Initializing Memory Indexing System...${NC}\n" mkdir -p "$INDEX_DIR" # Create comprehensive index database sqlite3 "$INDEX_DB" </dev/null done fi done < "$JOURNAL_FILE" echo -e "\n${GREEN}βœ… Indexes built successfully!${NC}\n" # Show statistics show_index_stats } # Show index statistics show_index_stats() { echo -e "${PURPLE}πŸ“Š Index Statistics:${NC}\n" local actions=$(sqlite3 "$INDEX_DB" "SELECT COUNT(*) FROM action_index") local entities=$(sqlite3 "$INDEX_DB" "SELECT COUNT(*) FROM entity_index") local agents=$(sqlite3 "$INDEX_DB" "SELECT COUNT(*) FROM agent_index") local dates=$(sqlite3 "$INDEX_DB" "SELECT COUNT(*) FROM date_index") local keywords=$(sqlite3 "$INDEX_DB" "SELECT COUNT(*) FROM keyword_index") local relations=$(sqlite3 "$INDEX_DB" "SELECT COUNT(*) FROM action_entity_relations") echo -e " ${CYAN}Actions indexed:${NC} $actions" echo -e " ${CYAN}Entities indexed:${NC} $entities" echo -e " ${CYAN}Agents indexed:${NC} $agents" echo -e " ${CYAN}Dates indexed:${NC} $dates" echo -e " ${CYAN}Keywords indexed:${NC} $keywords" echo -e " ${CYAN}Relationships:${NC} $relations\n" } # Fast lookup by action lookup_action() { local action="$1" echo -e "${CYAN}πŸ” Looking up action: ${YELLOW}$action${NC}\n" sqlite3 -column -header "$INDEX_DB" < 5 ORDER BY occurrences DESC LIMIT 10; EOF echo "" # Pattern 2: Agent specializations echo -e "${YELLOW}Agent Specializations:${NC}" sqlite3 -column "$INDEX_DB" < 3 GROUP BY a.agent_name LIMIT 10; EOF echo "" # Pattern 3: Time-based patterns echo -e "${YELLOW}Peak Activity Days:${NC}" sqlite3 -column "$INDEX_DB" < 2; -- Action affects Entity INSERT INTO knowledge_graph (subject, predicate, object, confidence, timestamp) SELECT action, 'affects', entity, CAST(count AS REAL) / (SELECT MAX(count) FROM action_entity_relations), last_occurrence FROM action_entity_relations WHERE count > 2; -- Entity related to Entity (co-occurrence) INSERT INTO knowledge_graph (subject, predicate, object, confidence, timestamp) SELECT DISTINCT r1.entity, 'related_to', r2.entity, 0.5, MAX(r1.last_occurrence, r2.last_occurrence) FROM action_entity_relations r1 JOIN action_entity_relations r2 ON r1.action = r2.action AND r1.entity != r2.entity WHERE r1.count > 1 AND r2.count > 1; EOF local graph_size=$(sqlite3 "$INDEX_DB" "SELECT COUNT(*) FROM knowledge_graph") echo -e "${GREEN}βœ… Knowledge graph built: $graph_size relationships${NC}\n" } # Query knowledge graph query_knowledge() { local subject="$1" echo -e "${CYAN}πŸ•ΈοΈ Knowledge graph for: ${YELLOW}$subject${NC}\n" sqlite3 -column -header "$INDEX_DB" <