/** * Multi-Provider Embedding Service using Vercel AI SDK * * Provides a unified interface for embedding generation across multiple providers * (OpenAI, Google, Cohere, Ollama) with automatic retry and timeout handling. * * Inspired by OpenClaw's provider system with improvements: * - Uses Vercel AI SDK for provider abstraction * - Exponential backoff retry (500ms base, 8s max) * - Configurable timeouts (60s remote, 5min local) * - Health checks before usage * - Batch operations with token limits */ import type { EmbeddingProviderConfig } from "./config.js"; /** * Base interface for embedding providers */ export interface EmbeddingProvider { /** Unique provider identifier */ id: string; /** Model identifier */ model: string; /** Embedding dimensions */ dimensions: number; /** Embed a single text query */ embedQuery(text: string): Promise; /** Embed multiple texts in batch */ embedBatch(texts: string[]): Promise; /** Check if provider is available and configured */ isAvailable(): Promise; /** Get provider configuration */ getConfig(): EmbeddingProviderConfig; } /** * AI SDK-based embedding provider implementation * * Supports OpenAI, Google, Cohere, and Ollama via Vercel AI SDK * with automatic retry, timeout, and health checking. */ export declare class AISDKEmbeddingProvider implements EmbeddingProvider { private readonly config; private readonly providerId; readonly id: string; readonly model: string; readonly dimensions: number; private readonly providerType; private readonly apiKey?; private readonly baseURL?; private readonly timeout; private readonly retryConfig; private readonly rateLimiter?; private static ollamaMutex; private static readonly OLLAMA_DELAY_MS; constructor(config: EmbeddingProviderConfig, providerId: string); private getEmbeddingModel; /** * Get provider options (API key, base URL) */ private getProviderOptions; /** * Truncate text to fit model context length. * * Limit is resolved per-provider from config.maxChars (see embeddings/config.ts * getMaxChars). Falls back to 4000 chars (bge-m3 safe default) when not set. */ private truncateText; /** * Sanitize text to prevent NaN errors in embedding models * * Removes: * - Control characters (U+0000 to U+001F, U+007F) * - Replacement character U+FFFD (indicates broken UTF-8) * - ONLY unpaired surrogate halves (invalid UTF-16) * - Zero-width and non-printable characters * * Preserves valid Unicode (emojis, accented chars, CJK, etc.) */ private sanitizeText; /** * Queue Ollama requests to prevent overwhelming the server */ private queueOllamaRequest; /** * POST to an Ollama endpoint with an AbortController tied to the provider * timeout. * * Why: when the Ollama runner wedges (model stuck mid-inference), it accepts * the TCP connection but never responds. A bare `fetch` with no signal stays * pending indefinitely, so `queueOllamaRequest`'s `try/finally` never reaches * `finally` and the serialization mutex is held forever — every subsequent * embed queues behind it and the whole index job hangs (only the stale-job * reaper recovers it, minutes later). The outer `withTimeout` rejects its own * race promise at the deadline but cannot cancel the dangling fetch. * * Arming an AbortController here guarantees the fetch settles (rejects with * AbortError) at the deadline: the mutex releases, `withRetry` can retry or * surface the error, and the job fails fast instead of hanging. */ private ollamaFetch; /** * Embed a single text query */ embedQuery(text: string): Promise; /** * Embed multiple texts in batch with intelligent rate limiting * * Features: * - Rate limiting for RPM/TPM/RPD * - Sub-batching to respect provider limits * - Sequential processing with delays between batches * - Progress logging for large batches */ embedBatch(texts: string[]): Promise; /** * Direct batch embedding without rate limiting (used internally) */ private embedBatchDirect; /** * Check if provider is available and configured correctly * * For Ollama: performs a fast 2s connectivity check first to avoid * long timeouts when the service is offline (issue #15). * * Then validates with a test embedding: * - API key is valid * - Model is accessible * - Network connectivity * - Service is responding */ isAvailable(): Promise; /** * Get provider configuration */ getConfig(): EmbeddingProviderConfig; /** * Get rate limiter status (if configured) */ getRateLimitStatus(): { rpm: { current: number; limit: number; percentage: number; }; tpm: { current: number; limit: number; percentage: number; }; rpd: { current: number; limit: number; percentage: number; }; } | null; } /** * Factory function to create embedding providers from configuration. * * Dispatches to the local transformers.js provider for `provider: "transformers"` * (roadmap A5); every other provider type uses the Vercel-AI-SDK-backed * AISDKEmbeddingProvider. */ export declare function createProvider(config: EmbeddingProviderConfig, providerId: string): EmbeddingProvider; //# sourceMappingURL=provider.d.ts.map