@retrivora-ai/rag-engine 2.6.0 → 2.6.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. package/README.md +8 -6
  2. package/dist/ChromaDBProvider-NDA2IBOH.mjs +8 -0
  3. package/dist/{ILLMProvider-DUCL1r75.d.mts → ILLMProvider-CzXcTA6d.d.mts} +21 -3
  4. package/dist/{ILLMProvider-DUCL1r75.d.ts → ILLMProvider-CzXcTA6d.d.ts} +21 -3
  5. package/dist/MilvusProvider-H6GGL5FQ.mjs +8 -0
  6. package/dist/MongoDBProvider-KC3GF3FH.mjs +8 -0
  7. package/dist/MultiTablePostgresProvider-6LJBFZL2.mjs +9 -0
  8. package/dist/PineconeProvider-22RF2QPF.mjs +10 -0
  9. package/dist/PineconeProvider-HBBIXLT3.mjs +10 -0
  10. package/dist/PostgreSQLProvider-VWFL4OIP.mjs +8 -0
  11. package/dist/QdrantProvider-UU7RGXBI.mjs +8 -0
  12. package/dist/RedisProvider-MIIPSAAE.mjs +8 -0
  13. package/dist/SimpleGraphProvider-HJIS4S7Y.mjs +70 -0
  14. package/dist/UniversalVectorProvider-NO3L255G.mjs +9 -0
  15. package/dist/WeaviateProvider-YRZDZFAC.mjs +8 -0
  16. package/dist/chunk-23PFNEBF.mjs +107 -0
  17. package/dist/chunk-3E4GZGDX.mjs +10851 -0
  18. package/dist/chunk-3SQ4DODU.mjs +238 -0
  19. package/dist/chunk-AHMMUZ4W.mjs +152 -0
  20. package/dist/chunk-ASBNNEVI.mjs +81 -0
  21. package/dist/chunk-AT4TFUQN.mjs +219 -0
  22. package/dist/chunk-BEPLYQBS.mjs +89 -0
  23. package/dist/chunk-G7MJRFCO.mjs +340 -0
  24. package/dist/chunk-IHWKVAUR.mjs +112 -0
  25. package/dist/chunk-IMP6FUCY.mjs +30 -0
  26. package/dist/chunk-LFNV6DEL.mjs +281 -0
  27. package/dist/chunk-MAXMVO7B.mjs +211 -0
  28. package/dist/chunk-MGJ6V2IT.mjs +203 -0
  29. package/dist/chunk-N3TI27LG.mjs +334 -0
  30. package/dist/chunk-NWYE4Q7B.mjs +334 -0
  31. package/dist/chunk-OJMAT4EG.mjs +219 -0
  32. package/dist/chunk-QKZM3RSJ.mjs +10851 -0
  33. package/dist/chunk-QUY3E6HJ.mjs +168 -0
  34. package/dist/chunk-SG6XYLQE.mjs +53 -0
  35. package/dist/chunk-UT6SZZEC.mjs +69 -0
  36. package/dist/chunk-VW3U4MMX.mjs +157 -0
  37. package/dist/chunk-WOJCOTXI.mjs +281 -0
  38. package/dist/chunk-XP6NJMX4.mjs +168 -0
  39. package/dist/chunk-XYBKDJDW.mjs +696 -0
  40. package/dist/handlers/index.d.mts +2 -2
  41. package/dist/handlers/index.d.ts +2 -2
  42. package/dist/handlers/index.js +9736 -9354
  43. package/dist/handlers/index.mjs +24 -13686
  44. package/dist/{index-CsBcqng1.d.ts → index-BBbt5A2y.d.mts} +61 -47
  45. package/dist/{index-BVSHblli.d.mts → index-BST9Gzsv.d.mts} +8 -7
  46. package/dist/{index-D6Oq2zyc.d.mts → index-BZ_rR6V8.d.ts} +61 -47
  47. package/dist/{index-DGW0NNmR.d.ts → index-qLvtMUr-.d.ts} +8 -7
  48. package/dist/index.css +23 -0
  49. package/dist/index.d.mts +4 -4
  50. package/dist/index.d.ts +4 -4
  51. package/dist/index.js +278 -190
  52. package/dist/index.min.css +1 -1
  53. package/dist/index.mjs +218 -1558
  54. package/dist/server.d.mts +58 -12
  55. package/dist/server.d.ts +58 -12
  56. package/dist/server.js +968 -577
  57. package/dist/server.mjs +395 -14244
  58. package/package.json +1 -1
