@retrivora-ai/rag-engine 1.8.0 → 1.8.2

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 (71) hide show
  1. package/dist/DocumentChunker-Dh9TvmGG.d.mts +45 -0
  2. package/dist/DocumentChunker-Dh9TvmGG.d.ts +45 -0
  3. package/dist/{index-DPsQodME.d.mts → ILLMProvider-BOJFz3Na.d.mts} +104 -1
  4. package/dist/{index-DPsQodME.d.ts → ILLMProvider-BOJFz3Na.d.ts} +104 -1
  5. package/dist/MultiTablePostgresProvider-ZLGSKTJR.mjs +8 -0
  6. package/dist/chunk-ICKRMZQK.mjs +76 -0
  7. package/dist/{chunk-PV3MFHWU.mjs → chunk-LZVVLSDN.mjs} +977 -516
  8. package/dist/chunk-OZFBG4BA.mjs +291 -0
  9. package/dist/handlers/index.d.mts +2 -2
  10. package/dist/handlers/index.d.ts +2 -2
  11. package/dist/handlers/index.js +1269 -656
  12. package/dist/handlers/index.mjs +4 -1
  13. package/dist/{index-Bb2yEopi.d.mts → index-BwpcaziY.d.ts} +10 -2
  14. package/dist/{index-CkbTzj9J.d.ts → index-D3V9Et2M.d.mts} +10 -2
  15. package/dist/index.d.mts +24 -4
  16. package/dist/index.d.ts +24 -4
  17. package/dist/index.js +1354 -826
  18. package/dist/index.mjs +1284 -795
  19. package/dist/server.d.mts +47 -27
  20. package/dist/server.d.ts +47 -27
  21. package/dist/server.js +1417 -829
  22. package/dist/server.mjs +164 -176
  23. package/package.json +6 -2
  24. package/src/app/api/upload/route.ts +4 -0
  25. package/src/app/constants.tsx +2 -2
  26. package/src/app/page.tsx +12 -322
  27. package/src/components/AmbientBackground.tsx +29 -0
  28. package/src/components/ArchitectureCard.tsx +17 -0
  29. package/src/components/ArchitectureCardsSection.tsx +15 -0
  30. package/src/components/ChatWindow.tsx +32 -0
  31. package/src/components/CodeViewer.tsx +51 -0
  32. package/src/components/ConfigProvider.tsx +1 -0
  33. package/src/components/DocViewer.tsx +37 -0
  34. package/src/components/DocumentUpload.tsx +44 -1
  35. package/src/components/Documentation.tsx +58 -0
  36. package/src/components/DynamicChart.tsx +27 -2
  37. package/src/components/Hero.tsx +59 -0
  38. package/src/components/HourglassLoader.tsx +87 -0
  39. package/src/components/Lifecycle.tsx +37 -0
  40. package/src/components/MarkdownComponents.tsx +140 -0
  41. package/src/components/MessageBubble.tsx +124 -904
  42. package/src/components/Navbar.tsx +55 -0
  43. package/src/components/ObservabilityPanel.tsx +374 -0
  44. package/src/components/ProductCard.tsx +5 -3
  45. package/src/components/UIDispatcher.tsx +344 -0
  46. package/src/components/VisualizationRenderer.tsx +372 -250
  47. package/src/config/RagConfig.ts +5 -0
  48. package/src/config/serverConfig.ts +3 -1
  49. package/src/core/Pipeline.ts +240 -271
  50. package/src/core/ProviderRegistry.ts +2 -2
  51. package/src/core/VectorPlugin.ts +9 -0
  52. package/src/handlers/index.ts +91 -15
  53. package/src/hooks/useRagChat.ts +21 -11
  54. package/src/index.ts +9 -1
  55. package/src/llm/LLMFactory.ts +54 -2
  56. package/src/llm/providers/AnthropicProvider.ts +12 -8
  57. package/src/llm/providers/GeminiProvider.ts +188 -143
  58. package/src/llm/providers/OllamaProvider.ts +7 -3
  59. package/src/llm/providers/OpenAIProvider.ts +12 -8
  60. package/src/providers/vectordb/MultiTablePostgresProvider.ts +150 -64
  61. package/src/types/chat.ts +8 -0
  62. package/src/types/index.ts +132 -0
  63. package/src/types/props.ts +9 -1
  64. package/src/utils/ProductExtractor.ts +347 -0
  65. package/src/utils/SchemaMapper.ts +129 -0
  66. package/src/utils/UITransformer.ts +470 -209
  67. package/src/utils/synonyms.ts +78 -0
  68. package/dist/DocumentChunker-C1GEEosY.d.ts +0 -93
  69. package/dist/DocumentChunker-CFEiRopR.d.mts +0 -93
  70. package/dist/PostgreSQLProvider-BMOETDZA.mjs +0 -8
  71. package/dist/chunk-FLOSGE6A.mjs +0 -202
