File size: 11,272 Bytes
c3aa9b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#!/usr/bin/env node

/**
 * Documentation indexing and cache management tool
 * 
 * Usage:
 *   node src/index-docs.js [source] [options]
 *   node src/index-docs.js --stats
 *   node src/index-docs.js --clear-cache [embedding|page|all]
 *   node src/index-docs.js --validate-cache
 *   node src/index-docs.js --rebuild-source <source-id>
 * 
 * Examples:
 *   node src/index-docs.js k3s
 *   node src/index-docs.js all --force
 *   node src/index-docs.js --stats
 *   node src/index-docs.js --clear-cache page
 *   node src/index-docs.js --rebuild-source suse
 */

import { DocumentationService } from '../services/documentation-service.js';
import { VectorService } from '../services/vector-service.js';
import { AIService } from '../services/ai-service.js';
import dotenv from 'dotenv';
import fs from 'fs/promises';
import path from 'path';

dotenv.config();

// Parse command line arguments
const args = process.argv.slice(2);
const flags = {
  stats: args.includes('--stats'),
  clearCache: args.find(arg => arg.startsWith('--clear-cache')),
  validateCache: args.includes('--validate-cache'),
  rebuildSource: args.includes('--rebuild-source'),
  force: args.includes('--force'),
};

const docService = new DocumentationService();
const vectorService = new VectorService();
const aiService = new AIService();

try {
  // Handle --stats flag
  if (flags.stats) {
    await showCacheStats();
    process.exit(0);
  }

  // Handle --clear-cache flag
  if (flags.clearCache) {
    const cacheTypeIndex = args.indexOf('--clear-cache') + 1;
    const cacheType = args[cacheTypeIndex] || 'all';
    await clearCache(cacheType);
    process.exit(0);
  }

  // Handle --validate-cache flag
  if (flags.validateCache) {
    await validateCache();
    process.exit(0);
  }

  // Handle --rebuild-source flag
  if (flags.rebuildSource) {
    const sourceIndex = args.indexOf('--rebuild-source') + 1;
    const source = args[sourceIndex];
    if (!source) {
      console.error('❌ Error: --rebuild-source requires a source ID (suse, rancher, k3s, or all)');
      process.exit(1);
    }
    await rebuildSource(source);
    process.exit(0);
  }

  // Default: Index documentation
  const source = args.find(arg => !arg.startsWith('--')) || 'k3s';
  await indexDocumentation(source, flags.force);

} catch (error) {
  console.error(`❌ Error:`, error);
  process.exit(1);
}

// Show detailed cache statistics
async function showCacheStats() {
  console.log('\n📊 Cache Statistics\n');
  console.log('═══════════════════════════════════════════════════════\n');

  // Load caches
  await docService.loadPageCache();
  await aiService.loadEmbeddingCache();

  // Page Cache Stats
  const pageCacheStats = docService.getPageCacheStats();
  const totalPages = docService.pageCache.size;
  
  // Count by source
  const sourceCounts = new Map();
  const indexedCounts = new Map();
  
  for (const [url, entry] of docService.pageCache.entries()) {
    const source = entry.source || docService.getSourceFromUrl(url);
    if (source) {
      sourceCounts.set(source, (sourceCounts.get(source) || 0) + 1);
      if (entry.indexed) {
        indexedCounts.set(source, (indexedCounts.get(source) || 0) + 1);
      }
    }
  }

  console.log('📄 Page Cache:');
  console.log(`   Total cached pages: ${totalPages}`);
  console.log(`   Cache file: data/page-cache.json`);
  console.log('');
  console.log('   By Source:');
  for (const [source, count] of sourceCounts.entries()) {
    const indexed = indexedCounts.get(source) || 0;
    console.log(`     ${source.padEnd(10)} ${indexed} indexed / ${count} cached`);
  }
  console.log('');
  console.log('   Session Stats:');
  console.log(`     Loaded: ${pageCacheStats.loaded}`);
  console.log(`     304 Not Modified: ${pageCacheStats.hits304}`);
  console.log(`     Hash matches: ${pageCacheStats.hitsCached}`);
  console.log(`     New/Modified: ${pageCacheStats.misses}`);
  console.log(`     Skipped (unchanged): ${pageCacheStats.skipped}`);

  // Embedding Cache Stats
  const embeddingStats = aiService.getCacheStats();
  
  console.log('\n🧠 Embedding Cache:');
  console.log(`   Total cached embeddings: ${embeddingStats.currentSize}`);
  console.log(`   Cache file: data/embedding-cache.json`);
  console.log('');
  console.log('   Session Stats:');
  console.log(`     Loaded: ${embeddingStats.loaded}`);
  console.log(`     Hits: ${embeddingStats.hits}`);
  console.log(`     Misses: ${embeddingStats.misses}`);
  console.log(`     Hit rate: ${embeddingStats.hitRate}`);

  // Vector Database Stats
  await vectorService.ensureInitialized();
  console.log('\n🗂️  Vector Database:');
  console.log(`   Location: ${vectorService.indexPath}`);
  
  try {
    const indexStats = await fs.stat(vectorService.indexPath);
    console.log(`   Created: ${indexStats.birthtime.toLocaleDateString()}`);
    console.log(`   Last modified: ${indexStats.mtime.toLocaleDateString()}`);
  } catch (error) {
    console.log('   Status: Not initialized');
  }

  console.log('\n═══════════════════════════════════════════════════════\n');
}

