@retrivora-ai/rag-engine 2.6.0 → 2.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/server.mjs CHANGED
@@ -20,12 +20,6 @@ var __spreadValues = (a, b) => {
20
20
  return a;
21
21
  };
22
22
  var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
23
- var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
24
- get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
25
- }) : x)(function(x) {
26
- if (typeof require !== "undefined") return require.apply(this, arguments);
27
- throw Error('Dynamic require of "' + x + '" is not supported');
28
- });
29
23
  var __objRest = (source, exclude) => {
30
24
  var target = {};
31
25
  for (var prop in source)
@@ -326,7 +320,7 @@ var init_package = __esm({
326
320
  "package.json"() {
327
321
  package_default = {
328
322
  name: "@retrivora-ai/rag-engine",
329
- version: "2.6.0",
323
+ version: "2.6.2",
330
324
  description: "Retrivora AI is a plug-and-play AI engine for RAG chat experiences \u2014 generic vector DB + LLM provider, embeddable or standalone.",
331
325
  author: "Abhinav Alkuchi",
332
326
  license: "UNLICENSED",
@@ -505,7 +499,7 @@ var init_ConfigFetcher = __esm({
505
499
  init_ProjectSettings();
506
500
  init_version();
507
501
  CONTROL_PLANE_URL = "https://www.retrivora.com";
508
- LLM_PROXY_BASE_URL = "https://www.retrivora.com/api/v1";
502
+ LLM_PROXY_BASE_URL = "https://llm.retrivora.com/api/v1";
509
503
  ConfigFetcher = class {
510
504
  // 5-minute in-memory session cache
511
505
  /**
@@ -2761,13 +2755,14 @@ function wrapError(err, defaultCode, defaultMessage) {
2761
2755
  const message = (error == null ? void 0 : error.message) || defaultMessage || String(err);
2762
2756
  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);
2763
2757
  const code = error == null ? void 0 : error.code;
2764
- if (status === 429 || /rate[- ]?limit/i.test(message) || code === "RATE_LIMIT_EXCEEDED") {
2758
+ if (status === 429 || /rate[- ]?limit|token limit|too many requests|tokens per minute/i.test(message) || code === "RATE_LIMIT_EXCEEDED") {
2765
2759
  return new RateLimitException(message, err);
2766
2760
  }
2767
- 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") {
2768
- const isLicenseError = /403|license|revoked|terminated/i.test(message) || status === 403;
2769
- const cleanMsg = isLicenseError ? "Your Retrivora license key has been terminated, suspended, or revoked. Access denied." : message;
2770
- return new AuthenticationException(cleanMsg, err);
2761
+ 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)) {
2762
+ return new AuthenticationException("Your Retrivora license key has been terminated, suspended, or revoked. Access denied.", err);
2763
+ }
2764
+ if (status === 401 || status === 403 || code === "INVALID_API_KEY" || code === "AUTHENTICATION_ERROR" || /unauthorized|forbidden|invalid api key/i.test(message)) {
2765
+ return new AuthenticationException(message, err);
2771
2766
  }
2772
2767
  switch (defaultCode) {
2773
2768
  case "PROVIDER_NOT_FOUND":
@@ -2788,6 +2783,20 @@ function wrapError(err, defaultCode, defaultMessage) {
2788
2783
  }
2789
2784
 
2790
2785
  // src/core/LicenseVerifier.ts
2786
+ function decodeBase64UrlToUtf8(b64url) {
2787
+ const normalized = b64url.replace(/-/g, "+").replace(/_/g, "/");
2788
+ const padded = normalized + "=".repeat((4 - normalized.length % 4) % 4);
2789
+ if (typeof Buffer !== "undefined" && typeof Buffer.from === "function") {
2790
+ return Buffer.from(padded, "base64").toString("utf8");
2791
+ }
2792
+ if (typeof atob === "function") {
2793
+ const binary = atob(padded);
2794
+ const bytes = new Uint8Array(binary.length);
2795
+ for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
2796
+ return new TextDecoder().decode(bytes);
2797
+ }
2798
+ throw new Error("Base64 decoding is not supported in this environment.");
2799
+ }
2791
2800
  var _LicenseVerifier = class _LicenseVerifier {
2792
2801
  // Cache verified license for 5 minutes
2793
2802
  /**
@@ -2919,6 +2928,9 @@ var _LicenseVerifier = class _LicenseVerifier {
2919
2928
  minimumSupportedVersion: data.minimumSupportedVersion,
2920
2929
  forceUpgrade: data.forceUpgrade
2921
2930
  };
2931
+ if (payload.expiresAt && Math.floor(Date.now() / 1e3) > payload.expiresAt) {
2932
+ throw new Error("License key has expired.");
2933
+ }
2922
2934
  if (isProduction && provider) {
2923
2935
  const normalizedProvider = provider.toLowerCase();
2924
2936
  if (tier === "hobby") {
@@ -2957,63 +2969,67 @@ var _LicenseVerifier = class _LicenseVerifier {
2957
2969
  }
2958
2970
  }
2959
2971
  static async verifyIP(licenseKey) {
2972
+ var _a2, _b;
2960
2973
  if (!licenseKey) return;
2961
2974
  try {
2962
2975
  const parts = licenseKey.split(".");
2963
2976
  if (parts.length !== 3) return;
2964
2977
  const [, payloadB64] = parts;
2965
2978
  const payload = JSON.parse(
2966
- Buffer.from(payloadB64, "base64url").toString("utf8")
2979
+ decodeBase64UrlToUtf8(payloadB64)
2967
2980
  );
2968
2981
  const tier = (payload.tier || "").toLowerCase();
2969
- if (tier === "enterprise" || tier === "custom") {
2982
+ if (tier === "enterprise" || tier === "custom" || !payload.ipv4 && !payload.ipv6) {
2970
2983
  return;
2971
2984
  }
2972
- if (payload.ipv4 || payload.ipv6) {
2973
- let currentIpv4 = "";
2974
- let currentIpv6 = "";
2975
- try {
2976
- const res = await fetch("https://api4.ipify.org?format=json", { signal: AbortSignal.timeout(3e3) });
2977
- const data = await res.json();
2978
- currentIpv4 = data.ip;
2979
- } catch (e) {
2985
+ let currentIpv4 = "";
2986
+ let currentIpv6 = "";
2987
+ const now = Date.now();
2988
+ if (this.cachedIps && now - this.cachedIps.fetchedAt < 10 * 60 * 1e3) {
2989
+ currentIpv4 = this.cachedIps.ipv4;
2990
+ currentIpv6 = this.cachedIps.ipv6;
2991
+ } else {
2992
+ const [v4Result, v6Result] = await Promise.allSettled([
2993
+ fetch("https://api4.ipify.org?format=json", { signal: AbortSignal.timeout(2e3) }).then((r) => r.json()),
2994
+ fetch("https://api6.ipify.org?format=json", { signal: AbortSignal.timeout(2e3) }).then((r) => r.json())
2995
+ ]);
2996
+ if (v4Result.status === "fulfilled" && ((_a2 = v4Result.value) == null ? void 0 : _a2.ip)) {
2997
+ currentIpv4 = v4Result.value.ip;
2980
2998
  }
2981
- try {
2982
- const res = await fetch("https://api6.ipify.org?format=json", { signal: AbortSignal.timeout(3e3) });
2983
- const data = await res.json();
2984
- currentIpv6 = data.ip;
2985
- } catch (e) {
2999
+ if (v6Result.status === "fulfilled" && ((_b = v6Result.value) == null ? void 0 : _b.ip)) {
3000
+ currentIpv6 = v6Result.value.ip;
2986
3001
  }
2987
- const localIps = [];
2988
- try {
2989
- const osModule = __require("os");
2990
- const interfaces = osModule.networkInterfaces();
2991
- for (const name of Object.keys(interfaces)) {
2992
- for (const iface of interfaces[name] || []) {
2993
- if (!iface.internal) {
2994
- localIps.push(iface.address);
2995
- }
3002
+ this.cachedIps = { ipv4: currentIpv4, ipv6: currentIpv6, fetchedAt: now };
3003
+ }
3004
+ const localIps = [];
3005
+ try {
3006
+ const osModule = await import("os");
3007
+ const interfaces = osModule.networkInterfaces();
3008
+ for (const name of Object.keys(interfaces)) {
3009
+ for (const iface of interfaces[name] || []) {
3010
+ if (!iface.internal) {
3011
+ localIps.push(iface.address);
2996
3012
  }
2997
3013
  }
2998
- } catch (e) {
2999
3014
  }
3000
- const isIpv4Match = payload.ipv4 && (currentIpv4 === payload.ipv4 || localIps.includes(payload.ipv4));
3001
- const isIpv6Match = payload.ipv6 && (currentIpv6 === payload.ipv6 || localIps.includes(payload.ipv6));
3002
- if (payload.ipv4 && payload.ipv6) {
3003
- if (!isIpv4Match && !isIpv6Match) {
3004
- throw new Error(
3005
- `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.`
3006
- );
3007
- }
3008
- } else if (payload.ipv4 && !isIpv4Match) {
3009
- throw new Error(
3010
- `License key access restricted. This license key was created for a different system (authorized IPv4: ${payload.ipv4}) and cannot be used on this machine.`
3011
- );
3012
- } else if (payload.ipv6 && !isIpv6Match) {
3015
+ } catch (e) {
3016
+ }
3017
+ const isIpv4Match = payload.ipv4 && (currentIpv4 === payload.ipv4 || localIps.includes(payload.ipv4));
3018
+ const isIpv6Match = payload.ipv6 && (currentIpv6 === payload.ipv6 || localIps.includes(payload.ipv6));
3019
+ if (payload.ipv4 && payload.ipv6) {
3020
+ if (!isIpv4Match && !isIpv6Match) {
3013
3021
  throw new Error(
3014
- `License key access restricted. This license key was created for a different system (authorized IPv6: ${payload.ipv6}) and cannot be used on this machine.`
3022
+ `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.`
3015
3023
  );
3016
3024
  }
3025
+ } else if (payload.ipv4 && !isIpv4Match) {
3026
+ throw new Error(
3027
+ `License key access restricted. This license key was created for a different system (authorized IPv4: ${payload.ipv4}) and cannot be used on this machine.`
3028
+ );
3029
+ } else if (payload.ipv6 && !isIpv6Match) {
3030
+ throw new Error(
3031
+ `License key access restricted. This license key was created for a different system (authorized IPv6: ${payload.ipv6}) and cannot be used on this machine.`
3032
+ );
3017
3033
  }
3018
3034
  } catch (err) {
3019
3035
  if (err.message.includes("License key access restricted")) {
@@ -3028,7 +3044,7 @@ var _LicenseVerifier = class _LicenseVerifier {
3028
3044
  const rawToken = sanitizedKey.replace(/^rtv_/i, "").trim();
3029
3045
  const parts = rawToken.split(".");
3030
3046
  if (parts.length === 3) {
3031
- const decodedPayload = JSON.parse(Buffer.from(parts[1], "base64url").toString("utf8"));
3047
+ const decodedPayload = JSON.parse(decodeBase64UrlToUtf8(parts[1]));
3032
3048
  if (decodedPayload.tier) return decodedPayload.tier;
3033
3049
  }
3034
3050
  } catch (e) {
@@ -3039,6 +3055,7 @@ var _LicenseVerifier = class _LicenseVerifier {
3039
3055
  // In-memory cache to save network overhead on hot paths.
3040
3056
  _LicenseVerifier.cache = /* @__PURE__ */ new Map();
3041
3057
  _LicenseVerifier.CACHE_TTL_MS = 5 * 60 * 1e3;
3058
+ _LicenseVerifier.cachedIps = null;
3042
3059
  var LicenseVerifier = _LicenseVerifier;
3043
3060
 
3044
3061
  // src/config/constants.ts
@@ -4294,7 +4311,7 @@ var GroqProvider = class {
4294
4311
  };
4295
4312
  }
4296
4313
  async chat(messages, context, options) {
4297
- var _a2, _b, _c, _d, _e, _f, _g2, _h, _i;
4314
+ var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k, _l;
4298
4315
  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.";
4299
4316
  const systemMessage = {
4300
4317
  role: "system",
@@ -4308,18 +4325,46 @@ var GroqProvider = class {
4308
4325
  systemMessage,
4309
4326
  ...safeMessages
4310
4327
  ];
4311
- const completion = await this.client.chat.completions.create({
4312
- model: this.llmConfig.model,
4313
- messages: formattedMessages,
4314
- max_tokens: (_d = (_c = options == null ? void 0 : options.maxTokens) != null ? _c : this.llmConfig.maxTokens) != null ? _d : 1024,
4315
- temperature: (_f = (_e = options == null ? void 0 : options.temperature) != null ? _e : this.llmConfig.temperature) != null ? _f : 0.7,
4316
- stop: options == null ? void 0 : options.stop
4317
- });
4318
- return (_i = (_h = (_g2 = completion.choices[0]) == null ? void 0 : _g2.message) == null ? void 0 : _h.content) != null ? _i : "";
4328
+ const model = this.llmConfig.model === "qwen/qwen3.6-27b" || this.llmConfig.model === "qwen-3.6-27b" ? "qwen/qwen3.8-27b" : this.llmConfig.model;
4329
+ try {
4330
+ const completion = await this.client.chat.completions.create({
4331
+ model,
4332
+ messages: formattedMessages,
4333
+ max_tokens: Math.min((_d = (_c = options == null ? void 0 : options.maxTokens) != null ? _c : this.llmConfig.maxTokens) != null ? _d : 800, 800),
4334
+ temperature: (_f = (_e = options == null ? void 0 : options.temperature) != null ? _e : this.llmConfig.temperature) != null ? _f : 0.7,
4335
+ stop: options == null ? void 0 : options.stop
4336
+ });
4337
+ return (_i = (_h = (_g2 = completion.choices[0]) == null ? void 0 : _g2.message) == null ? void 0 : _h.content) != null ? _i : "";
4338
+ } catch (err) {
4339
+ console.warn("[GroqProvider] Groq chat request failed, falling back strictly to Hugging Face Qwen:", (err == null ? void 0 : err.message) || err);
4340
+ const hfToken = process.env.HF_TOKEN || process.env.HUGGINGFACE_API_KEY || "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";
4341
+ try {
4342
+ const hfRes = await fetch("https://router.huggingface.co/v1/chat/completions", {
4343
+ method: "POST",
4344
+ headers: {
4345
+ "Content-Type": "application/json",
4346
+ "Authorization": `Bearer ${hfToken}`
4347
+ },
4348
+ body: JSON.stringify({
4349
+ model: "Qwen/Qwen2.5-Coder-32B-Instruct",
4350
+ messages: formattedMessages,
4351
+ max_tokens: 1024,
4352
+ temperature: 0.7
4353
+ })
4354
+ });
4355
+ if (hfRes.ok) {
4356
+ const data = await hfRes.json();
4357
+ 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;
4358
+ if (content !== void 0) return content;
4359
+ }
4360
+ } catch (e) {
4361
+ }
4362
+ throw err;
4363
+ }
4319
4364
  }
4320
4365
  chatStream(messages, context, options) {
4321
4366
  return __asyncGenerator(this, null, function* () {
4322
- var _a2, _b, _c, _d, _e, _f, _g2, _h;
4367
+ var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k, _l;
4323
4368
  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.";
4324
4369
  const systemMessage = {
4325
4370
  role: "system",
@@ -4333,32 +4378,81 @@ var GroqProvider = class {
4333
4378
  systemMessage,
4334
4379
  ...safeStreamMessages
4335
4380
  ];
4336
- const stream = yield new __await(this.client.chat.completions.create({
4337
- model: this.llmConfig.model,
4338
- messages: formattedMessages,
4339
- max_tokens: (_d = (_c = options == null ? void 0 : options.maxTokens) != null ? _c : this.llmConfig.maxTokens) != null ? _d : 1024,
4340
- temperature: (_f = (_e = options == null ? void 0 : options.temperature) != null ? _e : this.llmConfig.temperature) != null ? _f : 0.7,
4341
- stop: options == null ? void 0 : options.stop,
4342
- stream: true
4343
- }));
4344
- if (!stream) {
4345
- throw new Error("[GroqProvider] completions.create stream is undefined");
4346
- }
4381
+ const model = this.llmConfig.model === "qwen/qwen3.6-27b" || this.llmConfig.model === "qwen-3.6-27b" ? "qwen/qwen3.8-27b" : this.llmConfig.model;
4347
4382
  try {
4348
- for (var iter = __forAwait(stream), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
4349
- const chunk = temp.value;
4350
- const content = ((_h = (_g2 = chunk.choices[0]) == null ? void 0 : _g2.delta) == null ? void 0 : _h.content) || "";
4351
- if (content) yield content;
4383
+ const stream = yield new __await(this.client.chat.completions.create({
4384
+ model,
4385
+ messages: formattedMessages,
4386
+ max_tokens: Math.min((_d = (_c = options == null ? void 0 : options.maxTokens) != null ? _c : this.llmConfig.maxTokens) != null ? _d : 800, 800),
4387
+ temperature: (_f = (_e = options == null ? void 0 : options.temperature) != null ? _e : this.llmConfig.temperature) != null ? _f : 0.7,
4388
+ stop: options == null ? void 0 : options.stop,
4389
+ stream: true
4390
+ }));
4391
+ if (!stream) {
4392
+ throw new Error("[GroqProvider] completions.create stream is undefined");
4352
4393
  }
4353
- } catch (temp) {
4354
- error = [temp];
4355
- } finally {
4356
4394
  try {
4357
- more && (temp = iter.return) && (yield new __await(temp.call(iter)));
4395
+ for (var iter = __forAwait(stream), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
4396
+ const chunk = temp.value;
4397
+ const content = ((_h = (_g2 = chunk.choices[0]) == null ? void 0 : _g2.delta) == null ? void 0 : _h.content) || "";
4398
+ if (content) yield content;
4399
+ }
4400
+ } catch (temp) {
4401
+ error = [temp];
4358
4402
  } finally {
4359
- if (error)
4360
- throw error[0];
4403
+ try {
4404
+ more && (temp = iter.return) && (yield new __await(temp.call(iter)));
4405
+ } finally {
4406
+ if (error)
4407
+ throw error[0];
4408
+ }
4409
+ }
4410
+ } catch (err) {
4411
+ console.warn("[GroqProvider] Groq chat stream failed, falling back strictly to Hugging Face Qwen stream:", (err == null ? void 0 : err.message) || err);
4412
+ const hfToken = process.env.HF_TOKEN || process.env.HUGGINGFACE_API_KEY || "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";
4413
+ try {
4414
+ const hfRes = yield new __await(fetch("https://router.huggingface.co/v1/chat/completions", {
4415
+ method: "POST",
4416
+ headers: {
4417
+ "Content-Type": "application/json",
4418
+ "Authorization": `Bearer ${hfToken}`
4419
+ },
4420
+ body: JSON.stringify({
4421
+ model: "Qwen/Qwen2.5-Coder-32B-Instruct",
4422
+ messages: formattedMessages,
4423
+ max_tokens: 1024,
4424
+ temperature: 0.7,
4425
+ stream: true
4426
+ })
4427
+ }));
4428
+ if (hfRes.ok && hfRes.body) {
4429
+ const reader = hfRes.body.getReader();
4430
+ const decoder = new TextDecoder("utf-8");
4431
+ let buffer = "";
4432
+ while (true) {
4433
+ const { done, value } = yield new __await(reader.read());
4434
+ if (done) break;
4435
+ buffer += decoder.decode(value, { stream: true });
4436
+ const lines = buffer.split("\n");
4437
+ buffer = (_i = lines.pop()) != null ? _i : "";
4438
+ for (const line of lines) {
4439
+ const trimmed = line.trim();
4440
+ if (!trimmed || trimmed === "data: [DONE]") continue;
4441
+ if (!trimmed.startsWith("data:")) continue;
4442
+ try {
4443
+ const json = JSON.parse(trimmed.slice(5).trim());
4444
+ 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) || "";
4445
+ if (content) yield content;
4446
+ } catch (e) {
4447
+ }
4448
+ }
4449
+ }
4450
+ return;
4451
+ }
4452
+ } catch (hfErr) {
4453
+ console.warn("[GroqProvider] Hugging Face Qwen fallback stream failed:", hfErr);
4361
4454
  }
4455
+ throw err;
4362
4456
  }
4363
4457
  });
4364
4458
  }
@@ -4808,6 +4902,7 @@ ${context != null ? context : "None"}` },
4808
4902
  } catch (fetchErr) {
4809
4903
  console.warn(`[UniversalLLMAdapter] Remote HTTP stream fetch warning: ${fetchErr == null ? void 0 : fetchErr.message}. Attempting in-process fallback...`);
4810
4904
  }
4905
+ let lastDispatchErr = null;
4811
4906
  if (!streamBody) {
4812
4907
  const _g2 = globalThis;
4813
4908
  const dispatch = _g2.__retrivoraDispatchChat;
@@ -4830,12 +4925,16 @@ ${context != null ? context : "None"}` },
4830
4925
  }
4831
4926
  }
4832
4927
  } catch (dispatchErr) {
4833
- console.warn("[UniversalLLMAdapter] In-process dispatch error:", dispatchErr);
4928
+ lastDispatchErr = dispatchErr;
4929
+ console.warn("[UniversalLLMAdapter] In-process dispatch error:", (dispatchErr == null ? void 0 : dispatchErr.message) || dispatchErr);
4834
4930
  }
4835
4931
  }
4836
4932
  }
4837
4933
  if (!streamBody) {
4838
- throw new Error(`[UniversalLLMAdapter] Streaming request failed. Ensure RETRIVORA_LICENSE_KEY is valid or LLM service is available.`);
4934
+ if (lastDispatchErr) {
4935
+ throw lastDispatchErr;
4936
+ }
4937
+ throw new Error(`[UniversalLLMAdapter] Streaming request failed: LLM provider unavailable.`);
4839
4938
  }
4840
4939
  const reader = streamBody.getReader();
4841
4940
  const decoder = new TextDecoder("utf-8");
@@ -4874,72 +4973,74 @@ ${context != null ? context : "None"}` },
4874
4973
  });
4875
4974
  }
4876
4975
  async embed(text) {
4877
- var _a2, _b, _c, _d, _e, _f, _g2, _h;
4878
- const path2 = (_a2 = this.opts.embedPath) != null ? _a2 : "/embeddings";
4976
+ var _a2, _b, _c, _d, _e, _f, _g3, _h;
4977
+ const _g2 = globalThis;
4978
+ const dispatchEmbed = _g2.__retrivoraDispatchEmbedding;
4979
+ if (typeof dispatchEmbed === "function") {
4980
+ try {
4981
+ const res = await dispatchEmbed({ input: text, model: this.model }, this.apiKey);
4982
+ if ((_b = (_a2 = res == null ? void 0 : res.data) == null ? void 0 : _a2[0]) == null ? void 0 : _b.embedding) {
4983
+ return res.data[0].embedding;
4984
+ }
4985
+ } catch (dispatchErr) {
4986
+ }
4987
+ }
4988
+ const path2 = (_c = this.opts.embedPath) != null ? _c : "/embeddings";
4879
4989
  const payload = this.opts.embedPayloadTemplate ? buildPayload(this.opts.embedPayloadTemplate, { input: text, model: this.model }) : { input: text, model: this.model };
4880
4990
  try {
4881
4991
  const { data } = await this.http.post(path2, payload);
4882
- const extractPath = (_b = this.opts.embedExtractPath) != null ? _b : "data[0].embedding";
4992
+ const extractPath = (_d = this.opts.embedExtractPath) != null ? _d : "data[0].embedding";
4883
4993
  const vector = resolvePath(data, extractPath);
4884
4994
  if (Array.isArray(vector)) {
4885
4995
  return vector;
4886
4996
  }
4887
- console.warn(
4888
- `[UniversalLLMAdapter] Embedding API returned unexpected format for path '${extractPath}'. Response:`,
4889
- data
4890
- );
4891
4997
  } catch (err) {
4892
- console.warn(
4893
- `[UniversalLLMAdapter] HTTP embedding request failed: ${(err == null ? void 0 : err.message) || err}`,
4894
- { path: path2, model: this.model, response: (_c = err == null ? void 0 : err.response) == null ? void 0 : _c.data }
4895
- );
4896
- const _g3 = globalThis;
4897
- const dispatchEmbed = _g3.__retrivoraDispatchEmbedding;
4898
4998
  if (typeof dispatchEmbed === "function") {
4899
4999
  try {
4900
5000
  const res = await dispatchEmbed({ input: text, model: this.model }, this.apiKey);
4901
- if ((_e = (_d = res == null ? void 0 : res.data) == null ? void 0 : _d[0]) == null ? void 0 : _e.embedding) {
5001
+ if ((_f = (_e = res == null ? void 0 : res.data) == null ? void 0 : _e[0]) == null ? void 0 : _f.embedding) {
4902
5002
  return res.data[0].embedding;
4903
5003
  }
4904
5004
  } catch (dispatchErr) {
4905
5005
  console.warn("[UniversalLLMAdapter] In-process dispatch embedding failed:", dispatchErr);
4906
5006
  }
4907
5007
  }
4908
- }
4909
- try {
4910
- const { data } = await this.http.post(path2, payload);
4911
- const extractPath = (_f = this.opts.embedExtractPath) != null ? _f : "data[0].embedding";
4912
- const vector = resolvePath(data, extractPath);
4913
- if (Array.isArray(vector)) {
4914
- return vector;
4915
- }
4916
- console.error(
4917
- `[UniversalLLMAdapter] Retry embedding failed: unexpected response format for path '${extractPath}'. Response:`,
4918
- data
4919
- );
4920
- throw new Error(
4921
- `[UniversalLLMAdapter] Could not extract embedding vector. Expected array at '${extractPath}', got: ${typeof vector}. Response keys: ${Object.keys(data || {})}.`
4922
- );
4923
- } catch (err) {
4924
5008
  console.error(
4925
- `[UniversalLLMAdapter] Both embedding attempts failed. Path: ${path2}, Model: ${this.model}`,
4926
- { 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 }
5009
+ `[UniversalLLMAdapter] Embedding failed. Path: ${path2}, Model: ${this.model}`,
5010
+ { 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 }
4927
5011
  );
4928
5012
  throw new Error(
4929
- `[UniversalLLMAdapter] Failed to generate embedding after 2 attempts: ${(err == null ? void 0 : err.message) || err}`
5013
+ `[UniversalLLMAdapter] Failed to generate embedding: ${(err == null ? void 0 : err.message) || err}`
4930
5014
  );
4931
5015
  }
5016
+ throw new Error(`[UniversalLLMAdapter] Failed to extract embedding vector.`);
4932
5017
  }
4933
5018
  async batchEmbed(texts) {
4934
- const results = [];
4935
- for (const text of texts) {
5019
+ var _a2;
5020
+ if (!texts || texts.length === 0) return [];
5021
+ const _g2 = globalThis;
5022
+ const dispatchEmbed = _g2.__retrivoraDispatchEmbedding;
5023
+ if (typeof dispatchEmbed === "function") {
4936
5024
  try {
4937
- const vector = await this.embed(text);
4938
- results.push(vector);
4939
- } catch (err) {
4940
- console.warn(`[UniversalLLMAdapter] batchEmbed failed for text (first 50 chars): ${text.slice(0, 50)}...`, err);
4941
- throw err;
5025
+ const res = await dispatchEmbed({ input: texts, model: this.model }, this.apiKey);
5026
+ if ((res == null ? void 0 : res.data) && Array.isArray(res.data) && res.data.length === texts.length) {
5027
+ return res.data.map((d) => d.embedding);
5028
+ }
5029
+ } catch (e) {
5030
+ }
5031
+ }
5032
+ const path2 = (_a2 = this.opts.embedPath) != null ? _a2 : "/embeddings";
5033
+ try {
5034
+ const { data } = await this.http.post(path2, { input: texts, model: this.model });
5035
+ if ((data == null ? void 0 : data.data) && Array.isArray(data.data) && data.data.length === texts.length) {
5036
+ return data.data.map((d) => d.embedding);
4942
5037
  }
5038
+ } catch (e) {
5039
+ }
5040
+ const results = [];
5041
+ for (const text of texts) {
5042
+ const vector = await this.embed(text);
5043
+ results.push(vector);
4943
5044
  }
4944
5045
  return results;
4945
5046
  }
@@ -5473,9 +5574,6 @@ var ConfigValidator = class {
5473
5574
  }
5474
5575
  };
5475
5576
 
5476
- // src/core/Pipeline.ts
5477
- init_version();
5478
-
5479
5577
  // src/rag/DocumentChunker.ts
5480
5578
  var DocumentChunker = class {
5481
5579
  constructor(chunkSize = 1e3, chunkOverlap = 200, separators = ["\n# ", "\n## ", "\n### ", "\n#### ", "\n\n", "\n", " ", ""]) {
@@ -6418,6 +6516,73 @@ ${error instanceof Error ? error.message : String(error)}`
6418
6516
  }
6419
6517
  return String((_c = (_b = response == null ? void 0 : response.output) != null ? _b : response) != null ? _c : "");
6420
6518
  }
6519
+ /**
6520
+ * Run the agentic flow in streaming mode, emitting granular SSE event frames.
6521
+ * Leverages LangChain/LangGraph `streamEvents` (v2 API) to stream:
6522
+ * - Token-by-token LLM output (`type: "text"`)
6523
+ * - Tool invocation / thinking steps (`type: "thinking"`)
6524
+ * - Tool execution metadata & search sources
6525
+ */
6526
+ streamRun(_0) {
6527
+ return __asyncGenerator(this, arguments, function* (input, chatHistory = []) {
6528
+ var _a2;
6529
+ if (!this.agent) {
6530
+ throw new Error("[LangChainAgent] Agent not initialized. Call initialize() first.");
6531
+ }
6532
+ const { HumanMessage, AIMessage } = yield new __await(import("@langchain/core/messages"));
6533
+ const historyMessages = chatHistory.map(
6534
+ (m) => m.role === "user" ? new HumanMessage(m.content) : new AIMessage(m.content)
6535
+ );
6536
+ const inputMessages = [...historyMessages, new HumanMessage(input)];
6537
+ if (typeof this.agent.streamEvents === "function") {
6538
+ const eventStream = this.agent.streamEvents(
6539
+ { messages: inputMessages },
6540
+ { version: "v2" }
6541
+ );
6542
+ try {
6543
+ for (var iter = __forAwait(eventStream), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
6544
+ const event = temp.value;
6545
+ if (event.event === "on_tool_start") {
6546
+ const toolName = event.name || "document_search";
6547
+ yield {
6548
+ type: "thinking",
6549
+ text: `Analyzing repository with tool "${toolName}"...
6550
+ `
6551
+ };
6552
+ } else if (event.event === "on_tool_end") {
6553
+ const toolName = event.name || "document_search";
6554
+ yield {
6555
+ type: "thinking",
6556
+ text: `Tool "${toolName}" completed successfully.
6557
+ `
6558
+ };
6559
+ } else if (event.event === "on_chat_model_stream") {
6560
+ const chunk = (_a2 = event.data) == null ? void 0 : _a2.chunk;
6561
+ if (chunk == null ? void 0 : chunk.content) {
6562
+ const tokenText = typeof chunk.content === "string" ? chunk.content : JSON.stringify(chunk.content);
6563
+ yield {
6564
+ type: "text",
6565
+ text: tokenText
6566
+ };
6567
+ }
6568
+ }
6569
+ }
6570
+ } catch (temp) {
6571
+ error = [temp];
6572
+ } finally {
6573
+ try {
6574
+ more && (temp = iter.return) && (yield new __await(temp.call(iter)));
6575
+ } finally {
6576
+ if (error)
6577
+ throw error[0];
6578
+ }
6579
+ }
6580
+ } else {
6581
+ const result = yield new __await(this.run(input, chatHistory));
6582
+ yield { type: "text", text: result };
6583
+ }
6584
+ });
6585
+ }
6421
6586
  };
