@retrivora-ai/rag-engine 2.3.6 → 2.3.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@retrivora-ai/rag-engine",
3
- "version": "2.3.6",
3
+ "version": "2.3.7",
4
4
  "description": "Retrivora AI is a plug-and-play AI engine for RAG chat experiences — generic vector DB + LLM provider, embeddable or standalone.",
5
5
  "author": "Abhinav Alkuchi",
6
6
  "license": "UNLICENSED",
@@ -0,0 +1,273 @@
1
+ /**
2
+ * LicensedRetrivora — End-user facing SDK that only requires:
3
+ * - License Key (JWT from Retrivora)
4
+ * - Project ID (workspace identifier)
5
+ *
6
+ * Everything else (models, vector DB, embeddings) is resolved from
7
+ * the Retrivora admin dashboard based on tier.
8
+ *
9
+ * NOTE: This is a SERVER-ONLY module. Import from '@retrivora-ai/rag-engine/server'
10
+ * Do NOT import in client components.
11
+ */
12
+
13
+ import { LicenseConfigResolver } from './config/LicenseConfigResolver';
14
+ import { Pipeline } from './core/Pipeline';
15
+ import { RagConfig, VectorDBConfig, LLMConfig, EmbeddingConfig } from './config/RagConfig';
16
+ import {
17
+ initializeTelemetryService,
18
+ getTelemetryService,
19
+ shutdownTelemetryService,
20
+ EventType,
21
+ } from './telemetry/TelemetryService';
22
+ import { IngestDocument, ChatResponse } from './types';
23
+
24
+ export interface LicensedRetrivoraOptions {
25
+ licenseKey: string;
26
+ projectId: string;
27
+ userTelemetryUrl?: string; // Optional: user's telemetry endpoint
28
+ enableTelemetry?: boolean; // Default: true
29
+ }
30
+
31
+ /**
32
+ * End-user facing SDK for Retrivora RAG engine
33
+ */
34
+ export class LicensedRetrivora {
35
+ private licenseKey: string;
36
+ private projectId: string;
37
+ private pipeline?: Pipeline;
38
+ private ragConfig?: RagConfig;
39
+ private initialized: boolean = false;
40
+
41
+ constructor(options: LicensedRetrivoraOptions) {
42
+ this.licenseKey = options.licenseKey;
43
+ this.projectId = options.projectId;
44
+
45
+ // Initialize telemetry if enabled
46
+ if (options.enableTelemetry !== false) {
47
+ initializeTelemetryService(this.projectId, this.licenseKey, options.userTelemetryUrl);
48
+ }
49
+ }
50
+
51
+ /**
52
+ * Initialize the SDK by validating license and fetching config
53
+ */
54
+ private generateId(): string {
55
+ return `${Date.now()}-${Math.random().toString(36).substring(2, 9)}`;
56
+ }
57
+
58
+ /**
59
+ * Initialize the SDK by validating license and fetching config
60
+ */
61
+ async initialize(): Promise<void> {
62
+ if (this.initialized) {
63
+ console.log('[LicensedRetrivora] Already initialized');
64
+ return;
65
+ }
66
+
67
+ console.log('[LicensedRetrivora] Initializing with license key...');
68
+ const operationId = this.generateId();
69
+
70
+ try {
71
+ // Resolve configuration from license tier
72
+ const { llmConfig, embeddingConfig, vectorDbConfig, tierConfig } =
73
+ await LicenseConfigResolver.resolveFromLicense(this.licenseKey, this.projectId);
74
+
75
+ // Log successful configuration load
76
+ const telemetry = getTelemetryService();
77
+ if (telemetry) {
78
+ telemetry.recordEvent({
79
+ eventType: EventType.CONFIGURATION_LOADED,
80
+ operationId,
81
+ metadata: {
82
+ tier: tierConfig.tier,
83
+ llmModel: tierConfig.llmModel,
84
+ embeddingModel: tierConfig.embeddingModel,
85
+ vectorDb: tierConfig.vectorDb.provider,
86
+ },
87
+ });
88
+ }
89
+
90
+ // Build RAG config
91
+ this.ragConfig = {
92
+ projectId: this.projectId,
93
+ licenseKey: this.licenseKey,
94
+ llm: llmConfig,
95
+ embedding: embeddingConfig,
96
+ vectorDb: vectorDbConfig,
97
+ rag: {
98
+ topK: tierConfig.topK,
99
+ chunkSize: tierConfig.chunkSize,
100
+ chunkOverlap: tierConfig.chunkOverlap,
101
+ scoreThreshold: 0,
102
+ },
103
+ telemetry: {
104
+ enabled: true,
105
+ url: 'https://www.retrivora.com/api/telemetry',
106
+ },
107
+ };
108
+
109
+ // Initialize pipeline
110
+ this.pipeline = new Pipeline(this.ragConfig);
111
+ await this.pipeline.initialize();
112
+
113
+ this.initialized = true;
114
+ console.log(`[LicensedRetrivora] Initialized successfully for tier: ${tierConfig.tier}`);
115
+ } catch (error) {
116
+ const telemetry = getTelemetryService();
117
+ if (telemetry && error instanceof Error) {
118
+ telemetry.recordError(error, { operationId });
119
+ await telemetry.flush();
120
+ }
121
+ throw error;
122
+ }
123
+ }
124
+
125
+ /**
126
+ * Ingest documents (upload files to vector database)
127
+ * Returns success/failure status to end-user
128
+ */
129
+ async ingest(documents: IngestDocument[]): Promise<{ success: boolean; message: string; documentIds?: string[] }> {
130
+ if (!this.initialized || !this.pipeline) {
131
+ throw new Error('[LicensedRetrivora] SDK not initialized. Call initialize() first.');
132
+ }
133
+
134
+ const operationId = this.generateId();
135
+ const telemetry = getTelemetryService();
136
+ const startTime = Date.now();
137
+
138
+ try {
139
+ console.log(`[LicensedRetrivora] Starting ingestion of ${documents.length} document(s)`);
140
+
141
+ // Ingest documents
142
+ const results = await this.pipeline.ingest(documents);
143
+
144
+ const durationMs = Date.now() - startTime;
145
+
146
+ // Record successful ingestion
147
+ if (telemetry) {
148
+ for (const result of results) {
149
+ telemetry.recordDocumentIngested({
150
+ documentId: String(result.docId),
151
+ documentName: String(result.docId),
152
+ documentSize: 0, // TODO: track actual size
153
+ chunksGenerated: result.chunksIngested,
154
+ durationMs,
155
+ });
156
+ }
157
+ }
158
+
159
+ const successCount = results.length;
160
+ return {
161
+ success: successCount > 0,
162
+ message: `Successfully ingested ${successCount} document(s). Data is now available for search.`,
163
+ documentIds: results.map(r => String(r.docId)),
164
+ };
165
+ } catch (error) {
166
+ const durationMs = Date.now() - startTime;
167
+
168
+ if (telemetry && error instanceof Error) {
169
+ telemetry.recordError(error, { operationId, durationMs });
170
+ await telemetry.flush();
171
+ }
172
+
173
+ console.error('[LicensedRetrivora] Ingestion failed:', error);
174
+ throw error;
175
+ }
176
+ }
177
+
178
+ /**
179
+ * Execute a user query and get response
180
+ * Returns only the response text and sources (abstracted from underlying models)
181
+ */
182
+ async query(queryText: string, options?: { topK?: number }): Promise<ChatResponse> {
183
+ if (!this.initialized || !this.pipeline) {
184
+ throw new Error('[LicensedRetrivora] SDK not initialized. Call initialize() first.');
185
+ }
186
+
187
+ const operationId = this.generateId();
188
+ const telemetry = getTelemetryService();
189
+ const startTime = Date.now();
190
+
191
+ try {
192
+ console.log(`[LicensedRetrivora] Executing query: "${queryText.substring(0, 100)}..."`);
193
+
194
+ // Execute query
195
+ const response = await this.pipeline.runNormalQuery(queryText);
196
+
197
+ const durationMs = Date.now() - startTime;
198
+
199
+ // Record query execution
200
+ if (telemetry) {
201
+ telemetry.recordQueryExecuted({
202
+ queryText,
203
+ resultCount: response.sources?.length || 0,
204
+ durationMs,
205
+ operationId,
206
+ });
207
+ }
208
+
209
+ // Return abstracted response (hide implementation details)
210
+ return {
211
+ reply: response.reply,
212
+ sources: response.sources,
213
+ };
214
+ } catch (error) {
215
+ const durationMs = Date.now() - startTime;
216
+
217
+ if (telemetry && error instanceof Error) {
218
+ telemetry.recordError(error, { operationId, durationMs });
219
+ await telemetry.flush();
220
+ }
221
+
222
+ console.error('[LicensedRetrivora] Query failed:', error);
223
+ throw error;
224
+ }
225
+ }
226
+
227
+ /**
228
+ * Get license tier information (for UI display if needed)
229
+ */
230
+ async getTierInfo(): Promise<{ tier: string; model: string; embeddingModel: string }> {
231
+ if (!this.ragConfig) {
232
+ throw new Error('[LicensedRetrivora] SDK not initialized.');
233
+ }
234
+
235
+ return {
236
+ tier: 'Standard', // TODO: fetch from tier config
237
+ model: this.ragConfig.llm.model,
238
+ embeddingModel: this.ragConfig.embedding.model,
239
+ };
240
+ }
241
+
242
+ /**
243
+ * Cleanup and shutdown
244
+ */
245
+ async shutdown(): Promise<void> {
246
+ await shutdownTelemetryService();
247
+ this.initialized = false;
248
+ console.log('[LicensedRetrivora] Shutdown complete');
249
+ }
250
+
251
+ /**
252
+ * Check if SDK is initialized
253
+ */
254
+ isInitialized(): boolean {
255
+ return this.initialized;
256
+ }
257
+
258
+ /**
259
+ * Get current RAG configuration (for debugging)
260
+ */
261
+ getConfig(): RagConfig | undefined {
262
+ return this.ragConfig;
263
+ }
264
+ }
265
+
266
+ /**
267
+ * Convenience function to create and initialize SDK in one call
268
+ */
269
+ export async function createRetrivora(options: LicensedRetrivoraOptions): Promise<LicensedRetrivora> {
270
+ const retrivora = new LicensedRetrivora(options);
271
+ await retrivora.initialize();
272
+ return retrivora;
273
+ }
@@ -0,0 +1,296 @@
1
+ /**
2
+ * LicenseConfigResolver — Resolves LLM and embedding configuration
3
+ * based on the user's license tier from Retrivora admin dashboard.
4
+ *
5
+ * The end-user SDK only needs:
6
+ * - licenseKey (JWT token from Retrivora)
7
+ * - projectId (workspace identifier)
8
+ *
9
+ * Configuration includes:
10
+ * 1. Tier-based model selection (/api/v1/config)
11
+ * 2. Provider API credentials (/api/v1/credentials)
12
+ * 3. Vector DB and rate limit settings
13
+ *
14
+ * All provider API keys (Gemini, Groq, Pinecone) are stored on Retrivora backend
15
+ * and never exposed to end-users directly.
16
+ */
17
+
18
+ import axios, { AxiosInstance } from 'axios';
19
+ import { LLMConfig, EmbeddingConfig, VectorDBConfig } from './RagConfig';
20
+
21
+ export interface ProviderCredentials {
22
+ geminiApiKey?: string;
23
+ groqApiKey?: string;
24
+ pineconeApiKey?: string;
25
+ textEmbeddingProvider?: string;
26
+ }
27
+
28
+ export interface LicenseTierConfig {
29
+ tier: 'free_trial' | 'free' | 'hobby' | 'pro' | 'enterprise';
30
+ llmModel: string;
31
+ embeddingModel: string;
32
+ embeddingDimensions: number;
33
+ maxTokens: number;
34
+ chunkSize: number;
35
+ chunkOverlap: number;
36
+ topK: number;
37
+ vectorDb: {
38
+ provider: string;
39
+ indexName: string;
40
+ };
41
+ rateLimit?: {
42
+ requestsPerMinute: number;
43
+ tokensPerDay: number;
44
+ };
45
+ }
46
+
47
+ export interface ProviderCredentials {
48
+ geminiApiKey?: string;
49
+ groqApiKey?: string;
50
+ pineconeApiKey?: string;
51
+ textEmbeddingProvider?: string;
52
+ }
53
+
54
+ export interface ConfigAPIResponse {
55
+ success: boolean;
56
+ config?: LicenseTierConfig;
57
+ error?: string;
58
+ message?: string;
59
+ }
60
+
61
+ export interface CredentialsAPIResponse {
62
+ success: boolean;
63
+ credentials?: ProviderCredentials;
64
+ error?: string;
65
+ message?: string;
66
+ }
67
+
68
+ /**
69
+ * Resolver for license-based configuration.
70
+ * This is called by the end-user SDK after they instantiate with licenseKey + projectId.
71
+ */
72
+ export class LicenseConfigResolver {
73
+ private static readonly RETRIVORA_CONFIG_URL = 'https://www.retrivora.com/api/v1/config';
74
+ private static readonly RETRIVORA_CREDENTIALS_URL = 'https://www.retrivora.com/api/v1/credentials';
75
+ private static readonly CACHE_TTL_MS = 3600000; // 1 hour
76
+ private static configCache = new Map<string, { config: LicenseTierConfig; credentials: ProviderCredentials; timestamp: number }>();
77
+
78
+ /**
79
+ * Validate and resolve configuration from license key
80
+ * @param licenseKey JWT token from Retrivora (format: rtv_eyJ...)
81
+ * @param projectId Workspace identifier
82
+ * @returns Resolved LLM and embedding configs with provider credentials
83
+ */
84
+ static async resolveFromLicense(
85
+ licenseKey: string,
86
+ projectId: string
87
+ ): Promise<{
88
+ llmConfig: LLMConfig;
89
+ embeddingConfig: EmbeddingConfig;
90
+ vectorDbConfig: VectorDBConfig;
91
+ tierConfig: LicenseTierConfig;
92
+ credentials: ProviderCredentials;
93
+ }> {
94
+ // Check cache first
95
+ const cacheKey = `${licenseKey}:${projectId}`;
96
+ const cached = this.configCache.get(cacheKey);
97
+ if (cached && Date.now() - cached.timestamp < this.CACHE_TTL_MS) {
98
+ console.log('[LicenseConfigResolver] Using cached config for', projectId);
99
+ return this.buildConfigs(cached.config, cached.credentials, licenseKey, projectId);
100
+ }
101
+
102
+ // Fetch tier config from /api/v1/config
103
+ console.log('[LicenseConfigResolver] Fetching tier config from Retrivora...');
104
+ const tierConfig = await this.fetchTierConfig(licenseKey, projectId);
105
+
106
+ // Fetch provider credentials from /api/v1/credentials
107
+ console.log('[LicenseConfigResolver] Fetching provider credentials from Retrivora...');
108
+ const credentials = await this.fetchProviderCredentials(licenseKey, projectId);
109
+
110
+ // Cache both
111
+ this.configCache.set(cacheKey, { config: tierConfig, credentials, timestamp: Date.now() });
112
+
113
+ console.log('[LicenseConfigResolver] ✅ Successfully resolved config from Retrivora:', {
114
+ tier: tierConfig.tier,
115
+ llmModel: tierConfig.llmModel,
116
+ embeddingModel: tierConfig.embeddingModel,
117
+ });
118
+
119
+ return this.buildConfigs(tierConfig, credentials, licenseKey, projectId);
120
+ }
121
+
122
+ /**
123
+ * Fetch tier configuration from Retrivora config API
124
+ */
125
+ private static async fetchTierConfig(licenseKey: string, projectId: string): Promise<LicenseTierConfig> {
126
+ try {
127
+ const response = await axios.post<ConfigAPIResponse>(
128
+ this.RETRIVORA_CONFIG_URL,
129
+ { licenseKey, projectId },
130
+ {
131
+ headers: {
132
+ 'Content-Type': 'application/json',
133
+ Authorization: `Bearer ${licenseKey}`,
134
+ 'X-License-Key': licenseKey,
135
+ 'X-Project-ID': projectId,
136
+ },
137
+ timeout: 10000,
138
+ }
139
+ );
140
+
141
+ if (!response.data.success || !response.data.config) {
142
+ throw new Error(
143
+ `[LicenseConfigResolver] Config API returned error: ${response.data.error || response.data.message}`
144
+ );
145
+ }
146
+
147
+ console.log(`[LicenseConfigResolver] Resolved tier: ${response.data.config.tier}`);
148
+ return response.data.config;
149
+ } catch (err: any) {
150
+ console.error('[LicenseConfigResolver] Failed to fetch config:', err.message);
151
+ throw new Error(
152
+ `[LicenseConfigResolver] Could not validate license key or fetch tier configuration: ${err.message}`
153
+ );
154
+ }
155
+ }
156
+
157
+ /**
158
+ * Fetch provider credentials from Retrivora credentials API
159
+ */
160
+ private static async fetchProviderCredentials(licenseKey: string, projectId: string): Promise<ProviderCredentials> {
161
+ try {
162
+ const response = await axios.post<CredentialsAPIResponse>(
163
+ this.RETRIVORA_CREDENTIALS_URL,
164
+ { licenseKey, projectId },
165
+ {
166
+ headers: {
167
+ 'Content-Type': 'application/json',
168
+ Authorization: `Bearer ${licenseKey}`,
169
+ 'X-License-Key': licenseKey,
170
+ 'X-Project-ID': projectId,
171
+ },
172
+ timeout: 10000,
173
+ }
174
+ );
175
+
176
+ if (!response.data.success || !response.data.credentials) {
177
+ throw new Error(
178
+ `[LicenseConfigResolver] Credentials API returned error: ${response.data.error || response.data.message}`
179
+ );
180
+ }
181
+
182
+ console.log(`[LicenseConfigResolver] Retrieved provider credentials for project: ${projectId}`);
183
+ return response.data.credentials;
184
+ } catch (err: any) {
185
+ console.error('[LicenseConfigResolver] Failed to fetch credentials:', err.message);
186
+ throw new Error(
187
+ `[LicenseConfigResolver] Could not fetch provider credentials: ${err.message}`
188
+ );
189
+ }
190
+ }
191
+
192
+ /**
193
+ * Build LLM, embedding, and vector DB configs from tier and credentials
194
+ */
195
+ private static buildConfigs(
196
+ tierConfig: LicenseTierConfig,
197
+ credentials: ProviderCredentials,
198
+ licenseKey: string,
199
+ projectId: string
200
+ ): {
201
+ llmConfig: LLMConfig;
202
+ embeddingConfig: EmbeddingConfig;
203
+ vectorDbConfig: VectorDBConfig;
204
+ tierConfig: LicenseTierConfig;
205
+ credentials: ProviderCredentials;
206
+ } {
207
+ // Determine LLM provider and API key based on model
208
+ let llmProvider: 'gemini' | 'groq' | 'openai' = 'gemini';
209
+ let llmApiKey = credentials.geminiApiKey || licenseKey;
210
+
211
+ if (tierConfig.llmModel.includes('gpt')) {
212
+ llmProvider = 'openai';
213
+ } else if (tierConfig.llmModel.includes('groq') || tierConfig.llmModel.includes('llama')) {
214
+ llmProvider = 'groq';
215
+ llmApiKey = credentials.groqApiKey || licenseKey;
216
+ }
217
+
218
+ const llmConfig: LLMConfig = {
219
+ provider: llmProvider,
220
+ model: tierConfig.llmModel,
221
+ apiKey: llmApiKey,
222
+ maxTokens: tierConfig.maxTokens,
223
+ temperature: 0.7,
224
+ options: {},
225
+ };
226
+
227
+ // Embedding config - use Gemini's embeddings
228
+ const embeddingConfig: EmbeddingConfig = {
229
+ provider: 'gemini',
230
+ model: tierConfig.embeddingModel,
231
+ apiKey: credentials.geminiApiKey || licenseKey,
232
+ dimensions: tierConfig.embeddingDimensions,
233
+ options: {},
234
+ };
235
+
236
+ // Vector DB config - determined by tier from Retrivora
237
+ const vectorDbConfig: VectorDBConfig = {
238
+ provider: tierConfig.vectorDb.provider as any,
239
+ indexName: tierConfig.vectorDb.indexName,
240
+ options: this.getVectorDbOptions(tierConfig.vectorDb.provider, projectId, credentials),
241
+ };
242
+
243
+ return { llmConfig, embeddingConfig, vectorDbConfig, tierConfig, credentials };
244
+ }
245
+
246
+ /**
247
+ * Get provider-specific vector DB options
248
+ */
249
+ private static getVectorDbOptions(
250
+ provider: string,
251
+ projectId: string,
252
+ credentials: ProviderCredentials
253
+ ): Record<string, unknown> {
254
+ switch (provider) {
255
+ case 'pinecone':
256
+ return {
257
+ apiKey: credentials.pineconeApiKey,
258
+ indexName: `retrivora-${projectId}`,
259
+ };
260
+ case 'pgvector':
261
+ case 'postgresql':
262
+ return {
263
+ tables: [`documents_${projectId}`],
264
+ };
265
+ case 'mongodb':
266
+ return {
267
+ database: `retrivora_${projectId}`,
268
+ collection: `vectors_${projectId}`,
269
+ };
270
+ case 'qdrant':
271
+ return {
272
+ collection: `retrivora_${projectId}`,
273
+ };
274
+ default:
275
+ return {};
276
+ }
277
+ }
278
+
279
+ /**
280
+ * Clear configuration cache (useful for testing or manual refresh)
281
+ */
282
+ static clearCache(): void {
283
+ this.configCache.clear();
284
+ console.log('[LicenseConfigResolver] Configuration cache cleared');
285
+ }
286
+
287
+ /**
288
+ * Get cache stats for debugging
289
+ */
290
+ static getCacheStats(): { size: number; entries: string[] } {
291
+ return {
292
+ size: this.configCache.size,
293
+ entries: Array.from(this.configCache.keys()),
294
+ };
295
+ }
296
+ }
@@ -26,6 +26,49 @@ import {
26
26
  EMBEDDING_PROVIDERS,
27
27
  } from './constants';
