@retrivora-ai/rag-engine 1.1.9 → 1.2.0

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/index.js CHANGED
@@ -227,6 +227,24 @@ function MessageBubble({
227
227
  }
228
228
  return void 0;
229
229
  };
230
+ const productsFromSources = import_react4.default.useMemo(() => {
231
+ if (isUser || !sources) return [];
232
+ return sources.filter((s) => {
233
+ const m = s.metadata || {};
234
+ return m.price || m.image || m.img || m.thumbnail || m.images || m.brand || m.type === "product";
235
+ }).map((s) => {
236
+ const m = s.metadata || {};
237
+ return {
238
+ id: s.id,
239
+ name: m.name || m.title || s.content.split("\n")[0] || "Unknown Product",
240
+ brand: m.brand,
241
+ price: m.price,
242
+ image: resolveImage(m),
243
+ link: m.link,
244
+ description: s.content
245
+ };
246
+ });
247
+ }, [sources, isUser]);
230
248
  const { productsFromContent, cleanContent } = import_react4.default.useMemo(() => {
231
249
  if (isUser) return { productsFromContent: [], cleanContent: message.content };
232
250
  const jsonRegex = /```json\s*([\s\S]*?)\s*```/g;
@@ -250,7 +268,23 @@ function MessageBubble({
250
268
  }
251
269
  return { productsFromContent: products, cleanContent: content.trim() };
252
270
  }, [message.content, isUser]);
253
- const allProducts = productsFromContent;
271
+ const allProducts = import_react4.default.useMemo(() => {
272
+ if (productsFromContent.length > 0) return productsFromContent;
273
+ const uniqueProducts = [];
274
+ const seenIds = /* @__PURE__ */ new Set();
275
+ const contentLower = message.content.toLowerCase();
276
+ for (const p of productsFromSources) {
277
+ const identifier = p.id || p.name;
278
+ if (seenIds.has(identifier)) continue;
279
+ const nameMatch = contentLower.includes(p.name.toLowerCase());
280
+ const brandMatch = p.brand ? contentLower.includes(p.brand.toLowerCase()) : false;
281
+ if (nameMatch || brandMatch) {
282
+ seenIds.add(identifier);
283
+ uniqueProducts.push(p);
284
+ }
285
+ }
286
+ return uniqueProducts;
287
+ }, [productsFromSources, productsFromContent, message.content]);
254
288
  const hasProducts = allProducts.length > 0;
255
289
  return /* @__PURE__ */ import_react4.default.createElement("div", { className: `flex gap-3 ${isUser ? "flex-row-reverse" : "flex-row"} items-start` }, /* @__PURE__ */ import_react4.default.createElement(
256
290
  "div",
package/dist/index.mjs CHANGED
@@ -185,6 +185,24 @@ function MessageBubble({
185
185
  }
186
186
  return void 0;
187
187
  };
188
+ const productsFromSources = React4.useMemo(() => {
189
+ if (isUser || !sources) return [];
190
+ return sources.filter((s) => {
191
+ const m = s.metadata || {};
192
+ return m.price || m.image || m.img || m.thumbnail || m.images || m.brand || m.type === "product";
193
+ }).map((s) => {
194
+ const m = s.metadata || {};
195
+ return {
196
+ id: s.id,
197
+ name: m.name || m.title || s.content.split("\n")[0] || "Unknown Product",
198
+ brand: m.brand,
199
+ price: m.price,
200
+ image: resolveImage(m),
201
+ link: m.link,
202
+ description: s.content
203
+ };
204
+ });
205
+ }, [sources, isUser]);
188
206
  const { productsFromContent, cleanContent } = React4.useMemo(() => {
189
207
  if (isUser) return { productsFromContent: [], cleanContent: message.content };
190
208
  const jsonRegex = /```json\s*([\s\S]*?)\s*```/g;
@@ -208,7 +226,23 @@ function MessageBubble({
208
226
  }
209
227
  return { productsFromContent: products, cleanContent: content.trim() };
210
228
  }, [message.content, isUser]);
211
- const allProducts = productsFromContent;
229
+ const allProducts = React4.useMemo(() => {
230
+ if (productsFromContent.length > 0) return productsFromContent;
231
+ const uniqueProducts = [];
232
+ const seenIds = /* @__PURE__ */ new Set();
233
+ const contentLower = message.content.toLowerCase();
234
+ for (const p of productsFromSources) {
235
+ const identifier = p.id || p.name;
236
+ if (seenIds.has(identifier)) continue;
237
+ const nameMatch = contentLower.includes(p.name.toLowerCase());
238
+ const brandMatch = p.brand ? contentLower.includes(p.brand.toLowerCase()) : false;
239
+ if (nameMatch || brandMatch) {
240
+ seenIds.add(identifier);
241
+ uniqueProducts.push(p);
242
+ }
243
+ }
244
+ return uniqueProducts;
245
+ }, [productsFromSources, productsFromContent, message.content]);
212
246
  const hasProducts = allProducts.length > 0;
213
247
  return /* @__PURE__ */ React4.createElement("div", { className: `flex gap-3 ${isUser ? "flex-row-reverse" : "flex-row"} items-start` }, /* @__PURE__ */ React4.createElement(
214
248
  "div",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@retrivora-ai/rag-engine",
3
- "version": "1.1.9",
3
+ "version": "1.2.0",
4
4
  "description": "Retrivora AI is a plug-and-play AI engine for RAG chat experiences — generic vector DB + LLM provider, embeddable or standalone.",
5
5
  "author": "Abhinav Alkuchi",
6
6
  "license": "MIT",
@@ -33,26 +33,48 @@ export function MessageBubble({
33
33
  // Helper to extract image from metadata or object
34
34
  const resolveImage = (data: Record<string, unknown>): string | undefined => {
35
35
  if (!data) return undefined;
36
-
36
+
37
37
  // Check single string fields
38
38
  const singleFields = ['image', 'img', 'thumbnail'];
39
39
  for (const field of singleFields) {
40
40
  const val = data[field];
41
41
  if (typeof val === 'string' && val) return val;
42
42
  }
43
-
43
+
44
44
  // Check images array
45
45
  if (Array.isArray(data.images) && data.images.length > 0) {
46
46
  if (typeof data.images[0] === 'string') return data.images[0];
47
47
  }
48
-
48
+
49
49
  return undefined;
50
50
  };
51
51
 
52
+ // Extract products from sources
53
+ const productsFromSources = React.useMemo(() => {
54
+ if (isUser || !sources) return [];
55
+ return sources
56
+ .filter(s => {
57
+ const m = s.metadata || {};
58
+ return m.price || m.image || m.img || m.thumbnail || m.images || m.brand || m.type === 'product';
59
+ })
60
+ .map(s => {
61
+ const m = (s.metadata || {}) as Record<string, unknown>;
62
+ return {
63
+ id: s.id,
64
+ name: (m.name || m.title || s.content.split('\n')[0] || 'Unknown Product') as string,
65
+ brand: m.brand as string,
66
+ price: m.price as string | number,
67
+ image: resolveImage(m),
68
+ link: m.link as string,
69
+ description: s.content
70
+ };
71
+ });
72
+ }, [sources, isUser]);
73
+
52
74
  // Extract products from content (structured JSON)
53
75
  const { productsFromContent, cleanContent } = React.useMemo(() => {
54
76
  if (isUser) return { productsFromContent: [], cleanContent: message.content };
55
-
77
+
56
78
  const jsonRegex = /```json\s*([\s\S]*?)\s*```/g;
57
79
  const products: Product[] = [];
58
80
  let content = message.content;
@@ -80,19 +102,41 @@ export function MessageBubble({
80
102
  return { productsFromContent: products, cleanContent: content.trim() };
81
103
  }, [message.content, isUser]);
82
104
 
83
- const allProducts = productsFromContent;
105
+ const allProducts = React.useMemo(() => {
106
+ // If the LLM explicitly provided products in a structured format, only show those.
107
+ if (productsFromContent.length > 0) return productsFromContent;
108
+
109
+ // Fallback: Show unique products from sources, but ONLY if they are mentioned in the message text.
110
+ // This aligns the carousel with what the assistant is actually "displaying" in its response.
111
+ const uniqueProducts: Product[] = [];
112
+ const seenIds = new Set();
113
+ const contentLower = message.content.toLowerCase();
84
114
 
115
+ for (const p of productsFromSources) {
116
+ const identifier = p.id || p.name;
117
+ if (seenIds.has(identifier)) continue;
118
+
119
+ // Check if the product name or brand is mentioned in the assistant's text
120
+ const nameMatch = contentLower.includes(p.name.toLowerCase());
121
+ const brandMatch = p.brand ? contentLower.includes(p.brand.toLowerCase()) : false;
122
+
123
+ if (nameMatch || brandMatch) {
124
+ seenIds.add(identifier);
125
+ uniqueProducts.push(p);
126
+ }
127
+ }
128
+
129
+ return uniqueProducts;
130
+ }, [productsFromSources, productsFromContent, message.content]);
85
131
 
86
- // Only surface product cards if the LLM explicitly provided them via structured JSON
87
132
  const hasProducts = allProducts.length > 0;
88
133
 
89
134
  return (
90
135
  <div className={`flex gap-3 ${isUser ? 'flex-row-reverse' : 'flex-row'} items-start`}>
91
136
  {/* Avatar */}
92
137
  <div
93
- className={`flex-shrink-0 w-8 h-8 rounded-full flex items-center justify-center shadow-lg ${
94
- isUser ? 'text-white' : 'bg-slate-100 dark:bg-white/10 text-slate-700 dark:text-white/80'
95
- }`}
138
+ className={`flex-shrink-0 w-8 h-8 rounded-full flex items-center justify-center shadow-lg ${isUser ? 'text-white' : 'bg-slate-100 dark:bg-white/10 text-slate-700 dark:text-white/80'
139
+ }`}
96
140
  style={isUser ? { background: `linear-gradient(135deg, ${primaryColor}, ${accentColor})` } : {}}
97
141
  >
98
142
  {isUser
@@ -104,11 +148,10 @@ export function MessageBubble({
104
148
  <div className={`flex flex-col gap-1 max-w-[90%] ${isUser ? 'items-end' : 'items-start'}`}>
105
149
  {/* Bubble */}
106
150
  <div
107
- className={`relative px-4 py-3 rounded-2xl text-sm leading-relaxed shadow-sm dark:shadow-lg ${
108
- isUser
151
+ className={`relative px-4 py-3 rounded-2xl text-sm leading-relaxed shadow-sm dark:shadow-lg ${isUser
109
152
  ? 'text-white rounded-tr-sm'
110
153
  : 'bg-white dark:bg-white/5 border border-slate-200 dark:border-white/10 text-slate-800 dark:text-white/90 rounded-tl-sm'
111
- }`}
154
+ }`}
112
155
  style={
113
156
  isUser
114
157
  ? { background: `linear-gradient(135deg, ${primaryColor}, ${accentColor})` }
@@ -126,7 +169,7 @@ export function MessageBubble({
126
169
  <ReactMarkdown remarkPlugins={[remarkGfm]}>
127
170
  {cleanContent || message.content}
128
171
  </ReactMarkdown>
129
-
172
+
130
173
  {isStreaming && message.content && (
131
174
  <span className="inline-block w-1.5 h-3.5 ml-1 bg-emerald-500 animate-pulse align-middle" />
132
175
  )}
@@ -137,9 +180,9 @@ export function MessageBubble({
137
180
  {/* Product Carousel (Outside the bubble) */}
138
181
  {!isUser && hasProducts && (
139
182
  <div className="w-full mt-1">
140
- <ProductCarousel
141
- products={allProducts}
142
- primaryColor={primaryColor}
183
+ <ProductCarousel
184
+ products={allProducts}
185
+ primaryColor={primaryColor}
143
186
  onAddToCart={onAddToCart}
144
187
  />
145
188
  </div>