File size: 7,422 Bytes
bfc7d04 | 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 | // Code Indexer - Semantic code search for Stack 2.9
//
// Provides RAG capabilities by indexing code and enabling semantic search.
import { readdir, readFile, writeFile, stat } from 'fs/promises'
import { join, relative, extname } from 'path'
import crypto from 'crypto'
// Types
export interface CodeChunk {
id: string
filePath: string
content: string
startLine: number
endLine: number
language: string
chunkType: 'function' | 'class' | 'file' | 'block'
}
export interface CodeIndex {
version: string
projectPath: string
indexedAt: string
chunks: (CodeChunk & { embedding: number[] })[]
}
export interface CodeSearchResult {
chunk: Omit<CodeChunk, 'id'>
score: number
filePath: string
startLine: number
endLine: number
}
// Configuration
const CHUNK_SIZE = 2000
const TOP_K = 5
// Supported file extensions
const CODE_EXTENSIONS = new Set([
'.ts', '.tsx', '.js', '.jsx', '.py', '.go', '.rs', '.java',
'.c', '.cpp', '.h', '.hpp', '.cs', '.rb', '.php', '.swift',
'.kt', '.scala', '.vue', '.svelte', '.json', '.yaml', '.yml',
'.md', '.txt', '.sh', '.bash', '.zsh',
])
// Directories to skip
const SKIP_DIRS = new Set([
'node_modules', '.git', 'dist', 'build', 'out', '__pycache__',
'.next', '.nuxt', '.svelte-kit', 'coverage', '.cache',
'.venv', 'venv', 'env', '.env', 'vendor',
])
// Language detection
function getLanguage(filePath: string): string {
const ext = extname(filePath).toLowerCase()
const langMap: Record<string, string> = {
'.ts': 'typescript',
'.tsx': 'typescript',
'.js': 'javascript',
'.jsx': 'javascript',
'.py': 'python',
'.go': 'go',
'.rs': 'rust',
'.java': 'java',
'.c': 'c',
'.cpp': 'cpp',
'.h': 'c',
'.hpp': 'cpp',
'.cs': 'csharp',
'.rb': 'ruby',
'.php': 'php',
'.swift': 'swift',
'.kt': 'kotlin',
'.scala': 'scala',
'.vue': 'vue',
'.svelte': 'svelte',
'.json': 'json',
'.yaml': 'yaml',
'.yml': 'yaml',
'.md': 'markdown',
'.sh': 'bash',
'.bash': 'bash',
'.zsh': 'zsh',
}
return langMap[ext] ?? 'text'
}
// Simple hash-based embedding (for offline use)
// In production, use @xenova/transformers or OpenAI embeddings
function generateEmbedding(content: string): number[] {
const hash = crypto.createHash('sha256').update(content).digest()
const embedding: number[] = []
for (let i = 0; i < 256; i++) {
embedding.push((hash[i % hash.length] ?? 0) / 255)
}
return embedding
}
// Cosine similarity
function cosineSimilarity(a: number[], b: number[]): number {
let dot = 0
let normA = 0
let normB = 0
for (let i = 0; i < a.length; i++) {
dot += a[i] * b[i]
normA += a[i] * a[i]
normB += b[i] * b[i]
}
return dot / (Math.sqrt(normA) * Math.sqrt(normB) || 1)
}
// Split code into chunks
function splitIntoChunks(content: string, filePath: string, language: string): CodeChunk[] {
const lines = content.split('\n')
const chunks: CodeChunk[] = []
let chunkLines: string[] = []
let startLine = 1
for (let i = 0; i < lines.length; i++) {
const line = lines[i]
chunkLines.push(line)
// Create chunk when size exceeds limit or at logical boundaries
const shouldChunk = chunkLines.join('\n').length > CHUNK_SIZE ||
(line.match(/^(function|class|const|let|var|def|import|export|public|private)/) && chunkLines.length > 5)
if (shouldChunk && chunkLines.length > 3) {
chunks.push({
id: crypto.randomUUID(),
filePath,
content: chunkLines.join('\n'),
startLine,
endLine: i + 1,
language,
chunkType: 'block',
})
chunkLines = []
startLine = i + 2
}
}
// Add remaining as final chunk
if (chunkLines.length > 0) {
chunks.push({
id: crypto.randomUUID(),
filePath,
content: chunkLines.join('\n'),
startLine,
endLine: lines.length,
language,
chunkType: 'file',
})
}
return chunks
}
// Walk directory and collect files
async function* walkDirectory(dir: string): AsyncGenerator<string> {
try {
const entries = await readdir(dir, { withFileTypes: true })
for (const entry of entries) {
const fullPath = join(dir, entry.name)
if (entry.isDirectory()) {
if (!SKIP_DIRS.has(entry.name) && !entry.name.startsWith('.')) {
yield* walkDirectory(fullPath)
}
} else if (entry.isFile()) {
const ext = extname(entry.name).toLowerCase()
if (CODE_EXTENSIONS.has(ext)) {
yield fullPath
}
}
}
} catch (error) {
console.warn(`[index] Cannot read directory ${dir}:`, error)
}
}
// βββ Code Indexer Class βββ
export class CodeIndexer {
private index: CodeIndex | null = null
async indexProject(projectPath: string): Promise<void> {
console.log(`[index] Indexing project: ${projectPath}`)
const chunks: (CodeChunk & { embedding: number[] })[] = []
for await (const filePath of walkDirectory(projectPath)) {
try {
const content = await readFile(filePath, 'utf-8')
const relPath = relative(projectPath, filePath)
const language = getLanguage(filePath)
const fileChunks = splitIntoChunks(content, relPath, language)
for (const chunk of fileChunks) {
chunks.push({
...chunk,
embedding: generateEmbedding(chunk.content),
})
}
} catch (error) {
console.warn(`[index] Cannot read file ${filePath}:`, error)
}
}
this.index = {
version: '1.0.0',
projectPath,
indexedAt: new Date().toISOString(),
chunks,
}
console.log(`[index] Indexed ${chunks.length} chunks from project`)
}
async search(query: string, topK: number = TOP_K): Promise<CodeSearchResult[]> {
if (!this.index) {
console.warn('[index] No index loaded')
return []
}
const queryEmbedding = generateEmbedding(query)
// Calculate similarity for each chunk
const results = this.index.chunks.map(chunk => ({
chunk: {
filePath: chunk.filePath,
content: chunk.content,
startLine: chunk.startLine,
endLine: chunk.endLine,
language: chunk.language,
chunkType: chunk.chunkType,
},
filePath: chunk.filePath,
startLine: chunk.startLine,
endLine: chunk.endLine,
score: cosineSimilarity(queryEmbedding, chunk.embedding),
}))
// Sort by score and return top K
results.sort((a, b) => b.score - a.score)
return results.slice(0, topK)
}
async saveIndex(path: string): Promise<void> {
if (!this.index) {
throw new Error('No index to save')
}
await writeFile(path, JSON.stringify(this.index, null, 2))
console.log(`[index] Saved index to ${path}`)
}
async loadIndex(path: string): Promise<void> {
const content = await readFile(path, 'utf-8')
this.index = JSON.parse(content)
console.log(`[index] Loaded index with ${this.index.chunks.length} chunks`)
}
getIndexStats(): { chunkCount: number; indexedAt: string } | null {
if (!this.index) return null
return {
chunkCount: this.index.chunks.length,
indexedAt: this.index.indexedAt,
}
}
}
// βββ Factory βββ
export function createIndexer(): CodeIndexer {
return new CodeIndexer()
}
export default {
CodeIndexer,
createIndexer,
} |