6422
6587
 
6423
6588
  // src/rag/PromptOptimizer.ts
@@ -7325,25 +7490,43 @@ ${errorMessages}`
7325
7490
  * Processes up to `concurrency` items at the same time.
7326
7491
  */
7327
7492
  static async processConcurrent(items, processor, concurrency = 5, options) {
7493
+ var _a2, _b, _c, _d;
7328
7494
  const { throwOnPartialFailure = false } = options != null ? options : {};
7329
7495
  const results = [];
7330
7496
  const errors = [];
7497
+ const maxRetries = (_a2 = options == null ? void 0 : options.maxRetries) != null ? _a2 : 3;
7498
+ const initialDelayMs = (_b = options == null ? void 0 : options.initialDelayMs) != null ? _b : 100;
7499
+ const maxDelayMs = (_c = options == null ? void 0 : options.maxDelayMs) != null ? _c : 1e4;
7500
+ const backoffMultiplier = (_d = options == null ? void 0 : options.backoffMultiplier) != null ? _d : 2;
7331
7501
  for (let i = 0; i < items.length; i += concurrency) {
7332
7502
  const chunk = items.slice(i, i + concurrency).map((item, idx) => ({
7333
7503
  item,
7334
7504
  originalIndex: i + idx
7335
7505
  }));
7336
7506
  const chunkPromises = chunk.map(async ({ item, originalIndex }) => {
7337
- try {
7338
- const result = await _BatchProcessor.executeWithCircuitBreaker(
7339
- () => processor(item),
7340
- options == null ? void 0 : options.circuitBreaker
7341
- );
7342
- return { success: true, result, index: originalIndex };
7343
- } catch (err) {
7344
- const error = err instanceof Error ? err : new Error(String(err));
7345
- return { success: false, error, index: originalIndex };
7507
+ let lastErr;
7508
+ for (let attempt = 0; attempt <= maxRetries; attempt++) {
7509
+ try {
7510
+ const result = await _BatchProcessor.executeWithCircuitBreaker(
7511
+ () => processor(item),
7512
+ options == null ? void 0 : options.circuitBreaker
7513
+ );
7514
+ return { success: true, result, index: originalIndex };
7515
+ } catch (err) {
7516
+ lastErr = err instanceof Error ? err : new Error(String(err));
7517
+ if (!isTransientError(err) || attempt === maxRetries) {
7518
+ break;
7519
+ }
7520
+ const delay = calculateBackoffDelay(
7521
+ attempt,
7522
+ initialDelayMs,
7523
+ maxDelayMs,
7524
+ backoffMultiplier
7525
+ );
7526
+ await sleep(delay);
7527
+ }
7346
7528
  }
7529
+ return { success: false, error: lastErr || new Error("Unknown error"), index: originalIndex };
7347
7530
  });
7348
7531
  const chunkResults = await Promise.all(chunkPromises);
7349
7532
  for (const { success, result, error, index } of chunkResults) {
@@ -7816,14 +7999,14 @@ var IntentClassifier = class {
7816
7999
  static classify(context) {
7817
8000
  const q = (context.userQuery || "").toLowerCase().trim();
7818
8001
  const signals = this.extractDataSignals(context);
7819
- if (this.isInformationLookup(q)) {
7820
- return { intent: "information_lookup", signals };
8002
+ if (this.isProductQuery(q) || signals.isProductLike && (this.isRecommendationQuery(q) || this.isDiscoveryQuery(q))) {
8003
+ return { intent: "product_search", signals };
7821
8004
  }
7822
8005
  if (this.isComparisonQuery(q)) {
7823
8006
  return { intent: "comparison", signals };
7824
8007
  }
7825
- if (this.isProductQuery(q) || signals.isProductLike && this.isRecommendationQuery(q)) {
7826
- return { intent: "product_search", signals };
8008
+ if (this.isInformationLookup(q)) {
8009
+ return { intent: "information_lookup", signals };
7827
8010
  }
7828
8011
  if (this.isTrendQuery(q) || signals.hasDateFields && signals.hasNumericFields && /\b(growth|trend|over time|monthly|yearly|weekly)\b/.test(q)) {
7829
8012
  return { intent: "trend_analysis", signals };
@@ -7840,7 +8023,8 @@ var IntentClassifier = class {
7840
8023
  return { intent: "information_lookup", signals };
7841
8024
  }
7842
8025
  /**
7843
- * Extract key data structure signals from retrieved documents.
8026
+ * Extract key data structure signals dynamically from retrieved documents.
8027
+ * Inspects both structured metadata and parsed chunk text.
7844
8028
  */
7845
8029
  static extractDataSignals(context) {
7846
8030
  const docs = context.retrievedDocuments || [];
@@ -7873,18 +8057,21 @@ var IntentClassifier = class {
7873
8057
  let categoricalFieldCount = 0;
7874
8058
  if (docs.length > 0) {
7875
8059
  const keys = /* @__PURE__ */ new Set();
7876
- let numericKeys = /* @__PURE__ */ new Set();
7877
- let dateKeys = /* @__PURE__ */ new Set();
7878
- let catKeys = /* @__PURE__ */ new Set();
7879
- let productKeyMatches = 0;
8060
+ const numericKeys = /* @__PURE__ */ new Set();
8061
+ const dateKeys = /* @__PURE__ */ new Set();
8062
+ const catKeys = /* @__PURE__ */ new Set();
8063
+ let productDocCount = 0;
7880
8064
  docs.forEach((doc) => {
8065
+ if (!doc) return;
7881
8066
  const meta = doc.metadata || {};
8067
+ const content = doc.content || "";
8068
+ let hasDocProductSignal = false;
7882
8069
  Object.entries(meta).forEach(([key, val]) => {
7883
8070
  if (IGNORED_SYSTEM_KEYS.has(key.toLowerCase())) return;
7884
8071
  keys.add(key);
7885
8072
  const kLower = key.toLowerCase();
7886
- if (["price", "image", "brand", "in_stock", "rating", "sku", "product"].some((p) => kLower.includes(p))) {
7887
- productKeyMatches++;
8073
+ if (["price", "image", "brand", "in_stock", "rating", "sku", "product", "item", "cost", "msrp", "category", "vendor", "inventory", "stock"].some((p) => kLower.includes(p))) {
8074
+ hasDocProductSignal = true;
7888
8075
  }
7889
8076
  if (typeof val === "number") {
7890
8077
  numericKeys.add(key);
@@ -7896,13 +8083,23 @@ var IntentClassifier = class {
7896
8083
  }
7897
8084
  }
7898
8085
  });
8086
+ if (!hasDocProductSignal && content) {
8087
+ const hasContentName = /(?:^|\n)\s*(?:product\s*name|item\s*name|product|item|title|name)\s*[:\-–]\s*[^\n,]+/i.test(content);
8088
+ const hasContentAttr = /(?:^|\n)\s*(?:price|cost|msrp|brand|vendor|sku|category|stock|inventory|rating|image)\s*[:\-–]/i.test(content) || /\$\s*[\d,.]+/.test(content);
8089
+ if (hasContentName && hasContentAttr) {
8090
+ hasDocProductSignal = true;
8091
+ }
8092
+ }
8093
+ if (hasDocProductSignal) {
8094
+ productDocCount++;
8095
+ }
7899
8096
  });
7900
8097
  hasNumericFields = numericKeys.size > 0;
7901
8098
  hasDateFields = dateKeys.size > 0;
7902
8099
  hasCategoricalFields = catKeys.size > 0;
7903
8100
  numericFieldCount = numericKeys.size;
7904
8101
  categoricalFieldCount = catKeys.size;
7905
- if (productKeyMatches >= 2) {
8102
+ if (productDocCount > 0 && (productDocCount / docs.length >= 0.3 || productDocCount >= 2)) {
7906
8103
  isProductLike = true;
7907
8104
  }
7908
8105
  }
@@ -7917,8 +8114,15 @@ var IntentClassifier = class {
7917
8114
  };
7918
8115
  }
7919
8116
  // ─── Intent Heuristic Checkers ─────────────────────────────────────────────
8117
+ static isDiscoveryQuery(query) {
8118
+ const q = query.toLowerCase().trim();
8119
+ 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);
8120
+ }
7920
8121
  static isInformationLookup(query) {
7921
8122
  const q = query.toLowerCase().trim();
8123
+ 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)) {
8124
+ return false;
8125
+ }
7922
8126
  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)) {
7923
8127
  if (!/\b(chart|graph|plot|visualize|pie chart|bar chart|line chart|scatter plot|draw a)\b/i.test(q)) {
7924
8128
  return true;
@@ -7933,7 +8137,11 @@ var IntentClassifier = class {
7933
8137
  return /\b(compare|comparison|vs\.?|versus|difference between|differ|contrast|against)\b/i.test(query);
7934
8138
  }
7935
8139
  static isProductQuery(query) {
7936
- 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);
8140
+ const q = query.toLowerCase().trim();
8141
+ 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)) {
8142
+ return false;
8143
+ }
8144
+ 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);
7937
8145
  }
7938
8146
  static isRecommendationQuery(query) {
7939
8147
  return /\b(recommend|suggest|what should i (buy|get|choose)|best options|top picks)\b/i.test(query);
@@ -7998,14 +8206,23 @@ var Rule3ProductDiscoveryRule = class {
7998
8206
  }
7999
8207
  evaluate(context, intent) {
8000
8208
  const q = (context.userQuery || "").toLowerCase().trim();
8001
- 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);
8002
- const isNotChart = !/\b(chart|sales|revenue|monthly|growth|trend|over time)\b/i.test(q);
8003
- const isAnalyticalRanking = /\b(top performing|top selling|best performing|performing|sales|revenue|ranking)\b/i.test(q);
8004
- if (isDiscovery && isNotChart && !isAnalyticalRanking) {
8209
+ const isExplicitChart = /\b(chart|graph|plot|sales over time|monthly growth|revenue trend|pie chart|bar chart|line chart|histogram)\b/i.test(q);
8210
+ if (isExplicitChart) {
8211
+ return null;
8212
+ }
8213
+ 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);
8214
+ if (isFinancialReport) {
8215
+ return null;
8216
+ }
8217
+ const signals = IntentClassifier.extractDataSignals(context);
8218
+ 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);
8219
+ const isAnalyticalRanking = /\b(top performing|top selling|best performing|performing|sales revenue|revenue ranking)\b/i.test(q);
8220
+ const isGenericDocumentQuery = /\b(documents? in system|all documents|system documents|overall documents)\b/i.test(q);
8221
+ if (!isAnalyticalRanking && !isGenericDocumentQuery && (signals.isProductLike && hasDiscoveryAction || intent === "product_search" && !isAnalyticalRanking)) {
8005
8222
  return {
8006
8223
  renderType: "carousel",
8007
- confidence: 0.9,
8008
- reason: "Rule 3: Product discovery/recommendation query requires product carousel rendering."
8224
+ confidence: 0.92,
8225
+ reason: "Rule 3: Dynamically identified product catalog entities and discovery intent."
8009
8226
  };
8010
8227
  }
8011
8228
  return null;
@@ -8476,11 +8693,11 @@ var UITransformer = class _UITransformer {
8476
8693
  if (["tabular", "table"].includes(resolvedIntent.visualizationHint) || resolvedIntent.wantsExplicitTable) {
8477
8694
  return this.hasMultipleFields(filteredData) ? this.transformToTable(filteredData, userQuery) : this.transformToText(filteredData);
8478
8695
  }
8479
- if ((hasProducts || this.isProductQuery(userQuery)) && resolvedIntent.visualizationHint === "product_browse") {
8480
- if (!this.isProductQuery(userQuery)) {
8481
- return this.transformToText(filteredData);
8696
+ if (hasProducts || this.isProductQuery(userQuery) || resolvedIntent.visualizationHint === "product_browse") {
8697
+ const carousel = this.transformToProductCarousel(filteredData, config, trainedSchema);
8698
+ if (Array.isArray(carousel.data) && carousel.data.length > 0) {
8699
+ return carousel;
8482
8700
  }
8483
- return this.transformToProductCarousel(filteredData, config, trainedSchema);
8484
8701
  }
8485
8702
  const automatic = this.chooseAutomaticVisualization(filteredData, profile, userQuery);
8486
8703
  if (automatic) return automatic;
@@ -9117,15 +9334,17 @@ ${schemaProfileText}` : ""}`;
9117
9334
  return _UITransformer._productQueryTest(query);
9118
9335
  }
9119
9336
  static _productQueryTest(query) {
9120
- const q = query.toLowerCase();
9337
+ const q = query.toLowerCase().trim();
9121
9338
  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/;
9122
9339
  if (financialTerms.test(q)) {
9123
9340
  return false;
9124
9341
  }
9125
- const productTerms = /\b(product|products|item|items|sku|catalog|catalogue|brand|model|stock|inventory|in stock|out of stock|buy|shop)\b/.test(q);
9126
- const productAction = /\b(recommend|suggest|describe|description|detail|details|about|show|find|search|browse|list|explore|exploring)\b/.test(q);
9127
9342
  const nonProductEntityTerms = /\b(organization|organisations?|organizations?|company|companies|employee|employees|staff|headcount|workforce)\b/.test(q);
9128
- return productTerms && productAction && !nonProductEntityTerms;
9343
+ if (nonProductEntityTerms) {
9344
+ return false;
9345
+ }
9346
+ 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;
9347
+ return genericDiscoveryPatterns.test(q);
9129
9348
  }
9130
9349
  static isProductData(item) {
9131
9350
  if (!item) return false;
@@ -9142,10 +9361,17 @@ ${schemaProfileText}` : ""}`;
9142
9361
  if (financialTerms.test(content)) {
9143
9362
  return false;
9144
9363
  }
9145
- const hasSku = Boolean(resolveMetadataValue(metadata, "sku") || resolveMetadataValue(metadata, "product_id"));
9146
- const hasProductIdentity = Boolean(resolveMetadataValue(metadata, "product_name") || resolveMetadataValue(metadata, "brand") && resolveMetadataValue(metadata, "name"));
9147
- const hasPrice = Boolean(resolveMetadataValue(metadata, "retail_price") || resolveMetadataValue(metadata, "unit_price") || resolveMetadataValue(metadata, "msrp"));
9148
- return hasSku || hasProductIdentity && hasPrice;
9364
+ const hasSku = Boolean(resolveMetadataValue(metadata, "sku") || resolveMetadataValue(metadata, "product_id") || resolveMetadataValue(metadata, "item_id"));
9365
+ const hasProductIdentity = Boolean(resolveMetadataValue(metadata, "product_name") || resolveMetadataValue(metadata, "item_name") || resolveMetadataValue(metadata, "brand") && resolveMetadataValue(metadata, "name"));
9366
+ const hasPrice = Boolean(resolveMetadataValue(metadata, "price") || resolveMetadataValue(metadata, "retail_price") || resolveMetadataValue(metadata, "unit_price") || resolveMetadataValue(metadata, "msrp"));
9367
+ if (hasSku || hasProductIdentity && hasPrice || hasProductIdentity) {
9368
+ return true;
9369
+ }
9370
+ const hasContentName = /(?:^|\n)\s*(?:product\s*name|item\s*name|product|item|title|name)\s*[:\-–]\s*[^\n,]+/i.test(item.content || "");
9371
+ const hasContentPrice = /(?:^|\n)\s*(?:price|retail\s*price|unit\s*price|cost|msrp)\s*[:\-–]?\s*\$?\s*[\d,.]+/i.test(item.content || "");
9372
+ const hasContentCategory = /(?:^|\n)\s*(?:category|department|collection)\s*[:\-–]\s*[^\n,]+/i.test(item.content || "");
9373
+ const hasContentBrand = /(?:^|\n)\s*(?:brand|manufacturer|vendor)\s*[:\-–]\s*[^\n,]+/i.test(item.content || "");
9374
+ return hasContentName && (hasContentPrice || hasContentCategory || hasContentBrand);
9149
9375
  }
9150
9376
  static isTimeSeriesData(item) {
9151
9377
  const metadata = item.metadata || {};
@@ -9635,64 +9861,46 @@ ${schemaProfileText}` : ""}`;
9635
9861
  return resolveMetadataValue(meta, uiKey);
9636
9862
  }
9637
9863
  static extractProductInfo(item, config, trainedSchema) {
9638
- var _a2, _b;
9864
+ var _a2, _b, _c;
9639
9865
  if (!item) return null;
9640
9866
  const meta = item.metadata || {};
9641
9867
  const content = item.content || "";
9642
- const name = this.getDynamicVal(meta, "name", config, trainedSchema);
9643
- const price = this.getDynamicVal(meta, "price", config, trainedSchema);
9644
- const brand = this.getDynamicVal(meta, "brand", config, trainedSchema);
9868
+ let name = this.getDynamicVal(meta, "name", config, trainedSchema);
9869
+ let price = this.getDynamicVal(meta, "price", config, trainedSchema);
9870
+ let brand = this.getDynamicVal(meta, "brand", config, trainedSchema);
9645
9871
  const description = this.cleanProductDescription(
9646
9872
  (_a2 = this.extractProductDescriptionFromContent(content)) != null ? _a2 : this.getProductDescriptionValue(meta, config, trainedSchema)
9647
9873
  );
9648
- if (name || this.isProductData(item)) {
9649
- let finalName = name ? String(name) : void 0;
9650
- if (!finalName && content) {
9651
- const nameMatch = content.match(/(?:Product|Item|Name):\s*([^\n]+)/i);
9652
- finalName = nameMatch ? nameMatch[1].trim() : (_b = content.split("\n")[0]) == null ? void 0 : _b.substring(0, 60);
9874
+ if (!name && content) {
9875
+ 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);
9876
+ if (nameMatch && nameMatch[1]) {
9877
+ name = nameMatch[1].trim();
9653
9878
  }
9654
- if (!finalName) {
9655
- console.warn(
9656
- `[UITransformer] Unable to read product name from metadata or content for item ${item.id}. Defaulting to 'Unnamed Product'.`,
9657
- { metadataKeys: Object.keys(meta), contentLength: content.length }
9658
- );
9659
- }
9660
- let finalPrice = typeof price === "number" || typeof price === "string" ? price : void 0;
9661
- if (!finalPrice && content) {
9662
- const priceMatch = content.match(/(?:Price|Cost):\s*\$?([\d,.]+)/i) || content.match(/\$\s*([\d,.]+)/);
9663
- if (priceMatch) finalPrice = priceMatch[1].replace(/,/g, "");
9664
- }
9665
- if (finalPrice === void 0) {
9666
- console.warn(
9667
- `[UITransformer] Unable to read product price from metadata or content for item ${item.id}.`,
9668
- { metadataKeys: Object.keys(meta), contentSnippet: content.substring(0, 100) }
9669
- );
9670
- }
9671
- const imageValue = this.getDynamicVal(meta, "image", config, trainedSchema);
9672
- if (!imageValue) {
9673
- console.debug(
9674
- `[UITransformer] Unable to read product image URL from metadata for item ${item.id}.`
9675
- );
9879
+ }
9880
+ if (!brand && content) {
9881
+ const brandMatch = content.match(/(?:^|\n)\s*(?:brand|manufacturer|vendor)\s*[:\-–]\s*([^\n,]+)/i);
9882
+ if (brandMatch && brandMatch[1]) {
9883
+ brand = brandMatch[1].trim();
9676
9884
  }
9677
- if (!description) {
9678
- console.debug(
9679
- `[UITransformer] Unable to read product description from metadata or content for item ${item.id}.`
9680
- );
9885
+ }
9886
+ if (price === void 0 && content) {
9887
+ 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,.]+)/);
9888
+ if (priceMatch && priceMatch[1]) {
9889
+ price = priceMatch[1].replace(/,/g, "");
9681
9890
  }
9891
+ }
9892
+ if (name || this.isProductData(item)) {
9893
+ const finalName = name ? String(name) : ((_b = content.split("\n")[0]) == null ? void 0 : _b.substring(0, 60)) || "Unnamed Product";
9894
+ 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]);
9682
9895
  return {
9683
9896
  id: item.id,
9684
- name: finalName || "Unnamed Product",
9685
- price: finalPrice,
9897
+ name: finalName,
9898
+ price: typeof price === "number" || typeof price === "string" ? price : void 0,
9686
9899
  image: typeof imageValue === "string" ? imageValue : void 0,
9687
9900
  brand: brand ? String(brand) : void 0,
9688
9901
  description,
9689
9902
  inStock: this.determineStockStatus(item)
9690
9903
  };
9691
- } else {
9692
- console.warn(
9693
- `[UITransformer] Item ${item.id} skipped: unable to read name or identify as product data from metadata/content.`,
9694
- { metadataKeys: Object.keys(meta), contentSnippet: content.substring(0, 100) }
9695
- );
9696
9904
  }
9697
9905
  return null;
9698
9906
  }
@@ -10067,6 +10275,17 @@ var Pipeline = class {
10067
10275
  - If answer cannot be found in the sources, say that you cannot answer the question and do not hallucinate.
10068
10276
  - Do not use information from previous turns to answer the question.
10069
10277
  - 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.
10278
+ - Be concise, direct, and focused. Deliver clean, high-quality answers within token limits without unnecessary filler.
10279
+
10280
+ ### CATEGORY & DOMAIN SYNONYM MATCHING:
10281
+ - Intelligently understand common category synonyms and related terms from the context:
10282
+ - 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.
10283
+ - If the user asks about "clothes", "apparel", "wear", "shirts", "garments", or "fashion", map these to the "Clothing" category in the catalog.
10284
+ - If the user asks about "electronics", "gadgets", "tech", "devices", or "appliances", map these to the "Electronics" category.
10285
+ - If the user asks about "food", "snacks", "groceries", "beverages", or "drinks", map these to the "Grocery" category.
10286
+ - If the user asks about "office items", "stationery", "work supplies", or "desk equipment", map these to the "Office Supplies" category.
10287
+ - If the user asks about "toys", "games", "play items", or "hobbies", map these to the "Toys & Games" category.
10288
+ - 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.
10070
10289
 
10071
10290
  ### PRODUCT DISPLAY:
10072
10291
  - Recommended Products should only be displayed when the user explicitly asks for products.
@@ -10236,13 +10455,23 @@ ${m.content}`).join("\n\n---\n\n");
10236
10455
  const texts = chunks.map((c) => PIIMasker.mask(c.content));
