@retrivora-ai/rag-engine 2.4.2 → 2.4.5

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 (140) hide show
  1. package/dist/{BatchProcessor-zbQMaZHm.d.mts → BatchProcessor-Csy_K1qQ.d.mts} +2 -0
  2. package/dist/{BatchProcessor-D42E706a.d.ts → BatchProcessor-Dg18zZGk.d.ts} +2 -0
  3. package/dist/handlers/index.js +31 -5
  4. package/dist/handlers/index.mjs +31 -5
  5. package/dist/index.d.mts +3 -3
  6. package/dist/index.d.ts +3 -3
  7. package/dist/index.js +41 -21
  8. package/dist/index.min.css +2 -0
  9. package/dist/index.mjs +41 -21
  10. package/dist/server.d.mts +2 -2
  11. package/dist/server.d.ts +2 -2
  12. package/dist/server.js +32 -5
  13. package/dist/server.mjs +32 -5
  14. package/package.json +5 -4
  15. package/src/LicensedRetrivora.ts +0 -278
  16. package/src/app/api/chat/route.ts +0 -4
  17. package/src/app/api/health/route.ts +0 -5
  18. package/src/app/api/ingest/route.ts +0 -27
  19. package/src/app/api/suggestions/route.ts +0 -5
  20. package/src/app/api/upload/route.ts +0 -5
  21. package/src/app/constants.tsx +0 -270
  22. package/src/app/favicon.ico +0 -0
  23. package/src/app/globals.css +0 -449
  24. package/src/app/layout.tsx +0 -22
  25. package/src/app/page.tsx +0 -64
  26. package/src/app/types.ts +0 -30
  27. package/src/browser.tsx +0 -139
  28. package/src/components/ChatWidget.tsx +0 -393
  29. package/src/components/ChatWindow.tsx +0 -952
  30. package/src/components/CodeViewer.tsx +0 -56
  31. package/src/components/ConfigProvider.tsx +0 -123
  32. package/src/components/DocumentUpload.tsx +0 -354
  33. package/src/components/DynamicChart.tsx +0 -297
  34. package/src/components/HourglassLoader.tsx +0 -87
  35. package/src/components/MarkdownComponents.tsx +0 -186
  36. package/src/components/MessageBubble.tsx +0 -498
  37. package/src/components/ObservabilityPanel.tsx +0 -374
  38. package/src/components/ProductCard.tsx +0 -107
  39. package/src/components/ProductCarousel.tsx +0 -97
  40. package/src/components/SourceCard.tsx +0 -61
  41. package/src/components/ThemeProvider.tsx +0 -8
  42. package/src/components/ThemeToggle.tsx +0 -29
  43. package/src/components/ThinkingBlock.tsx +0 -75
  44. package/src/components/UIDispatcher.tsx +0 -342
  45. package/src/components/VisualizationRenderer.tsx +0 -621
  46. package/src/components/constants.tsx +0 -503
  47. package/src/config/ConfigBuilder.ts +0 -285
  48. package/src/config/EmbeddingStrategy.ts +0 -148
  49. package/src/config/LicenseConfigResolver.ts +0 -298
  50. package/src/config/RagConfig.ts +0 -328
  51. package/src/config/UniversalProfiles.ts +0 -82
  52. package/src/config/constants.ts +0 -64
  53. package/src/config/serverConfig.ts +0 -378
  54. package/src/config/uiConstants.ts +0 -47
  55. package/src/core/BatchProcessor.ts +0 -385
  56. package/src/core/CircuitBreaker.ts +0 -118
  57. package/src/core/ConfigFetcher.ts +0 -220
  58. package/src/core/ConfigResolver.ts +0 -165
  59. package/src/core/ConfigValidator.ts +0 -197
  60. package/src/core/DatabaseStorage.ts +0 -526
  61. package/src/core/FreeTierLimitsGuard.ts +0 -281
  62. package/src/core/LLMRouter.ts +0 -79
  63. package/src/core/LangChainAgent.ts +0 -132
  64. package/src/core/LicenseValidator.ts +0 -284
  65. package/src/core/LicenseVerifier.ts +0 -261
  66. package/src/core/MultiAgentCoordinator.ts +0 -239
  67. package/src/core/Pipeline.ts +0 -1279
  68. package/src/core/PluginManager.ts +0 -277
  69. package/src/core/ProjectSettings.ts +0 -247
  70. package/src/core/ProviderHealthCheck.ts +0 -133
  71. package/src/core/ProviderInterfaces.ts +0 -37
  72. package/src/core/ProviderRegistry.ts +0 -156
  73. package/src/core/QueryProcessor.ts +0 -399
  74. package/src/core/Retrivora.ts +0 -98
  75. package/src/core/VectorPlugin.ts +0 -193
  76. package/src/core/mcp.ts +0 -261
  77. package/src/exceptions/index.ts +0 -131
  78. package/src/handlers/index.ts +0 -1625
  79. package/src/hooks/useHydrated.ts +0 -13
  80. package/src/hooks/useProjectSettings.ts +0 -105
  81. package/src/hooks/useRagChat.ts +0 -436
  82. package/src/hooks/useStoredMessages.ts +0 -64
  83. package/src/index.css +0 -4269
  84. package/src/index.ts +0 -137
  85. package/src/lib/plugin.ts +0 -24
  86. package/src/llm/ILLMProvider.ts +0 -60
  87. package/src/llm/LLMFactory.ts +0 -141
  88. package/src/llm/providers/AnthropicProvider.ts +0 -215
  89. package/src/llm/providers/GeminiProvider.ts +0 -311
  90. package/src/llm/providers/GroqProvider.ts +0 -184
  91. package/src/llm/providers/OllamaProvider.ts +0 -232
  92. package/src/llm/providers/OpenAIProvider.ts +0 -188
  93. package/src/llm/providers/QwenProvider.ts +0 -199
  94. package/src/llm/providers/UniversalLLMAdapter.ts +0 -422
  95. package/src/llm/utils.ts +0 -46
  96. package/src/providers/graphdb/BaseGraphProvider.ts +0 -43
  97. package/src/providers/graphdb/SimpleGraphProvider.ts +0 -66
  98. package/src/providers/vectordb/BaseVectorProvider.ts +0 -98
  99. package/src/providers/vectordb/ChromaDBProvider.ts +0 -130
  100. package/src/providers/vectordb/MilvusProvider.ts +0 -134
  101. package/src/providers/vectordb/MongoDBProvider.ts +0 -250
  102. package/src/providers/vectordb/MultiTablePostgresProvider.ts +0 -461
  103. package/src/providers/vectordb/PineconeProvider.ts +0 -295
  104. package/src/providers/vectordb/PostgreSQLProvider.ts +0 -227
  105. package/src/providers/vectordb/QdrantProvider.ts +0 -294
  106. package/src/providers/vectordb/RedisProvider.ts +0 -105
  107. package/src/providers/vectordb/UniversalVectorProvider.ts +0 -221
  108. package/src/providers/vectordb/WeaviateProvider.ts +0 -172
  109. package/src/rag/DocumentChunker.ts +0 -144
  110. package/src/rag/EntityExtractor.ts +0 -90
  111. package/src/rag/LlamaIndexIngestor.ts +0 -60
  112. package/src/rag/Reranker.ts +0 -106
  113. package/src/rendering/IntentClassifier.ts +0 -167
  114. package/src/rendering/RendererRegistry.ts +0 -164
  115. package/src/rendering/RuleEngine.ts +0 -67
  116. package/src/rendering/VisualizationDecisionEngine.ts +0 -164
  117. package/src/rendering/__tests__/VisualizationDecisionEngine.test.ts +0 -237
  118. package/src/rendering/index.ts +0 -12
  119. package/src/rendering/rules/Rule1SpecificInfoRule.ts +0 -28
  120. package/src/rendering/rules/Rule2ComparisonRule.ts +0 -24
  121. package/src/rendering/rules/Rule3ProductDiscoveryRule.ts +0 -28
  122. package/src/rendering/rules/Rule4AnalyticalRule.ts +0 -38
  123. package/src/rendering/rules/Rule5MixedResponseRule.ts +0 -29
  124. package/src/rendering/rules/Rule6SmallResultSetRule.ts +0 -24
  125. package/src/rendering/rules/Rule7LargeTableRule.ts +0 -35
  126. package/src/rendering/types.ts +0 -96
  127. package/src/server.ts +0 -156
  128. package/src/tailwind.css +0 -194
  129. package/src/telemetry/TelemetryService.ts +0 -330
  130. package/src/types/chat.ts +0 -89
  131. package/src/types/index.ts +0 -278
  132. package/src/types/pdf-parse.d.ts +0 -13
  133. package/src/types/props.ts +0 -169
  134. package/src/utils/DocumentParser.ts +0 -111
  135. package/src/utils/ProductExtractor.ts +0 -350
  136. package/src/utils/SchemaMapper.ts +0 -176
  137. package/src/utils/UITransformer.ts +0 -1812
  138. package/src/utils/synonyms.ts +0 -80
  139. package/src/utils/templateUtils.ts +0 -78
  140. package/src/version.ts +0 -6
