@retrivora-ai/rag-engine 2.3.5 → 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-tVzb9CdE.d.mts → BatchProcessor-BhLczWj5.d.mts} +179 -3
- package/dist/{BatchProcessor-CQDtlggz.d.ts → BatchProcessor-C42_RVWO.d.ts} +179 -3
- package/dist/{ILLMProvider-CcJ5N6XD.d.mts → ILLMProvider-B8TAVx3j.d.mts} +63 -1
- package/dist/{ILLMProvider-CcJ5N6XD.d.ts → ILLMProvider-B8TAVx3j.d.ts} +63 -1
- package/dist/handlers/index.d.mts +2 -2
- package/dist/handlers/index.d.ts +2 -2
- package/dist/handlers/index.js +493 -217
- package/dist/handlers/index.mjs +493 -217
- package/dist/{index-BwmyPJHp.d.ts → index-ejOWsaqb.d.ts} +1 -1
- package/dist/{index-CL-zF6bD.d.mts → index-xxQOFx-u.d.mts} +1 -1
- package/dist/index.css +9 -0
- package/dist/index.d.mts +19 -5
- package/dist/index.d.ts +19 -5
- package/dist/index.js +643 -207
- package/dist/index.mjs +683 -252
- package/dist/server.d.mts +253 -7
- package/dist/server.d.ts +253 -7
- package/dist/server.js +1145 -230
- package/dist/server.mjs +1131 -230
- package/package.json +1 -1
- package/src/LicensedRetrivora.ts +273 -0
- package/src/components/ChatWidget.tsx +44 -19
- package/src/components/ChatWindow.tsx +30 -14
- package/src/components/ConfigProvider.tsx +86 -6
- package/src/components/DocumentUpload.tsx +8 -2
- package/src/config/LicenseConfigResolver.ts +296 -0
- package/src/config/RagConfig.ts +4 -0
- package/src/config/serverConfig.ts +74 -14
- package/src/core/ConfigFetcher.ts +10 -1
- package/src/core/ConfigResolver.ts +1 -0
- package/src/core/LicenseValidator.ts +21 -12
- package/src/core/LicenseVerifier.ts +7 -11
- package/src/core/ProjectSettings.ts +247 -0
- package/src/handlers/index.ts +65 -5
- package/src/hooks/useProjectSettings.ts +105 -0
- package/src/index.css +9 -0
- package/src/index.ts +37 -0
- package/src/llm/providers/UniversalLLMAdapter.ts +67 -12
- package/src/server.ts +46 -2
- package/src/telemetry/TelemetryService.ts +330 -0
- package/src/types/props.ts +14 -2
|
@@ -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
|
@@ -40,9 +40,33 @@ export { LicenseVerifier } from './core/LicenseVerifier';
|
|
|
40
40
|
export { SDK_VERSION } from './version';
|
|
41
41
|
export { ProviderHealthCheck } from './core/ProviderHealthCheck';
|
|
42
42
|
export { BatchProcessor } from './core/BatchProcessor';
|
|
43
|
+
export { ConfigFetcher } from './core/ConfigFetcher';
|
|
44
|
+
export {
|
|
45
|
+
createDefaultProjectSettings,
|
|
46
|
+
normalizeProjectSettings,
|
|
47
|
+
projectSettingsToUiConfig,
|
|
48
|
+
} from './core/ProjectSettings';
|
|
49
|
+
export type {
|
|
50
|
+
RemoteConfig,
|
|
51
|
+
RemoteVectorConfig,
|
|
52
|
+
RemoteEmbeddingConfig,
|
|
53
|
+
RemoteLLMConfig,
|
|
54
|
+
} from './core/ConfigFetcher';
|
|
55
|
+
export type {
|
|
56
|
+
AuthMode,
|
|
57
|
+
ProjectAiSettings,
|
|
58
|
+
ProjectAuthSettings,
|
|
59
|
+
ProjectBrandingSettings,
|
|
60
|
+
ProjectChatSettings,
|
|
61
|
+
ProjectTelemetrySettings,
|
|
62
|
+
ProjectWidgetSettings,
|
|
63
|
+
ThemeMode,
|
|
64
|
+
WidgetPosition,
|
|
65
|
+
} from './core/ProjectSettings';
|
|
43
66
|
|
|
44
67
|
// ── Configuration Helpers ──────────────────────────────────────
|
|
45
|
-
export { getRagConfig } from './config/serverConfig';
|
|
68
|
+
export { getRagConfig, injectRuntimeConfig, getRuntimeConfig } from './config/serverConfig';
|
|
69
|
+
export type { RuntimeTierConfig } from './config/serverConfig';
|
|
46
70
|
export { ConfigBuilder, PRESETS, createFromPreset } from './config/ConfigBuilder';
|
|
47
71
|
export { EmbeddingStrategyResolver, EmbeddingStrategy } from './config/EmbeddingStrategy';
|
|
48
72
|
export { LLM_PROFILES, VECTOR_PROFILES } from './config/UniversalProfiles';
|
|
@@ -95,6 +119,27 @@ export {
|
|
|
95
119
|
export { LicenseValidator } from './core/LicenseValidator';
|
|
96
120
|
export type { LicenseValidationRequest, LicenseValidationResponse } from './core/LicenseValidator';
|
|
97
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
|
+
|
|
98
143
|
// ── Exceptions ────────────────────────────────────────────────
|
|
99
144
|
export type { RetrivoraErrorCode } from './exceptions';
|
|
100
145
|
export {
|
|
@@ -109,4 +154,3 @@ export {
|
|
|
109
154
|
LicenseValidationError,
|
|
110
155
|
wrapError,
|
|
111
156
|
} from './exceptions';
|
|
112
|
-
|
|
@@ -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
|
+
}
|
package/src/types/props.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { ReactNode, CSSProperties, MouseEvent, ElementType } from 'react';
|
|
|
2
2
|
import { Product, VectorMatch } from './index';
|
|
3
3
|
import { RagMessage } from './chat';
|
|
4
4
|
import { UIConfig } from '../config/RagConfig';
|
|
5
|
+
import { ProjectChatSettings, WidgetPosition } from '../core/ProjectSettings';
|
|
5
6
|
|
|
6
7
|
export interface ArchitectureCardProps {
|
|
7
8
|
icon: ReactNode;
|
|
@@ -69,7 +70,7 @@ export interface ChatWindowProps {
|
|
|
69
70
|
|
|
70
71
|
export interface ChatWidgetProps {
|
|
71
72
|
/** Position of the floating button. Defaults to bottom-right. */
|
|
72
|
-
position?:
|
|
73
|
+
position?: WidgetPosition;
|
|
73
74
|
/** Called when the user clicks 'Add to Cart' on a product */
|
|
74
75
|
onAddToCart?: (product: Product) => void;
|
|
75
76
|
/** Optional custom API URL to send chat messages to */
|
|
@@ -134,13 +135,24 @@ export interface ClientConfig {
|
|
|
134
135
|
model?: string;
|
|
135
136
|
dimensions?: number;
|
|
136
137
|
};
|
|
138
|
+
licenseKey?: string;
|
|
139
|
+
headers?: Record<string, string>;
|
|
140
|
+
retrivoraApiBase?: string;
|
|
141
|
+
projectSettings?: ProjectChatSettings;
|
|
142
|
+
autoLoadProjectSettings?: boolean;
|
|
143
|
+
isProjectSettingsLoading?: boolean;
|
|
137
144
|
}
|
|
138
145
|
|
|
139
146
|
export interface ConfigProviderProps {
|
|
140
147
|
config?: {
|
|
141
148
|
projectId?: string;
|
|
142
149
|
ui?: Partial<UIConfig>;
|
|
143
|
-
embedding?: { model?: string; dimensions?: number }
|
|
150
|
+
embedding?: { model?: string; dimensions?: number };
|
|
151
|
+
licenseKey?: string;
|
|
152
|
+
headers?: Record<string, string>;
|
|
153
|
+
retrivoraApiBase?: string;
|
|
154
|
+
projectSettings?: Partial<ProjectChatSettings>;
|
|
155
|
+
autoLoadProjectSettings?: boolean;
|
|
144
156
|
};
|
|
145
157
|
children: ReactNode;
|
|
146
158
|
}
|