|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Postinstall script - downloads the embedding model to ~/.codegraph/models |
| 4 | + * This runs after `npm install` or `npx @colbymchenry/codegraph` |
| 5 | + */ |
| 6 | +const { existsSync, mkdirSync } = require('fs'); |
| 7 | +const { join } = require('path'); |
| 8 | +const { homedir } = require('os'); |
| 9 | + |
| 10 | +const CODEGRAPH_DIR = join(homedir(), '.codegraph'); |
| 11 | +const MODELS_DIR = join(CODEGRAPH_DIR, 'models'); |
| 12 | +const MODEL_ID = 'nomic-ai/nomic-embed-text-v1.5'; |
| 13 | + |
| 14 | +async function downloadModel() { |
| 15 | + // Ensure directories exist |
| 16 | + if (!existsSync(CODEGRAPH_DIR)) { |
| 17 | + mkdirSync(CODEGRAPH_DIR, { recursive: true }); |
| 18 | + } |
| 19 | + if (!existsSync(MODELS_DIR)) { |
| 20 | + mkdirSync(MODELS_DIR, { recursive: true }); |
| 21 | + } |
| 22 | + |
| 23 | + // Check if model is already cached |
| 24 | + const modelCachePath = join(MODELS_DIR, MODEL_ID.replace('/', '/')); |
| 25 | + if (existsSync(modelCachePath)) { |
| 26 | + console.log('Embedding model already downloaded.'); |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + console.log('Downloading embedding model (~130MB)...'); |
| 31 | + console.log('This is a one-time download for semantic code search.\n'); |
| 32 | + |
| 33 | + try { |
| 34 | + // Dynamic import for @xenova/transformers (ESM-only package) |
| 35 | + const { pipeline, env } = await import('@xenova/transformers'); |
| 36 | + |
| 37 | + // Configure cache directory |
| 38 | + env.cacheDir = MODELS_DIR; |
| 39 | + |
| 40 | + // Download with progress |
| 41 | + await pipeline('feature-extraction', MODEL_ID, { |
| 42 | + progress_callback: (progress) => { |
| 43 | + if (progress.status === 'progress' && progress.file && progress.progress !== undefined) { |
| 44 | + const fileName = progress.file.split('/').pop(); |
| 45 | + const percent = Math.round(progress.progress); |
| 46 | + process.stdout.write(`\rDownloading ${fileName}... ${percent}% `); |
| 47 | + } else if (progress.status === 'done') { |
| 48 | + process.stdout.write('\n'); |
| 49 | + } |
| 50 | + }, |
| 51 | + }); |
| 52 | + |
| 53 | + console.log('\nEmbedding model ready!'); |
| 54 | + } catch (error) { |
| 55 | + // Don't fail the install if model download fails |
| 56 | + // User can still use codegraph without semantic search |
| 57 | + console.log('\nNote: Could not download embedding model.'); |
| 58 | + console.log('Semantic search will download it on first use.'); |
| 59 | + if (process.env.DEBUG) { |
| 60 | + console.error(error); |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +downloadModel().catch(() => { |
| 66 | + // Silent exit - don't break npm install |
| 67 | + process.exit(0); |
| 68 | +}); |
0 commit comments