UNPKG

@claude-vector/core

Version:

Core vector search engine for code intelligence

199 lines (172 loc) 4.67 kB
/** * Configuration utilities */ export function createDefaultConfig() { return { // Vector search settings search: { threshold: 0.7, maxResults: 10, includeMetadata: true }, // Embedding settings embeddings: { model: 'text-embedding-3-small', batchSize: 100, dimensions: 1536, // for text-embedding-3-small maxRetries: 3, retryDelay: 1000 }, // Chunk processing settings chunks: { maxSize: 1000, minSize: 100, overlap: 200, splitByParagraph: true, preserveCodeBlocks: true }, // File patterns patterns: { include: [ '**/*.{js,jsx,ts,tsx}', '**/*.{md,mdx}', '**/*.{json,yaml,yml}', '**/*.{css,scss,sass}', '**/*.{html,xml}' ], exclude: [ '**/node_modules/**', '**/dist/**', '**/build/**', '**/.git/**', '**/coverage/**', '**/.next/**', '**/out/**', '**/*.min.js', '**/*.bundle.js', '**/vendor/**', '**/tmp/**', '**/temp/**', '**/.cache/**' ] }, // Cache settings cache: { enabled: true, ttl: 3600, // 1 hour in seconds maxSize: '100MB', compression: true }, // Index settings index: { path: '.claude-vector-index', autoUpdate: false, updateInterval: 300000, // 5 minutes compressionLevel: 6 }, // API settings api: { timeout: 30000, maxConcurrent: 5, retryOnRateLimit: true }, // Logging logging: { level: 'info', // debug, info, warn, error file: null, console: true } }; } /** * Merge configurations with proper precedence */ export function mergeConfig(...configs) { const result = createDefaultConfig(); for (const config of configs) { if (!config) continue; deepMerge(result, config); } return result; } /** * Deep merge helper */ function deepMerge(target, source) { for (const key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { if (isObject(source[key]) && isObject(target[key])) { deepMerge(target[key], source[key]); } else if (Array.isArray(source[key])) { target[key] = [...source[key]]; } else { target[key] = source[key]; } } } return target; } /** * Check if value is a plain object */ function isObject(value) { return value !== null && typeof value === 'object' && !Array.isArray(value); } /** * Validate configuration */ export function validateConfig(config) { const errors = []; // Check required fields if (!config.api?.openaiKey && !process.env.OPENAI_API_KEY) { errors.push('OpenAI API key is required (config.api.openaiKey or OPENAI_API_KEY env var)'); } // Validate numeric ranges if (config.search?.threshold !== undefined) { if (config.search.threshold < 0 || config.search.threshold > 1) { errors.push('search.threshold must be between 0 and 1'); } } if (config.chunks?.maxSize !== undefined) { if (config.chunks.maxSize < 100 || config.chunks.maxSize > 8000) { errors.push('chunks.maxSize must be between 100 and 8000'); } } if (config.chunks?.overlap !== undefined) { if (config.chunks.overlap < 0 || config.chunks.overlap >= config.chunks.maxSize) { errors.push('chunks.overlap must be between 0 and chunks.maxSize'); } } // Validate embedding model const validModels = ['text-embedding-3-small', 'text-embedding-3-large', 'text-embedding-ada-002']; if (config.embeddings?.model && !validModels.includes(config.embeddings.model)) { errors.push(`embeddings.model must be one of: ${validModels.join(', ')}`); } return { valid: errors.length === 0, errors }; } /** * Load configuration from environment variables */ export function loadEnvConfig() { const envConfig = {}; // API settings from env if (process.env.OPENAI_API_KEY) { envConfig.api = { openaiKey: process.env.OPENAI_API_KEY }; } if (process.env.CLAUDE_VECTOR_CACHE_DIR) { envConfig.cache = { path: process.env.CLAUDE_VECTOR_CACHE_DIR }; } if (process.env.CLAUDE_VECTOR_INDEX_PATH) { envConfig.index = { path: process.env.CLAUDE_VECTOR_INDEX_PATH }; } if (process.env.CLAUDE_VECTOR_LOG_LEVEL) { envConfig.logging = { level: process.env.CLAUDE_VECTOR_LOG_LEVEL }; } if (process.env.CLAUDE_VECTOR_EMBEDDING_MODEL) { envConfig.embeddings = { model: process.env.CLAUDE_VECTOR_EMBEDDING_MODEL }; } return envConfig; }