@retrivora-ai/rag-engine 2.4.3 → 2.4.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.
Files changed (142) hide show
  1. package/README.md +22 -0
  2. package/dist/{BatchProcessor-zbQMaZHm.d.mts → BatchProcessor-BGslIR5l.d.mts} +65 -4
  3. package/dist/{BatchProcessor-D42E706a.d.ts → BatchProcessor-C3gY6kZ0.d.ts} +65 -4
  4. package/dist/handlers/index.js +668 -837
  5. package/dist/handlers/index.mjs +678 -837
  6. package/dist/index.css +5 -0
  7. package/dist/index.d.mts +4 -28
  8. package/dist/index.d.ts +4 -28
  9. package/dist/index.js +91 -43
  10. package/dist/index.min.css +1 -1
  11. package/dist/index.mjs +91 -43
  12. package/dist/server.d.mts +21 -7
  13. package/dist/server.d.ts +21 -7
  14. package/dist/server.js +731 -875
  15. package/dist/server.mjs +741 -875
  16. package/package.json +11 -4
  17. package/src/LicensedRetrivora.ts +0 -278
  18. package/src/app/api/chat/route.ts +0 -4
  19. package/src/app/api/health/route.ts +0 -5
  20. package/src/app/api/ingest/route.ts +0 -27
  21. package/src/app/api/suggestions/route.ts +0 -5
  22. package/src/app/api/upload/route.ts +0 -5
  23. package/src/app/constants.tsx +0 -270
  24. package/src/app/favicon.ico +0 -0
  25. package/src/app/globals.css +0 -449
  26. package/src/app/layout.tsx +0 -22
  27. package/src/app/page.tsx +0 -64
  28. package/src/app/types.ts +0 -30
  29. package/src/browser.tsx +0 -127
  30. package/src/components/ChatWidget.tsx +0 -393
  31. package/src/components/ChatWindow.tsx +0 -952
  32. package/src/components/CodeViewer.tsx +0 -56
  33. package/src/components/ConfigProvider.tsx +0 -141
  34. package/src/components/DocumentUpload.tsx +0 -354
  35. package/src/components/DynamicChart.tsx +0 -297
  36. package/src/components/HourglassLoader.tsx +0 -87
  37. package/src/components/MarkdownComponents.tsx +0 -186
  38. package/src/components/MessageBubble.tsx +0 -498
  39. package/src/components/ObservabilityPanel.tsx +0 -374
  40. package/src/components/ProductCard.tsx +0 -107
  41. package/src/components/ProductCarousel.tsx +0 -97
  42. package/src/components/SourceCard.tsx +0 -61
  43. package/src/components/ThemeProvider.tsx +0 -8
  44. package/src/components/ThemeToggle.tsx +0 -29
  45. package/src/components/ThinkingBlock.tsx +0 -75
  46. package/src/components/UIDispatcher.tsx +0 -342
  47. package/src/components/VisualizationRenderer.tsx +0 -621
  48. package/src/components/constants.tsx +0 -503
  49. package/src/config/ConfigBuilder.ts +0 -285
  50. package/src/config/EmbeddingStrategy.ts +0 -148
  51. package/src/config/LicenseConfigResolver.ts +0 -298
  52. package/src/config/RagConfig.ts +0 -328
  53. package/src/config/UniversalProfiles.ts +0 -82
  54. package/src/config/constants.ts +0 -64
  55. package/src/config/serverConfig.ts +0 -378
  56. package/src/config/uiConstants.ts +0 -47
  57. package/src/core/BatchProcessor.ts +0 -385
  58. package/src/core/CircuitBreaker.ts +0 -118
  59. package/src/core/ConfigFetcher.ts +0 -220
  60. package/src/core/ConfigResolver.ts +0 -165
  61. package/src/core/ConfigValidator.ts +0 -197
  62. package/src/core/DatabaseStorage.ts +0 -526
  63. package/src/core/FreeTierLimitsGuard.ts +0 -281
  64. package/src/core/LLMRouter.ts +0 -79
  65. package/src/core/LangChainAgent.ts +0 -132
  66. package/src/core/LicenseValidator.ts +0 -284
  67. package/src/core/LicenseVerifier.ts +0 -261
  68. package/src/core/MultiAgentCoordinator.ts +0 -239
  69. package/src/core/Pipeline.ts +0 -1279
  70. package/src/core/PluginManager.ts +0 -277
  71. package/src/core/ProjectSettings.ts +0 -247
  72. package/src/core/ProviderHealthCheck.ts +0 -133
  73. package/src/core/ProviderInterfaces.ts +0 -37
  74. package/src/core/ProviderRegistry.ts +0 -156
  75. package/src/core/QueryProcessor.ts +0 -399
  76. package/src/core/Retrivora.ts +0 -98
  77. package/src/core/VectorPlugin.ts +0 -193
  78. package/src/core/mcp.ts +0 -261
  79. package/src/exceptions/index.ts +0 -131
  80. package/src/handlers/index.ts +0 -1625
  81. package/src/hooks/useHydrated.ts +0 -13
  82. package/src/hooks/useProjectSettings.ts +0 -105
  83. package/src/hooks/useRagChat.ts +0 -436
  84. package/src/hooks/useStoredMessages.ts +0 -64
  85. package/src/index.css +0 -4269
  86. package/src/index.ts +0 -137
  87. package/src/lib/plugin.ts +0 -24
  88. package/src/llm/ILLMProvider.ts +0 -60
  89. package/src/llm/LLMFactory.ts +0 -141
  90. package/src/llm/providers/AnthropicProvider.ts +0 -215
  91. package/src/llm/providers/GeminiProvider.ts +0 -311
  92. package/src/llm/providers/GroqProvider.ts +0 -184
  93. package/src/llm/providers/OllamaProvider.ts +0 -232
  94. package/src/llm/providers/OpenAIProvider.ts +0 -188
  95. package/src/llm/providers/QwenProvider.ts +0 -199
  96. package/src/llm/providers/UniversalLLMAdapter.ts +0 -422
  97. package/src/llm/utils.ts +0 -46
  98. package/src/providers/graphdb/BaseGraphProvider.ts +0 -43
  99. package/src/providers/graphdb/SimpleGraphProvider.ts +0 -66
  100. package/src/providers/vectordb/BaseVectorProvider.ts +0 -98
  101. package/src/providers/vectordb/ChromaDBProvider.ts +0 -130
  102. package/src/providers/vectordb/MilvusProvider.ts +0 -134
  103. package/src/providers/vectordb/MongoDBProvider.ts +0 -250
  104. package/src/providers/vectordb/MultiTablePostgresProvider.ts +0 -461
  105. package/src/providers/vectordb/PineconeProvider.ts +0 -295
  106. package/src/providers/vectordb/PostgreSQLProvider.ts +0 -227
  107. package/src/providers/vectordb/QdrantProvider.ts +0 -294
  108. package/src/providers/vectordb/RedisProvider.ts +0 -105
  109. package/src/providers/vectordb/UniversalVectorProvider.ts +0 -221
  110. package/src/providers/vectordb/WeaviateProvider.ts +0 -172
  111. package/src/rag/DocumentChunker.ts +0 -144
  112. package/src/rag/EntityExtractor.ts +0 -90
  113. package/src/rag/LlamaIndexIngestor.ts +0 -60
  114. package/src/rag/Reranker.ts +0 -106
  115. package/src/rendering/IntentClassifier.ts +0 -167
  116. package/src/rendering/RendererRegistry.ts +0 -164
  117. package/src/rendering/RuleEngine.ts +0 -67
  118. package/src/rendering/VisualizationDecisionEngine.ts +0 -164
  119. package/src/rendering/__tests__/VisualizationDecisionEngine.test.ts +0 -237
  120. package/src/rendering/index.ts +0 -12
  121. package/src/rendering/rules/Rule1SpecificInfoRule.ts +0 -28
  122. package/src/rendering/rules/Rule2ComparisonRule.ts +0 -24
  123. package/src/rendering/rules/Rule3ProductDiscoveryRule.ts +0 -28
  124. package/src/rendering/rules/Rule4AnalyticalRule.ts +0 -38
  125. package/src/rendering/rules/Rule5MixedResponseRule.ts +0 -29
  126. package/src/rendering/rules/Rule6SmallResultSetRule.ts +0 -24
  127. package/src/rendering/rules/Rule7LargeTableRule.ts +0 -35
  128. package/src/rendering/types.ts +0 -96
  129. package/src/server.ts +0 -156
  130. package/src/tailwind.css +0 -194
  131. package/src/telemetry/TelemetryService.ts +0 -330
  132. package/src/types/chat.ts +0 -89
  133. package/src/types/index.ts +0 -278
  134. package/src/types/pdf-parse.d.ts +0 -13
  135. package/src/types/props.ts +0 -169
  136. package/src/utils/DocumentParser.ts +0 -111
  137. package/src/utils/ProductExtractor.ts +0 -350
  138. package/src/utils/SchemaMapper.ts +0 -176
  139. package/src/utils/UITransformer.ts +0 -1812
  140. package/src/utils/synonyms.ts +0 -80
  141. package/src/utils/templateUtils.ts +0 -78
  142. package/src/version.ts +0 -6
@@ -1,5 +1,6 @@
1
1
  var __defProp = Object.defineProperty;
2
2
  var __defProps = Object.defineProperties;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
4
  var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
6
  var __getOwnPropSymbols = Object.getOwnPropertySymbols;
@@ -44,6 +45,15 @@ var __export = (target, all) => {
44
45
  for (var name in all)
45
46
  __defProp(target, name, { get: all[name], enumerable: true });
46
47
  };