10237
10456
  if (typeof this.embeddingProvider.batchEmbed === "function") {
10238
10457
  try {
10239
- const batchSize = 50;
10240
- const allVectors = [];
10458
+ const batchSize = 100;
10459
+ const slices = [];
10241
10460
  for (let i = 0; i < texts.length; i += batchSize) {
10242
- const slice = texts.slice(i, i + batchSize);
10243
- const batchVectors = await this.embeddingProvider.batchEmbed(slice, { taskType: "document" });
10244
- allVectors.push(...batchVectors);
10461
+ slices.push(texts.slice(i, i + batchSize));
10245
10462
  }
10463
+ const batchEmbedFn = this.embeddingProvider.batchEmbed.bind(this.embeddingProvider);
10464
+ const concurrentBatches = await BatchProcessor.processConcurrent(
10465
+ slices,
10466
+ (slice) => batchEmbedFn(slice, { taskType: "document" }),
10467
+ 5,
10468
+ {
10469
+ maxRetries: 3,
10470
+ initialDelayMs: 100,
10471
+ throwOnPartialFailure: false
10472
+ }
10473
+ );
10474
+ const allVectors = concurrentBatches.results.flat();
10246
10475
  if (allVectors.length === chunks.length) {
10247
10476
  return allVectors;
10248
10477
  }
@@ -10255,7 +10484,7 @@ ${m.content}`).join("\n\n---\n\n");
10255
10484
  const concurrentResult = await BatchProcessor.processConcurrent(
10256
10485
  texts,
10257
10486
  (text) => this.embeddingProvider.embed(text, { taskType: "document" }),
10258
- 10,
10487
+ 20,
10259
10488
  {
10260
10489
  maxRetries: 3,
10261
10490
  initialDelayMs: 100,
@@ -10273,7 +10502,7 @@ ${m.content}`).join("\n\n---\n\n");
10273
10502
  /** Step 3: Upsert chunks to vector database with retry logic. */
10274
10503
  async processUpserts(upsertDocs, namespace) {
10275
10504
  const upsertBatchOptions = {
10276
- batchSize: 100,
10505
+ batchSize: 250,
10277
10506
  maxRetries: 3,
10278
10507
  initialDelayMs: 100
10279
10508
  };
@@ -10431,7 +10660,7 @@ ${m.content}`).join("\n\n---\n\n");
10431
10660
  */
10432
10661
  askStreamInternal(_0) {
10433
10662
  return __asyncGenerator(this, arguments, function* (question, history = [], namespace) {
10434
- 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;
10663
+ 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;
10435
10664
  yield new __await(this.initialize());
10436
10665
  const ns = formatNamespace(namespace != null ? namespace : this.config.projectId);
10437
10666
  const topK = (_b = (_a2 = this.config.rag) == null ? void 0 : _a2.topK) != null ? _b : 5;
@@ -10502,7 +10731,17 @@ ${m.content}`).join("\n\n---\n\n");
10502
10731
  }
