@retrivora-ai/rag-engine 1.7.9 → 1.8.1

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 (47) hide show
  1. package/dist/DocumentChunker-Dh9TvmGG.d.mts +45 -0
  2. package/dist/DocumentChunker-Dh9TvmGG.d.ts +45 -0
  3. package/dist/{index-wCRqMtdX.d.mts → ILLMProvider-BfRgI1Xh.d.mts} +59 -1
  4. package/dist/{index-wCRqMtdX.d.ts → ILLMProvider-BfRgI1Xh.d.ts} +59 -1
  5. package/dist/MultiTablePostgresProvider-YY7LPNJK.mjs +8 -0
  6. package/dist/{chunk-UHGYLTTY.mjs → chunk-BFYLQYQU.mjs} +846 -458
  7. package/dist/chunk-R3RGUMHE.mjs +218 -0
  8. package/dist/handlers/index.d.mts +2 -2
  9. package/dist/handlers/index.d.ts +2 -2
  10. package/dist/handlers/index.js +1033 -622
  11. package/dist/handlers/index.mjs +1 -1
  12. package/dist/{index-BDqz2_Yu.d.ts → index-1Z4GuYBi.d.ts} +7 -1
  13. package/dist/{index-DhsG2o5q.d.mts → index-BV0z5mb6.d.mts} +7 -1
  14. package/dist/index.d.mts +3 -3
  15. package/dist/index.d.ts +3 -3
  16. package/dist/index.js +507 -352
  17. package/dist/index.mjs +427 -253
  18. package/dist/server.d.mts +35 -5
  19. package/dist/server.d.ts +35 -5
  20. package/dist/server.js +1183 -795
  21. package/dist/server.mjs +163 -176
  22. package/package.json +4 -2
  23. package/src/app/page.tsx +1 -2
  24. package/src/components/DynamicChart.tsx +10 -35
  25. package/src/components/MessageBubble.tsx +223 -74
  26. package/src/components/ProductCard.tsx +2 -2
  27. package/src/components/VisualizationRenderer.tsx +347 -247
  28. package/src/config/RagConfig.ts +5 -0
  29. package/src/config/serverConfig.ts +3 -1
  30. package/src/core/Pipeline.ts +70 -209
  31. package/src/core/ProviderRegistry.ts +2 -2
  32. package/src/core/VectorPlugin.ts +9 -0
  33. package/src/handlers/index.ts +23 -7
  34. package/src/hooks/useRagChat.ts +2 -2
  35. package/src/llm/LLMFactory.ts +54 -2
  36. package/src/llm/providers/AnthropicProvider.ts +12 -8
  37. package/src/llm/providers/GeminiProvider.ts +188 -143
  38. package/src/llm/providers/OllamaProvider.ts +7 -3
  39. package/src/llm/providers/OpenAIProvider.ts +12 -8
  40. package/src/types/chat.ts +6 -0
  41. package/src/types/index.ts +81 -0
  42. package/src/utils/SchemaMapper.ts +129 -0
  43. package/src/utils/UITransformer.ts +524 -194
  44. package/dist/DocumentChunker-Bmscbh-X.d.ts +0 -93
  45. package/dist/DocumentChunker-DCuxrOdM.d.mts +0 -93
  46. package/dist/PostgreSQLProvider-BMOETDZA.mjs +0 -8
  47. package/dist/chunk-FLOSGE6A.mjs +0 -202
