|
| 1 | +import * as vscode from "vscode"; |
| 2 | +import { Worker } from "worker_threads"; |
| 3 | +import * as path from "path"; |
| 4 | +import { Logger } from "../infrastructure/logger/logger"; |
| 5 | +import { SimpleVectorStore } from "./simple-vector-store"; |
| 6 | +import { EmbeddingService } from "./embedding"; |
| 7 | +import { getAPIKeyAndModel } from "../utils/utils"; |
| 8 | + |
| 9 | +export class AstIndexingService { |
| 10 | + private worker: Worker | undefined; |
| 11 | + private readonly logger: Logger; |
| 12 | + private vectorStore: SimpleVectorStore; |
| 13 | + private embeddingService: EmbeddingService; |
| 14 | + private queue: string[] = []; |
| 15 | + private isProcessing = false; |
| 16 | + |
| 17 | + private static instance: AstIndexingService; |
| 18 | + |
| 19 | + constructor(context: vscode.ExtensionContext) { |
| 20 | + this.logger = Logger.initialize("AstIndexingService", {}); |
| 21 | + this.vectorStore = new SimpleVectorStore(context); |
| 22 | + |
| 23 | + // Initialize embedding service |
| 24 | + const { apiKey } = getAPIKeyAndModel("gemini"); |
| 25 | + this.embeddingService = new EmbeddingService(apiKey); |
| 26 | + |
| 27 | + this.initializeWorker(context); |
| 28 | + } |
| 29 | + |
| 30 | + public static getInstance( |
| 31 | + context?: vscode.ExtensionContext, |
| 32 | + ): AstIndexingService { |
| 33 | + if (!AstIndexingService.instance) { |
| 34 | + if (!context) { |
| 35 | + throw new Error( |
| 36 | + "AstIndexingService not initialized. Context required for first initialization.", |
| 37 | + ); |
| 38 | + } |
| 39 | + AstIndexingService.instance = new AstIndexingService(context); |
| 40 | + } |
| 41 | + return AstIndexingService.instance; |
| 42 | + } |
| 43 | + |
| 44 | + private initializeWorker(context: vscode.ExtensionContext) { |
| 45 | + // Determine the worker execution path (dist for prod, out for dev) |
| 46 | + const isProd = __filename.includes("dist"); |
| 47 | + const workerRelativePath = isProd |
| 48 | + ? "../workers/ast-analyzer.worker.js" |
| 49 | + : "../../out/workers/ast-analyzer.worker.js"; |
| 50 | + |
| 51 | + const workerPath = path.resolve(__dirname, workerRelativePath); |
| 52 | + |
| 53 | + try { |
| 54 | + this.logger.info(`Initializing Indexing Worker at: ${workerPath}`); |
| 55 | + this.worker = new Worker(workerPath); |
| 56 | + |
| 57 | + this.worker.on("message", this.handleWorkerMessage.bind(this)); |
| 58 | + this.worker.on("error", (err) => this.logger.error("Worker error:", err)); |
| 59 | + this.worker.on("exit", (code) => { |
| 60 | + if (code !== 0) { |
| 61 | + this.logger.error(`Worker stopped with exit code ${code}`); |
| 62 | + // Restart specific worker logic could go here |
| 63 | + } |
| 64 | + }); |
| 65 | + } catch (error) { |
| 66 | + this.logger.error("Failed to initialize worker", error); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private async handleWorkerMessage(message: any) { |
| 71 | + if (message.type === "RESULT") { |
| 72 | + const { chunks, filePath } = message.data; |
| 73 | + this.logger.info( |
| 74 | + `Worker finished file: ${filePath}, generated ${chunks.length} chunks`, |
| 75 | + ); |
| 76 | + |
| 77 | + // Process embeddings in main thread (or separate worker) |
| 78 | + // Emitting to LLM API is I/O bound, so doing it here in batches is okay |
| 79 | + // provided we don't block. |
| 80 | + await this.processChunks(chunks); |
| 81 | + } else if (message.type === "ERROR") { |
| 82 | + this.logger.error("Worker processing error", message.error); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private async processChunks(chunks: any[]) { |
| 87 | + // We'll generate embeddings for these chunks |
| 88 | + for (const chunk of chunks) { |
| 89 | + try { |
| 90 | + const embedding = await this.embeddingService.generateEmbedding( |
| 91 | + chunk.text, |
| 92 | + ); |
| 93 | + if (embedding) { |
| 94 | + await this.vectorStore.addDocument({ |
| 95 | + id: chunk.id, |
| 96 | + text: chunk.text, |
| 97 | + vector: embedding, |
| 98 | + metadata: chunk.metadata, |
| 99 | + }); |
| 100 | + } |
| 101 | + } catch (err) { |
| 102 | + this.logger.warn( |
| 103 | + `Failed to generate embedding for chunk ${chunk.id}`, |
| 104 | + err, |
| 105 | + ); |
| 106 | + } |
| 107 | + } |
| 108 | + this.logger.info(`Persisted ${chunks.length} chunks to vector store`); |
| 109 | + } |
| 110 | + |
| 111 | + public indexFile(filePath: string, content: string) { |
| 112 | + if (!this.worker) return; |
| 113 | + |
| 114 | + // Send to worker |
| 115 | + this.worker.postMessage({ |
| 116 | + type: "INDEX_FILE", |
| 117 | + data: { filePath, content }, |
| 118 | + }); |
| 119 | + } |
| 120 | +} |
0 commit comments