10503
10732
  const rerankMs = performance.now() - rerankStart;
10504
10733
  const totalCount = fullSources.length;
10505
- const promptSources = fullSources.slice(0, 15);
10734
+ const rawPromptSources = fullSources.slice(0, 4);
10735
+ let accumulatedChars = 0;
10736
+ const promptSources = [];
10737
+ for (const s of rawPromptSources) {
10738
+ const textLen = ((s == null ? void 0 : s.content) || "").length;
10739
+ if (accumulatedChars + textLen > 3500 && promptSources.length >= 2) {
10740
+ break;
10741
+ }
10742
+ promptSources.push(s);
10743
+ accumulatedChars += textLen;
10744
+ }
10506
10745
  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]
10507
10746
 
10508
10747
  ` + promptSources.map((m, i) => {
@@ -10580,7 +10819,7 @@ ${context}`;
10580
10819
  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);
10581
10820
  const modelNameLower = this.config.llm.model.toLowerCase();
10582
10821
  const isReasoningModel = modelNameLower.includes("-r1") || modelNameLower.includes("deepseek-r1") || modelNameLower.includes("reasoning") || modelNameLower.includes("thinking");
10583
- 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);
10822
+ 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");
10584
10823
  let finalRestrictionSuffix = restrictionSuffix;
10585
10824
  if (isSimulatedThinking) {
10586
10825
  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.`)";
@@ -10591,7 +10830,7 @@ ${context}`;
10591
10830
  }));
10592
10831
  const userQuestion = { role: "user", content: effectiveQuestion + finalRestrictionSuffix };
10593
10832
  const messages = [...hardenedHistory, userQuestion];
10594
- const systemPrompt = (_w = this.config.llm.systemPrompt) != null ? _w : "";
10833
+ const systemPrompt = (_v = this.config.llm.systemPrompt) != null ? _v : "";
10595
10834
  const userPrompt = messages.map((m) => `${m.role}: ${m.content}`).join("\n");
10596
10835
  let fullReply = "";
10597
10836
  let thinkingText = "";
@@ -10613,57 +10852,54 @@ ${context}`;
10613
10852
  yield { type: "thinking", text: thinkingDelta };
10614
10853
  continue;
10615
10854
  }
10616
- if (isSimulatedThinking) {
10617
- textBuffer += chunk;
10618
- while (textBuffer.length > 0) {
10619
- if (!inThinking) {
10620
- const thinkIndex = textBuffer.indexOf("<think>");
10621
- if (thinkIndex !== -1) {
10622
- const before = textBuffer.slice(0, thinkIndex);
10623
- if (before) {
10624
- fullReply += before;
10625
- yield before;
10626
- }
10627
- inThinking = true;
10628
- thinkingStartMs = performance.now();
10629
- textBuffer = textBuffer.slice(thinkIndex + "<think>".length);
10630
- } else {
10631
- const maxSafeLength = textBuffer.length - "<think>".length;
10632
- if (maxSafeLength > 0) {
10633
- const safeText = textBuffer.slice(0, maxSafeLength);
10634
- fullReply += safeText;
10635
- yield safeText;
10636
- textBuffer = textBuffer.slice(maxSafeLength);
10637
- }
10638
- break;
10855
+ textBuffer += chunk;
10856
+ while (textBuffer.length > 0) {
10857
+ if (!inThinking) {
10858
+ const thinkIndex = textBuffer.indexOf("<think>");
10859
+ if (thinkIndex !== -1) {
10860
+ const before = textBuffer.slice(0, thinkIndex);
10861
+ if (before) {
10862
+ fullReply += before;
10863
+ yield before;
10639
10864
  }
10865
+ inThinking = true;
10866
+ thinkingStartMs = performance.now();
10867
+ textBuffer = textBuffer.slice(thinkIndex + "<think>".length);
10640
10868
  } else {
10641
- const endThinkIndex = textBuffer.indexOf("</think>");
10642
- if (endThinkIndex !== -1) {
10643
- const thinkingDelta = textBuffer.slice(0, endThinkIndex);
10644
- if (thinkingDelta) {
10645
- thinkingText += thinkingDelta;
10646
- thinkingDurationMs = performance.now() - thinkingStartMs;
10647
- yield { type: "thinking", text: thinkingDelta };
10648
- }
10649
- inThinking = false;
10650
- textBuffer = textBuffer.slice(endThinkIndex + "</think>".length);
10651
- } else {
10652
- const maxSafeLength = textBuffer.length - "</think>".length;
10653
- if (maxSafeLength > 0) {
10654
- const thinkingDelta = textBuffer.slice(0, maxSafeLength);
10655
- thinkingText += thinkingDelta;
10656
- thinkingDurationMs = performance.now() - thinkingStartMs;
10657
- yield { type: "thinking", text: thinkingDelta };
10658
- textBuffer = textBuffer.slice(maxSafeLength);
10659
- }
10660
- break;
10869
+ const partialMatch = textBuffer.match(/<t?(?:h(?:i(?:n(?:k)?)?)?)?$/);
10870
+ const safeLen = partialMatch ? textBuffer.length - partialMatch[0].length : textBuffer.length;
10871
+ if (safeLen > 0) {
10872
+ const safeText = textBuffer.slice(0, safeLen);
10873
+ fullReply += safeText;
10874
+ yield safeText;
10875
+ textBuffer = textBuffer.slice(safeLen);
10661
10876
  }
10877
+ break;
10878
+ }
10879
+ } else {
10880
+ const endThinkIndex = textBuffer.indexOf("</think>");
10881
+ if (endThinkIndex !== -1) {
10882
+ const thinkingDelta = textBuffer.slice(0, endThinkIndex);
10883
+ if (thinkingDelta) {
10884
+ thinkingText += thinkingDelta;
10885
+ thinkingDurationMs = performance.now() - thinkingStartMs;
10886
+ yield { type: "thinking", text: thinkingDelta };
10887
+ }
10888
+ inThinking = false;
10889
+ textBuffer = textBuffer.slice(endThinkIndex + "</think>".length);
10890
+ } else {
10891
+ const partialMatch = textBuffer.match(/<\/?t?(?:h(?:i(?:n(?:k)?)?)?)?$/);
10892
+ const safeLen = partialMatch ? textBuffer.length - partialMatch[0].length : textBuffer.length;
10893
+ if (safeLen > 0) {
10894
+ const thinkingDelta = textBuffer.slice(0, safeLen);
10895
+ thinkingText += thinkingDelta;
10896
+ thinkingDurationMs = performance.now() - thinkingStartMs;
10897
+ yield { type: "thinking", text: thinkingDelta };
10898
+ textBuffer = textBuffer.slice(safeLen);
10899
+ }
10900
+ break;
10662
10901
  }
10663
10902
  }
10664
- } else {
10665
- fullReply += chunk;
10666
- yield chunk;
10667
10903
  }
10668
10904
  }
10669
10905
  } catch (temp) {
@@ -10676,7 +10912,7 @@ ${context}`;
10676
10912
  throw error[0];
10677
10913
  }
10678
10914
  }
