@hpbyte/h-codex-core
Version:
Core indexing and search functionality for h-codex
65 lines • 2.81 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.indexer = exports.Indexer = void 0;
const chunker_1 = require("../chunker");
const embedder_1 = require("../embedder");
const explorer_1 = require("../explorer");
const chunk_embeddings_repository_1 = require("../../storage/chunk-embeddings.repository");
const projects_repository_1 = require("../../storage/projects.repository");
class Indexer {
async index(path) {
try {
const files = await explorer_1.fileExplorer.discover(path);
const project = await projects_repository_1.projectsRepository.create(path);
if (files.length === 0) {
return {
totalFiles: 0,
processedFiles: 0,
failedFiles: 0,
errors: [],
};
}
const { results, errors } = await explorer_1.fileExplorer.processInBatches(files, (filePath) => this.indexFile(filePath, project.id));
const processedFiles = results.filter(result => result !== null).length;
const failedFiles = errors.length;
return {
totalFiles: files.length,
processedFiles,
failedFiles,
errors: errors.map(error => ({
file: error.item,
error: error.error.message,
})),
};
}
catch (error) {
throw new Error(`Failed to index folder ${path}: ${error}`);
}
}
async indexFile(filePath, projectId) {
try {
const chunks = await chunker_1.chunker.processFile(filePath, projectId);
const newCodeChunks = await chunk_embeddings_repository_1.chunkEmbeddingsRepository.insertCodeChunks(chunks);
const embeddings = await embedder_1.embedder.generate(newCodeChunks);
const newEmbeddings = await chunk_embeddings_repository_1.chunkEmbeddingsRepository.insertEmbeddings(newCodeChunks, embeddings);
return { newCodeChunks, newEmbeddings };
}
catch (error) {
throw new Error(`Failed to index file ${filePath}: ${error}`);
}
}
async clear(projectName) {
try {
const project = await projects_repository_1.projectsRepository.get(projectName);
if (!project)
throw new Error(`Project ${projectName} not found`);
return await chunk_embeddings_repository_1.chunkEmbeddingsRepository.clearChunkEmbeddings(project.id);
}
catch (error) {
throw new Error(`Failed to clear index for project ${projectName}: ${error}`);
}
}
}
exports.Indexer = Indexer;
exports.indexer = new Indexer();
//# sourceMappingURL=index.js.map