@@ -21,6 +21,7 @@ export class MultiTablePostgresProvider extends BaseVectorProvider {
21
21
  private readonly connectionString: string;
22
22
  private tables: string[];
23
23
  private searchFields: string[];
24
+ private readonly uploadTable: string;
24
25
 
25
26
  constructor(config: VectorDBConfig) {
26
27
  super(config);
@@ -31,22 +32,8 @@ export class MultiTablePostgresProvider extends BaseVectorProvider {
31
32
  this.connectionString = opts.connectionString as string;
32
33
  this.dimensions = (opts.dimensions as number) ?? 768;
33
34
 
34
- // Tables can come from options.tables (custom key) or be derived from VECTOR_DB_TABLES env var
35
- const rawTables: string | string[] =
36
- (opts.tables as string | string[]) ??
37
- process.env.VECTOR_DB_TABLES ??
38
- '';
39
-
40
- this.tables = typeof rawTables === 'string'
41
- ? rawTables.split(',').map((t) => t.trim()).filter(Boolean)
42
- : rawTables;
43
-
44
- if (this.tables.length === 0) {
45
- console.warn(
46
- '[MultiTablePostgresProvider] No tables configured. ' +
47
- 'Set VECTOR_DB_TABLES as a comma-separated list of table names or pass options.tables.'
48
- );
49
- }
35
+ // The provider will dynamically discover tables during initialize().
36
+ this.tables = [];
50
37
 
51
38
  // Exact match fields can come from options.searchFields or be derived from VECTOR_DB_SEARCH_FIELDS env var
52
39
  const rawSearchFields: string | string[] =
@@ -57,6 +44,8 @@ export class MultiTablePostgresProvider extends BaseVectorProvider {
57
44
  this.searchFields = typeof rawSearchFields === 'string'
58
45
  ? rawSearchFields.split(',').map((f) => f.trim()).filter(Boolean)
59
46
  : rawSearchFields;
47
+
48
+ this.uploadTable = (opts.uploadTable as string) || 'document_chunks';
60
49
  }
61
50
 
62
51
  async initialize(): Promise<void> {
@@ -65,6 +54,24 @@ export class MultiTablePostgresProvider extends BaseVectorProvider {
65
54
  // Dynamically discover all tables that have an 'embedding' column
66
55
  const client: PoolClient = await this.pool.connect();
67
56
  try {
57
+ await client.query('CREATE EXTENSION IF NOT EXISTS vector');
58
+
59
+ // Ensure the upload table exists
60
+ await client.query(`
61
+ CREATE TABLE IF NOT EXISTS ${this.uploadTable} (
62
+ id TEXT PRIMARY KEY,
63
+ namespace TEXT NOT NULL DEFAULT '',
64
+ content TEXT NOT NULL,
65
+ metadata JSONB,
66
+ embedding VECTOR(${this.dimensions})
67
+ )
68
+ `);
69
+ await client.query(`
70
+ CREATE INDEX IF NOT EXISTS ${this.uploadTable}_embedding_idx
71
+ ON ${this.uploadTable}
72
+ USING hnsw (embedding vector_cosine_ops)
73
+ `);
74
+
68
75
  const res = await client.query(`
69
76
  SELECT table_name
70
77
  FROM information_schema.columns
@@ -72,21 +79,8 @@ export class MultiTablePostgresProvider extends BaseVectorProvider {
72
79
  AND table_schema = 'public'
73
80
  `);
74
81
 
75
- const discoveredTables = res.rows.map(r => r.table_name);
76
-
77
- if (this.tables.length === 0) {
78
- // No static tables configured, use everything discovered
79
- this.tables = discoveredTables;
80
- } else {
81
- // Filter configured tables to ensure they actually exist with embeddings
82
- const staticTables = [...this.tables];
83
- this.tables = staticTables.filter(t => discoveredTables.includes(t));
84
-
85
- if (this.tables.length < staticTables.length) {
86
- const missing = staticTables.filter(t => !discoveredTables.includes(t));
87
- console.warn(`[MultiTablePostgresProvider] Some configured tables were skipped (missing 'embedding' column): ${missing.join(', ')}`);
88
- }
89
- }
82
+ // Dynamically use all tables that have an embedding column
83
+ this.tables = res.rows.map(r => r.table_name);
90
84
 
91
85
  if (this.tables.length === 0) {
92
86
  console.warn('[MultiTablePostgresProvider] No tables with "embedding" columns found in the database.');
@@ -101,23 +95,125 @@ export class MultiTablePostgresProvider extends BaseVectorProvider {
101
95
  }
102
96
 
103
97
  /**
104
- * Upsert is not supported for MultiTablePostgresProvider as it's designed for
105
- * searching across pre-existing tables with varying schemas.
98
+ * Upsert a document by dynamically provisioning a table and columns.
106
99
  */
107
- async upsert(_doc: UpsertDocument, _namespace?: string): Promise<void> { // eslint-disable-line @typescript-eslint/no-unused-vars
108
- throw new Error(
109
- '[MultiTablePostgresProvider] upsert() is not supported in multi-table mode. ' +
110
- 'Please use the standard PostgreSQLProvider for single-table managed indices.'
111
- );
100
+ async upsert(doc: UpsertDocument, namespace = ''): Promise<void> {
101
+ await this.batchUpsert([doc], namespace);
112
102
  }
113
103
 
114
104
  /**
115
- * Batch upsert is not supported for MultiTablePostgresProvider.
105
+ * Batch upsert documents by dynamically provisioning tables and columns based on CSV headers.
116
106
  */
117
- async batchUpsert(_docs: UpsertDocument[], _namespace?: string): Promise<void> { // eslint-disable-line @typescript-eslint/no-unused-vars
118
- throw new Error(
119
- '[MultiTablePostgresProvider] batchUpsert() is not supported in multi-table mode.'
120
- );
107
+ async batchUpsert(docs: UpsertDocument[], namespace = ''): Promise<void> {
108
+ if (docs.length === 0) return;
109
+
110
+ // Group docs by fileName to support dynamic table creation per CSV
111
+ const docsByFile: Record<string, UpsertDocument[]> = {};
112
+ for (const doc of docs) {
113
+ const fileName = (doc.metadata?.fileName as string) || this.uploadTable;
114
+ if (!docsByFile[fileName]) docsByFile[fileName] = [];
115
+ docsByFile[fileName].push(doc);
116
+ }
117
+
118
+ const client = await this.pool.connect();
119
+ try {
120
+ await client.query('BEGIN');
121
+
122
+ for (const [fileName, fileDocs] of Object.entries(docsByFile)) {
123
+ // Sanitize table name (e.g., "jewelery.csv" -> "jewelery")
124
+ let tableName = fileName;
125
+ if (tableName.toLowerCase().endsWith('.csv')) {
126
+ tableName = tableName.slice(0, -4);
127
+ }
128
+ tableName = tableName.replace(/[^a-z0-9_]/gi, '_').toLowerCase() || this.uploadTable;
129
+
130
+ // Extract headers from the first doc's metadata
131
+ const firstMeta = fileDocs[0].metadata || {};
132
+ const systemKeys = ['fileName', 'fileSize', 'fileType', 'uploadedAt', 'dimension', 'chunkIndex', 'id', 'namespace', 'content', 'metadata', 'embedding'];
133
+ const csvHeaders = Object.keys(firstMeta).filter(k => !systemKeys.includes(k) && !systemKeys.includes(k.toLowerCase()));
134
+
135
+ // 1. Create Table Dynamically
136
+ const columnDefs = csvHeaders.map(h => `"${h}" TEXT`).join(',\n ');
137
+ const createTableSql = `
138
+ CREATE TABLE IF NOT EXISTS "${tableName}" (
139
+ id TEXT PRIMARY KEY,
140
+ ${columnDefs ? columnDefs + ',' : ''}
141
+ namespace TEXT NOT NULL DEFAULT '',
142
+ content TEXT NOT NULL,
143
+ metadata JSONB,
144
+ embedding VECTOR(${this.dimensions})
145
+ )
146
+ `;
147
+ await client.query(createTableSql);
148
+
149
+ // Create Index
150
+ await client.query(`
151
+ CREATE INDEX IF NOT EXISTS "${tableName}_embedding_idx"
152
+ ON "${tableName}"
153
+ USING hnsw (embedding vector_cosine_ops)
154
+ `);
155
+
156
+ // Ensure this new table is added to our search scope
157
+ if (!this.tables.includes(tableName)) {
158
+ this.tables.push(tableName);
159
+ }
160
+
161
+ // 2. Insert Data
162
+ const BATCH_SIZE = 50;
163
+ for (let i = 0; i < fileDocs.length; i += BATCH_SIZE) {
164
+ const batch = fileDocs.slice(i, i + BATCH_SIZE);
165
+ const values: unknown[] = [];
166
+
167
+ const headerColumns = csvHeaders.map(h => `"${h}"`).join(', ');
168
+ const allColumns = `id, ${headerColumns ? headerColumns + ', ' : ''}namespace, content, metadata, embedding`;
169
+
170
+ const valuePlaceholders = batch.map((doc, idx) => {
171
+ const offset = idx * (5 + csvHeaders.length);
172
+
173
+ values.push(doc.id);
174
+ // Push header values
175
+ for (const h of csvHeaders) {
176
+ values.push(String(doc.metadata?.[h] || ''));
177
+ }
178
+
179
+ values.push(namespace, doc.content, JSON.stringify(doc.metadata ?? {}), `[${doc.vector.join(',')}]`);
180
+
181
+ const docPlaceholders = [];
182
+ for (let j = 1; j <= 5 + csvHeaders.length; j++) {
183
+ if (j === 5 + csvHeaders.length) {
184
+ docPlaceholders.push(`$${offset + j}::vector`);
185
+ } else {
186
+ docPlaceholders.push(`$${offset + j}`);
187
+ }
188
+ }
189
+
190
+ return `(${docPlaceholders.join(', ')})`;
191
+ }).join(', ');
192
+
193
+ const conflictUpdates = csvHeaders.map(h => `"${h}" = EXCLUDED."${h}"`).join(',\n ');
194
+
195
+ const query = `
196
+ INSERT INTO "${tableName}" (${allColumns})
197
+ VALUES ${valuePlaceholders}
198
+ ON CONFLICT (id) DO UPDATE
199
+ SET ${conflictUpdates ? conflictUpdates + ',' : ''}
200
+ namespace = EXCLUDED.namespace,
201
+ content = EXCLUDED.content,
202
+ metadata = EXCLUDED.metadata,
203
+ embedding = EXCLUDED.embedding
204
+ `;
205
+
206
+ await client.query(query, values);
207
+ }
208
+ }
209
+
210
+ await client.query('COMMIT');
211
+ } catch (error) {
212
+ await client.query('ROLLBACK');
213
+ throw error;
214
+ } finally {
215
+ client.release();
216
+ }
121
217
  }
122
218
 
123
219
  /**
@@ -258,30 +354,20 @@ export class MultiTablePostgresProvider extends BaseVectorProvider {
258
354
  allResults.push(...tableResults);
259
355
  }
260
356
 
261
- // 1. Group results by table to ensure we take the top ones from each
262
- const resultsByTable: Record<string, VectorMatch[]> = {};
263
- for (const res of allResults) {
264
- const table = res.metadata?.source_table as string;
265
- if (!resultsByTable[table]) resultsByTable[table] = [];
266
- resultsByTable[table].push(res);
267
- }
268
-
269
- // 2. Take top 3 from EACH table guaranteed, then fill the rest with overall best
270
- const balancedResults: VectorMatch[] = [];
271
- const tables = Object.keys(resultsByTable);
357
+ // 1. Sort all results globally by semantic similarity score
358
+ allResults.sort((a, b) => b.score - a.score);
272
359
 
273
- // First, ensure every table's best match is included
274
- for (const table of tables) {
275
- balancedResults.push(...resultsByTable[table].slice(0, 3));
276
- }
360
+ if (allResults.length === 0) return [];
277
361
 
278
- // 3. Sort the balanced list by score
279
- const finalSorted = balancedResults.sort((a, b) => b.score - a.score);
362
+ // 2. Identify the "Active Table Context" from the absolute highest scoring match
363
+ const bestMatchTable = allResults[0].metadata?.source_table as string;
364
+
365
+ // 3. Filter out all results that don't belong to the active table to prevent cross-contamination
366
+ const finalSorted = allResults.filter(res => res.metadata?.source_table === bestMatchTable);
280
367
 
281
- if (finalSorted.length > 0) {
282
- console.log(`[MultiTablePostgresProvider] --- Search Complete ---`);
283
- console.log(`[MultiTablePostgresProvider] Final top match from "${finalSorted[0].metadata?.source_table ?? 'unknown'}" with score ${finalSorted[0].score.toFixed(4)}`);
284
- }
368
+ console.log(`[MultiTablePostgresProvider] --- Search Complete ---`);
369
+ console.log(`[MultiTablePostgresProvider] Final top match from "${bestMatchTable}" with score ${finalSorted[0].score.toFixed(4)}`);
370
+ console.log(`[MultiTablePostgresProvider] Restricting recommendations strictly to table: "${bestMatchTable}"`);
285
371
 
286
372
  return finalSorted.slice(0, Math.max(topK, 15));
287
373
  }
package/src/types/chat.ts CHANGED
@@ -11,6 +11,8 @@ export interface RagMessage extends ChatMessage {
11
11
  id: string;
12
12
  sources?: VectorMatch[];
13
13
  uiTransformation?: unknown;
14
+ /** Full observability trace emitted by the backend. Only present on assistant messages. */
15
+ trace?: import('./index').ObservabilityTrace;
14
16
  createdAt: string;
15
17
  }
16
18
 
@@ -21,6 +23,12 @@ export interface ChatOptions {
21
23
  temperature?: number;
22
24
  /** Stop sequences */
23
25
  stop?: string[];
26
+ /**
27
+ * Per-call system prompt override. When provided, providers should use this
28
+ * instead of (or in addition to) their configured llmConfig.systemPrompt.
29
+ * Useful for one-off analytical calls (e.g. UITransformer.analyzeAndDecide).
30
+ */
31
+ systemPrompt?: string;
24
32
  }
25
33
 
26
34
  export interface UseRagChatOptions {
@@ -1,3 +1,53 @@
1
+ // ─── Observability ────────────────────────────────────────────────────────────
2
+
3
+ /** Per-stage latency breakdown for a single RAG request. */
4
+ export interface LatencyBreakdown {
5
+ embedMs: number;
6
+ retrieveMs: number;
7
+ rerankMs?: number;
8
+ generateMs: number;
9
+ totalMs: number;
10
+ }
11
+
12
+ /** Token usage reported by the LLM (or estimated when the provider doesn't expose it). */
13
+ export interface TokenUsage {
14
+ promptTokens: number;
15
+ completionTokens: number;
16
+ totalTokens: number;
17
+ /** Rough cost in USD — estimated from token counts and model pricing. */
18
+ estimatedCostUsd?: number;
19
+ }
20
+
21
+ /** A single retrieved chunk annotated with its retrieval context. */
22
+ export interface RetrievedChunk {
23
+ id: string | number;
24
+ score: number;
25
+ content: string;
26
+ metadata: Record<string, unknown>;
27
+ namespace: string;
28
+ }
29
+
30
+ /**
31
+ * Full observability trace for one RAG request.
32
+ * Emitted by the backend as an SSE frame and stored on each RagMessage.
33
+ */
34
+ export interface ObservabilityTrace {
35
+ requestId: string;
36
+ query: string;
37
+ rewrittenQuery?: string;
38
+ systemPrompt: string;
39
+ userPrompt: string;
40
+ chunks: RetrievedChunk[];
41
+ latency: LatencyBreakdown;
42
+ tokens?: TokenUsage;
43
+ /** 0 = fully grounded, 1 = likely hallucinated. */
44
+ hallucinationScore?: number;
45
+ hallucinationReason?: string;
46
+ timestamp: string;
47
+ }
48
+
49
+ // ─── Vector / RAG ─────────────────────────────────────────────────────────────
50
+
1
51
  export interface VectorMatch {
2
52
  id: string | number;
3
53
  score: number;
@@ -23,6 +73,8 @@ export interface ChatResponse {
23
73
  sources: VectorMatch[];
24
74
  graphData?: GraphSearchResult;
25
75
  ui_transformation?: unknown;
76
+ /** Observability trace — populated by the instrumented Pipeline. */
77
+ trace?: ObservabilityTrace;
26
78
  }
27
79
 
28
80
  export interface GraphNode {
@@ -66,5 +118,85 @@ export interface Product {
66
118
  description?: string;
67
119
  }
68
120
 
121
+ /**
122
+ * Supported UI visualization types
123
+ */
124
+ export type VisualizationType =
125
+ | 'pie_chart'
126
+ | 'bar_chart'
127
+ | 'line_chart'
128
+ | 'radar_chart'
129
+ | 'table'
130
+ | 'product_carousel'
131
+ | 'carousel'
132
+ | 'text';
133
+
134
+ /**
135
+ * Base structure for all UI transformation responses
136
+ */
137
+ export interface UITransformationResponse {
138
+ type: VisualizationType;
139
+ title: string;
140
+ description?: string;
141
+ data: unknown;
142
+ }
143
+
144
+ /**
145
+ * Pie chart data structure
146
+ */
147
+ export interface PieChartData {
148
+ label: string;
149
+ value: number;
150
+ inStockCount?: number;
151
+ outOfStockCount?: number;
152
+ [key: string]: unknown;
153
+ }
154
+
155
+ /**
156
+ * Bar chart data structure
157
+ */
158
+ export interface BarChartData {
159
+ category: string;
160
+ value: number;
161
+ inStockCount?: number;
162
+ outOfStockCount?: number;
163
+ [key: string]: unknown;
164
+ }
165
+
166
+ /**
167
+ * Line chart data point
168
+ */
169
+ export interface LineChartDataPoint {
170
+ timestamp: string | number;
171
+ value: number;
172
+ label?: string;
173
+ [key: string]: unknown;
174
+ }
175
+
176
+ /**
177
+ * Table representation
178
+ */
179
+ export interface TableData {
180
+ columns: string[];
181
+ rows: (string | number | boolean)[][];
182
+ }
183
+
184
+
185
+ /**
186
+ * Product carousel item
187
+ */
188
+ export interface CarouselProduct {
189
+ id: string | number;
190
+ name: string;
191
+ price?: number | string;
192
+ image?: string;
193
+ inStock?: boolean;
194
+ brand?: string;
195
+ description?: string;
196
+ [key: string]: unknown;
197
+ }
198
+
199
+
200
+
69
201
  export * from './props';
70
202
  export * from './chat';
@@ -74,9 +74,17 @@ export interface SourceCardProps {
74
74
  export interface ClientConfig {
75
75
  projectId: string;
76
76
  ui: Required<UIConfig>;
77
+ embedding?: {
78
+ model?: string;
79
+ dimensions?: number;
80
+ };
77
81
  }
78
82
 
79
83
  export interface ConfigProviderProps {
80
- config?: { projectId?: string; ui?: Partial<UIConfig> };
84
+ config?: {
85
+ projectId?: string;
86
+ ui?: Partial<UIConfig>;
87
+ embedding?: { model?: string; dimensions?: number }
88
+ };
81
89
  children: ReactNode;
82
90
  }