@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
package/dist/server.js CHANGED
@@ -50,6 +50,7 @@ var __copyProps = (to, from, except, desc) => {
50
50
  }
51
51
  return to;
52
52
  };
53
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
53
54
  var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
54
55
  // If the importer is in node compatibility mode or this is not an ESM
55
56
  // file that has been converted to a CommonJS file using a Babel-
@@ -290,6 +291,7 @@ function projectSettingsToUiConfig(settings) {
290
291
  welcomeMessage: settings.branding.welcomeMessage,
291
292
  poweredBy: settings.widget.showRetrivoraBranding ? "Retrivora AI" : "",
292
293
  allowUpload: settings.ai.fileUploadEnabled,
294
+ showSources: settings.ai.citationsEnabled,
293
295
  borderRadius: settings.branding.borderRadius <= 4 ? "sm" : settings.branding.borderRadius <= 8 ? "md" : settings.branding.borderRadius <= 16 ? "xl" : "full"
294
296
  };
295
297
  }
@@ -330,7 +332,7 @@ var init_package = __esm({
330
332
  "package.json"() {
331
333
  package_default = {
332
334
  name: "@retrivora-ai/rag-engine",
333
- version: "2.4.3",
335
+ version: "2.4.6",
334
336
  description: "Retrivora AI is a plug-and-play AI engine for RAG chat experiences \u2014 generic vector DB + LLM provider, embeddable or standalone.",
335
337
  author: "Abhinav Alkuchi",
336
338
  license: "UNLICENSED",
@@ -402,8 +404,6 @@ var init_package = __esm({
402
404
  },
403
405
  files: [
404
406
  "dist",
405
- "src",
406
- ".env.example",
407
407
  "README.md",
408
408
  "FREE_TIER_ARCHITECTURE.md"
409
409
  ],
@@ -413,14 +413,19 @@ var init_package = __esm({
413
413
  "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",
414
414
  lint: "eslint",
415
415
  typecheck: "tsc --noEmit -p tsconfig.build.json",
416
+ test: "jest",
416
417
  clean: "rm -rf dist"
417
418
  },
418
419
  peerDependencies: {
420
+ axios: ">=1.0.0",
419
421
  next: ">=15.0.0",
420
422
  react: ">=18.0.0",
421
423
  "react-dom": ">=18.0.0"
422
424
  },
423
425
  peerDependenciesMeta: {
426
+ axios: {
427
+ optional: true
428
+ },
424
429
  next: {
425
430
  optional: true
426
431
  }
@@ -429,9 +434,9 @@ var init_package = __esm({
429
434
  "@anthropic-ai/sdk": "^0.95.1",
430
435
  "@google/genai": "^0.8.0",
431
436
  "@google/generative-ai": "^0.24.1",
437
+ "@retrivora-ai/billing-guards": "*",
432
438
  "@pinecone-database/pinecone": "^7.2.0",
433
439
  "@types/papaparse": "^5.5.2",
434
- axios: "^1.15.0",
435
440
  "lucide-react": "^1.8.0",
436
441
  "next-themes": "^0.4.6",
437
442
  openai: "^6.34.0",
@@ -456,16 +461,20 @@ var init_package = __esm({
456
461
  "@tailwindcss/cli": "^4",
457
462
  "@tailwindcss/postcss": "^4",
458
463
  "@types/estree": "^1.0.9",
464
+ "@types/jest": "^30.0.0",
459
465
  "@types/node": "^20",
460
466
  "@types/pdf-parse": "^1.1.5",
461
467
  "@types/pg": "^8.20.0",
462
468
  "@types/react": "^19.2.14",
463
469
  "@types/react-dom": "^19.2.3",
470
+ axios: "^1.15.0",
464
471
  dotenv: "^17.4.2",
465
472
  eslint: "^9",
466
473
  "eslint-config-next": "16.2.4",
474
+ jest: "^30.4.2",
467
475
  next: "16.2.10",
468
476
  tailwindcss: "^4",
477
+ "ts-jest": "^29.4.12",
469
478
  tsup: "^8.5.1",
470
479
  typescript: "^5"
471
480
  }
@@ -508,7 +517,7 @@ var init_ConfigFetcher = __esm({
508
517
  * PINECONE_API_KEY if available, or vector operations are handled server-side.
509
518
  */
510
519
  static async fetchRemoteConfig(projectId, licenseKey) {
511
- var _a3, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k;
520
+ var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k;
512
521
  const cacheKey = `${projectId}:${licenseKey || ""}`;
513
522
  const now = Date.now();
514
523
  const cached = this.cache.get(cacheKey);
@@ -553,7 +562,7 @@ var init_ConfigFetcher = __esm({
553
562
  indexName: data.vectorDb.indexName || "retrivora-free",
554
563
  provider: data.vectorDb.provider || "pinecone",
555
564
  projectId: fetchedProjectId,
556
- namespace: ((_a3 = data.workspace) == null ? void 0 : _a3.namespace) || data.vectorDb.namespace || fetchedProjectId
565
+ namespace: ((_a2 = data.workspace) == null ? void 0 : _a2.namespace) || data.vectorDb.namespace || fetchedProjectId
557
566
  },
558
567
  embedding: {
559
568
  provider: ((_b = data.embedding) == null ? void 0 : _b.provider) || "universal_rest",
@@ -610,16 +619,20 @@ var init_ConfigFetcher = __esm({
610
619
  return config;
611
620
  }
612
621
  }
613
- } catch (e) {
622
+ } catch (err) {
623
+ console.warn(
624
+ `[ConfigFetcher] Failed to fetch config from ${baseUrl}:`,
625
+ err instanceof Error ? err.message : String(err)
626
+ );
614
627
  }
615
628
  }
616
629
  return null;
617
630
  }
618
631
  /** Convenience: fetch only vector DB config (backwards compat). */
619
632
  static async fetchRemoteVectorConfig(projectId, licenseKey) {
620
- var _a3;
633
+ var _a2;
621
634
  const config = await this.fetchRemoteConfig(projectId, licenseKey);
622
- return (_a3 = config == null ? void 0 : config.vectorDb) != null ? _a3 : null;
635
+ return (_a2 = config == null ? void 0 : config.vectorDb) != null ? _a2 : null;
623
636
  }
624
637
  };
625
638
  ConfigFetcher.cache = /* @__PURE__ */ new Map();
@@ -659,7 +672,7 @@ var init_PineconeProvider = __esm({
659
672
  const errors = [];
660
673
  const opts = config.options || {};
661
674
  const hasLicenseKey = Boolean(
662
- config.licenseKey || process.env.RETRIVORA_LICENSE_KEY || process.env.NEXT_PUBLIC_RETRIVORA_LICENSE_KEY
675
+ config.licenseKey || process.env.RETRIVORA_LICENSE_KEY
663
676
  );
664
677
  if (!opts.apiKey && !hasLicenseKey) {
665
678
  errors.push({
@@ -676,7 +689,7 @@ var init_PineconeProvider = __esm({
676
689
  static getHealthChecker() {
677
690
  return {
678
691
  async check(config) {
679
- var _a3, _b;
692
+ var _a2, _b;
680
693
  const opts = config.options || {};
681
694
  const indexName = config.indexName;
682
695
  const timestamp = Date.now();
@@ -684,7 +697,7 @@ var init_PineconeProvider = __esm({
684
697
  const { Pinecone: Pinecone2 } = await import("@pinecone-database/pinecone");
685
698
  const client = new Pinecone2({ apiKey: opts.apiKey });
686
699
  const indexes = await client.listIndexes();
687
- const indexNames = (_b = (_a3 = indexes.indexes) == null ? void 0 : _a3.map((i) => i.name)) != null ? _b : [];
700
+ const indexNames = (_b = (_a2 = indexes.indexes) == null ? void 0 : _a2.map((i) => i.name)) != null ? _b : [];
688
701
  if (!indexNames.includes(indexName)) {
689
702
  return {
690
703
  healthy: false,
@@ -711,25 +724,13 @@ var init_PineconeProvider = __esm({
711
724
  };
712
725
  }
713
726
  async initialize() {
714
- var _a3, _b;
727
+ var _a2, _b;
715
728
  if (this.client) return;
716
729
  let key = this.apiKey || process.env.PINECONE_API_KEY || "";
717
730
  if (!key) {
718
- const licenseKey = process.env.RETRIVORA_LICENSE_KEY || process.env.NEXT_PUBLIC_RETRIVORA_LICENSE_KEY;
731
+ const licenseKey = process.env.RETRIVORA_LICENSE_KEY;
719
732
  this.licenseKey = licenseKey;
720
- const jwtProjectId = (() => {
721
- if (!licenseKey) return void 0;
722
- try {
723
- const parts = licenseKey.split(".");
724
- if (parts.length >= 2) {
725
- const payload = JSON.parse(Buffer.from(parts[1], "base64url").toString("utf8"));
726
- return payload == null ? void 0 : payload.projectId;
727
- }
728
- } catch (e) {
729
- }
730
- return void 0;
731
- })();
732
- const projectId = process.env.RAG_PROJECT_ID || process.env.NEXT_PUBLIC_RAG_PROJECT_ID || jwtProjectId || this.projectId || "default";
733
+ const projectId = process.env.RAG_PROJECT_ID || process.env.NEXT_PUBLIC_RAG_PROJECT_ID || this.projectId || "default";
733
734
  this.projectId = projectId;
734
735
  const remoteConfig = await ConfigFetcher.fetchRemoteVectorConfig(projectId, licenseKey);
735
736
  if (remoteConfig) {
@@ -757,7 +758,7 @@ var init_PineconeProvider = __esm({
757
758
  this.client = new import_pinecone.Pinecone({ apiKey: key });
758
759
  try {
759
760
  const indexes = await this.client.listIndexes();
760
- const names = (_b = (_a3 = indexes.indexes) == null ? void 0 : _a3.map((i) => i.name)) != null ? _b : [];
761
+ const names = (_b = (_a2 = indexes.indexes) == null ? void 0 : _a2.map((i) => i.name)) != null ? _b : [];
761
762
  if (!names.includes(this.indexName)) {
762
763
  console.warn(`[PineconeProvider] Target index "${this.indexName}" not listed. Proceeding with configured index name.`);
763
764
  }
@@ -776,7 +777,7 @@ var init_PineconeProvider = __esm({
776
777
  return namespace ? idx.namespace(namespace) : idx.namespace("");
777
778
  }
778
779
  async upsert(doc, namespace) {
779
- var _a3, _b;
780
+ var _a2, _b;
780
781
  if (!this.client) {
781
782
  if (!this.licenseKey) throw new Error("[PineconeProvider] Cannot execute vector operation: Missing API key and License key.");
782
783
  const res = await fetch(`${this.getControlPlaneUrl()}/api/v1/vectors/upsert`, {
@@ -786,7 +787,7 @@ var init_PineconeProvider = __esm({
786
787
  projectId: this.projectId,
787
788
  indexName: this.indexName,
788
789
  namespace: namespace || this.projectId,
789
- records: [{ id: String(doc.id), values: doc.vector, metadata: __spreadValues({ content: doc.content }, (_a3 = doc.metadata) != null ? _a3 : {}) }]
790
+ records: [{ id: String(doc.id), values: doc.vector, metadata: __spreadValues({ content: doc.content }, (_a2 = doc.metadata) != null ? _a2 : {}) }]
790
791
  })
791
792
  });
792
793
  if (!res.ok) throw new Error(`[PineconeProvider] Proxy upsert failed: ${await res.text()}`);
@@ -807,11 +808,11 @@ var init_PineconeProvider = __esm({
807
808
  if (!this.licenseKey) throw new Error("[PineconeProvider] Cannot execute vector operation: Missing API key and License key.");
808
809
  for (let i = 0; i < docs.length; i += BATCH) {
809
810
  const records = docs.slice(i, i + BATCH).map((d) => {
810
- var _a3;
811
+ var _a2;
811
812
  return {
812
813
  id: String(d.id),
813
814
  values: d.vector,
814
- metadata: __spreadValues({ content: d.content }, (_a3 = d.metadata) != null ? _a3 : {})
815
+ metadata: __spreadValues({ content: d.content }, (_a2 = d.metadata) != null ? _a2 : {})
815
816
  };
816
817
  });
817
818
  const res = await fetch(`${this.getControlPlaneUrl()}/api/v1/vectors/upsert`, {
@@ -831,18 +832,18 @@ var init_PineconeProvider = __esm({
831
832
  const targetIdx = await this.getActiveIndex(namespace);
832
833
  for (let i = 0; i < docs.length; i += BATCH) {
833
834
  const records = docs.slice(i, i + BATCH).map((d) => {
834
- var _a3;
835
+ var _a2;
835
836
  return {
836
837
  id: String(d.id),
837
838
  values: d.vector,
838
- metadata: __spreadValues({ content: d.content }, (_a3 = d.metadata) != null ? _a3 : {})
839
+ metadata: __spreadValues({ content: d.content }, (_a2 = d.metadata) != null ? _a2 : {})
839
840
  };
840
841
  });
841
842
  await targetIdx.upsert({ records });
842
843
  }
843
844
  }
844
845
  async query(vector, topK, namespace, filter) {
845
- var _a3, _b;
846
+ var _a2, _b;
846
847
  const pineconeFilter = this.sanitizeFilter(filter);
847
848
  if (!this.client) {
848
849
  if (!this.licenseKey) throw new Error("[PineconeProvider] Cannot execute vector operation: Missing API key and License key.");
@@ -860,11 +861,11 @@ var init_PineconeProvider = __esm({
860
861
  });
861
862
  if (!res.ok) throw new Error(`[PineconeProvider] Proxy query failed: ${await res.text()}`);
862
863
  const data = await res.json();
863
- return ((_a3 = data.matches) != null ? _a3 : []).map((m) => {
864
- var _a4, _b2;
864
+ return ((_a2 = data.matches) != null ? _a2 : []).map((m) => {
865
+ var _a3, _b2;
865
866
  return {
866
867
  id: m.id,
867
- score: (_a4 = m.score) != null ? _a4 : 0,
868
+ score: (_a3 = m.score) != null ? _a3 : 0,
868
869
  content: ((_b2 = m.metadata) == null ? void 0 : _b2.content) || "",
869
870
  metadata: m.metadata
870
871
  };
@@ -877,10 +878,10 @@ var init_PineconeProvider = __esm({
877
878
  includeMetadata: true
878
879
  }, Object.keys(pineconeFilter).length > 0 ? { filter: pineconeFilter } : {}));
879
880
  return ((_b = result.matches) != null ? _b : []).map((m) => {
880
- var _a4, _b2;
881
+ var _a3, _b2;
881
882
  return {
882
883
  id: m.id,
883
- score: (_a4 = m.score) != null ? _a4 : 0,
884
+ score: (_a3 = m.score) != null ? _a3 : 0,
884
885
  content: ((_b2 = m.metadata) == null ? void 0 : _b2.content) || "",
885
886
  metadata: m.metadata
886
887
  };
@@ -923,13 +924,13 @@ var init_PostgreSQLProvider = __esm({
923
924
  init_BaseVectorProvider();
924
925
  PostgreSQLProvider = class extends BaseVectorProvider {
925
926
  constructor(config) {
926
- var _a3;
927
+ var _a2;
927
928
  super(config);
928
929
  this.tableName = this.indexName.replace(/[^a-z0-9_]/gi, "_");
929
930
  const opts = config.options;
930
931
  if (!opts.connectionString) throw new Error("[PostgreSQLProvider] options.connectionString is required");
931
932
  this.connectionString = opts.connectionString;
932
- this.dimensions = (_a3 = opts.dimensions) != null ? _a3 : 1536;
933
+ this.dimensions = (_a2 = opts.dimensions) != null ? _a2 : 1536;
933
934
  }
934
935
  static getValidator() {
935
936
  return {
@@ -1010,7 +1011,7 @@ var init_PostgreSQLProvider = __esm({
1010
1011
  }
1011
1012
  }
1012
1013
  async upsert(doc, namespace = "") {
1013
- var _a3;
1014
+ var _a2;
1014
1015
  const vectorLiteral = `[${doc.vector.join(",")}]`;
1015
1016
  await this.pool.query(
1016
1017
  `INSERT INTO ${this.tableName} (id, namespace, content, metadata, embedding)
@@ -1020,7 +1021,7 @@ var init_PostgreSQLProvider = __esm({
1020
1021
  content = EXCLUDED.content,
1021
1022
  metadata = EXCLUDED.metadata,
1022
1023
  embedding = EXCLUDED.embedding`,
1023
- [doc.id, namespace, doc.content, JSON.stringify((_a3 = doc.metadata) != null ? _a3 : {}), vectorLiteral]
1024
+ [doc.id, namespace, doc.content, JSON.stringify((_a2 = doc.metadata) != null ? _a2 : {}), vectorLiteral]
1024
1025
  );
1025
1026
  }
1026
1027
  async batchUpsert(docs, namespace = "") {
@@ -1033,9 +1034,9 @@ var init_PostgreSQLProvider = __esm({
1033
1034
  const batch = docs.slice(i, i + BATCH_SIZE);
1034
1035
  const values = [];
1035
1036
  const valuePlaceholders = batch.map((doc, idx) => {
1036
- var _a3;
1037
+ var _a2;
1037
1038
  const offset = idx * 5;
1038
- values.push(doc.id, namespace, doc.content, JSON.stringify((_a3 = doc.metadata) != null ? _a3 : {}), `[${doc.vector.join(",")}]`);
1039
+ values.push(doc.id, namespace, doc.content, JSON.stringify((_a2 = doc.metadata) != null ? _a2 : {}), `[${doc.vector.join(",")}]`);
1039
1040
  return `($${offset + 1}, $${offset + 2}, $${offset + 3}, $${offset + 4}, $${offset + 5}::vector)`;
1040
1041
  }).join(", ");
1041
1042
  const query = `
@@ -1118,8 +1119,8 @@ var init_PostgreSQLProvider = __esm({
1118
1119
 
1119
1120
  // src/utils/synonyms.ts
1120
1121
  function resolveMetadataValue(meta, uiKey) {
1121
- var _a3;
1122
- const synonyms = (_a3 = FIELD_SYNONYMS[uiKey]) != null ? _a3 : [];
1122
+ var _a2;
1123
+ const synonyms = (_a2 = FIELD_SYNONYMS[uiKey]) != null ? _a2 : [];
1123
1124
  const keys = Object.keys(meta);
1124
1125
  const systemKeys = ["namespace", "filename", "filesize", "filetype", "docid", "uploadedat", "dimension", "chunkindex"];
1125
1126
  let match = keys.find((k) => k.toLowerCase() === uiKey.toLowerCase());
@@ -1206,14 +1207,14 @@ var init_MultiTablePostgresProvider = __esm({
1206
1207
  init_synonyms();
1207
1208
  MultiTablePostgresProvider = class extends BaseVectorProvider {
1208
1209
  constructor(config) {
1209
- var _a3, _b, _c;
1210
+ var _a2, _b, _c;
1210
1211
  super(config);
1211
1212
  const opts = config.options || {};
1212
1213
  if (!opts.connectionString) {
1213
1214
  throw new Error("[MultiTablePostgresProvider] options.connectionString is required");
1214
1215
  }
1215
1216
  this.connectionString = opts.connectionString;
1216
- this.dimensions = (_a3 = opts.dimensions) != null ? _a3 : 768;
1217
+ this.dimensions = (_a2 = opts.dimensions) != null ? _a2 : 768;
1217
1218
  this.tables = [];
1218
1219
  const rawSearchFields = (_c = (_b = opts.searchFields) != null ? _b : process.env.VECTOR_DB_SEARCH_FIELDS) != null ? _c : ["name", "product_name", "productname", "title"];
1219
1220
  this.searchFields = typeof rawSearchFields === "string" ? rawSearchFields.split(",").map((f) => f.trim()).filter(Boolean) : rawSearchFields;
@@ -1252,7 +1253,6 @@ var init_MultiTablePostgresProvider = __esm({
1252
1253
  this.tables = res.rows.map((r) => r.table_name);
1253
1254
  if (this.tables.length === 0) {
1254
1255
  console.warn('[MultiTablePostgresProvider] No tables with "embedding" columns found in the database.');
1255
- } else {
1256
1256
  }
1257
1257
  } finally {
1258
1258
  client.release();
@@ -1268,11 +1268,11 @@ var init_MultiTablePostgresProvider = __esm({
1268
1268
  * Batch upsert documents by dynamically provisioning tables and columns based on CSV headers.
1269
1269
  */
1270
1270
  async batchUpsert(docs, namespace = "") {
1271
- var _a3;
1271
+ var _a2;
1272
1272
  if (docs.length === 0) return;
1273
1273
  const docsByFile = {};
1274
1274
  for (const doc of docs) {
1275
- const fileName = ((_a3 = doc.metadata) == null ? void 0 : _a3.fileName) || this.uploadTable;
1275
+ const fileName = ((_a2 = doc.metadata) == null ? void 0 : _a2.fileName) || this.uploadTable;
1276
1276
  if (!docsByFile[fileName]) docsByFile[fileName] = [];
1277
1277
  docsByFile[fileName].push(doc);
1278
1278
  }
@@ -1325,11 +1325,11 @@ var init_MultiTablePostgresProvider = __esm({
1325
1325
  const headerColumns = csvHeaders.map((h) => `"${h}"`).join(", ");
1326
1326
  const allColumns = `id, ${headerColumns ? headerColumns + ", " : ""}namespace, content, metadata, embedding`;
1327
1327
  const valuePlaceholders = batch.map((doc, idx) => {
1328
- var _a4, _b;
1328
+ var _a3, _b;
1329
1329
  const offset = idx * (5 + csvHeaders.length);
1330
1330
  values.push(doc.id);
1331
1331
  for (const h of csvHeaders) {
1332
- values.push(String(((_a4 = doc.metadata) == null ? void 0 : _a4[h]) || ""));
1332
+ values.push(String(((_a3 = doc.metadata) == null ? void 0 : _a3[h]) || ""));
1333
1333
  }
1334
1334
  values.push(namespace, doc.content, JSON.stringify((_b = doc.metadata) != null ? _b : {}), `[${doc.vector.join(",")}]`);
1335
1335
  const docPlaceholders = [];
@@ -1368,7 +1368,7 @@ var init_MultiTablePostgresProvider = __esm({
1368
1368
  * Query all configured tables and merge results, sorted by cosine similarity score.
1369
1369
  */
1370
1370
  async query(vector, topK, _namespace, _filter) {
1371
- var _a3;
1371
+ var _a2;
1372
1372
  if (!this.pool) {
1373
1373
  throw new Error("[MultiTablePostgresProvider] Provider not initialized. Call initialize() first.");
1374
1374
  }
@@ -1396,11 +1396,11 @@ var init_MultiTablePostgresProvider = __esm({
1396
1396
  const filterParams = [];
1397
1397
  if (metadataFilters && Object.keys(metadataFilters).length > 0) {
1398
1398
  const conditions = Object.entries(metadataFilters).map(([key, val]) => {
1399
- var _a5;
1399
+ var _a4;
1400
1400
  filterParams.push(String(val));
1401
1401
  const baseOffset = queryText && dynamicKeywordQuery ? 2 : 1;
1402
1402
  const paramIdx = baseOffset + filterParams.length;
1403
- const synonyms = (_a5 = FIELD_SYNONYMS[key]) != null ? _a5 : [];
1403
+ const synonyms = (_a4 = FIELD_SYNONYMS[key]) != null ? _a4 : [];
1404
1404
  const keysToCheck = [key, ...synonyms];
1405
1405
  const coalesceExprs = keysToCheck.flatMap((k) => [
1406
1406
  `metadata->>'${k}'`,
@@ -1467,7 +1467,7 @@ var init_MultiTablePostgresProvider = __esm({
1467
1467
  }
1468
1468
  const tableResults = [];
1469
1469
  for (const row of result.rows) {
1470
- const _a4 = row, { hybrid_score, id } = _a4, rest = __objRest(_a4, ["hybrid_score", "id"]);
1470
+ const _a3 = row, { hybrid_score, id } = _a3, rest = __objRest(_a3, ["hybrid_score", "id"]);
1471
1471
  delete rest.embedding;
1472
1472
  delete rest.vector_score;
1473
1473
  delete rest.keyword_score;
@@ -1496,10 +1496,10 @@ var init_MultiTablePostgresProvider = __esm({
1496
1496
  }
1497
1497
  allResults.sort((a, b) => b.score - a.score);
1498
1498
  if (allResults.length === 0) return [];
1499
- const bestMatchTable = (_a3 = allResults[0].metadata) == null ? void 0 : _a3.source_table;
1499
+ const bestMatchTable = (_a2 = allResults[0].metadata) == null ? void 0 : _a2.source_table;
1500
1500
  const finalSorted = allResults.filter((res) => {
1501
- var _a4;
1502
- return ((_a4 = res.metadata) == null ? void 0 : _a4.source_table) === bestMatchTable;
1501
+ var _a3;
1502
+ return ((_a3 = res.metadata) == null ? void 0 : _a3.source_table) === bestMatchTable;
1503
1503
  });
1504
1504
  return finalSorted.slice(0, Math.max(topK, 15));
1505
1505
  }
@@ -1897,14 +1897,14 @@ var init_QdrantProvider = __esm({
1897
1897
  * Samples points from the collection to discover available payload fields.
1898
1898
  */
1899
1899
  async discoverSchema() {
1900
- var _a3;
1900
+ var _a2;
1901
1901
  try {
1902
1902
  const { data } = await this.http.post(`/collections/${this.indexName}/points/scroll`, {
1903
1903
  limit: 20,
1904
1904
  with_payload: true,
1905
1905
  with_vector: false
1906
1906
  });
1907
- const points = ((_a3 = data.result) == null ? void 0 : _a3.points) || [];
1907
+ const points = ((_a2 = data.result) == null ? void 0 : _a2.points) || [];
1908
1908
  const keys = /* @__PURE__ */ new Set();
1909
1909
  for (const point of points) {
1910
1910
  const payload = point.payload || {};
@@ -1930,11 +1930,11 @@ var init_QdrantProvider = __esm({
1930
1930
  * Ensures the collection exists. Creates it if missing.
1931
1931
  */
1932
1932
  async ensureCollection() {
1933
- var _a3;
1933
+ var _a2;
1934
1934
  try {
1935
1935
  await this.http.get(`/collections/${this.indexName}`);
1936
1936
  } catch (err) {
1937
- if (import_axios4.default.isAxiosError(err) && ((_a3 = err.response) == null ? void 0 : _a3.status) === 404) {
1937
+ if (import_axios4.default.isAxiosError(err) && ((_a2 = err.response) == null ? void 0 : _a2.status) === 404) {
1938
1938
  const opts = this.config.options;
1939
1939
  const dimensionsForCreate = opts.dimensions || 1536;
1940
1940
  await this.http.put(`/collections/${this.indexName}`, {
@@ -1952,7 +1952,7 @@ var init_QdrantProvider = __esm({
1952
1952
  * Ensures that a payload field has an index.
1953
1953
  */
1954
1954
  async ensureIndex(fieldName, schema = "keyword") {
1955
- var _a3;
1955
+ var _a2;
1956
1956
  let fullPath = fieldName;
1957
1957
  if (!this.isFlatPayload && !fieldName.includes(".") && fieldName !== "namespace" && fieldName !== this.contentField) {
1958
1958
  fullPath = `${this.metadataField}.${fieldName}`;
@@ -1964,7 +1964,7 @@ var init_QdrantProvider = __esm({
1964
1964
  });
1965
1965
  } catch (err) {
1966
1966
  let status;
1967
- if (import_axios4.default.isAxiosError(err)) status = (_a3 = err.response) == null ? void 0 : _a3.status;
1967
+ if (import_axios4.default.isAxiosError(err)) status = (_a2 = err.response) == null ? void 0 : _a2.status;
1968
1968
  if (status === 409) return;
1969
1969
  console.warn(`[QdrantProvider] \u26A0\uFE0F Could not ensure index for "${fullPath}":`, err instanceof Error ? err.message : String(err));
1970
1970
  }
@@ -1993,7 +1993,7 @@ var init_QdrantProvider = __esm({
1993
1993
  await this.http.put(`/collections/${this.indexName}/points`, payload);
1994
1994
  }
1995
1995
  async query(vector, topK, namespace, _filter) {
1996
- var _a3;
1996
+ var _a2;
1997
1997
  const must = [];
1998
1998
  if (namespace) {
1999
1999
  must.push({ key: "namespace", match: { value: namespace } });
@@ -2015,7 +2015,7 @@ var init_QdrantProvider = __esm({
2015
2015
  limit: topK,
2016
2016
  with_payload: true,
2017
2017
  params: {
2018
- hnsw_ef: ((_a3 = this.config.options) == null ? void 0 : _a3.efSearch) || Math.max(topK * 20, 128),
2018
+ hnsw_ef: ((_a2 = this.config.options) == null ? void 0 : _a2.efSearch) || Math.max(topK * 20, 128),
2019
2019
  exact: false
2020
2020
  },
2021
2021
  filter: must.length > 0 ? { must } : void 0
@@ -2110,12 +2110,12 @@ var init_ChromaDBProvider = __esm({
2110
2110
  * Get or create the ChromaDB collection.
2111
2111
  */
2112
2112
  async initialize() {
2113
- var _a3;
2113
+ var _a2;
2114
2114
  try {
2115
2115
  const { data } = await this.http.get(`/api/v1/collections/${this.indexName}`);
2116
2116
  this.collectionId = data.id;
2117
2117
  } catch (err) {
2118
- if (import_axios5.default.isAxiosError(err) && ((_a3 = err.response) == null ? void 0 : _a3.status) === 404) {
2118
+ if (import_axios5.default.isAxiosError(err) && ((_a2 = err.response) == null ? void 0 : _a2.status) === 404) {
2119
2119
  const { data } = await this.http.post("/api/v1/collections", {
2120
2120
  name: this.indexName
2121
2121
  });
@@ -2333,7 +2333,7 @@ var init_WeaviateProvider = __esm({
2333
2333
  await this.http.post("/v1/batch/objects", payload);
2334
2334
  }
2335
2335
  async query(vector, topK, namespace, _filter) {
2336
- var _a3, _b;
2336
+ var _a2, _b;
2337
2337
  const queryText = _filter == null ? void 0 : _filter.queryText;
2338
2338
  const sanitizedFilter = this.sanitizeFilter(_filter);
2339
2339
  const searchParams = queryText ? `hybrid: { query: ${JSON.stringify(queryText)}, alpha: 0.5 }` : `nearVector: { vector: ${JSON.stringify(vector)} }`;
@@ -2359,7 +2359,7 @@ var init_WeaviateProvider = __esm({
2359
2359
  `
2360
2360
  };
2361
2361
  const { data } = await this.http.post("/v1/graphql", graphqlQuery);
2362
- const results = ((_b = (_a3 = data.data) == null ? void 0 : _a3.Get) == null ? void 0 : _b[this.indexName]) || [];
2362
+ const results = ((_b = (_a2 = data.data) == null ? void 0 : _a2.Get) == null ? void 0 : _b[this.indexName]) || [];
2363
2363
  return results.map((res) => ({
2364
2364
  id: res["_additional"].id,
2365
2365
  score: 1 - res["_additional"].distance,
@@ -2441,21 +2441,21 @@ var init_UniversalVectorProvider = __esm({
2441
2441
  }
2442
2442
  }
2443
2443
  async initialize() {
2444
- var _a3;
2444
+ var _a2;
2445
2445
  this.http = import_axios8.default.create({
2446
2446
  baseURL: this.opts.baseUrl,
2447
2447
  headers: __spreadValues({
2448
2448
  "Content-Type": "application/json"
2449
2449
  }, this.opts.headers),
2450
- timeout: (_a3 = this.opts.timeout) != null ? _a3 : 3e4
2450
+ timeout: (_a2 = this.opts.timeout) != null ? _a2 : 3e4
2451
2451
  });
2452
2452
  if (!await this.ping()) {
2453
2453
  throw new Error(`[UniversalVectorProvider] Failed to connect to ${this.opts.baseUrl}`);
2454
2454
  }
2455
2455
  }
2456
2456
  async upsert(doc, namespace) {
2457
- var _a3, _b, _c;
2458
- const endpoint = (_a3 = this.opts.upsertEndpoint) != null ? _a3 : "/upsert";
2457
+ var _a2, _b, _c;
2458
+ const endpoint = (_a2 = this.opts.upsertEndpoint) != null ? _a2 : "/upsert";
2459
2459
  const template = (_b = this.opts.upsertTemplate) != null ? _b : JSON.stringify({
2460
2460
  id: "{{id}}",
2461
2461
  vector: "{{vector}}",
@@ -2488,8 +2488,8 @@ var init_UniversalVectorProvider = __esm({
2488
2488
  }
2489
2489
  }
2490
2490
  async query(vector, topK, namespace, filter) {
2491
- var _a3, _b;
2492
- const endpoint = (_a3 = this.opts.queryEndpoint) != null ? _a3 : "/query";
2491
+ var _a2, _b;
2492
+ const endpoint = (_a2 = this.opts.queryEndpoint) != null ? _a2 : "/query";
2493
2493
  const template = (_b = this.opts.queryTemplate) != null ? _b : JSON.stringify({
2494
2494
  vector: "{{vector}}",
2495
2495
  limit: "{{topK}}",
@@ -2515,9 +2515,9 @@ var init_UniversalVectorProvider = __esm({
2515
2515
  );
2516
2516
  }
2517
2517
  return results.map((item) => {
2518
- var _a4, _b2, _c, _d, _e, _f, _g2;
2518
+ var _a3, _b2, _c, _d, _e, _f, _g2;
2519
2519
  return {
2520
- id: item[(_a4 = this.opts.queryIdField) != null ? _a4 : "id"],
2520
+ id: item[(_a3 = this.opts.queryIdField) != null ? _a3 : "id"],
2521
2521
  score: (_c = item[(_b2 = this.opts.queryScoreField) != null ? _b2 : "score"]) != null ? _c : 0,
2522
2522
  content: (_e = item[(_d = this.opts.queryContentField) != null ? _d : "content"]) != null ? _e : "",
2523
2523
  metadata: (_g2 = item[(_f = this.opts.queryMetadataField) != null ? _f : "metadata"]) != null ? _g2 : {}
@@ -2530,8 +2530,8 @@ var init_UniversalVectorProvider = __esm({
2530
2530
  }
2531
2531
  }
2532
2532
  async delete(id, namespace) {
2533
- var _a3, _b;
2534
- const endpoint = (_a3 = this.opts.deleteEndpoint) != null ? _a3 : "/delete";
2533
+ var _a2, _b;
2534
+ const endpoint = (_a2 = this.opts.deleteEndpoint) != null ? _a2 : "/delete";
2535
2535
  const template = (_b = this.opts.deleteTemplate) != null ? _b : JSON.stringify({
2536
2536
  id: "{{id}}",
2537
2537
  namespace: "{{namespace}}"
@@ -2549,8 +2549,8 @@ var init_UniversalVectorProvider = __esm({
2549
2549
  }
2550
2550
  }
2551
2551
  async deleteNamespace(namespace) {
2552
- var _a3;
2553
- const endpoint = (_a3 = this.opts.deleteNamespaceEndpoint) != null ? _a3 : "/delete-namespace";
2552
+ var _a2;
2553
+ const endpoint = (_a2 = this.opts.deleteNamespaceEndpoint) != null ? _a2 : "/delete-namespace";
2554
2554
  try {
2555
2555
  await this.http.post(endpoint, { namespace });
2556
2556
  } catch (error) {
@@ -2560,9 +2560,9 @@ var init_UniversalVectorProvider = __esm({
2560
2560
  }
2561
2561
  }
2562
2562
  async ping() {
2563
- var _a3;
2563
+ var _a2;
2564
2564
  try {
2565
- const endpoint = (_a3 = this.opts.pingEndpoint) != null ? _a3 : "/health";
2565
+ const endpoint = (_a2 = this.opts.pingEndpoint) != null ? _a2 : "/health";
2566
2566
  const response = await this.http.get(endpoint, { timeout: 5e3 });
2567
2567
  return response.status >= 200 && response.status < 300;
2568
2568
  } catch (e) {
@@ -2801,13 +2801,13 @@ var LicenseValidationError = class extends RetrivoraError {
2801
2801
  }
2802
2802
  };
2803
2803
  function wrapError(err, defaultCode, defaultMessage) {
2804
- var _a3;
2804
+ var _a2;
2805
2805
  if (err instanceof RetrivoraError) {
2806
2806
  return err;
2807
2807
  }
2808
2808
  const error = err;
2809
2809
  const message = (error == null ? void 0 : error.message) || defaultMessage || String(err);
2810
- 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);
2810
+ 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);
2811
2811
  const code = error == null ? void 0 : error.code;
2812
2812
  if (status === 429 || /rate[- ]?limit/i.test(message) || code === "RATE_LIMIT_EXCEEDED") {
2813
2813
  return new RateLimitException(message, err);
@@ -3094,8 +3094,8 @@ function getRuntimeConfig() {
3094
3094
  return _runtimeConfig;
3095
3095
  }
3096
3096
  function readString(env, name) {
3097
- var _a3;
3098
- const value = (_a3 = env[name]) == null ? void 0 : _a3.trim();
3097
+ var _a2;
3098
+ const value = (_a2 = env[name]) == null ? void 0 : _a2.trim();
3099
3099
  return value ? value : void 0;
3100
3100
  }
3101
3101
  function readNumber(env, name, fallback) {
@@ -3108,8 +3108,8 @@ function readNumber(env, name, fallback) {
3108
3108
  return parsed;
3109
3109
  }
3110
3110
  function readEnum(env, name, fallback, allowed) {
3111
- var _a3;
3112
- const value = (_a3 = readString(env, name)) != null ? _a3 : fallback;
3111
+ var _a2;
3112
+ const value = (_a2 = readString(env, name)) != null ? _a2 : fallback;
3113
3113
  if (allowed.includes(value)) {
3114
3114
  return value;
3115
3115
  }
@@ -3119,58 +3119,52 @@ function getRagConfig(baseConfig, env = process.env) {
3119
3119
  return getEnvConfig(env, baseConfig);
3120
3120
  }
3121
3121
  function getEnvConfig(env = process.env, base) {
3122
- 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;
3123
- 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;
3124
- const jwtProjectId = (() => {
3125
- if (!licenseKey) return void 0;
3126
- try {
3127
- const parts = licenseKey.split(".");
3128
- if (parts.length >= 2) {
3129
- const payload = JSON.parse(Buffer.from(parts[1], "base64url").toString("utf8"));
3130
- return payload == null ? void 0 : payload.projectId;
3131
- }
3132
- } catch (e) {
3133
- }
3134
- return void 0;
3135
- })();
3136
- 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";
3137
- 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);
3122
+ 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;
3123
+ const licenseKey = (_c = (_b = (_a2 = readString(env, "RAG_LICENSE_KEY")) != null ? _a2 : readString(env, "RETRIVORA_LICENSE_KEY")) != null ? _b : (
3124
+ // Fix SEC-4: NEXT_PUBLIC_RETRIVORA_LICENSE_KEY is NOT included here.
3125
+ // That variable is inlined into the browser bundle by Next.js at build time
3126
+ // and must never be used as a server-side secret source. Developers who
3127
+ // previously relied on this should migrate to RETRIVORA_LICENSE_KEY.
3128
+ readString(env, "LICENSE_KEY")
3129
+ )) != null ? _c : base == null ? void 0 : base.licenseKey;
3130
+ 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";
3131
+ 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);
3138
3132
  const vectorProvider = readEnum(env, "VECTOR_DB_PROVIDER", "pinecone", VECTOR_DB_PROVIDERS);
3139
3133
  const embeddingDimensions = readNumber(env, "EMBEDDING_DIMENSIONS", 768);
3140
3134
  const vectorDbOptions = {};
3141
3135
  if (vectorProvider === "pinecone") {
3142
- 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 : "";
3143
- 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";
3136
+ 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 : "";
3137
+ 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";
3144
3138
  } else if (vectorProvider === "pgvector" || vectorProvider === "postgresql") {
3145
- 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 : "";
3146
- 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;
3147
- 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;
3139
+ 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 : "";
3140
+ 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;
3141
+ 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;
3148
3142
  vectorDbOptions.dimensions = embeddingDimensions;
3149
3143
  } else if (vectorProvider === "mongodb") {
3150
- 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 : "";
3151
- 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 : "";
3152
- 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 : "";
3153
- 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";
3144
+ 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 : "";
3145
+ 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 : "";
3146
+ 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 : "";
3147
+ 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";
3154
3148
  } else if (vectorProvider === "qdrant") {
3155
- 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";
3156
- 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;
3149
+ 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";
3150
+ 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;
3157
3151
  vectorDbOptions.dimensions = embeddingDimensions;
3158
3152
  } else if (vectorProvider === "milvus") {
3159
- vectorDbOptions.baseUrl = (_ha = readString(env, "MILVUS_URL")) != null ? _ha : "http://localhost:19530";
3153
+ vectorDbOptions.baseUrl = (_da = readString(env, "MILVUS_URL")) != null ? _da : "http://localhost:19530";
3160
3154
  vectorDbOptions.apiKey = readString(env, "MILVUS_API_KEY");
3161
3155
  } else if (vectorProvider === "chromadb") {
3162
- vectorDbOptions.baseUrl = (_ia = readString(env, "CHROMADB_URL")) != null ? _ia : "http://localhost:8000";
3156
+ vectorDbOptions.baseUrl = (_ea = readString(env, "CHROMADB_URL")) != null ? _ea : "http://localhost:8000";
3163
3157
  } else if (vectorProvider === "weaviate") {
3164
- vectorDbOptions.baseUrl = (_ja = readString(env, "WEAVIATE_URL")) != null ? _ja : "http://localhost:8080";
3158
+ vectorDbOptions.baseUrl = (_fa = readString(env, "WEAVIATE_URL")) != null ? _fa : "http://localhost:8080";
3165
3159
  vectorDbOptions.apiKey = readString(env, "WEAVIATE_API_KEY");
3166
3160
  } else if (vectorProvider === "redis") {
3167
- vectorDbOptions.baseUrl = (_ka = readString(env, "REDIS_URL")) != null ? _ka : "";
3161
+ vectorDbOptions.baseUrl = (_ga = readString(env, "REDIS_URL")) != null ? _ga : "";
3168
3162
  vectorDbOptions.apiKey = readString(env, "REDIS_API_KEY");
3169
3163
  } else if (vectorProvider === "rest") {
3170
- vectorDbOptions.baseUrl = (_la = readString(env, "VECTOR_DB_REST_URL")) != null ? _la : "";
3164
+ vectorDbOptions.baseUrl = (_ha = readString(env, "VECTOR_DB_REST_URL")) != null ? _ha : "";
3171
3165
  vectorDbOptions.headers = readString(env, "VECTOR_DB_REST_API_KEY") ? { "api-key": readString(env, "VECTOR_DB_REST_API_KEY") } : {};
3172
3166
  } else if (vectorProvider === "universal_rest") {
3173
- vectorDbOptions.baseUrl = (_na = (_ma = readString(env, "VECTOR_BASE_URL")) != null ? _ma : readString(env, "VECTOR_DB_REST_URL")) != null ? _na : "";
3167
+ vectorDbOptions.baseUrl = (_ja = (_ia = readString(env, "VECTOR_BASE_URL")) != null ? _ia : readString(env, "VECTOR_DB_REST_URL")) != null ? _ja : "";
3174
3168
  vectorDbOptions.profile = readString(env, "VECTOR_UNIVERSAL_PROFILE");
3175
3169
  vectorDbOptions.headers = readString(env, "VECTOR_DB_REST_API_KEY") ? { Authorization: `Bearer ${readString(env, "VECTOR_DB_REST_API_KEY")}` } : {};
3176
3170
  }
@@ -3189,8 +3183,8 @@ function getEnvConfig(env = process.env, base) {
3189
3183
  // Anthropic needs a separate embedding provider; key kept for completeness
3190
3184
  gemini: readString(env, "GEMINI_API_KEY"),
3191
3185
  ollama: void 0,
3192
- universal_rest: (_oa = readString(env, "EMBEDDING_API_KEY")) != null ? _oa : readString(env, "OPENAI_API_KEY"),
3193
- custom: (_pa = readString(env, "EMBEDDING_API_KEY")) != null ? _pa : readString(env, "OPENAI_API_KEY")
3186
+ universal_rest: (_ka = readString(env, "EMBEDDING_API_KEY")) != null ? _ka : readString(env, "OPENAI_API_KEY"),
3187
+ custom: (_la = readString(env, "EMBEDDING_API_KEY")) != null ? _la : readString(env, "OPENAI_API_KEY")
3194
3188
  };
3195
3189
  const DEFAULT_MODEL_BY_PROVIDER = {
3196
3190
  openai: "gpt-4o",
@@ -3212,27 +3206,27 @@ function getEnvConfig(env = process.env, base) {
3212
3206
  })();
3213
3207
  const tierBasedLLMModel = isFreeTier2 ? "llama-3.1-8b-instant" : "llama-3.3-70b-versatile";
3214
3208
  const tierBasedEmbeddingModel = isFreeTier2 ? "text-embedding-004" : "text-embedding-004";
3215
- const controlPlaneUrl = (_qa = readString(env, "RETRIVORA_CONTROL_PLANE_URL")) != null ? _qa : readString(env, "NEXT_PUBLIC_RETRIVORA_CONTROL_PLANE_URL");
3209
+ const controlPlaneUrl = (_ma = readString(env, "RETRIVORA_CONTROL_PLANE_URL")) != null ? _ma : readString(env, "NEXT_PUBLIC_RETRIVORA_CONTROL_PLANE_URL");
3216
3210
  const defaultGatewayUrl = controlPlaneUrl ? `${controlPlaneUrl.replace(/\/$/, "")}/api/v1` : "https://llm.retrivora.com/api/v1";
3217
3211
  const rt = _runtimeConfig;
3218
3212
  const defaultLlmProvider = "universal_rest";
3219
- 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;
3220
- 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;
3221
- 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;
3222
- 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;
3223
- 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";
3213
+ 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;
3214
+ 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;
3215
+ 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;
3216
+ 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;
3217
+ 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";
3224
3218
  const defaultEmbeddingProvider = "universal_rest";
3225
- 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;
3226
- 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;
3227
- 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;
3228
- 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;
3229
- 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";
3219
+ 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;
3220
+ 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;
3221
+ 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;
3222
+ 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;
3223
+ 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";
3230
3224
  return __spreadProps(__spreadValues({
3231
3225
  projectId,
3232
3226
  licenseKey,
3233
3227
  vectorDb: {
3234
3228
  provider: vectorProvider,
3235
- indexName: (_gb = (_fb = readString(env, "VECTOR_DB_INDEX")) != null ? _fb : vectorDbOptions.indexName) != null ? _gb : "rag-index",
3229
+ indexName: (_cb = (_bb = readString(env, "VECTOR_DB_INDEX")) != null ? _bb : vectorDbOptions.indexName) != null ? _cb : "rag-index",
3236
3230
  options: vectorDbOptions
3237
3231
  },
3238
3232
  llm: {
@@ -3240,8 +3234,8 @@ function getEnvConfig(env = process.env, base) {
3240
3234
  model: llmModel,
3241
3235
  apiKey: llmApiKey,
3242
3236
  baseUrl: llmBaseUrl,
3243
- systemPrompt: (_hb = readString(env, "LLM_SYSTEM_PROMPT")) != null ? _hb : rt == null ? void 0 : rt.systemPrompt,
3244
- maxTokens: readNumber(env, "LLM_MAX_TOKENS", (_ib = rt == null ? void 0 : rt.maxTokens) != null ? _ib : 4096),
3237
+ systemPrompt: (_db = readString(env, "LLM_SYSTEM_PROMPT")) != null ? _db : rt == null ? void 0 : rt.systemPrompt,
3238
+ maxTokens: readNumber(env, "LLM_MAX_TOKENS", (_eb = rt == null ? void 0 : rt.maxTokens) != null ? _eb : 4096),
3245
3239
  temperature: readNumber(env, "LLM_TEMPERATURE", 0.7),
3246
3240
  options: {
3247
3241
  profile: llmProfile,
@@ -3262,30 +3256,30 @@ function getEnvConfig(env = process.env, base) {
3262
3256
  }
3263
3257
  },
3264
3258
  ui: {
3265
- title: (_kb = (_jb = readString(env, "NEXT_PUBLIC_UI_TITLE")) != null ? _jb : readString(env, "UI_TITLE")) != null ? _kb : "AI Assistant",
3266
- subtitle: (_mb = (_lb = readString(env, "NEXT_PUBLIC_UI_SUBTITLE")) != null ? _lb : readString(env, "UI_SUBTITLE")) != null ? _mb : "Powered by RAG",
3267
- primaryColor: (_ob = (_nb = readString(env, "NEXT_PUBLIC_PRIMARY_COLOR")) != null ? _nb : readString(env, "UI_PRIMARY_COLOR")) != null ? _ob : "#10b981",
3268
- accentColor: (_qb = (_pb = readString(env, "NEXT_PUBLIC_ACCENT_COLOR")) != null ? _pb : readString(env, "UI_ACCENT_COLOR")) != null ? _qb : "#3b82f6",
3269
- logoUrl: (_rb = readString(env, "NEXT_PUBLIC_LOGO_URL")) != null ? _rb : readString(env, "UI_LOGO_URL"),
3270
- placeholder: (_tb = (_sb = readString(env, "NEXT_PUBLIC_PLACEHOLDER")) != null ? _sb : readString(env, "UI_PLACEHOLDER")) != null ? _tb : "Ask me anything\u2026",
3271
- showSources: ((_vb = (_ub = readString(env, "NEXT_PUBLIC_SHOW_SOURCES")) != null ? _ub : readString(env, "UI_SHOW_SOURCES")) != null ? _vb : "true") !== "false",
3272
- 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.",
3273
- visualStyle: (_zb = (_yb = readString(env, "NEXT_PUBLIC_UI_VISUAL_STYLE")) != null ? _yb : readString(env, "UI_VISUAL_STYLE")) != null ? _zb : "glass",
3274
- borderRadius: (_Bb = (_Ab = readString(env, "NEXT_PUBLIC_UI_BORDER_RADIUS")) != null ? _Ab : readString(env, "UI_BORDER_RADIUS")) != null ? _Bb : "xl",
3275
- allowUpload: ((_Db = (_Cb = readString(env, "NEXT_PUBLIC_ALLOW_UPLOAD")) != null ? _Cb : readString(env, "UI_ALLOW_UPLOAD")) != null ? _Db : "false") === "true"
3259
+ title: (_gb = (_fb = readString(env, "NEXT_PUBLIC_UI_TITLE")) != null ? _fb : readString(env, "UI_TITLE")) != null ? _gb : "AI Assistant",
3260
+ subtitle: (_ib = (_hb = readString(env, "NEXT_PUBLIC_UI_SUBTITLE")) != null ? _hb : readString(env, "UI_SUBTITLE")) != null ? _ib : "Powered by RAG",
3261
+ primaryColor: (_kb = (_jb = readString(env, "NEXT_PUBLIC_PRIMARY_COLOR")) != null ? _jb : readString(env, "UI_PRIMARY_COLOR")) != null ? _kb : "#10b981",
3262
+ accentColor: (_mb = (_lb = readString(env, "NEXT_PUBLIC_ACCENT_COLOR")) != null ? _lb : readString(env, "UI_ACCENT_COLOR")) != null ? _mb : "#3b82f6",
3263
+ logoUrl: (_nb = readString(env, "NEXT_PUBLIC_LOGO_URL")) != null ? _nb : readString(env, "UI_LOGO_URL"),
3264
+ placeholder: (_pb = (_ob = readString(env, "NEXT_PUBLIC_PLACEHOLDER")) != null ? _ob : readString(env, "UI_PLACEHOLDER")) != null ? _pb : "Ask me anything\u2026",
3265
+ showSources: ((_rb = (_qb = readString(env, "NEXT_PUBLIC_SHOW_SOURCES")) != null ? _qb : readString(env, "UI_SHOW_SOURCES")) != null ? _rb : "true") !== "false",
3266
+ 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.",
3267
+ visualStyle: (_vb = (_ub = readString(env, "NEXT_PUBLIC_UI_VISUAL_STYLE")) != null ? _ub : readString(env, "UI_VISUAL_STYLE")) != null ? _vb : "glass",
3268
+ borderRadius: (_xb = (_wb = readString(env, "NEXT_PUBLIC_UI_BORDER_RADIUS")) != null ? _wb : readString(env, "UI_BORDER_RADIUS")) != null ? _xb : "xl",
3269
+ allowUpload: ((_zb = (_yb = readString(env, "NEXT_PUBLIC_ALLOW_UPLOAD")) != null ? _yb : readString(env, "UI_ALLOW_UPLOAD")) != null ? _zb : "false") === "true"
3276
3270
  },
3277
3271
  rag: {
3278
- topK: readNumber(env, "RAG_TOP_K", (_Eb = rt == null ? void 0 : rt.topK) != null ? _Eb : 5),
3272
+ topK: readNumber(env, "RAG_TOP_K", (_Ab = rt == null ? void 0 : rt.topK) != null ? _Ab : 5),
3279
3273
  scoreThreshold: readNumber(env, "RAG_SCORE_THRESHOLD", 0),
3280
- chunkSize: readNumber(env, "RAG_CHUNK_SIZE", (_Fb = rt == null ? void 0 : rt.chunkSize) != null ? _Fb : 1e3),
3281
- chunkOverlap: readNumber(env, "RAG_CHUNK_OVERLAP", (_Gb = rt == null ? void 0 : rt.chunkOverlap) != null ? _Gb : 200),
3282
- filterableFields: (_Hb = readString(env, "RAG_FILTERABLE_FIELDS")) == null ? void 0 : _Hb.split(",").map((f) => f.trim()),
3274
+ chunkSize: readNumber(env, "RAG_CHUNK_SIZE", (_Bb = rt == null ? void 0 : rt.chunkSize) != null ? _Bb : 1e3),
3275
+ chunkOverlap: readNumber(env, "RAG_CHUNK_OVERLAP", (_Cb = rt == null ? void 0 : rt.chunkOverlap) != null ? _Cb : 200),
3276
+ filterableFields: (_Db = readString(env, "RAG_FILTERABLE_FIELDS")) == null ? void 0 : _Db.split(",").map((f) => f.trim()),
3283
3277
  // Query pipeline toggles — read from .env.local
3284
3278
  useQueryTransformation: readString(env, "RAG_USE_QUERY_TRANSFORMATION") === "true",
3285
3279
  useReranking: readString(env, "RAG_USE_RERANKING") === "true",
3286
3280
  useGraphRetrieval: readString(env, "RAG_USE_GRAPH_RETRIEVAL") === "true",
3287
- architecture: (_Ib = readString(env, "RAG_ARCHITECTURE")) != null ? _Ib : "simple",
3288
- chunkingStrategy: (_Jb = readString(env, "RAG_CHUNKING_STRATEGY")) != null ? _Jb : "recursive",
3281
+ architecture: (_Eb = readString(env, "RAG_ARCHITECTURE")) != null ? _Eb : "simple",
3282
+ chunkingStrategy: (_Fb = readString(env, "RAG_CHUNKING_STRATEGY")) != null ? _Fb : "recursive",
3289
3283
  uiMapping: (() => {
3290
3284
  const raw = readString(env, "RAG_UI_MAPPING");
3291
3285
  if (!raw) return void 0;
@@ -3298,7 +3292,7 @@ function getEnvConfig(env = process.env, base) {
3298
3292
  },
3299
3293
  telemetry: {
3300
3294
  enabled: telemetryEnabled,
3301
- 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"
3295
+ 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"
3302
3296
  }
3303
3297
  }, readString(env, "GRAPH_DB_PROVIDER") ? {
3304
3298
  graphDb: {
@@ -3311,8 +3305,8 @@ function getEnvConfig(env = process.env, base) {
3311
3305
  }
3312
3306
  } : {}), {
3313
3307
  mcpServers: (() => {
3314
- var _a4;
3315
- const raw = (_a4 = readString(env, "MCP_SERVERS")) != null ? _a4 : readString(env, "NEXT_PUBLIC_MCP_SERVERS");
3308
+ var _a3;
3309
+ const raw = (_a3 = readString(env, "MCP_SERVERS")) != null ? _a3 : readString(env, "NEXT_PUBLIC_MCP_SERVERS");
3316
3310
  if (!raw) return void 0;
3317
3311
  try {
3318
3312
  return JSON.parse(raw);
@@ -3358,10 +3352,10 @@ var ConfigResolver = class {
3358
3352
  * fallback behavior.
3359
3353
  */
3360
3354
  static resolveUniversal(hostConfig, env = process.env) {
3361
- var _a3;
3355
+ var _a2;
3362
3356
  if (!hostConfig) return this.resolve(void 0, env);
3363
3357
  const normalized = __spreadProps(__spreadValues({}, hostConfig), {
3364
- vectorDb: (_a3 = hostConfig.vectorDb) != null ? _a3 : hostConfig.vectorDatabase,
3358
+ vectorDb: (_a2 = hostConfig.vectorDb) != null ? _a2 : hostConfig.vectorDatabase,
3365
3359
  rag: this.mergeRetrievalWorkflow(hostConfig.rag, hostConfig.retrieval, hostConfig.workflow)
3366
3360
  });
3367
3361
  return this.resolve(normalized, env);
@@ -3381,11 +3375,11 @@ var ConfigResolver = class {
3381
3375
  }
3382
3376
  }
3383
3377
  static mergeRetrievalWorkflow(rag, retrieval, workflow) {
3384
- var _a3, _b, _c, _d, _e, _f;
3378
+ var _a2, _b, _c, _d, _e, _f;
3385
3379
  if (!rag && !retrieval && !workflow) return void 0;
3386
3380
  const normalized = __spreadValues({}, rag != null ? rag : {});
3387
3381
  if (retrieval) {
3388
- normalized.topK = (_a3 = retrieval.topK) != null ? _a3 : normalized.topK;
3382
+ normalized.topK = (_a2 = retrieval.topK) != null ? _a2 : normalized.topK;
3389
3383
  normalized.scoreThreshold = (_b = retrieval.scoreThreshold) != null ? _b : normalized.scoreThreshold;
3390
3384
  normalized.useReranking = (_c = retrieval.useReranking) != null ? _c : normalized.useReranking;
3391
3385
  if (retrieval.strategy === "hybrid") normalized.architecture = (_d = normalized.architecture) != null ? _d : "hybrid";
@@ -3513,8 +3507,8 @@ var OpenAIProvider = class {
3513
3507
  };
3514
3508
  }
3515
3509
  async chat(messages, context, options) {
3516
- var _a3, _b, _c, _d, _e, _f, _g2, _h, _i;
3517
- 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.";
3510
+ var _a2, _b, _c, _d, _e, _f, _g2, _h, _i;
3511
+ 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.";
3518
3512
  const systemMessage = {
3519
3513
  role: "system",
3520
3514
  content: buildSystemContent(basePrompt, context)
@@ -3538,8 +3532,8 @@ var OpenAIProvider = class {
3538
3532
  }
3539
3533
  chatStream(messages, context, options) {
3540
3534
  return __asyncGenerator(this, null, function* () {
3541
- var _a3, _b, _c, _d, _e, _f, _g2, _h;
3542
- 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.";
3535
+ var _a2, _b, _c, _d, _e, _f, _g2, _h;
3536
+ 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.";
3543
3537
  const systemMessage = {
3544
3538
  role: "system",
3545
3539
  content: buildSystemContent(basePrompt, context)
@@ -3586,8 +3580,8 @@ var OpenAIProvider = class {
3586
3580
  return results[0];
3587
3581
  }
3588
3582
  async batchEmbed(texts, options) {
3589
- var _a3, _b, _c, _d, _e;
3590
- 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";
3583
+ var _a2, _b, _c, _d, _e;
3584
+ 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";
3591
3585
  const apiKey = (_e = (_d = this.embeddingConfig) == null ? void 0 : _d.apiKey) != null ? _e : this.llmConfig.apiKey;
3592
3586
  const client = apiKey !== this.llmConfig.apiKey ? new import_openai.default({ apiKey }) : this.client;
3593
3587
  const response = await client.embeddings.create({ model, input: texts });
@@ -3646,11 +3640,7 @@ var AnthropicProvider = class {
3646
3640
  try {
3647
3641
  const { default: Anthropic2 } = await import("@anthropic-ai/sdk");
3648
3642
  const client = new Anthropic2({ apiKey });
3649
- await client.messages.create({
3650
- model: modelName,
3651
- max_tokens: 10,
3652
- messages: [{ role: "user", content: "ping" }]
3653
- });
3643
+ await client.models.list();
3654
3644
  return { healthy: true, provider: "anthropic", capabilities: { model: modelName }, timestamp };
3655
3645
  } catch (error) {
3656
3646
  return {
@@ -3664,8 +3654,8 @@ var AnthropicProvider = class {
3664
3654
  };
3665
3655
  }
3666
3656
  async chat(messages, context, options) {
3667
- var _a3, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k;
3668
- 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.";
3657
+ var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k;
3658
+ 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.";
3669
3659
  const system = buildSystemContent(basePrompt, context);
3670
3660
  const anthropicMessages = messages.map((m) => ({
3671
3661
  role: m.role === "assistant" ? "assistant" : "user",
@@ -3703,8 +3693,8 @@ var AnthropicProvider = class {
3703
3693
  }
3704
3694
  chatStream(messages, context, options) {
3705
3695
  return __asyncGenerator(this, null, function* () {
3706
- var _a3, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k;
3707
- 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.";
3696
+ var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k;
3697
+ 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.";
3708
3698
  const system = buildSystemContent(basePrompt, context);
3709
3699
  const anthropicMessages = messages.map((m) => ({
3710
3700
  role: m.role === "assistant" ? "assistant" : "user",
@@ -3788,8 +3778,8 @@ var AnthropicProvider = class {
3788
3778
  var import_axios = __toESM(require("axios"));
3789
3779
  var OllamaProvider = class {
3790
3780
  constructor(llmConfig, embeddingConfig) {
3791
- var _a3, _b, _c;
3792
- const rawBaseURL = ((_a3 = llmConfig.baseUrl) != null ? _a3 : "http://localhost:11434").replace(/\/+$/, "");
3781
+ var _a2, _b, _c;
3782
+ const rawBaseURL = ((_a2 = llmConfig.baseUrl) != null ? _a2 : "http://localhost:11434").replace(/\/+$/, "");
3793
3783
  const baseURL = rawBaseURL.replace(/\/(?:api\/v1|v1|api)$/i, "");
3794
3784
  const timeout = Number((_b = llmConfig.options) == null ? void 0 : _b.timeout) || 3e5;
3795
3785
  const headers = ((_c = llmConfig.options) == null ? void 0 : _c.headers) || {};
@@ -3822,8 +3812,8 @@ var OllamaProvider = class {
3822
3812
  const baseUrl = config.baseUrl || "http://localhost:11434";
3823
3813
  const modelName = config.model;
3824
3814
  try {
3825
- const axios11 = (await import("axios")).default;
3826
- const { data } = await axios11.get(`${baseUrl}/api/tags`);
3815
+ const axios10 = (await import("axios")).default;
3816
+ const { data } = await axios10.get(`${baseUrl}/api/tags`);
3827
3817
  const models = data.models || [];
3828
3818
  const hasModel = models.some((m) => m.name === modelName || m.name.startsWith(`${modelName}:`));
3829
3819
  return {
@@ -3849,8 +3839,8 @@ var OllamaProvider = class {
3849
3839
  };
3850
3840
  }
3851
3841
  async chat(messages, context, options) {
3852
- var _a3, _b, _c, _d, _e, _f;
3853
- 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.";
3842
+ var _a2, _b, _c, _d, _e, _f;
3843
+ 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.";
3854
3844
  const system = buildSystemContent(basePrompt, context);
3855
3845
  const { data } = await this.http.post("/api/chat", {
3856
3846
  model: this.llmConfig.model,
@@ -3868,8 +3858,8 @@ var OllamaProvider = class {
3868
3858
  }
3869
3859
  chatStream(messages, context, options) {
3870
3860
  return __asyncGenerator(this, null, function* () {
3871
- var _a3, _b, _c, _d, _e, _f, _g2, _h;
3872
- 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.";
3861
+ var _a2, _b, _c, _d, _e, _f, _g2, _h;
3862
+ 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.";
3873
3863
  const system = buildSystemContent(basePrompt, context);
3874
3864
  const response = yield new __await(this.http.post("/api/chat", {
3875
3865
  model: this.llmConfig.model,
@@ -3927,8 +3917,8 @@ var OllamaProvider = class {
3927
3917
  });
3928
3918
  }
3929
3919
  async embed(text, options) {
3930
- var _a3, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k;
3931
- 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";
3920
+ var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k;
3921
+ 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";
3932
3922
  const baseURL = (_f = (_e = (_d = this.embeddingConfig) == null ? void 0 : _d.baseUrl) != null ? _e : this.llmConfig.baseUrl) != null ? _f : "http://localhost:11434";
3933
3923
  const client = baseURL !== ((_g2 = this.llmConfig.baseUrl) != null ? _g2 : "http://localhost:11434") ? import_axios.default.create({ baseURL, timeout: 6e4 }) : this.http;
3934
3924
  let prompt = text;
@@ -4029,9 +4019,9 @@ var GeminiProvider = class {
4029
4019
  static getHealthChecker() {
4030
4020
  return {
4031
4021
  async check(config) {
4032
- var _a3, _b;
4022
+ var _a2, _b;
4033
4023
  const timestamp = Date.now();
4034
- const apiKey = (_b = (_a3 = config.apiKey) != null ? _a3 : process.env.GOOGLE_GENAI_API_KEY) != null ? _b : "";
4024
+ const apiKey = (_b = (_a2 = config.apiKey) != null ? _a2 : process.env.GOOGLE_GENAI_API_KEY) != null ? _b : "";
4035
4025
  const modelName = sanitizeModel(config.model);
4036
4026
  try {
4037
4027
  const { GoogleGenerativeAI: GoogleGenerativeAI2 } = await import("@google/generative-ai");
@@ -4061,16 +4051,16 @@ var GeminiProvider = class {
4061
4051
  /** Resolve the embedding client — uses a separate client when the embedding
4062
4052
  * API key differs from the LLM API key. */
4063
4053
  get embeddingClient() {
4064
- var _a3;
4065
- if (((_a3 = this.embeddingConfig) == null ? void 0 : _a3.apiKey) && this.embeddingConfig.apiKey !== this.llmConfig.apiKey) {
4054
+ var _a2;
4055
+ if (((_a2 = this.embeddingConfig) == null ? void 0 : _a2.apiKey) && this.embeddingConfig.apiKey !== this.llmConfig.apiKey) {
4066
4056
  return buildClient(this.embeddingConfig.apiKey);
4067
4057
  }
4068
4058
  return this.client;
4069
4059
  }
4070
4060
  /** Resolve the embedding model to use, in order of specificity. */
4071
4061
  resolveEmbeddingModel(optionsModel) {
4072
- var _a3, _b;
4073
- return sanitizeModel((_b = optionsModel != null ? optionsModel : (_a3 = this.embeddingConfig) == null ? void 0 : _a3.model) != null ? _b : DEFAULT_EMBEDDING_MODEL);
4062
+ var _a2, _b;
4063
+ return sanitizeModel((_b = optionsModel != null ? optionsModel : (_a2 = this.embeddingConfig) == null ? void 0 : _a2.model) != null ? _b : DEFAULT_EMBEDDING_MODEL);
4074
4064
  }
4075
4065
  /**
4076
4066
  * Convert ChatMessage[] to the Gemini contents format.
@@ -4090,8 +4080,8 @@ var GeminiProvider = class {
4090
4080
  // ILLMProvider — chat
4091
4081
  // -------------------------------------------------------------------------
4092
4082
  async chat(messages, context, options) {
4093
- var _a3, _b, _c, _d, _e, _f;
4094
- 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.";
4083
+ var _a2, _b, _c, _d, _e, _f;
4084
+ 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.";
4095
4085
  const model = this.client.getGenerativeModel({
4096
4086
  model: this.llmConfig.model,
4097
4087
  systemInstruction: buildSystemContent(basePrompt, context)
@@ -4108,8 +4098,8 @@ var GeminiProvider = class {
4108
4098
  }
4109
4099
  chatStream(messages, context, options) {
4110
4100
  return __asyncGenerator(this, null, function* () {
4111
- var _a3, _b, _c, _d, _e, _f;
4112
- 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.";
4101
+ var _a2, _b, _c, _d, _e, _f;
4102
+ 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.";
4113
4103
  const model = this.client.getGenerativeModel({
4114
4104
  model: this.llmConfig.model,
4115
4105
  systemInstruction: buildSystemContent(basePrompt, context)
@@ -4144,11 +4134,11 @@ var GeminiProvider = class {
4144
4134
  // ILLMProvider — embeddings
4145
4135
  // -------------------------------------------------------------------------
4146
4136
  async embed(text, options) {
4147
- var _a3, _b;
4137
+ var _a2, _b;
4148
4138
  const content = applyPrefix(
4149
4139
  text,
4150
4140
  options == null ? void 0 : options.taskType,
4151
- (_a3 = this.embeddingConfig) == null ? void 0 : _a3.queryPrefix,
4141
+ (_a2 = this.embeddingConfig) == null ? void 0 : _a2.queryPrefix,
4152
4142
  (_b = this.embeddingConfig) == null ? void 0 : _b.documentPrefix
4153
4143
  );
4154
4144
  const modelName = this.resolveEmbeddingModel(options == null ? void 0 : options.model);
@@ -4165,7 +4155,7 @@ var GeminiProvider = class {
4165
4155
  const modelName = this.resolveEmbeddingModel(options == null ? void 0 : options.model);
4166
4156
  const model = this.embeddingClient.getGenerativeModel({ model: modelName });
4167
4157
  const requests = texts.map((text) => {
4168
- var _a3, _b;
4158
+ var _a2, _b;
4169
4159
  return {
4170
4160
  content: {
4171
4161
  role: "user",
@@ -4173,7 +4163,7 @@ var GeminiProvider = class {
4173
4163
  text: applyPrefix(
4174
4164
  text,
4175
4165
  options == null ? void 0 : options.taskType,
4176
- (_a3 = this.embeddingConfig) == null ? void 0 : _a3.queryPrefix,
4166
+ (_a2 = this.embeddingConfig) == null ? void 0 : _a2.queryPrefix,
4177
4167
  (_b = this.embeddingConfig) == null ? void 0 : _b.documentPrefix
4178
4168
  )
4179
4169
  }]
@@ -4190,8 +4180,8 @@ var GeminiProvider = class {
4190
4180
  throw err;
4191
4181
  });
4192
4182
  return response.embeddings.map((e) => {
4193
- var _a3;
4194
- return (_a3 = e.values) != null ? _a3 : [];
4183
+ var _a2;
4184
+ return (_a2 = e.values) != null ? _a2 : [];
4195
4185
  });
4196
4186
  }
4197
4187
  // -------------------------------------------------------------------------
@@ -4280,8 +4270,8 @@ var GroqProvider = class {
4280
4270
  };
4281
4271
  }
4282
4272
  async chat(messages, context, options) {
4283
- var _a3, _b, _c, _d, _e, _f, _g2, _h, _i;
4284
- 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.";
4273
+ var _a2, _b, _c, _d, _e, _f, _g2, _h, _i;
4274
+ 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.";
4285
4275
  const systemMessage = {
4286
4276
  role: "system",
4287
4277
  content: buildSystemContent(basePrompt, context)
@@ -4305,8 +4295,8 @@ var GroqProvider = class {
4305
4295
  }
4306
4296
  chatStream(messages, context, options) {
4307
4297
  return __asyncGenerator(this, null, function* () {
4308
- var _a3, _b, _c, _d, _e, _f, _g2, _h;
4309
- 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;
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.";
4310
4300
  const systemMessage = {
4311
4301
  role: "system",
4312
4302
  content: buildSystemContent(basePrompt, context)
@@ -4440,8 +4430,8 @@ var QwenProvider = class {
4440
4430
  };
4441
4431
  }
4442
4432
  async chat(messages, context, options) {
4443
- var _a3, _b, _c, _d, _e, _f, _g2, _h, _i;
4444
- 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.";
4433
+ var _a2, _b, _c, _d, _e, _f, _g2, _h, _i;
4434
+ 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.";
4445
4435
  const systemMessage = {
4446
4436
  role: "system",
4447
4437
  content: buildSystemContent(basePrompt, context)
@@ -4465,8 +4455,8 @@ var QwenProvider = class {
4465
4455
  }
4466
4456
  chatStream(messages, context, options) {
4467
4457
  return __asyncGenerator(this, null, function* () {
4468
- var _a3, _b, _c, _d, _e, _f, _g2, _h;
4469
- 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.";
4458
+ var _a2, _b, _c, _d, _e, _f, _g2, _h;
4459
+ 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.";
4470
4460
  const systemMessage = {
4471
4461
  role: "system",
4472
4462
  content: buildSystemContent(basePrompt, context)
@@ -4513,8 +4503,8 @@ var QwenProvider = class {
4513
4503
  return results[0];
4514
4504
  }
4515
4505
  async batchEmbed(texts, options) {
4516
- var _a3, _b, _c, _d, _e, _f;
4517
- 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";
4506
+ var _a2, _b, _c, _d, _e, _f;
4507
+ 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";
4518
4508
  const apiKey = (_e = (_d = this.embeddingConfig) == null ? void 0 : _d.apiKey) != null ? _e : this.llmConfig.apiKey;
4519
4509
  const client = apiKey !== this.llmConfig.apiKey ? new import_openai3.default({
4520
4510
  apiKey,
@@ -4614,9 +4604,9 @@ var VECTOR_PROFILES = {
4614
4604
 
4615
4605
  // src/llm/providers/UniversalLLMAdapter.ts
4616
4606
  function extractContent(obj) {
4617
- var _a3, _b, _c;
4607
+ var _a2, _b, _c;
4618
4608
  if (!obj || typeof obj !== "object") return void 0;
4619
- const choice = (_a3 = obj.choices) == null ? void 0 : _a3[0];
4609
+ const choice = (_a2 = obj.choices) == null ? void 0 : _a2[0];
4620
4610
  if (!choice) return void 0;
4621
4611
  if (typeof ((_b = choice.message) == null ? void 0 : _b.content) === "string") return choice.message.content;
4622
4612
  if (typeof ((_c = choice.delta) == null ? void 0 : _c.content) === "string") return choice.delta.content;
@@ -4626,10 +4616,10 @@ function extractContent(obj) {
4626
4616
  }
4627
4617
  var UniversalLLMAdapter = class {
4628
4618
  constructor(config) {
4629
- var _a3, _b, _c, _d, _e, _f, _g2, _h;
4619
+ var _a2, _b, _c, _d, _e, _f, _g2, _h;
4630
4620
  this.model = config.model;
4631
4621
  const llmConfig = config;
4632
- const options = (_a3 = llmConfig.options) != null ? _a3 : {};
4622
+ const options = (_a2 = llmConfig.options) != null ? _a2 : {};
4633
4623
  let profile = {};
4634
4624
  if (typeof options.profile === "string") {
4635
4625
  profile = LLM_PROFILES[options.profile] || {};
@@ -4655,8 +4645,8 @@ var UniversalLLMAdapter = class {
4655
4645
  });
4656
4646
  }
4657
4647
  async chat(messages, context) {
4658
- var _a3, _b, _c, _d, _e, _f, _g2, _h, _i, _j;
4659
- const path2 = (_a3 = this.opts.chatPath) != null ? _a3 : "/chat/completions";
4648
+ var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j;
4649
+ const path2 = (_a2 = this.opts.chatPath) != null ? _a2 : "/chat/completions";
4660
4650
  const safeMessages = (Array.isArray(messages) ? messages : []).filter((m) => Boolean(m && typeof m === "object")).map((m) => ({
4661
4651
  role: m.role || "user",
4662
4652
  content: typeof m.content === "string" ? m.content : m.content != null ? String(m.content) : ""
@@ -4733,8 +4723,8 @@ ${context != null ? context : "None"}` },
4733
4723
  */
4734
4724
  chatStream(messages, context) {
4735
4725
  return __asyncGenerator(this, null, function* () {
4736
- var _a3, _b, _c, _d, _e, _f;
4737
- const path2 = (_a3 = this.opts.chatPath) != null ? _a3 : "/chat/completions";
4726
+ var _a2, _b, _c, _d, _e, _f;
4727
+ const path2 = (_a2 = this.opts.chatPath) != null ? _a2 : "/chat/completions";
4738
4728
  const url = `${this.baseUrl.replace(/\/$/, "")}${path2}`;
4739
4729
  const extractPath = ((_b = this.opts.responseExtractPath) != null ? _b : "choices[0].message.content").replace("message.content", "delta.content");
4740
4730
  const safeMessages = (Array.isArray(messages) ? messages : []).filter((m) => Boolean(m && typeof m === "object")).map((m) => ({
@@ -4860,8 +4850,8 @@ ${context != null ? context : "None"}` },
4860
4850
  });
4861
4851
  }
4862
4852
  async embed(text) {
4863
- var _a3, _b, _c, _d, _e, _f, _g2, _h;
4864
- const path2 = (_a3 = this.opts.embedPath) != null ? _a3 : "/embeddings";
4853
+ var _a2, _b, _c, _d, _e, _f, _g2, _h;
4854
+ const path2 = (_a2 = this.opts.embedPath) != null ? _a2 : "/embeddings";
4865
4855
  const payload = this.opts.embedPayloadTemplate ? buildPayload(this.opts.embedPayloadTemplate, { input: text, model: this.model }) : { input: text, model: this.model };
4866
4856
  try {
4867
4857
  const { data } = await this.http.post(path2, payload);
@@ -4985,7 +4975,7 @@ var LLMFactory = class _LLMFactory {
4985
4975
  ];
4986
4976
  }
4987
4977
  static create(llmConfig, embeddingConfig) {
4988
- var _a3, _b, _c;
4978
+ var _a2, _b, _c;
4989
4979
  switch (llmConfig.provider) {
4990
4980
  case "openai":
4991
4981
  return new OpenAIProvider(llmConfig, embeddingConfig);
@@ -5004,7 +4994,7 @@ var LLMFactory = class _LLMFactory {
5004
4994
  case "custom":
5005
4995
  return new UniversalLLMAdapter(llmConfig);
5006
4996
  default: {
5007
- const providerName = String((_a3 = llmConfig.provider) != null ? _a3 : "").toLowerCase();
4997
+ const providerName = String((_a2 = llmConfig.provider) != null ? _a2 : "").toLowerCase();
5008
4998
  const customFactory = customProviders.get(providerName);
5009
4999
  if (customFactory) {
5010
5000
  return customFactory(llmConfig);
@@ -5106,7 +5096,7 @@ var ProviderRegistry = class {
5106
5096
  return null;
5107
5097
  }
5108
5098
  static async loadVectorProviderClass(provider) {
5109
- var _a3;
5099
+ var _a2;
5110
5100
  if (this.vectorProviders[provider]) return this.vectorProviders[provider];
5111
5101
  switch (provider) {
5112
5102
  case "pinecone": {
@@ -5115,7 +5105,7 @@ var ProviderRegistry = class {
5115
5105
  }
5116
5106
  case "pgvector":
5117
5107
  case "postgresql": {
5118
- const postgresMode = ((_a3 = process.env.POSTGRES_MODE) != null ? _a3 : "multi").toLowerCase();
5108
+ const postgresMode = ((_a2 = process.env.POSTGRES_MODE) != null ? _a2 : "multi").toLowerCase();
5119
5109
  if (postgresMode === "single") {
5120
5110
  const { PostgreSQLProvider: PostgreSQLProvider2 } = await Promise.resolve().then(() => (init_PostgreSQLProvider(), PostgreSQLProvider_exports));
5121
5111
  return PostgreSQLProvider2;
@@ -5407,13 +5397,15 @@ var DocumentChunker = class {
5407
5397
  }
5408
5398
  }
5409
5399
  const parts = text.split(separator);
5410
- for (const part of parts) {
5411
- if (part.length <= chunkSize) {
5412
- finalSplits.push(part + (part === parts[parts.length - 1] ? "" : separator));
5400
+ for (let i = 0; i < parts.length; i++) {
5401
+ const part = parts[i];
5402
+ const splitVal = (i === 0 ? "" : separator) + part;
5403
+ if (splitVal.length <= chunkSize) {
5404
+ finalSplits.push(splitVal);
5413
5405
  } else if (nextSeparators.length > 0) {
5414
- finalSplits.push(...this.recursiveSplit(part, nextSeparators, chunkSize));
5406
+ finalSplits.push(...this.recursiveSplit(splitVal, nextSeparators, chunkSize));
5415
5407
  } else {
5416
- finalSplits.push(part);
5408
+ finalSplits.push(splitVal);
5417
5409
  }
5418
5410
  }
5419
5411
  return finalSplits;
@@ -5492,74 +5484,108 @@ Text to extract from:
5492
5484
  };
5493
5485
 
5494
5486
  // src/rag/Reranker.ts
5487
+ var MAX_RERANK_CONTENT_CHARS = 64e3;
5495
5488
  var Reranker = class {
5496
5489
  /**
5497
5490
  * Re-ranks matches based on a secondary relevance score using an LLM.
5491
+ *
5492
+ * Fix RAG-2: keyword re-ranking no longer mutates the original `score`
5493
+ * field (which consumers interpret as the raw vector similarity). A new
5494
+ * `rerankScore` field is added so both the original similarity and the
5495
+ * boosted rank signal are available to callers.
5498
5496
  */
5499
5497
  async rerank(matches, query, limit = 5, llm) {
5500
5498
  if (!llm || matches.length <= 1) {
5501
- const keywords = query.toLowerCase().replace(/[^\w\s]/g, "").split(/\s+/).filter((w) => w.length > 2);
5502
- if (keywords.length === 0) {
5503
- return matches.sort((a, b) => b.score - a.score).slice(0, limit);
5504
- }
5505
- const scoredMatches = matches.map((match) => {
5506
- var _a3;
5507
- const contentLower = ((_a3 = match == null ? void 0 : match.content) != null ? _a3 : "").toLowerCase();
5508
- let keywordScore = 0;
5509
- keywords.forEach((keyword) => {
5510
- if (contentLower.includes(keyword)) {
5511
- keywordScore += 1;
5512
- }
5513
- });
5514
- const normalizedKeywordScore = keywordScore / keywords.length;
5515
- const combinedScore = match.score + normalizedKeywordScore * 0.5;
5516
- return __spreadProps(__spreadValues({}, match), {
5517
- score: combinedScore
5518
- });
5519
- });
5520
- return scoredMatches.sort((a, b) => b.score - a.score).slice(0, limit);
5499
+ return this.keywordRerank(matches, query, limit);
5521
5500
  }
5522
5501
  try {
5523
- const topN = matches.slice(0, 10);
5524
- const response = await llm.chat(
5525
- [{ role: "user", content: `Query: "${query}"
5502
+ return await this.llmRerank(matches, query, limit, llm);
5503
+ } catch (error) {
5504
+ console.warn("[Reranker] LLM re-ranking failed, falling back to keyword sorting:", error);
5505
+ return this.keywordRerank(matches, query, limit);
5506
+ }
5507
+ }
5508
+ /**
5509
+ * Keyword-based re-ranking fallback.
5510
+ *
5511
+ * Fix RAG-2: `rerankScore` is a separate field; `score` (the original
5512
+ * vector similarity) is never mutated. Callers that display `score` to
5513
+ * end-users receive the canonical cosine similarity value.
5514
+ */
5515
+ keywordRerank(matches, query, limit) {
5516
+ const keywords = query.toLowerCase().replace(/[^\w\s]/g, "").split(/\s+/).filter((w) => w.length > 2);
5517
+ if (keywords.length === 0) {
5518
+ return matches.sort((a, b) => b.score - a.score).slice(0, limit);
5519
+ }
5520
+ const scoredMatches = matches.map((match) => {
5521
+ var _a2;
5522
+ const contentLower = ((_a2 = match == null ? void 0 : match.content) != null ? _a2 : "").toLowerCase();
5523
+ let keywordHits = 0;
5524
+ keywords.forEach((keyword) => {
5525
+ if (contentLower.includes(keyword)) {
5526
+ keywordHits++;
5527
+ }
5528
+ });
5529
+ const normalizedKeywordScore = keywordHits / keywords.length;
5530
+ return __spreadProps(__spreadValues({}, match), {
5531
+ rerankScore: match.score + normalizedKeywordScore * 0.5
5532
+ });
5533
+ });
5534
+ return scoredMatches.sort((a, b) => {
5535
+ var _a2, _b;
5536
+ return ((_a2 = b.rerankScore) != null ? _a2 : b.score) - ((_b = a.rerankScore) != null ? _b : a.score);
5537
+ }).slice(0, limit);
5538
+ }
5539
+ /**
5540
+ * LLM-based re-ranking.
5541
+ *
5542
+ * Fix RAG-1: total document content is now budgeted against
5543
+ * MAX_RERANK_CONTENT_CHARS to prevent silent truncation or failures
5544
+ * on models with small context windows.
5545
+ */
5546
+ async llmRerank(matches, query, limit, llm) {
5547
+ const candidates = matches.slice(0, 10);
5548
+ let charsUsed = 0;
5549
+ const budgetedCandidates = candidates.filter((m) => {
5550
+ const len = (m.content || "").length;
5551
+ if (charsUsed + len > MAX_RERANK_CONTENT_CHARS) return false;
5552
+ charsUsed += len;
5553
+ return true;
5554
+ });
5555
+ const docList = budgetedCandidates.map((m, i) => {
5556
+ var _a2;
5557
+ return `[${i}] ${((_a2 = m == null ? void 0 : m.content) != null ? _a2 : "").replace(/\n/g, " ")}`;
5558
+ }).join("\n");
5559
+ const response = await llm.chat(
5560
+ [{ role: "user", content: `Query: "${query}"
5526
5561
 
5527
5562
  Documents:
5528
- ${topN.map((m, i) => {
5529
- var _a3;
5530
- return `[${i}] ${((_a3 = m == null ? void 0 : m.content) != null ? _a3 : "").replace(/\n/g, " ")}`;
5531
- }).join("\n")}` }],
5532
- "",
5533
- {
5534
- 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.',
5535
- temperature: 0,
5536
- maxTokens: 50
5537
- }
5538
- );
5539
- const cleanedResponse = response.trim().replace(/[\[\]\s]/g, "");
5540
- const rankedIndices = cleanedResponse.split(",").map(Number).filter((n) => !isNaN(n));
5541
- if (rankedIndices.length > 0) {
5542
- const rankedMatches = [];
5543
- const usedIndices = /* @__PURE__ */ new Set();
5544
- for (const index of rankedIndices) {
5545
- if (index >= 0 && index < topN.length && !usedIndices.has(index)) {
5546
- rankedMatches.push(topN[index]);
5547
- usedIndices.add(index);
5548
- }
5549
- }
5550
- for (let i = 0; i < topN.length; i++) {
5551
- if (!usedIndices.has(i)) {
5552
- rankedMatches.push(topN[i]);
5553
- }
5554
- }
5555
- const rest = matches.slice(10);
5556
- rankedMatches.push(...rest);
5557
- return rankedMatches.slice(0, limit);
5563
+ ${docList}` }],
5564
+ "",
5565
+ {
5566
+ 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.',
5567
+ temperature: 0,
5568
+ maxTokens: 50
5569
+ }
5570
+ );
5571
+ const cleanedResponse = response.trim().replace(/[\[\]\s]/g, "");
5572
+ const rankedIndices = cleanedResponse.split(",").map(Number).filter((n) => !isNaN(n));
5573
+ if (rankedIndices.length === 0) {
5574
+ return matches.sort((a, b) => b.score - a.score).slice(0, limit);
5575
+ }
5576
+ const rankedMatches = [];
5577
+ const usedIndices = /* @__PURE__ */ new Set();
5578
+ for (const index of rankedIndices) {
5579
+ if (index >= 0 && index < budgetedCandidates.length && !usedIndices.has(index)) {
5580
+ rankedMatches.push(budgetedCandidates[index]);
5581
+ usedIndices.add(index);
5558
5582
  }
5559
- } catch (error) {
5560
- console.warn("[Reranker] LLM re-ranking failed, falling back to score-based sorting:", error);
5561
5583
  }
5562
- return matches.sort((a, b) => b.score - a.score).slice(0, limit);
5584
+ for (let i = 0; i < budgetedCandidates.length; i++) {
5585
+ if (!usedIndices.has(i)) rankedMatches.push(budgetedCandidates[i]);
5586
+ }
5587
+ rankedMatches.push(...matches.slice(10));
5588
+ return rankedMatches.slice(0, limit);
5563
5589
  }
5564
5590
  };
5565
5591
 
@@ -5571,11 +5597,11 @@ var LlamaIndexIngestor = class {
5571
5597
  * than standard character-count splitting.
5572
5598
  */
5573
5599
  async chunk(text, options = {}) {
5574
- var _a3, _b;
5600
+ var _a2, _b;
5575
5601
  try {
5576
5602
  const { Document, SentenceSplitter, MetadataMode } = await import(`${"llamaindex"}`);
5577
5603
  const splitter = new SentenceSplitter({
5578
- chunkSize: (_a3 = options.chunkSize) != null ? _a3 : 1e3,
5604
+ chunkSize: (_a2 = options.chunkSize) != null ? _a2 : 1e3,
5579
5605
  chunkOverlap: (_b = options.chunkOverlap) != null ? _b : 200
5580
5606
  });
5581
5607
  const doc = new Document({ text, metadata: options.metadata || {} });
@@ -5683,7 +5709,7 @@ ${error instanceof Error ? error.message : String(error)}`
5683
5709
  * The agent returns `{ messages: [...] }` — the last message is the final answer.
5684
5710
  */
5685
5711
  async run(input, chatHistory = []) {
5686
- var _a3, _b, _c;
5712
+ var _a2, _b, _c;
5687
5713
  if (!this.agent) {
5688
5714
  throw new Error("[LangChainAgent] Agent not initialized. Call initialize() first.");
5689
5715
  }
@@ -5694,7 +5720,7 @@ ${error instanceof Error ? error.message : String(error)}`
5694
5720
  const response = await this.agent.invoke({
5695
5721
  messages: [...historyMessages, new HumanMessage(input)]
5696
5722
  });
5697
- const lastMessage = (_a3 = response == null ? void 0 : response.messages) == null ? void 0 : _a3.at(-1);
5723
+ const lastMessage = (_a2 = response == null ? void 0 : response.messages) == null ? void 0 : _a2.at(-1);
5698
5724
  if (lastMessage && typeof lastMessage.content === "string") {
5699
5725
  return lastMessage.content;
5700
5726
  }
@@ -5745,7 +5771,7 @@ var MCPClient = class {
5745
5771
  }
5746
5772
  }
5747
5773
  async connectStdio() {
5748
- var _a3;
5774
+ var _a2;
5749
5775
  const cmd = this.config.command;
5750
5776
  if (!cmd) {
5751
5777
  throw new Error(`[MCPClient] Command option is required for stdio transport on "${this.config.name}"`);
@@ -5760,7 +5786,7 @@ var MCPClient = class {
5760
5786
  this.stdioBuffer += data.toString("utf-8");
5761
5787
  this.processStdioLines();
5762
5788
  });
5763
- (_a3 = this.childProcess.stderr) == null ? void 0 : _a3.on("data", (data) => {
5789
+ (_a2 = this.childProcess.stderr) == null ? void 0 : _a2.on("data", (data) => {
5764
5790
  console.warn(`[MCPClient] [stderr] [${this.config.name}]:`, data.toString("utf-8"));
5765
5791
  });
5766
5792
  this.childProcess.on("close", (code) => {
@@ -5834,14 +5860,14 @@ var MCPClient = class {
5834
5860
  }
5835
5861
  }
5836
5862
  async sendNotification(method, params) {
5837
- var _a3;
5863
+ var _a2;
5838
5864
  const notification = {
5839
5865
  jsonrpc: "2.0",
5840
5866
  method,
5841
5867
  params
5842
5868
  };
5843
5869
  if (this.config.transport === "stdio") {
5844
- if ((_a3 = this.childProcess) == null ? void 0 : _a3.stdin) {
5870
+ if ((_a2 = this.childProcess) == null ? void 0 : _a2.stdin) {
5845
5871
  this.childProcess.stdin.write(JSON.stringify(notification) + "\n", "utf-8");
5846
5872
  }
5847
5873
  } else if (this.sseUrl) {
@@ -5919,11 +5945,11 @@ var MCPRegistry = class {
5919
5945
  // src/core/MultiAgentCoordinator.ts
5920
5946
  var MultiAgentCoordinator = class {
5921
5947
  constructor(options) {
5922
- var _a3;
5948
+ var _a2;
5923
5949
  this.llmProvider = options.llmProvider;
5924
5950
  this.mcpRegistry = options.mcpRegistry;
5925
5951
  this.documentSearch = options.documentSearch;
5926
- this.maxIterations = (_a3 = options.maxIterations) != null ? _a3 : 5;
5952
+ this.maxIterations = (_a2 = options.maxIterations) != null ? _a2 : 5;
5927
5953
  }
5928
5954
  /**
5929
5955
  * Run the multi-agent coordination loop synchronously and return the final response.
@@ -5975,8 +6001,8 @@ Available Tools:
5975
6001
 
5976
6002
  ${mcpTools.length > 0 ? "MCP Server Tools:" : ""}
5977
6003
  ${mcpTools.map((mt) => {
5978
- var _a3;
5979
- return `- ${mt.tool.name}(${JSON.stringify(((_a3 = mt.tool.inputSchema) == null ? void 0 : _a3.properties) || {})}): ${mt.tool.description || "No description provided."}`;
6004
+ var _a2;
6005
+ return `- ${mt.tool.name}(${JSON.stringify(((_a2 = mt.tool.inputSchema) == null ? void 0 : _a2.properties) || {})}): ${mt.tool.description || "No description provided."}`;
5980
6006
  }).join("\n")}
5981
6007
 
5982
6008
  Tool Calling Protocol:
@@ -6134,100 +6160,6 @@ ${toolResultText}`
6134
6160
  }
6135
6161
  };
6136
6162
 
6137
- // src/core/CircuitBreaker.ts
6138
- var CircuitBreaker = class {
6139
- constructor(options = {}) {
6140
- this.state = "closed";
6141
- this.failureCount = 0;
6142
- this.successCount = 0;
6143
- this.lastFailureAt = 0;
6144
- this.halfOpenCalls = 0;
6145
- var _a3, _b, _c;
6146
- this.failureThreshold = (_a3 = options.failureThreshold) != null ? _a3 : 5;
6147
- this.resetTimeoutMs = (_b = options.resetTimeoutMs) != null ? _b : 3e4;
6148
- this.halfOpenMaxCalls = (_c = options.halfOpenMaxCalls) != null ? _c : 1;
6149
- }
6150
- record(success) {
6151
- const now = Date.now();
6152
- if (success) {
6153
- this.successCount++;
6154
- if (this.state === "half-open") {
6155
- this.state = "closed";
6156
- this.failureCount = 0;
6157
- this.halfOpenCalls = 0;
6158
- this.successCount = 1;
6159
- } else if (this.state === "closed") {
6160
- if (this.successCount >= this.failureThreshold) {
6161
- this.failureCount = Math.max(0, this.failureCount - 1);
6162
- this.successCount = 0;
6163
- }
6164
- }
6165
- } else {
6166
- this.failureCount++;
6167
- this.lastFailureAt = now;
6168
- this.successCount = 0;
6169
- if (this.state === "half-open") {
6170
- this.state = "open";
6171
- this.halfOpenCalls = 0;
6172
- } else if (this.state === "closed" && this.failureCount >= this.failureThreshold) {
6173
- this.state = "open";
6174
- }
6175
- }
6176
- }
6177
- canExecute() {
6178
- const now = Date.now();
6179
- if (this.state === "closed") {
6180
- return true;
6181
- }
6182
- if (this.state === "open") {
6183
- if (now - this.lastFailureAt >= this.resetTimeoutMs) {
6184
- this.state = "half-open";
6185
- this.halfOpenCalls = 0;
6186
- return true;
6187
- }
6188
- return false;
6189
- }
6190
- if (this.state === "half-open") {
6191
- return this.halfOpenCalls < this.halfOpenMaxCalls;
6192
- }
6193
- return true;
6194
- }
6195
- async wrap(fn) {
6196
- if (!this.canExecute()) {
6197
- const retryAfterSec = Math.ceil(
6198
- Math.max(0, this.resetTimeoutMs - (Date.now() - this.lastFailureAt)) / 1e3
6199
- );
6200
- throw new Error(
6201
- `Circuit breaker is open. Try again in ${retryAfterSec}s. Failed ${this.failureCount}/${this.failureThreshold} consecutive calls.`
6202
- );
6203
- }
6204
- if (this.state === "half-open") {
6205
- this.halfOpenCalls++;
6206
- }
6207
- try {
6208
- const result = await fn();
6209
- this.record(true);
6210
- return result;
6211
- } catch (err) {
6212
- this.record(false);
6213
- throw err;
6214
- }
6215
- }
6216
- getState() {
6217
- return this.state;
6218
- }
6219
- getFailureCount() {
6220
- return this.failureCount;
6221
- }
6222
- reset() {
6223
- this.state = "closed";
6224
- this.failureCount = 0;
6225
- this.successCount = 0;
6226
- this.lastFailureAt = 0;
6227
- this.halfOpenCalls = 0;
6228
- }
6229
- };
6230
-
6231
6163
  // src/core/BatchProcessor.ts
6232
6164
  function isTransientError(error) {
6233
6165
  if (!(error instanceof Error)) return false;
@@ -6251,30 +6183,34 @@ function calculateBackoffDelay(attempt, initialDelayMs, maxDelayMs, multiplier)
6251
6183
  function sleep(ms) {
6252
6184
  return new Promise((resolve) => setTimeout(resolve, ms));
6253
6185
  }
6254
- var _BatchProcessor = class _BatchProcessor {
6186
+ var BatchProcessor = class _BatchProcessor {
6255
6187
  /**
6256
- * Execute a processor call through the circuit breaker.
6257
- * Only transient failures count toward opening the circuit.
6188
+ * Execute a processor call, optionally through a caller-provided circuit
6189
+ * breaker. Only transient failures count toward opening the circuit.
6190
+ *
6191
+ * Fix ARCH-3: removed the static module-level `cb` field. Callers must
6192
+ * supply their own CircuitBreaker via BatchOptions.circuitBreaker.
6193
+ * Fix TYPE-1: uses the public retryAfterMs() and incrementHalfOpen()
6194
+ * API instead of accessing private fields via `as any`.
6258
6195
  */
6259
- static async executeWithCircuitBreaker(processor) {
6260
- if (!this.cb.canExecute()) {
6261
- const retryAfterSec = Math.ceil(
6262
- Math.max(0, 3e4 - (Date.now() - this.cb.lastFailureAt || 0)) / 1e3
6263
- );
6196
+ static async executeWithCircuitBreaker(processor, cb) {
6197
+ if (!cb) {
6198
+ return processor();
6199
+ }
6200
+ if (!cb.canExecute()) {
6201
+ const retryAfterSec = Math.ceil(cb.retryAfterMs() / 1e3);
6264
6202
  throw new Error(
6265
6203
  `[BatchProcessor] Circuit breaker is open. Try again in ${retryAfterSec}s.`
6266
6204
  );
6267
6205
  }
6268
- if (this.cb.state === "half-open") {
6269
- this.cb.halfOpenCalls++;
6270
- }
6206
+ cb.incrementHalfOpen();
6271
6207
  try {
6272
6208
  const result = await processor();
6273
- this.cb.record(true);
6209
+ cb.record(true);
6274
6210
  return result;
6275
6211
  } catch (err) {
6276
6212
  if (isTransientError(err)) {
6277
- this.cb.record(false);
6213
+ cb.record(false);
6278
6214
  }
6279
6215
  throw err;
6280
6216
  }
@@ -6319,7 +6255,10 @@ var _BatchProcessor = class _BatchProcessor {
6319
6255
  let lastError;
6320
6256
  for (let attempt = 0; attempt <= maxRetries; attempt++) {
6321
6257
  try {
6322
- const result = await _BatchProcessor.executeWithCircuitBreaker(() => processor(batch));
6258
+ const result = await _BatchProcessor.executeWithCircuitBreaker(
6259
+ () => processor(batch),
6260
+ options == null ? void 0 : options.circuitBreaker
6261
+ );
6323
6262
  results.push(result);
6324
6263
  success = true;
6325
6264
  break;
@@ -6377,7 +6316,10 @@ ${errorMessages}`
6377
6316
  let lastError;
6378
6317
  for (let attempt = 0; attempt <= maxRetries; attempt++) {
6379
6318
  try {
6380
- const result = await _BatchProcessor.executeWithCircuitBreaker(() => processor(item));
6319
+ const result = await _BatchProcessor.executeWithCircuitBreaker(
6320
+ () => processor(item),
6321
+ options == null ? void 0 : options.circuitBreaker
6322
+ );
6381
6323
  results.push(result);
6382
6324
  success = true;
6383
6325
  break;
@@ -6450,7 +6392,10 @@ ${errorMessages}`
6450
6392
  }));
6451
6393
  const chunkPromises = chunk.map(async ({ item, originalIndex }) => {
6452
6394
  try {
6453
- const result = await _BatchProcessor.executeWithCircuitBreaker(() => processor(item));
6395
+ const result = await _BatchProcessor.executeWithCircuitBreaker(
6396
+ () => processor(item),
6397
+ options == null ? void 0 : options.circuitBreaker
6398
+ );
6454
6399
  return { success: true, result, index: originalIndex };
6455
6400
  } catch (err) {
6456
6401
  const error = err instanceof Error ? err : new Error(String(err));
@@ -6478,12 +6423,6 @@ ${errorMessages}`
6478
6423
  return { results, errors, totalProcessed, totalFailed };
6479
6424
  }
6480
6425
  };
6481
- _BatchProcessor.cb = new CircuitBreaker({
6482
- failureThreshold: 5,
6483
- resetTimeoutMs: 3e4,
6484
- halfOpenMaxCalls: 1
6485
- });
6486
- var BatchProcessor = _BatchProcessor;
6487
6426
 
6488
6427
  // src/config/EmbeddingStrategy.ts
6489
6428
  var EmbeddingStrategy = /* @__PURE__ */ ((EmbeddingStrategy2) => {
@@ -6587,7 +6526,7 @@ var QueryProcessor = class {
6587
6526
  * @param validFields Optional list of known filterable fields to look for
6588
6527
  */
6589
6528
  static extractQueryFieldHints(question, validFields = []) {
6590
- var _a3, _b, _c, _d;
6529
+ var _a2, _b, _c, _d, _e, _f, _g2, _h;
6591
6530
  if (!question.trim()) return [];
6592
6531
  const hints = /* @__PURE__ */ new Map();
6593
6532
  const addHint = (value, field) => {
@@ -6632,11 +6571,11 @@ var QueryProcessor = class {
6632
6571
  if (name) addHint(name, "name");
6633
6572
  }
6634
6573
  }
6635
- const genericPattern = /\b([a-z][a-z0-9_]{1,30})\s*[:=]\s*["']?([^"'\n?.!,]{1,60})["']?/gi;
6574
+ const genericPattern = /\b([a-z][a-z0-9_]{1,30})\s*[:=]\s*(?:"([^"\n?.!,]{1,60})"|'([^'\n?.!,]{1,60})'|([^\s\n?.!,:=]{1,60}))/gi;
6636
6575
  for (const match of question.matchAll(genericPattern)) {
6637
6576
  const field = match[1];
6638
- const value = match[2];
6639
- if (field && !this.isLikelyPromptPhrase(field)) {
6577
+ const value = (_b = (_a2 = match[2]) != null ? _a2 : match[3]) != null ? _b : match[4];
6578
+ if (field && value && !this.isLikelyPromptPhrase(field)) {
6640
6579
  const resolvedField = resolveValidField(field);
6641
6580
  if (resolvedField) {
6642
6581
  addHint(value, resolvedField);
@@ -6667,7 +6606,7 @@ var QueryProcessor = class {
6667
6606
  ];
6668
6607
  for (const p of universalPatterns) {
6669
6608
  for (const match of question.matchAll(p.regex)) {
6670
- const val = p.group ? (_a3 = match[p.group]) != null ? _a3 : match[0] : match[0];
6609
+ const val = p.group ? (_c = match[p.group]) != null ? _c : match[0] : match[0];
6671
6610
  if (!val) continue;
6672
6611
  if (p.field) addHint(val, p.field);
6673
6612
  else addHint(val);
@@ -6675,22 +6614,23 @@ var QueryProcessor = class {
6675
6614
  }
6676
6615
  for (const pattern of naturalQuestionPatterns) {
6677
6616
  for (const match of question.matchAll(pattern)) {
6678
- const value = (_b = match[2]) != null ? _b : match[1];
6617
+ const value = (_d = match[2]) != null ? _d : match[1];
6679
6618
  if (value) addHint(value);
6680
6619
  }
6681
6620
  }
6682
6621
  const fieldPattern = `([^\\n:=?.!,]{1,60}?)`;
6683
- const valuePattern = `([^\\n?.!,]{1,120}?)`;
6622
+ const valuePattern = `(?:"([^"\\n?.!,]{1,120})"|'([^'\\n?.!,]{1,120})'|([^\\s\\n?.!,:=]{1,120}))`;
6684
6623
  const fieldValuePatterns = [
6685
- new RegExp(`\\b${fieldPattern}\\s*(?:=|:)\\s*["']?${valuePattern}["']?(?=[?.!,]|$)`, "gi"),
6686
- new RegExp(`\\b${fieldPattern}\\s+(?:is|are|was|were|equals?|equal to|named|called)\\s+["']?${valuePattern}["']?(?=[?.!,]|$)`, "gi"),
6687
- new RegExp(`\\bwith\\s+${fieldPattern}\\s+["']?${valuePattern}["']?(?=[?.!,]|$)`, "gi"),
6688
- new RegExp(`\\b(?:under|in|for|from|of)\\s+${fieldPattern}\\s+["']?${valuePattern}["']?(?=[?.!,]|$)`, "gi")
6624
+ new RegExp(`\\b${fieldPattern}\\s*(?:=|:)\\s*${valuePattern}`, "gi"),
6625
+ new RegExp(`\\b${fieldPattern}\\s+(?:is|are|was|were|equals?|equal to|named|called)\\s+${valuePattern}`, "gi"),
6626
+ new RegExp(`\\bwith\\s+${fieldPattern}\\s+${valuePattern}`, "gi"),
6627
+ new RegExp(`\\b(?:under|in|for|from|of)\\s+${fieldPattern}\\s+${valuePattern}`, "gi")
6689
6628
  ];
6690
6629
  for (const pattern of fieldValuePatterns) {
6691
6630
  for (const match of question.matchAll(pattern)) {
6692
- const field = (_c = match[1]) != null ? _c : "";
6693
- const value = (_d = match[2]) != null ? _d : "";
6631
+ const field = (_e = match[1]) != null ? _e : "";
6632
+ const value = (_h = (_g2 = (_f = match[2]) != null ? _f : match[3]) != null ? _g2 : match[4]) != null ? _h : "";
6633
+ if (!value) continue;
6694
6634
  const resolvedField = resolveValidField(field);
6695
6635
  if (resolvedField) {
6696
6636
  addHint(value, resolvedField);
@@ -6760,10 +6700,10 @@ var QueryProcessor = class {
6760
6700
  }
6761
6701
  static normalizeNumericOperator(operator) {
6762
6702
  const op = operator.toLowerCase().trim();
6763
- if (op === ">" || /\b(greater than|more than|above|over)\b/.test(op)) return "gt";
6764
6703
  if (op === ">=" || /\b(greater than or equal to|more than or equal to|at least)\b/.test(op)) return "gte";
6765
- if (op === "<" || /\b(less than|below|under)\b/.test(op)) return "lt";
6766
6704
  if (op === "<=" || /\b(less than or equal to|at most)\b/.test(op)) return "lte";
6705
+ if (op === ">" || /\b(greater than|more than|above|over)\b/.test(op)) return "gt";
6706
+ if (op === "<" || /\b(less than|below|under)\b/.test(op)) return "lt";
6767
6707
  return "eq";
6768
6708
  }
6769
6709
  static normalizePredicateField(field, validFields) {
@@ -6891,11 +6831,11 @@ var LLMRouter = class {
6891
6831
  * When provided it is used directly as the 'default' role without re-constructing.
6892
6832
  */
6893
6833
  async initialize(prebuiltDefault) {
6894
- var _a3;
6834
+ var _a2;
6895
6835
  const defaultModel = prebuiltDefault != null ? prebuiltDefault : LLMFactory.create(this.config.llm, this.config.embedding);
6896
6836
  this.models.set("default", defaultModel);
6897
6837
  const envFastModel = process.env.FAST_LLM_MODEL;
6898
- const providerFastDefault = (_a3 = FAST_MODEL_DEFAULTS[this.config.llm.provider]) != null ? _a3 : "";
6838
+ const providerFastDefault = (_a2 = FAST_MODEL_DEFAULTS[this.config.llm.provider]) != null ? _a2 : "";
6899
6839
  const fastModelName = envFastModel || providerFastDefault;
6900
6840
  if (fastModelName && fastModelName !== this.config.llm.model) {
6901
6841
  const fastConfig = __spreadProps(__spreadValues({}, this.config.llm), {
@@ -6912,8 +6852,8 @@ var LLMRouter = class {
6912
6852
  * Falls back to 'default' if the requested role is not registered.
6913
6853
  */
6914
6854
  get(role) {
6915
- var _a3;
6916
- const provider = (_a3 = this.models.get(role)) != null ? _a3 : this.models.get("default");
6855
+ var _a2;
6856
+ const provider = (_a2 = this.models.get(role)) != null ? _a2 : this.models.get("default");
6917
6857
  if (!provider) {
6918
6858
  throw new Error(`[LLMRouter] No provider registered for role: "${role}". Did you call initialize()?`);
6919
6859
  }
@@ -6999,8 +6939,8 @@ var IntentClassifier = class {
6999
6939
  numericFieldCount = numericKeys.size;
7000
6940
  categoricalFieldCount = catKeys.size;
7001
6941
  if (productKeyMatches >= 2 || docs.some((d) => {
7002
- var _a3, _b;
7003
- 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");
6942
+ var _a2, _b;
6943
+ 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");
7004
6944
  })) {
7005
6945
  isProductLike = true;
7006
6946
  }
@@ -7190,7 +7130,7 @@ var Rule6SmallResultSetRule = class {
7190
7130
  var Rule7LargeTableRule = class {
7191
7131
  constructor() {
7192
7132
  this.name = "Rule7LargeTable";
7193
- this.priority = 75;
7133
+ this.priority = 82;
7194
7134
  }
7195
7135
  evaluate(context, intent) {
7196
7136
  const docs = context.retrievedDocuments || [];
@@ -7233,8 +7173,8 @@ var RuleEngine = class {
7233
7173
  this.registerRule(new Rule5MixedResponseRule());
7234
7174
  this.registerRule(new Rule2ComparisonRule());
7235
7175
  this.registerRule(new Rule3ProductDiscoveryRule());
7236
- this.registerRule(new Rule4AnalyticalRule());
7237
7176
  this.registerRule(new Rule7LargeTableRule());
7177
+ this.registerRule(new Rule4AnalyticalRule());
7238
7178
  }
7239
7179
  /**
7240
7180
  * Add or replace a rule in the engine.
@@ -7282,8 +7222,8 @@ var TextRendererStrategy = class {
7282
7222
  }
7283
7223
  const docs = Array.isArray(data) ? data : [];
7284
7224
  const text = docs.map((d) => {
7285
- var _a3;
7286
- return (_a3 = d == null ? void 0 : d.content) != null ? _a3 : "";
7225
+ var _a2;
7226
+ return (_a2 = d == null ? void 0 : d.content) != null ? _a2 : "";
7287
7227
  }).join("\n\n");
7288
7228
  return { type: "text", title, data: { content: text || "No detailed content available." } };
7289
7229
  }
@@ -7426,9 +7366,9 @@ var LRUDecisionCache = class {
7426
7366
  this.maxSize = maxSize;
7427
7367
  }
7428
7368
  generateKey(context) {
7429
- var _a3, _b;
7369
+ var _a2, _b;
7430
7370
  const q = (context.userQuery || "").toLowerCase().trim();
7431
- const docCount = (_b = (_a3 = context.retrievedDocuments) == null ? void 0 : _a3.length) != null ? _b : 0;
7371
+ const docCount = (_b = (_a2 = context.retrievedDocuments) == null ? void 0 : _a2.length) != null ? _b : 0;
7432
7372
  return `${q}::docs:${docCount}`;
7433
7373
  }
7434
7374
  get(context) {
@@ -7537,7 +7477,7 @@ var UITransformer = class _UITransformer {
7537
7477
  * Prefer `analyzeAndDecide()` in production.
7538
7478
  */
7539
7479
  static transform(userQuery, retrievedData, config, trainedSchema, intent) {
7540
- var _a3, _b, _c;
7480
+ var _a2, _b, _c;
7541
7481
  if (!retrievedData || retrievedData.length === 0) {
7542
7482
  return this.createTextResponse("No data available", "No relevant data found for your query.");
7543
7483
  }
@@ -7557,7 +7497,7 @@ var UITransformer = class _UITransformer {
7557
7497
  return this.transformToBarChart(filteredData, profile, userQuery, resolvedIntent.visualizationHint === "ranking");
7558
7498
  }
7559
7499
  if (resolvedIntent.visualizationHint === "distribution") {
7560
- return (_a3 = this.transformToHistogram(profile, userQuery)) != null ? _a3 : this.transformToBarChart(filteredData, profile, userQuery);
7500
+ return (_a2 = this.transformToHistogram(profile, userQuery)) != null ? _a2 : this.transformToBarChart(filteredData, profile, userQuery);
7561
7501
  }
7562
7502
  if (resolvedIntent.visualizationHint === "correlation") {
7563
7503
  return (_b = this.transformToScatterPlot(profile, userQuery)) != null ? _b : this.transformToBarChart(filteredData, profile, userQuery);
@@ -7890,11 +7830,11 @@ ${schemaProfileText}` : ""}`;
7890
7830
  };
7891
7831
  }
7892
7832
  static transformToPieChart(data, profile, query = "") {
7893
- var _a3;
7833
+ var _a2;
7894
7834
  const dimension = profile ? this.selectDimensionField(profile, query) : void 0;
7895
7835
  const categories = dimension ? Array.from(new Set(profile.records.map((record) => {
7896
- var _a4;
7897
- return String((_a4 = record.fields[dimension.key]) != null ? _a4 : "");
7836
+ var _a3;
7837
+ return String((_a3 = record.fields[dimension.key]) != null ? _a3 : "");
7898
7838
  }).filter(Boolean))) : this.detectCategories(data);
7899
7839
  if (categories.length === 0) return null;
7900
7840
  const categoryData = dimension && profile ? this.aggregateProfileByDimension(profile, dimension.key) : this.aggregateByCategory(data, categories);
@@ -7904,7 +7844,7 @@ ${schemaProfileText}` : ""}`;
7904
7844
  });
7905
7845
  return {
7906
7846
  type: "pie_chart",
7907
- title: `Distribution by ${(_a3 = dimension == null ? void 0 : dimension.label) != null ? _a3 : "Category"}`,
7847
+ title: `Distribution by ${(_a2 = dimension == null ? void 0 : dimension.label) != null ? _a2 : "Category"}`,
7908
7848
  description: `Showing breakdown across ${pieData.length} categories`,
7909
7849
  data: pieData
7910
7850
  };
@@ -7914,8 +7854,8 @@ ${schemaProfileText}` : ""}`;
7914
7854
  const valueField = profile.numericFields[0];
7915
7855
  const buckets = /* @__PURE__ */ new Map();
7916
7856
  profile.records.forEach((record) => {
7917
- var _a3, _b, _c;
7918
- const timestamp = String((_a3 = record.fields[dateField.key]) != null ? _a3 : "");
7857
+ var _a2, _b, _c;
7858
+ const timestamp = String((_a2 = record.fields[dateField.key]) != null ? _a2 : "");
7919
7859
  const value = (_b = this.toFiniteNumber(record.fields[valueField.key])) != null ? _b : 0;
7920
7860
  buckets.set(timestamp, ((_c = buckets.get(timestamp)) != null ? _c : 0) + value);
7921
7861
  });
@@ -7928,23 +7868,23 @@ ${schemaProfileText}` : ""}`;
7928
7868
  };
7929
7869
  }
7930
7870
  static transformToBarChart(data, profile, query = "", horizontal = false) {
7931
- var _a3;
7871
+ var _a2;
7932
7872
  const dimension = profile ? this.selectDimensionField(profile, query) : void 0;
7933
7873
  const measure = profile ? this.selectNumericField(profile, query) : void 0;
7934
7874
  const aggregate = dimension && profile ? this.aggregateProfileByDimension(profile, dimension.key, measure == null ? void 0 : measure.key) : this.aggregateByCategory(data, this.detectCategories(data));
7935
7875
  const barData = Object.entries(aggregate).map(([category, value]) => ({ category, value: Number(value) })).sort((a, b) => horizontal ? b.value - a.value : 0).slice(0, 12);
7936
7876
  const fallbackData = barData.length > 0 ? barData : data.slice(0, 12).map((item, index) => {
7937
- var _a4, _b, _c, _d, _e;
7877
+ var _a3, _b, _c, _d, _e;
7938
7878
  const meta = item.metadata || {};
7939
7879
  const label = String(
7940
- (_c = (_b = (_a4 = this.getDynamicVal(meta, "name")) != null ? _a4 : meta.label) != null ? _b : meta.title) != null ? _c : `Result ${index + 1}`
7880
+ (_c = (_b = (_a3 = this.getDynamicVal(meta, "name")) != null ? _a3 : meta.label) != null ? _b : meta.title) != null ? _c : `Result ${index + 1}`
7941
7881
  );
7942
7882
  const value = (_e = (_d = this.extractNumericValue(meta)) != null ? _d : item.score) != null ? _e : 0;
7943
7883
  return { category: label, value: Number(value) };
7944
7884
  });
7945
7885
  return {
7946
7886
  type: horizontal ? "horizontal_bar" : "bar_chart",
7947
- title: dimension ? `${(_a3 = measure == null ? void 0 : measure.label) != null ? _a3 : "Count"} by ${dimension.label}` : "Comparison",
7887
+ title: dimension ? `${(_a2 = measure == null ? void 0 : measure.label) != null ? _a2 : "Count"} by ${dimension.label}` : "Comparison",
7948
7888
  description: `Showing ${fallbackData.length} comparable values`,
7949
7889
  data: fallbackData
7950
7890
  };
@@ -7979,11 +7919,11 @@ ${schemaProfileText}` : ""}`;
7979
7919
  if (fields.length < 2) return null;
7980
7920
  const [xField, yField] = fields;
7981
7921
  const points = profile.records.map((record) => {
7982
- var _a3;
7922
+ var _a2;
7983
7923
  const x = this.toFiniteNumber(record.fields[xField.key]);
7984
7924
  const y = this.toFiniteNumber(record.fields[yField.key]);
7985
7925
  if (x === null || y === null) return null;
7986
- return { x, y, label: String((_a3 = this.getRecordLabel(record)) != null ? _a3 : record.id) };
7926
+ return { x, y, label: String((_a2 = this.getRecordLabel(record)) != null ? _a2 : record.id) };
7987
7927
  }).filter((point) => point !== null).slice(0, 100);
7988
7928
  if (points.length === 0) return null;
7989
7929
  return {
@@ -8013,9 +7953,9 @@ ${schemaProfileText}` : ""}`;
8013
7953
  static transformToRadarChart(data) {
8014
7954
  const attributeMap = {};
8015
7955
  data.forEach((item) => {
8016
- var _a3, _b, _c;
7956
+ var _a2, _b, _c;
8017
7957
  const meta = item.metadata || {};
8018
- const seriesName = String((_c = (_b = (_a3 = meta.name) != null ? _a3 : meta.product) != null ? _b : item.id) != null ? _c : "Item");
7958
+ const seriesName = String((_c = (_b = (_a2 = meta.name) != null ? _a2 : meta.product) != null ? _b : item.id) != null ? _c : "Item");
8019
7959
  Object.entries(meta).forEach(([key, val]) => {
8020
7960
  if (typeof val === "number" && !["price", "id"].includes(key.toLowerCase())) {
8021
7961
  if (!attributeMap[key]) attributeMap[key] = {};
@@ -8031,8 +7971,8 @@ ${schemaProfileText}` : ""}`;
8031
7971
  title: "Product Comparison",
8032
7972
  description: `Comparing ${data.length} items across ${radarData.length} attributes`,
8033
7973
  data: radarData.length > 0 ? radarData : data.map((d) => {
8034
- var _a3;
8035
- return { attribute: ((_a3 = d == null ? void 0 : d.content) != null ? _a3 : "").substring(0, 40) };
7974
+ var _a2;
7975
+ return { attribute: ((_a2 = d == null ? void 0 : d.content) != null ? _a2 : "").substring(0, 40) };
8036
7976
  })
8037
7977
  };
8038
7978
  }
@@ -8051,8 +7991,8 @@ ${schemaProfileText}` : ""}`;
8051
7991
  return this.createTextResponse(
8052
7992
  "Retrieved Context",
8053
7993
  data.map((item) => {
8054
- var _a3;
8055
- return (_a3 = item == null ? void 0 : item.content) != null ? _a3 : "";
7994
+ var _a2;
7995
+ return (_a2 = item == null ? void 0 : item.content) != null ? _a2 : "";
8056
7996
  }).join("\n\n"),
8057
7997
  `Found ${data.length} relevant results`
8058
7998
  );
@@ -8103,11 +8043,11 @@ ${schemaProfileText}` : ""}`;
8103
8043
  return null;
8104
8044
  }
8105
8045
  static normalizeTransformation(payload) {
8106
- var _a3, _b, _c, _d, _e, _f, _g2, _h, _i, _j;
8046
+ var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j;
8107
8047
  if (!payload || typeof payload !== "object") return null;
8108
8048
  const p = payload;
8109
8049
  const type = this.normalizeVisualizationType(
8110
- String((_c = (_b = (_a3 = p.type) != null ? _a3 : p.view) != null ? _b : p.chartType) != null ? _c : "")
8050
+ String((_c = (_b = (_a2 = p.type) != null ? _a2 : p.view) != null ? _b : p.chartType) != null ? _c : "")
8111
8051
  );
8112
8052
  if (!type) return null;
8113
8053
  const title = String((_e = (_d = p.title) != null ? _d : p.heading) != null ? _e : "Visualization");
@@ -8118,7 +8058,7 @@ ${schemaProfileText}` : ""}`;
8118
8058
  return this.validateTransformation(transformation) ? transformation : null;
8119
8059
  }
8120
8060
  static normalizeVisualizationType(type) {
8121
- var _a3;
8061
+ var _a2;
8122
8062
  const mapping = {
8123
8063
  pie: "pie_chart",
8124
8064
  pie_chart: "pie_chart",
@@ -8146,7 +8086,7 @@ ${schemaProfileText}` : ""}`;
8146
8086
  product_carousel: "product_carousel",
8147
8087
  carousel: "carousel"
8148
8088
  };
8149
- return (_a3 = mapping[type.toLowerCase()]) != null ? _a3 : null;
8089
+ return (_a2 = mapping[type.toLowerCase()]) != null ? _a2 : null;
8150
8090
  }
8151
8091
  static validateTransformation(t) {
8152
8092
  const { type, data } = t;
@@ -8262,7 +8202,7 @@ ${schemaProfileText}` : ""}`;
8262
8202
  }
8263
8203
  static profileData(data) {
8264
8204
  const records = (data || []).filter((item) => Boolean(item && typeof item === "object")).map((item) => {
8265
- var _a3, _b;
8205
+ var _a2, _b;
8266
8206
  const fields2 = {};
8267
8207
  Object.entries(item.metadata || {}).forEach(([key, value]) => {
8268
8208
  const primitive = this.toPrimitive(value);
@@ -8271,7 +8211,7 @@ ${schemaProfileText}` : ""}`;
8271
8211
  if (!fields2.content && item.content) fields2.content = item.content.substring(0, 500);
8272
8212
  return {
8273
8213
  id: item.id,
8274
- content: (_a3 = item.content) != null ? _a3 : "",
8214
+ content: (_a2 = item.content) != null ? _a2 : "",
8275
8215
  score: (_b = item.score) != null ? _b : 0,
8276
8216
  fields: fields2,
8277
8217
  source: item
@@ -8352,16 +8292,16 @@ ${schemaProfileText}` : ""}`;
8352
8292
  return null;
8353
8293
  }
8354
8294
  static selectDimensionField(profile, query) {
8355
- var _a3, _b;
8295
+ var _a2, _b;
8356
8296
  const productCategory = profile.categoricalFields.find(
8357
8297
  (field) => /category|department|collection|type|group|segment|region|country|state|city/i.test(field.key)
8358
8298
  );
8359
8299
  const ranked = this.rankFieldsByQuery(profile.categoricalFields, query);
8360
- return (_b = (_a3 = ranked[0]) != null ? _a3 : productCategory) != null ? _b : profile.categoricalFields[0];
8300
+ return (_b = (_a2 = ranked[0]) != null ? _a2 : productCategory) != null ? _b : profile.categoricalFields[0];
8361
8301
  }
8362
8302
  static selectNumericField(profile, query) {
8363
- var _a3;
8364
- return (_a3 = this.rankFieldsByQuery(profile.numericFields, query)[0]) != null ? _a3 : profile.numericFields[0];
8303
+ var _a2;
8304
+ return (_a2 = this.rankFieldsByQuery(profile.numericFields, query)[0]) != null ? _a2 : profile.numericFields[0];
8365
8305
  }
8366
8306
  static rankFieldsByQuery(fields, query) {
8367
8307
  const q = query.toLowerCase();
@@ -8390,8 +8330,8 @@ ${schemaProfileText}` : ""}`;
8390
8330
  static aggregateProfileByDimension(profile, dimensionKey, measureKey) {
8391
8331
  const result = {};
8392
8332
  profile.records.forEach((record) => {
8393
- var _a3, _b, _c;
8394
- const category = String((_a3 = record.fields[dimensionKey]) != null ? _a3 : "Other").trim() || "Other";
8333
+ var _a2, _b, _c;
8334
+ const category = String((_a2 = record.fields[dimensionKey]) != null ? _a2 : "Other").trim() || "Other";
8395
8335
  const value = measureKey ? (_b = this.toFiniteNumber(record.fields[measureKey])) != null ? _b : 0 : 1;
8396
8336
  result[category] = ((_c = result[category]) != null ? _c : 0) + value;
8397
8337
  });
@@ -8471,8 +8411,8 @@ ${schemaProfileText}` : ""}`;
8471
8411
  static aggregateByCategory(data, categories) {
8472
8412
  const result = Object.fromEntries(categories.map((c) => [c, 0]));
8473
8413
  data.forEach((item) => {
8474
- var _a3;
8475
- const cat = (_a3 = this.getProductCategory(item)) != null ? _a3 : "Other";
8414
+ var _a2;
8415
+ const cat = (_a2 = this.getProductCategory(item)) != null ? _a2 : "Other";
8476
8416
  if (Object.prototype.hasOwnProperty.call(result, cat)) result[cat]++;
8477
8417
  else result["Other"] = (result["Other"] || 0) + 1;
8478
8418
  });
@@ -8480,17 +8420,17 @@ ${schemaProfileText}` : ""}`;
8480
8420
  }
8481
8421
  static extractTimeSeriesData(data) {
8482
8422
  return data.map((item) => {
8483
- var _a3, _b, _c, _d, _e, _f;
8423
+ var _a2, _b, _c, _d, _e, _f;
8484
8424
  const meta = item.metadata || {};
8485
8425
  return {
8486
- timestamp: (_b = (_a3 = meta.timestamp) != null ? _a3 : meta.date) != null ? _b : (/* @__PURE__ */ new Date()).toISOString(),
8426
+ timestamp: (_b = (_a2 = meta.timestamp) != null ? _a2 : meta.date) != null ? _b : (/* @__PURE__ */ new Date()).toISOString(),
8487
8427
  value: (_d = (_c = meta.value) != null ? _c : item.score) != null ? _d : 0,
8488
8428
  label: (_f = meta.label) != null ? _f : ((_e = item == null ? void 0 : item.content) != null ? _e : "").substring(0, 50)
8489
8429
  };
8490
8430
  });
8491
8431
  }
8492
8432
  static extractNumericValue(meta) {
8493
- var _a3;
8433
+ var _a2;
8494
8434
  const preferredKeys = [
8495
8435
  "value",
8496
8436
  "count",
@@ -8505,7 +8445,7 @@ ${schemaProfileText}` : ""}`;
8505
8445
  "price"
8506
8446
  ];
8507
8447
  for (const key of preferredKeys) {
8508
- const raw = (_a3 = resolveMetadataValue(meta, key)) != null ? _a3 : meta[key];
8448
+ const raw = (_a2 = resolveMetadataValue(meta, key)) != null ? _a2 : meta[key];
8509
8449
  const value = typeof raw === "number" ? raw : Number(String(raw != null ? raw : "").replace(/[$,% ,]/g, ""));
8510
8450
  if (Number.isFinite(value)) return value;
8511
8451
  }
@@ -8600,8 +8540,8 @@ ${schemaProfileText}` : ""}`;
8600
8540
  }, query.includes(normalizedField) ? 3 : 0);
8601
8541
  }
8602
8542
  static resolveTableCellValue(item, column) {
8603
- var _a3, _b, _c;
8604
- if (column === "Content") return ((_a3 = item == null ? void 0 : item.content) != null ? _a3 : "").substring(0, 100);
8543
+ var _a2, _b, _c;
8544
+ if (column === "Content") return ((_a2 = item == null ? void 0 : item.content) != null ? _a2 : "").substring(0, 100);
8605
8545
  const meta = item.metadata || {};
8606
8546
  const normalizedColumn = this.normalizeComparableField(column);
8607
8547
  const exactMetadata = Object.entries(meta).find(
@@ -8653,8 +8593,8 @@ ${schemaProfileText}` : ""}`;
8653
8593
  let inStock = 0;
8654
8594
  let outOfStock = 0;
8655
8595
  data.forEach((d) => {
8656
- var _a3, _b;
8657
- const cat = (_a3 = this.getProductCategory(d)) != null ? _a3 : "Other";
8596
+ var _a2, _b;
8597
+ const cat = (_a2 = this.getProductCategory(d)) != null ? _a2 : "Other";
8658
8598
  if (cat === category) {
8659
8599
  const quantity = (_b = this.extractStockQuantity(d)) != null ? _b : 1;
8660
8600
  if (this.determineStockStatus(d)) inStock += quantity;
@@ -8700,9 +8640,9 @@ ${schemaProfileText}` : ""}`;
8700
8640
  }
8701
8641
  // ─── Product Extraction ───────────────────────────────────────────────────
8702
8642
  static getDynamicVal(meta, uiKey, config, trainedSchema) {
8703
- var _a3;
8643
+ var _a2;
8704
8644
  if (!meta) return void 0;
8705
- const mapping = (_a3 = config == null ? void 0 : config.rag) == null ? void 0 : _a3.uiMapping;
8645
+ const mapping = (_a2 = config == null ? void 0 : config.rag) == null ? void 0 : _a2.uiMapping;
8706
8646
  if ((mapping == null ? void 0 : mapping[uiKey]) && meta[mapping[uiKey]] !== void 0) return meta[mapping[uiKey]];
8707
8647
  if (trainedSchema && typeof trainedSchema === "object") {
8708
8648
  const trainedKey = trainedSchema[uiKey];
@@ -8711,7 +8651,7 @@ ${schemaProfileText}` : ""}`;
8711
8651
  return resolveMetadataValue(meta, uiKey);
8712
8652
  }
8713
8653
  static extractProductInfo(item, config, trainedSchema) {
8714
- var _a3, _b;
8654
+ var _a2, _b;
8715
8655
  if (!item) return null;
8716
8656
  const meta = item.metadata || {};
8717
8657
  const content = item.content || "";
@@ -8719,7 +8659,7 @@ ${schemaProfileText}` : ""}`;
8719
8659
  const price = this.getDynamicVal(meta, "price", config, trainedSchema);
8720
8660
  const brand = this.getDynamicVal(meta, "brand", config, trainedSchema);
8721
8661
  const description = this.cleanProductDescription(
8722
- (_a3 = this.extractProductDescriptionFromContent(content)) != null ? _a3 : this.getProductDescriptionValue(meta, config, trainedSchema)
8662
+ (_a2 = this.extractProductDescriptionFromContent(content)) != null ? _a2 : this.getProductDescriptionValue(meta, config, trainedSchema)
8723
8663
  );
8724
8664
  if (name || this.isProductData(item)) {
8725
8665
  let finalName = name ? String(name) : void 0;
@@ -8854,10 +8794,10 @@ RULES:
8854
8794
  }
8855
8795
  static buildContextSummary(sources, maxChars = 6e3) {
8856
8796
  const items = (sources || []).filter(Boolean).map((s, i) => {
8857
- var _a3, _b, _c, _d;
8797
+ var _a2, _b, _c, _d;
8858
8798
  return {
8859
8799
  index: i + 1,
8860
- content: (_b = (_a3 = s == null ? void 0 : s.content) == null ? void 0 : _a3.substring(0, 400)) != null ? _b : "",
8800
+ content: (_b = (_a2 = s == null ? void 0 : s.content) == null ? void 0 : _a2.substring(0, 400)) != null ? _b : "",
8861
8801
  metadata: (_c = s == null ? void 0 : s.metadata) != null ? _c : {},
8862
8802
  score: (_d = s == null ? void 0 : s.score) != null ? _d : 0
8863
8803
  };
@@ -8897,7 +8837,7 @@ var SchemaMapper = class {
8897
8837
  return promise;
8898
8838
  }
8899
8839
  static async _doTrain(llm, cacheKey, keys) {
8900
- var _a3, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k;
8840
+ var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k;
8901
8841
  const propertyList = Object.entries(this.TARGET_PROPERTIES).map(([prop, desc]) => `- ${prop} (${desc})`).join("\n");
8902
8842
  const messages = [
8903
8843
  { role: "system", content: "You are a database schema expert. Respond ONLY with valid JSON." },
@@ -8912,7 +8852,7 @@ Return a JSON object like {"name":"Title","price":"Price",...}. Omit unmapped pr
8912
8852
  }
8913
8853
  ];
8914
8854
  try {
8915
- const baseUrl = (_a3 = llm == null ? void 0 : llm.baseUrl) != null ? _a3 : "";
8855
+ const baseUrl = (_a2 = llm == null ? void 0 : llm.baseUrl) != null ? _a2 : "";
8916
8856
  const apiKey = (_b = llm == null ? void 0 : llm.apiKey) != null ? _b : "";
8917
8857
  const model = (_c = llm == null ? void 0 : llm.model) != null ? _c : "llama-3.1-8b-instant";
8918
8858
  let responseText;
@@ -9087,9 +9027,9 @@ var Pipeline = class {
9087
9027
  this.initialised = false;
9088
9028
  /** Namespace-specific static cold context cache for CAG */
9089
9029
  this.coldContexts = /* @__PURE__ */ new Map();
9090
- var _a3, _b, _c, _d, _e;
9030
+ var _a2, _b, _c, _d, _e;
9091
9031
  this.chunker = new DocumentChunker(
9092
- (_b = (_a3 = config.rag) == null ? void 0 : _a3.chunkSize) != null ? _b : 1e3,
9032
+ (_b = (_a2 = config.rag) == null ? void 0 : _a2.chunkSize) != null ? _b : 1e3,
9093
9033
  (_d = (_c = config.rag) == null ? void 0 : _c.chunkOverlap) != null ? _d : 200
9094
9034
  );
9095
9035
  if (((_e = config.rag) == null ? void 0 : _e.chunkingStrategy) === "llamaindex") {
@@ -9105,7 +9045,7 @@ var Pipeline = class {
9105
9045
  return this.initialised ? this.llmProvider : void 0;
9106
9046
  }
9107
9047
  async initialize() {
9108
- var _a3, _b, _c, _d;
9048
+ var _a2, _b, _c, _d;
9109
9049
  if (this.initialised) return;
9110
9050
  const chartInstruction = `You are a helpful assistant. Use the provided context to answer questions accurately.
9111
9051
 
@@ -9148,7 +9088,7 @@ var Pipeline = class {
9148
9088
  this.entityExtractor = new EntityExtractor(this.llmProvider);
9149
9089
  }
9150
9090
  await this.vectorDB.initialize();
9151
- 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) {
9091
+ 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) {
9152
9092
  this.mcpRegistry = new MCPRegistry(this.config.mcpServers || []);
9153
9093
  this.multiAgentCoordinator = new MultiAgentCoordinator({
9154
9094
  llmProvider: this.llmProvider,
@@ -9166,8 +9106,8 @@ var Pipeline = class {
9166
9106
  this.initialised = true;
9167
9107
  }
9168
9108
  async loadColdContext(ns) {
9169
- var _a3, _b;
9170
- const cagConfig = (_a3 = this.config.rag) == null ? void 0 : _a3.cag;
9109
+ var _a2, _b;
9110
+ const cagConfig = (_a2 = this.config.rag) == null ? void 0 : _a2.cag;
9171
9111
  if (!cagConfig || !cagConfig.enabled) return;
9172
9112
  try {
9173
9113
  const coldNs = cagConfig.coldNamespace || ns;
@@ -9220,12 +9160,12 @@ ${m.content}`).join("\n\n---\n\n");
9220
9160
  }
9221
9161
  /** Step 1: Chunk the document content. */
9222
9162
  async prepareChunks(doc) {
9223
- var _a3, _b;
9163
+ var _a2, _b;
9224
9164
  if (this.llamaIngestor) {
9225
9165
  return this.llamaIngestor.chunk(doc.content, {
9226
9166
  docId: doc.docId,
9227
9167
  metadata: doc.metadata,
9228
- chunkSize: (_a3 = this.config.rag) == null ? void 0 : _a3.chunkSize,
9168
+ chunkSize: (_a2 = this.config.rag) == null ? void 0 : _a2.chunkSize,
9229
9169
  chunkOverlap: (_b = this.config.rag) == null ? void 0 : _b.chunkOverlap
9230
9170
  });
9231
9171
  }
@@ -9323,9 +9263,9 @@ ${m.content}`).join("\n\n---\n\n");
9323
9263
  return { reply, sources };
9324
9264
  }
9325
9265
  async ask(question, history = [], namespace) {
9326
- var _a3, _b;
9266
+ var _a2, _b;
9327
9267
  await this.initialize();
9328
- 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) {
9268
+ 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) {
9329
9269
  return await this.multiAgentCoordinator.run(question, history);
9330
9270
  }
9331
9271
  const stream = this.askStream(question, history, namespace);
@@ -9360,9 +9300,9 @@ ${m.content}`).join("\n\n---\n\n");
9360
9300
  }
9361
9301
  askStream(_0) {
9362
9302
  return __asyncGenerator(this, arguments, function* (question, history = [], namespace) {
9363
- var _a3, _b;
9303
+ var _a2, _b;
9364
9304
  yield new __await(this.initialize());
9365
- 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) {
9305
+ 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) {
9366
9306
  const stream2 = this.multiAgentCoordinator.runStream(question, history);
9367
9307
  try {
9368
9308
  for (var iter = __forAwait(stream2), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
@@ -9411,10 +9351,10 @@ ${m.content}`).join("\n\n---\n\n");
9411
9351
  */
9412
9352
  askStreamInternal(_0) {
9413
9353
  return __asyncGenerator(this, arguments, function* (question, history = [], namespace) {
9414
- 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;
9354
+ 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;
9415
9355
  yield new __await(this.initialize());
9416
9356
  const ns = formatNamespace(namespace != null ? namespace : this.config.projectId);
9417
- const topK = (_b = (_a3 = this.config.rag) == null ? void 0 : _a3.topK) != null ? _b : 5;
9357
+ const topK = (_b = (_a2 = this.config.rag) == null ? void 0 : _a2.topK) != null ? _b : 5;
9418
9358
  const scoreThreshold = (_d = (_c = this.config.rag) == null ? void 0 : _c.scoreThreshold) != null ? _d : 0.3;
9419
9359
  const requestId = `req_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
9420
9360
  const requestStart = performance.now();
@@ -9463,8 +9403,8 @@ ${m.content}`).join("\n\n---\n\n");
9463
9403
  const rerankStart = performance.now();
9464
9404
  const structuredSources = this.applyStructuredFilters(rawSources, filter).filter((s) => Boolean(s && typeof s === "object"));
9465
9405
  let fullSources = hasNumericPredicates ? structuredSources : structuredSources.filter((m) => {
9466
- var _a4;
9467
- return ((_a4 = m == null ? void 0 : m.score) != null ? _a4 : 0) >= scoreThreshold;
9406
+ var _a3;
9407
+ return ((_a3 = m == null ? void 0 : m.score) != null ? _a3 : 0) >= scoreThreshold;
9468
9408
  });
9469
9409
  const rerankLimit = Math.max(retrievalLimit, fullSources.length);
9470
9410
  const useReranking = arch !== "naive" && (arch === "retrieve-and-rerank" || ((_m = this.config.rag) == null ? void 0 : _m.useReranking));
@@ -9477,13 +9417,13 @@ ${m.content}`).join("\n\n---\n\n");
9477
9417
  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]
9478
9418
 
9479
9419
  ` + promptSources.map((m, i) => {
9480
- var _a4;
9420
+ var _a3;
9481
9421
  return `[Source ${i + 1}]
9482
- ${(_a4 = m == null ? void 0 : m.content) != null ? _a4 : ""}`;
9422
+ ${(_a3 = m == null ? void 0 : m.content) != null ? _a3 : ""}`;
9483
9423
  }).join("\n\n---\n\n") : "No relevant context found.";
9484
9424
  const sources = [...fullSources].filter((s) => Boolean(s && typeof s === "object")).sort((a, b) => {
9485
- var _a4, _b2;
9486
- return ((_a4 = b == null ? void 0 : b.score) != null ? _a4 : 0) - ((_b2 = a == null ? void 0 : a.score) != null ? _b2 : 0);
9425
+ var _a3, _b2;
9426
+ return ((_a3 = b == null ? void 0 : b.score) != null ? _a3 : 0) - ((_b2 = a == null ? void 0 : a.score) != null ? _b2 : 0);
9487
9427
  });
9488
9428
  if (graphData && graphData.nodes.length > 0) {
9489
9429
  const graphContext = graphData.nodes.map(
@@ -9511,10 +9451,10 @@ ${context}`;
9511
9451
  yield {
9512
9452
  reply: "",
9513
9453
  sources: (sources || []).filter((s) => Boolean(s && typeof s === "object")).map((s) => {
9514
- var _a4, _b2, _c2;
9454
+ var _a3, _b2, _c2;
9515
9455
  return {
9516
9456
  id: s.id,
9517
- score: (_a4 = s.score) != null ? _a4 : 0,
9457
+ score: (_a3 = s.score) != null ? _a3 : 0,
9518
9458
  content: (_b2 = s == null ? void 0 : s.content) != null ? _b2 : "",
9519
9459
  metadata: (_c2 = s == null ? void 0 : s.metadata) != null ? _c2 : {},
9520
9460
  namespace: ns
@@ -9707,11 +9647,11 @@ ${context}`;
9707
9647
  systemPrompt,
9708
9648
  userPrompt: question + finalRestrictionSuffix,
9709
9649
  chunks: (sources || []).filter(Boolean).map((s) => {
9710
- var _a4, _b2;
9650
+ var _a3, _b2;
9711
9651
  return {
9712
9652
  id: s.id,
9713
9653
  score: s.score,
9714
- content: (_a4 = s == null ? void 0 : s.content) != null ? _a4 : "",
9654
+ content: (_a3 = s == null ? void 0 : s.content) != null ? _a3 : "",
9715
9655
  metadata: (_b2 = s == null ? void 0 : s.metadata) != null ? _b2 : {},
9716
9656
  namespace: ns
9717
9657
  };
@@ -9730,7 +9670,7 @@ ${context}`;
9730
9670
  const telemetryUrl = ((_B = this.config.telemetry) == null ? void 0 : _B.url) || process.env.TELEMETRY_URL || process.env.NEXT_PUBLIC_TELEMETRY_URL || defaultUrl;
9731
9671
  const absoluteUrl = telemetryUrl.startsWith("http") ? telemetryUrl : "https://www.retrivora.com" + (telemetryUrl.startsWith("/") ? telemetryUrl : "/" + telemetryUrl);
9732
9672
  (async () => {
9733
- var _a4, _b2, _c2, _d2, _e2;
9673
+ var _a3, _b2, _c2, _d2, _e2;
9734
9674
  try {
9735
9675
  let finalTrace = trace;
9736
9676
  if (!awaitHallucination && runHallucination) {
@@ -9739,7 +9679,7 @@ ${context}`;
9739
9679
  finalTrace = buildTrace(backgroundScoreResult);
9740
9680
  }
9741
9681
  }
9742
- const modelName = (finalTrace == null ? void 0 : finalTrace.model) || ((_a4 = this.config.llm) == null ? void 0 : _a4.model) || "llama-3.1-8b-instant";
9682
+ const modelName = (finalTrace == null ? void 0 : finalTrace.model) || ((_a3 = this.config.llm) == null ? void 0 : _a3.model) || "llama-3.1-8b-instant";
9743
9683
  const providerName = (finalTrace == null ? void 0 : finalTrace.provider) || ((_b2 = this.config.llm) == null ? void 0 : _b2.provider) || "groq";
9744
9684
  const tokenCount = Number(((_c2 = finalTrace == null ? void 0 : finalTrace.tokens) == null ? void 0 : _c2.totalTokens) || 0);
9745
9685
  const costEst = Number(((_d2 = finalTrace == null ? void 0 : finalTrace.tokens) == null ? void 0 : _d2.estimatedCostUsd) || 0);
@@ -9793,7 +9733,7 @@ ${context}`;
9793
9733
  * Uses an LRU-bounded embedding cache to avoid re-embedding the same query.
9794
9734
  */
9795
9735
  async generateUiTransformation(question, sources, cachedSchema, forceDeterministic = false) {
9796
- var _a3;
9736
+ var _a2;
9797
9737
  if (!sources || sources.length === 0) {
9798
9738
  return UITransformer.transform(question, sources, this.config, cachedSchema);
9799
9739
  }
@@ -9806,7 +9746,7 @@ ${context}`;
9806
9746
  { visualizationHint: "product_browse", recommendedChart: "text", filterInStockOnly: false, wantsExplicitTable: false, isTemporal: false, isComparison: false, language: "en" }
9807
9747
  );
9808
9748
  }
9809
- const enableLlmUiTransform = ((_a3 = this.config.llm.options) == null ? void 0 : _a3.enableLlmUiTransform) === true;
9749
+ const enableLlmUiTransform = ((_a2 = this.config.llm.options) == null ? void 0 : _a2.enableLlmUiTransform) === true;
9810
9750
  if (forceDeterministic || !enableLlmUiTransform) {
9811
9751
  return UITransformer.transform(question, sources, this.config, cachedSchema);
9812
9752
  }
@@ -9824,15 +9764,15 @@ ${context}`;
9824
9764
  const value = this.resolveNumericPredicateValue(source, predicate);
9825
9765
  return value !== null && this.matchesNumericPredicate(value, predicate);
9826
9766
  })).sort((a, b) => {
9827
- var _a3, _b;
9767
+ var _a2, _b;
9828
9768
  const primary = predicates[0];
9829
- const aValue = (_a3 = this.resolveNumericPredicateValue(a, primary)) != null ? _a3 : 0;
9769
+ const aValue = (_a2 = this.resolveNumericPredicateValue(a, primary)) != null ? _a2 : 0;
9830
9770
  const bValue = (_b = this.resolveNumericPredicateValue(b, primary)) != null ? _b : 0;
9831
9771
  return primary.operator === "lt" || primary.operator === "lte" ? aValue - bValue : bValue - aValue;
9832
9772
  });
9833
9773
  }
9834
9774
  resolveNumericPredicateValue(source, predicate) {
9835
- var _a3;
9775
+ var _a2;
9836
9776
  if (!source) return null;
9837
9777
  const meta = source.metadata || {};
9838
9778
  const field = predicate.field;
@@ -9845,7 +9785,7 @@ ${context}`;
9845
9785
  const value = this.toFiniteNumber(Array.isArray(fuzzy) ? fuzzy[1] : fuzzy.value);
9846
9786
  if (value !== null) return value;
9847
9787
  }
9848
- const contentValue = this.extractNumericValueFromContent((_a3 = source.content) != null ? _a3 : "", field);
9788
+ const contentValue = this.extractNumericValueFromContent((_a2 = source.content) != null ? _a2 : "", field);
9849
9789
  if (contentValue !== null) return contentValue;
9850
9790
  }
9851
9791
  for (const [key, value] of entries) {
@@ -9901,8 +9841,8 @@ ${context}`;
9901
9841
  return Number.isFinite(numeric) ? numeric : null;
9902
9842
  }
9903
9843
  async retrieve(query, options) {
9904
- var _a3, _b, _c, _d, _e, _f, _g2;
9905
- const ns = formatNamespace((_a3 = options.namespace) != null ? _a3 : this.config.projectId);
9844
+ var _a2, _b, _c, _d, _e, _f, _g2;
9845
+ const ns = formatNamespace((_a2 = options.namespace) != null ? _a2 : this.config.projectId);
9906
9846
  const topK = (_b = options.topK) != null ? _b : 5;
9907
9847
  const cacheKey = `${ns}::${query}`;
9908
9848
  let queryVector = this.embeddingCache.get(cacheKey);
@@ -9947,11 +9887,11 @@ ${context}`;
9947
9887
  namespace: ns,
9948
9888
  count: resolvedSources.length,
9949
9889
  sample: resolvedSources.slice(0, 2).map((s) => {
9950
- var _a4;
9890
+ var _a3;
9951
9891
  return {
9952
9892
  id: s == null ? void 0 : s.id,
9953
9893
  score: s == null ? void 0 : s.score,
9954
- contentSnippet: String((_a4 = s == null ? void 0 : s.content) != null ? _a4 : "").substring(0, 150),
9894
+ contentSnippet: String((_a3 = s == null ? void 0 : s.content) != null ? _a3 : "").substring(0, 150),
9955
9895
  metadata: s == null ? void 0 : s.metadata
9956
9896
  };
9957
9897
  })
@@ -9965,8 +9905,8 @@ Focus on extracting the core intent and entities. Do not answer the question, ju
9965
9905
 
9966
9906
  History:
9967
9907
  ${(history || []).map((m) => {
9968
- var _a3;
9969
- return `${(m == null ? void 0 : m.role) || "user"}: ${(_a3 = m == null ? void 0 : m.content) != null ? _a3 : ""}`;
9908
+ var _a2;
9909
+ return `${(m == null ? void 0 : m.role) || "user"}: ${(_a2 = m == null ? void 0 : m.content) != null ? _a2 : ""}`;
9970
9910
  }).join("\n")}
9971
9911
 
9972
9912
  New Question: ${question}
@@ -9993,8 +9933,8 @@ Optimized Search Query:`;
9993
9933
  const { sources } = await this.retrieve(query, { namespace: ns, topK: 5 });
9994
9934
  if (!sources || sources.length === 0) return [];
9995
9935
  const context = sources.map((s) => {
9996
- var _a3;
9997
- return (_a3 = s == null ? void 0 : s.content) != null ? _a3 : "";
9936
+ var _a2;
9937
+ return (_a2 = s == null ? void 0 : s.content) != null ? _a2 : "";
9998
9938
  }).join("\n\n---\n\n");
9999
9939
  const prompt = `Based on the following snippets from a document, what are at least 5 short, relevant questions a user might ask?
10000
9940
  Focus on questions that can be answered by the context.
@@ -10036,13 +9976,13 @@ var Retrivora = class {
10036
9976
  this.pipeline = new Pipeline(this.config);
10037
9977
  }
10038
9978
  async initialize() {
10039
- var _a3;
9979
+ var _a2;
10040
9980
  try {
10041
9981
  await ConfigValidator.validateAndThrow(this.config);
10042
9982
  await LicenseVerifier.verifyAsync(
10043
9983
  this.config.licenseKey,
10044
9984
  this.config.projectId,
10045
- (_a3 = this.config.vectorDb) == null ? void 0 : _a3.provider
9985
+ (_a2 = this.config.vectorDb) == null ? void 0 : _a2.provider
10046
9986
  );
10047
9987
  await LicenseVerifier.verifyIP(this.config.licenseKey);
10048
9988
  } catch (err) {
@@ -10216,186 +10156,8 @@ var ProviderHealthCheck = class {
10216
10156
  };
10217
10157
 
10218
10158
  // src/core/FreeTierLimitsGuard.ts
10219
- var FREE_TIER_QUOTAS = {
10220
- MAX_DOCUMENTS: 25,
10221
- MAX_FILE_SIZE_BYTES: 20 * 1024 * 1024,
10222
- MAX_STORAGE_BYTES: 250 * 1024 * 1024,
10223
- MAX_TRIAL_REQUEST_UNITS: 500,
10224
- MAX_DAILY_REQUEST_UNITS: 50,
10225
- TRIAL_DURATION_DAYS: 14,
10226
- GRACE_PERIOD_DAYS: 2,
10227
- MAX_RPM: 10,
10228
- MAX_RPH: 100,
10229
- MAX_RPD: 500,
10230
- MAX_CONCURRENT_REQUESTS: 2,
10231
- MAX_INPUT_TOKENS: 2e4,
10232
- MAX_OUTPUT_TOKENS: 4e3,
10233
- UPGRADE_REMINDER_DAYS: [7, 12, 14],
10234
- MAX_USERS: 1,
10235
- MAX_NAMESPACES: 1,
10236
- MAX_PROJECTS: 1
10237
- };
10238
- var FreeTierLimitsGuard = class {
10239
- static calculateTrialTimestamps(startedAt) {
10240
- const startDate = startedAt ? new Date(startedAt) : /* @__PURE__ */ new Date();
10241
- const trialStartedAtMs = startDate.getTime();
10242
- const trialExpiresAtMs = trialStartedAtMs + FREE_TIER_QUOTAS.TRIAL_DURATION_DAYS * 24 * 60 * 60 * 1e3;
10243
- const gracePeriodExpiresAtMs = trialStartedAtMs + (FREE_TIER_QUOTAS.TRIAL_DURATION_DAYS + FREE_TIER_QUOTAS.GRACE_PERIOD_DAYS) * 24 * 60 * 60 * 1e3;
10244
- return {
10245
- trial_started_at: new Date(trialStartedAtMs).toISOString(),
10246
- trial_expires_at: new Date(trialExpiresAtMs).toISOString(),
10247
- grace_period_expires_at: new Date(gracePeriodExpiresAtMs).toISOString()
10248
- };
10249
- }
10250
- static calculateRequestUnits(operationType, options) {
10251
- var _a3;
10252
- const op = operationType.toLowerCase().trim();
10253
- 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")) {
10254
- return 0;
10255
- }
10256
- if (op.includes("image")) {
10257
- return 10;
10258
- }
10259
- if (op.includes("embedding")) {
10260
- const chunks = (_a3 = options == null ? void 0 : options.chunkCount) != null ? _a3 : 1;
10261
- return Math.max(1, Math.ceil(chunks / 1e3));
10262
- }
10263
- return 1;
10264
- }
10265
- static checkTrialStatus(startedAt, totalUnitsUsed = 0, nowServerTime = /* @__PURE__ */ new Date()) {
10266
- const timestamps = this.calculateTrialTimestamps(startedAt);
10267
- const startMs = new Date(timestamps.trial_started_at).getTime();
10268
- const expireMs = new Date(timestamps.trial_expires_at).getTime();
10269
- const graceExpireMs = new Date(timestamps.grace_period_expires_at).getTime();
10270
- const nowMs = nowServerTime.getTime();
10271
- const daysElapsed = Math.floor(Math.max(0, nowMs - startMs) / (1e3 * 60 * 60 * 24));
10272
- const daysRemaining = Math.max(0, Math.ceil((expireMs - nowMs) / (1e3 * 60 * 60 * 24)));
10273
- const isConsumedUnits = totalUnitsUsed >= FREE_TIER_QUOTAS.MAX_TRIAL_REQUEST_UNITS;
10274
- const isExpiredTime = nowMs > expireMs;
10275
- const isPastGraceTime = nowMs > graceExpireMs;
10276
- let status = "active";
10277
- let reason;
10278
- if (isPastGraceTime || isConsumedUnits && isExpiredTime) {
10279
- status = "expired";
10280
- reason = "Trial period and grace period have expired. Upgrade your plan.";
10281
- } else if (isExpiredTime || isConsumedUnits) {
10282
- status = "grace_period";
10283
- 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).";
10284
- }
10285
- let upgradeReminderDue;
10286
- if (FREE_TIER_QUOTAS.UPGRADE_REMINDER_DAYS.includes(daysElapsed)) {
10287
- upgradeReminderDue = daysElapsed;
10288
- }
10289
- let usageReminderDue;
10290
- const usagePercent = totalUnitsUsed / FREE_TIER_QUOTAS.MAX_TRIAL_REQUEST_UNITS * 100;
10291
- if (usagePercent >= 100) {
10292
- usageReminderDue = "100%";
10293
- } else if (usagePercent >= 90) {
10294
- usageReminderDue = "90%";
10295
- } else if (usagePercent >= 80) {
10296
- usageReminderDue = "80%";
10297
- }
10298
- return {
10299
- status,
10300
- daysElapsed,
10301
- daysRemaining,
10302
- isGracePeriod: status === "grace_period",
10303
- isExpired: status === "expired",
10304
- upgradeReminderDue,
10305
- usageReminderDue,
10306
- reason
10307
- };
10308
- }
10309
- static checkIngestionAllowed(stats, incomingSizeBytes = 0) {
10310
- var _a3, _b;
10311
- const docs = (_a3 = stats.documentCount) != null ? _a3 : 0;
10312
- const storage = (_b = stats.totalStorageBytes) != null ? _b : 0;
10313
- if (incomingSizeBytes > FREE_TIER_QUOTAS.MAX_FILE_SIZE_BYTES) {
10314
- const mbSize = (incomingSizeBytes / (1024 * 1024)).toFixed(1);
10315
- return {
10316
- allowed: false,
10317
- reason: `[FreeTierIngestor] File size (${mbSize}MB) exceeds maximum allowed size of 20MB under Free Tier rules.`
10318
- };
10319
- }
10320
- if (docs >= FREE_TIER_QUOTAS.MAX_DOCUMENTS) {
10321
- return {
10322
- allowed: false,
10323
- reason: `Document limit reached (${docs}/${FREE_TIER_QUOTAS.MAX_DOCUMENTS}). Upgrade to Pro to ingest more documents.`
10324
- };
10325
- }
10326
- if (storage + incomingSizeBytes > FREE_TIER_QUOTAS.MAX_STORAGE_BYTES) {
10327
- const mbUsed = (storage / (1024 * 1024)).toFixed(1);
10328
- return {
10329
- allowed: false,
10330
- reason: `Storage quota exceeded (${mbUsed}MB / 250MB). Upgrade for unlimited storage.`
10331
- };
10332
- }
10333
- return { allowed: true };
10334
- }
10335
- static checkRequestAllowed(params) {
10336
- var _a3, _b, _c;
10337
- const op = (_a3 = params.operationType) != null ? _a3 : "chat";
10338
- const costUnits = this.calculateRequestUnits(op, { chunkCount: params.chunkCount });
10339
- if (costUnits === 0) {
10340
- return { allowed: true, costUnits: 0 };
10341
- }
10342
- if (params.inputTokens && params.inputTokens > FREE_TIER_QUOTAS.MAX_INPUT_TOKENS) {
10343
- return {
10344
- allowed: false,
10345
- reason: "Token limit exceeded. Upgrade your plan.",
10346
- costUnits
10347
- };
10348
- }
10349
- if (params.outputTokens && params.outputTokens > FREE_TIER_QUOTAS.MAX_OUTPUT_TOKENS) {
10350
- return {
10351
- allowed: false,
10352
- reason: "Token limit exceeded. Upgrade your plan.",
10353
- costUnits
10354
- };
10355
- }
10356
- if (params.concurrentRequests && params.concurrentRequests > FREE_TIER_QUOTAS.MAX_CONCURRENT_REQUESTS) {
10357
- return {
10358
- allowed: false,
10359
- reason: `Concurrent limit exceeded (max ${FREE_TIER_QUOTAS.MAX_CONCURRENT_REQUESTS} active requests). Please wait for active streams to finish.`,
10360
- costUnits
10361
- };
10362
- }
10363
- const totalUnits = (_b = params.totalUnitsUsed) != null ? _b : 0;
10364
- const trialStatus = this.checkTrialStatus(params.trialStartedAt, totalUnits, params.nowServerTime);
10365
- if (trialStatus.status === "expired" || trialStatus.status === "grace_period") {
10366
- return {
10367
- allowed: false,
10368
- reason: trialStatus.reason || "Trial limit reached. Upgrade your plan.",
10369
- costUnits
10370
- };
10371
- }
10372
- if (totalUnits + costUnits > FREE_TIER_QUOTAS.MAX_TRIAL_REQUEST_UNITS) {
10373
- return {
10374
- allowed: false,
10375
- reason: `Total trial request unit limit reached (${totalUnits}/${FREE_TIER_QUOTAS.MAX_TRIAL_REQUEST_UNITS}). Upgrade your plan.`,
10376
- costUnits
10377
- };
10378
- }
10379
- const dailyUnits = (_c = params.dailyUnitsUsed) != null ? _c : 0;
10380
- if (dailyUnits + costUnits > FREE_TIER_QUOTAS.MAX_DAILY_REQUEST_UNITS) {
10381
- return {
10382
- allowed: false,
10383
- reason: `Daily request quota limit reached (${dailyUnits}/${FREE_TIER_QUOTAS.MAX_DAILY_REQUEST_UNITS} units/day). Quota resets tomorrow.`,
10384
- costUnits
10385
- };
10386
- }
10387
- return { allowed: true, costUnits };
10388
- }
10389
- static checkQueryAllowed(dailyQueriesCount = 0) {
10390
- if (dailyQueriesCount >= FREE_TIER_QUOTAS.MAX_DAILY_REQUEST_UNITS) {
10391
- return {
10392
- allowed: false,
10393
- reason: `Daily query limit reached (${FREE_TIER_QUOTAS.MAX_DAILY_REQUEST_UNITS}/day). Quota resets tomorrow.`
10394
- };
10395
- }
10396
- return { allowed: true };
10397
- }
10398
- };
10159
+ var FreeTierLimitsGuard_exports = {};
10160
+ __reExport(FreeTierLimitsGuard_exports, require("@retrivora-ai/billing-guards"));
10399
10161
 
10400
10162
  // src/core/VectorPlugin.ts
10401
10163
  var VectorPlugin = class {
@@ -10403,12 +10165,12 @@ var VectorPlugin = class {
10403
10165
  const resolvedConfig = ConfigResolver.resolve(hostConfig);
10404
10166
  this.config = resolvedConfig;
10405
10167
  this.validationPromise = (async () => {
10406
- var _a3;
10168
+ var _a2;
10407
10169
  await ConfigValidator.validateAndThrow(resolvedConfig);
10408
10170
  await LicenseVerifier.verifyAsync(
10409
10171
  resolvedConfig.licenseKey,
10410
10172
  resolvedConfig.projectId,
10411
- (_a3 = resolvedConfig.vectorDb) == null ? void 0 : _a3.provider
10173
+ (_a2 = resolvedConfig.vectorDb) == null ? void 0 : _a2.provider
10412
10174
  );
10413
10175
  })();
10414
10176
  this.validationPromise.catch(() => {
@@ -10444,8 +10206,8 @@ var VectorPlugin = class {
10444
10206
  }
10445
10207
  estimateInputTokens(message, history = []) {
10446
10208
  const allContent = message.length + history.reduce((acc, m) => {
10447
- var _a3, _b;
10448
- 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);
10209
+ var _a2, _b;
10210
+ 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);
10449
10211
  }, 0);
10450
10212
  return Math.ceil(allContent / 4);
10451
10213
  }
@@ -10453,7 +10215,7 @@ var VectorPlugin = class {
10453
10215
  * Run a chat query.
10454
10216
  */
10455
10217
  async chat(message, history = [], namespace) {
10456
- var _a3;
10218
+ var _a2;
10457
10219
  try {
10458
10220
  await this.validationPromise;
10459
10221
  } catch (err) {
@@ -10461,11 +10223,11 @@ var VectorPlugin = class {
10461
10223
  }
10462
10224
  try {
10463
10225
  const inputTokens = this.estimateInputTokens(message, history);
10464
- const tokenCheck = FreeTierLimitsGuard.checkRequestAllowed({
10226
+ const tokenCheck = FreeTierLimitsGuard_exports.FreeTierLimitsGuard.checkRequestAllowed({
10465
10227
  operationType: "chat",
10466
10228
  inputTokens
10467
10229
  });
10468
- if (!tokenCheck.allowed && ((_a3 = tokenCheck.reason) == null ? void 0 : _a3.toLowerCase().includes("token"))) {
10230
+ if (!tokenCheck.allowed && ((_a2 = tokenCheck.reason) == null ? void 0 : _a2.toLowerCase().includes("token"))) {
10469
10231
  throw wrapError(new Error(tokenCheck.reason), "RATE_LIMITED");
10470
10232
  }
10471
10233
  return await this.pipeline.ask(message, history, namespace);
@@ -10486,7 +10248,7 @@ var VectorPlugin = class {
10486
10248
  */
10487
10249
  chatStream(_0) {
10488
10250
  return __asyncGenerator(this, arguments, function* (message, history = [], namespace) {
10489
- var _a3;
10251
+ var _a2;
10490
10252
  try {
10491
10253
  yield new __await(this.validationPromise);
10492
10254
  } catch (err) {
@@ -10494,11 +10256,11 @@ var VectorPlugin = class {
10494
10256
  }
10495
10257
  try {
10496
10258
  const inputTokens = this.estimateInputTokens(message, history);
10497
- const tokenCheck = FreeTierLimitsGuard.checkRequestAllowed({
10259
+ const tokenCheck = FreeTierLimitsGuard_exports.FreeTierLimitsGuard.checkRequestAllowed({
10498
10260
  operationType: "chat_stream",
10499
10261
  inputTokens
10500
10262
  });
10501
- if (!tokenCheck.allowed && ((_a3 = tokenCheck.reason) == null ? void 0 : _a3.toLowerCase().includes("token"))) {
10263
+ if (!tokenCheck.allowed && ((_a2 = tokenCheck.reason) == null ? void 0 : _a2.toLowerCase().includes("token"))) {
10502
10264
  throw wrapError(new Error(tokenCheck.reason), "RATE_LIMITED");
10503
10265
  }
10504
10266
  const stream = this.pipeline.askStream(message, history, namespace);
@@ -10585,13 +10347,13 @@ var ConfigBuilder = class {
10585
10347
  * Configure the vector database provider
10586
10348
  */
10587
10349
  vectorDb(provider, options) {
10588
- var _a3;
10350
+ var _a2;
10589
10351
  if (provider === "auto") {
10590
10352
  this._vectorDb = this._autoDetectVectorDb();
10591
10353
  } else {
10592
10354
  this._vectorDb = {
10593
10355
  provider,
10594
- indexName: (_a3 = options == null ? void 0 : options.indexName) != null ? _a3 : "default",
10356
+ indexName: (_a2 = options == null ? void 0 : options.indexName) != null ? _a2 : "default",
10595
10357
  options: __spreadValues({}, options)
10596
10358
  };
10597
10359
  }
@@ -10601,7 +10363,7 @@ var ConfigBuilder = class {
10601
10363
  * Configure the LLM provider for chat
10602
10364
  */
10603
10365
  llm(provider, model, apiKey, options) {
10604
- var _a3, _b;
10366
+ var _a2, _b;
10605
10367
  if (provider === "auto") {
10606
10368
  this._llm = this._autoDetectLLM();
10607
10369
  } else {
@@ -10610,7 +10372,7 @@ var ConfigBuilder = class {
10610
10372
  model: model != null ? model : "default-model",
10611
10373
  apiKey,
10612
10374
  systemPrompt: options == null ? void 0 : options.systemPrompt,
10613
- maxTokens: (_a3 = options == null ? void 0 : options.maxTokens) != null ? _a3 : 1024,
10375
+ maxTokens: (_a2 = options == null ? void 0 : options.maxTokens) != null ? _a2 : 1024,
10614
10376
  temperature: (_b = options == null ? void 0 : options.temperature) != null ? _b : 0.7,
10615
10377
  baseUrl: options == null ? void 0 : options.baseUrl,
10616
10378
  options
@@ -10653,8 +10415,8 @@ var ConfigBuilder = class {
10653
10415
  * Set RAG-specific pipeline parameters
10654
10416
  */
10655
10417
  rag(options) {
10656
- var _a3;
10657
- this._rag = __spreadValues(__spreadValues({}, (_a3 = this._rag) != null ? _a3 : {}), options);
10418
+ var _a2;
10419
+ this._rag = __spreadValues(__spreadValues({}, (_a2 = this._rag) != null ? _a2 : {}), options);
10658
10420
  return this;
10659
10421
  }
10660
10422
  /**
@@ -10670,7 +10432,7 @@ var ConfigBuilder = class {
10670
10432
  * Throws if required fields (projectId, vectorDb, llm, embedding) are not set.
10671
10433
  */
10672
10434
  build() {
10673
- var _a3;
10435
+ var _a2;
10674
10436
  const missing = [];
10675
10437
  if (!this._projectId) missing.push('projectId (call .projectId("my-app"))');
10676
10438
  if (!this._vectorDb) missing.push('vectorDb (call .vectorDb("pinecone", { ... }))');
@@ -10682,7 +10444,7 @@ var ConfigBuilder = class {
10682
10444
  ${missing.join("\n ")}`
10683
10445
  );
10684
10446
  }
10685
- const ragConfig = (_a3 = this._rag) != null ? _a3 : { chunkSize: 1e3, chunkOverlap: 200, topK: 5 };
10447
+ const ragConfig = (_a2 = this._rag) != null ? _a2 : { chunkSize: 1e3, chunkOverlap: 200, topK: 5 };
10686
10448
  if (process.env.RAG_USE_GRAPH_RETRIEVAL === "true") {
10687
10449
  ragConfig.useGraphRetrieval = true;
10688
10450
  }
@@ -10778,8 +10540,8 @@ var DocumentParser = class {
10778
10540
  * Extract text from a File or Buffer based on its type.
10779
10541
  */
10780
10542
  static async parse(file, fileName, mimeType) {
10781
- var _a3;
10782
- const extension = ((_a3 = fileName.split(".").pop()) == null ? void 0 : _a3.toLowerCase()) || "";
10543
+ var _a2;
10544
+ const extension = ((_a2 = fileName.split(".").pop()) == null ? void 0 : _a2.toLowerCase()) || "";
10783
10545
  if (extension === "txt" || extension === "md" || mimeType === "text/plain" || mimeType === "text/markdown") {
10784
10546
  return this.readAsText(file);
10785
10547
  }
@@ -10874,7 +10636,17 @@ var import_server = require("next/server");
10874
10636
  var fs = __toESM(require("fs"));
10875
10637
  var path = __toESM(require("path"));
10876
10638
  var os = __toESM(require("os"));
10877
- var DatabaseStorage = class {
10639
+ function validateTableName(name) {
10640
+ if (!/^[a-zA-Z_][a-zA-Z0-9_]{0,62}$/.test(name)) {
10641
+ throw new Error(
10642
+ `[DatabaseStorage] Invalid table name "${name}". Table names must start with a letter or underscore and contain only alphanumeric characters and underscores (max 63 chars).`
10643
+ );
10644
+ }
10645
+ return name;
10646
+ }
10647
+ var _pgInitPromises = /* @__PURE__ */ new Map();
10648
+ var _DatabaseStorage = class _DatabaseStorage {
10649
+ // 5 minutes
10878
10650
  constructor(config) {
10879
10651
  // Dynamic PG Pool
10880
10652
  this.pgPool = null;
@@ -10886,36 +10658,50 @@ var DatabaseStorage = class {
10886
10658
  this.fallbackDir = this.isServerless ? path.join(os.tmpdir(), ".retrivora") : path.join(process.cwd(), ".retrivora");
10887
10659
  this.historyFile = path.join(this.fallbackDir, "history.json");
10888
10660
  this.feedbackFile = path.join(this.fallbackDir, "feedback.json");
10889
- var _a3, _b, _c, _d, _e;
10661
+ /** Subscription check result cache (Fix PERF-1). */
10662
+ this.subscriptionCheckedAt = 0;
10663
+ var _a2, _b, _c, _d, _e;
10890
10664
  this.config = config;
10891
- this.provider = ((_a3 = config.vectorDb) == null ? void 0 : _a3.provider) || "fs";
10665
+ this.provider = ((_a2 = config.vectorDb) == null ? void 0 : _a2.provider) || "fs";
10892
10666
  const rawHistoryTable = ((_c = (_b = config.rag) == null ? void 0 : _b.history) == null ? void 0 : _c.tableName) || "retrivora_history";
10893
10667
  const rawFeedbackTable = ((_e = (_d = config.rag) == null ? void 0 : _d.feedback) == null ? void 0 : _e.tableName) || "retrivora_feedback";
10894
- this.historyTableName = rawHistoryTable.replace(/[^a-zA-Z0-9_]/g, "_");
10895
- this.feedbackTableName = rawFeedbackTable.replace(/[^a-zA-Z0-9_]/g, "_");
10668
+ this.historyTableName = validateTableName(rawHistoryTable);
10669
+ this.feedbackTableName = validateTableName(rawFeedbackTable);
10896
10670
  }
10897
10671
  /**
10898
- * Asserts the user is running a valid Premium/Enterprise license in production.
10899
- * In non-production (development / test) environments this is a complete no-op —
10900
- * all History and Feedback features are freely accessible for local development.
10672
+ * Checks that the caller holds a valid paid-tier license in production.
10673
+ *
10674
+ * Fix PERF-1: result is cached for 5 minutes so we don't call
10675
+ * LicenseVerifier.verifyAsync() on every DB operation (saveMessage,
10676
+ * getHistory, etc.).
10677
+ *
10678
+ * Fix PERF-2: renamed from `checkPremiumSubscription` to clarify intent.
10679
+ * The previous allowlist admitted all free tiers, defeating the gate.
10680
+ * The new allowlist only passes paid tiers. Free-tier access to history
10681
+ * and feedback is a product decision — update the constant below if the
10682
+ * policy changes.
10683
+ *
10684
+ * In non-production environments this is a complete no-op so local
10685
+ * development works without a license key.
10901
10686
  */
10902
- async checkPremiumSubscription() {
10903
- if (process.env.NODE_ENV !== "production") {
10904
- return;
10905
- }
10687
+ async checkPaidSubscription() {
10688
+ if (process.env.NODE_ENV !== "production") return;
10689
+ if (Date.now() - this.subscriptionCheckedAt < _DatabaseStorage.SUBSCRIPTION_CACHE_TTL_MS) return;
10906
10690
  if (!this.config.licenseKey) {
10907
10691
  throw new Error(
10908
- "[Retrivora SDK] Chat History and Feedback features require a Premium or Enterprise license key in production."
10692
+ "[Retrivora SDK] Chat History and Feedback features require a paid license key in production."
10909
10693
  );
10910
10694
  }
10911
10695
  try {
10912
10696
  const payload = await LicenseVerifier.verifyAsync(this.config.licenseKey, this.config.projectId);
10913
10697
  const tier = (payload.tier || "").toLowerCase();
10914
- if (tier !== "premium" && tier !== "enterprise" && tier !== "growth" && tier !== "pro" && tier !== "developer_pro" && tier !== "free_trial" && tier !== "free_tier" && tier !== "free" && tier !== "hobby") {
10698
+ const PAID_TIERS = /* @__PURE__ */ new Set(["premium", "enterprise", "growth", "pro", "developer_pro"]);
10699
+ if (!PAID_TIERS.has(tier)) {
10915
10700
  throw new Error(
10916
10701
  `[Retrivora SDK] Chat History and Feedback features are not available on the "${tier}" tier. Please upgrade to a Developer Pro or Enterprise plan.`
10917
10702
  );
10918
10703
  }
10704
+ this.subscriptionCheckedAt = Date.now();
10919
10705
  } catch (err) {
10920
10706
  throw new Error(
10921
10707
  `[Retrivora SDK] Subscription check failed: ${err instanceof Error ? err.message : String(err)}`
@@ -10923,34 +10709,45 @@ var DatabaseStorage = class {
10923
10709
  }
10924
10710
  }
10925
10711
  async checkHistoryEnabled() {
10926
- var _a3, _b;
10927
- await this.checkPremiumSubscription();
10928
- if (((_b = (_a3 = this.config.rag) == null ? void 0 : _a3.history) == null ? void 0 : _b.enabled) === false) {
10712
+ var _a2, _b;
10713
+ await this.checkPaidSubscription();
10714
+ if (((_b = (_a2 = this.config.rag) == null ? void 0 : _a2.history) == null ? void 0 : _b.enabled) === false) {
10929
10715
  throw new Error("[Retrivora SDK] Chat History is disabled in RagConfig.");
10930
10716
  }
10931
10717
  }
10932
10718
  async checkFeedbackEnabled() {
10933
- var _a3, _b;
10934
- await this.checkPremiumSubscription();
10935
- if (((_b = (_a3 = this.config.rag) == null ? void 0 : _a3.feedback) == null ? void 0 : _b.enabled) === false) {
10719
+ var _a2, _b;
10720
+ await this.checkPaidSubscription();
10721
+ if (((_b = (_a2 = this.config.rag) == null ? void 0 : _a2.feedback) == null ? void 0 : _b.enabled) === false) {
10936
10722
  throw new Error("[Retrivora SDK] User Feedback is disabled in RagConfig.");
10937
10723
  }
10938
10724
  }
10939
- async getPGPool() {
10940
- const globalKey = `__retrivora_pg_pool_${this.config.projectId}`;
10941
- const _g2 = global;
10942
- if (_g2[globalKey]) {
10943
- this.pgPool = _g2[globalKey];
10944
- return this.pgPool;
10725
+ /**
10726
+ * Returns (or lazily creates) the PostgreSQL connection pool for this project.
10727
+ *
10728
+ * Fix ERR-2: the previous implementation had a double-initialization race
10729
+ * condition — two concurrent requests would both find _g[globalKey] missing,
10730
+ * both create a Pool, and the second Pool would be orphaned, leaking
10731
+ * database connections. This version stores a single Promise in the module-
10732
+ * level _pgInitPromises map so all concurrent callers await the same
10733
+ * initialization without creating duplicate pools.
10734
+ */
10735
+ getPGPool() {
10736
+ const globalKey = `pg:${this.config.projectId}`;
10737
+ if (!_pgInitPromises.has(globalKey)) {
10738
+ _pgInitPromises.set(globalKey, this.createPGPool());
10945
10739
  }
10740
+ return _pgInitPromises.get(globalKey);
10741
+ }
10742
+ async createPGPool() {
10946
10743
  const opts = this.config.vectorDb.options;
10947
10744
  if (!opts.connectionString) {
10948
10745
  throw new Error("[DatabaseStorage] pg connectionString option is missing.");
10949
10746
  }
10950
10747
  const { Pool: Pool3 } = await import("pg");
10951
- this.pgPool = new Pool3({ connectionString: opts.connectionString });
10952
- _g2[globalKey] = this.pgPool;
10953
- const client = await this.pgPool.connect();
10748
+ const pool = new Pool3({ connectionString: opts.connectionString });
10749
+ this.pgPool = pool;
10750
+ const client = await pool.connect();
10954
10751
  try {
10955
10752
  await client.query(`
10956
10753
  CREATE TABLE IF NOT EXISTS ${this.historyTableName} (
@@ -10977,7 +10774,7 @@ var DatabaseStorage = class {
10977
10774
  } finally {
10978
10775
  client.release();
10979
10776
  }
10980
- return this.pgPool;
10777
+ return pool;
10981
10778
  }
10982
10779
  async getMongoClient() {
10983
10780
  const globalKey = `__retrivora_mongo_client_${this.config.projectId}`;
@@ -11157,17 +10954,21 @@ var DatabaseStorage = class {
11157
10954
  }
11158
10955
  }
11159
10956
  /**
11160
- * Return true when `sessionId` falls into the allowed `projectIds` scope.
11161
- * When `projectIds` is empty / undefined we allow the call (legacy behavior
11162
- * for non-multi-tenant consumers). Session ids typically begin with the
11163
- * project id they were created for, so we prefix-match as well as contains-match
11164
- * to catch sharded / suffix-style ids used by some integrations.
10957
+ * Returns true when `sessionId` is in scope for the given `projectIds`.
10958
+ *
10959
+ * Fix RAG-5: the previous implementation used `sessionId.includes(pid)` which
10960
+ * allows cross-tenant data leakage. If a project ID "abc" is a substring of
10961
+ * another tenant's session ID (e.g., "xyz-abc-user-123"), the check would
10962
+ * incorrectly return true. We now only allow exact match or strict delimiter-
10963
+ * separated prefix match to prevent this attack.
11165
10964
  */
11166
10965
  sessionInScope(sessionId, projectIds) {
11167
10966
  if (!projectIds || projectIds.length === 0) return true;
11168
- return projectIds.some(
11169
- (pid) => pid && (sessionId === pid || sessionId.startsWith(pid) || sessionId.includes(pid))
11170
- );
10967
+ return projectIds.some((pid) => {
10968
+ if (!pid) return false;
10969
+ if (sessionId === pid) return true;
10970
+ return sessionId.startsWith(`${pid}-`) || sessionId.startsWith(`${pid}_`) || sessionId.startsWith(`${pid}/`);
10971
+ });
11171
10972
  }
11172
10973
  async listSessions(projectIds) {
11173
10974
  await this.checkHistoryEnabled();
@@ -11316,9 +11117,12 @@ var DatabaseStorage = class {
11316
11117
  }
11317
11118
  }
11318
11119
  };
11120
+ _DatabaseStorage.SUBSCRIPTION_CACHE_TTL_MS = 5 * 60 * 1e3;
11121
+ var DatabaseStorage = _DatabaseStorage;
11319
11122
 
11320
11123
  // src/handlers/index.ts
11321
11124
  init_version();
11125
+ var import_crypto2 = require("crypto");
11322
11126
  async function checkAuth(req, onAuthorize) {
11323
11127
  if (!onAuthorize) return null;
11324
11128
  try {
@@ -11340,6 +11144,17 @@ var RateLimiter = class {
11340
11144
  this.hits = /* @__PURE__ */ new Map();
11341
11145
  this.windowMs = windowMs;
11342
11146
  this.max = max;
11147
+ this.pruneTimer = setInterval(() => this.prune(), windowMs);
11148
+ if (typeof this.pruneTimer === "object" && "unref" in this.pruneTimer) {
11149
+ this.pruneTimer.unref();
11150
+ }
11151
+ }
11152
+ /** Prune entries whose timestamps are all outside the window. */
11153
+ prune() {
11154
+ const cutoff = Date.now() - this.windowMs;
11155
+ for (const [k, timestamps] of this.hits.entries()) {
11156
+ if (timestamps.every((t) => t <= cutoff)) this.hits.delete(k);
11157
+ }
11343
11158
  }
11344
11159
  /** Returns true if the request should be allowed, false if rate-limited. */
11345
11160
  allow(key) {
@@ -11348,11 +11163,6 @@ var RateLimiter = class {
11348
11163
  const existing = (this.hits.get(key) || []).filter((t) => t > cutoff);
11349
11164
  existing.push(now);
11350
11165
  this.hits.set(key, existing);
11351
- if (this.hits.size > 5e3) {
11352
- for (const [k, timestamps] of this.hits.entries()) {
11353
- if (timestamps.every((t) => t <= cutoff)) this.hits.delete(k);
11354
- }
11355
- }
11356
11166
  return existing.length <= this.max;
11357
11167
  }
11358
11168
  retryAfterSec(key) {
@@ -11367,8 +11177,8 @@ var _g = global;
11367
11177
  var _a;
11368
11178
  var rateLimiter = (_a = _g.__retrivoraRateLimiter) != null ? _a : _g.__retrivoraRateLimiter = new RateLimiter(6e4, 30);
11369
11179
  function getRateLimitKey(req) {
11370
- var _a3, _b;
11371
- 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";
11180
+ var _a2, _b;
11181
+ 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";
11372
11182
  return `ip:${ip}`;
11373
11183
  }
11374
11184
  function checkRateLimit(req) {
@@ -11395,8 +11205,6 @@ function isFreeTier(tier) {
11395
11205
  if (!tier) return true;
11396
11206
  return FREE_TIER_NAMES.has(tier.toLowerCase().trim());
11397
11207
  }
11398
- var _a2;
11399
- var _freeTierGuardInstance = (_a2 = _g.__retrivoraFreeTierGuard) != null ? _a2 : _g.__retrivoraFreeTierGuard = new FreeTierLimitsGuard();
11400
11208
  function createPaymentRequiredResponse(reason, details) {
11401
11209
  const body = __spreadProps(__spreadValues({
11402
11210
  type: "https://retrivora.com/docs/errors/payment-required",
@@ -11407,10 +11215,10 @@ function createPaymentRequiredResponse(reason, details) {
11407
11215
  instance: void 0
11408
11216
  }, details != null ? details : {}), {
11409
11217
  quota: {
11410
- maxDailyRequestUnits: FREE_TIER_QUOTAS.MAX_DAILY_REQUEST_UNITS,
11411
- maxTrialRequestUnits: FREE_TIER_QUOTAS.MAX_TRIAL_REQUEST_UNITS,
11412
- maxDocuments: FREE_TIER_QUOTAS.MAX_DOCUMENTS,
11413
- maxStorageBytes: FREE_TIER_QUOTAS.MAX_STORAGE_BYTES,
11218
+ maxDailyRequestUnits: FreeTierLimitsGuard_exports.FREE_TIER_QUOTAS.MAX_DAILY_REQUEST_UNITS,
11219
+ maxTrialRequestUnits: FreeTierLimitsGuard_exports.FREE_TIER_QUOTAS.MAX_TRIAL_REQUEST_UNITS,
11220
+ maxDocuments: FreeTierLimitsGuard_exports.FREE_TIER_QUOTAS.MAX_DOCUMENTS,
11221
+ maxStorageBytes: FreeTierLimitsGuard_exports.FREE_TIER_QUOTAS.MAX_STORAGE_BYTES,
11414
11222
  upgradeUrl: "https://retrivora.com/pricing"
11415
11223
  }
11416
11224
  });
@@ -11422,17 +11230,17 @@ function createPaymentRequiredResponse(reason, details) {
11422
11230
  });
11423
11231
  }
11424
11232
  function resolveLicenseKey(req, config, bodyLicenseKey) {
11425
- var _a3;
11426
- 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 || "";
11233
+ return req.headers.get("x-license-key") || bodyLicenseKey || config.licenseKey || process.env.RETRIVORA_LICENSE_KEY || process.env.RAG_LICENSE_KEY || "";
11427
11234
  }
11428
11235
  async function enforceLicense(req, config, bodyLicenseKey) {
11429
11236
  const rawKey = resolveLicenseKey(req, config, bodyLicenseKey);
11430
11237
  const licenseKey = (rawKey || "").trim().replace(/^["']|["']$/g, "").trim();
11431
- const projectId = config.projectId || "";
11238
+ const url = new URL(req.url);
11239
+ 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 || "";
11432
11240
  try {
11433
11241
  let payload;
11434
11242
  try {
11435
- payload = await LicenseVerifier.verifyAsync(licenseKey, projectId);
11243
+ payload = await LicenseVerifier.verifyAsync(licenseKey, requestedProjectId);
11436
11244
  } catch (err) {
11437
11245
  return {
11438
11246
  ok: false,
@@ -11444,7 +11252,7 @@ async function enforceLicense(req, config, bodyLicenseKey) {
11444
11252
  }
11445
11253
  return { ok: true, payload };
11446
11254
  } catch (err) {
11447
- 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.";
11255
+ const message = err instanceof Error ? err.message : "Missing or invalid RETRIVORA_LICENSE_KEY. Set RETRIVORA_LICENSE_KEY in your environment or contact your administrator.";
11448
11256
  const body = {
11449
11257
  error: {
11450
11258
  code: "LICENSE_REVOKED",
@@ -11502,38 +11310,59 @@ function sanitizeInput(raw) {
11502
11310
  }
11503
11311
  return { ok: true, value: trimmed };
11504
11312
  }
11313
+ var _pluginCache = /* @__PURE__ */ new Map();
11314
+ var PLUGIN_CACHE_TTL_MS = 10 * 60 * 1e3;
11315
+ function configHash(config) {
11316
+ return (0, import_crypto2.createHash)("sha1").update(JSON.stringify(config != null ? config : {})).digest("hex").slice(0, 16);
11317
+ }
11505
11318
  function getOrCreatePlugin(configOrPlugin) {
11506
11319
  if (configOrPlugin instanceof VectorPlugin) return configOrPlugin;
11507
- const cacheKey = "__retrivoraPlugin_" + JSON.stringify(configOrPlugin != null ? configOrPlugin : {}).slice(0, 64);
11508
- if (!_g[cacheKey]) {
11509
- _g[cacheKey] = new VectorPlugin(configOrPlugin);
11320
+ const key = configHash(configOrPlugin);
11321
+ const cached = _pluginCache.get(key);
11322
+ if (cached && Date.now() - cached.createdAt < PLUGIN_CACHE_TTL_MS) return cached.plugin;
11323
+ const plugin = new VectorPlugin(configOrPlugin);
11324
+ _pluginCache.set(key, { plugin, createdAt: Date.now() });
11325
+ return plugin;
11326
+ }
11327
+ var TELEMETRY_ALLOWED_HOSTS = /* @__PURE__ */ new Set([
11328
+ "retrivora.com",
11329
+ "www.retrivora.com"
11330
+ ]);
11331
+ function sanitizeTelemetryUrl(raw) {
11332
+ if (process.env.NODE_ENV !== "production") return raw;
11333
+ try {
11334
+ const parsed = new URL(raw);
11335
+ const hostname = parsed.hostname.toLowerCase().replace(/^www\./, "");
11336
+ if (!TELEMETRY_ALLOWED_HOSTS.has(`www.${hostname}`) && !TELEMETRY_ALLOWED_HOSTS.has(hostname)) {
11337
+ console.warn(
11338
+ `[Retrivora SDK Telemetry] Blocked non-allowlisted telemetry URL: ${parsed.hostname}. Only retrivora.com endpoints are permitted in production.`
11339
+ );
11340
+ return null;
11341
+ }
11342
+ return parsed.toString();
11343
+ } catch (e) {
11344
+ return null;
11510
11345
  }
11511
- return _g[cacheKey];
11512
11346
  }
11513
11347
  function reportTelemetry(req, plugin, action, status, details, trace) {
11514
- var _a3, _b, _c, _d, _e, _f, _g2;
11348
+ var _a2, _b, _c, _d, _e, _f, _g2;
11515
11349
  try {
11516
11350
  const config = plugin.getConfig();
11517
- const licenseKey = config.licenseKey || process.env.RAG_LICENSE_KEY || process.env.RETRIVORA_LICENSE_KEY || process.env.NEXT_PUBLIC_RETRIVORA_LICENSE_KEY;
11351
+ const licenseKey = config.licenseKey || process.env.RETRIVORA_LICENSE_KEY || process.env.RAG_LICENSE_KEY;
11518
11352
  const telemetryConfig = config.telemetry;
11519
- const host = req.headers.get("host") || "localhost";
11520
11353
  const defaultUrl = "https://www.retrivora.com/api/telemetry";
11521
- const telemetryUrl = (telemetryConfig == null ? void 0 : telemetryConfig.url) || process.env.TELEMETRY_URL || process.env.NEXT_PUBLIC_TELEMETRY_URL || defaultUrl;
11354
+ const rawTelemetryUrl = (telemetryConfig == null ? void 0 : telemetryConfig.url) || process.env.TELEMETRY_URL || process.env.NEXT_PUBLIC_TELEMETRY_URL || defaultUrl;
11355
+ const telemetryUrl = sanitizeTelemetryUrl(rawTelemetryUrl);
11356
+ if (!telemetryUrl) return;
11522
11357
  const userAgent = req.headers.get("user-agent") || `Retrivora-SDK/${SDK_VERSION}`;
11523
- let absoluteUrl = telemetryUrl;
11524
- if (!telemetryUrl.startsWith("http")) {
11525
- const proto = req.headers.get("x-forwarded-proto") || "http";
11526
- absoluteUrl = `${proto}://${host}${telemetryUrl}`;
11527
- }
11528
11358
  const projectId = config.projectId || "default";
11529
- 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";
11359
+ 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";
11530
11360
  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";
11531
11361
  const tokens = Number(((_e = trace == null ? void 0 : trace.tokens) == null ? void 0 : _e.totalTokens) || (trace == null ? void 0 : trace.totalTokens) || 0);
11532
11362
  const costUsd = Number(((_f = trace == null ? void 0 : trace.tokens) == null ? void 0 : _f.estimatedCostUsd) || (trace == null ? void 0 : trace.costUsd) || 0);
11533
11363
  const latencyMs = Number(((_g2 = trace == null ? void 0 : trace.latency) == null ? void 0 : _g2.totalMs) || (trace == null ? void 0 : trace.latencyMs) || 0);
11534
11364
  const payload = {
11535
11365
  trace,
11536
- licenseKey,
11537
11366
  projectId,
11538
11367
  organization: process.env.RETRIVORA_ORGANIZATION || "default-org",
11539
11368
  sdkVersion: SDK_VERSION,
@@ -11550,14 +11379,13 @@ function reportTelemetry(req, plugin, action, status, details, trace) {
11550
11379
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
11551
11380
  details
11552
11381
  };
11553
- fetch(absoluteUrl, {
11382
+ fetch(telemetryUrl, {
11554
11383
  method: "POST",
11555
- headers: {
11556
- "Content-Type": "application/json",
11557
- "x-forwarded-for": req.headers.get("x-forwarded-for") || "",
11558
- "x-real-ip": req.headers.get("x-real-ip") || "",
11384
+ headers: __spreadProps(__spreadValues({
11385
+ "Content-Type": "application/json"
11386
+ }, licenseKey ? { "x-license-key": licenseKey } : {}), {
11559
11387
  "user-agent": userAgent
11560
- },
11388
+ }),
11561
11389
  body: JSON.stringify(payload)
11562
11390
  }).catch((err) => {
11563
11391
  console.warn("[Retrivora SDK Telemetry] Async report warning:", err.message);
@@ -11607,7 +11435,7 @@ function createChatHandler(configOrPlugin, options) {
11607
11435
  const storage = new DatabaseStorage(plugin.getConfig());
11608
11436
  const onAuthorize = options == null ? void 0 : options.onAuthorize;
11609
11437
  return async function POST(req, context) {
11610
- var _a3, _b, _c, _d, _e, _f;
11438
+ var _a2, _b, _c, _d, _e, _f;
11611
11439
  const authResult = await checkAuth(req, onAuthorize);
11612
11440
  if (authResult) return authResult;
11613
11441
  const rateLimited = checkRateLimit(req);
@@ -11620,18 +11448,17 @@ function createChatHandler(configOrPlugin, options) {
11620
11448
  if (!licenseCheck.ok) return licenseCheck.response;
11621
11449
  if (isFreeTier(licenseCheck.payload.tier)) {
11622
11450
  const estimatedInputTokens = Math.ceil(
11623
- ((((_a3 = bodyObj.message) == null ? void 0 : _a3.length) || 0) + (Array.isArray(bodyObj.messages) ? bodyObj.messages.reduce((a, m) => {
11624
- var _a4, _b2;
11625
- 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);
11451
+ ((((_a2 = bodyObj.message) == null ? void 0 : _a2.length) || 0) + (Array.isArray(bodyObj.messages) ? bodyObj.messages.reduce((a, m) => {
11452
+ var _a3, _b2;
11453
+ 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);
11626
11454
  }, 0) : 0) + (((_b = bodyObj.history) == null ? void 0 : _b.reduce((a, m) => {
11627
- var _a4, _b2;
11628
- 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);
11455
+ var _a3, _b2;
11456
+ 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);
11629
11457
  }, 0)) || 0)) / 4
11630
11458
  );
11631
- const chatCheck = FreeTierLimitsGuard.checkRequestAllowed({
11459
+ const chatCheck = FreeTierLimitsGuard_exports.FreeTierLimitsGuard.checkRequestAllowed({
11632
11460
  operationType: "chat",
11633
- inputTokens: estimatedInputTokens,
11634
- projectId: licenseCheck.payload.projectId
11461
+ inputTokens: estimatedInputTokens
11635
11462
  });
11636
11463
  if (!chatCheck.allowed) {
11637
11464
  return createPaymentRequiredResponse(
@@ -11690,7 +11517,7 @@ function createStreamHandler(configOrPlugin, options) {
11690
11517
  const storage = new DatabaseStorage(plugin.getConfig());
11691
11518
  const onAuthorize = options == null ? void 0 : options.onAuthorize;
11692
11519
  return async function POST(req, context) {
11693
- var _a3, _b, _c;
11520
+ var _a2, _b;
11694
11521
  const authResult = await checkAuth(req, onAuthorize);
11695
11522
  if (authResult) return authResult;
11696
11523
  const rateLimited = checkRateLimit(req);
@@ -11698,10 +11525,6 @@ function createStreamHandler(configOrPlugin, options) {
11698
11525
  const config = plugin.getConfig();
11699
11526
  let body;
11700
11527
  try {
11701
- if ((_a3 = req.headers.get("content-type")) == null ? void 0 : _a3.includes("multipart/form-data")) {
11702
- const uploader = createUploadHandler(plugin, { onAuthorize });
11703
- return uploader(req);
11704
- }
11705
11528
  body = await req.json();
11706
11529
  } catch (e) {
11707
11530
  return new Response(JSON.stringify({ error: { code: "INVALID_JSON", message: "Invalid JSON body" } }), {
@@ -11714,15 +11537,15 @@ function createStreamHandler(configOrPlugin, options) {
11714
11537
  if (!licenseCheck.ok) return licenseCheck.response;
11715
11538
  if (isFreeTier(licenseCheck.payload.tier)) {
11716
11539
  const estimatedInputTokens = Math.ceil(
11717
- ((((_b = bodyObj.message) == null ? void 0 : _b.length) || 0) + (Array.isArray(bodyObj.messages) ? bodyObj.messages.reduce((a, m) => {
11718
- var _a4, _b2;
11719
- 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);
11720
- }, 0) : 0) + (((_c = bodyObj.history) == null ? void 0 : _c.reduce((a, m) => {
11721
- var _a4, _b2;
11722
- 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);
11540
+ ((((_a2 = bodyObj.message) == null ? void 0 : _a2.length) || 0) + (Array.isArray(bodyObj.messages) ? bodyObj.messages.reduce((a, m) => {
11541
+ var _a3, _b2;
11542
+ 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);
11543
+ }, 0) : 0) + (((_b = bodyObj.history) == null ? void 0 : _b.reduce((a, m) => {
11544
+ var _a3, _b2;
11545
+ 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);
11723
11546
  }, 0)) || 0)) / 4
11724
11547
  );
11725
- const chatCheck = FreeTierLimitsGuard.checkRequestAllowed({
11548
+ const chatCheck = FreeTierLimitsGuard_exports.FreeTierLimitsGuard.checkRequestAllowed({
11726
11549
  operationType: "chat_stream",
11727
11550
  inputTokens: estimatedInputTokens,
11728
11551
  projectId: licenseCheck.payload.projectId
@@ -11765,7 +11588,7 @@ function createStreamHandler(configOrPlugin, options) {
11765
11588
  let isActive = true;
11766
11589
  const stream = new ReadableStream({
11767
11590
  async start(controller) {
11768
- var _a4, _b2, _c2, _d, _e;
11591
+ var _a3, _b2, _c, _d, _e;
11769
11592
  const enqueue = (text) => {
11770
11593
  if (!isActive) return;
11771
11594
  try {
@@ -11798,7 +11621,7 @@ function createStreamHandler(configOrPlugin, options) {
11798
11621
  let uiTransformation = responseChunk == null ? void 0 : responseChunk.ui_transformation;
11799
11622
  if (sources.length > 0) {
11800
11623
  try {
11801
- uiTransformation = (_a4 = responseChunk == null ? void 0 : responseChunk.ui_transformation) != null ? _a4 : UITransformer.transform(message, sources, plugin.getConfig());
11624
+ uiTransformation = (_a3 = responseChunk == null ? void 0 : responseChunk.ui_transformation) != null ? _a3 : UITransformer.transform(message, sources, plugin.getConfig());
11802
11625
  if (uiTransformation) {
11803
11626
  enqueue(sseUIFrame(uiTransformation));
11804
11627
  }
@@ -11822,7 +11645,7 @@ function createStreamHandler(configOrPlugin, options) {
11822
11645
  uiTransformation,
11823
11646
  trace: responseChunk == null ? void 0 : responseChunk.trace
11824
11647
  }).catch((err) => console.warn("[createStreamHandler] Failed to save assistant message:", err));
11825
- 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);
11648
+ 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);
11826
11649
  }
11827
11650
  }
11828
11651
  } catch (temp) {
@@ -11887,7 +11710,7 @@ function createIngestHandler(configOrPlugin, options) {
11887
11710
  (sum, d) => sum + Buffer.byteLength(d.content || "", "utf8"),
11888
11711
  0
11889
11712
  );
11890
- const ingestCheck = FreeTierLimitsGuard.checkIngestionAllowed(
11713
+ const ingestCheck = FreeTierLimitsGuard_exports.FreeTierLimitsGuard.checkIngestionAllowed(
11891
11714
  { documentCount: documents.length },
11892
11715
  totalIncomingBytes
11893
11716
  );
@@ -11939,7 +11762,6 @@ function createLicenseHandler(configOrPlugin, options) {
11939
11762
  const plugin = getOrCreatePlugin(configOrPlugin);
11940
11763
  const onAuthorize = options == null ? void 0 : options.onAuthorize;
11941
11764
  return async function POST(req) {
11942
- var _a3;
11943
11765
  if (req) {
11944
11766
  const authResult = await checkAuth(req, onAuthorize);
11945
11767
  if (authResult) return authResult;
@@ -11947,7 +11769,17 @@ function createLicenseHandler(configOrPlugin, options) {
11947
11769
  try {
11948
11770
  const body = await req.json().catch(() => ({}));
11949
11771
  const config = plugin.getConfig();
11950
- 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 || "";
11772
+ const authHeader = req.headers.get("authorization");
11773
+ let authHeaderKey = "";
11774
+ if (authHeader && authHeader.toLowerCase().startsWith("bearer ")) {
11775
+ authHeaderKey = authHeader.replace(/^Bearer\s+/i, "");
11776
+ if (authHeaderKey) {
11777
+ console.warn(
11778
+ '[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.'
11779
+ );
11780
+ }
11781
+ }
11782
+ const rawKey = req.headers.get("x-license-key") || (body == null ? void 0 : body.licenseKey) || authHeaderKey || config.licenseKey || process.env.RETRIVORA_LICENSE_KEY || "";
11951
11783
  const licenseKey = (rawKey || "").trim().replace(/^["']|["']$/g, "").trim();
11952
11784
  const projectId = (body == null ? void 0 : body.projectId) || config.projectId || "";
11953
11785
  const payload = await LicenseVerifier.verifyAsync(licenseKey, projectId);
@@ -11998,7 +11830,7 @@ function createUploadHandler(configOrPlugin, options) {
11998
11830
  }
11999
11831
  if (isFreeTier(licenseCheck.payload.tier)) {
12000
11832
  const totalUploadBytes = files.reduce((sum, f) => sum + (f.size || 0), 0);
12001
- const uploadCheck = FreeTierLimitsGuard.checkIngestionAllowed(
11833
+ const uploadCheck = FreeTierLimitsGuard_exports.FreeTierLimitsGuard.checkIngestionAllowed(
12002
11834
  { documentCount: files.length },
12003
11835
  totalUploadBytes
12004
11836
  );
@@ -12010,8 +11842,8 @@ function createUploadHandler(configOrPlugin, options) {
12010
11842
  projectId: licenseCheck.payload.projectId,
12011
11843
  fileCount: files.length,
12012
11844
  totalUploadBytes,
12013
- maxFileSizeBytes: FREE_TIER_QUOTAS.MAX_FILE_SIZE_BYTES,
12014
- maxStorageBytes: FREE_TIER_QUOTAS.MAX_STORAGE_BYTES
11845
+ maxFileSizeBytes: FreeTierLimitsGuard_exports.FREE_TIER_QUOTAS.MAX_FILE_SIZE_BYTES,
11846
+ maxStorageBytes: FreeTierLimitsGuard_exports.FREE_TIER_QUOTAS.MAX_STORAGE_BYTES
12015
11847
  }
12016
11848
  );
12017
11849
  }
@@ -12325,7 +12157,7 @@ function createRagHandler(configOrPlugin, options) {
12325
12157
  }
12326
12158
  }
12327
12159
  async function getSegment(req, context) {
12328
- var _a3, _b, _c;
12160
+ var _a2, _b, _c;
12329
12161
  const contentType = req.headers.get("content-type") || "";
12330
12162
  if (contentType.includes("multipart/form-data")) {
12331
12163
  return "upload";
@@ -12344,7 +12176,7 @@ function createRagHandler(configOrPlugin, options) {
12344
12176
  if (pathname.endsWith("/sessions")) return "sessions";
12345
12177
  } catch (e) {
12346
12178
  }
12347
- 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;
12179
+ 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;
12348
12180
  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) || [];
12349
12181
  const joined = segments.join("/");
12350
12182
  return joined || "chat";
@@ -12422,7 +12254,6 @@ var _LicenseValidator = class _LicenseValidator {
12422
12254
  * lock the chat widget.
12423
12255
  */
12424
12256
  async validate(request) {
12425
- var _a3;
12426
12257
  const {
12427
12258
  licenseKey,
12428
12259
  projectId,
@@ -12441,7 +12272,17 @@ var _LicenseValidator = class _LicenseValidator {
12441
12272
  }
12442
12273
  return void 0;
12443
12274
  };
12444
- const rawKey = licenseKey || getHeader("x-license-key") || ((_a3 = getHeader("authorization")) == null ? void 0 : _a3.replace(/^Bearer\s+/i, "")) || (typeof process !== "undefined" ? process.env.NEXT_PUBLIC_RETRIVORA_LICENSE_KEY || process.env.RETRIVORA_LICENSE_KEY : "") || "";
12275
+ const authHeader = getHeader("authorization");
12276
+ let authHeaderKey = "";
12277
+ if (authHeader && authHeader.toLowerCase().startsWith("bearer ")) {
12278
+ authHeaderKey = authHeader.replace(/^Bearer\s+/i, "");
12279
+ if (authHeaderKey) {
12280
+ console.warn(
12281
+ '[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.'
12282
+ );
12283
+ }
12284
+ }
12285
+ const rawKey = licenseKey || getHeader("x-license-key") || authHeaderKey || (typeof process !== "undefined" ? process.env.NEXT_PUBLIC_RETRIVORA_LICENSE_KEY || process.env.RETRIVORA_LICENSE_KEY : "") || "";
12445
12286
  const effectiveLicenseKey = (rawKey || "").trim().replace(/^["']|["']$/g, "").trim();
12446
12287
  if (!effectiveLicenseKey) {
12447
12288
  this.purgeCache(effectiveLicenseKey);
@@ -12713,7 +12554,6 @@ LicenseConfigResolver.CACHE_TTL_MS = 36e5;
12713
12554
  LicenseConfigResolver.configCache = /* @__PURE__ */ new Map();
12714
12555
 
12715
12556
  // src/telemetry/TelemetryService.ts
12716
- var import_axios10 = __toESM(require("axios"));
12717
12557
  var EventType = /* @__PURE__ */ ((EventType2) => {
12718
12558
  EventType2["EMBEDDING_GENERATED"] = "embedding_generated";
12719
12559
  EventType2["LLM_CHAT_COMPLETION"] = "llm_chat_completion";
@@ -12733,14 +12573,11 @@ var _TelemetryService = class _TelemetryService {
12733
12573
  this.projectId = projectId;
12734
12574
  this.licenseKey = licenseKey;
12735
12575
  this.userTelemetryUrl = userTelemetryUrl || process.env[_TelemetryService.USER_TELEMETRY_URL_ENV_KEY];
12736
- this.http = import_axios10.default.create({
12737
- timeout: 5e3,
12738
- headers: {
12739
- "Content-Type": "application/json",
12740
- "X-License-Key": licenseKey,
12741
- "X-Project-ID": projectId
12742
- }
12743
- });
12576
+ this.baseHeaders = {
12577
+ "Content-Type": "application/json",
12578
+ "X-License-Key": licenseKey,
12579
+ "X-Project-ID": projectId
12580
+ };
12744
12581
  this.startAutoFlush();
12745
12582
  }
12746
12583
  /**
@@ -12748,7 +12585,7 @@ var _TelemetryService = class _TelemetryService {
12748
12585
  */
12749
12586
  recordEvent(event) {
12750
12587
  if (!this.enabled) return;
12751
- const _a3 = event, { licenseKey } = _a3, safeEvent = __objRest(_a3, ["licenseKey"]);
12588
+ const _a2 = event, { licenseKey } = _a2, safeEvent = __objRest(_a2, ["licenseKey"]);
12752
12589
  const completeEvent = __spreadValues({
12753
12590
  eventType: event.eventType || "error_occurred" /* ERROR_OCCURRED */,
12754
12591
  projectId: this.projectId,
@@ -12833,26 +12670,33 @@ var _TelemetryService = class _TelemetryService {
12833
12670
  if (this.eventQueue.length === 0) return;
12834
12671
  const events = this.eventQueue.splice(0, this.batchSize);
12835
12672
  const payload = {
12836
- events: events.map((e) => {
12837
- const eventWithoutKey = __objRest(e, []);
12838
- return eventWithoutKey;
12839
- }),
12673
+ events: events.map((e) => __spreadValues({}, e)),
12840
12674
  batchId: `${this.projectId}_${Date.now()}`
12841
12675
  };
12842
12676
  try {
12843
- await this.http.post(_TelemetryService.RETRIVORA_TELEMETRY_URL, payload);
12677
+ const res = await fetch(_TelemetryService.RETRIVORA_TELEMETRY_URL, {
12678
+ method: "POST",
12679
+ headers: this.baseHeaders,
12680
+ body: JSON.stringify(payload),
12681
+ signal: AbortSignal.timeout(5e3)
12682
+ });
12683
+ if (!res.ok) {
12684
+ throw new Error(`HTTP ${res.status}`);
12685
+ }
12844
12686
  } catch (err) {
12845
12687
  console.warn("[TelemetryService] Failed to send telemetry to Retrivora:", err.message);
12846
12688
  this.eventQueue.unshift(...events);
12847
12689
  }
12848
12690
  if (this.userTelemetryUrl) {
12849
12691
  try {
12850
- await import_axios10.default.post(this.userTelemetryUrl, payload, {
12851
- timeout: 5e3,
12692
+ await fetch(this.userTelemetryUrl, {
12693
+ method: "POST",
12852
12694
  headers: {
12853
12695
  "Content-Type": "application/json",
12854
12696
  "X-Project-ID": this.projectId
12855
- }
12697
+ },
12698
+ body: JSON.stringify(payload),
12699
+ signal: AbortSignal.timeout(5e3)
12856
12700
  });
12857
12701
  } catch (err) {
12858
12702
  console.warn("[TelemetryService] Failed to send telemetry to user endpoint:", err.message);
@@ -12927,7 +12771,9 @@ var LicensedRetrivora = class {
12927
12771
  }
12928
12772
  }
12929
12773
  /**
12930
- * Initialize the SDK by validating license and fetching config
12774
+ * Generates a unique ID for correlating telemetry events.
12775
+ * Format: `{timestamp}-{random}` — not cryptographically secure, used for
12776
+ * tracing only.
12931
12777
  */
12932
12778
  generateId() {
12933
12779
  return `${Date.now()}-${Math.random().toString(36).substring(2, 9)}`;
@@ -13033,7 +12879,7 @@ var LicensedRetrivora = class {
13033
12879
  * Returns only the response text and sources (abstracted from underlying models)
13034
12880
  */
13035
12881
  async query(queryText, options) {
13036
- var _a3;
12882
+ var _a2;
13037
12883
  if (!this.initialized || !this.pipeline) {
13038
12884
  throw new Error("[LicensedRetrivora] SDK not initialized. Call initialize() first.");
13039
12885
  }
@@ -13041,19 +12887,21 @@ var LicensedRetrivora = class {
13041
12887
  const telemetry = getTelemetryService();
13042
12888
  const startTime = Date.now();
13043
12889
  try {
13044
- const response = await this.pipeline.runNormalQuery(queryText);
12890
+ const response = await this.pipeline.ask(queryText);
13045
12891
  const durationMs = Date.now() - startTime;
13046
12892
  if (telemetry) {
13047
12893
  telemetry.recordQueryExecuted({
13048
12894
  queryText,
13049
- resultCount: ((_a3 = response.sources) == null ? void 0 : _a3.length) || 0,
12895
+ resultCount: ((_a2 = response.sources) == null ? void 0 : _a2.length) || 0,
13050
12896
  durationMs,
13051
12897
  operationId
13052
12898
  });
13053
12899
  }
13054
12900
  return {
13055
12901
  reply: response.reply,
13056
- sources: response.sources
12902
+ sources: response.sources,
12903
+ trace: response.trace,
12904
+ ui_transformation: response.ui_transformation
13057
12905
  };
13058
12906
  } catch (error) {
13059
12907
  const durationMs = Date.now() - startTime;
@@ -13092,9 +12940,17 @@ var LicensedRetrivora = class {
13092
12940
  return this.initialized;
13093
12941
  }
13094
12942
  /**
13095
- * Get current RAG configuration (for debugging)
12943
+ * Get current RAG configuration (for debugging).
12944
+ *
12945
+ * Fix DX-3: throws when called before `initialize()` so callers receive a
12946
+ * clear, actionable error instead of a silent `undefined` that causes a
12947
+ * confusing null-reference error downstream. Consistent with the behavior
12948
+ * of `query()` and `ingest()`.
13096
12949
  */
13097
12950
  getConfig() {
12951
+ if (!this.ragConfig) {
12952
+ throw new Error("[LicensedRetrivora] SDK not initialized. Call initialize() first.");
12953
+ }
13098
12954
  return this.ragConfig;
13099
12955
  }
13100
12956
  };