@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.
package/dist/server.mjs CHANGED
@@ -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;
@@ -316,7 +326,7 @@ var init_package = __esm({
316
326
  "package.json"() {
317
327
  package_default = {
318
328
  name: "@retrivora-ai/rag-engine",
319
- version: "2.4.5",
329
+ version: "2.4.6",
320
330
  description: "Retrivora AI is a plug-and-play AI engine for RAG chat experiences \u2014 generic vector DB + LLM provider, embeddable or standalone.",
321
331
  author: "Abhinav Alkuchi",
322
332
  license: "UNLICENSED",
@@ -397,14 +407,19 @@ var init_package = __esm({
397
407
  "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",
398
408
  lint: "eslint",
399
409
  typecheck: "tsc --noEmit -p tsconfig.build.json",
410
+ test: "jest",
400
411
  clean: "rm -rf dist"
401
412
  },
402
413
  peerDependencies: {
414
+ axios: ">=1.0.0",
403
415
  next: ">=15.0.0",
404
416
  react: ">=18.0.0",
405
417
  "react-dom": ">=18.0.0"
406
418
  },
407
419
  peerDependenciesMeta: {
420
+ axios: {
421
+ optional: true
422
+ },
408
423
  next: {
409
424
  optional: true
410
425
  }
@@ -413,9 +428,9 @@ var init_package = __esm({
413
428
  "@anthropic-ai/sdk": "^0.95.1",
414
429
  "@google/genai": "^0.8.0",
415
430
  "@google/generative-ai": "^0.24.1",
431
+ "@retrivora-ai/billing-guards": "*",
416
432
  "@pinecone-database/pinecone": "^7.2.0",
417
433
  "@types/papaparse": "^5.5.2",
418
- axios: "^1.15.0",
419
434
  "lucide-react": "^1.8.0",
420
435
  "next-themes": "^0.4.6",
421
436
  openai: "^6.34.0",
@@ -440,16 +455,20 @@ var init_package = __esm({
440
455
  "@tailwindcss/cli": "^4",
441
456
  "@tailwindcss/postcss": "^4",
442
457
  "@types/estree": "^1.0.9",
458
+ "@types/jest": "^30.0.0",
443
459
  "@types/node": "^20",
444
460
  "@types/pdf-parse": "^1.1.5",
445
461
  "@types/pg": "^8.20.0",
446
462
  "@types/react": "^19.2.14",
447
463
  "@types/react-dom": "^19.2.3",
464
+ axios: "^1.15.0",
448
465
  dotenv: "^17.4.2",
449
466
  eslint: "^9",
450
467
  "eslint-config-next": "16.2.4",
468
+ jest: "^30.4.2",
451
469
  next: "16.2.10",
452
470
  tailwindcss: "^4",
471
+ "ts-jest": "^29.4.12",
453
472
  tsup: "^8.5.1",
454
473
  typescript: "^5"
455
474
  }
@@ -492,7 +511,7 @@ var init_ConfigFetcher = __esm({
492
511
  * PINECONE_API_KEY if available, or vector operations are handled server-side.
493
512
  */
494
513
  static async fetchRemoteConfig(projectId, licenseKey) {
495
- var _a3, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k;
514
+ var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k;
496
515
  const cacheKey = `${projectId}:${licenseKey || ""}`;
497
516
  const now = Date.now();
498
517
  const cached = this.cache.get(cacheKey);
@@ -537,7 +556,7 @@ var init_ConfigFetcher = __esm({
537
556
  indexName: data.vectorDb.indexName || "retrivora-free",
538
557
  provider: data.vectorDb.provider || "pinecone",
539
558
  projectId: fetchedProjectId,
540
- namespace: ((_a3 = data.workspace) == null ? void 0 : _a3.namespace) || data.vectorDb.namespace || fetchedProjectId
559
+ namespace: ((_a2 = data.workspace) == null ? void 0 : _a2.namespace) || data.vectorDb.namespace || fetchedProjectId
541
560
  },
542
561
  embedding: {
543
562
  provider: ((_b = data.embedding) == null ? void 0 : _b.provider) || "universal_rest",
@@ -594,16 +613,20 @@ var init_ConfigFetcher = __esm({
594
613
  return config;
595
614
  }
596
615
  }
597
- } catch (e) {
616
+ } catch (err) {
617
+ console.warn(
618
+ `[ConfigFetcher] Failed to fetch config from ${baseUrl}:`,
619
+ err instanceof Error ? err.message : String(err)
620
+ );
598
621
  }
599
622
  }
600
623
  return null;
601
624
  }
602
625
  /** Convenience: fetch only vector DB config (backwards compat). */
603
626
  static async fetchRemoteVectorConfig(projectId, licenseKey) {
604
- var _a3;
627
+ var _a2;
605
628
  const config = await this.fetchRemoteConfig(projectId, licenseKey);
606
- return (_a3 = config == null ? void 0 : config.vectorDb) != null ? _a3 : null;
629
+ return (_a2 = config == null ? void 0 : config.vectorDb) != null ? _a2 : null;
607
630
  }
608
631
  };
609
632
  ConfigFetcher.cache = /* @__PURE__ */ new Map();
@@ -643,7 +666,7 @@ var init_PineconeProvider = __esm({
643
666
  const errors = [];
644
667
  const opts = config.options || {};
645
668
  const hasLicenseKey = Boolean(
646
- config.licenseKey || process.env.RETRIVORA_LICENSE_KEY || process.env.NEXT_PUBLIC_RETRIVORA_LICENSE_KEY
669
+ config.licenseKey || process.env.RETRIVORA_LICENSE_KEY
647
670
  );
648
671
  if (!opts.apiKey && !hasLicenseKey) {
649
672
  errors.push({
@@ -660,7 +683,7 @@ var init_PineconeProvider = __esm({
660
683
  static getHealthChecker() {
661
684
  return {
662
685
  async check(config) {
663
- var _a3, _b;
686
+ var _a2, _b;
664
687
  const opts = config.options || {};
665
688
  const indexName = config.indexName;
666
689
  const timestamp = Date.now();
@@ -668,7 +691,7 @@ var init_PineconeProvider = __esm({
668
691
  const { Pinecone: Pinecone2 } = await import("@pinecone-database/pinecone");
669
692
  const client = new Pinecone2({ apiKey: opts.apiKey });
670
693
  const indexes = await client.listIndexes();
671
- const indexNames = (_b = (_a3 = indexes.indexes) == null ? void 0 : _a3.map((i) => i.name)) != null ? _b : [];
694
+ const indexNames = (_b = (_a2 = indexes.indexes) == null ? void 0 : _a2.map((i) => i.name)) != null ? _b : [];
672
695
  if (!indexNames.includes(indexName)) {
673
696
  return {
674
697
  healthy: false,
@@ -695,25 +718,13 @@ var init_PineconeProvider = __esm({
695
718
  };
696
719
  }
697
720
  async initialize() {
698
- var _a3, _b;
721
+ var _a2, _b;
699
722
  if (this.client) return;
700
723
  let key = this.apiKey || process.env.PINECONE_API_KEY || "";
701
724
  if (!key) {
702
- const licenseKey = process.env.RETRIVORA_LICENSE_KEY || process.env.NEXT_PUBLIC_RETRIVORA_LICENSE_KEY;
725
+ const licenseKey = process.env.RETRIVORA_LICENSE_KEY;
703
726
  this.licenseKey = licenseKey;
704
- const jwtProjectId = (() => {
705
- if (!licenseKey) return void 0;
706
- try {
707
- const parts = licenseKey.split(".");
708
- if (parts.length >= 2) {
709
- const payload = JSON.parse(Buffer.from(parts[1], "base64url").toString("utf8"));
710
- return payload == null ? void 0 : payload.projectId;
711
- }
712
- } catch (e) {
713
- }
714
- return void 0;
715
- })();
716
- const projectId = process.env.RAG_PROJECT_ID || process.env.NEXT_PUBLIC_RAG_PROJECT_ID || jwtProjectId || this.projectId || "default";
727
+ const projectId = process.env.RAG_PROJECT_ID || process.env.NEXT_PUBLIC_RAG_PROJECT_ID || this.projectId || "default";
717
728
  this.projectId = projectId;
718
729
  const remoteConfig = await ConfigFetcher.fetchRemoteVectorConfig(projectId, licenseKey);
719
730
  if (remoteConfig) {
@@ -741,7 +752,7 @@ var init_PineconeProvider = __esm({
741
752
  this.client = new Pinecone({ apiKey: key });
742
753
  try {
743
754
  const indexes = await this.client.listIndexes();
744
- const names = (_b = (_a3 = indexes.indexes) == null ? void 0 : _a3.map((i) => i.name)) != null ? _b : [];
755
+ const names = (_b = (_a2 = indexes.indexes) == null ? void 0 : _a2.map((i) => i.name)) != null ? _b : [];
745
756
  if (!names.includes(this.indexName)) {
746
757
  console.warn(`[PineconeProvider] Target index "${this.indexName}" not listed. Proceeding with configured index name.`);
747
758
  }
@@ -760,7 +771,7 @@ var init_PineconeProvider = __esm({
760
771
  return namespace ? idx.namespace(namespace) : idx.namespace("");
761
772
  }
762
773
  async upsert(doc, namespace) {
763
- var _a3, _b;
774
+ var _a2, _b;
764
775
  if (!this.client) {
765
776
  if (!this.licenseKey) throw new Error("[PineconeProvider] Cannot execute vector operation: Missing API key and License key.");
766
777
  const res = await fetch(`${this.getControlPlaneUrl()}/api/v1/vectors/upsert`, {
@@ -770,7 +781,7 @@ var init_PineconeProvider = __esm({
770
781
  projectId: this.projectId,
771
782
  indexName: this.indexName,
772
783
  namespace: namespace || this.projectId,
773
- records: [{ id: String(doc.id), values: doc.vector, metadata: __spreadValues({ content: doc.content }, (_a3 = doc.metadata) != null ? _a3 : {}) }]
784
+ records: [{ id: String(doc.id), values: doc.vector, metadata: __spreadValues({ content: doc.content }, (_a2 = doc.metadata) != null ? _a2 : {}) }]
774
785
  })
775
786
  });
776
787
  if (!res.ok) throw new Error(`[PineconeProvider] Proxy upsert failed: ${await res.text()}`);
@@ -791,11 +802,11 @@ var init_PineconeProvider = __esm({
791
802
  if (!this.licenseKey) throw new Error("[PineconeProvider] Cannot execute vector operation: Missing API key and License key.");
792
803
  for (let i = 0; i < docs.length; i += BATCH) {
793
804
  const records = docs.slice(i, i + BATCH).map((d) => {
794
- var _a3;
805
+ var _a2;
795
806
  return {
796
807
  id: String(d.id),
797
808
  values: d.vector,
798
- metadata: __spreadValues({ content: d.content }, (_a3 = d.metadata) != null ? _a3 : {})
809
+ metadata: __spreadValues({ content: d.content }, (_a2 = d.metadata) != null ? _a2 : {})
799
810
  };
800
811
  });
801
812
  const res = await fetch(`${this.getControlPlaneUrl()}/api/v1/vectors/upsert`, {
@@ -815,18 +826,18 @@ var init_PineconeProvider = __esm({
815
826
  const targetIdx = await this.getActiveIndex(namespace);
816
827
  for (let i = 0; i < docs.length; i += BATCH) {
817
828
  const records = docs.slice(i, i + BATCH).map((d) => {
818
- var _a3;
829
+ var _a2;
819
830
  return {
820
831
  id: String(d.id),
821
832
  values: d.vector,
822
- metadata: __spreadValues({ content: d.content }, (_a3 = d.metadata) != null ? _a3 : {})
833
+ metadata: __spreadValues({ content: d.content }, (_a2 = d.metadata) != null ? _a2 : {})
823
834
  };
824
835
  });
825
836
  await targetIdx.upsert({ records });
826
837
  }
827
838
  }
828
839
  async query(vector, topK, namespace, filter) {
829
- var _a3, _b;
840
+ var _a2, _b;
830
841
  const pineconeFilter = this.sanitizeFilter(filter);
831
842
  if (!this.client) {
832
843
  if (!this.licenseKey) throw new Error("[PineconeProvider] Cannot execute vector operation: Missing API key and License key.");
@@ -844,11 +855,11 @@ var init_PineconeProvider = __esm({
844
855
  });
845
856
  if (!res.ok) throw new Error(`[PineconeProvider] Proxy query failed: ${await res.text()}`);
846
857
  const data = await res.json();
847
- return ((_a3 = data.matches) != null ? _a3 : []).map((m) => {
848
- var _a4, _b2;
858
+ return ((_a2 = data.matches) != null ? _a2 : []).map((m) => {
859
+ var _a3, _b2;
849
860
  return {
850
861
  id: m.id,
851
- score: (_a4 = m.score) != null ? _a4 : 0,
862
+ score: (_a3 = m.score) != null ? _a3 : 0,
852
863
  content: ((_b2 = m.metadata) == null ? void 0 : _b2.content) || "",
853
864
  metadata: m.metadata
854
865
  };
@@ -861,10 +872,10 @@ var init_PineconeProvider = __esm({
861
872
  includeMetadata: true
862
873
  }, Object.keys(pineconeFilter).length > 0 ? { filter: pineconeFilter } : {}));
863
874
  return ((_b = result.matches) != null ? _b : []).map((m) => {
864
- var _a4, _b2;
875
+ var _a3, _b2;
865
876
  return {
866
877
  id: m.id,
867
- score: (_a4 = m.score) != null ? _a4 : 0,
878
+ score: (_a3 = m.score) != null ? _a3 : 0,
868
879
  content: ((_b2 = m.metadata) == null ? void 0 : _b2.content) || "",
869
880
  metadata: m.metadata
870
881
  };
@@ -907,13 +918,13 @@ var init_PostgreSQLProvider = __esm({
907
918
  init_BaseVectorProvider();
908
919
  PostgreSQLProvider = class extends BaseVectorProvider {
909
920
  constructor(config) {
910
- var _a3;
921
+ var _a2;
911
922
  super(config);
912
923
  this.tableName = this.indexName.replace(/[^a-z0-9_]/gi, "_");
913
924
  const opts = config.options;
914
925
  if (!opts.connectionString) throw new Error("[PostgreSQLProvider] options.connectionString is required");
915
926
  this.connectionString = opts.connectionString;
916
- this.dimensions = (_a3 = opts.dimensions) != null ? _a3 : 1536;
927
+ this.dimensions = (_a2 = opts.dimensions) != null ? _a2 : 1536;
917
928
  }
918
929
  static getValidator() {
919
930
  return {
@@ -994,7 +1005,7 @@ var init_PostgreSQLProvider = __esm({
994
1005
  }
995
1006
  }
996
1007
  async upsert(doc, namespace = "") {
997
- var _a3;
1008
+ var _a2;
998
1009
  const vectorLiteral = `[${doc.vector.join(",")}]`;
999
1010
  await this.pool.query(
1000
1011
  `INSERT INTO ${this.tableName} (id, namespace, content, metadata, embedding)
@@ -1004,7 +1015,7 @@ var init_PostgreSQLProvider = __esm({
1004
1015
  content = EXCLUDED.content,
1005
1016
  metadata = EXCLUDED.metadata,
1006
1017
  embedding = EXCLUDED.embedding`,
1007
- [doc.id, namespace, doc.content, JSON.stringify((_a3 = doc.metadata) != null ? _a3 : {}), vectorLiteral]
1018
+ [doc.id, namespace, doc.content, JSON.stringify((_a2 = doc.metadata) != null ? _a2 : {}), vectorLiteral]
1008
1019
  );
1009
1020
  }
1010
1021
  async batchUpsert(docs, namespace = "") {
@@ -1017,9 +1028,9 @@ var init_PostgreSQLProvider = __esm({
1017
1028
  const batch = docs.slice(i, i + BATCH_SIZE);
1018
1029
  const values = [];
1019
1030
  const valuePlaceholders = batch.map((doc, idx) => {
1020
- var _a3;
1031
+ var _a2;
1021
1032
  const offset = idx * 5;
1022
- values.push(doc.id, namespace, doc.content, JSON.stringify((_a3 = doc.metadata) != null ? _a3 : {}), `[${doc.vector.join(",")}]`);
1033
+ values.push(doc.id, namespace, doc.content, JSON.stringify((_a2 = doc.metadata) != null ? _a2 : {}), `[${doc.vector.join(",")}]`);
1023
1034
  return `($${offset + 1}, $${offset + 2}, $${offset + 3}, $${offset + 4}, $${offset + 5}::vector)`;
1024
1035
  }).join(", ");
1025
1036
  const query = `
@@ -1102,8 +1113,8 @@ var init_PostgreSQLProvider = __esm({
1102
1113
 
1103
1114
  // src/utils/synonyms.ts
1104
1115
  function resolveMetadataValue(meta, uiKey) {
1105
- var _a3;
1106
- const synonyms = (_a3 = FIELD_SYNONYMS[uiKey]) != null ? _a3 : [];
1116
+ var _a2;
1117
+ const synonyms = (_a2 = FIELD_SYNONYMS[uiKey]) != null ? _a2 : [];
1107
1118
  const keys = Object.keys(meta);
1108
1119
  const systemKeys = ["namespace", "filename", "filesize", "filetype", "docid", "uploadedat", "dimension", "chunkindex"];
1109
1120
  let match = keys.find((k) => k.toLowerCase() === uiKey.toLowerCase());
@@ -1190,14 +1201,14 @@ var init_MultiTablePostgresProvider = __esm({
1190
1201
  init_synonyms();
1191
1202
  MultiTablePostgresProvider = class extends BaseVectorProvider {
1192
1203
  constructor(config) {
1193
- var _a3, _b, _c;
1204
+ var _a2, _b, _c;
1194
1205
  super(config);
1195
1206
  const opts = config.options || {};
1196
1207
  if (!opts.connectionString) {
1197
1208
  throw new Error("[MultiTablePostgresProvider] options.connectionString is required");
1198
1209
  }
1199
1210
  this.connectionString = opts.connectionString;
1200
- this.dimensions = (_a3 = opts.dimensions) != null ? _a3 : 768;
1211
+ this.dimensions = (_a2 = opts.dimensions) != null ? _a2 : 768;
1201
1212
  this.tables = [];
1202
1213
  const rawSearchFields = (_c = (_b = opts.searchFields) != null ? _b : process.env.VECTOR_DB_SEARCH_FIELDS) != null ? _c : ["name", "product_name", "productname", "title"];
1203
1214
  this.searchFields = typeof rawSearchFields === "string" ? rawSearchFields.split(",").map((f) => f.trim()).filter(Boolean) : rawSearchFields;
@@ -1236,7 +1247,6 @@ var init_MultiTablePostgresProvider = __esm({
1236
1247
  this.tables = res.rows.map((r) => r.table_name);
1237
1248
  if (this.tables.length === 0) {
1238
1249
  console.warn('[MultiTablePostgresProvider] No tables with "embedding" columns found in the database.');
1239
- } else {
1240
1250
  }
1241
1251
  } finally {
1242
1252
  client.release();
@@ -1252,11 +1262,11 @@ var init_MultiTablePostgresProvider = __esm({
1252
1262
  * Batch upsert documents by dynamically provisioning tables and columns based on CSV headers.
1253
1263
  */
1254
1264
  async batchUpsert(docs, namespace = "") {
1255
- var _a3;
1265
+ var _a2;
1256
1266
  if (docs.length === 0) return;
1257
1267
  const docsByFile = {};
1258
1268
  for (const doc of docs) {
1259
- const fileName = ((_a3 = doc.metadata) == null ? void 0 : _a3.fileName) || this.uploadTable;
1269
+ const fileName = ((_a2 = doc.metadata) == null ? void 0 : _a2.fileName) || this.uploadTable;
1260
1270
  if (!docsByFile[fileName]) docsByFile[fileName] = [];
1261
1271
  docsByFile[fileName].push(doc);
1262
1272
  }
@@ -1309,11 +1319,11 @@ var init_MultiTablePostgresProvider = __esm({
1309
1319
  const headerColumns = csvHeaders.map((h) => `"${h}"`).join(", ");
1310
1320
  const allColumns = `id, ${headerColumns ? headerColumns + ", " : ""}namespace, content, metadata, embedding`;
1311
1321
  const valuePlaceholders = batch.map((doc, idx) => {
1312
- var _a4, _b;
1322
+ var _a3, _b;
1313
1323
  const offset = idx * (5 + csvHeaders.length);
1314
1324
  values.push(doc.id);
1315
1325
  for (const h of csvHeaders) {
1316
- values.push(String(((_a4 = doc.metadata) == null ? void 0 : _a4[h]) || ""));
1326
+ values.push(String(((_a3 = doc.metadata) == null ? void 0 : _a3[h]) || ""));
1317
1327
  }
1318
1328
  values.push(namespace, doc.content, JSON.stringify((_b = doc.metadata) != null ? _b : {}), `[${doc.vector.join(",")}]`);
1319
1329
  const docPlaceholders = [];
@@ -1352,7 +1362,7 @@ var init_MultiTablePostgresProvider = __esm({
1352
1362
  * Query all configured tables and merge results, sorted by cosine similarity score.
1353
1363
  */
1354
1364
  async query(vector, topK, _namespace, _filter) {
1355
- var _a3;
1365
+ var _a2;
1356
1366
  if (!this.pool) {
1357
1367
  throw new Error("[MultiTablePostgresProvider] Provider not initialized. Call initialize() first.");
1358
1368
  }
@@ -1380,11 +1390,11 @@ var init_MultiTablePostgresProvider = __esm({
1380
1390
  const filterParams = [];
1381
1391
  if (metadataFilters && Object.keys(metadataFilters).length > 0) {
1382
1392
  const conditions = Object.entries(metadataFilters).map(([key, val]) => {
1383
- var _a5;
1393
+ var _a4;
1384
1394
  filterParams.push(String(val));
1385
1395
  const baseOffset = queryText && dynamicKeywordQuery ? 2 : 1;
1386
1396
  const paramIdx = baseOffset + filterParams.length;
1387
- const synonyms = (_a5 = FIELD_SYNONYMS[key]) != null ? _a5 : [];
1397
+ const synonyms = (_a4 = FIELD_SYNONYMS[key]) != null ? _a4 : [];
1388
1398
  const keysToCheck = [key, ...synonyms];
1389
1399
  const coalesceExprs = keysToCheck.flatMap((k) => [
1390
1400
  `metadata->>'${k}'`,
@@ -1451,7 +1461,7 @@ var init_MultiTablePostgresProvider = __esm({
1451
1461
  }
1452
1462
  const tableResults = [];
1453
1463
  for (const row of result.rows) {
1454
- const _a4 = row, { hybrid_score, id } = _a4, rest = __objRest(_a4, ["hybrid_score", "id"]);
1464
+ const _a3 = row, { hybrid_score, id } = _a3, rest = __objRest(_a3, ["hybrid_score", "id"]);
1455
1465
  delete rest.embedding;
1456
1466
  delete rest.vector_score;
1457
1467
  delete rest.keyword_score;
@@ -1480,10 +1490,10 @@ var init_MultiTablePostgresProvider = __esm({
1480
1490
  }
1481
1491
  allResults.sort((a, b) => b.score - a.score);
1482
1492
  if (allResults.length === 0) return [];
1483
- const bestMatchTable = (_a3 = allResults[0].metadata) == null ? void 0 : _a3.source_table;
1493
+ const bestMatchTable = (_a2 = allResults[0].metadata) == null ? void 0 : _a2.source_table;
1484
1494
  const finalSorted = allResults.filter((res) => {
1485
- var _a4;
1486
- return ((_a4 = res.metadata) == null ? void 0 : _a4.source_table) === bestMatchTable;
1495
+ var _a3;
1496
+ return ((_a3 = res.metadata) == null ? void 0 : _a3.source_table) === bestMatchTable;
1487
1497
  });
1488
1498
  return finalSorted.slice(0, Math.max(topK, 15));
1489
1499
  }
@@ -1881,14 +1891,14 @@ var init_QdrantProvider = __esm({
1881
1891
  * Samples points from the collection to discover available payload fields.
1882
1892
  */
1883
1893
  async discoverSchema() {
1884
- var _a3;
1894
+ var _a2;
1885
1895
  try {
1886
1896
  const { data } = await this.http.post(`/collections/${this.indexName}/points/scroll`, {
1887
1897
  limit: 20,
1888
1898
  with_payload: true,
1889
1899
  with_vector: false
1890
1900
  });
1891
- const points = ((_a3 = data.result) == null ? void 0 : _a3.points) || [];
1901
+ const points = ((_a2 = data.result) == null ? void 0 : _a2.points) || [];
1892
1902
  const keys = /* @__PURE__ */ new Set();
1893
1903
  for (const point of points) {
1894
1904
  const payload = point.payload || {};
@@ -1914,11 +1924,11 @@ var init_QdrantProvider = __esm({
1914
1924
  * Ensures the collection exists. Creates it if missing.
1915
1925
  */
1916
1926
  async ensureCollection() {
1917
- var _a3;
1927
+ var _a2;
1918
1928
  try {
1919
1929
  await this.http.get(`/collections/${this.indexName}`);
1920
1930
  } catch (err) {
1921
- if (axios4.isAxiosError(err) && ((_a3 = err.response) == null ? void 0 : _a3.status) === 404) {
1931
+ if (axios4.isAxiosError(err) && ((_a2 = err.response) == null ? void 0 : _a2.status) === 404) {
1922
1932
  const opts = this.config.options;
1923
1933
  const dimensionsForCreate = opts.dimensions || 1536;
1924
1934
  await this.http.put(`/collections/${this.indexName}`, {
@@ -1936,7 +1946,7 @@ var init_QdrantProvider = __esm({
1936
1946
  * Ensures that a payload field has an index.
1937
1947
  */
1938
1948
  async ensureIndex(fieldName, schema = "keyword") {
1939
- var _a3;
1949
+ var _a2;
1940
1950
  let fullPath = fieldName;
1941
1951
  if (!this.isFlatPayload && !fieldName.includes(".") && fieldName !== "namespace" && fieldName !== this.contentField) {
1942
1952
  fullPath = `${this.metadataField}.${fieldName}`;
@@ -1948,7 +1958,7 @@ var init_QdrantProvider = __esm({
1948
1958
  });
1949
1959
  } catch (err) {
1950
1960
  let status;
1951
- if (axios4.isAxiosError(err)) status = (_a3 = err.response) == null ? void 0 : _a3.status;
1961
+ if (axios4.isAxiosError(err)) status = (_a2 = err.response) == null ? void 0 : _a2.status;
1952
1962
  if (status === 409) return;
1953
1963
  console.warn(`[QdrantProvider] \u26A0\uFE0F Could not ensure index for "${fullPath}":`, err instanceof Error ? err.message : String(err));
1954
1964
  }
@@ -1977,7 +1987,7 @@ var init_QdrantProvider = __esm({
1977
1987
  await this.http.put(`/collections/${this.indexName}/points`, payload);
1978
1988
  }
1979
1989
  async query(vector, topK, namespace, _filter) {
1980
- var _a3;
1990
+ var _a2;
1981
1991
  const must = [];
1982
1992
  if (namespace) {
1983
1993
  must.push({ key: "namespace", match: { value: namespace } });
@@ -1999,7 +2009,7 @@ var init_QdrantProvider = __esm({
1999
2009
  limit: topK,
2000
2010
  with_payload: true,
2001
2011
  params: {
2002
- hnsw_ef: ((_a3 = this.config.options) == null ? void 0 : _a3.efSearch) || Math.max(topK * 20, 128),
2012
+ hnsw_ef: ((_a2 = this.config.options) == null ? void 0 : _a2.efSearch) || Math.max(topK * 20, 128),
2003
2013
  exact: false
2004
2014
  },
2005
2015
  filter: must.length > 0 ? { must } : void 0
@@ -2094,12 +2104,12 @@ var init_ChromaDBProvider = __esm({
2094
2104
  * Get or create the ChromaDB collection.
2095
2105
  */
2096
2106
  async initialize() {
2097
- var _a3;
2107
+ var _a2;
2098
2108
  try {
2099
2109
  const { data } = await this.http.get(`/api/v1/collections/${this.indexName}`);
2100
2110
  this.collectionId = data.id;
2101
2111
  } catch (err) {
2102
- if (axios5.isAxiosError(err) && ((_a3 = err.response) == null ? void 0 : _a3.status) === 404) {
2112
+ if (axios5.isAxiosError(err) && ((_a2 = err.response) == null ? void 0 : _a2.status) === 404) {
2103
2113
  const { data } = await this.http.post("/api/v1/collections", {
2104
2114
  name: this.indexName
2105
2115
  });
@@ -2317,7 +2327,7 @@ var init_WeaviateProvider = __esm({
2317
2327
  await this.http.post("/v1/batch/objects", payload);
2318
2328
  }
2319
2329
  async query(vector, topK, namespace, _filter) {
2320
- var _a3, _b;
2330
+ var _a2, _b;
2321
2331
  const queryText = _filter == null ? void 0 : _filter.queryText;
2322
2332
  const sanitizedFilter = this.sanitizeFilter(_filter);
2323
2333
  const searchParams = queryText ? `hybrid: { query: ${JSON.stringify(queryText)}, alpha: 0.5 }` : `nearVector: { vector: ${JSON.stringify(vector)} }`;
@@ -2343,7 +2353,7 @@ var init_WeaviateProvider = __esm({
2343
2353
  `
2344
2354
  };
2345
2355
  const { data } = await this.http.post("/v1/graphql", graphqlQuery);
2346
- const results = ((_b = (_a3 = data.data) == null ? void 0 : _a3.Get) == null ? void 0 : _b[this.indexName]) || [];
2356
+ const results = ((_b = (_a2 = data.data) == null ? void 0 : _a2.Get) == null ? void 0 : _b[this.indexName]) || [];
2347
2357
  return results.map((res) => ({
2348
2358
  id: res["_additional"].id,
2349
2359
  score: 1 - res["_additional"].distance,
@@ -2425,21 +2435,21 @@ var init_UniversalVectorProvider = __esm({
2425
2435
  }
2426
2436
  }
2427
2437
  async initialize() {
2428
- var _a3;
2438
+ var _a2;
2429
2439
  this.http = axios8.create({
2430
2440
  baseURL: this.opts.baseUrl,
2431
2441
  headers: __spreadValues({
2432
2442
  "Content-Type": "application/json"
2433
2443
  }, this.opts.headers),
2434
- timeout: (_a3 = this.opts.timeout) != null ? _a3 : 3e4
2444
+ timeout: (_a2 = this.opts.timeout) != null ? _a2 : 3e4
2435
2445
  });
2436
2446
  if (!await this.ping()) {
2437
2447
  throw new Error(`[UniversalVectorProvider] Failed to connect to ${this.opts.baseUrl}`);
2438
2448
  }
2439
2449
  }
2440
2450
  async upsert(doc, namespace) {
2441
- var _a3, _b, _c;
2442
- const endpoint = (_a3 = this.opts.upsertEndpoint) != null ? _a3 : "/upsert";
2451
+ var _a2, _b, _c;
2452
+ const endpoint = (_a2 = this.opts.upsertEndpoint) != null ? _a2 : "/upsert";
2443
2453
  const template = (_b = this.opts.upsertTemplate) != null ? _b : JSON.stringify({
2444
2454
  id: "{{id}}",
2445
2455
  vector: "{{vector}}",
@@ -2472,8 +2482,8 @@ var init_UniversalVectorProvider = __esm({
2472
2482
  }
2473
2483
  }
2474
2484
  async query(vector, topK, namespace, filter) {
2475
- var _a3, _b;
2476
- const endpoint = (_a3 = this.opts.queryEndpoint) != null ? _a3 : "/query";
2485
+ var _a2, _b;
2486
+ const endpoint = (_a2 = this.opts.queryEndpoint) != null ? _a2 : "/query";
2477
2487
  const template = (_b = this.opts.queryTemplate) != null ? _b : JSON.stringify({
2478
2488
  vector: "{{vector}}",
2479
2489
  limit: "{{topK}}",
@@ -2499,9 +2509,9 @@ var init_UniversalVectorProvider = __esm({
2499
2509
  );
2500
2510
  }
2501
2511
  return results.map((item) => {
2502
- var _a4, _b2, _c, _d, _e, _f, _g2;
2512
+ var _a3, _b2, _c, _d, _e, _f, _g2;
2503
2513
  return {
2504
- id: item[(_a4 = this.opts.queryIdField) != null ? _a4 : "id"],
2514
+ id: item[(_a3 = this.opts.queryIdField) != null ? _a3 : "id"],
2505
2515
  score: (_c = item[(_b2 = this.opts.queryScoreField) != null ? _b2 : "score"]) != null ? _c : 0,
2506
2516
  content: (_e = item[(_d = this.opts.queryContentField) != null ? _d : "content"]) != null ? _e : "",
2507
2517
  metadata: (_g2 = item[(_f = this.opts.queryMetadataField) != null ? _f : "metadata"]) != null ? _g2 : {}
@@ -2514,8 +2524,8 @@ var init_UniversalVectorProvider = __esm({
2514
2524
  }
2515
2525
  }
2516
2526
  async delete(id, namespace) {
2517
- var _a3, _b;
2518
- const endpoint = (_a3 = this.opts.deleteEndpoint) != null ? _a3 : "/delete";
2527
+ var _a2, _b;
2528
+ const endpoint = (_a2 = this.opts.deleteEndpoint) != null ? _a2 : "/delete";
2519
2529
  const template = (_b = this.opts.deleteTemplate) != null ? _b : JSON.stringify({
2520
2530
  id: "{{id}}",
2521
2531
  namespace: "{{namespace}}"
@@ -2533,8 +2543,8 @@ var init_UniversalVectorProvider = __esm({
2533
2543
  }
2534
2544
  }
2535
2545
  async deleteNamespace(namespace) {
2536
- var _a3;
2537
- const endpoint = (_a3 = this.opts.deleteNamespaceEndpoint) != null ? _a3 : "/delete-namespace";
2546
+ var _a2;
2547
+ const endpoint = (_a2 = this.opts.deleteNamespaceEndpoint) != null ? _a2 : "/delete-namespace";
2538
2548
  try {
2539
2549
  await this.http.post(endpoint, { namespace });
2540
2550
  } catch (error) {
@@ -2544,9 +2554,9 @@ var init_UniversalVectorProvider = __esm({
2544
2554
  }
2545
2555
  }
2546
2556
  async ping() {
2547
- var _a3;
2557
+ var _a2;
2548
2558
  try {
2549
- const endpoint = (_a3 = this.opts.pingEndpoint) != null ? _a3 : "/health";
2559
+ const endpoint = (_a2 = this.opts.pingEndpoint) != null ? _a2 : "/health";
2550
2560
  const response = await this.http.get(endpoint, { timeout: 5e3 });
2551
2561
  return response.status >= 200 && response.status < 300;
2552
2562
  } catch (e) {
@@ -2686,13 +2696,13 @@ var LicenseValidationError = class extends RetrivoraError {
2686
2696
  }
2687
2697
  };
2688
2698
  function wrapError(err, defaultCode, defaultMessage) {
2689
- var _a3;
2699
+ var _a2;
2690
2700
  if (err instanceof RetrivoraError) {
2691
2701
  return err;
2692
2702
  }
2693
2703
  const error = err;
2694
2704
  const message = (error == null ? void 0 : error.message) || defaultMessage || String(err);
2695
- 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);
2705
+ 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);
2696
2706
  const code = error == null ? void 0 : error.code;
2697
2707
  if (status === 429 || /rate[- ]?limit/i.test(message) || code === "RATE_LIMIT_EXCEEDED") {
2698
2708
  return new RateLimitException(message, err);
@@ -2979,8 +2989,8 @@ function getRuntimeConfig() {
2979
2989
  return _runtimeConfig;
2980
2990
  }
2981
2991
  function readString(env, name) {
2982
- var _a3;
2983
- const value = (_a3 = env[name]) == null ? void 0 : _a3.trim();
2992
+ var _a2;
2993
+ const value = (_a2 = env[name]) == null ? void 0 : _a2.trim();
2984
2994
  return value ? value : void 0;
2985
2995
  }
2986
2996
  function readNumber(env, name, fallback) {
@@ -2993,8 +3003,8 @@ function readNumber(env, name, fallback) {
2993
3003
  return parsed;
2994
3004
  }
2995
3005
  function readEnum(env, name, fallback, allowed) {
2996
- var _a3;
2997
- const value = (_a3 = readString(env, name)) != null ? _a3 : fallback;
3006
+ var _a2;
3007
+ const value = (_a2 = readString(env, name)) != null ? _a2 : fallback;
2998
3008
  if (allowed.includes(value)) {
2999
3009
  return value;
3000
3010
  }
@@ -3004,58 +3014,52 @@ function getRagConfig(baseConfig, env = process.env) {
3004
3014
  return getEnvConfig(env, baseConfig);
3005
3015
  }
3006
3016
  function getEnvConfig(env = process.env, base) {
3007
- 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;
3008
- 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;
3009
- const jwtProjectId = (() => {
3010
- if (!licenseKey) return void 0;
3011
- try {
3012
- const parts = licenseKey.split(".");
3013
- if (parts.length >= 2) {
3014
- const payload = JSON.parse(Buffer.from(parts[1], "base64url").toString("utf8"));
3015
- return payload == null ? void 0 : payload.projectId;
3016
- }
3017
- } catch (e) {
3018
- }
3019
- return void 0;
3020
- })();
3021
- 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";
3022
- 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);
3017
+ 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;
3018
+ const licenseKey = (_c = (_b = (_a2 = readString(env, "RAG_LICENSE_KEY")) != null ? _a2 : readString(env, "RETRIVORA_LICENSE_KEY")) != null ? _b : (
3019
+ // Fix SEC-4: NEXT_PUBLIC_RETRIVORA_LICENSE_KEY is NOT included here.
3020
+ // That variable is inlined into the browser bundle by Next.js at build time
3021
+ // and must never be used as a server-side secret source. Developers who
3022
+ // previously relied on this should migrate to RETRIVORA_LICENSE_KEY.
3023
+ readString(env, "LICENSE_KEY")
3024
+ )) != null ? _c : base == null ? void 0 : base.licenseKey;
3025
+ 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";
3026
+ 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);
3023
3027
  const vectorProvider = readEnum(env, "VECTOR_DB_PROVIDER", "pinecone", VECTOR_DB_PROVIDERS);
3024
3028
  const embeddingDimensions = readNumber(env, "EMBEDDING_DIMENSIONS", 768);
3025
3029
  const vectorDbOptions = {};
3026
3030
  if (vectorProvider === "pinecone") {
3027
- 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 : "";
3028
- 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";
3031
+ 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 : "";
3032
+ 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";
3029
3033
  } else if (vectorProvider === "pgvector" || vectorProvider === "postgresql") {
3030
- 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 : "";
3031
- 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;
3032
- 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;
3034
+ 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 : "";
3035
+ 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;
3036
+ 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;
3033
3037
  vectorDbOptions.dimensions = embeddingDimensions;
3034
3038
  } else if (vectorProvider === "mongodb") {
3035
- 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 : "";
3036
- 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 : "";
3037
- 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 : "";
3038
- 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";
3039
+ 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 : "";
3040
+ 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 : "";
3041
+ 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 : "";
3042
+ 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";
3039
3043
  } else if (vectorProvider === "qdrant") {
3040
- 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";
3041
- 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;
3044
+ 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";
3045
+ 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;
3042
3046
  vectorDbOptions.dimensions = embeddingDimensions;
3043
3047
  } else if (vectorProvider === "milvus") {
3044
- vectorDbOptions.baseUrl = (_ha = readString(env, "MILVUS_URL")) != null ? _ha : "http://localhost:19530";
3048
+ vectorDbOptions.baseUrl = (_da = readString(env, "MILVUS_URL")) != null ? _da : "http://localhost:19530";
3045
3049
  vectorDbOptions.apiKey = readString(env, "MILVUS_API_KEY");
3046
3050
  } else if (vectorProvider === "chromadb") {
3047
- vectorDbOptions.baseUrl = (_ia = readString(env, "CHROMADB_URL")) != null ? _ia : "http://localhost:8000";
3051
+ vectorDbOptions.baseUrl = (_ea = readString(env, "CHROMADB_URL")) != null ? _ea : "http://localhost:8000";
3048
3052
  } else if (vectorProvider === "weaviate") {
3049
- vectorDbOptions.baseUrl = (_ja = readString(env, "WEAVIATE_URL")) != null ? _ja : "http://localhost:8080";
3053
+ vectorDbOptions.baseUrl = (_fa = readString(env, "WEAVIATE_URL")) != null ? _fa : "http://localhost:8080";
3050
3054
  vectorDbOptions.apiKey = readString(env, "WEAVIATE_API_KEY");
3051
3055
  } else if (vectorProvider === "redis") {
3052
- vectorDbOptions.baseUrl = (_ka = readString(env, "REDIS_URL")) != null ? _ka : "";
3056
+ vectorDbOptions.baseUrl = (_ga = readString(env, "REDIS_URL")) != null ? _ga : "";
3053
3057
  vectorDbOptions.apiKey = readString(env, "REDIS_API_KEY");
3054
3058
  } else if (vectorProvider === "rest") {
3055
- vectorDbOptions.baseUrl = (_la = readString(env, "VECTOR_DB_REST_URL")) != null ? _la : "";
3059
+ vectorDbOptions.baseUrl = (_ha = readString(env, "VECTOR_DB_REST_URL")) != null ? _ha : "";
3056
3060
  vectorDbOptions.headers = readString(env, "VECTOR_DB_REST_API_KEY") ? { "api-key": readString(env, "VECTOR_DB_REST_API_KEY") } : {};
3057
3061
  } else if (vectorProvider === "universal_rest") {
3058
- vectorDbOptions.baseUrl = (_na = (_ma = readString(env, "VECTOR_BASE_URL")) != null ? _ma : readString(env, "VECTOR_DB_REST_URL")) != null ? _na : "";
3062
+ vectorDbOptions.baseUrl = (_ja = (_ia = readString(env, "VECTOR_BASE_URL")) != null ? _ia : readString(env, "VECTOR_DB_REST_URL")) != null ? _ja : "";
3059
3063
  vectorDbOptions.profile = readString(env, "VECTOR_UNIVERSAL_PROFILE");
3060
3064
  vectorDbOptions.headers = readString(env, "VECTOR_DB_REST_API_KEY") ? { Authorization: `Bearer ${readString(env, "VECTOR_DB_REST_API_KEY")}` } : {};
3061
3065
  }
@@ -3074,8 +3078,8 @@ function getEnvConfig(env = process.env, base) {
3074
3078
  // Anthropic needs a separate embedding provider; key kept for completeness
3075
3079
  gemini: readString(env, "GEMINI_API_KEY"),
3076
3080
  ollama: void 0,
3077
- universal_rest: (_oa = readString(env, "EMBEDDING_API_KEY")) != null ? _oa : readString(env, "OPENAI_API_KEY"),
3078
- custom: (_pa = readString(env, "EMBEDDING_API_KEY")) != null ? _pa : readString(env, "OPENAI_API_KEY")
3081
+ universal_rest: (_ka = readString(env, "EMBEDDING_API_KEY")) != null ? _ka : readString(env, "OPENAI_API_KEY"),
3082
+ custom: (_la = readString(env, "EMBEDDING_API_KEY")) != null ? _la : readString(env, "OPENAI_API_KEY")
3079
3083
  };
3080
3084
  const DEFAULT_MODEL_BY_PROVIDER = {
3081
3085
  openai: "gpt-4o",
@@ -3097,27 +3101,27 @@ function getEnvConfig(env = process.env, base) {
3097
3101
  })();
3098
3102
  const tierBasedLLMModel = isFreeTier2 ? "llama-3.1-8b-instant" : "llama-3.3-70b-versatile";
3099
3103
  const tierBasedEmbeddingModel = isFreeTier2 ? "text-embedding-004" : "text-embedding-004";
3100
- const controlPlaneUrl = (_qa = readString(env, "RETRIVORA_CONTROL_PLANE_URL")) != null ? _qa : readString(env, "NEXT_PUBLIC_RETRIVORA_CONTROL_PLANE_URL");
3104
+ const controlPlaneUrl = (_ma = readString(env, "RETRIVORA_CONTROL_PLANE_URL")) != null ? _ma : readString(env, "NEXT_PUBLIC_RETRIVORA_CONTROL_PLANE_URL");
3101
3105
  const defaultGatewayUrl = controlPlaneUrl ? `${controlPlaneUrl.replace(/\/$/, "")}/api/v1` : "https://llm.retrivora.com/api/v1";
3102
3106
  const rt = _runtimeConfig;
3103
3107
  const defaultLlmProvider = "universal_rest";
3104
- 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;
3105
- 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;
3106
- 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;
3107
- 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;
3108
- 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";
3108
+ 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;
3109
+ 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;
3110
+ 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;
3111
+ 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;
3112
+ 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";
3109
3113
  const defaultEmbeddingProvider = "universal_rest";
3110
- 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;
3111
- 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;
3112
- 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;
3113
- 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;
3114
- 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";
3114
+ 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;
3115
+ 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;
3116
+ 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;
3117
+ 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;
3118
+ 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";
3115
3119
  return __spreadProps(__spreadValues({
3116
3120
  projectId,
3117
3121
  licenseKey,
3118
3122
  vectorDb: {
3119
3123
  provider: vectorProvider,
3120
- indexName: (_gb = (_fb = readString(env, "VECTOR_DB_INDEX")) != null ? _fb : vectorDbOptions.indexName) != null ? _gb : "rag-index",
3124
+ indexName: (_cb = (_bb = readString(env, "VECTOR_DB_INDEX")) != null ? _bb : vectorDbOptions.indexName) != null ? _cb : "rag-index",
3121
3125
  options: vectorDbOptions
3122
3126
  },
3123
3127
  llm: {
@@ -3125,8 +3129,8 @@ function getEnvConfig(env = process.env, base) {
3125
3129
  model: llmModel,
3126
3130
  apiKey: llmApiKey,
3127
3131
  baseUrl: llmBaseUrl,
3128
- systemPrompt: (_hb = readString(env, "LLM_SYSTEM_PROMPT")) != null ? _hb : rt == null ? void 0 : rt.systemPrompt,
3129
- maxTokens: readNumber(env, "LLM_MAX_TOKENS", (_ib = rt == null ? void 0 : rt.maxTokens) != null ? _ib : 4096),
3132
+ systemPrompt: (_db = readString(env, "LLM_SYSTEM_PROMPT")) != null ? _db : rt == null ? void 0 : rt.systemPrompt,
3133
+ maxTokens: readNumber(env, "LLM_MAX_TOKENS", (_eb = rt == null ? void 0 : rt.maxTokens) != null ? _eb : 4096),
3130
3134
  temperature: readNumber(env, "LLM_TEMPERATURE", 0.7),
3131
3135
  options: {
3132
3136
  profile: llmProfile,
@@ -3147,30 +3151,30 @@ function getEnvConfig(env = process.env, base) {
3147
3151
  }
3148
3152
  },
3149
3153
  ui: {
3150
- title: (_kb = (_jb = readString(env, "NEXT_PUBLIC_UI_TITLE")) != null ? _jb : readString(env, "UI_TITLE")) != null ? _kb : "AI Assistant",
3151
- subtitle: (_mb = (_lb = readString(env, "NEXT_PUBLIC_UI_SUBTITLE")) != null ? _lb : readString(env, "UI_SUBTITLE")) != null ? _mb : "Powered by RAG",
3152
- primaryColor: (_ob = (_nb = readString(env, "NEXT_PUBLIC_PRIMARY_COLOR")) != null ? _nb : readString(env, "UI_PRIMARY_COLOR")) != null ? _ob : "#10b981",
3153
- accentColor: (_qb = (_pb = readString(env, "NEXT_PUBLIC_ACCENT_COLOR")) != null ? _pb : readString(env, "UI_ACCENT_COLOR")) != null ? _qb : "#3b82f6",
3154
- logoUrl: (_rb = readString(env, "NEXT_PUBLIC_LOGO_URL")) != null ? _rb : readString(env, "UI_LOGO_URL"),
3155
- placeholder: (_tb = (_sb = readString(env, "NEXT_PUBLIC_PLACEHOLDER")) != null ? _sb : readString(env, "UI_PLACEHOLDER")) != null ? _tb : "Ask me anything\u2026",
3156
- showSources: ((_vb = (_ub = readString(env, "NEXT_PUBLIC_SHOW_SOURCES")) != null ? _ub : readString(env, "UI_SHOW_SOURCES")) != null ? _vb : "true") !== "false",
3157
- 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.",
3158
- visualStyle: (_zb = (_yb = readString(env, "NEXT_PUBLIC_UI_VISUAL_STYLE")) != null ? _yb : readString(env, "UI_VISUAL_STYLE")) != null ? _zb : "glass",
3159
- borderRadius: (_Bb = (_Ab = readString(env, "NEXT_PUBLIC_UI_BORDER_RADIUS")) != null ? _Ab : readString(env, "UI_BORDER_RADIUS")) != null ? _Bb : "xl",
3160
- allowUpload: ((_Db = (_Cb = readString(env, "NEXT_PUBLIC_ALLOW_UPLOAD")) != null ? _Cb : readString(env, "UI_ALLOW_UPLOAD")) != null ? _Db : "false") === "true"
3154
+ title: (_gb = (_fb = readString(env, "NEXT_PUBLIC_UI_TITLE")) != null ? _fb : readString(env, "UI_TITLE")) != null ? _gb : "AI Assistant",
3155
+ subtitle: (_ib = (_hb = readString(env, "NEXT_PUBLIC_UI_SUBTITLE")) != null ? _hb : readString(env, "UI_SUBTITLE")) != null ? _ib : "Powered by RAG",
3156
+ primaryColor: (_kb = (_jb = readString(env, "NEXT_PUBLIC_PRIMARY_COLOR")) != null ? _jb : readString(env, "UI_PRIMARY_COLOR")) != null ? _kb : "#10b981",
3157
+ accentColor: (_mb = (_lb = readString(env, "NEXT_PUBLIC_ACCENT_COLOR")) != null ? _lb : readString(env, "UI_ACCENT_COLOR")) != null ? _mb : "#3b82f6",
3158
+ logoUrl: (_nb = readString(env, "NEXT_PUBLIC_LOGO_URL")) != null ? _nb : readString(env, "UI_LOGO_URL"),
3159
+ placeholder: (_pb = (_ob = readString(env, "NEXT_PUBLIC_PLACEHOLDER")) != null ? _ob : readString(env, "UI_PLACEHOLDER")) != null ? _pb : "Ask me anything\u2026",
3160
+ showSources: ((_rb = (_qb = readString(env, "NEXT_PUBLIC_SHOW_SOURCES")) != null ? _qb : readString(env, "UI_SHOW_SOURCES")) != null ? _rb : "true") !== "false",
3161
+ 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.",
3162
+ visualStyle: (_vb = (_ub = readString(env, "NEXT_PUBLIC_UI_VISUAL_STYLE")) != null ? _ub : readString(env, "UI_VISUAL_STYLE")) != null ? _vb : "glass",
3163
+ borderRadius: (_xb = (_wb = readString(env, "NEXT_PUBLIC_UI_BORDER_RADIUS")) != null ? _wb : readString(env, "UI_BORDER_RADIUS")) != null ? _xb : "xl",
3164
+ allowUpload: ((_zb = (_yb = readString(env, "NEXT_PUBLIC_ALLOW_UPLOAD")) != null ? _yb : readString(env, "UI_ALLOW_UPLOAD")) != null ? _zb : "false") === "true"
3161
3165
  },
3162
3166
  rag: {
3163
- topK: readNumber(env, "RAG_TOP_K", (_Eb = rt == null ? void 0 : rt.topK) != null ? _Eb : 5),
3167
+ topK: readNumber(env, "RAG_TOP_K", (_Ab = rt == null ? void 0 : rt.topK) != null ? _Ab : 5),
3164
3168
  scoreThreshold: readNumber(env, "RAG_SCORE_THRESHOLD", 0),
3165
- chunkSize: readNumber(env, "RAG_CHUNK_SIZE", (_Fb = rt == null ? void 0 : rt.chunkSize) != null ? _Fb : 1e3),
3166
- chunkOverlap: readNumber(env, "RAG_CHUNK_OVERLAP", (_Gb = rt == null ? void 0 : rt.chunkOverlap) != null ? _Gb : 200),
3167
- filterableFields: (_Hb = readString(env, "RAG_FILTERABLE_FIELDS")) == null ? void 0 : _Hb.split(",").map((f) => f.trim()),
3169
+ chunkSize: readNumber(env, "RAG_CHUNK_SIZE", (_Bb = rt == null ? void 0 : rt.chunkSize) != null ? _Bb : 1e3),
3170
+ chunkOverlap: readNumber(env, "RAG_CHUNK_OVERLAP", (_Cb = rt == null ? void 0 : rt.chunkOverlap) != null ? _Cb : 200),
3171
+ filterableFields: (_Db = readString(env, "RAG_FILTERABLE_FIELDS")) == null ? void 0 : _Db.split(",").map((f) => f.trim()),
3168
3172
  // Query pipeline toggles — read from .env.local
3169
3173
  useQueryTransformation: readString(env, "RAG_USE_QUERY_TRANSFORMATION") === "true",
3170
3174
  useReranking: readString(env, "RAG_USE_RERANKING") === "true",
3171
3175
  useGraphRetrieval: readString(env, "RAG_USE_GRAPH_RETRIEVAL") === "true",
3172
- architecture: (_Ib = readString(env, "RAG_ARCHITECTURE")) != null ? _Ib : "simple",
3173
- chunkingStrategy: (_Jb = readString(env, "RAG_CHUNKING_STRATEGY")) != null ? _Jb : "recursive",
3176
+ architecture: (_Eb = readString(env, "RAG_ARCHITECTURE")) != null ? _Eb : "simple",
3177
+ chunkingStrategy: (_Fb = readString(env, "RAG_CHUNKING_STRATEGY")) != null ? _Fb : "recursive",
3174
3178
  uiMapping: (() => {
3175
3179
  const raw = readString(env, "RAG_UI_MAPPING");
3176
3180
  if (!raw) return void 0;
@@ -3183,7 +3187,7 @@ function getEnvConfig(env = process.env, base) {
3183
3187
  },
3184
3188
  telemetry: {
3185
3189
  enabled: telemetryEnabled,
3186
- 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"
3190
+ 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"
3187
3191
  }
3188
3192
  }, readString(env, "GRAPH_DB_PROVIDER") ? {
3189
3193
  graphDb: {
@@ -3196,8 +3200,8 @@ function getEnvConfig(env = process.env, base) {
3196
3200
  }
3197
3201
  } : {}), {
3198
3202
  mcpServers: (() => {
3199
- var _a4;
3200
- const raw = (_a4 = readString(env, "MCP_SERVERS")) != null ? _a4 : readString(env, "NEXT_PUBLIC_MCP_SERVERS");
3203
+ var _a3;
3204
+ const raw = (_a3 = readString(env, "MCP_SERVERS")) != null ? _a3 : readString(env, "NEXT_PUBLIC_MCP_SERVERS");
3201
3205
  if (!raw) return void 0;
3202
3206
  try {
3203
3207
  return JSON.parse(raw);
@@ -3243,10 +3247,10 @@ var ConfigResolver = class {
3243
3247
  * fallback behavior.
3244
3248
  */
3245
3249
  static resolveUniversal(hostConfig, env = process.env) {
3246
- var _a3;
3250
+ var _a2;
3247
3251
  if (!hostConfig) return this.resolve(void 0, env);
3248
3252
  const normalized = __spreadProps(__spreadValues({}, hostConfig), {
3249
- vectorDb: (_a3 = hostConfig.vectorDb) != null ? _a3 : hostConfig.vectorDatabase,
3253
+ vectorDb: (_a2 = hostConfig.vectorDb) != null ? _a2 : hostConfig.vectorDatabase,
3250
3254
  rag: this.mergeRetrievalWorkflow(hostConfig.rag, hostConfig.retrieval, hostConfig.workflow)
3251
3255
  });
3252
3256
  return this.resolve(normalized, env);
@@ -3266,11 +3270,11 @@ var ConfigResolver = class {
3266
3270
  }
3267
3271
  }
3268
3272
  static mergeRetrievalWorkflow(rag, retrieval, workflow) {
3269
- var _a3, _b, _c, _d, _e, _f;
3273
+ var _a2, _b, _c, _d, _e, _f;
3270
3274
  if (!rag && !retrieval && !workflow) return void 0;
3271
3275
  const normalized = __spreadValues({}, rag != null ? rag : {});
3272
3276
  if (retrieval) {
3273
- normalized.topK = (_a3 = retrieval.topK) != null ? _a3 : normalized.topK;
3277
+ normalized.topK = (_a2 = retrieval.topK) != null ? _a2 : normalized.topK;
3274
3278
  normalized.scoreThreshold = (_b = retrieval.scoreThreshold) != null ? _b : normalized.scoreThreshold;
3275
3279
  normalized.useReranking = (_c = retrieval.useReranking) != null ? _c : normalized.useReranking;
3276
3280
  if (retrieval.strategy === "hybrid") normalized.architecture = (_d = normalized.architecture) != null ? _d : "hybrid";
@@ -3398,8 +3402,8 @@ var OpenAIProvider = class {
3398
3402
  };
3399
3403
  }
3400
3404
  async chat(messages, context, options) {
3401
- var _a3, _b, _c, _d, _e, _f, _g2, _h, _i;
3402
- 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.";
3405
+ var _a2, _b, _c, _d, _e, _f, _g2, _h, _i;
3406
+ 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.";
3403
3407
  const systemMessage = {
3404
3408
  role: "system",
3405
3409
  content: buildSystemContent(basePrompt, context)
@@ -3423,8 +3427,8 @@ var OpenAIProvider = class {
3423
3427
  }
3424
3428
  chatStream(messages, context, options) {
3425
3429
  return __asyncGenerator(this, null, function* () {
3426
- var _a3, _b, _c, _d, _e, _f, _g2, _h;
3427
- 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.";
3430
+ var _a2, _b, _c, _d, _e, _f, _g2, _h;
3431
+ 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.";
3428
3432
  const systemMessage = {
3429
3433
  role: "system",
3430
3434
  content: buildSystemContent(basePrompt, context)
@@ -3471,8 +3475,8 @@ var OpenAIProvider = class {
3471
3475
  return results[0];
3472
3476
  }
3473
3477
  async batchEmbed(texts, options) {
3474
- var _a3, _b, _c, _d, _e;
3475
- 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";
3478
+ var _a2, _b, _c, _d, _e;
3479
+ 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";
3476
3480
  const apiKey = (_e = (_d = this.embeddingConfig) == null ? void 0 : _d.apiKey) != null ? _e : this.llmConfig.apiKey;
3477
3481
  const client = apiKey !== this.llmConfig.apiKey ? new OpenAI({ apiKey }) : this.client;
3478
3482
  const response = await client.embeddings.create({ model, input: texts });
@@ -3531,11 +3535,7 @@ var AnthropicProvider = class {
3531
3535
  try {
3532
3536
  const { default: Anthropic2 } = await import("@anthropic-ai/sdk");
3533
3537
  const client = new Anthropic2({ apiKey });
3534
- await client.messages.create({
3535
- model: modelName,
3536
- max_tokens: 10,
3537
- messages: [{ role: "user", content: "ping" }]
3538
- });
3538
+ await client.models.list();
3539
3539
  return { healthy: true, provider: "anthropic", capabilities: { model: modelName }, timestamp };
3540
3540
  } catch (error) {
3541
3541
  return {
@@ -3549,8 +3549,8 @@ var AnthropicProvider = class {
3549
3549
  };
3550
3550
  }
3551
3551
  async chat(messages, context, options) {
3552
- var _a3, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k;
3553
- 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.";
3552
+ var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k;
3553
+ 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.";
3554
3554
  const system = buildSystemContent(basePrompt, context);
3555
3555
  const anthropicMessages = messages.map((m) => ({
3556
3556
  role: m.role === "assistant" ? "assistant" : "user",
@@ -3588,8 +3588,8 @@ var AnthropicProvider = class {
3588
3588
  }
3589
3589
  chatStream(messages, context, options) {
3590
3590
  return __asyncGenerator(this, null, function* () {
3591
- var _a3, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k;
3592
- 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.";
3591
+ var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k;
3592
+ 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.";
3593
3593
  const system = buildSystemContent(basePrompt, context);
3594
3594
  const anthropicMessages = messages.map((m) => ({
3595
3595
  role: m.role === "assistant" ? "assistant" : "user",
@@ -3673,8 +3673,8 @@ var AnthropicProvider = class {
3673
3673
  import axios from "axios";
3674
3674
  var OllamaProvider = class {
3675
3675
  constructor(llmConfig, embeddingConfig) {
3676
- var _a3, _b, _c;
3677
- const rawBaseURL = ((_a3 = llmConfig.baseUrl) != null ? _a3 : "http://localhost:11434").replace(/\/+$/, "");
3676
+ var _a2, _b, _c;
3677
+ const rawBaseURL = ((_a2 = llmConfig.baseUrl) != null ? _a2 : "http://localhost:11434").replace(/\/+$/, "");
3678
3678
  const baseURL = rawBaseURL.replace(/\/(?:api\/v1|v1|api)$/i, "");
3679
3679
  const timeout = Number((_b = llmConfig.options) == null ? void 0 : _b.timeout) || 3e5;
3680
3680
  const headers = ((_c = llmConfig.options) == null ? void 0 : _c.headers) || {};
@@ -3707,8 +3707,8 @@ var OllamaProvider = class {
3707
3707
  const baseUrl = config.baseUrl || "http://localhost:11434";
3708
3708
  const modelName = config.model;
3709
3709
  try {
3710
- const axios11 = (await import("axios")).default;
3711
- const { data } = await axios11.get(`${baseUrl}/api/tags`);
3710
+ const axios10 = (await import("axios")).default;
3711
+ const { data } = await axios10.get(`${baseUrl}/api/tags`);
3712
3712
  const models = data.models || [];
3713
3713
  const hasModel = models.some((m) => m.name === modelName || m.name.startsWith(`${modelName}:`));
3714
3714
  return {
@@ -3734,8 +3734,8 @@ var OllamaProvider = class {
3734
3734
  };
3735
3735
  }
3736
3736
  async chat(messages, context, options) {
3737
- var _a3, _b, _c, _d, _e, _f;
3738
- 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.";
3737
+ var _a2, _b, _c, _d, _e, _f;
3738
+ 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.";
3739
3739
  const system = buildSystemContent(basePrompt, context);
3740
3740
  const { data } = await this.http.post("/api/chat", {
3741
3741
  model: this.llmConfig.model,
@@ -3753,8 +3753,8 @@ var OllamaProvider = class {
3753
3753
  }
3754
3754
  chatStream(messages, context, options) {
3755
3755
  return __asyncGenerator(this, null, function* () {
3756
- var _a3, _b, _c, _d, _e, _f, _g2, _h;
3757
- 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.";
3756
+ var _a2, _b, _c, _d, _e, _f, _g2, _h;
3757
+ 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.";
3758
3758
  const system = buildSystemContent(basePrompt, context);
3759
3759
  const response = yield new __await(this.http.post("/api/chat", {
3760
3760
  model: this.llmConfig.model,
@@ -3812,8 +3812,8 @@ var OllamaProvider = class {
3812
3812
  });
3813
3813
  }
3814
3814
  async embed(text, options) {
3815
- var _a3, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k;
3816
- 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";
3815
+ var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k;
3816
+ 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";
3817
3817
  const baseURL = (_f = (_e = (_d = this.embeddingConfig) == null ? void 0 : _d.baseUrl) != null ? _e : this.llmConfig.baseUrl) != null ? _f : "http://localhost:11434";
3818
3818
  const client = baseURL !== ((_g2 = this.llmConfig.baseUrl) != null ? _g2 : "http://localhost:11434") ? axios.create({ baseURL, timeout: 6e4 }) : this.http;
3819
3819
  let prompt = text;
@@ -3914,9 +3914,9 @@ var GeminiProvider = class {
3914
3914
  static getHealthChecker() {
3915
3915
  return {
3916
3916
  async check(config) {
3917
- var _a3, _b;
3917
+ var _a2, _b;
3918
3918
  const timestamp = Date.now();
3919
- const apiKey = (_b = (_a3 = config.apiKey) != null ? _a3 : process.env.GOOGLE_GENAI_API_KEY) != null ? _b : "";
3919
+ const apiKey = (_b = (_a2 = config.apiKey) != null ? _a2 : process.env.GOOGLE_GENAI_API_KEY) != null ? _b : "";
3920
3920
  const modelName = sanitizeModel(config.model);
3921
3921
  try {
3922
3922
  const { GoogleGenerativeAI: GoogleGenerativeAI2 } = await import("@google/generative-ai");
@@ -3946,16 +3946,16 @@ var GeminiProvider = class {
3946
3946
  /** Resolve the embedding client — uses a separate client when the embedding
3947
3947
  * API key differs from the LLM API key. */
3948
3948
  get embeddingClient() {
3949
- var _a3;
3950
- if (((_a3 = this.embeddingConfig) == null ? void 0 : _a3.apiKey) && this.embeddingConfig.apiKey !== this.llmConfig.apiKey) {
3949
+ var _a2;
3950
+ if (((_a2 = this.embeddingConfig) == null ? void 0 : _a2.apiKey) && this.embeddingConfig.apiKey !== this.llmConfig.apiKey) {
3951
3951
  return buildClient(this.embeddingConfig.apiKey);
3952
3952
  }
3953
3953
  return this.client;
3954
3954
  }
3955
3955
  /** Resolve the embedding model to use, in order of specificity. */
3956
3956
  resolveEmbeddingModel(optionsModel) {
3957
- var _a3, _b;
3958
- return sanitizeModel((_b = optionsModel != null ? optionsModel : (_a3 = this.embeddingConfig) == null ? void 0 : _a3.model) != null ? _b : DEFAULT_EMBEDDING_MODEL);
3957
+ var _a2, _b;
3958
+ return sanitizeModel((_b = optionsModel != null ? optionsModel : (_a2 = this.embeddingConfig) == null ? void 0 : _a2.model) != null ? _b : DEFAULT_EMBEDDING_MODEL);
3959
3959
  }
3960
3960
  /**
3961
3961
  * Convert ChatMessage[] to the Gemini contents format.
@@ -3975,8 +3975,8 @@ var GeminiProvider = class {
3975
3975
  // ILLMProvider — chat
3976
3976
  // -------------------------------------------------------------------------
3977
3977
  async chat(messages, context, options) {
3978
- var _a3, _b, _c, _d, _e, _f;
3979
- 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.";
3978
+ var _a2, _b, _c, _d, _e, _f;
3979
+ 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.";
3980
3980
  const model = this.client.getGenerativeModel({
3981
3981
  model: this.llmConfig.model,
3982
3982
  systemInstruction: buildSystemContent(basePrompt, context)
@@ -3993,8 +3993,8 @@ var GeminiProvider = class {
3993
3993
  }
3994
3994
  chatStream(messages, context, options) {
3995
3995
  return __asyncGenerator(this, null, function* () {
3996
- var _a3, _b, _c, _d, _e, _f;
3997
- 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.";
3996
+ var _a2, _b, _c, _d, _e, _f;
3997
+ 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.";
3998
3998
  const model = this.client.getGenerativeModel({
3999
3999
  model: this.llmConfig.model,
4000
4000
  systemInstruction: buildSystemContent(basePrompt, context)
@@ -4029,11 +4029,11 @@ var GeminiProvider = class {
4029
4029
  // ILLMProvider — embeddings
4030
4030
  // -------------------------------------------------------------------------
4031
4031
  async embed(text, options) {
4032
- var _a3, _b;
4032
+ var _a2, _b;
4033
4033
  const content = applyPrefix(
4034
4034
  text,
4035
4035
  options == null ? void 0 : options.taskType,
4036
- (_a3 = this.embeddingConfig) == null ? void 0 : _a3.queryPrefix,
4036
+ (_a2 = this.embeddingConfig) == null ? void 0 : _a2.queryPrefix,
4037
4037
  (_b = this.embeddingConfig) == null ? void 0 : _b.documentPrefix
4038
4038
  );
4039
4039
  const modelName = this.resolveEmbeddingModel(options == null ? void 0 : options.model);
@@ -4050,7 +4050,7 @@ var GeminiProvider = class {
4050
4050
  const modelName = this.resolveEmbeddingModel(options == null ? void 0 : options.model);
4051
4051
  const model = this.embeddingClient.getGenerativeModel({ model: modelName });
4052
4052
  const requests = texts.map((text) => {
4053
- var _a3, _b;
4053
+ var _a2, _b;
4054
4054
  return {
4055
4055
  content: {
4056
4056
  role: "user",
@@ -4058,7 +4058,7 @@ var GeminiProvider = class {
4058
4058
  text: applyPrefix(
4059
4059
  text,
4060
4060
  options == null ? void 0 : options.taskType,
4061
- (_a3 = this.embeddingConfig) == null ? void 0 : _a3.queryPrefix,
4061
+ (_a2 = this.embeddingConfig) == null ? void 0 : _a2.queryPrefix,
4062
4062
  (_b = this.embeddingConfig) == null ? void 0 : _b.documentPrefix
4063
4063
  )
4064
4064
  }]
@@ -4075,8 +4075,8 @@ var GeminiProvider = class {
4075
4075
  throw err;
4076
4076
  });
4077
4077
  return response.embeddings.map((e) => {
4078
- var _a3;
4079
- return (_a3 = e.values) != null ? _a3 : [];
4078
+ var _a2;
4079
+ return (_a2 = e.values) != null ? _a2 : [];
4080
4080
  });
4081
4081
  }
4082
4082
  // -------------------------------------------------------------------------
@@ -4165,8 +4165,8 @@ var GroqProvider = class {
4165
4165
  };
4166
4166
  }
4167
4167
  async chat(messages, context, options) {
4168
- var _a3, _b, _c, _d, _e, _f, _g2, _h, _i;
4169
- 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.";
4168
+ var _a2, _b, _c, _d, _e, _f, _g2, _h, _i;
4169
+ 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.";
4170
4170
  const systemMessage = {
4171
4171
  role: "system",
4172
4172
  content: buildSystemContent(basePrompt, context)
@@ -4190,8 +4190,8 @@ var GroqProvider = class {
4190
4190
  }
4191
4191
  chatStream(messages, context, options) {
4192
4192
  return __asyncGenerator(this, null, function* () {
4193
- var _a3, _b, _c, _d, _e, _f, _g2, _h;
4194
- 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.";
4193
+ var _a2, _b, _c, _d, _e, _f, _g2, _h;
4194
+ 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.";
4195
4195
  const systemMessage = {
4196
4196
  role: "system",
4197
4197
  content: buildSystemContent(basePrompt, context)
@@ -4325,8 +4325,8 @@ var QwenProvider = class {
4325
4325
  };
4326
4326
  }
4327
4327
  async chat(messages, context, options) {
4328
- var _a3, _b, _c, _d, _e, _f, _g2, _h, _i;
4329
- 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.";
4328
+ var _a2, _b, _c, _d, _e, _f, _g2, _h, _i;
4329
+ 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.";
4330
4330
  const systemMessage = {
4331
4331
  role: "system",
4332
4332
  content: buildSystemContent(basePrompt, context)
@@ -4350,8 +4350,8 @@ var QwenProvider = class {
4350
4350
  }
4351
4351
  chatStream(messages, context, options) {
4352
4352
  return __asyncGenerator(this, null, function* () {
4353
- var _a3, _b, _c, _d, _e, _f, _g2, _h;
4354
- 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.";
4353
+ var _a2, _b, _c, _d, _e, _f, _g2, _h;
4354
+ 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.";
4355
4355
  const systemMessage = {
4356
4356
  role: "system",
4357
4357
  content: buildSystemContent(basePrompt, context)
@@ -4398,8 +4398,8 @@ var QwenProvider = class {
4398
4398
  return results[0];
4399
4399
  }
4400
4400
  async batchEmbed(texts, options) {
4401
- var _a3, _b, _c, _d, _e, _f;
4402
- 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";
4401
+ var _a2, _b, _c, _d, _e, _f;
4402
+ 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";
4403
4403
  const apiKey = (_e = (_d = this.embeddingConfig) == null ? void 0 : _d.apiKey) != null ? _e : this.llmConfig.apiKey;
4404
4404
  const client = apiKey !== this.llmConfig.apiKey ? new OpenAI3({
4405
4405
  apiKey,
@@ -4499,9 +4499,9 @@ var VECTOR_PROFILES = {
4499
4499
 
4500
4500
  // src/llm/providers/UniversalLLMAdapter.ts
4501
4501
  function extractContent(obj) {
4502
- var _a3, _b, _c;
4502
+ var _a2, _b, _c;
4503
4503
  if (!obj || typeof obj !== "object") return void 0;
4504
- const choice = (_a3 = obj.choices) == null ? void 0 : _a3[0];
4504
+ const choice = (_a2 = obj.choices) == null ? void 0 : _a2[0];
4505
4505
  if (!choice) return void 0;
4506
4506
  if (typeof ((_b = choice.message) == null ? void 0 : _b.content) === "string") return choice.message.content;
4507
4507
  if (typeof ((_c = choice.delta) == null ? void 0 : _c.content) === "string") return choice.delta.content;
@@ -4511,10 +4511,10 @@ function extractContent(obj) {
4511
4511
  }
4512
4512
  var UniversalLLMAdapter = class {
4513
4513
  constructor(config) {
4514
- var _a3, _b, _c, _d, _e, _f, _g2, _h;
4514
+ var _a2, _b, _c, _d, _e, _f, _g2, _h;
4515
4515
  this.model = config.model;
4516
4516
  const llmConfig = config;
4517
- const options = (_a3 = llmConfig.options) != null ? _a3 : {};
4517
+ const options = (_a2 = llmConfig.options) != null ? _a2 : {};
4518
4518
  let profile = {};
4519
4519
  if (typeof options.profile === "string") {
4520
4520
  profile = LLM_PROFILES[options.profile] || {};
@@ -4540,8 +4540,8 @@ var UniversalLLMAdapter = class {
4540
4540
  });
4541
4541
  }
4542
4542
  async chat(messages, context) {
4543
- var _a3, _b, _c, _d, _e, _f, _g2, _h, _i, _j;
4544
- const path2 = (_a3 = this.opts.chatPath) != null ? _a3 : "/chat/completions";
4543
+ var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j;
4544
+ const path2 = (_a2 = this.opts.chatPath) != null ? _a2 : "/chat/completions";
4545
4545
  const safeMessages = (Array.isArray(messages) ? messages : []).filter((m) => Boolean(m && typeof m === "object")).map((m) => ({
4546
4546
  role: m.role || "user",
4547
4547
  content: typeof m.content === "string" ? m.content : m.content != null ? String(m.content) : ""
@@ -4618,8 +4618,8 @@ ${context != null ? context : "None"}` },
4618
4618
  */
4619
4619
  chatStream(messages, context) {
4620
4620
  return __asyncGenerator(this, null, function* () {
4621
- var _a3, _b, _c, _d, _e, _f;
4622
- const path2 = (_a3 = this.opts.chatPath) != null ? _a3 : "/chat/completions";
4621
+ var _a2, _b, _c, _d, _e, _f;
4622
+ const path2 = (_a2 = this.opts.chatPath) != null ? _a2 : "/chat/completions";
4623
4623
  const url = `${this.baseUrl.replace(/\/$/, "")}${path2}`;
4624
4624
  const extractPath = ((_b = this.opts.responseExtractPath) != null ? _b : "choices[0].message.content").replace("message.content", "delta.content");
4625
4625
  const safeMessages = (Array.isArray(messages) ? messages : []).filter((m) => Boolean(m && typeof m === "object")).map((m) => ({
@@ -4745,8 +4745,8 @@ ${context != null ? context : "None"}` },
4745
4745
  });
4746
4746
  }
4747
4747
  async embed(text) {
4748
- var _a3, _b, _c, _d, _e, _f, _g2, _h;
4749
- const path2 = (_a3 = this.opts.embedPath) != null ? _a3 : "/embeddings";
4748
+ var _a2, _b, _c, _d, _e, _f, _g2, _h;
4749
+ const path2 = (_a2 = this.opts.embedPath) != null ? _a2 : "/embeddings";
4750
4750
  const payload = this.opts.embedPayloadTemplate ? buildPayload(this.opts.embedPayloadTemplate, { input: text, model: this.model }) : { input: text, model: this.model };
4751
4751
  try {
4752
4752
  const { data } = await this.http.post(path2, payload);
@@ -4870,7 +4870,7 @@ var LLMFactory = class _LLMFactory {
4870
4870
  ];
4871
4871
  }
4872
4872
  static create(llmConfig, embeddingConfig) {
4873
- var _a3, _b, _c;
4873
+ var _a2, _b, _c;
4874
4874
  switch (llmConfig.provider) {
4875
4875
  case "openai":
4876
4876
  return new OpenAIProvider(llmConfig, embeddingConfig);
@@ -4889,7 +4889,7 @@ var LLMFactory = class _LLMFactory {
4889
4889
  case "custom":
4890
4890
  return new UniversalLLMAdapter(llmConfig);
4891
4891
  default: {
4892
- const providerName = String((_a3 = llmConfig.provider) != null ? _a3 : "").toLowerCase();
4892
+ const providerName = String((_a2 = llmConfig.provider) != null ? _a2 : "").toLowerCase();
4893
4893
  const customFactory = customProviders.get(providerName);
4894
4894
  if (customFactory) {
4895
4895
  return customFactory(llmConfig);
@@ -4991,7 +4991,7 @@ var ProviderRegistry = class {
4991
4991
  return null;
4992
4992
  }
4993
4993
  static async loadVectorProviderClass(provider) {
4994
- var _a3;
4994
+ var _a2;
4995
4995
  if (this.vectorProviders[provider]) return this.vectorProviders[provider];
4996
4996
  switch (provider) {
4997
4997
  case "pinecone": {
@@ -5000,7 +5000,7 @@ var ProviderRegistry = class {
5000
5000
  }
5001
5001
  case "pgvector":
5002
5002
  case "postgresql": {
5003
- const postgresMode = ((_a3 = process.env.POSTGRES_MODE) != null ? _a3 : "multi").toLowerCase();
5003
+ const postgresMode = ((_a2 = process.env.POSTGRES_MODE) != null ? _a2 : "multi").toLowerCase();
5004
5004
  if (postgresMode === "single") {
5005
5005
  const { PostgreSQLProvider: PostgreSQLProvider2 } = await Promise.resolve().then(() => (init_PostgreSQLProvider(), PostgreSQLProvider_exports));
5006
5006
  return PostgreSQLProvider2;
@@ -5292,13 +5292,15 @@ var DocumentChunker = class {
5292
5292
  }
5293
5293
  }
5294
5294
  const parts = text.split(separator);
5295
- for (const part of parts) {
5296
- if (part.length <= chunkSize) {
5297
- finalSplits.push(part + (part === parts[parts.length - 1] ? "" : separator));
5295
+ for (let i = 0; i < parts.length; i++) {
5296
+ const part = parts[i];
5297
+ const splitVal = (i === 0 ? "" : separator) + part;
5298
+ if (splitVal.length <= chunkSize) {
5299
+ finalSplits.push(splitVal);
5298
5300
  } else if (nextSeparators.length > 0) {
5299
- finalSplits.push(...this.recursiveSplit(part, nextSeparators, chunkSize));
5301
+ finalSplits.push(...this.recursiveSplit(splitVal, nextSeparators, chunkSize));
5300
5302
  } else {
5301
- finalSplits.push(part);
5303
+ finalSplits.push(splitVal);
5302
5304
  }
5303
5305
  }
5304
5306
  return finalSplits;
@@ -5377,74 +5379,108 @@ Text to extract from:
5377
5379
  };
5378
5380
 
5379
5381
  // src/rag/Reranker.ts
5382
+ var MAX_RERANK_CONTENT_CHARS = 64e3;
5380
5383
  var Reranker = class {
5381
5384
  /**
5382
5385
  * Re-ranks matches based on a secondary relevance score using an LLM.
5386
+ *
5387
+ * Fix RAG-2: keyword re-ranking no longer mutates the original `score`
5388
+ * field (which consumers interpret as the raw vector similarity). A new
5389
+ * `rerankScore` field is added so both the original similarity and the
5390
+ * boosted rank signal are available to callers.
5383
5391
  */
5384
5392
  async rerank(matches, query, limit = 5, llm) {
5385
5393
  if (!llm || matches.length <= 1) {
5386
- const keywords = query.toLowerCase().replace(/[^\w\s]/g, "").split(/\s+/).filter((w) => w.length > 2);
5387
- if (keywords.length === 0) {
5388
- return matches.sort((a, b) => b.score - a.score).slice(0, limit);
5389
- }
5390
- const scoredMatches = matches.map((match) => {
5391
- var _a3;
5392
- const contentLower = ((_a3 = match == null ? void 0 : match.content) != null ? _a3 : "").toLowerCase();
5393
- let keywordScore = 0;
5394
- keywords.forEach((keyword) => {
5395
- if (contentLower.includes(keyword)) {
5396
- keywordScore += 1;
5397
- }
5398
- });
5399
- const normalizedKeywordScore = keywordScore / keywords.length;
5400
- const combinedScore = match.score + normalizedKeywordScore * 0.5;
5401
- return __spreadProps(__spreadValues({}, match), {
5402
- score: combinedScore
5403
- });
5404
- });
5405
- return scoredMatches.sort((a, b) => b.score - a.score).slice(0, limit);
5394
+ return this.keywordRerank(matches, query, limit);
5406
5395
  }
5407
5396
  try {
5408
- const topN = matches.slice(0, 10);
5409
- const response = await llm.chat(
5410
- [{ role: "user", content: `Query: "${query}"
5397
+ return await this.llmRerank(matches, query, limit, llm);
5398
+ } catch (error) {
5399
+ console.warn("[Reranker] LLM re-ranking failed, falling back to keyword sorting:", error);
5400
+ return this.keywordRerank(matches, query, limit);
5401
+ }
5402
+ }
5403
+ /**
5404
+ * Keyword-based re-ranking fallback.
5405
+ *
5406
+ * Fix RAG-2: `rerankScore` is a separate field; `score` (the original
5407
+ * vector similarity) is never mutated. Callers that display `score` to
5408
+ * end-users receive the canonical cosine similarity value.
5409
+ */
5410
+ keywordRerank(matches, query, limit) {
5411
+ const keywords = query.toLowerCase().replace(/[^\w\s]/g, "").split(/\s+/).filter((w) => w.length > 2);
5412
+ if (keywords.length === 0) {
5413
+ return matches.sort((a, b) => b.score - a.score).slice(0, limit);
5414
+ }
5415
+ const scoredMatches = matches.map((match) => {
5416
+ var _a2;
5417
+ const contentLower = ((_a2 = match == null ? void 0 : match.content) != null ? _a2 : "").toLowerCase();
5418
+ let keywordHits = 0;
5419
+ keywords.forEach((keyword) => {
5420
+ if (contentLower.includes(keyword)) {
5421
+ keywordHits++;
5422
+ }
5423
+ });
5424
+ const normalizedKeywordScore = keywordHits / keywords.length;
5425
+ return __spreadProps(__spreadValues({}, match), {
5426
+ rerankScore: match.score + normalizedKeywordScore * 0.5
5427
+ });
5428
+ });
5429
+ return scoredMatches.sort((a, b) => {
5430
+ var _a2, _b;
5431
+ return ((_a2 = b.rerankScore) != null ? _a2 : b.score) - ((_b = a.rerankScore) != null ? _b : a.score);
5432
+ }).slice(0, limit);
5433
+ }
5434
+ /**
5435
+ * LLM-based re-ranking.
5436
+ *
5437
+ * Fix RAG-1: total document content is now budgeted against
5438
+ * MAX_RERANK_CONTENT_CHARS to prevent silent truncation or failures
5439
+ * on models with small context windows.
5440
+ */
5441
+ async llmRerank(matches, query, limit, llm) {
5442
+ const candidates = matches.slice(0, 10);
5443
+ let charsUsed = 0;
5444
+ const budgetedCandidates = candidates.filter((m) => {
5445
+ const len = (m.content || "").length;
5446
+ if (charsUsed + len > MAX_RERANK_CONTENT_CHARS) return false;
5447
+ charsUsed += len;
5448
+ return true;
5449
+ });
5450
+ const docList = budgetedCandidates.map((m, i) => {
5451
+ var _a2;
5452
+ return `[${i}] ${((_a2 = m == null ? void 0 : m.content) != null ? _a2 : "").replace(/\n/g, " ")}`;
5453
+ }).join("\n");
5454
+ const response = await llm.chat(
5455
+ [{ role: "user", content: `Query: "${query}"
5411
5456
 
5412
5457
  Documents:
5413
- ${topN.map((m, i) => {
5414
- var _a3;
5415
- return `[${i}] ${((_a3 = m == null ? void 0 : m.content) != null ? _a3 : "").replace(/\n/g, " ")}`;
5416
- }).join("\n")}` }],
5417
- "",
5418
- {
5419
- 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.',
5420
- temperature: 0,
5421
- maxTokens: 50
5422
- }
5423
- );
5424
- const cleanedResponse = response.trim().replace(/[\[\]\s]/g, "");
5425
- const rankedIndices = cleanedResponse.split(",").map(Number).filter((n) => !isNaN(n));
5426
- if (rankedIndices.length > 0) {
5427
- const rankedMatches = [];
5428
- const usedIndices = /* @__PURE__ */ new Set();
5429
- for (const index of rankedIndices) {
5430
- if (index >= 0 && index < topN.length && !usedIndices.has(index)) {
5431
- rankedMatches.push(topN[index]);
5432
- usedIndices.add(index);
5433
- }
5434
- }
5435
- for (let i = 0; i < topN.length; i++) {
5436
- if (!usedIndices.has(i)) {
5437
- rankedMatches.push(topN[i]);
5438
- }
5439
- }
5440
- const rest = matches.slice(10);
5441
- rankedMatches.push(...rest);
5442
- return rankedMatches.slice(0, limit);
5458
+ ${docList}` }],
5459
+ "",
5460
+ {
5461
+ 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.',
5462
+ temperature: 0,
5463
+ maxTokens: 50
5443
5464
  }
5444
- } catch (error) {
5445
- console.warn("[Reranker] LLM re-ranking failed, falling back to score-based sorting:", error);
5465
+ );
5466
+ const cleanedResponse = response.trim().replace(/[\[\]\s]/g, "");
5467
+ const rankedIndices = cleanedResponse.split(",").map(Number).filter((n) => !isNaN(n));
5468
+ if (rankedIndices.length === 0) {
5469
+ return matches.sort((a, b) => b.score - a.score).slice(0, limit);
5446
5470
  }
5447
- return matches.sort((a, b) => b.score - a.score).slice(0, limit);
5471
+ const rankedMatches = [];
5472
+ const usedIndices = /* @__PURE__ */ new Set();
5473
+ for (const index of rankedIndices) {
5474
+ if (index >= 0 && index < budgetedCandidates.length && !usedIndices.has(index)) {
5475
+ rankedMatches.push(budgetedCandidates[index]);
5476
+ usedIndices.add(index);
5477
+ }
5478
+ }
5479
+ for (let i = 0; i < budgetedCandidates.length; i++) {
5480
+ if (!usedIndices.has(i)) rankedMatches.push(budgetedCandidates[i]);
5481
+ }
5482
+ rankedMatches.push(...matches.slice(10));
5483
+ return rankedMatches.slice(0, limit);
5448
5484
  }
5449
5485
  };
5450
5486
 
@@ -5456,11 +5492,11 @@ var LlamaIndexIngestor = class {
5456
5492
  * than standard character-count splitting.
5457
5493
  */
5458
5494
  async chunk(text, options = {}) {
5459
- var _a3, _b;
5495
+ var _a2, _b;
5460
5496
  try {
5461
5497
  const { Document, SentenceSplitter, MetadataMode } = await import(`${"llamaindex"}`);
5462
5498
  const splitter = new SentenceSplitter({
5463
- chunkSize: (_a3 = options.chunkSize) != null ? _a3 : 1e3,
5499
+ chunkSize: (_a2 = options.chunkSize) != null ? _a2 : 1e3,
5464
5500
  chunkOverlap: (_b = options.chunkOverlap) != null ? _b : 200
5465
5501
  });
5466
5502
  const doc = new Document({ text, metadata: options.metadata || {} });
@@ -5568,7 +5604,7 @@ ${error instanceof Error ? error.message : String(error)}`
5568
5604
  * The agent returns `{ messages: [...] }` — the last message is the final answer.
5569
5605
  */
5570
5606
  async run(input, chatHistory = []) {
5571
- var _a3, _b, _c;
5607
+ var _a2, _b, _c;
5572
5608
  if (!this.agent) {
5573
5609
  throw new Error("[LangChainAgent] Agent not initialized. Call initialize() first.");
5574
5610
  }
@@ -5579,7 +5615,7 @@ ${error instanceof Error ? error.message : String(error)}`
5579
5615
  const response = await this.agent.invoke({
5580
5616
  messages: [...historyMessages, new HumanMessage(input)]
5581
5617
  });
5582
- const lastMessage = (_a3 = response == null ? void 0 : response.messages) == null ? void 0 : _a3.at(-1);
5618
+ const lastMessage = (_a2 = response == null ? void 0 : response.messages) == null ? void 0 : _a2.at(-1);
5583
5619
  if (lastMessage && typeof lastMessage.content === "string") {
5584
5620
  return lastMessage.content;
5585
5621
  }
@@ -5630,7 +5666,7 @@ var MCPClient = class {
5630
5666
  }
5631
5667
  }
5632
5668
  async connectStdio() {
5633
- var _a3;
5669
+ var _a2;
5634
5670
  const cmd = this.config.command;
5635
5671
  if (!cmd) {
5636
5672
  throw new Error(`[MCPClient] Command option is required for stdio transport on "${this.config.name}"`);
@@ -5645,7 +5681,7 @@ var MCPClient = class {
5645
5681
  this.stdioBuffer += data.toString("utf-8");
5646
5682
  this.processStdioLines();
5647
5683
  });
5648
- (_a3 = this.childProcess.stderr) == null ? void 0 : _a3.on("data", (data) => {
5684
+ (_a2 = this.childProcess.stderr) == null ? void 0 : _a2.on("data", (data) => {
5649
5685
  console.warn(`[MCPClient] [stderr] [${this.config.name}]:`, data.toString("utf-8"));
5650
5686
  });
5651
5687
  this.childProcess.on("close", (code) => {
@@ -5719,14 +5755,14 @@ var MCPClient = class {
5719
5755
  }
5720
5756
  }
5721
5757
  async sendNotification(method, params) {
5722
- var _a3;
5758
+ var _a2;
5723
5759
  const notification = {
5724
5760
  jsonrpc: "2.0",
5725
5761
  method,
5726
5762
  params
5727
5763
  };
5728
5764
  if (this.config.transport === "stdio") {
5729
- if ((_a3 = this.childProcess) == null ? void 0 : _a3.stdin) {
5765
+ if ((_a2 = this.childProcess) == null ? void 0 : _a2.stdin) {
5730
5766
  this.childProcess.stdin.write(JSON.stringify(notification) + "\n", "utf-8");
5731
5767
  }
5732
5768
  } else if (this.sseUrl) {
@@ -5804,11 +5840,11 @@ var MCPRegistry = class {
5804
5840
  // src/core/MultiAgentCoordinator.ts
5805
5841
  var MultiAgentCoordinator = class {
5806
5842
  constructor(options) {
5807
- var _a3;
5843
+ var _a2;
5808
5844
  this.llmProvider = options.llmProvider;
5809
5845
  this.mcpRegistry = options.mcpRegistry;
5810
5846
  this.documentSearch = options.documentSearch;
5811
- this.maxIterations = (_a3 = options.maxIterations) != null ? _a3 : 5;
5847
+ this.maxIterations = (_a2 = options.maxIterations) != null ? _a2 : 5;
5812
5848
  }
5813
5849
  /**
5814
5850
  * Run the multi-agent coordination loop synchronously and return the final response.
@@ -5860,8 +5896,8 @@ Available Tools:
5860
5896
 
5861
5897
  ${mcpTools.length > 0 ? "MCP Server Tools:" : ""}
5862
5898
  ${mcpTools.map((mt) => {
5863
- var _a3;
5864
- return `- ${mt.tool.name}(${JSON.stringify(((_a3 = mt.tool.inputSchema) == null ? void 0 : _a3.properties) || {})}): ${mt.tool.description || "No description provided."}`;
5899
+ var _a2;
5900
+ return `- ${mt.tool.name}(${JSON.stringify(((_a2 = mt.tool.inputSchema) == null ? void 0 : _a2.properties) || {})}): ${mt.tool.description || "No description provided."}`;
5865
5901
  }).join("\n")}
5866
5902
 
5867
5903
  Tool Calling Protocol:
@@ -6019,100 +6055,6 @@ ${toolResultText}`
6019
6055
  }
6020
6056
  };
6021
6057
 
6022
- // src/core/CircuitBreaker.ts
6023
- var CircuitBreaker = class {
6024
- constructor(options = {}) {
6025
- this.state = "closed";
6026
- this.failureCount = 0;
6027
- this.successCount = 0;
6028
- this.lastFailureAt = 0;
6029
- this.halfOpenCalls = 0;
6030
- var _a3, _b, _c;
6031
- this.failureThreshold = (_a3 = options.failureThreshold) != null ? _a3 : 5;
6032
- this.resetTimeoutMs = (_b = options.resetTimeoutMs) != null ? _b : 3e4;
6033
- this.halfOpenMaxCalls = (_c = options.halfOpenMaxCalls) != null ? _c : 1;
6034
- }
6035
- record(success) {
6036
- const now = Date.now();
6037
- if (success) {
6038
- this.successCount++;
6039
- if (this.state === "half-open") {
6040
- this.state = "closed";
6041
- this.failureCount = 0;
6042
- this.halfOpenCalls = 0;
6043
- this.successCount = 1;
6044
- } else if (this.state === "closed") {
6045
- if (this.successCount >= this.failureThreshold) {
6046
- this.failureCount = Math.max(0, this.failureCount - 1);
6047
- this.successCount = 0;
6048
- }
6049
- }
6050
- } else {
6051
- this.failureCount++;
6052
- this.lastFailureAt = now;
6053
- this.successCount = 0;
6054
- if (this.state === "half-open") {
6055
- this.state = "open";
6056
- this.halfOpenCalls = 0;
6057
- } else if (this.state === "closed" && this.failureCount >= this.failureThreshold) {
6058
- this.state = "open";
6059
- }
6060
- }
6061
- }
6062
- canExecute() {
6063
- const now = Date.now();
6064
- if (this.state === "closed") {
6065
- return true;
6066
- }
6067
- if (this.state === "open") {
6068
- if (now - this.lastFailureAt >= this.resetTimeoutMs) {
6069
- this.state = "half-open";
6070
- this.halfOpenCalls = 0;
6071
- return true;
6072
- }
6073
- return false;
6074
- }
6075
- if (this.state === "half-open") {
6076
- return this.halfOpenCalls < this.halfOpenMaxCalls;
6077
- }
6078
- return true;
6079
- }
6080
- async wrap(fn) {
6081
- if (!this.canExecute()) {
6082
- const retryAfterSec = Math.ceil(
6083
- Math.max(0, this.resetTimeoutMs - (Date.now() - this.lastFailureAt)) / 1e3
6084
- );
6085
- throw new Error(
6086
- `Circuit breaker is open. Try again in ${retryAfterSec}s. Failed ${this.failureCount}/${this.failureThreshold} consecutive calls.`
6087
- );
6088
- }
6089
- if (this.state === "half-open") {
6090
- this.halfOpenCalls++;
6091
- }
6092
- try {
6093
- const result = await fn();
6094
- this.record(true);
6095
- return result;
6096
- } catch (err) {
6097
- this.record(false);
6098
- throw err;
6099
- }
6100
- }
6101
- getState() {
6102
- return this.state;
6103
- }
6104
- getFailureCount() {
6105
- return this.failureCount;
6106
- }
6107
- reset() {
6108
- this.state = "closed";
6109
- this.failureCount = 0;
6110
- this.successCount = 0;
6111
- this.lastFailureAt = 0;
6112
- this.halfOpenCalls = 0;
6113
- }
6114
- };
6115
-
6116
6058
  // src/core/BatchProcessor.ts
6117
6059
  function isTransientError(error) {
6118
6060
  if (!(error instanceof Error)) return false;
@@ -6136,30 +6078,34 @@ function calculateBackoffDelay(attempt, initialDelayMs, maxDelayMs, multiplier)
6136
6078
  function sleep(ms) {
6137
6079
  return new Promise((resolve) => setTimeout(resolve, ms));
6138
6080
  }
6139
- var _BatchProcessor = class _BatchProcessor {
6081
+ var BatchProcessor = class _BatchProcessor {
6140
6082
  /**
6141
- * Execute a processor call through the circuit breaker.
6142
- * Only transient failures count toward opening the circuit.
6083
+ * Execute a processor call, optionally through a caller-provided circuit
6084
+ * breaker. Only transient failures count toward opening the circuit.
6085
+ *
6086
+ * Fix ARCH-3: removed the static module-level `cb` field. Callers must
6087
+ * supply their own CircuitBreaker via BatchOptions.circuitBreaker.
6088
+ * Fix TYPE-1: uses the public retryAfterMs() and incrementHalfOpen()
6089
+ * API instead of accessing private fields via `as any`.
6143
6090
  */
6144
- static async executeWithCircuitBreaker(processor) {
6145
- if (!this.cb.canExecute()) {
6146
- const retryAfterSec = Math.ceil(
6147
- Math.max(0, 3e4 - (Date.now() - this.cb.lastFailureAt || 0)) / 1e3
6148
- );
6091
+ static async executeWithCircuitBreaker(processor, cb) {
6092
+ if (!cb) {
6093
+ return processor();
6094
+ }
6095
+ if (!cb.canExecute()) {
6096
+ const retryAfterSec = Math.ceil(cb.retryAfterMs() / 1e3);
6149
6097
  throw new Error(
6150
6098
  `[BatchProcessor] Circuit breaker is open. Try again in ${retryAfterSec}s.`
6151
6099
  );
6152
6100
  }
6153
- if (this.cb.state === "half-open") {
6154
- this.cb.halfOpenCalls++;
6155
- }
6101
+ cb.incrementHalfOpen();
6156
6102
  try {
6157
6103
  const result = await processor();
6158
- this.cb.record(true);
6104
+ cb.record(true);
6159
6105
  return result;
6160
6106
  } catch (err) {
6161
6107
  if (isTransientError(err)) {
6162
- this.cb.record(false);
6108
+ cb.record(false);
6163
6109
  }
6164
6110
  throw err;
6165
6111
  }
@@ -6204,7 +6150,10 @@ var _BatchProcessor = class _BatchProcessor {
6204
6150
  let lastError;
6205
6151
  for (let attempt = 0; attempt <= maxRetries; attempt++) {
6206
6152
  try {
6207
- const result = await _BatchProcessor.executeWithCircuitBreaker(() => processor(batch));
6153
+ const result = await _BatchProcessor.executeWithCircuitBreaker(
6154
+ () => processor(batch),
6155
+ options == null ? void 0 : options.circuitBreaker
6156
+ );
6208
6157
  results.push(result);
6209
6158
  success = true;
6210
6159
  break;
@@ -6262,7 +6211,10 @@ ${errorMessages}`
6262
6211
  let lastError;
6263
6212
  for (let attempt = 0; attempt <= maxRetries; attempt++) {
6264
6213
  try {
6265
- const result = await _BatchProcessor.executeWithCircuitBreaker(() => processor(item));
6214
+ const result = await _BatchProcessor.executeWithCircuitBreaker(
6215
+ () => processor(item),
6216
+ options == null ? void 0 : options.circuitBreaker
6217
+ );
6266
6218
  results.push(result);
6267
6219
  success = true;
6268
6220
  break;
@@ -6335,7 +6287,10 @@ ${errorMessages}`
6335
6287
  }));
6336
6288
  const chunkPromises = chunk.map(async ({ item, originalIndex }) => {
6337
6289
  try {
6338
- const result = await _BatchProcessor.executeWithCircuitBreaker(() => processor(item));
6290
+ const result = await _BatchProcessor.executeWithCircuitBreaker(
6291
+ () => processor(item),
6292
+ options == null ? void 0 : options.circuitBreaker
6293
+ );
6339
6294
  return { success: true, result, index: originalIndex };
6340
6295
  } catch (err) {
6341
6296
  const error = err instanceof Error ? err : new Error(String(err));
@@ -6363,12 +6318,6 @@ ${errorMessages}`
6363
6318
  return { results, errors, totalProcessed, totalFailed };
6364
6319
  }
6365
6320
  };
6366
- _BatchProcessor.cb = new CircuitBreaker({
6367
- failureThreshold: 5,
6368
- resetTimeoutMs: 3e4,
6369
- halfOpenMaxCalls: 1
6370
- });
6371
- var BatchProcessor = _BatchProcessor;
6372
6321
 
6373
6322
  // src/config/EmbeddingStrategy.ts
6374
6323
  var EmbeddingStrategy = /* @__PURE__ */ ((EmbeddingStrategy2) => {
@@ -6472,7 +6421,7 @@ var QueryProcessor = class {
6472
6421
  * @param validFields Optional list of known filterable fields to look for
6473
6422
  */
6474
6423
  static extractQueryFieldHints(question, validFields = []) {
6475
- var _a3, _b, _c, _d;
6424
+ var _a2, _b, _c, _d, _e, _f, _g2, _h;
6476
6425
  if (!question.trim()) return [];
6477
6426
  const hints = /* @__PURE__ */ new Map();
6478
6427
  const addHint = (value, field) => {
@@ -6517,11 +6466,11 @@ var QueryProcessor = class {
6517
6466
  if (name) addHint(name, "name");
6518
6467
  }
6519
6468
  }
6520
- const genericPattern = /\b([a-z][a-z0-9_]{1,30})\s*[:=]\s*["']?([^"'\n?.!,]{1,60})["']?/gi;
6469
+ const genericPattern = /\b([a-z][a-z0-9_]{1,30})\s*[:=]\s*(?:"([^"\n?.!,]{1,60})"|'([^'\n?.!,]{1,60})'|([^\s\n?.!,:=]{1,60}))/gi;
6521
6470
  for (const match of question.matchAll(genericPattern)) {
6522
6471
  const field = match[1];
6523
- const value = match[2];
6524
- if (field && !this.isLikelyPromptPhrase(field)) {
6472
+ const value = (_b = (_a2 = match[2]) != null ? _a2 : match[3]) != null ? _b : match[4];
6473
+ if (field && value && !this.isLikelyPromptPhrase(field)) {
6525
6474
  const resolvedField = resolveValidField(field);
6526
6475
  if (resolvedField) {
6527
6476
  addHint(value, resolvedField);
@@ -6552,7 +6501,7 @@ var QueryProcessor = class {
6552
6501
  ];
6553
6502
  for (const p of universalPatterns) {
6554
6503
  for (const match of question.matchAll(p.regex)) {
6555
- const val = p.group ? (_a3 = match[p.group]) != null ? _a3 : match[0] : match[0];
6504
+ const val = p.group ? (_c = match[p.group]) != null ? _c : match[0] : match[0];
6556
6505
  if (!val) continue;
6557
6506
  if (p.field) addHint(val, p.field);
6558
6507
  else addHint(val);
@@ -6560,22 +6509,23 @@ var QueryProcessor = class {
6560
6509
  }
6561
6510
  for (const pattern of naturalQuestionPatterns) {
6562
6511
  for (const match of question.matchAll(pattern)) {
6563
- const value = (_b = match[2]) != null ? _b : match[1];
6512
+ const value = (_d = match[2]) != null ? _d : match[1];
6564
6513
  if (value) addHint(value);
6565
6514
  }
6566
6515
  }
6567
6516
  const fieldPattern = `([^\\n:=?.!,]{1,60}?)`;
6568
- const valuePattern = `([^\\n?.!,]{1,120}?)`;
6517
+ const valuePattern = `(?:"([^"\\n?.!,]{1,120})"|'([^'\\n?.!,]{1,120})'|([^\\s\\n?.!,:=]{1,120}))`;
6569
6518
  const fieldValuePatterns = [
6570
- new RegExp(`\\b${fieldPattern}\\s*(?:=|:)\\s*["']?${valuePattern}["']?(?=[?.!,]|$)`, "gi"),
6571
- new RegExp(`\\b${fieldPattern}\\s+(?:is|are|was|were|equals?|equal to|named|called)\\s+["']?${valuePattern}["']?(?=[?.!,]|$)`, "gi"),
6572
- new RegExp(`\\bwith\\s+${fieldPattern}\\s+["']?${valuePattern}["']?(?=[?.!,]|$)`, "gi"),
6573
- new RegExp(`\\b(?:under|in|for|from|of)\\s+${fieldPattern}\\s+["']?${valuePattern}["']?(?=[?.!,]|$)`, "gi")
6519
+ new RegExp(`\\b${fieldPattern}\\s*(?:=|:)\\s*${valuePattern}`, "gi"),
6520
+ new RegExp(`\\b${fieldPattern}\\s+(?:is|are|was|were|equals?|equal to|named|called)\\s+${valuePattern}`, "gi"),
6521
+ new RegExp(`\\bwith\\s+${fieldPattern}\\s+${valuePattern}`, "gi"),
6522
+ new RegExp(`\\b(?:under|in|for|from|of)\\s+${fieldPattern}\\s+${valuePattern}`, "gi")
6574
6523
  ];
6575
6524
  for (const pattern of fieldValuePatterns) {
6576
6525
  for (const match of question.matchAll(pattern)) {
6577
- const field = (_c = match[1]) != null ? _c : "";
6578
- const value = (_d = match[2]) != null ? _d : "";
6526
+ const field = (_e = match[1]) != null ? _e : "";
6527
+ const value = (_h = (_g2 = (_f = match[2]) != null ? _f : match[3]) != null ? _g2 : match[4]) != null ? _h : "";
6528
+ if (!value) continue;
6579
6529
  const resolvedField = resolveValidField(field);
6580
6530
  if (resolvedField) {
6581
6531
  addHint(value, resolvedField);
@@ -6645,10 +6595,10 @@ var QueryProcessor = class {
6645
6595
  }
6646
6596
  static normalizeNumericOperator(operator) {
6647
6597
  const op = operator.toLowerCase().trim();
6648
- if (op === ">" || /\b(greater than|more than|above|over)\b/.test(op)) return "gt";
6649
6598
  if (op === ">=" || /\b(greater than or equal to|more than or equal to|at least)\b/.test(op)) return "gte";
6650
- if (op === "<" || /\b(less than|below|under)\b/.test(op)) return "lt";
6651
6599
  if (op === "<=" || /\b(less than or equal to|at most)\b/.test(op)) return "lte";
6600
+ if (op === ">" || /\b(greater than|more than|above|over)\b/.test(op)) return "gt";
6601
+ if (op === "<" || /\b(less than|below|under)\b/.test(op)) return "lt";
6652
6602
  return "eq";
6653
6603
  }
6654
6604
  static normalizePredicateField(field, validFields) {
@@ -6776,11 +6726,11 @@ var LLMRouter = class {
6776
6726
  * When provided it is used directly as the 'default' role without re-constructing.
6777
6727
  */
6778
6728
  async initialize(prebuiltDefault) {
6779
- var _a3;
6729
+ var _a2;
6780
6730
  const defaultModel = prebuiltDefault != null ? prebuiltDefault : LLMFactory.create(this.config.llm, this.config.embedding);
6781
6731
  this.models.set("default", defaultModel);
6782
6732
  const envFastModel = process.env.FAST_LLM_MODEL;
6783
- const providerFastDefault = (_a3 = FAST_MODEL_DEFAULTS[this.config.llm.provider]) != null ? _a3 : "";
6733
+ const providerFastDefault = (_a2 = FAST_MODEL_DEFAULTS[this.config.llm.provider]) != null ? _a2 : "";
6784
6734
  const fastModelName = envFastModel || providerFastDefault;
6785
6735
  if (fastModelName && fastModelName !== this.config.llm.model) {
6786
6736
  const fastConfig = __spreadProps(__spreadValues({}, this.config.llm), {
@@ -6797,8 +6747,8 @@ var LLMRouter = class {
6797
6747
  * Falls back to 'default' if the requested role is not registered.
6798
6748
  */
6799
6749
  get(role) {
6800
- var _a3;
6801
- const provider = (_a3 = this.models.get(role)) != null ? _a3 : this.models.get("default");
6750
+ var _a2;
6751
+ const provider = (_a2 = this.models.get(role)) != null ? _a2 : this.models.get("default");
6802
6752
  if (!provider) {
6803
6753
  throw new Error(`[LLMRouter] No provider registered for role: "${role}". Did you call initialize()?`);
6804
6754
  }
@@ -6884,8 +6834,8 @@ var IntentClassifier = class {
6884
6834
  numericFieldCount = numericKeys.size;
6885
6835
  categoricalFieldCount = catKeys.size;
6886
6836
  if (productKeyMatches >= 2 || docs.some((d) => {
6887
- var _a3, _b;
6888
- 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");
6837
+ var _a2, _b;
6838
+ 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");
6889
6839
  })) {
6890
6840
  isProductLike = true;
6891
6841
  }
@@ -7075,7 +7025,7 @@ var Rule6SmallResultSetRule = class {
7075
7025
  var Rule7LargeTableRule = class {
7076
7026
  constructor() {
7077
7027
  this.name = "Rule7LargeTable";
7078
- this.priority = 75;
7028
+ this.priority = 82;
7079
7029
  }
7080
7030
  evaluate(context, intent) {
7081
7031
  const docs = context.retrievedDocuments || [];
@@ -7118,8 +7068,8 @@ var RuleEngine = class {
7118
7068
  this.registerRule(new Rule5MixedResponseRule());
7119
7069
  this.registerRule(new Rule2ComparisonRule());
7120
7070
  this.registerRule(new Rule3ProductDiscoveryRule());
7121
- this.registerRule(new Rule4AnalyticalRule());
7122
7071
  this.registerRule(new Rule7LargeTableRule());
7072
+ this.registerRule(new Rule4AnalyticalRule());
7123
7073
  }
7124
7074
  /**
7125
7075
  * Add or replace a rule in the engine.
@@ -7167,8 +7117,8 @@ var TextRendererStrategy = class {
7167
7117
  }
7168
7118
  const docs = Array.isArray(data) ? data : [];
7169
7119
  const text = docs.map((d) => {
7170
- var _a3;
7171
- return (_a3 = d == null ? void 0 : d.content) != null ? _a3 : "";
7120
+ var _a2;
7121
+ return (_a2 = d == null ? void 0 : d.content) != null ? _a2 : "";
7172
7122
  }).join("\n\n");
7173
7123
  return { type: "text", title, data: { content: text || "No detailed content available." } };
7174
7124
  }
@@ -7311,9 +7261,9 @@ var LRUDecisionCache = class {
7311
7261
  this.maxSize = maxSize;
7312
7262
  }
7313
7263
  generateKey(context) {
7314
- var _a3, _b;
7264
+ var _a2, _b;
7315
7265
  const q = (context.userQuery || "").toLowerCase().trim();
7316
- const docCount = (_b = (_a3 = context.retrievedDocuments) == null ? void 0 : _a3.length) != null ? _b : 0;
7266
+ const docCount = (_b = (_a2 = context.retrievedDocuments) == null ? void 0 : _a2.length) != null ? _b : 0;
7317
7267
  return `${q}::docs:${docCount}`;
7318
7268
  }
7319
7269
  get(context) {
@@ -7422,7 +7372,7 @@ var UITransformer = class _UITransformer {
7422
7372
  * Prefer `analyzeAndDecide()` in production.
7423
7373
  */
7424
7374
  static transform(userQuery, retrievedData, config, trainedSchema, intent) {
7425
- var _a3, _b, _c;
7375
+ var _a2, _b, _c;
7426
7376
  if (!retrievedData || retrievedData.length === 0) {
7427
7377
  return this.createTextResponse("No data available", "No relevant data found for your query.");
7428
7378
  }
@@ -7442,7 +7392,7 @@ var UITransformer = class _UITransformer {
7442
7392
  return this.transformToBarChart(filteredData, profile, userQuery, resolvedIntent.visualizationHint === "ranking");
7443
7393
  }
7444
7394
  if (resolvedIntent.visualizationHint === "distribution") {
7445
- return (_a3 = this.transformToHistogram(profile, userQuery)) != null ? _a3 : this.transformToBarChart(filteredData, profile, userQuery);
7395
+ return (_a2 = this.transformToHistogram(profile, userQuery)) != null ? _a2 : this.transformToBarChart(filteredData, profile, userQuery);
7446
7396
  }
7447
7397
  if (resolvedIntent.visualizationHint === "correlation") {
7448
7398
  return (_b = this.transformToScatterPlot(profile, userQuery)) != null ? _b : this.transformToBarChart(filteredData, profile, userQuery);
@@ -7775,11 +7725,11 @@ ${schemaProfileText}` : ""}`;
7775
7725
  };
7776
7726
  }
7777
7727
  static transformToPieChart(data, profile, query = "") {
7778
- var _a3;
7728
+ var _a2;
7779
7729
  const dimension = profile ? this.selectDimensionField(profile, query) : void 0;
7780
7730
  const categories = dimension ? Array.from(new Set(profile.records.map((record) => {
7781
- var _a4;
7782
- return String((_a4 = record.fields[dimension.key]) != null ? _a4 : "");
7731
+ var _a3;
7732
+ return String((_a3 = record.fields[dimension.key]) != null ? _a3 : "");
7783
7733
  }).filter(Boolean))) : this.detectCategories(data);
7784
7734
  if (categories.length === 0) return null;
7785
7735
  const categoryData = dimension && profile ? this.aggregateProfileByDimension(profile, dimension.key) : this.aggregateByCategory(data, categories);
@@ -7789,7 +7739,7 @@ ${schemaProfileText}` : ""}`;
7789
7739
  });
7790
7740
  return {
7791
7741
  type: "pie_chart",
7792
- title: `Distribution by ${(_a3 = dimension == null ? void 0 : dimension.label) != null ? _a3 : "Category"}`,
7742
+ title: `Distribution by ${(_a2 = dimension == null ? void 0 : dimension.label) != null ? _a2 : "Category"}`,
7793
7743
  description: `Showing breakdown across ${pieData.length} categories`,
7794
7744
  data: pieData
7795
7745
  };
@@ -7799,8 +7749,8 @@ ${schemaProfileText}` : ""}`;
7799
7749
  const valueField = profile.numericFields[0];
7800
7750
  const buckets = /* @__PURE__ */ new Map();
7801
7751
  profile.records.forEach((record) => {
7802
- var _a3, _b, _c;
7803
- const timestamp = String((_a3 = record.fields[dateField.key]) != null ? _a3 : "");
7752
+ var _a2, _b, _c;
7753
+ const timestamp = String((_a2 = record.fields[dateField.key]) != null ? _a2 : "");
7804
7754
  const value = (_b = this.toFiniteNumber(record.fields[valueField.key])) != null ? _b : 0;
7805
7755
  buckets.set(timestamp, ((_c = buckets.get(timestamp)) != null ? _c : 0) + value);
7806
7756
  });
@@ -7813,23 +7763,23 @@ ${schemaProfileText}` : ""}`;
7813
7763
  };
7814
7764
  }
7815
7765
  static transformToBarChart(data, profile, query = "", horizontal = false) {
7816
- var _a3;
7766
+ var _a2;
7817
7767
  const dimension = profile ? this.selectDimensionField(profile, query) : void 0;
7818
7768
  const measure = profile ? this.selectNumericField(profile, query) : void 0;
7819
7769
  const aggregate = dimension && profile ? this.aggregateProfileByDimension(profile, dimension.key, measure == null ? void 0 : measure.key) : this.aggregateByCategory(data, this.detectCategories(data));
7820
7770
  const barData = Object.entries(aggregate).map(([category, value]) => ({ category, value: Number(value) })).sort((a, b) => horizontal ? b.value - a.value : 0).slice(0, 12);
7821
7771
  const fallbackData = barData.length > 0 ? barData : data.slice(0, 12).map((item, index) => {
7822
- var _a4, _b, _c, _d, _e;
7772
+ var _a3, _b, _c, _d, _e;
7823
7773
  const meta = item.metadata || {};
7824
7774
  const label = String(
7825
- (_c = (_b = (_a4 = this.getDynamicVal(meta, "name")) != null ? _a4 : meta.label) != null ? _b : meta.title) != null ? _c : `Result ${index + 1}`
7775
+ (_c = (_b = (_a3 = this.getDynamicVal(meta, "name")) != null ? _a3 : meta.label) != null ? _b : meta.title) != null ? _c : `Result ${index + 1}`
7826
7776
  );
7827
7777
  const value = (_e = (_d = this.extractNumericValue(meta)) != null ? _d : item.score) != null ? _e : 0;
7828
7778
  return { category: label, value: Number(value) };
7829
7779
  });
7830
7780
  return {
7831
7781
  type: horizontal ? "horizontal_bar" : "bar_chart",
7832
- title: dimension ? `${(_a3 = measure == null ? void 0 : measure.label) != null ? _a3 : "Count"} by ${dimension.label}` : "Comparison",
7782
+ title: dimension ? `${(_a2 = measure == null ? void 0 : measure.label) != null ? _a2 : "Count"} by ${dimension.label}` : "Comparison",
7833
7783
  description: `Showing ${fallbackData.length} comparable values`,
7834
7784
  data: fallbackData
7835
7785
  };
@@ -7864,11 +7814,11 @@ ${schemaProfileText}` : ""}`;
7864
7814
  if (fields.length < 2) return null;
7865
7815
  const [xField, yField] = fields;
7866
7816
  const points = profile.records.map((record) => {
7867
- var _a3;
7817
+ var _a2;
7868
7818
  const x = this.toFiniteNumber(record.fields[xField.key]);
7869
7819
  const y = this.toFiniteNumber(record.fields[yField.key]);
7870
7820
  if (x === null || y === null) return null;
7871
- return { x, y, label: String((_a3 = this.getRecordLabel(record)) != null ? _a3 : record.id) };
7821
+ return { x, y, label: String((_a2 = this.getRecordLabel(record)) != null ? _a2 : record.id) };
7872
7822
  }).filter((point) => point !== null).slice(0, 100);
7873
7823
  if (points.length === 0) return null;
7874
7824
  return {
@@ -7898,9 +7848,9 @@ ${schemaProfileText}` : ""}`;
7898
7848
  static transformToRadarChart(data) {
7899
7849
  const attributeMap = {};
7900
7850
  data.forEach((item) => {
7901
- var _a3, _b, _c;
7851
+ var _a2, _b, _c;
7902
7852
  const meta = item.metadata || {};
7903
- const seriesName = String((_c = (_b = (_a3 = meta.name) != null ? _a3 : meta.product) != null ? _b : item.id) != null ? _c : "Item");
7853
+ const seriesName = String((_c = (_b = (_a2 = meta.name) != null ? _a2 : meta.product) != null ? _b : item.id) != null ? _c : "Item");
7904
7854
  Object.entries(meta).forEach(([key, val]) => {
7905
7855
  if (typeof val === "number" && !["price", "id"].includes(key.toLowerCase())) {
7906
7856
  if (!attributeMap[key]) attributeMap[key] = {};
@@ -7916,8 +7866,8 @@ ${schemaProfileText}` : ""}`;
7916
7866
  title: "Product Comparison",
7917
7867
  description: `Comparing ${data.length} items across ${radarData.length} attributes`,
7918
7868
  data: radarData.length > 0 ? radarData : data.map((d) => {
7919
- var _a3;
7920
- return { attribute: ((_a3 = d == null ? void 0 : d.content) != null ? _a3 : "").substring(0, 40) };
7869
+ var _a2;
7870
+ return { attribute: ((_a2 = d == null ? void 0 : d.content) != null ? _a2 : "").substring(0, 40) };
7921
7871
  })
7922
7872
  };
7923
7873
  }
@@ -7936,8 +7886,8 @@ ${schemaProfileText}` : ""}`;
7936
7886
  return this.createTextResponse(
7937
7887
  "Retrieved Context",
7938
7888
  data.map((item) => {
7939
- var _a3;
7940
- return (_a3 = item == null ? void 0 : item.content) != null ? _a3 : "";
7889
+ var _a2;
7890
+ return (_a2 = item == null ? void 0 : item.content) != null ? _a2 : "";
7941
7891
  }).join("\n\n"),
7942
7892
  `Found ${data.length} relevant results`
7943
7893
  );
@@ -7988,11 +7938,11 @@ ${schemaProfileText}` : ""}`;
7988
7938
  return null;
7989
7939
  }
7990
7940
  static normalizeTransformation(payload) {
7991
- var _a3, _b, _c, _d, _e, _f, _g2, _h, _i, _j;
7941
+ var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j;
7992
7942
  if (!payload || typeof payload !== "object") return null;
7993
7943
  const p = payload;
7994
7944
  const type = this.normalizeVisualizationType(
7995
- String((_c = (_b = (_a3 = p.type) != null ? _a3 : p.view) != null ? _b : p.chartType) != null ? _c : "")
7945
+ String((_c = (_b = (_a2 = p.type) != null ? _a2 : p.view) != null ? _b : p.chartType) != null ? _c : "")
7996
7946
  );
7997
7947
  if (!type) return null;
7998
7948
  const title = String((_e = (_d = p.title) != null ? _d : p.heading) != null ? _e : "Visualization");
@@ -8003,7 +7953,7 @@ ${schemaProfileText}` : ""}`;
8003
7953
  return this.validateTransformation(transformation) ? transformation : null;
8004
7954
  }
8005
7955
  static normalizeVisualizationType(type) {
8006
- var _a3;
7956
+ var _a2;
8007
7957
  const mapping = {
8008
7958
  pie: "pie_chart",
8009
7959
  pie_chart: "pie_chart",
@@ -8031,7 +7981,7 @@ ${schemaProfileText}` : ""}`;
8031
7981
  product_carousel: "product_carousel",
8032
7982
  carousel: "carousel"
8033
7983
  };
8034
- return (_a3 = mapping[type.toLowerCase()]) != null ? _a3 : null;
7984
+ return (_a2 = mapping[type.toLowerCase()]) != null ? _a2 : null;
8035
7985
  }
8036
7986
  static validateTransformation(t) {
8037
7987
  const { type, data } = t;
@@ -8147,7 +8097,7 @@ ${schemaProfileText}` : ""}`;
8147
8097
  }
8148
8098
  static profileData(data) {
8149
8099
  const records = (data || []).filter((item) => Boolean(item && typeof item === "object")).map((item) => {
8150
- var _a3, _b;
8100
+ var _a2, _b;
8151
8101
  const fields2 = {};
8152
8102
  Object.entries(item.metadata || {}).forEach(([key, value]) => {
8153
8103
  const primitive = this.toPrimitive(value);
@@ -8156,7 +8106,7 @@ ${schemaProfileText}` : ""}`;
8156
8106
  if (!fields2.content && item.content) fields2.content = item.content.substring(0, 500);
8157
8107
  return {
8158
8108
  id: item.id,
8159
- content: (_a3 = item.content) != null ? _a3 : "",
8109
+ content: (_a2 = item.content) != null ? _a2 : "",
8160
8110
  score: (_b = item.score) != null ? _b : 0,
8161
8111
  fields: fields2,
8162
8112
  source: item
@@ -8237,16 +8187,16 @@ ${schemaProfileText}` : ""}`;
8237
8187
  return null;
8238
8188
  }
8239
8189
  static selectDimensionField(profile, query) {
8240
- var _a3, _b;
8190
+ var _a2, _b;
8241
8191
  const productCategory = profile.categoricalFields.find(
8242
8192
  (field) => /category|department|collection|type|group|segment|region|country|state|city/i.test(field.key)
8243
8193
  );
8244
8194
  const ranked = this.rankFieldsByQuery(profile.categoricalFields, query);
8245
- return (_b = (_a3 = ranked[0]) != null ? _a3 : productCategory) != null ? _b : profile.categoricalFields[0];
8195
+ return (_b = (_a2 = ranked[0]) != null ? _a2 : productCategory) != null ? _b : profile.categoricalFields[0];
8246
8196
  }
8247
8197
  static selectNumericField(profile, query) {
8248
- var _a3;
8249
- return (_a3 = this.rankFieldsByQuery(profile.numericFields, query)[0]) != null ? _a3 : profile.numericFields[0];
8198
+ var _a2;
8199
+ return (_a2 = this.rankFieldsByQuery(profile.numericFields, query)[0]) != null ? _a2 : profile.numericFields[0];
8250
8200
  }
8251
8201
  static rankFieldsByQuery(fields, query) {
8252
8202
  const q = query.toLowerCase();
@@ -8275,8 +8225,8 @@ ${schemaProfileText}` : ""}`;
8275
8225
  static aggregateProfileByDimension(profile, dimensionKey, measureKey) {
8276
8226
  const result = {};
8277
8227
  profile.records.forEach((record) => {
8278
- var _a3, _b, _c;
8279
- const category = String((_a3 = record.fields[dimensionKey]) != null ? _a3 : "Other").trim() || "Other";
8228
+ var _a2, _b, _c;
8229
+ const category = String((_a2 = record.fields[dimensionKey]) != null ? _a2 : "Other").trim() || "Other";
8280
8230
  const value = measureKey ? (_b = this.toFiniteNumber(record.fields[measureKey])) != null ? _b : 0 : 1;
8281
8231
  result[category] = ((_c = result[category]) != null ? _c : 0) + value;
8282
8232
  });
@@ -8356,8 +8306,8 @@ ${schemaProfileText}` : ""}`;
8356
8306
  static aggregateByCategory(data, categories) {
8357
8307
  const result = Object.fromEntries(categories.map((c) => [c, 0]));
8358
8308
  data.forEach((item) => {
8359
- var _a3;
8360
- const cat = (_a3 = this.getProductCategory(item)) != null ? _a3 : "Other";
8309
+ var _a2;
8310
+ const cat = (_a2 = this.getProductCategory(item)) != null ? _a2 : "Other";
8361
8311
  if (Object.prototype.hasOwnProperty.call(result, cat)) result[cat]++;
8362
8312
  else result["Other"] = (result["Other"] || 0) + 1;
8363
8313
  });
@@ -8365,17 +8315,17 @@ ${schemaProfileText}` : ""}`;
8365
8315
  }
8366
8316
  static extractTimeSeriesData(data) {
8367
8317
  return data.map((item) => {
8368
- var _a3, _b, _c, _d, _e, _f;
8318
+ var _a2, _b, _c, _d, _e, _f;
8369
8319
  const meta = item.metadata || {};
8370
8320
  return {
8371
- timestamp: (_b = (_a3 = meta.timestamp) != null ? _a3 : meta.date) != null ? _b : (/* @__PURE__ */ new Date()).toISOString(),
8321
+ timestamp: (_b = (_a2 = meta.timestamp) != null ? _a2 : meta.date) != null ? _b : (/* @__PURE__ */ new Date()).toISOString(),
8372
8322
  value: (_d = (_c = meta.value) != null ? _c : item.score) != null ? _d : 0,
8373
8323
  label: (_f = meta.label) != null ? _f : ((_e = item == null ? void 0 : item.content) != null ? _e : "").substring(0, 50)
8374
8324
  };
8375
8325
  });
8376
8326
  }
8377
8327
  static extractNumericValue(meta) {
8378
- var _a3;
8328
+ var _a2;
8379
8329
  const preferredKeys = [
8380
8330
  "value",
8381
8331
  "count",
@@ -8390,7 +8340,7 @@ ${schemaProfileText}` : ""}`;
8390
8340
  "price"
8391
8341
  ];
8392
8342
  for (const key of preferredKeys) {
8393
- const raw = (_a3 = resolveMetadataValue(meta, key)) != null ? _a3 : meta[key];
8343
+ const raw = (_a2 = resolveMetadataValue(meta, key)) != null ? _a2 : meta[key];
8394
8344
  const value = typeof raw === "number" ? raw : Number(String(raw != null ? raw : "").replace(/[$,% ,]/g, ""));
8395
8345
  if (Number.isFinite(value)) return value;
8396
8346
  }
@@ -8485,8 +8435,8 @@ ${schemaProfileText}` : ""}`;
8485
8435
  }, query.includes(normalizedField) ? 3 : 0);
8486
8436
  }
8487
8437
  static resolveTableCellValue(item, column) {
8488
- var _a3, _b, _c;
8489
- if (column === "Content") return ((_a3 = item == null ? void 0 : item.content) != null ? _a3 : "").substring(0, 100);
8438
+ var _a2, _b, _c;
8439
+ if (column === "Content") return ((_a2 = item == null ? void 0 : item.content) != null ? _a2 : "").substring(0, 100);
8490
8440
  const meta = item.metadata || {};
8491
8441
  const normalizedColumn = this.normalizeComparableField(column);
8492
8442
  const exactMetadata = Object.entries(meta).find(
@@ -8538,8 +8488,8 @@ ${schemaProfileText}` : ""}`;
8538
8488
  let inStock = 0;
8539
8489
  let outOfStock = 0;
8540
8490
  data.forEach((d) => {
8541
- var _a3, _b;
8542
- const cat = (_a3 = this.getProductCategory(d)) != null ? _a3 : "Other";
8491
+ var _a2, _b;
8492
+ const cat = (_a2 = this.getProductCategory(d)) != null ? _a2 : "Other";
8543
8493
  if (cat === category) {
8544
8494
  const quantity = (_b = this.extractStockQuantity(d)) != null ? _b : 1;
8545
8495
  if (this.determineStockStatus(d)) inStock += quantity;
@@ -8585,9 +8535,9 @@ ${schemaProfileText}` : ""}`;
8585
8535
  }
8586
8536
  // ─── Product Extraction ───────────────────────────────────────────────────
8587
8537
  static getDynamicVal(meta, uiKey, config, trainedSchema) {
8588
- var _a3;
8538
+ var _a2;
8589
8539
  if (!meta) return void 0;
8590
- const mapping = (_a3 = config == null ? void 0 : config.rag) == null ? void 0 : _a3.uiMapping;
8540
+ const mapping = (_a2 = config == null ? void 0 : config.rag) == null ? void 0 : _a2.uiMapping;
8591
8541
  if ((mapping == null ? void 0 : mapping[uiKey]) && meta[mapping[uiKey]] !== void 0) return meta[mapping[uiKey]];
8592
8542
  if (trainedSchema && typeof trainedSchema === "object") {
8593
8543
  const trainedKey = trainedSchema[uiKey];
@@ -8596,7 +8546,7 @@ ${schemaProfileText}` : ""}`;
8596
8546
  return resolveMetadataValue(meta, uiKey);
8597
8547
  }
8598
8548
  static extractProductInfo(item, config, trainedSchema) {
8599
- var _a3, _b;
8549
+ var _a2, _b;
8600
8550
  if (!item) return null;
8601
8551
  const meta = item.metadata || {};
8602
8552
  const content = item.content || "";
@@ -8604,7 +8554,7 @@ ${schemaProfileText}` : ""}`;
8604
8554
  const price = this.getDynamicVal(meta, "price", config, trainedSchema);
8605
8555
  const brand = this.getDynamicVal(meta, "brand", config, trainedSchema);
8606
8556
  const description = this.cleanProductDescription(
8607
- (_a3 = this.extractProductDescriptionFromContent(content)) != null ? _a3 : this.getProductDescriptionValue(meta, config, trainedSchema)
8557
+ (_a2 = this.extractProductDescriptionFromContent(content)) != null ? _a2 : this.getProductDescriptionValue(meta, config, trainedSchema)
8608
8558
  );
8609
8559
  if (name || this.isProductData(item)) {
8610
8560
  let finalName = name ? String(name) : void 0;
@@ -8739,10 +8689,10 @@ RULES:
8739
8689
  }
8740
8690
  static buildContextSummary(sources, maxChars = 6e3) {
8741
8691
  const items = (sources || []).filter(Boolean).map((s, i) => {
8742
- var _a3, _b, _c, _d;
8692
+ var _a2, _b, _c, _d;
8743
8693
  return {
8744
8694
  index: i + 1,
8745
- content: (_b = (_a3 = s == null ? void 0 : s.content) == null ? void 0 : _a3.substring(0, 400)) != null ? _b : "",
8695
+ content: (_b = (_a2 = s == null ? void 0 : s.content) == null ? void 0 : _a2.substring(0, 400)) != null ? _b : "",
8746
8696
  metadata: (_c = s == null ? void 0 : s.metadata) != null ? _c : {},
8747
8697
  score: (_d = s == null ? void 0 : s.score) != null ? _d : 0
8748
8698
  };
@@ -8782,7 +8732,7 @@ var SchemaMapper = class {
8782
8732
  return promise;
8783
8733
  }
8784
8734
  static async _doTrain(llm, cacheKey, keys) {
8785
- var _a3, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k;
8735
+ var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k;
8786
8736
  const propertyList = Object.entries(this.TARGET_PROPERTIES).map(([prop, desc]) => `- ${prop} (${desc})`).join("\n");
8787
8737
  const messages = [
8788
8738
  { role: "system", content: "You are a database schema expert. Respond ONLY with valid JSON." },
@@ -8797,7 +8747,7 @@ Return a JSON object like {"name":"Title","price":"Price",...}. Omit unmapped pr
8797
8747
  }
8798
8748
  ];
8799
8749
  try {
8800
- const baseUrl = (_a3 = llm == null ? void 0 : llm.baseUrl) != null ? _a3 : "";
8750
+ const baseUrl = (_a2 = llm == null ? void 0 : llm.baseUrl) != null ? _a2 : "";
8801
8751
  const apiKey = (_b = llm == null ? void 0 : llm.apiKey) != null ? _b : "";
8802
8752
  const model = (_c = llm == null ? void 0 : llm.model) != null ? _c : "llama-3.1-8b-instant";
8803
8753
  let responseText;
@@ -8972,9 +8922,9 @@ var Pipeline = class {
8972
8922
  this.initialised = false;
8973
8923
  /** Namespace-specific static cold context cache for CAG */
8974
8924
  this.coldContexts = /* @__PURE__ */ new Map();
8975
- var _a3, _b, _c, _d, _e;
8925
+ var _a2, _b, _c, _d, _e;
8976
8926
  this.chunker = new DocumentChunker(
8977
- (_b = (_a3 = config.rag) == null ? void 0 : _a3.chunkSize) != null ? _b : 1e3,
8927
+ (_b = (_a2 = config.rag) == null ? void 0 : _a2.chunkSize) != null ? _b : 1e3,
8978
8928
  (_d = (_c = config.rag) == null ? void 0 : _c.chunkOverlap) != null ? _d : 200
8979
8929
  );
8980
8930
  if (((_e = config.rag) == null ? void 0 : _e.chunkingStrategy) === "llamaindex") {
@@ -8990,7 +8940,7 @@ var Pipeline = class {
8990
8940
  return this.initialised ? this.llmProvider : void 0;
8991
8941
  }
8992
8942
  async initialize() {
8993
- var _a3, _b, _c, _d;
8943
+ var _a2, _b, _c, _d;
8994
8944
  if (this.initialised) return;
8995
8945
  const chartInstruction = `You are a helpful assistant. Use the provided context to answer questions accurately.
8996
8946
 
@@ -9033,7 +8983,7 @@ var Pipeline = class {
9033
8983
  this.entityExtractor = new EntityExtractor(this.llmProvider);
9034
8984
  }
9035
8985
  await this.vectorDB.initialize();
9036
- 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) {
8986
+ 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) {
9037
8987
  this.mcpRegistry = new MCPRegistry(this.config.mcpServers || []);
9038
8988
  this.multiAgentCoordinator = new MultiAgentCoordinator({
9039
8989
  llmProvider: this.llmProvider,
@@ -9051,8 +9001,8 @@ var Pipeline = class {
9051
9001
  this.initialised = true;
9052
9002
  }
9053
9003
  async loadColdContext(ns) {
9054
- var _a3, _b;
9055
- const cagConfig = (_a3 = this.config.rag) == null ? void 0 : _a3.cag;
9004
+ var _a2, _b;
9005
+ const cagConfig = (_a2 = this.config.rag) == null ? void 0 : _a2.cag;
9056
9006
  if (!cagConfig || !cagConfig.enabled) return;
9057
9007
  try {
9058
9008
  const coldNs = cagConfig.coldNamespace || ns;
@@ -9105,12 +9055,12 @@ ${m.content}`).join("\n\n---\n\n");
9105
9055
  }
9106
9056
  /** Step 1: Chunk the document content. */
9107
9057
  async prepareChunks(doc) {
9108
- var _a3, _b;
9058
+ var _a2, _b;
9109
9059
  if (this.llamaIngestor) {
9110
9060
  return this.llamaIngestor.chunk(doc.content, {
9111
9061
  docId: doc.docId,
9112
9062
  metadata: doc.metadata,
9113
- chunkSize: (_a3 = this.config.rag) == null ? void 0 : _a3.chunkSize,
9063
+ chunkSize: (_a2 = this.config.rag) == null ? void 0 : _a2.chunkSize,
9114
9064
  chunkOverlap: (_b = this.config.rag) == null ? void 0 : _b.chunkOverlap
9115
9065
  });
9116
9066
  }
@@ -9208,9 +9158,9 @@ ${m.content}`).join("\n\n---\n\n");
9208
9158
  return { reply, sources };
9209
9159
  }
9210
9160
  async ask(question, history = [], namespace) {
9211
- var _a3, _b;
9161
+ var _a2, _b;
9212
9162
  await this.initialize();
9213
- 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) {
9163
+ 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) {
9214
9164
  return await this.multiAgentCoordinator.run(question, history);
9215
9165
  }
9216
9166
  const stream = this.askStream(question, history, namespace);
@@ -9245,9 +9195,9 @@ ${m.content}`).join("\n\n---\n\n");
9245
9195
  }
9246
9196
  askStream(_0) {
9247
9197
  return __asyncGenerator(this, arguments, function* (question, history = [], namespace) {
9248
- var _a3, _b;
9198
+ var _a2, _b;
9249
9199
  yield new __await(this.initialize());
9250
- 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) {
9200
+ 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) {
9251
9201
  const stream2 = this.multiAgentCoordinator.runStream(question, history);
9252
9202
  try {
9253
9203
  for (var iter = __forAwait(stream2), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
@@ -9296,10 +9246,10 @@ ${m.content}`).join("\n\n---\n\n");
9296
9246
  */
9297
9247
  askStreamInternal(_0) {
9298
9248
  return __asyncGenerator(this, arguments, function* (question, history = [], namespace) {
9299
- 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;
9249
+ 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;
9300
9250
  yield new __await(this.initialize());
9301
9251
  const ns = formatNamespace(namespace != null ? namespace : this.config.projectId);
9302
- const topK = (_b = (_a3 = this.config.rag) == null ? void 0 : _a3.topK) != null ? _b : 5;
9252
+ const topK = (_b = (_a2 = this.config.rag) == null ? void 0 : _a2.topK) != null ? _b : 5;
9303
9253
  const scoreThreshold = (_d = (_c = this.config.rag) == null ? void 0 : _c.scoreThreshold) != null ? _d : 0.3;
9304
9254
  const requestId = `req_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
9305
9255
  const requestStart = performance.now();
@@ -9348,8 +9298,8 @@ ${m.content}`).join("\n\n---\n\n");
9348
9298
  const rerankStart = performance.now();
9349
9299
  const structuredSources = this.applyStructuredFilters(rawSources, filter).filter((s) => Boolean(s && typeof s === "object"));
9350
9300
  let fullSources = hasNumericPredicates ? structuredSources : structuredSources.filter((m) => {
9351
- var _a4;
9352
- return ((_a4 = m == null ? void 0 : m.score) != null ? _a4 : 0) >= scoreThreshold;
9301
+ var _a3;
9302
+ return ((_a3 = m == null ? void 0 : m.score) != null ? _a3 : 0) >= scoreThreshold;
9353
9303
  });
9354
9304
  const rerankLimit = Math.max(retrievalLimit, fullSources.length);
9355
9305
  const useReranking = arch !== "naive" && (arch === "retrieve-and-rerank" || ((_m = this.config.rag) == null ? void 0 : _m.useReranking));
@@ -9362,13 +9312,13 @@ ${m.content}`).join("\n\n---\n\n");
9362
9312
  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]
9363
9313
 
9364
9314
  ` + promptSources.map((m, i) => {
9365
- var _a4;
9315
+ var _a3;
9366
9316
  return `[Source ${i + 1}]
9367
- ${(_a4 = m == null ? void 0 : m.content) != null ? _a4 : ""}`;
9317
+ ${(_a3 = m == null ? void 0 : m.content) != null ? _a3 : ""}`;
9368
9318
  }).join("\n\n---\n\n") : "No relevant context found.";
9369
9319
  const sources = [...fullSources].filter((s) => Boolean(s && typeof s === "object")).sort((a, b) => {
9370
- var _a4, _b2;
9371
- return ((_a4 = b == null ? void 0 : b.score) != null ? _a4 : 0) - ((_b2 = a == null ? void 0 : a.score) != null ? _b2 : 0);
9320
+ var _a3, _b2;
9321
+ return ((_a3 = b == null ? void 0 : b.score) != null ? _a3 : 0) - ((_b2 = a == null ? void 0 : a.score) != null ? _b2 : 0);
9372
9322
  });
9373
9323
  if (graphData && graphData.nodes.length > 0) {
9374
9324
  const graphContext = graphData.nodes.map(
@@ -9396,10 +9346,10 @@ ${context}`;
9396
9346
  yield {
9397
9347
  reply: "",
9398
9348
  sources: (sources || []).filter((s) => Boolean(s && typeof s === "object")).map((s) => {
9399
- var _a4, _b2, _c2;
9349
+ var _a3, _b2, _c2;
9400
9350
  return {
9401
9351
  id: s.id,
9402
- score: (_a4 = s.score) != null ? _a4 : 0,
9352
+ score: (_a3 = s.score) != null ? _a3 : 0,
9403
9353
  content: (_b2 = s == null ? void 0 : s.content) != null ? _b2 : "",
9404
9354
  metadata: (_c2 = s == null ? void 0 : s.metadata) != null ? _c2 : {},
9405
9355
  namespace: ns
@@ -9592,11 +9542,11 @@ ${context}`;
9592
9542
  systemPrompt,
9593
9543
  userPrompt: question + finalRestrictionSuffix,
9594
9544
  chunks: (sources || []).filter(Boolean).map((s) => {
9595
- var _a4, _b2;
9545
+ var _a3, _b2;
9596
9546
  return {
9597
9547
  id: s.id,
9598
9548
  score: s.score,
9599
- content: (_a4 = s == null ? void 0 : s.content) != null ? _a4 : "",
9549
+ content: (_a3 = s == null ? void 0 : s.content) != null ? _a3 : "",
9600
9550
  metadata: (_b2 = s == null ? void 0 : s.metadata) != null ? _b2 : {},
9601
9551
  namespace: ns
9602
9552
  };
@@ -9615,7 +9565,7 @@ ${context}`;
9615
9565
  const telemetryUrl = ((_B = this.config.telemetry) == null ? void 0 : _B.url) || process.env.TELEMETRY_URL || process.env.NEXT_PUBLIC_TELEMETRY_URL || defaultUrl;
9616
9566
  const absoluteUrl = telemetryUrl.startsWith("http") ? telemetryUrl : "https://www.retrivora.com" + (telemetryUrl.startsWith("/") ? telemetryUrl : "/" + telemetryUrl);
9617
9567
  (async () => {
9618
- var _a4, _b2, _c2, _d2, _e2;
9568
+ var _a3, _b2, _c2, _d2, _e2;
9619
9569
  try {
9620
9570
  let finalTrace = trace;
9621
9571
  if (!awaitHallucination && runHallucination) {
@@ -9624,7 +9574,7 @@ ${context}`;
9624
9574
  finalTrace = buildTrace(backgroundScoreResult);
9625
9575
  }
9626
9576
  }
9627
- const modelName = (finalTrace == null ? void 0 : finalTrace.model) || ((_a4 = this.config.llm) == null ? void 0 : _a4.model) || "llama-3.1-8b-instant";
9577
+ const modelName = (finalTrace == null ? void 0 : finalTrace.model) || ((_a3 = this.config.llm) == null ? void 0 : _a3.model) || "llama-3.1-8b-instant";
9628
9578
  const providerName = (finalTrace == null ? void 0 : finalTrace.provider) || ((_b2 = this.config.llm) == null ? void 0 : _b2.provider) || "groq";
9629
9579
  const tokenCount = Number(((_c2 = finalTrace == null ? void 0 : finalTrace.tokens) == null ? void 0 : _c2.totalTokens) || 0);
9630
9580
  const costEst = Number(((_d2 = finalTrace == null ? void 0 : finalTrace.tokens) == null ? void 0 : _d2.estimatedCostUsd) || 0);
@@ -9678,7 +9628,7 @@ ${context}`;
9678
9628
  * Uses an LRU-bounded embedding cache to avoid re-embedding the same query.
9679
9629
  */
9680
9630
  async generateUiTransformation(question, sources, cachedSchema, forceDeterministic = false) {
9681
- var _a3;
9631
+ var _a2;
9682
9632
  if (!sources || sources.length === 0) {
9683
9633
  return UITransformer.transform(question, sources, this.config, cachedSchema);
9684
9634
  }
@@ -9691,7 +9641,7 @@ ${context}`;
9691
9641
  { visualizationHint: "product_browse", recommendedChart: "text", filterInStockOnly: false, wantsExplicitTable: false, isTemporal: false, isComparison: false, language: "en" }
9692
9642
  );
9693
9643
  }
9694
- const enableLlmUiTransform = ((_a3 = this.config.llm.options) == null ? void 0 : _a3.enableLlmUiTransform) === true;
9644
+ const enableLlmUiTransform = ((_a2 = this.config.llm.options) == null ? void 0 : _a2.enableLlmUiTransform) === true;
9695
9645
  if (forceDeterministic || !enableLlmUiTransform) {
9696
9646
  return UITransformer.transform(question, sources, this.config, cachedSchema);
9697
9647
  }
@@ -9709,15 +9659,15 @@ ${context}`;
9709
9659
  const value = this.resolveNumericPredicateValue(source, predicate);
9710
9660
  return value !== null && this.matchesNumericPredicate(value, predicate);
9711
9661
  })).sort((a, b) => {
9712
- var _a3, _b;
9662
+ var _a2, _b;
9713
9663
  const primary = predicates[0];
9714
- const aValue = (_a3 = this.resolveNumericPredicateValue(a, primary)) != null ? _a3 : 0;
9664
+ const aValue = (_a2 = this.resolveNumericPredicateValue(a, primary)) != null ? _a2 : 0;
9715
9665
  const bValue = (_b = this.resolveNumericPredicateValue(b, primary)) != null ? _b : 0;
9716
9666
  return primary.operator === "lt" || primary.operator === "lte" ? aValue - bValue : bValue - aValue;
9717
9667
  });
9718
9668
  }
9719
9669
  resolveNumericPredicateValue(source, predicate) {
9720
- var _a3;
9670
+ var _a2;
9721
9671
  if (!source) return null;
9722
9672
  const meta = source.metadata || {};
9723
9673
  const field = predicate.field;
@@ -9730,7 +9680,7 @@ ${context}`;
9730
9680
  const value = this.toFiniteNumber(Array.isArray(fuzzy) ? fuzzy[1] : fuzzy.value);
9731
9681
  if (value !== null) return value;
9732
9682
  }
9733
- const contentValue = this.extractNumericValueFromContent((_a3 = source.content) != null ? _a3 : "", field);
9683
+ const contentValue = this.extractNumericValueFromContent((_a2 = source.content) != null ? _a2 : "", field);
9734
9684
  if (contentValue !== null) return contentValue;
9735
9685
  }
9736
9686
  for (const [key, value] of entries) {
@@ -9786,8 +9736,8 @@ ${context}`;
9786
9736
  return Number.isFinite(numeric) ? numeric : null;
9787
9737
  }
9788
9738
  async retrieve(query, options) {
9789
- var _a3, _b, _c, _d, _e, _f, _g2;
9790
- const ns = formatNamespace((_a3 = options.namespace) != null ? _a3 : this.config.projectId);
9739
+ var _a2, _b, _c, _d, _e, _f, _g2;
9740
+ const ns = formatNamespace((_a2 = options.namespace) != null ? _a2 : this.config.projectId);
9791
9741
  const topK = (_b = options.topK) != null ? _b : 5;
9792
9742
  const cacheKey = `${ns}::${query}`;
9793
9743
  let queryVector = this.embeddingCache.get(cacheKey);
@@ -9832,11 +9782,11 @@ ${context}`;
9832
9782
  namespace: ns,
9833
9783
  count: resolvedSources.length,
9834
9784
  sample: resolvedSources.slice(0, 2).map((s) => {
9835
- var _a4;
9785
+ var _a3;
9836
9786
  return {
9837
9787
  id: s == null ? void 0 : s.id,
9838
9788
  score: s == null ? void 0 : s.score,
9839
- contentSnippet: String((_a4 = s == null ? void 0 : s.content) != null ? _a4 : "").substring(0, 150),
9789
+ contentSnippet: String((_a3 = s == null ? void 0 : s.content) != null ? _a3 : "").substring(0, 150),
9840
9790
  metadata: s == null ? void 0 : s.metadata
9841
9791
  };
9842
9792
  })
@@ -9850,8 +9800,8 @@ Focus on extracting the core intent and entities. Do not answer the question, ju
9850
9800
 
9851
9801
  History:
9852
9802
  ${(history || []).map((m) => {
9853
- var _a3;
9854
- return `${(m == null ? void 0 : m.role) || "user"}: ${(_a3 = m == null ? void 0 : m.content) != null ? _a3 : ""}`;
9803
+ var _a2;
9804
+ return `${(m == null ? void 0 : m.role) || "user"}: ${(_a2 = m == null ? void 0 : m.content) != null ? _a2 : ""}`;
9855
9805
  }).join("\n")}
9856
9806
 
9857
9807
  New Question: ${question}
@@ -9878,8 +9828,8 @@ Optimized Search Query:`;
9878
9828
  const { sources } = await this.retrieve(query, { namespace: ns, topK: 5 });
9879
9829
  if (!sources || sources.length === 0) return [];
9880
9830
  const context = sources.map((s) => {
9881
- var _a3;
9882
- return (_a3 = s == null ? void 0 : s.content) != null ? _a3 : "";
9831
+ var _a2;
9832
+ return (_a2 = s == null ? void 0 : s.content) != null ? _a2 : "";
9883
9833
  }).join("\n\n---\n\n");
9884
9834
  const prompt = `Based on the following snippets from a document, what are at least 5 short, relevant questions a user might ask?
9885
9835
  Focus on questions that can be answered by the context.
@@ -9921,13 +9871,13 @@ var Retrivora = class {
9921
9871
  this.pipeline = new Pipeline(this.config);
9922
9872
  }
9923
9873
  async initialize() {
9924
- var _a3;
9874
+ var _a2;
9925
9875
  try {
9926
9876
  await ConfigValidator.validateAndThrow(this.config);
9927
9877
  await LicenseVerifier.verifyAsync(
9928
9878
  this.config.licenseKey,
9929
9879
  this.config.projectId,
9930
- (_a3 = this.config.vectorDb) == null ? void 0 : _a3.provider
9880
+ (_a2 = this.config.vectorDb) == null ? void 0 : _a2.provider
9931
9881
  );
9932
9882
  await LicenseVerifier.verifyIP(this.config.licenseKey);
9933
9883
  } catch (err) {
@@ -10101,186 +10051,9 @@ var ProviderHealthCheck = class {
10101
10051
  };
10102
10052
 
10103
10053
  // src/core/FreeTierLimitsGuard.ts
10104
- var FREE_TIER_QUOTAS = {
10105
- MAX_DOCUMENTS: 25,
10106
- MAX_FILE_SIZE_BYTES: 20 * 1024 * 1024,
10107
- MAX_STORAGE_BYTES: 250 * 1024 * 1024,
10108
- MAX_TRIAL_REQUEST_UNITS: 500,
10109
- MAX_DAILY_REQUEST_UNITS: 50,
10110
- TRIAL_DURATION_DAYS: 14,
10111
- GRACE_PERIOD_DAYS: 2,
10112
- MAX_RPM: 10,
10113
- MAX_RPH: 100,
10114
- MAX_RPD: 500,
10115
- MAX_CONCURRENT_REQUESTS: 2,
10116
- MAX_INPUT_TOKENS: 2e4,
10117
- MAX_OUTPUT_TOKENS: 4e3,
10118
- UPGRADE_REMINDER_DAYS: [7, 12, 14],
10119
- MAX_USERS: 1,
10120
- MAX_NAMESPACES: 1,
10121
- MAX_PROJECTS: 1
10122
- };
10123
- var FreeTierLimitsGuard = class {
10124
- static calculateTrialTimestamps(startedAt) {
10125
- const startDate = startedAt ? new Date(startedAt) : /* @__PURE__ */ new Date();
10126
- const trialStartedAtMs = startDate.getTime();
10127
- const trialExpiresAtMs = trialStartedAtMs + FREE_TIER_QUOTAS.TRIAL_DURATION_DAYS * 24 * 60 * 60 * 1e3;
10128
- const gracePeriodExpiresAtMs = trialStartedAtMs + (FREE_TIER_QUOTAS.TRIAL_DURATION_DAYS + FREE_TIER_QUOTAS.GRACE_PERIOD_DAYS) * 24 * 60 * 60 * 1e3;
10129
- return {
10130
- trial_started_at: new Date(trialStartedAtMs).toISOString(),
10131
- trial_expires_at: new Date(trialExpiresAtMs).toISOString(),
10132
- grace_period_expires_at: new Date(gracePeriodExpiresAtMs).toISOString()
10133
- };
10134
- }
10135
- static calculateRequestUnits(operationType, options) {
10136
- var _a3;
10137
- const op = operationType.toLowerCase().trim();
10138
- 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")) {
10139
- return 0;
10140
- }
10141
- if (op.includes("image")) {
10142
- return 10;
10143
- }
10144
- if (op.includes("embedding")) {
10145
- const chunks = (_a3 = options == null ? void 0 : options.chunkCount) != null ? _a3 : 1;
10146
- return Math.max(1, Math.ceil(chunks / 1e3));
10147
- }
10148
- return 1;
10149
- }
10150
- static checkTrialStatus(startedAt, totalUnitsUsed = 0, nowServerTime = /* @__PURE__ */ new Date()) {
10151
- const timestamps = this.calculateTrialTimestamps(startedAt);
10152
- const startMs = new Date(timestamps.trial_started_at).getTime();
10153
- const expireMs = new Date(timestamps.trial_expires_at).getTime();
10154
- const graceExpireMs = new Date(timestamps.grace_period_expires_at).getTime();
10155
- const nowMs = nowServerTime.getTime();
10156
- const daysElapsed = Math.floor(Math.max(0, nowMs - startMs) / (1e3 * 60 * 60 * 24));
10157
- const daysRemaining = Math.max(0, Math.ceil((expireMs - nowMs) / (1e3 * 60 * 60 * 24)));
10158
- const isConsumedUnits = totalUnitsUsed >= FREE_TIER_QUOTAS.MAX_TRIAL_REQUEST_UNITS;
10159
- const isExpiredTime = nowMs > expireMs;
10160
- const isPastGraceTime = nowMs > graceExpireMs;
10161
- let status = "active";
10162
- let reason;
10163
- if (isPastGraceTime || isConsumedUnits && isExpiredTime) {
10164
- status = "expired";
10165
- reason = "Trial period and grace period have expired. Upgrade your plan.";
10166
- } else if (isExpiredTime || isConsumedUnits) {
10167
- status = "grace_period";
10168
- 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).";
10169
- }
10170
- let upgradeReminderDue;
10171
- if (FREE_TIER_QUOTAS.UPGRADE_REMINDER_DAYS.includes(daysElapsed)) {
10172
- upgradeReminderDue = daysElapsed;
10173
- }
10174
- let usageReminderDue;
10175
- const usagePercent = totalUnitsUsed / FREE_TIER_QUOTAS.MAX_TRIAL_REQUEST_UNITS * 100;
10176
- if (usagePercent >= 100) {
10177
- usageReminderDue = "100%";
10178
- } else if (usagePercent >= 90) {
10179
- usageReminderDue = "90%";
10180
- } else if (usagePercent >= 80) {
10181
- usageReminderDue = "80%";
10182
- }
10183
- return {
10184
- status,
10185
- daysElapsed,
10186
- daysRemaining,
10187
- isGracePeriod: status === "grace_period",
10188
- isExpired: status === "expired",
10189
- upgradeReminderDue,
10190
- usageReminderDue,
10191
- reason
10192
- };
10193
- }
10194
- static checkIngestionAllowed(stats, incomingSizeBytes = 0) {
10195
- var _a3, _b;
10196
- const docs = (_a3 = stats.documentCount) != null ? _a3 : 0;
10197
- const storage = (_b = stats.totalStorageBytes) != null ? _b : 0;
10198
- if (incomingSizeBytes > FREE_TIER_QUOTAS.MAX_FILE_SIZE_BYTES) {
10199
- const mbSize = (incomingSizeBytes / (1024 * 1024)).toFixed(1);
10200
- return {
10201
- allowed: false,
10202
- reason: `[FreeTierIngestor] File size (${mbSize}MB) exceeds maximum allowed size of 20MB under Free Tier rules.`
10203
- };
10204
- }
10205
- if (docs >= FREE_TIER_QUOTAS.MAX_DOCUMENTS) {
10206
- return {
10207
- allowed: false,
10208
- reason: `Document limit reached (${docs}/${FREE_TIER_QUOTAS.MAX_DOCUMENTS}). Upgrade to Pro to ingest more documents.`
10209
- };
10210
- }
10211
- if (storage + incomingSizeBytes > FREE_TIER_QUOTAS.MAX_STORAGE_BYTES) {
10212
- const mbUsed = (storage / (1024 * 1024)).toFixed(1);
10213
- return {
10214
- allowed: false,
10215
- reason: `Storage quota exceeded (${mbUsed}MB / 250MB). Upgrade for unlimited storage.`
10216
- };
10217
- }
10218
- return { allowed: true };
10219
- }
10220
- static checkRequestAllowed(params) {
10221
- var _a3, _b, _c;
10222
- const op = (_a3 = params.operationType) != null ? _a3 : "chat";
10223
- const costUnits = this.calculateRequestUnits(op, { chunkCount: params.chunkCount });
10224
- if (costUnits === 0) {
10225
- return { allowed: true, costUnits: 0 };
10226
- }
10227
- if (params.inputTokens && params.inputTokens > FREE_TIER_QUOTAS.MAX_INPUT_TOKENS) {
10228
- return {
10229
- allowed: false,
10230
- reason: "Token limit exceeded. Upgrade your plan.",
10231
- costUnits
10232
- };
10233
- }
10234
- if (params.outputTokens && params.outputTokens > FREE_TIER_QUOTAS.MAX_OUTPUT_TOKENS) {
10235
- return {
10236
- allowed: false,
10237
- reason: "Token limit exceeded. Upgrade your plan.",
10238
- costUnits
10239
- };
10240
- }
10241
- if (params.concurrentRequests && params.concurrentRequests > FREE_TIER_QUOTAS.MAX_CONCURRENT_REQUESTS) {
10242
- return {
10243
- allowed: false,
10244
- reason: `Concurrent limit exceeded (max ${FREE_TIER_QUOTAS.MAX_CONCURRENT_REQUESTS} active requests). Please wait for active streams to finish.`,
10245
- costUnits
10246
- };
10247
- }
10248
- const totalUnits = (_b = params.totalUnitsUsed) != null ? _b : 0;
10249
- const trialStatus = this.checkTrialStatus(params.trialStartedAt, totalUnits, params.nowServerTime);
10250
- if (trialStatus.status === "expired" || trialStatus.status === "grace_period") {
10251
- return {
10252
- allowed: false,
10253
- reason: trialStatus.reason || "Trial limit reached. Upgrade your plan.",
10254
- costUnits
10255
- };
10256
- }
10257
- if (totalUnits + costUnits > FREE_TIER_QUOTAS.MAX_TRIAL_REQUEST_UNITS) {
10258
- return {
10259
- allowed: false,
10260
- reason: `Total trial request unit limit reached (${totalUnits}/${FREE_TIER_QUOTAS.MAX_TRIAL_REQUEST_UNITS}). Upgrade your plan.`,
10261
- costUnits
10262
- };
10263
- }
10264
- const dailyUnits = (_c = params.dailyUnitsUsed) != null ? _c : 0;
10265
- if (dailyUnits + costUnits > FREE_TIER_QUOTAS.MAX_DAILY_REQUEST_UNITS) {
10266
- return {
10267
- allowed: false,
10268
- reason: `Daily request quota limit reached (${dailyUnits}/${FREE_TIER_QUOTAS.MAX_DAILY_REQUEST_UNITS} units/day). Quota resets tomorrow.`,
10269
- costUnits
10270
- };
10271
- }
10272
- return { allowed: true, costUnits };
10273
- }
10274
- static checkQueryAllowed(dailyQueriesCount = 0) {
10275
- if (dailyQueriesCount >= FREE_TIER_QUOTAS.MAX_DAILY_REQUEST_UNITS) {
10276
- return {
10277
- allowed: false,
10278
- reason: `Daily query limit reached (${FREE_TIER_QUOTAS.MAX_DAILY_REQUEST_UNITS}/day). Quota resets tomorrow.`
10279
- };
10280
- }
10281
- return { allowed: true };
10282
- }
10283
- };
10054
+ var FreeTierLimitsGuard_exports = {};
10055
+ __reExport(FreeTierLimitsGuard_exports, billing_guards_star);
10056
+ import * as billing_guards_star from "@retrivora-ai/billing-guards";
10284
10057
 
10285
10058
  // src/core/VectorPlugin.ts
10286
10059
  var VectorPlugin = class {
@@ -10288,12 +10061,12 @@ var VectorPlugin = class {
10288
10061
  const resolvedConfig = ConfigResolver.resolve(hostConfig);
10289
10062
  this.config = resolvedConfig;
10290
10063
  this.validationPromise = (async () => {
10291
- var _a3;
10064
+ var _a2;
10292
10065
  await ConfigValidator.validateAndThrow(resolvedConfig);
10293
10066
  await LicenseVerifier.verifyAsync(
10294
10067
  resolvedConfig.licenseKey,
10295
10068
  resolvedConfig.projectId,
10296
- (_a3 = resolvedConfig.vectorDb) == null ? void 0 : _a3.provider
10069
+ (_a2 = resolvedConfig.vectorDb) == null ? void 0 : _a2.provider
10297
10070
  );
10298
10071
  })();
10299
10072
  this.validationPromise.catch(() => {
@@ -10329,8 +10102,8 @@ var VectorPlugin = class {
10329
10102
  }
10330
10103
  estimateInputTokens(message, history = []) {
10331
10104
  const allContent = message.length + history.reduce((acc, m) => {
10332
- var _a3, _b;
10333
- 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);
10105
+ var _a2, _b;
10106
+ 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);
10334
10107
  }, 0);
10335
10108
  return Math.ceil(allContent / 4);
10336
10109
  }
@@ -10338,7 +10111,7 @@ var VectorPlugin = class {
10338
10111
  * Run a chat query.
10339
10112
  */
10340
10113
  async chat(message, history = [], namespace) {
10341
- var _a3;
10114
+ var _a2;
10342
10115
  try {
10343
10116
  await this.validationPromise;
10344
10117
  } catch (err) {
@@ -10346,11 +10119,11 @@ var VectorPlugin = class {
10346
10119
  }
10347
10120
  try {
10348
10121
  const inputTokens = this.estimateInputTokens(message, history);
10349
- const tokenCheck = FreeTierLimitsGuard.checkRequestAllowed({
10122
+ const tokenCheck = FreeTierLimitsGuard_exports.FreeTierLimitsGuard.checkRequestAllowed({
10350
10123
  operationType: "chat",
10351
10124
  inputTokens
10352
10125
  });
10353
- if (!tokenCheck.allowed && ((_a3 = tokenCheck.reason) == null ? void 0 : _a3.toLowerCase().includes("token"))) {
10126
+ if (!tokenCheck.allowed && ((_a2 = tokenCheck.reason) == null ? void 0 : _a2.toLowerCase().includes("token"))) {
10354
10127
  throw wrapError(new Error(tokenCheck.reason), "RATE_LIMITED");
10355
10128
  }
10356
10129
  return await this.pipeline.ask(message, history, namespace);
@@ -10371,7 +10144,7 @@ var VectorPlugin = class {
10371
10144
  */
10372
10145
  chatStream(_0) {
10373
10146
  return __asyncGenerator(this, arguments, function* (message, history = [], namespace) {
10374
- var _a3;
10147
+ var _a2;
10375
10148
  try {
10376
10149
  yield new __await(this.validationPromise);
10377
10150
  } catch (err) {
@@ -10379,11 +10152,11 @@ var VectorPlugin = class {
10379
10152
  }
10380
10153
  try {
10381
10154
  const inputTokens = this.estimateInputTokens(message, history);
10382
- const tokenCheck = FreeTierLimitsGuard.checkRequestAllowed({
10155
+ const tokenCheck = FreeTierLimitsGuard_exports.FreeTierLimitsGuard.checkRequestAllowed({
10383
10156
  operationType: "chat_stream",
10384
10157
  inputTokens
10385
10158
  });
10386
- if (!tokenCheck.allowed && ((_a3 = tokenCheck.reason) == null ? void 0 : _a3.toLowerCase().includes("token"))) {
10159
+ if (!tokenCheck.allowed && ((_a2 = tokenCheck.reason) == null ? void 0 : _a2.toLowerCase().includes("token"))) {
10387
10160
  throw wrapError(new Error(tokenCheck.reason), "RATE_LIMITED");
10388
10161
  }
10389
10162
  const stream = this.pipeline.askStream(message, history, namespace);
@@ -10470,13 +10243,13 @@ var ConfigBuilder = class {
10470
10243
  * Configure the vector database provider
10471
10244
  */
10472
10245
  vectorDb(provider, options) {
10473
- var _a3;
10246
+ var _a2;
10474
10247
  if (provider === "auto") {
10475
10248
  this._vectorDb = this._autoDetectVectorDb();
10476
10249
  } else {
10477
10250
  this._vectorDb = {
10478
10251
  provider,
10479
- indexName: (_a3 = options == null ? void 0 : options.indexName) != null ? _a3 : "default",
10252
+ indexName: (_a2 = options == null ? void 0 : options.indexName) != null ? _a2 : "default",
10480
10253
  options: __spreadValues({}, options)
10481
10254
  };
10482
10255
  }
@@ -10486,7 +10259,7 @@ var ConfigBuilder = class {
10486
10259
  * Configure the LLM provider for chat
10487
10260
  */
10488
10261
  llm(provider, model, apiKey, options) {
10489
- var _a3, _b;
10262
+ var _a2, _b;
10490
10263
  if (provider === "auto") {
10491
10264
  this._llm = this._autoDetectLLM();
10492
10265
  } else {
@@ -10495,7 +10268,7 @@ var ConfigBuilder = class {
10495
10268
  model: model != null ? model : "default-model",
10496
10269
  apiKey,
10497
10270
  systemPrompt: options == null ? void 0 : options.systemPrompt,
10498
- maxTokens: (_a3 = options == null ? void 0 : options.maxTokens) != null ? _a3 : 1024,
10271
+ maxTokens: (_a2 = options == null ? void 0 : options.maxTokens) != null ? _a2 : 1024,
10499
10272
  temperature: (_b = options == null ? void 0 : options.temperature) != null ? _b : 0.7,
10500
10273
  baseUrl: options == null ? void 0 : options.baseUrl,
10501
10274
  options
@@ -10538,8 +10311,8 @@ var ConfigBuilder = class {
10538
10311
  * Set RAG-specific pipeline parameters
10539
10312
  */
10540
10313
  rag(options) {
10541
- var _a3;
10542
- this._rag = __spreadValues(__spreadValues({}, (_a3 = this._rag) != null ? _a3 : {}), options);
10314
+ var _a2;
10315
+ this._rag = __spreadValues(__spreadValues({}, (_a2 = this._rag) != null ? _a2 : {}), options);
10543
10316
  return this;
10544
10317
  }
10545
10318
  /**
@@ -10555,7 +10328,7 @@ var ConfigBuilder = class {
10555
10328
  * Throws if required fields (projectId, vectorDb, llm, embedding) are not set.
10556
10329
  */
10557
10330
  build() {
10558
- var _a3;
10331
+ var _a2;
10559
10332
  const missing = [];
10560
10333
  if (!this._projectId) missing.push('projectId (call .projectId("my-app"))');
10561
10334
  if (!this._vectorDb) missing.push('vectorDb (call .vectorDb("pinecone", { ... }))');
@@ -10567,7 +10340,7 @@ var ConfigBuilder = class {
10567
10340
  ${missing.join("\n ")}`
10568
10341
  );
10569
10342
  }
10570
- const ragConfig = (_a3 = this._rag) != null ? _a3 : { chunkSize: 1e3, chunkOverlap: 200, topK: 5 };
10343
+ const ragConfig = (_a2 = this._rag) != null ? _a2 : { chunkSize: 1e3, chunkOverlap: 200, topK: 5 };
10571
10344
  if (process.env.RAG_USE_GRAPH_RETRIEVAL === "true") {
10572
10345
  ragConfig.useGraphRetrieval = true;
10573
10346
  }
@@ -10663,8 +10436,8 @@ var DocumentParser = class {
10663
10436
  * Extract text from a File or Buffer based on its type.
10664
10437
  */
10665
10438
  static async parse(file, fileName, mimeType) {
10666
- var _a3;
10667
- const extension = ((_a3 = fileName.split(".").pop()) == null ? void 0 : _a3.toLowerCase()) || "";
10439
+ var _a2;
10440
+ const extension = ((_a2 = fileName.split(".").pop()) == null ? void 0 : _a2.toLowerCase()) || "";
10668
10441
  if (extension === "txt" || extension === "md" || mimeType === "text/plain" || mimeType === "text/markdown") {
10669
10442
  return this.readAsText(file);
10670
10443
  }
@@ -10759,7 +10532,17 @@ import { NextResponse } from "next/server";
10759
10532
  import * as fs from "fs";
10760
10533
  import * as path from "path";
10761
10534
  import * as os from "os";
10762
- var DatabaseStorage = class {
10535
+ function validateTableName(name) {
10536
+ if (!/^[a-zA-Z_][a-zA-Z0-9_]{0,62}$/.test(name)) {
10537
+ throw new Error(
10538
+ `[DatabaseStorage] Invalid table name "${name}". Table names must start with a letter or underscore and contain only alphanumeric characters and underscores (max 63 chars).`
10539
+ );
10540
+ }
10541
+ return name;
10542
+ }
10543
+ var _pgInitPromises = /* @__PURE__ */ new Map();
10544
+ var _DatabaseStorage = class _DatabaseStorage {
10545
+ // 5 minutes
10763
10546
  constructor(config) {
10764
10547
  // Dynamic PG Pool
10765
10548
  this.pgPool = null;
@@ -10771,36 +10554,50 @@ var DatabaseStorage = class {
10771
10554
  this.fallbackDir = this.isServerless ? path.join(os.tmpdir(), ".retrivora") : path.join(process.cwd(), ".retrivora");
10772
10555
  this.historyFile = path.join(this.fallbackDir, "history.json");
10773
10556
  this.feedbackFile = path.join(this.fallbackDir, "feedback.json");
10774
- var _a3, _b, _c, _d, _e;
10557
+ /** Subscription check result cache (Fix PERF-1). */
10558
+ this.subscriptionCheckedAt = 0;
10559
+ var _a2, _b, _c, _d, _e;
10775
10560
  this.config = config;
10776
- this.provider = ((_a3 = config.vectorDb) == null ? void 0 : _a3.provider) || "fs";
10561
+ this.provider = ((_a2 = config.vectorDb) == null ? void 0 : _a2.provider) || "fs";
10777
10562
  const rawHistoryTable = ((_c = (_b = config.rag) == null ? void 0 : _b.history) == null ? void 0 : _c.tableName) || "retrivora_history";
10778
10563
  const rawFeedbackTable = ((_e = (_d = config.rag) == null ? void 0 : _d.feedback) == null ? void 0 : _e.tableName) || "retrivora_feedback";
10779
- this.historyTableName = rawHistoryTable.replace(/[^a-zA-Z0-9_]/g, "_");
10780
- this.feedbackTableName = rawFeedbackTable.replace(/[^a-zA-Z0-9_]/g, "_");
10564
+ this.historyTableName = validateTableName(rawHistoryTable);
10565
+ this.feedbackTableName = validateTableName(rawFeedbackTable);
10781
10566
  }
10782
10567
  /**
10783
- * Asserts the user is running a valid Premium/Enterprise license in production.
10784
- * In non-production (development / test) environments this is a complete no-op —
10785
- * all History and Feedback features are freely accessible for local development.
10568
+ * Checks that the caller holds a valid paid-tier license in production.
10569
+ *
10570
+ * Fix PERF-1: result is cached for 5 minutes so we don't call
10571
+ * LicenseVerifier.verifyAsync() on every DB operation (saveMessage,
10572
+ * getHistory, etc.).
10573
+ *
10574
+ * Fix PERF-2: renamed from `checkPremiumSubscription` to clarify intent.
10575
+ * The previous allowlist admitted all free tiers, defeating the gate.
10576
+ * The new allowlist only passes paid tiers. Free-tier access to history
10577
+ * and feedback is a product decision — update the constant below if the
10578
+ * policy changes.
10579
+ *
10580
+ * In non-production environments this is a complete no-op so local
10581
+ * development works without a license key.
10786
10582
  */
10787
- async checkPremiumSubscription() {
10788
- if (process.env.NODE_ENV !== "production") {
10789
- return;
10790
- }
10583
+ async checkPaidSubscription() {
10584
+ if (process.env.NODE_ENV !== "production") return;
10585
+ if (Date.now() - this.subscriptionCheckedAt < _DatabaseStorage.SUBSCRIPTION_CACHE_TTL_MS) return;
10791
10586
  if (!this.config.licenseKey) {
10792
10587
  throw new Error(
10793
- "[Retrivora SDK] Chat History and Feedback features require a Premium or Enterprise license key in production."
10588
+ "[Retrivora SDK] Chat History and Feedback features require a paid license key in production."
10794
10589
  );
10795
10590
  }
10796
10591
  try {
10797
10592
  const payload = await LicenseVerifier.verifyAsync(this.config.licenseKey, this.config.projectId);
10798
10593
  const tier = (payload.tier || "").toLowerCase();
10799
- if (tier !== "premium" && tier !== "enterprise" && tier !== "growth" && tier !== "pro" && tier !== "developer_pro" && tier !== "free_trial" && tier !== "free_tier" && tier !== "free" && tier !== "hobby") {
10594
+ const PAID_TIERS = /* @__PURE__ */ new Set(["premium", "enterprise", "growth", "pro", "developer_pro"]);
10595
+ if (!PAID_TIERS.has(tier)) {
10800
10596
  throw new Error(
10801
10597
  `[Retrivora SDK] Chat History and Feedback features are not available on the "${tier}" tier. Please upgrade to a Developer Pro or Enterprise plan.`
10802
10598
  );
10803
10599
  }
10600
+ this.subscriptionCheckedAt = Date.now();
10804
10601
  } catch (err) {
10805
10602
  throw new Error(
10806
10603
  `[Retrivora SDK] Subscription check failed: ${err instanceof Error ? err.message : String(err)}`
@@ -10808,34 +10605,45 @@ var DatabaseStorage = class {
10808
10605
  }
10809
10606
  }
10810
10607
  async checkHistoryEnabled() {
10811
- var _a3, _b;
10812
- await this.checkPremiumSubscription();
10813
- if (((_b = (_a3 = this.config.rag) == null ? void 0 : _a3.history) == null ? void 0 : _b.enabled) === false) {
10608
+ var _a2, _b;
10609
+ await this.checkPaidSubscription();
10610
+ if (((_b = (_a2 = this.config.rag) == null ? void 0 : _a2.history) == null ? void 0 : _b.enabled) === false) {
10814
10611
  throw new Error("[Retrivora SDK] Chat History is disabled in RagConfig.");
10815
10612
  }
10816
10613
  }
10817
10614
  async checkFeedbackEnabled() {
10818
- var _a3, _b;
10819
- await this.checkPremiumSubscription();
10820
- if (((_b = (_a3 = this.config.rag) == null ? void 0 : _a3.feedback) == null ? void 0 : _b.enabled) === false) {
10615
+ var _a2, _b;
10616
+ await this.checkPaidSubscription();
10617
+ if (((_b = (_a2 = this.config.rag) == null ? void 0 : _a2.feedback) == null ? void 0 : _b.enabled) === false) {
10821
10618
  throw new Error("[Retrivora SDK] User Feedback is disabled in RagConfig.");
10822
10619
  }
10823
10620
  }
10824
- async getPGPool() {
10825
- const globalKey = `__retrivora_pg_pool_${this.config.projectId}`;
10826
- const _g2 = global;
10827
- if (_g2[globalKey]) {
10828
- this.pgPool = _g2[globalKey];
10829
- return this.pgPool;
10621
+ /**
10622
+ * Returns (or lazily creates) the PostgreSQL connection pool for this project.
10623
+ *
10624
+ * Fix ERR-2: the previous implementation had a double-initialization race
10625
+ * condition — two concurrent requests would both find _g[globalKey] missing,
10626
+ * both create a Pool, and the second Pool would be orphaned, leaking
10627
+ * database connections. This version stores a single Promise in the module-
10628
+ * level _pgInitPromises map so all concurrent callers await the same
10629
+ * initialization without creating duplicate pools.
10630
+ */
10631
+ getPGPool() {
10632
+ const globalKey = `pg:${this.config.projectId}`;
10633
+ if (!_pgInitPromises.has(globalKey)) {
10634
+ _pgInitPromises.set(globalKey, this.createPGPool());
10830
10635
  }
10636
+ return _pgInitPromises.get(globalKey);
10637
+ }
10638
+ async createPGPool() {
10831
10639
  const opts = this.config.vectorDb.options;
10832
10640
  if (!opts.connectionString) {
10833
10641
  throw new Error("[DatabaseStorage] pg connectionString option is missing.");
10834
10642
  }
10835
10643
  const { Pool: Pool3 } = await import("pg");
10836
- this.pgPool = new Pool3({ connectionString: opts.connectionString });
10837
- _g2[globalKey] = this.pgPool;
10838
- const client = await this.pgPool.connect();
10644
+ const pool = new Pool3({ connectionString: opts.connectionString });
10645
+ this.pgPool = pool;
10646
+ const client = await pool.connect();
10839
10647
  try {
10840
10648
  await client.query(`
10841
10649
  CREATE TABLE IF NOT EXISTS ${this.historyTableName} (
@@ -10862,7 +10670,7 @@ var DatabaseStorage = class {
10862
10670
  } finally {
10863
10671
  client.release();
10864
10672
  }
10865
- return this.pgPool;
10673
+ return pool;
10866
10674
  }
10867
10675
  async getMongoClient() {
10868
10676
  const globalKey = `__retrivora_mongo_client_${this.config.projectId}`;
@@ -11042,17 +10850,21 @@ var DatabaseStorage = class {
11042
10850
  }
11043
10851
  }
11044
10852
  /**
11045
- * Return true when `sessionId` falls into the allowed `projectIds` scope.
11046
- * When `projectIds` is empty / undefined we allow the call (legacy behavior
11047
- * for non-multi-tenant consumers). Session ids typically begin with the
11048
- * project id they were created for, so we prefix-match as well as contains-match
11049
- * to catch sharded / suffix-style ids used by some integrations.
10853
+ * Returns true when `sessionId` is in scope for the given `projectIds`.
10854
+ *
10855
+ * Fix RAG-5: the previous implementation used `sessionId.includes(pid)` which
10856
+ * allows cross-tenant data leakage. If a project ID "abc" is a substring of
10857
+ * another tenant's session ID (e.g., "xyz-abc-user-123"), the check would
10858
+ * incorrectly return true. We now only allow exact match or strict delimiter-
10859
+ * separated prefix match to prevent this attack.
11050
10860
  */
11051
10861
  sessionInScope(sessionId, projectIds) {
11052
10862
  if (!projectIds || projectIds.length === 0) return true;
11053
- return projectIds.some(
11054
- (pid) => pid && (sessionId === pid || sessionId.startsWith(pid) || sessionId.includes(pid))
11055
- );
10863
+ return projectIds.some((pid) => {
10864
+ if (!pid) return false;
10865
+ if (sessionId === pid) return true;
10866
+ return sessionId.startsWith(`${pid}-`) || sessionId.startsWith(`${pid}_`) || sessionId.startsWith(`${pid}/`);
10867
+ });
11056
10868
  }
11057
10869
  async listSessions(projectIds) {
11058
10870
  await this.checkHistoryEnabled();
@@ -11201,9 +11013,12 @@ var DatabaseStorage = class {
11201
11013
  }
11202
11014
  }
11203
11015
  };
11016
+ _DatabaseStorage.SUBSCRIPTION_CACHE_TTL_MS = 5 * 60 * 1e3;
11017
+ var DatabaseStorage = _DatabaseStorage;
11204
11018
 
11205
11019
  // src/handlers/index.ts
11206
11020
  init_version();
11021
+ import { createHash } from "crypto";
11207
11022
  async function checkAuth(req, onAuthorize) {
11208
11023
  if (!onAuthorize) return null;
11209
11024
  try {
@@ -11225,6 +11040,17 @@ var RateLimiter = class {
11225
11040
  this.hits = /* @__PURE__ */ new Map();
11226
11041
  this.windowMs = windowMs;
11227
11042
  this.max = max;
11043
+ this.pruneTimer = setInterval(() => this.prune(), windowMs);
11044
+ if (typeof this.pruneTimer === "object" && "unref" in this.pruneTimer) {
11045
+ this.pruneTimer.unref();
11046
+ }
11047
+ }
11048
+ /** Prune entries whose timestamps are all outside the window. */
11049
+ prune() {
11050
+ const cutoff = Date.now() - this.windowMs;
11051
+ for (const [k, timestamps] of this.hits.entries()) {
11052
+ if (timestamps.every((t) => t <= cutoff)) this.hits.delete(k);
11053
+ }
11228
11054
  }
11229
11055
  /** Returns true if the request should be allowed, false if rate-limited. */
11230
11056
  allow(key) {
@@ -11233,11 +11059,6 @@ var RateLimiter = class {
11233
11059
  const existing = (this.hits.get(key) || []).filter((t) => t > cutoff);
11234
11060
  existing.push(now);
11235
11061
  this.hits.set(key, existing);
11236
- if (this.hits.size > 5e3) {
11237
- for (const [k, timestamps] of this.hits.entries()) {
11238
- if (timestamps.every((t) => t <= cutoff)) this.hits.delete(k);
11239
- }
11240
- }
11241
11062
  return existing.length <= this.max;
11242
11063
  }
11243
11064
  retryAfterSec(key) {
@@ -11252,8 +11073,8 @@ var _g = global;
11252
11073
  var _a;
11253
11074
  var rateLimiter = (_a = _g.__retrivoraRateLimiter) != null ? _a : _g.__retrivoraRateLimiter = new RateLimiter(6e4, 30);
11254
11075
  function getRateLimitKey(req) {
11255
- var _a3, _b;
11256
- 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";
11076
+ var _a2, _b;
11077
+ 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";
11257
11078
  return `ip:${ip}`;
11258
11079
  }
11259
11080
  function checkRateLimit(req) {
@@ -11280,8 +11101,6 @@ function isFreeTier(tier) {
11280
11101
  if (!tier) return true;
11281
11102
  return FREE_TIER_NAMES.has(tier.toLowerCase().trim());
11282
11103
  }
11283
- var _a2;
11284
- var _freeTierGuardInstance = (_a2 = _g.__retrivoraFreeTierGuard) != null ? _a2 : _g.__retrivoraFreeTierGuard = new FreeTierLimitsGuard();
11285
11104
  function createPaymentRequiredResponse(reason, details) {
11286
11105
  const body = __spreadProps(__spreadValues({
11287
11106
  type: "https://retrivora.com/docs/errors/payment-required",
@@ -11292,10 +11111,10 @@ function createPaymentRequiredResponse(reason, details) {
11292
11111
  instance: void 0
11293
11112
  }, details != null ? details : {}), {
11294
11113
  quota: {
11295
- maxDailyRequestUnits: FREE_TIER_QUOTAS.MAX_DAILY_REQUEST_UNITS,
11296
- maxTrialRequestUnits: FREE_TIER_QUOTAS.MAX_TRIAL_REQUEST_UNITS,
11297
- maxDocuments: FREE_TIER_QUOTAS.MAX_DOCUMENTS,
11298
- maxStorageBytes: FREE_TIER_QUOTAS.MAX_STORAGE_BYTES,
11114
+ maxDailyRequestUnits: FreeTierLimitsGuard_exports.FREE_TIER_QUOTAS.MAX_DAILY_REQUEST_UNITS,
11115
+ maxTrialRequestUnits: FreeTierLimitsGuard_exports.FREE_TIER_QUOTAS.MAX_TRIAL_REQUEST_UNITS,
11116
+ maxDocuments: FreeTierLimitsGuard_exports.FREE_TIER_QUOTAS.MAX_DOCUMENTS,
11117
+ maxStorageBytes: FreeTierLimitsGuard_exports.FREE_TIER_QUOTAS.MAX_STORAGE_BYTES,
11299
11118
  upgradeUrl: "https://retrivora.com/pricing"
11300
11119
  }
11301
11120
  });
@@ -11307,42 +11126,17 @@ function createPaymentRequiredResponse(reason, details) {
11307
11126
  });
11308
11127
  }
11309
11128
  function resolveLicenseKey(req, config, bodyLicenseKey) {
11310
- var _a3;
11311
- 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 || "";
11312
- }
11313
- function extractProjectIdFromLicenseKey(licenseKey) {
11314
- try {
11315
- const sanitizedKey = licenseKey.trim().replace(/^["']|["']$/g, "").trim();
11316
- const rawToken = sanitizedKey.replace(/^rtv_/i, "").trim();
11317
- const parts = rawToken.split(".");
11318
- if (parts.length === 3) {
11319
- const decodedPayload = JSON.parse(Buffer.from(parts[1], "base64url").toString("utf8"));
11320
- return decodedPayload.projectId;
11321
- }
11322
- } catch (e) {
11323
- }
11324
- return void 0;
11129
+ return req.headers.get("x-license-key") || bodyLicenseKey || config.licenseKey || process.env.RETRIVORA_LICENSE_KEY || process.env.RAG_LICENSE_KEY || "";
11325
11130
  }
11326
11131
  async function enforceLicense(req, config, bodyLicenseKey) {
11327
11132
  const rawKey = resolveLicenseKey(req, config, bodyLicenseKey);
11328
11133
  const licenseKey = (rawKey || "").trim().replace(/^["']|["']$/g, "").trim();
11329
11134
  const url = new URL(req.url);
11330
- const queryProjectId = url.searchParams.get("projectId") || url.searchParams.get("project-id");
11331
- const headerProjectId = req.headers.get("x-project-id") || req.headers.get("X-Project-Id");
11332
- const jwtProjectId = extractProjectIdFromLicenseKey(licenseKey);
11333
- let bodyProjectId = "";
11334
- if (req.method === "POST") {
11335
- try {
11336
- const body = await req.clone().json();
11337
- bodyProjectId = (body == null ? void 0 : body.projectId) || "";
11338
- } catch (e) {
11339
- }
11340
- }
11341
- const projectId = queryProjectId || headerProjectId || bodyProjectId || jwtProjectId || config.projectId || "";
11135
+ 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 || "";
11342
11136
  try {
11343
11137
  let payload;
11344
11138
  try {
11345
- payload = await LicenseVerifier.verifyAsync(licenseKey, projectId);
11139
+ payload = await LicenseVerifier.verifyAsync(licenseKey, requestedProjectId);
11346
11140
  } catch (err) {
11347
11141
  return {
11348
11142
  ok: false,
@@ -11354,7 +11148,7 @@ async function enforceLicense(req, config, bodyLicenseKey) {
11354
11148
  }
11355
11149
  return { ok: true, payload };
11356
11150
  } catch (err) {
11357
- 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.";
11151
+ const message = err instanceof Error ? err.message : "Missing or invalid RETRIVORA_LICENSE_KEY. Set RETRIVORA_LICENSE_KEY in your environment or contact your administrator.";
11358
11152
  const body = {
11359
11153
  error: {
11360
11154
  code: "LICENSE_REVOKED",
@@ -11412,38 +11206,59 @@ function sanitizeInput(raw) {
11412
11206
  }
11413
11207
  return { ok: true, value: trimmed };
11414
11208
  }
11209
+ var _pluginCache = /* @__PURE__ */ new Map();
11210
+ var PLUGIN_CACHE_TTL_MS = 10 * 60 * 1e3;
11211
+ function configHash(config) {
11212
+ return createHash("sha1").update(JSON.stringify(config != null ? config : {})).digest("hex").slice(0, 16);
11213
+ }
11415
11214
  function getOrCreatePlugin(configOrPlugin) {
11416
11215
  if (configOrPlugin instanceof VectorPlugin) return configOrPlugin;
11417
- const cacheKey = "__retrivoraPlugin_" + JSON.stringify(configOrPlugin != null ? configOrPlugin : {}).slice(0, 64);
11418
- if (!_g[cacheKey]) {
11419
- _g[cacheKey] = new VectorPlugin(configOrPlugin);
11216
+ const key = configHash(configOrPlugin);
11217
+ const cached = _pluginCache.get(key);
11218
+ if (cached && Date.now() - cached.createdAt < PLUGIN_CACHE_TTL_MS) return cached.plugin;
11219
+ const plugin = new VectorPlugin(configOrPlugin);
11220
+ _pluginCache.set(key, { plugin, createdAt: Date.now() });
11221
+ return plugin;
11222
+ }
11223
+ var TELEMETRY_ALLOWED_HOSTS = /* @__PURE__ */ new Set([
11224
+ "retrivora.com",
11225
+ "www.retrivora.com"
11226
+ ]);
11227
+ function sanitizeTelemetryUrl(raw) {
11228
+ if (process.env.NODE_ENV !== "production") return raw;
11229
+ try {
11230
+ const parsed = new URL(raw);
11231
+ const hostname = parsed.hostname.toLowerCase().replace(/^www\./, "");
11232
+ if (!TELEMETRY_ALLOWED_HOSTS.has(`www.${hostname}`) && !TELEMETRY_ALLOWED_HOSTS.has(hostname)) {
11233
+ console.warn(
11234
+ `[Retrivora SDK Telemetry] Blocked non-allowlisted telemetry URL: ${parsed.hostname}. Only retrivora.com endpoints are permitted in production.`
11235
+ );
11236
+ return null;
11237
+ }
11238
+ return parsed.toString();
11239
+ } catch (e) {
11240
+ return null;
11420
11241
  }
11421
- return _g[cacheKey];
11422
11242
  }
11423
11243
  function reportTelemetry(req, plugin, action, status, details, trace) {
11424
- var _a3, _b, _c, _d, _e, _f, _g2;
11244
+ var _a2, _b, _c, _d, _e, _f, _g2;
11425
11245
  try {
11426
11246
  const config = plugin.getConfig();
11427
- const licenseKey = config.licenseKey || process.env.RAG_LICENSE_KEY || process.env.RETRIVORA_LICENSE_KEY || process.env.NEXT_PUBLIC_RETRIVORA_LICENSE_KEY;
11247
+ const licenseKey = config.licenseKey || process.env.RETRIVORA_LICENSE_KEY || process.env.RAG_LICENSE_KEY;
11428
11248
  const telemetryConfig = config.telemetry;
11429
- const host = req.headers.get("host") || "localhost";
11430
11249
  const defaultUrl = "https://www.retrivora.com/api/telemetry";
11431
- const telemetryUrl = (telemetryConfig == null ? void 0 : telemetryConfig.url) || process.env.TELEMETRY_URL || process.env.NEXT_PUBLIC_TELEMETRY_URL || defaultUrl;
11250
+ const rawTelemetryUrl = (telemetryConfig == null ? void 0 : telemetryConfig.url) || process.env.TELEMETRY_URL || process.env.NEXT_PUBLIC_TELEMETRY_URL || defaultUrl;
11251
+ const telemetryUrl = sanitizeTelemetryUrl(rawTelemetryUrl);
11252
+ if (!telemetryUrl) return;
11432
11253
  const userAgent = req.headers.get("user-agent") || `Retrivora-SDK/${SDK_VERSION}`;
11433
- let absoluteUrl = telemetryUrl;
11434
- if (!telemetryUrl.startsWith("http")) {
11435
- const proto = req.headers.get("x-forwarded-proto") || "http";
11436
- absoluteUrl = `${proto}://${host}${telemetryUrl}`;
11437
- }
11438
11254
  const projectId = config.projectId || "default";
11439
- 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";
11255
+ 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";
11440
11256
  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";
11441
11257
  const tokens = Number(((_e = trace == null ? void 0 : trace.tokens) == null ? void 0 : _e.totalTokens) || (trace == null ? void 0 : trace.totalTokens) || 0);
11442
11258
  const costUsd = Number(((_f = trace == null ? void 0 : trace.tokens) == null ? void 0 : _f.estimatedCostUsd) || (trace == null ? void 0 : trace.costUsd) || 0);
11443
11259
  const latencyMs = Number(((_g2 = trace == null ? void 0 : trace.latency) == null ? void 0 : _g2.totalMs) || (trace == null ? void 0 : trace.latencyMs) || 0);
11444
11260
  const payload = {
11445
11261
  trace,
11446
- licenseKey,
11447
11262
  projectId,
11448
11263
  organization: process.env.RETRIVORA_ORGANIZATION || "default-org",
11449
11264
  sdkVersion: SDK_VERSION,
@@ -11460,14 +11275,13 @@ function reportTelemetry(req, plugin, action, status, details, trace) {
11460
11275
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
11461
11276
  details
11462
11277
  };
11463
- fetch(absoluteUrl, {
11278
+ fetch(telemetryUrl, {
11464
11279
  method: "POST",
11465
- headers: {
11466
- "Content-Type": "application/json",
11467
- "x-forwarded-for": req.headers.get("x-forwarded-for") || "",
11468
- "x-real-ip": req.headers.get("x-real-ip") || "",
11280
+ headers: __spreadProps(__spreadValues({
11281
+ "Content-Type": "application/json"
11282
+ }, licenseKey ? { "x-license-key": licenseKey } : {}), {
11469
11283
  "user-agent": userAgent
11470
- },
11284
+ }),
11471
11285
  body: JSON.stringify(payload)
11472
11286
  }).catch((err) => {
11473
11287
  console.warn("[Retrivora SDK Telemetry] Async report warning:", err.message);
@@ -11517,7 +11331,7 @@ function createChatHandler(configOrPlugin, options) {
11517
11331
  const storage = new DatabaseStorage(plugin.getConfig());
11518
11332
  const onAuthorize = options == null ? void 0 : options.onAuthorize;
11519
11333
  return async function POST(req, context) {
11520
- var _a3, _b, _c, _d, _e, _f;
11334
+ var _a2, _b, _c, _d, _e, _f;
11521
11335
  const authResult = await checkAuth(req, onAuthorize);
11522
11336
  if (authResult) return authResult;
11523
11337
  const rateLimited = checkRateLimit(req);
@@ -11530,18 +11344,17 @@ function createChatHandler(configOrPlugin, options) {
11530
11344
  if (!licenseCheck.ok) return licenseCheck.response;
11531
11345
  if (isFreeTier(licenseCheck.payload.tier)) {
11532
11346
  const estimatedInputTokens = Math.ceil(
11533
- ((((_a3 = bodyObj.message) == null ? void 0 : _a3.length) || 0) + (Array.isArray(bodyObj.messages) ? bodyObj.messages.reduce((a, m) => {
11534
- var _a4, _b2;
11535
- 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);
11347
+ ((((_a2 = bodyObj.message) == null ? void 0 : _a2.length) || 0) + (Array.isArray(bodyObj.messages) ? bodyObj.messages.reduce((a, m) => {
11348
+ var _a3, _b2;
11349
+ 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);
11536
11350
  }, 0) : 0) + (((_b = bodyObj.history) == null ? void 0 : _b.reduce((a, m) => {
11537
- var _a4, _b2;
11538
- 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);
11351
+ var _a3, _b2;
11352
+ 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);
11539
11353
  }, 0)) || 0)) / 4
11540
11354
  );
11541
- const chatCheck = FreeTierLimitsGuard.checkRequestAllowed({
11355
+ const chatCheck = FreeTierLimitsGuard_exports.FreeTierLimitsGuard.checkRequestAllowed({
11542
11356
  operationType: "chat",
11543
- inputTokens: estimatedInputTokens,
11544
- projectId: licenseCheck.payload.projectId
11357
+ inputTokens: estimatedInputTokens
11545
11358
  });
11546
11359
  if (!chatCheck.allowed) {
11547
11360
  return createPaymentRequiredResponse(
@@ -11600,7 +11413,7 @@ function createStreamHandler(configOrPlugin, options) {
11600
11413
  const storage = new DatabaseStorage(plugin.getConfig());
11601
11414
  const onAuthorize = options == null ? void 0 : options.onAuthorize;
11602
11415
  return async function POST(req, context) {
11603
- var _a3, _b, _c;
11416
+ var _a2, _b;
11604
11417
  const authResult = await checkAuth(req, onAuthorize);
11605
11418
  if (authResult) return authResult;
11606
11419
  const rateLimited = checkRateLimit(req);
@@ -11608,10 +11421,6 @@ function createStreamHandler(configOrPlugin, options) {
11608
11421
  const config = plugin.getConfig();
11609
11422
  let body;
11610
11423
  try {
11611
- if ((_a3 = req.headers.get("content-type")) == null ? void 0 : _a3.includes("multipart/form-data")) {
11612
- const uploader = createUploadHandler(plugin, { onAuthorize });
11613
- return uploader(req);
11614
- }
11615
11424
  body = await req.json();
11616
11425
  } catch (e) {
11617
11426
  return new Response(JSON.stringify({ error: { code: "INVALID_JSON", message: "Invalid JSON body" } }), {
@@ -11624,15 +11433,15 @@ function createStreamHandler(configOrPlugin, options) {
11624
11433
  if (!licenseCheck.ok) return licenseCheck.response;
11625
11434
  if (isFreeTier(licenseCheck.payload.tier)) {
11626
11435
  const estimatedInputTokens = Math.ceil(
11627
- ((((_b = bodyObj.message) == null ? void 0 : _b.length) || 0) + (Array.isArray(bodyObj.messages) ? bodyObj.messages.reduce((a, m) => {
11628
- var _a4, _b2;
11629
- 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);
11630
- }, 0) : 0) + (((_c = bodyObj.history) == null ? void 0 : _c.reduce((a, m) => {
11631
- var _a4, _b2;
11632
- 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);
11436
+ ((((_a2 = bodyObj.message) == null ? void 0 : _a2.length) || 0) + (Array.isArray(bodyObj.messages) ? bodyObj.messages.reduce((a, m) => {
11437
+ var _a3, _b2;
11438
+ 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);
11439
+ }, 0) : 0) + (((_b = bodyObj.history) == null ? void 0 : _b.reduce((a, m) => {
11440
+ var _a3, _b2;
11441
+ 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);
11633
11442
  }, 0)) || 0)) / 4
11634
11443
  );
11635
- const chatCheck = FreeTierLimitsGuard.checkRequestAllowed({
11444
+ const chatCheck = FreeTierLimitsGuard_exports.FreeTierLimitsGuard.checkRequestAllowed({
11636
11445
  operationType: "chat_stream",
11637
11446
  inputTokens: estimatedInputTokens,
11638
11447
  projectId: licenseCheck.payload.projectId
@@ -11675,7 +11484,7 @@ function createStreamHandler(configOrPlugin, options) {
11675
11484
  let isActive = true;
11676
11485
  const stream = new ReadableStream({
11677
11486
  async start(controller) {
11678
- var _a4, _b2, _c2, _d, _e;
11487
+ var _a3, _b2, _c, _d, _e;
11679
11488
  const enqueue = (text) => {
11680
11489
  if (!isActive) return;
11681
11490
  try {
@@ -11708,7 +11517,7 @@ function createStreamHandler(configOrPlugin, options) {
11708
11517
  let uiTransformation = responseChunk == null ? void 0 : responseChunk.ui_transformation;
11709
11518
  if (sources.length > 0) {
11710
11519
  try {
11711
- uiTransformation = (_a4 = responseChunk == null ? void 0 : responseChunk.ui_transformation) != null ? _a4 : UITransformer.transform(message, sources, plugin.getConfig());
11520
+ uiTransformation = (_a3 = responseChunk == null ? void 0 : responseChunk.ui_transformation) != null ? _a3 : UITransformer.transform(message, sources, plugin.getConfig());
11712
11521
  if (uiTransformation) {
11713
11522
  enqueue(sseUIFrame(uiTransformation));
11714
11523
  }
@@ -11732,7 +11541,7 @@ function createStreamHandler(configOrPlugin, options) {
11732
11541
  uiTransformation,
11733
11542
  trace: responseChunk == null ? void 0 : responseChunk.trace
11734
11543
  }).catch((err) => console.warn("[createStreamHandler] Failed to save assistant message:", err));
11735
- 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);
11544
+ 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);
11736
11545
  }
11737
11546
  }
11738
11547
  } catch (temp) {
@@ -11797,7 +11606,7 @@ function createIngestHandler(configOrPlugin, options) {
11797
11606
  (sum, d) => sum + Buffer.byteLength(d.content || "", "utf8"),
11798
11607
  0
11799
11608
  );
11800
- const ingestCheck = FreeTierLimitsGuard.checkIngestionAllowed(
11609
+ const ingestCheck = FreeTierLimitsGuard_exports.FreeTierLimitsGuard.checkIngestionAllowed(
11801
11610
  { documentCount: documents.length },
11802
11611
  totalIncomingBytes
11803
11612
  );
@@ -11849,7 +11658,6 @@ function createLicenseHandler(configOrPlugin, options) {
11849
11658
  const plugin = getOrCreatePlugin(configOrPlugin);
11850
11659
  const onAuthorize = options == null ? void 0 : options.onAuthorize;
11851
11660
  return async function POST(req) {
11852
- var _a3;
11853
11661
  if (req) {
11854
11662
  const authResult = await checkAuth(req, onAuthorize);
11855
11663
  if (authResult) return authResult;
@@ -11857,7 +11665,17 @@ function createLicenseHandler(configOrPlugin, options) {
11857
11665
  try {
11858
11666
  const body = await req.json().catch(() => ({}));
11859
11667
  const config = plugin.getConfig();
11860
- 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 || "";
11668
+ const authHeader = req.headers.get("authorization");
11669
+ let authHeaderKey = "";
11670
+ if (authHeader && authHeader.toLowerCase().startsWith("bearer ")) {
11671
+ authHeaderKey = authHeader.replace(/^Bearer\s+/i, "");
11672
+ if (authHeaderKey) {
11673
+ console.warn(
11674
+ '[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.'
11675
+ );
11676
+ }
11677
+ }
11678
+ const rawKey = req.headers.get("x-license-key") || (body == null ? void 0 : body.licenseKey) || authHeaderKey || config.licenseKey || process.env.RETRIVORA_LICENSE_KEY || "";
11861
11679
  const licenseKey = (rawKey || "").trim().replace(/^["']|["']$/g, "").trim();
11862
11680
  const projectId = (body == null ? void 0 : body.projectId) || config.projectId || "";
11863
11681
  const payload = await LicenseVerifier.verifyAsync(licenseKey, projectId);
@@ -11908,7 +11726,7 @@ function createUploadHandler(configOrPlugin, options) {
11908
11726
  }
11909
11727
  if (isFreeTier(licenseCheck.payload.tier)) {
11910
11728
  const totalUploadBytes = files.reduce((sum, f) => sum + (f.size || 0), 0);
11911
- const uploadCheck = FreeTierLimitsGuard.checkIngestionAllowed(
11729
+ const uploadCheck = FreeTierLimitsGuard_exports.FreeTierLimitsGuard.checkIngestionAllowed(
11912
11730
  { documentCount: files.length },
11913
11731
  totalUploadBytes
11914
11732
  );
@@ -11920,8 +11738,8 @@ function createUploadHandler(configOrPlugin, options) {
11920
11738
  projectId: licenseCheck.payload.projectId,
11921
11739
  fileCount: files.length,
11922
11740
  totalUploadBytes,
11923
- maxFileSizeBytes: FREE_TIER_QUOTAS.MAX_FILE_SIZE_BYTES,
11924
- maxStorageBytes: FREE_TIER_QUOTAS.MAX_STORAGE_BYTES
11741
+ maxFileSizeBytes: FreeTierLimitsGuard_exports.FREE_TIER_QUOTAS.MAX_FILE_SIZE_BYTES,
11742
+ maxStorageBytes: FreeTierLimitsGuard_exports.FREE_TIER_QUOTAS.MAX_STORAGE_BYTES
11925
11743
  }
11926
11744
  );
11927
11745
  }
@@ -12235,7 +12053,7 @@ function createRagHandler(configOrPlugin, options) {
12235
12053
  }
12236
12054
  }
12237
12055
  async function getSegment(req, context) {
12238
- var _a3, _b, _c;
12056
+ var _a2, _b, _c;
12239
12057
  const contentType = req.headers.get("content-type") || "";
12240
12058
  if (contentType.includes("multipart/form-data")) {
12241
12059
  return "upload";
@@ -12254,7 +12072,7 @@ function createRagHandler(configOrPlugin, options) {
12254
12072
  if (pathname.endsWith("/sessions")) return "sessions";
12255
12073
  } catch (e) {
12256
12074
  }
12257
- 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;
12075
+ 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;
12258
12076
  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) || [];
12259
12077
  const joined = segments.join("/");
12260
12078
  return joined || "chat";
@@ -12332,7 +12150,6 @@ var _LicenseValidator = class _LicenseValidator {
12332
12150
  * lock the chat widget.
12333
12151
  */
12334
12152
  async validate(request) {
12335
- var _a3;
12336
12153
  const {
12337
12154
  licenseKey,
12338
12155
  projectId,
@@ -12351,7 +12168,17 @@ var _LicenseValidator = class _LicenseValidator {
12351
12168
  }
12352
12169
  return void 0;
12353
12170
  };
12354
- const rawKey = licenseKey || getHeader("x-license-key") || ((_a3 = getHeader("authorization")) == null ? void 0 : _a3.replace(/^Bearer\s+/i, "")) || (typeof process !== "undefined" ? process.env.NEXT_PUBLIC_RETRIVORA_LICENSE_KEY || process.env.RETRIVORA_LICENSE_KEY : "") || "";
12171
+ const authHeader = getHeader("authorization");
12172
+ let authHeaderKey = "";
12173
+ if (authHeader && authHeader.toLowerCase().startsWith("bearer ")) {
12174
+ authHeaderKey = authHeader.replace(/^Bearer\s+/i, "");
12175
+ if (authHeaderKey) {
12176
+ console.warn(
12177
+ '[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.'
12178
+ );
12179
+ }
12180
+ }
12181
+ const rawKey = licenseKey || getHeader("x-license-key") || authHeaderKey || (typeof process !== "undefined" ? process.env.NEXT_PUBLIC_RETRIVORA_LICENSE_KEY || process.env.RETRIVORA_LICENSE_KEY : "") || "";
12355
12182
  const effectiveLicenseKey = (rawKey || "").trim().replace(/^["']|["']$/g, "").trim();
12356
12183
  if (!effectiveLicenseKey) {
12357
12184
  this.purgeCache(effectiveLicenseKey);
@@ -12623,7 +12450,6 @@ LicenseConfigResolver.CACHE_TTL_MS = 36e5;
12623
12450
  LicenseConfigResolver.configCache = /* @__PURE__ */ new Map();
12624
12451
 
12625
12452
  // src/telemetry/TelemetryService.ts
12626
- import axios10 from "axios";
12627
12453
  var EventType = /* @__PURE__ */ ((EventType2) => {
12628
12454
  EventType2["EMBEDDING_GENERATED"] = "embedding_generated";
12629
12455
  EventType2["LLM_CHAT_COMPLETION"] = "llm_chat_completion";
@@ -12643,14 +12469,11 @@ var _TelemetryService = class _TelemetryService {
12643
12469
  this.projectId = projectId;
12644
12470
  this.licenseKey = licenseKey;
12645
12471
  this.userTelemetryUrl = userTelemetryUrl || process.env[_TelemetryService.USER_TELEMETRY_URL_ENV_KEY];
12646
- this.http = axios10.create({
12647
- timeout: 5e3,
12648
- headers: {
12649
- "Content-Type": "application/json",
12650
- "X-License-Key": licenseKey,
12651
- "X-Project-ID": projectId
12652
- }
12653
- });
12472
+ this.baseHeaders = {
12473
+ "Content-Type": "application/json",
12474
+ "X-License-Key": licenseKey,
12475
+ "X-Project-ID": projectId
12476
+ };
12654
12477
  this.startAutoFlush();
12655
12478
  }
12656
12479
  /**
@@ -12658,7 +12481,7 @@ var _TelemetryService = class _TelemetryService {
12658
12481
  */
12659
12482
  recordEvent(event) {
12660
12483
  if (!this.enabled) return;
12661
- const _a3 = event, { licenseKey } = _a3, safeEvent = __objRest(_a3, ["licenseKey"]);
12484
+ const _a2 = event, { licenseKey } = _a2, safeEvent = __objRest(_a2, ["licenseKey"]);
12662
12485
  const completeEvent = __spreadValues({
12663
12486
  eventType: event.eventType || "error_occurred" /* ERROR_OCCURRED */,
12664
12487
  projectId: this.projectId,
@@ -12743,26 +12566,33 @@ var _TelemetryService = class _TelemetryService {
12743
12566
  if (this.eventQueue.length === 0) return;
12744
12567
  const events = this.eventQueue.splice(0, this.batchSize);
12745
12568
  const payload = {
12746
- events: events.map((e) => {
12747
- const eventWithoutKey = __objRest(e, []);
12748
- return eventWithoutKey;
12749
- }),
12569
+ events: events.map((e) => __spreadValues({}, e)),
12750
12570
  batchId: `${this.projectId}_${Date.now()}`
12751
12571
  };
12752
12572
  try {
12753
- await this.http.post(_TelemetryService.RETRIVORA_TELEMETRY_URL, payload);
12573
+ const res = await fetch(_TelemetryService.RETRIVORA_TELEMETRY_URL, {
12574
+ method: "POST",
12575
+ headers: this.baseHeaders,
12576
+ body: JSON.stringify(payload),
12577
+ signal: AbortSignal.timeout(5e3)
12578
+ });
12579
+ if (!res.ok) {
12580
+ throw new Error(`HTTP ${res.status}`);
12581
+ }
12754
12582
  } catch (err) {
12755
12583
  console.warn("[TelemetryService] Failed to send telemetry to Retrivora:", err.message);
12756
12584
  this.eventQueue.unshift(...events);
12757
12585
  }
12758
12586
  if (this.userTelemetryUrl) {
12759
12587
  try {
12760
- await axios10.post(this.userTelemetryUrl, payload, {
12761
- timeout: 5e3,
12588
+ await fetch(this.userTelemetryUrl, {
12589
+ method: "POST",
12762
12590
  headers: {
12763
12591
  "Content-Type": "application/json",
12764
12592
  "X-Project-ID": this.projectId
12765
- }
12593
+ },
12594
+ body: JSON.stringify(payload),
12595
+ signal: AbortSignal.timeout(5e3)
12766
12596
  });
12767
12597
  } catch (err) {
12768
12598
  console.warn("[TelemetryService] Failed to send telemetry to user endpoint:", err.message);
@@ -12837,7 +12667,9 @@ var LicensedRetrivora = class {
12837
12667
  }
12838
12668
  }
12839
12669
  /**
12840
- * Initialize the SDK by validating license and fetching config
12670
+ * Generates a unique ID for correlating telemetry events.
12671
+ * Format: `{timestamp}-{random}` — not cryptographically secure, used for
12672
+ * tracing only.
12841
12673
  */
12842
12674
  generateId() {
12843
12675
  return `${Date.now()}-${Math.random().toString(36).substring(2, 9)}`;
@@ -12943,7 +12775,7 @@ var LicensedRetrivora = class {
12943
12775
  * Returns only the response text and sources (abstracted from underlying models)
12944
12776
  */
12945
12777
  async query(queryText, options) {
12946
- var _a3;
12778
+ var _a2;
12947
12779
  if (!this.initialized || !this.pipeline) {
12948
12780
  throw new Error("[LicensedRetrivora] SDK not initialized. Call initialize() first.");
12949
12781
  }
@@ -12951,19 +12783,21 @@ var LicensedRetrivora = class {
12951
12783
  const telemetry = getTelemetryService();
12952
12784
  const startTime = Date.now();
12953
12785
  try {
12954
- const response = await this.pipeline.runNormalQuery(queryText);
12786
+ const response = await this.pipeline.ask(queryText);
12955
12787
  const durationMs = Date.now() - startTime;
12956
12788
  if (telemetry) {
12957
12789
  telemetry.recordQueryExecuted({
12958
12790
  queryText,
12959
- resultCount: ((_a3 = response.sources) == null ? void 0 : _a3.length) || 0,
12791
+ resultCount: ((_a2 = response.sources) == null ? void 0 : _a2.length) || 0,
12960
12792
  durationMs,
12961
12793
  operationId
12962
12794
  });
12963
12795
  }
12964
12796
  return {
12965
12797
  reply: response.reply,
12966
- sources: response.sources
12798
+ sources: response.sources,
12799
+ trace: response.trace,
12800
+ ui_transformation: response.ui_transformation
12967
12801
  };
12968
12802
  } catch (error) {
12969
12803
  const durationMs = Date.now() - startTime;
@@ -13002,9 +12836,17 @@ var LicensedRetrivora = class {
13002
12836
  return this.initialized;
13003
12837
  }
13004
12838
  /**
13005
- * Get current RAG configuration (for debugging)
12839
+ * Get current RAG configuration (for debugging).
12840
+ *
12841
+ * Fix DX-3: throws when called before `initialize()` so callers receive a
12842
+ * clear, actionable error instead of a silent `undefined` that causes a
12843
+ * confusing null-reference error downstream. Consistent with the behavior
12844
+ * of `query()` and `ingest()`.
13006
12845
  */
13007
12846
  getConfig() {
12847
+ if (!this.ragConfig) {
12848
+ throw new Error("[LicensedRetrivora] SDK not initialized. Call initialize() first.");
12849
+ }
13008
12850
  return this.ragConfig;
13009
12851
  }
13010
12852
  };