@retrivora-ai/rag-engine 2.4.5 → 2.4.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -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-
@@ -317,7 +318,7 @@ var init_package = __esm({
317
318
  "package.json"() {
318
319
  package_default = {
319
320
  name: "@retrivora-ai/rag-engine",
320
- version: "2.4.5",
321
+ version: "2.4.6",
321
322
  description: "Retrivora AI is a plug-and-play AI engine for RAG chat experiences \u2014 generic vector DB + LLM provider, embeddable or standalone.",
322
323
  author: "Abhinav Alkuchi",
323
324
  license: "UNLICENSED",
@@ -398,14 +399,19 @@ var init_package = __esm({
398
399
  "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",
399
400
  lint: "eslint",
400
401
  typecheck: "tsc --noEmit -p tsconfig.build.json",
402
+ test: "jest",
401
403
  clean: "rm -rf dist"
402
404
  },
403
405
  peerDependencies: {
406
+ axios: ">=1.0.0",
404
407
  next: ">=15.0.0",
405
408
  react: ">=18.0.0",
406
409
  "react-dom": ">=18.0.0"
407
410
  },
408
411
  peerDependenciesMeta: {
412
+ axios: {
413
+ optional: true
414
+ },
409
415
  next: {
410
416
  optional: true
411
417
  }
@@ -414,9 +420,9 @@ var init_package = __esm({
414
420
  "@anthropic-ai/sdk": "^0.95.1",
415
421
  "@google/genai": "^0.8.0",
416
422
  "@google/generative-ai": "^0.24.1",
423
+ "@retrivora-ai/billing-guards": "*",
417
424
  "@pinecone-database/pinecone": "^7.2.0",
418
425
  "@types/papaparse": "^5.5.2",
419
- axios: "^1.15.0",
420
426
  "lucide-react": "^1.8.0",
421
427
  "next-themes": "^0.4.6",
422
428
  openai: "^6.34.0",
@@ -441,16 +447,20 @@ var init_package = __esm({
441
447
  "@tailwindcss/cli": "^4",
442
448
  "@tailwindcss/postcss": "^4",
443
449
  "@types/estree": "^1.0.9",
450
+ "@types/jest": "^30.0.0",
444
451
  "@types/node": "^20",
445
452
  "@types/pdf-parse": "^1.1.5",
446
453
  "@types/pg": "^8.20.0",
447
454
  "@types/react": "^19.2.14",
448
455
  "@types/react-dom": "^19.2.3",
456
+ axios: "^1.15.0",
449
457
  dotenv: "^17.4.2",
450
458
  eslint: "^9",
451
459
  "eslint-config-next": "16.2.4",
460
+ jest: "^30.4.2",
452
461
  next: "16.2.10",
453
462
  tailwindcss: "^4",
463
+ "ts-jest": "^29.4.12",
454
464
  tsup: "^8.5.1",
455
465
  typescript: "^5"
456
466
  }
@@ -493,7 +503,7 @@ var init_ConfigFetcher = __esm({
493
503
  * PINECONE_API_KEY if available, or vector operations are handled server-side.
494
504
  */
495
505
  static async fetchRemoteConfig(projectId, licenseKey) {
496
- var _a3, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k;
506
+ var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k;
497
507
  const cacheKey = `${projectId}:${licenseKey || ""}`;
498
508
  const now = Date.now();
499
509
  const cached = this.cache.get(cacheKey);
@@ -538,7 +548,7 @@ var init_ConfigFetcher = __esm({
538
548
  indexName: data.vectorDb.indexName || "retrivora-free",
539
549
  provider: data.vectorDb.provider || "pinecone",
540
550
  projectId: fetchedProjectId,
541
- namespace: ((_a3 = data.workspace) == null ? void 0 : _a3.namespace) || data.vectorDb.namespace || fetchedProjectId
551
+ namespace: ((_a2 = data.workspace) == null ? void 0 : _a2.namespace) || data.vectorDb.namespace || fetchedProjectId
542
552
  },
543
553
  embedding: {
544
554
  provider: ((_b = data.embedding) == null ? void 0 : _b.provider) || "universal_rest",
@@ -595,16 +605,20 @@ var init_ConfigFetcher = __esm({
595
605
  return config;
596
606
  }
597
607
  }
598
- } catch (e) {
608
+ } catch (err) {
609
+ console.warn(
610
+ `[ConfigFetcher] Failed to fetch config from ${baseUrl}:`,
611
+ err instanceof Error ? err.message : String(err)
612
+ );
599
613
  }
600
614
  }
601
615
  return null;
602
616
  }
603
617
  /** Convenience: fetch only vector DB config (backwards compat). */
604
618
  static async fetchRemoteVectorConfig(projectId, licenseKey) {
605
- var _a3;
619
+ var _a2;
606
620
  const config = await this.fetchRemoteConfig(projectId, licenseKey);
607
- return (_a3 = config == null ? void 0 : config.vectorDb) != null ? _a3 : null;
621
+ return (_a2 = config == null ? void 0 : config.vectorDb) != null ? _a2 : null;
608
622
  }
609
623
  };
610
624
  ConfigFetcher.cache = /* @__PURE__ */ new Map();
@@ -644,7 +658,7 @@ var init_PineconeProvider = __esm({
644
658
  const errors = [];
645
659
  const opts = config.options || {};
646
660
  const hasLicenseKey = Boolean(
647
- config.licenseKey || process.env.RETRIVORA_LICENSE_KEY || process.env.NEXT_PUBLIC_RETRIVORA_LICENSE_KEY
661
+ config.licenseKey || process.env.RETRIVORA_LICENSE_KEY
648
662
  );
649
663
  if (!opts.apiKey && !hasLicenseKey) {
650
664
  errors.push({
@@ -661,7 +675,7 @@ var init_PineconeProvider = __esm({
661
675
  static getHealthChecker() {
662
676
  return {
663
677
  async check(config) {
664
- var _a3, _b;
678
+ var _a2, _b;
665
679
  const opts = config.options || {};
666
680
  const indexName = config.indexName;
667
681
  const timestamp = Date.now();
@@ -669,7 +683,7 @@ var init_PineconeProvider = __esm({
669
683
  const { Pinecone: Pinecone2 } = await import("@pinecone-database/pinecone");
670
684
  const client = new Pinecone2({ apiKey: opts.apiKey });
671
685
  const indexes = await client.listIndexes();
672
- const indexNames = (_b = (_a3 = indexes.indexes) == null ? void 0 : _a3.map((i) => i.name)) != null ? _b : [];
686
+ const indexNames = (_b = (_a2 = indexes.indexes) == null ? void 0 : _a2.map((i) => i.name)) != null ? _b : [];
673
687
  if (!indexNames.includes(indexName)) {
674
688
  return {
675
689
  healthy: false,
@@ -696,25 +710,13 @@ var init_PineconeProvider = __esm({
696
710
  };
697
711
  }
698
712
  async initialize() {
699
- var _a3, _b;
713
+ var _a2, _b;
700
714
  if (this.client) return;
701
715
  let key = this.apiKey || process.env.PINECONE_API_KEY || "";
702
716
  if (!key) {
703
- const licenseKey = process.env.RETRIVORA_LICENSE_KEY || process.env.NEXT_PUBLIC_RETRIVORA_LICENSE_KEY;
717
+ const licenseKey = process.env.RETRIVORA_LICENSE_KEY;
704
718
  this.licenseKey = licenseKey;
705
- const jwtProjectId = (() => {
706
- if (!licenseKey) return void 0;
707
- try {
708
- const parts = licenseKey.split(".");
709
- if (parts.length >= 2) {
710
- const payload = JSON.parse(Buffer.from(parts[1], "base64url").toString("utf8"));
711
- return payload == null ? void 0 : payload.projectId;
712
- }
713
- } catch (e) {
714
- }
715
- return void 0;
716
- })();
717
- const projectId = process.env.RAG_PROJECT_ID || process.env.NEXT_PUBLIC_RAG_PROJECT_ID || jwtProjectId || this.projectId || "default";
719
+ const projectId = process.env.RAG_PROJECT_ID || process.env.NEXT_PUBLIC_RAG_PROJECT_ID || this.projectId || "default";
718
720
  this.projectId = projectId;
719
721
  const remoteConfig = await ConfigFetcher.fetchRemoteVectorConfig(projectId, licenseKey);
720
722
  if (remoteConfig) {
@@ -742,7 +744,7 @@ var init_PineconeProvider = __esm({
742
744
  this.client = new import_pinecone.Pinecone({ apiKey: key });
743
745
  try {
744
746
  const indexes = await this.client.listIndexes();
745
- const names = (_b = (_a3 = indexes.indexes) == null ? void 0 : _a3.map((i) => i.name)) != null ? _b : [];
747
+ const names = (_b = (_a2 = indexes.indexes) == null ? void 0 : _a2.map((i) => i.name)) != null ? _b : [];
746
748
  if (!names.includes(this.indexName)) {
747
749
  console.warn(`[PineconeProvider] Target index "${this.indexName}" not listed. Proceeding with configured index name.`);
748
750
  }
@@ -761,7 +763,7 @@ var init_PineconeProvider = __esm({
761
763
  return namespace ? idx.namespace(namespace) : idx.namespace("");
762
764
  }
763
765
  async upsert(doc, namespace) {
764
- var _a3, _b;
766
+ var _a2, _b;
765
767
  if (!this.client) {
766
768
  if (!this.licenseKey) throw new Error("[PineconeProvider] Cannot execute vector operation: Missing API key and License key.");
767
769
  const res = await fetch(`${this.getControlPlaneUrl()}/api/v1/vectors/upsert`, {
@@ -771,7 +773,7 @@ var init_PineconeProvider = __esm({
771
773
  projectId: this.projectId,
772
774
  indexName: this.indexName,
773
775
  namespace: namespace || this.projectId,
774
- records: [{ id: String(doc.id), values: doc.vector, metadata: __spreadValues({ content: doc.content }, (_a3 = doc.metadata) != null ? _a3 : {}) }]
776
+ records: [{ id: String(doc.id), values: doc.vector, metadata: __spreadValues({ content: doc.content }, (_a2 = doc.metadata) != null ? _a2 : {}) }]
775
777
  })
776
778
  });
777
779
  if (!res.ok) throw new Error(`[PineconeProvider] Proxy upsert failed: ${await res.text()}`);
@@ -792,11 +794,11 @@ var init_PineconeProvider = __esm({
792
794
  if (!this.licenseKey) throw new Error("[PineconeProvider] Cannot execute vector operation: Missing API key and License key.");
793
795
  for (let i = 0; i < docs.length; i += BATCH) {
794
796
  const records = docs.slice(i, i + BATCH).map((d) => {
795
- var _a3;
797
+ var _a2;
796
798
  return {
797
799
  id: String(d.id),
798
800
  values: d.vector,
799
- metadata: __spreadValues({ content: d.content }, (_a3 = d.metadata) != null ? _a3 : {})
801
+ metadata: __spreadValues({ content: d.content }, (_a2 = d.metadata) != null ? _a2 : {})
800
802
  };
801
803
  });
802
804
  const res = await fetch(`${this.getControlPlaneUrl()}/api/v1/vectors/upsert`, {
@@ -816,18 +818,18 @@ var init_PineconeProvider = __esm({
816
818
  const targetIdx = await this.getActiveIndex(namespace);
817
819
  for (let i = 0; i < docs.length; i += BATCH) {
818
820
  const records = docs.slice(i, i + BATCH).map((d) => {
819
- var _a3;
821
+ var _a2;
820
822
  return {
821
823
  id: String(d.id),
822
824
  values: d.vector,
823
- metadata: __spreadValues({ content: d.content }, (_a3 = d.metadata) != null ? _a3 : {})
825
+ metadata: __spreadValues({ content: d.content }, (_a2 = d.metadata) != null ? _a2 : {})
824
826
  };
825
827
  });
826
828
  await targetIdx.upsert({ records });
827
829
  }
828
830
  }
829
831
  async query(vector, topK, namespace, filter) {
830
- var _a3, _b;
832
+ var _a2, _b;
831
833
  const pineconeFilter = this.sanitizeFilter(filter);
832
834
  if (!this.client) {
833
835
  if (!this.licenseKey) throw new Error("[PineconeProvider] Cannot execute vector operation: Missing API key and License key.");
@@ -845,11 +847,11 @@ var init_PineconeProvider = __esm({
845
847
  });
846
848
  if (!res.ok) throw new Error(`[PineconeProvider] Proxy query failed: ${await res.text()}`);
847
849
  const data = await res.json();
848
- return ((_a3 = data.matches) != null ? _a3 : []).map((m) => {
849
- var _a4, _b2;
850
+ return ((_a2 = data.matches) != null ? _a2 : []).map((m) => {
851
+ var _a3, _b2;
850
852
  return {
851
853
  id: m.id,
852
- score: (_a4 = m.score) != null ? _a4 : 0,
854
+ score: (_a3 = m.score) != null ? _a3 : 0,
853
855
  content: ((_b2 = m.metadata) == null ? void 0 : _b2.content) || "",
854
856
  metadata: m.metadata
855
857
  };
@@ -862,10 +864,10 @@ var init_PineconeProvider = __esm({
862
864
  includeMetadata: true
863
865
  }, Object.keys(pineconeFilter).length > 0 ? { filter: pineconeFilter } : {}));
864
866
  return ((_b = result.matches) != null ? _b : []).map((m) => {
865
- var _a4, _b2;
867
+ var _a3, _b2;
866
868
  return {
867
869
  id: m.id,
868
- score: (_a4 = m.score) != null ? _a4 : 0,
870
+ score: (_a3 = m.score) != null ? _a3 : 0,
869
871
  content: ((_b2 = m.metadata) == null ? void 0 : _b2.content) || "",
870
872
  metadata: m.metadata
871
873
  };
@@ -908,13 +910,13 @@ var init_PostgreSQLProvider = __esm({
908
910
  init_BaseVectorProvider();
909
911
  PostgreSQLProvider = class extends BaseVectorProvider {
910
912
  constructor(config) {
911
- var _a3;
913
+ var _a2;
912
914
  super(config);
913
915
  this.tableName = this.indexName.replace(/[^a-z0-9_]/gi, "_");
914
916
  const opts = config.options;
915
917
  if (!opts.connectionString) throw new Error("[PostgreSQLProvider] options.connectionString is required");
916
918
  this.connectionString = opts.connectionString;
917
- this.dimensions = (_a3 = opts.dimensions) != null ? _a3 : 1536;
919
+ this.dimensions = (_a2 = opts.dimensions) != null ? _a2 : 1536;
918
920
  }
919
921
  static getValidator() {
920
922
  return {
@@ -995,7 +997,7 @@ var init_PostgreSQLProvider = __esm({
995
997
  }
996
998
  }
997
999
  async upsert(doc, namespace = "") {
998
- var _a3;
1000
+ var _a2;
999
1001
  const vectorLiteral = `[${doc.vector.join(",")}]`;
1000
1002
  await this.pool.query(
1001
1003
  `INSERT INTO ${this.tableName} (id, namespace, content, metadata, embedding)
@@ -1005,7 +1007,7 @@ var init_PostgreSQLProvider = __esm({
1005
1007
  content = EXCLUDED.content,
1006
1008
  metadata = EXCLUDED.metadata,
1007
1009
  embedding = EXCLUDED.embedding`,
1008
- [doc.id, namespace, doc.content, JSON.stringify((_a3 = doc.metadata) != null ? _a3 : {}), vectorLiteral]
1010
+ [doc.id, namespace, doc.content, JSON.stringify((_a2 = doc.metadata) != null ? _a2 : {}), vectorLiteral]
1009
1011
  );
1010
1012
  }
1011
1013
  async batchUpsert(docs, namespace = "") {
@@ -1018,9 +1020,9 @@ var init_PostgreSQLProvider = __esm({
1018
1020
  const batch = docs.slice(i, i + BATCH_SIZE);
1019
1021
  const values = [];
1020
1022
  const valuePlaceholders = batch.map((doc, idx) => {
1021
- var _a3;
1023
+ var _a2;
1022
1024
  const offset = idx * 5;
1023
- values.push(doc.id, namespace, doc.content, JSON.stringify((_a3 = doc.metadata) != null ? _a3 : {}), `[${doc.vector.join(",")}]`);
1025
+ values.push(doc.id, namespace, doc.content, JSON.stringify((_a2 = doc.metadata) != null ? _a2 : {}), `[${doc.vector.join(",")}]`);
1024
1026
  return `($${offset + 1}, $${offset + 2}, $${offset + 3}, $${offset + 4}, $${offset + 5}::vector)`;
1025
1027
  }).join(", ");
1026
1028
  const query = `
@@ -1103,8 +1105,8 @@ var init_PostgreSQLProvider = __esm({
1103
1105
 
1104
1106
  // src/utils/synonyms.ts
1105
1107
  function resolveMetadataValue(meta, uiKey) {
1106
- var _a3;
1107
- const synonyms = (_a3 = FIELD_SYNONYMS[uiKey]) != null ? _a3 : [];
1108
+ var _a2;
1109
+ const synonyms = (_a2 = FIELD_SYNONYMS[uiKey]) != null ? _a2 : [];
1108
1110
  const keys = Object.keys(meta);
1109
1111
  const systemKeys = ["namespace", "filename", "filesize", "filetype", "docid", "uploadedat", "dimension", "chunkindex"];
1110
1112
  let match = keys.find((k) => k.toLowerCase() === uiKey.toLowerCase());
@@ -1191,14 +1193,14 @@ var init_MultiTablePostgresProvider = __esm({
1191
1193
  init_synonyms();
1192
1194
  MultiTablePostgresProvider = class extends BaseVectorProvider {
1193
1195
  constructor(config) {
1194
- var _a3, _b, _c;
1196
+ var _a2, _b, _c;
1195
1197
  super(config);
1196
1198
  const opts = config.options || {};
1197
1199
  if (!opts.connectionString) {
1198
1200
  throw new Error("[MultiTablePostgresProvider] options.connectionString is required");
1199
1201
  }
1200
1202
  this.connectionString = opts.connectionString;
1201
- this.dimensions = (_a3 = opts.dimensions) != null ? _a3 : 768;
1203
+ this.dimensions = (_a2 = opts.dimensions) != null ? _a2 : 768;
1202
1204
  this.tables = [];
1203
1205
  const rawSearchFields = (_c = (_b = opts.searchFields) != null ? _b : process.env.VECTOR_DB_SEARCH_FIELDS) != null ? _c : ["name", "product_name", "productname", "title"];
1204
1206
  this.searchFields = typeof rawSearchFields === "string" ? rawSearchFields.split(",").map((f) => f.trim()).filter(Boolean) : rawSearchFields;
@@ -1237,7 +1239,6 @@ var init_MultiTablePostgresProvider = __esm({
1237
1239
  this.tables = res.rows.map((r) => r.table_name);
1238
1240
  if (this.tables.length === 0) {
1239
1241
  console.warn('[MultiTablePostgresProvider] No tables with "embedding" columns found in the database.');
1240
- } else {
1241
1242
  }
1242
1243
  } finally {
1243
1244
  client.release();
@@ -1253,11 +1254,11 @@ var init_MultiTablePostgresProvider = __esm({
1253
1254
  * Batch upsert documents by dynamically provisioning tables and columns based on CSV headers.
1254
1255
  */
1255
1256
  async batchUpsert(docs, namespace = "") {
1256
- var _a3;
1257
+ var _a2;
1257
1258
  if (docs.length === 0) return;
1258
1259
  const docsByFile = {};
1259
1260
  for (const doc of docs) {
1260
- const fileName = ((_a3 = doc.metadata) == null ? void 0 : _a3.fileName) || this.uploadTable;
1261
+ const fileName = ((_a2 = doc.metadata) == null ? void 0 : _a2.fileName) || this.uploadTable;
1261
1262
  if (!docsByFile[fileName]) docsByFile[fileName] = [];
1262
1263
  docsByFile[fileName].push(doc);
1263
1264
  }
@@ -1310,11 +1311,11 @@ var init_MultiTablePostgresProvider = __esm({
1310
1311
  const headerColumns = csvHeaders.map((h) => `"${h}"`).join(", ");
1311
1312
  const allColumns = `id, ${headerColumns ? headerColumns + ", " : ""}namespace, content, metadata, embedding`;
1312
1313
  const valuePlaceholders = batch.map((doc, idx) => {
1313
- var _a4, _b;
1314
+ var _a3, _b;
1314
1315
  const offset = idx * (5 + csvHeaders.length);
1315
1316
  values.push(doc.id);
1316
1317
  for (const h of csvHeaders) {
1317
- values.push(String(((_a4 = doc.metadata) == null ? void 0 : _a4[h]) || ""));
1318
+ values.push(String(((_a3 = doc.metadata) == null ? void 0 : _a3[h]) || ""));
1318
1319
  }
1319
1320
  values.push(namespace, doc.content, JSON.stringify((_b = doc.metadata) != null ? _b : {}), `[${doc.vector.join(",")}]`);
1320
1321
  const docPlaceholders = [];
@@ -1353,7 +1354,7 @@ var init_MultiTablePostgresProvider = __esm({
1353
1354
  * Query all configured tables and merge results, sorted by cosine similarity score.
1354
1355
  */
1355
1356
  async query(vector, topK, _namespace, _filter) {
1356
- var _a3;
1357
+ var _a2;
1357
1358
  if (!this.pool) {
1358
1359
  throw new Error("[MultiTablePostgresProvider] Provider not initialized. Call initialize() first.");
1359
1360
  }
@@ -1381,11 +1382,11 @@ var init_MultiTablePostgresProvider = __esm({
1381
1382
  const filterParams = [];
1382
1383
  if (metadataFilters && Object.keys(metadataFilters).length > 0) {
1383
1384
  const conditions = Object.entries(metadataFilters).map(([key, val]) => {
1384
- var _a5;
1385
+ var _a4;
1385
1386
  filterParams.push(String(val));
1386
1387
  const baseOffset = queryText && dynamicKeywordQuery ? 2 : 1;
1387
1388
  const paramIdx = baseOffset + filterParams.length;
1388
- const synonyms = (_a5 = FIELD_SYNONYMS[key]) != null ? _a5 : [];
1389
+ const synonyms = (_a4 = FIELD_SYNONYMS[key]) != null ? _a4 : [];
1389
1390
  const keysToCheck = [key, ...synonyms];
1390
1391
  const coalesceExprs = keysToCheck.flatMap((k) => [
1391
1392
  `metadata->>'${k}'`,
@@ -1452,7 +1453,7 @@ var init_MultiTablePostgresProvider = __esm({
1452
1453
  }
1453
1454
  const tableResults = [];
1454
1455
  for (const row of result.rows) {
1455
- const _a4 = row, { hybrid_score, id } = _a4, rest = __objRest(_a4, ["hybrid_score", "id"]);
1456
+ const _a3 = row, { hybrid_score, id } = _a3, rest = __objRest(_a3, ["hybrid_score", "id"]);
1456
1457
  delete rest.embedding;
1457
1458
  delete rest.vector_score;
1458
1459
  delete rest.keyword_score;
@@ -1481,10 +1482,10 @@ var init_MultiTablePostgresProvider = __esm({
1481
1482
  }
1482
1483
  allResults.sort((a, b) => b.score - a.score);
1483
1484
  if (allResults.length === 0) return [];
1484
- const bestMatchTable = (_a3 = allResults[0].metadata) == null ? void 0 : _a3.source_table;
1485
+ const bestMatchTable = (_a2 = allResults[0].metadata) == null ? void 0 : _a2.source_table;
1485
1486
  const finalSorted = allResults.filter((res) => {
1486
- var _a4;
1487
- return ((_a4 = res.metadata) == null ? void 0 : _a4.source_table) === bestMatchTable;
1487
+ var _a3;
1488
+ return ((_a3 = res.metadata) == null ? void 0 : _a3.source_table) === bestMatchTable;
1488
1489
  });
1489
1490
  return finalSorted.slice(0, Math.max(topK, 15));
1490
1491
  }
@@ -1882,14 +1883,14 @@ var init_QdrantProvider = __esm({
1882
1883
  * Samples points from the collection to discover available payload fields.
1883
1884
  */
1884
1885
  async discoverSchema() {
1885
- var _a3;
1886
+ var _a2;
1886
1887
  try {
1887
1888
  const { data } = await this.http.post(`/collections/${this.indexName}/points/scroll`, {
1888
1889
  limit: 20,
1889
1890
  with_payload: true,
1890
1891
  with_vector: false
1891
1892
  });
1892
- const points = ((_a3 = data.result) == null ? void 0 : _a3.points) || [];
1893
+ const points = ((_a2 = data.result) == null ? void 0 : _a2.points) || [];
1893
1894
  const keys = /* @__PURE__ */ new Set();
1894
1895
  for (const point of points) {
1895
1896
  const payload = point.payload || {};
@@ -1915,11 +1916,11 @@ var init_QdrantProvider = __esm({
1915
1916
  * Ensures the collection exists. Creates it if missing.
1916
1917
  */
1917
1918
  async ensureCollection() {
1918
- var _a3;
1919
+ var _a2;
1919
1920
  try {
1920
1921
  await this.http.get(`/collections/${this.indexName}`);
1921
1922
  } catch (err) {
1922
- if (import_axios4.default.isAxiosError(err) && ((_a3 = err.response) == null ? void 0 : _a3.status) === 404) {
1923
+ if (import_axios4.default.isAxiosError(err) && ((_a2 = err.response) == null ? void 0 : _a2.status) === 404) {
1923
1924
  const opts = this.config.options;
1924
1925
  const dimensionsForCreate = opts.dimensions || 1536;
1925
1926
  await this.http.put(`/collections/${this.indexName}`, {
@@ -1937,7 +1938,7 @@ var init_QdrantProvider = __esm({
1937
1938
  * Ensures that a payload field has an index.
1938
1939
  */
1939
1940
  async ensureIndex(fieldName, schema = "keyword") {
1940
- var _a3;
1941
+ var _a2;
1941
1942
  let fullPath = fieldName;
1942
1943
  if (!this.isFlatPayload && !fieldName.includes(".") && fieldName !== "namespace" && fieldName !== this.contentField) {
1943
1944
  fullPath = `${this.metadataField}.${fieldName}`;
@@ -1949,7 +1950,7 @@ var init_QdrantProvider = __esm({
1949
1950
  });
1950
1951
  } catch (err) {
1951
1952
  let status;
1952
- if (import_axios4.default.isAxiosError(err)) status = (_a3 = err.response) == null ? void 0 : _a3.status;
1953
+ if (import_axios4.default.isAxiosError(err)) status = (_a2 = err.response) == null ? void 0 : _a2.status;
1953
1954
  if (status === 409) return;
1954
1955
  console.warn(`[QdrantProvider] \u26A0\uFE0F Could not ensure index for "${fullPath}":`, err instanceof Error ? err.message : String(err));
1955
1956
  }
@@ -1978,7 +1979,7 @@ var init_QdrantProvider = __esm({
1978
1979
  await this.http.put(`/collections/${this.indexName}/points`, payload);
1979
1980
  }
1980
1981
  async query(vector, topK, namespace, _filter) {
1981
- var _a3;
1982
+ var _a2;
1982
1983
  const must = [];
1983
1984
  if (namespace) {
1984
1985
  must.push({ key: "namespace", match: { value: namespace } });
@@ -2000,7 +2001,7 @@ var init_QdrantProvider = __esm({
2000
2001
  limit: topK,
2001
2002
  with_payload: true,
2002
2003
  params: {
2003
- hnsw_ef: ((_a3 = this.config.options) == null ? void 0 : _a3.efSearch) || Math.max(topK * 20, 128),
2004
+ hnsw_ef: ((_a2 = this.config.options) == null ? void 0 : _a2.efSearch) || Math.max(topK * 20, 128),
2004
2005
  exact: false
2005
2006
  },
2006
2007
  filter: must.length > 0 ? { must } : void 0
@@ -2095,12 +2096,12 @@ var init_ChromaDBProvider = __esm({
2095
2096
  * Get or create the ChromaDB collection.
2096
2097
  */
2097
2098
  async initialize() {
2098
- var _a3;
2099
+ var _a2;
2099
2100
  try {
2100
2101
  const { data } = await this.http.get(`/api/v1/collections/${this.indexName}`);
2101
2102
  this.collectionId = data.id;
2102
2103
  } catch (err) {
2103
- if (import_axios5.default.isAxiosError(err) && ((_a3 = err.response) == null ? void 0 : _a3.status) === 404) {
2104
+ if (import_axios5.default.isAxiosError(err) && ((_a2 = err.response) == null ? void 0 : _a2.status) === 404) {
2104
2105
  const { data } = await this.http.post("/api/v1/collections", {
2105
2106
  name: this.indexName
2106
2107
  });
@@ -2318,7 +2319,7 @@ var init_WeaviateProvider = __esm({
2318
2319
  await this.http.post("/v1/batch/objects", payload);
2319
2320
  }
2320
2321
  async query(vector, topK, namespace, _filter) {
2321
- var _a3, _b;
2322
+ var _a2, _b;
2322
2323
  const queryText = _filter == null ? void 0 : _filter.queryText;
2323
2324
  const sanitizedFilter = this.sanitizeFilter(_filter);
2324
2325
  const searchParams = queryText ? `hybrid: { query: ${JSON.stringify(queryText)}, alpha: 0.5 }` : `nearVector: { vector: ${JSON.stringify(vector)} }`;
@@ -2344,7 +2345,7 @@ var init_WeaviateProvider = __esm({
2344
2345
  `
2345
2346
  };
2346
2347
  const { data } = await this.http.post("/v1/graphql", graphqlQuery);
2347
- const results = ((_b = (_a3 = data.data) == null ? void 0 : _a3.Get) == null ? void 0 : _b[this.indexName]) || [];
2348
+ const results = ((_b = (_a2 = data.data) == null ? void 0 : _a2.Get) == null ? void 0 : _b[this.indexName]) || [];
2348
2349
  return results.map((res) => ({
2349
2350
  id: res["_additional"].id,
2350
2351
  score: 1 - res["_additional"].distance,
@@ -2426,21 +2427,21 @@ var init_UniversalVectorProvider = __esm({
2426
2427
  }
2427
2428
  }
2428
2429
  async initialize() {
2429
- var _a3;
2430
+ var _a2;
2430
2431
  this.http = import_axios8.default.create({
2431
2432
  baseURL: this.opts.baseUrl,
2432
2433
  headers: __spreadValues({
2433
2434
  "Content-Type": "application/json"
2434
2435
  }, this.opts.headers),
2435
- timeout: (_a3 = this.opts.timeout) != null ? _a3 : 3e4
2436
+ timeout: (_a2 = this.opts.timeout) != null ? _a2 : 3e4
2436
2437
  });
2437
2438
  if (!await this.ping()) {
2438
2439
  throw new Error(`[UniversalVectorProvider] Failed to connect to ${this.opts.baseUrl}`);
2439
2440
  }
2440
2441
  }
2441
2442
  async upsert(doc, namespace) {
2442
- var _a3, _b, _c;
2443
- const endpoint = (_a3 = this.opts.upsertEndpoint) != null ? _a3 : "/upsert";
2443
+ var _a2, _b, _c;
2444
+ const endpoint = (_a2 = this.opts.upsertEndpoint) != null ? _a2 : "/upsert";
2444
2445
  const template = (_b = this.opts.upsertTemplate) != null ? _b : JSON.stringify({
2445
2446
  id: "{{id}}",
2446
2447
  vector: "{{vector}}",
@@ -2473,8 +2474,8 @@ var init_UniversalVectorProvider = __esm({
2473
2474
  }
2474
2475
  }
2475
2476
  async query(vector, topK, namespace, filter) {
2476
- var _a3, _b;
2477
- const endpoint = (_a3 = this.opts.queryEndpoint) != null ? _a3 : "/query";
2477
+ var _a2, _b;
2478
+ const endpoint = (_a2 = this.opts.queryEndpoint) != null ? _a2 : "/query";
2478
2479
  const template = (_b = this.opts.queryTemplate) != null ? _b : JSON.stringify({
2479
2480
  vector: "{{vector}}",
2480
2481
  limit: "{{topK}}",
@@ -2500,9 +2501,9 @@ var init_UniversalVectorProvider = __esm({
2500
2501
  );
2501
2502
  }
2502
2503
  return results.map((item) => {
2503
- var _a4, _b2, _c, _d, _e, _f, _g2;
2504
+ var _a3, _b2, _c, _d, _e, _f, _g2;
2504
2505
  return {
2505
- id: item[(_a4 = this.opts.queryIdField) != null ? _a4 : "id"],
2506
+ id: item[(_a3 = this.opts.queryIdField) != null ? _a3 : "id"],
2506
2507
  score: (_c = item[(_b2 = this.opts.queryScoreField) != null ? _b2 : "score"]) != null ? _c : 0,
2507
2508
  content: (_e = item[(_d = this.opts.queryContentField) != null ? _d : "content"]) != null ? _e : "",
2508
2509
  metadata: (_g2 = item[(_f = this.opts.queryMetadataField) != null ? _f : "metadata"]) != null ? _g2 : {}
@@ -2515,8 +2516,8 @@ var init_UniversalVectorProvider = __esm({
2515
2516
  }
2516
2517
  }
2517
2518
  async delete(id, namespace) {
2518
- var _a3, _b;
2519
- const endpoint = (_a3 = this.opts.deleteEndpoint) != null ? _a3 : "/delete";
2519
+ var _a2, _b;
2520
+ const endpoint = (_a2 = this.opts.deleteEndpoint) != null ? _a2 : "/delete";
2520
2521
  const template = (_b = this.opts.deleteTemplate) != null ? _b : JSON.stringify({
2521
2522
  id: "{{id}}",
2522
2523
  namespace: "{{namespace}}"
@@ -2534,8 +2535,8 @@ var init_UniversalVectorProvider = __esm({
2534
2535
  }
2535
2536
  }
2536
2537
  async deleteNamespace(namespace) {
2537
- var _a3;
2538
- const endpoint = (_a3 = this.opts.deleteNamespaceEndpoint) != null ? _a3 : "/delete-namespace";
2538
+ var _a2;
2539
+ const endpoint = (_a2 = this.opts.deleteNamespaceEndpoint) != null ? _a2 : "/delete-namespace";
2539
2540
  try {
2540
2541
  await this.http.post(endpoint, { namespace });
2541
2542
  } catch (error) {
@@ -2545,9 +2546,9 @@ var init_UniversalVectorProvider = __esm({
2545
2546
  }
2546
2547
  }
2547
2548
  async ping() {
2548
- var _a3;
2549
+ var _a2;
2549
2550
  try {
2550
- const endpoint = (_a3 = this.opts.pingEndpoint) != null ? _a3 : "/health";
2551
+ const endpoint = (_a2 = this.opts.pingEndpoint) != null ? _a2 : "/health";
2551
2552
  const response = await this.http.get(endpoint, { timeout: 5e3 });
2552
2553
  return response.status >= 200 && response.status < 300;
2553
2554
  } catch (e) {
@@ -2701,13 +2702,13 @@ var AuthenticationException = class extends RetrivoraError {
2701
2702
  }
2702
2703
  };
2703
2704
  function wrapError(err, defaultCode, defaultMessage) {
2704
- var _a3;
2705
+ var _a2;
2705
2706
  if (err instanceof RetrivoraError) {
2706
2707
  return err;
2707
2708
  }
2708
2709
  const error = err;
2709
2710
  const message = (error == null ? void 0 : error.message) || defaultMessage || String(err);
2710
- 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);
2711
+ 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);
2711
2712
  const code = error == null ? void 0 : error.code;
2712
2713
  if (status === 429 || /rate[- ]?limit/i.test(message) || code === "RATE_LIMIT_EXCEEDED") {
2713
2714
  return new RateLimitException(message, err);
@@ -2988,8 +2989,8 @@ var UI_BORDER_RADIUS_OPTIONS = ["none", "sm", "md", "lg", "xl", "full"];
2988
2989
  // src/config/serverConfig.ts
2989
2990
  var _runtimeConfig = null;
2990
2991
  function readString(env, name) {
2991
- var _a3;
2992
- const value = (_a3 = env[name]) == null ? void 0 : _a3.trim();
2992
+ var _a2;
2993
+ const value = (_a2 = env[name]) == null ? void 0 : _a2.trim();
2993
2994
  return value ? value : void 0;
2994
2995
  }
2995
2996
  function readNumber(env, name, fallback) {
@@ -3002,66 +3003,60 @@ function readNumber(env, name, fallback) {
3002
3003
  return parsed;
3003
3004
  }
3004
3005
  function readEnum(env, name, fallback, allowed) {
3005
- var _a3;
3006
- const value = (_a3 = readString(env, name)) != null ? _a3 : fallback;
3006
+ var _a2;
3007
+ const value = (_a2 = readString(env, name)) != null ? _a2 : fallback;
3007
3008
  if (allowed.includes(value)) {
3008
3009
  return value;
3009
3010
  }
3010
3011
  throw new Error(`[getRagConfig] ${name} must be one of: ${allowed.join(", ")}`);
3011
3012
  }
3012
3013
  function getEnvConfig(env = process.env, base) {
3013
- 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;
3014
- 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;
3015
- const jwtProjectId = (() => {
3016
- if (!licenseKey) return void 0;
3017
- try {
3018
- const parts = licenseKey.split(".");
3019
- if (parts.length >= 2) {
3020
- const payload = JSON.parse(Buffer.from(parts[1], "base64url").toString("utf8"));
3021
- return payload == null ? void 0 : payload.projectId;
3022
- }
3023
- } catch (e) {
3024
- }
3025
- return void 0;
3026
- })();
3027
- 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";
3028
- 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);
3014
+ 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;
3015
+ const licenseKey = (_c = (_b = (_a2 = readString(env, "RAG_LICENSE_KEY")) != null ? _a2 : readString(env, "RETRIVORA_LICENSE_KEY")) != null ? _b : (
3016
+ // Fix SEC-4: NEXT_PUBLIC_RETRIVORA_LICENSE_KEY is NOT included here.
3017
+ // That variable is inlined into the browser bundle by Next.js at build time
3018
+ // and must never be used as a server-side secret source. Developers who
3019
+ // previously relied on this should migrate to RETRIVORA_LICENSE_KEY.
3020
+ readString(env, "LICENSE_KEY")
3021
+ )) != null ? _c : base == null ? void 0 : base.licenseKey;
3022
+ 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";
3023
+ 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);
3029
3024
  const vectorProvider = readEnum(env, "VECTOR_DB_PROVIDER", "pinecone", VECTOR_DB_PROVIDERS);
3030
3025
  const embeddingDimensions = readNumber(env, "EMBEDDING_DIMENSIONS", 768);
3031
3026
  const vectorDbOptions = {};
3032
3027
  if (vectorProvider === "pinecone") {
3033
- 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 : "";
3034
- 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";
3028
+ 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 : "";
3029
+ 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";
3035
3030
  } else if (vectorProvider === "pgvector" || vectorProvider === "postgresql") {
3036
- 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 : "";
3037
- 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;
3038
- 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;
3031
+ 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 : "";
3032
+ 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;
3033
+ 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;
3039
3034
  vectorDbOptions.dimensions = embeddingDimensions;
3040
3035
  } else if (vectorProvider === "mongodb") {
3041
- 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 : "";
3042
- 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 : "";
3043
- 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 : "";
3044
- 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";
3036
+ 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 : "";
3037
+ 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 : "";
3038
+ 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 : "";
3039
+ 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";
3045
3040
  } else if (vectorProvider === "qdrant") {
3046
- 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";
3047
- 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;
3041
+ 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";
3042
+ 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;
3048
3043
  vectorDbOptions.dimensions = embeddingDimensions;
3049
3044
  } else if (vectorProvider === "milvus") {
3050
- vectorDbOptions.baseUrl = (_ha = readString(env, "MILVUS_URL")) != null ? _ha : "http://localhost:19530";
3045
+ vectorDbOptions.baseUrl = (_da = readString(env, "MILVUS_URL")) != null ? _da : "http://localhost:19530";
3051
3046
  vectorDbOptions.apiKey = readString(env, "MILVUS_API_KEY");
3052
3047
  } else if (vectorProvider === "chromadb") {
3053
- vectorDbOptions.baseUrl = (_ia = readString(env, "CHROMADB_URL")) != null ? _ia : "http://localhost:8000";
3048
+ vectorDbOptions.baseUrl = (_ea = readString(env, "CHROMADB_URL")) != null ? _ea : "http://localhost:8000";
3054
3049
  } else if (vectorProvider === "weaviate") {
3055
- vectorDbOptions.baseUrl = (_ja = readString(env, "WEAVIATE_URL")) != null ? _ja : "http://localhost:8080";
3050
+ vectorDbOptions.baseUrl = (_fa = readString(env, "WEAVIATE_URL")) != null ? _fa : "http://localhost:8080";
3056
3051
  vectorDbOptions.apiKey = readString(env, "WEAVIATE_API_KEY");
3057
3052
  } else if (vectorProvider === "redis") {
3058
- vectorDbOptions.baseUrl = (_ka = readString(env, "REDIS_URL")) != null ? _ka : "";
3053
+ vectorDbOptions.baseUrl = (_ga = readString(env, "REDIS_URL")) != null ? _ga : "";
3059
3054
  vectorDbOptions.apiKey = readString(env, "REDIS_API_KEY");
3060
3055
  } else if (vectorProvider === "rest") {
3061
- vectorDbOptions.baseUrl = (_la = readString(env, "VECTOR_DB_REST_URL")) != null ? _la : "";
3056
+ vectorDbOptions.baseUrl = (_ha = readString(env, "VECTOR_DB_REST_URL")) != null ? _ha : "";
3062
3057
  vectorDbOptions.headers = readString(env, "VECTOR_DB_REST_API_KEY") ? { "api-key": readString(env, "VECTOR_DB_REST_API_KEY") } : {};
3063
3058
  } else if (vectorProvider === "universal_rest") {
3064
- vectorDbOptions.baseUrl = (_na = (_ma = readString(env, "VECTOR_BASE_URL")) != null ? _ma : readString(env, "VECTOR_DB_REST_URL")) != null ? _na : "";
3059
+ vectorDbOptions.baseUrl = (_ja = (_ia = readString(env, "VECTOR_BASE_URL")) != null ? _ia : readString(env, "VECTOR_DB_REST_URL")) != null ? _ja : "";
3065
3060
  vectorDbOptions.profile = readString(env, "VECTOR_UNIVERSAL_PROFILE");
3066
3061
  vectorDbOptions.headers = readString(env, "VECTOR_DB_REST_API_KEY") ? { Authorization: `Bearer ${readString(env, "VECTOR_DB_REST_API_KEY")}` } : {};
3067
3062
  }
@@ -3080,8 +3075,8 @@ function getEnvConfig(env = process.env, base) {
3080
3075
  // Anthropic needs a separate embedding provider; key kept for completeness
3081
3076
  gemini: readString(env, "GEMINI_API_KEY"),
3082
3077
  ollama: void 0,
3083
- universal_rest: (_oa = readString(env, "EMBEDDING_API_KEY")) != null ? _oa : readString(env, "OPENAI_API_KEY"),
3084
- custom: (_pa = readString(env, "EMBEDDING_API_KEY")) != null ? _pa : readString(env, "OPENAI_API_KEY")
3078
+ universal_rest: (_ka = readString(env, "EMBEDDING_API_KEY")) != null ? _ka : readString(env, "OPENAI_API_KEY"),
3079
+ custom: (_la = readString(env, "EMBEDDING_API_KEY")) != null ? _la : readString(env, "OPENAI_API_KEY")
3085
3080
  };
3086
3081
  const DEFAULT_MODEL_BY_PROVIDER = {
3087
3082
  openai: "gpt-4o",
@@ -3103,27 +3098,27 @@ function getEnvConfig(env = process.env, base) {
3103
3098
  })();
3104
3099
  const tierBasedLLMModel = isFreeTier2 ? "llama-3.1-8b-instant" : "llama-3.3-70b-versatile";
3105
3100
  const tierBasedEmbeddingModel = isFreeTier2 ? "text-embedding-004" : "text-embedding-004";
3106
- const controlPlaneUrl = (_qa = readString(env, "RETRIVORA_CONTROL_PLANE_URL")) != null ? _qa : readString(env, "NEXT_PUBLIC_RETRIVORA_CONTROL_PLANE_URL");
3101
+ const controlPlaneUrl = (_ma = readString(env, "RETRIVORA_CONTROL_PLANE_URL")) != null ? _ma : readString(env, "NEXT_PUBLIC_RETRIVORA_CONTROL_PLANE_URL");
3107
3102
  const defaultGatewayUrl = controlPlaneUrl ? `${controlPlaneUrl.replace(/\/$/, "")}/api/v1` : "https://llm.retrivora.com/api/v1";
3108
3103
  const rt = _runtimeConfig;
3109
3104
  const defaultLlmProvider = "universal_rest";
3110
- 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;
3111
- 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;
3112
- 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;
3113
- 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;
3114
- 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";
3105
+ 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;
3106
+ 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;
3107
+ 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;
3108
+ 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;
3109
+ 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";
3115
3110
  const defaultEmbeddingProvider = "universal_rest";
3116
- 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;
3117
- 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;
3118
- 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;
3119
- 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;
3120
- 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";
3111
+ 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;
3112
+ 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;
3113
+ 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;
3114
+ 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;
3115
+ 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";
3121
3116
  return __spreadProps(__spreadValues({
3122
3117
  projectId,
3123
3118
  licenseKey,
3124
3119
  vectorDb: {
3125
3120
  provider: vectorProvider,
3126
- indexName: (_gb = (_fb = readString(env, "VECTOR_DB_INDEX")) != null ? _fb : vectorDbOptions.indexName) != null ? _gb : "rag-index",
3121
+ indexName: (_cb = (_bb = readString(env, "VECTOR_DB_INDEX")) != null ? _bb : vectorDbOptions.indexName) != null ? _cb : "rag-index",
3127
3122
  options: vectorDbOptions
3128
3123
  },
3129
3124
  llm: {
@@ -3131,8 +3126,8 @@ function getEnvConfig(env = process.env, base) {
3131
3126
  model: llmModel,
3132
3127
  apiKey: llmApiKey,
3133
3128
  baseUrl: llmBaseUrl,
3134
- systemPrompt: (_hb = readString(env, "LLM_SYSTEM_PROMPT")) != null ? _hb : rt == null ? void 0 : rt.systemPrompt,
3135
- maxTokens: readNumber(env, "LLM_MAX_TOKENS", (_ib = rt == null ? void 0 : rt.maxTokens) != null ? _ib : 4096),
3129
+ systemPrompt: (_db = readString(env, "LLM_SYSTEM_PROMPT")) != null ? _db : rt == null ? void 0 : rt.systemPrompt,
3130
+ maxTokens: readNumber(env, "LLM_MAX_TOKENS", (_eb = rt == null ? void 0 : rt.maxTokens) != null ? _eb : 4096),
3136
3131
  temperature: readNumber(env, "LLM_TEMPERATURE", 0.7),
3137
3132
  options: {
3138
3133
  profile: llmProfile,
@@ -3153,30 +3148,30 @@ function getEnvConfig(env = process.env, base) {
3153
3148
  }
3154
3149
  },
3155
3150
  ui: {
3156
- title: (_kb = (_jb = readString(env, "NEXT_PUBLIC_UI_TITLE")) != null ? _jb : readString(env, "UI_TITLE")) != null ? _kb : "AI Assistant",
3157
- subtitle: (_mb = (_lb = readString(env, "NEXT_PUBLIC_UI_SUBTITLE")) != null ? _lb : readString(env, "UI_SUBTITLE")) != null ? _mb : "Powered by RAG",
3158
- primaryColor: (_ob = (_nb = readString(env, "NEXT_PUBLIC_PRIMARY_COLOR")) != null ? _nb : readString(env, "UI_PRIMARY_COLOR")) != null ? _ob : "#10b981",
3159
- accentColor: (_qb = (_pb = readString(env, "NEXT_PUBLIC_ACCENT_COLOR")) != null ? _pb : readString(env, "UI_ACCENT_COLOR")) != null ? _qb : "#3b82f6",
3160
- logoUrl: (_rb = readString(env, "NEXT_PUBLIC_LOGO_URL")) != null ? _rb : readString(env, "UI_LOGO_URL"),
3161
- placeholder: (_tb = (_sb = readString(env, "NEXT_PUBLIC_PLACEHOLDER")) != null ? _sb : readString(env, "UI_PLACEHOLDER")) != null ? _tb : "Ask me anything\u2026",
3162
- showSources: ((_vb = (_ub = readString(env, "NEXT_PUBLIC_SHOW_SOURCES")) != null ? _ub : readString(env, "UI_SHOW_SOURCES")) != null ? _vb : "true") !== "false",
3163
- 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.",
3164
- visualStyle: (_zb = (_yb = readString(env, "NEXT_PUBLIC_UI_VISUAL_STYLE")) != null ? _yb : readString(env, "UI_VISUAL_STYLE")) != null ? _zb : "glass",
3165
- borderRadius: (_Bb = (_Ab = readString(env, "NEXT_PUBLIC_UI_BORDER_RADIUS")) != null ? _Ab : readString(env, "UI_BORDER_RADIUS")) != null ? _Bb : "xl",
3166
- allowUpload: ((_Db = (_Cb = readString(env, "NEXT_PUBLIC_ALLOW_UPLOAD")) != null ? _Cb : readString(env, "UI_ALLOW_UPLOAD")) != null ? _Db : "false") === "true"
3151
+ title: (_gb = (_fb = readString(env, "NEXT_PUBLIC_UI_TITLE")) != null ? _fb : readString(env, "UI_TITLE")) != null ? _gb : "AI Assistant",
3152
+ subtitle: (_ib = (_hb = readString(env, "NEXT_PUBLIC_UI_SUBTITLE")) != null ? _hb : readString(env, "UI_SUBTITLE")) != null ? _ib : "Powered by RAG",
3153
+ primaryColor: (_kb = (_jb = readString(env, "NEXT_PUBLIC_PRIMARY_COLOR")) != null ? _jb : readString(env, "UI_PRIMARY_COLOR")) != null ? _kb : "#10b981",
3154
+ accentColor: (_mb = (_lb = readString(env, "NEXT_PUBLIC_ACCENT_COLOR")) != null ? _lb : readString(env, "UI_ACCENT_COLOR")) != null ? _mb : "#3b82f6",
3155
+ logoUrl: (_nb = readString(env, "NEXT_PUBLIC_LOGO_URL")) != null ? _nb : readString(env, "UI_LOGO_URL"),
3156
+ placeholder: (_pb = (_ob = readString(env, "NEXT_PUBLIC_PLACEHOLDER")) != null ? _ob : readString(env, "UI_PLACEHOLDER")) != null ? _pb : "Ask me anything\u2026",
3157
+ showSources: ((_rb = (_qb = readString(env, "NEXT_PUBLIC_SHOW_SOURCES")) != null ? _qb : readString(env, "UI_SHOW_SOURCES")) != null ? _rb : "true") !== "false",
3158
+ 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.",
3159
+ visualStyle: (_vb = (_ub = readString(env, "NEXT_PUBLIC_UI_VISUAL_STYLE")) != null ? _ub : readString(env, "UI_VISUAL_STYLE")) != null ? _vb : "glass",
3160
+ borderRadius: (_xb = (_wb = readString(env, "NEXT_PUBLIC_UI_BORDER_RADIUS")) != null ? _wb : readString(env, "UI_BORDER_RADIUS")) != null ? _xb : "xl",
3161
+ allowUpload: ((_zb = (_yb = readString(env, "NEXT_PUBLIC_ALLOW_UPLOAD")) != null ? _yb : readString(env, "UI_ALLOW_UPLOAD")) != null ? _zb : "false") === "true"
3167
3162
  },
3168
3163
  rag: {
3169
- topK: readNumber(env, "RAG_TOP_K", (_Eb = rt == null ? void 0 : rt.topK) != null ? _Eb : 5),
3164
+ topK: readNumber(env, "RAG_TOP_K", (_Ab = rt == null ? void 0 : rt.topK) != null ? _Ab : 5),
3170
3165
  scoreThreshold: readNumber(env, "RAG_SCORE_THRESHOLD", 0),
3171
- chunkSize: readNumber(env, "RAG_CHUNK_SIZE", (_Fb = rt == null ? void 0 : rt.chunkSize) != null ? _Fb : 1e3),
3172
- chunkOverlap: readNumber(env, "RAG_CHUNK_OVERLAP", (_Gb = rt == null ? void 0 : rt.chunkOverlap) != null ? _Gb : 200),
3173
- filterableFields: (_Hb = readString(env, "RAG_FILTERABLE_FIELDS")) == null ? void 0 : _Hb.split(",").map((f) => f.trim()),
3166
+ chunkSize: readNumber(env, "RAG_CHUNK_SIZE", (_Bb = rt == null ? void 0 : rt.chunkSize) != null ? _Bb : 1e3),
3167
+ chunkOverlap: readNumber(env, "RAG_CHUNK_OVERLAP", (_Cb = rt == null ? void 0 : rt.chunkOverlap) != null ? _Cb : 200),
3168
+ filterableFields: (_Db = readString(env, "RAG_FILTERABLE_FIELDS")) == null ? void 0 : _Db.split(",").map((f) => f.trim()),
3174
3169
  // Query pipeline toggles — read from .env.local
3175
3170
  useQueryTransformation: readString(env, "RAG_USE_QUERY_TRANSFORMATION") === "true",
3176
3171
  useReranking: readString(env, "RAG_USE_RERANKING") === "true",
3177
3172
  useGraphRetrieval: readString(env, "RAG_USE_GRAPH_RETRIEVAL") === "true",
3178
- architecture: (_Ib = readString(env, "RAG_ARCHITECTURE")) != null ? _Ib : "simple",
3179
- chunkingStrategy: (_Jb = readString(env, "RAG_CHUNKING_STRATEGY")) != null ? _Jb : "recursive",
3173
+ architecture: (_Eb = readString(env, "RAG_ARCHITECTURE")) != null ? _Eb : "simple",
3174
+ chunkingStrategy: (_Fb = readString(env, "RAG_CHUNKING_STRATEGY")) != null ? _Fb : "recursive",
3180
3175
  uiMapping: (() => {
3181
3176
  const raw = readString(env, "RAG_UI_MAPPING");
3182
3177
  if (!raw) return void 0;
@@ -3189,7 +3184,7 @@ function getEnvConfig(env = process.env, base) {
3189
3184
  },
3190
3185
  telemetry: {
3191
3186
  enabled: telemetryEnabled,
3192
- 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"
3187
+ 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"
3193
3188
  }
3194
3189
  }, readString(env, "GRAPH_DB_PROVIDER") ? {
3195
3190
  graphDb: {
@@ -3202,8 +3197,8 @@ function getEnvConfig(env = process.env, base) {
3202
3197
  }
3203
3198
  } : {}), {
3204
3199
  mcpServers: (() => {
3205
- var _a4;
3206
- const raw = (_a4 = readString(env, "MCP_SERVERS")) != null ? _a4 : readString(env, "NEXT_PUBLIC_MCP_SERVERS");
3200
+ var _a3;
3201
+ const raw = (_a3 = readString(env, "MCP_SERVERS")) != null ? _a3 : readString(env, "NEXT_PUBLIC_MCP_SERVERS");
3207
3202
  if (!raw) return void 0;
3208
3203
  try {
3209
3204
  return JSON.parse(raw);
@@ -3249,10 +3244,10 @@ var ConfigResolver = class {
3249
3244
  * fallback behavior.
3250
3245
  */
3251
3246
  static resolveUniversal(hostConfig, env = process.env) {
3252
- var _a3;
3247
+ var _a2;
3253
3248
  if (!hostConfig) return this.resolve(void 0, env);
3254
3249
  const normalized = __spreadProps(__spreadValues({}, hostConfig), {
3255
- vectorDb: (_a3 = hostConfig.vectorDb) != null ? _a3 : hostConfig.vectorDatabase,
3250
+ vectorDb: (_a2 = hostConfig.vectorDb) != null ? _a2 : hostConfig.vectorDatabase,
3256
3251
  rag: this.mergeRetrievalWorkflow(hostConfig.rag, hostConfig.retrieval, hostConfig.workflow)
3257
3252
  });
3258
3253
  return this.resolve(normalized, env);
@@ -3272,11 +3267,11 @@ var ConfigResolver = class {
3272
3267
  }
3273
3268
  }
3274
3269
  static mergeRetrievalWorkflow(rag, retrieval, workflow) {
3275
- var _a3, _b, _c, _d, _e, _f;
3270
+ var _a2, _b, _c, _d, _e, _f;
3276
3271
  if (!rag && !retrieval && !workflow) return void 0;
3277
3272
  const normalized = __spreadValues({}, rag != null ? rag : {});
3278
3273
  if (retrieval) {
3279
- normalized.topK = (_a3 = retrieval.topK) != null ? _a3 : normalized.topK;
3274
+ normalized.topK = (_a2 = retrieval.topK) != null ? _a2 : normalized.topK;
3280
3275
  normalized.scoreThreshold = (_b = retrieval.scoreThreshold) != null ? _b : normalized.scoreThreshold;
3281
3276
  normalized.useReranking = (_c = retrieval.useReranking) != null ? _c : normalized.useReranking;
3282
3277
  if (retrieval.strategy === "hybrid") normalized.architecture = (_d = normalized.architecture) != null ? _d : "hybrid";
@@ -3404,8 +3399,8 @@ var OpenAIProvider = class {
3404
3399
  };
3405
3400
  }
3406
3401
  async chat(messages, context, options) {
3407
- var _a3, _b, _c, _d, _e, _f, _g2, _h, _i;
3408
- 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.";
3402
+ var _a2, _b, _c, _d, _e, _f, _g2, _h, _i;
3403
+ 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.";
3409
3404
  const systemMessage = {
3410
3405
  role: "system",
3411
3406
  content: buildSystemContent(basePrompt, context)
@@ -3429,8 +3424,8 @@ var OpenAIProvider = class {
3429
3424
  }
3430
3425
  chatStream(messages, context, options) {
3431
3426
  return __asyncGenerator(this, null, function* () {
3432
- var _a3, _b, _c, _d, _e, _f, _g2, _h;
3433
- 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.";
3427
+ var _a2, _b, _c, _d, _e, _f, _g2, _h;
3428
+ 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.";
3434
3429
  const systemMessage = {
3435
3430
  role: "system",
3436
3431
  content: buildSystemContent(basePrompt, context)
@@ -3477,8 +3472,8 @@ var OpenAIProvider = class {
3477
3472
  return results[0];
3478
3473
  }
3479
3474
  async batchEmbed(texts, options) {
3480
- var _a3, _b, _c, _d, _e;
3481
- 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";
3475
+ var _a2, _b, _c, _d, _e;
3476
+ 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";
3482
3477
  const apiKey = (_e = (_d = this.embeddingConfig) == null ? void 0 : _d.apiKey) != null ? _e : this.llmConfig.apiKey;
3483
3478
  const client = apiKey !== this.llmConfig.apiKey ? new import_openai.default({ apiKey }) : this.client;
3484
3479
  const response = await client.embeddings.create({ model, input: texts });
@@ -3537,11 +3532,7 @@ var AnthropicProvider = class {
3537
3532
  try {
3538
3533
  const { default: Anthropic2 } = await import("@anthropic-ai/sdk");
3539
3534
  const client = new Anthropic2({ apiKey });
3540
- await client.messages.create({
3541
- model: modelName,
3542
- max_tokens: 10,
3543
- messages: [{ role: "user", content: "ping" }]
3544
- });
3535
+ await client.models.list();
3545
3536
  return { healthy: true, provider: "anthropic", capabilities: { model: modelName }, timestamp };
3546
3537
  } catch (error) {
3547
3538
  return {
@@ -3555,8 +3546,8 @@ var AnthropicProvider = class {
3555
3546
  };
3556
3547
  }
3557
3548
  async chat(messages, context, options) {
3558
- var _a3, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k;
3559
- 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.";
3549
+ var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k;
3550
+ 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.";
3560
3551
  const system = buildSystemContent(basePrompt, context);
3561
3552
  const anthropicMessages = messages.map((m) => ({
3562
3553
  role: m.role === "assistant" ? "assistant" : "user",
@@ -3594,8 +3585,8 @@ var AnthropicProvider = class {
3594
3585
  }
3595
3586
  chatStream(messages, context, options) {
3596
3587
  return __asyncGenerator(this, null, function* () {
3597
- var _a3, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k;
3598
- 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.";
3588
+ var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k;
3589
+ 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.";
3599
3590
  const system = buildSystemContent(basePrompt, context);
3600
3591
  const anthropicMessages = messages.map((m) => ({
3601
3592
  role: m.role === "assistant" ? "assistant" : "user",
@@ -3679,8 +3670,8 @@ var AnthropicProvider = class {
3679
3670
  var import_axios = __toESM(require("axios"));
3680
3671
  var OllamaProvider = class {
3681
3672
  constructor(llmConfig, embeddingConfig) {
3682
- var _a3, _b, _c;
3683
- const rawBaseURL = ((_a3 = llmConfig.baseUrl) != null ? _a3 : "http://localhost:11434").replace(/\/+$/, "");
3673
+ var _a2, _b, _c;
3674
+ const rawBaseURL = ((_a2 = llmConfig.baseUrl) != null ? _a2 : "http://localhost:11434").replace(/\/+$/, "");
3684
3675
  const baseURL = rawBaseURL.replace(/\/(?:api\/v1|v1|api)$/i, "");
3685
3676
  const timeout = Number((_b = llmConfig.options) == null ? void 0 : _b.timeout) || 3e5;
3686
3677
  const headers = ((_c = llmConfig.options) == null ? void 0 : _c.headers) || {};
@@ -3740,8 +3731,8 @@ var OllamaProvider = class {
3740
3731
  };
3741
3732
  }
3742
3733
  async chat(messages, context, options) {
3743
- var _a3, _b, _c, _d, _e, _f;
3744
- 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.";
3734
+ var _a2, _b, _c, _d, _e, _f;
3735
+ 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.";
3745
3736
  const system = buildSystemContent(basePrompt, context);
3746
3737
  const { data } = await this.http.post("/api/chat", {
3747
3738
  model: this.llmConfig.model,
@@ -3759,8 +3750,8 @@ var OllamaProvider = class {
3759
3750
  }
3760
3751
  chatStream(messages, context, options) {
3761
3752
  return __asyncGenerator(this, null, function* () {
3762
- var _a3, _b, _c, _d, _e, _f, _g2, _h;
3763
- 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.";
3753
+ var _a2, _b, _c, _d, _e, _f, _g2, _h;
3754
+ 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.";
3764
3755
  const system = buildSystemContent(basePrompt, context);
3765
3756
  const response = yield new __await(this.http.post("/api/chat", {
3766
3757
  model: this.llmConfig.model,
@@ -3818,8 +3809,8 @@ var OllamaProvider = class {
3818
3809
  });
3819
3810
  }
3820
3811
  async embed(text, options) {
3821
- var _a3, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k;
3822
- 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";
3812
+ var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k;
3813
+ 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";
3823
3814
  const baseURL = (_f = (_e = (_d = this.embeddingConfig) == null ? void 0 : _d.baseUrl) != null ? _e : this.llmConfig.baseUrl) != null ? _f : "http://localhost:11434";
3824
3815
  const client = baseURL !== ((_g2 = this.llmConfig.baseUrl) != null ? _g2 : "http://localhost:11434") ? import_axios.default.create({ baseURL, timeout: 6e4 }) : this.http;
3825
3816
  let prompt = text;
@@ -3920,9 +3911,9 @@ var GeminiProvider = class {
3920
3911
  static getHealthChecker() {
3921
3912
  return {
3922
3913
  async check(config) {
3923
- var _a3, _b;
3914
+ var _a2, _b;
3924
3915
  const timestamp = Date.now();
3925
- const apiKey = (_b = (_a3 = config.apiKey) != null ? _a3 : process.env.GOOGLE_GENAI_API_KEY) != null ? _b : "";
3916
+ const apiKey = (_b = (_a2 = config.apiKey) != null ? _a2 : process.env.GOOGLE_GENAI_API_KEY) != null ? _b : "";
3926
3917
  const modelName = sanitizeModel(config.model);
3927
3918
  try {
3928
3919
  const { GoogleGenerativeAI: GoogleGenerativeAI2 } = await import("@google/generative-ai");
@@ -3952,16 +3943,16 @@ var GeminiProvider = class {
3952
3943
  /** Resolve the embedding client — uses a separate client when the embedding
3953
3944
  * API key differs from the LLM API key. */
3954
3945
  get embeddingClient() {
3955
- var _a3;
3956
- if (((_a3 = this.embeddingConfig) == null ? void 0 : _a3.apiKey) && this.embeddingConfig.apiKey !== this.llmConfig.apiKey) {
3946
+ var _a2;
3947
+ if (((_a2 = this.embeddingConfig) == null ? void 0 : _a2.apiKey) && this.embeddingConfig.apiKey !== this.llmConfig.apiKey) {
3957
3948
  return buildClient(this.embeddingConfig.apiKey);
3958
3949
  }
3959
3950
  return this.client;
3960
3951
  }
3961
3952
  /** Resolve the embedding model to use, in order of specificity. */
3962
3953
  resolveEmbeddingModel(optionsModel) {
3963
- var _a3, _b;
3964
- return sanitizeModel((_b = optionsModel != null ? optionsModel : (_a3 = this.embeddingConfig) == null ? void 0 : _a3.model) != null ? _b : DEFAULT_EMBEDDING_MODEL);
3954
+ var _a2, _b;
3955
+ return sanitizeModel((_b = optionsModel != null ? optionsModel : (_a2 = this.embeddingConfig) == null ? void 0 : _a2.model) != null ? _b : DEFAULT_EMBEDDING_MODEL);
3965
3956
  }
3966
3957
  /**
3967
3958
  * Convert ChatMessage[] to the Gemini contents format.
@@ -3981,8 +3972,8 @@ var GeminiProvider = class {
3981
3972
  // ILLMProvider — chat
3982
3973
  // -------------------------------------------------------------------------
3983
3974
  async chat(messages, context, options) {
3984
- var _a3, _b, _c, _d, _e, _f;
3985
- 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.";
3975
+ var _a2, _b, _c, _d, _e, _f;
3976
+ 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.";
3986
3977
  const model = this.client.getGenerativeModel({
3987
3978
  model: this.llmConfig.model,
3988
3979
  systemInstruction: buildSystemContent(basePrompt, context)
@@ -3999,8 +3990,8 @@ var GeminiProvider = class {
3999
3990
  }
4000
3991
  chatStream(messages, context, options) {
4001
3992
  return __asyncGenerator(this, null, function* () {
4002
- var _a3, _b, _c, _d, _e, _f;
4003
- 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.";
3993
+ var _a2, _b, _c, _d, _e, _f;
3994
+ 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.";
4004
3995
  const model = this.client.getGenerativeModel({
4005
3996
  model: this.llmConfig.model,
4006
3997
  systemInstruction: buildSystemContent(basePrompt, context)
@@ -4035,11 +4026,11 @@ var GeminiProvider = class {
4035
4026
  // ILLMProvider — embeddings
4036
4027
  // -------------------------------------------------------------------------
4037
4028
  async embed(text, options) {
4038
- var _a3, _b;
4029
+ var _a2, _b;
4039
4030
  const content = applyPrefix(
4040
4031
  text,
4041
4032
  options == null ? void 0 : options.taskType,
4042
- (_a3 = this.embeddingConfig) == null ? void 0 : _a3.queryPrefix,
4033
+ (_a2 = this.embeddingConfig) == null ? void 0 : _a2.queryPrefix,
4043
4034
  (_b = this.embeddingConfig) == null ? void 0 : _b.documentPrefix
4044
4035
  );
4045
4036
  const modelName = this.resolveEmbeddingModel(options == null ? void 0 : options.model);
@@ -4056,7 +4047,7 @@ var GeminiProvider = class {
4056
4047
  const modelName = this.resolveEmbeddingModel(options == null ? void 0 : options.model);
4057
4048
  const model = this.embeddingClient.getGenerativeModel({ model: modelName });
4058
4049
  const requests = texts.map((text) => {
4059
- var _a3, _b;
4050
+ var _a2, _b;
4060
4051
  return {
4061
4052
  content: {
4062
4053
  role: "user",
@@ -4064,7 +4055,7 @@ var GeminiProvider = class {
4064
4055
  text: applyPrefix(
4065
4056
  text,
4066
4057
  options == null ? void 0 : options.taskType,
4067
- (_a3 = this.embeddingConfig) == null ? void 0 : _a3.queryPrefix,
4058
+ (_a2 = this.embeddingConfig) == null ? void 0 : _a2.queryPrefix,
4068
4059
  (_b = this.embeddingConfig) == null ? void 0 : _b.documentPrefix
4069
4060
  )
4070
4061
  }]
@@ -4081,8 +4072,8 @@ var GeminiProvider = class {
4081
4072
  throw err;
4082
4073
  });
4083
4074
  return response.embeddings.map((e) => {
4084
- var _a3;
4085
- return (_a3 = e.values) != null ? _a3 : [];
4075
+ var _a2;
4076
+ return (_a2 = e.values) != null ? _a2 : [];
4086
4077
  });
4087
4078
  }
4088
4079
  // -------------------------------------------------------------------------
@@ -4171,8 +4162,8 @@ var GroqProvider = class {
4171
4162
  };
4172
4163
  }
4173
4164
  async chat(messages, context, options) {
4174
- var _a3, _b, _c, _d, _e, _f, _g2, _h, _i;
4175
- 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.";
4165
+ var _a2, _b, _c, _d, _e, _f, _g2, _h, _i;
4166
+ 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.";
4176
4167
  const systemMessage = {
4177
4168
  role: "system",
4178
4169
  content: buildSystemContent(basePrompt, context)
@@ -4196,8 +4187,8 @@ var GroqProvider = class {
4196
4187
  }
4197
4188
  chatStream(messages, context, options) {
4198
4189
  return __asyncGenerator(this, null, function* () {
4199
- var _a3, _b, _c, _d, _e, _f, _g2, _h;
4200
- 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.";
4190
+ var _a2, _b, _c, _d, _e, _f, _g2, _h;
4191
+ 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.";
4201
4192
  const systemMessage = {
4202
4193
  role: "system",
4203
4194
  content: buildSystemContent(basePrompt, context)
@@ -4331,8 +4322,8 @@ var QwenProvider = class {
4331
4322
  };
4332
4323
  }
4333
4324
  async chat(messages, context, options) {
4334
- var _a3, _b, _c, _d, _e, _f, _g2, _h, _i;
4335
- 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.";
4325
+ var _a2, _b, _c, _d, _e, _f, _g2, _h, _i;
4326
+ 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.";
4336
4327
  const systemMessage = {
4337
4328
  role: "system",
4338
4329
  content: buildSystemContent(basePrompt, context)
@@ -4356,8 +4347,8 @@ var QwenProvider = class {
4356
4347
  }
4357
4348
  chatStream(messages, context, options) {
4358
4349
  return __asyncGenerator(this, null, function* () {
4359
- var _a3, _b, _c, _d, _e, _f, _g2, _h;
4360
- 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.";
4350
+ var _a2, _b, _c, _d, _e, _f, _g2, _h;
4351
+ 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.";
4361
4352
  const systemMessage = {
4362
4353
  role: "system",
4363
4354
  content: buildSystemContent(basePrompt, context)
@@ -4404,8 +4395,8 @@ var QwenProvider = class {
4404
4395
  return results[0];
4405
4396
  }
4406
4397
  async batchEmbed(texts, options) {
4407
- var _a3, _b, _c, _d, _e, _f;
4408
- 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";
4398
+ var _a2, _b, _c, _d, _e, _f;
4399
+ 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";
4409
4400
  const apiKey = (_e = (_d = this.embeddingConfig) == null ? void 0 : _d.apiKey) != null ? _e : this.llmConfig.apiKey;
4410
4401
  const client = apiKey !== this.llmConfig.apiKey ? new import_openai3.default({
4411
4402
  apiKey,
@@ -4466,9 +4457,9 @@ var LLM_PROFILES = {
4466
4457
 
4467
4458
  // src/llm/providers/UniversalLLMAdapter.ts
4468
4459
  function extractContent(obj) {
4469
- var _a3, _b, _c;
4460
+ var _a2, _b, _c;
4470
4461
  if (!obj || typeof obj !== "object") return void 0;
4471
- const choice = (_a3 = obj.choices) == null ? void 0 : _a3[0];
4462
+ const choice = (_a2 = obj.choices) == null ? void 0 : _a2[0];
4472
4463
  if (!choice) return void 0;
4473
4464
  if (typeof ((_b = choice.message) == null ? void 0 : _b.content) === "string") return choice.message.content;
4474
4465
  if (typeof ((_c = choice.delta) == null ? void 0 : _c.content) === "string") return choice.delta.content;
@@ -4478,10 +4469,10 @@ function extractContent(obj) {
4478
4469
  }
4479
4470
  var UniversalLLMAdapter = class {
4480
4471
  constructor(config) {
4481
- var _a3, _b, _c, _d, _e, _f, _g2, _h;
4472
+ var _a2, _b, _c, _d, _e, _f, _g2, _h;
4482
4473
  this.model = config.model;
4483
4474
  const llmConfig = config;
4484
- const options = (_a3 = llmConfig.options) != null ? _a3 : {};
4475
+ const options = (_a2 = llmConfig.options) != null ? _a2 : {};
4485
4476
  let profile = {};
4486
4477
  if (typeof options.profile === "string") {
4487
4478
  profile = LLM_PROFILES[options.profile] || {};
@@ -4507,8 +4498,8 @@ var UniversalLLMAdapter = class {
4507
4498
  });
4508
4499
  }
4509
4500
  async chat(messages, context) {
4510
- var _a3, _b, _c, _d, _e, _f, _g2, _h, _i, _j;
4511
- const path2 = (_a3 = this.opts.chatPath) != null ? _a3 : "/chat/completions";
4501
+ var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j;
4502
+ const path2 = (_a2 = this.opts.chatPath) != null ? _a2 : "/chat/completions";
4512
4503
  const safeMessages = (Array.isArray(messages) ? messages : []).filter((m) => Boolean(m && typeof m === "object")).map((m) => ({
4513
4504
  role: m.role || "user",
4514
4505
  content: typeof m.content === "string" ? m.content : m.content != null ? String(m.content) : ""
@@ -4585,8 +4576,8 @@ ${context != null ? context : "None"}` },
4585
4576
  */
4586
4577
  chatStream(messages, context) {
4587
4578
  return __asyncGenerator(this, null, function* () {
4588
- var _a3, _b, _c, _d, _e, _f;
4589
- const path2 = (_a3 = this.opts.chatPath) != null ? _a3 : "/chat/completions";
4579
+ var _a2, _b, _c, _d, _e, _f;
4580
+ const path2 = (_a2 = this.opts.chatPath) != null ? _a2 : "/chat/completions";
4590
4581
  const url = `${this.baseUrl.replace(/\/$/, "")}${path2}`;
4591
4582
  const extractPath = ((_b = this.opts.responseExtractPath) != null ? _b : "choices[0].message.content").replace("message.content", "delta.content");
4592
4583
  const safeMessages = (Array.isArray(messages) ? messages : []).filter((m) => Boolean(m && typeof m === "object")).map((m) => ({
@@ -4712,8 +4703,8 @@ ${context != null ? context : "None"}` },
4712
4703
  });
4713
4704
  }
4714
4705
  async embed(text) {
4715
- var _a3, _b, _c, _d, _e, _f, _g2, _h;
4716
- const path2 = (_a3 = this.opts.embedPath) != null ? _a3 : "/embeddings";
4706
+ var _a2, _b, _c, _d, _e, _f, _g2, _h;
4707
+ const path2 = (_a2 = this.opts.embedPath) != null ? _a2 : "/embeddings";
4717
4708
  const payload = this.opts.embedPayloadTemplate ? buildPayload(this.opts.embedPayloadTemplate, { input: text, model: this.model }) : { input: text, model: this.model };
4718
4709
  try {
4719
4710
  const { data } = await this.http.post(path2, payload);
@@ -4837,7 +4828,7 @@ var LLMFactory = class _LLMFactory {
4837
4828
  ];
4838
4829
  }
4839
4830
  static create(llmConfig, embeddingConfig) {
4840
- var _a3, _b, _c;
4831
+ var _a2, _b, _c;
4841
4832
  switch (llmConfig.provider) {
4842
4833
  case "openai":
4843
4834
  return new OpenAIProvider(llmConfig, embeddingConfig);
@@ -4856,7 +4847,7 @@ var LLMFactory = class _LLMFactory {
4856
4847
  case "custom":
4857
4848
  return new UniversalLLMAdapter(llmConfig);
4858
4849
  default: {
4859
- const providerName = String((_a3 = llmConfig.provider) != null ? _a3 : "").toLowerCase();
4850
+ const providerName = String((_a2 = llmConfig.provider) != null ? _a2 : "").toLowerCase();
4860
4851
  const customFactory = customProviders.get(providerName);
4861
4852
  if (customFactory) {
4862
4853
  return customFactory(llmConfig);
@@ -4958,7 +4949,7 @@ var ProviderRegistry = class {
4958
4949
  return null;
4959
4950
  }
4960
4951
  static async loadVectorProviderClass(provider) {
4961
- var _a3;
4952
+ var _a2;
4962
4953
  if (this.vectorProviders[provider]) return this.vectorProviders[provider];
4963
4954
  switch (provider) {
4964
4955
  case "pinecone": {
@@ -4967,7 +4958,7 @@ var ProviderRegistry = class {
4967
4958
  }
4968
4959
  case "pgvector":
4969
4960
  case "postgresql": {
4970
- const postgresMode = ((_a3 = process.env.POSTGRES_MODE) != null ? _a3 : "multi").toLowerCase();
4961
+ const postgresMode = ((_a2 = process.env.POSTGRES_MODE) != null ? _a2 : "multi").toLowerCase();
4971
4962
  if (postgresMode === "single") {
4972
4963
  const { PostgreSQLProvider: PostgreSQLProvider2 } = await Promise.resolve().then(() => (init_PostgreSQLProvider(), PostgreSQLProvider_exports));
4973
4964
  return PostgreSQLProvider2;
@@ -5259,13 +5250,15 @@ var DocumentChunker = class {
5259
5250
  }
5260
5251
  }
5261
5252
  const parts = text.split(separator);
5262
- for (const part of parts) {
5263
- if (part.length <= chunkSize) {
5264
- finalSplits.push(part + (part === parts[parts.length - 1] ? "" : separator));
5253
+ for (let i = 0; i < parts.length; i++) {
5254
+ const part = parts[i];
5255
+ const splitVal = (i === 0 ? "" : separator) + part;
5256
+ if (splitVal.length <= chunkSize) {
5257
+ finalSplits.push(splitVal);
5265
5258
  } else if (nextSeparators.length > 0) {
5266
- finalSplits.push(...this.recursiveSplit(part, nextSeparators, chunkSize));
5259
+ finalSplits.push(...this.recursiveSplit(splitVal, nextSeparators, chunkSize));
5267
5260
  } else {
5268
- finalSplits.push(part);
5261
+ finalSplits.push(splitVal);
5269
5262
  }
5270
5263
  }
5271
5264
  return finalSplits;
@@ -5344,74 +5337,108 @@ Text to extract from:
5344
5337
  };
5345
5338
 
5346
5339
  // src/rag/Reranker.ts
5340
+ var MAX_RERANK_CONTENT_CHARS = 64e3;
5347
5341
  var Reranker = class {
5348
5342
  /**
5349
5343
  * Re-ranks matches based on a secondary relevance score using an LLM.
5344
+ *
5345
+ * Fix RAG-2: keyword re-ranking no longer mutates the original `score`
5346
+ * field (which consumers interpret as the raw vector similarity). A new
5347
+ * `rerankScore` field is added so both the original similarity and the
5348
+ * boosted rank signal are available to callers.
5350
5349
  */
5351
5350
  async rerank(matches, query, limit = 5, llm) {
5352
5351
  if (!llm || matches.length <= 1) {
5353
- const keywords = query.toLowerCase().replace(/[^\w\s]/g, "").split(/\s+/).filter((w) => w.length > 2);
5354
- if (keywords.length === 0) {
5355
- return matches.sort((a, b) => b.score - a.score).slice(0, limit);
5356
- }
5357
- const scoredMatches = matches.map((match) => {
5358
- var _a3;
5359
- const contentLower = ((_a3 = match == null ? void 0 : match.content) != null ? _a3 : "").toLowerCase();
5360
- let keywordScore = 0;
5361
- keywords.forEach((keyword) => {
5362
- if (contentLower.includes(keyword)) {
5363
- keywordScore += 1;
5364
- }
5365
- });
5366
- const normalizedKeywordScore = keywordScore / keywords.length;
5367
- const combinedScore = match.score + normalizedKeywordScore * 0.5;
5368
- return __spreadProps(__spreadValues({}, match), {
5369
- score: combinedScore
5370
- });
5371
- });
5372
- return scoredMatches.sort((a, b) => b.score - a.score).slice(0, limit);
5352
+ return this.keywordRerank(matches, query, limit);
5373
5353
  }
5374
5354
  try {
5375
- const topN = matches.slice(0, 10);
5376
- const response = await llm.chat(
5377
- [{ role: "user", content: `Query: "${query}"
5355
+ return await this.llmRerank(matches, query, limit, llm);
5356
+ } catch (error) {
5357
+ console.warn("[Reranker] LLM re-ranking failed, falling back to keyword sorting:", error);
5358
+ return this.keywordRerank(matches, query, limit);
5359
+ }
5360
+ }
5361
+ /**
5362
+ * Keyword-based re-ranking fallback.
5363
+ *
5364
+ * Fix RAG-2: `rerankScore` is a separate field; `score` (the original
5365
+ * vector similarity) is never mutated. Callers that display `score` to
5366
+ * end-users receive the canonical cosine similarity value.
5367
+ */
5368
+ keywordRerank(matches, query, limit) {
5369
+ const keywords = query.toLowerCase().replace(/[^\w\s]/g, "").split(/\s+/).filter((w) => w.length > 2);
5370
+ if (keywords.length === 0) {
5371
+ return matches.sort((a, b) => b.score - a.score).slice(0, limit);
5372
+ }
5373
+ const scoredMatches = matches.map((match) => {
5374
+ var _a2;
5375
+ const contentLower = ((_a2 = match == null ? void 0 : match.content) != null ? _a2 : "").toLowerCase();
5376
+ let keywordHits = 0;
5377
+ keywords.forEach((keyword) => {
5378
+ if (contentLower.includes(keyword)) {
5379
+ keywordHits++;
5380
+ }
5381
+ });
5382
+ const normalizedKeywordScore = keywordHits / keywords.length;
5383
+ return __spreadProps(__spreadValues({}, match), {
5384
+ rerankScore: match.score + normalizedKeywordScore * 0.5
5385
+ });
5386
+ });
5387
+ return scoredMatches.sort((a, b) => {
5388
+ var _a2, _b;
5389
+ return ((_a2 = b.rerankScore) != null ? _a2 : b.score) - ((_b = a.rerankScore) != null ? _b : a.score);
5390
+ }).slice(0, limit);
5391
+ }
5392
+ /**
5393
+ * LLM-based re-ranking.
5394
+ *
5395
+ * Fix RAG-1: total document content is now budgeted against
5396
+ * MAX_RERANK_CONTENT_CHARS to prevent silent truncation or failures
5397
+ * on models with small context windows.
5398
+ */
5399
+ async llmRerank(matches, query, limit, llm) {
5400
+ const candidates = matches.slice(0, 10);
5401
+ let charsUsed = 0;
5402
+ const budgetedCandidates = candidates.filter((m) => {
5403
+ const len = (m.content || "").length;
5404
+ if (charsUsed + len > MAX_RERANK_CONTENT_CHARS) return false;
5405
+ charsUsed += len;
5406
+ return true;
5407
+ });
5408
+ const docList = budgetedCandidates.map((m, i) => {
5409
+ var _a2;
5410
+ return `[${i}] ${((_a2 = m == null ? void 0 : m.content) != null ? _a2 : "").replace(/\n/g, " ")}`;
5411
+ }).join("\n");
5412
+ const response = await llm.chat(
5413
+ [{ role: "user", content: `Query: "${query}"
5378
5414
 
5379
5415
  Documents:
5380
- ${topN.map((m, i) => {
5381
- var _a3;
5382
- return `[${i}] ${((_a3 = m == null ? void 0 : m.content) != null ? _a3 : "").replace(/\n/g, " ")}`;
5383
- }).join("\n")}` }],
5384
- "",
5385
- {
5386
- 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.',
5387
- temperature: 0,
5388
- maxTokens: 50
5389
- }
5390
- );
5391
- const cleanedResponse = response.trim().replace(/[\[\]\s]/g, "");
5392
- const rankedIndices = cleanedResponse.split(",").map(Number).filter((n) => !isNaN(n));
5393
- if (rankedIndices.length > 0) {
5394
- const rankedMatches = [];
5395
- const usedIndices = /* @__PURE__ */ new Set();
5396
- for (const index of rankedIndices) {
5397
- if (index >= 0 && index < topN.length && !usedIndices.has(index)) {
5398
- rankedMatches.push(topN[index]);
5399
- usedIndices.add(index);
5400
- }
5401
- }
5402
- for (let i = 0; i < topN.length; i++) {
5403
- if (!usedIndices.has(i)) {
5404
- rankedMatches.push(topN[i]);
5405
- }
5406
- }
5407
- const rest = matches.slice(10);
5408
- rankedMatches.push(...rest);
5409
- return rankedMatches.slice(0, limit);
5416
+ ${docList}` }],
5417
+ "",
5418
+ {
5419
+ systemPrompt: 'You are a relevance ranking expert. Given the user query and a list of retrieved document chunks, rank the chunks by relevance to the query. Return the result as a comma-separated list of document indices in order of relevance, from most relevant to least relevant. Use the indices provided in brackets like [0], [1], etc. Only return the indices (e.g., "2,0,1"), nothing else. Do not include brackets in the output.',
5420
+ temperature: 0,
5421
+ maxTokens: 50
5410
5422
  }
5411
- } catch (error) {
5412
- console.warn("[Reranker] LLM re-ranking failed, falling back to score-based sorting:", error);
5423
+ );
5424
+ const cleanedResponse = response.trim().replace(/[\[\]\s]/g, "");
5425
+ const rankedIndices = cleanedResponse.split(",").map(Number).filter((n) => !isNaN(n));
5426
+ if (rankedIndices.length === 0) {
5427
+ return matches.sort((a, b) => b.score - a.score).slice(0, limit);
5428
+ }
5429
+ const rankedMatches = [];
5430
+ const usedIndices = /* @__PURE__ */ new Set();
5431
+ for (const index of rankedIndices) {
5432
+ if (index >= 0 && index < budgetedCandidates.length && !usedIndices.has(index)) {
5433
+ rankedMatches.push(budgetedCandidates[index]);
5434
+ usedIndices.add(index);
5435
+ }
5436
+ }
5437
+ for (let i = 0; i < budgetedCandidates.length; i++) {
5438
+ if (!usedIndices.has(i)) rankedMatches.push(budgetedCandidates[i]);
5413
5439
  }
5414
- return matches.sort((a, b) => b.score - a.score).slice(0, limit);
5440
+ rankedMatches.push(...matches.slice(10));
5441
+ return rankedMatches.slice(0, limit);
5415
5442
  }
5416
5443
  };
5417
5444
 
@@ -5423,11 +5450,11 @@ var LlamaIndexIngestor = class {
5423
5450
  * than standard character-count splitting.
5424
5451
  */
5425
5452
  async chunk(text, options = {}) {
5426
- var _a3, _b;
5453
+ var _a2, _b;
5427
5454
  try {
5428
5455
  const { Document, SentenceSplitter, MetadataMode } = await import(`${"llamaindex"}`);
5429
5456
  const splitter = new SentenceSplitter({
5430
- chunkSize: (_a3 = options.chunkSize) != null ? _a3 : 1e3,
5457
+ chunkSize: (_a2 = options.chunkSize) != null ? _a2 : 1e3,
5431
5458
  chunkOverlap: (_b = options.chunkOverlap) != null ? _b : 200
5432
5459
  });
5433
5460
  const doc = new Document({ text, metadata: options.metadata || {} });
@@ -5535,7 +5562,7 @@ ${error instanceof Error ? error.message : String(error)}`
5535
5562
  * The agent returns `{ messages: [...] }` — the last message is the final answer.
5536
5563
  */
5537
5564
  async run(input, chatHistory = []) {
5538
- var _a3, _b, _c;
5565
+ var _a2, _b, _c;
5539
5566
  if (!this.agent) {
5540
5567
  throw new Error("[LangChainAgent] Agent not initialized. Call initialize() first.");
5541
5568
  }
@@ -5546,7 +5573,7 @@ ${error instanceof Error ? error.message : String(error)}`
5546
5573
  const response = await this.agent.invoke({
5547
5574
  messages: [...historyMessages, new HumanMessage(input)]
5548
5575
  });
5549
- const lastMessage = (_a3 = response == null ? void 0 : response.messages) == null ? void 0 : _a3.at(-1);
5576
+ const lastMessage = (_a2 = response == null ? void 0 : response.messages) == null ? void 0 : _a2.at(-1);
5550
5577
  if (lastMessage && typeof lastMessage.content === "string") {
5551
5578
  return lastMessage.content;
5552
5579
  }
@@ -5597,7 +5624,7 @@ var MCPClient = class {
5597
5624
  }
5598
5625
  }
5599
5626
  async connectStdio() {
5600
- var _a3;
5627
+ var _a2;
5601
5628
  const cmd = this.config.command;
5602
5629
  if (!cmd) {
5603
5630
  throw new Error(`[MCPClient] Command option is required for stdio transport on "${this.config.name}"`);
@@ -5612,7 +5639,7 @@ var MCPClient = class {
5612
5639
  this.stdioBuffer += data.toString("utf-8");
5613
5640
  this.processStdioLines();
5614
5641
  });
5615
- (_a3 = this.childProcess.stderr) == null ? void 0 : _a3.on("data", (data) => {
5642
+ (_a2 = this.childProcess.stderr) == null ? void 0 : _a2.on("data", (data) => {
5616
5643
  console.warn(`[MCPClient] [stderr] [${this.config.name}]:`, data.toString("utf-8"));
5617
5644
  });
5618
5645
  this.childProcess.on("close", (code) => {
@@ -5686,14 +5713,14 @@ var MCPClient = class {
5686
5713
  }
5687
5714
  }
5688
5715
  async sendNotification(method, params) {
5689
- var _a3;
5716
+ var _a2;
5690
5717
  const notification = {
5691
5718
  jsonrpc: "2.0",
5692
5719
  method,
5693
5720
  params
5694
5721
  };
5695
5722
  if (this.config.transport === "stdio") {
5696
- if ((_a3 = this.childProcess) == null ? void 0 : _a3.stdin) {
5723
+ if ((_a2 = this.childProcess) == null ? void 0 : _a2.stdin) {
5697
5724
  this.childProcess.stdin.write(JSON.stringify(notification) + "\n", "utf-8");
5698
5725
  }
5699
5726
  } else if (this.sseUrl) {
@@ -5771,11 +5798,11 @@ var MCPRegistry = class {
5771
5798
  // src/core/MultiAgentCoordinator.ts
5772
5799
  var MultiAgentCoordinator = class {
5773
5800
  constructor(options) {
5774
- var _a3;
5801
+ var _a2;
5775
5802
  this.llmProvider = options.llmProvider;
5776
5803
  this.mcpRegistry = options.mcpRegistry;
5777
5804
  this.documentSearch = options.documentSearch;
5778
- this.maxIterations = (_a3 = options.maxIterations) != null ? _a3 : 5;
5805
+ this.maxIterations = (_a2 = options.maxIterations) != null ? _a2 : 5;
5779
5806
  }
5780
5807
  /**
5781
5808
  * Run the multi-agent coordination loop synchronously and return the final response.
@@ -5827,8 +5854,8 @@ Available Tools:
5827
5854
 
5828
5855
  ${mcpTools.length > 0 ? "MCP Server Tools:" : ""}
5829
5856
  ${mcpTools.map((mt) => {
5830
- var _a3;
5831
- return `- ${mt.tool.name}(${JSON.stringify(((_a3 = mt.tool.inputSchema) == null ? void 0 : _a3.properties) || {})}): ${mt.tool.description || "No description provided."}`;
5857
+ var _a2;
5858
+ return `- ${mt.tool.name}(${JSON.stringify(((_a2 = mt.tool.inputSchema) == null ? void 0 : _a2.properties) || {})}): ${mt.tool.description || "No description provided."}`;
5832
5859
  }).join("\n")}
5833
5860
 
5834
5861
  Tool Calling Protocol:
@@ -5986,100 +6013,6 @@ ${toolResultText}`
5986
6013
  }
5987
6014
  };
5988
6015
 
5989
- // src/core/CircuitBreaker.ts
5990
- var CircuitBreaker = class {
5991
- constructor(options = {}) {
5992
- this.state = "closed";
5993
- this.failureCount = 0;
5994
- this.successCount = 0;
5995
- this.lastFailureAt = 0;
5996
- this.halfOpenCalls = 0;
5997
- var _a3, _b, _c;
5998
- this.failureThreshold = (_a3 = options.failureThreshold) != null ? _a3 : 5;
5999
- this.resetTimeoutMs = (_b = options.resetTimeoutMs) != null ? _b : 3e4;
6000
- this.halfOpenMaxCalls = (_c = options.halfOpenMaxCalls) != null ? _c : 1;
6001
- }
6002
- record(success) {
6003
- const now = Date.now();
6004
- if (success) {
6005
- this.successCount++;
6006
- if (this.state === "half-open") {
6007
- this.state = "closed";
6008
- this.failureCount = 0;
6009
- this.halfOpenCalls = 0;
6010
- this.successCount = 1;
6011
- } else if (this.state === "closed") {
6012
- if (this.successCount >= this.failureThreshold) {
6013
- this.failureCount = Math.max(0, this.failureCount - 1);
6014
- this.successCount = 0;
6015
- }
6016
- }
6017
- } else {
6018
- this.failureCount++;
6019
- this.lastFailureAt = now;
6020
- this.successCount = 0;
6021
- if (this.state === "half-open") {
6022
- this.state = "open";
6023
- this.halfOpenCalls = 0;
6024
- } else if (this.state === "closed" && this.failureCount >= this.failureThreshold) {
6025
- this.state = "open";
6026
- }
6027
- }
6028
- }
6029
- canExecute() {
6030
- const now = Date.now();
6031
- if (this.state === "closed") {
6032
- return true;
6033
- }
6034
- if (this.state === "open") {
6035
- if (now - this.lastFailureAt >= this.resetTimeoutMs) {
6036
- this.state = "half-open";
6037
- this.halfOpenCalls = 0;
6038
- return true;
6039
- }
6040
- return false;
6041
- }
6042
- if (this.state === "half-open") {
6043
- return this.halfOpenCalls < this.halfOpenMaxCalls;
6044
- }
6045
- return true;
6046
- }
6047
- async wrap(fn) {
6048
- if (!this.canExecute()) {
6049
- const retryAfterSec = Math.ceil(
6050
- Math.max(0, this.resetTimeoutMs - (Date.now() - this.lastFailureAt)) / 1e3
6051
- );
6052
- throw new Error(
6053
- `Circuit breaker is open. Try again in ${retryAfterSec}s. Failed ${this.failureCount}/${this.failureThreshold} consecutive calls.`
6054
- );
6055
- }
6056
- if (this.state === "half-open") {
6057
- this.halfOpenCalls++;
6058
- }
6059
- try {
6060
- const result = await fn();
6061
- this.record(true);
6062
- return result;
6063
- } catch (err) {
6064
- this.record(false);
6065
- throw err;
6066
- }
6067
- }
6068
- getState() {
6069
- return this.state;
6070
- }
6071
- getFailureCount() {
6072
- return this.failureCount;
6073
- }
6074
- reset() {
6075
- this.state = "closed";
6076
- this.failureCount = 0;
6077
- this.successCount = 0;
6078
- this.lastFailureAt = 0;
6079
- this.halfOpenCalls = 0;
6080
- }
6081
- };
6082
-
6083
6016
  // src/core/BatchProcessor.ts
6084
6017
  function isTransientError(error) {
6085
6018
  if (!(error instanceof Error)) return false;
@@ -6103,30 +6036,34 @@ function calculateBackoffDelay(attempt, initialDelayMs, maxDelayMs, multiplier)
6103
6036
  function sleep(ms) {
6104
6037
  return new Promise((resolve) => setTimeout(resolve, ms));
6105
6038
  }
6106
- var _BatchProcessor = class _BatchProcessor {
6039
+ var BatchProcessor = class _BatchProcessor {
6107
6040
  /**
6108
- * Execute a processor call through the circuit breaker.
6109
- * Only transient failures count toward opening the circuit.
6041
+ * Execute a processor call, optionally through a caller-provided circuit
6042
+ * breaker. Only transient failures count toward opening the circuit.
6043
+ *
6044
+ * Fix ARCH-3: removed the static module-level `cb` field. Callers must
6045
+ * supply their own CircuitBreaker via BatchOptions.circuitBreaker.
6046
+ * Fix TYPE-1: uses the public retryAfterMs() and incrementHalfOpen()
6047
+ * API instead of accessing private fields via `as any`.
6110
6048
  */
6111
- static async executeWithCircuitBreaker(processor) {
6112
- if (!this.cb.canExecute()) {
6113
- const retryAfterSec = Math.ceil(
6114
- Math.max(0, 3e4 - (Date.now() - this.cb.lastFailureAt || 0)) / 1e3
6115
- );
6049
+ static async executeWithCircuitBreaker(processor, cb) {
6050
+ if (!cb) {
6051
+ return processor();
6052
+ }
6053
+ if (!cb.canExecute()) {
6054
+ const retryAfterSec = Math.ceil(cb.retryAfterMs() / 1e3);
6116
6055
  throw new Error(
6117
6056
  `[BatchProcessor] Circuit breaker is open. Try again in ${retryAfterSec}s.`
6118
6057
  );
6119
6058
  }
6120
- if (this.cb.state === "half-open") {
6121
- this.cb.halfOpenCalls++;
6122
- }
6059
+ cb.incrementHalfOpen();
6123
6060
  try {
6124
6061
  const result = await processor();
6125
- this.cb.record(true);
6062
+ cb.record(true);
6126
6063
  return result;
6127
6064
  } catch (err) {
6128
6065
  if (isTransientError(err)) {
6129
- this.cb.record(false);
6066
+ cb.record(false);
6130
6067
  }
6131
6068
  throw err;
6132
6069
  }
@@ -6171,7 +6108,10 @@ var _BatchProcessor = class _BatchProcessor {
6171
6108
  let lastError;
6172
6109
  for (let attempt = 0; attempt <= maxRetries; attempt++) {
6173
6110
  try {
6174
- const result = await _BatchProcessor.executeWithCircuitBreaker(() => processor(batch));
6111
+ const result = await _BatchProcessor.executeWithCircuitBreaker(
6112
+ () => processor(batch),
6113
+ options == null ? void 0 : options.circuitBreaker
6114
+ );
6175
6115
  results.push(result);
6176
6116
  success = true;
6177
6117
  break;
@@ -6229,7 +6169,10 @@ ${errorMessages}`
6229
6169
  let lastError;
6230
6170
  for (let attempt = 0; attempt <= maxRetries; attempt++) {
6231
6171
  try {
6232
- const result = await _BatchProcessor.executeWithCircuitBreaker(() => processor(item));
6172
+ const result = await _BatchProcessor.executeWithCircuitBreaker(
6173
+ () => processor(item),
6174
+ options == null ? void 0 : options.circuitBreaker
6175
+ );
6233
6176
  results.push(result);
6234
6177
  success = true;
6235
6178
  break;
@@ -6302,7 +6245,10 @@ ${errorMessages}`
6302
6245
  }));
6303
6246
  const chunkPromises = chunk.map(async ({ item, originalIndex }) => {
6304
6247
  try {
6305
- const result = await _BatchProcessor.executeWithCircuitBreaker(() => processor(item));
6248
+ const result = await _BatchProcessor.executeWithCircuitBreaker(
6249
+ () => processor(item),
6250
+ options == null ? void 0 : options.circuitBreaker
6251
+ );
6306
6252
  return { success: true, result, index: originalIndex };
6307
6253
  } catch (err) {
6308
6254
  const error = err instanceof Error ? err : new Error(String(err));
@@ -6330,12 +6276,6 @@ ${errorMessages}`
6330
6276
  return { results, errors, totalProcessed, totalFailed };
6331
6277
  }
6332
6278
  };
6333
- _BatchProcessor.cb = new CircuitBreaker({
6334
- failureThreshold: 5,
6335
- resetTimeoutMs: 3e4,
6336
- halfOpenMaxCalls: 1
6337
- });
6338
- var BatchProcessor = _BatchProcessor;
6339
6279
 
6340
6280
  // src/config/EmbeddingStrategy.ts
6341
6281
  var EmbeddingStrategyResolver = class {
@@ -6433,7 +6373,7 @@ var QueryProcessor = class {
6433
6373
  * @param validFields Optional list of known filterable fields to look for
6434
6374
  */
6435
6375
  static extractQueryFieldHints(question, validFields = []) {
6436
- var _a3, _b, _c, _d;
6376
+ var _a2, _b, _c, _d, _e, _f, _g2, _h;
6437
6377
  if (!question.trim()) return [];
6438
6378
  const hints = /* @__PURE__ */ new Map();
6439
6379
  const addHint = (value, field) => {
@@ -6478,11 +6418,11 @@ var QueryProcessor = class {
6478
6418
  if (name) addHint(name, "name");
6479
6419
  }
6480
6420
  }
6481
- const genericPattern = /\b([a-z][a-z0-9_]{1,30})\s*[:=]\s*["']?([^"'\n?.!,]{1,60})["']?/gi;
6421
+ const genericPattern = /\b([a-z][a-z0-9_]{1,30})\s*[:=]\s*(?:"([^"\n?.!,]{1,60})"|'([^'\n?.!,]{1,60})'|([^\s\n?.!,:=]{1,60}))/gi;
6482
6422
  for (const match of question.matchAll(genericPattern)) {
6483
6423
  const field = match[1];
6484
- const value = match[2];
6485
- if (field && !this.isLikelyPromptPhrase(field)) {
6424
+ const value = (_b = (_a2 = match[2]) != null ? _a2 : match[3]) != null ? _b : match[4];
6425
+ if (field && value && !this.isLikelyPromptPhrase(field)) {
6486
6426
  const resolvedField = resolveValidField(field);
6487
6427
  if (resolvedField) {
6488
6428
  addHint(value, resolvedField);
@@ -6513,7 +6453,7 @@ var QueryProcessor = class {
6513
6453
  ];
6514
6454
  for (const p of universalPatterns) {
6515
6455
  for (const match of question.matchAll(p.regex)) {
6516
- const val = p.group ? (_a3 = match[p.group]) != null ? _a3 : match[0] : match[0];
6456
+ const val = p.group ? (_c = match[p.group]) != null ? _c : match[0] : match[0];
6517
6457
  if (!val) continue;
6518
6458
  if (p.field) addHint(val, p.field);
6519
6459
  else addHint(val);
@@ -6521,22 +6461,23 @@ var QueryProcessor = class {
6521
6461
  }
6522
6462
  for (const pattern of naturalQuestionPatterns) {
6523
6463
  for (const match of question.matchAll(pattern)) {
6524
- const value = (_b = match[2]) != null ? _b : match[1];
6464
+ const value = (_d = match[2]) != null ? _d : match[1];
6525
6465
  if (value) addHint(value);
6526
6466
  }
6527
6467
  }
6528
6468
  const fieldPattern = `([^\\n:=?.!,]{1,60}?)`;
6529
- const valuePattern = `([^\\n?.!,]{1,120}?)`;
6469
+ const valuePattern = `(?:"([^"\\n?.!,]{1,120})"|'([^'\\n?.!,]{1,120})'|([^\\s\\n?.!,:=]{1,120}))`;
6530
6470
  const fieldValuePatterns = [
6531
- new RegExp(`\\b${fieldPattern}\\s*(?:=|:)\\s*["']?${valuePattern}["']?(?=[?.!,]|$)`, "gi"),
6532
- new RegExp(`\\b${fieldPattern}\\s+(?:is|are|was|were|equals?|equal to|named|called)\\s+["']?${valuePattern}["']?(?=[?.!,]|$)`, "gi"),
6533
- new RegExp(`\\bwith\\s+${fieldPattern}\\s+["']?${valuePattern}["']?(?=[?.!,]|$)`, "gi"),
6534
- new RegExp(`\\b(?:under|in|for|from|of)\\s+${fieldPattern}\\s+["']?${valuePattern}["']?(?=[?.!,]|$)`, "gi")
6471
+ new RegExp(`\\b${fieldPattern}\\s*(?:=|:)\\s*${valuePattern}`, "gi"),
6472
+ new RegExp(`\\b${fieldPattern}\\s+(?:is|are|was|were|equals?|equal to|named|called)\\s+${valuePattern}`, "gi"),
6473
+ new RegExp(`\\bwith\\s+${fieldPattern}\\s+${valuePattern}`, "gi"),
6474
+ new RegExp(`\\b(?:under|in|for|from|of)\\s+${fieldPattern}\\s+${valuePattern}`, "gi")
6535
6475
  ];
6536
6476
  for (const pattern of fieldValuePatterns) {
6537
6477
  for (const match of question.matchAll(pattern)) {
6538
- const field = (_c = match[1]) != null ? _c : "";
6539
- const value = (_d = match[2]) != null ? _d : "";
6478
+ const field = (_e = match[1]) != null ? _e : "";
6479
+ const value = (_h = (_g2 = (_f = match[2]) != null ? _f : match[3]) != null ? _g2 : match[4]) != null ? _h : "";
6480
+ if (!value) continue;
6540
6481
  const resolvedField = resolveValidField(field);
6541
6482
  if (resolvedField) {
6542
6483
  addHint(value, resolvedField);
@@ -6606,10 +6547,10 @@ var QueryProcessor = class {
6606
6547
  }
6607
6548
  static normalizeNumericOperator(operator) {
6608
6549
  const op = operator.toLowerCase().trim();
6609
- if (op === ">" || /\b(greater than|more than|above|over)\b/.test(op)) return "gt";
6610
6550
  if (op === ">=" || /\b(greater than or equal to|more than or equal to|at least)\b/.test(op)) return "gte";
6611
- if (op === "<" || /\b(less than|below|under)\b/.test(op)) return "lt";
6612
6551
  if (op === "<=" || /\b(less than or equal to|at most)\b/.test(op)) return "lte";
6552
+ if (op === ">" || /\b(greater than|more than|above|over)\b/.test(op)) return "gt";
6553
+ if (op === "<" || /\b(less than|below|under)\b/.test(op)) return "lt";
6613
6554
  return "eq";
6614
6555
  }
6615
6556
  static normalizePredicateField(field, validFields) {
@@ -6737,11 +6678,11 @@ var LLMRouter = class {
6737
6678
  * When provided it is used directly as the 'default' role without re-constructing.
6738
6679
  */
6739
6680
  async initialize(prebuiltDefault) {
6740
- var _a3;
6681
+ var _a2;
6741
6682
  const defaultModel = prebuiltDefault != null ? prebuiltDefault : LLMFactory.create(this.config.llm, this.config.embedding);
6742
6683
  this.models.set("default", defaultModel);
6743
6684
  const envFastModel = process.env.FAST_LLM_MODEL;
6744
- const providerFastDefault = (_a3 = FAST_MODEL_DEFAULTS[this.config.llm.provider]) != null ? _a3 : "";
6685
+ const providerFastDefault = (_a2 = FAST_MODEL_DEFAULTS[this.config.llm.provider]) != null ? _a2 : "";
6745
6686
  const fastModelName = envFastModel || providerFastDefault;
6746
6687
  if (fastModelName && fastModelName !== this.config.llm.model) {
6747
6688
  const fastConfig = __spreadProps(__spreadValues({}, this.config.llm), {
@@ -6758,8 +6699,8 @@ var LLMRouter = class {
6758
6699
  * Falls back to 'default' if the requested role is not registered.
6759
6700
  */
6760
6701
  get(role) {
6761
- var _a3;
6762
- const provider = (_a3 = this.models.get(role)) != null ? _a3 : this.models.get("default");
6702
+ var _a2;
6703
+ const provider = (_a2 = this.models.get(role)) != null ? _a2 : this.models.get("default");
6763
6704
  if (!provider) {
6764
6705
  throw new Error(`[LLMRouter] No provider registered for role: "${role}". Did you call initialize()?`);
6765
6706
  }
@@ -6845,8 +6786,8 @@ var IntentClassifier = class {
6845
6786
  numericFieldCount = numericKeys.size;
6846
6787
  categoricalFieldCount = catKeys.size;
6847
6788
  if (productKeyMatches >= 2 || docs.some((d) => {
6848
- var _a3, _b;
6849
- 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");
6789
+ var _a2, _b;
6790
+ 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");
6850
6791
  })) {
6851
6792
  isProductLike = true;
6852
6793
  }
@@ -7036,7 +6977,7 @@ var Rule6SmallResultSetRule = class {
7036
6977
  var Rule7LargeTableRule = class {
7037
6978
  constructor() {
7038
6979
  this.name = "Rule7LargeTable";
7039
- this.priority = 75;
6980
+ this.priority = 82;
7040
6981
  }
7041
6982
  evaluate(context, intent) {
7042
6983
  const docs = context.retrievedDocuments || [];
@@ -7079,8 +7020,8 @@ var RuleEngine = class {
7079
7020
  this.registerRule(new Rule5MixedResponseRule());
7080
7021
  this.registerRule(new Rule2ComparisonRule());
7081
7022
  this.registerRule(new Rule3ProductDiscoveryRule());
7082
- this.registerRule(new Rule4AnalyticalRule());
7083
7023
  this.registerRule(new Rule7LargeTableRule());
7024
+ this.registerRule(new Rule4AnalyticalRule());
7084
7025
  }
7085
7026
  /**
7086
7027
  * Add or replace a rule in the engine.
@@ -7128,8 +7069,8 @@ var TextRendererStrategy = class {
7128
7069
  }
7129
7070
  const docs = Array.isArray(data) ? data : [];
7130
7071
  const text = docs.map((d) => {
7131
- var _a3;
7132
- return (_a3 = d == null ? void 0 : d.content) != null ? _a3 : "";
7072
+ var _a2;
7073
+ return (_a2 = d == null ? void 0 : d.content) != null ? _a2 : "";
7133
7074
  }).join("\n\n");
7134
7075
  return { type: "text", title, data: { content: text || "No detailed content available." } };
7135
7076
  }
@@ -7272,9 +7213,9 @@ var LRUDecisionCache = class {
7272
7213
  this.maxSize = maxSize;
7273
7214
  }
7274
7215
  generateKey(context) {
7275
- var _a3, _b;
7216
+ var _a2, _b;
7276
7217
  const q = (context.userQuery || "").toLowerCase().trim();
7277
- const docCount = (_b = (_a3 = context.retrievedDocuments) == null ? void 0 : _a3.length) != null ? _b : 0;
7218
+ const docCount = (_b = (_a2 = context.retrievedDocuments) == null ? void 0 : _a2.length) != null ? _b : 0;
7278
7219
  return `${q}::docs:${docCount}`;
7279
7220
  }
7280
7221
  get(context) {
@@ -7375,7 +7316,7 @@ var UITransformer = class _UITransformer {
7375
7316
  * Prefer `analyzeAndDecide()` in production.
7376
7317
  */
7377
7318
  static transform(userQuery, retrievedData, config, trainedSchema, intent) {
7378
- var _a3, _b, _c;
7319
+ var _a2, _b, _c;
7379
7320
  if (!retrievedData || retrievedData.length === 0) {
7380
7321
  return this.createTextResponse("No data available", "No relevant data found for your query.");
7381
7322
  }
@@ -7395,7 +7336,7 @@ var UITransformer = class _UITransformer {
7395
7336
  return this.transformToBarChart(filteredData, profile, userQuery, resolvedIntent.visualizationHint === "ranking");
7396
7337
  }
7397
7338
  if (resolvedIntent.visualizationHint === "distribution") {
7398
- return (_a3 = this.transformToHistogram(profile, userQuery)) != null ? _a3 : this.transformToBarChart(filteredData, profile, userQuery);
7339
+ return (_a2 = this.transformToHistogram(profile, userQuery)) != null ? _a2 : this.transformToBarChart(filteredData, profile, userQuery);
7399
7340
  }
7400
7341
  if (resolvedIntent.visualizationHint === "correlation") {
7401
7342
  return (_b = this.transformToScatterPlot(profile, userQuery)) != null ? _b : this.transformToBarChart(filteredData, profile, userQuery);
@@ -7728,11 +7669,11 @@ ${schemaProfileText}` : ""}`;
7728
7669
  };
7729
7670
  }
7730
7671
  static transformToPieChart(data, profile, query = "") {
7731
- var _a3;
7672
+ var _a2;
7732
7673
  const dimension = profile ? this.selectDimensionField(profile, query) : void 0;
7733
7674
  const categories = dimension ? Array.from(new Set(profile.records.map((record) => {
7734
- var _a4;
7735
- return String((_a4 = record.fields[dimension.key]) != null ? _a4 : "");
7675
+ var _a3;
7676
+ return String((_a3 = record.fields[dimension.key]) != null ? _a3 : "");
7736
7677
  }).filter(Boolean))) : this.detectCategories(data);
7737
7678
  if (categories.length === 0) return null;
7738
7679
  const categoryData = dimension && profile ? this.aggregateProfileByDimension(profile, dimension.key) : this.aggregateByCategory(data, categories);
@@ -7742,7 +7683,7 @@ ${schemaProfileText}` : ""}`;
7742
7683
  });
7743
7684
  return {
7744
7685
  type: "pie_chart",
7745
- title: `Distribution by ${(_a3 = dimension == null ? void 0 : dimension.label) != null ? _a3 : "Category"}`,
7686
+ title: `Distribution by ${(_a2 = dimension == null ? void 0 : dimension.label) != null ? _a2 : "Category"}`,
7746
7687
  description: `Showing breakdown across ${pieData.length} categories`,
7747
7688
  data: pieData
7748
7689
  };
@@ -7752,8 +7693,8 @@ ${schemaProfileText}` : ""}`;
7752
7693
  const valueField = profile.numericFields[0];
7753
7694
  const buckets = /* @__PURE__ */ new Map();
7754
7695
  profile.records.forEach((record) => {
7755
- var _a3, _b, _c;
7756
- const timestamp = String((_a3 = record.fields[dateField.key]) != null ? _a3 : "");
7696
+ var _a2, _b, _c;
7697
+ const timestamp = String((_a2 = record.fields[dateField.key]) != null ? _a2 : "");
7757
7698
  const value = (_b = this.toFiniteNumber(record.fields[valueField.key])) != null ? _b : 0;
7758
7699
  buckets.set(timestamp, ((_c = buckets.get(timestamp)) != null ? _c : 0) + value);
7759
7700
  });
@@ -7766,23 +7707,23 @@ ${schemaProfileText}` : ""}`;
7766
7707
  };
7767
7708
  }
7768
7709
  static transformToBarChart(data, profile, query = "", horizontal = false) {
7769
- var _a3;
7710
+ var _a2;
7770
7711
  const dimension = profile ? this.selectDimensionField(profile, query) : void 0;
7771
7712
  const measure = profile ? this.selectNumericField(profile, query) : void 0;
7772
7713
  const aggregate = dimension && profile ? this.aggregateProfileByDimension(profile, dimension.key, measure == null ? void 0 : measure.key) : this.aggregateByCategory(data, this.detectCategories(data));
7773
7714
  const barData = Object.entries(aggregate).map(([category, value]) => ({ category, value: Number(value) })).sort((a, b) => horizontal ? b.value - a.value : 0).slice(0, 12);
7774
7715
  const fallbackData = barData.length > 0 ? barData : data.slice(0, 12).map((item, index) => {
7775
- var _a4, _b, _c, _d, _e;
7716
+ var _a3, _b, _c, _d, _e;
7776
7717
  const meta = item.metadata || {};
7777
7718
  const label = String(
7778
- (_c = (_b = (_a4 = this.getDynamicVal(meta, "name")) != null ? _a4 : meta.label) != null ? _b : meta.title) != null ? _c : `Result ${index + 1}`
7719
+ (_c = (_b = (_a3 = this.getDynamicVal(meta, "name")) != null ? _a3 : meta.label) != null ? _b : meta.title) != null ? _c : `Result ${index + 1}`
7779
7720
  );
7780
7721
  const value = (_e = (_d = this.extractNumericValue(meta)) != null ? _d : item.score) != null ? _e : 0;
7781
7722
  return { category: label, value: Number(value) };
7782
7723
  });
7783
7724
  return {
7784
7725
  type: horizontal ? "horizontal_bar" : "bar_chart",
7785
- title: dimension ? `${(_a3 = measure == null ? void 0 : measure.label) != null ? _a3 : "Count"} by ${dimension.label}` : "Comparison",
7726
+ title: dimension ? `${(_a2 = measure == null ? void 0 : measure.label) != null ? _a2 : "Count"} by ${dimension.label}` : "Comparison",
7786
7727
  description: `Showing ${fallbackData.length} comparable values`,
7787
7728
  data: fallbackData
7788
7729
  };
@@ -7817,11 +7758,11 @@ ${schemaProfileText}` : ""}`;
7817
7758
  if (fields.length < 2) return null;
7818
7759
  const [xField, yField] = fields;
7819
7760
  const points = profile.records.map((record) => {
7820
- var _a3;
7761
+ var _a2;
7821
7762
  const x = this.toFiniteNumber(record.fields[xField.key]);
7822
7763
  const y = this.toFiniteNumber(record.fields[yField.key]);
7823
7764
  if (x === null || y === null) return null;
7824
- return { x, y, label: String((_a3 = this.getRecordLabel(record)) != null ? _a3 : record.id) };
7765
+ return { x, y, label: String((_a2 = this.getRecordLabel(record)) != null ? _a2 : record.id) };
7825
7766
  }).filter((point) => point !== null).slice(0, 100);
7826
7767
  if (points.length === 0) return null;
7827
7768
  return {
@@ -7851,9 +7792,9 @@ ${schemaProfileText}` : ""}`;
7851
7792
  static transformToRadarChart(data) {
7852
7793
  const attributeMap = {};
7853
7794
  data.forEach((item) => {
7854
- var _a3, _b, _c;
7795
+ var _a2, _b, _c;
7855
7796
  const meta = item.metadata || {};
7856
- const seriesName = String((_c = (_b = (_a3 = meta.name) != null ? _a3 : meta.product) != null ? _b : item.id) != null ? _c : "Item");
7797
+ const seriesName = String((_c = (_b = (_a2 = meta.name) != null ? _a2 : meta.product) != null ? _b : item.id) != null ? _c : "Item");
7857
7798
  Object.entries(meta).forEach(([key, val]) => {
7858
7799
  if (typeof val === "number" && !["price", "id"].includes(key.toLowerCase())) {
7859
7800
  if (!attributeMap[key]) attributeMap[key] = {};
@@ -7869,8 +7810,8 @@ ${schemaProfileText}` : ""}`;
7869
7810
  title: "Product Comparison",
7870
7811
  description: `Comparing ${data.length} items across ${radarData.length} attributes`,
7871
7812
  data: radarData.length > 0 ? radarData : data.map((d) => {
7872
- var _a3;
7873
- return { attribute: ((_a3 = d == null ? void 0 : d.content) != null ? _a3 : "").substring(0, 40) };
7813
+ var _a2;
7814
+ return { attribute: ((_a2 = d == null ? void 0 : d.content) != null ? _a2 : "").substring(0, 40) };
7874
7815
  })
7875
7816
  };
7876
7817
  }
@@ -7889,8 +7830,8 @@ ${schemaProfileText}` : ""}`;
7889
7830
  return this.createTextResponse(
7890
7831
  "Retrieved Context",
7891
7832
  data.map((item) => {
7892
- var _a3;
7893
- return (_a3 = item == null ? void 0 : item.content) != null ? _a3 : "";
7833
+ var _a2;
7834
+ return (_a2 = item == null ? void 0 : item.content) != null ? _a2 : "";
7894
7835
  }).join("\n\n"),
7895
7836
  `Found ${data.length} relevant results`
7896
7837
  );
@@ -7941,11 +7882,11 @@ ${schemaProfileText}` : ""}`;
7941
7882
  return null;
7942
7883
  }
7943
7884
  static normalizeTransformation(payload) {
7944
- var _a3, _b, _c, _d, _e, _f, _g2, _h, _i, _j;
7885
+ var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j;
7945
7886
  if (!payload || typeof payload !== "object") return null;
7946
7887
  const p = payload;
7947
7888
  const type = this.normalizeVisualizationType(
7948
- String((_c = (_b = (_a3 = p.type) != null ? _a3 : p.view) != null ? _b : p.chartType) != null ? _c : "")
7889
+ String((_c = (_b = (_a2 = p.type) != null ? _a2 : p.view) != null ? _b : p.chartType) != null ? _c : "")
7949
7890
  );
7950
7891
  if (!type) return null;
7951
7892
  const title = String((_e = (_d = p.title) != null ? _d : p.heading) != null ? _e : "Visualization");
@@ -7956,7 +7897,7 @@ ${schemaProfileText}` : ""}`;
7956
7897
  return this.validateTransformation(transformation) ? transformation : null;
7957
7898
  }
7958
7899
  static normalizeVisualizationType(type) {
7959
- var _a3;
7900
+ var _a2;
7960
7901
  const mapping = {
7961
7902
  pie: "pie_chart",
7962
7903
  pie_chart: "pie_chart",
@@ -7984,7 +7925,7 @@ ${schemaProfileText}` : ""}`;
7984
7925
  product_carousel: "product_carousel",
7985
7926
  carousel: "carousel"
7986
7927
  };
7987
- return (_a3 = mapping[type.toLowerCase()]) != null ? _a3 : null;
7928
+ return (_a2 = mapping[type.toLowerCase()]) != null ? _a2 : null;
7988
7929
  }
7989
7930
  static validateTransformation(t) {
7990
7931
  const { type, data } = t;
@@ -8100,7 +8041,7 @@ ${schemaProfileText}` : ""}`;
8100
8041
  }
8101
8042
  static profileData(data) {
8102
8043
  const records = (data || []).filter((item) => Boolean(item && typeof item === "object")).map((item) => {
8103
- var _a3, _b;
8044
+ var _a2, _b;
8104
8045
  const fields2 = {};
8105
8046
  Object.entries(item.metadata || {}).forEach(([key, value]) => {
8106
8047
  const primitive = this.toPrimitive(value);
@@ -8109,7 +8050,7 @@ ${schemaProfileText}` : ""}`;
8109
8050
  if (!fields2.content && item.content) fields2.content = item.content.substring(0, 500);
8110
8051
  return {
8111
8052
  id: item.id,
8112
- content: (_a3 = item.content) != null ? _a3 : "",
8053
+ content: (_a2 = item.content) != null ? _a2 : "",
8113
8054
  score: (_b = item.score) != null ? _b : 0,
8114
8055
  fields: fields2,
8115
8056
  source: item
@@ -8190,16 +8131,16 @@ ${schemaProfileText}` : ""}`;
8190
8131
  return null;
8191
8132
  }
8192
8133
  static selectDimensionField(profile, query) {
8193
- var _a3, _b;
8134
+ var _a2, _b;
8194
8135
  const productCategory = profile.categoricalFields.find(
8195
8136
  (field) => /category|department|collection|type|group|segment|region|country|state|city/i.test(field.key)
8196
8137
  );
8197
8138
  const ranked = this.rankFieldsByQuery(profile.categoricalFields, query);
8198
- return (_b = (_a3 = ranked[0]) != null ? _a3 : productCategory) != null ? _b : profile.categoricalFields[0];
8139
+ return (_b = (_a2 = ranked[0]) != null ? _a2 : productCategory) != null ? _b : profile.categoricalFields[0];
8199
8140
  }
8200
8141
  static selectNumericField(profile, query) {
8201
- var _a3;
8202
- return (_a3 = this.rankFieldsByQuery(profile.numericFields, query)[0]) != null ? _a3 : profile.numericFields[0];
8142
+ var _a2;
8143
+ return (_a2 = this.rankFieldsByQuery(profile.numericFields, query)[0]) != null ? _a2 : profile.numericFields[0];
8203
8144
  }
8204
8145
  static rankFieldsByQuery(fields, query) {
8205
8146
  const q = query.toLowerCase();
@@ -8228,8 +8169,8 @@ ${schemaProfileText}` : ""}`;
8228
8169
  static aggregateProfileByDimension(profile, dimensionKey, measureKey) {
8229
8170
  const result = {};
8230
8171
  profile.records.forEach((record) => {
8231
- var _a3, _b, _c;
8232
- const category = String((_a3 = record.fields[dimensionKey]) != null ? _a3 : "Other").trim() || "Other";
8172
+ var _a2, _b, _c;
8173
+ const category = String((_a2 = record.fields[dimensionKey]) != null ? _a2 : "Other").trim() || "Other";
8233
8174
  const value = measureKey ? (_b = this.toFiniteNumber(record.fields[measureKey])) != null ? _b : 0 : 1;
8234
8175
  result[category] = ((_c = result[category]) != null ? _c : 0) + value;
8235
8176
  });
@@ -8309,8 +8250,8 @@ ${schemaProfileText}` : ""}`;
8309
8250
  static aggregateByCategory(data, categories) {
8310
8251
  const result = Object.fromEntries(categories.map((c) => [c, 0]));
8311
8252
  data.forEach((item) => {
8312
- var _a3;
8313
- const cat = (_a3 = this.getProductCategory(item)) != null ? _a3 : "Other";
8253
+ var _a2;
8254
+ const cat = (_a2 = this.getProductCategory(item)) != null ? _a2 : "Other";
8314
8255
  if (Object.prototype.hasOwnProperty.call(result, cat)) result[cat]++;
8315
8256
  else result["Other"] = (result["Other"] || 0) + 1;
8316
8257
  });
@@ -8318,17 +8259,17 @@ ${schemaProfileText}` : ""}`;
8318
8259
  }
8319
8260
  static extractTimeSeriesData(data) {
8320
8261
  return data.map((item) => {
8321
- var _a3, _b, _c, _d, _e, _f;
8262
+ var _a2, _b, _c, _d, _e, _f;
8322
8263
  const meta = item.metadata || {};
8323
8264
  return {
8324
- timestamp: (_b = (_a3 = meta.timestamp) != null ? _a3 : meta.date) != null ? _b : (/* @__PURE__ */ new Date()).toISOString(),
8265
+ timestamp: (_b = (_a2 = meta.timestamp) != null ? _a2 : meta.date) != null ? _b : (/* @__PURE__ */ new Date()).toISOString(),
8325
8266
  value: (_d = (_c = meta.value) != null ? _c : item.score) != null ? _d : 0,
8326
8267
  label: (_f = meta.label) != null ? _f : ((_e = item == null ? void 0 : item.content) != null ? _e : "").substring(0, 50)
8327
8268
  };
8328
8269
  });
8329
8270
  }
8330
8271
  static extractNumericValue(meta) {
8331
- var _a3;
8272
+ var _a2;
8332
8273
  const preferredKeys = [
8333
8274
  "value",
8334
8275
  "count",
@@ -8343,7 +8284,7 @@ ${schemaProfileText}` : ""}`;
8343
8284
  "price"
8344
8285
  ];
8345
8286
  for (const key of preferredKeys) {
8346
- const raw = (_a3 = resolveMetadataValue(meta, key)) != null ? _a3 : meta[key];
8287
+ const raw = (_a2 = resolveMetadataValue(meta, key)) != null ? _a2 : meta[key];
8347
8288
  const value = typeof raw === "number" ? raw : Number(String(raw != null ? raw : "").replace(/[$,% ,]/g, ""));
8348
8289
  if (Number.isFinite(value)) return value;
8349
8290
  }
@@ -8438,8 +8379,8 @@ ${schemaProfileText}` : ""}`;
8438
8379
  }, query.includes(normalizedField) ? 3 : 0);
8439
8380
  }
8440
8381
  static resolveTableCellValue(item, column) {
8441
- var _a3, _b, _c;
8442
- if (column === "Content") return ((_a3 = item == null ? void 0 : item.content) != null ? _a3 : "").substring(0, 100);
8382
+ var _a2, _b, _c;
8383
+ if (column === "Content") return ((_a2 = item == null ? void 0 : item.content) != null ? _a2 : "").substring(0, 100);
8443
8384
  const meta = item.metadata || {};
8444
8385
  const normalizedColumn = this.normalizeComparableField(column);
8445
8386
  const exactMetadata = Object.entries(meta).find(
@@ -8491,8 +8432,8 @@ ${schemaProfileText}` : ""}`;
8491
8432
  let inStock = 0;
8492
8433
  let outOfStock = 0;
8493
8434
  data.forEach((d) => {
8494
- var _a3, _b;
8495
- const cat = (_a3 = this.getProductCategory(d)) != null ? _a3 : "Other";
8435
+ var _a2, _b;
8436
+ const cat = (_a2 = this.getProductCategory(d)) != null ? _a2 : "Other";
8496
8437
  if (cat === category) {
8497
8438
  const quantity = (_b = this.extractStockQuantity(d)) != null ? _b : 1;
8498
8439
  if (this.determineStockStatus(d)) inStock += quantity;
@@ -8538,9 +8479,9 @@ ${schemaProfileText}` : ""}`;
8538
8479
  }
8539
8480
  // ─── Product Extraction ───────────────────────────────────────────────────
8540
8481
  static getDynamicVal(meta, uiKey, config, trainedSchema) {
8541
- var _a3;
8482
+ var _a2;
8542
8483
  if (!meta) return void 0;
8543
- const mapping = (_a3 = config == null ? void 0 : config.rag) == null ? void 0 : _a3.uiMapping;
8484
+ const mapping = (_a2 = config == null ? void 0 : config.rag) == null ? void 0 : _a2.uiMapping;
8544
8485
  if ((mapping == null ? void 0 : mapping[uiKey]) && meta[mapping[uiKey]] !== void 0) return meta[mapping[uiKey]];
8545
8486
  if (trainedSchema && typeof trainedSchema === "object") {
8546
8487
  const trainedKey = trainedSchema[uiKey];
@@ -8549,7 +8490,7 @@ ${schemaProfileText}` : ""}`;
8549
8490
  return resolveMetadataValue(meta, uiKey);
8550
8491
  }
8551
8492
  static extractProductInfo(item, config, trainedSchema) {
8552
- var _a3, _b;
8493
+ var _a2, _b;
8553
8494
  if (!item) return null;
8554
8495
  const meta = item.metadata || {};
8555
8496
  const content = item.content || "";
@@ -8557,7 +8498,7 @@ ${schemaProfileText}` : ""}`;
8557
8498
  const price = this.getDynamicVal(meta, "price", config, trainedSchema);
8558
8499
  const brand = this.getDynamicVal(meta, "brand", config, trainedSchema);
8559
8500
  const description = this.cleanProductDescription(
8560
- (_a3 = this.extractProductDescriptionFromContent(content)) != null ? _a3 : this.getProductDescriptionValue(meta, config, trainedSchema)
8501
+ (_a2 = this.extractProductDescriptionFromContent(content)) != null ? _a2 : this.getProductDescriptionValue(meta, config, trainedSchema)
8561
8502
  );
8562
8503
  if (name || this.isProductData(item)) {
8563
8504
  let finalName = name ? String(name) : void 0;
@@ -8692,10 +8633,10 @@ RULES:
8692
8633
  }
8693
8634
  static buildContextSummary(sources, maxChars = 6e3) {
8694
8635
  const items = (sources || []).filter(Boolean).map((s, i) => {
8695
- var _a3, _b, _c, _d;
8636
+ var _a2, _b, _c, _d;
8696
8637
  return {
8697
8638
  index: i + 1,
8698
- content: (_b = (_a3 = s == null ? void 0 : s.content) == null ? void 0 : _a3.substring(0, 400)) != null ? _b : "",
8639
+ content: (_b = (_a2 = s == null ? void 0 : s.content) == null ? void 0 : _a2.substring(0, 400)) != null ? _b : "",
8699
8640
  metadata: (_c = s == null ? void 0 : s.metadata) != null ? _c : {},
8700
8641
  score: (_d = s == null ? void 0 : s.score) != null ? _d : 0
8701
8642
  };
@@ -8735,7 +8676,7 @@ var SchemaMapper = class {
8735
8676
  return promise;
8736
8677
  }
8737
8678
  static async _doTrain(llm, cacheKey, keys) {
8738
- var _a3, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k;
8679
+ var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k;
8739
8680
  const propertyList = Object.entries(this.TARGET_PROPERTIES).map(([prop, desc]) => `- ${prop} (${desc})`).join("\n");
8740
8681
  const messages = [
8741
8682
  { role: "system", content: "You are a database schema expert. Respond ONLY with valid JSON." },
@@ -8750,7 +8691,7 @@ Return a JSON object like {"name":"Title","price":"Price",...}. Omit unmapped pr
8750
8691
  }
8751
8692
  ];
8752
8693
  try {
8753
- const baseUrl = (_a3 = llm == null ? void 0 : llm.baseUrl) != null ? _a3 : "";
8694
+ const baseUrl = (_a2 = llm == null ? void 0 : llm.baseUrl) != null ? _a2 : "";
8754
8695
  const apiKey = (_b = llm == null ? void 0 : llm.apiKey) != null ? _b : "";
8755
8696
  const model = (_c = llm == null ? void 0 : llm.model) != null ? _c : "llama-3.1-8b-instant";
8756
8697
  let responseText;
@@ -8925,9 +8866,9 @@ var Pipeline = class {
8925
8866
  this.initialised = false;
8926
8867
  /** Namespace-specific static cold context cache for CAG */
8927
8868
  this.coldContexts = /* @__PURE__ */ new Map();
8928
- var _a3, _b, _c, _d, _e;
8869
+ var _a2, _b, _c, _d, _e;
8929
8870
  this.chunker = new DocumentChunker(
8930
- (_b = (_a3 = config.rag) == null ? void 0 : _a3.chunkSize) != null ? _b : 1e3,
8871
+ (_b = (_a2 = config.rag) == null ? void 0 : _a2.chunkSize) != null ? _b : 1e3,
8931
8872
  (_d = (_c = config.rag) == null ? void 0 : _c.chunkOverlap) != null ? _d : 200
8932
8873
  );
8933
8874
  if (((_e = config.rag) == null ? void 0 : _e.chunkingStrategy) === "llamaindex") {
@@ -8943,7 +8884,7 @@ var Pipeline = class {
8943
8884
  return this.initialised ? this.llmProvider : void 0;
8944
8885
  }
8945
8886
  async initialize() {
8946
- var _a3, _b, _c, _d;
8887
+ var _a2, _b, _c, _d;
8947
8888
  if (this.initialised) return;
8948
8889
  const chartInstruction = `You are a helpful assistant. Use the provided context to answer questions accurately.
8949
8890
 
@@ -8986,7 +8927,7 @@ var Pipeline = class {
8986
8927
  this.entityExtractor = new EntityExtractor(this.llmProvider);
8987
8928
  }
8988
8929
  await this.vectorDB.initialize();
8989
- 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) {
8930
+ 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) {
8990
8931
  this.mcpRegistry = new MCPRegistry(this.config.mcpServers || []);
8991
8932
  this.multiAgentCoordinator = new MultiAgentCoordinator({
8992
8933
  llmProvider: this.llmProvider,
@@ -9004,8 +8945,8 @@ var Pipeline = class {
9004
8945
  this.initialised = true;
9005
8946
  }
9006
8947
  async loadColdContext(ns) {
9007
- var _a3, _b;
9008
- const cagConfig = (_a3 = this.config.rag) == null ? void 0 : _a3.cag;
8948
+ var _a2, _b;
8949
+ const cagConfig = (_a2 = this.config.rag) == null ? void 0 : _a2.cag;
9009
8950
  if (!cagConfig || !cagConfig.enabled) return;
9010
8951
  try {
9011
8952
  const coldNs = cagConfig.coldNamespace || ns;
@@ -9058,12 +8999,12 @@ ${m.content}`).join("\n\n---\n\n");
9058
8999
  }
9059
9000
  /** Step 1: Chunk the document content. */
9060
9001
  async prepareChunks(doc) {
9061
- var _a3, _b;
9002
+ var _a2, _b;
9062
9003
  if (this.llamaIngestor) {
9063
9004
  return this.llamaIngestor.chunk(doc.content, {
9064
9005
  docId: doc.docId,
9065
9006
  metadata: doc.metadata,
9066
- chunkSize: (_a3 = this.config.rag) == null ? void 0 : _a3.chunkSize,
9007
+ chunkSize: (_a2 = this.config.rag) == null ? void 0 : _a2.chunkSize,
9067
9008
  chunkOverlap: (_b = this.config.rag) == null ? void 0 : _b.chunkOverlap
9068
9009
  });
9069
9010
  }
@@ -9161,9 +9102,9 @@ ${m.content}`).join("\n\n---\n\n");
9161
9102
  return { reply, sources };
9162
9103
  }
9163
9104
  async ask(question, history = [], namespace) {
9164
- var _a3, _b;
9105
+ var _a2, _b;
9165
9106
  await this.initialize();
9166
- 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) {
9107
+ 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) {
9167
9108
  return await this.multiAgentCoordinator.run(question, history);
9168
9109
  }
9169
9110
  const stream = this.askStream(question, history, namespace);
@@ -9198,9 +9139,9 @@ ${m.content}`).join("\n\n---\n\n");
9198
9139
  }
9199
9140
  askStream(_0) {
9200
9141
  return __asyncGenerator(this, arguments, function* (question, history = [], namespace) {
9201
- var _a3, _b;
9142
+ var _a2, _b;
9202
9143
  yield new __await(this.initialize());
9203
- 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) {
9144
+ 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) {
9204
9145
  const stream2 = this.multiAgentCoordinator.runStream(question, history);
9205
9146
  try {
9206
9147
  for (var iter = __forAwait(stream2), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
@@ -9249,10 +9190,10 @@ ${m.content}`).join("\n\n---\n\n");
9249
9190
  */
9250
9191
  askStreamInternal(_0) {
9251
9192
  return __asyncGenerator(this, arguments, function* (question, history = [], namespace) {
9252
- 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;
9193
+ 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;
9253
9194
  yield new __await(this.initialize());
9254
9195
  const ns = formatNamespace(namespace != null ? namespace : this.config.projectId);
9255
- const topK = (_b = (_a3 = this.config.rag) == null ? void 0 : _a3.topK) != null ? _b : 5;
9196
+ const topK = (_b = (_a2 = this.config.rag) == null ? void 0 : _a2.topK) != null ? _b : 5;
9256
9197
  const scoreThreshold = (_d = (_c = this.config.rag) == null ? void 0 : _c.scoreThreshold) != null ? _d : 0.3;
9257
9198
  const requestId = `req_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
9258
9199
  const requestStart = performance.now();
@@ -9301,8 +9242,8 @@ ${m.content}`).join("\n\n---\n\n");
9301
9242
  const rerankStart = performance.now();
9302
9243
  const structuredSources = this.applyStructuredFilters(rawSources, filter).filter((s) => Boolean(s && typeof s === "object"));
9303
9244
  let fullSources = hasNumericPredicates ? structuredSources : structuredSources.filter((m) => {
9304
- var _a4;
9305
- return ((_a4 = m == null ? void 0 : m.score) != null ? _a4 : 0) >= scoreThreshold;
9245
+ var _a3;
9246
+ return ((_a3 = m == null ? void 0 : m.score) != null ? _a3 : 0) >= scoreThreshold;
9306
9247
  });
9307
9248
  const rerankLimit = Math.max(retrievalLimit, fullSources.length);
9308
9249
  const useReranking = arch !== "naive" && (arch === "retrieve-and-rerank" || ((_m = this.config.rag) == null ? void 0 : _m.useReranking));
@@ -9315,13 +9256,13 @@ ${m.content}`).join("\n\n---\n\n");
9315
9256
  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]
9316
9257
 
9317
9258
  ` + promptSources.map((m, i) => {
9318
- var _a4;
9259
+ var _a3;
9319
9260
  return `[Source ${i + 1}]
9320
- ${(_a4 = m == null ? void 0 : m.content) != null ? _a4 : ""}`;
9261
+ ${(_a3 = m == null ? void 0 : m.content) != null ? _a3 : ""}`;
9321
9262
  }).join("\n\n---\n\n") : "No relevant context found.";
9322
9263
  const sources = [...fullSources].filter((s) => Boolean(s && typeof s === "object")).sort((a, b) => {
9323
- var _a4, _b2;
9324
- return ((_a4 = b == null ? void 0 : b.score) != null ? _a4 : 0) - ((_b2 = a == null ? void 0 : a.score) != null ? _b2 : 0);
9264
+ var _a3, _b2;
9265
+ return ((_a3 = b == null ? void 0 : b.score) != null ? _a3 : 0) - ((_b2 = a == null ? void 0 : a.score) != null ? _b2 : 0);
9325
9266
  });
9326
9267
  if (graphData && graphData.nodes.length > 0) {
9327
9268
  const graphContext = graphData.nodes.map(
@@ -9349,10 +9290,10 @@ ${context}`;
9349
9290
  yield {
9350
9291
  reply: "",
9351
9292
  sources: (sources || []).filter((s) => Boolean(s && typeof s === "object")).map((s) => {
9352
- var _a4, _b2, _c2;
9293
+ var _a3, _b2, _c2;
9353
9294
  return {
9354
9295
  id: s.id,
9355
- score: (_a4 = s.score) != null ? _a4 : 0,
9296
+ score: (_a3 = s.score) != null ? _a3 : 0,
9356
9297
  content: (_b2 = s == null ? void 0 : s.content) != null ? _b2 : "",
9357
9298
  metadata: (_c2 = s == null ? void 0 : s.metadata) != null ? _c2 : {},
9358
9299
  namespace: ns
@@ -9545,11 +9486,11 @@ ${context}`;
9545
9486
  systemPrompt,
9546
9487
  userPrompt: question + finalRestrictionSuffix,
9547
9488
  chunks: (sources || []).filter(Boolean).map((s) => {
9548
- var _a4, _b2;
9489
+ var _a3, _b2;
9549
9490
  return {
9550
9491
  id: s.id,
9551
9492
  score: s.score,
9552
- content: (_a4 = s == null ? void 0 : s.content) != null ? _a4 : "",
9493
+ content: (_a3 = s == null ? void 0 : s.content) != null ? _a3 : "",
9553
9494
  metadata: (_b2 = s == null ? void 0 : s.metadata) != null ? _b2 : {},
9554
9495
  namespace: ns
9555
9496
  };
@@ -9568,7 +9509,7 @@ ${context}`;
9568
9509
  const telemetryUrl = ((_B = this.config.telemetry) == null ? void 0 : _B.url) || process.env.TELEMETRY_URL || process.env.NEXT_PUBLIC_TELEMETRY_URL || defaultUrl;
9569
9510
  const absoluteUrl = telemetryUrl.startsWith("http") ? telemetryUrl : "https://www.retrivora.com" + (telemetryUrl.startsWith("/") ? telemetryUrl : "/" + telemetryUrl);
9570
9511
  (async () => {
9571
- var _a4, _b2, _c2, _d2, _e2;
9512
+ var _a3, _b2, _c2, _d2, _e2;
9572
9513
  try {
9573
9514
  let finalTrace = trace;
9574
9515
  if (!awaitHallucination && runHallucination) {
@@ -9577,7 +9518,7 @@ ${context}`;
9577
9518
  finalTrace = buildTrace(backgroundScoreResult);
9578
9519
  }
9579
9520
  }
9580
- const modelName = (finalTrace == null ? void 0 : finalTrace.model) || ((_a4 = this.config.llm) == null ? void 0 : _a4.model) || "llama-3.1-8b-instant";
9521
+ const modelName = (finalTrace == null ? void 0 : finalTrace.model) || ((_a3 = this.config.llm) == null ? void 0 : _a3.model) || "llama-3.1-8b-instant";
9581
9522
  const providerName = (finalTrace == null ? void 0 : finalTrace.provider) || ((_b2 = this.config.llm) == null ? void 0 : _b2.provider) || "groq";
9582
9523
  const tokenCount = Number(((_c2 = finalTrace == null ? void 0 : finalTrace.tokens) == null ? void 0 : _c2.totalTokens) || 0);
9583
9524
  const costEst = Number(((_d2 = finalTrace == null ? void 0 : finalTrace.tokens) == null ? void 0 : _d2.estimatedCostUsd) || 0);
@@ -9631,7 +9572,7 @@ ${context}`;
9631
9572
  * Uses an LRU-bounded embedding cache to avoid re-embedding the same query.
9632
9573
  */
9633
9574
  async generateUiTransformation(question, sources, cachedSchema, forceDeterministic = false) {
9634
- var _a3;
9575
+ var _a2;
9635
9576
  if (!sources || sources.length === 0) {
9636
9577
  return UITransformer.transform(question, sources, this.config, cachedSchema);
9637
9578
  }
@@ -9644,7 +9585,7 @@ ${context}`;
9644
9585
  { visualizationHint: "product_browse", recommendedChart: "text", filterInStockOnly: false, wantsExplicitTable: false, isTemporal: false, isComparison: false, language: "en" }
9645
9586
  );
9646
9587
  }
9647
- const enableLlmUiTransform = ((_a3 = this.config.llm.options) == null ? void 0 : _a3.enableLlmUiTransform) === true;
9588
+ const enableLlmUiTransform = ((_a2 = this.config.llm.options) == null ? void 0 : _a2.enableLlmUiTransform) === true;
9648
9589
  if (forceDeterministic || !enableLlmUiTransform) {
9649
9590
  return UITransformer.transform(question, sources, this.config, cachedSchema);
9650
9591
  }
@@ -9662,15 +9603,15 @@ ${context}`;
9662
9603
  const value = this.resolveNumericPredicateValue(source, predicate);
9663
9604
  return value !== null && this.matchesNumericPredicate(value, predicate);
9664
9605
  })).sort((a, b) => {
9665
- var _a3, _b;
9606
+ var _a2, _b;
9666
9607
  const primary = predicates[0];
9667
- const aValue = (_a3 = this.resolveNumericPredicateValue(a, primary)) != null ? _a3 : 0;
9608
+ const aValue = (_a2 = this.resolveNumericPredicateValue(a, primary)) != null ? _a2 : 0;
9668
9609
  const bValue = (_b = this.resolveNumericPredicateValue(b, primary)) != null ? _b : 0;
9669
9610
  return primary.operator === "lt" || primary.operator === "lte" ? aValue - bValue : bValue - aValue;
9670
9611
  });
9671
9612
  }
9672
9613
  resolveNumericPredicateValue(source, predicate) {
9673
- var _a3;
9614
+ var _a2;
9674
9615
  if (!source) return null;
9675
9616
  const meta = source.metadata || {};
9676
9617
  const field = predicate.field;
@@ -9683,7 +9624,7 @@ ${context}`;
9683
9624
  const value = this.toFiniteNumber(Array.isArray(fuzzy) ? fuzzy[1] : fuzzy.value);
9684
9625
  if (value !== null) return value;
9685
9626
  }
9686
- const contentValue = this.extractNumericValueFromContent((_a3 = source.content) != null ? _a3 : "", field);
9627
+ const contentValue = this.extractNumericValueFromContent((_a2 = source.content) != null ? _a2 : "", field);
9687
9628
  if (contentValue !== null) return contentValue;
9688
9629
  }
9689
9630
  for (const [key, value] of entries) {
@@ -9739,8 +9680,8 @@ ${context}`;
9739
9680
  return Number.isFinite(numeric) ? numeric : null;
9740
9681
  }
9741
9682
  async retrieve(query, options) {
9742
- var _a3, _b, _c, _d, _e, _f, _g2;
9743
- const ns = formatNamespace((_a3 = options.namespace) != null ? _a3 : this.config.projectId);
9683
+ var _a2, _b, _c, _d, _e, _f, _g2;
9684
+ const ns = formatNamespace((_a2 = options.namespace) != null ? _a2 : this.config.projectId);
9744
9685
  const topK = (_b = options.topK) != null ? _b : 5;
9745
9686
  const cacheKey = `${ns}::${query}`;
9746
9687
  let queryVector = this.embeddingCache.get(cacheKey);
@@ -9785,11 +9726,11 @@ ${context}`;
9785
9726
  namespace: ns,
9786
9727
  count: resolvedSources.length,
9787
9728
  sample: resolvedSources.slice(0, 2).map((s) => {
9788
- var _a4;
9729
+ var _a3;
9789
9730
  return {
9790
9731
  id: s == null ? void 0 : s.id,
9791
9732
  score: s == null ? void 0 : s.score,
9792
- contentSnippet: String((_a4 = s == null ? void 0 : s.content) != null ? _a4 : "").substring(0, 150),
9733
+ contentSnippet: String((_a3 = s == null ? void 0 : s.content) != null ? _a3 : "").substring(0, 150),
9793
9734
  metadata: s == null ? void 0 : s.metadata
9794
9735
  };
9795
9736
  })
@@ -9803,8 +9744,8 @@ Focus on extracting the core intent and entities. Do not answer the question, ju
9803
9744
 
9804
9745
  History:
9805
9746
  ${(history || []).map((m) => {
9806
- var _a3;
9807
- return `${(m == null ? void 0 : m.role) || "user"}: ${(_a3 = m == null ? void 0 : m.content) != null ? _a3 : ""}`;
9747
+ var _a2;
9748
+ return `${(m == null ? void 0 : m.role) || "user"}: ${(_a2 = m == null ? void 0 : m.content) != null ? _a2 : ""}`;
9808
9749
  }).join("\n")}
9809
9750
 
9810
9751
  New Question: ${question}
@@ -9831,8 +9772,8 @@ Optimized Search Query:`;
9831
9772
  const { sources } = await this.retrieve(query, { namespace: ns, topK: 5 });
9832
9773
  if (!sources || sources.length === 0) return [];
9833
9774
  const context = sources.map((s) => {
9834
- var _a3;
9835
- return (_a3 = s == null ? void 0 : s.content) != null ? _a3 : "";
9775
+ var _a2;
9776
+ return (_a2 = s == null ? void 0 : s.content) != null ? _a2 : "";
9836
9777
  }).join("\n\n---\n\n");
9837
9778
  const prompt = `Based on the following snippets from a document, what are at least 5 short, relevant questions a user might ask?
9838
9779
  Focus on questions that can be answered by the context.
@@ -9971,186 +9912,8 @@ var ProviderHealthCheck = class {
9971
9912
  };
9972
9913
 
9973
9914
  // src/core/FreeTierLimitsGuard.ts
9974
- var FREE_TIER_QUOTAS = {
9975
- MAX_DOCUMENTS: 25,
9976
- MAX_FILE_SIZE_BYTES: 20 * 1024 * 1024,
9977
- MAX_STORAGE_BYTES: 250 * 1024 * 1024,
9978
- MAX_TRIAL_REQUEST_UNITS: 500,
9979
- MAX_DAILY_REQUEST_UNITS: 50,
9980
- TRIAL_DURATION_DAYS: 14,
9981
- GRACE_PERIOD_DAYS: 2,
9982
- MAX_RPM: 10,
9983
- MAX_RPH: 100,
9984
- MAX_RPD: 500,
9985
- MAX_CONCURRENT_REQUESTS: 2,
9986
- MAX_INPUT_TOKENS: 2e4,
9987
- MAX_OUTPUT_TOKENS: 4e3,
9988
- UPGRADE_REMINDER_DAYS: [7, 12, 14],
9989
- MAX_USERS: 1,
9990
- MAX_NAMESPACES: 1,
9991
- MAX_PROJECTS: 1
9992
- };
9993
- var FreeTierLimitsGuard = class {
9994
- static calculateTrialTimestamps(startedAt) {
9995
- const startDate = startedAt ? new Date(startedAt) : /* @__PURE__ */ new Date();
9996
- const trialStartedAtMs = startDate.getTime();
9997
- const trialExpiresAtMs = trialStartedAtMs + FREE_TIER_QUOTAS.TRIAL_DURATION_DAYS * 24 * 60 * 60 * 1e3;
9998
- const gracePeriodExpiresAtMs = trialStartedAtMs + (FREE_TIER_QUOTAS.TRIAL_DURATION_DAYS + FREE_TIER_QUOTAS.GRACE_PERIOD_DAYS) * 24 * 60 * 60 * 1e3;
9999
- return {
10000
- trial_started_at: new Date(trialStartedAtMs).toISOString(),
10001
- trial_expires_at: new Date(trialExpiresAtMs).toISOString(),
10002
- grace_period_expires_at: new Date(gracePeriodExpiresAtMs).toISOString()
10003
- };
10004
- }
10005
- static calculateRequestUnits(operationType, options) {
10006
- var _a3;
10007
- const op = operationType.toLowerCase().trim();
10008
- 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")) {
10009
- return 0;
10010
- }
10011
- if (op.includes("image")) {
10012
- return 10;
10013
- }
10014
- if (op.includes("embedding")) {
10015
- const chunks = (_a3 = options == null ? void 0 : options.chunkCount) != null ? _a3 : 1;
10016
- return Math.max(1, Math.ceil(chunks / 1e3));
10017
- }
10018
- return 1;
10019
- }
10020
- static checkTrialStatus(startedAt, totalUnitsUsed = 0, nowServerTime = /* @__PURE__ */ new Date()) {
10021
- const timestamps = this.calculateTrialTimestamps(startedAt);
10022
- const startMs = new Date(timestamps.trial_started_at).getTime();
10023
- const expireMs = new Date(timestamps.trial_expires_at).getTime();
10024
- const graceExpireMs = new Date(timestamps.grace_period_expires_at).getTime();
10025
- const nowMs = nowServerTime.getTime();
10026
- const daysElapsed = Math.floor(Math.max(0, nowMs - startMs) / (1e3 * 60 * 60 * 24));
10027
- const daysRemaining = Math.max(0, Math.ceil((expireMs - nowMs) / (1e3 * 60 * 60 * 24)));
10028
- const isConsumedUnits = totalUnitsUsed >= FREE_TIER_QUOTAS.MAX_TRIAL_REQUEST_UNITS;
10029
- const isExpiredTime = nowMs > expireMs;
10030
- const isPastGraceTime = nowMs > graceExpireMs;
10031
- let status = "active";
10032
- let reason;
10033
- if (isPastGraceTime || isConsumedUnits && isExpiredTime) {
10034
- status = "expired";
10035
- reason = "Trial period and grace period have expired. Upgrade your plan.";
10036
- } else if (isExpiredTime || isConsumedUnits) {
10037
- status = "grace_period";
10038
- 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).";
10039
- }
10040
- let upgradeReminderDue;
10041
- if (FREE_TIER_QUOTAS.UPGRADE_REMINDER_DAYS.includes(daysElapsed)) {
10042
- upgradeReminderDue = daysElapsed;
10043
- }
10044
- let usageReminderDue;
10045
- const usagePercent = totalUnitsUsed / FREE_TIER_QUOTAS.MAX_TRIAL_REQUEST_UNITS * 100;
10046
- if (usagePercent >= 100) {
10047
- usageReminderDue = "100%";
10048
- } else if (usagePercent >= 90) {
10049
- usageReminderDue = "90%";
10050
- } else if (usagePercent >= 80) {
10051
- usageReminderDue = "80%";
10052
- }
10053
- return {
10054
- status,
10055
- daysElapsed,
10056
- daysRemaining,
10057
- isGracePeriod: status === "grace_period",
10058
- isExpired: status === "expired",
10059
- upgradeReminderDue,
10060
- usageReminderDue,
10061
- reason
10062
- };
10063
- }
10064
- static checkIngestionAllowed(stats, incomingSizeBytes = 0) {
10065
- var _a3, _b;
10066
- const docs = (_a3 = stats.documentCount) != null ? _a3 : 0;
10067
- const storage = (_b = stats.totalStorageBytes) != null ? _b : 0;
10068
- if (incomingSizeBytes > FREE_TIER_QUOTAS.MAX_FILE_SIZE_BYTES) {
10069
- const mbSize = (incomingSizeBytes / (1024 * 1024)).toFixed(1);
10070
- return {
10071
- allowed: false,
10072
- reason: `[FreeTierIngestor] File size (${mbSize}MB) exceeds maximum allowed size of 20MB under Free Tier rules.`
10073
- };
10074
- }
10075
- if (docs >= FREE_TIER_QUOTAS.MAX_DOCUMENTS) {
10076
- return {
10077
- allowed: false,
10078
- reason: `Document limit reached (${docs}/${FREE_TIER_QUOTAS.MAX_DOCUMENTS}). Upgrade to Pro to ingest more documents.`
10079
- };
10080
- }
10081
- if (storage + incomingSizeBytes > FREE_TIER_QUOTAS.MAX_STORAGE_BYTES) {
10082
- const mbUsed = (storage / (1024 * 1024)).toFixed(1);
10083
- return {
10084
- allowed: false,
10085
- reason: `Storage quota exceeded (${mbUsed}MB / 250MB). Upgrade for unlimited storage.`
10086
- };
10087
- }
10088
- return { allowed: true };
10089
- }
10090
- static checkRequestAllowed(params) {
10091
- var _a3, _b, _c;
10092
- const op = (_a3 = params.operationType) != null ? _a3 : "chat";
10093
- const costUnits = this.calculateRequestUnits(op, { chunkCount: params.chunkCount });
10094
- if (costUnits === 0) {
10095
- return { allowed: true, costUnits: 0 };
10096
- }
10097
- if (params.inputTokens && params.inputTokens > FREE_TIER_QUOTAS.MAX_INPUT_TOKENS) {
10098
- return {
10099
- allowed: false,
10100
- reason: "Token limit exceeded. Upgrade your plan.",
10101
- costUnits
10102
- };
10103
- }
10104
- if (params.outputTokens && params.outputTokens > FREE_TIER_QUOTAS.MAX_OUTPUT_TOKENS) {
10105
- return {
10106
- allowed: false,
10107
- reason: "Token limit exceeded. Upgrade your plan.",
10108
- costUnits
10109
- };
10110
- }
10111
- if (params.concurrentRequests && params.concurrentRequests > FREE_TIER_QUOTAS.MAX_CONCURRENT_REQUESTS) {
10112
- return {
10113
- allowed: false,
10114
- reason: `Concurrent limit exceeded (max ${FREE_TIER_QUOTAS.MAX_CONCURRENT_REQUESTS} active requests). Please wait for active streams to finish.`,
10115
- costUnits
10116
- };
10117
- }
10118
- const totalUnits = (_b = params.totalUnitsUsed) != null ? _b : 0;
10119
- const trialStatus = this.checkTrialStatus(params.trialStartedAt, totalUnits, params.nowServerTime);
10120
- if (trialStatus.status === "expired" || trialStatus.status === "grace_period") {
10121
- return {
10122
- allowed: false,
10123
- reason: trialStatus.reason || "Trial limit reached. Upgrade your plan.",
10124
- costUnits
10125
- };
10126
- }
10127
- if (totalUnits + costUnits > FREE_TIER_QUOTAS.MAX_TRIAL_REQUEST_UNITS) {
10128
- return {
10129
- allowed: false,
10130
- reason: `Total trial request unit limit reached (${totalUnits}/${FREE_TIER_QUOTAS.MAX_TRIAL_REQUEST_UNITS}). Upgrade your plan.`,
10131
- costUnits
10132
- };
10133
- }
10134
- const dailyUnits = (_c = params.dailyUnitsUsed) != null ? _c : 0;
10135
- if (dailyUnits + costUnits > FREE_TIER_QUOTAS.MAX_DAILY_REQUEST_UNITS) {
10136
- return {
10137
- allowed: false,
10138
- reason: `Daily request quota limit reached (${dailyUnits}/${FREE_TIER_QUOTAS.MAX_DAILY_REQUEST_UNITS} units/day). Quota resets tomorrow.`,
10139
- costUnits
10140
- };
10141
- }
10142
- return { allowed: true, costUnits };
10143
- }
10144
- static checkQueryAllowed(dailyQueriesCount = 0) {
10145
- if (dailyQueriesCount >= FREE_TIER_QUOTAS.MAX_DAILY_REQUEST_UNITS) {
10146
- return {
10147
- allowed: false,
10148
- reason: `Daily query limit reached (${FREE_TIER_QUOTAS.MAX_DAILY_REQUEST_UNITS}/day). Quota resets tomorrow.`
10149
- };
10150
- }
10151
- return { allowed: true };
10152
- }
10153
- };
9915
+ var FreeTierLimitsGuard_exports = {};
9916
+ __reExport(FreeTierLimitsGuard_exports, require("@retrivora-ai/billing-guards"));
10154
9917
 
10155
9918
  // src/core/VectorPlugin.ts
10156
9919
  var VectorPlugin = class {
@@ -10158,12 +9921,12 @@ var VectorPlugin = class {
10158
9921
  const resolvedConfig = ConfigResolver.resolve(hostConfig);
10159
9922
  this.config = resolvedConfig;
10160
9923
  this.validationPromise = (async () => {
10161
- var _a3;
9924
+ var _a2;
10162
9925
  await ConfigValidator.validateAndThrow(resolvedConfig);
10163
9926
  await LicenseVerifier.verifyAsync(
10164
9927
  resolvedConfig.licenseKey,
10165
9928
  resolvedConfig.projectId,
10166
- (_a3 = resolvedConfig.vectorDb) == null ? void 0 : _a3.provider
9929
+ (_a2 = resolvedConfig.vectorDb) == null ? void 0 : _a2.provider
10167
9930
  );
10168
9931
  })();
10169
9932
  this.validationPromise.catch(() => {
@@ -10199,8 +9962,8 @@ var VectorPlugin = class {
10199
9962
  }
10200
9963
  estimateInputTokens(message, history = []) {
10201
9964
  const allContent = message.length + history.reduce((acc, m) => {
10202
- var _a3, _b;
10203
- 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);
9965
+ var _a2, _b;
9966
+ 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);
10204
9967
  }, 0);
10205
9968
  return Math.ceil(allContent / 4);
10206
9969
  }
@@ -10208,7 +9971,7 @@ var VectorPlugin = class {
10208
9971
  * Run a chat query.
10209
9972
  */
10210
9973
  async chat(message, history = [], namespace) {
10211
- var _a3;
9974
+ var _a2;
10212
9975
  try {
10213
9976
  await this.validationPromise;
10214
9977
  } catch (err) {
@@ -10216,11 +9979,11 @@ var VectorPlugin = class {
10216
9979
  }
10217
9980
  try {
10218
9981
  const inputTokens = this.estimateInputTokens(message, history);
10219
- const tokenCheck = FreeTierLimitsGuard.checkRequestAllowed({
9982
+ const tokenCheck = FreeTierLimitsGuard_exports.FreeTierLimitsGuard.checkRequestAllowed({
10220
9983
  operationType: "chat",
10221
9984
  inputTokens
10222
9985
  });
10223
- if (!tokenCheck.allowed && ((_a3 = tokenCheck.reason) == null ? void 0 : _a3.toLowerCase().includes("token"))) {
9986
+ if (!tokenCheck.allowed && ((_a2 = tokenCheck.reason) == null ? void 0 : _a2.toLowerCase().includes("token"))) {
10224
9987
  throw wrapError(new Error(tokenCheck.reason), "RATE_LIMITED");
10225
9988
  }
10226
9989
  return await this.pipeline.ask(message, history, namespace);
@@ -10241,7 +10004,7 @@ var VectorPlugin = class {
10241
10004
  */
10242
10005
  chatStream(_0) {
10243
10006
  return __asyncGenerator(this, arguments, function* (message, history = [], namespace) {
10244
- var _a3;
10007
+ var _a2;
10245
10008
  try {
10246
10009
  yield new __await(this.validationPromise);
10247
10010
  } catch (err) {
@@ -10249,11 +10012,11 @@ var VectorPlugin = class {
10249
10012
  }
10250
10013
  try {
10251
10014
  const inputTokens = this.estimateInputTokens(message, history);
10252
- const tokenCheck = FreeTierLimitsGuard.checkRequestAllowed({
10015
+ const tokenCheck = FreeTierLimitsGuard_exports.FreeTierLimitsGuard.checkRequestAllowed({
10253
10016
  operationType: "chat_stream",
10254
10017
  inputTokens
10255
10018
  });
10256
- if (!tokenCheck.allowed && ((_a3 = tokenCheck.reason) == null ? void 0 : _a3.toLowerCase().includes("token"))) {
10019
+ if (!tokenCheck.allowed && ((_a2 = tokenCheck.reason) == null ? void 0 : _a2.toLowerCase().includes("token"))) {
10257
10020
  throw wrapError(new Error(tokenCheck.reason), "RATE_LIMITED");
10258
10021
  }
10259
10022
  const stream = this.pipeline.askStream(message, history, namespace);
@@ -10328,8 +10091,8 @@ var DocumentParser = class {
10328
10091
  * Extract text from a File or Buffer based on its type.
10329
10092
  */
10330
10093
  static async parse(file, fileName, mimeType) {
10331
- var _a3;
10332
- const extension = ((_a3 = fileName.split(".").pop()) == null ? void 0 : _a3.toLowerCase()) || "";
10094
+ var _a2;
10095
+ const extension = ((_a2 = fileName.split(".").pop()) == null ? void 0 : _a2.toLowerCase()) || "";
10333
10096
  if (extension === "txt" || extension === "md" || mimeType === "text/plain" || mimeType === "text/markdown") {
10334
10097
  return this.readAsText(file);
10335
10098
  }
@@ -10408,7 +10171,17 @@ ${csv.trim()}`);
10408
10171
  var fs = __toESM(require("fs"));
10409
10172
  var path = __toESM(require("path"));
10410
10173
  var os = __toESM(require("os"));
10411
- var DatabaseStorage = class {
10174
+ function validateTableName(name) {
10175
+ if (!/^[a-zA-Z_][a-zA-Z0-9_]{0,62}$/.test(name)) {
10176
+ throw new Error(
10177
+ `[DatabaseStorage] Invalid table name "${name}". Table names must start with a letter or underscore and contain only alphanumeric characters and underscores (max 63 chars).`
10178
+ );
10179
+ }
10180
+ return name;
10181
+ }
10182
+ var _pgInitPromises = /* @__PURE__ */ new Map();
10183
+ var _DatabaseStorage = class _DatabaseStorage {
10184
+ // 5 minutes
10412
10185
  constructor(config) {
10413
10186
  // Dynamic PG Pool
10414
10187
  this.pgPool = null;
@@ -10420,36 +10193,50 @@ var DatabaseStorage = class {
10420
10193
  this.fallbackDir = this.isServerless ? path.join(os.tmpdir(), ".retrivora") : path.join(process.cwd(), ".retrivora");
10421
10194
  this.historyFile = path.join(this.fallbackDir, "history.json");
10422
10195
  this.feedbackFile = path.join(this.fallbackDir, "feedback.json");
10423
- var _a3, _b, _c, _d, _e;
10196
+ /** Subscription check result cache (Fix PERF-1). */
10197
+ this.subscriptionCheckedAt = 0;
10198
+ var _a2, _b, _c, _d, _e;
10424
10199
  this.config = config;
10425
- this.provider = ((_a3 = config.vectorDb) == null ? void 0 : _a3.provider) || "fs";
10200
+ this.provider = ((_a2 = config.vectorDb) == null ? void 0 : _a2.provider) || "fs";
10426
10201
  const rawHistoryTable = ((_c = (_b = config.rag) == null ? void 0 : _b.history) == null ? void 0 : _c.tableName) || "retrivora_history";
10427
10202
  const rawFeedbackTable = ((_e = (_d = config.rag) == null ? void 0 : _d.feedback) == null ? void 0 : _e.tableName) || "retrivora_feedback";
10428
- this.historyTableName = rawHistoryTable.replace(/[^a-zA-Z0-9_]/g, "_");
10429
- this.feedbackTableName = rawFeedbackTable.replace(/[^a-zA-Z0-9_]/g, "_");
10203
+ this.historyTableName = validateTableName(rawHistoryTable);
10204
+ this.feedbackTableName = validateTableName(rawFeedbackTable);
10430
10205
  }
10431
10206
  /**
10432
- * Asserts the user is running a valid Premium/Enterprise license in production.
10433
- * In non-production (development / test) environments this is a complete no-op —
10434
- * all History and Feedback features are freely accessible for local development.
10207
+ * Checks that the caller holds a valid paid-tier license in production.
10208
+ *
10209
+ * Fix PERF-1: result is cached for 5 minutes so we don't call
10210
+ * LicenseVerifier.verifyAsync() on every DB operation (saveMessage,
10211
+ * getHistory, etc.).
10212
+ *
10213
+ * Fix PERF-2: renamed from `checkPremiumSubscription` to clarify intent.
10214
+ * The previous allowlist admitted all free tiers, defeating the gate.
10215
+ * The new allowlist only passes paid tiers. Free-tier access to history
10216
+ * and feedback is a product decision — update the constant below if the
10217
+ * policy changes.
10218
+ *
10219
+ * In non-production environments this is a complete no-op so local
10220
+ * development works without a license key.
10435
10221
  */
10436
- async checkPremiumSubscription() {
10437
- if (process.env.NODE_ENV !== "production") {
10438
- return;
10439
- }
10222
+ async checkPaidSubscription() {
10223
+ if (process.env.NODE_ENV !== "production") return;
10224
+ if (Date.now() - this.subscriptionCheckedAt < _DatabaseStorage.SUBSCRIPTION_CACHE_TTL_MS) return;
10440
10225
  if (!this.config.licenseKey) {
10441
10226
  throw new Error(
10442
- "[Retrivora SDK] Chat History and Feedback features require a Premium or Enterprise license key in production."
10227
+ "[Retrivora SDK] Chat History and Feedback features require a paid license key in production."
10443
10228
  );
10444
10229
  }
10445
10230
  try {
10446
10231
  const payload = await LicenseVerifier.verifyAsync(this.config.licenseKey, this.config.projectId);
10447
10232
  const tier = (payload.tier || "").toLowerCase();
10448
- if (tier !== "premium" && tier !== "enterprise" && tier !== "growth" && tier !== "pro" && tier !== "developer_pro" && tier !== "free_trial" && tier !== "free_tier" && tier !== "free" && tier !== "hobby") {
10233
+ const PAID_TIERS = /* @__PURE__ */ new Set(["premium", "enterprise", "growth", "pro", "developer_pro"]);
10234
+ if (!PAID_TIERS.has(tier)) {
10449
10235
  throw new Error(
10450
10236
  `[Retrivora SDK] Chat History and Feedback features are not available on the "${tier}" tier. Please upgrade to a Developer Pro or Enterprise plan.`
10451
10237
  );
10452
10238
  }
10239
+ this.subscriptionCheckedAt = Date.now();
10453
10240
  } catch (err) {
10454
10241
  throw new Error(
10455
10242
  `[Retrivora SDK] Subscription check failed: ${err instanceof Error ? err.message : String(err)}`
@@ -10457,34 +10244,45 @@ var DatabaseStorage = class {
10457
10244
  }
10458
10245
  }
10459
10246
  async checkHistoryEnabled() {
10460
- var _a3, _b;
10461
- await this.checkPremiumSubscription();
10462
- if (((_b = (_a3 = this.config.rag) == null ? void 0 : _a3.history) == null ? void 0 : _b.enabled) === false) {
10247
+ var _a2, _b;
10248
+ await this.checkPaidSubscription();
10249
+ if (((_b = (_a2 = this.config.rag) == null ? void 0 : _a2.history) == null ? void 0 : _b.enabled) === false) {
10463
10250
  throw new Error("[Retrivora SDK] Chat History is disabled in RagConfig.");
10464
10251
  }
10465
10252
  }
10466
10253
  async checkFeedbackEnabled() {
10467
- var _a3, _b;
10468
- await this.checkPremiumSubscription();
10469
- if (((_b = (_a3 = this.config.rag) == null ? void 0 : _a3.feedback) == null ? void 0 : _b.enabled) === false) {
10254
+ var _a2, _b;
10255
+ await this.checkPaidSubscription();
10256
+ if (((_b = (_a2 = this.config.rag) == null ? void 0 : _a2.feedback) == null ? void 0 : _b.enabled) === false) {
10470
10257
  throw new Error("[Retrivora SDK] User Feedback is disabled in RagConfig.");
10471
10258
  }
10472
10259
  }
10473
- async getPGPool() {
10474
- const globalKey = `__retrivora_pg_pool_${this.config.projectId}`;
10475
- const _g2 = global;
10476
- if (_g2[globalKey]) {
10477
- this.pgPool = _g2[globalKey];
10478
- return this.pgPool;
10260
+ /**
10261
+ * Returns (or lazily creates) the PostgreSQL connection pool for this project.
10262
+ *
10263
+ * Fix ERR-2: the previous implementation had a double-initialization race
10264
+ * condition — two concurrent requests would both find _g[globalKey] missing,
10265
+ * both create a Pool, and the second Pool would be orphaned, leaking
10266
+ * database connections. This version stores a single Promise in the module-
10267
+ * level _pgInitPromises map so all concurrent callers await the same
10268
+ * initialization without creating duplicate pools.
10269
+ */
10270
+ getPGPool() {
10271
+ const globalKey = `pg:${this.config.projectId}`;
10272
+ if (!_pgInitPromises.has(globalKey)) {
10273
+ _pgInitPromises.set(globalKey, this.createPGPool());
10479
10274
  }
10275
+ return _pgInitPromises.get(globalKey);
10276
+ }
10277
+ async createPGPool() {
10480
10278
  const opts = this.config.vectorDb.options;
10481
10279
  if (!opts.connectionString) {
10482
10280
  throw new Error("[DatabaseStorage] pg connectionString option is missing.");
10483
10281
  }
10484
10282
  const { Pool: Pool3 } = await import("pg");
10485
- this.pgPool = new Pool3({ connectionString: opts.connectionString });
10486
- _g2[globalKey] = this.pgPool;
10487
- const client = await this.pgPool.connect();
10283
+ const pool = new Pool3({ connectionString: opts.connectionString });
10284
+ this.pgPool = pool;
10285
+ const client = await pool.connect();
10488
10286
  try {
10489
10287
  await client.query(`
10490
10288
  CREATE TABLE IF NOT EXISTS ${this.historyTableName} (
@@ -10511,7 +10309,7 @@ var DatabaseStorage = class {
10511
10309
  } finally {
10512
10310
  client.release();
10513
10311
  }
10514
- return this.pgPool;
10312
+ return pool;
10515
10313
  }
10516
10314
  async getMongoClient() {
10517
10315
  const globalKey = `__retrivora_mongo_client_${this.config.projectId}`;
@@ -10691,17 +10489,21 @@ var DatabaseStorage = class {
10691
10489
  }
10692
10490
  }
10693
10491
  /**
10694
- * Return true when `sessionId` falls into the allowed `projectIds` scope.
10695
- * When `projectIds` is empty / undefined we allow the call (legacy behavior
10696
- * for non-multi-tenant consumers). Session ids typically begin with the
10697
- * project id they were created for, so we prefix-match as well as contains-match
10698
- * to catch sharded / suffix-style ids used by some integrations.
10492
+ * Returns true when `sessionId` is in scope for the given `projectIds`.
10493
+ *
10494
+ * Fix RAG-5: the previous implementation used `sessionId.includes(pid)` which
10495
+ * allows cross-tenant data leakage. If a project ID "abc" is a substring of
10496
+ * another tenant's session ID (e.g., "xyz-abc-user-123"), the check would
10497
+ * incorrectly return true. We now only allow exact match or strict delimiter-
10498
+ * separated prefix match to prevent this attack.
10699
10499
  */
10700
10500
  sessionInScope(sessionId, projectIds) {
10701
10501
  if (!projectIds || projectIds.length === 0) return true;
10702
- return projectIds.some(
10703
- (pid) => pid && (sessionId === pid || sessionId.startsWith(pid) || sessionId.includes(pid))
10704
- );
10502
+ return projectIds.some((pid) => {
10503
+ if (!pid) return false;
10504
+ if (sessionId === pid) return true;
10505
+ return sessionId.startsWith(`${pid}-`) || sessionId.startsWith(`${pid}_`) || sessionId.startsWith(`${pid}/`);
10506
+ });
10705
10507
  }
10706
10508
  async listSessions(projectIds) {
10707
10509
  await this.checkHistoryEnabled();
@@ -10850,9 +10652,12 @@ var DatabaseStorage = class {
10850
10652
  }
10851
10653
  }
10852
10654
  };
10655
+ _DatabaseStorage.SUBSCRIPTION_CACHE_TTL_MS = 5 * 60 * 1e3;
10656
+ var DatabaseStorage = _DatabaseStorage;
10853
10657
 
10854
10658
  // src/handlers/index.ts
10855
10659
  init_version();
10660
+ var import_crypto2 = require("crypto");
10856
10661
  async function checkAuth(req, onAuthorize) {
10857
10662
  if (!onAuthorize) return null;
10858
10663
  try {
@@ -10874,6 +10679,17 @@ var RateLimiter = class {
10874
10679
  this.hits = /* @__PURE__ */ new Map();
10875
10680
  this.windowMs = windowMs;
10876
10681
  this.max = max;
10682
+ this.pruneTimer = setInterval(() => this.prune(), windowMs);
10683
+ if (typeof this.pruneTimer === "object" && "unref" in this.pruneTimer) {
10684
+ this.pruneTimer.unref();
10685
+ }
10686
+ }
10687
+ /** Prune entries whose timestamps are all outside the window. */
10688
+ prune() {
10689
+ const cutoff = Date.now() - this.windowMs;
10690
+ for (const [k, timestamps] of this.hits.entries()) {
10691
+ if (timestamps.every((t) => t <= cutoff)) this.hits.delete(k);
10692
+ }
10877
10693
  }
10878
10694
  /** Returns true if the request should be allowed, false if rate-limited. */
10879
10695
  allow(key) {
@@ -10882,11 +10698,6 @@ var RateLimiter = class {
10882
10698
  const existing = (this.hits.get(key) || []).filter((t) => t > cutoff);
10883
10699
  existing.push(now);
10884
10700
  this.hits.set(key, existing);
10885
- if (this.hits.size > 5e3) {
10886
- for (const [k, timestamps] of this.hits.entries()) {
10887
- if (timestamps.every((t) => t <= cutoff)) this.hits.delete(k);
10888
- }
10889
- }
10890
10701
  return existing.length <= this.max;
10891
10702
  }
10892
10703
  retryAfterSec(key) {
@@ -10901,8 +10712,8 @@ var _g = global;
10901
10712
  var _a;
10902
10713
  var rateLimiter = (_a = _g.__retrivoraRateLimiter) != null ? _a : _g.__retrivoraRateLimiter = new RateLimiter(6e4, 30);
10903
10714
  function getRateLimitKey(req) {
10904
- var _a3, _b;
10905
- 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";
10715
+ var _a2, _b;
10716
+ 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";
10906
10717
  return `ip:${ip}`;
10907
10718
  }
10908
10719
  function checkRateLimit(req) {
@@ -10929,8 +10740,6 @@ function isFreeTier(tier) {
10929
10740
  if (!tier) return true;
10930
10741
  return FREE_TIER_NAMES.has(tier.toLowerCase().trim());
10931
10742
  }
10932
- var _a2;
10933
- var _freeTierGuardInstance = (_a2 = _g.__retrivoraFreeTierGuard) != null ? _a2 : _g.__retrivoraFreeTierGuard = new FreeTierLimitsGuard();
10934
10743
  function createPaymentRequiredResponse(reason, details) {
10935
10744
  const body = __spreadProps(__spreadValues({
10936
10745
  type: "https://retrivora.com/docs/errors/payment-required",
@@ -10941,10 +10750,10 @@ function createPaymentRequiredResponse(reason, details) {
10941
10750
  instance: void 0
10942
10751
  }, details != null ? details : {}), {
10943
10752
  quota: {
10944
- maxDailyRequestUnits: FREE_TIER_QUOTAS.MAX_DAILY_REQUEST_UNITS,
10945
- maxTrialRequestUnits: FREE_TIER_QUOTAS.MAX_TRIAL_REQUEST_UNITS,
10946
- maxDocuments: FREE_TIER_QUOTAS.MAX_DOCUMENTS,
10947
- maxStorageBytes: FREE_TIER_QUOTAS.MAX_STORAGE_BYTES,
10753
+ maxDailyRequestUnits: FreeTierLimitsGuard_exports.FREE_TIER_QUOTAS.MAX_DAILY_REQUEST_UNITS,
10754
+ maxTrialRequestUnits: FreeTierLimitsGuard_exports.FREE_TIER_QUOTAS.MAX_TRIAL_REQUEST_UNITS,
10755
+ maxDocuments: FreeTierLimitsGuard_exports.FREE_TIER_QUOTAS.MAX_DOCUMENTS,
10756
+ maxStorageBytes: FreeTierLimitsGuard_exports.FREE_TIER_QUOTAS.MAX_STORAGE_BYTES,
10948
10757
  upgradeUrl: "https://retrivora.com/pricing"
10949
10758
  }
10950
10759
  });
@@ -10956,42 +10765,17 @@ function createPaymentRequiredResponse(reason, details) {
10956
10765
  });
10957
10766
  }
10958
10767
  function resolveLicenseKey(req, config, bodyLicenseKey) {
10959
- var _a3;
10960
- 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 || "";
10961
- }
10962
- function extractProjectIdFromLicenseKey(licenseKey) {
10963
- try {
10964
- const sanitizedKey = licenseKey.trim().replace(/^["']|["']$/g, "").trim();
10965
- const rawToken = sanitizedKey.replace(/^rtv_/i, "").trim();
10966
- const parts = rawToken.split(".");
10967
- if (parts.length === 3) {
10968
- const decodedPayload = JSON.parse(Buffer.from(parts[1], "base64url").toString("utf8"));
10969
- return decodedPayload.projectId;
10970
- }
10971
- } catch (e) {
10972
- }
10973
- return void 0;
10768
+ return req.headers.get("x-license-key") || bodyLicenseKey || config.licenseKey || process.env.RETRIVORA_LICENSE_KEY || process.env.RAG_LICENSE_KEY || "";
10974
10769
  }
10975
10770
  async function enforceLicense(req, config, bodyLicenseKey) {
10976
10771
  const rawKey = resolveLicenseKey(req, config, bodyLicenseKey);
10977
10772
  const licenseKey = (rawKey || "").trim().replace(/^["']|["']$/g, "").trim();
10978
10773
  const url = new URL(req.url);
10979
- const queryProjectId = url.searchParams.get("projectId") || url.searchParams.get("project-id");
10980
- const headerProjectId = req.headers.get("x-project-id") || req.headers.get("X-Project-Id");
10981
- const jwtProjectId = extractProjectIdFromLicenseKey(licenseKey);
10982
- let bodyProjectId = "";
10983
- if (req.method === "POST") {
10984
- try {
10985
- const body = await req.clone().json();
10986
- bodyProjectId = (body == null ? void 0 : body.projectId) || "";
10987
- } catch (e) {
10988
- }
10989
- }
10990
- const projectId = queryProjectId || headerProjectId || bodyProjectId || jwtProjectId || config.projectId || "";
10774
+ 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 || "";
10991
10775
  try {
10992
10776
  let payload;
10993
10777
  try {
10994
- payload = await LicenseVerifier.verifyAsync(licenseKey, projectId);
10778
+ payload = await LicenseVerifier.verifyAsync(licenseKey, requestedProjectId);
10995
10779
  } catch (err) {
10996
10780
  return {
10997
10781
  ok: false,
@@ -11003,7 +10787,7 @@ async function enforceLicense(req, config, bodyLicenseKey) {
11003
10787
  }
11004
10788
  return { ok: true, payload };
11005
10789
  } catch (err) {
11006
- 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.";
10790
+ const message = err instanceof Error ? err.message : "Missing or invalid RETRIVORA_LICENSE_KEY. Set RETRIVORA_LICENSE_KEY in your environment or contact your administrator.";
11007
10791
  const body = {
11008
10792
  error: {
11009
10793
  code: "LICENSE_REVOKED",
@@ -11061,38 +10845,59 @@ function sanitizeInput(raw) {
11061
10845
  }
11062
10846
  return { ok: true, value: trimmed };
11063
10847
  }
10848
+ var _pluginCache = /* @__PURE__ */ new Map();
10849
+ var PLUGIN_CACHE_TTL_MS = 10 * 60 * 1e3;
10850
+ function configHash(config) {
10851
+ return (0, import_crypto2.createHash)("sha1").update(JSON.stringify(config != null ? config : {})).digest("hex").slice(0, 16);
10852
+ }
11064
10853
  function getOrCreatePlugin(configOrPlugin) {
11065
10854
  if (configOrPlugin instanceof VectorPlugin) return configOrPlugin;
11066
- const cacheKey = "__retrivoraPlugin_" + JSON.stringify(configOrPlugin != null ? configOrPlugin : {}).slice(0, 64);
11067
- if (!_g[cacheKey]) {
11068
- _g[cacheKey] = new VectorPlugin(configOrPlugin);
10855
+ const key = configHash(configOrPlugin);
10856
+ const cached = _pluginCache.get(key);
10857
+ if (cached && Date.now() - cached.createdAt < PLUGIN_CACHE_TTL_MS) return cached.plugin;
10858
+ const plugin = new VectorPlugin(configOrPlugin);
10859
+ _pluginCache.set(key, { plugin, createdAt: Date.now() });
10860
+ return plugin;
10861
+ }
10862
+ var TELEMETRY_ALLOWED_HOSTS = /* @__PURE__ */ new Set([
10863
+ "retrivora.com",
10864
+ "www.retrivora.com"
10865
+ ]);
10866
+ function sanitizeTelemetryUrl(raw) {
10867
+ if (process.env.NODE_ENV !== "production") return raw;
10868
+ try {
10869
+ const parsed = new URL(raw);
10870
+ const hostname = parsed.hostname.toLowerCase().replace(/^www\./, "");
10871
+ if (!TELEMETRY_ALLOWED_HOSTS.has(`www.${hostname}`) && !TELEMETRY_ALLOWED_HOSTS.has(hostname)) {
10872
+ console.warn(
10873
+ `[Retrivora SDK Telemetry] Blocked non-allowlisted telemetry URL: ${parsed.hostname}. Only retrivora.com endpoints are permitted in production.`
10874
+ );
10875
+ return null;
10876
+ }
10877
+ return parsed.toString();
10878
+ } catch (e) {
10879
+ return null;
11069
10880
  }
11070
- return _g[cacheKey];
11071
10881
  }
11072
10882
  function reportTelemetry(req, plugin, action, status, details, trace) {
11073
- var _a3, _b, _c, _d, _e, _f, _g2;
10883
+ var _a2, _b, _c, _d, _e, _f, _g2;
11074
10884
  try {
11075
10885
  const config = plugin.getConfig();
11076
- const licenseKey = config.licenseKey || process.env.RAG_LICENSE_KEY || process.env.RETRIVORA_LICENSE_KEY || process.env.NEXT_PUBLIC_RETRIVORA_LICENSE_KEY;
10886
+ const licenseKey = config.licenseKey || process.env.RETRIVORA_LICENSE_KEY || process.env.RAG_LICENSE_KEY;
11077
10887
  const telemetryConfig = config.telemetry;
11078
- const host = req.headers.get("host") || "localhost";
11079
10888
  const defaultUrl = "https://www.retrivora.com/api/telemetry";
11080
- const telemetryUrl = (telemetryConfig == null ? void 0 : telemetryConfig.url) || process.env.TELEMETRY_URL || process.env.NEXT_PUBLIC_TELEMETRY_URL || defaultUrl;
10889
+ const rawTelemetryUrl = (telemetryConfig == null ? void 0 : telemetryConfig.url) || process.env.TELEMETRY_URL || process.env.NEXT_PUBLIC_TELEMETRY_URL || defaultUrl;
10890
+ const telemetryUrl = sanitizeTelemetryUrl(rawTelemetryUrl);
10891
+ if (!telemetryUrl) return;
11081
10892
  const userAgent = req.headers.get("user-agent") || `Retrivora-SDK/${SDK_VERSION}`;
11082
- let absoluteUrl = telemetryUrl;
11083
- if (!telemetryUrl.startsWith("http")) {
11084
- const proto = req.headers.get("x-forwarded-proto") || "http";
11085
- absoluteUrl = `${proto}://${host}${telemetryUrl}`;
11086
- }
11087
10893
  const projectId = config.projectId || "default";
11088
- 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";
10894
+ 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";
11089
10895
  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";
11090
10896
  const tokens = Number(((_e = trace == null ? void 0 : trace.tokens) == null ? void 0 : _e.totalTokens) || (trace == null ? void 0 : trace.totalTokens) || 0);
11091
10897
  const costUsd = Number(((_f = trace == null ? void 0 : trace.tokens) == null ? void 0 : _f.estimatedCostUsd) || (trace == null ? void 0 : trace.costUsd) || 0);
11092
10898
  const latencyMs = Number(((_g2 = trace == null ? void 0 : trace.latency) == null ? void 0 : _g2.totalMs) || (trace == null ? void 0 : trace.latencyMs) || 0);
11093
10899
  const payload = {
11094
10900
  trace,
11095
- licenseKey,
11096
10901
  projectId,
11097
10902
  organization: process.env.RETRIVORA_ORGANIZATION || "default-org",
11098
10903
  sdkVersion: SDK_VERSION,
@@ -11109,14 +10914,13 @@ function reportTelemetry(req, plugin, action, status, details, trace) {
11109
10914
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
11110
10915
  details
11111
10916
  };
11112
- fetch(absoluteUrl, {
10917
+ fetch(telemetryUrl, {
11113
10918
  method: "POST",
11114
- headers: {
11115
- "Content-Type": "application/json",
11116
- "x-forwarded-for": req.headers.get("x-forwarded-for") || "",
11117
- "x-real-ip": req.headers.get("x-real-ip") || "",
10919
+ headers: __spreadProps(__spreadValues({
10920
+ "Content-Type": "application/json"
10921
+ }, licenseKey ? { "x-license-key": licenseKey } : {}), {
11118
10922
  "user-agent": userAgent
11119
- },
10923
+ }),
11120
10924
  body: JSON.stringify(payload)
11121
10925
  }).catch((err) => {
11122
10926
  console.warn("[Retrivora SDK Telemetry] Async report warning:", err.message);
@@ -11166,7 +10970,7 @@ function createChatHandler(configOrPlugin, options) {
11166
10970
  const storage = new DatabaseStorage(plugin.getConfig());
11167
10971
  const onAuthorize = options == null ? void 0 : options.onAuthorize;
11168
10972
  return async function POST(req, context) {
11169
- var _a3, _b, _c, _d, _e, _f;
10973
+ var _a2, _b, _c, _d, _e, _f;
11170
10974
  const authResult = await checkAuth(req, onAuthorize);
11171
10975
  if (authResult) return authResult;
11172
10976
  const rateLimited = checkRateLimit(req);
@@ -11179,18 +10983,17 @@ function createChatHandler(configOrPlugin, options) {
11179
10983
  if (!licenseCheck.ok) return licenseCheck.response;
11180
10984
  if (isFreeTier(licenseCheck.payload.tier)) {
11181
10985
  const estimatedInputTokens = Math.ceil(
11182
- ((((_a3 = bodyObj.message) == null ? void 0 : _a3.length) || 0) + (Array.isArray(bodyObj.messages) ? bodyObj.messages.reduce((a, m) => {
11183
- var _a4, _b2;
11184
- 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);
10986
+ ((((_a2 = bodyObj.message) == null ? void 0 : _a2.length) || 0) + (Array.isArray(bodyObj.messages) ? bodyObj.messages.reduce((a, m) => {
10987
+ var _a3, _b2;
10988
+ 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);
11185
10989
  }, 0) : 0) + (((_b = bodyObj.history) == null ? void 0 : _b.reduce((a, m) => {
11186
- var _a4, _b2;
11187
- 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);
10990
+ var _a3, _b2;
10991
+ 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);
11188
10992
  }, 0)) || 0)) / 4
11189
10993
  );
11190
- const chatCheck = FreeTierLimitsGuard.checkRequestAllowed({
10994
+ const chatCheck = FreeTierLimitsGuard_exports.FreeTierLimitsGuard.checkRequestAllowed({
11191
10995
  operationType: "chat",
11192
- inputTokens: estimatedInputTokens,
11193
- projectId: licenseCheck.payload.projectId
10996
+ inputTokens: estimatedInputTokens
11194
10997
  });
11195
10998
  if (!chatCheck.allowed) {
11196
10999
  return createPaymentRequiredResponse(
@@ -11249,7 +11052,7 @@ function createStreamHandler(configOrPlugin, options) {
11249
11052
  const storage = new DatabaseStorage(plugin.getConfig());
11250
11053
  const onAuthorize = options == null ? void 0 : options.onAuthorize;
11251
11054
  return async function POST(req, context) {
11252
- var _a3, _b, _c;
11055
+ var _a2, _b;
11253
11056
  const authResult = await checkAuth(req, onAuthorize);
11254
11057
  if (authResult) return authResult;
11255
11058
  const rateLimited = checkRateLimit(req);
@@ -11257,10 +11060,6 @@ function createStreamHandler(configOrPlugin, options) {
11257
11060
  const config = plugin.getConfig();
11258
11061
  let body;
11259
11062
  try {
11260
- if ((_a3 = req.headers.get("content-type")) == null ? void 0 : _a3.includes("multipart/form-data")) {
11261
- const uploader = createUploadHandler(plugin, { onAuthorize });
11262
- return uploader(req);
11263
- }
11264
11063
  body = await req.json();
11265
11064
  } catch (e) {
11266
11065
  return new Response(JSON.stringify({ error: { code: "INVALID_JSON", message: "Invalid JSON body" } }), {
@@ -11273,15 +11072,15 @@ function createStreamHandler(configOrPlugin, options) {
11273
11072
  if (!licenseCheck.ok) return licenseCheck.response;
11274
11073
  if (isFreeTier(licenseCheck.payload.tier)) {
11275
11074
  const estimatedInputTokens = Math.ceil(
11276
- ((((_b = bodyObj.message) == null ? void 0 : _b.length) || 0) + (Array.isArray(bodyObj.messages) ? bodyObj.messages.reduce((a, m) => {
11277
- var _a4, _b2;
11278
- 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);
11279
- }, 0) : 0) + (((_c = bodyObj.history) == null ? void 0 : _c.reduce((a, m) => {
11280
- var _a4, _b2;
11281
- 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);
11075
+ ((((_a2 = bodyObj.message) == null ? void 0 : _a2.length) || 0) + (Array.isArray(bodyObj.messages) ? bodyObj.messages.reduce((a, m) => {
11076
+ var _a3, _b2;
11077
+ 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);
11078
+ }, 0) : 0) + (((_b = bodyObj.history) == null ? void 0 : _b.reduce((a, m) => {
11079
+ var _a3, _b2;
11080
+ 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);
11282
11081
  }, 0)) || 0)) / 4
11283
11082
  );
11284
- const chatCheck = FreeTierLimitsGuard.checkRequestAllowed({
11083
+ const chatCheck = FreeTierLimitsGuard_exports.FreeTierLimitsGuard.checkRequestAllowed({
11285
11084
  operationType: "chat_stream",
11286
11085
  inputTokens: estimatedInputTokens,
11287
11086
  projectId: licenseCheck.payload.projectId
@@ -11324,7 +11123,7 @@ function createStreamHandler(configOrPlugin, options) {
11324
11123
  let isActive = true;
11325
11124
  const stream = new ReadableStream({
11326
11125
  async start(controller) {
11327
- var _a4, _b2, _c2, _d, _e;
11126
+ var _a3, _b2, _c, _d, _e;
11328
11127
  const enqueue = (text) => {
11329
11128
  if (!isActive) return;
11330
11129
  try {
@@ -11357,7 +11156,7 @@ function createStreamHandler(configOrPlugin, options) {
11357
11156
  let uiTransformation = responseChunk == null ? void 0 : responseChunk.ui_transformation;
11358
11157
  if (sources.length > 0) {
11359
11158
  try {
11360
- uiTransformation = (_a4 = responseChunk == null ? void 0 : responseChunk.ui_transformation) != null ? _a4 : UITransformer.transform(message, sources, plugin.getConfig());
11159
+ uiTransformation = (_a3 = responseChunk == null ? void 0 : responseChunk.ui_transformation) != null ? _a3 : UITransformer.transform(message, sources, plugin.getConfig());
11361
11160
  if (uiTransformation) {
11362
11161
  enqueue(sseUIFrame(uiTransformation));
11363
11162
  }
@@ -11381,7 +11180,7 @@ function createStreamHandler(configOrPlugin, options) {
11381
11180
  uiTransformation,
11382
11181
  trace: responseChunk == null ? void 0 : responseChunk.trace
11383
11182
  }).catch((err) => console.warn("[createStreamHandler] Failed to save assistant message:", err));
11384
- 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);
11183
+ 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);
11385
11184
  }
11386
11185
  }
11387
11186
  } catch (temp) {
@@ -11446,7 +11245,7 @@ function createIngestHandler(configOrPlugin, options) {
11446
11245
  (sum, d) => sum + Buffer.byteLength(d.content || "", "utf8"),
11447
11246
  0
11448
11247
  );
11449
- const ingestCheck = FreeTierLimitsGuard.checkIngestionAllowed(
11248
+ const ingestCheck = FreeTierLimitsGuard_exports.FreeTierLimitsGuard.checkIngestionAllowed(
11450
11249
  { documentCount: documents.length },
11451
11250
  totalIncomingBytes
11452
11251
  );
@@ -11498,7 +11297,6 @@ function createLicenseHandler(configOrPlugin, options) {
11498
11297
  const plugin = getOrCreatePlugin(configOrPlugin);
11499
11298
  const onAuthorize = options == null ? void 0 : options.onAuthorize;
11500
11299
  return async function POST(req) {
11501
- var _a3;
11502
11300
  if (req) {
11503
11301
  const authResult = await checkAuth(req, onAuthorize);
11504
11302
  if (authResult) return authResult;
@@ -11506,7 +11304,17 @@ function createLicenseHandler(configOrPlugin, options) {
11506
11304
  try {
11507
11305
  const body = await req.json().catch(() => ({}));
11508
11306
  const config = plugin.getConfig();
11509
- 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 || "";
11307
+ const authHeader = req.headers.get("authorization");
11308
+ let authHeaderKey = "";
11309
+ if (authHeader && authHeader.toLowerCase().startsWith("bearer ")) {
11310
+ authHeaderKey = authHeader.replace(/^Bearer\s+/i, "");
11311
+ if (authHeaderKey) {
11312
+ console.warn(
11313
+ '[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.'
11314
+ );
11315
+ }
11316
+ }
11317
+ const rawKey = req.headers.get("x-license-key") || (body == null ? void 0 : body.licenseKey) || authHeaderKey || config.licenseKey || process.env.RETRIVORA_LICENSE_KEY || "";
11510
11318
  const licenseKey = (rawKey || "").trim().replace(/^["']|["']$/g, "").trim();
11511
11319
  const projectId = (body == null ? void 0 : body.projectId) || config.projectId || "";
11512
11320
  const payload = await LicenseVerifier.verifyAsync(licenseKey, projectId);
@@ -11557,7 +11365,7 @@ function createUploadHandler(configOrPlugin, options) {
11557
11365
  }
11558
11366
  if (isFreeTier(licenseCheck.payload.tier)) {
11559
11367
  const totalUploadBytes = files.reduce((sum, f) => sum + (f.size || 0), 0);
11560
- const uploadCheck = FreeTierLimitsGuard.checkIngestionAllowed(
11368
+ const uploadCheck = FreeTierLimitsGuard_exports.FreeTierLimitsGuard.checkIngestionAllowed(
11561
11369
  { documentCount: files.length },
11562
11370
  totalUploadBytes
11563
11371
  );
@@ -11569,8 +11377,8 @@ function createUploadHandler(configOrPlugin, options) {
11569
11377
  projectId: licenseCheck.payload.projectId,
11570
11378
  fileCount: files.length,
11571
11379
  totalUploadBytes,
11572
- maxFileSizeBytes: FREE_TIER_QUOTAS.MAX_FILE_SIZE_BYTES,
11573
- maxStorageBytes: FREE_TIER_QUOTAS.MAX_STORAGE_BYTES
11380
+ maxFileSizeBytes: FreeTierLimitsGuard_exports.FREE_TIER_QUOTAS.MAX_FILE_SIZE_BYTES,
11381
+ maxStorageBytes: FreeTierLimitsGuard_exports.FREE_TIER_QUOTAS.MAX_STORAGE_BYTES
11574
11382
  }
11575
11383
  );
11576
11384
  }
@@ -11884,7 +11692,7 @@ function createRagHandler(configOrPlugin, options) {
11884
11692
  }
11885
11693
  }
11886
11694
  async function getSegment(req, context) {
11887
- var _a3, _b, _c;
11695
+ var _a2, _b, _c;
11888
11696
  const contentType = req.headers.get("content-type") || "";
11889
11697
  if (contentType.includes("multipart/form-data")) {
11890
11698
  return "upload";
@@ -11903,7 +11711,7 @@ function createRagHandler(configOrPlugin, options) {
11903
11711
  if (pathname.endsWith("/sessions")) return "sessions";
11904
11712
  } catch (e) {
11905
11713
  }
11906
- 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;
11714
+ 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;
11907
11715
  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) || [];
11908
11716
  const joined = segments.join("/");
11909
11717
  return joined || "chat";