@@ -0,0 +1,129 @@
1
+ import { ILLMProvider } from '../llm/ILLMProvider';
2
+
3
+ export interface SchemaMap {
4
+ name?: string;
5
+ price?: string;
6
+ brand?: string;
7
+ image?: string;
8
+ stock?: string;
9
+ description?: string;
10
+ category?: string;
11
+ }
12
+
13
+ /**
14
+ * SchemaMapper — Uses AI to "train" the plugin on unknown database schemas.
15
+ * It maps arbitrary metadata keys to standard UI properties.
16
+ */
17
+ export class SchemaMapper {
18
+ private static cache = new Map<string, SchemaMap>();
19
+
20
+ /**
21
+ * Descriptions of standard UI properties to help the AI map fields accurately.
22
+ */
23
+ private static readonly TARGET_PROPERTIES: Record<string, string> = {
24
+ name: 'The primary title, name, or label of the item',
25
+ price: 'The numeric cost, price, MSRP, or amount',
26
+ brand: 'The manufacturer, vendor, brand, or maker',
27
+ image: 'The URL to an image, thumbnail, photo, or picture',
28
+ stock: 'The availability, inventory count, quantity, or stock level',
29
+ description: 'The detailed text content, summary, or body info',
30
+ category: 'The group, department, type, or category name',
31
+ };
32
+
33
+ /**
34
+ * Trains the plugin on a set of keys.
35
+ * This is done once per schema and cached.
36
+ */
37
+ static async train(
38
+ llm: ILLMProvider,
39
+ projectId: string,
40
+ keys: string[]
41
+ ): Promise<SchemaMap> {
42
+ const cacheKey = `${projectId}:${keys.sort().join(',')}`;
43
+ if (this.cache.has(cacheKey)) {
44
+ return this.cache.get(cacheKey)!;
45
+ }
46
+
47
+ console.log(`[SchemaMapper] 🧠 Training AI on new schema keys: ${keys.join(', ')}`);
48
+
49
+ // Dynamically build the property list for the prompt
50
+ const propertyList = Object.entries(this.TARGET_PROPERTIES)
51
+ .map(([prop, desc]) => `- ${prop} (${desc})`)
52
+ .join('\n');
53
+
54
+ const prompt = `
55
+ Given these metadata keys from a database: [${keys.join(', ')}]
56
+
57
+ Identify which keys best correspond to these standard UI properties:
58
+ ${propertyList}
59
+
60
+ Return ONLY a JSON object where the keys are the UI properties and the values are the matching database keys.
61
+ If no good match is found for a property, omit it.
62
+
63
+ Example:
64
+ {
65
+ "name": "Product_Title",
66
+ "price": "MSRP_USD",
67
+ "brand": "VendorName"
68
+ }
69
+ `;
70
+
71
+ try {
72
+ const response = await llm.chat(
73
+ [
74
+ { role: 'system', content: 'You are a database schema expert. You ONLY respond with valid JSON.' },
75
+ { role: 'user', content: prompt }
76
+ ],
77
+ ''
78
+ );
79
+
80
+ // Extract the first balanced JSON object from the response
81
+ const startIdx = response.indexOf('{');
82
+ if (startIdx !== -1) {
83
+ let braceCount = 0;
84
+ let endIdx = -1;
85
+
86
+ for (let i = startIdx; i < response.length; i++) {
87
+ if (response[i] === '{') braceCount++;
88
+ else if (response[i] === '}') {
89
+ braceCount--;
90
+ if (braceCount === 0) {
91
+ endIdx = i;
92
+ break;
93
+ }
94
+ }
95
+ }
96
+
97
+ if (endIdx !== -1) {
98
+ const jsonContent = response.substring(startIdx, endIdx + 1);
99
+ const cleanJson = this.sanitizeJson(jsonContent);
100
+ const mapping = JSON.parse(cleanJson) as SchemaMap;
101
+ this.cache.set(cacheKey, mapping);
102
+ return mapping;
103
+ }
104
+ }
105
+ } catch (error) {
106
+ console.warn('[SchemaMapper] AI training failed, falling back to heuristics:', error);
107
+ }
108
+
109
+ return {};
110
+ }
111
+
112
+ /**
113
+ * Forgiving JSON parser that fixes common AI formatting mistakes.
114
+ */
115
+ private static sanitizeJson(s: string): string {
116
+ return s
117
+ .replace(/\/\*[\s\S]*?\*\/|([^:]|^)\/\/.*$/gm, '') // Remove comments
118
+ .replace(/[\u201C\u201D\u2018\u2019]/g, '"') // Replace smart quotes
119
+ .replace(/'/g, '"') // Replace single quotes
120
+ .replace(/([{,]\s*)([a-zA-Z0-9_]+?)\s*:/g, '$1"$2":') // Add quotes to keys
121
+ .replace(/,\s*([}\]])/g, '$1') // Remove trailing commas
122
+ .trim();
123
+ }
124
+
125
+ static getCached(projectId: string, keys: string[]): SchemaMap | undefined {
126
+ const cacheKey = `${projectId}:${keys.sort().join(',')}`;
127
+ return this.cache.get(cacheKey);
128
+ }
129
+ }