@retrivora-ai/rag-engine 2.3.6 → 2.3.8

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.
Files changed (34) hide show
  1. package/dist/{BatchProcessor-Dfn23VAd.d.mts → BatchProcessor-BhLczWj5.d.mts} +100 -2
  2. package/dist/{BatchProcessor-ByBl-1MA.d.ts → BatchProcessor-C42_RVWO.d.ts} +100 -2
  3. package/dist/handlers/index.d.mts +1 -1
  4. package/dist/handlers/index.d.ts +1 -1
  5. package/dist/handlers/index.js +340 -172
  6. package/dist/handlers/index.mjs +342 -174
  7. package/dist/{index-ejOWsaqb.d.ts → index-BQd6Bax0.d.ts} +1 -1
  8. package/dist/{index-xxQOFx-u.d.mts → index-C7aHK7_g.d.mts} +1 -1
  9. package/dist/index.d.mts +2 -2
  10. package/dist/index.d.ts +2 -2
  11. package/dist/index.js +114 -87
  12. package/dist/index.mjs +114 -87
  13. package/dist/server.d.mts +263 -37
  14. package/dist/server.d.ts +263 -37
  15. package/dist/server.js +957 -178
  16. package/dist/server.mjs +949 -180
  17. package/package.json +1 -1
  18. package/src/LicensedRetrivora.ts +273 -0
  19. package/src/components/ChatWidget.tsx +0 -1
  20. package/src/components/ConfigProvider.tsx +2 -1
  21. package/src/config/LicenseConfigResolver.ts +296 -0
  22. package/src/config/serverConfig.ts +75 -16
  23. package/src/core/ConfigFetcher.ts +36 -3
  24. package/src/core/DatabaseStorage.ts +13 -13
  25. package/src/core/LicenseValidator.ts +5 -3
  26. package/src/core/LicenseVerifier.ts +85 -120
  27. package/src/core/Retrivora.ts +1 -1
  28. package/src/core/VectorPlugin.ts +1 -1
  29. package/src/handlers/index.ts +25 -11
  30. package/src/index.ts +9 -0
  31. package/src/llm/providers/UniversalLLMAdapter.ts +67 -12
  32. package/src/providers/vectordb/PineconeProvider.ts +69 -2
  33. package/src/server.ts +23 -1
  34. package/src/telemetry/TelemetryService.ts +330 -0
