@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/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.js +121 -63
- package/dist/handlers/index.mjs +121 -63
- package/dist/index.d.mts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +8 -8
- package/dist/index.mjs +8 -8
- package/dist/server.d.mts +251 -5
- package/dist/server.d.ts +251 -5
- package/dist/server.js +733 -65
- package/dist/server.mjs +723 -65
- package/package.json +1 -1
- package/src/LicensedRetrivora.ts +273 -0
- package/src/config/LicenseConfigResolver.ts +296 -0
- package/src/config/serverConfig.ts +74 -14
- package/src/core/LicenseVerifier.ts +7 -7
- package/src/handlers/index.ts +4 -0
- package/src/index.ts +9 -0
- package/src/llm/providers/UniversalLLMAdapter.ts +67 -12
- package/src/server.ts +23 -1
- package/src/telemetry/TelemetryService.ts +330 -0
|
@@ -51,13 +51,13 @@ export class LicenseVerifier {
|
|
|
51
51
|
// In production, this matches the private key held by Retrivora SaaS.
|
|
52
52
|
// Rotate this key only when the signing private key is replaced.
|
|
53
53
|
private static readonly PUBLIC_KEY = `-----BEGIN PUBLIC KEY-----
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
54
|
+
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyKvvOSYdCimG3b236ebV
|
|
55
|
+
jpG88xQiU8dSiaMahtBJDf4w0AewRj7hMNgmeZ1wyTOj8kVgeGBOolp1Z0QmTr6O
|
|
56
|
+
Tj4EDQ/zjsg12If7hSf3NiwLHCYmMLslyK0aaP6PQoTrYkBv3GNdkMecrIjKCijb
|
|
57
|
+
6VvDLLF3X696melTVM0z/d2e50BW2AlRw/rf1EqYi10d0BcPo5zje0XETr/WGmqu
|
|
58
|
+
sDiXtsP5MlwkL9X7RQY2uovRa+l/vOAqAfdmJ+OzXuk5P7mDUPb+gCtKnoDbb1tr
|
|
59
|
+
6UAu733dkUnetAcMqyT3fNPBLO66DCc6m9rGCYYAaG7piMEDwG0/ZySZBFbgllJc
|
|
60
|
+
6QIDAQAB
|
|
61
61
|
-----END PUBLIC KEY-----`;
|
|
62
62
|
|
|
63
63
|
/**
|
package/src/handlers/index.ts
CHANGED
|
@@ -216,6 +216,8 @@ function enforceLicense(
|
|
|
216
216
|
const projectId = config.projectId || '';
|
|
217
217
|
|
|
218
218
|
try {
|
|
219
|
+
// Always use the hardcoded public key baked into LicenseVerifier (dist/server.js).
|
|
220
|
+
// No runtime env-var override — the static key IS the production key.
|
|
219
221
|
const payload = LicenseVerifier.verify(licenseKey, projectId);
|
|
220
222
|
return { ok: true, payload };
|
|
221
223
|
} catch (err: any) {
|
|
@@ -1039,6 +1041,7 @@ export function createUploadHandler(
|
|
|
1039
1041
|
const onAuthorize = options?.onAuthorize;
|
|
1040
1042
|
|
|
1041
1043
|
return async function POST(req: any, context?: any) {
|
|
1044
|
+
console.log('Upload handler invoked');
|
|
1042
1045
|
const authResult = await checkAuth(req, onAuthorize);
|
|
1043
1046
|
if (authResult) return authResult as any;
|
|
1044
1047
|
|
|
@@ -1050,6 +1053,7 @@ export function createUploadHandler(
|
|
|
1050
1053
|
try {
|
|
1051
1054
|
const formData = await req.formData();
|
|
1052
1055
|
const files = formData.getAll('files') as File[];
|
|
1056
|
+
console.log('Files received for upload:', files.map(f => f.name));
|
|
1053
1057
|
const namespace = (formData.get('namespace') as string) || undefined;
|
|
1054
1058
|
const dimensionRaw = formData.get('dimension');
|
|
1055
1059
|
const dimension = dimensionRaw ? parseInt(dimensionRaw as string, 10) : undefined;
|
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
|
-
|
|
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
|
-
|
|
318
|
-
|
|
319
|
-
|
|
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
|
-
|
|
325
|
-
|
|
326
|
-
|
|
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
|
-
|
|
329
|
-
|
|
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
|
-
|
|
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
|
}
|
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 {
|
|
@@ -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
|
+
}
|