@retrivora-ai/rag-engine 2.6.5 → 2.6.6

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.
@@ -410,7 +410,14 @@ interface UseRagChatReturn {
410
410
  /** Clear the conversation (and localStorage if persist=true) */
411
411
  clear: () => void;
412
412
  /** Retry the last failed send */
413
+ /** Retry the last failed send */
413
414
  retry: () => Promise<void>;
415
+ /** Dismiss the current error banner */
416
+ clearError: () => void;
417
+ /** Regenerate the latest assistant response */
418
+ regenerate: () => Promise<void>;
419
+ /** Edit a user message and resubmit the conversation from that point */
420
+ editAndResend: (messageId: string, newText: string) => Promise<void>;
414
421
  /** Programmatically set the conversation (e.g. to restore from a DB) */
415
422
  setMessages: React.Dispatch<React.SetStateAction<RagMessage[]>>;
416
423
  /** Abort the current active stream request */
@@ -410,7 +410,14 @@ interface UseRagChatReturn {
410
410
  /** Clear the conversation (and localStorage if persist=true) */
411
411
  clear: () => void;
412
412
  /** Retry the last failed send */
413
+ /** Retry the last failed send */
413
414
  retry: () => Promise<void>;
415
+ /** Dismiss the current error banner */
416
+ clearError: () => void;
417
+ /** Regenerate the latest assistant response */
418
+ regenerate: () => Promise<void>;
419
+ /** Edit a user message and resubmit the conversation from that point */
420
+ editAndResend: (messageId: string, newText: string) => Promise<void>;
414
421
  /** Programmatically set the conversation (e.g. to restore from a DB) */
415
422
  setMessages: React.Dispatch<React.SetStateAction<RagMessage[]>>;
416
423
  /** Abort the current active stream request */
@@ -1,8 +1,8 @@
1
1
  import {
2
2
  PineconeProvider
3
- } from "./chunk-UB4IVERI.mjs";
4
- import "./chunk-PLODIWBN.mjs";
5
- import "./chunk-4NRSH3VX.mjs";
3
+ } from "./chunk-JNPVVOIF.mjs";
4
+ import "./chunk-557UWRCG.mjs";
5
+ import "./chunk-Q4MVJ7NW.mjs";
6
6
  import "./chunk-IMP6FUCY.mjs";
7
7
  import "./chunk-UT6SZZEC.mjs";
8
8
  export {
@@ -5,7 +5,7 @@ import {
5
5
  } from "./chunk-XYBKDJDW.mjs";
6
6
  import {
7
7
  SDK_VERSION
8
- } from "./chunk-4NRSH3VX.mjs";
8
+ } from "./chunk-Q4MVJ7NW.mjs";
9
9
  import {
10
10
  __spreadValues
11
11
  } from "./chunk-UT6SZZEC.mjs";
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  SDK_VERSION
3
- } from "./chunk-4NRSH3VX.mjs";
3
+ } from "./chunk-Q4MVJ7NW.mjs";
4
4
  import {
5
5
  __spreadValues
6
6
  } from "./chunk-UT6SZZEC.mjs";
@@ -14,7 +14,7 @@ import {
14
14
  } from "./chunk-SG6XYLQE.mjs";
15
15
  import {
16
16
  SDK_VERSION
17
- } from "./chunk-4NRSH3VX.mjs";
17
+ } from "./chunk-Q4MVJ7NW.mjs";
18
18
  import {
19
19
  resolveMetadataValue
20
20
  } from "./chunk-BEPLYQBS.mjs";
