/** * Embedding Queue Manager * * Manages an async queue of embedding requests, processing them in batches * via a worker thread to avoid blocking the main Node.js event loop. * * Architecture: * - {@link EmbeddingQueue} is a singleton — one worker thread per process * - {@link EmbeddingQueue.enqueue} adds items; the drain loop batches them * - Batches up to {@link BATCH_SIZE} items per processing cycle * - Falls back to `setImmediate` + direct embedding when worker threads * are unavailable or the worker script cannot be resolved * - Registers a `process.on('exit')` handler for graceful shutdown * * @epic T134 * @task T137 * @why Non-blocking embedding generation for observeBrain() * @what Singleton queue + worker-thread batch processor */ /** * Singleton embedding queue. * * Batches embedding requests and processes them via a worker thread, * keeping heavy model inference off the main event loop. * * Use {@link getEmbeddingQueue} to obtain the shared instance. */ export declare class EmbeddingQueue { private readonly queue; private worker; private workerAvailable; private draining; private shutdownPromise; /** * Store the observation ID → DB write callback so the worker result * can be persisted back to brain_embeddings without coupling to SQLite here. */ private readonly callbacks; /** Initialise the worker thread if possible. Called lazily on first enqueue. */ private initWorker; /** * Add an observation to the embedding queue. * * The observation must already be persisted in brain_observations before * calling this method. `onComplete` is called asynchronously with the * generated vector once the worker finishes. * * @param observationId - The brain observation ID (e.g. `O-abc123-0`) * @param text - Raw text to embed * @param onComplete - Callback to persist the embedding vector */ enqueue(observationId: string, text: string, onComplete: (observationId: string, embedding: Float32Array) => Promise): void; /** Schedule the next drain cycle via setImmediate. */ private scheduleDrain; /** * Process up to {@link BATCH_SIZE} items from the queue. * Uses the worker thread when available, falls back to inline setImmediate. */ private drain; /** * Inline fallback embedding — used when worker thread is unavailable. * Runs directly on the main thread (but inside setImmediate to yield first). */ private fallbackEmbed; /** * Flush the queue and terminate the worker thread. * * Waits for in-flight worker messages to drain, then terminates. * Safe to call multiple times — subsequent calls return the same promise. */ shutdown(): Promise; private doShutdown; } /** * Get or create the shared EmbeddingQueue singleton. * * Registers a process `exit` handler on first call to flush and * terminate the worker thread cleanly. * * @returns The shared EmbeddingQueue instance. */ export declare function getEmbeddingQueue(): EmbeddingQueue; /** * Reset the singleton (for testing only). * Shuts down the existing queue before clearing the reference. * * @internal */ export declare function resetEmbeddingQueue(): Promise; //# sourceMappingURL=embedding-queue.d.ts.map