10679
- if (isSimulatedThinking && textBuffer.length > 0) {
10915
+ if (textBuffer.length > 0) {
10680
10916
  if (inThinking) {
10681
10917
  thinkingText += textBuffer;
10682
10918
  yield { type: "thinking", text: textBuffer };
@@ -10687,22 +10923,21 @@ ${context}`;
10687
10923
  }
10688
10924
  } else {
10689
10925
  const reply = yield new __await(this.llmProvider.chat(messages, context));
10690
- if (isSimulatedThinking) {
10691
- const thinkStart = reply.indexOf("<think>");
10692
- const thinkEnd = reply.indexOf("</think>");
10693
- if (thinkStart !== -1 && thinkEnd !== -1 && thinkEnd > thinkStart) {
10694
- thinkingText = reply.slice(thinkStart + "<think>".length, thinkEnd);
10695
- fullReply = reply.slice(0, thinkStart) + reply.slice(thinkEnd + "</think>".length);
10696
- thinkingDurationMs = 100;
10697
- } else {
10698
- fullReply = reply;
10699
- }
10926
+ const thinkStart = reply.indexOf("<think>");
10927
+ const thinkEnd = reply.indexOf("</think>");
10928
+ if (thinkStart !== -1 && thinkEnd !== -1 && thinkEnd > thinkStart) {
10929
+ thinkingText = reply.slice(thinkStart + "<think>".length, thinkEnd);
10930
+ fullReply = reply.slice(0, thinkStart) + reply.slice(thinkEnd + "</think>".length);
10931
+ thinkingDurationMs = 100;
10932
+ } else if (thinkStart !== -1) {
10933
+ thinkingText = reply.slice(thinkStart + "<think>".length);
10934
+ fullReply = reply.slice(0, thinkStart);
10700
10935
  } else {
10701
10936
  fullReply = reply;
10702
10937
  }
10703
10938
  yield fullReply;
10704
10939
  }
10705
- const runHallucination = ((_x = this.config.llm.options) == null ? void 0 : _x.hallucinationScoring) === true;
10940
+ const runHallucination = ((_w = this.config.llm.options) == null ? void 0 : _w.hallucinationScoring) === true;
10706
10941
  if (runHallucination) {
10707
10942
  hallucinationScoringPromise = scoreHallucination(this.llmProvider, fullReply, context).catch(() => void 0);
10708
10943
  }
@@ -10724,96 +10959,45 @@ ${context}`;
10724
10959
  totalTokens: promptTokens + completionTokens,
10725
10960
  estimatedCostUsd: estimateCostUsd(promptTokens, completionTokens, this.config.llm.model)
10726
10961
  };
10727
- const awaitHallucination = ((_y = this.config.llm.options) == null ? void 0 : _y.awaitHallucination) === true;
10962
+ const awaitHallucination = ((_x = this.config.llm.options) == null ? void 0 : _x.awaitHallucination) === true;
10728
10963
  const [uiTransformation, hallucinationResult] = yield new __await(Promise.all([
10729
10964
  uiTransformationPromise,
10730
10965
  awaitHallucination ? hallucinationScoringPromise : Promise.resolve(void 0)
10731
10966
  ]));
10732
- const triadScore = yield new __await(RAGEvaluator.evaluate(effectiveQuestion, context, fullReply));
10733
- const buildTrace = (hScore) => __spreadValues({
10734
- requestId,
10735
- query: question,
10736
- rewrittenQuery: rewrittenQuery || ((promptOptResult == null ? void 0 : promptOptResult.wasModified) ? promptOptResult.optimizedPrompt : void 0),
10737
- systemPrompt,
10738
- userPrompt: effectiveQuestion + finalRestrictionSuffix,
10739
- chunks: (sources || []).filter(Boolean).map((s) => {
10740
- var _a3, _b2;
10741
- return {
10742
- id: s.id,
10743
- score: s.score,
10744
- content: (_a3 = s == null ? void 0 : s.content) != null ? _a3 : "",
10745
- metadata: (_b2 = s == null ? void 0 : s.metadata) != null ? _b2 : {},
10746
- namespace: ns
10747
- };
10748
- }),
10749
- latency,
10750
- tokens,
10751
- timestamp: (/* @__PURE__ */ new Date()).toISOString(),
10752
- ragTriad: {
10753
- contextRelevance: triadScore.contextRelevance,
10754
- faithfulness: triadScore.faithfulness,
10755
- answerRelevance: triadScore.answerRelevance,
10756
- overallQuality: triadScore.overallQuality
10757
- }
10758
- }, hScore ? {
10759
- hallucinationScore: hScore.score,
10760
- hallucinationReason: hScore.reason
10761
- } : {});
10967
+ RAGEvaluator.evaluate(effectiveQuestion, context, fullReply).catch(() => {
10968
+ });
10969
+ const buildTrace = (hScore) => {
10970
+ var _a3, _b2;
10971
+ return __spreadValues({
10972
+ requestId,
10973
+ query: question,
10974
+ rewrittenQuery: rewrittenQuery || ((promptOptResult == null ? void 0 : promptOptResult.wasModified) ? promptOptResult.optimizedPrompt : void 0),
10975
+ // systemPrompt / userPrompt intentionally excluded:
10976
+ // they may contain business logic or post-masking user PII and were
10977
+ // being emitted to the client browser via SSE (security risk).
10978
+ chunks: (sources || []).filter(Boolean).map((s) => {
10979
+ var _a4, _b3;
10980
+ return {
10981
+ id: s.id,
10982
+ score: s.score,
10983
+ content: (_a4 = s == null ? void 0 : s.content) != null ? _a4 : "",
10984
+ metadata: (_b3 = s == null ? void 0 : s.metadata) != null ? _b3 : {},
10985
+ namespace: ns
10986
+ };
10987
+ }),
10988
+ latency,
10989
+ tokens,
10990
+ // Always populate model/provider on the trace (fixes gap where telemetry
10991
+ // was reading finalTrace?.model with a fallback — now it's always set).
10992
+ model: (_a3 = this.config.llm) == null ? void 0 : _a3.model,
10993
+ provider: (_b2 = this.config.llm) == null ? void 0 : _b2.provider,
10994
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
10995
+ }, hScore ? {
10996
+ hallucinationScore: hScore.score,
10997
+ hallucinationReason: hScore.reason
10998
+ } : {});
10999
+ };
10762
11000
  const trace = buildTrace(hallucinationResult);
10763
- const isTelemetryActive = (_A = (_z = this.config.telemetry) == null ? void 0 : _z.enabled) != null ? _A : Boolean(this.config.licenseKey);
10764
- if (isTelemetryActive) {
10765
- const defaultUrl = "https://www.retrivora.com/api/telemetry";
10766
- const telemetryUrl = ((_B = this.config.telemetry) == null ? void 0 : _B.url) || process.env.TELEMETRY_URL || process.env.NEXT_PUBLIC_TELEMETRY_URL || defaultUrl;
10767
- const absoluteUrl = telemetryUrl.startsWith("http") ? telemetryUrl : "https://www.retrivora.com" + (telemetryUrl.startsWith("/") ? telemetryUrl : "/" + telemetryUrl);
10768
- (async () => {
10769
- var _a3, _b2, _c2, _d2, _e2;
10770
- try {
10771
- let finalTrace = trace;
10772
- if (!awaitHallucination && runHallucination) {
10773
- const backgroundScoreResult = await hallucinationScoringPromise;
10774
- if (backgroundScoreResult) {
10775
- finalTrace = buildTrace(backgroundScoreResult);
10776
- }
10777
- }
10778
- const modelName = (finalTrace == null ? void 0 : finalTrace.model) || ((_a3 = this.config.llm) == null ? void 0 : _a3.model) || "llama-3.1-8b-instant";
10779
- const providerName = (finalTrace == null ? void 0 : finalTrace.provider) || ((_b2 = this.config.llm) == null ? void 0 : _b2.provider) || "groq";
10780
- const tokenCount = Number(((_c2 = finalTrace == null ? void 0 : finalTrace.tokens) == null ? void 0 : _c2.totalTokens) || 0);
10781
- const costEst = Number(((_d2 = finalTrace == null ? void 0 : finalTrace.tokens) == null ? void 0 : _d2.estimatedCostUsd) || 0);
10782
- const latencyDuration = Number(((_e2 = finalTrace == null ? void 0 : finalTrace.latency) == null ? void 0 : _e2.totalMs) || 0);
10783
- if (typeof window === "undefined") {
10784
- Logger.info(`[Retrivora Pipeline Telemetry] \u{1F4CA} Dispatching RAG telemetry -> Project: "${ns}", Tokens: ${tokenCount}, Latency: ${latencyDuration}ms`);
10785
- }
10786
- await fetch(absoluteUrl, {
10787
- method: "POST",
10788
- headers: {
10789
- "Content-Type": "application/json"
10790
- },
10791
- body: JSON.stringify({
10792
- trace: finalTrace,
10793
- licenseKey: this.config.licenseKey,
10794
- projectId: ns,
10795
- organization: process.env.RETRIVORA_ORGANIZATION || "default-org",
10796
- sdkVersion: SDK_VERSION,
10797
- model: modelName,
10798
- provider: providerName,
10799
- tokens: tokenCount,
10800
- cost: costEst,
10801
- costUsd: costEst,
10802
- latencyMs: latencyDuration,
10803
- status: "success",
10804
- action: "RAG_QUERY",
10805
- feature: "RAG",
10806
- timestamp: (/* @__PURE__ */ new Date()).toISOString(),
10807
- details: `Org: default-org | Model: ${providerName}/${modelName} | Tokens: ${tokenCount} | Latency: ${latencyDuration}ms | Cost: $${costEst} | SDK: v${SDK_VERSION} | Feature: RAG`
10808
- })
10809
- }).catch((err) => {
10810
- Logger.warn(`[Pipeline Telemetry] Failed to send trace async to ${absoluteUrl}`, { error: err.message });
10811
- });
10812
- } catch (err) {
10813
- Logger.warn(`[Pipeline Telemetry] Failed to send trace async to ${absoluteUrl}:`, { error: err.message });
10814
- }
10815
- })();
10816
- }
10817
11001
  yield {
10818
11002
  reply: "",
10819
11003
  sources,
@@ -12472,8 +12656,8 @@ init_RedisProvider();
12472
12656
  init_WeaviateProvider();
12473
12657
  init_UniversalVectorProvider();
12474
12658
 
12475
- // src/handlers/index.ts
12476
- import { NextResponse } from "next/server";
12659
+ // src/handlers/chatHandler.ts
12660
+ import { NextResponse as NextResponse3 } from "next/server";
12477
12661
 
12478
12662
  // src/core/DatabaseStorage.ts
12479
12663
  import * as fs from "fs";
@@ -12957,9 +13141,8 @@ var _DatabaseStorage = class _DatabaseStorage {
12957
13141
  _DatabaseStorage.SUBSCRIPTION_CACHE_TTL_MS = 5 * 60 * 1e3;
12958
13142
  var DatabaseStorage = _DatabaseStorage;
12959
13143
 
12960
- // src/handlers/index.ts
12961
- init_version();
12962
- import { createHash } from "crypto";
13144
+ // src/handlers/internal/auth.ts
13145
+ import { NextResponse } from "next/server";
12963
13146
  async function checkAuth(req, onAuthorize) {
12964
13147
  if (!onAuthorize) return null;
12965
13148
  try {
@@ -12976,6 +13159,8 @@ async function checkAuth(req, onAuthorize) {
12976
13159
  return NextResponse.json({ error: msg }, { status: 401 });
12977
13160
  }
12978
13161
  }
13162
+
13163
+ // src/handlers/internal/rateLimit.ts
12979
13164
  var RateLimiter = class {
12980
13165
  constructor(windowMs = 6e4, max = 30) {
12981
13166
  this.hits = /* @__PURE__ */ new Map();
@@ -13037,6 +13222,9 @@ function checkRateLimit(req) {
13037
13222
  }
13038
13223
  return null;
13039
13224
  }
13225
+
13226
+ // src/handlers/internal/license.ts
13227
+ import { NextResponse as NextResponse2 } from "next/server";
13040
13228
  var FREE_TIER_NAMES = /* @__PURE__ */ new Set(["hobby", "free", "free_trial", "trial"]);
13041
13229
  function isFreeTier(tier) {
13042
13230
  if (!tier) return true;
@@ -13081,7 +13269,7 @@ async function enforceLicense(req, config, bodyLicenseKey) {
13081
13269
  } catch (err) {
13082
13270
  return {
13083
13271
  ok: false,
13084
- response: NextResponse.json(
13272
+ response: NextResponse2.json(
13085
13273
  { error: `License validation failed: ${err.message}` },
13086
13274
  { status: 401 }
13087
13275
  )
@@ -13139,6 +13327,8 @@ function resolveLicensedNamespace(payload, requestedNamespace) {
13139
13327
  const defaultNamespace = ((_a2 = payload.projectId) == null ? void 0 : _a2.startsWith("retrivora-")) ? payload.projectId : `retrivora-${payload.projectId || "default"}`;
13140
13328
  return { ok: true, namespace: requested || defaultNamespace };
13141
13329
  }
13330
+
13331
+ // src/handlers/internal/sanitize.ts
13142
13332
  var MAX_MESSAGE_LENGTH = 8e3;
13143
13333
  function sanitizeInput(raw) {
13144
13334
  const stripped = raw.replace(/[\u0000-\u0008\u000B\u000C\u000E-\u001F\u007F-\u009F]/g, "");
@@ -13151,6 +13341,9 @@ function sanitizeInput(raw) {
13151
13341
  }
13152
13342
  return { ok: true, value: trimmed };
13153
13343
  }
13344
+
13345
+ // src/handlers/internal/plugin.ts
13346
+ import { createHash } from "crypto";
13154
13347
  var _pluginCache = /* @__PURE__ */ new Map();
13155
13348
  var PLUGIN_CACHE_TTL_MS = 10 * 60 * 1e3;
13156
13349
  function configHash(config) {
@@ -13165,6 +13358,9 @@ function getOrCreatePlugin(configOrPlugin) {
13165
13358
  _pluginCache.set(key, { plugin, createdAt: Date.now() });
13166
13359
  return plugin;
13167
13360
  }
13361
+
13362
+ // src/handlers/internal/telemetry.ts
13363
+ init_version();
13168
13364
  var TELEMETRY_ALLOWED_HOSTS = /* @__PURE__ */ new Set([
13169
13365
  "retrivora.com",
13170
13366
  "www.retrivora.com"
@@ -13185,8 +13381,20 @@ function sanitizeTelemetryUrl(raw) {
13185
13381
  return null;
13186
13382
  }
13187
13383
  }
13188
- async function reportTelemetry(req, plugin, action, status, details, trace) {
13189
- var _a2, _b, _c, _d, _e, _f, _g2;
13384
+ function classifyError(message) {
13385
+ const m = (message || "").toLowerCase();
13386
+ if (/license|revoked|terminated|suspended/.test(m)) return { errorCode: "LICENSE_ERROR", errorType: "LICENSE_ERROR" };
13387
+ if (/rate.?limit|429/.test(m)) return { errorCode: "RATE_LIMIT_ERROR", errorType: "RATE_LIMIT_ERROR" };
13388
+ if (/unauthorized|401|403/.test(m)) return { errorCode: "AUTH_ERROR", errorType: "AUTH_ERROR" };
13389
+ if (/embedding|embed.?fail/.test(m)) return { errorCode: "EMBEDDING_ERROR", errorType: "EMBEDDING_ERROR" };
13390
+ if (/vector|pinecone|qdrant|pgvector|chroma|milvus|mongodb/.test(m)) return { errorCode: "VECTOR_DB_ERROR", errorType: "VECTOR_DB_ERROR" };
13391
+ if (/llm|chat|generation|openai|groq|anthropic|gemini|mistral/.test(m)) return { errorCode: "LLM_ERROR", errorType: "LLM_ERROR" };
13392
+ if (/fetch|network|timeout|econnrefused|socket/.test(m)) return { errorCode: "NETWORK_ERROR", errorType: "NETWORK_ERROR" };
13393
+ if (/stream/.test(m)) return { errorCode: "STREAMING_ERROR", errorType: "STREAMING_ERROR" };
13394
+ return { errorCode: "INTERNAL_ERROR", errorType: "INTERNAL_ERROR" };
13395
+ }
13396
+ async function reportTelemetry(req, plugin, action, status, details, trace, extra) {
13397
+ var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k, _l, _m, _n, _o, _p;
13190
13398
  try {
13191
13399
  const config = plugin.getConfig();
13192
13400
  const licenseKey = config.licenseKey || process.env.RETRIVORA_LICENSE_KEY || process.env.RAG_LICENSE_KEY;
@@ -13197,13 +13405,14 @@ async function reportTelemetry(req, plugin, action, status, details, trace) {
13197
13405
  if (!telemetryUrl) return;
13198
13406
  const userAgent = req.headers.get("user-agent") || `Retrivora-SDK/${SDK_VERSION}`;
13199
13407
  const projectId = config.projectId || "default";
13200
- 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";
13201
- 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";
13202
- const tokens = Number(((_e = trace == null ? void 0 : trace.tokens) == null ? void 0 : _e.totalTokens) || (trace == null ? void 0 : trace.totalTokens) || 0);
13203
- const costUsd = Number(((_f = trace == null ? void 0 : trace.tokens) == null ? void 0 : _f.estimatedCostUsd) || (trace == null ? void 0 : trace.costUsd) || 0);
13204
- const latencyMs = Number(((_g2 = trace == null ? void 0 : trace.latency) == null ? void 0 : _g2.totalMs) || (trace == null ? void 0 : trace.latencyMs) || 0);
13408
+ const model = (trace == null ? void 0 : trace.model) || ((_a2 = config.llm) == null ? void 0 : _a2.model) || "llama-3.1-8b-instant";
13409
+ const provider = (trace == null ? void 0 : trace.provider) || ((_b = config.llm) == null ? void 0 : _b.provider) || "groq";
13410
+ const tokens = Number(((_c = trace == null ? void 0 : trace.tokens) == null ? void 0 : _c.totalTokens) || (trace == null ? void 0 : trace.totalTokens) || 0);
13411
+ const costUsd = Number(((_d = trace == null ? void 0 : trace.tokens) == null ? void 0 : _d.estimatedCostUsd) || (trace == null ? void 0 : trace.costUsd) || 0);
13412
+ const latencyMs = Number(((_e = trace == null ? void 0 : trace.latency) == null ? void 0 : _e.totalMs) || (trace == null ? void 0 : trace.latencyMs) || 0);
13413
+ const sanitizedTrace = trace ? __spreadProps(__spreadValues({}, trace), { systemPrompt: void 0, userPrompt: void 0 }) : void 0;
13205
13414
  const payload = {
13206
- trace,
13415
+ trace: sanitizedTrace,
13207
13416
  projectId,
13208
13417
  organization: process.env.RETRIVORA_ORGANIZATION || "default-org",
13209
13418
  sdkVersion: SDK_VERSION,
@@ -13218,7 +13427,17 @@ async function reportTelemetry(req, plugin, action, status, details, trace) {
13218
13427
  feature: action,
13219
13428
  userAgent,
13220
13429
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
13221
- details
13430
+ details,
13431
+ // ── Structured observability fields ──────────────────────────────────
13432
+ requestId: (_f = trace == null ? void 0 : trace.requestId) != null ? _f : null,
13433
+ embedMs: (_h = (_g2 = trace == null ? void 0 : trace.latency) == null ? void 0 : _g2.embedMs) != null ? _h : null,
13434
+ retrieveMs: (_j = (_i = trace == null ? void 0 : trace.latency) == null ? void 0 : _i.retrieveMs) != null ? _j : null,
13435
+ generateMs: (_l = (_k = trace == null ? void 0 : trace.latency) == null ? void 0 : _k.generateMs) != null ? _l : null,
13436
+ ttftMs: (_m = extra == null ? void 0 : extra.ttftMs) != null ? _m : null,
13437
+ errorCode: (_n = extra == null ? void 0 : extra.errorCode) != null ? _n : null,
13438
+ errorType: (_o = extra == null ? void 0 : extra.errorType) != null ? _o : null,
13439
+ isStreaming: (_p = extra == null ? void 0 : extra.isStreaming) != null ? _p : false,
13440
+ environment: process.env.NODE_ENV || "development"
13222
13441
  };
13223
13442
  await fetch(telemetryUrl, {
13224
13443
  method: "POST",
@@ -13234,43 +13453,8 @@ async function reportTelemetry(req, plugin, action, status, details, trace) {
13234
13453
  } catch (e) {
13235
13454
  }
13236
13455
  }
13237
- function sseFrame(payload) {
13238
- return `data: ${JSON.stringify(payload)}
13239
-
13240
- `;
13241
- }
13242
- function sseTextFrame(text) {
13243
- return `data: ${JSON.stringify({ type: "text", text })}
13244
-
13245
- `;
13246
- }
13247
- function sseMetaFrame(meta) {
13248
- return `data: ${JSON.stringify(__spreadValues({ type: "metadata" }, meta != null ? meta : {}))}
13249
-
13250
- `;
13251
- }
13252
- function sseUIFrame(uiTransformation) {
13253
- return `data: ${JSON.stringify({ type: "ui_transformation", data: uiTransformation })}
13254
-
13255
- `;
13256
- }
13257
- function sseObservabilityFrame(trace) {
13258
- return `data: ${JSON.stringify({ type: "observability", data: trace })}
13259
13456
 
13260
- `;
13261
- }
13262
- function sseErrorFrame(message) {
13263
- return `data: ${JSON.stringify({ type: "error", error: message })}
13264
-
13265
- `;
13266
- }
13267
- var SSE_HEADERS = {
13268
- "Content-Type": "text/event-stream; charset=utf-8",
13269
- "Cache-Control": "no-cache, no-transform",
13270
- Connection: "keep-alive",
13271
- "X-Accel-Buffering": "no"
13272
- // Disable Nginx buffering for streaming
13273
- };
13457
+ // src/handlers/chatHandler.ts
13274
13458
  function createChatHandler(configOrPlugin, options) {
13275
13459
  const plugin = getOrCreatePlugin(configOrPlugin);
13276
13460
  const storage = new DatabaseStorage(plugin.getConfig());
@@ -13322,7 +13506,7 @@ function createChatHandler(configOrPlugin, options) {
13322
13506
  if (!namespaceCheck.ok) return namespaceCheck.response;
13323
13507
  const sanitized = sanitizeInput(rawMessage != null ? rawMessage : "");
13324
13508
  if (!sanitized.ok) {
13325
- return NextResponse.json({ error: { code: "INVALID_INPUT", message: sanitized.error } }, { status: 400 });
13509
+ return NextResponse3.json({ error: { code: "INVALID_INPUT", message: sanitized.error } }, { status: 400 });
13326
13510
  }
13327
13511
  const message = sanitized.value;
13328
13512
  const userMsgId = userMessageId || `user_${Date.now()}_${Math.random().toString(36).slice(2, 6)}`;
@@ -13341,18 +13525,68 @@ function createChatHandler(configOrPlugin, options) {
13341
13525
  uiTransformation: result.ui_transformation,
13342
13526
  trace: result.trace
13343
13527
  }).catch((err) => console.warn("[createChatHandler] Failed to save assistant message:", err));
13344
- 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);
13345
- return NextResponse.json(__spreadProps(__spreadValues({}, result), {
13528
+ await reportTelemetry(
13529
+ req,
13530
+ plugin,
13531
+ "RAG_QUERY",
13532
+ "success",
13533
+ `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`,
13534
+ result.trace,
13535
+ { isStreaming: false }
13536
+ );
13537
+ return NextResponse3.json(__spreadProps(__spreadValues({}, result), {
13346
13538
  messageId: assistantMsgId,
13347
13539
  userMessageId: userMsgId
13348
13540
  }));
13349
13541
  } catch (err) {
13350
13542
  const message = err instanceof Error ? err.message : "Internal server error";
13351
- await reportTelemetry(req, plugin, "QUERY_GENERATION", "error", message);
13352
- return NextResponse.json({ error: message }, { status: 500 });
13543
+ const { errorCode, errorType } = classifyError(message);
13544
+ await reportTelemetry(req, plugin, "RAG_QUERY", "error", message, void 0, { isStreaming: false, errorCode, errorType });
13545
+ return NextResponse3.json({ error: message }, { status: 500 });
13353
13546
  }
13354
13547
  };
13355
13548
  }
13549
+
13550
+ // src/handlers/internal/sse.ts
13551
+ function sseFrame(payload) {
13552
+ return `data: ${JSON.stringify(payload)}
13553
+
13554
+ `;
13555
+ }
13556
+ function sseTextFrame(text) {
13557
+ return `data: ${JSON.stringify({ type: "text", text })}
13558
+
13559
+ `;
13560
+ }
13561
+ function sseMetaFrame(meta) {
13562
+ return `data: ${JSON.stringify(__spreadValues({ type: "metadata" }, meta != null ? meta : {}))}
13563
+
13564
+ `;
13565
+ }
13566
+ function sseUIFrame(uiTransformation) {
13567
+ return `data: ${JSON.stringify({ type: "ui_transformation", data: uiTransformation })}
13568
+
13569
+ `;
13570
+ }
13571
+ function sseObservabilityFrame(trace) {
13572
+ return `data: ${JSON.stringify({ type: "observability", data: trace })}
13573
+
13574
+ `;
13575
+ }
13576
+ function sseErrorFrame(message) {
13577
+ return `data: ${JSON.stringify({ type: "error", error: message })}
13578
+
13579
+ `;
13580
+ }
13581
+ var SSE_HEADERS = {
13582
+ "Content-Type": "text/event-stream; charset=utf-8",
13583
+ "Cache-Control": "no-cache, no-transform",
13584
+ Connection: "keep-alive",
13585
+ "X-Accel-Buffering": "no"
13586
+ // Disable Nginx buffering for streaming
13587
+ };
13588
+
13589
+ // src/handlers/streamHandler.ts
13356
13590
  function createStreamHandler(configOrPlugin, options) {
13357
13591
  const plugin = getOrCreatePlugin(configOrPlugin);
13358
13592
  const storage = new DatabaseStorage(plugin.getConfig());
@@ -13427,6 +13661,7 @@ function createStreamHandler(configOrPlugin, options) {
13427
13661
  const assistantMsgId = messageId || `assistant_${Date.now()}_${Math.random().toString(36).slice(2, 6)}`;
13428
13662
  const encoder = new TextEncoder();
13429
13663
  let isActive = true;
13664
+ let streamTelemetrySent = false;
13430
13665
  const stream = new ReadableStream({
13431
13666
  async start(controller) {
13432
13667
  var _a3, _b2, _c, _d, _e;
@@ -13441,11 +13676,16 @@ function createStreamHandler(configOrPlugin, options) {
13441
13676
  try {
13442
13677
  const pipelineStream = plugin.chatStream(message, history, namespaceCheck.namespace);
13443
13678
  let fullReply = "";
13679
+ let ttftMs;
13680
+ const handlerRequestStartMs = Date.now();
13444
13681
  try {
13445
13682
  for (var iter = __forAwait(pipelineStream), more, temp, error; more = !(temp = await iter.next()).done; more = false) {
13446
13683
  const chunk = temp.value;
13447
13684
  if (!isActive) break;
13448
13685
  if (typeof chunk === "string") {
13686
+ if (ttftMs === void 0) {
13687
+ ttftMs = Date.now() - handlerRequestStartMs;
13688
+ }
13449
13689
  fullReply += chunk;
13450
13690
  enqueue(sseTextFrame(chunk));
13451
13691
  } else if (chunk && typeof chunk === "object" && "type" in chunk && chunk.type === "thinking") {
@@ -13486,7 +13726,16 @@ function createStreamHandler(configOrPlugin, options) {
13486
13726
  uiTransformation,
13487
13727
  trace: responseChunk == null ? void 0 : responseChunk.trace
13488
13728
  }).catch((err) => console.warn("[createStreamHandler] Failed to save assistant message:", err));
13489
- 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);
13729
+ streamTelemetrySent = true;
13730
+ await reportTelemetry(
13731
+ req,
13732
+ plugin,
13733
+ "RAG_QUERY",
13734
+ "success",
13735
+ `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`,
13736
+ responseChunk == null ? void 0 : responseChunk.trace,
13737
+ { ttftMs, isStreaming: true }
13738
+ );
13490
13739
  }
13491
13740
  }
13492
13741
  } catch (temp) {
@@ -13502,11 +13751,21 @@ function createStreamHandler(configOrPlugin, options) {
13502
13751
  } catch (streamError) {
13503
13752
  if (isActive) {
13504
13753
  let errorMessage = streamError instanceof Error ? streamError.message : String(streamError);
13505
- if (errorMessage.includes("403") || errorMessage.toLowerCase().includes("license") || errorMessage.toLowerCase().includes("revoked") || errorMessage.toLowerCase().includes("terminated")) {
13754
+ 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")) {
13506
13755
  errorMessage = "Your Retrivora license key has been terminated, suspended, or revoked. Access denied.";
13507
13756
  }
13508
13757
  console.error("[createStreamHandler] Stream error:", streamError);
13509
- await reportTelemetry(req, plugin, "QUERY_GENERATION", "error", errorMessage);
13758
+ const { errorCode, errorType } = classifyError(errorMessage);
13759
+ streamTelemetrySent = true;
13760
+ await reportTelemetry(
13761
+ req,
13762
+ plugin,
13763
+ "RAG_QUERY",
13764
+ "error",
13765
+ errorMessage,
13766
+ void 0,
13767
+ { isStreaming: true, errorCode, errorType }
13768
+ );
13510
13769
  try {
13511
13770
  enqueue(sseErrorFrame(errorMessage));
13512
13771
  } catch (e) {
@@ -13515,6 +13774,10 @@ function createStreamHandler(configOrPlugin, options) {
13515
13774
  } finally {
13516
13775
  if (isActive) {
13517
13776
  isActive = false;
13777
+ try {
13778
+ enqueue(sseFrame({ type: "done" }));
13779
+ } catch (e) {
13780
+ }
13518
13781
  try {
13519
13782
  controller.close();
13520
13783
  } catch (e) {
@@ -13524,11 +13787,26 @@ function createStreamHandler(configOrPlugin, options) {
13524
13787
  },
13525
13788
  cancel(reason) {
13526
13789
  isActive = false;
13790
+ if (!streamTelemetrySent) {
13791
+ reportTelemetry(
13792
+ req,
13793
+ plugin,
13794
+ "RAG_QUERY_ABORTED",
13795
+ "error",
13796
+ "Client disconnected mid-stream",
13797
+ void 0,
13798
+ { isStreaming: true, errorCode: "CLIENT_DISCONNECT", errorType: "STREAMING_ERROR" }
13799
+ ).catch(() => {
13800
+ });
13801
+ }
13527
13802
  }
13528
13803
  });
13529
13804
  return new Response(stream, { headers: __spreadProps(__spreadValues({}, SSE_HEADERS), { "x-message-id": assistantMsgId, "x-user-message-id": userMsgId }) });
13530
13805
  };
13531
13806
  }
13807
+
13808
+ // src/handlers/ingestHandler.ts
13809
+ import { NextResponse as NextResponse4 } from "next/server";
13532
13810
  function createIngestHandler(configOrPlugin, options) {
13533
13811
  const plugin = getOrCreatePlugin(configOrPlugin);
13534
13812
  const onAuthorize = options == null ? void 0 : options.onAuthorize;
@@ -13542,7 +13820,7 @@ function createIngestHandler(configOrPlugin, options) {
13542
13820
  if (!licenseCheck.ok) return licenseCheck.response;
13543
13821
  const { documents, namespace } = body;
13544
13822
  if (!Array.isArray(documents) || documents.length === 0) {
13545
- return NextResponse.json({ error: "documents array is required" }, { status: 400 });
13823
+ return NextResponse4.json({ error: "documents array is required" }, { status: 400 });
13546
13824
  }
13547
13825
  const namespaceCheck = resolveLicensedNamespace(licenseCheck.payload, namespace);
13548
13826
  if (!namespaceCheck.ok) return namespaceCheck.response;
@@ -13569,14 +13847,17 @@ function createIngestHandler(configOrPlugin, options) {
13569
13847
  }
13570
13848
  const results = await plugin.ingest(documents, namespaceCheck.namespace);
13571
13849
  await reportTelemetry(req, plugin, "DOCUMENT_INGESTION", "success", `Ingested ${documents.length} document(s)`);
13572
- return NextResponse.json({ results });
13850
+ return NextResponse4.json({ results });
13573
13851
  } catch (err) {
13574
13852
  const message = err instanceof Error ? err.message : "Internal server error";
13575
13853
  await reportTelemetry(req, plugin, "DOCUMENT_INGESTION", "error", message);
13576
- return NextResponse.json({ error: message }, { status: 500 });
13854
+ return NextResponse4.json({ error: message }, { status: 500 });
13577
13855
  }
13578
13856
  };
13579
13857
  }
13858
+
13859
+ // src/handlers/healthHandler.ts
13860
+ import { NextResponse as NextResponse5 } from "next/server";
13580
13861
  function createHealthHandler(configOrPlugin, options) {
13581
13862
  const plugin = getOrCreatePlugin(configOrPlugin);
13582
13863
  const onAuthorize = options == null ? void 0 : options.onAuthorize;
@@ -13588,17 +13869,21 @@ function createHealthHandler(configOrPlugin, options) {
13588
13869
  try {
13589
13870
  const health = await plugin.checkHealth();
13590
13871
  const status = health.allHealthy ? "ok" : "degraded";
13591
- return NextResponse.json(__spreadProps(__spreadValues({
13872
+ return NextResponse5.json(__spreadProps(__spreadValues({
13592
13873
  status
13593
13874
  }, health), {
13594
13875
  timestamp: (/* @__PURE__ */ new Date()).toISOString()
13595
13876
  }));
13596
13877
  } catch (err) {
13597
13878
  const message = err instanceof Error ? err.message : "Unknown error";
13598
- return NextResponse.json({ status: "error", error: message }, { status: 500 });
13879
+ return NextResponse5.json({ status: "error", error: message }, { status: 500 });
13599
13880
  }
13600
13881
  };
13601
13882
  }
13883
+
13884
+ // src/handlers/licenseHandler.ts
13885
+ import { NextResponse as NextResponse6 } from "next/server";
13886
+ init_version();
13602
13887
  function createLicenseHandler(configOrPlugin, options) {
13603
13888
  const plugin = getOrCreatePlugin(configOrPlugin);
13604
13889
  const onAuthorize = options == null ? void 0 : options.onAuthorize;
@@ -13624,7 +13909,7 @@ function createLicenseHandler(configOrPlugin, options) {
13624
13909
  const licenseKey = (rawKey || "").trim().replace(/^["']|["']$/g, "").trim();
13625
13910
  const projectId = (body == null ? void 0 : body.projectId) || config.projectId || "";
13626
13911
  const payload = await LicenseVerifier.verifyAsync(licenseKey, projectId);
13627
- return NextResponse.json({
13912
+ return NextResponse6.json({
13628
13913
  valid: true,
13629
13914
  licenseStatus: "ACTIVE",
13630
13915
  minimumSupportedVersion: "2.1.0",
@@ -13634,7 +13919,7 @@ function createLicenseHandler(configOrPlugin, options) {
13634
13919
  message: "License key is active and valid."
13635
13920
  });
13636
13921
  } catch (err) {
13637
- return NextResponse.json(
13922
+ return NextResponse6.json(
13638
13923
  {
13639
13924
  valid: false,
13640
13925
  licenseStatus: "REVOKED",
@@ -13649,6 +13934,9 @@ function createLicenseHandler(configOrPlugin, options) {
13649
13934
  }
13650
13935
  };
13651
13936
  }
13937
+
13938
+ // src/handlers/uploadHandler.ts
13939
+ import { NextResponse as NextResponse7 } from "next/server";
13652
13940
  function createUploadHandler(configOrPlugin, options) {
13653
13941
  const plugin = getOrCreatePlugin(configOrPlugin);
13654
13942
  const onAuthorize = options == null ? void 0 : options.onAuthorize;
@@ -13667,7 +13955,7 @@ function createUploadHandler(configOrPlugin, options) {
13667
13955
  const namespaceCheck = resolveLicensedNamespace(licenseCheck.payload, namespace);
13668
13956
  if (!namespaceCheck.ok) return namespaceCheck.response;
13669
13957
  if (!files || files.length === 0) {
13670
- return NextResponse.json({ error: "No files provided" }, { status: 400 });
13958
+ return NextResponse7.json({ error: "No files provided" }, { status: 400 });
13671
13959
  }
13672
13960
  if (isFreeTier(licenseCheck.payload.tier)) {
13673
13961
  const totalUploadBytes = files.reduce((sum, f) => sum + (f.size || 0), 0);
@@ -13689,137 +13977,174 @@ function createUploadHandler(configOrPlugin, options) {
13689
13977
  );
13690
13978
  }
13691
13979
  }
13692
- const documents = [];
13693
- for (const file of files) {
13694
- const buffer = Buffer.isBuffer(file) ? file : Buffer.from(await file.arrayBuffer());
13695
- const isExcel = file.name.toLowerCase().endsWith(".xlsx") || file.name.toLowerCase().endsWith(".xls") || file.type.includes("spreadsheet") || file.type.includes("excel");
13696
- if (file.name.toLowerCase().endsWith(".csv") || file.type === "text/csv") {
13697
- const text = buffer.toString("utf-8");
13698
- const Papa = await import("papaparse");
13699
- const parsed = Papa.default.parse(text, { header: true, skipEmptyLines: true });
13700
- if (parsed.data && parsed.data.length > 0) {
13701
- let i = 0;
13702
- let lastRowData = null;
13703
- const csvCols = Object.keys(parsed.data[0]);
13704
- for (const row of parsed.data) {
13705
- i++;
13706
- const rowData = row;
13707
- const groupingKey = Object.keys(rowData)[0];
13708
- if (lastRowData && groupingKey && rowData[groupingKey] && rowData[groupingKey] === lastRowData[groupingKey]) {
13709
- for (const key of Object.keys(rowData)) {
13710
- if (!rowData[key] && lastRowData[key]) {
13711
- rowData[key] = lastRowData[key];
13980
+ const fileDocsArrays = await Promise.all(
13981
+ files.map(async (file) => {
13982
+ const fileDocs = [];
13983
+ const buffer = Buffer.isBuffer(file) ? file : Buffer.from(await file.arrayBuffer());
13984
+ const isExcel = file.name.toLowerCase().endsWith(".xlsx") || file.name.toLowerCase().endsWith(".xls") || file.type.includes("spreadsheet") || file.type.includes("excel");
13985
+ const SPREADSHEET_CHUNK_SIZE = 30;
13986
+ if (file.name.toLowerCase().endsWith(".csv") || file.name.toLowerCase().endsWith(".tsv") || file.type === "text/csv" || file.type.includes("tab-separated")) {
13987
+ const text = buffer.toString("utf-8");
13988
+ const Papa = await import("papaparse");
13989
+ const parsed = Papa.default.parse(text, { header: true, skipEmptyLines: true });
13990
+ if (parsed.data && parsed.data.length > 0) {
13991
+ const csvCols = Object.keys(parsed.data[0]);
13992
+ const rawRows = [];
13993
+ let lastRowData = null;
13994
+ for (const row of parsed.data) {
13995
+ const rowData = __spreadValues({}, row);
13996
+ const groupingKey = Object.keys(rowData)[0];
13997
+ if (lastRowData && groupingKey && rowData[groupingKey] && rowData[groupingKey] === lastRowData[groupingKey]) {
13998
+ for (const key of Object.keys(rowData)) {
13999
+ if (!rowData[key] && lastRowData[key]) {
14000
+ rowData[key] = lastRowData[key];
14001
+ }
13712
14002
  }
13713
14003
  }
14004
+ lastRowData = __spreadValues({}, rowData);
14005
+ rawRows.push(rowData);
13714
14006
  }
13715
- lastRowData = __spreadValues({}, rowData);
13716
- const contentParts = [];
13717
- for (const [key, val] of Object.entries(rowData)) {
13718
- if (key && val) {
13719
- contentParts.push(`${key}: ${val}`);
14007
+ for (let i = 0; i < rawRows.length; i += SPREADSHEET_CHUNK_SIZE) {
14008
+ const slice = rawRows.slice(i, i + SPREADSHEET_CHUNK_SIZE);
14009
+ const startRow = i + 1;
14010
+ const endRow = i + slice.length;
14011
+ const chunkIndex = Math.floor(i / SPREADSHEET_CHUNK_SIZE) + 1;
14012
+ const lines = [
14013
+ `File: ${file.name} (Rows ${startRow}-${endRow} of ${rawRows.length})`,
14014
+ `| ${csvCols.join(" | ")} |`,
14015
+ `| ${csvCols.map(() => "---").join(" | ")} |`
14016
+ ];
14017
+ for (const r of slice) {
14018
+ const rowValues = csvCols.map((col) => {
14019
+ const val = r[col];
14020
+ if (val === void 0 || val === null) return "";
14021
+ return String(val).replace(/\|/g, "\\|").replace(/\r?\n/g, " ").trim();
14022
+ });
14023
+ lines.push(`| ${rowValues.join(" | ")} |`);
13720
14024
  }
14025
+ fileDocs.push({
14026
+ docId: `${file.name}-chunk-${chunkIndex}`,
14027
+ content: lines.join("\n"),
14028
+ metadata: __spreadValues({
14029
+ fileName: file.name,
14030
+ fileSize: file.size,
14031
+ fileType: file.type || "text/csv",
14032
+ uploadedAt: (/* @__PURE__ */ new Date()).toISOString(),
14033
+ startRow,
14034
+ endRow,
14035
+ rowCount: slice.length,
14036
+ headers: csvCols
14037
+ }, dimension ? { dimension } : {})
14038
+ });
13721
14039
  }
13722
- documents.push({
13723
- docId: `${file.name}-row-${i}`,
13724
- content: contentParts.join(", "),
13725
- metadata: __spreadValues(__spreadProps(__spreadValues({
13726
- fileName: file.name,
13727
- fileSize: file.size,
13728
- fileType: file.type || "text/csv",
13729
- uploadedAt: (/* @__PURE__ */ new Date()).toISOString()
13730
- }, dimension ? { dimension } : {}), {
13731
- csvHeaders: csvCols
13732
- }), rowData)
13733
- });
13734
14040
  }
13735
- }
13736
- } else if (isExcel) {
13737
- let excelLoaded = false;
13738
- try {
13739
- const isZip = buffer.length >= 4 && buffer[0] === 80 && buffer[1] === 75;
13740
- if (isZip) {
13741
- const ExcelJS = await import("exceljs");
13742
- const workbook = new ExcelJS.Workbook();
13743
- await workbook.xlsx.load(buffer);
13744
- workbook.eachSheet((sheet, sheetId) => {
13745
- const sheetName = sheet.name;
13746
- let headers = [];
13747
- let i = 0;
13748
- sheet.eachRow((row, rowNumber) => {
13749
- const values = Array.isArray(row.values) ? row.values.slice(1) : [];
13750
- if (rowNumber === 1) {
13751
- headers = values.map((v) => String(v));
13752
- return;
13753
- }
13754
- i++;
13755
- const contentParts = [];
13756
- const rowData = {};
13757
- values.forEach((val, index) => {
13758
- const key = headers[index] || `Column${index + 1}`;
13759
- rowData[key] = val;
13760
- if (key && val !== void 0 && val !== null && String(val).trim()) {
13761
- contentParts.push(`${key}: ${val}`);
13762
- }
13763
- });
13764
- if (contentParts.length > 0) {
13765
- documents.push({
13766
- docId: `${file.name}-${sheetName}-row-${i}`,
13767
- content: contentParts.join(", "),
13768
- metadata: __spreadValues(__spreadProps(__spreadValues({
13769
- fileName: file.name,
13770
- sheetName,
13771
- fileSize: file.size,
13772
- fileType: file.type || "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
13773
- uploadedAt: (/* @__PURE__ */ new Date()).toISOString()
13774
- }, dimension ? { dimension } : {}), {
13775
- headers
13776
- }), rowData)
14041
+ } else if (isExcel) {
14042
+ let excelLoaded = false;
14043
+ try {
14044
+ const XLSX = await import("xlsx");
14045
+ const workbook = XLSX.read(buffer, { type: "buffer", cellDates: true });
14046
+ for (const sheetName of workbook.SheetNames) {
14047
+ const sheet = workbook.Sheets[sheetName];
14048
+ if (!sheet) continue;
14049
+ const rawGrid = XLSX.utils.sheet_to_json(sheet, { header: 1, defval: "", blankrows: false });
14050
+ if (!rawGrid || rawGrid.length === 0) continue;
14051
+ let headerRowIndex = 0;
14052
+ while (headerRowIndex < rawGrid.length && (!rawGrid[headerRowIndex] || !rawGrid[headerRowIndex].some((c) => String(c != null ? c : "").trim().length > 0))) {
14053
+ headerRowIndex++;
14054
+ }
14055
+ if (headerRowIndex >= rawGrid.length) continue;
14056
+ const rawHeaders = rawGrid[headerRowIndex].map((h, idx) => {
14057
+ const s = String(h != null ? h : "").trim();
14058
+ return s || `Column_${idx + 1}`;
14059
+ });
14060
+ const dataRows = rawGrid.slice(headerRowIndex + 1).filter(
14061
+ (row) => row && row.some((c) => String(c != null ? c : "").trim().length > 0)
14062
+ );
14063
+ if (dataRows.length === 0) continue;
14064
+ for (let i = 0; i < dataRows.length; i += SPREADSHEET_CHUNK_SIZE) {
14065
+ const slice = dataRows.slice(i, i + SPREADSHEET_CHUNK_SIZE);
14066
+ const startRow = headerRowIndex + 1 + i + 1;
14067
+ const endRow = startRow + slice.length - 1;
14068
+ const chunkIndex = Math.floor(i / SPREADSHEET_CHUNK_SIZE) + 1;
14069
+ const lines = [
14070
+ `File: ${file.name} | Sheet: ${sheetName} (Rows ${startRow}-${endRow} of ${dataRows.length})`,
14071
+ `| ${rawHeaders.join(" | ")} |`,
14072
+ `| ${rawHeaders.map(() => "---").join(" | ")} |`
14073
+ ];
14074
+ for (const r of slice) {
14075
+ const rowValues = rawHeaders.map((_, colIdx) => {
14076
+ const val = r[colIdx];
14077
+ if (val === void 0 || val === null) return "";
14078
+ return String(val).replace(/\|/g, "\\|").replace(/\r?\n/g, " ").trim();
13777
14079
  });
14080
+ lines.push(`| ${rowValues.join(" | ")} |`);
13778
14081
  }
14082
+ fileDocs.push({
14083
+ docId: `${file.name}-${sheetName}-chunk-${chunkIndex}`,
14084
+ content: lines.join("\n"),
14085
+ metadata: __spreadValues({
14086
+ fileName: file.name,
14087
+ sheetName,
14088
+ fileSize: file.size,
14089
+ fileType: file.type || "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
14090
+ uploadedAt: (/* @__PURE__ */ new Date()).toISOString(),
14091
+ startRow,
14092
+ endRow,
14093
+ rowCount: slice.length,
14094
+ headers: rawHeaders
14095
+ }, dimension ? { dimension } : {})
14096
+ });
14097
+ }
14098
+ }
14099
+ excelLoaded = fileDocs.length > 0;
14100
+ } catch (xlsxErr) {
14101
+ excelLoaded = false;
14102
+ }
14103
+ if (!excelLoaded) {
14104
+ const content = await DocumentParser.parse(buffer, file.name, file.type);
14105
+ if (content && content.trim()) {
14106
+ fileDocs.push({
14107
+ docId: file.name,
14108
+ content,
14109
+ metadata: __spreadValues({
14110
+ fileName: file.name,
14111
+ fileSize: file.size,
14112
+ fileType: file.type || "application/vnd.ms-excel",
14113
+ uploadedAt: (/* @__PURE__ */ new Date()).toISOString()
14114
+ }, dimension ? { dimension } : {})
13779
14115
  });
13780
- });
13781
- excelLoaded = documents.length > 0;
14116
+ }
13782
14117
  }
13783
- } catch (excelErr) {
13784
- excelLoaded = false;
13785
- }
13786
- if (!excelLoaded) {
14118
+ } else {
13787
14119
  const content = await DocumentParser.parse(buffer, file.name, file.type);
13788
- documents.push({
14120
+ fileDocs.push({
13789
14121
  docId: file.name,
13790
14122
  content,
13791
14123
  metadata: __spreadValues({
13792
14124
  fileName: file.name,
13793
14125
  fileSize: file.size,
13794
- fileType: file.type || "application/vnd.ms-excel",
14126
+ fileType: file.type,
13795
14127
  uploadedAt: (/* @__PURE__ */ new Date()).toISOString()
13796
14128
  }, dimension ? { dimension } : {})
13797
14129
  });
13798
14130
  }
13799
- } else {
13800
- const content = await DocumentParser.parse(buffer, file.name, file.type);
13801
- documents.push({
13802
- docId: file.name,
13803
- content,
13804
- metadata: __spreadValues({
13805
- fileName: file.name,
13806
- fileSize: file.size,
13807
- fileType: file.type,
13808
- uploadedAt: (/* @__PURE__ */ new Date()).toISOString()
13809
- }, dimension ? { dimension } : {})
13810
- });
13811
- }
13812
- }
14131
+ return fileDocs;
14132
+ })
14133
+ );
14134
+ const documents = fileDocsArrays.flat();
13813
14135
  const results = await plugin.ingest(documents, namespaceCheck.namespace);
13814
14136
  await reportTelemetry(req, plugin, "DOCUMENT_INGESTION", "success", `Uploaded & ingested ${files.length} file(s)`);
13815
- return NextResponse.json({ message: "Upload successful", results });
14137
+ return NextResponse7.json({ message: "Upload successful", results });
13816
14138
  } catch (err) {
13817
14139
  const message = err instanceof Error ? err.message : "Upload failed";
13818
14140
  await reportTelemetry(req, plugin, "DOCUMENT_INGESTION", "error", message);
13819
- return NextResponse.json({ error: message }, { status: 500 });
14141
+ return NextResponse7.json({ error: message }, { status: 500 });
13820
14142
  }
13821
14143
  };
13822
14144
  }
14145
+
14146
+ // src/handlers/suggestionsHandler.ts
14147
+ import { NextResponse as NextResponse8 } from "next/server";
13823
14148
  function createSuggestionsHandler(configOrPlugin, options) {
13824
14149
  const plugin = getOrCreatePlugin(configOrPlugin);
13825
14150
  const onAuthorize = options == null ? void 0 : options.onAuthorize;
@@ -13850,20 +14175,23 @@ function createSuggestionsHandler(configOrPlugin, options) {
13850
14175
  namespace = url.searchParams.get("namespace") || void 0;
13851
14176
  }
13852
14177
  if (!query || typeof query !== "string" || !query.trim()) {
13853
- return NextResponse.json({ suggestions: [] });
14178
+ return NextResponse8.json({ suggestions: [] });
13854
14179
  }
13855
14180
  const namespaceCheck = resolveLicensedNamespace(effectiveLicensePayload, namespace);
13856
14181
  if (!namespaceCheck.ok) return namespaceCheck.response;
13857
14182
  const suggestions = await plugin.getSuggestions(query, namespaceCheck.namespace);
13858
14183
  await reportTelemetry(req, plugin, "AUTO_SUGGESTIONS", "success", `Fetched ${suggestions.length} suggestions for: ${query.slice(0, 30)}`);
13859
- return NextResponse.json({ suggestions });
14184
+ return NextResponse8.json({ suggestions });
13860
14185
  } catch (err) {
13861
14186
  const message = err instanceof Error ? err.message : "Internal server error";
13862
14187
  await reportTelemetry(req, plugin, "AUTO_SUGGESTIONS", "error", message);
13863
- return NextResponse.json({ error: message }, { status: 500 });
14188
+ return NextResponse8.json({ error: message }, { status: 500 });
13864
14189
  }
13865
14190
  };
13866
14191
  }
14192
+
14193
+ // src/handlers/historyHandler.ts
14194
+ import { NextResponse as NextResponse9 } from "next/server";
13867
14195
  function createHistoryHandler(configOrPlugin, options) {
13868
14196
  const plugin = getOrCreatePlugin(configOrPlugin);
13869
14197
  const storage = new DatabaseStorage(plugin.getConfig());
@@ -13878,25 +14206,28 @@ function createHistoryHandler(configOrPlugin, options) {
13878
14206
  if (req.method === "GET") {
13879
14207
  try {
13880
14208
  const history = await storage.getHistory(sessionId, scope);
13881
- return NextResponse.json({ history });
14209
+ return NextResponse9.json({ history });
13882
14210
  } catch (err) {
13883
14211
  const message = err instanceof Error ? err.message : "Failed to fetch history";
13884
- return NextResponse.json({ error: message }, { status: 500 });
14212
+ return NextResponse9.json({ error: message }, { status: 500 });
13885
14213
  }
13886
14214
  } else if (req.method === "POST") {
13887
14215
  try {
13888
14216
  const body = await req.json().catch(() => ({}));
13889
14217
  const sid = body.sessionId || sessionId;
13890
14218
  await storage.clearHistory(sid, scope);
13891
- return NextResponse.json({ success: true });
14219
+ return NextResponse9.json({ success: true });
13892
14220
  } catch (err) {
13893
14221
  const message = err instanceof Error ? err.message : "Failed to clear history";
13894
- return NextResponse.json({ error: message }, { status: 500 });
14222
+ return NextResponse9.json({ error: message }, { status: 500 });
13895
14223
  }
13896
14224
  }
13897
- return NextResponse.json({ error: "Method Not Allowed" }, { status: 405 });
14225
+ return NextResponse9.json({ error: "Method Not Allowed" }, { status: 405 });
13898
14226
  };
13899
14227
  }
14228
+
14229
+ // src/handlers/feedbackHandler.ts
14230
+ import { NextResponse as NextResponse10 } from "next/server";
13900
14231
  function createFeedbackHandler(configOrPlugin, options) {
13901
14232
  const plugin = getOrCreatePlugin(configOrPlugin);
13902
14233
  const storage = new DatabaseStorage(plugin.getConfig());
@@ -13912,45 +14243,48 @@ function createFeedbackHandler(configOrPlugin, options) {
13912
14243
  const messageId = url.searchParams.get("messageId");
13913
14244
  if (!messageId) {
13914
14245
  const feedbackList = await storage.listFeedback(scope);
13915
- return NextResponse.json({ feedback: feedbackList });
14246
+ return NextResponse10.json({ feedback: feedbackList });
13916
14247
  }
13917
14248
  const feedback = await storage.getFeedback(messageId, scope);
13918
- return NextResponse.json({ feedback });
14249
+ return NextResponse10.json({ feedback });
13919
14250
  } catch (err) {
13920
14251
  const message = err instanceof Error ? err.message : "Failed to fetch feedback";
13921
- return NextResponse.json({ error: message }, { status: 500 });
14252
+ return NextResponse10.json({ error: message }, { status: 500 });
13922
14253
  }
13923
14254
  } else if (req.method === "POST") {
13924
14255
  try {
13925
14256
  const body = await req.json();
13926
14257
  const { messageId, sessionId = "default", rating, comment } = body;
13927
14258
  if (!messageId) {
13928
- return NextResponse.json({ error: "messageId is required" }, { status: 400 });
14259
+ return NextResponse10.json({ error: "messageId is required" }, { status: 400 });
13929
14260
  }
13930
14261
  if (!rating) {
13931
- return NextResponse.json({ error: "rating is required" }, { status: 400 });
14262
+ return NextResponse10.json({ error: "rating is required" }, { status: 400 });
13932
14263
  }
13933
14264
  if (scope && scope.length > 0) {
13934
14265
  const matches = scope.some(
13935
14266
  (pid) => pid && (sessionId === pid || sessionId.startsWith(pid) || sessionId.includes(pid))
13936
14267
  );
13937
14268
  if (!matches) {
13938
- return NextResponse.json(
14269
+ return NextResponse10.json(
13939
14270
  { error: "sessionId is outside the authorized project scope" },
13940
14271
  { status: 403 }
13941
14272
  );
13942
14273
  }
13943
14274
  }
13944
14275
  await storage.saveFeedback({ messageId, sessionId, rating, comment });
13945
- return NextResponse.json({ success: true });
14276
+ return NextResponse10.json({ success: true });
13946
14277
  } catch (err) {
13947
14278
  const message = err instanceof Error ? err.message : "Failed to save feedback";
13948
- return NextResponse.json({ error: message }, { status: 500 });
14279
+ return NextResponse10.json({ error: message }, { status: 500 });
13949
14280
  }
13950
14281
  }
13951
- return NextResponse.json({ error: "Method Not Allowed" }, { status: 405 });
14282
+ return NextResponse10.json({ error: "Method Not Allowed" }, { status: 405 });
13952
14283
  };
13953
14284
  }
14285
+
14286
+ // src/handlers/sessionsHandler.ts
14287
+ import { NextResponse as NextResponse11 } from "next/server";
13954
14288
  function createSessionsHandler(configOrPlugin, options) {
13955
14289
  const plugin = getOrCreatePlugin(configOrPlugin);
13956
14290
  const storage = new DatabaseStorage(plugin.getConfig());
@@ -13964,13 +14298,16 @@ function createSessionsHandler(configOrPlugin, options) {
13964
14298
  try {
13965
14299
  const scope = req && onResolveScope ? await onResolveScope(req) : void 0;
13966
14300
  const sessions = await storage.listSessions(scope);
13967
- return NextResponse.json({ sessions });
14301
+ return NextResponse11.json({ sessions });
13968
14302
  } catch (err) {
13969
14303
  const message = err instanceof Error ? err.message : "Failed to list sessions";
13970
- return NextResponse.json({ error: message }, { status: 500 });
14304
+ return NextResponse11.json({ error: message }, { status: 500 });
13971
14305
  }
13972
14306
  };
13973
14307
  }
14308
+
14309
+ // src/handlers/ragHandler.ts
14310
+ import { NextResponse as NextResponse12 } from "next/server";
13974
14311
  function createRagHandler(configOrPlugin, options) {
13975
14312
  const plugin = getOrCreatePlugin(configOrPlugin);
13976
14313
  const onAuthorize = options == null ? void 0 : options.onAuthorize;
@@ -14006,7 +14343,7 @@ function createRagHandler(configOrPlugin, options) {
14006
14343
  case "v1/license/validate":
14007
14344
  return licenseHandler(req);
14008
14345
  default:
14009
- return NextResponse.json({ error: `Not Found: POST segment "${segment}" not supported.` }, { status: 404 });
14346
+ return NextResponse12.json({ error: `Not Found: POST segment "${segment}" not supported.` }, { status: 404 });
14010
14347
  }
14011
14348
  }
14012
14349
  async function routeGetRequest(req, segment) {
@@ -14022,7 +14359,7 @@ function createRagHandler(configOrPlugin, options) {
14022
14359
  case "sessions":
14023
14360
  return sessionsHandler(req);
14024
14361
  default:
14025
- return NextResponse.json({ error: `Method Not Allowed: GET segment "${segment}" not supported.` }, { status: 405 });
14362
+ return NextResponse12.json({ error: `Method Not Allowed: GET segment "${segment}" not supported.` }, { status: 405 });
14026
14363
  }
14027
14364
  }
14028
14365
  async function getSegment(req, context) {
@@ -14057,7 +14394,7 @@ function createRagHandler(configOrPlugin, options) {
14057
14394
  return await routeGetRequest(req, segment);
14058
14395
  } catch (err) {
14059
14396
  const msg = err instanceof Error ? err.message : "GET Routing failed";
14060
- return NextResponse.json({ error: msg }, { status: 500 });
14397
+ return NextResponse12.json({ error: msg }, { status: 500 });
14061
14398
  }
14062
14399
  },
14063
14400
  POST: async (req, context) => {
@@ -14066,7 +14403,7 @@ function createRagHandler(configOrPlugin, options) {
14066
14403
  return await routePostRequest(req, segment);
14067
14404
  } catch (err) {
14068
14405
  const msg = err instanceof Error ? err.message : "POST Routing failed";
14069
- return NextResponse.json({ error: msg }, { status: 500 });
14406
+ return NextResponse12.json({ error: msg }, { status: 500 });
14070
14407
  }
14071
14408
  }
14072
14409
  };
@@ -14115,11 +14452,11 @@ var _LicenseValidator = class _LicenseValidator {
14115
14452
  * Primary validation entry-point for UI / hook consumers.
14116
14453
  *
14117
14454
  * Resolves the license key, consults the local success cache, performs a
14118
- * server POST for authoritative status, and falls back to local RSA
14119
- * signature verification if the server can't be reached.
14455
+ * server POST for authoritative status, and falls back to `LicenseVerifier.verifyAsync()`
14456
+ * if the server can't be reached.
14120
14457
  *
14121
14458
  * @throws LicenseValidationError - When no key is provided OR both the
14122
- * server call AND the local-RSA fallback report an invalid/expired key.
14459
+ * server call AND the fallback report an invalid/expired key.
14123
14460
  * @throws SDKVersionUnsupportedError - When server reports forceUpgrade=true.
14124
14461
  * @returns A `LicenseValidationResponse` that the UI can use to enable or
14125
14462
  * lock the chat widget.
@@ -14168,6 +14505,8 @@ var _LicenseValidator = class _LicenseValidator {
14168
14505
  this.cache.delete(effectiveLicenseKey);
14169
14506
  }
14170
14507
  }
14508
+ const existing = this.inflight.get(effectiveLicenseKey);
14509
+ if (existing) return existing;
14171
14510
  const inflightPromise = this._doNetworkValidate(
14172
14511
  effectiveLicenseKey,
14173
14512
  effectiveProjectId,
@@ -14436,8 +14775,8 @@ var LicenseConfigResolver = class {
14436
14775
  };
14437
14776
  }
14438
14777
  };
14439
- LicenseConfigResolver.RETRIVORA_CONFIG_URL = "https://www.retrivora.com/api/v1/config";
14440
- LicenseConfigResolver.RETRIVORA_CREDENTIALS_URL = "https://www.retrivora.com/api/v1/credentials";
14778
+ LicenseConfigResolver.RETRIVORA_CONFIG_URL = "https://llm.retrivora.com/api/v1/config";
14779
+ LicenseConfigResolver.RETRIVORA_CREDENTIALS_URL = "https://llm.retrivora.com/api/v1/credentials";
14441
14780
  LicenseConfigResolver.CACHE_TTL_MS = 36e5;
14442
14781
  // 1 hour
14443
14782
  LicenseConfigResolver.configCache = /* @__PURE__ */ new Map();
@@ -14601,6 +14940,9 @@ var _TelemetryService = class _TelemetryService {
14601
14940
  console.warn("[TelemetryService] Auto-flush error:", err.message);
14602
14941
  });
14603
14942
  }, this.flushIntervalMs);
14943
+ if (this.flushTimer && typeof this.flushTimer.unref === "function") {
14944
+ this.flushTimer.unref();
14945
+ }
14604
14946
  }
14605
14947
  /**
14606
14948
  * Stop telemetry service and flush remaining events
@@ -14873,6 +15215,7 @@ export {
14873
15215
  IntentClassifier,
14874
15216
  LLMFactory,
14875
15217
  LLM_PROFILES,
15218
+ LangChainAgent,
14876
15219
  LicenseConfigResolver,
14877
15220
  LicenseValidationError,
14878
15221
  LicenseValidator,
@@ -14932,10 +15275,12 @@ export {
14932
15275
  createHealthHandler,
14933
15276
  createHistoryHandler,
14934
15277
  createIngestHandler,
15278
+ createLicenseHandler,
14935
15279
  createRagHandler,
14936
15280
  createRetrivora,
14937
15281
  createSessionsHandler,
14938
15282
  createStreamHandler,
15283
+ createSuggestionsHandler,
14939
15284
  createUploadHandler,
14940
15285
  decideVisualization,
14941
15286
  getRagConfig,
@@ -14949,6 +15294,8 @@ export {
14949
15294
  sseErrorFrame,
14950
15295
  sseFrame,
14951
15296
  sseMetaFrame,
15297
+ sseObservabilityFrame,
14952
15298
  sseTextFrame,
15299
+ sseUIFrame,
14953
15300
  wrapError
14954
15301
  };