// Clear cache(s)
async function clearCache(cacheType) {
  console.log(`\n🗑️  Clearing ${cacheType} cache...\n`);

  if (cacheType === 'embedding' || cacheType === 'all') {
    try {
      await fs.unlink('data/embedding-cache.json');
      console.log('✓ Cleared embedding cache (data/embedding-cache.json)');
    } catch (error) {
      if (error.code !== 'ENOENT') {
        console.error('✗ Failed to clear embedding cache:', error.message);
      }
    }
  }

  if (cacheType === 'page' || cacheType === 'all') {
    try {
      await fs.unlink('data/page-cache.json');
      console.log('✓ Cleared page cache (data/page-cache.json)');
      
      // Also clear HTML cache directory
      try {
        const htmlDir = 'data/html';
        const files = await fs.readdir(htmlDir);
        for (const file of files) {
          await fs.unlink(path.join(htmlDir, file));
        }
        console.log(`✓ Cleared ${files.length} cached HTML files`);
      } catch (error) {
        if (error.code !== 'ENOENT') {
          console.error('✗ Failed to clear HTML cache:', error.message);
        }
      }
    } catch (error) {
      if (error.code !== 'ENOENT') {
        console.error('✗ Failed to clear page cache:', error.message);
      }
    }
  }

  if (cacheType === 'vectors' || cacheType === 'all') {
    try {
      const vectorPath = process.env.VECTOR_DB_PATH || './data/vectors';
      await fs.rm(vectorPath, { recursive: true, force: true });
      console.log(`✓ Cleared vector database (${vectorPath})`);
    } catch (error) {
      console.error('✗ Failed to clear vector database:', error.message);
    }
  }

  if (!['embedding', 'page', 'vectors', 'all'].includes(cacheType)) {
    console.error(`❌ Invalid cache type: ${cacheType}`);
    console.error('Valid options: embedding, page, vectors, all');
    process.exit(1);
  }

  console.log('\n✅ Cache clearing complete!\n');
}

// Validate cache integrity
async function validateCache() {
  console.log('\n🔍 Validating caches...\n');

  let errors = 0;

  // Validate page cache
  try {
    await docService.loadPageCache();
    console.log(`✓ Page cache loaded successfully (${docService.pageCache.size} entries)`);
    
    // Check for corrupted entries
    for (const [url, entry] of docService.pageCache.entries()) {
      if (!entry.contentHash) {
        console.warn(`  ⚠️  Missing contentHash: ${url}`);
        errors++;
      }
      if (entry.indexed && !entry.source) {
        console.warn(`  ⚠️  Indexed entry missing source: ${url}`);
        errors++;
      }
    }
  } catch (error) {
    console.error('✗ Page cache validation failed:', error.message);
    errors++;
  }

  // Validate embedding cache
  try {
    await aiService.loadEmbeddingCache();
    const stats = aiService.getCacheStats();
    console.log(`✓ Embedding cache loaded successfully (${stats.currentSize} entries)`);
    
    // Check for corrupted entries
    let checkedCount = 0;
    for (const [hash, embedding] of aiService.embeddingCache.entries()) {
      if (!Array.isArray(embedding) || embedding.length === 0) {
        console.warn(`  ⚠️  Invalid embedding for hash: ${hash.substring(0, 16)}...`);
        errors++;
      }
      if (++checkedCount > 100) break; // Sample check, don't validate all
    }
  } catch (error) {
    console.error('✗ Embedding cache validation failed:', error.message);
    errors++;
  }

  // Validate vector database
  try {
    await vectorService.ensureInitialized();
    console.log(`✓ Vector database accessible (${vectorService.indexPath})`);
  } catch (error) {
    console.error('✗ Vector database validation failed:', error.message);
    errors++;
  }

  console.log('');
  if (errors === 0) {
    console.log('✅ All caches valid!\n');
  } else {
    console.log(`⚠️  Found ${errors} issue(s). Consider clearing and rebuilding caches.\n`);
  }
}

// Rebuild specific source
async function rebuildSource(sourceId) {
  console.log(`\n🔄 Rebuilding source: ${sourceId}\n`);
  console.log('⚠️  Note: This will clear ALL caches and rebuild from scratch.');
  console.log('   Vectra does not support partial updates, so all sources must be reindexed.\n');

  // Clear all caches for a fresh start
  console.log('Clearing caches...');
  await clearCache('all');

  // Re-index with force refresh
  console.log(`\n🚀 Starting re-indexing...\n`);
  await indexDocumentation(sourceId, true);
}

// Index documentation with optional locking
async function indexDocumentation(source, forceRefresh) {
  console.log(`\n🚀 Starting documentation indexing for: ${source}`);
  if (forceRefresh) {
    console.log('   Force refresh: Ignoring cache, fetching all documents');
  }
  console.log('This may take several minutes...\n');

  // Try to acquire lock for concurrent indexing safety (SQLite only)
  let lock = null;
  if (!docService.useJsonCache) {
    await docService.loadPageCache();
    lock = docService.pageCache.acquireLock(`index-${source}`, 1800); // 30 min timeout
    
    if (!lock.acquired) {
      console.log(`⚠️  Another indexing process is already running for ${source}`);
      console.log(`   Lock expires in ${Math.round(lock.expiresIn / 60)} minutes`);
      console.log(`   If this is incorrect, run: npm run clear-locks\n`);
      return;
    }
    
    console.log(`🔒 Acquired indexing lock: ${lock.lockId}\n`);
  }

  try {
    const result = await docService.indexDocumentation(source, forceRefresh);
    
    if (result.success) {
      console.log(`✅ Successfully indexed ${result.documentsIndexed} documents!`);
    } else {
      console.log(`❌ Indexing failed: ${result.error}`);
    }
  } finally {
    // Release lock
    if (lock && lock.acquired) {
      docService.pageCache.releaseLock(`index-${source}`, lock.lockId);
      console.log(`🔓 Released indexing lock\n`);
    }
  }

  console.log('\n✅ Done! You can now search the documentation.\n');
}