File size: 1,291 Bytes
ea4a035
5b13454
ea4a035
c977dc0
ea4a035
 
 
 
5b13454
f2f7fed
5b13454
 
ea4a035
 
5b13454
 
ea4a035
5b13454
ea4a035
5b13454
ea4a035
 
 
c977dc0
ea4a035
5b13454
 
ea4a035
 
 
 
5b13454
c977dc0
5b13454
 
 
 
c977dc0
5b13454
 
 
ea4a035
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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 };