@@ -3046,7 +3046,7 @@ var ProviderRegistry = class {
3046
3046
  if (this.vectorProviders[provider]) return this.vectorProviders[provider];
3047
3047
  switch (provider) {
3048
3048
  case "pinecone": {
3049
- const { PineconeProvider } = await import("./PineconeProvider-RDXBD3PE.mjs");
3049
+ const { PineconeProvider } = await import("./PineconeProvider-S7AV7FKC.mjs");
3050
3050
  return PineconeProvider;
3051
3051
  }
3052
3052
  case "pgvector":
@@ -6274,7 +6274,9 @@ var LRUDecisionCache = class {
6274
6274
  var _a2, _b;
6275
6275
  const q = (context.userQuery || "").toLowerCase().trim();
6276
6276
  const docCount = (_b = (_a2 = context.retrievedDocuments) == null ? void 0 : _a2.length) != null ? _b : 0;
6277
- return `${q}::docs:${docCount}`;
6277
+ const resp = context.llmResponse ? context.llmResponse.trim() : "";
6278
+ const respKey = resp ? `::resp:${resp.length}:${resp.slice(0, 32)}:${resp.slice(-32)}` : "";
6279
+ return `${q}::docs:${docCount}${respKey}`;
6278
6280
  }
6279
6281
  get(context) {
6280
6282
  const key = this.generateKey(context);
@@ -6315,6 +6317,13 @@ var VisualizationDecisionEngine = class {
6315
6317
  llmResponse,
6316
6318
  metadata
6317
6319
  };
6320
+ if (this.isNegativeResponse(llmResponse)) {
6321
+ return {
6322
+ renderType: "text",
6323
+ confidence: 0.99,
6324
+ reason: "Chat response indicates no relevant data or inability to answer; suppressing visualization."
6325
+ };
6326
+ }
6318
6327
  const cached = this.cache.get(context);
6319
6328
  if (cached) {
6320
6329
  return cached;
@@ -6352,6 +6361,16 @@ var VisualizationDecisionEngine = class {
6352
6361
  /**
6353
6362
  * Clear the decision cache.
6354
6363
  */
6364
+ /**
6365
+ * Returns true when the generated chat response indicates there is no relevant
6366
+ * data or the assistant could not answer the query.
6367
+ */
6368
+ static isNegativeResponse(response) {
6369
+ if (!response) return false;
6370
+ const text = response.trim().toLowerCase();
6371
+ if (!text) return false;
6372
+ return text.includes("cannot answer") || text.includes("can't answer") || text.includes("no relevant data") || text.includes("no data available") || text.includes("no matching data") || text.includes("don't have enough information") || text.includes("do not have enough information") || text.includes("couldn't find any") || text.includes("could not find any") || text.includes("unable to find");
6373
+ }
6355
6374
  static clearCache() {
6356
6375
  this.cache.clear();
6357
6376
  }
@@ -6381,11 +6400,20 @@ var UITransformer = class _UITransformer {
6381
6400
  * Uses the lightweight heuristic intent detector as a fallback.
6382
6401
  * Prefer `analyzeAndDecide()` in production.
6383
6402
  */
6384
- static transform(userQuery, retrievedData, config, trainedSchema, intent) {
6403
+ static transform(userQuery, retrievedData, config, trainedSchema, intent, chatResponse) {
6385
6404
  var _a2, _b, _c;
6386
6405
  if (!retrievedData || retrievedData.length === 0) {
6387
6406
  return this.createTextResponse("No data available", "No relevant data found for your query.");
6388
6407
  }
6408
+ if (VisualizationDecisionEngine.isNegativeResponse(chatResponse)) {
6409
+ return this.transformToText(retrievedData);
6410
+ }
6411
+ if (!intent && chatResponse) {
6412
+ const decision = VisualizationDecisionEngine.decideVisualization(userQuery, retrievedData, chatResponse);
6413
+ if (decision.renderType === "text") {
6414
+ return this.transformToText(retrievedData);
6415
+ }
6416
+ }
6389
6417
  const resolvedIntent = intent != null ? intent : this.detectIntentHeuristic(userQuery);
6390
6418
  const filteredData = resolvedIntent.filterInStockOnly ? retrievedData.filter((item) => this.determineStockStatus(item)) : retrievedData;
6391
6419
  const profile = this.profileData(filteredData);
@@ -6436,15 +6464,15 @@ var UITransformer = class _UITransformer {
6436
6464
  * Step 2 — Pass the intent + data to the visualization-selection prompt.
6437
6465
  * Step 3 — Fall back to the heuristic `transform()` if either LLM call fails.
6438
6466
  */
6439
- static async analyzeAndDecide(query, sources, llm) {
6440
- const decision = VisualizationDecisionEngine.decideVisualization(query, sources);
6467
+ static async analyzeAndDecide(query, sources, llm, chatResponse) {
6468
+ const decision = VisualizationDecisionEngine.decideVisualization(query, sources, chatResponse);
6441
6469
  if (decision.renderType === "text") {
6442
6470
  console.debug("[UITransformer] Decision Engine selected plain text rendering \u2014 skipping visual LLM calls.");
6443
6471
  return this.transformToText(sources);
6444
6472
  }
6445
6473
  let intent;
6446
6474
  try {
6447
- intent = await this.detectIntent(query, llm, sources);
6475
+ intent = await this.detectIntent(query, llm, sources, chatResponse);
6448
6476
  console.debug("[UITransformer] Detected intent:", intent);
6449
6477
  } catch (err) {
6450
6478
  console.warn("[UITransformer] Intent detection failed; using heuristic.", err);
@@ -6473,6 +6501,8 @@ RETRIEVED DATA SCHEMA:
6473
6501
  `;
6474
6502
  const userPrompt = [
6475
6503
  `USER QUESTION: ${query}`,
6504
+ ...chatResponse ? ["", `CHAT RESPONSE (RESULT):
6505
+ ${chatResponse.slice(0, 2e3)}`] : [],
6476
6506
  "",
6477
6507
  `DETECTED INTENT (JSON): ${JSON.stringify(intent)}`,
6478
6508
  "",
@@ -6501,17 +6531,17 @@ RETRIEVED DATA SCHEMA:
6501
6531
  }
6502
6532
  if (!isCompatible) {
6503
6533
  console.warn(`[UITransformer] LLM chose incompatible visualization type "${parsed.type}" for retrieved schema. Falling back to heuristic.`);
6504
- return this.transform(query, sources, void 0, void 0, intent);
6534
+ return this.transform(query, sources, void 0, void 0, intent, chatResponse);
6505
6535
  }
6506
6536
  const intentAllowsTable = intent.wantsExplicitTable || ["tabular", "table", "geographic"].includes(intent.visualizationHint);
6507
6537
  const intentWantsPieLikeChart = ["pie_chart", "donut_chart"].includes(intent.recommendedChart) || ["composition", "category_breakdown"].includes(intent.visualizationHint);
6508
6538
  if (parsed.type === "table" && !intentAllowsTable) {
6509
6539
  console.debug("[UITransformer] LLM chose table but intent says no. Falling back to text.");
6510
- return this.transform(query, sources, void 0, void 0, intent);
6540
+ return this.transform(query, sources, void 0, void 0, intent, chatResponse);
6511
6541
  }
6512
6542
  if (intentWantsPieLikeChart && parsed.type !== "pie_chart" && this.detectCategories(sources).length > 1) {
6513
6543
  console.debug("[UITransformer] LLM ignored pie/composition intent. Using deterministic pie chart.");
6514
- return this.transform(query, sources, void 0, void 0, intent);
6544
+ return this.transform(query, sources, void 0, void 0, intent, chatResponse);
6515
6545
  }
6516
6546
  console.debug("[UITransformer] LLM chose visualization type:", parsed.type);
6517
6547
  return parsed;
@@ -6520,7 +6550,7 @@ RETRIEVED DATA SCHEMA:
6520
6550
  } catch (err) {
6521
6551
  console.warn("[UITransformer] analyzeAndDecide LLM call failed; falling back to heuristic.", err);
6522
6552
  }
6523
- return this.transform(query, sources, void 0, void 0, intent);
6553
+ return this.transform(query, sources, void 0, void 0, intent, chatResponse);
6524
6554
  }
6525
6555
  // ─── Dynamic Intent Detection ─────────────────────────────────────────────
6526
6556
  /**
@@ -6532,7 +6562,7 @@ RETRIEVED DATA SCHEMA:
6532
6562
  * - The intent object can be reused across both the heuristic and LLM paths.
6533
6563
  * - It is easy to unit-test intent detection in isolation.
6534
6564
  */
6535
- static async detectIntent(query, llm, sources) {
6565
+ static async detectIntent(query, llm, sources, chatResponse) {
6536
6566
  let schemaProfileText = "";
6537
6567
  if (sources && sources.length > 0) {
6538
6568
  const profile = this.profileData(sources);
@@ -6587,7 +6617,10 @@ RULES:
6587
6617
  - isTemporal: true if time words appear (trend, historical, over time, last year, monthly, etc.)
6588
6618
  - isComparison: true if user compares, ranks, or contrasts entities or categories.
6589
6619
  - language: detect from the query text itself; default "en" if uncertain.`;
6590
- const userPrompt = `QUERY: ${query}${schemaProfileText ? `
6620
+ const userPrompt = `QUERY: ${query}${chatResponse ? `
6621
+
6622
+ CHAT RESPONSE (RESULT):
6623
+ ${chatResponse.slice(0, 1500)}` : ""}${schemaProfileText ? `
6591
6624
 
6592
6625
  ${schemaProfileText}` : ""}`;
6593
6626
  const rawResponse = await llm.chat(
@@ -7671,8 +7704,8 @@ ${schemaProfileText}` : ""}`;
7671
7704
  // ─── Visualization Prompt ─────────────────────────────────────────────────
7672
7705
  static buildVisualizationSystemPrompt() {
7673
7706
  return `You are a data visualization expert embedded in a RAG chat system.
7674
- You will receive a user question, a pre-computed INTENT object, and structured data from a vector database.
7675
- Your ONLY job is to return a single JSON object describing how to visualize the data.
7707
+ You will receive a user question, the generated chat response (result), a pre-computed INTENT object, and structured data from a vector database.
7708
+ Your ONLY job is to return a single JSON object describing how to visualize the result and data after the chat response (or "text" if no visualization is needed).
7676
7709
  The INTENT object is authoritative \u2014 honor it. For example:
7677
7710
  - If intent.wantsExplicitTable is false, never return type "table".
7678
7711
  - If intent.isTemporal is true, prefer line_chart.
@@ -8561,24 +8594,6 @@ ${context}`;
8561
8594
  SchemaMapper.train(this.llmProvider, ns, allMetadataKeys).catch(() => {
8562
8595
  });
8563
8596
  }
8564
- const isProductQ = UITransformer.isProductQueryPublic(effectiveQuestion);
8565
- const uiTransformationPromise = isProductQ ? Promise.resolve(
8566
- UITransformer.transform(
8567
- effectiveQuestion,
8568
- sources,
8569
- this.config,
8570
- cachedSchema,
8571
- { visualizationHint: "product_browse", recommendedChart: "text", filterInStockOnly: false, wantsExplicitTable: false, isTemporal: false, isComparison: false, language: "en" }
8572
- )
8573
- ) : this.generateUiTransformation(
8574
- effectiveQuestion,
8575
- sources,
8576
- cachedSchema,
8577
- hasNumericPredicates
8578
- ).catch((uiError) => {
8579
- Logger.warn("[Pipeline] UI transformation failed concurrently", { error: String(uiError) });
8580
- return UITransformer.transform(effectiveQuestion, sources, this.config, cachedSchema);
8581
- });
8582
8597
  let hallucinationScoringPromise = Promise.resolve(void 0);
8583
8598
  const restrictionSuffix = "\n\n(IMPORTANT: Format your response beautifully using rich Markdown! Use bolding for emphasis, headings (##) for structure, bullet points for lists, and proper line breaks between paragraphs to make it highly readable. However, NEVER generate Markdown tables, HTML figures, or text charts (the UI renders requested charts separately, so NEVER say you cannot create, display, draw, render, or provide a chart/visualization). If listing products, use simple markdown bullet points or comma-separated names. NEVER use plus signs (+) to separate product names, category names, or list items. For product description/detail questions, provide customer-facing prose only and do NOT list internal catalog fields such as Handle, Body (HTML), Vendor, Type, Tags, Published, Option, Variant, SKU, or raw metadata labels. You CAN use numbers for years/counts like 2006 or 5800, but NEVER put a dollar sign ($) before them.)";
8584
8599
  const isClaude37 = this.config.llm.model.includes("claude-3-7-sonnet");
@@ -8726,6 +8741,26 @@ ${context}`;
8726
8741
  estimatedCostUsd: estimateCostUsd(promptTokens, completionTokens, this.config.llm.model)
8727
8742
  };
8728
8743
  const awaitHallucination = ((_E = this.config.llm.options) == null ? void 0 : _E.awaitHallucination) === true;
8744
+ const isProductQ = UITransformer.isProductQueryPublic(effectiveQuestion);
8745
+ const uiTransformationPromise = isProductQ ? Promise.resolve(
8746
+ UITransformer.transform(
8747
+ effectiveQuestion,
8748
+ sources,
8749
+ this.config,
8750
+ cachedSchema,
8751
+ { visualizationHint: "product_browse", recommendedChart: "text", filterInStockOnly: false, wantsExplicitTable: false, isTemporal: false, isComparison: false, language: "en" },
8752
+ fullReply
8753
+ )
8754
+ ) : this.generateUiTransformation(
8755
+ effectiveQuestion,
8756
+ sources,
8757
+ fullReply,
8758
+ cachedSchema,
8759
+ hasNumericPredicates
8760
+ ).catch((uiError) => {
8761
+ Logger.warn("[Pipeline] UI transformation failed after generation", { error: String(uiError) });
8762
+ return UITransformer.transform(effectiveQuestion, sources, this.config, cachedSchema, void 0, fullReply);
8763
+ });
8729
8764
  const [uiTransformation, hallucinationResult] = yield new __await(Promise.all([
8730
8765
  uiTransformationPromise,
8731
8766
  awaitHallucination ? hallucinationScoringPromise : Promise.resolve(void 0)
@@ -8793,10 +8828,10 @@ ${context}`;
8793
8828
  * Universal retrieval method combining all enabled providers.
8794
8829
  * Uses an LRU-bounded embedding cache to avoid re-embedding the same query.
8795
8830
  */
8796
- async generateUiTransformation(question, sources, cachedSchema, forceDeterministic = false) {
8831
+ async generateUiTransformation(question, sources, chatResponse, cachedSchema, forceDeterministic = false) {
8797
8832
  var _a2;
8798
8833
  if (!sources || sources.length === 0) {
8799
- return UITransformer.transform(question, sources, this.config, cachedSchema);
8834
+ return UITransformer.transform(question, sources, this.config, cachedSchema, void 0, chatResponse);
8800
8835
  }
8801
8836
  if (UITransformer.isProductQueryPublic(question)) {
8802
8837
  return UITransformer.transform(
@@ -8804,18 +8839,19 @@ ${context}`;
8804
8839
  sources,
8805
8840
  this.config,
8806
8841
  cachedSchema,
8807
- { visualizationHint: "product_browse", recommendedChart: "text", filterInStockOnly: false, wantsExplicitTable: false, isTemporal: false, isComparison: false, language: "en" }
8842
+ { visualizationHint: "product_browse", recommendedChart: "text", filterInStockOnly: false, wantsExplicitTable: false, isTemporal: false, isComparison: false, language: "en" },
8843
+ chatResponse
8808
8844
  );
8809
8845
  }
8810
8846
  const enableLlmUiTransform = ((_a2 = this.config.llm.options) == null ? void 0 : _a2.enableLlmUiTransform) === true;
8811
8847
  if (forceDeterministic || !enableLlmUiTransform) {
8812
- return UITransformer.transform(question, sources, this.config, cachedSchema);
8848
+ return UITransformer.transform(question, sources, this.config, cachedSchema, void 0, chatResponse);
8813
8849
  }
8814
8850
  try {
8815
- return await UITransformer.analyzeAndDecide(question, sources, this.llmRouter.get("fast"));
8851
+ return await UITransformer.analyzeAndDecide(question, sources, this.llmRouter.get("fast"), chatResponse);
8816
8852
  } catch (err) {
8817
8853
  Logger.warn("[Pipeline] generateUiTransformation failed, using heuristic fallback", { error: String(err) });
8818
- return UITransformer.transform(question, sources, this.config, cachedSchema);
8854
+ return UITransformer.transform(question, sources, this.config, cachedSchema, void 0, chatResponse);
8819
8855
  }
8820
8856
  }
8821
8857
  applyStructuredFilters(sources, filter) {
@@ -9654,14 +9690,14 @@ function createStreamHandler(configOrPlugin, options) {
9654
9690
  let uiTransformation = responseChunk == null ? void 0 : responseChunk.ui_transformation;
9655
9691
  if (sources.length > 0) {
9656
9692
  try {
9657
- uiTransformation = (_a3 = responseChunk == null ? void 0 : responseChunk.ui_transformation) != null ? _a3 : UITransformer.transform(message, sources, plugin.getConfig());
9693
+ uiTransformation = (_a3 = responseChunk == null ? void 0 : responseChunk.ui_transformation) != null ? _a3 : UITransformer.transform(message, sources, plugin.getConfig(), void 0, void 0, fullReply);
9658
9694
  if (uiTransformation) {
9659
9695
  enqueue(sseUIFrame(uiTransformation));
9660
9696
  }
9661
9697
  } catch (transformError) {
9662
9698
  console.warn("[createStreamHandler] UI transformation warning:", transformError);
9663
9699
  try {
9664
- const fallback = UITransformer.transform(message, sources, plugin.getConfig());
9700
+ const fallback = UITransformer.transform(message, sources, plugin.getConfig(), void 0, void 0, fullReply);
9665
9701
  if (fallback) {
9666
9702
  uiTransformation = fallback;
9667
9703
  enqueue(sseUIFrame(fallback));
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  ConfigFetcher
3
- } from "./chunk-PLODIWBN.mjs";
3
+ } from "./chunk-557UWRCG.mjs";
4
4
  import {
5
5
  BaseVectorProvider
6
6
  } from "./chunk-IMP6FUCY.mjs";
@@ -1,7 +1,7 @@
1
1
  // package.json
2
2
  var package_default = {
3
3
  name: "@retrivora-ai/rag-engine",
4
- version: "2.6.5",
4
+ version: "2.6.6",
5
5
  description: "Retrivora AI is a plug-and-play AI engine for RAG chat experiences \u2014 generic vector DB + LLM provider, embeddable or standalone.",
6
6
  author: "Abhinav Alkuchi",
7
7
  license: "UNLICENSED",
@@ -1,3 +1,3 @@
1
- export { b as HandlerOptions, d as createChatHandler, e as createFeedbackHandler, f as createHealthHandler, g as createHistoryHandler, h as createIngestHandler, i as createLicenseHandler, j as createRagHandler, k as createSessionsHandler, l as createStreamHandler, m as createSuggestionsHandler, n as createUploadHandler, s as sseErrorFrame, o as sseFrame, p as sseMetaFrame, q as sseObservabilityFrame, r as sseTextFrame, t as sseUIFrame } from '../index-CYhWkWaX.mjs';
1
+ export { b as HandlerOptions, d as createChatHandler, e as createFeedbackHandler, f as createHealthHandler, g as createHistoryHandler, h as createIngestHandler, i as createLicenseHandler, j as createRagHandler, k as createSessionsHandler, l as createStreamHandler, m as createSuggestionsHandler, n as createUploadHandler, s as sseErrorFrame, o as sseFrame, p as sseMetaFrame, q as sseObservabilityFrame, r as sseTextFrame, t as sseUIFrame } from '../index-C2ONv-uf.mjs';
2
2
  import 'next/server';
3
- import '../ILLMProvider-Csukdic7.mjs';
3
+ import '../ILLMProvider-BQXlVWNn.mjs';
@@ -1,3 +1,3 @@
1
- export { b as HandlerOptions, d as createChatHandler, e as createFeedbackHandler, f as createHealthHandler, g as createHistoryHandler, h as createIngestHandler, i as createLicenseHandler, j as createRagHandler, k as createSessionsHandler, l as createStreamHandler, m as createSuggestionsHandler, n as createUploadHandler, s as sseErrorFrame, o as sseFrame, p as sseMetaFrame, q as sseObservabilityFrame, r as sseTextFrame, t as sseUIFrame } from '../index-CAN5ZTWN.js';
1
+ export { b as HandlerOptions, d as createChatHandler, e as createFeedbackHandler, f as createHealthHandler, g as createHistoryHandler, h as createIngestHandler, i as createLicenseHandler, j as createRagHandler, k as createSessionsHandler, l as createStreamHandler, m as createSuggestionsHandler, n as createUploadHandler, s as sseErrorFrame, o as sseFrame, p as sseMetaFrame, q as sseObservabilityFrame, r as sseTextFrame, t as sseUIFrame } from '../index-BVNd1Ii7.js';
2
2
  import 'next/server';
3
- import '../ILLMProvider-Csukdic7.js';
3
+ import '../ILLMProvider-BQXlVWNn.js';
@@ -322,7 +322,7 @@ var init_package = __esm({
322
322
  "package.json"() {
323
323
  package_default = {
324
324
  name: "@retrivora-ai/rag-engine",
325
- version: "2.6.5",
325
+ version: "2.6.6",
326
326
  description: "Retrivora AI is a plug-and-play AI engine for RAG chat experiences \u2014 generic vector DB + LLM provider, embeddable or standalone.",
327
327
  author: "Abhinav Alkuchi",
328
328
  license: "UNLICENSED",
@@ -9592,7 +9592,9 @@ var LRUDecisionCache = class {
9592
9592
  var _a2, _b;
9593
9593
  const q = (context.userQuery || "").toLowerCase().trim();
9594
9594
  const docCount = (_b = (_a2 = context.retrievedDocuments) == null ? void 0 : _a2.length) != null ? _b : 0;
9595
- return `${q}::docs:${docCount}`;
9595
+ const resp = context.llmResponse ? context.llmResponse.trim() : "";
9596
+ const respKey = resp ? `::resp:${resp.length}:${resp.slice(0, 32)}:${resp.slice(-32)}` : "";
9597
+ return `${q}::docs:${docCount}${respKey}`;
9596
9598
  }
9597
9599
  get(context) {
9598
9600
  const key = this.generateKey(context);
@@ -9633,6 +9635,13 @@ var VisualizationDecisionEngine = class {
9633
9635
  llmResponse,
9634
9636
  metadata
9635
9637
  };
9638
+ if (this.isNegativeResponse(llmResponse)) {
9639
+ return {
9640
+ renderType: "text",
9641
+ confidence: 0.99,
9642
+ reason: "Chat response indicates no relevant data or inability to answer; suppressing visualization."
9643
+ };
9644
+ }
9636
9645
  const cached = this.cache.get(context);
9637
9646
  if (cached) {
9638
9647
  return cached;
@@ -9670,6 +9679,16 @@ var VisualizationDecisionEngine = class {
9670
9679
  /**
9671
9680
  * Clear the decision cache.
9672
9681
  */
9682
+ /**
9683
+ * Returns true when the generated chat response indicates there is no relevant
9684
+ * data or the assistant could not answer the query.
9685
+ */
9686
+ static isNegativeResponse(response) {
9687
+ if (!response) return false;
9688
+ const text = response.trim().toLowerCase();
9689
+ if (!text) return false;
9690
+ return text.includes("cannot answer") || text.includes("can't answer") || text.includes("no relevant data") || text.includes("no data available") || text.includes("no matching data") || text.includes("don't have enough information") || text.includes("do not have enough information") || text.includes("couldn't find any") || text.includes("could not find any") || text.includes("unable to find");
9691
+ }
9673
9692
  static clearCache() {
9674
9693
  this.cache.clear();
9675
9694
  }
@@ -9691,11 +9710,20 @@ var UITransformer = class _UITransformer {
9691
9710
  * Uses the lightweight heuristic intent detector as a fallback.
9692
9711
  * Prefer `analyzeAndDecide()` in production.
9693
9712
  */
9694
- static transform(userQuery, retrievedData, config, trainedSchema, intent) {
9713
+ static transform(userQuery, retrievedData, config, trainedSchema, intent, chatResponse) {
9695
9714
  var _a2, _b, _c;
9696
9715
  if (!retrievedData || retrievedData.length === 0) {
9697
9716
  return this.createTextResponse("No data available", "No relevant data found for your query.");
9698
9717
  }
9718
+ if (VisualizationDecisionEngine.isNegativeResponse(chatResponse)) {
9719
+ return this.transformToText(retrievedData);
9720
+ }
9721
+ if (!intent && chatResponse) {
9722
+ const decision = VisualizationDecisionEngine.decideVisualization(userQuery, retrievedData, chatResponse);
9723
+ if (decision.renderType === "text") {
9724
+ return this.transformToText(retrievedData);
9725
+ }
9726
+ }
9699
9727
  const resolvedIntent = intent != null ? intent : this.detectIntentHeuristic(userQuery);
9700
9728
  const filteredData = resolvedIntent.filterInStockOnly ? retrievedData.filter((item) => this.determineStockStatus(item)) : retrievedData;
9701
9729
  const profile = this.profileData(filteredData);
@@ -9746,15 +9774,15 @@ var UITransformer = class _UITransformer {
9746
9774
  * Step 2 — Pass the intent + data to the visualization-selection prompt.
9747
9775
  * Step 3 — Fall back to the heuristic `transform()` if either LLM call fails.
9748
9776
  */
9749
- static async analyzeAndDecide(query, sources, llm) {
9750
- const decision = VisualizationDecisionEngine.decideVisualization(query, sources);
9777
+ static async analyzeAndDecide(query, sources, llm, chatResponse) {
9778
+ const decision = VisualizationDecisionEngine.decideVisualization(query, sources, chatResponse);
9751
9779
  if (decision.renderType === "text") {
9752
9780
  console.debug("[UITransformer] Decision Engine selected plain text rendering \u2014 skipping visual LLM calls.");
9753
9781
  return this.transformToText(sources);
9754
9782
  }
9755
9783
  let intent;
9756
9784
  try {
9757
- intent = await this.detectIntent(query, llm, sources);
9785
+ intent = await this.detectIntent(query, llm, sources, chatResponse);
9758
9786
  console.debug("[UITransformer] Detected intent:", intent);
9759
9787
  } catch (err) {
9760
9788
  console.warn("[UITransformer] Intent detection failed; using heuristic.", err);
@@ -9783,6 +9811,8 @@ RETRIEVED DATA SCHEMA:
9783
9811
  `;
9784
9812
  const userPrompt = [
9785
9813
  `USER QUESTION: ${query}`,
9814
+ ...chatResponse ? ["", `CHAT RESPONSE (RESULT):
9815
+ ${chatResponse.slice(0, 2e3)}`] : [],
9786
9816
  "",
9787
9817
  `DETECTED INTENT (JSON): ${JSON.stringify(intent)}`,
9788
9818
  "",
@@ -9811,17 +9841,17 @@ RETRIEVED DATA SCHEMA:
9811
9841
  }
9812
9842
  if (!isCompatible) {
9813
9843
  console.warn(`[UITransformer] LLM chose incompatible visualization type "${parsed.type}" for retrieved schema. Falling back to heuristic.`);
9814
- return this.transform(query, sources, void 0, void 0, intent);
9844
+ return this.transform(query, sources, void 0, void 0, intent, chatResponse);
9815
9845
  }
9816
9846
  const intentAllowsTable = intent.wantsExplicitTable || ["tabular", "table", "geographic"].includes(intent.visualizationHint);
9817
9847
  const intentWantsPieLikeChart = ["pie_chart", "donut_chart"].includes(intent.recommendedChart) || ["composition", "category_breakdown"].includes(intent.visualizationHint);
9818
9848
  if (parsed.type === "table" && !intentAllowsTable) {
9819
9849
  console.debug("[UITransformer] LLM chose table but intent says no. Falling back to text.");
9820
- return this.transform(query, sources, void 0, void 0, intent);
9850
+ return this.transform(query, sources, void 0, void 0, intent, chatResponse);
9821
9851
  }
9822
9852
  if (intentWantsPieLikeChart && parsed.type !== "pie_chart" && this.detectCategories(sources).length > 1) {
9823
9853
  console.debug("[UITransformer] LLM ignored pie/composition intent. Using deterministic pie chart.");
9824
- return this.transform(query, sources, void 0, void 0, intent);
9854
+ return this.transform(query, sources, void 0, void 0, intent, chatResponse);
9825
9855
  }
9826
9856
  console.debug("[UITransformer] LLM chose visualization type:", parsed.type);
9827
9857
  return parsed;
@@ -9830,7 +9860,7 @@ RETRIEVED DATA SCHEMA:
9830
9860
  } catch (err) {
9831
9861
  console.warn("[UITransformer] analyzeAndDecide LLM call failed; falling back to heuristic.", err);
9832
9862
  }
9833
- return this.transform(query, sources, void 0, void 0, intent);
9863
+ return this.transform(query, sources, void 0, void 0, intent, chatResponse);
9834
9864
  }
9835
9865
  // ─── Dynamic Intent Detection ─────────────────────────────────────────────
9836
9866
  /**
@@ -9842,7 +9872,7 @@ RETRIEVED DATA SCHEMA:
9842
9872
  * - The intent object can be reused across both the heuristic and LLM paths.
9843
9873
  * - It is easy to unit-test intent detection in isolation.
9844
9874
  */
9845
- static async detectIntent(query, llm, sources) {
9875
+ static async detectIntent(query, llm, sources, chatResponse) {
9846
9876
  let schemaProfileText = "";
9847
9877
  if (sources && sources.length > 0) {
9848
9878
  const profile = this.profileData(sources);
@@ -9897,7 +9927,10 @@ RULES:
9897
9927
  - isTemporal: true if time words appear (trend, historical, over time, last year, monthly, etc.)
9898
9928
  - isComparison: true if user compares, ranks, or contrasts entities or categories.
9899
9929
  - language: detect from the query text itself; default "en" if uncertain.`;
9900
- const userPrompt = `QUERY: ${query}${schemaProfileText ? `
9930
+ const userPrompt = `QUERY: ${query}${chatResponse ? `
9931
+
9932
+ CHAT RESPONSE (RESULT):
9933
+ ${chatResponse.slice(0, 1500)}` : ""}${schemaProfileText ? `
9901
9934
 
9902
9935
  ${schemaProfileText}` : ""}`;
9903
9936
  const rawResponse = await llm.chat(
@@ -10981,8 +11014,8 @@ ${schemaProfileText}` : ""}`;
10981
11014
  // ─── Visualization Prompt ─────────────────────────────────────────────────
10982
11015
  static buildVisualizationSystemPrompt() {
10983
11016
  return `You are a data visualization expert embedded in a RAG chat system.
10984
- You will receive a user question, a pre-computed INTENT object, and structured data from a vector database.
10985
- Your ONLY job is to return a single JSON object describing how to visualize the data.
11017
+ You will receive a user question, the generated chat response (result), a pre-computed INTENT object, and structured data from a vector database.
11018
+ Your ONLY job is to return a single JSON object describing how to visualize the result and data after the chat response (or "text" if no visualization is needed).
10986
11019
  The INTENT object is authoritative \u2014 honor it. For example:
10987
11020
  - If intent.wantsExplicitTable is false, never return type "table".
10988
11021
  - If intent.isTemporal is true, prefer line_chart.
@@ -11871,24 +11904,6 @@ ${context}`;
11871
11904
  SchemaMapper.train(this.llmProvider, ns, allMetadataKeys).catch(() => {
11872
11905
  });
11873
11906
  }
11874
- const isProductQ = UITransformer.isProductQueryPublic(effectiveQuestion);
11875
- const uiTransformationPromise = isProductQ ? Promise.resolve(
11876
- UITransformer.transform(
11877
- effectiveQuestion,
11878
- sources,
11879
- this.config,
11880
- cachedSchema,
11881
- { visualizationHint: "product_browse", recommendedChart: "text", filterInStockOnly: false, wantsExplicitTable: false, isTemporal: false, isComparison: false, language: "en" }
11882
- )
11883
- ) : this.generateUiTransformation(
11884
- effectiveQuestion,
11885
- sources,
11886
- cachedSchema,
11887
- hasNumericPredicates
11888
- ).catch((uiError) => {
11889
- Logger.warn("[Pipeline] UI transformation failed concurrently", { error: String(uiError) });
11890
- return UITransformer.transform(effectiveQuestion, sources, this.config, cachedSchema);
11891
- });
11892
11907
  let hallucinationScoringPromise = Promise.resolve(void 0);
11893
11908
  const restrictionSuffix = "\n\n(IMPORTANT: Format your response beautifully using rich Markdown! Use bolding for emphasis, headings (##) for structure, bullet points for lists, and proper line breaks between paragraphs to make it highly readable. However, NEVER generate Markdown tables, HTML figures, or text charts (the UI renders requested charts separately, so NEVER say you cannot create, display, draw, render, or provide a chart/visualization). If listing products, use simple markdown bullet points or comma-separated names. NEVER use plus signs (+) to separate product names, category names, or list items. For product description/detail questions, provide customer-facing prose only and do NOT list internal catalog fields such as Handle, Body (HTML), Vendor, Type, Tags, Published, Option, Variant, SKU, or raw metadata labels. You CAN use numbers for years/counts like 2006 or 5800, but NEVER put a dollar sign ($) before them.)";
11894
11909
  const isClaude37 = this.config.llm.model.includes("claude-3-7-sonnet");
@@ -12036,6 +12051,26 @@ ${context}`;
12036
12051
  estimatedCostUsd: estimateCostUsd(promptTokens, completionTokens, this.config.llm.model)
12037
12052
  };
12038
12053
  const awaitHallucination = ((_E = this.config.llm.options) == null ? void 0 : _E.awaitHallucination) === true;
12054
+ const isProductQ = UITransformer.isProductQueryPublic(effectiveQuestion);
12055
+ const uiTransformationPromise = isProductQ ? Promise.resolve(
12056
+ UITransformer.transform(
12057
+ effectiveQuestion,
12058
+ sources,
12059
+ this.config,
12060
+ cachedSchema,
12061
+ { visualizationHint: "product_browse", recommendedChart: "text", filterInStockOnly: false, wantsExplicitTable: false, isTemporal: false, isComparison: false, language: "en" },
12062
+ fullReply
12063
+ )
12064
+ ) : this.generateUiTransformation(
12065
+ effectiveQuestion,
12066
+ sources,
12067
+ fullReply,
12068
+ cachedSchema,
12069
+ hasNumericPredicates
12070
+ ).catch((uiError) => {
12071
+ Logger.warn("[Pipeline] UI transformation failed after generation", { error: String(uiError) });
12072
+ return UITransformer.transform(effectiveQuestion, sources, this.config, cachedSchema, void 0, fullReply);
12073
+ });
12039
12074
  const [uiTransformation, hallucinationResult] = yield new __await(Promise.all([
12040
12075
  uiTransformationPromise,
12041
12076
  awaitHallucination ? hallucinationScoringPromise : Promise.resolve(void 0)
@@ -12103,10 +12138,10 @@ ${context}`;
12103
12138
  * Universal retrieval method combining all enabled providers.
12104
12139
  * Uses an LRU-bounded embedding cache to avoid re-embedding the same query.
12105
12140
  */
12106
- async generateUiTransformation(question, sources, cachedSchema, forceDeterministic = false) {
12141
+ async generateUiTransformation(question, sources, chatResponse, cachedSchema, forceDeterministic = false) {
12107
12142
  var _a2;
12108
12143
  if (!sources || sources.length === 0) {
12109
- return UITransformer.transform(question, sources, this.config, cachedSchema);
12144
+ return UITransformer.transform(question, sources, this.config, cachedSchema, void 0, chatResponse);
12110
12145
  }
12111
12146
  if (UITransformer.isProductQueryPublic(question)) {
12112
12147
  return UITransformer.transform(
@@ -12114,18 +12149,19 @@ ${context}`;
12114
12149
  sources,
12115
12150
  this.config,
12116
12151
  cachedSchema,
12117
- { visualizationHint: "product_browse", recommendedChart: "text", filterInStockOnly: false, wantsExplicitTable: false, isTemporal: false, isComparison: false, language: "en" }
12152
+ { visualizationHint: "product_browse", recommendedChart: "text", filterInStockOnly: false, wantsExplicitTable: false, isTemporal: false, isComparison: false, language: "en" },
12153
+ chatResponse
12118
12154
  );
12119
12155
  }
12120
12156
  const enableLlmUiTransform = ((_a2 = this.config.llm.options) == null ? void 0 : _a2.enableLlmUiTransform) === true;
12121
12157
  if (forceDeterministic || !enableLlmUiTransform) {
12122
- return UITransformer.transform(question, sources, this.config, cachedSchema);
12158
+ return UITransformer.transform(question, sources, this.config, cachedSchema, void 0, chatResponse);
12123
12159
  }
12124
12160
  try {
12125
- return await UITransformer.analyzeAndDecide(question, sources, this.llmRouter.get("fast"));
12161
+ return await UITransformer.analyzeAndDecide(question, sources, this.llmRouter.get("fast"), chatResponse);
12126
12162
  } catch (err) {
12127
12163
  Logger.warn("[Pipeline] generateUiTransformation failed, using heuristic fallback", { error: String(err) });
12128
- return UITransformer.transform(question, sources, this.config, cachedSchema);
12164
+ return UITransformer.transform(question, sources, this.config, cachedSchema, void 0, chatResponse);
12129
12165
  }
12130
12166
  }
12131
12167
  applyStructuredFilters(sources, filter) {
@@ -12965,14 +13001,14 @@ function createStreamHandler(configOrPlugin, options) {
12965
13001
  let uiTransformation = responseChunk == null ? void 0 : responseChunk.ui_transformation;
12966
13002
  if (sources.length > 0) {
12967
13003
  try {
12968
- uiTransformation = (_a3 = responseChunk == null ? void 0 : responseChunk.ui_transformation) != null ? _a3 : UITransformer.transform(message, sources, plugin.getConfig());
13004
+ uiTransformation = (_a3 = responseChunk == null ? void 0 : responseChunk.ui_transformation) != null ? _a3 : UITransformer.transform(message, sources, plugin.getConfig(), void 0, void 0, fullReply);
12969
13005
  if (uiTransformation) {
12970
13006
  enqueue(sseUIFrame(uiTransformation));
12971
13007
  }
12972
13008
  } catch (transformError) {
12973
13009
  console.warn("[createStreamHandler] UI transformation warning:", transformError);
12974
13010
  try {
12975
- const fallback = UITransformer.transform(message, sources, plugin.getConfig());
13011
+ const fallback = UITransformer.transform(message, sources, plugin.getConfig(), void 0, void 0, fullReply);
12976
13012
  if (fallback) {
12977
13013
  uiTransformation = fallback;
12978
13014
  enqueue(sseUIFrame(fallback));