@@ -206,17 +206,29 @@ function resolveLicenseKey(
206
206
  * console warning but passes (fail-open local-dev behavior); in production any
207
207
  * invalid / expired / missing key produces a hard 403.
208
208
  */
209
- function enforceLicense(
209
+ async function enforceLicense(
210
210
  req: NextRequest,
211
211
  config: RagConfig,
212
212
  bodyLicenseKey?: string
213
- ): { ok: true; payload: LicensePayload } | { ok: false; response: Response } {
213
+ ): Promise<{ ok: true; payload: LicensePayload } | { ok: false; response: Response }> {
214
214
  const rawKey = resolveLicenseKey(req, config, bodyLicenseKey);
215
215
  const licenseKey = (rawKey || '').trim().replace(/^["']|["']$/g, '').trim();
216
216
  const projectId = config.projectId || '';
217
217
 
218
218
  try {
219
- const payload = LicenseVerifier.verify(licenseKey, projectId);
219
+ // Always use the async verifier which hits the Retrivora backend API.
220
+ let payload;
221
+ try {
222
+ payload = await LicenseVerifier.verifyAsync(licenseKey, projectId);
223
+ } catch (err: any) {
224
+ return {
225
+ ok: false,
226
+ response: NextResponse.json(
227
+ { error: `License validation failed: ${err.message}` },
228
+ { status: 401 }
229
+ )
230
+ };
231
+ }
220
232
  return { ok: true, payload };
221
233
  } catch (err: any) {
222
234
  const message =
@@ -490,7 +502,7 @@ export function createChatHandler(
490
502
  const bodyObj = (body ?? {}) as any;
491
503
 
492
504
  // 2. License enforcement (after body parse so we can read body.licenseKey)
493
- const licenseCheck = enforceLicense(req as NextRequest, config, bodyObj?.licenseKey);
505
+ const licenseCheck = await enforceLicense(req as NextRequest, config, bodyObj?.licenseKey);
494
506
  if (!licenseCheck.ok) return licenseCheck.response as any;
495
507
 
496
508
  // 2b. Free Tier limits check (hobby / free / free_trial tiers)
@@ -641,7 +653,7 @@ export function createStreamHandler(
641
653
  const bodyObj = (body ?? {}) as any;
642
654
 
643
655
  // 2b. License enforcement (for JSON-body requests)
644
- const licenseCheck = enforceLicense(req as NextRequest, config, bodyObj?.licenseKey);
656
+ const licenseCheck = await enforceLicense(req as NextRequest, config, bodyObj?.licenseKey);
645
657
  if (!licenseCheck.ok) return licenseCheck.response as any;
646
658
 
647
659
  // 2c. Free Tier limits check (hobby / free / free_trial tiers)
@@ -841,7 +853,7 @@ export function createIngestHandler(
841
853
  const body = await req.json();
842
854
 
843
855
  // License enforcement before ingestion
844
- const licenseCheck = enforceLicense(req as NextRequest, config, body?.licenseKey);
856
+ const licenseCheck = await enforceLicense(req as NextRequest, config, body?.licenseKey);
845
857
  if (!licenseCheck.ok) return licenseCheck.response as any;
846
858
 
847
859
  const { documents, namespace } = body as {
@@ -947,7 +959,7 @@ export function createHealthHandler(
947
959
  *
948
960
  * Client flow: `LicenseValidator.validate()` calls this URL, receives
949
961
  * authoritative DB-backed status (ACTIVE / TERMINATED / SUSPENDED / EXPIRED /
950
- * REVOKED), and falls back to pure-local `LicenseVerifier.verify()` if the
962
+ // REVOKED), and falls back to pure-local `LicenseVerifier.verifyAsync()` if the
951
963
  * server is unreachable or returns an error.
952
964
  *
953
965
  * This handler does NOT enforce license — it is the verifier itself. It does,
@@ -986,7 +998,7 @@ export function createLicenseHandler(
986
998
 
987
999
  const projectId = body?.projectId || config.projectId || '';
988
1000
 
989
- const payload = LicenseVerifier.verify(licenseKey, projectId);
1001
+ const payload = await LicenseVerifier.verifyAsync(licenseKey, projectId);
990
1002
 
991
1003
  return NextResponse.json({
992
1004
  valid: true,
@@ -1039,17 +1051,19 @@ export function createUploadHandler(
1039
1051
  const onAuthorize = options?.onAuthorize;
1040
1052
 
1041
1053
  return async function POST(req: any, context?: any) {
1054
+ console.log('Upload handler invoked');
1042
1055
  const authResult = await checkAuth(req, onAuthorize);
1043
1056
  if (authResult) return authResult as any;
1044
1057
 
1045
1058
  // License enforcement (uploads use headers/env for license key, no JSON body)
1046
1059
  const config = plugin.getConfig();
1047
- const licenseCheck = enforceLicense(req as NextRequest, config);
1060
+ const licenseCheck = await enforceLicense(req as NextRequest, config);
1048
1061
  if (!licenseCheck.ok) return licenseCheck.response as any;
1049
1062
 
1050
1063
  try {
1051
1064
  const formData = await req.formData();
1052
1065
  const files = formData.getAll('files') as File[];
1066
+ console.log('Files received for upload:', files.map(f => f.name));
1053
1067
  const namespace = (formData.get('namespace') as string) || undefined;
1054
1068
  const dimensionRaw = formData.get('dimension');
1055
1069
  const dimension = dimensionRaw ? parseInt(dimensionRaw as string, 10) : undefined;
@@ -1235,7 +1249,7 @@ export function createSuggestionsHandler(
1235
1249
 
1236
1250
  // License enforcement for auto-suggestions endpoint
1237
1251
  const config = plugin.getConfig();
1238
- const licenseCheck = enforceLicense(req, config);
1252
+ const licenseCheck = await enforceLicense(req, config);
1239
1253
  if (!licenseCheck.ok) return licenseCheck.response as any;
1240
1254
  let effectiveLicensePayload = licenseCheck.payload;
1241
1255
 
@@ -1251,7 +1265,7 @@ export function createSuggestionsHandler(
1251
1265
  bodyLicenseKey = body.licenseKey;
1252
1266
  // Re-check license if body contained a key override
1253
1267
  if (bodyLicenseKey) {
1254
- const recheck = enforceLicense(req, config, bodyLicenseKey);
1268
+ const recheck = await enforceLicense(req, config, bodyLicenseKey);
1255
1269
  if (!recheck.ok) return recheck.response as any;
1256
1270
  effectiveLicensePayload = recheck.payload;
1257
1271
  }
package/src/index.ts CHANGED
@@ -27,6 +27,15 @@ export { useProjectSettings } from './hooks/useProjectSettings';
27
27
  // ── Utils ─────────────────────────────────────────────────────
28
28
  export { addSynonyms } from './utils/synonyms';
29
29
 
30
+ // ── License Config Resolver (Server-only, types exported) ────
31
+ // Implementation available via server.ts import only
32
+ // export { LicenseConfigResolver } from './config/LicenseConfigResolver';
33
+ export type { LicenseTierConfig, ProviderCredentials } from './config/LicenseConfigResolver';
34
+
35
+ // ── Telemetry Service (Server-only) ────────────────────────────
36
+ // Only available via server.ts import
37
+ // export { TelemetryService, EventType, ... } from './telemetry/TelemetryService';
38
+
30
39
  // ── Visualization Types ────────────────────────────────────────
31
40
  // Runtime rendering utilities are exported from the server entrypoint to
32
41
  // avoid pulling the rendering engine into client bundles unnecessarily.
@@ -111,6 +111,10 @@ export class UniversalLLMAdapter implements ILLMProvider {
111
111
 
112
112
  // Tier 1: Remote HTTP POST to Retrivora Cloud Proxy (e.g. https://www.retrivora.com/api/v1/chat/completions)
113
113
  try {
114
+ console.debug(`[UniversalLLMAdapter] POST ${this.baseUrl}${path}`);
115
+ console.debug(`[UniversalLLMAdapter] Model: ${this.model}`);
116
+ console.debug(`[UniversalLLMAdapter] Auth: Bearer ${this.apiKey?.substring(0, 20) || 'none'}...`);
117
+
114
118
  const { data } = await this.http.post(path, payload);
115
119
  const extractPath = this.opts.responseExtractPath ?? 'choices[0].message.content';
116
120
  const result = resolvePath(data, extractPath) ?? extractContent(data);
@@ -118,11 +122,20 @@ export class UniversalLLMAdapter implements ILLMProvider {
118
122
  return String(result);
119
123
  }
120
124
  } catch (httpErr: any) {
125
+ console.error('[UniversalLLMAdapter] ❌ HTTP chat request failed:', {
126
+ status: httpErr?.response?.status,
127
+ statusText: httpErr?.response?.statusText,
128
+ url: httpErr?.config?.url,
129
+ errorData: httpErr?.response?.data,
130
+ message: httpErr?.message,
131
+ });
132
+
121
133
  // Tier 2: In-process fallback (only if HTTP call fails and server registered dispatch on globalThis)
122
134
  const _g = globalThis as any;
123
135
  const dispatch = _g.__retrivoraDispatchChat;
124
136
 
125
137
  if (typeof dispatch === 'function') {
138
+ console.debug('[UniversalLLMAdapter] 🔄 Attempting in-process fallback dispatch');
126
139
  const res = await dispatch({
127
140
  model: this.model,
128
141
  messages: formattedMessages as any,
@@ -303,39 +316,81 @@ export class UniversalLLMAdapter implements ILLMProvider {
303
316
  ? buildPayload(this.opts.embedPayloadTemplate, { input: text, model: this.model })
304
317
  : { input: text, model: this.model };
305
318
 
319
+ // First attempt: try HTTP call with fallback
306
320
  try {
307
321
  const { data } = await this.http.post(path, payload);
308
322
  const extractPath = this.opts.embedExtractPath ?? 'data[0].embedding';
309
323
  const vector = resolvePath(data, extractPath) as number[] | undefined;
310
324
  if (Array.isArray(vector)) return vector;
311
- } catch {
325
+
326
+ // If we get here, the API succeeded but the response format was invalid
327
+ console.warn(
328
+ `[UniversalLLMAdapter] Embedding API returned unexpected format for path '${extractPath}'. Response:`,
329
+ data
330
+ );
331
+ } catch (err: any) {
332
+ console.warn(
333
+ `[UniversalLLMAdapter] HTTP embedding request failed: ${err?.message || err}`,
334
+ { path, model: this.model }
335
+ );
336
+
312
337
  // In-process fallback for embeddings
313
338
  const _g = globalThis as any;
314
339
  const dispatchEmbed = _g.__retrivoraDispatchEmbedding;
315
340
 
316
341
  if (typeof dispatchEmbed === 'function') {
317
- const res = await dispatchEmbed({ input: text, model: this.model }, this.apiKey);
318
- if (res?.data?.[0]?.embedding) {
319
- return res.data[0].embedding;
342
+ try {
343
+ const res = await dispatchEmbed({ input: text, model: this.model }, this.apiKey);
344
+ if (res?.data?.[0]?.embedding) {
345
+ return res.data[0].embedding;
346
+ }
347
+ } catch (dispatchErr: any) {
348
+ console.warn('[UniversalLLMAdapter] In-process dispatch embedding failed:', dispatchErr);
320
349
  }
321
350
  }
322
351
  }
323
352
 
324
- const { data } = await this.http.post(path, payload);
325
- const extractPath = this.opts.embedExtractPath ?? 'data[0].embedding';
326
- const vector = resolvePath(data, extractPath) as number[] | undefined;
353
+ // Second attempt: retry the HTTP call
354
+ try {
355
+ const { data } = await this.http.post(path, payload);
356
+ const extractPath = this.opts.embedExtractPath ?? 'data[0].embedding';
357
+ const vector = resolvePath(data, extractPath) as number[] | undefined;
327
358
 
328
- if (!Array.isArray(vector)) {
329
- throw new Error(`[UniversalLLMAdapter] Could not extract embedding vector from path '${extractPath}' in response.`);
359
+ if (Array.isArray(vector)) {
360
+ return vector;
361
+ }
362
+
363
+ console.error(
364
+ `[UniversalLLMAdapter] Retry embedding failed: unexpected response format for path '${extractPath}'. Response:`,
365
+ data
366
+ );
367
+ throw new Error(
368
+ `[UniversalLLMAdapter] Could not extract embedding vector. ` +
369
+ `Expected array at '${extractPath}', got: ${typeof vector}. ` +
370
+ `Response keys: ${Object.keys(data || {})}.`
371
+ );
372
+ } catch (err: any) {
373
+ console.error(
374
+ `[UniversalLLMAdapter] Both embedding attempts failed. Path: ${path}, Model: ${this.model}`,
375
+ { error: err?.message, statusCode: err?.response?.status, response: err?.response?.data }
376
+ );
377
+ throw new Error(
378
+ `[UniversalLLMAdapter] Failed to generate embedding after 2 attempts: ${err?.message || err}`
379
+ );
330
380
  }
331
-
332
- return vector;
333
381
  }
334
382
 
335
383
  async batchEmbed(texts: string[]): Promise<number[][]> {
336
384
  const results: number[][] = [];
337
385
  for (const text of texts) {
338
- results.push(await this.embed(text));
386
+ try {
387
+ const vector = await this.embed(text);
388
+ results.push(vector);
389
+ } catch (err) {
390
+ console.warn(`[UniversalLLMAdapter] batchEmbed failed for text (first 50 chars): ${text.slice(0, 50)}...`, err);
391
+ // Don't push anything - the error will propagate
392
+ throw err;
393
+ }
339
394
  }
340
395
  return results;
341
396
  }
@@ -12,6 +12,7 @@ import { ConfigFetcher } from '../../core/ConfigFetcher';
12
12
  export class PineconeProvider extends BaseVectorProvider {
13
13
  private client!: Pinecone;
14
14
  private apiKey: string;
15
+ private licenseKey?: string;
15
16
 
16
17
  constructor(config: VectorDBConfig) {
17
18
  super(config);
@@ -19,6 +20,15 @@ export class PineconeProvider extends BaseVectorProvider {
19
20
  this.apiKey = opts.apiKey || process.env.PINECONE_API_KEY || '';
20
21
  }
21
22
 
23
+ private getControlPlaneUrl(): string {
24
+ const urls = [
25
+ process.env.RETRIVORA_CONTROL_PLANE_URL,
26
+ process.env.NEXT_PUBLIC_RETRIVORA_CONTROL_PLANE_URL,
27
+ 'https://www.retrivora.com'
28
+ ].filter((url): url is string => Boolean(url));
29
+ return urls[0].replace(/\/$/, '');
30
+ }
31
+
22
32
  static getValidator(): IProviderValidator {
23
33
  return {
24
34
  validate(config: Record<string, unknown>): ValidationError[] {
@@ -89,6 +99,7 @@ export class PineconeProvider extends BaseVectorProvider {
89
99
 
90
100
  if (!key) {
91
101
  const licenseKey = process.env.RETRIVORA_LICENSE_KEY || process.env.NEXT_PUBLIC_RETRIVORA_LICENSE_KEY;
102
+ this.licenseKey = licenseKey;
92
103
  const jwtProjectId = (() => {
93
104
  if (!licenseKey) return undefined;
94
105
  try {
@@ -102,6 +113,7 @@ export class PineconeProvider extends BaseVectorProvider {
102
113
  })();
103
114
 
104
115
  const projectId = process.env.RAG_PROJECT_ID || process.env.NEXT_PUBLIC_RAG_PROJECT_ID || jwtProjectId || this.projectId || 'default';
116
+ this.projectId = projectId;
105
117
 
106
118
  const remoteConfig = await ConfigFetcher.fetchRemoteVectorConfig(projectId, licenseKey);
107
119
 
@@ -158,6 +170,21 @@ export class PineconeProvider extends BaseVectorProvider {
158
170
  }
159
171
 
160
172
  async upsert(doc: UpsertDocument, namespace?: string): Promise<void> {
173
+ if (!this.client) {
174
+ if (!this.licenseKey) throw new Error('[PineconeProvider] Cannot execute vector operation: Missing API key and License key.');
175
+ const res = await fetch(`${this.getControlPlaneUrl()}/api/v1/vectors/upsert`, {
176
+ method: 'POST',
177
+ headers: { 'Content-Type': 'application/json', 'x-license-key': this.licenseKey },
178
+ body: JSON.stringify({
179
+ projectId: this.projectId,
180
+ indexName: this.indexName,
181
+ namespace: namespace || this.projectId,
182
+ records: [{ id: String(doc.id), values: doc.vector, metadata: { content: doc.content, ...(doc.metadata ?? {}) } }]
183
+ })
184
+ });
185
+ if (!res.ok) throw new Error(`[PineconeProvider] Proxy upsert failed: ${await res.text()}`);
186
+ return;
187
+ }
161
188
  const targetIdx = await this.getActiveIndex(namespace);
162
189
  await targetIdx.upsert({
163
190
  records: [{
@@ -169,8 +196,26 @@ export class PineconeProvider extends BaseVectorProvider {
169
196
  }
170
197
 
171
198
  async batchUpsert(docs: UpsertDocument[], namespace?: string): Promise<void> {
172
- const targetIdx = await this.getActiveIndex(namespace);
173
199
  const BATCH = 100;
200
+ if (!this.client) {
201
+ if (!this.licenseKey) throw new Error('[PineconeProvider] Cannot execute vector operation: Missing API key and License key.');
202
+ for (let i = 0; i < docs.length; i += BATCH) {
203
+ const records = docs.slice(i, i + BATCH).map((d) => ({
204
+ id: String(d.id), values: d.vector, metadata: { content: d.content, ...(d.metadata ?? {}) }
205
+ }));
206
+ const res = await fetch(`${this.getControlPlaneUrl()}/api/v1/vectors/upsert`, {
207
+ method: 'POST',
208
+ headers: { 'Content-Type': 'application/json', 'x-license-key': this.licenseKey },
209
+ body: JSON.stringify({
210
+ projectId: this.projectId, indexName: this.indexName, namespace: namespace || this.projectId, records
211
+ })
212
+ });
213
+ if (!res.ok) throw new Error(`[PineconeProvider] Proxy batch upsert failed: ${await res.text()}`);
214
+ }
215
+ return;
216
+ }
217
+
218
+ const targetIdx = await this.getActiveIndex(namespace);
174
219
  for (let i = 0; i < docs.length; i += BATCH) {
175
220
  const records = docs.slice(i, i + BATCH).map((d) => ({
176
221
  id: String(d.id),
@@ -187,8 +232,30 @@ export class PineconeProvider extends BaseVectorProvider {
187
232
  namespace?: string,
188
233
  filter?: Record<string, unknown>
189
234
  ): Promise<VectorMatch[]> {
190
- const targetIdx = await this.getActiveIndex(namespace);
191
235
  const pineconeFilter = this.sanitizeFilter(filter);
236
+
237
+ if (!this.client) {
238
+ if (!this.licenseKey) throw new Error('[PineconeProvider] Cannot execute vector operation: Missing API key and License key.');
239
+ const res = await fetch(`${this.getControlPlaneUrl()}/api/v1/vectors/query`, {
240
+ method: 'POST',
241
+ headers: { 'Content-Type': 'application/json', 'x-license-key': this.licenseKey },
242
+ body: JSON.stringify({
243
+ projectId: this.projectId,
244
+ indexName: this.indexName,
245
+ namespace: namespace || this.projectId,
246
+ vector,
247
+ topK,
248
+ filter: pineconeFilter
249
+ })
250
+ });
251
+ if (!res.ok) throw new Error(`[PineconeProvider] Proxy query failed: ${await res.text()}`);
252
+ const data = await res.json();
253
+ return (data.matches ?? []).map((m: any) => ({
254
+ id: m.id, score: m.score ?? 0, content: (m.metadata?.content || '') as string, metadata: m.metadata as Record<string, unknown>
255
+ }));
256
+ }
257
+
258
+ const targetIdx = await this.getActiveIndex(namespace);
192
259
  const result = await targetIdx.query({
193
260
  vector,
194
261
  topK,
package/src/server.ts CHANGED
@@ -65,7 +65,8 @@ export type {
65
65
  } from './core/ProjectSettings';
66
66
 
67
67
  // ── Configuration Helpers ──────────────────────────────────────
68
- export { getRagConfig } from './config/serverConfig';
68
+ export { getRagConfig, injectRuntimeConfig, getRuntimeConfig } from './config/serverConfig';
69
+ export type { RuntimeTierConfig } from './config/serverConfig';
69
70
  export { ConfigBuilder, PRESETS, createFromPreset } from './config/ConfigBuilder';
70
71
  export { EmbeddingStrategyResolver, EmbeddingStrategy } from './config/EmbeddingStrategy';
71
72
  export { LLM_PROFILES, VECTOR_PROFILES } from './config/UniversalProfiles';
@@ -118,6 +119,27 @@ export {
118
119
  export { LicenseValidator } from './core/LicenseValidator';
119
120
  export type { LicenseValidationRequest, LicenseValidationResponse } from './core/LicenseValidator';
120
121
 
122
+ // ── Licensed Retrivora SDK (Server-only) ──────────────────────
123
+ // End-user SDK that only requires license key + project ID
124
+ // Only available via server.ts import, not from main index.ts
125
+ export { LicensedRetrivora, createRetrivora } from './LicensedRetrivora';
126
+ export type { LicensedRetrivoraOptions } from './LicensedRetrivora';
127
+
128
+ // ── License Config Resolver (Server-only) ──────────────────────
129
+ export { LicenseConfigResolver } from './config/LicenseConfigResolver';
130
+ export type { LicenseTierConfig, ProviderCredentials } from './config/LicenseConfigResolver';
131
+
132
+ // ── Telemetry Service (Server-only) ─────────────────────────────
133
+ // Tracks usage, billing, and analytics
134
+ export {
135
+ TelemetryService,
136
+ EventType,
137
+ getTelemetryService,
138
+ initializeTelemetryService,
139
+ shutdownTelemetryService,
140
+ } from './telemetry/TelemetryService';
141
+ export type { TelemetryEvent } from './telemetry/TelemetryService';
142
+
121
143
  // ── Exceptions ────────────────────────────────────────────────
122
144
  export type { RetrivoraErrorCode } from './exceptions';
123
145
  export {