28
28
 
29
+ /**
30
+ * Runtime tier configuration injected from the /api/v1/config + /api/v1/credentials
31
+ * API responses at app startup. This overrides static env-var defaults so that
32
+ * the tier-based model selection from the license is applied to all SDK calls.
33
+ */
34
+ export interface RuntimeTierConfig {
35
+ llmModel?: string;
36
+ embeddingModel?: string;
37
+ /** LiteLLM master key for authenticating with llm.retrivora.com */
38
+ litellmApiKey?: string;
39
+ /** LiteLLM gateway base URL (defaults to https://llm.retrivora.com/api/v1) */
40
+ llmBaseUrl?: string;
41
+ embeddingBaseUrl?: string;
42
+ maxTokens?: number;
43
+ topK?: number;
44
+ chunkSize?: number;
45
+ chunkOverlap?: number;
46
+ tier?: string;
47
+ }
48
+
49
+ /** Module-level store for runtime-injected tier config */
50
+ let _runtimeConfig: RuntimeTierConfig | null = null;
51
+
52
+ /**
53
+ * Inject tier-based config fetched from /api/v1/config + /api/v1/credentials.
54
+ * Call this once after successful license init (e.g. in validate-env.ts).
55
+ */
56
+ export function injectRuntimeConfig(config: RuntimeTierConfig): void {
57
+ _runtimeConfig = config;
58
+ console.log('[serverConfig] ✅ Runtime tier config injected:', {
59
+ tier: config.tier,
60
+ llmModel: config.llmModel,
61
+ embeddingModel: config.embeddingModel,
62
+ llmBaseUrl: config.llmBaseUrl,
63
+ hasLitellmKey: !!config.litellmApiKey,
64
+ });
65
+ }
66
+
67
+ /** Returns the currently injected runtime config, or null if not yet set. */
68
+ export function getRuntimeConfig(): RuntimeTierConfig | null {
69
+ return _runtimeConfig;
70
+ }
71
+
29
72
  function readString(env: Record<string, string | undefined>, name: string): string | undefined {
30
73
  const value = env[name]?.trim();
31
74
  return value ? value : undefined;
@@ -185,25 +228,42 @@ export function getEnvConfig(env: Record<string, string | undefined> = process.e
185
228
  }
186
229
  })();
