@retrivora-ai/rag-engine 1.8.0 → 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.
- package/dist/DocumentChunker-Dh9TvmGG.d.mts +45 -0
- package/dist/DocumentChunker-Dh9TvmGG.d.ts +45 -0
- package/dist/{index-DPsQodME.d.mts → ILLMProvider-BfRgI1Xh.d.mts} +58 -1
- package/dist/{index-DPsQodME.d.ts → ILLMProvider-BfRgI1Xh.d.ts} +58 -1
- package/dist/MultiTablePostgresProvider-YY7LPNJK.mjs +8 -0
- package/dist/{chunk-PV3MFHWU.mjs → chunk-BFYLQYQU.mjs} +808 -439
- package/dist/chunk-R3RGUMHE.mjs +218 -0
- package/dist/handlers/index.d.mts +2 -2
- package/dist/handlers/index.d.ts +2 -2
- package/dist/handlers/index.js +995 -603
- package/dist/handlers/index.mjs +1 -1
- package/dist/{index-Bb2yEopi.d.mts → index-1Z4GuYBi.d.ts} +7 -1
- package/dist/{index-CkbTzj9J.d.ts → index-BV0z5mb6.d.mts} +7 -1
- package/dist/index.d.mts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +491 -316
- package/dist/index.mjs +411 -217
- package/dist/server.d.mts +35 -5
- package/dist/server.d.ts +35 -5
- package/dist/server.js +1146 -777
- package/dist/server.mjs +163 -176
- package/package.json +4 -2
- package/src/app/page.tsx +1 -2
- package/src/components/MessageBubble.tsx +211 -69
- package/src/components/ProductCard.tsx +2 -2
- package/src/components/VisualizationRenderer.tsx +347 -247
- package/src/config/RagConfig.ts +5 -0
- package/src/config/serverConfig.ts +3 -1
- package/src/core/Pipeline.ts +65 -206
- package/src/core/ProviderRegistry.ts +2 -2
- package/src/core/VectorPlugin.ts +9 -0
- package/src/handlers/index.ts +23 -7
- package/src/hooks/useRagChat.ts +2 -2
- package/src/llm/LLMFactory.ts +54 -2
- package/src/llm/providers/AnthropicProvider.ts +12 -8
- package/src/llm/providers/GeminiProvider.ts +188 -143
- package/src/llm/providers/OllamaProvider.ts +7 -3
- package/src/llm/providers/OpenAIProvider.ts +12 -8
- package/src/types/chat.ts +6 -0
- package/src/types/index.ts +80 -0
- package/src/utils/SchemaMapper.ts +129 -0
- package/src/utils/UITransformer.ts +491 -181
- package/dist/DocumentChunker-C1GEEosY.d.ts +0 -93
- package/dist/DocumentChunker-CFEiRopR.d.mts +0 -93
- package/dist/PostgreSQLProvider-BMOETDZA.mjs +0 -8
- 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
|
+
}
|