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