187
230
 
188
- // Default gateway: retrivora.com/api/v1 acts as a secure proxy that validates the license key
189
- // and forwards to llm.retrivora.com with LITELLM_MASTER_KEY (which end-users never need to know).
190
- const defaultGatewayUrl =
191
- readString(env, 'LITELLM_BASE_URL') ??
192
- readString(env, 'LLM_BASE_URL') ??
193
- 'https://www.retrivora.com/api/v1';
231
+ // Tier-based model selection (resolved from license tier)
232
+ const tierBasedLLMModel = isFreeTier ? 'llama-3.1-8b-instant' : 'llama-3.3-70b-versatile';
233
+ const tierBasedEmbeddingModel = isFreeTier ? 'text-embedding-004' : 'text-embedding-004';
194
234
 
195
- const defaultLlmProvider = isFreeTier ? 'universal_rest' : 'universal_rest';
235
+ // Default gateway: always use LiteLLM at llm.retrivora.com for end-users
236
+ // End-users provide license key + project ID; the gateway validates and routes internally
237
+ const defaultGatewayUrl = 'https://llm.retrivora.com/api/v1';
238
+
239
+ // ── Runtime-injected tier config (from /api/v1/config + /api/v1/credentials) ──
240
+ // Priority: explicit env var > injected runtime config > static tier fallback
241
+ const rt = _runtimeConfig;
242
+
243
+ const defaultLlmProvider = 'universal_rest';
196
244
  const llmProvider = (readEnum(env, 'LLM_PROVIDER', defaultLlmProvider, LLM_PROVIDERS) ?? base?.llm?.provider ?? defaultLlmProvider) as LLMProvider;
