@retrivora-ai/rag-engine 2.0.6 → 2.0.8

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
@@ -1750,7 +1750,7 @@ function extractProductsFromSources(sources, isUser) {
1750
1750
  const hasPricePattern = /\$\s*\d+/.test(s.content);
1751
1751
  return hasStrongProductKey || hasProductIdentity || hasPricePattern || m.type === "product";
1752
1752
  }).map((s) => {
1753
- var _a, _b;
1753
+ var _a, _b, _c;
1754
1754
  const m = (_a = s.metadata) != null ? _a : {};
1755
1755
  const name = resolveMetadataValue(m, "name");
1756
1756
  const brand = resolveMetadataValue(m, "brand");
@@ -1758,7 +1758,7 @@ function extractProductsFromSources(sources, isUser) {
1758
1758
  const description = resolveMetadataValue(m, "description");
1759
1759
  return {
1760
1760
  id: s.id,
1761
- name: (_b = name != null ? name : s.content.split("\n")[0]) != null ? _b : "Unknown Product",
1761
+ name: (_c = name != null ? name : ((_b = s == null ? void 0 : s.content) != null ? _b : "").split("\n")[0]) != null ? _c : "Unknown Product",
1762
1762
  brand,
1763
1763
  price,
1764
1764
  image: resolveImage(m),
@@ -3654,7 +3654,10 @@ var IntentClassifier = class {
3654
3654
  hasCategoricalFields = catKeys.size > 0;
3655
3655
  numericFieldCount = numericKeys.size;
3656
3656
  categoricalFieldCount = catKeys.size;
3657
- if (productKeyMatches >= 2 || docs.some((d) => d.content.toLowerCase().includes("price") || d.content.toLowerCase().includes("brand"))) {
3657
+ if (productKeyMatches >= 2 || docs.some((d) => {
3658
+ var _a, _b;
3659
+ return ((_a = d == null ? void 0 : d.content) != null ? _a : "").toLowerCase().includes("price") || ((_b = d == null ? void 0 : d.content) != null ? _b : "").toLowerCase().includes("brand");
3660
+ })) {
3658
3661
  isProductLike = true;
3659
3662
  }
3660
3663
  }
@@ -4045,6 +4048,19 @@ var UITransformer = class _UITransformer {
4045
4048
  if (!retrievedData || retrievedData.length === 0) {
4046
4049
  return this.createTextResponse("No data available", "No relevant data found for your query.");
4047
4050
  }
4051
+ console.log("[UITransformer.transform] Processing retrievedData:", {
4052
+ userQuery,
4053
+ retrievedCount: retrievedData.length,
4054
+ sample: retrievedData.slice(0, 2).map((item) => {
4055
+ var _a2;
4056
+ return {
4057
+ id: item == null ? void 0 : item.id,
4058
+ contentType: typeof (item == null ? void 0 : item.content),
4059
+ contentSnippet: String((_a2 = item == null ? void 0 : item.content) != null ? _a2 : "").substring(0, 150),
4060
+ metadataKeys: Object.keys((item == null ? void 0 : item.metadata) || {})
4061
+ };
4062
+ })
4063
+ });
4048
4064
  const resolvedIntent = intent != null ? intent : this.detectIntentHeuristic(userQuery);
4049
4065
  const filteredData = resolvedIntent.filterInStockOnly ? retrievedData.filter((item) => this.determineStockStatus(item)) : retrievedData;
4050
4066
  const profile = this.profileData(filteredData);
@@ -4385,7 +4401,7 @@ ${schemaProfileText}` : ""}`;
4385
4401
  }
4386
4402
  // ─── Transform Helpers ────────────────────────────────────────────────────
4387
4403
  static transformToProductCarousel(data, config, trainedSchema) {
4388
- const products = data.map((item) => this.extractProductInfo(item, config, trainedSchema)).filter((p) => p !== null).slice(0, 15);
4404
+ const products = data.map((item) => this.extractProductInfo(item, config, trainedSchema)).filter((p) => p !== null);
4389
4405
  return {
4390
4406
  type: "product_carousel",
4391
4407
  title: "Recommended Products",
@@ -4534,7 +4550,10 @@ ${schemaProfileText}` : ""}`;
4534
4550
  type: "radar_chart",
4535
4551
  title: "Product Comparison",
4536
4552
  description: `Comparing ${data.length} items across ${radarData.length} attributes`,
4537
- data: radarData.length > 0 ? radarData : data.map((d) => ({ attribute: d.content.substring(0, 40) }))
4553
+ data: radarData.length > 0 ? radarData : data.map((d) => {
4554
+ var _a;
4555
+ return { attribute: ((_a = d == null ? void 0 : d.content) != null ? _a : "").substring(0, 40) };
4556
+ })
4538
4557
  };
4539
4558
  }
4540
4559
  static transformToTable(data, query = "") {
@@ -4551,7 +4570,10 @@ ${schemaProfileText}` : ""}`;
4551
4570
  static transformToText(data) {
4552
4571
  return this.createTextResponse(
4553
4572
  "Retrieved Context",
4554
- data.map((item) => item.content).join("\n\n"),
4573
+ data.map((item) => {
4574
+ var _a;
4575
+ return (_a = item == null ? void 0 : item.content) != null ? _a : "";
4576
+ }).join("\n\n"),
4555
4577
  `Found ${data.length} relevant results`
4556
4578
  );
4557
4579
  }
@@ -4712,6 +4734,7 @@ ${schemaProfileText}` : ""}`;
4712
4734
  return productTerms && productAction && !nonProductEntityTerms;
4713
4735
  }
4714
4736
  static isProductData(item) {
4737
+ if (!item) return false;
4715
4738
  const content = (item.content || "").toLowerCase();
4716
4739
  const productKeywords = [
4717
4740
  "product",
@@ -4758,7 +4781,8 @@ ${schemaProfileText}` : ""}`;
4758
4781
  return fieldCount.size > 2;
4759
4782
  }
4760
4783
  static profileData(data) {
4761
- const records = data.map((item) => {
4784
+ const records = (data || []).filter((item) => Boolean(item && typeof item === "object")).map((item) => {
4785
+ var _a, _b;
4762
4786
  const fields2 = {};
4763
4787
  Object.entries(item.metadata || {}).forEach(([key, value]) => {
4764
4788
  const primitive = this.toPrimitive(value);
@@ -4767,8 +4791,8 @@ ${schemaProfileText}` : ""}`;
4767
4791
  if (!fields2.content && item.content) fields2.content = item.content.substring(0, 500);
4768
4792
  return {
4769
4793
  id: item.id,
4770
- content: item.content,
4771
- score: item.score,
4794
+ content: (_a = item.content) != null ? _a : "",
4795
+ score: (_b = item.score) != null ? _b : 0,
4772
4796
  fields: fields2,
4773
4797
  source: item
4774
4798
  };
@@ -4970,12 +4994,12 @@ ${schemaProfileText}` : ""}`;
4970
4994
  }
4971
4995
  static extractTimeSeriesData(data) {
4972
4996
  return data.map((item) => {
4973
- var _a, _b, _c, _d, _e;
4997
+ var _a, _b, _c, _d, _e, _f;
4974
4998
  const meta = item.metadata || {};
4975
4999
  return {
4976
5000
  timestamp: (_b = (_a = meta.timestamp) != null ? _a : meta.date) != null ? _b : (/* @__PURE__ */ new Date()).toISOString(),
4977
5001
  value: (_d = (_c = meta.value) != null ? _c : item.score) != null ? _d : 0,
4978
- label: (_e = meta.label) != null ? _e : item.content.substring(0, 50)
5002
+ label: (_f = meta.label) != null ? _f : ((_e = item == null ? void 0 : item.content) != null ? _e : "").substring(0, 50)
4979
5003
  };
4980
5004
  });
4981
5005
  }
@@ -5089,7 +5113,8 @@ ${schemaProfileText}` : ""}`;
5089
5113
  }, query.includes(normalizedField) ? 3 : 0);
5090
5114
  }
5091
5115
  static resolveTableCellValue(item, column) {
5092
- if (column === "Content") return item.content.substring(0, 100);
5116
+ var _a, _b, _c;
5117
+ if (column === "Content") return ((_a = item == null ? void 0 : item.content) != null ? _a : "").substring(0, 100);
5093
5118
  const meta = item.metadata || {};
5094
5119
  const normalizedColumn = this.normalizeComparableField(column);
5095
5120
  const exactMetadata = Object.entries(meta).find(
@@ -5100,9 +5125,9 @@ ${schemaProfileText}` : ""}`;
5100
5125
  }
5101
5126
  const aliasValue = this.resolveAliasedTableCell(meta, column);
5102
5127
  if (aliasValue !== void 0) return this.toDisplayValue(aliasValue);
5103
- const contentValue = this.extractContentFieldValue(item.content, column);
5128
+ const contentValue = this.extractContentFieldValue((_b = item == null ? void 0 : item.content) != null ? _b : "", column);
5104
5129
  if (contentValue !== null) return contentValue;
5105
- const aliasContentValue = this.extractAliasedContentFieldValue(item.content, column);
5130
+ const aliasContentValue = this.extractAliasedContentFieldValue((_c = item == null ? void 0 : item.content) != null ? _c : "", column);
5106
5131
  return aliasContentValue != null ? aliasContentValue : "";
5107
5132
  }
5108
5133
  static resolveAliasedTableCell(meta, column) {
@@ -5152,6 +5177,7 @@ ${schemaProfileText}` : ""}`;
5152
5177
  return { inStockCount: inStock, outOfStockCount: outOfStock };
5153
5178
  }
5154
5179
  static determineStockStatus(item) {
5180
+ if (!item) return true;
5155
5181
  const meta = item.metadata || {};
5156
5182
  const stockValue = resolveMetadataValue(meta, "stock");
5157
5183
  if (stockValue !== void 0) {
@@ -5197,35 +5223,64 @@ ${schemaProfileText}` : ""}`;
5197
5223
  return resolveMetadataValue(meta, uiKey);
5198
5224
  }
5199
5225
  static extractProductInfo(item, config, trainedSchema) {
5200
- var _a;
5226
+ var _a, _b;
5227
+ if (!item) return null;
5201
5228
  const meta = item.metadata || {};
5229
+ const content = item.content || "";
5202
5230
  const name = this.getDynamicVal(meta, "name", config, trainedSchema);
5203
5231
  const price = this.getDynamicVal(meta, "price", config, trainedSchema);
5204
5232
  const brand = this.getDynamicVal(meta, "brand", config, trainedSchema);
5205
5233
  const description = this.cleanProductDescription(
5206
- (_a = this.extractProductDescriptionFromContent(item.content)) != null ? _a : this.getProductDescriptionValue(meta, config, trainedSchema)
5234
+ (_a = this.extractProductDescriptionFromContent(content)) != null ? _a : this.getProductDescriptionValue(meta, config, trainedSchema)
5207
5235
  );
5208
5236
  if (name || this.isProductData(item)) {
5209
5237
  let finalName = name ? String(name) : void 0;
5238
+ if (!finalName && content) {
5239
+ const nameMatch = content.match(/(?:Product|Item|Name):\s*([^\n]+)/i);
5240
+ finalName = nameMatch ? nameMatch[1].trim() : (_b = content.split("\n")[0]) == null ? void 0 : _b.substring(0, 60);
5241
+ }
5210
5242
  if (!finalName) {
5211
- const nameMatch = item.content.match(/(?:Product|Item|Name):\s*([^\n]+)/i);
5212
- finalName = nameMatch ? nameMatch[1].trim() : item.content.split("\n")[0].substring(0, 60);
5243
+ console.warn(
5244
+ `[UITransformer] Unable to read product name from metadata or content for item ${item.id}. Defaulting to 'Unnamed Product'.`,
5245
+ { metadataKeys: Object.keys(meta), contentLength: content.length }
5246
+ );
5213
5247
  }
5214
5248
  let finalPrice = typeof price === "number" || typeof price === "string" ? price : void 0;
5215
- if (!finalPrice) {
5216
- const priceMatch = item.content.match(/(?:Price|Cost):\s*\$?([\d,.]+)/i) || item.content.match(/\$\s*([\d,.]+)/);
5249
+ if (!finalPrice && content) {
5250
+ const priceMatch = content.match(/(?:Price|Cost):\s*\$?([\d,.]+)/i) || content.match(/\$\s*([\d,.]+)/);
5217
5251
  if (priceMatch) finalPrice = priceMatch[1].replace(/,/g, "");
5218
5252
  }
5253
+ if (finalPrice === void 0) {
5254
+ console.warn(
5255
+ `[UITransformer] Unable to read product price from metadata or content for item ${item.id}.`,
5256
+ { metadataKeys: Object.keys(meta), contentSnippet: content.substring(0, 100) }
5257
+ );
5258
+ }
5219
5259
  const imageValue = this.getDynamicVal(meta, "image", config, trainedSchema);
5260
+ if (!imageValue) {
5261
+ console.debug(
5262
+ `[UITransformer] Unable to read product image URL from metadata for item ${item.id}.`
5263
+ );
5264
+ }
5265
+ if (!description) {
5266
+ console.debug(
5267
+ `[UITransformer] Unable to read product description from metadata or content for item ${item.id}.`
5268
+ );
5269
+ }
5220
5270
  return {
5221
5271
  id: item.id,
5222
- name: finalName,
5272
+ name: finalName || "Unnamed Product",
5223
5273
  price: finalPrice,
5224
5274
  image: typeof imageValue === "string" ? imageValue : void 0,
5225
5275
  brand: brand ? String(brand) : void 0,
5226
5276
  description,
5227
5277
  inStock: this.determineStockStatus(item)
5228
5278
  };
5279
+ } else {
5280
+ console.warn(
5281
+ `[UITransformer] Item ${item.id} skipped: unable to read name or identify as product data from metadata/content.`,
5282
+ { metadataKeys: Object.keys(meta), contentSnippet: content.substring(0, 100) }
5283
+ );
5229
5284
  }
5230
5285
  return null;
5231
5286
  }
@@ -5310,13 +5365,13 @@ RULES:
5310
5365
  5. If no relevant data is found, return text: "I cannot answer this question based on the available information."`;
5311
5366
  }
5312
5367
  static buildContextSummary(sources, maxChars = 6e3) {
5313
- const items = sources.map((s, i) => {
5368
+ const items = (sources || []).filter(Boolean).map((s, i) => {
5314
5369
  var _a, _b, _c, _d;
5315
5370
  return {
5316
5371
  index: i + 1,
5317
- content: (_b = (_a = s.content) == null ? void 0 : _a.substring(0, 400)) != null ? _b : "",
5318
- metadata: (_c = s.metadata) != null ? _c : {},
5319
- score: (_d = s.score) != null ? _d : 0
5372
+ content: (_b = (_a = s == null ? void 0 : s.content) == null ? void 0 : _a.substring(0, 400)) != null ? _b : "",
5373
+ metadata: (_c = s == null ? void 0 : s.metadata) != null ? _c : {},
5374
+ score: (_d = s == null ? void 0 : s.score) != null ? _d : 0
5320
5375
  };
5321
5376
  });
5322
5377
  const full = JSON.stringify(items, null, 2);
@@ -5344,7 +5399,10 @@ var TextRendererStrategy = class {
5344
5399
  return { type: "text", title, data: { content: data } };
5345
5400
  }
5346
5401
  const docs = Array.isArray(data) ? data : [];
5347
- const text = docs.map((d) => d.content).join("\n\n");
5402
+ const text = docs.map((d) => {
5403
+ var _a;
5404
+ return (_a = d == null ? void 0 : d.content) != null ? _a : "";
5405
+ }).join("\n\n");
5348
5406
  return { type: "text", title, data: { content: text || "No detailed content available." } };
5349
5407
  }
5350
5408
  };
package/dist/index.mjs CHANGED
@@ -1752,7 +1752,7 @@ function extractProductsFromSources(sources, isUser) {
1752
1752
  const hasPricePattern = /\$\s*\d+/.test(s.content);
1753
1753
  return hasStrongProductKey || hasProductIdentity || hasPricePattern || m.type === "product";
1754
1754
  }).map((s) => {
1755
- var _a, _b;
1755
+ var _a, _b, _c;
1756
1756
  const m = (_a = s.metadata) != null ? _a : {};
1757
1757
  const name = resolveMetadataValue(m, "name");
1758
1758
  const brand = resolveMetadataValue(m, "brand");
@@ -1760,7 +1760,7 @@ function extractProductsFromSources(sources, isUser) {
1760
1760
  const description = resolveMetadataValue(m, "description");
1761
1761
  return {
1762
1762
  id: s.id,
1763
- name: (_b = name != null ? name : s.content.split("\n")[0]) != null ? _b : "Unknown Product",
1763
+ name: (_c = name != null ? name : ((_b = s == null ? void 0 : s.content) != null ? _b : "").split("\n")[0]) != null ? _c : "Unknown Product",
1764
1764
  brand,
1765
1765
  price,
1766
1766
  image: resolveImage(m),
@@ -3656,7 +3656,10 @@ var IntentClassifier = class {
3656
3656
  hasCategoricalFields = catKeys.size > 0;
3657
3657
  numericFieldCount = numericKeys.size;
3658
3658
  categoricalFieldCount = catKeys.size;
3659
- if (productKeyMatches >= 2 || docs.some((d) => d.content.toLowerCase().includes("price") || d.content.toLowerCase().includes("brand"))) {
3659
+ if (productKeyMatches >= 2 || docs.some((d) => {
3660
+ var _a, _b;
3661
+ return ((_a = d == null ? void 0 : d.content) != null ? _a : "").toLowerCase().includes("price") || ((_b = d == null ? void 0 : d.content) != null ? _b : "").toLowerCase().includes("brand");
3662
+ })) {
3660
3663
  isProductLike = true;
3661
3664
  }
3662
3665
  }
@@ -4047,6 +4050,19 @@ var UITransformer = class _UITransformer {
4047
4050
  if (!retrievedData || retrievedData.length === 0) {
4048
4051
  return this.createTextResponse("No data available", "No relevant data found for your query.");
4049
4052
  }
4053
+ console.log("[UITransformer.transform] Processing retrievedData:", {
4054
+ userQuery,
4055
+ retrievedCount: retrievedData.length,
4056
+ sample: retrievedData.slice(0, 2).map((item) => {
4057
+ var _a2;
4058
+ return {
4059
+ id: item == null ? void 0 : item.id,
4060
+ contentType: typeof (item == null ? void 0 : item.content),
4061
+ contentSnippet: String((_a2 = item == null ? void 0 : item.content) != null ? _a2 : "").substring(0, 150),
4062
+ metadataKeys: Object.keys((item == null ? void 0 : item.metadata) || {})
4063
+ };
4064
+ })
4065
+ });
4050
4066
  const resolvedIntent = intent != null ? intent : this.detectIntentHeuristic(userQuery);
4051
4067
  const filteredData = resolvedIntent.filterInStockOnly ? retrievedData.filter((item) => this.determineStockStatus(item)) : retrievedData;
4052
4068
  const profile = this.profileData(filteredData);
@@ -4387,7 +4403,7 @@ ${schemaProfileText}` : ""}`;
4387
4403
  }
4388
4404
  // ─── Transform Helpers ────────────────────────────────────────────────────
4389
4405
  static transformToProductCarousel(data, config, trainedSchema) {
4390
- const products = data.map((item) => this.extractProductInfo(item, config, trainedSchema)).filter((p) => p !== null).slice(0, 15);
4406
+ const products = data.map((item) => this.extractProductInfo(item, config, trainedSchema)).filter((p) => p !== null);
4391
4407
  return {
4392
4408
  type: "product_carousel",
4393
4409
  title: "Recommended Products",
@@ -4536,7 +4552,10 @@ ${schemaProfileText}` : ""}`;
4536
4552
  type: "radar_chart",
4537
4553
  title: "Product Comparison",
4538
4554
  description: `Comparing ${data.length} items across ${radarData.length} attributes`,
4539
- data: radarData.length > 0 ? radarData : data.map((d) => ({ attribute: d.content.substring(0, 40) }))
4555
+ data: radarData.length > 0 ? radarData : data.map((d) => {
4556
+ var _a;
4557
+ return { attribute: ((_a = d == null ? void 0 : d.content) != null ? _a : "").substring(0, 40) };
4558
+ })
4540
4559
  };
4541
4560
  }
4542
4561
  static transformToTable(data, query = "") {
@@ -4553,7 +4572,10 @@ ${schemaProfileText}` : ""}`;
4553
4572
  static transformToText(data) {
4554
4573
  return this.createTextResponse(
4555
4574
  "Retrieved Context",
4556
- data.map((item) => item.content).join("\n\n"),
4575
+ data.map((item) => {
4576
+ var _a;
4577
+ return (_a = item == null ? void 0 : item.content) != null ? _a : "";
4578
+ }).join("\n\n"),
4557
4579
  `Found ${data.length} relevant results`
4558
4580
  );
4559
4581
  }
@@ -4714,6 +4736,7 @@ ${schemaProfileText}` : ""}`;
4714
4736
  return productTerms && productAction && !nonProductEntityTerms;
4715
4737
  }
4716
4738
  static isProductData(item) {
4739
+ if (!item) return false;
4717
4740
  const content = (item.content || "").toLowerCase();
4718
4741
  const productKeywords = [
4719
4742
  "product",
@@ -4760,7 +4783,8 @@ ${schemaProfileText}` : ""}`;
4760
4783
  return fieldCount.size > 2;
4761
4784
  }
4762
4785
  static profileData(data) {
4763
- const records = data.map((item) => {
4786
+ const records = (data || []).filter((item) => Boolean(item && typeof item === "object")).map((item) => {
4787
+ var _a, _b;
4764
4788
  const fields2 = {};
4765
4789
  Object.entries(item.metadata || {}).forEach(([key, value]) => {
4766
4790
  const primitive = this.toPrimitive(value);
@@ -4769,8 +4793,8 @@ ${schemaProfileText}` : ""}`;
4769
4793
  if (!fields2.content && item.content) fields2.content = item.content.substring(0, 500);
4770
4794
  return {
4771
4795
  id: item.id,
4772
- content: item.content,
4773
- score: item.score,
4796
+ content: (_a = item.content) != null ? _a : "",
4797
+ score: (_b = item.score) != null ? _b : 0,
4774
4798
  fields: fields2,
4775
4799
  source: item
4776
4800
  };
@@ -4972,12 +4996,12 @@ ${schemaProfileText}` : ""}`;
4972
4996
  }
4973
4997
  static extractTimeSeriesData(data) {
4974
4998
  return data.map((item) => {
4975
- var _a, _b, _c, _d, _e;
4999
+ var _a, _b, _c, _d, _e, _f;
4976
5000
  const meta = item.metadata || {};
4977
5001
  return {
4978
5002
  timestamp: (_b = (_a = meta.timestamp) != null ? _a : meta.date) != null ? _b : (/* @__PURE__ */ new Date()).toISOString(),
4979
5003
  value: (_d = (_c = meta.value) != null ? _c : item.score) != null ? _d : 0,
4980
- label: (_e = meta.label) != null ? _e : item.content.substring(0, 50)
5004
+ label: (_f = meta.label) != null ? _f : ((_e = item == null ? void 0 : item.content) != null ? _e : "").substring(0, 50)
4981
5005
  };
4982
5006
  });
4983
5007
  }
@@ -5091,7 +5115,8 @@ ${schemaProfileText}` : ""}`;
5091
5115
  }, query.includes(normalizedField) ? 3 : 0);
5092
5116
  }
5093
5117
  static resolveTableCellValue(item, column) {
5094
- if (column === "Content") return item.content.substring(0, 100);
5118
+ var _a, _b, _c;
5119
+ if (column === "Content") return ((_a = item == null ? void 0 : item.content) != null ? _a : "").substring(0, 100);
5095
5120
  const meta = item.metadata || {};
5096
5121
  const normalizedColumn = this.normalizeComparableField(column);
5097
5122
  const exactMetadata = Object.entries(meta).find(
@@ -5102,9 +5127,9 @@ ${schemaProfileText}` : ""}`;
5102
5127
  }
5103
5128
  const aliasValue = this.resolveAliasedTableCell(meta, column);
5104
5129
  if (aliasValue !== void 0) return this.toDisplayValue(aliasValue);
5105
- const contentValue = this.extractContentFieldValue(item.content, column);
5130
+ const contentValue = this.extractContentFieldValue((_b = item == null ? void 0 : item.content) != null ? _b : "", column);
5106
5131
  if (contentValue !== null) return contentValue;
5107
- const aliasContentValue = this.extractAliasedContentFieldValue(item.content, column);
5132
+ const aliasContentValue = this.extractAliasedContentFieldValue((_c = item == null ? void 0 : item.content) != null ? _c : "", column);
5108
5133
  return aliasContentValue != null ? aliasContentValue : "";
5109
5134
  }
5110
5135
  static resolveAliasedTableCell(meta, column) {
@@ -5154,6 +5179,7 @@ ${schemaProfileText}` : ""}`;
5154
5179
  return { inStockCount: inStock, outOfStockCount: outOfStock };
5155
5180
  }
5156
5181
  static determineStockStatus(item) {
5182
+ if (!item) return true;
5157
5183
  const meta = item.metadata || {};
5158
5184
  const stockValue = resolveMetadataValue(meta, "stock");
5159
5185
  if (stockValue !== void 0) {
@@ -5199,35 +5225,64 @@ ${schemaProfileText}` : ""}`;
5199
5225
  return resolveMetadataValue(meta, uiKey);
5200
5226
  }
5201
5227
  static extractProductInfo(item, config, trainedSchema) {
5202
- var _a;
5228
+ var _a, _b;
5229
+ if (!item) return null;
5203
5230
  const meta = item.metadata || {};
5231
+ const content = item.content || "";
5204
5232
  const name = this.getDynamicVal(meta, "name", config, trainedSchema);
5205
5233
  const price = this.getDynamicVal(meta, "price", config, trainedSchema);
5206
5234
  const brand = this.getDynamicVal(meta, "brand", config, trainedSchema);
5207
5235
  const description = this.cleanProductDescription(
5208
- (_a = this.extractProductDescriptionFromContent(item.content)) != null ? _a : this.getProductDescriptionValue(meta, config, trainedSchema)
5236
+ (_a = this.extractProductDescriptionFromContent(content)) != null ? _a : this.getProductDescriptionValue(meta, config, trainedSchema)
5209
5237
  );
5210
5238
  if (name || this.isProductData(item)) {
5211
5239
  let finalName = name ? String(name) : void 0;
5240
+ if (!finalName && content) {
5241
+ const nameMatch = content.match(/(?:Product|Item|Name):\s*([^\n]+)/i);
5242
+ finalName = nameMatch ? nameMatch[1].trim() : (_b = content.split("\n")[0]) == null ? void 0 : _b.substring(0, 60);
5243
+ }
5212
5244
  if (!finalName) {
5213
- const nameMatch = item.content.match(/(?:Product|Item|Name):\s*([^\n]+)/i);
5214
- finalName = nameMatch ? nameMatch[1].trim() : item.content.split("\n")[0].substring(0, 60);
5245
+ console.warn(
5246
+ `[UITransformer] Unable to read product name from metadata or content for item ${item.id}. Defaulting to 'Unnamed Product'.`,
5247
+ { metadataKeys: Object.keys(meta), contentLength: content.length }
5248
+ );
5215
5249
  }
5216
5250
  let finalPrice = typeof price === "number" || typeof price === "string" ? price : void 0;
5217
- if (!finalPrice) {
5218
- const priceMatch = item.content.match(/(?:Price|Cost):\s*\$?([\d,.]+)/i) || item.content.match(/\$\s*([\d,.]+)/);
5251
+ if (!finalPrice && content) {
5252
+ const priceMatch = content.match(/(?:Price|Cost):\s*\$?([\d,.]+)/i) || content.match(/\$\s*([\d,.]+)/);
5219
5253
  if (priceMatch) finalPrice = priceMatch[1].replace(/,/g, "");
5220
5254
  }
5255
+ if (finalPrice === void 0) {
5256
+ console.warn(
5257
+ `[UITransformer] Unable to read product price from metadata or content for item ${item.id}.`,
5258
+ { metadataKeys: Object.keys(meta), contentSnippet: content.substring(0, 100) }
5259
+ );
5260
+ }
5221
5261
  const imageValue = this.getDynamicVal(meta, "image", config, trainedSchema);
5262
+ if (!imageValue) {
5263
+ console.debug(
5264
+ `[UITransformer] Unable to read product image URL from metadata for item ${item.id}.`
5265
+ );
5266
+ }
5267
+ if (!description) {
5268
+ console.debug(
5269
+ `[UITransformer] Unable to read product description from metadata or content for item ${item.id}.`
5270
+ );
5271
+ }
5222
5272
  return {
5223
5273
  id: item.id,
5224
- name: finalName,
5274
+ name: finalName || "Unnamed Product",
5225
5275
  price: finalPrice,
5226
5276
  image: typeof imageValue === "string" ? imageValue : void 0,
5227
5277
  brand: brand ? String(brand) : void 0,
5228
5278
  description,
5229
5279
  inStock: this.determineStockStatus(item)
5230
5280
  };
5281
+ } else {
5282
+ console.warn(
5283
+ `[UITransformer] Item ${item.id} skipped: unable to read name or identify as product data from metadata/content.`,
5284
+ { metadataKeys: Object.keys(meta), contentSnippet: content.substring(0, 100) }
5285
+ );
5231
5286
  }
5232
5287
  return null;
5233
5288
  }
@@ -5312,13 +5367,13 @@ RULES:
5312
5367
  5. If no relevant data is found, return text: "I cannot answer this question based on the available information."`;