@@ -1,422 +0,0 @@
1
- import axios, { AxiosInstance } from 'axios';
2
- import { ILLMProvider } from '../ILLMProvider';
3
- import { ChatMessage } from '../../types';
4
- import { LLMConfig, EmbeddingConfig } from '../../config/RagConfig';
5
- import { resolvePath, buildPayload } from '../../utils/templateUtils';
6
- import { LLM_PROFILES } from '../../config/UniversalProfiles';
7
-
8
- interface UniversalLLMOptions {
9
- baseUrl?: string;
10
- headers?: Record<string, string>;
11
- chatPath?: string;
12
- embedPath?: string;
13
- pingPath?: string;
14
- chatPayloadTemplate?: string;
15
- embedPayloadTemplate?: string;
16
- responseExtractPath?: string;
17
- embedExtractPath?: string;
18
- timeout?: number;
19
- }
20
-
21
- export function extractContent(obj: unknown): string | undefined {
22
- if (!obj || typeof obj !== 'object') return undefined;
23
- const choice = (obj as any).choices?.[0];
24
- if (!choice) return undefined;
25
- if (typeof choice.message?.content === 'string') return choice.message.content;
26
- if (typeof choice.delta?.content === 'string') return choice.delta.content;
27
- if (typeof choice.text === 'string') return choice.text;
28
- if (typeof choice.content === 'string') return choice.content;
29
- return undefined;
30
- }
31
-
32
- export class UniversalLLMAdapter implements ILLMProvider {
33
- private readonly http: AxiosInstance;
34
- private readonly model: string;
35
- private readonly opts: UniversalLLMOptions;
36
- private readonly systemPrompt: string;
37
- private readonly maxTokens: number;
38
- private readonly temperature: number;
39
- private readonly baseUrl: string;
40
- private readonly apiKey?: string;
41
- private readonly resolvedHeaders: Record<string, string>;
42
-
43
- constructor(config: LLMConfig | EmbeddingConfig) {
44
- this.model = config.model;
45
-
46
- const llmConfig = config as LLMConfig;
47
- const options = (llmConfig.options as Record<string, unknown>) ?? {};
48
- let profile: Record<string, unknown> = {};
49
-
50
- if (typeof options.profile === 'string') {
51
- profile = (LLM_PROFILES as Record<string, Record<string, unknown>>)[options.profile] || {};
52
- } else if (typeof options.profile === 'object' && options.profile !== null) {
53
- profile = options.profile as Record<string, unknown>;
54
- }
55
-
56
- this.opts = { ...profile, ...(llmConfig.options ?? {}) } as UniversalLLMOptions;
57
- this.systemPrompt = llmConfig.systemPrompt ?? 'You are a helpful AI assistant. Use the provided context to answer the user.';
58
- this.maxTokens = llmConfig.maxTokens ?? 1024;
59
- this.temperature = llmConfig.temperature ?? 0;
60
- this.apiKey = config.apiKey;
61
-
62
- this.baseUrl = llmConfig.baseUrl ?? this.opts.baseUrl ?? '';
63
- if (!this.baseUrl) {
64
- throw new Error('[UniversalLLMAdapter] baseUrl is required in config or config.options');
65
- }
66
-
67
- this.resolvedHeaders = {
68
- 'Content-Type': 'application/json',
69
- ...(config.apiKey ? { Authorization: `Bearer ${config.apiKey}` } : {}),
70
- ...(this.opts.headers || {}),
71
- };
72
-
73
- // console.log('[UniversalLLMAdapter] Initialized with:', {
74
- // model: this.model,
75
- // baseUrl: this.baseUrl,
76
- // hasApiKey: !!this.apiKey,
77
- // apiKeyPrefix: this.apiKey ? `${this.apiKey.slice(0, 12)}...` : undefined,
78
- // });
79
-
80
- this.http = axios.create({
81
- baseURL: this.baseUrl,
82
- headers: this.resolvedHeaders,
83
- timeout: this.opts.timeout ?? 60_000,
84
- });
85
- }
86
-
87
- async chat(messages: ChatMessage[], context?: string): Promise<string> {
88
- const path = this.opts.chatPath ?? '/chat/completions';
89
-
90
- const safeMessages = (Array.isArray(messages) ? messages : [])
91
- .filter((m): m is ChatMessage => Boolean(m && typeof m === 'object'))
92
- .map((m) => ({
93
- role: m.role || 'user',
94
- content: typeof m.content === 'string' ? m.content : (m.content != null ? String(m.content) : ''),
95
- }));
96
-
97
- const formattedMessages = [
98
- { role: 'system', content: `${this.systemPrompt}\n\nContext:\n${context ?? 'None'}` },
99
- ...safeMessages,
100
- ];
101
-
102
- let payload: unknown;
103
- if (this.opts.chatPayloadTemplate) {
104
- payload = buildPayload(this.opts.chatPayloadTemplate, {
105
- model: this.model,
106
- messages: formattedMessages,
107
- maxTokens: this.maxTokens,
108
- temperature: this.temperature,
109
- });
110
- } else {
111
- payload = {
112
- model: this.model,
113
- messages: formattedMessages,
114
- max_tokens: this.maxTokens,
115
- temperature: this.temperature,
116
- };
117
- }
118
-
119
- // Tier 1: Remote HTTP POST to Retrivora Cloud Proxy (e.g. https://www.retrivora.com/api/v1/chat/completions)
120
- try {
121
- console.debug(`[UniversalLLMAdapter] POST ${this.baseUrl}${path}`);
122
- console.debug(`[UniversalLLMAdapter] Model: ${this.model}`);
123
- console.debug(`[UniversalLLMAdapter] Auth: Bearer ${this.apiKey?.substring(0, 20) || 'none'}...`);
124
-
125
- const { data } = await this.http.post(path, payload);
126
- const extractPath = this.opts.responseExtractPath ?? 'choices[0].message.content';
127
- const result = resolvePath(data, extractPath) ?? extractContent(data);
128
- if (result !== undefined) {
129
- return String(result);
130
- }
131
- } catch (httpErr: any) {
132
- console.error('[UniversalLLMAdapter] ❌ HTTP chat request failed:', {
133
- status: httpErr?.response?.status,
134
- statusText: httpErr?.response?.statusText,
135
- url: httpErr?.config?.url,
136
- errorData: httpErr?.response?.data,
137
- message: httpErr?.message,
138
- });
139
-
140
- // Tier 2: In-process fallback (only if HTTP call fails and server registered dispatch on globalThis)
141
- const _g = globalThis as any;
142
- const dispatch = _g.__retrivoraDispatchChat;
143
-
144
- if (typeof dispatch === 'function') {
145
- console.debug('[UniversalLLMAdapter] 🔄 Attempting in-process fallback dispatch');
146
- const res = await dispatch({
147
- model: this.model,
148
- messages: formattedMessages as any,
149
- max_tokens: this.maxTokens,
150
- temperature: this.temperature,
151
- }, this.apiKey);
152
-
153
- const content = extractContent(res?.response);
154
- if (content !== undefined) {
155
- return content;
156
- }
157
- }
158
- throw httpErr;
159
- }
160
-
161
- const { data } = await this.http.post(path, payload);
162
- const extractPath = this.opts.responseExtractPath ?? 'choices[0].message.content';
163
- const result = resolvePath(data, extractPath) ?? extractContent(data);
164
-
165
- if (result === undefined) {
166
- throw new Error(`[UniversalLLMAdapter] Could not extract text from path '${extractPath}' in response.`);
167
- }
168
-
169
- return String(result);
170
- }
171
-
172
- /**
173
- * Streaming chat using native fetch + ReadableStream.
174
- * Parses OpenAI-compatible SSE frames: `data: {...}\n\n`
175
- */
176
- async * chatStream(messages: ChatMessage[], context?: string): AsyncIterable<string> {
177
- const path = this.opts.chatPath ?? '/chat/completions';
178
- const url = `${this.baseUrl.replace(/\/$/, '')}${path}`;
179
- const extractPath = (this.opts.responseExtractPath ?? 'choices[0].message.content')
180
- .replace('message.content', 'delta.content');
181
-
182
- const safeMessages = (Array.isArray(messages) ? messages : [])
183
- .filter((m): m is ChatMessage => Boolean(m && typeof m === 'object'))
184
- .map((m) => ({
185
- role: m.role || 'user',
186
- content: typeof m.content === 'string' ? m.content : (m.content != null ? String(m.content) : ''),
187
- }));
188
-
189
- const formattedMessages = [
190
- { role: 'system', content: `${this.systemPrompt}\n\nContext:\n${context ?? 'None'}` },
191
- ...safeMessages,
192
- ];
193
-
194
- let payload: unknown;
195
- if (this.opts.chatPayloadTemplate) {
196
- payload = buildPayload(this.opts.chatPayloadTemplate, {
197
- model: this.model,
198
- messages: formattedMessages,
199
- maxTokens: this.maxTokens,
200
- temperature: this.temperature,
201
- });
202
- if (typeof payload === 'object' && payload !== null) {
203
- (payload as Record<string, unknown>).stream = true;
204
- }
205
- } else {
206
- payload = {
207
- model: this.model,
208
- messages: formattedMessages,
209
- max_tokens: this.maxTokens,
210
- temperature: this.temperature,
211
- stream: true,
212
- };
213
- }
214
-
215
- let streamBody: ReadableStream<Uint8Array> | null = null;
216
-
217
- // ── Tier 1: HTTP Stream Request to Retrivora Gateway Proxy ─────────────────
218
- try {
219
- const response = await fetch(url, {
220
- method: 'POST',
221
- headers: this.resolvedHeaders,
222
- body: typeof payload === 'string' ? payload : JSON.stringify(payload),
223
- });
224
-
225
- if (response.ok) {
226
- const contentType = response.headers.get('content-type') || '';
227
- if (contentType.includes('application/json')) {
228
- const json = await response.json();
229
- const text = (resolvePath(json, extractPath) as string | undefined) ?? extractContent(json);
230
- if (text && typeof text === 'string') {
231
- yield text;
232
- return;
233
- }
234
- } else if (response.body) {
235
- streamBody = response.body;
236
- }
237
- } else {
238
- const errorText = await response.text().catch(() => response.statusText);
239
- console.warn(`[UniversalLLMAdapter] Remote HTTP stream returned ${response.status}: ${errorText}. Attempting in-process fallback...`);
240
- }
241
- } catch (fetchErr: any) {
242
- console.warn(`[UniversalLLMAdapter] Remote HTTP stream fetch warning: ${fetchErr?.message}. Attempting in-process fallback...`);
243
- }
244
-
245
- // ── Tier 2: In-process Dispatch Fallback (if remote HTTP fails) ────────────
246
- if (!streamBody) {
247
- const _g = globalThis as any;
248
- const dispatch = _g.__retrivoraDispatchChat;
249
-
250
- if (typeof dispatch === 'function') {
251
- try {
252
- const res = await dispatch({
253
- model: this.model,
254
- messages: formattedMessages as any,
255
- max_tokens: this.maxTokens,
256
- temperature: this.temperature,
257
- stream: true,
258
- }, this.apiKey);
259
-
260
- if (res?.stream) {
261
- streamBody = res.stream as ReadableStream<Uint8Array>;
262
- } else {
263
- const content = extractContent(res?.response);
264
- if (content !== undefined) {
265
- yield content;
266
- return;
267
- }
268
- }
269
- } catch (dispatchErr) {
270
- console.warn('[UniversalLLMAdapter] In-process dispatch error:', dispatchErr);
271
- }
272
- }
273
- }
274
-
275
- if (!streamBody) {
276
- throw new Error(`[UniversalLLMAdapter] Streaming request failed. Ensure RETRIVORA_LICENSE_KEY is valid or LLM service is available.`);
277
- }
278
-
279
- const reader = streamBody.getReader();
280
- const decoder = new TextDecoder('utf-8');
281
- let buffer = '';
282
-
283
- try {
284
- while (true) {
285
- const { done, value } = await reader.read();
286
- if (done) break;
287
-
288
- buffer += decoder.decode(value, { stream: true });
289
- const lines = buffer.split('\n');
290
- buffer = lines.pop() ?? '';
291
-
292
- for (const line of lines) {
293
- const trimmed = line.trim();
294
- if (!trimmed || trimmed === 'data: [DONE]') continue;
295
- if (!trimmed.startsWith('data:')) continue;
296
-
297
- try {
298
- const json = JSON.parse(trimmed.slice(5).trim());
299
- const text = (resolvePath(json, extractPath) as string | undefined) ?? extractContent(json);
300
- if (text && typeof text === 'string') yield text;
301
- } catch {
302
- // Skip malformed SSE lines
303
- }
304
- }
305
- }
306
-
307
- if (buffer.trim() && buffer.trim() !== 'data: [DONE]') {
308
- const jsonStr = buffer.replace(/^data:\s*/, '').trim();
309
- try {
310
- const json = JSON.parse(jsonStr);
311
- const text = (resolvePath(json, extractPath) as string | undefined) ?? extractContent(json);
312
- if (text && typeof text === 'string') yield text;
313
- } catch { /* ignore final partial line */ }
314
- }
315
- } finally {
316
- reader.releaseLock();
317
- }
318
- }
319
-
320
- async embed(text: string): Promise<number[]> {
321
- const path = this.opts.embedPath ?? '/embeddings';
322
- const payload = this.opts.embedPayloadTemplate
323
- ? buildPayload(this.opts.embedPayloadTemplate, { input: text, model: this.model })
324
- : { input: text, model: this.model };
325
-
326
- // First attempt: try HTTP call with fallback
327
- try {
328
- // console.log(`[UniversalLLMAdapter] Posting to embedding endpoint: ${this.baseUrl}${path}`, {
329
- // model: this.model,
330
- // inputLength: typeof text === 'string' ? text.length : 'unknown',
331
- // });
332
- const { data } = await this.http.post(path, payload);
333
- const extractPath = this.opts.embedExtractPath ?? 'data[0].embedding';
334
- const vector = resolvePath(data, extractPath) as number[] | undefined;
335
- if (Array.isArray(vector)) {
336
- //console.log(`[UniversalLLMAdapter] Embedding HTTP request successful. Vector size: ${vector.length}`);
337
- return vector;
338
- }
339
-
340
- // If we get here, the API succeeded but the response format was invalid
341
- console.warn(
342
- `[UniversalLLMAdapter] Embedding API returned unexpected format for path '${extractPath}'. Response:`,
343
- data
344
- );
345
- } catch (err: any) {
346
- console.warn(
347
- `[UniversalLLMAdapter] HTTP embedding request failed: ${err?.message || err}`,
348
- { path, model: this.model, response: err?.response?.data }
349
- );
350
-
351
- // In-process fallback for embeddings
352
- const _g = globalThis as any;
353
- const dispatchEmbed = _g.__retrivoraDispatchEmbedding;
354
-
355
- if (typeof dispatchEmbed === 'function') {
356
- try {
357
- const res = await dispatchEmbed({ input: text, model: this.model }, this.apiKey);
358
- if (res?.data?.[0]?.embedding) {
359
- return res.data[0].embedding;
360
- }
361
- } catch (dispatchErr: any) {
362
- console.warn('[UniversalLLMAdapter] In-process dispatch embedding failed:', dispatchErr);
363
- }
364
- }
365
- }
366
-
367
- // Second attempt: retry the HTTP call
368
- try {
369
- //console.log(`[UniversalLLMAdapter] Retrying embedding request to: ${this.baseUrl}${path}`);
370
- const { data } = await this.http.post(path, payload);
371
- const extractPath = this.opts.embedExtractPath ?? 'data[0].embedding';
372
- const vector = resolvePath(data, extractPath) as number[] | undefined;
373
-
374
- if (Array.isArray(vector)) {
375
- //console.log(`[UniversalLLMAdapter] Retry embedding HTTP request successful. Vector size: ${vector.length}`);
376
- return vector;
377
- }
378
-
379
- console.error(
380
- `[UniversalLLMAdapter] Retry embedding failed: unexpected response format for path '${extractPath}'. Response:`,
381
- data
382
- );
383
- throw new Error(
384
- `[UniversalLLMAdapter] Could not extract embedding vector. ` +
385
- `Expected array at '${extractPath}', got: ${typeof vector}. ` +
386
- `Response keys: ${Object.keys(data || {})}.`
387
- );
388
- } catch (err: any) {
389
- console.error(
390
- `[UniversalLLMAdapter] Both embedding attempts failed. Path: ${path}, Model: ${this.model}`,
391
- { error: err?.message, statusCode: err?.response?.status, response: err?.response?.data }
392
- );
393
- throw new Error(
394
- `[UniversalLLMAdapter] Failed to generate embedding after 2 attempts: ${err?.message || err}`
395
- );
396
- }
397
- }
398
-
399
- async batchEmbed(texts: string[]): Promise<number[][]> {
400
- const results: number[][] = [];
401
- for (const text of texts) {
402
- try {
403
- const vector = await this.embed(text);
404
- results.push(vector);
405
- } catch (err) {
406
- console.warn(`[UniversalLLMAdapter] batchEmbed failed for text (first 50 chars): ${text.slice(0, 50)}...`, err);
407
- // Don't push anything - the error will propagate
408
- throw err;
409
- }
410
- }
411
- return results;
412
- }
413
-
414
- async ping(): Promise<boolean> {
415
- try {
416
- await this.embed('ping');
417
- return true;
418
- } catch {
419
- return false;
420
- }
421
- }
422
- }
package/src/llm/utils.ts DELETED
@@ -1,46 +0,0 @@
1
- /**
2
- * LLM utility helpers shared across all provider implementations.
3
- *
4
- * These are pure functions with no external dependencies so they can
5
- * be imported by any provider without creating circular references.
6
- */
7
-
8
- /**
9
- * Builds the final system message content by injecting the RAG context
10
- * into a system prompt using one of two strategies:
11
- *
12
- * 1. **Placeholder substitution** — if the prompt contains `{{context}}`,
13
- * that token is replaced with the retrieved context string.
14
- *
15
- * 2. **Appendage** — if there is no placeholder, the context is appended
16
- * after a separator so the LLM always receives the retrieved evidence,
17
- * regardless of whether the caller supplied a per-call `systemPrompt`
18
- * override.
19
- *
20
- * This function is intentionally strict: it never silently drops context.
21
- * If `context` is empty or the sentinel "No relevant context found." the
22
- * prompt is returned as-is so the LLM is not polluted with an empty block.
23
- *
24
- * @param systemPrompt - The base system prompt (from config or per-call override).
25
- * @param context - The retrieved RAG context string.
26
- * @returns - The resolved system message content ready to send.
27
- */
28
- export function buildSystemContent(systemPrompt: string, context: string): string {
29
- const noContext =
30
- !context ||
31
- context.trim() === '' ||
32
- context.trim() === 'No relevant context found.';
33
-
34
- // Replace {{context}} placeholder regardless of whether context is empty
35
- // so the prompt is never sent with a raw placeholder token.
36
- if (systemPrompt.includes('{{context}}')) {
37
- return systemPrompt.replace('{{context}}', noContext ? '' : context);
38
- }
39
-
40
- // Without a placeholder: only append context when there is actual content.
41
- if (noContext) {
42
- return systemPrompt;
43
- }
44
-
45
- return `${systemPrompt}\n\n---\nContext:\n${context}`;
46
- }
@@ -1,43 +0,0 @@
1
- import { GraphDBConfig } from '../../config/RagConfig';
2
- import { GraphNode, Edge, GraphSearchResult } from '../../types';
3
-
4
- /**
5
- * BaseGraphProvider — Abstract base class for Graph DB providers.
6
- */
7
- export abstract class BaseGraphProvider {
8
- protected config: GraphDBConfig;
9
-
10
- constructor(config: GraphDBConfig) {
11
- this.config = config;
12
- }
13
-
14
- /**
15
- * Initialise connection to the graph database.
16
- */
17
- abstract initialize(): Promise<void>;
18
-
19
- /**
20
- * Add nodes to the graph.
21
- */
22
- abstract addNodes(nodes: GraphNode[]): Promise<void>;
23
-
24
- /**
25
- * Add edges (relationships) to the graph.
26
- */
27
- abstract addEdges(edges: Edge[]): Promise<void>;
28
-
29
- /**
30
- * Query the graph for relevant nodes and edges.
31
- */
32
- abstract query(queryText: string, limit?: number): Promise<GraphSearchResult>;
33
-
34
- /**
35
- * Check if the database is reachable.
36
- */
37
- abstract ping(): Promise<boolean>;
38
-
39
- /**
40
- * Close connection.
41
- */
42
- abstract disconnect(): Promise<void>;
43
- }
@@ -1,66 +0,0 @@
1
- import { BaseGraphProvider } from './BaseGraphProvider';
2
- import { GraphNode, Edge, GraphSearchResult } from '../../types';
3
-
4
- /**
5
- * SimpleGraphProvider — In-memory graph storage for demonstration and local dev.
6
- */
7
- export class SimpleGraphProvider extends BaseGraphProvider {
8
- private nodes: Map<string, GraphNode> = new Map();
9
- private edges: Edge[] = [];
10
-
11
- async initialize(): Promise<void> {
12
- //console.log('[SimpleGraphProvider] Initialised in-memory graph store.');
13
- }
14
-
15
- async addNodes(nodes: GraphNode[]): Promise<void> {
16
- for (const node of nodes) {
17
- this.nodes.set(node.id, node);
18
- }
19
- }
20
-
21
- async addEdges(edges: Edge[]): Promise<void> {
22
- this.edges.push(...edges);
23
- }
24
-
25
- async query(queryText: string, limit = 5): Promise<GraphSearchResult> {
26
- const q = queryText.toLowerCase();
27
-
28
- // Simple keyword matching for demonstration
29
- const matchedNodes = Array.from(this.nodes.values()).filter(node =>
30
- node.id.toLowerCase().includes(q) ||
31
- node.label.toLowerCase().includes(q) ||
32
- JSON.stringify(node.properties).toLowerCase().includes(q)
33
- ).slice(0, limit);
34
-
35
- const matchedNodeIds = new Set(matchedNodes.map(n => n.id));
36
- const matchedEdges = this.edges.filter(edge =>
37
- matchedNodeIds.has(edge.source) || matchedNodeIds.has(edge.target)
38
- );
39
-
40
- // Also include connected nodes
41
- for (const edge of matchedEdges) {
42
- if (!matchedNodeIds.has(edge.source)) {
43
- const source = this.nodes.get(edge.source);
44
- if (source) matchedNodes.push(source);
45
- }
46
- if (!matchedNodeIds.has(edge.target)) {
47
- const target = this.nodes.get(edge.target);
48
- if (target) matchedNodes.push(target);
49
- }
50
- }
51
-
52
- return {
53
- nodes: Array.from(new Set(matchedNodes)),
54
- edges: matchedEdges
55
- };
56
- }
57
-
58
- async ping(): Promise<boolean> {
59
- return true;
60
- }
61
-
62
- async disconnect(): Promise<void> {
63
- this.nodes.clear();
64
- this.edges = [];
65
- }
66
- }
@@ -1,98 +0,0 @@
1
- import { VectorDBConfig } from '../../config/RagConfig';
2
- import { VectorMatch, UpsertDocument } from '../../types';
3
- import { IProviderValidator, IProviderHealthChecker } from '../../core/ProviderInterfaces';
4
-
5
- /**
6
- * BaseVectorProvider — Abstract base class for all vector database providers.
7
- * Each provider (Pinecone, Milvus, Redis, etc.) must extend this class.
8
- */
9
- export abstract class BaseVectorProvider {
10
- protected readonly config: VectorDBConfig;
11
- protected indexName: string;
12
- protected projectId?: string;
13
-
14
- constructor(config: VectorDBConfig) {
15
- this.config = config;
16
- this.indexName = config.indexName || 'default';
17
- }
18
-
19
- /**
20
- * Initialise the connection (create tables, verify index, etc.)
21
- */
22
- abstract initialize(): Promise<void>;
23
-
24
- /**
25
- * Insert or update a single vector + content pair.
26
- */
27
- abstract upsert(doc: UpsertDocument, namespace?: string): Promise<void>;
28
-
29
- /**
30
- * Batch upsert for efficient ingestion of many documents.
31
- */
32
- abstract batchUpsert(docs: UpsertDocument[], namespace?: string): Promise<void>;
33
-
34
- /**
35
- * Find the top-K most similar vectors to the query vector.
36
- */
37
- abstract query(
38
- vector: number[],
39
- topK: number,
40
- namespace?: string,
41
- filter?: Record<string, unknown>
42
- ): Promise<VectorMatch[]>;
43
-
44
- /**
45
- * Delete a stored vector by ID.
46
- */
47
- abstract delete(id: string | number, namespace?: string): Promise<void>;
48
-
49
- /**
50
- * Delete all vectors in a namespace (full data reset for a project).
51
- */
52
- abstract deleteNamespace(namespace: string): Promise<void>;
53
-
54
- /**
55
- * Check if the underlying DB is reachable.
56
- */
57
- abstract ping(): Promise<boolean>;
58
-
59
- /**
60
- * Gracefully close the connection.
61
- */
62
- abstract disconnect(): Promise<void>;
63
-
64
- /**
65
- * Returns a validator for this provider's configuration.
66
- */
67
- static getValidator?(): IProviderValidator;
68
-
69
- /**
70
- * Returns a health checker for this provider.
71
- */
72
- static getHealthChecker?(): IProviderHealthChecker;
73
-
74
- /**
75
- * Remove internal keys (starting with __) and queryText from a filter object
76
- * before passing it to an external provider that might not support them.
77
- */
78
- protected sanitizeFilter(filter?: Record<string, unknown>): Record<string, unknown> {
79
- if (!filter) return {};
80
-
81
- const result: Record<string, unknown> = {};
82
-
83
- for (const [key, value] of Object.entries(filter)) {
84
- if (key.startsWith('__') || key === 'queryText' || key === 'keywords') {
85
- continue;
86
- }
87
-
88
- if (key === 'metadata' && typeof value === 'object' && value !== null) {
89
- // Merge metadata fields into the root for providers that expect flat filters
90
- Object.assign(result, value);
91
- } else {
92
- result[key] = value;
93
- }
94
- }
95
-
96
- return result;
97
- }
98
- }