197
- const llmBaseUrl = readString(env, 'LITELLM_BASE_URL') ?? readString(env, 'LLM_BASE_URL') ?? base?.llm?.baseUrl ?? defaultGatewayUrl;
198
- const llmModel = readString(env, 'LLM_MODEL') ?? base?.llm?.model ?? (isFreeTier ? 'llama-3.1-8b-instant' : (DEFAULT_MODEL_BY_PROVIDER[llmProvider] ?? 'gpt-4o'));
199
- const llmApiKey = llmApiKeyByProvider[llmProvider] ?? readString(env, 'LLM_API_KEY') ?? readString(env, 'LITELLM_MASTER_KEY') ?? readString(env, 'LITELLM_API_KEY') ?? base?.llm?.apiKey ?? licenseKey;
245
+ // llmBaseUrl: env var wins, then injected gateway URL, then static default
246
+ const llmBaseUrl = readString(env, 'LITELLM_BASE_URL') ?? readString(env, 'LLM_BASE_URL') ?? base?.llm?.baseUrl ?? rt?.llmBaseUrl ?? defaultGatewayUrl;
247
+ // llmModel: env var wins, then injected tier model, then static tier fallback
248
+ const llmModel = readString(env, 'LLM_MODEL') ?? base?.llm?.model ?? rt?.llmModel ?? tierBasedLLMModel;
249
+ // llmApiKey: LITELLM_MASTER_KEY env wins, then injected litellm key, then licenseKey
250
+ const llmApiKey = readString(env, 'LITELLM_MASTER_KEY') ?? rt?.litellmApiKey ?? base?.llm?.apiKey ?? licenseKey;
200
251
  const llmProfile = readString(env, 'LLM_UNIVERSAL_PROFILE') ?? (base?.llm?.options?.profile as string) ?? 'litellm';