package/dist/server.js CHANGED
@@ -84,6 +84,9 @@ function isRecord(value) {
84
84
  function resolvePath(obj, path2) {
85
85
  if (!path2 || !obj) return void 0;
86
86
  return path2.replace(/\[(\w+)\]/g, ".$1").replace(/^\./, "").split(".").reduce((current, segment) => {
87
+ if (segment === "__proto__" || segment === "constructor" || segment === "prototype") {
88
+ return void 0;
89
+ }
87
90
  if (!isRecord(current) && !Array.isArray(current)) {
88
91
  return void 0;
89
92
  }
@@ -94,8 +97,9 @@ function buildPayload(template, vars) {
94
97
  let raw = template;
95
98
  for (const [key, val] of Object.entries(vars)) {
96
99
  const stringifiedVal = val === void 0 ? "null" : JSON.stringify(val);
97
- raw = raw.replace(new RegExp(`"\\{\\{${key}\\}\\}"`, "g"), () => stringifiedVal);
98
- raw = raw.replace(new RegExp(`\\{\\{${key}\\}\\}`, "g"), () => stringifiedVal);
100
+ const escapedKey = key.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
101
+ raw = raw.replace(new RegExp(`"\\{\\{${escapedKey}\\}\\}"`, "g"), () => stringifiedVal);
102
+ raw = raw.replace(new RegExp(`\\{\\{${escapedKey}\\}\\}`, "g"), () => stringifiedVal);
99
103
  }
100
104
  try {
101
105
  return JSON.parse(raw);
@@ -332,7 +336,7 @@ var init_package = __esm({
332
336
  "package.json"() {
333
337
  package_default = {
334
338
  name: "@retrivora-ai/rag-engine",
335
- version: "2.6.0",
339
+ version: "2.6.3",
336
340
  description: "Retrivora AI is a plug-and-play AI engine for RAG chat experiences \u2014 generic vector DB + LLM provider, embeddable or standalone.",
337
341
  author: "Abhinav Alkuchi",
338
342
  license: "UNLICENSED",
@@ -511,7 +515,7 @@ var init_ConfigFetcher = __esm({
511
515
  init_ProjectSettings();
512
516
  init_version();
513
517
  CONTROL_PLANE_URL = "https://www.retrivora.com";
514
- LLM_PROXY_BASE_URL = "https://www.retrivora.com/api/v1";
518
+ LLM_PROXY_BASE_URL = "https://llm.retrivora.com/api/v1";
515
519
  ConfigFetcher = class {
516
520
  // 5-minute in-memory session cache
517
521
  /**
@@ -1104,13 +1108,14 @@ var init_PostgreSQLProvider = __esm({
1104
1108
  const filterConditions = Object.entries(publicFilter).map(([key, val]) => {
1105
1109
  const paramIdx = params.length + 1;
1106
1110
  params.push(String(val));
1107
- return `metadata->>'${key}' = $${paramIdx}`;
1111
+ return `metadata->>'${key.replace(/[^a-zA-Z0-9_.-]/g, "")}' = $${paramIdx}`;
1108
1112
  }).join(" AND ");
1109
1113
  whereClause = whereClause ? `${whereClause} AND ${filterConditions}` : `WHERE ${filterConditions}`;
1110
1114
  }
1111
1115
  const client = await this.pool.connect();
1112
1116
  try {
1113
- const efSearch = this.config.options.efSearch || Math.max(topK * 10, 40);
1117
+ const rawEfSearch = Number(this.config.options.efSearch) || Math.max(topK * 10, 40);
1118
+ const efSearch = Math.max(1, Math.min(1e3, Math.floor(Number.isFinite(rawEfSearch) ? rawEfSearch : 40)));
1114
1119
  await client.query(`SET LOCAL hnsw.ef_search = ${efSearch}`);
1115
1120
  const result = await client.query(
1116
1121
  `SELECT id, content, metadata, 1 - (embedding <=> $1::vector) AS score
@@ -1254,7 +1259,11 @@ var init_MultiTablePostgresProvider = __esm({
1254
1259
  this.tables = [];
1255
1260
  const rawSearchFields = (_c = (_b = opts.searchFields) != null ? _b : process.env.VECTOR_DB_SEARCH_FIELDS) != null ? _c : ["name", "product_name", "productname", "title"];
1256
1261
  this.searchFields = typeof rawSearchFields === "string" ? rawSearchFields.split(",").map((f) => f.trim()).filter(Boolean) : rawSearchFields;
1257
- this.uploadTable = opts.uploadTable || "document_chunks";
1262
+ const rawUploadTable = opts.uploadTable || "document_chunks";
1263
+ if (!/^[a-zA-Z_][a-zA-Z0-9_]{0,62}$/.test(rawUploadTable)) {
1264
+ throw new Error(`Invalid uploadTable name: ${rawUploadTable}`);
1265
+ }
1266
+ this.uploadTable = rawUploadTable;
1258
1267
  }
1259
1268
  async initialize() {
1260
1269
  this.pool = new import_pg2.Pool({ connectionString: this.connectionString });
@@ -1437,7 +1446,7 @@ var init_MultiTablePostgresProvider = __esm({
1437
1446
  const baseOffset = queryText && dynamicKeywordQuery ? 2 : 1;
1438
1447
  const paramIdx = baseOffset + filterParams.length;
1439
1448
  const synonyms = (_a4 = FIELD_SYNONYMS[key]) != null ? _a4 : [];
1440
- const keysToCheck = [key, ...synonyms];
1449
+ const keysToCheck = [key, ...synonyms].filter((k) => /^[a-zA-Z_][a-zA-Z0-9_]{0,62}$/.test(k));
1441
1450
  const coalesceExprs = keysToCheck.flatMap((k) => [
1442
1451
  `metadata->>'${k}'`,
1443
1452
  `to_jsonb(t)->>'${k}'`
@@ -1454,7 +1463,7 @@ var init_MultiTablePostgresProvider = __esm({
1454
1463
  THEN 1.0 ELSE 0.0 END
1455
1464
  ), 0)
1456
1465
  FROM jsonb_each_text(COALESCE(v.metadata, k.metadata)) AS kv(key, val)
1457
- WHERE key IN (${this.searchFields.map((f) => `'${f}'`).join(", ")})
1466
+ WHERE key IN (${this.searchFields.map((f) => `'${f.replace(/'/g, "''")}'`).join(", ")})
1458
1467
  ) * 3.0` : "";
1459
1468
  sqlQuery = `
1460
1469
  WITH vector_search AS (
@@ -2733,6 +2742,7 @@ __export(server_exports, {
2733
2742
  IntentClassifier: () => IntentClassifier,
2734
2743
  LLMFactory: () => LLMFactory,
2735
2744
  LLM_PROFILES: () => LLM_PROFILES,
2745
+ LangChainAgent: () => LangChainAgent,
2736
2746
  LicenseConfigResolver: () => LicenseConfigResolver,
2737
2747
  LicenseValidationError: () => LicenseValidationError,
2738
2748
  LicenseValidator: () => LicenseValidator,
@@ -2792,10 +2802,12 @@ __export(server_exports, {
2792
2802
  createHealthHandler: () => createHealthHandler,
2793
2803
  createHistoryHandler: () => createHistoryHandler,
2794
2804
  createIngestHandler: () => createIngestHandler,
2805
+ createLicenseHandler: () => createLicenseHandler,
2795
2806
  createRagHandler: () => createRagHandler,
2796
2807
  createRetrivora: () => createRetrivora,
2797
2808
  createSessionsHandler: () => createSessionsHandler,
2798
2809
  createStreamHandler: () => createStreamHandler,
2810
+ createSuggestionsHandler: () => createSuggestionsHandler,
2799
2811
  createUploadHandler: () => createUploadHandler,
2800
2812
  decideVisualization: () => decideVisualization,
2801
2813
  getRagConfig: () => getRagConfig,
@@ -2809,7 +2821,9 @@ __export(server_exports, {
2809
2821
  sseErrorFrame: () => sseErrorFrame,
2810
2822
  sseFrame: () => sseFrame,
2811
2823
  sseMetaFrame: () => sseMetaFrame,
2824
+ sseObservabilityFrame: () => sseObservabilityFrame,
2812
2825
  sseTextFrame: () => sseTextFrame,
2826
+ sseUIFrame: () => sseUIFrame,
2813
2827
  wrapError: () => wrapError
2814
2828
  });
2815
2829
  module.exports = __toCommonJS(server_exports);
@@ -2875,13 +2889,14 @@ function wrapError(err, defaultCode, defaultMessage) {
2875
2889
  const message = (error == null ? void 0 : error.message) || defaultMessage || String(err);
2876
2890
  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);
2877
2891
  const code = error == null ? void 0 : error.code;
2878
- if (status === 429 || /rate[- ]?limit/i.test(message) || code === "RATE_LIMIT_EXCEEDED") {
2892
+ if (status === 429 || /rate[- ]?limit|token limit|too many requests|tokens per minute/i.test(message) || code === "RATE_LIMIT_EXCEEDED") {
2879
2893
  return new RateLimitException(message, err);
2880
2894
  }
2881
- if (status === 401 || status === 403 || /unauthorized|auth|license|revoked|terminated|api[- ]?key/i.test(message) || code === "INVALID_API_KEY" || code === "LICENSE_DELETED" || code === "LICENSE_INACTIVE") {
2882
- const isLicenseError = /403|license|revoked|terminated/i.test(message) || status === 403;
2883
- const cleanMsg = isLicenseError ? "Your Retrivora license key has been terminated, suspended, or revoked. Access denied." : message;
2884
- return new AuthenticationException(cleanMsg, err);
2895
+ if (code === "LICENSE_DELETED" || code === "LICENSE_INACTIVE" || code === "LICENSE_REVOKED" || /license key has been terminated|license is revoked|license is suspended|license is inactive/i.test(message)) {
2896
+ return new AuthenticationException("Your Retrivora license key has been terminated, suspended, or revoked. Access denied.", err);
2897
+ }
2898
+ if (status === 401 || status === 403 || code === "INVALID_API_KEY" || code === "AUTHENTICATION_ERROR" || /unauthorized|forbidden|invalid api key/i.test(message)) {
2899
+ return new AuthenticationException(message, err);
2885
2900
  }
2886
2901
  switch (defaultCode) {
2887
2902
  case "PROVIDER_NOT_FOUND":
@@ -2902,12 +2917,27 @@ function wrapError(err, defaultCode, defaultMessage) {
2902
2917
  }
2903
2918
 
2904
2919
  // src/core/LicenseVerifier.ts
2920
+ function decodeBase64UrlToUtf8(b64url) {
2921
+ const normalized = b64url.replace(/-/g, "+").replace(/_/g, "/");
2922
+ const padded = normalized + "=".repeat((4 - normalized.length % 4) % 4);
2923
+ if (typeof Buffer !== "undefined" && typeof Buffer.from === "function") {
2924
+ return Buffer.from(padded, "base64").toString("utf8");
2925
+ }
2926
+ if (typeof atob === "function") {
2927
+ const binary = atob(padded);
2928
+ const bytes = new Uint8Array(binary.length);
2929
+ for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
2930
+ return new TextDecoder().decode(bytes);
2931
+ }
2932
+ throw new Error("Base64 decoding is not supported in this environment.");
2933
+ }
2905
2934
  var _LicenseVerifier = class _LicenseVerifier {
2906
2935
  // Cache verified license for 5 minutes
2907
2936
  /**
2908
2937
  * Computes a deterministic hardware fingerprint to enforce node-locked licensing.
2909
2938
  */
2910
2939
  static async generateMachineFingerprint() {
2940
+ if (typeof process !== "undefined" && (process.env.VERCEL || process.env.AWS_LAMBDA_FUNCTION_NAME)) return "browser-client";
2911
2941
  if (typeof window !== "undefined" && typeof navigator !== "undefined") {
2912
2942
  return "browser-client";
2913
2943
  }
@@ -2971,7 +3001,7 @@ var _LicenseVerifier = class _LicenseVerifier {
2971
3001
  }
2972
3002
  }
2973
3003
  try {
2974
- let baseUrl = typeof process !== "undefined" && process.env.RETRIVORA_CONTROL_PLANE_URL ? process.env.RETRIVORA_CONTROL_PLANE_URL : "https://retrivora.com";
3004
+ let baseUrl = typeof process !== "undefined" && process.env.RETRIVORA_CONTROL_PLANE_URL ? process.env.RETRIVORA_CONTROL_PLANE_URL : "https://www.retrivora.com";
2975
3005
  if (typeof process !== "undefined" && process.env.TELEMETRY_URL) {
2976
3006
  baseUrl = process.env.TELEMETRY_URL.replace(/\/api\/telemetry\/?$/, "");
2977
3007
  }
@@ -3033,6 +3063,9 @@ var _LicenseVerifier = class _LicenseVerifier {
3033
3063
  minimumSupportedVersion: data.minimumSupportedVersion,
3034
3064
  forceUpgrade: data.forceUpgrade
3035
3065
  };
3066
+ if (payload.expiresAt && Math.floor(Date.now() / 1e3) > payload.expiresAt) {
3067
+ throw new Error("License key has expired.");
3068
+ }
3036
3069
  if (isProduction && provider) {
3037
3070
  const normalizedProvider = provider.toLowerCase();
3038
3071
  if (tier === "hobby") {
@@ -3071,63 +3104,67 @@ var _LicenseVerifier = class _LicenseVerifier {
3071
3104
  }
3072
3105
  }
3073
3106
  static async verifyIP(licenseKey) {
3107
+ var _a2, _b;
3074
3108
  if (!licenseKey) return;
3075
3109
  try {
3076
3110
  const parts = licenseKey.split(".");
3077
3111
  if (parts.length !== 3) return;
3078
3112
  const [, payloadB64] = parts;
3079
3113
  const payload = JSON.parse(
3080
- Buffer.from(payloadB64, "base64url").toString("utf8")
3114
+ decodeBase64UrlToUtf8(payloadB64)
3081
3115
  );
3082
3116
  const tier = (payload.tier || "").toLowerCase();
3083
- if (tier === "enterprise" || tier === "custom") {
3117
+ if (tier === "enterprise" || tier === "custom" || !payload.ipv4 && !payload.ipv6) {
3084
3118
  return;
3085
3119
  }
3086
- if (payload.ipv4 || payload.ipv6) {
3087
- let currentIpv4 = "";
3088
- let currentIpv6 = "";
3089
- try {
3090
- const res = await fetch("https://api4.ipify.org?format=json", { signal: AbortSignal.timeout(3e3) });
3091
- const data = await res.json();
3092
- currentIpv4 = data.ip;
3093
- } catch (e) {
3120
+ let currentIpv4 = "";
3121
+ let currentIpv6 = "";
3122
+ const now = Date.now();
3123
+ if (this.cachedIps && now - this.cachedIps.fetchedAt < 10 * 60 * 1e3) {
3124
+ currentIpv4 = this.cachedIps.ipv4;
3125
+ currentIpv6 = this.cachedIps.ipv6;
3126
+ } else {
3127
+ const [v4Result, v6Result] = await Promise.allSettled([
3128
+ fetch("https://api4.ipify.org?format=json", { signal: AbortSignal.timeout(2e3) }).then((r) => r.json()),
3129
+ fetch("https://api6.ipify.org?format=json", { signal: AbortSignal.timeout(2e3) }).then((r) => r.json())
3130
+ ]);
3131
+ if (v4Result.status === "fulfilled" && ((_a2 = v4Result.value) == null ? void 0 : _a2.ip)) {
3132
+ currentIpv4 = v4Result.value.ip;
3094
3133
  }
3095
- try {
3096
- const res = await fetch("https://api6.ipify.org?format=json", { signal: AbortSignal.timeout(3e3) });
3097
- const data = await res.json();
3098
- currentIpv6 = data.ip;
3099
- } catch (e) {
3134
+ if (v6Result.status === "fulfilled" && ((_b = v6Result.value) == null ? void 0 : _b.ip)) {
3135
+ currentIpv6 = v6Result.value.ip;
3100
3136
  }
3101
- const localIps = [];
3102
- try {
3103
- const osModule = require("os");
3104
- const interfaces = osModule.networkInterfaces();
3105
- for (const name of Object.keys(interfaces)) {
3106
- for (const iface of interfaces[name] || []) {
3107
- if (!iface.internal) {
3108
- localIps.push(iface.address);
3109
- }
3137
+ this.cachedIps = { ipv4: currentIpv4, ipv6: currentIpv6, fetchedAt: now };
3138
+ }
3139
+ const localIps = [];
3140
+ try {
3141
+ const osModule = await import("os");
3142
+ const interfaces = osModule.networkInterfaces();
3143
+ for (const name of Object.keys(interfaces)) {
3144
+ for (const iface of interfaces[name] || []) {
3145
+ if (!iface.internal) {
3146
+ localIps.push(iface.address);
3110
3147
  }
3111
3148
  }
3112
- } catch (e) {
3113
3149
  }
3114
- const isIpv4Match = payload.ipv4 && (currentIpv4 === payload.ipv4 || localIps.includes(payload.ipv4));
3115
- const isIpv6Match = payload.ipv6 && (currentIpv6 === payload.ipv6 || localIps.includes(payload.ipv6));
3116
- if (payload.ipv4 && payload.ipv6) {
3117
- if (!isIpv4Match && !isIpv6Match) {
3118
- throw new Error(
3119
- `License key access restricted. This license key was created for a different system (authorized IPv4: ${payload.ipv4}, IPv6: ${payload.ipv6}) and cannot be used on this machine.`
3120
- );
3121
- }
3122
- } else if (payload.ipv4 && !isIpv4Match) {
3123
- throw new Error(
3124
- `License key access restricted. This license key was created for a different system (authorized IPv4: ${payload.ipv4}) and cannot be used on this machine.`
3125
- );
3126
- } else if (payload.ipv6 && !isIpv6Match) {
3150
+ } catch (e) {
3151
+ }
3152
+ const isIpv4Match = payload.ipv4 && (currentIpv4 === payload.ipv4 || localIps.includes(payload.ipv4));
3153
+ const isIpv6Match = payload.ipv6 && (currentIpv6 === payload.ipv6 || localIps.includes(payload.ipv6));
3154
+ if (payload.ipv4 && payload.ipv6) {
3155
+ if (!isIpv4Match && !isIpv6Match) {
3127
3156
  throw new Error(
3128
- `License key access restricted. This license key was created for a different system (authorized IPv6: ${payload.ipv6}) and cannot be used on this machine.`
3157
+ `License key access restricted. This license key was created for a different system (authorized IPv4: ${payload.ipv4}, IPv6: ${payload.ipv6}) and cannot be used on this machine.`
3129
3158
  );
3130
3159
  }
3160
+ } else if (payload.ipv4 && !isIpv4Match) {
3161
+ throw new Error(
3162
+ `License key access restricted. This license key was created for a different system (authorized IPv4: ${payload.ipv4}) and cannot be used on this machine.`
3163
+ );
3164
+ } else if (payload.ipv6 && !isIpv6Match) {
3165
+ throw new Error(
3166
+ `License key access restricted. This license key was created for a different system (authorized IPv6: ${payload.ipv6}) and cannot be used on this machine.`
3167
+ );
3131
3168
  }
3132
3169
  } catch (err) {
3133
3170
  if (err.message.includes("License key access restricted")) {
@@ -3142,7 +3179,7 @@ var _LicenseVerifier = class _LicenseVerifier {
3142
3179
  const rawToken = sanitizedKey.replace(/^rtv_/i, "").trim();
3143
3180
  const parts = rawToken.split(".");
3144
3181
  if (parts.length === 3) {
3145
- const decodedPayload = JSON.parse(Buffer.from(parts[1], "base64url").toString("utf8"));
3182
+ const decodedPayload = JSON.parse(decodeBase64UrlToUtf8(parts[1]));
3146
3183
  if (decodedPayload.tier) return decodedPayload.tier;
3147
3184
  }
3148
3185
  } catch (e) {
@@ -3153,6 +3190,7 @@ var _LicenseVerifier = class _LicenseVerifier {
3153
3190
  // In-memory cache to save network overhead on hot paths.
3154
3191
  _LicenseVerifier.cache = /* @__PURE__ */ new Map();
3155
3192
  _LicenseVerifier.CACHE_TTL_MS = 5 * 60 * 1e3;
3193
+ _LicenseVerifier.cachedIps = null;
3156
3194
  var LicenseVerifier = _LicenseVerifier;
3157
3195
 
3158
3196
  // src/config/constants.ts
@@ -4408,7 +4446,7 @@ var GroqProvider = class {
4408
4446
  };
4409
4447
  }
4410
4448
  async chat(messages, context, options) {
4411
- var _a2, _b, _c, _d, _e, _f, _g2, _h, _i;
4449
+ var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k, _l;
4412
4450
  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.";
4413
4451
  const systemMessage = {
4414
4452
  role: "system",
@@ -4422,18 +4460,46 @@ var GroqProvider = class {
4422
4460
  systemMessage,
4423
4461
  ...safeMessages
4424
4462
  ];
4425
- const completion = await this.client.chat.completions.create({
4426
- model: this.llmConfig.model,
4427
- messages: formattedMessages,
4428
- max_tokens: (_d = (_c = options == null ? void 0 : options.maxTokens) != null ? _c : this.llmConfig.maxTokens) != null ? _d : 1024,
4429
- temperature: (_f = (_e = options == null ? void 0 : options.temperature) != null ? _e : this.llmConfig.temperature) != null ? _f : 0.7,
4430
- stop: options == null ? void 0 : options.stop
4431
- });
4432
- return (_i = (_h = (_g2 = completion.choices[0]) == null ? void 0 : _g2.message) == null ? void 0 : _h.content) != null ? _i : "";
4463
+ const model = this.llmConfig.model === "qwen/qwen3.6-27b" || this.llmConfig.model === "qwen-3.6-27b" ? "qwen/qwen3.8-27b" : this.llmConfig.model;
4464
+ try {
4465
+ const completion = await this.client.chat.completions.create({
4466
+ model,
4467
+ messages: formattedMessages,
4468
+ max_tokens: Math.min((_d = (_c = options == null ? void 0 : options.maxTokens) != null ? _c : this.llmConfig.maxTokens) != null ? _d : 800, 800),
4469
+ temperature: (_f = (_e = options == null ? void 0 : options.temperature) != null ? _e : this.llmConfig.temperature) != null ? _f : 0.7,
4470
+ stop: options == null ? void 0 : options.stop
4471
+ });
4472
+ return (_i = (_h = (_g2 = completion.choices[0]) == null ? void 0 : _g2.message) == null ? void 0 : _h.content) != null ? _i : "";
4473
+ } catch (err) {
4474
+ console.warn("[GroqProvider] Groq chat request failed, falling back strictly to Hugging Face Qwen:", (err == null ? void 0 : err.message) || err);
4475
+ const hfToken = process.env.HF_TOKEN || process.env.HUGGINGFACE_API_KEY || "";
4476
+ try {
4477
+ const hfRes = await fetch("https://router.huggingface.co/v1/chat/completions", {
4478
+ method: "POST",
4479
+ headers: {
4480
+ "Content-Type": "application/json",
4481
+ "Authorization": `Bearer ${hfToken}`
4482
+ },
4483
+ body: JSON.stringify({
4484
+ model: "Qwen/Qwen2.5-Coder-32B-Instruct",
4485
+ messages: formattedMessages,
4486
+ max_tokens: 1024,
4487
+ temperature: 0.7
4488
+ })
4489
+ });
4490
+ if (hfRes.ok) {
4491
+ const data = await hfRes.json();
4492
+ const content = (_l = (_k = (_j = data == null ? void 0 : data.choices) == null ? void 0 : _j[0]) == null ? void 0 : _k.message) == null ? void 0 : _l.content;
4493
+ if (content !== void 0) return content;
4494
+ }
4495
+ } catch (e) {
4496
+ }
4497
+ throw err;
4498
+ }
4433
4499
  }
4434
4500
  chatStream(messages, context, options) {
4435
4501
  return __asyncGenerator(this, null, function* () {
4436
- var _a2, _b, _c, _d, _e, _f, _g2, _h;
4502
+ var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k, _l;
4437
4503
  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.";
4438
4504
  const systemMessage = {
4439
4505
  role: "system",
@@ -4447,32 +4513,81 @@ var GroqProvider = class {
4447
4513
  systemMessage,
4448
4514
  ...safeStreamMessages
4449
4515
  ];
4450
- const stream = yield new __await(this.client.chat.completions.create({
4451
- model: this.llmConfig.model,
4452
- messages: formattedMessages,
4453
- max_tokens: (_d = (_c = options == null ? void 0 : options.maxTokens) != null ? _c : this.llmConfig.maxTokens) != null ? _d : 1024,
4454
- temperature: (_f = (_e = options == null ? void 0 : options.temperature) != null ? _e : this.llmConfig.temperature) != null ? _f : 0.7,
4455
- stop: options == null ? void 0 : options.stop,
4456
- stream: true
4457
- }));
4458
- if (!stream) {
4459
- throw new Error("[GroqProvider] completions.create stream is undefined");
4460
- }
4516
+ const model = this.llmConfig.model === "qwen/qwen3.6-27b" || this.llmConfig.model === "qwen-3.6-27b" ? "qwen/qwen3.8-27b" : this.llmConfig.model;
4461
4517
  try {
4462
- for (var iter = __forAwait(stream), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
4463
- const chunk = temp.value;
4464
- const content = ((_h = (_g2 = chunk.choices[0]) == null ? void 0 : _g2.delta) == null ? void 0 : _h.content) || "";
4465
- if (content) yield content;
4518
+ const stream = yield new __await(this.client.chat.completions.create({
4519
+ model,
4520
+ messages: formattedMessages,
4521
+ max_tokens: Math.min((_d = (_c = options == null ? void 0 : options.maxTokens) != null ? _c : this.llmConfig.maxTokens) != null ? _d : 800, 800),
4522
+ temperature: (_f = (_e = options == null ? void 0 : options.temperature) != null ? _e : this.llmConfig.temperature) != null ? _f : 0.7,
4523
+ stop: options == null ? void 0 : options.stop,
4524
+ stream: true
4525
+ }));
4526
+ if (!stream) {
4527
+ throw new Error("[GroqProvider] completions.create stream is undefined");
4466
4528
  }
4467
- } catch (temp) {
4468
- error = [temp];
4469
- } finally {
4470
4529
  try {
4471
- more && (temp = iter.return) && (yield new __await(temp.call(iter)));
4530
+ for (var iter = __forAwait(stream), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
4531
+ const chunk = temp.value;
4532
+ const content = ((_h = (_g2 = chunk.choices[0]) == null ? void 0 : _g2.delta) == null ? void 0 : _h.content) || "";
4533
+ if (content) yield content;
4534
+ }
4535
+ } catch (temp) {
4536
+ error = [temp];
4472
4537
  } finally {
4473
- if (error)
4474
- throw error[0];
4538
+ try {
4539
+ more && (temp = iter.return) && (yield new __await(temp.call(iter)));
4540
+ } finally {
4541
+ if (error)
4542
+ throw error[0];
4543
+ }
4475
4544
  }
4545
+ } catch (err) {
4546
+ console.warn("[GroqProvider] Groq chat stream failed, falling back strictly to Hugging Face Qwen stream:", (err == null ? void 0 : err.message) || err);
4547
+ const hfToken = process.env.HF_TOKEN || process.env.HUGGINGFACE_API_KEY || "";
4548
+ try {
4549
+ const hfRes = yield new __await(fetch("https://router.huggingface.co/v1/chat/completions", {
4550
+ method: "POST",
4551
+ headers: {
4552
+ "Content-Type": "application/json",
4553
+ "Authorization": `Bearer ${hfToken}`
4554
+ },
4555
+ body: JSON.stringify({
4556
+ model: "Qwen/Qwen2.5-Coder-32B-Instruct",
4557
+ messages: formattedMessages,
4558
+ max_tokens: 1024,
4559
+ temperature: 0.7,
4560
+ stream: true
4561
+ })
4562
+ }));
4563
+ if (hfRes.ok && hfRes.body) {
4564
+ const reader = hfRes.body.getReader();
4565
+ const decoder = new TextDecoder("utf-8");
4566
+ let buffer = "";
4567
+ while (true) {
4568
+ const { done, value } = yield new __await(reader.read());
4569
+ if (done) break;
4570
+ buffer += decoder.decode(value, { stream: true });
4571
+ const lines = buffer.split("\n");
4572
+ buffer = (_i = lines.pop()) != null ? _i : "";
4573
+ for (const line of lines) {
4574
+ const trimmed = line.trim();
4575
+ if (!trimmed || trimmed === "data: [DONE]") continue;
4576
+ if (!trimmed.startsWith("data:")) continue;
4577
+ try {
4578
+ const json = JSON.parse(trimmed.slice(5).trim());
4579
+ const content = ((_l = (_k = (_j = json == null ? void 0 : json.choices) == null ? void 0 : _j[0]) == null ? void 0 : _k.delta) == null ? void 0 : _l.content) || "";
4580
+ if (content) yield content;
4581
+ } catch (e) {
4582
+ }
4583
+ }
4584
+ }
4585
+ return;
4586
+ }
4587
+ } catch (hfErr) {
4588
+ console.warn("[GroqProvider] Hugging Face Qwen fallback stream failed:", hfErr);
4589
+ }
4590
+ throw err;
4476
4591
  }
4477
4592
  });
4478
4593
  }
@@ -4922,6 +5037,7 @@ ${context != null ? context : "None"}` },
4922
5037
  } catch (fetchErr) {
4923
5038
  console.warn(`[UniversalLLMAdapter] Remote HTTP stream fetch warning: ${fetchErr == null ? void 0 : fetchErr.message}. Attempting in-process fallback...`);
4924
5039
  }
5040
+ let lastDispatchErr = null;
4925
5041
  if (!streamBody) {
4926
5042
  const _g2 = globalThis;
4927
5043
  const dispatch = _g2.__retrivoraDispatchChat;
@@ -4944,12 +5060,16 @@ ${context != null ? context : "None"}` },
4944
5060
  }
4945
5061
  }
4946
5062
  } catch (dispatchErr) {
4947
- console.warn("[UniversalLLMAdapter] In-process dispatch error:", dispatchErr);
5063
+ lastDispatchErr = dispatchErr;
5064
+ console.warn("[UniversalLLMAdapter] In-process dispatch error:", (dispatchErr == null ? void 0 : dispatchErr.message) || dispatchErr);
4948
5065
  }
4949
5066
  }
4950
5067
  }
4951
5068
  if (!streamBody) {
4952
- throw new Error(`[UniversalLLMAdapter] Streaming request failed. Ensure RETRIVORA_LICENSE_KEY is valid or LLM service is available.`);
5069
+ if (lastDispatchErr) {
5070
+ throw lastDispatchErr;
5071
+ }
5072
+ throw new Error(`[UniversalLLMAdapter] Streaming request failed: LLM provider unavailable.`);
4953
5073
  }
4954
5074
  const reader = streamBody.getReader();
4955
5075
  const decoder = new TextDecoder("utf-8");
@@ -4988,72 +5108,74 @@ ${context != null ? context : "None"}` },
4988
5108
  });
4989
5109
  }
4990
5110
  async embed(text) {
4991
- var _a2, _b, _c, _d, _e, _f, _g2, _h;
4992
- const path2 = (_a2 = this.opts.embedPath) != null ? _a2 : "/embeddings";
5111
+ var _a2, _b, _c, _d, _e, _f, _g3, _h;
5112
+ const _g2 = globalThis;
5113
+ const dispatchEmbed = _g2.__retrivoraDispatchEmbedding;
5114
+ if (typeof dispatchEmbed === "function") {
5115
+ try {
5116
+ const res = await dispatchEmbed({ input: text, model: this.model }, this.apiKey);
5117
+ if ((_b = (_a2 = res == null ? void 0 : res.data) == null ? void 0 : _a2[0]) == null ? void 0 : _b.embedding) {
5118
+ return res.data[0].embedding;
5119
+ }
5120
+ } catch (dispatchErr) {
5121
+ }
5122
+ }
5123
+ const path2 = (_c = this.opts.embedPath) != null ? _c : "/embeddings";
4993
5124
  const payload = this.opts.embedPayloadTemplate ? buildPayload(this.opts.embedPayloadTemplate, { input: text, model: this.model }) : { input: text, model: this.model };
4994
5125
  try {
4995
5126
  const { data } = await this.http.post(path2, payload);
4996
- const extractPath = (_b = this.opts.embedExtractPath) != null ? _b : "data[0].embedding";
5127
+ const extractPath = (_d = this.opts.embedExtractPath) != null ? _d : "data[0].embedding";
4997
5128
  const vector = resolvePath(data, extractPath);
4998
5129
  if (Array.isArray(vector)) {
4999
5130
  return vector;
5000
5131
  }
5001
- console.warn(
5002
- `[UniversalLLMAdapter] Embedding API returned unexpected format for path '${extractPath}'. Response:`,
5003
- data
5004
- );
5005
5132
  } catch (err) {
5006
- console.warn(
5007
- `[UniversalLLMAdapter] HTTP embedding request failed: ${(err == null ? void 0 : err.message) || err}`,
5008
- { path: path2, model: this.model, response: (_c = err == null ? void 0 : err.response) == null ? void 0 : _c.data }
5009
- );
5010
- const _g3 = globalThis;
5011
- const dispatchEmbed = _g3.__retrivoraDispatchEmbedding;
5012
5133
  if (typeof dispatchEmbed === "function") {
5013
5134
  try {
5014
5135
  const res = await dispatchEmbed({ input: text, model: this.model }, this.apiKey);
5015
- if ((_e = (_d = res == null ? void 0 : res.data) == null ? void 0 : _d[0]) == null ? void 0 : _e.embedding) {
5136
+ if ((_f = (_e = res == null ? void 0 : res.data) == null ? void 0 : _e[0]) == null ? void 0 : _f.embedding) {
5016
5137
  return res.data[0].embedding;
5017
5138
  }
5018
5139
  } catch (dispatchErr) {
5019
5140
  console.warn("[UniversalLLMAdapter] In-process dispatch embedding failed:", dispatchErr);
5020
5141
  }
5021
5142
  }
5022
- }
5023
- try {
5024
- const { data } = await this.http.post(path2, payload);
5025
- const extractPath = (_f = this.opts.embedExtractPath) != null ? _f : "data[0].embedding";
5026
- const vector = resolvePath(data, extractPath);
5027
- if (Array.isArray(vector)) {
5028
- return vector;
5029
- }
5030
- console.error(
5031
- `[UniversalLLMAdapter] Retry embedding failed: unexpected response format for path '${extractPath}'. Response:`,
5032
- data
5033
- );
5034
- throw new Error(
5035
- `[UniversalLLMAdapter] Could not extract embedding vector. Expected array at '${extractPath}', got: ${typeof vector}. Response keys: ${Object.keys(data || {})}.`
5036
- );
5037
- } catch (err) {
5038
5143
  console.error(
5039
- `[UniversalLLMAdapter] Both embedding attempts failed. Path: ${path2}, Model: ${this.model}`,
5040
- { error: err == null ? void 0 : err.message, statusCode: (_g2 = err == null ? void 0 : err.response) == null ? void 0 : _g2.status, response: (_h = err == null ? void 0 : err.response) == null ? void 0 : _h.data }
5144
+ `[UniversalLLMAdapter] Embedding failed. Path: ${path2}, Model: ${this.model}`,
5145
+ { error: err == null ? void 0 : err.message, statusCode: (_g3 = err == null ? void 0 : err.response) == null ? void 0 : _g3.status, response: (_h = err == null ? void 0 : err.response) == null ? void 0 : _h.data }
5041
5146
  );
5042
5147
  throw new Error(
5043
- `[UniversalLLMAdapter] Failed to generate embedding after 2 attempts: ${(err == null ? void 0 : err.message) || err}`
5148
+ `[UniversalLLMAdapter] Failed to generate embedding: ${(err == null ? void 0 : err.message) || err}`
5044
5149
  );
5045
5150
  }
5151
+ throw new Error(`[UniversalLLMAdapter] Failed to extract embedding vector.`);
5046
5152
  }
5047
5153
  async batchEmbed(texts) {
5048
- const results = [];
5049
- for (const text of texts) {
5154
+ var _a2;
5155
+ if (!texts || texts.length === 0) return [];
5156
+ const _g2 = globalThis;
5157
+ const dispatchEmbed = _g2.__retrivoraDispatchEmbedding;
5158
+ if (typeof dispatchEmbed === "function") {
5050
5159
  try {
5051
- const vector = await this.embed(text);
5052
- results.push(vector);
5053
- } catch (err) {
5054
- console.warn(`[UniversalLLMAdapter] batchEmbed failed for text (first 50 chars): ${text.slice(0, 50)}...`, err);
5055
- throw err;
5160
+ const res = await dispatchEmbed({ input: texts, model: this.model }, this.apiKey);
5161
+ if ((res == null ? void 0 : res.data) && Array.isArray(res.data) && res.data.length === texts.length) {
5162
+ return res.data.map((d) => d.embedding);
5163
+ }
5164
+ } catch (e) {
5165
+ }
5166
+ }
5167
+ const path2 = (_a2 = this.opts.embedPath) != null ? _a2 : "/embeddings";
5168
+ try {
5169
+ const { data } = await this.http.post(path2, { input: texts, model: this.model });
5170
+ if ((data == null ? void 0 : data.data) && Array.isArray(data.data) && data.data.length === texts.length) {
5171
+ return data.data.map((d) => d.embedding);
5056
5172
  }
5173
+ } catch (e) {
5174
+ }
5175
+ const results = [];
5176
+ for (const text of texts) {
5177
+ const vector = await this.embed(text);
5178
+ results.push(vector);
5057
5179
  }
5058
5180
  return results;
5059
5181
  }
@@ -5587,9 +5709,6 @@ var ConfigValidator = class {
5587
5709
  }
5588
5710
  };
5589
5711
 
5590
- // src/core/Pipeline.ts
5591
- init_version();
5592
-
5593
5712
  // src/rag/DocumentChunker.ts
5594
5713
  var DocumentChunker = class {
5595
5714
  constructor(chunkSize = 1e3, chunkOverlap = 200, separators = ["\n# ", "\n## ", "\n### ", "\n#### ", "\n\n", "\n", " ", ""]) {
@@ -6532,6 +6651,73 @@ ${error instanceof Error ? error.message : String(error)}`
6532
6651
  }
6533
6652
  return String((_c = (_b = response == null ? void 0 : response.output) != null ? _b : response) != null ? _c : "");
6534
6653
  }
6654
+ /**
6655
+ * Run the agentic flow in streaming mode, emitting granular SSE event frames.
6656
+ * Leverages LangChain/LangGraph `streamEvents` (v2 API) to stream:
6657
+ * - Token-by-token LLM output (`type: "text"`)
6658
+ * - Tool invocation / thinking steps (`type: "thinking"`)
6659
+ * - Tool execution metadata & search sources
6660
+ */
6661
+ streamRun(_0) {
6662
+ return __asyncGenerator(this, arguments, function* (input, chatHistory = []) {
6663
+ var _a2;
6664
+ if (!this.agent) {
6665
+ throw new Error("[LangChainAgent] Agent not initialized. Call initialize() first.");
6666
+ }
6667
+ const { HumanMessage, AIMessage } = yield new __await(import("@langchain/core/messages"));
6668
+ const historyMessages = chatHistory.map(
6669
+ (m) => m.role === "user" ? new HumanMessage(m.content) : new AIMessage(m.content)
6670
+ );
6671
+ const inputMessages = [...historyMessages, new HumanMessage(input)];
6672
+ if (typeof this.agent.streamEvents === "function") {
6673
+ const eventStream = this.agent.streamEvents(
6674
+ { messages: inputMessages },
6675
+ { version: "v2" }
6676
+ );
6677
+ try {
6678
+ for (var iter = __forAwait(eventStream), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
6679
+ const event = temp.value;
6680
+ if (event.event === "on_tool_start") {
6681
+ const toolName = event.name || "document_search";
6682
+ yield {
6683
+ type: "thinking",
6684
+ text: `Analyzing repository with tool "${toolName}"...
6685
+ `
6686
+ };
6687
+ } else if (event.event === "on_tool_end") {
6688
+ const toolName = event.name || "document_search";
6689
+ yield {
6690
+ type: "thinking",
6691
+ text: `Tool "${toolName}" completed successfully.
6692
+ `
6693
+ };
6694
+ } else if (event.event === "on_chat_model_stream") {
6695
+ const chunk = (_a2 = event.data) == null ? void 0 : _a2.chunk;
6696
+ if (chunk == null ? void 0 : chunk.content) {
6697
+ const tokenText = typeof chunk.content === "string" ? chunk.content : JSON.stringify(chunk.content);
6698
+ yield {
6699
+ type: "text",
6700
+ text: tokenText
6701
+ };
6702
+ }
6703
+ }
6704
+ }
6705
+ } catch (temp) {
6706
+ error = [temp];
6707
+ } finally {
6708
+ try {
6709
+ more && (temp = iter.return) && (yield new __await(temp.call(iter)));
6710
+ } finally {
6711
+ if (error)
6712
+ throw error[0];
6713
+ }
6714
+ }
6715
+ } else {
6716
+ const result = yield new __await(this.run(input, chatHistory));
6717
+ yield { type: "text", text: result };
6718
+ }
6719
+ });
6720
+ }
6535
6721
  };
6536
6722
 
6537
6723
  // src/rag/PromptOptimizer.ts
@@ -6783,7 +6969,7 @@ var MCPClient = class {
6783
6969
  }
6784
6970
  const args = this.config.args || [];
6785
6971
  const env = __spreadValues(__spreadValues({}, process.env), this.config.env || {});
6786
- this.childProcess = (0, import_child_process.spawn)(cmd, args, { env, shell: true });
6972
+ this.childProcess = (0, import_child_process.spawn)(cmd, args, { env });
6787
6973
  if (!this.childProcess || !this.childProcess.stdout || !this.childProcess.stdin) {
6788
6974
  throw new Error(`[MCPClient] Failed to spawn stdio child process for "${this.config.name}"`);
6789
6975
  }
@@ -6827,7 +7013,11 @@ var MCPClient = class {
6827
7013
  if (!this.config.url) {
6828
7014
  throw new Error(`[MCPClient] URL is required for sse transport on "${this.config.name}"`);
6829
7015
  }
6830
- this.sseUrl = this.config.url;
7016
+ const parsed = new URL(this.config.url);
7017
+ if (parsed.protocol !== "https:" && parsed.protocol !== "http:") {
7018
+ throw new Error(`[MCPClient] Invalid SSE URL protocol: ${parsed.protocol}`);
7019
+ }
7020
+ this.sseUrl = parsed.toString();
6831
7021
  }
6832
7022
  async sendJsonRpc(method, params) {
6833
7023
  const id = this.getNextMessageId();
@@ -6842,7 +7032,16 @@ var MCPClient = class {
6842
7032
  throw new Error(`[MCPClient] Server "${this.config.name}" is not connected (stdio process missing)`);
6843
7033
  }
6844
7034
  return new Promise((resolve, reject) => {
6845
- this.pendingRequests.set(id, { resolve, reject });
7035
+ const timer = setTimeout(() => {
7036
+ if (this.pendingRequests.delete(id)) reject(new Error(`[MCPClient] Request ${method} (id=${id}) timed out after 30000ms`));
7037
+ }, 3e4);
7038
+ this.pendingRequests.set(id, { resolve: (val) => {
7039
+ clearTimeout(timer);
7040
+ resolve(val);
7041
+ }, reject: (err) => {
7042
+ clearTimeout(timer);
7043
+ reject(err);
7044
+ } });
6846
7045
  this.childProcess.stdin.write(JSON.stringify(request) + "\n", "utf-8");
6847
7046
  });
6848
7047
  } else {
@@ -7181,7 +7380,7 @@ Once you have gathered enough information to fully satisfy the user query, write
7181
7380
  try {
7182
7381
  if (toolName === "document_search") {
7183
7382
  const query = String((_a2 = toolArgs.query) != null ? _a2 : "");
7184
- const { sources } = yield new __await(this.retriever.retrieve(query));
7383
+ const { sources } = yield new __await(this.retriever.retrieve(query, { namespace: typeof toolArgs.namespace === "string" ? toolArgs.namespace : void 0 }));
7185
7384
  const snippets = sources.slice(0, 5).map((s, i) => {
7186
7385
  var _a3;
7187
7386
  return `[${i + 1}] ${String((_a3 = s.content) != null ? _a3 : "").slice(0, 400)}`;
@@ -7439,25 +7638,43 @@ ${errorMessages}`
7439
7638
  * Processes up to `concurrency` items at the same time.
7440
7639
  */
7441
7640
  static async processConcurrent(items, processor, concurrency = 5, options) {
7641
+ var _a2, _b, _c, _d;
7442
7642
  const { throwOnPartialFailure = false } = options != null ? options : {};
7443
7643
  const results = [];
7444
7644
  const errors = [];
7645
+ const maxRetries = (_a2 = options == null ? void 0 : options.maxRetries) != null ? _a2 : 3;
7646
+ const initialDelayMs = (_b = options == null ? void 0 : options.initialDelayMs) != null ? _b : 100;
7647
+ const maxDelayMs = (_c = options == null ? void 0 : options.maxDelayMs) != null ? _c : 1e4;
7648
+ const backoffMultiplier = (_d = options == null ? void 0 : options.backoffMultiplier) != null ? _d : 2;
7445
7649
  for (let i = 0; i < items.length; i += concurrency) {
7446
7650
  const chunk = items.slice(i, i + concurrency).map((item, idx) => ({
7447
7651
  item,
7448
7652
  originalIndex: i + idx
7449
7653
  }));
7450
7654
  const chunkPromises = chunk.map(async ({ item, originalIndex }) => {
7451
- try {
7452
- const result = await _BatchProcessor.executeWithCircuitBreaker(
7453
- () => processor(item),
7454
- options == null ? void 0 : options.circuitBreaker
7455
- );
7456
- return { success: true, result, index: originalIndex };
7457
- } catch (err) {
7458
- const error = err instanceof Error ? err : new Error(String(err));
7459
- return { success: false, error, index: originalIndex };
7655
+ let lastErr;
7656
+ for (let attempt = 0; attempt <= maxRetries; attempt++) {
7657
+ try {
7658
+ const result = await _BatchProcessor.executeWithCircuitBreaker(
7659
+ () => processor(item),
7660
+ options == null ? void 0 : options.circuitBreaker
7661
+ );
7662
+ return { success: true, result, index: originalIndex };
7663
+ } catch (err) {
7664
+ lastErr = err instanceof Error ? err : new Error(String(err));
7665
+ if (!isTransientError(err) || attempt === maxRetries) {
7666
+ break;
7667
+ }
7668
+ const delay = calculateBackoffDelay(
7669
+ attempt,
7670
+ initialDelayMs,
7671
+ maxDelayMs,
7672
+ backoffMultiplier
7673
+ );
7674
+ await sleep(delay);
7675
+ }
7460
7676
  }
7677
+ return { success: false, error: lastErr || new Error("Unknown error"), index: originalIndex };
7461
7678
  });
7462
7679
  const chunkResults = await Promise.all(chunkPromises);
7463
7680
  for (const { success, result, error, index } of chunkResults) {
@@ -7644,7 +7861,8 @@ var QueryProcessor = class {
7644
7861
  }
7645
7862
  if (validFields.length > 0) {
7646
7863
  for (const field of validFields) {
7647
- const pattern = new RegExp(`\\b${field}\\s*(?:is|are|:|of)?\\s+["']?([^"\\n?.!,]{1,60})["']?(?=[?.!,]|$)`, "gi");
7864
+ const escapedField = field.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
7865
+ const pattern = new RegExp(`\\b${escapedField}\\s*(?:is|are|:|of)?\\s+["']?([^"\\n?.!,]{1,60})["']?(?=[?.!,]|$)`, "gi");
7648
7866
  for (const match of question.matchAll(pattern)) {
7649
7867
  const value = match[1];
7650
7868
  if (value && !this.isLikelyPromptPhrase(value)) {
@@ -7930,14 +8148,14 @@ var IntentClassifier = class {
7930
8148
  static classify(context) {
7931
8149
  const q = (context.userQuery || "").toLowerCase().trim();
7932
8150
  const signals = this.extractDataSignals(context);
7933
- if (this.isInformationLookup(q)) {
7934
- return { intent: "information_lookup", signals };
8151
+ if (this.isProductQuery(q) || signals.isProductLike && (this.isRecommendationQuery(q) || this.isDiscoveryQuery(q))) {
8152
+ return { intent: "product_search", signals };
7935
8153
  }
7936
8154
  if (this.isComparisonQuery(q)) {
7937
8155
  return { intent: "comparison", signals };
7938
8156
  }
7939
- if (this.isProductQuery(q) || signals.isProductLike && this.isRecommendationQuery(q)) {
7940
- return { intent: "product_search", signals };
8157
+ if (this.isInformationLookup(q)) {
8158
+ return { intent: "information_lookup", signals };
7941
8159
  }
7942
8160
  if (this.isTrendQuery(q) || signals.hasDateFields && signals.hasNumericFields && /\b(growth|trend|over time|monthly|yearly|weekly)\b/.test(q)) {
7943
8161
  return { intent: "trend_analysis", signals };
@@ -7954,7 +8172,8 @@ var IntentClassifier = class {
7954
8172
  return { intent: "information_lookup", signals };
7955
8173
  }
7956
8174
  /**
7957
- * Extract key data structure signals from retrieved documents.
8175
+ * Extract key data structure signals dynamically from retrieved documents.
8176
+ * Inspects both structured metadata and parsed chunk text.
7958
8177
  */
7959
8178
  static extractDataSignals(context) {
7960
8179
  const docs = context.retrievedDocuments || [];
@@ -7987,18 +8206,21 @@ var IntentClassifier = class {
7987
8206
  let categoricalFieldCount = 0;
7988
8207
  if (docs.length > 0) {
7989
8208
  const keys = /* @__PURE__ */ new Set();
7990
- let numericKeys = /* @__PURE__ */ new Set();
7991
- let dateKeys = /* @__PURE__ */ new Set();
7992
- let catKeys = /* @__PURE__ */ new Set();
7993
- let productKeyMatches = 0;
8209
+ const numericKeys = /* @__PURE__ */ new Set();
8210
+ const dateKeys = /* @__PURE__ */ new Set();
8211
+ const catKeys = /* @__PURE__ */ new Set();
8212
+ let productDocCount = 0;
7994
8213
  docs.forEach((doc) => {
8214
+ if (!doc) return;
7995
8215
  const meta = doc.metadata || {};
8216
+ const content = doc.content || "";
8217
+ let hasDocProductSignal = false;
7996
8218
  Object.entries(meta).forEach(([key, val]) => {
7997
8219
  if (IGNORED_SYSTEM_KEYS.has(key.toLowerCase())) return;
7998
8220
  keys.add(key);
7999
8221
  const kLower = key.toLowerCase();
8000
- if (["price", "image", "brand", "in_stock", "rating", "sku", "product"].some((p) => kLower.includes(p))) {
8001
- productKeyMatches++;
8222
+ if (["price", "image", "brand", "in_stock", "rating", "sku", "product", "item", "cost", "msrp", "category", "vendor", "inventory", "stock"].some((p) => kLower.includes(p))) {
8223
+ hasDocProductSignal = true;
8002
8224
  }
8003
8225
  if (typeof val === "number") {
8004
8226
  numericKeys.add(key);
@@ -8010,13 +8232,23 @@ var IntentClassifier = class {
8010
8232
  }
8011
8233
  }
8012
8234
  });
8235
+ if (!hasDocProductSignal && content) {
8236
+ const hasContentName = /(?:^|\n)\s*(?:product\s*name|item\s*name|product|item|title|name)\s*[:\-–]\s*[^\n,]+/i.test(content);
8237
+ const hasContentAttr = /(?:^|\n)\s*(?:price|cost|msrp|brand|vendor|sku|category|stock|inventory|rating|image)\s*[:\-–]/i.test(content) || /\$\s*[\d,.]+/.test(content);
8238
+ if (hasContentName && hasContentAttr) {
8239
+ hasDocProductSignal = true;
8240
+ }
8241
+ }
8242
+ if (hasDocProductSignal) {
8243
+ productDocCount++;
8244
+ }
8013
8245
  });
8014
8246
  hasNumericFields = numericKeys.size > 0;
8015
8247
  hasDateFields = dateKeys.size > 0;
8016
8248
  hasCategoricalFields = catKeys.size > 0;
8017
8249
  numericFieldCount = numericKeys.size;
8018
8250
  categoricalFieldCount = catKeys.size;
8019
- if (productKeyMatches >= 2) {
8251
+ if (productDocCount > 0 && (productDocCount / docs.length >= 0.3 || productDocCount >= 2)) {
8020
8252
  isProductLike = true;
8021
8253
  }
8022
8254
  }
@@ -8031,8 +8263,15 @@ var IntentClassifier = class {
8031
8263
  };
8032
8264
  }
8033
8265
  // ─── Intent Heuristic Checkers ─────────────────────────────────────────────
8266
+ static isDiscoveryQuery(query) {
8267
+ const q = query.toLowerCase().trim();
8268
+ return /\b(show|find|list|browse|search|explore|get|buy|shop|shopping|purchase|options|selection|picks|available|what do you have|what is in stock|looking for|under \$|cheap|affordable|best|top)\b/i.test(q) && !/\b(chart|graph|plot|sales over time|monthly trend)\b/i.test(q);
8269
+ }
8034
8270
  static isInformationLookup(query) {
8035
8271
  const q = query.toLowerCase().trim();
8272
+ if (/\b(show|find|list|browse|recommend|suggest|available|in stock|buy|shop|catalog|inventory|what do you have|looking for)\b/i.test(q)) {
8273
+ return false;
8274
+ }
8036
8275
  if (/^(what|who|explain|explore|define|where|when|why|how|how does|can|could|does|do|is|are|has|have|will|should|would|meaning of|tell me about|show details of|overview of|summarize)\s+/i.test(q)) {
8037
8276
  if (!/\b(chart|graph|plot|visualize|pie chart|bar chart|line chart|scatter plot|draw a)\b/i.test(q)) {
8038
8277
  return true;
@@ -8047,7 +8286,11 @@ var IntentClassifier = class {
8047
8286
  return /\b(compare|comparison|vs\.?|versus|difference between|differ|contrast|against)\b/i.test(query);
8048
8287
  }
8049
8288
  static isProductQuery(query) {
8050
- return /\b(best|recommend|top|buy|shopping|under \$|cheap|shoes|laptops|chairs|headphones|phone|sneakers|apparel|products)\b/i.test(query) && !/\b(compare|chart|sales|revenue|trend)\b/i.test(query);
8289
+ const q = query.toLowerCase().trim();
8290
+ if (/\b(price per share|share price|total cost of the deal|debt|equity|ratio|revenue|ebit|ebitda|fcfe|fcff|wacc)\b/i.test(q)) {
8291
+ return false;
8292
+ }
8293
+ return /\b(best|recommend|top|buy|shopping|shop|under \$|cheap|affordable|products?|items?|catalog|inventory|in stock|show available|what do you have|options for)\b/i.test(q) && !/\b(compare|chart|sales|revenue|trend)\b/i.test(q);
8051
8294
  }
8052
8295
  static isRecommendationQuery(query) {
8053
8296
  return /\b(recommend|suggest|what should i (buy|get|choose)|best options|top picks)\b/i.test(query);
@@ -8112,14 +8355,23 @@ var Rule3ProductDiscoveryRule = class {
8112
8355
  }
8113
8356
  evaluate(context, intent) {
8114
8357
  const q = (context.userQuery || "").toLowerCase().trim();
8115
- const isDiscovery = intent === "product_search" || intent === "recommendation" || /\b(best|recommend|top|buy|shopping|under \$|cheap|shoes|laptops|chairs|headphones|sneakers|apparel|products|show available)\b/i.test(q);
8116
- const isNotChart = !/\b(chart|sales|revenue|monthly|growth|trend|over time)\b/i.test(q);
8117
- const isAnalyticalRanking = /\b(top performing|top selling|best performing|performing|sales|revenue|ranking)\b/i.test(q);
8118
- if (isDiscovery && isNotChart && !isAnalyticalRanking) {
8358
+ const isExplicitChart = /\b(chart|graph|plot|sales over time|monthly growth|revenue trend|pie chart|bar chart|line chart|histogram)\b/i.test(q);
8359
+ if (isExplicitChart) {
8360
+ return null;
8361
+ }
8362
+ const isFinancialReport = /\b(price per share|share price|debt to equity|ebitda|fcfe|fcff|wacc|beta|balance sheet|cash flow statement)\b/i.test(q);
8363
+ if (isFinancialReport) {
8364
+ return null;
8365
+ }
8366
+ const signals = IntentClassifier.extractDataSignals(context);
8367
+ const hasDiscoveryAction = intent === "product_search" || intent === "recommendation" || /\b(recommend|recommendation|suggest|show|find|list|browse|search|explore|get|buy|shop|shopping|purchase|options|selection|picks|available|what do you have|what is in stock|looking for|under \$|cheap|affordable|best|top|products?|items?|catalog|inventory)\b/i.test(q);
8368
+ const isAnalyticalRanking = /\b(top performing|top selling|best performing|performing|sales revenue|revenue ranking)\b/i.test(q);
8369
+ const isGenericDocumentQuery = /\b(documents? in system|all documents|system documents|overall documents)\b/i.test(q);
8370
+ if (!isAnalyticalRanking && !isGenericDocumentQuery && (signals.isProductLike && hasDiscoveryAction || intent === "product_search" && !isAnalyticalRanking)) {
8119
8371
  return {
8120
8372
  renderType: "carousel",
8121
- confidence: 0.9,
8122
- reason: "Rule 3: Product discovery/recommendation query requires product carousel rendering."
8373
+ confidence: 0.92,
8374
+ reason: "Rule 3: Dynamically identified product catalog entities and discovery intent."
8123
8375
  };
8124
8376
  }
8125
8377
  return null;
@@ -8590,11 +8842,11 @@ var UITransformer = class _UITransformer {
8590
8842
  if (["tabular", "table"].includes(resolvedIntent.visualizationHint) || resolvedIntent.wantsExplicitTable) {
8591
8843
  return this.hasMultipleFields(filteredData) ? this.transformToTable(filteredData, userQuery) : this.transformToText(filteredData);
8592
8844
  }
8593
- if ((hasProducts || this.isProductQuery(userQuery)) && resolvedIntent.visualizationHint === "product_browse") {
8594
- if (!this.isProductQuery(userQuery)) {
8595
- return this.transformToText(filteredData);
8845
+ if (hasProducts || this.isProductQuery(userQuery) || resolvedIntent.visualizationHint === "product_browse") {
8846
+ const carousel = this.transformToProductCarousel(filteredData, config, trainedSchema);
8847
+ if (Array.isArray(carousel.data) && carousel.data.length > 0) {
8848
+ return carousel;
8596
8849
  }
8597
- return this.transformToProductCarousel(filteredData, config, trainedSchema);
8598
8850
  }
8599
8851
  const automatic = this.chooseAutomaticVisualization(filteredData, profile, userQuery);
8600
8852
  if (automatic) return automatic;
@@ -9231,15 +9483,17 @@ ${schemaProfileText}` : ""}`;
9231
9483
  return _UITransformer._productQueryTest(query);
9232
9484
  }
9233
9485
  static _productQueryTest(query) {
9234
- const q = query.toLowerCase();
9486
+ const q = query.toLowerCase().trim();
9235
9487
  const financialTerms = /\b(price per share|share price|total cost of the deal|cost of the deal|debt|equity|ratio|revenue|ebit|ebitda|tax|depreciation|interest rate|cashflow|cash flow|fcfe|fcff|wacc|beta|working capital|cogs|capital spending|valuation|leverage)\b/;
9236
9488
  if (financialTerms.test(q)) {
9237
9489
  return false;
9238
9490
  }
9239
- const productTerms = /\b(product|products|item|items|sku|catalog|catalogue|brand|model|stock|inventory|in stock|out of stock|buy|shop)\b/.test(q);
9240
- const productAction = /\b(recommend|suggest|describe|description|detail|details|about|show|find|search|browse|list|explore|exploring)\b/.test(q);
9241
9491
  const nonProductEntityTerms = /\b(organization|organisations?|organizations?|company|companies|employee|employees|staff|headcount|workforce)\b/.test(q);
9242
- return productTerms && productAction && !nonProductEntityTerms;
9492
+ if (nonProductEntityTerms) {
9493
+ return false;
9494
+ }
9495
+ const genericDiscoveryPatterns = /\b(product|products|item|items|sku|catalog|catalogue|brand|model|stock|inventory|in stock|out of stock|buy|shop|shopping|purchase|order|deals|recommend|recommendation|suggestions|show available|what do you have|available|looking for|options for|under \$|cheap|affordable|best|top|find me|browse)\b/i;
9496
+ return genericDiscoveryPatterns.test(q);
9243
9497
  }
9244
9498
  static isProductData(item) {
9245
9499
  if (!item) return false;
@@ -9256,10 +9510,17 @@ ${schemaProfileText}` : ""}`;
9256
9510
  if (financialTerms.test(content)) {
9257
9511
  return false;
9258
9512
  }
9259
- const hasSku = Boolean(resolveMetadataValue(metadata, "sku") || resolveMetadataValue(metadata, "product_id"));
9260
- const hasProductIdentity = Boolean(resolveMetadataValue(metadata, "product_name") || resolveMetadataValue(metadata, "brand") && resolveMetadataValue(metadata, "name"));
9261
- const hasPrice = Boolean(resolveMetadataValue(metadata, "retail_price") || resolveMetadataValue(metadata, "unit_price") || resolveMetadataValue(metadata, "msrp"));
9262
- return hasSku || hasProductIdentity && hasPrice;
9513
+ const hasSku = Boolean(resolveMetadataValue(metadata, "sku") || resolveMetadataValue(metadata, "product_id") || resolveMetadataValue(metadata, "item_id"));
9514
+ const hasProductIdentity = Boolean(resolveMetadataValue(metadata, "product_name") || resolveMetadataValue(metadata, "item_name") || resolveMetadataValue(metadata, "brand") && resolveMetadataValue(metadata, "name"));
9515
+ const hasPrice = Boolean(resolveMetadataValue(metadata, "price") || resolveMetadataValue(metadata, "retail_price") || resolveMetadataValue(metadata, "unit_price") || resolveMetadataValue(metadata, "msrp"));
9516
+ if (hasSku || hasProductIdentity && hasPrice || hasProductIdentity) {
9517
+ return true;
9518
+ }
9519
+ const hasContentName = /(?:^|\n)\s*(?:product\s*name|item\s*name|product|item|title|name)\s*[:\-–]\s*[^\n,]+/i.test(item.content || "");
9520
+ const hasContentPrice = /(?:^|\n)\s*(?:price|retail\s*price|unit\s*price|cost|msrp)\s*[:\-–]?\s*\$?\s*[\d,.]+/i.test(item.content || "");
9521
+ const hasContentCategory = /(?:^|\n)\s*(?:category|department|collection)\s*[:\-–]\s*[^\n,]+/i.test(item.content || "");
9522
+ const hasContentBrand = /(?:^|\n)\s*(?:brand|manufacturer|vendor)\s*[:\-–]\s*[^\n,]+/i.test(item.content || "");
9523
+ return hasContentName && (hasContentPrice || hasContentCategory || hasContentBrand);
9263
9524
  }
9264
9525
  static isTimeSeriesData(item) {
9265
9526
  const metadata = item.metadata || {};
@@ -9749,64 +10010,46 @@ ${schemaProfileText}` : ""}`;
9749
10010
  return resolveMetadataValue(meta, uiKey);
9750
10011
  }
9751
10012
  static extractProductInfo(item, config, trainedSchema) {
9752
- var _a2, _b;
10013
+ var _a2, _b, _c;
9753
10014
  if (!item) return null;
9754
10015
  const meta = item.metadata || {};
9755
10016
  const content = item.content || "";
9756
- const name = this.getDynamicVal(meta, "name", config, trainedSchema);
9757
- const price = this.getDynamicVal(meta, "price", config, trainedSchema);
9758
- const brand = this.getDynamicVal(meta, "brand", config, trainedSchema);
10017
+ let name = this.getDynamicVal(meta, "name", config, trainedSchema);
10018
+ let price = this.getDynamicVal(meta, "price", config, trainedSchema);
10019
+ let brand = this.getDynamicVal(meta, "brand", config, trainedSchema);
9759
10020
  const description = this.cleanProductDescription(
9760
10021
  (_a2 = this.extractProductDescriptionFromContent(content)) != null ? _a2 : this.getProductDescriptionValue(meta, config, trainedSchema)
9761
10022
  );
9762
- if (name || this.isProductData(item)) {
9763
- let finalName = name ? String(name) : void 0;
9764
- if (!finalName && content) {
9765
- const nameMatch = content.match(/(?:Product|Item|Name):\s*([^\n]+)/i);
9766
- finalName = nameMatch ? nameMatch[1].trim() : (_b = content.split("\n")[0]) == null ? void 0 : _b.substring(0, 60);
9767
- }
9768
- if (!finalName) {
9769
- console.warn(
9770
- `[UITransformer] Unable to read product name from metadata or content for item ${item.id}. Defaulting to 'Unnamed Product'.`,
9771
- { metadataKeys: Object.keys(meta), contentLength: content.length }
9772
- );
10023
+ if (!name && content) {
10024
+ const nameMatch = content.match(/(?:^|\n)\s*(?:product\s*name|item\s*name|product|item|title|name)\s*[:\-–]\s*([^\n,]+)/i) || content.match(/(?:Product|Item|Name):\s*([^\n]+)/i);
10025
+ if (nameMatch && nameMatch[1]) {
10026
+ name = nameMatch[1].trim();
9773
10027
  }
9774
- let finalPrice = typeof price === "number" || typeof price === "string" ? price : void 0;
9775
- if (!finalPrice && content) {
9776
- const priceMatch = content.match(/(?:Price|Cost):\s*\$?([\d,.]+)/i) || content.match(/\$\s*([\d,.]+)/);
9777
- if (priceMatch) finalPrice = priceMatch[1].replace(/,/g, "");
9778
- }
9779
- if (finalPrice === void 0) {
9780
- console.warn(
9781
- `[UITransformer] Unable to read product price from metadata or content for item ${item.id}.`,
9782
- { metadataKeys: Object.keys(meta), contentSnippet: content.substring(0, 100) }
9783
- );
9784
- }
9785
- const imageValue = this.getDynamicVal(meta, "image", config, trainedSchema);
9786
- if (!imageValue) {
9787
- console.debug(
9788
- `[UITransformer] Unable to read product image URL from metadata for item ${item.id}.`
9789
- );
10028
+ }
10029
+ if (!brand && content) {
10030
+ const brandMatch = content.match(/(?:^|\n)\s*(?:brand|manufacturer|vendor)\s*[:\-–]\s*([^\n,]+)/i);
10031
+ if (brandMatch && brandMatch[1]) {
10032
+ brand = brandMatch[1].trim();
9790
10033
  }
9791
- if (!description) {
9792
- console.debug(
9793
- `[UITransformer] Unable to read product description from metadata or content for item ${item.id}.`
9794
- );
10034
+ }
10035
+ if (price === void 0 && content) {
10036
+ const priceMatch = content.match(/(?:^|\n)\s*(?:price|retail\s*price|unit\s*price|cost|msrp)\s*[:\-–]?\s*\$?\s*([\d,.]+)/i) || content.match(/(?:Price|Cost):\s*\$?([\d,.]+)/i) || content.match(/\$\s*([\d,.]+)/);
10037
+ if (priceMatch && priceMatch[1]) {
10038
+ price = priceMatch[1].replace(/,/g, "");
9795
10039
  }
10040
+ }
10041
+ if (name || this.isProductData(item)) {
10042
+ const finalName = name ? String(name) : ((_b = content.split("\n")[0]) == null ? void 0 : _b.substring(0, 60)) || "Unnamed Product";
10043
+ const imageValue = this.getDynamicVal(meta, "image", config, trainedSchema) || ((_c = content.match(/(?:^|\n)\s*(?:image|image\s*url|photo|thumbnail)\s*[:\-–]\s*(https?:\/\/[^\s\n,]+)/i)) == null ? void 0 : _c[1]);
9796
10044
  return {
9797
10045
  id: item.id,
9798
- name: finalName || "Unnamed Product",
9799
- price: finalPrice,
10046
+ name: finalName,
10047
+ price: typeof price === "number" || typeof price === "string" ? price : void 0,
9800
10048
  image: typeof imageValue === "string" ? imageValue : void 0,
9801
10049
  brand: brand ? String(brand) : void 0,
9802
10050
  description,
9803
10051
  inStock: this.determineStockStatus(item)
9804
10052
  };
9805
- } else {
9806
- console.warn(
9807
- `[UITransformer] Item ${item.id} skipped: unable to read name or identify as product data from metadata/content.`,
9808
- { metadataKeys: Object.keys(meta), contentSnippet: content.substring(0, 100) }
9809
- );
9810
10053
  }
9811
10054
  return null;
9812
10055
  }
@@ -10181,6 +10424,17 @@ var Pipeline = class {
10181
10424
  - If answer cannot be found in the sources, say that you cannot answer the question and do not hallucinate.
10182
10425
  - Do not use information from previous turns to answer the question.
10183
10426
  - You CAN use numbers for years and counts (e.g., 2006, 5800). But NEVER put a dollar sign ($) before non-monetary numbers like Stock quantities, EAN numbers, or years. Only use dollar signs for actual monetary prices.
10427
+ - Be concise, direct, and focused. Deliver clean, high-quality answers within token limits without unnecessary filler.
10428
+
10429
+ ### CATEGORY & DOMAIN SYNONYM MATCHING:
10430
+ - Intelligently understand common category synonyms and related terms from the context:
10431
+ - If the user asks about "makeup", "cosmetics", "skincare", "toiletries", "personal care", or "beauty products", map these to the "Beauty" category in the catalog and present the matching products.
10432
+ - If the user asks about "clothes", "apparel", "wear", "shirts", "garments", or "fashion", map these to the "Clothing" category in the catalog.
10433
+ - If the user asks about "electronics", "gadgets", "tech", "devices", or "appliances", map these to the "Electronics" category.
10434
+ - If the user asks about "food", "snacks", "groceries", "beverages", or "drinks", map these to the "Grocery" category.
10435
+ - If the user asks about "office items", "stationery", "work supplies", or "desk equipment", map these to the "Office Supplies" category.
10436
+ - If the user asks about "toys", "games", "play items", or "hobbies", map these to the "Toys & Games" category.
10437
+ - Do NOT reject a query or say no items exist simply because the user used a natural synonym (e.g., "makeup") instead of the exact catalog column label (e.g., "Beauty"). Identify the relevant items from the context and present them.
10184
10438
 
10185
10439
  ### PRODUCT DISPLAY:
10186
10440
  - Recommended Products should only be displayed when the user explicitly asks for products.
@@ -10350,13 +10604,23 @@ ${m.content}`).join("\n\n---\n\n");
10350
10604
  const texts = chunks.map((c) => PIIMasker.mask(c.content));
10351
10605
  if (typeof this.embeddingProvider.batchEmbed === "function") {
10352
10606
  try {
10353
- const batchSize = 50;
10354
- const allVectors = [];
10607
+ const batchSize = 100;
10608
+ const slices = [];
10355
10609
  for (let i = 0; i < texts.length; i += batchSize) {
10356
- const slice = texts.slice(i, i + batchSize);
10357
- const batchVectors = await this.embeddingProvider.batchEmbed(slice, { taskType: "document" });
10358
- allVectors.push(...batchVectors);
10610
+ slices.push(texts.slice(i, i + batchSize));
10359
10611
  }
10612
+ const batchEmbedFn = this.embeddingProvider.batchEmbed.bind(this.embeddingProvider);
10613
+ const concurrentBatches = await BatchProcessor.processConcurrent(
10614
+ slices,
10615
+ (slice) => batchEmbedFn(slice, { taskType: "document" }),
10616
+ 5,
10617
+ {
10618
+ maxRetries: 3,
10619
+ initialDelayMs: 100,
10620
+ throwOnPartialFailure: false
10621
+ }
10622
+ );
10623
+ const allVectors = concurrentBatches.results.flat();
10360
10624
  if (allVectors.length === chunks.length) {
10361
10625
  return allVectors;
10362
10626
  }
@@ -10369,7 +10633,7 @@ ${m.content}`).join("\n\n---\n\n");
10369
10633
  const concurrentResult = await BatchProcessor.processConcurrent(
10370
10634
  texts,
10371
10635
  (text) => this.embeddingProvider.embed(text, { taskType: "document" }),
10372
- 10,
10636
+ 20,
10373
10637
  {
10374
10638
  maxRetries: 3,
10375
10639
  initialDelayMs: 100,
@@ -10387,7 +10651,7 @@ ${m.content}`).join("\n\n---\n\n");
10387
10651
  /** Step 3: Upsert chunks to vector database with retry logic. */
10388
10652
  async processUpserts(upsertDocs, namespace) {
10389
10653
  const upsertBatchOptions = {
10390
- batchSize: 100,
10654
+ batchSize: 250,
10391
10655
  maxRetries: 3,
10392
10656
  initialDelayMs: 100
10393
10657
  };
@@ -10545,7 +10809,7 @@ ${m.content}`).join("\n\n---\n\n");
10545
10809
  */
10546
10810
  askStreamInternal(_0) {
10547
10811
  return __asyncGenerator(this, arguments, function* (question, history = [], namespace) {
10548
- 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;
10812
+ 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;
10549
10813
  yield new __await(this.initialize());
10550
10814
  const ns = formatNamespace(namespace != null ? namespace : this.config.projectId);
10551
10815
  const topK = (_b = (_a2 = this.config.rag) == null ? void 0 : _a2.topK) != null ? _b : 5;
@@ -10616,7 +10880,17 @@ ${m.content}`).join("\n\n---\n\n");
10616
10880
  }
10617
10881
  const rerankMs = performance.now() - rerankStart;
10618
10882
  const totalCount = fullSources.length;
10619
- const promptSources = fullSources.slice(0, 15);
10883
+ const rawPromptSources = fullSources.slice(0, 4);
10884
+ let accumulatedChars = 0;
10885
+ const promptSources = [];
10886
+ for (const s of rawPromptSources) {
10887
+ const textLen = ((s == null ? void 0 : s.content) || "").length;
10888
+ if (accumulatedChars + textLen > 3500 && promptSources.length >= 2) {
10889
+ break;
10890
+ }
10891
+ promptSources.push(s);
10892
+ accumulatedChars += textLen;
10893
+ }
10620
10894
  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]
10621
10895
 
10622
10896
  ` + promptSources.map((m, i) => {
@@ -10694,7 +10968,7 @@ ${context}`;
10694
10968
  const isNativeThinking = isClaude37 && (((_q = this.config.llm.options) == null ? void 0 : _q.thinking) === true || ((_r = this.config.llm.options) == null ? void 0 : _r.thinking) === "enabled" || ((_s = this.config.llm.options) == null ? void 0 : _s.thinking) !== false);
10695
10969
  const modelNameLower = this.config.llm.model.toLowerCase();
10696
10970
  const isReasoningModel = modelNameLower.includes("-r1") || modelNameLower.includes("deepseek-r1") || modelNameLower.includes("reasoning") || modelNameLower.includes("thinking");
10697
- const isSimulatedThinking = !isNativeThinking && (((_t = this.config.llm.options) == null ? void 0 : _t.thinking) === true || ((_u = this.config.llm.options) == null ? void 0 : _u.thinking) === "enabled" || ((_v = this.config.llm.options) == null ? void 0 : _v.thinking) !== false);
10971
+ const isSimulatedThinking = !isNativeThinking && (((_t = this.config.llm.options) == null ? void 0 : _t.thinking) === true || ((_u = this.config.llm.options) == null ? void 0 : _u.thinking) === "enabled");
10698
10972
  let finalRestrictionSuffix = restrictionSuffix;
10699
10973
  if (isSimulatedThinking) {
10700
10974
  finalRestrictionSuffix += "\n\n(IMPORTANT: You must think step-by-step before answering. Write your thinking process inside a `<think>...</think>` block. Write the `<think>` block first, then follow it with your final response. Keep the thinking process thorough and detailed. Example: `<think>Thinking details here...</think>Final answer text here.`)";
@@ -10705,7 +10979,7 @@ ${context}`;
10705
10979
  }));
10706
10980
  const userQuestion = { role: "user", content: effectiveQuestion + finalRestrictionSuffix };
10707
10981
  const messages = [...hardenedHistory, userQuestion];
10708
- const systemPrompt = (_w = this.config.llm.systemPrompt) != null ? _w : "";
10982
+ const systemPrompt = (_v = this.config.llm.systemPrompt) != null ? _v : "";
10709
10983
  const userPrompt = messages.map((m) => `${m.role}: ${m.content}`).join("\n");
10710
10984
  let fullReply = "";
10711
10985
  let thinkingText = "";
@@ -10727,57 +11001,54 @@ ${context}`;
10727
11001
  yield { type: "thinking", text: thinkingDelta };
10728
11002
  continue;
10729
11003
  }
10730
- if (isSimulatedThinking) {
10731
- textBuffer += chunk;
10732
- while (textBuffer.length > 0) {
10733
- if (!inThinking) {
10734
- const thinkIndex = textBuffer.indexOf("<think>");
10735
- if (thinkIndex !== -1) {
10736
- const before = textBuffer.slice(0, thinkIndex);
10737
- if (before) {
10738
- fullReply += before;
10739
- yield before;
10740
- }
10741
- inThinking = true;
10742
- thinkingStartMs = performance.now();
10743
- textBuffer = textBuffer.slice(thinkIndex + "<think>".length);
10744
- } else {
10745
- const maxSafeLength = textBuffer.length - "<think>".length;
10746
- if (maxSafeLength > 0) {
10747
- const safeText = textBuffer.slice(0, maxSafeLength);
10748
- fullReply += safeText;
10749
- yield safeText;
10750
- textBuffer = textBuffer.slice(maxSafeLength);
10751
- }
10752
- break;
11004
+ textBuffer += chunk;
11005
+ while (textBuffer.length > 0) {
11006
+ if (!inThinking) {
11007
+ const thinkIndex = textBuffer.indexOf("<think>");
11008
+ if (thinkIndex !== -1) {
11009
+ const before = textBuffer.slice(0, thinkIndex);
11010
+ if (before) {
11011
+ fullReply += before;
11012
+ yield before;
10753
11013
  }
11014
+ inThinking = true;
11015
+ thinkingStartMs = performance.now();
11016
+ textBuffer = textBuffer.slice(thinkIndex + "<think>".length);
10754
11017
  } else {
10755
- const endThinkIndex = textBuffer.indexOf("</think>");
10756
- if (endThinkIndex !== -1) {
10757
- const thinkingDelta = textBuffer.slice(0, endThinkIndex);
10758
- if (thinkingDelta) {
10759
- thinkingText += thinkingDelta;
10760
- thinkingDurationMs = performance.now() - thinkingStartMs;
10761
- yield { type: "thinking", text: thinkingDelta };
10762
- }
10763
- inThinking = false;
10764
- textBuffer = textBuffer.slice(endThinkIndex + "</think>".length);
10765
- } else {
10766
- const maxSafeLength = textBuffer.length - "</think>".length;
10767
- if (maxSafeLength > 0) {
10768
- const thinkingDelta = textBuffer.slice(0, maxSafeLength);
10769
- thinkingText += thinkingDelta;
10770
- thinkingDurationMs = performance.now() - thinkingStartMs;
10771
- yield { type: "thinking", text: thinkingDelta };
10772
- textBuffer = textBuffer.slice(maxSafeLength);
10773
- }
10774
- break;
11018
+ const partialMatch = textBuffer.match(/<t?(?:h(?:i(?:n(?:k)?)?)?)?$/);
11019
+ const safeLen = partialMatch ? textBuffer.length - partialMatch[0].length : textBuffer.length;
11020
+ if (safeLen > 0) {
11021
+ const safeText = textBuffer.slice(0, safeLen);
11022
+ fullReply += safeText;
11023
+ yield safeText;
11024
+ textBuffer = textBuffer.slice(safeLen);
10775
11025
  }
11026
+ break;
11027
+ }
11028
+ } else {
11029
+ const endThinkIndex = textBuffer.indexOf("</think>");
11030
+ if (endThinkIndex !== -1) {
11031
+ const thinkingDelta = textBuffer.slice(0, endThinkIndex);
11032
+ if (thinkingDelta) {
11033
+ thinkingText += thinkingDelta;
11034
+ thinkingDurationMs = performance.now() - thinkingStartMs;
11035
+ yield { type: "thinking", text: thinkingDelta };
11036
+ }
11037
+ inThinking = false;
11038
+ textBuffer = textBuffer.slice(endThinkIndex + "</think>".length);
11039
+ } else {
11040
+ const partialMatch = textBuffer.match(/<\/?t?(?:h(?:i(?:n(?:k)?)?)?)?$/);
11041
+ const safeLen = partialMatch ? textBuffer.length - partialMatch[0].length : textBuffer.length;
11042
+ if (safeLen > 0) {
11043
+ const thinkingDelta = textBuffer.slice(0, safeLen);
11044
+ thinkingText += thinkingDelta;
11045
+ thinkingDurationMs = performance.now() - thinkingStartMs;
11046
+ yield { type: "thinking", text: thinkingDelta };
11047
+ textBuffer = textBuffer.slice(safeLen);
11048
+ }
11049
+ break;
10776
11050
  }
10777
11051
  }
10778
- } else {
10779
- fullReply += chunk;
10780
- yield chunk;
10781
11052
  }
10782
11053
  }
10783
11054
  } catch (temp) {
@@ -10790,7 +11061,7 @@ ${context}`;
10790
11061
  throw error[0];
10791
11062
  }
10792
11063
  }
10793
- if (isSimulatedThinking && textBuffer.length > 0) {
11064
+ if (textBuffer.length > 0) {
10794
11065
  if (inThinking) {
10795
11066
  thinkingText += textBuffer;
10796
11067
  yield { type: "thinking", text: textBuffer };
@@ -10801,22 +11072,21 @@ ${context}`;
10801
11072
  }
10802
11073
  } else {
10803
11074
  const reply = yield new __await(this.llmProvider.chat(messages, context));
10804
- if (isSimulatedThinking) {
10805
- const thinkStart = reply.indexOf("<think>");
10806
- const thinkEnd = reply.indexOf("</think>");
10807
- if (thinkStart !== -1 && thinkEnd !== -1 && thinkEnd > thinkStart) {
10808
- thinkingText = reply.slice(thinkStart + "<think>".length, thinkEnd);
10809
- fullReply = reply.slice(0, thinkStart) + reply.slice(thinkEnd + "</think>".length);
10810
- thinkingDurationMs = 100;
10811
- } else {
10812
- fullReply = reply;
10813
- }
11075
+ const thinkStart = reply.indexOf("<think>");
11076
+ const thinkEnd = reply.indexOf("</think>");
11077
+ if (thinkStart !== -1 && thinkEnd !== -1 && thinkEnd > thinkStart) {
11078
+ thinkingText = reply.slice(thinkStart + "<think>".length, thinkEnd);
11079
+ fullReply = reply.slice(0, thinkStart) + reply.slice(thinkEnd + "</think>".length);
11080
+ thinkingDurationMs = 100;
11081
+ } else if (thinkStart !== -1) {
11082
+ thinkingText = reply.slice(thinkStart + "<think>".length);
11083
+ fullReply = reply.slice(0, thinkStart);
10814
11084
  } else {
10815
11085
  fullReply = reply;
10816
11086
  }
10817
11087
  yield fullReply;
10818
11088
  }
10819
- const runHallucination = ((_x = this.config.llm.options) == null ? void 0 : _x.hallucinationScoring) === true;
11089
+ const runHallucination = ((_w = this.config.llm.options) == null ? void 0 : _w.hallucinationScoring) === true;
10820
11090
  if (runHallucination) {
10821
11091
  hallucinationScoringPromise = scoreHallucination(this.llmProvider, fullReply, context).catch(() => void 0);
10822
11092
  }
@@ -10838,96 +11108,45 @@ ${context}`;
10838
11108
  totalTokens: promptTokens + completionTokens,
10839
11109
  estimatedCostUsd: estimateCostUsd(promptTokens, completionTokens, this.config.llm.model)
10840
11110
  };
10841
- const awaitHallucination = ((_y = this.config.llm.options) == null ? void 0 : _y.awaitHallucination) === true;
11111
+ const awaitHallucination = ((_x = this.config.llm.options) == null ? void 0 : _x.awaitHallucination) === true;
10842
11112
  const [uiTransformation, hallucinationResult] = yield new __await(Promise.all([
10843
11113
  uiTransformationPromise,
10844
11114
  awaitHallucination ? hallucinationScoringPromise : Promise.resolve(void 0)
10845
11115
  ]));
10846
- const triadScore = yield new __await(RAGEvaluator.evaluate(effectiveQuestion, context, fullReply));
10847
- const buildTrace = (hScore) => __spreadValues({
10848
- requestId,
10849
- query: question,
10850
- rewrittenQuery: rewrittenQuery || ((promptOptResult == null ? void 0 : promptOptResult.wasModified) ? promptOptResult.optimizedPrompt : void 0),
10851
- systemPrompt,
10852
- userPrompt: effectiveQuestion + finalRestrictionSuffix,
10853
- chunks: (sources || []).filter(Boolean).map((s) => {
10854
- var _a3, _b2;
10855
- return {
10856
- id: s.id,
10857
- score: s.score,
10858
- content: (_a3 = s == null ? void 0 : s.content) != null ? _a3 : "",
10859
- metadata: (_b2 = s == null ? void 0 : s.metadata) != null ? _b2 : {},
10860
- namespace: ns
10861
- };
10862
- }),
10863
- latency,
10864
- tokens,
10865
- timestamp: (/* @__PURE__ */ new Date()).toISOString(),
10866
- ragTriad: {
10867
- contextRelevance: triadScore.contextRelevance,
10868
- faithfulness: triadScore.faithfulness,
10869
- answerRelevance: triadScore.answerRelevance,
10870
- overallQuality: triadScore.overallQuality
10871
- }
10872
- }, hScore ? {
10873
- hallucinationScore: hScore.score,
10874
- hallucinationReason: hScore.reason
10875
- } : {});
11116
+ RAGEvaluator.evaluate(effectiveQuestion, context, fullReply).catch(() => {
11117
+ });
11118
+ const buildTrace = (hScore) => {
11119
+ var _a3, _b2;
11120
+ return __spreadValues({
11121
+ requestId,
11122
+ query: question,
11123
+ rewrittenQuery: rewrittenQuery || ((promptOptResult == null ? void 0 : promptOptResult.wasModified) ? promptOptResult.optimizedPrompt : void 0),
11124
+ // systemPrompt / userPrompt intentionally excluded:
11125
+ // they may contain business logic or post-masking user PII and were
11126
+ // being emitted to the client browser via SSE (security risk).
11127
+ chunks: (sources || []).filter(Boolean).map((s) => {
11128
+ var _a4, _b3;
11129
+ return {
11130
+ id: s.id,
11131
+ score: s.score,
11132
+ content: (_a4 = s == null ? void 0 : s.content) != null ? _a4 : "",
11133
+ metadata: (_b3 = s == null ? void 0 : s.metadata) != null ? _b3 : {},
11134
+ namespace: ns
11135
+ };
11136
+ }),
11137
+ latency,
11138
+ tokens,
11139
+ // Always populate model/provider on the trace (fixes gap where telemetry
11140
+ // was reading finalTrace?.model with a fallback — now it's always set).
11141
+ model: (_a3 = this.config.llm) == null ? void 0 : _a3.model,
11142
+ provider: (_b2 = this.config.llm) == null ? void 0 : _b2.provider,
11143
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
11144
+ }, hScore ? {
11145
+ hallucinationScore: hScore.score,
11146
+ hallucinationReason: hScore.reason
11147
+ } : {});
11148
+ };
10876
11149
  const trace = buildTrace(hallucinationResult);
10877
- const isTelemetryActive = (_A = (_z = this.config.telemetry) == null ? void 0 : _z.enabled) != null ? _A : Boolean(this.config.licenseKey);
10878
- if (isTelemetryActive) {
10879
- const defaultUrl = "https://www.retrivora.com/api/telemetry";
10880
- const telemetryUrl = ((_B = this.config.telemetry) == null ? void 0 : _B.url) || process.env.TELEMETRY_URL || process.env.NEXT_PUBLIC_TELEMETRY_URL || defaultUrl;
10881
- const absoluteUrl = telemetryUrl.startsWith("http") ? telemetryUrl : "https://www.retrivora.com" + (telemetryUrl.startsWith("/") ? telemetryUrl : "/" + telemetryUrl);
10882
- (async () => {
10883
- var _a3, _b2, _c2, _d2, _e2;
10884
- try {
10885
- let finalTrace = trace;
10886
- if (!awaitHallucination && runHallucination) {
10887
- const backgroundScoreResult = await hallucinationScoringPromise;
10888
- if (backgroundScoreResult) {
10889
- finalTrace = buildTrace(backgroundScoreResult);
10890
- }
10891
- }
10892
- const modelName = (finalTrace == null ? void 0 : finalTrace.model) || ((_a3 = this.config.llm) == null ? void 0 : _a3.model) || "llama-3.1-8b-instant";
10893
- const providerName = (finalTrace == null ? void 0 : finalTrace.provider) || ((_b2 = this.config.llm) == null ? void 0 : _b2.provider) || "groq";
10894
- const tokenCount = Number(((_c2 = finalTrace == null ? void 0 : finalTrace.tokens) == null ? void 0 : _c2.totalTokens) || 0);
10895
- const costEst = Number(((_d2 = finalTrace == null ? void 0 : finalTrace.tokens) == null ? void 0 : _d2.estimatedCostUsd) || 0);
10896
- const latencyDuration = Number(((_e2 = finalTrace == null ? void 0 : finalTrace.latency) == null ? void 0 : _e2.totalMs) || 0);
10897
- if (typeof window === "undefined") {
10898
- Logger.info(`[Retrivora Pipeline Telemetry] \u{1F4CA} Dispatching RAG telemetry -> Project: "${ns}", Tokens: ${tokenCount}, Latency: ${latencyDuration}ms`);
10899
- }
10900
- await fetch(absoluteUrl, {
10901
- method: "POST",
10902
- headers: {
10903
- "Content-Type": "application/json"
10904
- },
10905
- body: JSON.stringify({
10906
- trace: finalTrace,
10907
- licenseKey: this.config.licenseKey,
10908
- projectId: ns,
10909
- organization: process.env.RETRIVORA_ORGANIZATION || "default-org",
10910
- sdkVersion: SDK_VERSION,
10911
- model: modelName,
10912
- provider: providerName,
10913
- tokens: tokenCount,
10914
- cost: costEst,
10915
- costUsd: costEst,
10916
- latencyMs: latencyDuration,
10917
- status: "success",
10918
- action: "RAG_QUERY",
10919
- feature: "RAG",
10920
- timestamp: (/* @__PURE__ */ new Date()).toISOString(),
10921
- details: `Org: default-org | Model: ${providerName}/${modelName} | Tokens: ${tokenCount} | Latency: ${latencyDuration}ms | Cost: $${costEst} | SDK: v${SDK_VERSION} | Feature: RAG`
10922
- })
10923
- }).catch((err) => {
10924
- Logger.warn(`[Pipeline Telemetry] Failed to send trace async to ${absoluteUrl}`, { error: err.message });
10925
- });
10926
- } catch (err) {
10927
- Logger.warn(`[Pipeline Telemetry] Failed to send trace async to ${absoluteUrl}:`, { error: err.message });
10928
- }
10929
- })();
10930
- }
10931
11150
  yield {
10932
11151
  reply: "",
10933
11152
  sources,
@@ -11365,12 +11584,14 @@ __reExport(FreeTierLimitsGuard_exports, require("@retrivora-ai/billing-guards"))
11365
11584
 
11366
11585
  // src/core/VectorPlugin.ts
11367
11586
  var VectorPlugin = class {
11368
- constructor(hostConfig) {
11587
+ constructor(hostConfig, options) {
11369
11588
  const resolvedConfig = ConfigResolver.resolve(hostConfig);
11370
11589
  this.config = resolvedConfig;
11371
11590
  this.validationPromise = (async () => {
11372
11591
  var _a2;
11373
11592
  await ConfigValidator.validateAndThrow(resolvedConfig);
11593
+ const isControlPlane = Boolean(typeof process !== "undefined" && process.env.LITELLM_MASTER_KEY && resolvedConfig.licenseKey === process.env.LITELLM_MASTER_KEY);
11594
+ if ((options == null ? void 0 : options.skipLicenseCheck) || isControlPlane) return;
11374
11595
  await LicenseVerifier.verifyAsync(
11375
11596
  resolvedConfig.licenseKey,
11376
11597
  resolvedConfig.projectId,
@@ -12585,8 +12806,8 @@ init_RedisProvider();
12585
12806
  init_WeaviateProvider();
12586
12807
  init_UniversalVectorProvider();
12587
12808
 
12588
- // src/handlers/index.ts
12589
- var import_server = require("next/server");
12809
+ // src/handlers/chatHandler.ts
12810
+ var import_server3 = require("next/server");
12590
12811
 
12591
12812
  // src/core/DatabaseStorage.ts
12592
12813
  var fs = __toESM(require("fs"));
@@ -12685,7 +12906,13 @@ var _DatabaseStorage = class _DatabaseStorage {
12685
12906
  getPGPool() {
12686
12907
  const globalKey = `pg:${this.config.projectId}`;
12687
12908
  if (!_pgInitPromises.has(globalKey)) {
12688
- _pgInitPromises.set(globalKey, this.createPGPool());
12909
+ _pgInitPromises.set(
12910
+ globalKey,
12911
+ this.createPGPool().catch((err) => {
12912
+ _pgInitPromises.delete(globalKey);
12913
+ throw err;
12914
+ })
12915
+ );
12689
12916
  }
12690
12917
  return _pgInitPromises.get(globalKey);
12691
12918
  }
@@ -13070,9 +13297,8 @@ var _DatabaseStorage = class _DatabaseStorage {
13070
13297
  _DatabaseStorage.SUBSCRIPTION_CACHE_TTL_MS = 5 * 60 * 1e3;
13071
13298
  var DatabaseStorage = _DatabaseStorage;
13072
13299
 
13073
- // src/handlers/index.ts
13074
- init_version();
13075
- var import_crypto2 = require("crypto");
13300
+ // src/handlers/internal/auth.ts
13301
+ var import_server = require("next/server");
13076
13302
  async function checkAuth(req, onAuthorize) {
13077
13303
  if (!onAuthorize) return null;
13078
13304
  try {
@@ -13089,6 +13315,8 @@ async function checkAuth(req, onAuthorize) {
13089
13315
  return import_server.NextResponse.json({ error: msg }, { status: 401 });
13090
13316
  }
13091
13317
  }
13318
+
13319
+ // src/handlers/internal/rateLimit.ts
13092
13320
  var RateLimiter = class {
13093
13321
  constructor(windowMs = 6e4, max = 30) {
13094
13322
  this.hits = /* @__PURE__ */ new Map();
@@ -13150,6 +13378,9 @@ function checkRateLimit(req) {
13150
13378
  }
13151
13379
  return null;
13152
13380
  }
13381
+
13382
+ // src/handlers/internal/license.ts
13383
+ var import_server2 = require("next/server");
13153
13384
  var FREE_TIER_NAMES = /* @__PURE__ */ new Set(["hobby", "free", "free_trial", "trial"]);
13154
13385
  function isFreeTier(tier) {
13155
13386
  if (!tier) return true;
@@ -13194,7 +13425,7 @@ async function enforceLicense(req, config, bodyLicenseKey) {
13194
13425
  } catch (err) {
13195
13426
  return {
13196
13427
  ok: false,
13197
- response: import_server.NextResponse.json(
13428
+ response: import_server2.NextResponse.json(
13198
13429
  { error: `License validation failed: ${err.message}` },
13199
13430
  { status: 401 }
13200
13431
  )
@@ -13234,8 +13465,8 @@ function resolveLicensedNamespace(payload, requestedNamespace) {
13234
13465
  var _a2;
13235
13466
  const allowed = getAllowedNamespaces(payload.projectId);
13236
13467
  const requested = requestedNamespace == null ? void 0 : requestedNamespace.trim();
13237
- const isDevOrFree = process.env.NODE_ENV !== "production" || isFreeTier(payload.tier);
13238
- if (requested && !isDevOrFree && !allowed.has(requested)) {
13468
+ const isDev = process.env.NODE_ENV !== "production";
13469
+ if (requested && !isDev && !allowed.has(requested)) {
13239
13470
  return {
13240
13471
  ok: false,
13241
13472
  response: new Response(JSON.stringify({
@@ -13252,6 +13483,8 @@ function resolveLicensedNamespace(payload, requestedNamespace) {
13252
13483
  const defaultNamespace = ((_a2 = payload.projectId) == null ? void 0 : _a2.startsWith("retrivora-")) ? payload.projectId : `retrivora-${payload.projectId || "default"}`;
13253
13484
  return { ok: true, namespace: requested || defaultNamespace };
13254
13485
  }
13486
+
13487
+ // src/handlers/internal/sanitize.ts
13255
13488
  var MAX_MESSAGE_LENGTH = 8e3;
13256
13489
  function sanitizeInput(raw) {
13257
13490
  const stripped = raw.replace(/[\u0000-\u0008\u000B\u000C\u000E-\u001F\u007F-\u009F]/g, "");
@@ -13264,6 +13497,9 @@ function sanitizeInput(raw) {
13264
13497
  }
13265
13498
  return { ok: true, value: trimmed };
13266
13499
  }
13500
+
13501
+ // src/handlers/internal/plugin.ts
13502
+ var import_crypto2 = require("crypto");
13267
13503
  var _pluginCache = /* @__PURE__ */ new Map();
13268
13504
  var PLUGIN_CACHE_TTL_MS = 10 * 60 * 1e3;
13269
13505
  function configHash(config) {
@@ -13274,10 +13510,13 @@ function getOrCreatePlugin(configOrPlugin) {
13274
13510
  const key = configHash(configOrPlugin);
13275
13511
  const cached = _pluginCache.get(key);
13276
13512
  if (cached && Date.now() - cached.createdAt < PLUGIN_CACHE_TTL_MS) return cached.plugin;
13277
- const plugin = new VectorPlugin(configOrPlugin);
13513
+ const plugin = new VectorPlugin(configOrPlugin, { skipLicenseCheck: true });
13278
13514
  _pluginCache.set(key, { plugin, createdAt: Date.now() });
13279
13515
  return plugin;
13280
13516
  }
13517
+
13518
+ // src/handlers/internal/telemetry.ts
13519
+ init_version();
13281
13520
  var TELEMETRY_ALLOWED_HOSTS = /* @__PURE__ */ new Set([
13282
13521
  "retrivora.com",
13283
13522
  "www.retrivora.com"
@@ -13298,8 +13537,20 @@ function sanitizeTelemetryUrl(raw) {
13298
13537
  return null;
13299
13538
  }
13300
13539
  }
13301
- async function reportTelemetry(req, plugin, action, status, details, trace) {
13302
- var _a2, _b, _c, _d, _e, _f, _g2;
13540
+ function classifyError(message) {
13541
+ const m = (message || "").toLowerCase();
13542
+ if (/license|revoked|terminated|suspended/.test(m)) return { errorCode: "LICENSE_ERROR", errorType: "LICENSE_ERROR" };
13543
+ if (/rate.?limit|429/.test(m)) return { errorCode: "RATE_LIMIT_ERROR", errorType: "RATE_LIMIT_ERROR" };
13544
+ if (/unauthorized|401|403/.test(m)) return { errorCode: "AUTH_ERROR", errorType: "AUTH_ERROR" };
13545
+ if (/embedding|embed.?fail/.test(m)) return { errorCode: "EMBEDDING_ERROR", errorType: "EMBEDDING_ERROR" };
13546
+ if (/vector|pinecone|qdrant|pgvector|chroma|milvus|mongodb/.test(m)) return { errorCode: "VECTOR_DB_ERROR", errorType: "VECTOR_DB_ERROR" };
13547
+ if (/llm|chat|generation|openai|groq|anthropic|gemini|mistral/.test(m)) return { errorCode: "LLM_ERROR", errorType: "LLM_ERROR" };
13548
+ if (/fetch|network|timeout|econnrefused|socket/.test(m)) return { errorCode: "NETWORK_ERROR", errorType: "NETWORK_ERROR" };
13549
+ if (/stream/.test(m)) return { errorCode: "STREAMING_ERROR", errorType: "STREAMING_ERROR" };
13550
+ return { errorCode: "INTERNAL_ERROR", errorType: "INTERNAL_ERROR" };
13551
+ }
13552
+ async function reportTelemetry(req, plugin, action, status, details, trace, extra) {
13553
+ var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k, _l, _m, _n, _o, _p;
13303
13554
  try {
13304
13555
  const config = plugin.getConfig();
13305
13556
  const licenseKey = config.licenseKey || process.env.RETRIVORA_LICENSE_KEY || process.env.RAG_LICENSE_KEY;
@@ -13310,13 +13561,14 @@ async function reportTelemetry(req, plugin, action, status, details, trace) {
13310
13561
  if (!telemetryUrl) return;
13311
13562
  const userAgent = req.headers.get("user-agent") || `Retrivora-SDK/${SDK_VERSION}`;
13312
13563
  const projectId = config.projectId || "default";
13313
- 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";
13314
- 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";
13315
- const tokens = Number(((_e = trace == null ? void 0 : trace.tokens) == null ? void 0 : _e.totalTokens) || (trace == null ? void 0 : trace.totalTokens) || 0);
13316
- const costUsd = Number(((_f = trace == null ? void 0 : trace.tokens) == null ? void 0 : _f.estimatedCostUsd) || (trace == null ? void 0 : trace.costUsd) || 0);
13317
- const latencyMs = Number(((_g2 = trace == null ? void 0 : trace.latency) == null ? void 0 : _g2.totalMs) || (trace == null ? void 0 : trace.latencyMs) || 0);
13564
+ const model = (trace == null ? void 0 : trace.model) || ((_a2 = config.llm) == null ? void 0 : _a2.model) || "llama-3.1-8b-instant";
13565
+ const provider = (trace == null ? void 0 : trace.provider) || ((_b = config.llm) == null ? void 0 : _b.provider) || "groq";
13566
+ const tokens = Number(((_c = trace == null ? void 0 : trace.tokens) == null ? void 0 : _c.totalTokens) || (trace == null ? void 0 : trace.totalTokens) || 0);
13567
+ const costUsd = Number(((_d = trace == null ? void 0 : trace.tokens) == null ? void 0 : _d.estimatedCostUsd) || (trace == null ? void 0 : trace.costUsd) || 0);
13568
+ const latencyMs = Number(((_e = trace == null ? void 0 : trace.latency) == null ? void 0 : _e.totalMs) || (trace == null ? void 0 : trace.latencyMs) || 0);
13569
+ const sanitizedTrace = trace ? __spreadProps(__spreadValues({}, trace), { systemPrompt: void 0, userPrompt: void 0 }) : void 0;
13318
13570
  const payload = {
13319
- trace,
13571
+ trace: sanitizedTrace,
13320
13572
  projectId,
13321
13573
  organization: process.env.RETRIVORA_ORGANIZATION || "default-org",
13322
13574
  sdkVersion: SDK_VERSION,
@@ -13331,7 +13583,17 @@ async function reportTelemetry(req, plugin, action, status, details, trace) {
13331
13583
  feature: action,
13332
13584
  userAgent,
13333
13585
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
13334
- details
13586
+ details,
13587
+ // ── Structured observability fields ──────────────────────────────────
13588
+ requestId: (_f = trace == null ? void 0 : trace.requestId) != null ? _f : null,
13589
+ embedMs: (_h = (_g2 = trace == null ? void 0 : trace.latency) == null ? void 0 : _g2.embedMs) != null ? _h : null,
13590
+ retrieveMs: (_j = (_i = trace == null ? void 0 : trace.latency) == null ? void 0 : _i.retrieveMs) != null ? _j : null,
13591
+ generateMs: (_l = (_k = trace == null ? void 0 : trace.latency) == null ? void 0 : _k.generateMs) != null ? _l : null,
13592
+ ttftMs: (_m = extra == null ? void 0 : extra.ttftMs) != null ? _m : null,
13593
+ errorCode: (_n = extra == null ? void 0 : extra.errorCode) != null ? _n : null,
13594
+ errorType: (_o = extra == null ? void 0 : extra.errorType) != null ? _o : null,
13595
+ isStreaming: (_p = extra == null ? void 0 : extra.isStreaming) != null ? _p : false,
13596
+ environment: process.env.NODE_ENV || "development"
13335
13597
  };
13336
13598
  await fetch(telemetryUrl, {
13337
13599
  method: "POST",
@@ -13347,43 +13609,8 @@ async function reportTelemetry(req, plugin, action, status, details, trace) {
13347
13609
  } catch (e) {
13348
13610
  }
13349
13611
  }
13350
- function sseFrame(payload) {
13351
- return `data: ${JSON.stringify(payload)}
13352
-
13353
- `;
13354
- }
13355
- function sseTextFrame(text) {
13356
- return `data: ${JSON.stringify({ type: "text", text })}
13357
-
13358
- `;
13359
- }
13360
- function sseMetaFrame(meta) {
13361
- return `data: ${JSON.stringify(__spreadValues({ type: "metadata" }, meta != null ? meta : {}))}
13362
13612
 
13363
- `;
13364
- }
13365
- function sseUIFrame(uiTransformation) {
13366
- return `data: ${JSON.stringify({ type: "ui_transformation", data: uiTransformation })}
13367
-
13368
- `;
13369
- }
13370
- function sseObservabilityFrame(trace) {
13371
- return `data: ${JSON.stringify({ type: "observability", data: trace })}
13372
-
13373
- `;
13374
- }
13375
- function sseErrorFrame(message) {
13376
- return `data: ${JSON.stringify({ type: "error", error: message })}
13377
-
13378
- `;
13379
- }
13380
- var SSE_HEADERS = {
13381
- "Content-Type": "text/event-stream; charset=utf-8",
13382
- "Cache-Control": "no-cache, no-transform",
13383
- Connection: "keep-alive",
13384
- "X-Accel-Buffering": "no"
13385
- // Disable Nginx buffering for streaming
13386
- };
13613
+ // src/handlers/chatHandler.ts
13387
13614
  function createChatHandler(configOrPlugin, options) {
13388
13615
  const plugin = getOrCreatePlugin(configOrPlugin);
13389
13616
  const storage = new DatabaseStorage(plugin.getConfig());
@@ -13435,7 +13662,7 @@ function createChatHandler(configOrPlugin, options) {
13435
13662
  if (!namespaceCheck.ok) return namespaceCheck.response;
13436
13663
  const sanitized = sanitizeInput(rawMessage != null ? rawMessage : "");
13437
13664
  if (!sanitized.ok) {
13438
- return import_server.NextResponse.json({ error: { code: "INVALID_INPUT", message: sanitized.error } }, { status: 400 });
13665
+ return import_server3.NextResponse.json({ error: { code: "INVALID_INPUT", message: sanitized.error } }, { status: 400 });
13439
13666
  }
13440
13667
  const message = sanitized.value;
13441
13668
  const userMsgId = userMessageId || `user_${Date.now()}_${Math.random().toString(36).slice(2, 6)}`;
@@ -13454,18 +13681,68 @@ function createChatHandler(configOrPlugin, options) {
13454
13681
  uiTransformation: result.ui_transformation,
13455
13682
  trace: result.trace
13456
13683
  }).catch((err) => console.warn("[createChatHandler] Failed to save assistant message:", err));
13457
- await reportTelemetry(req, plugin, "QUERY_GENERATION", "success", `Tokens: ${((_d = (_c = result.trace) == null ? void 0 : _c.tokens) == null ? void 0 : _d.totalTokens) || 0}, Latency: ${((_f = (_e = result.trace) == null ? void 0 : _e.latency) == null ? void 0 : _f.totalMs) || 0}ms`, result.trace);
13458
- return import_server.NextResponse.json(__spreadProps(__spreadValues({}, result), {
13684
+ await reportTelemetry(
13685
+ req,
13686
+ plugin,
13687
+ "RAG_QUERY",
13688
+ "success",
13689
+ `Tokens: ${((_d = (_c = result.trace) == null ? void 0 : _c.tokens) == null ? void 0 : _d.totalTokens) || 0}, Latency: ${((_f = (_e = result.trace) == null ? void 0 : _e.latency) == null ? void 0 : _f.totalMs) || 0}ms`,
13690
+ result.trace,
13691
+ { isStreaming: false }
13692
+ );
13693
+ return import_server3.NextResponse.json(__spreadProps(__spreadValues({}, result), {
13459
13694
  messageId: assistantMsgId,
13460
13695
  userMessageId: userMsgId
13461
13696
  }));
13462
13697
  } catch (err) {
13463
13698
  const message = err instanceof Error ? err.message : "Internal server error";
13464
- await reportTelemetry(req, plugin, "QUERY_GENERATION", "error", message);
13465
- return import_server.NextResponse.json({ error: message }, { status: 500 });
13699
+ const { errorCode, errorType } = classifyError(message);
13700
+ await reportTelemetry(req, plugin, "RAG_QUERY", "error", message, void 0, { isStreaming: false, errorCode, errorType });
13701
+ return import_server3.NextResponse.json({ error: message }, { status: 500 });
13466
13702
  }
13467
13703
  };
13468
13704
  }
13705
+
13706
+ // src/handlers/internal/sse.ts
13707
+ function sseFrame(payload) {
13708
+ return `data: ${JSON.stringify(payload)}
13709
+
13710
+ `;
13711
+ }
13712
+ function sseTextFrame(text) {
13713
+ return `data: ${JSON.stringify({ type: "text", text })}
13714
+
13715
+ `;
13716
+ }
13717
+ function sseMetaFrame(meta) {
13718
+ return `data: ${JSON.stringify(__spreadValues({ type: "metadata" }, meta != null ? meta : {}))}
13719
+
13720
+ `;
13721
+ }
13722
+ function sseUIFrame(uiTransformation) {
13723
+ return `data: ${JSON.stringify({ type: "ui_transformation", data: uiTransformation })}
13724
+
13725
+ `;
13726
+ }
13727
+ function sseObservabilityFrame(trace) {
13728
+ return `data: ${JSON.stringify({ type: "observability", data: trace })}
13729
+
13730
+ `;
13731
+ }
13732
+ function sseErrorFrame(message) {
13733
+ return `data: ${JSON.stringify({ type: "error", error: message })}
13734
+
13735
+ `;
13736
+ }
13737
+ var SSE_HEADERS = {
13738
+ "Content-Type": "text/event-stream; charset=utf-8",
13739
+ "Cache-Control": "no-cache, no-transform",
13740
+ Connection: "keep-alive",
13741
+ "X-Accel-Buffering": "no"
13742
+ // Disable Nginx buffering for streaming
13743
+ };
13744
+
13745
+ // src/handlers/streamHandler.ts
13469
13746
  function createStreamHandler(configOrPlugin, options) {
13470
13747
  const plugin = getOrCreatePlugin(configOrPlugin);
13471
13748
  const storage = new DatabaseStorage(plugin.getConfig());
@@ -13540,6 +13817,7 @@ function createStreamHandler(configOrPlugin, options) {
13540
13817
  const assistantMsgId = messageId || `assistant_${Date.now()}_${Math.random().toString(36).slice(2, 6)}`;
13541
13818
  const encoder = new TextEncoder();
13542
13819
  let isActive = true;
13820
+ let streamTelemetrySent = false;
13543
13821
  const stream = new ReadableStream({
13544
13822
  async start(controller) {
13545
13823
  var _a3, _b2, _c, _d, _e;
@@ -13554,11 +13832,16 @@ function createStreamHandler(configOrPlugin, options) {
13554
13832
  try {
13555
13833
  const pipelineStream = plugin.chatStream(message, history, namespaceCheck.namespace);
13556
13834
  let fullReply = "";
13835
+ let ttftMs;
13836
+ const handlerRequestStartMs = Date.now();
13557
13837
  try {
13558
13838
  for (var iter = __forAwait(pipelineStream), more, temp, error; more = !(temp = await iter.next()).done; more = false) {
13559
13839
  const chunk = temp.value;
13560
13840
  if (!isActive) break;
13561
13841
  if (typeof chunk === "string") {
13842
+ if (ttftMs === void 0) {
13843
+ ttftMs = Date.now() - handlerRequestStartMs;
13844
+ }
13562
13845
  fullReply += chunk;
13563
13846
  enqueue(sseTextFrame(chunk));
13564
13847
  } else if (chunk && typeof chunk === "object" && "type" in chunk && chunk.type === "thinking") {
@@ -13599,7 +13882,16 @@ function createStreamHandler(configOrPlugin, options) {
13599
13882
  uiTransformation,
13600
13883
  trace: responseChunk == null ? void 0 : responseChunk.trace
13601
13884
  }).catch((err) => console.warn("[createStreamHandler] Failed to save assistant message:", err));
13602
- await 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);
13885
+ streamTelemetrySent = true;
13886
+ await reportTelemetry(
13887
+ req,
13888
+ plugin,
13889
+ "RAG_QUERY",
13890
+ "success",
13891
+ `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`,
13892
+ responseChunk == null ? void 0 : responseChunk.trace,
13893
+ { ttftMs, isStreaming: true }
13894
+ );
13603
13895
  }
13604
13896
  }
13605
13897
  } catch (temp) {
@@ -13615,11 +13907,21 @@ function createStreamHandler(configOrPlugin, options) {
13615
13907
  } catch (streamError) {
13616
13908
  if (isActive) {
13617
13909
  let errorMessage = streamError instanceof Error ? streamError.message : String(streamError);
13618
- if (errorMessage.includes("403") || errorMessage.toLowerCase().includes("license") || errorMessage.toLowerCase().includes("revoked") || errorMessage.toLowerCase().includes("terminated")) {
13910
+ if (errorMessage.includes("LICENSE_REVOKED") || errorMessage.toLowerCase().includes("key has been revoked") || errorMessage.toLowerCase().includes("key has been terminated") || errorMessage.toLowerCase().includes("key has been suspended")) {
13619
13911
  errorMessage = "Your Retrivora license key has been terminated, suspended, or revoked. Access denied.";
13620
13912
  }
13621
13913
  console.error("[createStreamHandler] Stream error:", streamError);
13622
- await reportTelemetry(req, plugin, "QUERY_GENERATION", "error", errorMessage);
13914
+ const { errorCode, errorType } = classifyError(errorMessage);
13915
+ streamTelemetrySent = true;
13916
+ await reportTelemetry(
13917
+ req,
13918
+ plugin,
13919
+ "RAG_QUERY",
13920
+ "error",
13921
+ errorMessage,
13922
+ void 0,
13923
+ { isStreaming: true, errorCode, errorType }
13924
+ );
13623
13925
  try {
13624
13926
  enqueue(sseErrorFrame(errorMessage));
13625
13927
  } catch (e) {
@@ -13628,6 +13930,10 @@ function createStreamHandler(configOrPlugin, options) {
13628
13930
  } finally {
13629
13931
  if (isActive) {
13630
13932
  isActive = false;
13933
+ try {
13934
+ enqueue(sseFrame({ type: "done" }));
13935
+ } catch (e) {
13936
+ }
13631
13937
  try {
13632
13938
  controller.close();
13633
13939
  } catch (e) {
@@ -13637,11 +13943,26 @@ function createStreamHandler(configOrPlugin, options) {
13637
13943
  },
13638
13944
  cancel(reason) {
13639
13945
  isActive = false;
13946
+ if (!streamTelemetrySent) {
13947
+ reportTelemetry(
13948
+ req,
13949
+ plugin,
13950
+ "RAG_QUERY_ABORTED",
13951
+ "error",
13952
+ "Client disconnected mid-stream",
13953
+ void 0,
13954
+ { isStreaming: true, errorCode: "CLIENT_DISCONNECT", errorType: "STREAMING_ERROR" }
13955
+ ).catch(() => {
13956
+ });
13957
+ }
13640
13958
  }
13641
13959
  });
13642
13960
  return new Response(stream, { headers: __spreadProps(__spreadValues({}, SSE_HEADERS), { "x-message-id": assistantMsgId, "x-user-message-id": userMsgId }) });
13643
13961
  };
13644
13962
  }
13963
+
13964
+ // src/handlers/ingestHandler.ts
13965
+ var import_server4 = require("next/server");
13645
13966
  function createIngestHandler(configOrPlugin, options) {
13646
13967
  const plugin = getOrCreatePlugin(configOrPlugin);
13647
13968
  const onAuthorize = options == null ? void 0 : options.onAuthorize;
@@ -13655,7 +13976,7 @@ function createIngestHandler(configOrPlugin, options) {
13655
13976
  if (!licenseCheck.ok) return licenseCheck.response;
13656
13977
  const { documents, namespace } = body;
13657
13978
  if (!Array.isArray(documents) || documents.length === 0) {
13658
- return import_server.NextResponse.json({ error: "documents array is required" }, { status: 400 });
13979
+ return import_server4.NextResponse.json({ error: "documents array is required" }, { status: 400 });
13659
13980
  }
13660
13981
  const namespaceCheck = resolveLicensedNamespace(licenseCheck.payload, namespace);
13661
13982
  if (!namespaceCheck.ok) return namespaceCheck.response;
@@ -13682,14 +14003,17 @@ function createIngestHandler(configOrPlugin, options) {
13682
14003
  }
13683
14004
  const results = await plugin.ingest(documents, namespaceCheck.namespace);
13684
14005
  await reportTelemetry(req, plugin, "DOCUMENT_INGESTION", "success", `Ingested ${documents.length} document(s)`);
13685
- return import_server.NextResponse.json({ results });
14006
+ return import_server4.NextResponse.json({ results });
13686
14007
  } catch (err) {
13687
14008
  const message = err instanceof Error ? err.message : "Internal server error";
13688
14009
  await reportTelemetry(req, plugin, "DOCUMENT_INGESTION", "error", message);
13689
- return import_server.NextResponse.json({ error: message }, { status: 500 });
14010
+ return import_server4.NextResponse.json({ error: message }, { status: 500 });
13690
14011
  }
13691
14012
  };
13692
14013
  }
14014
+
14015
+ // src/handlers/healthHandler.ts
14016
+ var import_server5 = require("next/server");
13693
14017
  function createHealthHandler(configOrPlugin, options) {
13694
14018
  const plugin = getOrCreatePlugin(configOrPlugin);
13695
14019
  const onAuthorize = options == null ? void 0 : options.onAuthorize;
@@ -13701,17 +14025,21 @@ function createHealthHandler(configOrPlugin, options) {
13701
14025
  try {
13702
14026
  const health = await plugin.checkHealth();
13703
14027
  const status = health.allHealthy ? "ok" : "degraded";
13704
- return import_server.NextResponse.json(__spreadProps(__spreadValues({
14028
+ return import_server5.NextResponse.json(__spreadProps(__spreadValues({
13705
14029
  status
13706
14030
  }, health), {
13707
14031
  timestamp: (/* @__PURE__ */ new Date()).toISOString()
13708
14032
  }));
13709
14033
  } catch (err) {
13710
14034
  const message = err instanceof Error ? err.message : "Unknown error";
13711
- return import_server.NextResponse.json({ status: "error", error: message }, { status: 500 });
14035
+ return import_server5.NextResponse.json({ status: "error", error: message }, { status: 500 });
13712
14036
  }
13713
14037
  };
13714
14038
  }
14039
+
14040
+ // src/handlers/licenseHandler.ts
14041
+ var import_server6 = require("next/server");
14042
+ init_version();
13715
14043
  function createLicenseHandler(configOrPlugin, options) {
13716
14044
  const plugin = getOrCreatePlugin(configOrPlugin);
13717
14045
  const onAuthorize = options == null ? void 0 : options.onAuthorize;
@@ -13737,7 +14065,7 @@ function createLicenseHandler(configOrPlugin, options) {
13737
14065
  const licenseKey = (rawKey || "").trim().replace(/^["']|["']$/g, "").trim();
13738
14066
  const projectId = (body == null ? void 0 : body.projectId) || config.projectId || "";
13739
14067
  const payload = await LicenseVerifier.verifyAsync(licenseKey, projectId);
13740
- return import_server.NextResponse.json({
14068
+ return import_server6.NextResponse.json({
13741
14069
  valid: true,
13742
14070
  licenseStatus: "ACTIVE",
13743
14071
  minimumSupportedVersion: "2.1.0",
@@ -13747,7 +14075,7 @@ function createLicenseHandler(configOrPlugin, options) {
13747
14075
  message: "License key is active and valid."
13748
14076
  });
13749
14077
  } catch (err) {
13750
- return import_server.NextResponse.json(
14078
+ return import_server6.NextResponse.json(
13751
14079
  {
13752
14080
  valid: false,
13753
14081
  licenseStatus: "REVOKED",
@@ -13762,6 +14090,9 @@ function createLicenseHandler(configOrPlugin, options) {
13762
14090
  }
13763
14091
  };
13764
14092
  }
14093
+
14094
+ // src/handlers/uploadHandler.ts
14095
+ var import_server7 = require("next/server");
13765
14096
  function createUploadHandler(configOrPlugin, options) {
13766
14097
  const plugin = getOrCreatePlugin(configOrPlugin);
13767
14098
  const onAuthorize = options == null ? void 0 : options.onAuthorize;
@@ -13780,7 +14111,7 @@ function createUploadHandler(configOrPlugin, options) {
13780
14111
  const namespaceCheck = resolveLicensedNamespace(licenseCheck.payload, namespace);
13781
14112
  if (!namespaceCheck.ok) return namespaceCheck.response;
13782
14113
  if (!files || files.length === 0) {
13783
- return import_server.NextResponse.json({ error: "No files provided" }, { status: 400 });
14114
+ return import_server7.NextResponse.json({ error: "No files provided" }, { status: 400 });
13784
14115
  }
13785
14116
  if (isFreeTier(licenseCheck.payload.tier)) {
13786
14117
  const totalUploadBytes = files.reduce((sum, f) => sum + (f.size || 0), 0);
@@ -13802,137 +14133,174 @@ function createUploadHandler(configOrPlugin, options) {
13802
14133
  );
13803
14134
  }
13804
14135
  }
13805
- const documents = [];
13806
- for (const file of files) {
13807
- const buffer = Buffer.isBuffer(file) ? file : Buffer.from(await file.arrayBuffer());
13808
- const isExcel = file.name.toLowerCase().endsWith(".xlsx") || file.name.toLowerCase().endsWith(".xls") || file.type.includes("spreadsheet") || file.type.includes("excel");
13809
- if (file.name.toLowerCase().endsWith(".csv") || file.type === "text/csv") {
13810
- const text = buffer.toString("utf-8");
13811
- const Papa = await import("papaparse");
13812
- const parsed = Papa.default.parse(text, { header: true, skipEmptyLines: true });
13813
- if (parsed.data && parsed.data.length > 0) {
13814
- let i = 0;
13815
- let lastRowData = null;
13816
- const csvCols = Object.keys(parsed.data[0]);
13817
- for (const row of parsed.data) {
13818
- i++;
13819
- const rowData = row;
13820
- const groupingKey = Object.keys(rowData)[0];
13821
- if (lastRowData && groupingKey && rowData[groupingKey] && rowData[groupingKey] === lastRowData[groupingKey]) {
13822
- for (const key of Object.keys(rowData)) {
13823
- if (!rowData[key] && lastRowData[key]) {
13824
- rowData[key] = lastRowData[key];
14136
+ const fileDocsArrays = await Promise.all(
14137
+ files.map(async (file) => {
14138
+ const fileDocs = [];
14139
+ const buffer = Buffer.isBuffer(file) ? file : Buffer.from(await file.arrayBuffer());
14140
+ const isExcel = file.name.toLowerCase().endsWith(".xlsx") || file.name.toLowerCase().endsWith(".xls") || file.type.includes("spreadsheet") || file.type.includes("excel");
14141
+ const SPREADSHEET_CHUNK_SIZE = 30;
14142
+ if (file.name.toLowerCase().endsWith(".csv") || file.name.toLowerCase().endsWith(".tsv") || file.type === "text/csv" || file.type.includes("tab-separated")) {
14143
+ const text = buffer.toString("utf-8");
14144
+ const Papa = await import("papaparse");
14145
+ const parsed = Papa.default.parse(text, { header: true, skipEmptyLines: true });
14146
+ if (parsed.data && parsed.data.length > 0) {
14147
+ const csvCols = Object.keys(parsed.data[0]);
14148
+ const rawRows = [];
14149
+ let lastRowData = null;
14150
+ for (const row of parsed.data) {
14151
+ const rowData = __spreadValues({}, row);
14152
+ const groupingKey = Object.keys(rowData)[0];
14153
+ if (lastRowData && groupingKey && rowData[groupingKey] && rowData[groupingKey] === lastRowData[groupingKey]) {
14154
+ for (const key of Object.keys(rowData)) {
14155
+ if (!rowData[key] && lastRowData[key]) {
14156
+ rowData[key] = lastRowData[key];
14157
+ }
13825
14158
  }
13826
14159
  }
14160
+ lastRowData = __spreadValues({}, rowData);
14161
+ rawRows.push(rowData);
13827
14162
  }
13828
- lastRowData = __spreadValues({}, rowData);
13829
- const contentParts = [];
13830
- for (const [key, val] of Object.entries(rowData)) {
13831
- if (key && val) {
13832
- contentParts.push(`${key}: ${val}`);
14163
+ for (let i = 0; i < rawRows.length; i += SPREADSHEET_CHUNK_SIZE) {
14164
+ const slice = rawRows.slice(i, i + SPREADSHEET_CHUNK_SIZE);
14165
+ const startRow = i + 1;
14166
+ const endRow = i + slice.length;
14167
+ const chunkIndex = Math.floor(i / SPREADSHEET_CHUNK_SIZE) + 1;
14168
+ const lines = [
14169
+ `File: ${file.name} (Rows ${startRow}-${endRow} of ${rawRows.length})`,
14170
+ `| ${csvCols.join(" | ")} |`,
14171
+ `| ${csvCols.map(() => "---").join(" | ")} |`
14172
+ ];
14173
+ for (const r of slice) {
14174
+ const rowValues = csvCols.map((col) => {
14175
+ const val = r[col];
14176
+ if (val === void 0 || val === null) return "";
14177
+ return String(val).replace(/\|/g, "\\|").replace(/\r?\n/g, " ").trim();
14178
+ });
14179
+ lines.push(`| ${rowValues.join(" | ")} |`);
13833
14180
  }
14181
+ fileDocs.push({
14182
+ docId: `${file.name}-chunk-${chunkIndex}`,
14183
+ content: lines.join("\n"),
14184
+ metadata: __spreadValues({
14185
+ fileName: file.name,
14186
+ fileSize: file.size,
14187
+ fileType: file.type || "text/csv",
14188
+ uploadedAt: (/* @__PURE__ */ new Date()).toISOString(),
14189
+ startRow,
14190
+ endRow,
14191
+ rowCount: slice.length,
14192
+ headers: csvCols
14193
+ }, dimension ? { dimension } : {})
14194
+ });
13834
14195
  }
13835
- documents.push({
13836
- docId: `${file.name}-row-${i}`,
13837
- content: contentParts.join(", "),
13838
- metadata: __spreadValues(__spreadProps(__spreadValues({
13839
- fileName: file.name,
13840
- fileSize: file.size,
13841
- fileType: file.type || "text/csv",
13842
- uploadedAt: (/* @__PURE__ */ new Date()).toISOString()
13843
- }, dimension ? { dimension } : {}), {
13844
- csvHeaders: csvCols
13845
- }), rowData)
13846
- });
13847
14196
  }
13848
- }
13849
- } else if (isExcel) {
13850
- let excelLoaded = false;
13851
- try {
13852
- const isZip = buffer.length >= 4 && buffer[0] === 80 && buffer[1] === 75;
13853
- if (isZip) {
13854
- const ExcelJS = await import("exceljs");
13855
- const workbook = new ExcelJS.Workbook();
13856
- await workbook.xlsx.load(buffer);
13857
- workbook.eachSheet((sheet, sheetId) => {
13858
- const sheetName = sheet.name;
13859
- let headers = [];
13860
- let i = 0;
13861
- sheet.eachRow((row, rowNumber) => {
13862
- const values = Array.isArray(row.values) ? row.values.slice(1) : [];
13863
- if (rowNumber === 1) {
13864
- headers = values.map((v) => String(v));
13865
- return;
13866
- }
13867
- i++;
13868
- const contentParts = [];
13869
- const rowData = {};
13870
- values.forEach((val, index) => {
13871
- const key = headers[index] || `Column${index + 1}`;
13872
- rowData[key] = val;
13873
- if (key && val !== void 0 && val !== null && String(val).trim()) {
13874
- contentParts.push(`${key}: ${val}`);
13875
- }
13876
- });
13877
- if (contentParts.length > 0) {
13878
- documents.push({
13879
- docId: `${file.name}-${sheetName}-row-${i}`,
13880
- content: contentParts.join(", "),
13881
- metadata: __spreadValues(__spreadProps(__spreadValues({
13882
- fileName: file.name,
13883
- sheetName,
13884
- fileSize: file.size,
13885
- fileType: file.type || "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
13886
- uploadedAt: (/* @__PURE__ */ new Date()).toISOString()
13887
- }, dimension ? { dimension } : {}), {
13888
- headers
13889
- }), rowData)
14197
+ } else if (isExcel) {
14198
+ let excelLoaded = false;
14199
+ try {
14200
+ const XLSX = await import("xlsx");
14201
+ const workbook = XLSX.read(buffer, { type: "buffer", cellDates: true });
14202
+ for (const sheetName of workbook.SheetNames) {
14203
+ const sheet = workbook.Sheets[sheetName];
14204
+ if (!sheet) continue;
14205
+ const rawGrid = XLSX.utils.sheet_to_json(sheet, { header: 1, defval: "", blankrows: false });
14206
+ if (!rawGrid || rawGrid.length === 0) continue;
14207
+ let headerRowIndex = 0;
14208
+ while (headerRowIndex < rawGrid.length && (!rawGrid[headerRowIndex] || !rawGrid[headerRowIndex].some((c) => String(c != null ? c : "").trim().length > 0))) {
14209
+ headerRowIndex++;
14210
+ }
14211
+ if (headerRowIndex >= rawGrid.length) continue;
14212
+ const rawHeaders = rawGrid[headerRowIndex].map((h, idx) => {
14213
+ const s = String(h != null ? h : "").trim();
14214
+ return s || `Column_${idx + 1}`;
14215
+ });
14216
+ const dataRows = rawGrid.slice(headerRowIndex + 1).filter(
14217
+ (row) => row && row.some((c) => String(c != null ? c : "").trim().length > 0)
14218
+ );
14219
+ if (dataRows.length === 0) continue;
14220
+ for (let i = 0; i < dataRows.length; i += SPREADSHEET_CHUNK_SIZE) {
14221
+ const slice = dataRows.slice(i, i + SPREADSHEET_CHUNK_SIZE);
14222
+ const startRow = headerRowIndex + 1 + i + 1;
14223
+ const endRow = startRow + slice.length - 1;
14224
+ const chunkIndex = Math.floor(i / SPREADSHEET_CHUNK_SIZE) + 1;
14225
+ const lines = [
14226
+ `File: ${file.name} | Sheet: ${sheetName} (Rows ${startRow}-${endRow} of ${dataRows.length})`,
14227
+ `| ${rawHeaders.join(" | ")} |`,
14228
+ `| ${rawHeaders.map(() => "---").join(" | ")} |`
14229
+ ];
14230
+ for (const r of slice) {
14231
+ const rowValues = rawHeaders.map((_, colIdx) => {
14232
+ const val = r[colIdx];
14233
+ if (val === void 0 || val === null) return "";
14234
+ return String(val).replace(/\|/g, "\\|").replace(/\r?\n/g, " ").trim();
13890
14235
  });
14236
+ lines.push(`| ${rowValues.join(" | ")} |`);
13891
14237
  }
14238
+ fileDocs.push({
14239
+ docId: `${file.name}-${sheetName}-chunk-${chunkIndex}`,
14240
+ content: lines.join("\n"),
14241
+ metadata: __spreadValues({
14242
+ fileName: file.name,
14243
+ sheetName,
14244
+ fileSize: file.size,
14245
+ fileType: file.type || "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
14246
+ uploadedAt: (/* @__PURE__ */ new Date()).toISOString(),
14247
+ startRow,
14248
+ endRow,
14249
+ rowCount: slice.length,
14250
+ headers: rawHeaders
14251
+ }, dimension ? { dimension } : {})
14252
+ });
14253
+ }
14254
+ }
14255
+ excelLoaded = fileDocs.length > 0;
14256
+ } catch (xlsxErr) {
14257
+ excelLoaded = false;
14258
+ }
14259
+ if (!excelLoaded) {
14260
+ const content = await DocumentParser.parse(buffer, file.name, file.type);
14261
+ if (content && content.trim()) {
14262
+ fileDocs.push({
14263
+ docId: file.name,
14264
+ content,
14265
+ metadata: __spreadValues({
14266
+ fileName: file.name,
14267
+ fileSize: file.size,
14268
+ fileType: file.type || "application/vnd.ms-excel",
14269
+ uploadedAt: (/* @__PURE__ */ new Date()).toISOString()
14270
+ }, dimension ? { dimension } : {})
13892
14271
  });
13893
- });
13894
- excelLoaded = documents.length > 0;
14272
+ }
13895
14273
  }
13896
- } catch (excelErr) {
13897
- excelLoaded = false;
13898
- }
13899
- if (!excelLoaded) {
14274
+ } else {
13900
14275
  const content = await DocumentParser.parse(buffer, file.name, file.type);
13901
- documents.push({
14276
+ fileDocs.push({
13902
14277
  docId: file.name,
13903
14278
  content,
13904
14279
  metadata: __spreadValues({
13905
14280
  fileName: file.name,
13906
14281
  fileSize: file.size,
13907
- fileType: file.type || "application/vnd.ms-excel",
14282
+ fileType: file.type,
13908
14283
  uploadedAt: (/* @__PURE__ */ new Date()).toISOString()
13909
14284
  }, dimension ? { dimension } : {})
13910
14285
  });
13911
14286
  }
13912
- } else {
13913
- const content = await DocumentParser.parse(buffer, file.name, file.type);
13914
- documents.push({
13915
- docId: file.name,
13916
- content,
13917
- metadata: __spreadValues({
13918
- fileName: file.name,
13919
- fileSize: file.size,
13920
- fileType: file.type,
13921
- uploadedAt: (/* @__PURE__ */ new Date()).toISOString()
13922
- }, dimension ? { dimension } : {})
13923
- });
13924
- }
13925
- }
14287
+ return fileDocs;
14288
+ })
14289
+ );
14290
+ const documents = fileDocsArrays.flat();
13926
14291
  const results = await plugin.ingest(documents, namespaceCheck.namespace);
13927
14292
  await reportTelemetry(req, plugin, "DOCUMENT_INGESTION", "success", `Uploaded & ingested ${files.length} file(s)`);
13928
- return import_server.NextResponse.json({ message: "Upload successful", results });
14293
+ return import_server7.NextResponse.json({ message: "Upload successful", results });
13929
14294
  } catch (err) {
13930
14295
  const message = err instanceof Error ? err.message : "Upload failed";
13931
14296
  await reportTelemetry(req, plugin, "DOCUMENT_INGESTION", "error", message);
13932
- return import_server.NextResponse.json({ error: message }, { status: 500 });
14297
+ return import_server7.NextResponse.json({ error: message }, { status: 500 });
13933
14298
  }
13934
14299
  };
13935
14300
  }
14301
+
14302
+ // src/handlers/suggestionsHandler.ts
14303
+ var import_server8 = require("next/server");
13936
14304
  function createSuggestionsHandler(configOrPlugin, options) {
13937
14305
  const plugin = getOrCreatePlugin(configOrPlugin);
13938
14306
  const onAuthorize = options == null ? void 0 : options.onAuthorize;
@@ -13963,20 +14331,23 @@ function createSuggestionsHandler(configOrPlugin, options) {
13963
14331
  namespace = url.searchParams.get("namespace") || void 0;
13964
14332
  }
13965
14333
  if (!query || typeof query !== "string" || !query.trim()) {
13966
- return import_server.NextResponse.json({ suggestions: [] });
14334
+ return import_server8.NextResponse.json({ suggestions: [] });
13967
14335
  }
13968
14336
  const namespaceCheck = resolveLicensedNamespace(effectiveLicensePayload, namespace);
13969
14337
  if (!namespaceCheck.ok) return namespaceCheck.response;
13970
14338
  const suggestions = await plugin.getSuggestions(query, namespaceCheck.namespace);
13971
14339
  await reportTelemetry(req, plugin, "AUTO_SUGGESTIONS", "success", `Fetched ${suggestions.length} suggestions for: ${query.slice(0, 30)}`);
13972
- return import_server.NextResponse.json({ suggestions });
14340
+ return import_server8.NextResponse.json({ suggestions });
13973
14341
  } catch (err) {
13974
14342
  const message = err instanceof Error ? err.message : "Internal server error";
13975
14343
  await reportTelemetry(req, plugin, "AUTO_SUGGESTIONS", "error", message);
13976
- return import_server.NextResponse.json({ error: message }, { status: 500 });
14344
+ return import_server8.NextResponse.json({ error: message }, { status: 500 });
13977
14345
  }
13978
14346
  };
13979
14347
  }
14348
+
14349
+ // src/handlers/historyHandler.ts
14350
+ var import_server9 = require("next/server");
13980
14351
  function createHistoryHandler(configOrPlugin, options) {
13981
14352
  const plugin = getOrCreatePlugin(configOrPlugin);
13982
14353
  const storage = new DatabaseStorage(plugin.getConfig());
@@ -13991,25 +14362,28 @@ function createHistoryHandler(configOrPlugin, options) {
13991
14362
  if (req.method === "GET") {
13992
14363
  try {
13993
14364
  const history = await storage.getHistory(sessionId, scope);
13994
- return import_server.NextResponse.json({ history });
14365
+ return import_server9.NextResponse.json({ history });
13995
14366
  } catch (err) {
13996
14367
  const message = err instanceof Error ? err.message : "Failed to fetch history";
13997
- return import_server.NextResponse.json({ error: message }, { status: 500 });
14368
+ return import_server9.NextResponse.json({ error: message }, { status: 500 });
13998
14369
  }
13999
14370
  } else if (req.method === "POST") {
14000
14371
  try {
14001
14372
  const body = await req.json().catch(() => ({}));
14002
14373
  const sid = body.sessionId || sessionId;
14003
14374
  await storage.clearHistory(sid, scope);
14004
- return import_server.NextResponse.json({ success: true });
14375
+ return import_server9.NextResponse.json({ success: true });
14005
14376
  } catch (err) {
14006
14377
  const message = err instanceof Error ? err.message : "Failed to clear history";
14007
- return import_server.NextResponse.json({ error: message }, { status: 500 });
14378
+ return import_server9.NextResponse.json({ error: message }, { status: 500 });
14008
14379
  }
14009
14380
  }
14010
- return import_server.NextResponse.json({ error: "Method Not Allowed" }, { status: 405 });
14381
+ return import_server9.NextResponse.json({ error: "Method Not Allowed" }, { status: 405 });
14011
14382
  };
14012
14383
  }
14384
+
14385
+ // src/handlers/feedbackHandler.ts
14386
+ var import_server10 = require("next/server");
14013
14387
  function createFeedbackHandler(configOrPlugin, options) {
14014
14388
  const plugin = getOrCreatePlugin(configOrPlugin);
14015
14389
  const storage = new DatabaseStorage(plugin.getConfig());
@@ -14025,45 +14399,48 @@ function createFeedbackHandler(configOrPlugin, options) {
14025
14399
  const messageId = url.searchParams.get("messageId");
14026
14400
  if (!messageId) {
14027
14401
  const feedbackList = await storage.listFeedback(scope);
14028
- return import_server.NextResponse.json({ feedback: feedbackList });
14402
+ return import_server10.NextResponse.json({ feedback: feedbackList });
14029
14403
  }
14030
14404
  const feedback = await storage.getFeedback(messageId, scope);
14031
- return import_server.NextResponse.json({ feedback });
14405
+ return import_server10.NextResponse.json({ feedback });
14032
14406
  } catch (err) {
14033
14407
  const message = err instanceof Error ? err.message : "Failed to fetch feedback";
14034
- return import_server.NextResponse.json({ error: message }, { status: 500 });
14408
+ return import_server10.NextResponse.json({ error: message }, { status: 500 });
14035
14409
  }
14036
14410
  } else if (req.method === "POST") {
14037
14411
  try {
14038
14412
  const body = await req.json();
14039
14413
  const { messageId, sessionId = "default", rating, comment } = body;
14040
14414
  if (!messageId) {
14041
- return import_server.NextResponse.json({ error: "messageId is required" }, { status: 400 });
14415
+ return import_server10.NextResponse.json({ error: "messageId is required" }, { status: 400 });
14042
14416
  }
14043
14417
  if (!rating) {
14044
- return import_server.NextResponse.json({ error: "rating is required" }, { status: 400 });
14418
+ return import_server10.NextResponse.json({ error: "rating is required" }, { status: 400 });
14045
14419
  }
14046
14420
  if (scope && scope.length > 0) {
14047
14421
  const matches = scope.some(
14048
- (pid) => pid && (sessionId === pid || sessionId.startsWith(pid) || sessionId.includes(pid))
14422
+ (pid) => pid && (sessionId === pid || sessionId.startsWith(`${pid}:`) || sessionId.startsWith(`${pid}-`) || sessionId.startsWith(`${pid}_`))
14049
14423
  );
14050
14424
  if (!matches) {
14051
- return import_server.NextResponse.json(
14425
+ return import_server10.NextResponse.json(
14052
14426
  { error: "sessionId is outside the authorized project scope" },
14053
14427
  { status: 403 }
14054
14428
  );
14055
14429
  }
14056
14430
  }
14057
14431
  await storage.saveFeedback({ messageId, sessionId, rating, comment });
14058
- return import_server.NextResponse.json({ success: true });
14432
+ return import_server10.NextResponse.json({ success: true });
14059
14433
  } catch (err) {
14060
14434
  const message = err instanceof Error ? err.message : "Failed to save feedback";
14061
- return import_server.NextResponse.json({ error: message }, { status: 500 });
14435
+ return import_server10.NextResponse.json({ error: message }, { status: 500 });
14062
14436
  }
14063
14437
  }
14064
- return import_server.NextResponse.json({ error: "Method Not Allowed" }, { status: 405 });
14438
+ return import_server10.NextResponse.json({ error: "Method Not Allowed" }, { status: 405 });
14065
14439
  };
14066
14440
  }
14441
+
14442
+ // src/handlers/sessionsHandler.ts
14443
+ var import_server11 = require("next/server");
14067
14444
  function createSessionsHandler(configOrPlugin, options) {
14068
14445
  const plugin = getOrCreatePlugin(configOrPlugin);
14069
14446
  const storage = new DatabaseStorage(plugin.getConfig());
@@ -14077,13 +14454,16 @@ function createSessionsHandler(configOrPlugin, options) {
14077
14454
  try {
14078
14455
  const scope = req && onResolveScope ? await onResolveScope(req) : void 0;
14079
14456
  const sessions = await storage.listSessions(scope);
14080
- return import_server.NextResponse.json({ sessions });
14457
+ return import_server11.NextResponse.json({ sessions });
14081
14458
  } catch (err) {
14082
14459
  const message = err instanceof Error ? err.message : "Failed to list sessions";
14083
- return import_server.NextResponse.json({ error: message }, { status: 500 });
14460
+ return import_server11.NextResponse.json({ error: message }, { status: 500 });
14084
14461
  }
14085
14462
  };
14086
14463
  }
14464
+
14465
+ // src/handlers/ragHandler.ts
14466
+ var import_server12 = require("next/server");
14087
14467
  function createRagHandler(configOrPlugin, options) {
14088
14468
  const plugin = getOrCreatePlugin(configOrPlugin);
14089
14469
  const onAuthorize = options == null ? void 0 : options.onAuthorize;
@@ -14119,7 +14499,7 @@ function createRagHandler(configOrPlugin, options) {
14119
14499
  case "v1/license/validate":
14120
14500
  return licenseHandler(req);
14121
14501
  default:
14122
- return import_server.NextResponse.json({ error: `Not Found: POST segment "${segment}" not supported.` }, { status: 404 });
14502
+ return import_server12.NextResponse.json({ error: `Not Found: POST segment "${segment}" not supported.` }, { status: 404 });
14123
14503
  }
14124
14504
  }
14125
14505
  async function routeGetRequest(req, segment) {
@@ -14135,7 +14515,7 @@ function createRagHandler(configOrPlugin, options) {
14135
14515
  case "sessions":
14136
14516
  return sessionsHandler(req);
14137
14517
  default:
14138
- return import_server.NextResponse.json({ error: `Method Not Allowed: GET segment "${segment}" not supported.` }, { status: 405 });
14518
+ return import_server12.NextResponse.json({ error: `Method Not Allowed: GET segment "${segment}" not supported.` }, { status: 405 });
14139
14519
  }
14140
14520
  }
14141
14521
  async function getSegment(req, context) {
@@ -14149,7 +14529,8 @@ function createRagHandler(configOrPlugin, options) {
14149
14529
  const pathname = url.pathname;
14150
14530
  if (pathname.includes("/license/validate")) return "v1/license/validate";
14151
14531
  if (pathname.endsWith("/upload")) return "upload";
14152
- if (pathname.endsWith("/chat") || pathname.endsWith("/chat-sync")) return "chat";
14532
+ if (pathname.endsWith("/chat-sync")) return "chat-sync";
14533
+ if (pathname.endsWith("/chat")) return "chat";
14153
14534
  if (pathname.endsWith("/health")) return "health";
14154
14535
  if (pathname.endsWith("/suggestions")) return "suggestions";
14155
14536
  if (pathname.endsWith("/feedback")) return "feedback";
@@ -14170,7 +14551,7 @@ function createRagHandler(configOrPlugin, options) {
14170
14551
  return await routeGetRequest(req, segment);
14171
14552
  } catch (err) {
14172
14553
  const msg = err instanceof Error ? err.message : "GET Routing failed";
14173
- return import_server.NextResponse.json({ error: msg }, { status: 500 });
14554
+ return import_server12.NextResponse.json({ error: msg }, { status: 500 });
14174
14555
  }
14175
14556
  },
14176
14557
  POST: async (req, context) => {
@@ -14179,7 +14560,7 @@ function createRagHandler(configOrPlugin, options) {
14179
14560
  return await routePostRequest(req, segment);
14180
14561
  } catch (err) {
14181
14562
  const msg = err instanceof Error ? err.message : "POST Routing failed";
14182
- return import_server.NextResponse.json({ error: msg }, { status: 500 });
14563
+ return import_server12.NextResponse.json({ error: msg }, { status: 500 });
14183
14564
  }
14184
14565
  }
14185
14566
  };
@@ -14228,11 +14609,11 @@ var _LicenseValidator = class _LicenseValidator {
14228
14609
  * Primary validation entry-point for UI / hook consumers.
14229
14610
  *
14230
14611
  * Resolves the license key, consults the local success cache, performs a
14231
- * server POST for authoritative status, and falls back to local RSA
14232
- * signature verification if the server can't be reached.
14612
+ * server POST for authoritative status, and falls back to `LicenseVerifier.verifyAsync()`
14613
+ * if the server can't be reached.
14233
14614
  *
14234
14615
  * @throws LicenseValidationError - When no key is provided OR both the
14235
- * server call AND the local-RSA fallback report an invalid/expired key.
14616
+ * server call AND the fallback report an invalid/expired key.
14236
14617
  * @throws SDKVersionUnsupportedError - When server reports forceUpgrade=true.
14237
14618
  * @returns A `LicenseValidationResponse` that the UI can use to enable or
14238
14619
  * lock the chat widget.
@@ -14281,6 +14662,8 @@ var _LicenseValidator = class _LicenseValidator {
14281
14662
  this.cache.delete(effectiveLicenseKey);
14282
14663
  }
14283
14664
  }
14665
+ const existing = this.inflight.get(effectiveLicenseKey);
14666
+ if (existing) return existing;
14284
14667
  const inflightPromise = this._doNetworkValidate(
14285
14668
  effectiveLicenseKey,
14286
14669
  effectiveProjectId,
@@ -14549,8 +14932,8 @@ var LicenseConfigResolver = class {
14549
14932
  };
14550
14933
  }
14551
14934
  };
14552
- LicenseConfigResolver.RETRIVORA_CONFIG_URL = "https://www.retrivora.com/api/v1/config";
14553
- LicenseConfigResolver.RETRIVORA_CREDENTIALS_URL = "https://www.retrivora.com/api/v1/credentials";
14935
+ LicenseConfigResolver.RETRIVORA_CONFIG_URL = "https://llm.retrivora.com/api/v1/config";
14936
+ LicenseConfigResolver.RETRIVORA_CREDENTIALS_URL = "https://llm.retrivora.com/api/v1/credentials";
14554
14937
  LicenseConfigResolver.CACHE_TTL_MS = 36e5;
14555
14938
  // 1 hour
14556
14939
  LicenseConfigResolver.configCache = /* @__PURE__ */ new Map();
@@ -14714,6 +15097,9 @@ var _TelemetryService = class _TelemetryService {
14714
15097
  console.warn("[TelemetryService] Auto-flush error:", err.message);
14715
15098
  });
14716
15099
  }, this.flushIntervalMs);
15100
+ if (this.flushTimer && typeof this.flushTimer.unref === "function") {
15101
+ this.flushTimer.unref();
15102
+ }
14717
15103
  }
14718
15104
  /**
14719
15105
  * Stop telemetry service and flush remaining events
@@ -14987,6 +15373,7 @@ async function createRetrivora(options) {
14987
15373
  IntentClassifier,
14988
15374
  LLMFactory,
14989
15375
  LLM_PROFILES,
15376
+ LangChainAgent,
14990
15377
  LicenseConfigResolver,
14991
15378
  LicenseValidationError,
14992
15379
  LicenseValidator,
@@ -15046,10 +15433,12 @@ async function createRetrivora(options) {
15046
15433
  createHealthHandler,
15047
15434
  createHistoryHandler,
15048
15435
  createIngestHandler,
15436
+ createLicenseHandler,
15049
15437
  createRagHandler,
15050
15438
  createRetrivora,
15051
15439
  createSessionsHandler,
15052
15440
  createStreamHandler,
15441
+ createSuggestionsHandler,
15053
15442
  createUploadHandler,
15054
15443
  decideVisualization,
15055
15444
  getRagConfig,
@@ -15063,6 +15452,8 @@ async function createRetrivora(options) {
15063
15452
  sseErrorFrame,
15064
15453
  sseFrame,
15065
15454
  sseMetaFrame,
15455
+ sseObservabilityFrame,
15066
15456
  sseTextFrame,
15457
+ sseUIFrame,
15067
15458
  wrapError
15068
15459
  });