5313
5368
  }
5314
5369
  static buildContextSummary(sources, maxChars = 6e3) {
5315
- const items = sources.map((s, i) => {
5370
+ const items = (sources || []).filter(Boolean).map((s, i) => {
5316
5371
  var _a, _b, _c, _d;
5317
5372
  return {
5318
5373
  index: i + 1,
5319
- content: (_b = (_a = s.content) == null ? void 0 : _a.substring(0, 400)) != null ? _b : "",
5320
- metadata: (_c = s.metadata) != null ? _c : {},
5321
- score: (_d = s.score) != null ? _d : 0
5374
+ content: (_b = (_a = s == null ? void 0 : s.content) == null ? void 0 : _a.substring(0, 400)) != null ? _b : "",
5375
+ metadata: (_c = s == null ? void 0 : s.metadata) != null ? _c : {},
5376
+ score: (_d = s == null ? void 0 : s.score) != null ? _d : 0
5322
5377
  };
5323
5378
  });
5324
5379
  const full = JSON.stringify(items, null, 2);
@@ -5346,7 +5401,10 @@ var TextRendererStrategy = class {
5346
5401
  return { type: "text", title, data: { content: data } };
5347
5402
  }
5348
5403
  const docs = Array.isArray(data) ? data : [];
5349
- const text = docs.map((d) => d.content).join("\n\n");
5404
+ const text = docs.map((d) => {
5405
+ var _a;
5406
+ return (_a = d == null ? void 0 : d.content) != null ? _a : "";
5407
+ }).join("\n\n");
5350
5408
  return { type: "text", title, data: { content: text || "No detailed content available." } };
5351
5409
  }
5352
5410
  };