@retrivora-ai/rag-engine 2.4.5 → 2.4.7

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