201
252
 
202
- const defaultEmbeddingProvider = isFreeTier ? 'universal_rest' : 'universal_rest';
253
+ console.log('[serverConfig] 🔧 LLM Configuration:', {
254
+ baseUrl: llmBaseUrl,
255
+ model: llmModel,
256
+ provider: llmProvider,
257
+ hasApiKey: !!llmApiKey,
258
+ source: rt?.llmModel ? 'runtime-injected' : 'env/fallback',
259
+ });
260
+
261
+ const defaultEmbeddingProvider = 'universal_rest';
203
262
  const embeddingProvider = (readEnum(env, 'EMBEDDING_PROVIDER', defaultEmbeddingProvider, EMBEDDING_PROVIDERS) ?? base?.embedding?.provider ?? defaultEmbeddingProvider) as EmbeddingProvider;
204
- const embeddingBaseUrl = readString(env, 'LITELLM_BASE_URL') ?? readString(env, 'EMBEDDING_BASE_URL') ?? readString(env, 'LLM_BASE_URL') ?? base?.embedding?.baseUrl ?? defaultGatewayUrl;
205
- const embeddingModel = readString(env, 'EMBEDDING_MODEL') ?? base?.embedding?.model ?? 'text-embedding-004';
206
- const embeddingApiKey = embeddingApiKeyByProvider[embeddingProvider] ?? readString(env, 'EMBEDDING_API_KEY') ?? readString(env, 'LLM_API_KEY') ?? readString(env, 'LITELLM_MASTER_KEY') ?? readString(env, 'LITELLM_API_KEY') ?? base?.embedding?.apiKey ?? licenseKey;
263
+ const embeddingBaseUrl = readString(env, 'LITELLM_BASE_URL') ?? readString(env, 'EMBEDDING_BASE_URL') ?? readString(env, 'LLM_BASE_URL') ?? base?.embedding?.baseUrl ?? rt?.embeddingBaseUrl ?? rt?.llmBaseUrl ?? defaultGatewayUrl;
264
+ const embeddingModel = readString(env, 'EMBEDDING_MODEL') ?? base?.embedding?.model ?? rt?.embeddingModel ?? tierBasedEmbeddingModel;
265
+ // embeddingApiKey: same key as LLM (LiteLLM gateway handles both)
266
+ const embeddingApiKey = readString(env, 'LITELLM_MASTER_KEY') ?? rt?.litellmApiKey ?? base?.embedding?.apiKey ?? licenseKey;
207
267
  const embeddingProfile = readString(env, 'EMBEDDING_UNIVERSAL_PROFILE') ?? (base?.embedding?.options?.profile as string) ?? 'litellm';
208
268
 
209
269
  return {