Spaces:
Running
Running
| const { createClient } = require('@supabase/supabase-js'); | |
| const MiniSearch = require('minisearch'); | |
| // require('dotenv').config(); | |
| // 1. Initialize Supabase | |
| const supabaseUrl = process.env.SUPABASE_URL; | |
| const supabaseKey = process.env.SUPABASE_KEY; | |
| const supabase = createClient(supabaseUrl, supabaseKey); | |
| // 2. Initialize In-Memory Search Engine | |
| const searchEngine = new MiniSearch({ | |
| fields: ['name', 'description', 'keywords'], | |
| storeFields: ['id', 'name', 'description', 'url', 'install_command', 'stars', 'reputation_score'], | |
| searchOptions: { fuzzy: 0.2, prefix: true } | |
| }); | |
| // 3. Hydrate RAM from Supabase | |
| async function hydrateMemory() { | |
| console.log("💧 Hydrating In-Memory Index from Supabase..."); | |
| try { | |
| const { data, error } = await supabase | |
| .from('mcp_tools') | |
| .select('*'); | |
| if (error) throw error; | |
| searchEngine.removeAll(); | |
| if (data && data.length > 0) { | |
| searchEngine.addAll(data); | |
| } | |
| console.log(`✅ Loaded ${data.length} tools into RAM.`); | |
| } catch (error) { | |
| console.error("❌ Hydration Failed:", error.message); | |
| } | |
| } | |
| function searchTools(query) { | |
| if (searchEngine.documentCount === 0) return []; | |
| return searchEngine.search(query); | |
| } | |
| module.exports = { supabase, searchEngine, hydrateMemory, searchTools }; |