@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.
- package/dist/{BatchProcessor-Dfn23VAd.d.mts → BatchProcessor-BhLczWj5.d.mts} +100 -2
- package/dist/{BatchProcessor-ByBl-1MA.d.ts → BatchProcessor-C42_RVWO.d.ts} +100 -2
- package/dist/handlers/index.d.mts +1 -1
- package/dist/handlers/index.d.ts +1 -1
- package/dist/handlers/index.js +340 -172
- package/dist/handlers/index.mjs +342 -174
- package/dist/{index-ejOWsaqb.d.ts → index-BQd6Bax0.d.ts} +1 -1
- package/dist/{index-xxQOFx-u.d.mts → index-C7aHK7_g.d.mts} +1 -1
- package/dist/index.d.mts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +114 -87
- package/dist/index.mjs +114 -87
- package/dist/server.d.mts +263 -37
- package/dist/server.d.ts +263 -37
- package/dist/server.js +957 -178
- package/dist/server.mjs +949 -180
- package/package.json +1 -1
- package/src/LicensedRetrivora.ts +273 -0
- package/src/components/ChatWidget.tsx +0 -1
- package/src/components/ConfigProvider.tsx +2 -1
- package/src/config/LicenseConfigResolver.ts +296 -0
- package/src/config/serverConfig.ts +75 -16
- package/src/core/ConfigFetcher.ts +36 -3
- package/src/core/DatabaseStorage.ts +13 -13
- package/src/core/LicenseValidator.ts +5 -3
- package/src/core/LicenseVerifier.ts +85 -120
- package/src/core/Retrivora.ts +1 -1
- package/src/core/VectorPlugin.ts +1 -1
- package/src/handlers/index.ts +25 -11
- package/src/index.ts +9 -0
- package/src/llm/providers/UniversalLLMAdapter.ts +67 -12
- package/src/providers/vectordb/PineconeProvider.ts +69 -2
- package/src/server.ts +23 -1
- package/src/telemetry/TelemetryService.ts +330 -0
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TelemetryService — Tracks LLM calls, embedding operations, and user interactions
|
|
3
|
+
* for billing, analytics, and usage monitoring.
|
|
4
|
+
*
|
|
5
|
+
* Sends telemetry to both:
|
|
6
|
+
* - Admin dashboard (retrivora.com)
|
|
7
|
+
* - User's own dashboard (if configured)
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import axios, { AxiosInstance } from 'axios';
|
|
11
|
+
|
|
12
|
+
export enum EventType {
|
|
13
|
+
EMBEDDING_GENERATED = 'embedding_generated',
|
|
14
|
+
LLM_CHAT_COMPLETION = 'llm_chat_completion',
|
|
15
|
+
DOCUMENT_INGESTED = 'document_ingested',
|
|
16
|
+
QUERY_EXECUTED = 'query_executed',
|
|
17
|
+
ERROR_OCCURRED = 'error_occurred',
|
|
18
|
+
CONFIGURATION_LOADED = 'configuration_loaded',
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface TelemetryEvent {
|
|
22
|
+
eventType: EventType;
|
|
23
|
+
projectId: string;
|
|
24
|
+
timestamp: number;
|
|
25
|
+
|
|
26
|
+
// Operation details
|
|
27
|
+
operationId?: string; // Unique ID for correlating related events
|
|
28
|
+
durationMs?: number;
|
|
29
|
+
|
|
30
|
+
// LLM-specific
|
|
31
|
+
model?: string;
|
|
32
|
+
inputTokens?: number;
|
|
33
|
+
outputTokens?: number;
|
|
34
|
+
totalTokens?: number;
|
|
35
|
+
|
|
36
|
+
// Embedding-specific
|
|
37
|
+
embeddingModel?: string;
|
|
38
|
+
vectorDimensions?: number;
|
|
39
|
+
textLength?: number;
|
|
40
|
+
|
|
41
|
+
// Document ingestion
|
|
42
|
+
documentId?: string;
|
|
43
|
+
documentName?: string;
|
|
44
|
+
documentSize?: number;
|
|
45
|
+
chunksGenerated?: number;
|
|
46
|
+
|
|
47
|
+
// Query
|
|
48
|
+
queryText?: string;
|
|
49
|
+
resultCount?: number;
|
|
50
|
+
|
|
51
|
+
// Error tracking
|
|
52
|
+
errorCode?: string;
|
|
53
|
+
errorMessage?: string;
|
|
54
|
+
stackTrace?: string;
|
|
55
|
+
|
|
56
|
+
// Custom metadata
|
|
57
|
+
metadata?: Record<string, unknown>;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Telemetry service for tracking usage, billing, and analytics
|
|
62
|
+
*/
|
|
63
|
+
export class TelemetryService {
|
|
64
|
+
private static readonly RETRIVORA_TELEMETRY_URL = 'https://www.retrivora.com/api/telemetry';
|
|
65
|
+
private static readonly USER_TELEMETRY_URL_ENV_KEY = 'TELEMETRY_URL';
|
|
66
|
+
|
|
67
|
+
private http: AxiosInstance;
|
|
68
|
+
private projectId: string;
|
|
69
|
+
private licenseKey: string;
|
|
70
|
+
private userTelemetryUrl?: string;
|
|
71
|
+
private batchSize: number = 10;
|
|
72
|
+
private flushIntervalMs: number = 30000; // 30 seconds
|
|
73
|
+
private eventQueue: TelemetryEvent[] = [];
|
|
74
|
+
private flushTimer?: NodeJS.Timeout;
|
|
75
|
+
private enabled: boolean = true;
|
|
76
|
+
|
|
77
|
+
constructor(projectId: string, licenseKey: string, userTelemetryUrl?: string) {
|
|
78
|
+
this.projectId = projectId;
|
|
79
|
+
this.licenseKey = licenseKey;
|
|
80
|
+
this.userTelemetryUrl = userTelemetryUrl || process.env[TelemetryService.USER_TELEMETRY_URL_ENV_KEY];
|
|
81
|
+
|
|
82
|
+
this.http = axios.create({
|
|
83
|
+
timeout: 5000,
|
|
84
|
+
headers: {
|
|
85
|
+
'Content-Type': 'application/json',
|
|
86
|
+
'X-License-Key': licenseKey,
|
|
87
|
+
'X-Project-ID': projectId,
|
|
88
|
+
},
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
// Start automatic flush interval
|
|
92
|
+
this.startAutoFlush();
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Record a telemetry event
|
|
97
|
+
*/
|
|
98
|
+
recordEvent(event: Partial<TelemetryEvent>): void {
|
|
99
|
+
if (!this.enabled) return;
|
|
100
|
+
|
|
101
|
+
// Exclude licenseKey from event payload (privacy/security)
|
|
102
|
+
const { licenseKey, ...safeEvent } = event as any;
|
|
103
|
+
|
|
104
|
+
const completeEvent: TelemetryEvent = {
|
|
105
|
+
eventType: event.eventType || EventType.ERROR_OCCURRED,
|
|
106
|
+
projectId: this.projectId,
|
|
107
|
+
timestamp: event.timestamp || Date.now(),
|
|
108
|
+
...safeEvent,
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
this.eventQueue.push(completeEvent);
|
|
112
|
+
|
|
113
|
+
// Flush if batch size reached
|
|
114
|
+
if (this.eventQueue.length >= this.batchSize) {
|
|
115
|
+
this.flush().catch(err => {
|
|
116
|
+
console.warn('[TelemetryService] Flush error:', err.message);
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Record LLM completion event
|
|
123
|
+
*/
|
|
124
|
+
recordLLMCompletion(data: {
|
|
125
|
+
model: string;
|
|
126
|
+
inputTokens: number;
|
|
127
|
+
outputTokens: number;
|
|
128
|
+
durationMs: number;
|
|
129
|
+
operationId?: string;
|
|
130
|
+
metadata?: Record<string, unknown>;
|
|
131
|
+
}): void {
|
|
132
|
+
this.recordEvent({
|
|
133
|
+
eventType: EventType.LLM_CHAT_COMPLETION,
|
|
134
|
+
model: data.model,
|
|
135
|
+
inputTokens: data.inputTokens,
|
|
136
|
+
outputTokens: data.outputTokens,
|
|
137
|
+
totalTokens: data.inputTokens + data.outputTokens,
|
|
138
|
+
durationMs: data.durationMs,
|
|
139
|
+
operationId: data.operationId,
|
|
140
|
+
metadata: data.metadata,
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Record embedding generation event
|
|
146
|
+
*/
|
|
147
|
+
recordEmbeddingGenerated(data: {
|
|
148
|
+
embeddingModel: string;
|
|
149
|
+
vectorDimensions: number;
|
|
150
|
+
textLength: number;
|
|
151
|
+
durationMs: number;
|
|
152
|
+
operationId?: string;
|
|
153
|
+
}): void {
|
|
154
|
+
this.recordEvent({
|
|
155
|
+
eventType: EventType.EMBEDDING_GENERATED,
|
|
156
|
+
embeddingModel: data.embeddingModel,
|
|
157
|
+
vectorDimensions: data.vectorDimensions,
|
|
158
|
+
textLength: data.textLength,
|
|
159
|
+
durationMs: data.durationMs,
|
|
160
|
+
operationId: data.operationId,
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Record document ingestion event
|
|
166
|
+
*/
|
|
167
|
+
recordDocumentIngested(data: {
|
|
168
|
+
documentId: string;
|
|
169
|
+
documentName: string;
|
|
170
|
+
documentSize: number;
|
|
171
|
+
chunksGenerated: number;
|
|
172
|
+
durationMs: number;
|
|
173
|
+
}): void {
|
|
174
|
+
this.recordEvent({
|
|
175
|
+
eventType: EventType.DOCUMENT_INGESTED,
|
|
176
|
+
documentId: data.documentId,
|
|
177
|
+
documentName: data.documentName,
|
|
178
|
+
documentSize: data.documentSize,
|
|
179
|
+
chunksGenerated: data.chunksGenerated,
|
|
180
|
+
durationMs: data.durationMs,
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Record query execution event
|
|
186
|
+
*/
|
|
187
|
+
recordQueryExecuted(data: {
|
|
188
|
+
queryText: string;
|
|
189
|
+
resultCount: number;
|
|
190
|
+
durationMs: number;
|
|
191
|
+
operationId?: string;
|
|
192
|
+
}): void {
|
|
193
|
+
this.recordEvent({
|
|
194
|
+
eventType: EventType.QUERY_EXECUTED,
|
|
195
|
+
queryText: data.queryText,
|
|
196
|
+
resultCount: data.resultCount,
|
|
197
|
+
durationMs: data.durationMs,
|
|
198
|
+
operationId: data.operationId,
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Record error event
|
|
204
|
+
*/
|
|
205
|
+
recordError(error: Error, context?: Record<string, unknown>): void {
|
|
206
|
+
this.recordEvent({
|
|
207
|
+
eventType: EventType.ERROR_OCCURRED,
|
|
208
|
+
errorCode: (error as any).code || 'UNKNOWN',
|
|
209
|
+
errorMessage: error.message,
|
|
210
|
+
stackTrace: error.stack,
|
|
211
|
+
metadata: context,
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Flush all queued events to telemetry endpoints
|
|
217
|
+
*/
|
|
218
|
+
async flush(): Promise<void> {
|
|
219
|
+
if (this.eventQueue.length === 0) return;
|
|
220
|
+
|
|
221
|
+
const events = this.eventQueue.splice(0, this.batchSize);
|
|
222
|
+
|
|
223
|
+
// Create payload without license key (send only in headers)
|
|
224
|
+
const payload = {
|
|
225
|
+
events: events.map(e => {
|
|
226
|
+
const { ...eventWithoutKey } = e;
|
|
227
|
+
return eventWithoutKey;
|
|
228
|
+
}),
|
|
229
|
+
batchId: `${this.projectId}_${Date.now()}`,
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
// Send to Retrivora admin dashboard
|
|
233
|
+
try {
|
|
234
|
+
await this.http.post(TelemetryService.RETRIVORA_TELEMETRY_URL, payload);
|
|
235
|
+
console.log(`[TelemetryService] Flushed ${events.length} events to Retrivora`);
|
|
236
|
+
} catch (err: any) {
|
|
237
|
+
console.warn('[TelemetryService] Failed to send telemetry to Retrivora:', err.message);
|
|
238
|
+
// Re-queue events if send fails
|
|
239
|
+
this.eventQueue.unshift(...events);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// Send to user's telemetry endpoint if configured
|
|
243
|
+
if (this.userTelemetryUrl) {
|
|
244
|
+
try {
|
|
245
|
+
await axios.post(this.userTelemetryUrl, payload, {
|
|
246
|
+
timeout: 5000,
|
|
247
|
+
headers: {
|
|
248
|
+
'Content-Type': 'application/json',
|
|
249
|
+
'X-Project-ID': this.projectId,
|
|
250
|
+
},
|
|
251
|
+
});
|
|
252
|
+
console.log(`[TelemetryService] Flushed ${events.length} events to user telemetry`);
|
|
253
|
+
} catch (err: any) {
|
|
254
|
+
console.warn('[TelemetryService] Failed to send telemetry to user endpoint:', err.message);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* Start automatic flush interval
|
|
261
|
+
*/
|
|
262
|
+
private startAutoFlush(): void {
|
|
263
|
+
this.flushTimer = setInterval(() => {
|
|
264
|
+
this.flush().catch(err => {
|
|
265
|
+
console.warn('[TelemetryService] Auto-flush error:', err.message);
|
|
266
|
+
});
|
|
267
|
+
}, this.flushIntervalMs);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Stop telemetry service and flush remaining events
|
|
272
|
+
*/
|
|
273
|
+
async shutdown(): Promise<void> {
|
|
274
|
+
if (this.flushTimer) {
|
|
275
|
+
clearInterval(this.flushTimer);
|
|
276
|
+
}
|
|
277
|
+
await this.flush();
|
|
278
|
+
this.enabled = false;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* Disable telemetry temporarily
|
|
283
|
+
*/
|
|
284
|
+
disable(): void {
|
|
285
|
+
this.enabled = false;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Enable telemetry
|
|
290
|
+
*/
|
|
291
|
+
enable(): void {
|
|
292
|
+
this.enabled = true;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Get current queue size (for debugging)
|
|
297
|
+
*/
|
|
298
|
+
getQueueSize(): number {
|
|
299
|
+
return this.eventQueue.length;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
// Singleton instance
|
|
304
|
+
let globalTelemetryService: TelemetryService | null = null;
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Get or create global telemetry service instance
|
|
308
|
+
*/
|
|
309
|
+
export function getTelemetryService(): TelemetryService | null {
|
|
310
|
+
return globalTelemetryService;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* Initialize global telemetry service
|
|
315
|
+
*/
|
|
316
|
+
export function initializeTelemetryService(projectId: string, licenseKey: string, userTelemetryUrl?: string): TelemetryService {
|
|
317
|
+
globalTelemetryService = new TelemetryService(projectId, licenseKey, userTelemetryUrl);
|
|
318
|
+
console.log('[TelemetryService] Initialized for project:', projectId);
|
|
319
|
+
return globalTelemetryService;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* Shutdown global telemetry service
|
|
324
|
+
*/
|
|
325
|
+
export async function shutdownTelemetryService(): Promise<void> {
|
|
326
|
+
if (globalTelemetryService) {
|
|
327
|
+
await globalTelemetryService.shutdown();
|
|
328
|
+
globalTelemetryService = null;
|
|
329
|
+
}
|
|
330
|
+
}
|