48
+ var __copyProps = (to, from, except, desc) => {
49
+ if (from && typeof from === "object" || typeof from === "function") {
50
+ for (let key of __getOwnPropNames(from))
51
+ if (!__hasOwnProp.call(to, key) && key !== except)
52
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
53
+ }
54
+ return to;
55
+ };
56
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
47
57
  var __await = function(promise, isYieldStar) {
48
58
  this[0] = promise;
49
59
  this[1] = isYieldStar;
@@ -302,7 +312,7 @@ var init_package = __esm({
302
312
  "package.json"() {
303
313
  package_default = {
304
314
  name: "@retrivora-ai/rag-engine",
305
- version: "2.4.3",
315
+ version: "2.4.6",
306
316
  description: "Retrivora AI is a plug-and-play AI engine for RAG chat experiences \u2014 generic vector DB + LLM provider, embeddable or standalone.",
307
317
  author: "Abhinav Alkuchi",
308
318
  license: "UNLICENSED",
@@ -374,8 +384,6 @@ var init_package = __esm({
374
384
  },
375
385
  files: [
376
386
  "dist",
377
- "src",
378
- ".env.example",
379
387
  "README.md",
380
388
  "FREE_TIER_ARCHITECTURE.md"
381
389
  ],
@@ -385,14 +393,19 @@ var init_package = __esm({
385
393
  "build:pkg": "npx @tailwindcss/cli -i src/tailwind.css -o src/index.css && tsup src/index.ts src/handlers/index.ts src/server.ts --format cjs,esm --dts --clean --no-splitting --tsconfig tsconfig.build.json --external react,react-dom,next,mongodb,pg,openai,@anthropic-ai/sdk,@pinecone-database/pinecone,axios,lucide-react,mammoth,pdf-parse,xlsx,react-markdown,remark-gfm,next-themes,langchain,@langchain/core,@langchain/openai,llamaindex --inject-style && npx @tailwindcss/cli -i src/tailwind.css -o dist/index.css && npx @tailwindcss/cli -i src/tailwind.css -o dist/index.min.css --minify",
386
394
  lint: "eslint",
387
395
  typecheck: "tsc --noEmit -p tsconfig.build.json",
396
+ test: "jest",
388
397
  clean: "rm -rf dist"
389
398
  },
390
399
  peerDependencies: {
400
+ axios: ">=1.0.0",
391
401
  next: ">=15.0.0",
392
402
  react: ">=18.0.0",
393
403
  "react-dom": ">=18.0.0"
394
404
  },
395
405
  peerDependenciesMeta: {
406
+ axios: {
407
+ optional: true
408
+ },
396
409
  next: {
397
410
  optional: true
398
411
  }
@@ -401,9 +414,9 @@ var init_package = __esm({
401
414
  "@anthropic-ai/sdk": "^0.95.1",
402
415
  "@google/genai": "^0.8.0",
403
416
  "@google/generative-ai": "^0.24.1",
417
+ "@retrivora-ai/billing-guards": "*",
404
418
  "@pinecone-database/pinecone": "^7.2.0",
405
419
  "@types/papaparse": "^5.5.2",
406
- axios: "^1.15.0",
407
420
  "lucide-react": "^1.8.0",
408
421
  "next-themes": "^0.4.6",
409
422
  openai: "^6.34.0",
@@ -428,16 +441,20 @@ var init_package = __esm({
428
441
  "@tailwindcss/cli": "^4",
429
442
  "@tailwindcss/postcss": "^4",
430
443
  "@types/estree": "^1.0.9",
444
+ "@types/jest": "^30.0.0",
431
445
  "@types/node": "^20",
432
446
  "@types/pdf-parse": "^1.1.5",
433
447
  "@types/pg": "^8.20.0",
434
448
  "@types/react": "^19.2.14",
435
449
  "@types/react-dom": "^19.2.3",
450
+ axios: "^1.15.0",
436
451
  dotenv: "^17.4.2",
437
452
  eslint: "^9",
438
453
  "eslint-config-next": "16.2.4",
454
+ jest: "^30.4.2",
439
455
  next: "16.2.10",
440
456
  tailwindcss: "^4",
457
+ "ts-jest": "^29.4.12",
441
458
  tsup: "^8.5.1",
442
459
  typescript: "^5"
443
460
  }
@@ -480,7 +497,7 @@ var init_ConfigFetcher = __esm({
480
497
  * PINECONE_API_KEY if available, or vector operations are handled server-side.
481
498
  */
482
499
  static async fetchRemoteConfig(projectId, licenseKey) {
483
- var _a3, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k;
500
+ var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k;
484
501
  const cacheKey = `${projectId}:${licenseKey || ""}`;
485
502
  const now = Date.now();
486
503
  const cached = this.cache.get(cacheKey);
@@ -525,7 +542,7 @@ var init_ConfigFetcher = __esm({
525
542
  indexName: data.vectorDb.indexName || "retrivora-free",
526
543
  provider: data.vectorDb.provider || "pinecone",
527
544
  projectId: fetchedProjectId,
528
- namespace: ((_a3 = data.workspace) == null ? void 0 : _a3.namespace) || data.vectorDb.namespace || fetchedProjectId
545
+ namespace: ((_a2 = data.workspace) == null ? void 0 : _a2.namespace) || data.vectorDb.namespace || fetchedProjectId
529
546
  },
530
547
  embedding: {
531
548
  provider: ((_b = data.embedding) == null ? void 0 : _b.provider) || "universal_rest",
@@ -582,16 +599,20 @@ var init_ConfigFetcher = __esm({
582
599
  return config;
583
600
  }
584
601
  }
585
- } catch (e) {
602
+ } catch (err) {
603
+ console.warn(
604
+ `[ConfigFetcher] Failed to fetch config from ${baseUrl}:`,
605
+ err instanceof Error ? err.message : String(err)
606
+ );
586
607
  }
587
608
  }
588
609
  return null;
589
610
  }
590
611
  /** Convenience: fetch only vector DB config (backwards compat). */
591
612
  static async fetchRemoteVectorConfig(projectId, licenseKey) {
592
- var _a3;
613
+ var _a2;
593
614
  const config = await this.fetchRemoteConfig(projectId, licenseKey);
594
- return (_a3 = config == null ? void 0 : config.vectorDb) != null ? _a3 : null;
615
+ return (_a2 = config == null ? void 0 : config.vectorDb) != null ? _a2 : null;
595
616
  }
596
617
  };
597
618
  ConfigFetcher.cache = /* @__PURE__ */ new Map();
@@ -631,7 +652,7 @@ var init_PineconeProvider = __esm({
631
652
  const errors = [];
632
653
  const opts = config.options || {};
633
654
  const hasLicenseKey = Boolean(
634
- config.licenseKey || process.env.RETRIVORA_LICENSE_KEY || process.env.NEXT_PUBLIC_RETRIVORA_LICENSE_KEY
655
+ config.licenseKey || process.env.RETRIVORA_LICENSE_KEY
635
656
  );
636
657
  if (!opts.apiKey && !hasLicenseKey) {
637
658
  errors.push({
@@ -648,7 +669,7 @@ var init_PineconeProvider = __esm({
648
669
  static getHealthChecker() {
649
670
  return {
650
671
  async check(config) {
651
- var _a3, _b;
672
+ var _a2, _b;
652
673
  const opts = config.options || {};
653
674
  const indexName = config.indexName;
654
675
  const timestamp = Date.now();
@@ -656,7 +677,7 @@ var init_PineconeProvider = __esm({
656
677
  const { Pinecone: Pinecone2 } = await import("@pinecone-database/pinecone");
657
678
  const client = new Pinecone2({ apiKey: opts.apiKey });
658
679
  const indexes = await client.listIndexes();
659
- const indexNames = (_b = (_a3 = indexes.indexes) == null ? void 0 : _a3.map((i) => i.name)) != null ? _b : [];
680
+ const indexNames = (_b = (_a2 = indexes.indexes) == null ? void 0 : _a2.map((i) => i.name)) != null ? _b : [];
660
681
  if (!indexNames.includes(indexName)) {
661
682
  return {
662
683
  healthy: false,
@@ -683,25 +704,13 @@ var init_PineconeProvider = __esm({
683
704
  };
684
705
  }
685
706
  async initialize() {
686
- var _a3, _b;
707
+ var _a2, _b;
687
708
  if (this.client) return;
688
709
  let key = this.apiKey || process.env.PINECONE_API_KEY || "";
689
710
  if (!key) {
690
- const licenseKey = process.env.RETRIVORA_LICENSE_KEY || process.env.NEXT_PUBLIC_RETRIVORA_LICENSE_KEY;
711
+ const licenseKey = process.env.RETRIVORA_LICENSE_KEY;
691
712
  this.licenseKey = licenseKey;
692
- const jwtProjectId = (() => {
693
- if (!licenseKey) return void 0;
694
- try {
695
- const parts = licenseKey.split(".");
696
- if (parts.length >= 2) {
697
- const payload = JSON.parse(Buffer.from(parts[1], "base64url").toString("utf8"));
698
- return payload == null ? void 0 : payload.projectId;
699
- }
700
- } catch (e) {
701
- }
702
- return void 0;
703
- })();
704
- const projectId = process.env.RAG_PROJECT_ID || process.env.NEXT_PUBLIC_RAG_PROJECT_ID || jwtProjectId || this.projectId || "default";
713
+ const projectId = process.env.RAG_PROJECT_ID || process.env.NEXT_PUBLIC_RAG_PROJECT_ID || this.projectId || "default";
705
714
  this.projectId = projectId;
706
715
  const remoteConfig = await ConfigFetcher.fetchRemoteVectorConfig(projectId, licenseKey);
707
716
  if (remoteConfig) {
@@ -729,7 +738,7 @@ var init_PineconeProvider = __esm({
729
738
  this.client = new Pinecone({ apiKey: key });
730
739
  try {
731
740
  const indexes = await this.client.listIndexes();
732
- const names = (_b = (_a3 = indexes.indexes) == null ? void 0 : _a3.map((i) => i.name)) != null ? _b : [];
741
+ const names = (_b = (_a2 = indexes.indexes) == null ? void 0 : _a2.map((i) => i.name)) != null ? _b : [];
733
742
  if (!names.includes(this.indexName)) {
734
743
  console.warn(`[PineconeProvider] Target index "${this.indexName}" not listed. Proceeding with configured index name.`);
735
744
  }
@@ -748,7 +757,7 @@ var init_PineconeProvider = __esm({
748
757
  return namespace ? idx.namespace(namespace) : idx.namespace("");
749
758
  }
750
759
  async upsert(doc, namespace) {
751
- var _a3, _b;
760
+ var _a2, _b;
752
761
  if (!this.client) {
753
762
  if (!this.licenseKey) throw new Error("[PineconeProvider] Cannot execute vector operation: Missing API key and License key.");
754
763
  const res = await fetch(`${this.getControlPlaneUrl()}/api/v1/vectors/upsert`, {
@@ -758,7 +767,7 @@ var init_PineconeProvider = __esm({
758
767
  projectId: this.projectId,
759
768
  indexName: this.indexName,
760
769
  namespace: namespace || this.projectId,
761
- records: [{ id: String(doc.id), values: doc.vector, metadata: __spreadValues({ content: doc.content }, (_a3 = doc.metadata) != null ? _a3 : {}) }]
770
+ records: [{ id: String(doc.id), values: doc.vector, metadata: __spreadValues({ content: doc.content }, (_a2 = doc.metadata) != null ? _a2 : {}) }]
762
771
  })
763
772
  });
764
773
  if (!res.ok) throw new Error(`[PineconeProvider] Proxy upsert failed: ${await res.text()}`);
@@ -779,11 +788,11 @@ var init_PineconeProvider = __esm({
779
788
  if (!this.licenseKey) throw new Error("[PineconeProvider] Cannot execute vector operation: Missing API key and License key.");
780
789
  for (let i = 0; i < docs.length; i += BATCH) {
781
790
  const records = docs.slice(i, i + BATCH).map((d) => {
782
- var _a3;
791
+ var _a2;
783
792
  return {
784
793
  id: String(d.id),
785
794
  values: d.vector,
786
- metadata: __spreadValues({ content: d.content }, (_a3 = d.metadata) != null ? _a3 : {})
795
+ metadata: __spreadValues({ content: d.content }, (_a2 = d.metadata) != null ? _a2 : {})
787
796
  };
788
797
  });
789
798
  const res = await fetch(`${this.getControlPlaneUrl()}/api/v1/vectors/upsert`, {
@@ -803,18 +812,18 @@ var init_PineconeProvider = __esm({
803
812
  const targetIdx = await this.getActiveIndex(namespace);
804
813
  for (let i = 0; i < docs.length; i += BATCH) {
805
814
  const records = docs.slice(i, i + BATCH).map((d) => {
806
- var _a3;
815
+ var _a2;
807
816
  return {
808
817
  id: String(d.id),
809
818
  values: d.vector,
810
- metadata: __spreadValues({ content: d.content }, (_a3 = d.metadata) != null ? _a3 : {})
819
+ metadata: __spreadValues({ content: d.content }, (_a2 = d.metadata) != null ? _a2 : {})
811
820
  };
812
821
  });
813
822
  await targetIdx.upsert({ records });
814
823
  }
815
824
  }
816
825
  async query(vector, topK, namespace, filter) {
817
- var _a3, _b;
826
+ var _a2, _b;
818
827
  const pineconeFilter = this.sanitizeFilter(filter);
819
828
  if (!this.client) {
820
829
  if (!this.licenseKey) throw new Error("[PineconeProvider] Cannot execute vector operation: Missing API key and License key.");
@@ -832,11 +841,11 @@ var init_PineconeProvider = __esm({
832
841
  });
833
842
  if (!res.ok) throw new Error(`[PineconeProvider] Proxy query failed: ${await res.text()}`);
834
843
  const data = await res.json();
835
- return ((_a3 = data.matches) != null ? _a3 : []).map((m) => {
836
- var _a4, _b2;
844
+ return ((_a2 = data.matches) != null ? _a2 : []).map((m) => {
845
+ var _a3, _b2;
837
846
  return {
838
847
  id: m.id,
839
- score: (_a4 = m.score) != null ? _a4 : 0,
848
+ score: (_a3 = m.score) != null ? _a3 : 0,
840
849
  content: ((_b2 = m.metadata) == null ? void 0 : _b2.content) || "",
841
850
  metadata: m.metadata
842
851
  };
@@ -849,10 +858,10 @@ var init_PineconeProvider = __esm({
849
858
  includeMetadata: true
850
859
  }, Object.keys(pineconeFilter).length > 0 ? { filter: pineconeFilter } : {}));
851
860
  return ((_b = result.matches) != null ? _b : []).map((m) => {
852
- var _a4, _b2;
861
+ var _a3, _b2;
853
862
  return {
854
863
  id: m.id,
855
- score: (_a4 = m.score) != null ? _a4 : 0,
864
+ score: (_a3 = m.score) != null ? _a3 : 0,
856
865
  content: ((_b2 = m.metadata) == null ? void 0 : _b2.content) || "",
857
866
  metadata: m.metadata
858
867
  };
@@ -895,13 +904,13 @@ var init_PostgreSQLProvider = __esm({
895
904
  init_BaseVectorProvider();
896
905
  PostgreSQLProvider = class extends BaseVectorProvider {
897
906
  constructor(config) {
898
- var _a3;
907
+ var _a2;
899
908
  super(config);
900
909
  this.tableName = this.indexName.replace(/[^a-z0-9_]/gi, "_");
901
910
  const opts = config.options;
902
911
  if (!opts.connectionString) throw new Error("[PostgreSQLProvider] options.connectionString is required");
903
912
  this.connectionString = opts.connectionString;
904
- this.dimensions = (_a3 = opts.dimensions) != null ? _a3 : 1536;
913
+ this.dimensions = (_a2 = opts.dimensions) != null ? _a2 : 1536;
905
914
  }
906
915
  static getValidator() {
907
916
  return {
@@ -982,7 +991,7 @@ var init_PostgreSQLProvider = __esm({
982
991
  }
983
992
  }
984
993
  async upsert(doc, namespace = "") {
985
- var _a3;
994
+ var _a2;
986
995
  const vectorLiteral = `[${doc.vector.join(",")}]`;
987
996
  await this.pool.query(
988
997
  `INSERT INTO ${this.tableName} (id, namespace, content, metadata, embedding)
@@ -992,7 +1001,7 @@ var init_PostgreSQLProvider = __esm({
992
1001
  content = EXCLUDED.content,
993
1002
  metadata = EXCLUDED.metadata,
994
1003
  embedding = EXCLUDED.embedding`,
995
- [doc.id, namespace, doc.content, JSON.stringify((_a3 = doc.metadata) != null ? _a3 : {}), vectorLiteral]
1004
+ [doc.id, namespace, doc.content, JSON.stringify((_a2 = doc.metadata) != null ? _a2 : {}), vectorLiteral]
996
1005
  );
997
1006
  }
998
1007
  async batchUpsert(docs, namespace = "") {
@@ -1005,9 +1014,9 @@ var init_PostgreSQLProvider = __esm({
1005
1014
  const batch = docs.slice(i, i + BATCH_SIZE);
1006
1015
  const values = [];
1007
1016
  const valuePlaceholders = batch.map((doc, idx) => {
1008
- var _a3;
1017
+ var _a2;
1009
1018
  const offset = idx * 5;
1010
- values.push(doc.id, namespace, doc.content, JSON.stringify((_a3 = doc.metadata) != null ? _a3 : {}), `[${doc.vector.join(",")}]`);
1019
+ values.push(doc.id, namespace, doc.content, JSON.stringify((_a2 = doc.metadata) != null ? _a2 : {}), `[${doc.vector.join(",")}]`);
1011
1020
  return `($${offset + 1}, $${offset + 2}, $${offset + 3}, $${offset + 4}, $${offset + 5}::vector)`;
1012
1021
  }).join(", ");
1013
1022
  const query = `
@@ -1090,8 +1099,8 @@ var init_PostgreSQLProvider = __esm({
1090
1099
 
1091
1100
  // src/utils/synonyms.ts
1092
1101
  function resolveMetadataValue(meta, uiKey) {
1093
- var _a3;
1094
- const synonyms = (_a3 = FIELD_SYNONYMS[uiKey]) != null ? _a3 : [];
1102
+ var _a2;
1103
+ const synonyms = (_a2 = FIELD_SYNONYMS[uiKey]) != null ? _a2 : [];
1095
1104
  const keys = Object.keys(meta);
1096
1105
  const systemKeys = ["namespace", "filename", "filesize", "filetype", "docid", "uploadedat", "dimension", "chunkindex"];
1097
1106
  let match = keys.find((k) => k.toLowerCase() === uiKey.toLowerCase());
@@ -1178,14 +1187,14 @@ var init_MultiTablePostgresProvider = __esm({
1178
1187
  init_synonyms();
1179
1188
  MultiTablePostgresProvider = class extends BaseVectorProvider {
1180
1189
  constructor(config) {
1181
- var _a3, _b, _c;
1190
+ var _a2, _b, _c;
1182
1191
  super(config);
1183
1192
  const opts = config.options || {};
1184
1193
  if (!opts.connectionString) {
1185
1194
  throw new Error("[MultiTablePostgresProvider] options.connectionString is required");
1186
1195
  }
1187
1196
  this.connectionString = opts.connectionString;
1188
- this.dimensions = (_a3 = opts.dimensions) != null ? _a3 : 768;
1197
+ this.dimensions = (_a2 = opts.dimensions) != null ? _a2 : 768;
1189
1198
  this.tables = [];
1190
1199
  const rawSearchFields = (_c = (_b = opts.searchFields) != null ? _b : process.env.VECTOR_DB_SEARCH_FIELDS) != null ? _c : ["name", "product_name", "productname", "title"];
1191
1200
  this.searchFields = typeof rawSearchFields === "string" ? rawSearchFields.split(",").map((f) => f.trim()).filter(Boolean) : rawSearchFields;
@@ -1224,7 +1233,6 @@ var init_MultiTablePostgresProvider = __esm({
1224
1233
  this.tables = res.rows.map((r) => r.table_name);
1225
1234
  if (this.tables.length === 0) {
1226
1235
  console.warn('[MultiTablePostgresProvider] No tables with "embedding" columns found in the database.');
1227
- } else {
1228
1236
  }
1229
1237
  } finally {
1230
1238
  client.release();
@@ -1240,11 +1248,11 @@ var init_MultiTablePostgresProvider = __esm({
1240
1248
  * Batch upsert documents by dynamically provisioning tables and columns based on CSV headers.
1241
1249
  */
1242
1250
  async batchUpsert(docs, namespace = "") {
1243
- var _a3;
1251
+ var _a2;
1244
1252
  if (docs.length === 0) return;
1245
1253
  const docsByFile = {};
1246
1254
  for (const doc of docs) {
1247
- const fileName = ((_a3 = doc.metadata) == null ? void 0 : _a3.fileName) || this.uploadTable;
1255
+ const fileName = ((_a2 = doc.metadata) == null ? void 0 : _a2.fileName) || this.uploadTable;
1248
1256
  if (!docsByFile[fileName]) docsByFile[fileName] = [];
1249
1257
  docsByFile[fileName].push(doc);
1250
1258
  }
@@ -1297,11 +1305,11 @@ var init_MultiTablePostgresProvider = __esm({
1297
1305
  const headerColumns = csvHeaders.map((h) => `"${h}"`).join(", ");
1298
1306
  const allColumns = `id, ${headerColumns ? headerColumns + ", " : ""}namespace, content, metadata, embedding`;
1299
1307
  const valuePlaceholders = batch.map((doc, idx) => {
1300
- var _a4, _b;
1308
+ var _a3, _b;
1301
1309
  const offset = idx * (5 + csvHeaders.length);
1302
1310
  values.push(doc.id);
1303
1311
  for (const h of csvHeaders) {
1304
- values.push(String(((_a4 = doc.metadata) == null ? void 0 : _a4[h]) || ""));
1312
+ values.push(String(((_a3 = doc.metadata) == null ? void 0 : _a3[h]) || ""));
1305
1313
  }
1306
1314
  values.push(namespace, doc.content, JSON.stringify((_b = doc.metadata) != null ? _b : {}), `[${doc.vector.join(",")}]`);
1307
1315
  const docPlaceholders = [];
@@ -1340,7 +1348,7 @@ var init_MultiTablePostgresProvider = __esm({
1340
1348
  * Query all configured tables and merge results, sorted by cosine similarity score.
1341
1349
  */
1342
1350
  async query(vector, topK, _namespace, _filter) {
1343
- var _a3;
1351
+ var _a2;
1344
1352
  if (!this.pool) {
1345
1353
  throw new Error("[MultiTablePostgresProvider] Provider not initialized. Call initialize() first.");
1346
1354
  }
@@ -1368,11 +1376,11 @@ var init_MultiTablePostgresProvider = __esm({
1368
1376
  const filterParams = [];
1369
1377
  if (metadataFilters && Object.keys(metadataFilters).length > 0) {
1370
1378
  const conditions = Object.entries(metadataFilters).map(([key, val]) => {
1371
- var _a5;
1379
+ var _a4;
1372
1380
  filterParams.push(String(val));
1373
1381
  const baseOffset = queryText && dynamicKeywordQuery ? 2 : 1;
1374
1382
  const paramIdx = baseOffset + filterParams.length;
1375
- const synonyms = (_a5 = FIELD_SYNONYMS[key]) != null ? _a5 : [];
1383
+ const synonyms = (_a4 = FIELD_SYNONYMS[key]) != null ? _a4 : [];
1376
1384
  const keysToCheck = [key, ...synonyms];
1377
1385
  const coalesceExprs = keysToCheck.flatMap((k) => [
1378
1386
  `metadata->>'${k}'`,
@@ -1439,7 +1447,7 @@ var init_MultiTablePostgresProvider = __esm({
1439
1447
  }
1440
1448
  const tableResults = [];
1441
1449
  for (const row of result.rows) {
1442
- const _a4 = row, { hybrid_score, id } = _a4, rest = __objRest(_a4, ["hybrid_score", "id"]);
1450
+ const _a3 = row, { hybrid_score, id } = _a3, rest = __objRest(_a3, ["hybrid_score", "id"]);
1443
1451
  delete rest.embedding;
1444
1452
  delete rest.vector_score;
1445
1453
  delete rest.keyword_score;
@@ -1468,10 +1476,10 @@ var init_MultiTablePostgresProvider = __esm({
1468
1476
  }
1469
1477
  allResults.sort((a, b) => b.score - a.score);
1470
1478
  if (allResults.length === 0) return [];
1471
- const bestMatchTable = (_a3 = allResults[0].metadata) == null ? void 0 : _a3.source_table;
1479
+ const bestMatchTable = (_a2 = allResults[0].metadata) == null ? void 0 : _a2.source_table;
1472
1480
  const finalSorted = allResults.filter((res) => {
1473
- var _a4;
1474
- return ((_a4 = res.metadata) == null ? void 0 : _a4.source_table) === bestMatchTable;
1481
+ var _a3;
1482
+ return ((_a3 = res.metadata) == null ? void 0 : _a3.source_table) === bestMatchTable;
1475
1483
  });
1476
1484
  return finalSorted.slice(0, Math.max(topK, 15));
1477
1485
  }
@@ -1869,14 +1877,14 @@ var init_QdrantProvider = __esm({
1869
1877
  * Samples points from the collection to discover available payload fields.
1870
1878
  */
1871
1879
  async discoverSchema() {
1872
- var _a3;
1880
+ var _a2;
1873
1881
  try {
1874
1882
  const { data } = await this.http.post(`/collections/${this.indexName}/points/scroll`, {
1875
1883
  limit: 20,
1876
1884
  with_payload: true,
1877
1885
  with_vector: false
1878
1886
  });
1879
- const points = ((_a3 = data.result) == null ? void 0 : _a3.points) || [];
1887
+ const points = ((_a2 = data.result) == null ? void 0 : _a2.points) || [];
1880
1888
  const keys = /* @__PURE__ */ new Set();
1881
1889
  for (const point of points) {
1882
1890
  const payload = point.payload || {};
@@ -1902,11 +1910,11 @@ var init_QdrantProvider = __esm({
1902
1910
  * Ensures the collection exists. Creates it if missing.
1903
1911
  */
1904
1912
  async ensureCollection() {
1905
- var _a3;
1913
+ var _a2;
1906
1914
  try {
1907
1915
  await this.http.get(`/collections/${this.indexName}`);
1908
1916
  } catch (err) {
1909
- if (axios4.isAxiosError(err) && ((_a3 = err.response) == null ? void 0 : _a3.status) === 404) {
1917
+ if (axios4.isAxiosError(err) && ((_a2 = err.response) == null ? void 0 : _a2.status) === 404) {
1910
1918
  const opts = this.config.options;
1911
1919
  const dimensionsForCreate = opts.dimensions || 1536;
1912
1920
  await this.http.put(`/collections/${this.indexName}`, {
@@ -1924,7 +1932,7 @@ var init_QdrantProvider = __esm({
1924
1932
  * Ensures that a payload field has an index.
1925
1933
  */
1926
1934
  async ensureIndex(fieldName, schema = "keyword") {
1927
- var _a3;
1935
+ var _a2;
1928
1936
  let fullPath = fieldName;
1929
1937
  if (!this.isFlatPayload && !fieldName.includes(".") && fieldName !== "namespace" && fieldName !== this.contentField) {
1930
1938
  fullPath = `${this.metadataField}.${fieldName}`;
@@ -1936,7 +1944,7 @@ var init_QdrantProvider = __esm({
1936
1944
  });
1937
1945
  } catch (err) {
1938
1946
  let status;
1939
- if (axios4.isAxiosError(err)) status = (_a3 = err.response) == null ? void 0 : _a3.status;
1947
+ if (axios4.isAxiosError(err)) status = (_a2 = err.response) == null ? void 0 : _a2.status;
1940
1948
  if (status === 409) return;
1941
1949
  console.warn(`[QdrantProvider] \u26A0\uFE0F Could not ensure index for "${fullPath}":`, err instanceof Error ? err.message : String(err));
1942
1950
  }
@@ -1965,7 +1973,7 @@ var init_QdrantProvider = __esm({
1965
1973
  await this.http.put(`/collections/${this.indexName}/points`, payload);
1966
1974
  }
1967
1975
  async query(vector, topK, namespace, _filter) {
1968
- var _a3;
1976
+ var _a2;
1969
1977
  const must = [];
1970
1978
  if (namespace) {
1971
1979
  must.push({ key: "namespace", match: { value: namespace } });
@@ -1987,7 +1995,7 @@ var init_QdrantProvider = __esm({
1987
1995
  limit: topK,
1988
1996
  with_payload: true,
1989
1997
  params: {
1990
- hnsw_ef: ((_a3 = this.config.options) == null ? void 0 : _a3.efSearch) || Math.max(topK * 20, 128),
1998
+ hnsw_ef: ((_a2 = this.config.options) == null ? void 0 : _a2.efSearch) || Math.max(topK * 20, 128),
1991
1999
  exact: false
1992
2000
  },
1993
2001
  filter: must.length > 0 ? { must } : void 0
@@ -2082,12 +2090,12 @@ var init_ChromaDBProvider = __esm({
2082
2090
  * Get or create the ChromaDB collection.
2083
2091
  */
2084
2092
  async initialize() {
2085
- var _a3;
2093
+ var _a2;
2086
2094
  try {
2087
2095
  const { data } = await this.http.get(`/api/v1/collections/${this.indexName}`);
2088
2096
  this.collectionId = data.id;
2089
2097
  } catch (err) {
2090
- if (axios5.isAxiosError(err) && ((_a3 = err.response) == null ? void 0 : _a3.status) === 404) {
2098
+ if (axios5.isAxiosError(err) && ((_a2 = err.response) == null ? void 0 : _a2.status) === 404) {
2091
2099
  const { data } = await this.http.post("/api/v1/collections", {
2092
2100
  name: this.indexName
2093
2101
  });
@@ -2305,7 +2313,7 @@ var init_WeaviateProvider = __esm({
2305
2313
  await this.http.post("/v1/batch/objects", payload);
2306
2314
  }
2307
2315
  async query(vector, topK, namespace, _filter) {
2308
- var _a3, _b;
2316
+ var _a2, _b;
2309
2317
  const queryText = _filter == null ? void 0 : _filter.queryText;
2310
2318
  const sanitizedFilter = this.sanitizeFilter(_filter);
2311
2319
  const searchParams = queryText ? `hybrid: { query: ${JSON.stringify(queryText)}, alpha: 0.5 }` : `nearVector: { vector: ${JSON.stringify(vector)} }`;
@@ -2331,7 +2339,7 @@ var init_WeaviateProvider = __esm({
2331
2339
  `
2332
2340
  };
2333
2341
  const { data } = await this.http.post("/v1/graphql", graphqlQuery);
2334
- const results = ((_b = (_a3 = data.data) == null ? void 0 : _a3.Get) == null ? void 0 : _b[this.indexName]) || [];
2342
+ const results = ((_b = (_a2 = data.data) == null ? void 0 : _a2.Get) == null ? void 0 : _b[this.indexName]) || [];
2335
2343
  return results.map((res) => ({
2336
2344
  id: res["_additional"].id,
2337
2345
  score: 1 - res["_additional"].distance,
@@ -2413,21 +2421,21 @@ var init_UniversalVectorProvider = __esm({
2413
2421
  }
2414
2422
  }
2415
2423
  async initialize() {
2416
- var _a3;
2424
+ var _a2;
2417
2425
  this.http = axios8.create({
2418
2426
  baseURL: this.opts.baseUrl,
2419
2427
  headers: __spreadValues({
2420
2428
  "Content-Type": "application/json"
2421
2429
  }, this.opts.headers),
2422
- timeout: (_a3 = this.opts.timeout) != null ? _a3 : 3e4
2430
+ timeout: (_a2 = this.opts.timeout) != null ? _a2 : 3e4
2423
2431
  });
2424
2432
  if (!await this.ping()) {
2425
2433
  throw new Error(`[UniversalVectorProvider] Failed to connect to ${this.opts.baseUrl}`);
2426
2434
  }
2427
2435
  }
2428
2436
  async upsert(doc, namespace) {
2429
- var _a3, _b, _c;
2430
- const endpoint = (_a3 = this.opts.upsertEndpoint) != null ? _a3 : "/upsert";
2437
+ var _a2, _b, _c;
2438
+ const endpoint = (_a2 = this.opts.upsertEndpoint) != null ? _a2 : "/upsert";
2431
2439
  const template = (_b = this.opts.upsertTemplate) != null ? _b : JSON.stringify({
2432
2440
  id: "{{id}}",
2433
2441
  vector: "{{vector}}",
@@ -2460,8 +2468,8 @@ var init_UniversalVectorProvider = __esm({
2460
2468
  }
2461
2469
  }
2462
2470
  async query(vector, topK, namespace, filter) {
2463
- var _a3, _b;
2464
- const endpoint = (_a3 = this.opts.queryEndpoint) != null ? _a3 : "/query";
2471
+ var _a2, _b;
2472
+ const endpoint = (_a2 = this.opts.queryEndpoint) != null ? _a2 : "/query";
2465
2473
  const template = (_b = this.opts.queryTemplate) != null ? _b : JSON.stringify({
2466
2474
  vector: "{{vector}}",
2467
2475
  limit: "{{topK}}",
@@ -2487,9 +2495,9 @@ var init_UniversalVectorProvider = __esm({
2487
2495
  );
2488
2496
  }
2489
2497
  return results.map((item) => {
2490
- var _a4, _b2, _c, _d, _e, _f, _g2;
2498
+ var _a3, _b2, _c, _d, _e, _f, _g2;
2491
2499
  return {
2492
- id: item[(_a4 = this.opts.queryIdField) != null ? _a4 : "id"],
2500
+ id: item[(_a3 = this.opts.queryIdField) != null ? _a3 : "id"],
2493
2501
  score: (_c = item[(_b2 = this.opts.queryScoreField) != null ? _b2 : "score"]) != null ? _c : 0,
2494
2502
  content: (_e = item[(_d = this.opts.queryContentField) != null ? _d : "content"]) != null ? _e : "",
2495
2503
  metadata: (_g2 = item[(_f = this.opts.queryMetadataField) != null ? _f : "metadata"]) != null ? _g2 : {}
@@ -2502,8 +2510,8 @@ var init_UniversalVectorProvider = __esm({
2502
2510
  }
2503
2511
  }
2504
2512
  async delete(id, namespace) {
2505
- var _a3, _b;
2506
- const endpoint = (_a3 = this.opts.deleteEndpoint) != null ? _a3 : "/delete";
2513
+ var _a2, _b;
2514
+ const endpoint = (_a2 = this.opts.deleteEndpoint) != null ? _a2 : "/delete";
2507
2515
  const template = (_b = this.opts.deleteTemplate) != null ? _b : JSON.stringify({
2508
2516
  id: "{{id}}",
2509
2517
  namespace: "{{namespace}}"
@@ -2521,8 +2529,8 @@ var init_UniversalVectorProvider = __esm({
2521
2529
  }
2522
2530
  }
2523
2531
  async deleteNamespace(namespace) {
2524
- var _a3;
2525
- const endpoint = (_a3 = this.opts.deleteNamespaceEndpoint) != null ? _a3 : "/delete-namespace";
2532
+ var _a2;
2533
+ const endpoint = (_a2 = this.opts.deleteNamespaceEndpoint) != null ? _a2 : "/delete-namespace";
2526
2534
  try {
2527
2535
  await this.http.post(endpoint, { namespace });
2528
2536
  } catch (error) {
@@ -2532,9 +2540,9 @@ var init_UniversalVectorProvider = __esm({
2532
2540
  }
2533
2541
  }
2534
2542
  async ping() {
2535
- var _a3;
2543
+ var _a2;
2536
2544
  try {
2537
- const endpoint = (_a3 = this.opts.pingEndpoint) != null ? _a3 : "/health";
2545
+ const endpoint = (_a2 = this.opts.pingEndpoint) != null ? _a2 : "/health";
2538
2546
  const response = await this.http.get(endpoint, { timeout: 5e3 });
2539
2547
  return response.status >= 200 && response.status < 300;
2540
2548
  } catch (e) {
@@ -2667,13 +2675,13 @@ var AuthenticationException = class extends RetrivoraError {
2667
2675
  }
2668
2676
  };
2669
2677
  function wrapError(err, defaultCode, defaultMessage) {
2670
- var _a3;
2678
+ var _a2;
2671
2679
  if (err instanceof RetrivoraError) {
2672
2680
  return err;
2673
2681
  }
2674
2682
  const error = err;
2675
2683
  const message = (error == null ? void 0 : error.message) || defaultMessage || String(err);
2676
- const status = (error == null ? void 0 : error.status) || (error == null ? void 0 : error.statusCode) || ((_a3 = error == null ? void 0 : error.response) == null ? void 0 : _a3.status);
2684
+ const status = (error == null ? void 0 : error.status) || (error == null ? void 0 : error.statusCode) || ((_a2 = error == null ? void 0 : error.response) == null ? void 0 : _a2.status);
2677
2685
  const code = error == null ? void 0 : error.code;
2678
2686
  if (status === 429 || /rate[- ]?limit/i.test(message) || code === "RATE_LIMIT_EXCEEDED") {
2679
2687
  return new RateLimitException(message, err);
@@ -2954,8 +2962,8 @@ var UI_BORDER_RADIUS_OPTIONS = ["none", "sm", "md", "lg", "xl", "full"];
2954
2962
  // src/config/serverConfig.ts
2955
2963
  var _runtimeConfig = null;
2956
2964
  function readString(env, name) {
2957
- var _a3;
2958
- const value = (_a3 = env[name]) == null ? void 0 : _a3.trim();
2965
+ var _a2;
2966
+ const value = (_a2 = env[name]) == null ? void 0 : _a2.trim();
2959
2967
  return value ? value : void 0;
2960
2968
  }
2961
2969
  function readNumber(env, name, fallback) {
@@ -2968,66 +2976,60 @@ function readNumber(env, name, fallback) {
2968
2976
  return parsed;
2969
2977
  }
2970
2978
  function readEnum(env, name, fallback, allowed) {
2971
- var _a3;
2972
- const value = (_a3 = readString(env, name)) != null ? _a3 : fallback;
2979
+ var _a2;
2980
+ const value = (_a2 = readString(env, name)) != null ? _a2 : fallback;
2973
2981
  if (allowed.includes(value)) {
2974
2982
  return value;
2975
2983
  }
2976
2984
  throw new Error(`[getRagConfig] ${name} must be one of: ${allowed.join(", ")}`);
2977
2985
  }
2978
2986
  function getEnvConfig(env = process.env, base) {
2979
- var _a3, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _A, _B, _C, _D, _E, _F, _G, _H, _I, _J, _K, _L, _M, _N, _O, _P, _Q, _R, _S, _T, _U, _V, _W, _X, _Y, _Z, __, _$, _aa, _ba, _ca, _da, _ea, _fa, _ga, _ha, _ia, _ja, _ka, _la, _ma, _na, _oa, _pa, _qa, _ra, _sa, _ta, _ua, _va, _wa, _xa, _ya, _za, _Aa, _Ba, _Ca, _Da, _Ea, _Fa, _Ga, _Ha, _Ia, _Ja, _Ka, _La, _Ma, _Na, _Oa, _Pa, _Qa, _Ra, _Sa, _Ta, _Ua, _Va, _Wa, _Xa, _Ya, _Za, __a, _$a, _ab, _bb, _cb, _db, _eb, _fb, _gb, _hb, _ib, _jb, _kb, _lb, _mb, _nb, _ob, _pb, _qb, _rb, _sb, _tb, _ub, _vb, _wb, _xb, _yb, _zb, _Ab, _Bb, _Cb, _Db, _Eb, _Fb, _Gb, _Hb, _Ib, _Jb, _Kb, _Lb, _Mb, _Nb;
2980
- const licenseKey = (_d = (_c = (_b = (_a3 = readString(env, "RAG_LICENSE_KEY")) != null ? _a3 : readString(env, "RETRIVORA_LICENSE_KEY")) != null ? _b : readString(env, "NEXT_PUBLIC_RETRIVORA_LICENSE_KEY")) != null ? _c : readString(env, "LICENSE_KEY")) != null ? _d : base == null ? void 0 : base.licenseKey;
2981
- const jwtProjectId = (() => {
2982
- if (!licenseKey) return void 0;
2983
- try {
2984
- const parts = licenseKey.split(".");
2985
- if (parts.length >= 2) {
2986
- const payload = JSON.parse(Buffer.from(parts[1], "base64url").toString("utf8"));
2987
- return payload == null ? void 0 : payload.projectId;
2988
- }
2989
- } catch (e) {
2990
- }
2991
- return void 0;
2992
- })();
2993
- const projectId = (_i = (_h = (_g2 = (_f = (_e = readString(env, "RAG_PROJECT_ID")) != null ? _e : readString(env, "NEXT_PUBLIC_RAG_PROJECT_ID")) != null ? _f : readString(env, "NEXT_PUBLIC_PROJECT_ID")) != null ? _g2 : base == null ? void 0 : base.projectId) != null ? _h : jwtProjectId) != null ? _i : "default";
2994
- const telemetryEnabled = readString(env, "TELEMETRY_ENABLED") === "true" || readString(env, "NEXT_PUBLIC_TELEMETRY_ENABLED") === "true" || ((_j = base == null ? void 0 : base.telemetry) == null ? void 0 : _j.enabled) || Boolean(licenseKey);
2987
+ var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _A, _B, _C, _D, _E, _F, _G, _H, _I, _J, _K, _L, _M, _N, _O, _P, _Q, _R, _S, _T, _U, _V, _W, _X, _Y, _Z, __, _$, _aa, _ba, _ca, _da, _ea, _fa, _ga, _ha, _ia, _ja, _ka, _la, _ma, _na, _oa, _pa, _qa, _ra, _sa, _ta, _ua, _va, _wa, _xa, _ya, _za, _Aa, _Ba, _Ca, _Da, _Ea, _Fa, _Ga, _Ha, _Ia, _Ja, _Ka, _La, _Ma, _Na, _Oa, _Pa, _Qa, _Ra, _Sa, _Ta, _Ua, _Va, _Wa, _Xa, _Ya, _Za, __a, _$a, _ab, _bb, _cb, _db, _eb, _fb, _gb, _hb, _ib, _jb, _kb, _lb, _mb, _nb, _ob, _pb, _qb, _rb, _sb, _tb, _ub, _vb, _wb, _xb, _yb, _zb, _Ab, _Bb, _Cb, _Db, _Eb, _Fb, _Gb, _Hb, _Ib, _Jb;
2988
+ const licenseKey = (_c = (_b = (_a2 = readString(env, "RAG_LICENSE_KEY")) != null ? _a2 : readString(env, "RETRIVORA_LICENSE_KEY")) != null ? _b : (
2989
+ // Fix SEC-4: NEXT_PUBLIC_RETRIVORA_LICENSE_KEY is NOT included here.
2990
+ // That variable is inlined into the browser bundle by Next.js at build time
2991
+ // and must never be used as a server-side secret source. Developers who
2992
+ // previously relied on this should migrate to RETRIVORA_LICENSE_KEY.
2993
+ readString(env, "LICENSE_KEY")
2994
+ )) != null ? _c : base == null ? void 0 : base.licenseKey;
2995
+ const projectId = (_g2 = (_f = (_e = (_d = readString(env, "RAG_PROJECT_ID")) != null ? _d : readString(env, "NEXT_PUBLIC_RAG_PROJECT_ID")) != null ? _e : readString(env, "NEXT_PUBLIC_PROJECT_ID")) != null ? _f : base == null ? void 0 : base.projectId) != null ? _g2 : "default";
2996
+ const telemetryEnabled = readString(env, "TELEMETRY_ENABLED") === "true" || readString(env, "NEXT_PUBLIC_TELEMETRY_ENABLED") === "true" || ((_h = base == null ? void 0 : base.telemetry) == null ? void 0 : _h.enabled) || Boolean(licenseKey);
2995
2997
  const vectorProvider = readEnum(env, "VECTOR_DB_PROVIDER", "pinecone", VECTOR_DB_PROVIDERS);
2996
2998
  const embeddingDimensions = readNumber(env, "EMBEDDING_DIMENSIONS", 768);
2997
2999
  const vectorDbOptions = {};
2998
3000
  if (vectorProvider === "pinecone") {
2999
- vectorDbOptions.apiKey = (_o = (_n = (_m = readString(env, "PINECONE_API_KEY")) != null ? _m : (_l = (_k = base == null ? void 0 : base.vectorDb) == null ? void 0 : _k.options) == null ? void 0 : _l.apiKey) != null ? _n : process.env.PINECONE_API_KEY) != null ? _o : "";
3000
- vectorDbOptions.indexName = (_v = (_u = (_t = (_q = readString(env, "PINECONE_INDEX")) != null ? _q : (_p = base == null ? void 0 : base.vectorDb) == null ? void 0 : _p.indexName) != null ? _t : (_s = (_r = base == null ? void 0 : base.vectorDb) == null ? void 0 : _r.options) == null ? void 0 : _s.indexName) != null ? _u : process.env.PINECONE_INDEX) != null ? _v : "retrivora-free";
3001
+ vectorDbOptions.apiKey = (_l = (_k = readString(env, "PINECONE_API_KEY")) != null ? _k : (_j = (_i = base == null ? void 0 : base.vectorDb) == null ? void 0 : _i.options) == null ? void 0 : _j.apiKey) != null ? _l : "";
3002
+ vectorDbOptions.indexName = (_r = (_q = (_n = readString(env, "PINECONE_INDEX")) != null ? _n : (_m = base == null ? void 0 : base.vectorDb) == null ? void 0 : _m.indexName) != null ? _q : (_p = (_o = base == null ? void 0 : base.vectorDb) == null ? void 0 : _o.options) == null ? void 0 : _p.indexName) != null ? _r : "retrivora-free";
3001
3003
  } else if (vectorProvider === "pgvector" || vectorProvider === "postgresql") {
3002
- vectorDbOptions.connectionString = (_A = (_z = (_w = readString(env, "PGVECTOR_CONNECTION_STRING")) != null ? _w : readString(env, "POSTGRES_URL")) != null ? _z : (_y = (_x = base == null ? void 0 : base.vectorDb) == null ? void 0 : _x.options) == null ? void 0 : _y.connectionString) != null ? _A : "";
3003
- vectorDbOptions.tables = (_F = (_C = (_B = readString(env, "VECTOR_DB_TABLES")) != null ? _B : readString(env, "POSTGRES_TABLES")) == null ? void 0 : _C.split(",").map((t) => t.trim())) != null ? _F : (_E = (_D = base == null ? void 0 : base.vectorDb) == null ? void 0 : _D.options) == null ? void 0 : _E.tables;
3004
- vectorDbOptions.searchFields = (_J = (_G = readString(env, "POSTGRES_SEARCH_FIELDS")) == null ? void 0 : _G.split(",").map((f) => f.trim())) != null ? _J : (_I = (_H = base == null ? void 0 : base.vectorDb) == null ? void 0 : _H.options) == null ? void 0 : _I.searchFields;
3004
+ vectorDbOptions.connectionString = (_w = (_v = (_s = readString(env, "PGVECTOR_CONNECTION_STRING")) != null ? _s : readString(env, "POSTGRES_URL")) != null ? _v : (_u = (_t = base == null ? void 0 : base.vectorDb) == null ? void 0 : _t.options) == null ? void 0 : _u.connectionString) != null ? _w : "";
3005
+ vectorDbOptions.tables = (_B = (_y = (_x = readString(env, "VECTOR_DB_TABLES")) != null ? _x : readString(env, "POSTGRES_TABLES")) == null ? void 0 : _y.split(",").map((t) => t.trim())) != null ? _B : (_A = (_z = base == null ? void 0 : base.vectorDb) == null ? void 0 : _z.options) == null ? void 0 : _A.tables;
3006
+ vectorDbOptions.searchFields = (_F = (_C = readString(env, "POSTGRES_SEARCH_FIELDS")) == null ? void 0 : _C.split(",").map((f) => f.trim())) != null ? _F : (_E = (_D = base == null ? void 0 : base.vectorDb) == null ? void 0 : _D.options) == null ? void 0 : _E.searchFields;
3005
3007
  vectorDbOptions.dimensions = embeddingDimensions;
3006
3008
  } else if (vectorProvider === "mongodb") {
3007
- vectorDbOptions.uri = (_N = (_M = readString(env, "MONGODB_URI")) != null ? _M : (_L = (_K = base == null ? void 0 : base.vectorDb) == null ? void 0 : _K.options) == null ? void 0 : _L.uri) != null ? _N : "";
3008
- vectorDbOptions.database = (_R = (_Q = readString(env, "MONGODB_DB")) != null ? _Q : (_P = (_O = base == null ? void 0 : base.vectorDb) == null ? void 0 : _O.options) == null ? void 0 : _P.database) != null ? _R : "";
3009
- vectorDbOptions.collection = (_V = (_U = readString(env, "MONGODB_COLLECTION")) != null ? _U : (_T = (_S = base == null ? void 0 : base.vectorDb) == null ? void 0 : _S.options) == null ? void 0 : _T.collection) != null ? _V : "";
3010
- vectorDbOptions.indexName = (_$ = (__ = (_X = readString(env, "MONGODB_INDEX_NAME")) != null ? _X : (_W = base == null ? void 0 : base.vectorDb) == null ? void 0 : _W.indexName) != null ? __ : (_Z = (_Y = base == null ? void 0 : base.vectorDb) == null ? void 0 : _Y.options) == null ? void 0 : _Z.indexName) != null ? _$ : "vector_index";
3009
+ vectorDbOptions.uri = (_J = (_I = readString(env, "MONGODB_URI")) != null ? _I : (_H = (_G = base == null ? void 0 : base.vectorDb) == null ? void 0 : _G.options) == null ? void 0 : _H.uri) != null ? _J : "";
3010
+ vectorDbOptions.database = (_N = (_M = readString(env, "MONGODB_DB")) != null ? _M : (_L = (_K = base == null ? void 0 : base.vectorDb) == null ? void 0 : _K.options) == null ? void 0 : _L.database) != null ? _N : "";
3011
+ vectorDbOptions.collection = (_R = (_Q = readString(env, "MONGODB_COLLECTION")) != null ? _Q : (_P = (_O = base == null ? void 0 : base.vectorDb) == null ? void 0 : _O.options) == null ? void 0 : _P.collection) != null ? _R : "";
3012
+ vectorDbOptions.indexName = (_X = (_W = (_T = readString(env, "MONGODB_INDEX_NAME")) != null ? _T : (_S = base == null ? void 0 : base.vectorDb) == null ? void 0 : _S.indexName) != null ? _W : (_V = (_U = base == null ? void 0 : base.vectorDb) == null ? void 0 : _U.options) == null ? void 0 : _V.indexName) != null ? _X : "vector_index";
3011
3013
  } else if (vectorProvider === "qdrant") {
3012
- vectorDbOptions.baseUrl = (_da = (_ca = readString(env, "QDRANT_URL")) != null ? _ca : (_ba = (_aa = base == null ? void 0 : base.vectorDb) == null ? void 0 : _aa.options) == null ? void 0 : _ba.baseUrl) != null ? _da : "http://localhost:6333";
3013
- vectorDbOptions.apiKey = (_ga = readString(env, "QDRANT_API_KEY")) != null ? _ga : (_fa = (_ea = base == null ? void 0 : base.vectorDb) == null ? void 0 : _ea.options) == null ? void 0 : _fa.apiKey;
3014
+ vectorDbOptions.baseUrl = (_$ = (__ = readString(env, "QDRANT_URL")) != null ? __ : (_Z = (_Y = base == null ? void 0 : base.vectorDb) == null ? void 0 : _Y.options) == null ? void 0 : _Z.baseUrl) != null ? _$ : "http://localhost:6333";
3015
+ vectorDbOptions.apiKey = (_ca = readString(env, "QDRANT_API_KEY")) != null ? _ca : (_ba = (_aa = base == null ? void 0 : base.vectorDb) == null ? void 0 : _aa.options) == null ? void 0 : _ba.apiKey;
3014
3016
  vectorDbOptions.dimensions = embeddingDimensions;
3015
3017
  } else if (vectorProvider === "milvus") {
3016
- vectorDbOptions.baseUrl = (_ha = readString(env, "MILVUS_URL")) != null ? _ha : "http://localhost:19530";
3018
+ vectorDbOptions.baseUrl = (_da = readString(env, "MILVUS_URL")) != null ? _da : "http://localhost:19530";
3017
3019
  vectorDbOptions.apiKey = readString(env, "MILVUS_API_KEY");
3018
3020
  } else if (vectorProvider === "chromadb") {
3019
- vectorDbOptions.baseUrl = (_ia = readString(env, "CHROMADB_URL")) != null ? _ia : "http://localhost:8000";
3021
+ vectorDbOptions.baseUrl = (_ea = readString(env, "CHROMADB_URL")) != null ? _ea : "http://localhost:8000";
3020
3022
  } else if (vectorProvider === "weaviate") {
3021
- vectorDbOptions.baseUrl = (_ja = readString(env, "WEAVIATE_URL")) != null ? _ja : "http://localhost:8080";
3023
+ vectorDbOptions.baseUrl = (_fa = readString(env, "WEAVIATE_URL")) != null ? _fa : "http://localhost:8080";
3022
3024
  vectorDbOptions.apiKey = readString(env, "WEAVIATE_API_KEY");
3023
3025
  } else if (vectorProvider === "redis") {
3024
- vectorDbOptions.baseUrl = (_ka = readString(env, "REDIS_URL")) != null ? _ka : "";
3026
+ vectorDbOptions.baseUrl = (_ga = readString(env, "REDIS_URL")) != null ? _ga : "";
3025
3027
  vectorDbOptions.apiKey = readString(env, "REDIS_API_KEY");
3026
3028
  } else if (vectorProvider === "rest") {
3027
- vectorDbOptions.baseUrl = (_la = readString(env, "VECTOR_DB_REST_URL")) != null ? _la : "";
3029
+ vectorDbOptions.baseUrl = (_ha = readString(env, "VECTOR_DB_REST_URL")) != null ? _ha : "";
3028
3030
  vectorDbOptions.headers = readString(env, "VECTOR_DB_REST_API_KEY") ? { "api-key": readString(env, "VECTOR_DB_REST_API_KEY") } : {};
3029
3031
  } else if (vectorProvider === "universal_rest") {
3030
- vectorDbOptions.baseUrl = (_na = (_ma = readString(env, "VECTOR_BASE_URL")) != null ? _ma : readString(env, "VECTOR_DB_REST_URL")) != null ? _na : "";
3032
+ vectorDbOptions.baseUrl = (_ja = (_ia = readString(env, "VECTOR_BASE_URL")) != null ? _ia : readString(env, "VECTOR_DB_REST_URL")) != null ? _ja : "";
3031
3033
  vectorDbOptions.profile = readString(env, "VECTOR_UNIVERSAL_PROFILE");
3032
3034
  vectorDbOptions.headers = readString(env, "VECTOR_DB_REST_API_KEY") ? { Authorization: `Bearer ${readString(env, "VECTOR_DB_REST_API_KEY")}` } : {};
3033
3035
  }
@@ -3046,8 +3048,8 @@ function getEnvConfig(env = process.env, base) {
3046
3048
  // Anthropic needs a separate embedding provider; key kept for completeness
3047
3049
  gemini: readString(env, "GEMINI_API_KEY"),
3048
3050
  ollama: void 0,
3049
- universal_rest: (_oa = readString(env, "EMBEDDING_API_KEY")) != null ? _oa : readString(env, "OPENAI_API_KEY"),
3050
- custom: (_pa = readString(env, "EMBEDDING_API_KEY")) != null ? _pa : readString(env, "OPENAI_API_KEY")
3051
+ universal_rest: (_ka = readString(env, "EMBEDDING_API_KEY")) != null ? _ka : readString(env, "OPENAI_API_KEY"),
3052
+ custom: (_la = readString(env, "EMBEDDING_API_KEY")) != null ? _la : readString(env, "OPENAI_API_KEY")
3051
3053
  };
3052
3054
  const DEFAULT_MODEL_BY_PROVIDER = {
3053
3055
  openai: "gpt-4o",
@@ -3069,27 +3071,27 @@ function getEnvConfig(env = process.env, base) {
3069
3071
  })();
3070
3072
  const tierBasedLLMModel = isFreeTier2 ? "llama-3.1-8b-instant" : "llama-3.3-70b-versatile";
3071
3073
  const tierBasedEmbeddingModel = isFreeTier2 ? "text-embedding-004" : "text-embedding-004";
3072
- const controlPlaneUrl = (_qa = readString(env, "RETRIVORA_CONTROL_PLANE_URL")) != null ? _qa : readString(env, "NEXT_PUBLIC_RETRIVORA_CONTROL_PLANE_URL");
3074
+ const controlPlaneUrl = (_ma = readString(env, "RETRIVORA_CONTROL_PLANE_URL")) != null ? _ma : readString(env, "NEXT_PUBLIC_RETRIVORA_CONTROL_PLANE_URL");
3073
3075
  const defaultGatewayUrl = controlPlaneUrl ? `${controlPlaneUrl.replace(/\/$/, "")}/api/v1` : "https://llm.retrivora.com/api/v1";
3074
3076
  const rt = _runtimeConfig;
3075
3077
  const defaultLlmProvider = "universal_rest";
3076
- const llmProvider = (_ta = (_sa = readEnum(env, "LLM_PROVIDER", defaultLlmProvider, LLM_PROVIDERS)) != null ? _sa : (_ra = base == null ? void 0 : base.llm) == null ? void 0 : _ra.provider) != null ? _ta : defaultLlmProvider;
3077
- const llmBaseUrl = (_ya = (_xa = (_wa = (_ua = readString(env, "LITELLM_BASE_URL")) != null ? _ua : readString(env, "LLM_BASE_URL")) != null ? _wa : (_va = base == null ? void 0 : base.llm) == null ? void 0 : _va.baseUrl) != null ? _xa : rt == null ? void 0 : rt.llmBaseUrl) != null ? _ya : defaultGatewayUrl;
3078
- const llmModel = (_Ca = (_Ba = (_Aa = readString(env, "LLM_MODEL")) != null ? _Aa : (_za = base == null ? void 0 : base.llm) == null ? void 0 : _za.model) != null ? _Ba : rt == null ? void 0 : rt.llmModel) != null ? _Ca : tierBasedLLMModel;
3079
- const llmApiKey = (_Ga = (_Fa = (_Da = readString(env, "LITELLM_MASTER_KEY")) != null ? _Da : rt == null ? void 0 : rt.litellmApiKey) != null ? _Fa : (_Ea = base == null ? void 0 : base.llm) == null ? void 0 : _Ea.apiKey) != null ? _Ga : licenseKey;
3080
- const llmProfile = (_Ka = (_Ja = readString(env, "LLM_UNIVERSAL_PROFILE")) != null ? _Ja : (_Ia = (_Ha = base == null ? void 0 : base.llm) == null ? void 0 : _Ha.options) == null ? void 0 : _Ia.profile) != null ? _Ka : "litellm";
3078
+ const llmProvider = (_pa = (_oa = readEnum(env, "LLM_PROVIDER", defaultLlmProvider, LLM_PROVIDERS)) != null ? _oa : (_na = base == null ? void 0 : base.llm) == null ? void 0 : _na.provider) != null ? _pa : defaultLlmProvider;
3079
+ const llmBaseUrl = (_ua = (_ta = (_sa = (_qa = readString(env, "LITELLM_BASE_URL")) != null ? _qa : readString(env, "LLM_BASE_URL")) != null ? _sa : (_ra = base == null ? void 0 : base.llm) == null ? void 0 : _ra.baseUrl) != null ? _ta : rt == null ? void 0 : rt.llmBaseUrl) != null ? _ua : defaultGatewayUrl;
3080
+ const llmModel = (_ya = (_xa = (_wa = readString(env, "LLM_MODEL")) != null ? _wa : (_va = base == null ? void 0 : base.llm) == null ? void 0 : _va.model) != null ? _xa : rt == null ? void 0 : rt.llmModel) != null ? _ya : tierBasedLLMModel;
3081
+ const llmApiKey = (_Ca = (_Ba = (_za = readString(env, "LITELLM_MASTER_KEY")) != null ? _za : rt == null ? void 0 : rt.litellmApiKey) != null ? _Ba : (_Aa = base == null ? void 0 : base.llm) == null ? void 0 : _Aa.apiKey) != null ? _Ca : licenseKey;
3082
+ const llmProfile = (_Ga = (_Fa = readString(env, "LLM_UNIVERSAL_PROFILE")) != null ? _Fa : (_Ea = (_Da = base == null ? void 0 : base.llm) == null ? void 0 : _Da.options) == null ? void 0 : _Ea.profile) != null ? _Ga : "litellm";
3081
3083
  const defaultEmbeddingProvider = "universal_rest";
3082
- const embeddingProvider = (_Na = (_Ma = readEnum(env, "EMBEDDING_PROVIDER", defaultEmbeddingProvider, EMBEDDING_PROVIDERS)) != null ? _Ma : (_La = base == null ? void 0 : base.embedding) == null ? void 0 : _La.provider) != null ? _Na : defaultEmbeddingProvider;
3083
- const embeddingBaseUrl = (_Ua = (_Ta = (_Sa = (_Ra = (_Pa = (_Oa = readString(env, "LITELLM_BASE_URL")) != null ? _Oa : readString(env, "EMBEDDING_BASE_URL")) != null ? _Pa : readString(env, "LLM_BASE_URL")) != null ? _Ra : (_Qa = base == null ? void 0 : base.embedding) == null ? void 0 : _Qa.baseUrl) != null ? _Sa : rt == null ? void 0 : rt.embeddingBaseUrl) != null ? _Ta : rt == null ? void 0 : rt.llmBaseUrl) != null ? _Ua : defaultGatewayUrl;
3084
- const embeddingModel = (_Ya = (_Xa = (_Wa = readString(env, "EMBEDDING_MODEL")) != null ? _Wa : (_Va = base == null ? void 0 : base.embedding) == null ? void 0 : _Va.model) != null ? _Xa : rt == null ? void 0 : rt.embeddingModel) != null ? _Ya : tierBasedEmbeddingModel;
3085
- const embeddingApiKey = (_ab = (_$a = (_Za = readString(env, "LITELLM_MASTER_KEY")) != null ? _Za : rt == null ? void 0 : rt.litellmApiKey) != null ? _$a : (__a = base == null ? void 0 : base.embedding) == null ? void 0 : __a.apiKey) != null ? _ab : licenseKey;
3086
- const embeddingProfile = (_eb = (_db = readString(env, "EMBEDDING_UNIVERSAL_PROFILE")) != null ? _db : (_cb = (_bb = base == null ? void 0 : base.embedding) == null ? void 0 : _bb.options) == null ? void 0 : _cb.profile) != null ? _eb : "litellm";
3084
+ const embeddingProvider = (_Ja = (_Ia = readEnum(env, "EMBEDDING_PROVIDER", defaultEmbeddingProvider, EMBEDDING_PROVIDERS)) != null ? _Ia : (_Ha = base == null ? void 0 : base.embedding) == null ? void 0 : _Ha.provider) != null ? _Ja : defaultEmbeddingProvider;
3085
+ const embeddingBaseUrl = (_Qa = (_Pa = (_Oa = (_Na = (_La = (_Ka = readString(env, "LITELLM_BASE_URL")) != null ? _Ka : readString(env, "EMBEDDING_BASE_URL")) != null ? _La : readString(env, "LLM_BASE_URL")) != null ? _Na : (_Ma = base == null ? void 0 : base.embedding) == null ? void 0 : _Ma.baseUrl) != null ? _Oa : rt == null ? void 0 : rt.embeddingBaseUrl) != null ? _Pa : rt == null ? void 0 : rt.llmBaseUrl) != null ? _Qa : defaultGatewayUrl;
3086
+ const embeddingModel = (_Ua = (_Ta = (_Sa = readString(env, "EMBEDDING_MODEL")) != null ? _Sa : (_Ra = base == null ? void 0 : base.embedding) == null ? void 0 : _Ra.model) != null ? _Ta : rt == null ? void 0 : rt.embeddingModel) != null ? _Ua : tierBasedEmbeddingModel;
3087
+ const embeddingApiKey = (_Ya = (_Xa = (_Va = readString(env, "LITELLM_MASTER_KEY")) != null ? _Va : rt == null ? void 0 : rt.litellmApiKey) != null ? _Xa : (_Wa = base == null ? void 0 : base.embedding) == null ? void 0 : _Wa.apiKey) != null ? _Ya : licenseKey;
3088
+ const embeddingProfile = (_ab = (_$a = readString(env, "EMBEDDING_UNIVERSAL_PROFILE")) != null ? _$a : (__a = (_Za = base == null ? void 0 : base.embedding) == null ? void 0 : _Za.options) == null ? void 0 : __a.profile) != null ? _ab : "litellm";
3087
3089
  return __spreadProps(__spreadValues({
3088
3090
  projectId,
3089
3091
  licenseKey,
3090
3092
  vectorDb: {
3091
3093
  provider: vectorProvider,
3092
- indexName: (_gb = (_fb = readString(env, "VECTOR_DB_INDEX")) != null ? _fb : vectorDbOptions.indexName) != null ? _gb : "rag-index",
3094
+ indexName: (_cb = (_bb = readString(env, "VECTOR_DB_INDEX")) != null ? _bb : vectorDbOptions.indexName) != null ? _cb : "rag-index",
3093
3095
  options: vectorDbOptions
3094
3096
  },
3095
3097
  llm: {
@@ -3097,8 +3099,8 @@ function getEnvConfig(env = process.env, base) {
3097
3099
  model: llmModel,
3098
3100
  apiKey: llmApiKey,
3099
3101
  baseUrl: llmBaseUrl,
3100
- systemPrompt: (_hb = readString(env, "LLM_SYSTEM_PROMPT")) != null ? _hb : rt == null ? void 0 : rt.systemPrompt,
3101
- maxTokens: readNumber(env, "LLM_MAX_TOKENS", (_ib = rt == null ? void 0 : rt.maxTokens) != null ? _ib : 4096),
3102
+ systemPrompt: (_db = readString(env, "LLM_SYSTEM_PROMPT")) != null ? _db : rt == null ? void 0 : rt.systemPrompt,
3103
+ maxTokens: readNumber(env, "LLM_MAX_TOKENS", (_eb = rt == null ? void 0 : rt.maxTokens) != null ? _eb : 4096),
3102
3104
  temperature: readNumber(env, "LLM_TEMPERATURE", 0.7),
3103
3105
  options: {
3104
3106
  profile: llmProfile,
@@ -3119,30 +3121,30 @@ function getEnvConfig(env = process.env, base) {
3119
3121
  }
3120
3122
  },
3121
3123
  ui: {
3122
- title: (_kb = (_jb = readString(env, "NEXT_PUBLIC_UI_TITLE")) != null ? _jb : readString(env, "UI_TITLE")) != null ? _kb : "AI Assistant",
3123
- subtitle: (_mb = (_lb = readString(env, "NEXT_PUBLIC_UI_SUBTITLE")) != null ? _lb : readString(env, "UI_SUBTITLE")) != null ? _mb : "Powered by RAG",
3124
- primaryColor: (_ob = (_nb = readString(env, "NEXT_PUBLIC_PRIMARY_COLOR")) != null ? _nb : readString(env, "UI_PRIMARY_COLOR")) != null ? _ob : "#10b981",
3125
- accentColor: (_qb = (_pb = readString(env, "NEXT_PUBLIC_ACCENT_COLOR")) != null ? _pb : readString(env, "UI_ACCENT_COLOR")) != null ? _qb : "#3b82f6",
3126
- logoUrl: (_rb = readString(env, "NEXT_PUBLIC_LOGO_URL")) != null ? _rb : readString(env, "UI_LOGO_URL"),
3127
- placeholder: (_tb = (_sb = readString(env, "NEXT_PUBLIC_PLACEHOLDER")) != null ? _sb : readString(env, "UI_PLACEHOLDER")) != null ? _tb : "Ask me anything\u2026",
3128
- showSources: ((_vb = (_ub = readString(env, "NEXT_PUBLIC_SHOW_SOURCES")) != null ? _ub : readString(env, "UI_SHOW_SOURCES")) != null ? _vb : "true") !== "false",
3129
- welcomeMessage: (_xb = (_wb = readString(env, "NEXT_PUBLIC_WELCOME_MESSAGE")) != null ? _wb : readString(env, "UI_WELCOME_MESSAGE")) != null ? _xb : "Hello! I'm your AI assistant. Ask me anything about your documents.",
3130
- visualStyle: (_zb = (_yb = readString(env, "NEXT_PUBLIC_UI_VISUAL_STYLE")) != null ? _yb : readString(env, "UI_VISUAL_STYLE")) != null ? _zb : "glass",
3131
- borderRadius: (_Bb = (_Ab = readString(env, "NEXT_PUBLIC_UI_BORDER_RADIUS")) != null ? _Ab : readString(env, "UI_BORDER_RADIUS")) != null ? _Bb : "xl",
3132
- allowUpload: ((_Db = (_Cb = readString(env, "NEXT_PUBLIC_ALLOW_UPLOAD")) != null ? _Cb : readString(env, "UI_ALLOW_UPLOAD")) != null ? _Db : "false") === "true"
3124
+ title: (_gb = (_fb = readString(env, "NEXT_PUBLIC_UI_TITLE")) != null ? _fb : readString(env, "UI_TITLE")) != null ? _gb : "AI Assistant",
3125
+ subtitle: (_ib = (_hb = readString(env, "NEXT_PUBLIC_UI_SUBTITLE")) != null ? _hb : readString(env, "UI_SUBTITLE")) != null ? _ib : "Powered by RAG",
3126
+ primaryColor: (_kb = (_jb = readString(env, "NEXT_PUBLIC_PRIMARY_COLOR")) != null ? _jb : readString(env, "UI_PRIMARY_COLOR")) != null ? _kb : "#10b981",
3127
+ accentColor: (_mb = (_lb = readString(env, "NEXT_PUBLIC_ACCENT_COLOR")) != null ? _lb : readString(env, "UI_ACCENT_COLOR")) != null ? _mb : "#3b82f6",
3128
+ logoUrl: (_nb = readString(env, "NEXT_PUBLIC_LOGO_URL")) != null ? _nb : readString(env, "UI_LOGO_URL"),
3129
+ placeholder: (_pb = (_ob = readString(env, "NEXT_PUBLIC_PLACEHOLDER")) != null ? _ob : readString(env, "UI_PLACEHOLDER")) != null ? _pb : "Ask me anything\u2026",
3130
+ showSources: ((_rb = (_qb = readString(env, "NEXT_PUBLIC_SHOW_SOURCES")) != null ? _qb : readString(env, "UI_SHOW_SOURCES")) != null ? _rb : "true") !== "false",
3131
+ welcomeMessage: (_tb = (_sb = readString(env, "NEXT_PUBLIC_WELCOME_MESSAGE")) != null ? _sb : readString(env, "UI_WELCOME_MESSAGE")) != null ? _tb : "Hello! I'm your AI assistant. Ask me anything about your documents.",
3132
+ visualStyle: (_vb = (_ub = readString(env, "NEXT_PUBLIC_UI_VISUAL_STYLE")) != null ? _ub : readString(env, "UI_VISUAL_STYLE")) != null ? _vb : "glass",
3133
+ borderRadius: (_xb = (_wb = readString(env, "NEXT_PUBLIC_UI_BORDER_RADIUS")) != null ? _wb : readString(env, "UI_BORDER_RADIUS")) != null ? _xb : "xl",
3134
+ allowUpload: ((_zb = (_yb = readString(env, "NEXT_PUBLIC_ALLOW_UPLOAD")) != null ? _yb : readString(env, "UI_ALLOW_UPLOAD")) != null ? _zb : "false") === "true"
3133
3135
  },
3134
3136
  rag: {
3135
- topK: readNumber(env, "RAG_TOP_K", (_Eb = rt == null ? void 0 : rt.topK) != null ? _Eb : 5),
3137
+ topK: readNumber(env, "RAG_TOP_K", (_Ab = rt == null ? void 0 : rt.topK) != null ? _Ab : 5),
3136
3138
  scoreThreshold: readNumber(env, "RAG_SCORE_THRESHOLD", 0),
3137
- chunkSize: readNumber(env, "RAG_CHUNK_SIZE", (_Fb = rt == null ? void 0 : rt.chunkSize) != null ? _Fb : 1e3),
3138
- chunkOverlap: readNumber(env, "RAG_CHUNK_OVERLAP", (_Gb = rt == null ? void 0 : rt.chunkOverlap) != null ? _Gb : 200),
3139
- filterableFields: (_Hb = readString(env, "RAG_FILTERABLE_FIELDS")) == null ? void 0 : _Hb.split(",").map((f) => f.trim()),
3139
+ chunkSize: readNumber(env, "RAG_CHUNK_SIZE", (_Bb = rt == null ? void 0 : rt.chunkSize) != null ? _Bb : 1e3),
3140
+ chunkOverlap: readNumber(env, "RAG_CHUNK_OVERLAP", (_Cb = rt == null ? void 0 : rt.chunkOverlap) != null ? _Cb : 200),
3141
+ filterableFields: (_Db = readString(env, "RAG_FILTERABLE_FIELDS")) == null ? void 0 : _Db.split(",").map((f) => f.trim()),
3140
3142
  // Query pipeline toggles — read from .env.local
3141
3143
  useQueryTransformation: readString(env, "RAG_USE_QUERY_TRANSFORMATION") === "true",
3142
3144
  useReranking: readString(env, "RAG_USE_RERANKING") === "true",
3143
3145
  useGraphRetrieval: readString(env, "RAG_USE_GRAPH_RETRIEVAL") === "true",
3144
- architecture: (_Ib = readString(env, "RAG_ARCHITECTURE")) != null ? _Ib : "simple",
3145
- chunkingStrategy: (_Jb = readString(env, "RAG_CHUNKING_STRATEGY")) != null ? _Jb : "recursive",
3146
+ architecture: (_Eb = readString(env, "RAG_ARCHITECTURE")) != null ? _Eb : "simple",
3147
+ chunkingStrategy: (_Fb = readString(env, "RAG_CHUNKING_STRATEGY")) != null ? _Fb : "recursive",
3146
3148
  uiMapping: (() => {
3147
3149
  const raw = readString(env, "RAG_UI_MAPPING");
3148
3150
  if (!raw) return void 0;
@@ -3155,7 +3157,7 @@ function getEnvConfig(env = process.env, base) {
3155
3157
  },
3156
3158
  telemetry: {
3157
3159
  enabled: telemetryEnabled,
3158
- url: (_Nb = (_Mb = (_Kb = readString(env, "TELEMETRY_URL")) != null ? _Kb : readString(env, "NEXT_PUBLIC_TELEMETRY_URL")) != null ? _Mb : (_Lb = base == null ? void 0 : base.telemetry) == null ? void 0 : _Lb.url) != null ? _Nb : "https://www.retrivora.com/api/telemetry"
3160
+ url: (_Jb = (_Ib = (_Gb = readString(env, "TELEMETRY_URL")) != null ? _Gb : readString(env, "NEXT_PUBLIC_TELEMETRY_URL")) != null ? _Ib : (_Hb = base == null ? void 0 : base.telemetry) == null ? void 0 : _Hb.url) != null ? _Jb : "https://www.retrivora.com/api/telemetry"
3159
3161
  }
3160
3162
  }, readString(env, "GRAPH_DB_PROVIDER") ? {
3161
3163
  graphDb: {
@@ -3168,8 +3170,8 @@ function getEnvConfig(env = process.env, base) {
3168
3170
  }
3169
3171
  } : {}), {
3170
3172
  mcpServers: (() => {
3171
- var _a4;
3172
- const raw = (_a4 = readString(env, "MCP_SERVERS")) != null ? _a4 : readString(env, "NEXT_PUBLIC_MCP_SERVERS");
3173
+ var _a3;
3174
+ const raw = (_a3 = readString(env, "MCP_SERVERS")) != null ? _a3 : readString(env, "NEXT_PUBLIC_MCP_SERVERS");
3173
3175
  if (!raw) return void 0;
3174
3176
  try {
3175
3177
  return JSON.parse(raw);
@@ -3215,10 +3217,10 @@ var ConfigResolver = class {
3215
3217
  * fallback behavior.
3216
3218
  */
3217
3219
  static resolveUniversal(hostConfig, env = process.env) {
3218
- var _a3;
3220
+ var _a2;
3219
3221
  if (!hostConfig) return this.resolve(void 0, env);
3220
3222
  const normalized = __spreadProps(__spreadValues({}, hostConfig), {
3221
- vectorDb: (_a3 = hostConfig.vectorDb) != null ? _a3 : hostConfig.vectorDatabase,
3223
+ vectorDb: (_a2 = hostConfig.vectorDb) != null ? _a2 : hostConfig.vectorDatabase,
3222
3224
  rag: this.mergeRetrievalWorkflow(hostConfig.rag, hostConfig.retrieval, hostConfig.workflow)
3223
3225
  });
3224
3226
  return this.resolve(normalized, env);
@@ -3238,11 +3240,11 @@ var ConfigResolver = class {
3238
3240
  }
3239
3241
  }
3240
3242
  static mergeRetrievalWorkflow(rag, retrieval, workflow) {
3241
- var _a3, _b, _c, _d, _e, _f;
3243
+ var _a2, _b, _c, _d, _e, _f;
3242
3244
  if (!rag && !retrieval && !workflow) return void 0;
3243
3245
  const normalized = __spreadValues({}, rag != null ? rag : {});
3244
3246
  if (retrieval) {
3245
- normalized.topK = (_a3 = retrieval.topK) != null ? _a3 : normalized.topK;
3247
+ normalized.topK = (_a2 = retrieval.topK) != null ? _a2 : normalized.topK;
3246
3248
  normalized.scoreThreshold = (_b = retrieval.scoreThreshold) != null ? _b : normalized.scoreThreshold;
3247
3249
  normalized.useReranking = (_c = retrieval.useReranking) != null ? _c : normalized.useReranking;
3248
3250
  if (retrieval.strategy === "hybrid") normalized.architecture = (_d = normalized.architecture) != null ? _d : "hybrid";
@@ -3370,8 +3372,8 @@ var OpenAIProvider = class {
3370
3372
  };
3371
3373
  }
3372
3374
  async chat(messages, context, options) {
3373
- var _a3, _b, _c, _d, _e, _f, _g2, _h, _i;
3374
- const basePrompt = (_b = (_a3 = options == null ? void 0 : options.systemPrompt) != null ? _a3 : this.llmConfig.systemPrompt) != null ? _b : "You are a helpful assistant. Answer questions based on the provided context.";
3375
+ var _a2, _b, _c, _d, _e, _f, _g2, _h, _i;
3376
+ const basePrompt = (_b = (_a2 = options == null ? void 0 : options.systemPrompt) != null ? _a2 : this.llmConfig.systemPrompt) != null ? _b : "You are a helpful assistant. Answer questions based on the provided context.";
3375
3377
  const systemMessage = {
3376
3378
  role: "system",
3377
3379
  content: buildSystemContent(basePrompt, context)
@@ -3395,8 +3397,8 @@ var OpenAIProvider = class {
3395
3397
  }
3396
3398
  chatStream(messages, context, options) {
3397
3399
  return __asyncGenerator(this, null, function* () {
3398
- var _a3, _b, _c, _d, _e, _f, _g2, _h;
3399
- const basePrompt = (_b = (_a3 = options == null ? void 0 : options.systemPrompt) != null ? _a3 : this.llmConfig.systemPrompt) != null ? _b : "You are a helpful assistant. Answer questions based on the provided context.";
3400
+ var _a2, _b, _c, _d, _e, _f, _g2, _h;
3401
+ const basePrompt = (_b = (_a2 = options == null ? void 0 : options.systemPrompt) != null ? _a2 : this.llmConfig.systemPrompt) != null ? _b : "You are a helpful assistant. Answer questions based on the provided context.";
3400
3402
  const systemMessage = {
3401
3403
  role: "system",
3402
3404
  content: buildSystemContent(basePrompt, context)
@@ -3443,8 +3445,8 @@ var OpenAIProvider = class {
3443
3445
  return results[0];
3444
3446
  }
3445
3447
  async batchEmbed(texts, options) {
3446
- var _a3, _b, _c, _d, _e;
3447
- const model = (_c = (_b = options == null ? void 0 : options.model) != null ? _b : (_a3 = this.embeddingConfig) == null ? void 0 : _a3.model) != null ? _c : "text-embedding-3-small";
3448
+ var _a2, _b, _c, _d, _e;
3449
+ const model = (_c = (_b = options == null ? void 0 : options.model) != null ? _b : (_a2 = this.embeddingConfig) == null ? void 0 : _a2.model) != null ? _c : "text-embedding-3-small";
3448
3450
  const apiKey = (_e = (_d = this.embeddingConfig) == null ? void 0 : _d.apiKey) != null ? _e : this.llmConfig.apiKey;
3449
3451
  const client = apiKey !== this.llmConfig.apiKey ? new OpenAI({ apiKey }) : this.client;
3450
3452
  const response = await client.embeddings.create({ model, input: texts });
@@ -3503,11 +3505,7 @@ var AnthropicProvider = class {
3503
3505
  try {
3504
3506
  const { default: Anthropic2 } = await import("@anthropic-ai/sdk");
3505
3507
  const client = new Anthropic2({ apiKey });
3506
- await client.messages.create({
3507
- model: modelName,
3508
- max_tokens: 10,
3509
- messages: [{ role: "user", content: "ping" }]
3510
- });
3508
+ await client.models.list();
3511
3509
  return { healthy: true, provider: "anthropic", capabilities: { model: modelName }, timestamp };
3512
3510
  } catch (error) {
3513
3511
  return {
@@ -3521,8 +3519,8 @@ var AnthropicProvider = class {
3521
3519
  };
3522
3520
  }
3523
3521
  async chat(messages, context, options) {
3524
- var _a3, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k;
3525
- const basePrompt = (_b = (_a3 = options == null ? void 0 : options.systemPrompt) != null ? _a3 : this.llmConfig.systemPrompt) != null ? _b : "You are a helpful assistant. Use the context below to answer the user's question accurately.";
3522
+ var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k;
3523
+ const basePrompt = (_b = (_a2 = options == null ? void 0 : options.systemPrompt) != null ? _a2 : this.llmConfig.systemPrompt) != null ? _b : "You are a helpful assistant. Use the context below to answer the user's question accurately.";
3526
3524
  const system = buildSystemContent(basePrompt, context);
3527
3525
  const anthropicMessages = messages.map((m) => ({
3528
3526
  role: m.role === "assistant" ? "assistant" : "user",
@@ -3560,8 +3558,8 @@ var AnthropicProvider = class {
3560
3558
  }
3561
3559
  chatStream(messages, context, options) {
3562
3560
  return __asyncGenerator(this, null, function* () {
3563
- var _a3, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k;
3564
- const basePrompt = (_b = (_a3 = options == null ? void 0 : options.systemPrompt) != null ? _a3 : this.llmConfig.systemPrompt) != null ? _b : "You are a helpful assistant. Use the context below to answer the user's question accurately.";
3561
+ var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k;
3562
+ const basePrompt = (_b = (_a2 = options == null ? void 0 : options.systemPrompt) != null ? _a2 : this.llmConfig.systemPrompt) != null ? _b : "You are a helpful assistant. Use the context below to answer the user's question accurately.";
3565
3563
  const system = buildSystemContent(basePrompt, context);
3566
3564
  const anthropicMessages = messages.map((m) => ({
3567
3565
  role: m.role === "assistant" ? "assistant" : "user",
@@ -3645,8 +3643,8 @@ var AnthropicProvider = class {
3645
3643
  import axios from "axios";
3646
3644
  var OllamaProvider = class {
3647
3645
  constructor(llmConfig, embeddingConfig) {
3648
- var _a3, _b, _c;
3649
- const rawBaseURL = ((_a3 = llmConfig.baseUrl) != null ? _a3 : "http://localhost:11434").replace(/\/+$/, "");
3646
+ var _a2, _b, _c;
3647
+ const rawBaseURL = ((_a2 = llmConfig.baseUrl) != null ? _a2 : "http://localhost:11434").replace(/\/+$/, "");
3650
3648
  const baseURL = rawBaseURL.replace(/\/(?:api\/v1|v1|api)$/i, "");
3651
3649
  const timeout = Number((_b = llmConfig.options) == null ? void 0 : _b.timeout) || 3e5;
3652
3650
  const headers = ((_c = llmConfig.options) == null ? void 0 : _c.headers) || {};
@@ -3706,8 +3704,8 @@ var OllamaProvider = class {
3706
3704
  };
3707
3705
  }
3708
3706
  async chat(messages, context, options) {
3709
- var _a3, _b, _c, _d, _e, _f;
3710
- const basePrompt = (_b = (_a3 = options == null ? void 0 : options.systemPrompt) != null ? _a3 : this.llmConfig.systemPrompt) != null ? _b : "You are a helpful assistant. Use the provided context to answer the user's question.";
3707
+ var _a2, _b, _c, _d, _e, _f;
3708
+ const basePrompt = (_b = (_a2 = options == null ? void 0 : options.systemPrompt) != null ? _a2 : this.llmConfig.systemPrompt) != null ? _b : "You are a helpful assistant. Use the provided context to answer the user's question.";
3711
3709
  const system = buildSystemContent(basePrompt, context);
3712
3710
  const { data } = await this.http.post("/api/chat", {
3713
3711
  model: this.llmConfig.model,
@@ -3725,8 +3723,8 @@ var OllamaProvider = class {
3725
3723
  }
3726
3724
  chatStream(messages, context, options) {
3727
3725
  return __asyncGenerator(this, null, function* () {
3728
- var _a3, _b, _c, _d, _e, _f, _g2, _h;
3729
- const basePrompt = (_b = (_a3 = options == null ? void 0 : options.systemPrompt) != null ? _a3 : this.llmConfig.systemPrompt) != null ? _b : "You are a helpful assistant. Use the provided context to answer the user's question.";
3726
+ var _a2, _b, _c, _d, _e, _f, _g2, _h;
3727
+ const basePrompt = (_b = (_a2 = options == null ? void 0 : options.systemPrompt) != null ? _a2 : this.llmConfig.systemPrompt) != null ? _b : "You are a helpful assistant. Use the provided context to answer the user's question.";
3730
3728
  const system = buildSystemContent(basePrompt, context);
3731
3729
  const response = yield new __await(this.http.post("/api/chat", {
3732
3730
  model: this.llmConfig.model,
@@ -3784,8 +3782,8 @@ var OllamaProvider = class {
3784
3782
  });
3785
3783
  }
3786
3784
  async embed(text, options) {
3787
- var _a3, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k;
3788
- const model = (_c = (_b = options == null ? void 0 : options.model) != null ? _b : (_a3 = this.embeddingConfig) == null ? void 0 : _a3.model) != null ? _c : "nomic-embed-text";
3785
+ var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k;
3786
+ const model = (_c = (_b = options == null ? void 0 : options.model) != null ? _b : (_a2 = this.embeddingConfig) == null ? void 0 : _a2.model) != null ? _c : "nomic-embed-text";
3789
3787
  const baseURL = (_f = (_e = (_d = this.embeddingConfig) == null ? void 0 : _d.baseUrl) != null ? _e : this.llmConfig.baseUrl) != null ? _f : "http://localhost:11434";
3790
3788
  const client = baseURL !== ((_g2 = this.llmConfig.baseUrl) != null ? _g2 : "http://localhost:11434") ? axios.create({ baseURL, timeout: 6e4 }) : this.http;
3791
3789
  let prompt = text;
@@ -3886,9 +3884,9 @@ var GeminiProvider = class {
3886
3884
  static getHealthChecker() {
3887
3885
  return {
3888
3886
  async check(config) {
3889
- var _a3, _b;
3887
+ var _a2, _b;
3890
3888
  const timestamp = Date.now();
3891
- const apiKey = (_b = (_a3 = config.apiKey) != null ? _a3 : process.env.GOOGLE_GENAI_API_KEY) != null ? _b : "";
3889
+ const apiKey = (_b = (_a2 = config.apiKey) != null ? _a2 : process.env.GOOGLE_GENAI_API_KEY) != null ? _b : "";
3892
3890
  const modelName = sanitizeModel(config.model);
3893
3891
  try {
3894
3892
  const { GoogleGenerativeAI: GoogleGenerativeAI2 } = await import("@google/generative-ai");
@@ -3918,16 +3916,16 @@ var GeminiProvider = class {
3918
3916
  /** Resolve the embedding client — uses a separate client when the embedding
3919
3917
  * API key differs from the LLM API key. */
3920
3918
  get embeddingClient() {
3921
- var _a3;
3922
- if (((_a3 = this.embeddingConfig) == null ? void 0 : _a3.apiKey) && this.embeddingConfig.apiKey !== this.llmConfig.apiKey) {
3919
+ var _a2;
3920
+ if (((_a2 = this.embeddingConfig) == null ? void 0 : _a2.apiKey) && this.embeddingConfig.apiKey !== this.llmConfig.apiKey) {
3923
3921
  return buildClient(this.embeddingConfig.apiKey);
3924
3922
  }
3925
3923
  return this.client;
3926
3924
  }
3927
3925
  /** Resolve the embedding model to use, in order of specificity. */
3928
3926
  resolveEmbeddingModel(optionsModel) {
3929
- var _a3, _b;
3930
- return sanitizeModel((_b = optionsModel != null ? optionsModel : (_a3 = this.embeddingConfig) == null ? void 0 : _a3.model) != null ? _b : DEFAULT_EMBEDDING_MODEL);
3927
+ var _a2, _b;
3928
+ return sanitizeModel((_b = optionsModel != null ? optionsModel : (_a2 = this.embeddingConfig) == null ? void 0 : _a2.model) != null ? _b : DEFAULT_EMBEDDING_MODEL);
3931
3929
  }
3932
3930
  /**
3933
3931
  * Convert ChatMessage[] to the Gemini contents format.
@@ -3947,8 +3945,8 @@ var GeminiProvider = class {
3947
3945
  // ILLMProvider — chat
3948
3946
  // -------------------------------------------------------------------------
3949
3947
  async chat(messages, context, options) {
3950
- var _a3, _b, _c, _d, _e, _f;
3951
- const basePrompt = (_b = (_a3 = options == null ? void 0 : options.systemPrompt) != null ? _a3 : this.llmConfig.systemPrompt) != null ? _b : "You are a helpful assistant. Answer questions based on the provided context.";
3948
+ var _a2, _b, _c, _d, _e, _f;
3949
+ const basePrompt = (_b = (_a2 = options == null ? void 0 : options.systemPrompt) != null ? _a2 : this.llmConfig.systemPrompt) != null ? _b : "You are a helpful assistant. Answer questions based on the provided context.";
3952
3950
  const model = this.client.getGenerativeModel({
3953
3951
  model: this.llmConfig.model,
3954
3952
  systemInstruction: buildSystemContent(basePrompt, context)
@@ -3965,8 +3963,8 @@ var GeminiProvider = class {
3965
3963
  }
3966
3964
  chatStream(messages, context, options) {
3967
3965
  return __asyncGenerator(this, null, function* () {
3968
- var _a3, _b, _c, _d, _e, _f;
3969
- const basePrompt = (_b = (_a3 = options == null ? void 0 : options.systemPrompt) != null ? _a3 : this.llmConfig.systemPrompt) != null ? _b : "You are a helpful assistant. Answer questions based on the provided context.";
3966
+ var _a2, _b, _c, _d, _e, _f;
3967
+ const basePrompt = (_b = (_a2 = options == null ? void 0 : options.systemPrompt) != null ? _a2 : this.llmConfig.systemPrompt) != null ? _b : "You are a helpful assistant. Answer questions based on the provided context.";
3970
3968
  const model = this.client.getGenerativeModel({
3971
3969
  model: this.llmConfig.model,
3972
3970
  systemInstruction: buildSystemContent(basePrompt, context)
@@ -4001,11 +3999,11 @@ var GeminiProvider = class {
4001
3999
  // ILLMProvider — embeddings
4002
4000
  // -------------------------------------------------------------------------
4003
4001
  async embed(text, options) {
4004
- var _a3, _b;
4002
+ var _a2, _b;
4005
4003
  const content = applyPrefix(
4006
4004
  text,
4007
4005
  options == null ? void 0 : options.taskType,
4008
- (_a3 = this.embeddingConfig) == null ? void 0 : _a3.queryPrefix,
4006
+ (_a2 = this.embeddingConfig) == null ? void 0 : _a2.queryPrefix,
4009
4007
  (_b = this.embeddingConfig) == null ? void 0 : _b.documentPrefix
4010
4008
  );
4011
4009
  const modelName = this.resolveEmbeddingModel(options == null ? void 0 : options.model);
@@ -4022,7 +4020,7 @@ var GeminiProvider = class {
4022
4020
  const modelName = this.resolveEmbeddingModel(options == null ? void 0 : options.model);
4023
4021
  const model = this.embeddingClient.getGenerativeModel({ model: modelName });
4024
4022
  const requests = texts.map((text) => {
4025
- var _a3, _b;
4023
+ var _a2, _b;
4026
4024
  return {
4027
4025
  content: {
4028
4026
  role: "user",
@@ -4030,7 +4028,7 @@ var GeminiProvider = class {
4030
4028
  text: applyPrefix(
4031
4029
  text,
4032
4030
  options == null ? void 0 : options.taskType,
4033
- (_a3 = this.embeddingConfig) == null ? void 0 : _a3.queryPrefix,
4031
+ (_a2 = this.embeddingConfig) == null ? void 0 : _a2.queryPrefix,
4034
4032
  (_b = this.embeddingConfig) == null ? void 0 : _b.documentPrefix
4035
4033
  )
4036
4034
  }]
@@ -4047,8 +4045,8 @@ var GeminiProvider = class {
4047
4045
  throw err;
4048
4046
  });
4049
4047
  return response.embeddings.map((e) => {
4050
- var _a3;
4051
- return (_a3 = e.values) != null ? _a3 : [];
4048
+ var _a2;
4049
+ return (_a2 = e.values) != null ? _a2 : [];
4052
4050
  });
4053
4051
  }
4054
4052
  // -------------------------------------------------------------------------
@@ -4137,8 +4135,8 @@ var GroqProvider = class {
4137
4135
  };
4138
4136
  }
4139
4137
  async chat(messages, context, options) {
4140
- var _a3, _b, _c, _d, _e, _f, _g2, _h, _i;
4141
- const basePrompt = (_b = (_a3 = options == null ? void 0 : options.systemPrompt) != null ? _a3 : this.llmConfig.systemPrompt) != null ? _b : "You are a helpful assistant. Answer questions based on the provided context.";
4138
+ var _a2, _b, _c, _d, _e, _f, _g2, _h, _i;
4139
+ const basePrompt = (_b = (_a2 = options == null ? void 0 : options.systemPrompt) != null ? _a2 : this.llmConfig.systemPrompt) != null ? _b : "You are a helpful assistant. Answer questions based on the provided context.";
4142
4140
  const systemMessage = {
4143
4141
  role: "system",
4144
4142
  content: buildSystemContent(basePrompt, context)
@@ -4162,8 +4160,8 @@ var GroqProvider = class {
4162
4160
  }
4163
4161
  chatStream(messages, context, options) {
4164
4162
  return __asyncGenerator(this, null, function* () {
4165
- var _a3, _b, _c, _d, _e, _f, _g2, _h;
4166
- const basePrompt = (_b = (_a3 = options == null ? void 0 : options.systemPrompt) != null ? _a3 : this.llmConfig.systemPrompt) != null ? _b : "You are a helpful assistant. Answer questions based on the provided context.";
4163
+ var _a2, _b, _c, _d, _e, _f, _g2, _h;
4164
+ const basePrompt = (_b = (_a2 = options == null ? void 0 : options.systemPrompt) != null ? _a2 : this.llmConfig.systemPrompt) != null ? _b : "You are a helpful assistant. Answer questions based on the provided context.";
4167
4165
  const systemMessage = {
4168
4166
  role: "system",
4169
4167
  content: buildSystemContent(basePrompt, context)
@@ -4297,8 +4295,8 @@ var QwenProvider = class {
4297
4295
  };
4298
4296
  }
4299
4297
  async chat(messages, context, options) {
4300
- var _a3, _b, _c, _d, _e, _f, _g2, _h, _i;
4301
- const basePrompt = (_b = (_a3 = options == null ? void 0 : options.systemPrompt) != null ? _a3 : this.llmConfig.systemPrompt) != null ? _b : "You are a helpful assistant. Answer questions based on the provided context.";
4298
+ var _a2, _b, _c, _d, _e, _f, _g2, _h, _i;
4299
+ const basePrompt = (_b = (_a2 = options == null ? void 0 : options.systemPrompt) != null ? _a2 : this.llmConfig.systemPrompt) != null ? _b : "You are a helpful assistant. Answer questions based on the provided context.";
4302
4300
  const systemMessage = {
4303
4301
  role: "system",
4304
4302
  content: buildSystemContent(basePrompt, context)
@@ -4322,8 +4320,8 @@ var QwenProvider = class {
4322
4320
  }
4323
4321
  chatStream(messages, context, options) {
4324
4322
  return __asyncGenerator(this, null, function* () {
4325
- var _a3, _b, _c, _d, _e, _f, _g2, _h;
4326
- const basePrompt = (_b = (_a3 = options == null ? void 0 : options.systemPrompt) != null ? _a3 : this.llmConfig.systemPrompt) != null ? _b : "You are a helpful assistant. Answer questions based on the provided context.";
4323
+ var _a2, _b, _c, _d, _e, _f, _g2, _h;
4324
+ const basePrompt = (_b = (_a2 = options == null ? void 0 : options.systemPrompt) != null ? _a2 : this.llmConfig.systemPrompt) != null ? _b : "You are a helpful assistant. Answer questions based on the provided context.";
4327
4325
  const systemMessage = {
4328
4326
  role: "system",
4329
4327
  content: buildSystemContent(basePrompt, context)
@@ -4370,8 +4368,8 @@ var QwenProvider = class {
4370
4368
  return results[0];
4371
4369
  }
4372
4370
  async batchEmbed(texts, options) {
4373
- var _a3, _b, _c, _d, _e, _f;
4374
- const model = (_c = (_b = options == null ? void 0 : options.model) != null ? _b : (_a3 = this.embeddingConfig) == null ? void 0 : _a3.model) != null ? _c : "text-embedding-v1";
4371
+ var _a2, _b, _c, _d, _e, _f;
4372
+ const model = (_c = (_b = options == null ? void 0 : options.model) != null ? _b : (_a2 = this.embeddingConfig) == null ? void 0 : _a2.model) != null ? _c : "text-embedding-v1";
4375
4373
  const apiKey = (_e = (_d = this.embeddingConfig) == null ? void 0 : _d.apiKey) != null ? _e : this.llmConfig.apiKey;
4376
4374
  const client = apiKey !== this.llmConfig.apiKey ? new OpenAI3({
4377
4375
  apiKey,
@@ -4432,9 +4430,9 @@ var LLM_PROFILES = {
4432
4430
 
4433
4431
  // src/llm/providers/UniversalLLMAdapter.ts
4434
4432
  function extractContent(obj) {
4435
- var _a3, _b, _c;
4433
+ var _a2, _b, _c;
4436
4434
  if (!obj || typeof obj !== "object") return void 0;
4437
- const choice = (_a3 = obj.choices) == null ? void 0 : _a3[0];
4435
+ const choice = (_a2 = obj.choices) == null ? void 0 : _a2[0];
4438
4436
  if (!choice) return void 0;
4439
4437
  if (typeof ((_b = choice.message) == null ? void 0 : _b.content) === "string") return choice.message.content;
4440
4438
  if (typeof ((_c = choice.delta) == null ? void 0 : _c.content) === "string") return choice.delta.content;
@@ -4444,10 +4442,10 @@ function extractContent(obj) {
4444
4442
  }
4445
4443
  var UniversalLLMAdapter = class {
4446
4444
  constructor(config) {
4447
- var _a3, _b, _c, _d, _e, _f, _g2, _h;
4445
+ var _a2, _b, _c, _d, _e, _f, _g2, _h;
4448
4446
  this.model = config.model;
4449
4447
  const llmConfig = config;
4450
- const options = (_a3 = llmConfig.options) != null ? _a3 : {};
4448
+ const options = (_a2 = llmConfig.options) != null ? _a2 : {};
4451
4449
  let profile = {};
4452
4450
  if (typeof options.profile === "string") {
4453
4451
  profile = LLM_PROFILES[options.profile] || {};
@@ -4473,8 +4471,8 @@ var UniversalLLMAdapter = class {
4473
4471
  });
4474
4472
  }
4475
4473
  async chat(messages, context) {
4476
- var _a3, _b, _c, _d, _e, _f, _g2, _h, _i, _j;
4477
- const path2 = (_a3 = this.opts.chatPath) != null ? _a3 : "/chat/completions";
4474
+ var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j;
4475
+ const path2 = (_a2 = this.opts.chatPath) != null ? _a2 : "/chat/completions";
4478
4476
  const safeMessages = (Array.isArray(messages) ? messages : []).filter((m) => Boolean(m && typeof m === "object")).map((m) => ({
4479
4477
  role: m.role || "user",
4480
4478
  content: typeof m.content === "string" ? m.content : m.content != null ? String(m.content) : ""
@@ -4551,8 +4549,8 @@ ${context != null ? context : "None"}` },
4551
4549
  */
4552
4550
  chatStream(messages, context) {
4553
4551
  return __asyncGenerator(this, null, function* () {
4554
- var _a3, _b, _c, _d, _e, _f;
4555
- const path2 = (_a3 = this.opts.chatPath) != null ? _a3 : "/chat/completions";
4552
+ var _a2, _b, _c, _d, _e, _f;
4553
+ const path2 = (_a2 = this.opts.chatPath) != null ? _a2 : "/chat/completions";
4556
4554
  const url = `${this.baseUrl.replace(/\/$/, "")}${path2}`;
4557
4555
  const extractPath = ((_b = this.opts.responseExtractPath) != null ? _b : "choices[0].message.content").replace("message.content", "delta.content");
4558
4556
  const safeMessages = (Array.isArray(messages) ? messages : []).filter((m) => Boolean(m && typeof m === "object")).map((m) => ({
@@ -4678,8 +4676,8 @@ ${context != null ? context : "None"}` },
4678
4676
  });
4679
4677
  }
4680
4678
  async embed(text) {
4681
- var _a3, _b, _c, _d, _e, _f, _g2, _h;
4682
- const path2 = (_a3 = this.opts.embedPath) != null ? _a3 : "/embeddings";
4679
+ var _a2, _b, _c, _d, _e, _f, _g2, _h;
4680
+ const path2 = (_a2 = this.opts.embedPath) != null ? _a2 : "/embeddings";
4683
4681
  const payload = this.opts.embedPayloadTemplate ? buildPayload(this.opts.embedPayloadTemplate, { input: text, model: this.model }) : { input: text, model: this.model };
4684
4682
  try {
4685
4683
  const { data } = await this.http.post(path2, payload);
@@ -4803,7 +4801,7 @@ var LLMFactory = class _LLMFactory {
4803
4801
  ];
4804
4802
  }
4805
4803
  static create(llmConfig, embeddingConfig) {
4806
- var _a3, _b, _c;
4804
+ var _a2, _b, _c;
4807
4805
  switch (llmConfig.provider) {
4808
4806
  case "openai":
4809
4807
  return new OpenAIProvider(llmConfig, embeddingConfig);
@@ -4822,7 +4820,7 @@ var LLMFactory = class _LLMFactory {
4822
4820
  case "custom":
4823
4821
  return new UniversalLLMAdapter(llmConfig);
4824
4822
  default: {
4825
- const providerName = String((_a3 = llmConfig.provider) != null ? _a3 : "").toLowerCase();
4823
+ const providerName = String((_a2 = llmConfig.provider) != null ? _a2 : "").toLowerCase();
4826
4824
  const customFactory = customProviders.get(providerName);
4827
4825
  if (customFactory) {
4828
4826
  return customFactory(llmConfig);
@@ -4924,7 +4922,7 @@ var ProviderRegistry = class {
4924
4922
  return null;
4925
4923
  }
4926
4924
  static async loadVectorProviderClass(provider) {
4927
- var _a3;
4925
+ var _a2;
4928
4926
  if (this.vectorProviders[provider]) return this.vectorProviders[provider];
4929
4927
  switch (provider) {
4930
4928
  case "pinecone": {
@@ -4933,7 +4931,7 @@ var ProviderRegistry = class {
4933
4931
  }
4934
4932
  case "pgvector":
4935
4933
  case "postgresql": {
4936
- const postgresMode = ((_a3 = process.env.POSTGRES_MODE) != null ? _a3 : "multi").toLowerCase();
4934
+ const postgresMode = ((_a2 = process.env.POSTGRES_MODE) != null ? _a2 : "multi").toLowerCase();
4937
4935
  if (postgresMode === "single") {
4938
4936
  const { PostgreSQLProvider: PostgreSQLProvider2 } = await Promise.resolve().then(() => (init_PostgreSQLProvider(), PostgreSQLProvider_exports));
4939
4937
  return PostgreSQLProvider2;
@@ -5225,13 +5223,15 @@ var DocumentChunker = class {
5225
5223
  }
5226
5224
  }
5227
5225
  const parts = text.split(separator);
5228
- for (const part of parts) {
5229
- if (part.length <= chunkSize) {
5230
- finalSplits.push(part + (part === parts[parts.length - 1] ? "" : separator));
5226
+ for (let i = 0; i < parts.length; i++) {
5227
+ const part = parts[i];
5228
+ const splitVal = (i === 0 ? "" : separator) + part;
5229
+ if (splitVal.length <= chunkSize) {
5230
+ finalSplits.push(splitVal);
5231
5231
  } else if (nextSeparators.length > 0) {
5232
- finalSplits.push(...this.recursiveSplit(part, nextSeparators, chunkSize));
5232
+ finalSplits.push(...this.recursiveSplit(splitVal, nextSeparators, chunkSize));
5233
5233
  } else {
5234
- finalSplits.push(part);
5234
+ finalSplits.push(splitVal);
5235
5235
  }
5236
5236
  }
5237
5237
  return finalSplits;
@@ -5310,74 +5310,108 @@ Text to extract from:
5310
5310
  };
5311
5311
 
5312
5312
  // src/rag/Reranker.ts
5313
+ var MAX_RERANK_CONTENT_CHARS = 64e3;
5313
5314
  var Reranker = class {
5314
5315
  /**
5315
5316
  * Re-ranks matches based on a secondary relevance score using an LLM.
5317
+ *
5318
+ * Fix RAG-2: keyword re-ranking no longer mutates the original `score`
5319
+ * field (which consumers interpret as the raw vector similarity). A new
5320
+ * `rerankScore` field is added so both the original similarity and the
5321
+ * boosted rank signal are available to callers.
5316
5322
  */
5317
5323
  async rerank(matches, query, limit = 5, llm) {
5318
5324
  if (!llm || matches.length <= 1) {
5319
- const keywords = query.toLowerCase().replace(/[^\w\s]/g, "").split(/\s+/).filter((w) => w.length > 2);
5320
- if (keywords.length === 0) {
5321
- return matches.sort((a, b) => b.score - a.score).slice(0, limit);
5322
- }
5323
- const scoredMatches = matches.map((match) => {
5324
- var _a3;
5325
- const contentLower = ((_a3 = match == null ? void 0 : match.content) != null ? _a3 : "").toLowerCase();
5326
- let keywordScore = 0;
5327
- keywords.forEach((keyword) => {
5328
- if (contentLower.includes(keyword)) {
5329
- keywordScore += 1;
5330
- }
5331
- });
5332
- const normalizedKeywordScore = keywordScore / keywords.length;
5333
- const combinedScore = match.score + normalizedKeywordScore * 0.5;
5334
- return __spreadProps(__spreadValues({}, match), {
5335
- score: combinedScore
5336
- });
5337
- });
5338
- return scoredMatches.sort((a, b) => b.score - a.score).slice(0, limit);
5325
+ return this.keywordRerank(matches, query, limit);
5339
5326
  }
5340
5327
  try {
5341
- const topN = matches.slice(0, 10);
5342
- const response = await llm.chat(
5343
- [{ role: "user", content: `Query: "${query}"
5328
+ return await this.llmRerank(matches, query, limit, llm);
5329
+ } catch (error) {
5330
+ console.warn("[Reranker] LLM re-ranking failed, falling back to keyword sorting:", error);
5331
+ return this.keywordRerank(matches, query, limit);
5332
+ }
5333
+ }
5334
+ /**
5335
+ * Keyword-based re-ranking fallback.
5336
+ *
5337
+ * Fix RAG-2: `rerankScore` is a separate field; `score` (the original
5338
+ * vector similarity) is never mutated. Callers that display `score` to
5339
+ * end-users receive the canonical cosine similarity value.
5340
+ */
5341
+ keywordRerank(matches, query, limit) {
5342
+ const keywords = query.toLowerCase().replace(/[^\w\s]/g, "").split(/\s+/).filter((w) => w.length > 2);
5343
+ if (keywords.length === 0) {
5344
+ return matches.sort((a, b) => b.score - a.score).slice(0, limit);
5345
+ }
5346
+ const scoredMatches = matches.map((match) => {
5347
+ var _a2;
5348
+ const contentLower = ((_a2 = match == null ? void 0 : match.content) != null ? _a2 : "").toLowerCase();
5349
+ let keywordHits = 0;
5350
+ keywords.forEach((keyword) => {
5351
+ if (contentLower.includes(keyword)) {
5352
+ keywordHits++;
5353
+ }
5354
+ });
5355
+ const normalizedKeywordScore = keywordHits / keywords.length;
5356
+ return __spreadProps(__spreadValues({}, match), {
5357
+ rerankScore: match.score + normalizedKeywordScore * 0.5
5358
+ });
5359
+ });
5360
+ return scoredMatches.sort((a, b) => {
5361
+ var _a2, _b;
5362
+ return ((_a2 = b.rerankScore) != null ? _a2 : b.score) - ((_b = a.rerankScore) != null ? _b : a.score);
5363
+ }).slice(0, limit);
5364
+ }
5365
+ /**
5366
+ * LLM-based re-ranking.
5367
+ *
5368
+ * Fix RAG-1: total document content is now budgeted against
5369
+ * MAX_RERANK_CONTENT_CHARS to prevent silent truncation or failures
5370
+ * on models with small context windows.
5371
+ */
5372
+ async llmRerank(matches, query, limit, llm) {
5373
+ const candidates = matches.slice(0, 10);
5374
+ let charsUsed = 0;
5375
+ const budgetedCandidates = candidates.filter((m) => {
5376
+ const len = (m.content || "").length;
5377
+ if (charsUsed + len > MAX_RERANK_CONTENT_CHARS) return false;
5378
+ charsUsed += len;
5379
+ return true;
5380
+ });
5381
+ const docList = budgetedCandidates.map((m, i) => {
5382
+ var _a2;
5383
+ return `[${i}] ${((_a2 = m == null ? void 0 : m.content) != null ? _a2 : "").replace(/\n/g, " ")}`;
5384
+ }).join("\n");
5385
+ const response = await llm.chat(
5386
+ [{ role: "user", content: `Query: "${query}"
5344
5387
 
5345
5388
  Documents:
5346
- ${topN.map((m, i) => {
5347
- var _a3;
5348
- return `[${i}] ${((_a3 = m == null ? void 0 : m.content) != null ? _a3 : "").replace(/\n/g, " ")}`;
5349
- }).join("\n")}` }],
5350
- "",
5351
- {
5352
- systemPrompt: 'You are a relevance ranking expert. Given the user query and a list of retrieved document chunks, rank the chunks by relevance to the query. Return the result as a comma-separated list of document indices in order of relevance, from most relevant to least relevant. Use the indices provided in brackets like [0], [1], etc. Only return the indices (e.g., "2,0,1"), nothing else. Do not include brackets in the output.',
5353
- temperature: 0,
5354
- maxTokens: 50
5355
- }
5356
- );
5357
- const cleanedResponse = response.trim().replace(/[\[\]\s]/g, "");
5358
- const rankedIndices = cleanedResponse.split(",").map(Number).filter((n) => !isNaN(n));
5359
- if (rankedIndices.length > 0) {
5360
- const rankedMatches = [];
5361
- const usedIndices = /* @__PURE__ */ new Set();
5362
- for (const index of rankedIndices) {
5363
- if (index >= 0 && index < topN.length && !usedIndices.has(index)) {
5364
- rankedMatches.push(topN[index]);
5365
- usedIndices.add(index);
5366
- }
5367
- }
5368
- for (let i = 0; i < topN.length; i++) {
5369
- if (!usedIndices.has(i)) {
5370
- rankedMatches.push(topN[i]);
5371
- }
5372
- }
5373
- const rest = matches.slice(10);
5374
- rankedMatches.push(...rest);
5375
- return rankedMatches.slice(0, limit);
5389
+ ${docList}` }],
5390
+ "",
5391
+ {
5392
+ systemPrompt: 'You are a relevance ranking expert. Given the user query and a list of retrieved document chunks, rank the chunks by relevance to the query. Return the result as a comma-separated list of document indices in order of relevance, from most relevant to least relevant. Use the indices provided in brackets like [0], [1], etc. Only return the indices (e.g., "2,0,1"), nothing else. Do not include brackets in the output.',
5393
+ temperature: 0,
5394
+ maxTokens: 50
5376
5395
  }
5377
- } catch (error) {
5378
- console.warn("[Reranker] LLM re-ranking failed, falling back to score-based sorting:", error);
5396
+ );
5397
+ const cleanedResponse = response.trim().replace(/[\[\]\s]/g, "");
5398
+ const rankedIndices = cleanedResponse.split(",").map(Number).filter((n) => !isNaN(n));
5399
+ if (rankedIndices.length === 0) {
5400
+ return matches.sort((a, b) => b.score - a.score).slice(0, limit);
5401
+ }
5402
+ const rankedMatches = [];
5403
+ const usedIndices = /* @__PURE__ */ new Set();
5404
+ for (const index of rankedIndices) {
5405
+ if (index >= 0 && index < budgetedCandidates.length && !usedIndices.has(index)) {
5406
+ rankedMatches.push(budgetedCandidates[index]);
5407
+ usedIndices.add(index);
5408
+ }
5409
+ }
5410
+ for (let i = 0; i < budgetedCandidates.length; i++) {
5411
+ if (!usedIndices.has(i)) rankedMatches.push(budgetedCandidates[i]);
5379
5412
  }
5380
- return matches.sort((a, b) => b.score - a.score).slice(0, limit);
5413
+ rankedMatches.push(...matches.slice(10));
5414
+ return rankedMatches.slice(0, limit);
5381
5415
  }
5382
5416
  };
5383
5417
 
@@ -5389,11 +5423,11 @@ var LlamaIndexIngestor = class {
5389
5423
  * than standard character-count splitting.
5390
5424
  */
5391
5425
  async chunk(text, options = {}) {
5392
- var _a3, _b;
5426
+ var _a2, _b;
5393
5427
  try {
5394
5428
  const { Document, SentenceSplitter, MetadataMode } = await import(`${"llamaindex"}`);
5395
5429
  const splitter = new SentenceSplitter({
5396
- chunkSize: (_a3 = options.chunkSize) != null ? _a3 : 1e3,
5430
+ chunkSize: (_a2 = options.chunkSize) != null ? _a2 : 1e3,
5397
5431
  chunkOverlap: (_b = options.chunkOverlap) != null ? _b : 200
5398
5432
  });
5399
5433
  const doc = new Document({ text, metadata: options.metadata || {} });
@@ -5501,7 +5535,7 @@ ${error instanceof Error ? error.message : String(error)}`
5501
5535
  * The agent returns `{ messages: [...] }` — the last message is the final answer.
5502
5536
  */
5503
5537
  async run(input, chatHistory = []) {
5504
- var _a3, _b, _c;
5538
+ var _a2, _b, _c;
5505
5539
  if (!this.agent) {
5506
5540
  throw new Error("[LangChainAgent] Agent not initialized. Call initialize() first.");
5507
5541
  }
@@ -5512,7 +5546,7 @@ ${error instanceof Error ? error.message : String(error)}`
5512
5546
  const response = await this.agent.invoke({
5513
5547
  messages: [...historyMessages, new HumanMessage(input)]
5514
5548
  });
5515
- const lastMessage = (_a3 = response == null ? void 0 : response.messages) == null ? void 0 : _a3.at(-1);
5549
+ const lastMessage = (_a2 = response == null ? void 0 : response.messages) == null ? void 0 : _a2.at(-1);
5516
5550
  if (lastMessage && typeof lastMessage.content === "string") {
5517
5551
  return lastMessage.content;
5518
5552
  }
@@ -5563,7 +5597,7 @@ var MCPClient = class {
5563
5597
  }
5564
5598
  }
5565
5599
  async connectStdio() {
5566
- var _a3;
5600
+ var _a2;
5567
5601
  const cmd = this.config.command;
5568
5602
  if (!cmd) {
5569
5603
  throw new Error(`[MCPClient] Command option is required for stdio transport on "${this.config.name}"`);
@@ -5578,7 +5612,7 @@ var MCPClient = class {
5578
5612
  this.stdioBuffer += data.toString("utf-8");
5579
5613
  this.processStdioLines();
5580
5614
  });
5581
- (_a3 = this.childProcess.stderr) == null ? void 0 : _a3.on("data", (data) => {
5615
+ (_a2 = this.childProcess.stderr) == null ? void 0 : _a2.on("data", (data) => {
5582
5616
  console.warn(`[MCPClient] [stderr] [${this.config.name}]:`, data.toString("utf-8"));
5583
5617
  });
5584
5618
  this.childProcess.on("close", (code) => {
@@ -5652,14 +5686,14 @@ var MCPClient = class {
5652
5686
  }
5653
5687
  }
5654
5688
  async sendNotification(method, params) {
5655
- var _a3;
5689
+ var _a2;
5656
5690
  const notification = {
5657
5691
  jsonrpc: "2.0",
5658
5692
  method,
5659
5693
  params
5660
5694
  };
5661
5695
  if (this.config.transport === "stdio") {
5662
- if ((_a3 = this.childProcess) == null ? void 0 : _a3.stdin) {
5696
+ if ((_a2 = this.childProcess) == null ? void 0 : _a2.stdin) {
5663
5697
  this.childProcess.stdin.write(JSON.stringify(notification) + "\n", "utf-8");
5664
5698
  }
5665
5699
  } else if (this.sseUrl) {
@@ -5737,11 +5771,11 @@ var MCPRegistry = class {
5737
5771
  // src/core/MultiAgentCoordinator.ts
5738
5772
  var MultiAgentCoordinator = class {
5739
5773
  constructor(options) {
5740
- var _a3;
5774
+ var _a2;
5741
5775
  this.llmProvider = options.llmProvider;
5742
5776
  this.mcpRegistry = options.mcpRegistry;
5743
5777
  this.documentSearch = options.documentSearch;
5744
- this.maxIterations = (_a3 = options.maxIterations) != null ? _a3 : 5;
5778
+ this.maxIterations = (_a2 = options.maxIterations) != null ? _a2 : 5;
5745
5779
  }
5746
5780
  /**
5747
5781
  * Run the multi-agent coordination loop synchronously and return the final response.
@@ -5793,8 +5827,8 @@ Available Tools:
5793
5827
 
5794
5828
  ${mcpTools.length > 0 ? "MCP Server Tools:" : ""}
5795
5829
  ${mcpTools.map((mt) => {
5796
- var _a3;
5797
- return `- ${mt.tool.name}(${JSON.stringify(((_a3 = mt.tool.inputSchema) == null ? void 0 : _a3.properties) || {})}): ${mt.tool.description || "No description provided."}`;
5830
+ var _a2;
5831
+ return `- ${mt.tool.name}(${JSON.stringify(((_a2 = mt.tool.inputSchema) == null ? void 0 : _a2.properties) || {})}): ${mt.tool.description || "No description provided."}`;
5798
5832
  }).join("\n")}
5799
5833
 
5800
5834
  Tool Calling Protocol:
@@ -5952,100 +5986,6 @@ ${toolResultText}`
5952
5986
  }
5953
5987
  };
5954
5988
 
5955
- // src/core/CircuitBreaker.ts
5956
- var CircuitBreaker = class {
5957
- constructor(options = {}) {
5958
- this.state = "closed";
5959
- this.failureCount = 0;
5960
- this.successCount = 0;
5961
- this.lastFailureAt = 0;
5962
- this.halfOpenCalls = 0;
5963
- var _a3, _b, _c;
5964
- this.failureThreshold = (_a3 = options.failureThreshold) != null ? _a3 : 5;
5965
- this.resetTimeoutMs = (_b = options.resetTimeoutMs) != null ? _b : 3e4;
5966
- this.halfOpenMaxCalls = (_c = options.halfOpenMaxCalls) != null ? _c : 1;
5967
- }
5968
- record(success) {
5969
- const now = Date.now();
5970
- if (success) {
5971
- this.successCount++;
5972
- if (this.state === "half-open") {
5973
- this.state = "closed";
5974
- this.failureCount = 0;
5975
- this.halfOpenCalls = 0;
5976
- this.successCount = 1;
5977
- } else if (this.state === "closed") {
5978
- if (this.successCount >= this.failureThreshold) {
5979
- this.failureCount = Math.max(0, this.failureCount - 1);
5980
- this.successCount = 0;
5981
- }
5982
- }
5983
- } else {
5984
- this.failureCount++;
5985
- this.lastFailureAt = now;
5986
- this.successCount = 0;
5987
- if (this.state === "half-open") {
5988
- this.state = "open";
5989
- this.halfOpenCalls = 0;
5990
- } else if (this.state === "closed" && this.failureCount >= this.failureThreshold) {
5991
- this.state = "open";
5992
- }
5993
- }
5994
- }
5995
- canExecute() {
5996
- const now = Date.now();
5997
- if (this.state === "closed") {
5998
- return true;
5999
- }
6000
- if (this.state === "open") {
6001
- if (now - this.lastFailureAt >= this.resetTimeoutMs) {
6002
- this.state = "half-open";
6003
- this.halfOpenCalls = 0;
6004
- return true;
6005
- }
6006
- return false;
6007
- }
6008
- if (this.state === "half-open") {
6009
- return this.halfOpenCalls < this.halfOpenMaxCalls;
6010
- }
6011
- return true;
6012
- }
6013
- async wrap(fn) {
6014
- if (!this.canExecute()) {
6015
- const retryAfterSec = Math.ceil(
6016
- Math.max(0, this.resetTimeoutMs - (Date.now() - this.lastFailureAt)) / 1e3
6017
- );
6018
- throw new Error(
6019
- `Circuit breaker is open. Try again in ${retryAfterSec}s. Failed ${this.failureCount}/${this.failureThreshold} consecutive calls.`
6020
- );
6021
- }
6022
- if (this.state === "half-open") {
6023
- this.halfOpenCalls++;
6024
- }
6025
- try {
6026
- const result = await fn();
6027
- this.record(true);
6028
- return result;
6029
- } catch (err) {
6030
- this.record(false);
6031
- throw err;
6032
- }
6033
- }
6034
- getState() {
6035
- return this.state;
6036
- }
6037
- getFailureCount() {
6038
- return this.failureCount;
6039
- }
6040
- reset() {
6041
- this.state = "closed";
6042
- this.failureCount = 0;
6043
- this.successCount = 0;
6044
- this.lastFailureAt = 0;
6045
- this.halfOpenCalls = 0;
6046
- }
6047
- };
6048
-
6049
5989
  // src/core/BatchProcessor.ts
6050
5990
  function isTransientError(error) {
6051
5991
  if (!(error instanceof Error)) return false;
@@ -6069,30 +6009,34 @@ function calculateBackoffDelay(attempt, initialDelayMs, maxDelayMs, multiplier)
6069
6009
  function sleep(ms) {
6070
6010
  return new Promise((resolve) => setTimeout(resolve, ms));
6071
6011
  }
6072
- var _BatchProcessor = class _BatchProcessor {
6012
+ var BatchProcessor = class _BatchProcessor {
6073
6013
  /**
6074
- * Execute a processor call through the circuit breaker.
6075
- * Only transient failures count toward opening the circuit.
6014
+ * Execute a processor call, optionally through a caller-provided circuit
6015
+ * breaker. Only transient failures count toward opening the circuit.
6016
+ *
6017
+ * Fix ARCH-3: removed the static module-level `cb` field. Callers must
6018
+ * supply their own CircuitBreaker via BatchOptions.circuitBreaker.
6019
+ * Fix TYPE-1: uses the public retryAfterMs() and incrementHalfOpen()
6020
+ * API instead of accessing private fields via `as any`.
6076
6021
  */
6077
- static async executeWithCircuitBreaker(processor) {
6078
- if (!this.cb.canExecute()) {
6079
- const retryAfterSec = Math.ceil(
6080
- Math.max(0, 3e4 - (Date.now() - this.cb.lastFailureAt || 0)) / 1e3
6081
- );
6022
+ static async executeWithCircuitBreaker(processor, cb) {
6023
+ if (!cb) {
6024
+ return processor();
6025
+ }
6026
+ if (!cb.canExecute()) {
6027
+ const retryAfterSec = Math.ceil(cb.retryAfterMs() / 1e3);
6082
6028
  throw new Error(
6083
6029
  `[BatchProcessor] Circuit breaker is open. Try again in ${retryAfterSec}s.`
6084
6030
  );
6085
6031
  }
6086
- if (this.cb.state === "half-open") {
6087
- this.cb.halfOpenCalls++;
6088
- }
6032
+ cb.incrementHalfOpen();
6089
6033
  try {
6090
6034
  const result = await processor();
6091
- this.cb.record(true);
6035
+ cb.record(true);
6092
6036
  return result;
6093
6037
  } catch (err) {
6094
6038
  if (isTransientError(err)) {
6095
- this.cb.record(false);
6039
+ cb.record(false);
6096
6040
  }
6097
6041
  throw err;
6098
6042
  }
@@ -6137,7 +6081,10 @@ var _BatchProcessor = class _BatchProcessor {
6137
6081
  let lastError;
6138
6082
  for (let attempt = 0; attempt <= maxRetries; attempt++) {
6139
6083
  try {
6140
- const result = await _BatchProcessor.executeWithCircuitBreaker(() => processor(batch));
6084
+ const result = await _BatchProcessor.executeWithCircuitBreaker(
6085
+ () => processor(batch),
6086
+ options == null ? void 0 : options.circuitBreaker
6087
+ );
6141
6088
  results.push(result);
6142
6089
  success = true;
6143
6090
  break;
@@ -6195,7 +6142,10 @@ ${errorMessages}`
6195
6142
  let lastError;
6196
6143
  for (let attempt = 0; attempt <= maxRetries; attempt++) {
6197
6144
  try {
6198
- const result = await _BatchProcessor.executeWithCircuitBreaker(() => processor(item));
6145
+ const result = await _BatchProcessor.executeWithCircuitBreaker(
6146
+ () => processor(item),
6147
+ options == null ? void 0 : options.circuitBreaker
6148
+ );
6199
6149
  results.push(result);
6200
6150
  success = true;
6201
6151
  break;
@@ -6268,7 +6218,10 @@ ${errorMessages}`
6268
6218
  }));
6269
6219
  const chunkPromises = chunk.map(async ({ item, originalIndex }) => {
6270
6220
  try {
6271
- const result = await _BatchProcessor.executeWithCircuitBreaker(() => processor(item));
6221
+ const result = await _BatchProcessor.executeWithCircuitBreaker(
6222
+ () => processor(item),
6223
+ options == null ? void 0 : options.circuitBreaker
6224
+ );
6272
6225
  return { success: true, result, index: originalIndex };
6273
6226
  } catch (err) {
6274
6227
  const error = err instanceof Error ? err : new Error(String(err));
@@ -6296,12 +6249,6 @@ ${errorMessages}`
6296
6249
  return { results, errors, totalProcessed, totalFailed };
6297
6250
  }
6298
6251
  };
6299
- _BatchProcessor.cb = new CircuitBreaker({
6300
- failureThreshold: 5,
6301
- resetTimeoutMs: 3e4,
6302
- halfOpenMaxCalls: 1
6303
- });
6304
- var BatchProcessor = _BatchProcessor;
6305
6252
 
6306
6253
  // src/config/EmbeddingStrategy.ts
6307
6254
  var EmbeddingStrategyResolver = class {
@@ -6399,7 +6346,7 @@ var QueryProcessor = class {
6399
6346
  * @param validFields Optional list of known filterable fields to look for
6400
6347
  */
6401
6348
  static extractQueryFieldHints(question, validFields = []) {
6402
- var _a3, _b, _c, _d;
6349
+ var _a2, _b, _c, _d, _e, _f, _g2, _h;
6403
6350
  if (!question.trim()) return [];
6404
6351
  const hints = /* @__PURE__ */ new Map();
6405
6352
  const addHint = (value, field) => {
@@ -6444,11 +6391,11 @@ var QueryProcessor = class {
6444
6391
  if (name) addHint(name, "name");
6445
6392
  }
6446
6393
  }
6447
- const genericPattern = /\b([a-z][a-z0-9_]{1,30})\s*[:=]\s*["']?([^"'\n?.!,]{1,60})["']?/gi;
6394
+ const genericPattern = /\b([a-z][a-z0-9_]{1,30})\s*[:=]\s*(?:"([^"\n?.!,]{1,60})"|'([^'\n?.!,]{1,60})'|([^\s\n?.!,:=]{1,60}))/gi;
6448
6395
  for (const match of question.matchAll(genericPattern)) {
6449
6396
  const field = match[1];
6450
- const value = match[2];
6451
- if (field && !this.isLikelyPromptPhrase(field)) {
6397
+ const value = (_b = (_a2 = match[2]) != null ? _a2 : match[3]) != null ? _b : match[4];
6398
+ if (field && value && !this.isLikelyPromptPhrase(field)) {
6452
6399
  const resolvedField = resolveValidField(field);
6453
6400
  if (resolvedField) {
6454
6401
  addHint(value, resolvedField);
@@ -6479,7 +6426,7 @@ var QueryProcessor = class {
6479
6426
  ];
6480
6427
  for (const p of universalPatterns) {
6481
6428
  for (const match of question.matchAll(p.regex)) {
6482
- const val = p.group ? (_a3 = match[p.group]) != null ? _a3 : match[0] : match[0];
6429
+ const val = p.group ? (_c = match[p.group]) != null ? _c : match[0] : match[0];
6483
6430
  if (!val) continue;
6484
6431
  if (p.field) addHint(val, p.field);
6485
6432
  else addHint(val);
@@ -6487,22 +6434,23 @@ var QueryProcessor = class {
6487
6434
  }
6488
6435
  for (const pattern of naturalQuestionPatterns) {
6489
6436
  for (const match of question.matchAll(pattern)) {
6490
- const value = (_b = match[2]) != null ? _b : match[1];
6437
+ const value = (_d = match[2]) != null ? _d : match[1];
6491
6438
  if (value) addHint(value);
6492
6439
  }
6493
6440
  }
6494
6441
  const fieldPattern = `([^\\n:=?.!,]{1,60}?)`;
6495
- const valuePattern = `([^\\n?.!,]{1,120}?)`;
6442
+ const valuePattern = `(?:"([^"\\n?.!,]{1,120})"|'([^'\\n?.!,]{1,120})'|([^\\s\\n?.!,:=]{1,120}))`;
6496
6443
  const fieldValuePatterns = [
6497
- new RegExp(`\\b${fieldPattern}\\s*(?:=|:)\\s*["']?${valuePattern}["']?(?=[?.!,]|$)`, "gi"),
6498
- new RegExp(`\\b${fieldPattern}\\s+(?:is|are|was|were|equals?|equal to|named|called)\\s+["']?${valuePattern}["']?(?=[?.!,]|$)`, "gi"),
6499
- new RegExp(`\\bwith\\s+${fieldPattern}\\s+["']?${valuePattern}["']?(?=[?.!,]|$)`, "gi"),
6500
- new RegExp(`\\b(?:under|in|for|from|of)\\s+${fieldPattern}\\s+["']?${valuePattern}["']?(?=[?.!,]|$)`, "gi")
6444
+ new RegExp(`\\b${fieldPattern}\\s*(?:=|:)\\s*${valuePattern}`, "gi"),
6445
+ new RegExp(`\\b${fieldPattern}\\s+(?:is|are|was|were|equals?|equal to|named|called)\\s+${valuePattern}`, "gi"),
6446
+ new RegExp(`\\bwith\\s+${fieldPattern}\\s+${valuePattern}`, "gi"),
6447
+ new RegExp(`\\b(?:under|in|for|from|of)\\s+${fieldPattern}\\s+${valuePattern}`, "gi")
6501
6448
  ];
6502
6449
  for (const pattern of fieldValuePatterns) {
6503
6450
  for (const match of question.matchAll(pattern)) {
6504
- const field = (_c = match[1]) != null ? _c : "";
6505
- const value = (_d = match[2]) != null ? _d : "";
6451
+ const field = (_e = match[1]) != null ? _e : "";
6452
+ const value = (_h = (_g2 = (_f = match[2]) != null ? _f : match[3]) != null ? _g2 : match[4]) != null ? _h : "";
6453
+ if (!value) continue;
6506
6454
  const resolvedField = resolveValidField(field);
6507
6455
  if (resolvedField) {
6508
6456
  addHint(value, resolvedField);
@@ -6572,10 +6520,10 @@ var QueryProcessor = class {
6572
6520
  }
6573
6521
  static normalizeNumericOperator(operator) {
6574
6522
  const op = operator.toLowerCase().trim();
6575
- if (op === ">" || /\b(greater than|more than|above|over)\b/.test(op)) return "gt";
6576
6523
  if (op === ">=" || /\b(greater than or equal to|more than or equal to|at least)\b/.test(op)) return "gte";
6577
- if (op === "<" || /\b(less than|below|under)\b/.test(op)) return "lt";
6578
6524
  if (op === "<=" || /\b(less than or equal to|at most)\b/.test(op)) return "lte";
6525
+ if (op === ">" || /\b(greater than|more than|above|over)\b/.test(op)) return "gt";
6526
+ if (op === "<" || /\b(less than|below|under)\b/.test(op)) return "lt";
6579
6527
  return "eq";
6580
6528
  }
6581
6529
  static normalizePredicateField(field, validFields) {
@@ -6703,11 +6651,11 @@ var LLMRouter = class {
6703
6651
  * When provided it is used directly as the 'default' role without re-constructing.
6704
6652
  */
6705
6653
  async initialize(prebuiltDefault) {
6706
- var _a3;
6654
+ var _a2;
6707
6655
  const defaultModel = prebuiltDefault != null ? prebuiltDefault : LLMFactory.create(this.config.llm, this.config.embedding);
6708
6656
  this.models.set("default", defaultModel);
6709
6657
  const envFastModel = process.env.FAST_LLM_MODEL;
6710
- const providerFastDefault = (_a3 = FAST_MODEL_DEFAULTS[this.config.llm.provider]) != null ? _a3 : "";
6658
+ const providerFastDefault = (_a2 = FAST_MODEL_DEFAULTS[this.config.llm.provider]) != null ? _a2 : "";
6711
6659
  const fastModelName = envFastModel || providerFastDefault;
6712
6660
  if (fastModelName && fastModelName !== this.config.llm.model) {
6713
6661
  const fastConfig = __spreadProps(__spreadValues({}, this.config.llm), {
@@ -6724,8 +6672,8 @@ var LLMRouter = class {
6724
6672
  * Falls back to 'default' if the requested role is not registered.
6725
6673
  */
6726
6674
  get(role) {
6727
- var _a3;
6728
- const provider = (_a3 = this.models.get(role)) != null ? _a3 : this.models.get("default");
6675
+ var _a2;
6676
+ const provider = (_a2 = this.models.get(role)) != null ? _a2 : this.models.get("default");
6729
6677
  if (!provider) {
6730
6678
  throw new Error(`[LLMRouter] No provider registered for role: "${role}". Did you call initialize()?`);
6731
6679
  }
@@ -6811,8 +6759,8 @@ var IntentClassifier = class {
6811
6759
  numericFieldCount = numericKeys.size;
6812
6760
  categoricalFieldCount = catKeys.size;
6813
6761
  if (productKeyMatches >= 2 || docs.some((d) => {
6814
- var _a3, _b;
6815
- return ((_a3 = d == null ? void 0 : d.content) != null ? _a3 : "").toLowerCase().includes("price") || ((_b = d == null ? void 0 : d.content) != null ? _b : "").toLowerCase().includes("brand");
6762
+ var _a2, _b;
6763
+ return ((_a2 = d == null ? void 0 : d.content) != null ? _a2 : "").toLowerCase().includes("price") || ((_b = d == null ? void 0 : d.content) != null ? _b : "").toLowerCase().includes("brand");
6816
6764
  })) {
6817
6765
  isProductLike = true;
6818
6766
  }
@@ -7002,7 +6950,7 @@ var Rule6SmallResultSetRule = class {
7002
6950
  var Rule7LargeTableRule = class {
7003
6951
  constructor() {
7004
6952
  this.name = "Rule7LargeTable";
7005
- this.priority = 75;
6953
+ this.priority = 82;
7006
6954
  }
7007
6955
  evaluate(context, intent) {
7008
6956
  const docs = context.retrievedDocuments || [];
@@ -7045,8 +6993,8 @@ var RuleEngine = class {
7045
6993
  this.registerRule(new Rule5MixedResponseRule());
7046
6994
  this.registerRule(new Rule2ComparisonRule());
7047
6995
  this.registerRule(new Rule3ProductDiscoveryRule());
7048
- this.registerRule(new Rule4AnalyticalRule());
7049
6996
  this.registerRule(new Rule7LargeTableRule());
6997
+ this.registerRule(new Rule4AnalyticalRule());
7050
6998
  }
7051
6999
  /**
7052
7000
  * Add or replace a rule in the engine.
@@ -7094,8 +7042,8 @@ var TextRendererStrategy = class {
7094
7042
  }
7095
7043
  const docs = Array.isArray(data) ? data : [];
7096
7044
  const text = docs.map((d) => {
7097
- var _a3;
7098
- return (_a3 = d == null ? void 0 : d.content) != null ? _a3 : "";
7045
+ var _a2;
7046
+ return (_a2 = d == null ? void 0 : d.content) != null ? _a2 : "";
7099
7047
  }).join("\n\n");
7100
7048
  return { type: "text", title, data: { content: text || "No detailed content available." } };
7101
7049
  }
@@ -7238,9 +7186,9 @@ var LRUDecisionCache = class {
7238
7186
  this.maxSize = maxSize;
7239
7187
  }
7240
7188
  generateKey(context) {
7241
- var _a3, _b;
7189
+ var _a2, _b;
7242
7190
  const q = (context.userQuery || "").toLowerCase().trim();
7243
- const docCount = (_b = (_a3 = context.retrievedDocuments) == null ? void 0 : _a3.length) != null ? _b : 0;
7191
+ const docCount = (_b = (_a2 = context.retrievedDocuments) == null ? void 0 : _a2.length) != null ? _b : 0;
7244
7192
  return `${q}::docs:${docCount}`;
7245
7193
  }
7246
7194
  get(context) {
@@ -7341,7 +7289,7 @@ var UITransformer = class _UITransformer {
7341
7289
  * Prefer `analyzeAndDecide()` in production.
7342
7290
  */
7343
7291
  static transform(userQuery, retrievedData, config, trainedSchema, intent) {
7344
- var _a3, _b, _c;
7292
+ var _a2, _b, _c;
7345
7293
  if (!retrievedData || retrievedData.length === 0) {
7346
7294
  return this.createTextResponse("No data available", "No relevant data found for your query.");
7347
7295
  }
@@ -7361,7 +7309,7 @@ var UITransformer = class _UITransformer {
7361
7309
  return this.transformToBarChart(filteredData, profile, userQuery, resolvedIntent.visualizationHint === "ranking");
7362
7310
  }
7363
7311
  if (resolvedIntent.visualizationHint === "distribution") {
7364
- return (_a3 = this.transformToHistogram(profile, userQuery)) != null ? _a3 : this.transformToBarChart(filteredData, profile, userQuery);
7312
+ return (_a2 = this.transformToHistogram(profile, userQuery)) != null ? _a2 : this.transformToBarChart(filteredData, profile, userQuery);
7365
7313
  }
7366
7314
  if (resolvedIntent.visualizationHint === "correlation") {
7367
7315
  return (_b = this.transformToScatterPlot(profile, userQuery)) != null ? _b : this.transformToBarChart(filteredData, profile, userQuery);
@@ -7694,11 +7642,11 @@ ${schemaProfileText}` : ""}`;
7694
7642
  };
7695
7643
  }
7696
7644
  static transformToPieChart(data, profile, query = "") {
7697
- var _a3;
7645
+ var _a2;
7698
7646
  const dimension = profile ? this.selectDimensionField(profile, query) : void 0;
7699
7647
  const categories = dimension ? Array.from(new Set(profile.records.map((record) => {
7700
- var _a4;
7701
- return String((_a4 = record.fields[dimension.key]) != null ? _a4 : "");
7648
+ var _a3;
7649
+ return String((_a3 = record.fields[dimension.key]) != null ? _a3 : "");
7702
7650
  }).filter(Boolean))) : this.detectCategories(data);
7703
7651
  if (categories.length === 0) return null;
7704
7652
  const categoryData = dimension && profile ? this.aggregateProfileByDimension(profile, dimension.key) : this.aggregateByCategory(data, categories);
@@ -7708,7 +7656,7 @@ ${schemaProfileText}` : ""}`;
7708
7656
  });
7709
7657
  return {
7710
7658
  type: "pie_chart",
7711
- title: `Distribution by ${(_a3 = dimension == null ? void 0 : dimension.label) != null ? _a3 : "Category"}`,
7659
+ title: `Distribution by ${(_a2 = dimension == null ? void 0 : dimension.label) != null ? _a2 : "Category"}`,
7712
7660
  description: `Showing breakdown across ${pieData.length} categories`,
7713
7661
  data: pieData
7714
7662
  };
@@ -7718,8 +7666,8 @@ ${schemaProfileText}` : ""}`;
7718
7666
  const valueField = profile.numericFields[0];
7719
7667
  const buckets = /* @__PURE__ */ new Map();
7720
7668
  profile.records.forEach((record) => {
7721
- var _a3, _b, _c;
7722
- const timestamp = String((_a3 = record.fields[dateField.key]) != null ? _a3 : "");
7669
+ var _a2, _b, _c;
7670
+ const timestamp = String((_a2 = record.fields[dateField.key]) != null ? _a2 : "");
7723
7671
  const value = (_b = this.toFiniteNumber(record.fields[valueField.key])) != null ? _b : 0;
7724
7672
  buckets.set(timestamp, ((_c = buckets.get(timestamp)) != null ? _c : 0) + value);
7725
7673
  });
@@ -7732,23 +7680,23 @@ ${schemaProfileText}` : ""}`;
7732
7680
  };
7733
7681
  }
7734
7682
  static transformToBarChart(data, profile, query = "", horizontal = false) {
7735
- var _a3;
7683
+ var _a2;
7736
7684
  const dimension = profile ? this.selectDimensionField(profile, query) : void 0;
7737
7685
  const measure = profile ? this.selectNumericField(profile, query) : void 0;
7738
7686
  const aggregate = dimension && profile ? this.aggregateProfileByDimension(profile, dimension.key, measure == null ? void 0 : measure.key) : this.aggregateByCategory(data, this.detectCategories(data));
7739
7687
  const barData = Object.entries(aggregate).map(([category, value]) => ({ category, value: Number(value) })).sort((a, b) => horizontal ? b.value - a.value : 0).slice(0, 12);
7740
7688
  const fallbackData = barData.length > 0 ? barData : data.slice(0, 12).map((item, index) => {
7741
- var _a4, _b, _c, _d, _e;
7689
+ var _a3, _b, _c, _d, _e;
7742
7690
  const meta = item.metadata || {};
7743
7691
  const label = String(
7744
- (_c = (_b = (_a4 = this.getDynamicVal(meta, "name")) != null ? _a4 : meta.label) != null ? _b : meta.title) != null ? _c : `Result ${index + 1}`
7692
+ (_c = (_b = (_a3 = this.getDynamicVal(meta, "name")) != null ? _a3 : meta.label) != null ? _b : meta.title) != null ? _c : `Result ${index + 1}`
7745
7693
  );
7746
7694
  const value = (_e = (_d = this.extractNumericValue(meta)) != null ? _d : item.score) != null ? _e : 0;
7747
7695
  return { category: label, value: Number(value) };
7748
7696
  });
7749
7697
  return {
7750
7698
  type: horizontal ? "horizontal_bar" : "bar_chart",
7751
- title: dimension ? `${(_a3 = measure == null ? void 0 : measure.label) != null ? _a3 : "Count"} by ${dimension.label}` : "Comparison",
7699
+ title: dimension ? `${(_a2 = measure == null ? void 0 : measure.label) != null ? _a2 : "Count"} by ${dimension.label}` : "Comparison",
7752
7700
  description: `Showing ${fallbackData.length} comparable values`,
7753
7701
  data: fallbackData
7754
7702
  };
@@ -7783,11 +7731,11 @@ ${schemaProfileText}` : ""}`;
7783
7731
  if (fields.length < 2) return null;
7784
7732
  const [xField, yField] = fields;
7785
7733
  const points = profile.records.map((record) => {
7786
- var _a3;
7734
+ var _a2;
7787
7735
  const x = this.toFiniteNumber(record.fields[xField.key]);
7788
7736
  const y = this.toFiniteNumber(record.fields[yField.key]);
7789
7737
  if (x === null || y === null) return null;
7790
- return { x, y, label: String((_a3 = this.getRecordLabel(record)) != null ? _a3 : record.id) };
7738
+ return { x, y, label: String((_a2 = this.getRecordLabel(record)) != null ? _a2 : record.id) };
7791
7739
  }).filter((point) => point !== null).slice(0, 100);
7792
7740
  if (points.length === 0) return null;
7793
7741
  return {
@@ -7817,9 +7765,9 @@ ${schemaProfileText}` : ""}`;
7817
7765
  static transformToRadarChart(data) {
7818
7766
  const attributeMap = {};
7819
7767
  data.forEach((item) => {
7820
- var _a3, _b, _c;
7768
+ var _a2, _b, _c;
7821
7769
  const meta = item.metadata || {};
7822
- const seriesName = String((_c = (_b = (_a3 = meta.name) != null ? _a3 : meta.product) != null ? _b : item.id) != null ? _c : "Item");
7770
+ const seriesName = String((_c = (_b = (_a2 = meta.name) != null ? _a2 : meta.product) != null ? _b : item.id) != null ? _c : "Item");
7823
7771
  Object.entries(meta).forEach(([key, val]) => {
7824
7772
  if (typeof val === "number" && !["price", "id"].includes(key.toLowerCase())) {
7825
7773
  if (!attributeMap[key]) attributeMap[key] = {};
@@ -7835,8 +7783,8 @@ ${schemaProfileText}` : ""}`;
7835
7783
  title: "Product Comparison",
7836
7784
  description: `Comparing ${data.length} items across ${radarData.length} attributes`,
7837
7785
  data: radarData.length > 0 ? radarData : data.map((d) => {
7838
- var _a3;
7839
- return { attribute: ((_a3 = d == null ? void 0 : d.content) != null ? _a3 : "").substring(0, 40) };
7786
+ var _a2;
7787
+ return { attribute: ((_a2 = d == null ? void 0 : d.content) != null ? _a2 : "").substring(0, 40) };
7840
7788
  })
7841
7789
  };
7842
7790
  }
@@ -7855,8 +7803,8 @@ ${schemaProfileText}` : ""}`;
7855
7803
  return this.createTextResponse(
7856
7804
  "Retrieved Context",
7857
7805
  data.map((item) => {
7858
- var _a3;
7859
- return (_a3 = item == null ? void 0 : item.content) != null ? _a3 : "";
7806
+ var _a2;
7807
+ return (_a2 = item == null ? void 0 : item.content) != null ? _a2 : "";
7860
7808
  }).join("\n\n"),
7861
7809
  `Found ${data.length} relevant results`
7862
7810
  );
@@ -7907,11 +7855,11 @@ ${schemaProfileText}` : ""}`;
7907
7855
  return null;
7908
7856
  }
7909
7857
  static normalizeTransformation(payload) {
7910
- var _a3, _b, _c, _d, _e, _f, _g2, _h, _i, _j;
7858
+ var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j;
7911
7859
  if (!payload || typeof payload !== "object") return null;
7912
7860
  const p = payload;
7913
7861
  const type = this.normalizeVisualizationType(
7914
- String((_c = (_b = (_a3 = p.type) != null ? _a3 : p.view) != null ? _b : p.chartType) != null ? _c : "")
7862
+ String((_c = (_b = (_a2 = p.type) != null ? _a2 : p.view) != null ? _b : p.chartType) != null ? _c : "")
7915
7863
  );
7916
7864
  if (!type) return null;
7917
7865
  const title = String((_e = (_d = p.title) != null ? _d : p.heading) != null ? _e : "Visualization");
@@ -7922,7 +7870,7 @@ ${schemaProfileText}` : ""}`;
7922
7870
  return this.validateTransformation(transformation) ? transformation : null;
7923
7871
  }
7924
7872
  static normalizeVisualizationType(type) {
7925
- var _a3;
7873
+ var _a2;
7926
7874
  const mapping = {
7927
7875
  pie: "pie_chart",
7928
7876
  pie_chart: "pie_chart",
@@ -7950,7 +7898,7 @@ ${schemaProfileText}` : ""}`;
7950
7898
  product_carousel: "product_carousel",
7951
7899
  carousel: "carousel"
7952
7900
  };
7953
- return (_a3 = mapping[type.toLowerCase()]) != null ? _a3 : null;
7901
+ return (_a2 = mapping[type.toLowerCase()]) != null ? _a2 : null;
7954
7902
  }
7955
7903
  static validateTransformation(t) {
7956
7904
  const { type, data } = t;
@@ -8066,7 +8014,7 @@ ${schemaProfileText}` : ""}`;
8066
8014
  }
8067
8015
  static profileData(data) {
8068
8016
  const records = (data || []).filter((item) => Boolean(item && typeof item === "object")).map((item) => {
8069
- var _a3, _b;
8017
+ var _a2, _b;
8070
8018
  const fields2 = {};
8071
8019
  Object.entries(item.metadata || {}).forEach(([key, value]) => {
8072
8020
  const primitive = this.toPrimitive(value);
@@ -8075,7 +8023,7 @@ ${schemaProfileText}` : ""}`;
8075
8023
  if (!fields2.content && item.content) fields2.content = item.content.substring(0, 500);
8076
8024
  return {
8077
8025
  id: item.id,
8078
- content: (_a3 = item.content) != null ? _a3 : "",
8026
+ content: (_a2 = item.content) != null ? _a2 : "",
8079
8027
  score: (_b = item.score) != null ? _b : 0,
8080
8028
  fields: fields2,
8081
8029
  source: item
@@ -8156,16 +8104,16 @@ ${schemaProfileText}` : ""}`;
8156
8104
  return null;
8157
8105
  }
8158
8106
  static selectDimensionField(profile, query) {
8159
- var _a3, _b;
8107
+ var _a2, _b;
8160
8108
  const productCategory = profile.categoricalFields.find(
8161
8109
  (field) => /category|department|collection|type|group|segment|region|country|state|city/i.test(field.key)
8162
8110
  );
8163
8111
  const ranked = this.rankFieldsByQuery(profile.categoricalFields, query);
8164
- return (_b = (_a3 = ranked[0]) != null ? _a3 : productCategory) != null ? _b : profile.categoricalFields[0];
8112
+ return (_b = (_a2 = ranked[0]) != null ? _a2 : productCategory) != null ? _b : profile.categoricalFields[0];
8165
8113
  }
8166
8114
  static selectNumericField(profile, query) {
8167
- var _a3;
8168
- return (_a3 = this.rankFieldsByQuery(profile.numericFields, query)[0]) != null ? _a3 : profile.numericFields[0];
8115
+ var _a2;
8116
+ return (_a2 = this.rankFieldsByQuery(profile.numericFields, query)[0]) != null ? _a2 : profile.numericFields[0];
8169
8117
  }
8170
8118
  static rankFieldsByQuery(fields, query) {
8171
8119
  const q = query.toLowerCase();
@@ -8194,8 +8142,8 @@ ${schemaProfileText}` : ""}`;
8194
8142
  static aggregateProfileByDimension(profile, dimensionKey, measureKey) {
8195
8143
  const result = {};
8196
8144
  profile.records.forEach((record) => {
8197
- var _a3, _b, _c;
8198
- const category = String((_a3 = record.fields[dimensionKey]) != null ? _a3 : "Other").trim() || "Other";
8145
+ var _a2, _b, _c;
8146
+ const category = String((_a2 = record.fields[dimensionKey]) != null ? _a2 : "Other").trim() || "Other";
8199
8147
  const value = measureKey ? (_b = this.toFiniteNumber(record.fields[measureKey])) != null ? _b : 0 : 1;
8200
8148
  result[category] = ((_c = result[category]) != null ? _c : 0) + value;
8201
8149
  });
@@ -8275,8 +8223,8 @@ ${schemaProfileText}` : ""}`;
8275
8223
  static aggregateByCategory(data, categories) {
8276
8224
  const result = Object.fromEntries(categories.map((c) => [c, 0]));
8277
8225
  data.forEach((item) => {
8278
- var _a3;
8279
- const cat = (_a3 = this.getProductCategory(item)) != null ? _a3 : "Other";
8226
+ var _a2;
8227
+ const cat = (_a2 = this.getProductCategory(item)) != null ? _a2 : "Other";
8280
8228
  if (Object.prototype.hasOwnProperty.call(result, cat)) result[cat]++;
8281
8229
  else result["Other"] = (result["Other"] || 0) + 1;
8282
8230
  });
@@ -8284,17 +8232,17 @@ ${schemaProfileText}` : ""}`;
8284
8232
  }
8285
8233
  static extractTimeSeriesData(data) {
8286
8234
  return data.map((item) => {
8287
- var _a3, _b, _c, _d, _e, _f;
8235
+ var _a2, _b, _c, _d, _e, _f;
8288
8236
  const meta = item.metadata || {};
8289
8237
  return {
8290
- timestamp: (_b = (_a3 = meta.timestamp) != null ? _a3 : meta.date) != null ? _b : (/* @__PURE__ */ new Date()).toISOString(),
8238
+ timestamp: (_b = (_a2 = meta.timestamp) != null ? _a2 : meta.date) != null ? _b : (/* @__PURE__ */ new Date()).toISOString(),
8291
8239
  value: (_d = (_c = meta.value) != null ? _c : item.score) != null ? _d : 0,
8292
8240
  label: (_f = meta.label) != null ? _f : ((_e = item == null ? void 0 : item.content) != null ? _e : "").substring(0, 50)
8293
8241
  };
8294
8242
  });
8295
8243
  }
8296
8244
  static extractNumericValue(meta) {
8297
- var _a3;
8245
+ var _a2;
8298
8246
  const preferredKeys = [
8299
8247
  "value",
8300
8248
  "count",
@@ -8309,7 +8257,7 @@ ${schemaProfileText}` : ""}`;
8309
8257
  "price"
8310
8258
  ];
8311
8259
  for (const key of preferredKeys) {
8312
- const raw = (_a3 = resolveMetadataValue(meta, key)) != null ? _a3 : meta[key];
8260
+ const raw = (_a2 = resolveMetadataValue(meta, key)) != null ? _a2 : meta[key];
8313
8261
  const value = typeof raw === "number" ? raw : Number(String(raw != null ? raw : "").replace(/[$,% ,]/g, ""));
8314
8262
  if (Number.isFinite(value)) return value;
8315
8263
  }
@@ -8404,8 +8352,8 @@ ${schemaProfileText}` : ""}`;
8404
8352
  }, query.includes(normalizedField) ? 3 : 0);
8405
8353
  }
8406
8354
  static resolveTableCellValue(item, column) {
8407
- var _a3, _b, _c;
8408
- if (column === "Content") return ((_a3 = item == null ? void 0 : item.content) != null ? _a3 : "").substring(0, 100);
8355
+ var _a2, _b, _c;
8356
+ if (column === "Content") return ((_a2 = item == null ? void 0 : item.content) != null ? _a2 : "").substring(0, 100);
8409
8357
  const meta = item.metadata || {};
8410
8358
  const normalizedColumn = this.normalizeComparableField(column);
8411
8359
  const exactMetadata = Object.entries(meta).find(
@@ -8457,8 +8405,8 @@ ${schemaProfileText}` : ""}`;
8457
8405
  let inStock = 0;
8458
8406
  let outOfStock = 0;
8459
8407
  data.forEach((d) => {
8460
- var _a3, _b;
8461
- const cat = (_a3 = this.getProductCategory(d)) != null ? _a3 : "Other";
8408
+ var _a2, _b;
8409
+ const cat = (_a2 = this.getProductCategory(d)) != null ? _a2 : "Other";
8462
8410
  if (cat === category) {
8463
8411
  const quantity = (_b = this.extractStockQuantity(d)) != null ? _b : 1;
8464
8412
  if (this.determineStockStatus(d)) inStock += quantity;
@@ -8504,9 +8452,9 @@ ${schemaProfileText}` : ""}`;
8504
8452
  }
8505
8453
  // ─── Product Extraction ───────────────────────────────────────────────────
8506
8454
  static getDynamicVal(meta, uiKey, config, trainedSchema) {
8507
- var _a3;
8455
+ var _a2;
8508
8456
  if (!meta) return void 0;
8509
- const mapping = (_a3 = config == null ? void 0 : config.rag) == null ? void 0 : _a3.uiMapping;
8457
+ const mapping = (_a2 = config == null ? void 0 : config.rag) == null ? void 0 : _a2.uiMapping;
8510
8458
  if ((mapping == null ? void 0 : mapping[uiKey]) && meta[mapping[uiKey]] !== void 0) return meta[mapping[uiKey]];
8511
8459
  if (trainedSchema && typeof trainedSchema === "object") {
8512
8460
  const trainedKey = trainedSchema[uiKey];
@@ -8515,7 +8463,7 @@ ${schemaProfileText}` : ""}`;
8515
8463
  return resolveMetadataValue(meta, uiKey);
8516
8464
  }
8517
8465
  static extractProductInfo(item, config, trainedSchema) {
8518
- var _a3, _b;
8466
+ var _a2, _b;
8519
8467
  if (!item) return null;
8520
8468
  const meta = item.metadata || {};
8521
8469
  const content = item.content || "";
@@ -8523,7 +8471,7 @@ ${schemaProfileText}` : ""}`;
8523
8471
  const price = this.getDynamicVal(meta, "price", config, trainedSchema);
8524
8472
  const brand = this.getDynamicVal(meta, "brand", config, trainedSchema);
8525
8473
  const description = this.cleanProductDescription(
8526
- (_a3 = this.extractProductDescriptionFromContent(content)) != null ? _a3 : this.getProductDescriptionValue(meta, config, trainedSchema)
8474
+ (_a2 = this.extractProductDescriptionFromContent(content)) != null ? _a2 : this.getProductDescriptionValue(meta, config, trainedSchema)
8527
8475
  );
8528
8476
  if (name || this.isProductData(item)) {
8529
8477
  let finalName = name ? String(name) : void 0;
@@ -8658,10 +8606,10 @@ RULES:
8658
8606
  }
8659
8607
  static buildContextSummary(sources, maxChars = 6e3) {
8660
8608
  const items = (sources || []).filter(Boolean).map((s, i) => {
8661
- var _a3, _b, _c, _d;
8609
+ var _a2, _b, _c, _d;
8662
8610
  return {
8663
8611
  index: i + 1,
8664
- content: (_b = (_a3 = s == null ? void 0 : s.content) == null ? void 0 : _a3.substring(0, 400)) != null ? _b : "",
8612
+ content: (_b = (_a2 = s == null ? void 0 : s.content) == null ? void 0 : _a2.substring(0, 400)) != null ? _b : "",
8665
8613
  metadata: (_c = s == null ? void 0 : s.metadata) != null ? _c : {},
8666
8614
  score: (_d = s == null ? void 0 : s.score) != null ? _d : 0
8667
8615
  };
@@ -8701,7 +8649,7 @@ var SchemaMapper = class {
8701
8649
  return promise;
8702
8650
  }
8703
8651
  static async _doTrain(llm, cacheKey, keys) {
8704
- var _a3, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k;
8652
+ var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k;
8705
8653
  const propertyList = Object.entries(this.TARGET_PROPERTIES).map(([prop, desc]) => `- ${prop} (${desc})`).join("\n");
8706
8654
  const messages = [
8707
8655
  { role: "system", content: "You are a database schema expert. Respond ONLY with valid JSON." },
@@ -8716,7 +8664,7 @@ Return a JSON object like {"name":"Title","price":"Price",...}. Omit unmapped pr
8716
8664
  }
8717
8665
  ];
8718
8666
  try {
8719
- const baseUrl = (_a3 = llm == null ? void 0 : llm.baseUrl) != null ? _a3 : "";
8667
+ const baseUrl = (_a2 = llm == null ? void 0 : llm.baseUrl) != null ? _a2 : "";
8720
8668
  const apiKey = (_b = llm == null ? void 0 : llm.apiKey) != null ? _b : "";
8721
8669
  const model = (_c = llm == null ? void 0 : llm.model) != null ? _c : "llama-3.1-8b-instant";
8722
8670
  let responseText;
@@ -8891,9 +8839,9 @@ var Pipeline = class {
8891
8839
  this.initialised = false;
8892
8840
  /** Namespace-specific static cold context cache for CAG */
8893
8841
  this.coldContexts = /* @__PURE__ */ new Map();
8894
- var _a3, _b, _c, _d, _e;
8842
+ var _a2, _b, _c, _d, _e;
8895
8843
  this.chunker = new DocumentChunker(
8896
- (_b = (_a3 = config.rag) == null ? void 0 : _a3.chunkSize) != null ? _b : 1e3,
8844
+ (_b = (_a2 = config.rag) == null ? void 0 : _a2.chunkSize) != null ? _b : 1e3,
8897
8845
  (_d = (_c = config.rag) == null ? void 0 : _c.chunkOverlap) != null ? _d : 200
8898
8846
  );
8899
8847
  if (((_e = config.rag) == null ? void 0 : _e.chunkingStrategy) === "llamaindex") {
@@ -8909,7 +8857,7 @@ var Pipeline = class {
8909
8857
  return this.initialised ? this.llmProvider : void 0;
8910
8858
  }
8911
8859
  async initialize() {
8912
- var _a3, _b, _c, _d;
8860
+ var _a2, _b, _c, _d;
8913
8861
  if (this.initialised) return;
8914
8862
  const chartInstruction = `You are a helpful assistant. Use the provided context to answer questions accurately.
8915
8863
 
@@ -8952,7 +8900,7 @@ var Pipeline = class {
8952
8900
  this.entityExtractor = new EntityExtractor(this.llmProvider);
8953
8901
  }
8954
8902
  await this.vectorDB.initialize();
8955
- if (((_a3 = this.config.rag) == null ? void 0 : _a3.architecture) === "agentic" || ((_b = this.config.rag) == null ? void 0 : _b.architecture) === "multi-agent" || this.config.mcpServers && this.config.mcpServers.length > 0) {
8903
+ if (((_a2 = this.config.rag) == null ? void 0 : _a2.architecture) === "agentic" || ((_b = this.config.rag) == null ? void 0 : _b.architecture) === "multi-agent" || this.config.mcpServers && this.config.mcpServers.length > 0) {
8956
8904
  this.mcpRegistry = new MCPRegistry(this.config.mcpServers || []);
8957
8905
  this.multiAgentCoordinator = new MultiAgentCoordinator({
8958
8906
  llmProvider: this.llmProvider,
@@ -8970,8 +8918,8 @@ var Pipeline = class {
8970
8918
  this.initialised = true;
8971
8919
  }
8972
8920
  async loadColdContext(ns) {
8973
- var _a3, _b;
8974
- const cagConfig = (_a3 = this.config.rag) == null ? void 0 : _a3.cag;
8921
+ var _a2, _b;
8922
+ const cagConfig = (_a2 = this.config.rag) == null ? void 0 : _a2.cag;
8975
8923
  if (!cagConfig || !cagConfig.enabled) return;
8976
8924
  try {
8977
8925
  const coldNs = cagConfig.coldNamespace || ns;
@@ -9024,12 +8972,12 @@ ${m.content}`).join("\n\n---\n\n");
9024
8972
  }
9025
8973
  /** Step 1: Chunk the document content. */
9026
8974
  async prepareChunks(doc) {
9027
- var _a3, _b;
8975
+ var _a2, _b;
9028
8976
  if (this.llamaIngestor) {
9029
8977
  return this.llamaIngestor.chunk(doc.content, {
9030
8978
  docId: doc.docId,
9031
8979
  metadata: doc.metadata,
9032
- chunkSize: (_a3 = this.config.rag) == null ? void 0 : _a3.chunkSize,
8980
+ chunkSize: (_a2 = this.config.rag) == null ? void 0 : _a2.chunkSize,
9033
8981
  chunkOverlap: (_b = this.config.rag) == null ? void 0 : _b.chunkOverlap
9034
8982
  });
9035
8983
  }
@@ -9127,9 +9075,9 @@ ${m.content}`).join("\n\n---\n\n");
9127
9075
  return { reply, sources };
9128
9076
  }
9129
9077
  async ask(question, history = [], namespace) {
9130
- var _a3, _b;
9078
+ var _a2, _b;
9131
9079
  await this.initialize();
9132
- if ((((_a3 = this.config.rag) == null ? void 0 : _a3.architecture) === "agentic" || ((_b = this.config.rag) == null ? void 0 : _b.architecture) === "multi-agent" || this.config.mcpServers && this.config.mcpServers.length > 0) && this.multiAgentCoordinator) {
9080
+ if ((((_a2 = this.config.rag) == null ? void 0 : _a2.architecture) === "agentic" || ((_b = this.config.rag) == null ? void 0 : _b.architecture) === "multi-agent" || this.config.mcpServers && this.config.mcpServers.length > 0) && this.multiAgentCoordinator) {
9133
9081
  return await this.multiAgentCoordinator.run(question, history);
9134
9082
  }
9135
9083
  const stream = this.askStream(question, history, namespace);
@@ -9164,9 +9112,9 @@ ${m.content}`).join("\n\n---\n\n");
9164
9112
  }
9165
9113
  askStream(_0) {
9166
9114
  return __asyncGenerator(this, arguments, function* (question, history = [], namespace) {
9167
- var _a3, _b;
9115
+ var _a2, _b;
9168
9116
  yield new __await(this.initialize());
9169
- if ((((_a3 = this.config.rag) == null ? void 0 : _a3.architecture) === "agentic" || ((_b = this.config.rag) == null ? void 0 : _b.architecture) === "multi-agent" || this.config.mcpServers && this.config.mcpServers.length > 0) && this.multiAgentCoordinator) {
9117
+ if ((((_a2 = this.config.rag) == null ? void 0 : _a2.architecture) === "agentic" || ((_b = this.config.rag) == null ? void 0 : _b.architecture) === "multi-agent" || this.config.mcpServers && this.config.mcpServers.length > 0) && this.multiAgentCoordinator) {
9170
9118
  const stream2 = this.multiAgentCoordinator.runStream(question, history);
9171
9119
  try {
9172
9120
  for (var iter = __forAwait(stream2), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
@@ -9215,10 +9163,10 @@ ${m.content}`).join("\n\n---\n\n");
9215
9163
  */
9216
9164
  askStreamInternal(_0) {
9217
9165
  return __asyncGenerator(this, arguments, function* (question, history = [], namespace) {
9218
- var _a3, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _A, _B;
9166
+ var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _A, _B;
9219
9167
  yield new __await(this.initialize());
9220
9168
  const ns = formatNamespace(namespace != null ? namespace : this.config.projectId);
9221
- const topK = (_b = (_a3 = this.config.rag) == null ? void 0 : _a3.topK) != null ? _b : 5;
9169
+ const topK = (_b = (_a2 = this.config.rag) == null ? void 0 : _a2.topK) != null ? _b : 5;
9222
9170
  const scoreThreshold = (_d = (_c = this.config.rag) == null ? void 0 : _c.scoreThreshold) != null ? _d : 0.3;
9223
9171
  const requestId = `req_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
9224
9172
  const requestStart = performance.now();
@@ -9267,8 +9215,8 @@ ${m.content}`).join("\n\n---\n\n");
9267
9215
  const rerankStart = performance.now();
9268
9216
  const structuredSources = this.applyStructuredFilters(rawSources, filter).filter((s) => Boolean(s && typeof s === "object"));
9269
9217
  let fullSources = hasNumericPredicates ? structuredSources : structuredSources.filter((m) => {
9270
- var _a4;
9271
- return ((_a4 = m == null ? void 0 : m.score) != null ? _a4 : 0) >= scoreThreshold;
9218
+ var _a3;
9219
+ return ((_a3 = m == null ? void 0 : m.score) != null ? _a3 : 0) >= scoreThreshold;
9272
9220
  });
9273
9221
  const rerankLimit = Math.max(retrievalLimit, fullSources.length);
9274
9222
  const useReranking = arch !== "naive" && (arch === "retrieve-and-rerank" || ((_m = this.config.rag) == null ? void 0 : _m.useReranking));
@@ -9281,13 +9229,13 @@ ${m.content}`).join("\n\n---\n\n");
9281
9229
  let context = promptSources.length ? `[SEARCH SUMMARY: Found ${totalCount} matching record(s) out of ${rawSources.length} total resources searched in database. Context includes top ${promptSources.length} relevant records]
9282
9230
 
9283
9231
  ` + promptSources.map((m, i) => {
9284
- var _a4;
9232
+ var _a3;
9285
9233
  return `[Source ${i + 1}]
9286
- ${(_a4 = m == null ? void 0 : m.content) != null ? _a4 : ""}`;
9234
+ ${(_a3 = m == null ? void 0 : m.content) != null ? _a3 : ""}`;
9287
9235
  }).join("\n\n---\n\n") : "No relevant context found.";
9288
9236
  const sources = [...fullSources].filter((s) => Boolean(s && typeof s === "object")).sort((a, b) => {
9289
- var _a4, _b2;
9290
- return ((_a4 = b == null ? void 0 : b.score) != null ? _a4 : 0) - ((_b2 = a == null ? void 0 : a.score) != null ? _b2 : 0);
9237
+ var _a3, _b2;
9238
+ return ((_a3 = b == null ? void 0 : b.score) != null ? _a3 : 0) - ((_b2 = a == null ? void 0 : a.score) != null ? _b2 : 0);
9291
9239
  });
9292
9240
  if (graphData && graphData.nodes.length > 0) {
9293
9241
  const graphContext = graphData.nodes.map(
@@ -9315,10 +9263,10 @@ ${context}`;
9315
9263
  yield {
9316
9264
  reply: "",
9317
9265
  sources: (sources || []).filter((s) => Boolean(s && typeof s === "object")).map((s) => {
9318
- var _a4, _b2, _c2;
9266
+ var _a3, _b2, _c2;
9319
9267
  return {
9320
9268
  id: s.id,
9321
- score: (_a4 = s.score) != null ? _a4 : 0,
9269
+ score: (_a3 = s.score) != null ? _a3 : 0,
9322
9270
  content: (_b2 = s == null ? void 0 : s.content) != null ? _b2 : "",
9323
9271
  metadata: (_c2 = s == null ? void 0 : s.metadata) != null ? _c2 : {},
9324
9272
  namespace: ns
@@ -9511,11 +9459,11 @@ ${context}`;
9511
9459
  systemPrompt,
9512
9460
  userPrompt: question + finalRestrictionSuffix,
9513
9461
  chunks: (sources || []).filter(Boolean).map((s) => {
9514
- var _a4, _b2;
9462
+ var _a3, _b2;
9515
9463
  return {
9516
9464
  id: s.id,
9517
9465
  score: s.score,
9518
- content: (_a4 = s == null ? void 0 : s.content) != null ? _a4 : "",
9466
+ content: (_a3 = s == null ? void 0 : s.content) != null ? _a3 : "",
9519
9467
  metadata: (_b2 = s == null ? void 0 : s.metadata) != null ? _b2 : {},
9520
9468
  namespace: ns
9521
9469
  };
@@ -9534,7 +9482,7 @@ ${context}`;
9534
9482
  const telemetryUrl = ((_B = this.config.telemetry) == null ? void 0 : _B.url) || process.env.TELEMETRY_URL || process.env.NEXT_PUBLIC_TELEMETRY_URL || defaultUrl;
9535
9483
  const absoluteUrl = telemetryUrl.startsWith("http") ? telemetryUrl : "https://www.retrivora.com" + (telemetryUrl.startsWith("/") ? telemetryUrl : "/" + telemetryUrl);
9536
9484
  (async () => {
9537
- var _a4, _b2, _c2, _d2, _e2;
9485
+ var _a3, _b2, _c2, _d2, _e2;
9538
9486
  try {
9539
9487
  let finalTrace = trace;
9540
9488
  if (!awaitHallucination && runHallucination) {
@@ -9543,7 +9491,7 @@ ${context}`;
9543
9491
  finalTrace = buildTrace(backgroundScoreResult);
9544
9492
  }
9545
9493
  }
9546
- const modelName = (finalTrace == null ? void 0 : finalTrace.model) || ((_a4 = this.config.llm) == null ? void 0 : _a4.model) || "llama-3.1-8b-instant";
9494
+ const modelName = (finalTrace == null ? void 0 : finalTrace.model) || ((_a3 = this.config.llm) == null ? void 0 : _a3.model) || "llama-3.1-8b-instant";
9547
9495
  const providerName = (finalTrace == null ? void 0 : finalTrace.provider) || ((_b2 = this.config.llm) == null ? void 0 : _b2.provider) || "groq";
9548
9496
  const tokenCount = Number(((_c2 = finalTrace == null ? void 0 : finalTrace.tokens) == null ? void 0 : _c2.totalTokens) || 0);
9549
9497
  const costEst = Number(((_d2 = finalTrace == null ? void 0 : finalTrace.tokens) == null ? void 0 : _d2.estimatedCostUsd) || 0);
@@ -9597,7 +9545,7 @@ ${context}`;
9597
9545
  * Uses an LRU-bounded embedding cache to avoid re-embedding the same query.
9598
9546
  */
9599
9547
  async generateUiTransformation(question, sources, cachedSchema, forceDeterministic = false) {
9600
- var _a3;
9548
+ var _a2;
9601
9549
  if (!sources || sources.length === 0) {
9602
9550
  return UITransformer.transform(question, sources, this.config, cachedSchema);
9603
9551
  }
@@ -9610,7 +9558,7 @@ ${context}`;
9610
9558
  { visualizationHint: "product_browse", recommendedChart: "text", filterInStockOnly: false, wantsExplicitTable: false, isTemporal: false, isComparison: false, language: "en" }
9611
9559
  );
9612
9560
  }
9613
- const enableLlmUiTransform = ((_a3 = this.config.llm.options) == null ? void 0 : _a3.enableLlmUiTransform) === true;
9561
+ const enableLlmUiTransform = ((_a2 = this.config.llm.options) == null ? void 0 : _a2.enableLlmUiTransform) === true;
9614
9562
  if (forceDeterministic || !enableLlmUiTransform) {
9615
9563
  return UITransformer.transform(question, sources, this.config, cachedSchema);
9616
9564
  }
@@ -9628,15 +9576,15 @@ ${context}`;
9628
9576
  const value = this.resolveNumericPredicateValue(source, predicate);
9629
9577
  return value !== null && this.matchesNumericPredicate(value, predicate);
9630
9578
  })).sort((a, b) => {
9631
- var _a3, _b;
9579
+ var _a2, _b;
9632
9580
  const primary = predicates[0];
9633
- const aValue = (_a3 = this.resolveNumericPredicateValue(a, primary)) != null ? _a3 : 0;
9581
+ const aValue = (_a2 = this.resolveNumericPredicateValue(a, primary)) != null ? _a2 : 0;
9634
9582
  const bValue = (_b = this.resolveNumericPredicateValue(b, primary)) != null ? _b : 0;
9635
9583
  return primary.operator === "lt" || primary.operator === "lte" ? aValue - bValue : bValue - aValue;
9636
9584
  });
9637
9585
  }
9638
9586
  resolveNumericPredicateValue(source, predicate) {
9639
- var _a3;
9587
+ var _a2;
9640
9588
  if (!source) return null;
9641
9589
  const meta = source.metadata || {};
9642
9590
  const field = predicate.field;
@@ -9649,7 +9597,7 @@ ${context}`;
9649
9597
  const value = this.toFiniteNumber(Array.isArray(fuzzy) ? fuzzy[1] : fuzzy.value);
9650
9598
  if (value !== null) return value;
9651
9599
  }
9652
- const contentValue = this.extractNumericValueFromContent((_a3 = source.content) != null ? _a3 : "", field);
9600
+ const contentValue = this.extractNumericValueFromContent((_a2 = source.content) != null ? _a2 : "", field);
9653
9601
  if (contentValue !== null) return contentValue;
9654
9602
  }
9655
9603
  for (const [key, value] of entries) {
@@ -9705,8 +9653,8 @@ ${context}`;
9705
9653
  return Number.isFinite(numeric) ? numeric : null;
9706
9654
  }
9707
9655
  async retrieve(query, options) {
9708
- var _a3, _b, _c, _d, _e, _f, _g2;
9709
- const ns = formatNamespace((_a3 = options.namespace) != null ? _a3 : this.config.projectId);
9656
+ var _a2, _b, _c, _d, _e, _f, _g2;
9657
+ const ns = formatNamespace((_a2 = options.namespace) != null ? _a2 : this.config.projectId);
9710
9658
  const topK = (_b = options.topK) != null ? _b : 5;
9711
9659
  const cacheKey = `${ns}::${query}`;
9712
9660
  let queryVector = this.embeddingCache.get(cacheKey);
@@ -9751,11 +9699,11 @@ ${context}`;
9751
9699
  namespace: ns,
9752
9700
  count: resolvedSources.length,
9753
9701
  sample: resolvedSources.slice(0, 2).map((s) => {
9754
- var _a4;
9702
+ var _a3;
9755
9703
  return {
9756
9704
  id: s == null ? void 0 : s.id,
9757
9705
  score: s == null ? void 0 : s.score,
9758
- contentSnippet: String((_a4 = s == null ? void 0 : s.content) != null ? _a4 : "").substring(0, 150),
9706
+ contentSnippet: String((_a3 = s == null ? void 0 : s.content) != null ? _a3 : "").substring(0, 150),
9759
9707
  metadata: s == null ? void 0 : s.metadata
9760
9708
  };
9761
9709
  })
@@ -9769,8 +9717,8 @@ Focus on extracting the core intent and entities. Do not answer the question, ju
9769
9717
 
9770
9718
  History:
9771
9719
  ${(history || []).map((m) => {
9772
- var _a3;
9773
- return `${(m == null ? void 0 : m.role) || "user"}: ${(_a3 = m == null ? void 0 : m.content) != null ? _a3 : ""}`;
9720
+ var _a2;
9721
+ return `${(m == null ? void 0 : m.role) || "user"}: ${(_a2 = m == null ? void 0 : m.content) != null ? _a2 : ""}`;
9774
9722
  }).join("\n")}
9775
9723
 
9776
9724
  New Question: ${question}
@@ -9797,8 +9745,8 @@ Optimized Search Query:`;
9797
9745
  const { sources } = await this.retrieve(query, { namespace: ns, topK: 5 });
9798
9746
  if (!sources || sources.length === 0) return [];
9799
9747
  const context = sources.map((s) => {
9800
- var _a3;
9801
- return (_a3 = s == null ? void 0 : s.content) != null ? _a3 : "";
9748
+ var _a2;
9749
+ return (_a2 = s == null ? void 0 : s.content) != null ? _a2 : "";
9802
9750
  }).join("\n\n---\n\n");
9803
9751
  const prompt = `Based on the following snippets from a document, what are at least 5 short, relevant questions a user might ask?
9804
9752
  Focus on questions that can be answered by the context.
@@ -9937,186 +9885,9 @@ var ProviderHealthCheck = class {
9937
9885
  };
9938
9886
 
9939
9887
  // src/core/FreeTierLimitsGuard.ts
9940
- var FREE_TIER_QUOTAS = {
9941
- MAX_DOCUMENTS: 25,
9942
- MAX_FILE_SIZE_BYTES: 20 * 1024 * 1024,
9943
- MAX_STORAGE_BYTES: 250 * 1024 * 1024,
9944
- MAX_TRIAL_REQUEST_UNITS: 500,
9945
- MAX_DAILY_REQUEST_UNITS: 50,
9946
- TRIAL_DURATION_DAYS: 14,
9947
- GRACE_PERIOD_DAYS: 2,
9948
- MAX_RPM: 10,
9949
- MAX_RPH: 100,
9950
- MAX_RPD: 500,
9951
- MAX_CONCURRENT_REQUESTS: 2,
9952
- MAX_INPUT_TOKENS: 2e4,
9953
- MAX_OUTPUT_TOKENS: 4e3,
9954
- UPGRADE_REMINDER_DAYS: [7, 12, 14],
9955
- MAX_USERS: 1,
9956
- MAX_NAMESPACES: 1,
9957
- MAX_PROJECTS: 1
9958
- };
9959
- var FreeTierLimitsGuard = class {
9960
- static calculateTrialTimestamps(startedAt) {
9961
- const startDate = startedAt ? new Date(startedAt) : /* @__PURE__ */ new Date();
9962
- const trialStartedAtMs = startDate.getTime();
9963
- const trialExpiresAtMs = trialStartedAtMs + FREE_TIER_QUOTAS.TRIAL_DURATION_DAYS * 24 * 60 * 60 * 1e3;
9964
- const gracePeriodExpiresAtMs = trialStartedAtMs + (FREE_TIER_QUOTAS.TRIAL_DURATION_DAYS + FREE_TIER_QUOTAS.GRACE_PERIOD_DAYS) * 24 * 60 * 60 * 1e3;
9965
- return {
9966
- trial_started_at: new Date(trialStartedAtMs).toISOString(),
9967
- trial_expires_at: new Date(trialExpiresAtMs).toISOString(),
9968
- grace_period_expires_at: new Date(gracePeriodExpiresAtMs).toISOString()
9969
- };
9970
- }
9971
- static calculateRequestUnits(operationType, options) {
9972
- var _a3;
9973
- const op = operationType.toLowerCase().trim();
9974
- if (op.includes("suggestion") || op.includes("template") || op.includes("history") || op.includes("model") || op.includes("health") || op.includes("auth") || op.includes("license") || op.includes("usage") || op.includes("feedback") || op.includes("analytic")) {
9975
- return 0;
9976
- }
9977
- if (op.includes("image")) {
9978
- return 10;
9979
- }
9980
- if (op.includes("embedding")) {
9981
- const chunks = (_a3 = options == null ? void 0 : options.chunkCount) != null ? _a3 : 1;
9982
- return Math.max(1, Math.ceil(chunks / 1e3));
9983
- }
9984
- return 1;
9985
- }
9986
- static checkTrialStatus(startedAt, totalUnitsUsed = 0, nowServerTime = /* @__PURE__ */ new Date()) {
9987
- const timestamps = this.calculateTrialTimestamps(startedAt);
9988
- const startMs = new Date(timestamps.trial_started_at).getTime();
9989
- const expireMs = new Date(timestamps.trial_expires_at).getTime();
9990
- const graceExpireMs = new Date(timestamps.grace_period_expires_at).getTime();
9991
- const nowMs = nowServerTime.getTime();
9992
- const daysElapsed = Math.floor(Math.max(0, nowMs - startMs) / (1e3 * 60 * 60 * 24));
9993
- const daysRemaining = Math.max(0, Math.ceil((expireMs - nowMs) / (1e3 * 60 * 60 * 24)));
9994
- const isConsumedUnits = totalUnitsUsed >= FREE_TIER_QUOTAS.MAX_TRIAL_REQUEST_UNITS;
9995
- const isExpiredTime = nowMs > expireMs;
9996
- const isPastGraceTime = nowMs > graceExpireMs;
9997
- let status = "active";
9998
- let reason;
9999
- if (isPastGraceTime || isConsumedUnits && isExpiredTime) {
10000
- status = "expired";
10001
- reason = "Trial period and grace period have expired. Upgrade your plan.";
10002
- } else if (isExpiredTime || isConsumedUnits) {
10003
- status = "grace_period";
10004
- reason = isConsumedUnits ? "Total trial request quota (500 units) consumed. Grace period active (dashboard access only)." : "14-day trial period expired. 2-day grace period active (dashboard access only).";
10005
- }
10006
- let upgradeReminderDue;
10007
- if (FREE_TIER_QUOTAS.UPGRADE_REMINDER_DAYS.includes(daysElapsed)) {
10008
- upgradeReminderDue = daysElapsed;
10009
- }
10010
- let usageReminderDue;
10011
- const usagePercent = totalUnitsUsed / FREE_TIER_QUOTAS.MAX_TRIAL_REQUEST_UNITS * 100;
10012
- if (usagePercent >= 100) {
10013
- usageReminderDue = "100%";
10014
- } else if (usagePercent >= 90) {
10015
- usageReminderDue = "90%";
10016
- } else if (usagePercent >= 80) {
10017
- usageReminderDue = "80%";
10018
- }
10019
- return {
10020
- status,
10021
- daysElapsed,
10022
- daysRemaining,
10023
- isGracePeriod: status === "grace_period",
10024
- isExpired: status === "expired",
10025
- upgradeReminderDue,
10026
- usageReminderDue,
10027
- reason
10028
- };
10029
- }
10030
- static checkIngestionAllowed(stats, incomingSizeBytes = 0) {
10031
- var _a3, _b;
10032
- const docs = (_a3 = stats.documentCount) != null ? _a3 : 0;
10033
- const storage = (_b = stats.totalStorageBytes) != null ? _b : 0;
10034
- if (incomingSizeBytes > FREE_TIER_QUOTAS.MAX_FILE_SIZE_BYTES) {
10035
- const mbSize = (incomingSizeBytes / (1024 * 1024)).toFixed(1);
10036
- return {
10037
- allowed: false,
10038
- reason: `[FreeTierIngestor] File size (${mbSize}MB) exceeds maximum allowed size of 20MB under Free Tier rules.`
10039
- };
10040
- }
10041
- if (docs >= FREE_TIER_QUOTAS.MAX_DOCUMENTS) {
10042
- return {
10043
- allowed: false,
10044
- reason: `Document limit reached (${docs}/${FREE_TIER_QUOTAS.MAX_DOCUMENTS}). Upgrade to Pro to ingest more documents.`
10045
- };
10046
- }
10047
- if (storage + incomingSizeBytes > FREE_TIER_QUOTAS.MAX_STORAGE_BYTES) {
10048
- const mbUsed = (storage / (1024 * 1024)).toFixed(1);
10049
- return {
10050
- allowed: false,
10051
- reason: `Storage quota exceeded (${mbUsed}MB / 250MB). Upgrade for unlimited storage.`
10052
- };
10053
- }
10054
- return { allowed: true };
10055
- }
10056
- static checkRequestAllowed(params) {
10057
- var _a3, _b, _c;
10058
- const op = (_a3 = params.operationType) != null ? _a3 : "chat";
10059
- const costUnits = this.calculateRequestUnits(op, { chunkCount: params.chunkCount });
10060
- if (costUnits === 0) {
10061
- return { allowed: true, costUnits: 0 };
10062
- }
10063
- if (params.inputTokens && params.inputTokens > FREE_TIER_QUOTAS.MAX_INPUT_TOKENS) {
10064
- return {
10065
- allowed: false,
10066
- reason: "Token limit exceeded. Upgrade your plan.",
10067
- costUnits
10068
- };
10069
- }
10070
- if (params.outputTokens && params.outputTokens > FREE_TIER_QUOTAS.MAX_OUTPUT_TOKENS) {
10071
- return {
10072
- allowed: false,
10073
- reason: "Token limit exceeded. Upgrade your plan.",
10074
- costUnits
10075
- };
10076
- }
10077
- if (params.concurrentRequests && params.concurrentRequests > FREE_TIER_QUOTAS.MAX_CONCURRENT_REQUESTS) {
10078
- return {
10079
- allowed: false,
10080
- reason: `Concurrent limit exceeded (max ${FREE_TIER_QUOTAS.MAX_CONCURRENT_REQUESTS} active requests). Please wait for active streams to finish.`,
10081
- costUnits
10082
- };
10083
- }
10084
- const totalUnits = (_b = params.totalUnitsUsed) != null ? _b : 0;
10085
- const trialStatus = this.checkTrialStatus(params.trialStartedAt, totalUnits, params.nowServerTime);
10086
- if (trialStatus.status === "expired" || trialStatus.status === "grace_period") {
10087
- return {
10088
- allowed: false,
10089
- reason: trialStatus.reason || "Trial limit reached. Upgrade your plan.",
10090
- costUnits
10091
- };
10092
- }
10093
- if (totalUnits + costUnits > FREE_TIER_QUOTAS.MAX_TRIAL_REQUEST_UNITS) {
10094
- return {
10095
- allowed: false,
10096
- reason: `Total trial request unit limit reached (${totalUnits}/${FREE_TIER_QUOTAS.MAX_TRIAL_REQUEST_UNITS}). Upgrade your plan.`,
10097
- costUnits
10098
- };
10099
- }
10100
- const dailyUnits = (_c = params.dailyUnitsUsed) != null ? _c : 0;
10101
- if (dailyUnits + costUnits > FREE_TIER_QUOTAS.MAX_DAILY_REQUEST_UNITS) {
10102
- return {
10103
- allowed: false,
10104
- reason: `Daily request quota limit reached (${dailyUnits}/${FREE_TIER_QUOTAS.MAX_DAILY_REQUEST_UNITS} units/day). Quota resets tomorrow.`,
10105
- costUnits
10106
- };
10107
- }
10108
- return { allowed: true, costUnits };
10109
- }
10110
- static checkQueryAllowed(dailyQueriesCount = 0) {
10111
- if (dailyQueriesCount >= FREE_TIER_QUOTAS.MAX_DAILY_REQUEST_UNITS) {
10112
- return {
10113
- allowed: false,
10114
- reason: `Daily query limit reached (${FREE_TIER_QUOTAS.MAX_DAILY_REQUEST_UNITS}/day). Quota resets tomorrow.`
10115
- };
10116
- }
10117
- return { allowed: true };
10118
- }
10119
- };
9888
+ var FreeTierLimitsGuard_exports = {};
9889
+ __reExport(FreeTierLimitsGuard_exports, billing_guards_star);
9890
+ import * as billing_guards_star from "@retrivora-ai/billing-guards";
10120
9891
 
10121
9892
  // src/core/VectorPlugin.ts
10122
9893
  var VectorPlugin = class {
@@ -10124,12 +9895,12 @@ var VectorPlugin = class {
10124
9895
  const resolvedConfig = ConfigResolver.resolve(hostConfig);
10125
9896
  this.config = resolvedConfig;
10126
9897
  this.validationPromise = (async () => {
10127
- var _a3;
9898
+ var _a2;
10128
9899
  await ConfigValidator.validateAndThrow(resolvedConfig);
10129
9900
  await LicenseVerifier.verifyAsync(
10130
9901
  resolvedConfig.licenseKey,
10131
9902
  resolvedConfig.projectId,
10132
- (_a3 = resolvedConfig.vectorDb) == null ? void 0 : _a3.provider
9903
+ (_a2 = resolvedConfig.vectorDb) == null ? void 0 : _a2.provider
10133
9904
  );
10134
9905
  })();
10135
9906
  this.validationPromise.catch(() => {
@@ -10165,8 +9936,8 @@ var VectorPlugin = class {
10165
9936
  }
10166
9937
  estimateInputTokens(message, history = []) {
10167
9938
  const allContent = message.length + history.reduce((acc, m) => {
10168
- var _a3, _b;
10169
- return acc + (((_a3 = m == null ? void 0 : m.content) == null ? void 0 : _a3.length) || ((_b = m == null ? void 0 : m.text) == null ? void 0 : _b.length) || 0);
9939
+ var _a2, _b;
9940
+ return acc + (((_a2 = m == null ? void 0 : m.content) == null ? void 0 : _a2.length) || ((_b = m == null ? void 0 : m.text) == null ? void 0 : _b.length) || 0);
10170
9941
  }, 0);
10171
9942
  return Math.ceil(allContent / 4);
10172
9943
  }
@@ -10174,7 +9945,7 @@ var VectorPlugin = class {
10174
9945
  * Run a chat query.
10175
9946
  */
10176
9947
  async chat(message, history = [], namespace) {
10177
- var _a3;
9948
+ var _a2;
10178
9949
  try {
10179
9950
  await this.validationPromise;
10180
9951
  } catch (err) {
@@ -10182,11 +9953,11 @@ var VectorPlugin = class {
10182
9953
  }
10183
9954
  try {
10184
9955
  const inputTokens = this.estimateInputTokens(message, history);
10185
- const tokenCheck = FreeTierLimitsGuard.checkRequestAllowed({
9956
+ const tokenCheck = FreeTierLimitsGuard_exports.FreeTierLimitsGuard.checkRequestAllowed({
10186
9957
  operationType: "chat",
10187
9958
  inputTokens
10188
9959
  });
10189
- if (!tokenCheck.allowed && ((_a3 = tokenCheck.reason) == null ? void 0 : _a3.toLowerCase().includes("token"))) {
9960
+ if (!tokenCheck.allowed && ((_a2 = tokenCheck.reason) == null ? void 0 : _a2.toLowerCase().includes("token"))) {
10190
9961
  throw wrapError(new Error(tokenCheck.reason), "RATE_LIMITED");
10191
9962
  }
10192
9963
  return await this.pipeline.ask(message, history, namespace);
@@ -10207,7 +9978,7 @@ var VectorPlugin = class {
10207
9978
  */
10208
9979
  chatStream(_0) {
10209
9980
  return __asyncGenerator(this, arguments, function* (message, history = [], namespace) {
10210
- var _a3;
9981
+ var _a2;
10211
9982
  try {
10212
9983
  yield new __await(this.validationPromise);
10213
9984
  } catch (err) {
@@ -10215,11 +9986,11 @@ var VectorPlugin = class {
10215
9986
  }
10216
9987
  try {
10217
9988
  const inputTokens = this.estimateInputTokens(message, history);
10218
- const tokenCheck = FreeTierLimitsGuard.checkRequestAllowed({
9989
+ const tokenCheck = FreeTierLimitsGuard_exports.FreeTierLimitsGuard.checkRequestAllowed({
10219
9990
  operationType: "chat_stream",
10220
9991
  inputTokens
10221
9992
  });
10222
- if (!tokenCheck.allowed && ((_a3 = tokenCheck.reason) == null ? void 0 : _a3.toLowerCase().includes("token"))) {
9993
+ if (!tokenCheck.allowed && ((_a2 = tokenCheck.reason) == null ? void 0 : _a2.toLowerCase().includes("token"))) {
10223
9994
  throw wrapError(new Error(tokenCheck.reason), "RATE_LIMITED");
10224
9995
  }
10225
9996
  const stream = this.pipeline.askStream(message, history, namespace);
@@ -10294,8 +10065,8 @@ var DocumentParser = class {
10294
10065
  * Extract text from a File or Buffer based on its type.
10295
10066
  */
10296
10067
  static async parse(file, fileName, mimeType) {
10297
- var _a3;
10298
- const extension = ((_a3 = fileName.split(".").pop()) == null ? void 0 : _a3.toLowerCase()) || "";
10068
+ var _a2;
10069
+ const extension = ((_a2 = fileName.split(".").pop()) == null ? void 0 : _a2.toLowerCase()) || "";
10299
10070
  if (extension === "txt" || extension === "md" || mimeType === "text/plain" || mimeType === "text/markdown") {
10300
10071
  return this.readAsText(file);
10301
10072
  }
@@ -10374,7 +10145,17 @@ ${csv.trim()}`);
10374
10145
  import * as fs from "fs";
10375
10146
  import * as path from "path";
10376
10147
  import * as os from "os";
10377
- var DatabaseStorage = class {
10148
+ function validateTableName(name) {
10149
+ if (!/^[a-zA-Z_][a-zA-Z0-9_]{0,62}$/.test(name)) {
10150
+ throw new Error(
10151
+ `[DatabaseStorage] Invalid table name "${name}". Table names must start with a letter or underscore and contain only alphanumeric characters and underscores (max 63 chars).`
10152
+ );
10153
+ }
10154
+ return name;
10155
+ }
10156
+ var _pgInitPromises = /* @__PURE__ */ new Map();
10157
+ var _DatabaseStorage = class _DatabaseStorage {
10158
+ // 5 minutes
10378
10159
  constructor(config) {
10379
10160
  // Dynamic PG Pool
10380
10161
  this.pgPool = null;
@@ -10386,36 +10167,50 @@ var DatabaseStorage = class {
10386
10167
  this.fallbackDir = this.isServerless ? path.join(os.tmpdir(), ".retrivora") : path.join(process.cwd(), ".retrivora");
10387
10168
  this.historyFile = path.join(this.fallbackDir, "history.json");
10388
10169
  this.feedbackFile = path.join(this.fallbackDir, "feedback.json");
10389
- var _a3, _b, _c, _d, _e;
10170
+ /** Subscription check result cache (Fix PERF-1). */
10171
+ this.subscriptionCheckedAt = 0;
10172
+ var _a2, _b, _c, _d, _e;
10390
10173
  this.config = config;
10391
- this.provider = ((_a3 = config.vectorDb) == null ? void 0 : _a3.provider) || "fs";
10174
+ this.provider = ((_a2 = config.vectorDb) == null ? void 0 : _a2.provider) || "fs";
10392
10175
  const rawHistoryTable = ((_c = (_b = config.rag) == null ? void 0 : _b.history) == null ? void 0 : _c.tableName) || "retrivora_history";
10393
10176
  const rawFeedbackTable = ((_e = (_d = config.rag) == null ? void 0 : _d.feedback) == null ? void 0 : _e.tableName) || "retrivora_feedback";
10394
- this.historyTableName = rawHistoryTable.replace(/[^a-zA-Z0-9_]/g, "_");
10395
- this.feedbackTableName = rawFeedbackTable.replace(/[^a-zA-Z0-9_]/g, "_");
10177
+ this.historyTableName = validateTableName(rawHistoryTable);
10178
+ this.feedbackTableName = validateTableName(rawFeedbackTable);
10396
10179
  }
10397
10180
  /**
10398
- * Asserts the user is running a valid Premium/Enterprise license in production.
10399
- * In non-production (development / test) environments this is a complete no-op —
10400
- * all History and Feedback features are freely accessible for local development.
10181
+ * Checks that the caller holds a valid paid-tier license in production.
10182
+ *
10183
+ * Fix PERF-1: result is cached for 5 minutes so we don't call
10184
+ * LicenseVerifier.verifyAsync() on every DB operation (saveMessage,
10185
+ * getHistory, etc.).
10186
+ *
10187
+ * Fix PERF-2: renamed from `checkPremiumSubscription` to clarify intent.
10188
+ * The previous allowlist admitted all free tiers, defeating the gate.
10189
+ * The new allowlist only passes paid tiers. Free-tier access to history
10190
+ * and feedback is a product decision — update the constant below if the
10191
+ * policy changes.
10192
+ *
10193
+ * In non-production environments this is a complete no-op so local
10194
+ * development works without a license key.
10401
10195
  */
10402
- async checkPremiumSubscription() {
10403
- if (process.env.NODE_ENV !== "production") {
10404
- return;
10405
- }
10196
+ async checkPaidSubscription() {
10197
+ if (process.env.NODE_ENV !== "production") return;
10198
+ if (Date.now() - this.subscriptionCheckedAt < _DatabaseStorage.SUBSCRIPTION_CACHE_TTL_MS) return;
10406
10199
  if (!this.config.licenseKey) {
10407
10200
  throw new Error(
10408
- "[Retrivora SDK] Chat History and Feedback features require a Premium or Enterprise license key in production."
10201
+ "[Retrivora SDK] Chat History and Feedback features require a paid license key in production."
10409
10202
  );
10410
10203
  }
10411
10204
  try {
10412
10205
  const payload = await LicenseVerifier.verifyAsync(this.config.licenseKey, this.config.projectId);
10413
10206
  const tier = (payload.tier || "").toLowerCase();
10414
- if (tier !== "premium" && tier !== "enterprise" && tier !== "growth" && tier !== "pro" && tier !== "developer_pro" && tier !== "free_trial" && tier !== "free_tier" && tier !== "free" && tier !== "hobby") {
10207
+ const PAID_TIERS = /* @__PURE__ */ new Set(["premium", "enterprise", "growth", "pro", "developer_pro"]);
10208
+ if (!PAID_TIERS.has(tier)) {
10415
10209
  throw new Error(
10416
10210
  `[Retrivora SDK] Chat History and Feedback features are not available on the "${tier}" tier. Please upgrade to a Developer Pro or Enterprise plan.`
10417
10211
  );
10418
10212
  }
10213
+ this.subscriptionCheckedAt = Date.now();
10419
10214
  } catch (err) {
10420
10215
  throw new Error(
10421
10216
  `[Retrivora SDK] Subscription check failed: ${err instanceof Error ? err.message : String(err)}`
@@ -10423,34 +10218,45 @@ var DatabaseStorage = class {
10423
10218
  }
10424
10219
  }
10425
10220
  async checkHistoryEnabled() {
10426
- var _a3, _b;
10427
- await this.checkPremiumSubscription();
10428
- if (((_b = (_a3 = this.config.rag) == null ? void 0 : _a3.history) == null ? void 0 : _b.enabled) === false) {
10221
+ var _a2, _b;
10222
+ await this.checkPaidSubscription();
10223
+ if (((_b = (_a2 = this.config.rag) == null ? void 0 : _a2.history) == null ? void 0 : _b.enabled) === false) {
10429
10224
  throw new Error("[Retrivora SDK] Chat History is disabled in RagConfig.");
10430
10225
  }
10431
10226
  }
10432
10227
  async checkFeedbackEnabled() {
10433
- var _a3, _b;
10434
- await this.checkPremiumSubscription();
10435
- if (((_b = (_a3 = this.config.rag) == null ? void 0 : _a3.feedback) == null ? void 0 : _b.enabled) === false) {
10228
+ var _a2, _b;
10229
+ await this.checkPaidSubscription();
10230
+ if (((_b = (_a2 = this.config.rag) == null ? void 0 : _a2.feedback) == null ? void 0 : _b.enabled) === false) {
10436
10231
  throw new Error("[Retrivora SDK] User Feedback is disabled in RagConfig.");
10437
10232
  }
10438
10233
  }
10439
- async getPGPool() {
10440
- const globalKey = `__retrivora_pg_pool_${this.config.projectId}`;
10441
- const _g2 = global;
10442
- if (_g2[globalKey]) {
10443
- this.pgPool = _g2[globalKey];
10444
- return this.pgPool;
10234
+ /**
10235
+ * Returns (or lazily creates) the PostgreSQL connection pool for this project.
10236
+ *
10237
+ * Fix ERR-2: the previous implementation had a double-initialization race
10238
+ * condition — two concurrent requests would both find _g[globalKey] missing,
10239
+ * both create a Pool, and the second Pool would be orphaned, leaking
10240
+ * database connections. This version stores a single Promise in the module-
10241
+ * level _pgInitPromises map so all concurrent callers await the same
10242
+ * initialization without creating duplicate pools.
10243
+ */
10244
+ getPGPool() {
10245
+ const globalKey = `pg:${this.config.projectId}`;
10246
+ if (!_pgInitPromises.has(globalKey)) {
10247
+ _pgInitPromises.set(globalKey, this.createPGPool());
10445
10248
  }
10249
+ return _pgInitPromises.get(globalKey);
10250
+ }
10251
+ async createPGPool() {
10446
10252
  const opts = this.config.vectorDb.options;
10447
10253
  if (!opts.connectionString) {
10448
10254
  throw new Error("[DatabaseStorage] pg connectionString option is missing.");
10449
10255
  }
10450
10256
  const { Pool: Pool3 } = await import("pg");
10451
- this.pgPool = new Pool3({ connectionString: opts.connectionString });
10452
- _g2[globalKey] = this.pgPool;
10453
- const client = await this.pgPool.connect();
10257
+ const pool = new Pool3({ connectionString: opts.connectionString });
10258
+ this.pgPool = pool;
10259
+ const client = await pool.connect();
10454
10260
  try {
10455
10261
  await client.query(`
10456
10262
  CREATE TABLE IF NOT EXISTS ${this.historyTableName} (
@@ -10477,7 +10283,7 @@ var DatabaseStorage = class {
10477
10283
  } finally {
10478
10284
  client.release();
10479
10285
  }
10480
- return this.pgPool;
10286
+ return pool;
10481
10287
  }
10482
10288
  async getMongoClient() {
10483
10289
  const globalKey = `__retrivora_mongo_client_${this.config.projectId}`;
@@ -10657,17 +10463,21 @@ var DatabaseStorage = class {
10657
10463
  }
10658
10464
  }
10659
10465
  /**
10660
- * Return true when `sessionId` falls into the allowed `projectIds` scope.
10661
- * When `projectIds` is empty / undefined we allow the call (legacy behavior
10662
- * for non-multi-tenant consumers). Session ids typically begin with the
10663
- * project id they were created for, so we prefix-match as well as contains-match
10664
- * to catch sharded / suffix-style ids used by some integrations.
10466
+ * Returns true when `sessionId` is in scope for the given `projectIds`.
10467
+ *
10468
+ * Fix RAG-5: the previous implementation used `sessionId.includes(pid)` which
10469
+ * allows cross-tenant data leakage. If a project ID "abc" is a substring of
10470
+ * another tenant's session ID (e.g., "xyz-abc-user-123"), the check would
10471
+ * incorrectly return true. We now only allow exact match or strict delimiter-
10472
+ * separated prefix match to prevent this attack.
10665
10473
  */
10666
10474
  sessionInScope(sessionId, projectIds) {
10667
10475
  if (!projectIds || projectIds.length === 0) return true;
10668
- return projectIds.some(
10669
- (pid) => pid && (sessionId === pid || sessionId.startsWith(pid) || sessionId.includes(pid))
10670
- );
10476
+ return projectIds.some((pid) => {
10477
+ if (!pid) return false;
10478
+ if (sessionId === pid) return true;
10479
+ return sessionId.startsWith(`${pid}-`) || sessionId.startsWith(`${pid}_`) || sessionId.startsWith(`${pid}/`);
10480
+ });
10671
10481
  }
10672
10482
  async listSessions(projectIds) {
10673
10483
  await this.checkHistoryEnabled();
@@ -10816,9 +10626,12 @@ var DatabaseStorage = class {
10816
10626
  }
10817
10627
  }
10818
10628
  };
10629
+ _DatabaseStorage.SUBSCRIPTION_CACHE_TTL_MS = 5 * 60 * 1e3;
10630
+ var DatabaseStorage = _DatabaseStorage;
10819
10631
 
10820
10632
  // src/handlers/index.ts
10821
10633
  init_version();
10634
+ import { createHash } from "crypto";
10822
10635
  async function checkAuth(req, onAuthorize) {
10823
10636
  if (!onAuthorize) return null;
10824
10637
  try {
@@ -10840,6 +10653,17 @@ var RateLimiter = class {
10840
10653
  this.hits = /* @__PURE__ */ new Map();
10841
10654
  this.windowMs = windowMs;
10842
10655
  this.max = max;
10656
+ this.pruneTimer = setInterval(() => this.prune(), windowMs);
10657
+ if (typeof this.pruneTimer === "object" && "unref" in this.pruneTimer) {
10658
+ this.pruneTimer.unref();
10659
+ }
10660
+ }
10661
+ /** Prune entries whose timestamps are all outside the window. */
10662
+ prune() {
10663
+ const cutoff = Date.now() - this.windowMs;
10664
+ for (const [k, timestamps] of this.hits.entries()) {
10665
+ if (timestamps.every((t) => t <= cutoff)) this.hits.delete(k);
10666
+ }
10843
10667
  }
10844
10668
  /** Returns true if the request should be allowed, false if rate-limited. */
10845
10669
  allow(key) {
@@ -10848,11 +10672,6 @@ var RateLimiter = class {
10848
10672
  const existing = (this.hits.get(key) || []).filter((t) => t > cutoff);
10849
10673
  existing.push(now);
10850
10674
  this.hits.set(key, existing);
10851
- if (this.hits.size > 5e3) {
10852
- for (const [k, timestamps] of this.hits.entries()) {
10853
- if (timestamps.every((t) => t <= cutoff)) this.hits.delete(k);
10854
- }
10855
- }
10856
10675
  return existing.length <= this.max;
10857
10676
  }
10858
10677
  retryAfterSec(key) {
@@ -10867,8 +10686,8 @@ var _g = global;
10867
10686
  var _a;
10868
10687
  var rateLimiter = (_a = _g.__retrivoraRateLimiter) != null ? _a : _g.__retrivoraRateLimiter = new RateLimiter(6e4, 30);
10869
10688
  function getRateLimitKey(req) {
10870
- var _a3, _b;
10871
- const ip = ((_b = (_a3 = req.headers.get("x-forwarded-for")) == null ? void 0 : _a3.split(",")[0]) == null ? void 0 : _b.trim()) || req.headers.get("x-real-ip") || "127.0.0.1";
10689
+ var _a2, _b;
10690
+ const ip = ((_b = (_a2 = req.headers.get("x-forwarded-for")) == null ? void 0 : _a2.split(",")[0]) == null ? void 0 : _b.trim()) || req.headers.get("x-real-ip") || "127.0.0.1";
10872
10691
  return `ip:${ip}`;
10873
10692
  }
10874
10693
  function checkRateLimit(req) {
@@ -10895,8 +10714,6 @@ function isFreeTier(tier) {
10895
10714
  if (!tier) return true;
10896
10715
  return FREE_TIER_NAMES.has(tier.toLowerCase().trim());
10897
10716
  }
10898
- var _a2;
10899
- var _freeTierGuardInstance = (_a2 = _g.__retrivoraFreeTierGuard) != null ? _a2 : _g.__retrivoraFreeTierGuard = new FreeTierLimitsGuard();
10900
10717
  function createPaymentRequiredResponse(reason, details) {
10901
10718
  const body = __spreadProps(__spreadValues({
10902
10719
  type: "https://retrivora.com/docs/errors/payment-required",
@@ -10907,10 +10724,10 @@ function createPaymentRequiredResponse(reason, details) {
10907
10724
  instance: void 0
10908
10725
  }, details != null ? details : {}), {
10909
10726
  quota: {
10910
- maxDailyRequestUnits: FREE_TIER_QUOTAS.MAX_DAILY_REQUEST_UNITS,
10911
- maxTrialRequestUnits: FREE_TIER_QUOTAS.MAX_TRIAL_REQUEST_UNITS,
10912
- maxDocuments: FREE_TIER_QUOTAS.MAX_DOCUMENTS,
10913
- maxStorageBytes: FREE_TIER_QUOTAS.MAX_STORAGE_BYTES,
10727
+ maxDailyRequestUnits: FreeTierLimitsGuard_exports.FREE_TIER_QUOTAS.MAX_DAILY_REQUEST_UNITS,
10728
+ maxTrialRequestUnits: FreeTierLimitsGuard_exports.FREE_TIER_QUOTAS.MAX_TRIAL_REQUEST_UNITS,
10729
+ maxDocuments: FreeTierLimitsGuard_exports.FREE_TIER_QUOTAS.MAX_DOCUMENTS,
10730
+ maxStorageBytes: FreeTierLimitsGuard_exports.FREE_TIER_QUOTAS.MAX_STORAGE_BYTES,
10914
10731
  upgradeUrl: "https://retrivora.com/pricing"
10915
10732
  }
10916
10733
  });
@@ -10922,17 +10739,17 @@ function createPaymentRequiredResponse(reason, details) {
10922
10739
  });
10923
10740
  }
10924
10741
  function resolveLicenseKey(req, config, bodyLicenseKey) {
10925
- var _a3;
10926
- return req.headers.get("x-license-key") || bodyLicenseKey || ((_a3 = req.headers.get("authorization")) == null ? void 0 : _a3.replace(/^Bearer\s+/i, "")) || config.licenseKey || process.env.NEXT_PUBLIC_RETRIVORA_LICENSE_KEY || process.env.RETRIVORA_LICENSE_KEY || process.env.RAG_LICENSE_KEY || "";
10742
+ return req.headers.get("x-license-key") || bodyLicenseKey || config.licenseKey || process.env.RETRIVORA_LICENSE_KEY || process.env.RAG_LICENSE_KEY || "";
10927
10743
  }
10928
10744
  async function enforceLicense(req, config, bodyLicenseKey) {
10929
10745
  const rawKey = resolveLicenseKey(req, config, bodyLicenseKey);
10930
10746
  const licenseKey = (rawKey || "").trim().replace(/^["']|["']$/g, "").trim();
10931
- const projectId = config.projectId || "";
10747
+ const url = new URL(req.url);
10748
+ const requestedProjectId = url.searchParams.get("projectId") || url.searchParams.get("project-id") || req.headers.get("x-project-id") || req.headers.get("X-Project-Id") || config.projectId || "";
10932
10749
  try {
10933
10750
  let payload;
10934
10751
  try {
10935
- payload = await LicenseVerifier.verifyAsync(licenseKey, projectId);
10752
+ payload = await LicenseVerifier.verifyAsync(licenseKey, requestedProjectId);
10936
10753
  } catch (err) {
10937
10754
  return {
10938
10755
  ok: false,
@@ -10944,7 +10761,7 @@ async function enforceLicense(req, config, bodyLicenseKey) {
10944
10761
  }
10945
10762
  return { ok: true, payload };
10946
10763
  } catch (err) {
10947
- const message = err instanceof Error ? err.message : "Missing or invalid RETRIVORA_LICENSE_KEY. Set NEXT_PUBLIC_RETRIVORA_LICENSE_KEY in your environment or contact your administrator.";
10764
+ const message = err instanceof Error ? err.message : "Missing or invalid RETRIVORA_LICENSE_KEY. Set RETRIVORA_LICENSE_KEY in your environment or contact your administrator.";
10948
10765
  const body = {
10949
10766
  error: {
10950
10767
  code: "LICENSE_REVOKED",
@@ -11002,38 +10819,59 @@ function sanitizeInput(raw) {
11002
10819
  }
11003
10820
  return { ok: true, value: trimmed };
11004
10821
  }
10822
+ var _pluginCache = /* @__PURE__ */ new Map();
10823
+ var PLUGIN_CACHE_TTL_MS = 10 * 60 * 1e3;
10824
+ function configHash(config) {
10825
+ return createHash("sha1").update(JSON.stringify(config != null ? config : {})).digest("hex").slice(0, 16);
10826
+ }
11005
10827
  function getOrCreatePlugin(configOrPlugin) {
11006
10828
  if (configOrPlugin instanceof VectorPlugin) return configOrPlugin;
11007
- const cacheKey = "__retrivoraPlugin_" + JSON.stringify(configOrPlugin != null ? configOrPlugin : {}).slice(0, 64);
11008
- if (!_g[cacheKey]) {
11009
- _g[cacheKey] = new VectorPlugin(configOrPlugin);
10829
+ const key = configHash(configOrPlugin);
10830
+ const cached = _pluginCache.get(key);
10831
+ if (cached && Date.now() - cached.createdAt < PLUGIN_CACHE_TTL_MS) return cached.plugin;
10832
+ const plugin = new VectorPlugin(configOrPlugin);
10833
+ _pluginCache.set(key, { plugin, createdAt: Date.now() });
10834
+ return plugin;
10835
+ }
10836
+ var TELEMETRY_ALLOWED_HOSTS = /* @__PURE__ */ new Set([
10837
+ "retrivora.com",
10838
+ "www.retrivora.com"
10839
+ ]);
10840
+ function sanitizeTelemetryUrl(raw) {
10841
+ if (process.env.NODE_ENV !== "production") return raw;
10842
+ try {
10843
+ const parsed = new URL(raw);
10844
+ const hostname = parsed.hostname.toLowerCase().replace(/^www\./, "");
10845
+ if (!TELEMETRY_ALLOWED_HOSTS.has(`www.${hostname}`) && !TELEMETRY_ALLOWED_HOSTS.has(hostname)) {
10846
+ console.warn(
10847
+ `[Retrivora SDK Telemetry] Blocked non-allowlisted telemetry URL: ${parsed.hostname}. Only retrivora.com endpoints are permitted in production.`
10848
+ );
10849
+ return null;
10850
+ }
10851
+ return parsed.toString();
10852
+ } catch (e) {
10853
+ return null;
11010
10854
  }
11011
- return _g[cacheKey];
11012
10855
  }
11013
10856
  function reportTelemetry(req, plugin, action, status, details, trace) {
11014
- var _a3, _b, _c, _d, _e, _f, _g2;
10857
+ var _a2, _b, _c, _d, _e, _f, _g2;
11015
10858
  try {
11016
10859
  const config = plugin.getConfig();
11017
- const licenseKey = config.licenseKey || process.env.RAG_LICENSE_KEY || process.env.RETRIVORA_LICENSE_KEY || process.env.NEXT_PUBLIC_RETRIVORA_LICENSE_KEY;
10860
+ const licenseKey = config.licenseKey || process.env.RETRIVORA_LICENSE_KEY || process.env.RAG_LICENSE_KEY;
11018
10861
  const telemetryConfig = config.telemetry;
11019
- const host = req.headers.get("host") || "localhost";
11020
10862
  const defaultUrl = "https://www.retrivora.com/api/telemetry";
11021
- const telemetryUrl = (telemetryConfig == null ? void 0 : telemetryConfig.url) || process.env.TELEMETRY_URL || process.env.NEXT_PUBLIC_TELEMETRY_URL || defaultUrl;
10863
+ const rawTelemetryUrl = (telemetryConfig == null ? void 0 : telemetryConfig.url) || process.env.TELEMETRY_URL || process.env.NEXT_PUBLIC_TELEMETRY_URL || defaultUrl;
10864
+ const telemetryUrl = sanitizeTelemetryUrl(rawTelemetryUrl);
10865
+ if (!telemetryUrl) return;
11022
10866
  const userAgent = req.headers.get("user-agent") || `Retrivora-SDK/${SDK_VERSION}`;
11023
- let absoluteUrl = telemetryUrl;
11024
- if (!telemetryUrl.startsWith("http")) {
11025
- const proto = req.headers.get("x-forwarded-proto") || "http";
11026
- absoluteUrl = `${proto}://${host}${telemetryUrl}`;
11027
- }
11028
10867
  const projectId = config.projectId || "default";
11029
- const model = (trace == null ? void 0 : trace.model) || ((_a3 = config.llm) == null ? void 0 : _a3.model) || ((_b = config.embedding) == null ? void 0 : _b.model) || "llama-3.1-8b-instant";
10868
+ const model = (trace == null ? void 0 : trace.model) || ((_a2 = config.llm) == null ? void 0 : _a2.model) || ((_b = config.embedding) == null ? void 0 : _b.model) || "llama-3.1-8b-instant";
11030
10869
  const provider = (trace == null ? void 0 : trace.provider) || ((_c = config.llm) == null ? void 0 : _c.provider) || ((_d = config.embedding) == null ? void 0 : _d.provider) || "groq";
11031
10870
  const tokens = Number(((_e = trace == null ? void 0 : trace.tokens) == null ? void 0 : _e.totalTokens) || (trace == null ? void 0 : trace.totalTokens) || 0);
11032
10871
  const costUsd = Number(((_f = trace == null ? void 0 : trace.tokens) == null ? void 0 : _f.estimatedCostUsd) || (trace == null ? void 0 : trace.costUsd) || 0);
11033
10872
  const latencyMs = Number(((_g2 = trace == null ? void 0 : trace.latency) == null ? void 0 : _g2.totalMs) || (trace == null ? void 0 : trace.latencyMs) || 0);
11034
10873
  const payload = {
11035
10874
  trace,
11036
- licenseKey,
11037
10875
  projectId,
11038
10876
  organization: process.env.RETRIVORA_ORGANIZATION || "default-org",
11039
10877
  sdkVersion: SDK_VERSION,
@@ -11050,14 +10888,13 @@ function reportTelemetry(req, plugin, action, status, details, trace) {
11050
10888
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
11051
10889
  details
11052
10890
  };
11053
- fetch(absoluteUrl, {
10891
+ fetch(telemetryUrl, {
11054
10892
  method: "POST",
11055
- headers: {
11056
- "Content-Type": "application/json",
11057
- "x-forwarded-for": req.headers.get("x-forwarded-for") || "",
11058
- "x-real-ip": req.headers.get("x-real-ip") || "",
10893
+ headers: __spreadProps(__spreadValues({
10894
+ "Content-Type": "application/json"
10895
+ }, licenseKey ? { "x-license-key": licenseKey } : {}), {
11059
10896
  "user-agent": userAgent
11060
- },
10897
+ }),
11061
10898
  body: JSON.stringify(payload)
11062
10899
  }).catch((err) => {
11063
10900
  console.warn("[Retrivora SDK Telemetry] Async report warning:", err.message);
@@ -11107,7 +10944,7 @@ function createChatHandler(configOrPlugin, options) {
11107
10944
  const storage = new DatabaseStorage(plugin.getConfig());
11108
10945
  const onAuthorize = options == null ? void 0 : options.onAuthorize;
11109
10946
  return async function POST(req, context) {
11110
- var _a3, _b, _c, _d, _e, _f;
10947
+ var _a2, _b, _c, _d, _e, _f;
11111
10948
  const authResult = await checkAuth(req, onAuthorize);
11112
10949
  if (authResult) return authResult;
11113
10950
  const rateLimited = checkRateLimit(req);
@@ -11120,18 +10957,17 @@ function createChatHandler(configOrPlugin, options) {
11120
10957
  if (!licenseCheck.ok) return licenseCheck.response;
11121
10958
  if (isFreeTier(licenseCheck.payload.tier)) {
11122
10959
  const estimatedInputTokens = Math.ceil(
11123
- ((((_a3 = bodyObj.message) == null ? void 0 : _a3.length) || 0) + (Array.isArray(bodyObj.messages) ? bodyObj.messages.reduce((a, m) => {
11124
- var _a4, _b2;
11125
- return a + (((_a4 = m == null ? void 0 : m.content) == null ? void 0 : _a4.length) || ((_b2 = m == null ? void 0 : m.text) == null ? void 0 : _b2.length) || 0);
10960
+ ((((_a2 = bodyObj.message) == null ? void 0 : _a2.length) || 0) + (Array.isArray(bodyObj.messages) ? bodyObj.messages.reduce((a, m) => {
10961
+ var _a3, _b2;
10962
+ return a + (((_a3 = m == null ? void 0 : m.content) == null ? void 0 : _a3.length) || ((_b2 = m == null ? void 0 : m.text) == null ? void 0 : _b2.length) || 0);
11126
10963
  }, 0) : 0) + (((_b = bodyObj.history) == null ? void 0 : _b.reduce((a, m) => {
11127
- var _a4, _b2;
11128
- return a + (((_a4 = m == null ? void 0 : m.content) == null ? void 0 : _a4.length) || ((_b2 = m == null ? void 0 : m.text) == null ? void 0 : _b2.length) || 0);
10964
+ var _a3, _b2;
10965
+ return a + (((_a3 = m == null ? void 0 : m.content) == null ? void 0 : _a3.length) || ((_b2 = m == null ? void 0 : m.text) == null ? void 0 : _b2.length) || 0);
11129
10966
  }, 0)) || 0)) / 4
11130
10967
  );
11131
- const chatCheck = FreeTierLimitsGuard.checkRequestAllowed({
10968
+ const chatCheck = FreeTierLimitsGuard_exports.FreeTierLimitsGuard.checkRequestAllowed({
11132
10969
  operationType: "chat",
11133
- inputTokens: estimatedInputTokens,
11134
- projectId: licenseCheck.payload.projectId
10970
+ inputTokens: estimatedInputTokens
11135
10971
  });
11136
10972
  if (!chatCheck.allowed) {
11137
10973
  return createPaymentRequiredResponse(
@@ -11190,7 +11026,7 @@ function createStreamHandler(configOrPlugin, options) {
11190
11026
  const storage = new DatabaseStorage(plugin.getConfig());
11191
11027
  const onAuthorize = options == null ? void 0 : options.onAuthorize;
11192
11028
  return async function POST(req, context) {
11193
- var _a3, _b, _c;
11029
+ var _a2, _b;
11194
11030
  const authResult = await checkAuth(req, onAuthorize);
11195
11031
  if (authResult) return authResult;
11196
11032
  const rateLimited = checkRateLimit(req);
@@ -11198,10 +11034,6 @@ function createStreamHandler(configOrPlugin, options) {
11198
11034
  const config = plugin.getConfig();
11199
11035
  let body;
11200
11036
  try {
11201
- if ((_a3 = req.headers.get("content-type")) == null ? void 0 : _a3.includes("multipart/form-data")) {
11202
- const uploader = createUploadHandler(plugin, { onAuthorize });
11203
- return uploader(req);
11204
- }
11205
11037
  body = await req.json();
11206
11038
  } catch (e) {
11207
11039
  return new Response(JSON.stringify({ error: { code: "INVALID_JSON", message: "Invalid JSON body" } }), {
@@ -11214,15 +11046,15 @@ function createStreamHandler(configOrPlugin, options) {
11214
11046
  if (!licenseCheck.ok) return licenseCheck.response;
11215
11047
  if (isFreeTier(licenseCheck.payload.tier)) {
11216
11048
  const estimatedInputTokens = Math.ceil(
11217
- ((((_b = bodyObj.message) == null ? void 0 : _b.length) || 0) + (Array.isArray(bodyObj.messages) ? bodyObj.messages.reduce((a, m) => {
11218
- var _a4, _b2;
11219
- return a + (((_a4 = m == null ? void 0 : m.content) == null ? void 0 : _a4.length) || ((_b2 = m == null ? void 0 : m.text) == null ? void 0 : _b2.length) || 0);
11220
- }, 0) : 0) + (((_c = bodyObj.history) == null ? void 0 : _c.reduce((a, m) => {
11221
- var _a4, _b2;
11222
- return a + (((_a4 = m == null ? void 0 : m.content) == null ? void 0 : _a4.length) || ((_b2 = m == null ? void 0 : m.text) == null ? void 0 : _b2.length) || 0);
11049
+ ((((_a2 = bodyObj.message) == null ? void 0 : _a2.length) || 0) + (Array.isArray(bodyObj.messages) ? bodyObj.messages.reduce((a, m) => {
11050
+ var _a3, _b2;
11051
+ return a + (((_a3 = m == null ? void 0 : m.content) == null ? void 0 : _a3.length) || ((_b2 = m == null ? void 0 : m.text) == null ? void 0 : _b2.length) || 0);
11052
+ }, 0) : 0) + (((_b = bodyObj.history) == null ? void 0 : _b.reduce((a, m) => {
11053
+ var _a3, _b2;
11054
+ return a + (((_a3 = m == null ? void 0 : m.content) == null ? void 0 : _a3.length) || ((_b2 = m == null ? void 0 : m.text) == null ? void 0 : _b2.length) || 0);
11223
11055
  }, 0)) || 0)) / 4
11224
11056
  );
11225
- const chatCheck = FreeTierLimitsGuard.checkRequestAllowed({
11057
+ const chatCheck = FreeTierLimitsGuard_exports.FreeTierLimitsGuard.checkRequestAllowed({
11226
11058
  operationType: "chat_stream",
11227
11059
  inputTokens: estimatedInputTokens,
11228
11060
  projectId: licenseCheck.payload.projectId
@@ -11265,7 +11097,7 @@ function createStreamHandler(configOrPlugin, options) {
11265
11097
  let isActive = true;
11266
11098
  const stream = new ReadableStream({
11267
11099
  async start(controller) {
11268
- var _a4, _b2, _c2, _d, _e;
11100
+ var _a3, _b2, _c, _d, _e;
11269
11101
  const enqueue = (text) => {
11270
11102
  if (!isActive) return;
11271
11103
  try {
@@ -11298,7 +11130,7 @@ function createStreamHandler(configOrPlugin, options) {
11298
11130
  let uiTransformation = responseChunk == null ? void 0 : responseChunk.ui_transformation;
11299
11131
  if (sources.length > 0) {
11300
11132
  try {
11301
- uiTransformation = (_a4 = responseChunk == null ? void 0 : responseChunk.ui_transformation) != null ? _a4 : UITransformer.transform(message, sources, plugin.getConfig());
11133
+ uiTransformation = (_a3 = responseChunk == null ? void 0 : responseChunk.ui_transformation) != null ? _a3 : UITransformer.transform(message, sources, plugin.getConfig());
11302
11134
  if (uiTransformation) {
11303
11135
  enqueue(sseUIFrame(uiTransformation));
11304
11136
  }
@@ -11322,7 +11154,7 @@ function createStreamHandler(configOrPlugin, options) {
11322
11154
  uiTransformation,
11323
11155
  trace: responseChunk == null ? void 0 : responseChunk.trace
11324
11156
  }).catch((err) => console.warn("[createStreamHandler] Failed to save assistant message:", err));
11325
- reportTelemetry(req, plugin, "QUERY_GENERATION", "success", `Tokens: ${((_c2 = (_b2 = responseChunk == null ? void 0 : responseChunk.trace) == null ? void 0 : _b2.tokens) == null ? void 0 : _c2.totalTokens) || 0}, Latency: ${((_e = (_d = responseChunk == null ? void 0 : responseChunk.trace) == null ? void 0 : _d.latency) == null ? void 0 : _e.totalMs) || 0}ms`, responseChunk == null ? void 0 : responseChunk.trace);
11157
+ reportTelemetry(req, plugin, "QUERY_GENERATION", "success", `Tokens: ${((_c = (_b2 = responseChunk == null ? void 0 : responseChunk.trace) == null ? void 0 : _b2.tokens) == null ? void 0 : _c.totalTokens) || 0}, Latency: ${((_e = (_d = responseChunk == null ? void 0 : responseChunk.trace) == null ? void 0 : _d.latency) == null ? void 0 : _e.totalMs) || 0}ms`, responseChunk == null ? void 0 : responseChunk.trace);
11326
11158
  }
11327
11159
  }
11328
11160
  } catch (temp) {
@@ -11387,7 +11219,7 @@ function createIngestHandler(configOrPlugin, options) {
11387
11219
  (sum, d) => sum + Buffer.byteLength(d.content || "", "utf8"),
11388
11220
  0
11389
11221
  );
11390
- const ingestCheck = FreeTierLimitsGuard.checkIngestionAllowed(
11222
+ const ingestCheck = FreeTierLimitsGuard_exports.FreeTierLimitsGuard.checkIngestionAllowed(
11391
11223
  { documentCount: documents.length },
11392
11224
  totalIncomingBytes
11393
11225
  );
@@ -11439,7 +11271,6 @@ function createLicenseHandler(configOrPlugin, options) {
11439
11271
  const plugin = getOrCreatePlugin(configOrPlugin);
11440
11272
  const onAuthorize = options == null ? void 0 : options.onAuthorize;
11441
11273
  return async function POST(req) {
11442
- var _a3;
11443
11274
  if (req) {
11444
11275
  const authResult = await checkAuth(req, onAuthorize);
11445
11276
  if (authResult) return authResult;
@@ -11447,7 +11278,17 @@ function createLicenseHandler(configOrPlugin, options) {
11447
11278
  try {
11448
11279
  const body = await req.json().catch(() => ({}));
11449
11280
  const config = plugin.getConfig();
11450
- const rawKey = req.headers.get("x-license-key") || (body == null ? void 0 : body.licenseKey) || ((_a3 = req.headers.get("authorization")) == null ? void 0 : _a3.replace(/^Bearer\s+/i, "")) || config.licenseKey || process.env.NEXT_PUBLIC_RETRIVORA_LICENSE_KEY || process.env.RETRIVORA_LICENSE_KEY || "";
11281
+ const authHeader = req.headers.get("authorization");
11282
+ let authHeaderKey = "";
11283
+ if (authHeader && authHeader.toLowerCase().startsWith("bearer ")) {
11284
+ authHeaderKey = authHeader.replace(/^Bearer\s+/i, "");
11285
+ if (authHeaderKey) {
11286
+ console.warn(
11287
+ '[Retrivora SDK] DEPRECATION WARNING (SEC-7): Repurposing the standard "Authorization: Bearer" header to carry the license key is deprecated and will be removed in a future release. Please use the custom "x-license-key" header instead.'
11288
+ );
11289
+ }
11290
+ }
11291
+ const rawKey = req.headers.get("x-license-key") || (body == null ? void 0 : body.licenseKey) || authHeaderKey || config.licenseKey || process.env.RETRIVORA_LICENSE_KEY || "";
11451
11292
  const licenseKey = (rawKey || "").trim().replace(/^["']|["']$/g, "").trim();
11452
11293
  const projectId = (body == null ? void 0 : body.projectId) || config.projectId || "";
11453
11294
  const payload = await LicenseVerifier.verifyAsync(licenseKey, projectId);
@@ -11498,7 +11339,7 @@ function createUploadHandler(configOrPlugin, options) {
11498
11339
  }
11499
11340
  if (isFreeTier(licenseCheck.payload.tier)) {
11500
11341
  const totalUploadBytes = files.reduce((sum, f) => sum + (f.size || 0), 0);
11501
- const uploadCheck = FreeTierLimitsGuard.checkIngestionAllowed(
11342
+ const uploadCheck = FreeTierLimitsGuard_exports.FreeTierLimitsGuard.checkIngestionAllowed(
11502
11343
  { documentCount: files.length },
11503
11344
  totalUploadBytes
11504
11345
  );
@@ -11510,8 +11351,8 @@ function createUploadHandler(configOrPlugin, options) {
11510
11351
  projectId: licenseCheck.payload.projectId,
11511
11352
  fileCount: files.length,
11512
11353
  totalUploadBytes,
11513
- maxFileSizeBytes: FREE_TIER_QUOTAS.MAX_FILE_SIZE_BYTES,
11514
- maxStorageBytes: FREE_TIER_QUOTAS.MAX_STORAGE_BYTES
11354
+ maxFileSizeBytes: FreeTierLimitsGuard_exports.FREE_TIER_QUOTAS.MAX_FILE_SIZE_BYTES,
11355
+ maxStorageBytes: FreeTierLimitsGuard_exports.FREE_TIER_QUOTAS.MAX_STORAGE_BYTES
11515
11356
  }
11516
11357
  );
11517
11358
  }
@@ -11825,7 +11666,7 @@ function createRagHandler(configOrPlugin, options) {
11825
11666
  }
11826
11667
  }
11827
11668
  async function getSegment(req, context) {
11828
- var _a3, _b, _c;
11669
+ var _a2, _b, _c;
11829
11670
  const contentType = req.headers.get("content-type") || "";
11830
11671
  if (contentType.includes("multipart/form-data")) {
11831
11672
  return "upload";
@@ -11844,7 +11685,7 @@ function createRagHandler(configOrPlugin, options) {
11844
11685
  if (pathname.endsWith("/sessions")) return "sessions";
11845
11686
  } catch (e) {
11846
11687
  }
11847
- const resolvedParams = typeof ((_a3 = context == null ? void 0 : context.params) == null ? void 0 : _a3.then) === "function" ? await context.params : context == null ? void 0 : context.params;
11688
+ const resolvedParams = typeof ((_a2 = context == null ? void 0 : context.params) == null ? void 0 : _a2.then) === "function" ? await context.params : context == null ? void 0 : context.params;
11848
11689
  const segments = (resolvedParams == null ? void 0 : resolvedParams.retrivora) || ((_b = resolvedParams == null ? void 0 : resolvedParams.params) == null ? void 0 : _b.retrivora) || (resolvedParams == null ? void 0 : resolvedParams.rag) || ((_c = resolvedParams == null ? void 0 : resolvedParams.params) == null ? void 0 : _c.rag) || [];
11849
11690
  const joined = segments.join("/");
11850
11691
  return joined || "chat";