@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/README.md +8 -6
- package/dist/{ILLMProvider-DUCL1r75.d.mts → ILLMProvider-CzXcTA6d.d.mts} +21 -3
- package/dist/{ILLMProvider-DUCL1r75.d.ts → ILLMProvider-CzXcTA6d.d.ts} +21 -3
- package/dist/handlers/index.d.mts +2 -2
- package/dist/handlers/index.d.ts +2 -2
- package/dist/handlers/index.js +9692 -9343
- package/dist/handlers/index.mjs +9692 -9351
- package/dist/{index-BVSHblli.d.mts → index-BST9Gzsv.d.mts} +8 -7
- package/dist/{index-CsBcqng1.d.ts → index-CFl4KrKq.d.ts} +58 -46
- package/dist/{index-D6Oq2zyc.d.mts → index-D7FuQRSb.d.mts} +58 -46
- package/dist/{index-DGW0NNmR.d.ts → index-qLvtMUr-.d.ts} +8 -7
- package/dist/index.css +23 -0
- package/dist/index.d.mts +4 -4
- package/dist/index.d.ts +4 -4
- package/dist/index.js +267 -187
- package/dist/index.min.css +1 -1
- package/dist/index.mjs +268 -193
- package/dist/server.d.mts +58 -12
- package/dist/server.d.ts +58 -12
- package/dist/server.js +915 -557
- package/dist/server.mjs +910 -563
- package/package.json +1 -1
package/dist/server.js
CHANGED
|
@@ -332,7 +332,7 @@ var init_package = __esm({
|
|
|
332
332
|
"package.json"() {
|
|
333
333
|
package_default = {
|
|
334
334
|
name: "@retrivora-ai/rag-engine",
|
|
335
|
-
version: "2.6.
|
|
335
|
+
version: "2.6.2",
|
|
336
336
|
description: "Retrivora AI is a plug-and-play AI engine for RAG chat experiences \u2014 generic vector DB + LLM provider, embeddable or standalone.",
|
|
337
337
|
author: "Abhinav Alkuchi",
|
|
338
338
|
license: "UNLICENSED",
|
|
@@ -511,7 +511,7 @@ var init_ConfigFetcher = __esm({
|
|
|
511
511
|
init_ProjectSettings();
|
|
512
512
|
init_version();
|
|
513
513
|
CONTROL_PLANE_URL = "https://www.retrivora.com";
|
|
514
|
-
LLM_PROXY_BASE_URL = "https://
|
|
514
|
+
LLM_PROXY_BASE_URL = "https://llm.retrivora.com/api/v1";
|
|
515
515
|
ConfigFetcher = class {
|
|
516
516
|
// 5-minute in-memory session cache
|
|
517
517
|
/**
|
|
@@ -2733,6 +2733,7 @@ __export(server_exports, {
|
|
|
2733
2733
|
IntentClassifier: () => IntentClassifier,
|
|
2734
2734
|
LLMFactory: () => LLMFactory,
|
|
2735
2735
|
LLM_PROFILES: () => LLM_PROFILES,
|
|
2736
|
+
LangChainAgent: () => LangChainAgent,
|
|
2736
2737
|
LicenseConfigResolver: () => LicenseConfigResolver,
|
|
2737
2738
|
LicenseValidationError: () => LicenseValidationError,
|
|
2738
2739
|
LicenseValidator: () => LicenseValidator,
|
|
@@ -2792,10 +2793,12 @@ __export(server_exports, {
|
|
|
2792
2793
|
createHealthHandler: () => createHealthHandler,
|
|
2793
2794
|
createHistoryHandler: () => createHistoryHandler,
|
|
2794
2795
|
createIngestHandler: () => createIngestHandler,
|
|
2796
|
+
createLicenseHandler: () => createLicenseHandler,
|
|
2795
2797
|
createRagHandler: () => createRagHandler,
|
|
2796
2798
|
createRetrivora: () => createRetrivora,
|
|
2797
2799
|
createSessionsHandler: () => createSessionsHandler,
|
|
2798
2800
|
createStreamHandler: () => createStreamHandler,
|
|
2801
|
+
createSuggestionsHandler: () => createSuggestionsHandler,
|
|
2799
2802
|
createUploadHandler: () => createUploadHandler,
|
|
2800
2803
|
decideVisualization: () => decideVisualization,
|
|
2801
2804
|
getRagConfig: () => getRagConfig,
|
|
@@ -2809,7 +2812,9 @@ __export(server_exports, {
|
|
|
2809
2812
|
sseErrorFrame: () => sseErrorFrame,
|
|
2810
2813
|
sseFrame: () => sseFrame,
|
|
2811
2814
|
sseMetaFrame: () => sseMetaFrame,
|
|
2815
|
+
sseObservabilityFrame: () => sseObservabilityFrame,
|
|
2812
2816
|
sseTextFrame: () => sseTextFrame,
|
|
2817
|
+
sseUIFrame: () => sseUIFrame,
|
|
2813
2818
|
wrapError: () => wrapError
|
|
2814
2819
|
});
|
|
2815
2820
|
module.exports = __toCommonJS(server_exports);
|
|
@@ -2875,13 +2880,14 @@ function wrapError(err, defaultCode, defaultMessage) {
|
|
|
2875
2880
|
const message = (error == null ? void 0 : error.message) || defaultMessage || String(err);
|
|
2876
2881
|
const status = (error == null ? void 0 : error.status) || (error == null ? void 0 : error.statusCode) || ((_a2 = error == null ? void 0 : error.response) == null ? void 0 : _a2.status);
|
|
2877
2882
|
const code = error == null ? void 0 : error.code;
|
|
2878
|
-
if (status === 429 || /rate[- ]?limit/i.test(message) || code === "RATE_LIMIT_EXCEEDED") {
|
|
2883
|
+
if (status === 429 || /rate[- ]?limit|token limit|too many requests|tokens per minute/i.test(message) || code === "RATE_LIMIT_EXCEEDED") {
|
|
2879
2884
|
return new RateLimitException(message, err);
|
|
2880
2885
|
}
|
|
2881
|
-
if (
|
|
2882
|
-
|
|
2883
|
-
|
|
2884
|
-
|
|
2886
|
+
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)) {
|
|
2887
|
+
return new AuthenticationException("Your Retrivora license key has been terminated, suspended, or revoked. Access denied.", err);
|
|
2888
|
+
}
|
|
2889
|
+
if (status === 401 || status === 403 || code === "INVALID_API_KEY" || code === "AUTHENTICATION_ERROR" || /unauthorized|forbidden|invalid api key/i.test(message)) {
|
|
2890
|
+
return new AuthenticationException(message, err);
|
|
2885
2891
|
}
|
|
2886
2892
|
switch (defaultCode) {
|
|
2887
2893
|
case "PROVIDER_NOT_FOUND":
|
|
@@ -2902,6 +2908,20 @@ function wrapError(err, defaultCode, defaultMessage) {
|
|
|
2902
2908
|
}
|
|
2903
2909
|
|
|
2904
2910
|
// src/core/LicenseVerifier.ts
|
|
2911
|
+
function decodeBase64UrlToUtf8(b64url) {
|
|
2912
|
+
const normalized = b64url.replace(/-/g, "+").replace(/_/g, "/");
|
|
2913
|
+
const padded = normalized + "=".repeat((4 - normalized.length % 4) % 4);
|
|
2914
|
+
if (typeof Buffer !== "undefined" && typeof Buffer.from === "function") {
|
|
2915
|
+
return Buffer.from(padded, "base64").toString("utf8");
|
|
2916
|
+
}
|
|
2917
|
+
if (typeof atob === "function") {
|
|
2918
|
+
const binary = atob(padded);
|
|
2919
|
+
const bytes = new Uint8Array(binary.length);
|
|
2920
|
+
for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
|
|
2921
|
+
return new TextDecoder().decode(bytes);
|
|
2922
|
+
}
|
|
2923
|
+
throw new Error("Base64 decoding is not supported in this environment.");
|
|
2924
|
+
}
|
|
2905
2925
|
var _LicenseVerifier = class _LicenseVerifier {
|
|
2906
2926
|
// Cache verified license for 5 minutes
|
|
2907
2927
|
/**
|
|
@@ -3033,6 +3053,9 @@ var _LicenseVerifier = class _LicenseVerifier {
|
|
|
3033
3053
|
minimumSupportedVersion: data.minimumSupportedVersion,
|
|
3034
3054
|
forceUpgrade: data.forceUpgrade
|
|
3035
3055
|
};
|
|
3056
|
+
if (payload.expiresAt && Math.floor(Date.now() / 1e3) > payload.expiresAt) {
|
|
3057
|
+
throw new Error("License key has expired.");
|
|
3058
|
+
}
|
|
3036
3059
|
if (isProduction && provider) {
|
|
3037
3060
|
const normalizedProvider = provider.toLowerCase();
|
|
3038
3061
|
if (tier === "hobby") {
|
|
@@ -3071,63 +3094,67 @@ var _LicenseVerifier = class _LicenseVerifier {
|
|
|
3071
3094
|
}
|
|
3072
3095
|
}
|
|
3073
3096
|
static async verifyIP(licenseKey) {
|
|
3097
|
+
var _a2, _b;
|
|
3074
3098
|
if (!licenseKey) return;
|
|
3075
3099
|
try {
|
|
3076
3100
|
const parts = licenseKey.split(".");
|
|
3077
3101
|
if (parts.length !== 3) return;
|
|
3078
3102
|
const [, payloadB64] = parts;
|
|
3079
3103
|
const payload = JSON.parse(
|
|
3080
|
-
|
|
3104
|
+
decodeBase64UrlToUtf8(payloadB64)
|
|
3081
3105
|
);
|
|
3082
3106
|
const tier = (payload.tier || "").toLowerCase();
|
|
3083
|
-
if (tier === "enterprise" || tier === "custom") {
|
|
3107
|
+
if (tier === "enterprise" || tier === "custom" || !payload.ipv4 && !payload.ipv6) {
|
|
3084
3108
|
return;
|
|
3085
3109
|
}
|
|
3086
|
-
|
|
3087
|
-
|
|
3088
|
-
|
|
3089
|
-
|
|
3090
|
-
|
|
3091
|
-
|
|
3092
|
-
|
|
3093
|
-
|
|
3110
|
+
let currentIpv4 = "";
|
|
3111
|
+
let currentIpv6 = "";
|
|
3112
|
+
const now = Date.now();
|
|
3113
|
+
if (this.cachedIps && now - this.cachedIps.fetchedAt < 10 * 60 * 1e3) {
|
|
3114
|
+
currentIpv4 = this.cachedIps.ipv4;
|
|
3115
|
+
currentIpv6 = this.cachedIps.ipv6;
|
|
3116
|
+
} else {
|
|
3117
|
+
const [v4Result, v6Result] = await Promise.allSettled([
|
|
3118
|
+
fetch("https://api4.ipify.org?format=json", { signal: AbortSignal.timeout(2e3) }).then((r) => r.json()),
|
|
3119
|
+
fetch("https://api6.ipify.org?format=json", { signal: AbortSignal.timeout(2e3) }).then((r) => r.json())
|
|
3120
|
+
]);
|
|
3121
|
+
if (v4Result.status === "fulfilled" && ((_a2 = v4Result.value) == null ? void 0 : _a2.ip)) {
|
|
3122
|
+
currentIpv4 = v4Result.value.ip;
|
|
3094
3123
|
}
|
|
3095
|
-
|
|
3096
|
-
|
|
3097
|
-
const data = await res.json();
|
|
3098
|
-
currentIpv6 = data.ip;
|
|
3099
|
-
} catch (e) {
|
|
3124
|
+
if (v6Result.status === "fulfilled" && ((_b = v6Result.value) == null ? void 0 : _b.ip)) {
|
|
3125
|
+
currentIpv6 = v6Result.value.ip;
|
|
3100
3126
|
}
|
|
3101
|
-
|
|
3102
|
-
|
|
3103
|
-
|
|
3104
|
-
|
|
3105
|
-
|
|
3106
|
-
|
|
3107
|
-
|
|
3108
|
-
|
|
3109
|
-
|
|
3127
|
+
this.cachedIps = { ipv4: currentIpv4, ipv6: currentIpv6, fetchedAt: now };
|
|
3128
|
+
}
|
|
3129
|
+
const localIps = [];
|
|
3130
|
+
try {
|
|
3131
|
+
const osModule = await import("os");
|
|
3132
|
+
const interfaces = osModule.networkInterfaces();
|
|
3133
|
+
for (const name of Object.keys(interfaces)) {
|
|
3134
|
+
for (const iface of interfaces[name] || []) {
|
|
3135
|
+
if (!iface.internal) {
|
|
3136
|
+
localIps.push(iface.address);
|
|
3110
3137
|
}
|
|
3111
3138
|
}
|
|
3112
|
-
} catch (e) {
|
|
3113
3139
|
}
|
|
3114
|
-
|
|
3115
|
-
|
|
3116
|
-
|
|
3117
|
-
|
|
3118
|
-
|
|
3119
|
-
|
|
3120
|
-
);
|
|
3121
|
-
}
|
|
3122
|
-
} else if (payload.ipv4 && !isIpv4Match) {
|
|
3123
|
-
throw new Error(
|
|
3124
|
-
`License key access restricted. This license key was created for a different system (authorized IPv4: ${payload.ipv4}) and cannot be used on this machine.`
|
|
3125
|
-
);
|
|
3126
|
-
} else if (payload.ipv6 && !isIpv6Match) {
|
|
3140
|
+
} catch (e) {
|
|
3141
|
+
}
|
|
3142
|
+
const isIpv4Match = payload.ipv4 && (currentIpv4 === payload.ipv4 || localIps.includes(payload.ipv4));
|
|
3143
|
+
const isIpv6Match = payload.ipv6 && (currentIpv6 === payload.ipv6 || localIps.includes(payload.ipv6));
|
|
3144
|
+
if (payload.ipv4 && payload.ipv6) {
|
|
3145
|
+
if (!isIpv4Match && !isIpv6Match) {
|
|
3127
3146
|
throw new Error(
|
|
3128
|
-
`License key access restricted. This license key was created for a different system (authorized IPv6: ${payload.ipv6}) and cannot be used on this machine.`
|
|
3147
|
+
`License key access restricted. This license key was created for a different system (authorized IPv4: ${payload.ipv4}, IPv6: ${payload.ipv6}) and cannot be used on this machine.`
|
|
3129
3148
|
);
|
|
3130
3149
|
}
|
|
3150
|
+
} else if (payload.ipv4 && !isIpv4Match) {
|
|
3151
|
+
throw new Error(
|
|
3152
|
+
`License key access restricted. This license key was created for a different system (authorized IPv4: ${payload.ipv4}) and cannot be used on this machine.`
|
|
3153
|
+
);
|
|
3154
|
+
} else if (payload.ipv6 && !isIpv6Match) {
|
|
3155
|
+
throw new Error(
|
|
3156
|
+
`License key access restricted. This license key was created for a different system (authorized IPv6: ${payload.ipv6}) and cannot be used on this machine.`
|
|
3157
|
+
);
|
|
3131
3158
|
}
|
|
3132
3159
|
} catch (err) {
|
|
3133
3160
|
if (err.message.includes("License key access restricted")) {
|
|
@@ -3142,7 +3169,7 @@ var _LicenseVerifier = class _LicenseVerifier {
|
|
|
3142
3169
|
const rawToken = sanitizedKey.replace(/^rtv_/i, "").trim();
|
|
3143
3170
|
const parts = rawToken.split(".");
|
|
3144
3171
|
if (parts.length === 3) {
|
|
3145
|
-
const decodedPayload = JSON.parse(
|
|
3172
|
+
const decodedPayload = JSON.parse(decodeBase64UrlToUtf8(parts[1]));
|
|
3146
3173
|
if (decodedPayload.tier) return decodedPayload.tier;
|
|
3147
3174
|
}
|
|
3148
3175
|
} catch (e) {
|
|
@@ -3153,6 +3180,7 @@ var _LicenseVerifier = class _LicenseVerifier {
|
|
|
3153
3180
|
// In-memory cache to save network overhead on hot paths.
|
|
3154
3181
|
_LicenseVerifier.cache = /* @__PURE__ */ new Map();
|
|
3155
3182
|
_LicenseVerifier.CACHE_TTL_MS = 5 * 60 * 1e3;
|
|
3183
|
+
_LicenseVerifier.cachedIps = null;
|
|
3156
3184
|
var LicenseVerifier = _LicenseVerifier;
|
|
3157
3185
|
|
|
3158
3186
|
// src/config/constants.ts
|
|
@@ -4408,7 +4436,7 @@ var GroqProvider = class {
|
|
|
4408
4436
|
};
|
|
4409
4437
|
}
|
|
4410
4438
|
async chat(messages, context, options) {
|
|
4411
|
-
var _a2, _b, _c, _d, _e, _f, _g2, _h, _i;
|
|
4439
|
+
var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k, _l;
|
|
4412
4440
|
const basePrompt = (_b = (_a2 = options == null ? void 0 : options.systemPrompt) != null ? _a2 : this.llmConfig.systemPrompt) != null ? _b : "You are a helpful assistant. Answer questions based on the provided context.";
|
|
4413
4441
|
const systemMessage = {
|
|
4414
4442
|
role: "system",
|
|
@@ -4422,18 +4450,46 @@ var GroqProvider = class {
|
|
|
4422
4450
|
systemMessage,
|
|
4423
4451
|
...safeMessages
|
|
4424
4452
|
];
|
|
4425
|
-
const
|
|
4426
|
-
|
|
4427
|
-
|
|
4428
|
-
|
|
4429
|
-
|
|
4430
|
-
|
|
4431
|
-
|
|
4432
|
-
|
|
4453
|
+
const model = this.llmConfig.model === "qwen/qwen3.6-27b" || this.llmConfig.model === "qwen-3.6-27b" ? "qwen/qwen3.8-27b" : this.llmConfig.model;
|
|
4454
|
+
try {
|
|
4455
|
+
const completion = await this.client.chat.completions.create({
|
|
4456
|
+
model,
|
|
4457
|
+
messages: formattedMessages,
|
|
4458
|
+
max_tokens: Math.min((_d = (_c = options == null ? void 0 : options.maxTokens) != null ? _c : this.llmConfig.maxTokens) != null ? _d : 800, 800),
|
|
4459
|
+
temperature: (_f = (_e = options == null ? void 0 : options.temperature) != null ? _e : this.llmConfig.temperature) != null ? _f : 0.7,
|
|
4460
|
+
stop: options == null ? void 0 : options.stop
|
|
4461
|
+
});
|
|
4462
|
+
return (_i = (_h = (_g2 = completion.choices[0]) == null ? void 0 : _g2.message) == null ? void 0 : _h.content) != null ? _i : "";
|
|
4463
|
+
} catch (err) {
|
|
4464
|
+
console.warn("[GroqProvider] Groq chat request failed, falling back strictly to Hugging Face Qwen:", (err == null ? void 0 : err.message) || err);
|
|
4465
|
+
const hfToken = process.env.HF_TOKEN || process.env.HUGGINGFACE_API_KEY || "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";
|
|
4466
|
+
try {
|
|
4467
|
+
const hfRes = await fetch("https://router.huggingface.co/v1/chat/completions", {
|
|
4468
|
+
method: "POST",
|
|
4469
|
+
headers: {
|
|
4470
|
+
"Content-Type": "application/json",
|
|
4471
|
+
"Authorization": `Bearer ${hfToken}`
|
|
4472
|
+
},
|
|
4473
|
+
body: JSON.stringify({
|
|
4474
|
+
model: "Qwen/Qwen2.5-Coder-32B-Instruct",
|
|
4475
|
+
messages: formattedMessages,
|
|
4476
|
+
max_tokens: 1024,
|
|
4477
|
+
temperature: 0.7
|
|
4478
|
+
})
|
|
4479
|
+
});
|
|
4480
|
+
if (hfRes.ok) {
|
|
4481
|
+
const data = await hfRes.json();
|
|
4482
|
+
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;
|
|
4483
|
+
if (content !== void 0) return content;
|
|
4484
|
+
}
|
|
4485
|
+
} catch (e) {
|
|
4486
|
+
}
|
|
4487
|
+
throw err;
|
|
4488
|
+
}
|
|
4433
4489
|
}
|
|
4434
4490
|
chatStream(messages, context, options) {
|
|
4435
4491
|
return __asyncGenerator(this, null, function* () {
|
|
4436
|
-
var _a2, _b, _c, _d, _e, _f, _g2, _h;
|
|
4492
|
+
var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k, _l;
|
|
4437
4493
|
const basePrompt = (_b = (_a2 = options == null ? void 0 : options.systemPrompt) != null ? _a2 : this.llmConfig.systemPrompt) != null ? _b : "You are a helpful assistant. Answer questions based on the provided context.";
|
|
4438
4494
|
const systemMessage = {
|
|
4439
4495
|
role: "system",
|
|
@@ -4447,32 +4503,81 @@ var GroqProvider = class {
|
|
|
4447
4503
|
systemMessage,
|
|
4448
4504
|
...safeStreamMessages
|
|
4449
4505
|
];
|
|
4450
|
-
const
|
|
4451
|
-
model: this.llmConfig.model,
|
|
4452
|
-
messages: formattedMessages,
|
|
4453
|
-
max_tokens: (_d = (_c = options == null ? void 0 : options.maxTokens) != null ? _c : this.llmConfig.maxTokens) != null ? _d : 1024,
|
|
4454
|
-
temperature: (_f = (_e = options == null ? void 0 : options.temperature) != null ? _e : this.llmConfig.temperature) != null ? _f : 0.7,
|
|
4455
|
-
stop: options == null ? void 0 : options.stop,
|
|
4456
|
-
stream: true
|
|
4457
|
-
}));
|
|
4458
|
-
if (!stream) {
|
|
4459
|
-
throw new Error("[GroqProvider] completions.create stream is undefined");
|
|
4460
|
-
}
|
|
4506
|
+
const model = this.llmConfig.model === "qwen/qwen3.6-27b" || this.llmConfig.model === "qwen-3.6-27b" ? "qwen/qwen3.8-27b" : this.llmConfig.model;
|
|
4461
4507
|
try {
|
|
4462
|
-
|
|
4463
|
-
|
|
4464
|
-
|
|
4465
|
-
|
|
4508
|
+
const stream = yield new __await(this.client.chat.completions.create({
|
|
4509
|
+
model,
|
|
4510
|
+
messages: formattedMessages,
|
|
4511
|
+
max_tokens: Math.min((_d = (_c = options == null ? void 0 : options.maxTokens) != null ? _c : this.llmConfig.maxTokens) != null ? _d : 800, 800),
|
|
4512
|
+
temperature: (_f = (_e = options == null ? void 0 : options.temperature) != null ? _e : this.llmConfig.temperature) != null ? _f : 0.7,
|
|
4513
|
+
stop: options == null ? void 0 : options.stop,
|
|
4514
|
+
stream: true
|
|
4515
|
+
}));
|
|
4516
|
+
if (!stream) {
|
|
4517
|
+
throw new Error("[GroqProvider] completions.create stream is undefined");
|
|
4466
4518
|
}
|
|
4467
|
-
} catch (temp) {
|
|
4468
|
-
error = [temp];
|
|
4469
|
-
} finally {
|
|
4470
4519
|
try {
|
|
4471
|
-
|
|
4520
|
+
for (var iter = __forAwait(stream), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
|
|
4521
|
+
const chunk = temp.value;
|
|
4522
|
+
const content = ((_h = (_g2 = chunk.choices[0]) == null ? void 0 : _g2.delta) == null ? void 0 : _h.content) || "";
|
|
4523
|
+
if (content) yield content;
|
|
4524
|
+
}
|
|
4525
|
+
} catch (temp) {
|
|
4526
|
+
error = [temp];
|
|
4472
4527
|
} finally {
|
|
4473
|
-
|
|
4474
|
-
|
|
4528
|
+
try {
|
|
4529
|
+
more && (temp = iter.return) && (yield new __await(temp.call(iter)));
|
|
4530
|
+
} finally {
|
|
4531
|
+
if (error)
|
|
4532
|
+
throw error[0];
|
|
4533
|
+
}
|
|
4534
|
+
}
|
|
4535
|
+
} catch (err) {
|
|
4536
|
+
console.warn("[GroqProvider] Groq chat stream failed, falling back strictly to Hugging Face Qwen stream:", (err == null ? void 0 : err.message) || err);
|
|
4537
|
+
const hfToken = process.env.HF_TOKEN || process.env.HUGGINGFACE_API_KEY || "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";
|
|
4538
|
+
try {
|
|
4539
|
+
const hfRes = yield new __await(fetch("https://router.huggingface.co/v1/chat/completions", {
|
|
4540
|
+
method: "POST",
|
|
4541
|
+
headers: {
|
|
4542
|
+
"Content-Type": "application/json",
|
|
4543
|
+
"Authorization": `Bearer ${hfToken}`
|
|
4544
|
+
},
|
|
4545
|
+
body: JSON.stringify({
|
|
4546
|
+
model: "Qwen/Qwen2.5-Coder-32B-Instruct",
|
|
4547
|
+
messages: formattedMessages,
|
|
4548
|
+
max_tokens: 1024,
|
|
4549
|
+
temperature: 0.7,
|
|
4550
|
+
stream: true
|
|
4551
|
+
})
|
|
4552
|
+
}));
|
|
4553
|
+
if (hfRes.ok && hfRes.body) {
|
|
4554
|
+
const reader = hfRes.body.getReader();
|
|
4555
|
+
const decoder = new TextDecoder("utf-8");
|
|
4556
|
+
let buffer = "";
|
|
4557
|
+
while (true) {
|
|
4558
|
+
const { done, value } = yield new __await(reader.read());
|
|
4559
|
+
if (done) break;
|
|
4560
|
+
buffer += decoder.decode(value, { stream: true });
|
|
4561
|
+
const lines = buffer.split("\n");
|
|
4562
|
+
buffer = (_i = lines.pop()) != null ? _i : "";
|
|
4563
|
+
for (const line of lines) {
|
|
4564
|
+
const trimmed = line.trim();
|
|
4565
|
+
if (!trimmed || trimmed === "data: [DONE]") continue;
|
|
4566
|
+
if (!trimmed.startsWith("data:")) continue;
|
|
4567
|
+
try {
|
|
4568
|
+
const json = JSON.parse(trimmed.slice(5).trim());
|
|
4569
|
+
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) || "";
|
|
4570
|
+
if (content) yield content;
|
|
4571
|
+
} catch (e) {
|
|
4572
|
+
}
|
|
4573
|
+
}
|
|
4574
|
+
}
|
|
4575
|
+
return;
|
|
4576
|
+
}
|
|
4577
|
+
} catch (hfErr) {
|
|
4578
|
+
console.warn("[GroqProvider] Hugging Face Qwen fallback stream failed:", hfErr);
|
|
4475
4579
|
}
|
|
4580
|
+
throw err;
|
|
4476
4581
|
}
|
|
4477
4582
|
});
|
|
4478
4583
|
}
|
|
@@ -4922,6 +5027,7 @@ ${context != null ? context : "None"}` },
|
|
|
4922
5027
|
} catch (fetchErr) {
|
|
4923
5028
|
console.warn(`[UniversalLLMAdapter] Remote HTTP stream fetch warning: ${fetchErr == null ? void 0 : fetchErr.message}. Attempting in-process fallback...`);
|
|
4924
5029
|
}
|
|
5030
|
+
let lastDispatchErr = null;
|
|
4925
5031
|
if (!streamBody) {
|
|
4926
5032
|
const _g2 = globalThis;
|
|
4927
5033
|
const dispatch = _g2.__retrivoraDispatchChat;
|
|
@@ -4944,12 +5050,16 @@ ${context != null ? context : "None"}` },
|
|
|
4944
5050
|
}
|
|
4945
5051
|
}
|
|
4946
5052
|
} catch (dispatchErr) {
|
|
4947
|
-
|
|
5053
|
+
lastDispatchErr = dispatchErr;
|
|
5054
|
+
console.warn("[UniversalLLMAdapter] In-process dispatch error:", (dispatchErr == null ? void 0 : dispatchErr.message) || dispatchErr);
|
|
4948
5055
|
}
|
|
4949
5056
|
}
|
|
4950
5057
|
}
|
|
4951
5058
|
if (!streamBody) {
|
|
4952
|
-
|
|
5059
|
+
if (lastDispatchErr) {
|
|
5060
|
+
throw lastDispatchErr;
|
|
5061
|
+
}
|
|
5062
|
+
throw new Error(`[UniversalLLMAdapter] Streaming request failed: LLM provider unavailable.`);
|
|
4953
5063
|
}
|
|
4954
5064
|
const reader = streamBody.getReader();
|
|
4955
5065
|
const decoder = new TextDecoder("utf-8");
|
|
@@ -4988,72 +5098,74 @@ ${context != null ? context : "None"}` },
|
|
|
4988
5098
|
});
|
|
4989
5099
|
}
|
|
4990
5100
|
async embed(text) {
|
|
4991
|
-
var _a2, _b, _c, _d, _e, _f,
|
|
4992
|
-
const
|
|
5101
|
+
var _a2, _b, _c, _d, _e, _f, _g3, _h;
|
|
5102
|
+
const _g2 = globalThis;
|
|
5103
|
+
const dispatchEmbed = _g2.__retrivoraDispatchEmbedding;
|
|
5104
|
+
if (typeof dispatchEmbed === "function") {
|
|
5105
|
+
try {
|
|
5106
|
+
const res = await dispatchEmbed({ input: text, model: this.model }, this.apiKey);
|
|
5107
|
+
if ((_b = (_a2 = res == null ? void 0 : res.data) == null ? void 0 : _a2[0]) == null ? void 0 : _b.embedding) {
|
|
5108
|
+
return res.data[0].embedding;
|
|
5109
|
+
}
|
|
5110
|
+
} catch (dispatchErr) {
|
|
5111
|
+
}
|
|
5112
|
+
}
|
|
5113
|
+
const path2 = (_c = this.opts.embedPath) != null ? _c : "/embeddings";
|
|
4993
5114
|
const payload = this.opts.embedPayloadTemplate ? buildPayload(this.opts.embedPayloadTemplate, { input: text, model: this.model }) : { input: text, model: this.model };
|
|
4994
5115
|
try {
|
|
4995
5116
|
const { data } = await this.http.post(path2, payload);
|
|
4996
|
-
const extractPath = (
|
|
5117
|
+
const extractPath = (_d = this.opts.embedExtractPath) != null ? _d : "data[0].embedding";
|
|
4997
5118
|
const vector = resolvePath(data, extractPath);
|
|
4998
5119
|
if (Array.isArray(vector)) {
|
|
4999
5120
|
return vector;
|
|
5000
5121
|
}
|
|
5001
|
-
console.warn(
|
|
5002
|
-
`[UniversalLLMAdapter] Embedding API returned unexpected format for path '${extractPath}'. Response:`,
|
|
5003
|
-
data
|
|
5004
|
-
);
|
|
5005
5122
|
} catch (err) {
|
|
5006
|
-
console.warn(
|
|
5007
|
-
`[UniversalLLMAdapter] HTTP embedding request failed: ${(err == null ? void 0 : err.message) || err}`,
|
|
5008
|
-
{ path: path2, model: this.model, response: (_c = err == null ? void 0 : err.response) == null ? void 0 : _c.data }
|
|
5009
|
-
);
|
|
5010
|
-
const _g3 = globalThis;
|
|
5011
|
-
const dispatchEmbed = _g3.__retrivoraDispatchEmbedding;
|
|
5012
5123
|
if (typeof dispatchEmbed === "function") {
|
|
5013
5124
|
try {
|
|
5014
5125
|
const res = await dispatchEmbed({ input: text, model: this.model }, this.apiKey);
|
|
5015
|
-
if ((
|
|
5126
|
+
if ((_f = (_e = res == null ? void 0 : res.data) == null ? void 0 : _e[0]) == null ? void 0 : _f.embedding) {
|
|
5016
5127
|
return res.data[0].embedding;
|
|
5017
5128
|
}
|
|
5018
5129
|
} catch (dispatchErr) {
|
|
5019
5130
|
console.warn("[UniversalLLMAdapter] In-process dispatch embedding failed:", dispatchErr);
|
|
5020
5131
|
}
|
|
5021
5132
|
}
|
|
5022
|
-
}
|
|
5023
|
-
try {
|
|
5024
|
-
const { data } = await this.http.post(path2, payload);
|
|
5025
|
-
const extractPath = (_f = this.opts.embedExtractPath) != null ? _f : "data[0].embedding";
|
|
5026
|
-
const vector = resolvePath(data, extractPath);
|
|
5027
|
-
if (Array.isArray(vector)) {
|
|
5028
|
-
return vector;
|
|
5029
|
-
}
|
|
5030
|
-
console.error(
|
|
5031
|
-
`[UniversalLLMAdapter] Retry embedding failed: unexpected response format for path '${extractPath}'. Response:`,
|
|
5032
|
-
data
|
|
5033
|
-
);
|
|
5034
|
-
throw new Error(
|
|
5035
|
-
`[UniversalLLMAdapter] Could not extract embedding vector. Expected array at '${extractPath}', got: ${typeof vector}. Response keys: ${Object.keys(data || {})}.`
|
|
5036
|
-
);
|
|
5037
|
-
} catch (err) {
|
|
5038
5133
|
console.error(
|
|
5039
|
-
`[UniversalLLMAdapter]
|
|
5040
|
-
{ error: err == null ? void 0 : err.message, statusCode: (
|
|
5134
|
+
`[UniversalLLMAdapter] Embedding failed. Path: ${path2}, Model: ${this.model}`,
|
|
5135
|
+
{ error: err == null ? void 0 : err.message, statusCode: (_g3 = err == null ? void 0 : err.response) == null ? void 0 : _g3.status, response: (_h = err == null ? void 0 : err.response) == null ? void 0 : _h.data }
|
|
5041
5136
|
);
|
|
5042
5137
|
throw new Error(
|
|
5043
|
-
`[UniversalLLMAdapter] Failed to generate embedding
|
|
5138
|
+
`[UniversalLLMAdapter] Failed to generate embedding: ${(err == null ? void 0 : err.message) || err}`
|
|
5044
5139
|
);
|
|
5045
5140
|
}
|
|
5141
|
+
throw new Error(`[UniversalLLMAdapter] Failed to extract embedding vector.`);
|
|
5046
5142
|
}
|
|
5047
5143
|
async batchEmbed(texts) {
|
|
5048
|
-
|
|
5049
|
-
|
|
5144
|
+
var _a2;
|
|
5145
|
+
if (!texts || texts.length === 0) return [];
|
|
5146
|
+
const _g2 = globalThis;
|
|
5147
|
+
const dispatchEmbed = _g2.__retrivoraDispatchEmbedding;
|
|
5148
|
+
if (typeof dispatchEmbed === "function") {
|
|
5050
5149
|
try {
|
|
5051
|
-
const
|
|
5052
|
-
|
|
5053
|
-
|
|
5054
|
-
|
|
5055
|
-
|
|
5150
|
+
const res = await dispatchEmbed({ input: texts, model: this.model }, this.apiKey);
|
|
5151
|
+
if ((res == null ? void 0 : res.data) && Array.isArray(res.data) && res.data.length === texts.length) {
|
|
5152
|
+
return res.data.map((d) => d.embedding);
|
|
5153
|
+
}
|
|
5154
|
+
} catch (e) {
|
|
5155
|
+
}
|
|
5156
|
+
}
|
|
5157
|
+
const path2 = (_a2 = this.opts.embedPath) != null ? _a2 : "/embeddings";
|
|
5158
|
+
try {
|
|
5159
|
+
const { data } = await this.http.post(path2, { input: texts, model: this.model });
|
|
5160
|
+
if ((data == null ? void 0 : data.data) && Array.isArray(data.data) && data.data.length === texts.length) {
|
|
5161
|
+
return data.data.map((d) => d.embedding);
|
|
5056
5162
|
}
|
|
5163
|
+
} catch (e) {
|
|
5164
|
+
}
|
|
5165
|
+
const results = [];
|
|
5166
|
+
for (const text of texts) {
|
|
5167
|
+
const vector = await this.embed(text);
|
|
5168
|
+
results.push(vector);
|
|
5057
5169
|
}
|
|
5058
5170
|
return results;
|
|
5059
5171
|
}
|
|
@@ -5587,9 +5699,6 @@ var ConfigValidator = class {
|
|
|
5587
5699
|
}
|
|
5588
5700
|
};
|
|
5589
5701
|
|
|
5590
|
-
// src/core/Pipeline.ts
|
|
5591
|
-
init_version();
|
|
5592
|
-
|
|
5593
5702
|
// src/rag/DocumentChunker.ts
|
|
5594
5703
|
var DocumentChunker = class {
|
|
5595
5704
|
constructor(chunkSize = 1e3, chunkOverlap = 200, separators = ["\n# ", "\n## ", "\n### ", "\n#### ", "\n\n", "\n", " ", ""]) {
|
|
@@ -6532,6 +6641,73 @@ ${error instanceof Error ? error.message : String(error)}`
|
|
|
6532
6641
|
}
|
|
6533
6642
|
return String((_c = (_b = response == null ? void 0 : response.output) != null ? _b : response) != null ? _c : "");
|
|
6534
6643
|
}
|
|
6644
|
+
/**
|
|
6645
|
+
* Run the agentic flow in streaming mode, emitting granular SSE event frames.
|
|
6646
|
+
* Leverages LangChain/LangGraph `streamEvents` (v2 API) to stream:
|
|
6647
|
+
* - Token-by-token LLM output (`type: "text"`)
|
|
6648
|
+
* - Tool invocation / thinking steps (`type: "thinking"`)
|
|
6649
|
+
* - Tool execution metadata & search sources
|
|
6650
|
+
*/
|
|
6651
|
+
streamRun(_0) {
|
|
6652
|
+
return __asyncGenerator(this, arguments, function* (input, chatHistory = []) {
|
|
6653
|
+
var _a2;
|
|
6654
|
+
if (!this.agent) {
|
|
6655
|
+
throw new Error("[LangChainAgent] Agent not initialized. Call initialize() first.");
|
|
6656
|
+
}
|
|
6657
|
+
const { HumanMessage, AIMessage } = yield new __await(import("@langchain/core/messages"));
|
|
6658
|
+
const historyMessages = chatHistory.map(
|
|
6659
|
+
(m) => m.role === "user" ? new HumanMessage(m.content) : new AIMessage(m.content)
|
|
6660
|
+
);
|
|
6661
|
+
const inputMessages = [...historyMessages, new HumanMessage(input)];
|
|
6662
|
+
if (typeof this.agent.streamEvents === "function") {
|
|
6663
|
+
const eventStream = this.agent.streamEvents(
|
|
6664
|
+
{ messages: inputMessages },
|
|
6665
|
+
{ version: "v2" }
|
|
6666
|
+
);
|
|
6667
|
+
try {
|
|
6668
|
+
for (var iter = __forAwait(eventStream), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
|
|
6669
|
+
const event = temp.value;
|
|
6670
|
+
if (event.event === "on_tool_start") {
|
|
6671
|
+
const toolName = event.name || "document_search";
|
|
6672
|
+
yield {
|
|
6673
|
+
type: "thinking",
|
|
6674
|
+
text: `Analyzing repository with tool "${toolName}"...
|
|
6675
|
+
`
|
|
6676
|
+
};
|
|
6677
|
+
} else if (event.event === "on_tool_end") {
|
|
6678
|
+
const toolName = event.name || "document_search";
|
|
6679
|
+
yield {
|
|
6680
|
+
type: "thinking",
|
|
6681
|
+
text: `Tool "${toolName}" completed successfully.
|
|
6682
|
+
`
|
|
6683
|
+
};
|
|
6684
|
+
} else if (event.event === "on_chat_model_stream") {
|
|
6685
|
+
const chunk = (_a2 = event.data) == null ? void 0 : _a2.chunk;
|
|
6686
|
+
if (chunk == null ? void 0 : chunk.content) {
|
|
6687
|
+
const tokenText = typeof chunk.content === "string" ? chunk.content : JSON.stringify(chunk.content);
|
|
6688
|
+
yield {
|
|
6689
|
+
type: "text",
|
|
6690
|
+
text: tokenText
|
|
6691
|
+
};
|
|
6692
|
+
}
|
|
6693
|
+
}
|
|
6694
|
+
}
|
|
6695
|
+
} catch (temp) {
|
|
6696
|
+
error = [temp];
|
|
6697
|
+
} finally {
|
|
6698
|
+
try {
|
|
6699
|
+
more && (temp = iter.return) && (yield new __await(temp.call(iter)));
|
|
6700
|
+
} finally {
|
|
6701
|
+
if (error)
|
|
6702
|
+
throw error[0];
|
|
6703
|
+
}
|
|
6704
|
+
}
|
|
6705
|
+
} else {
|
|
6706
|
+
const result = yield new __await(this.run(input, chatHistory));
|
|
6707
|
+
yield { type: "text", text: result };
|
|
6708
|
+
}
|
|
6709
|
+
});
|
|
6710
|
+
}
|
|
6535
6711
|
};
|
|
6536
6712
|
|
|
6537
6713
|
// src/rag/PromptOptimizer.ts
|
|
@@ -7439,25 +7615,43 @@ ${errorMessages}`
|
|
|
7439
7615
|
* Processes up to `concurrency` items at the same time.
|
|
7440
7616
|
*/
|
|
7441
7617
|
static async processConcurrent(items, processor, concurrency = 5, options) {
|
|
7618
|
+
var _a2, _b, _c, _d;
|
|
7442
7619
|
const { throwOnPartialFailure = false } = options != null ? options : {};
|
|
7443
7620
|
const results = [];
|
|
7444
7621
|
const errors = [];
|
|
7622
|
+
const maxRetries = (_a2 = options == null ? void 0 : options.maxRetries) != null ? _a2 : 3;
|
|
7623
|
+
const initialDelayMs = (_b = options == null ? void 0 : options.initialDelayMs) != null ? _b : 100;
|
|
7624
|
+
const maxDelayMs = (_c = options == null ? void 0 : options.maxDelayMs) != null ? _c : 1e4;
|
|
7625
|
+
const backoffMultiplier = (_d = options == null ? void 0 : options.backoffMultiplier) != null ? _d : 2;
|
|
7445
7626
|
for (let i = 0; i < items.length; i += concurrency) {
|
|
7446
7627
|
const chunk = items.slice(i, i + concurrency).map((item, idx) => ({
|
|
7447
7628
|
item,
|
|
7448
7629
|
originalIndex: i + idx
|
|
7449
7630
|
}));
|
|
7450
7631
|
const chunkPromises = chunk.map(async ({ item, originalIndex }) => {
|
|
7451
|
-
|
|
7452
|
-
|
|
7453
|
-
|
|
7454
|
-
|
|
7455
|
-
|
|
7456
|
-
|
|
7457
|
-
|
|
7458
|
-
|
|
7459
|
-
|
|
7632
|
+
let lastErr;
|
|
7633
|
+
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
7634
|
+
try {
|
|
7635
|
+
const result = await _BatchProcessor.executeWithCircuitBreaker(
|
|
7636
|
+
() => processor(item),
|
|
7637
|
+
options == null ? void 0 : options.circuitBreaker
|
|
7638
|
+
);
|
|
7639
|
+
return { success: true, result, index: originalIndex };
|
|
7640
|
+
} catch (err) {
|
|
7641
|
+
lastErr = err instanceof Error ? err : new Error(String(err));
|
|
7642
|
+
if (!isTransientError(err) || attempt === maxRetries) {
|
|
7643
|
+
break;
|
|
7644
|
+
}
|
|
7645
|
+
const delay = calculateBackoffDelay(
|
|
7646
|
+
attempt,
|
|
7647
|
+
initialDelayMs,
|
|
7648
|
+
maxDelayMs,
|
|
7649
|
+
backoffMultiplier
|
|
7650
|
+
);
|
|
7651
|
+
await sleep(delay);
|
|
7652
|
+
}
|
|
7460
7653
|
}
|
|
7654
|
+
return { success: false, error: lastErr || new Error("Unknown error"), index: originalIndex };
|
|
7461
7655
|
});
|
|
7462
7656
|
const chunkResults = await Promise.all(chunkPromises);
|
|
7463
7657
|
for (const { success, result, error, index } of chunkResults) {
|
|
@@ -7930,14 +8124,14 @@ var IntentClassifier = class {
|
|
|
7930
8124
|
static classify(context) {
|
|
7931
8125
|
const q = (context.userQuery || "").toLowerCase().trim();
|
|
7932
8126
|
const signals = this.extractDataSignals(context);
|
|
7933
|
-
if (this.
|
|
7934
|
-
return { intent: "
|
|
8127
|
+
if (this.isProductQuery(q) || signals.isProductLike && (this.isRecommendationQuery(q) || this.isDiscoveryQuery(q))) {
|
|
8128
|
+
return { intent: "product_search", signals };
|
|
7935
8129
|
}
|
|
7936
8130
|
if (this.isComparisonQuery(q)) {
|
|
7937
8131
|
return { intent: "comparison", signals };
|
|
7938
8132
|
}
|
|
7939
|
-
if (this.
|
|
7940
|
-
return { intent: "
|
|
8133
|
+
if (this.isInformationLookup(q)) {
|
|
8134
|
+
return { intent: "information_lookup", signals };
|
|
7941
8135
|
}
|
|
7942
8136
|
if (this.isTrendQuery(q) || signals.hasDateFields && signals.hasNumericFields && /\b(growth|trend|over time|monthly|yearly|weekly)\b/.test(q)) {
|
|
7943
8137
|
return { intent: "trend_analysis", signals };
|
|
@@ -7954,7 +8148,8 @@ var IntentClassifier = class {
|
|
|
7954
8148
|
return { intent: "information_lookup", signals };
|
|
7955
8149
|
}
|
|
7956
8150
|
/**
|
|
7957
|
-
* Extract key data structure signals from retrieved documents.
|
|
8151
|
+
* Extract key data structure signals dynamically from retrieved documents.
|
|
8152
|
+
* Inspects both structured metadata and parsed chunk text.
|
|
7958
8153
|
*/
|
|
7959
8154
|
static extractDataSignals(context) {
|
|
7960
8155
|
const docs = context.retrievedDocuments || [];
|
|
@@ -7987,18 +8182,21 @@ var IntentClassifier = class {
|
|
|
7987
8182
|
let categoricalFieldCount = 0;
|
|
7988
8183
|
if (docs.length > 0) {
|
|
7989
8184
|
const keys = /* @__PURE__ */ new Set();
|
|
7990
|
-
|
|
7991
|
-
|
|
7992
|
-
|
|
7993
|
-
let
|
|
8185
|
+
const numericKeys = /* @__PURE__ */ new Set();
|
|
8186
|
+
const dateKeys = /* @__PURE__ */ new Set();
|
|
8187
|
+
const catKeys = /* @__PURE__ */ new Set();
|
|
8188
|
+
let productDocCount = 0;
|
|
7994
8189
|
docs.forEach((doc) => {
|
|
8190
|
+
if (!doc) return;
|
|
7995
8191
|
const meta = doc.metadata || {};
|
|
8192
|
+
const content = doc.content || "";
|
|
8193
|
+
let hasDocProductSignal = false;
|
|
7996
8194
|
Object.entries(meta).forEach(([key, val]) => {
|
|
7997
8195
|
if (IGNORED_SYSTEM_KEYS.has(key.toLowerCase())) return;
|
|
7998
8196
|
keys.add(key);
|
|
7999
8197
|
const kLower = key.toLowerCase();
|
|
8000
|
-
if (["price", "image", "brand", "in_stock", "rating", "sku", "product"].some((p) => kLower.includes(p))) {
|
|
8001
|
-
|
|
8198
|
+
if (["price", "image", "brand", "in_stock", "rating", "sku", "product", "item", "cost", "msrp", "category", "vendor", "inventory", "stock"].some((p) => kLower.includes(p))) {
|
|
8199
|
+
hasDocProductSignal = true;
|
|
8002
8200
|
}
|
|
8003
8201
|
if (typeof val === "number") {
|
|
8004
8202
|
numericKeys.add(key);
|
|
@@ -8010,13 +8208,23 @@ var IntentClassifier = class {
|
|
|
8010
8208
|
}
|
|
8011
8209
|
}
|
|
8012
8210
|
});
|
|
8211
|
+
if (!hasDocProductSignal && content) {
|
|
8212
|
+
const hasContentName = /(?:^|\n)\s*(?:product\s*name|item\s*name|product|item|title|name)\s*[:\-–]\s*[^\n,]+/i.test(content);
|
|
8213
|
+
const hasContentAttr = /(?:^|\n)\s*(?:price|cost|msrp|brand|vendor|sku|category|stock|inventory|rating|image)\s*[:\-–]/i.test(content) || /\$\s*[\d,.]+/.test(content);
|
|
8214
|
+
if (hasContentName && hasContentAttr) {
|
|
8215
|
+
hasDocProductSignal = true;
|
|
8216
|
+
}
|
|
8217
|
+
}
|
|
8218
|
+
if (hasDocProductSignal) {
|
|
8219
|
+
productDocCount++;
|
|
8220
|
+
}
|
|
8013
8221
|
});
|
|
8014
8222
|
hasNumericFields = numericKeys.size > 0;
|
|
8015
8223
|
hasDateFields = dateKeys.size > 0;
|
|
8016
8224
|
hasCategoricalFields = catKeys.size > 0;
|
|
8017
8225
|
numericFieldCount = numericKeys.size;
|
|
8018
8226
|
categoricalFieldCount = catKeys.size;
|
|
8019
|
-
if (
|
|
8227
|
+
if (productDocCount > 0 && (productDocCount / docs.length >= 0.3 || productDocCount >= 2)) {
|
|
8020
8228
|
isProductLike = true;
|
|
8021
8229
|
}
|
|
8022
8230
|
}
|
|
@@ -8031,8 +8239,15 @@ var IntentClassifier = class {
|
|
|
8031
8239
|
};
|
|
8032
8240
|
}
|
|
8033
8241
|
// ─── Intent Heuristic Checkers ─────────────────────────────────────────────
|
|
8242
|
+
static isDiscoveryQuery(query) {
|
|
8243
|
+
const q = query.toLowerCase().trim();
|
|
8244
|
+
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);
|
|
8245
|
+
}
|
|
8034
8246
|
static isInformationLookup(query) {
|
|
8035
8247
|
const q = query.toLowerCase().trim();
|
|
8248
|
+
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)) {
|
|
8249
|
+
return false;
|
|
8250
|
+
}
|
|
8036
8251
|
if (/^(what|who|explain|explore|define|where|when|why|how|how does|can|could|does|do|is|are|has|have|will|should|would|meaning of|tell me about|show details of|overview of|summarize)\s+/i.test(q)) {
|
|
8037
8252
|
if (!/\b(chart|graph|plot|visualize|pie chart|bar chart|line chart|scatter plot|draw a)\b/i.test(q)) {
|
|
8038
8253
|
return true;
|
|
@@ -8047,7 +8262,11 @@ var IntentClassifier = class {
|
|
|
8047
8262
|
return /\b(compare|comparison|vs\.?|versus|difference between|differ|contrast|against)\b/i.test(query);
|
|
8048
8263
|
}
|
|
8049
8264
|
static isProductQuery(query) {
|
|
8050
|
-
|
|
8265
|
+
const q = query.toLowerCase().trim();
|
|
8266
|
+
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)) {
|
|
8267
|
+
return false;
|
|
8268
|
+
}
|
|
8269
|
+
return /\b(best|recommend|top|buy|shopping|shop|under \$|cheap|affordable|products?|items?|catalog|inventory|in stock|show available|what do you have|options for)\b/i.test(q) && !/\b(compare|chart|sales|revenue|trend)\b/i.test(q);
|
|
8051
8270
|
}
|
|
8052
8271
|
static isRecommendationQuery(query) {
|
|
8053
8272
|
return /\b(recommend|suggest|what should i (buy|get|choose)|best options|top picks)\b/i.test(query);
|
|
@@ -8112,14 +8331,23 @@ var Rule3ProductDiscoveryRule = class {
|
|
|
8112
8331
|
}
|
|
8113
8332
|
evaluate(context, intent) {
|
|
8114
8333
|
const q = (context.userQuery || "").toLowerCase().trim();
|
|
8115
|
-
const
|
|
8116
|
-
|
|
8117
|
-
|
|
8118
|
-
|
|
8334
|
+
const isExplicitChart = /\b(chart|graph|plot|sales over time|monthly growth|revenue trend|pie chart|bar chart|line chart|histogram)\b/i.test(q);
|
|
8335
|
+
if (isExplicitChart) {
|
|
8336
|
+
return null;
|
|
8337
|
+
}
|
|
8338
|
+
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);
|
|
8339
|
+
if (isFinancialReport) {
|
|
8340
|
+
return null;
|
|
8341
|
+
}
|
|
8342
|
+
const signals = IntentClassifier.extractDataSignals(context);
|
|
8343
|
+
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);
|
|
8344
|
+
const isAnalyticalRanking = /\b(top performing|top selling|best performing|performing|sales revenue|revenue ranking)\b/i.test(q);
|
|
8345
|
+
const isGenericDocumentQuery = /\b(documents? in system|all documents|system documents|overall documents)\b/i.test(q);
|
|
8346
|
+
if (!isAnalyticalRanking && !isGenericDocumentQuery && (signals.isProductLike && hasDiscoveryAction || intent === "product_search" && !isAnalyticalRanking)) {
|
|
8119
8347
|
return {
|
|
8120
8348
|
renderType: "carousel",
|
|
8121
|
-
confidence: 0.
|
|
8122
|
-
reason: "Rule 3:
|
|
8349
|
+
confidence: 0.92,
|
|
8350
|
+
reason: "Rule 3: Dynamically identified product catalog entities and discovery intent."
|
|
8123
8351
|
};
|
|
8124
8352
|
}
|
|
8125
8353
|
return null;
|
|
@@ -8590,11 +8818,11 @@ var UITransformer = class _UITransformer {
|
|
|
8590
8818
|
if (["tabular", "table"].includes(resolvedIntent.visualizationHint) || resolvedIntent.wantsExplicitTable) {
|
|
8591
8819
|
return this.hasMultipleFields(filteredData) ? this.transformToTable(filteredData, userQuery) : this.transformToText(filteredData);
|
|
8592
8820
|
}
|
|
8593
|
-
if (
|
|
8594
|
-
|
|
8595
|
-
|
|
8821
|
+
if (hasProducts || this.isProductQuery(userQuery) || resolvedIntent.visualizationHint === "product_browse") {
|
|
8822
|
+
const carousel = this.transformToProductCarousel(filteredData, config, trainedSchema);
|
|
8823
|
+
if (Array.isArray(carousel.data) && carousel.data.length > 0) {
|
|
8824
|
+
return carousel;
|
|
8596
8825
|
}
|
|
8597
|
-
return this.transformToProductCarousel(filteredData, config, trainedSchema);
|
|
8598
8826
|
}
|
|
8599
8827
|
const automatic = this.chooseAutomaticVisualization(filteredData, profile, userQuery);
|
|
8600
8828
|
if (automatic) return automatic;
|
|
@@ -9231,15 +9459,17 @@ ${schemaProfileText}` : ""}`;
|
|
|
9231
9459
|
return _UITransformer._productQueryTest(query);
|
|
9232
9460
|
}
|
|
9233
9461
|
static _productQueryTest(query) {
|
|
9234
|
-
const q = query.toLowerCase();
|
|
9462
|
+
const q = query.toLowerCase().trim();
|
|
9235
9463
|
const financialTerms = /\b(price per share|share price|total cost of the deal|cost of the deal|debt|equity|ratio|revenue|ebit|ebitda|tax|depreciation|interest rate|cashflow|cash flow|fcfe|fcff|wacc|beta|working capital|cogs|capital spending|valuation|leverage)\b/;
|
|
9236
9464
|
if (financialTerms.test(q)) {
|
|
9237
9465
|
return false;
|
|
9238
9466
|
}
|
|
9239
|
-
const productTerms = /\b(product|products|item|items|sku|catalog|catalogue|brand|model|stock|inventory|in stock|out of stock|buy|shop)\b/.test(q);
|
|
9240
|
-
const productAction = /\b(recommend|suggest|describe|description|detail|details|about|show|find|search|browse|list|explore|exploring)\b/.test(q);
|
|
9241
9467
|
const nonProductEntityTerms = /\b(organization|organisations?|organizations?|company|companies|employee|employees|staff|headcount|workforce)\b/.test(q);
|
|
9242
|
-
|
|
9468
|
+
if (nonProductEntityTerms) {
|
|
9469
|
+
return false;
|
|
9470
|
+
}
|
|
9471
|
+
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;
|
|
9472
|
+
return genericDiscoveryPatterns.test(q);
|
|
9243
9473
|
}
|
|
9244
9474
|
static isProductData(item) {
|
|
9245
9475
|
if (!item) return false;
|
|
@@ -9256,10 +9486,17 @@ ${schemaProfileText}` : ""}`;
|
|
|
9256
9486
|
if (financialTerms.test(content)) {
|
|
9257
9487
|
return false;
|
|
9258
9488
|
}
|
|
9259
|
-
const hasSku = Boolean(resolveMetadataValue(metadata, "sku") || resolveMetadataValue(metadata, "product_id"));
|
|
9260
|
-
const hasProductIdentity = Boolean(resolveMetadataValue(metadata, "product_name") || resolveMetadataValue(metadata, "brand") && resolveMetadataValue(metadata, "name"));
|
|
9261
|
-
const hasPrice = Boolean(resolveMetadataValue(metadata, "retail_price") || resolveMetadataValue(metadata, "unit_price") || resolveMetadataValue(metadata, "msrp"));
|
|
9262
|
-
|
|
9489
|
+
const hasSku = Boolean(resolveMetadataValue(metadata, "sku") || resolveMetadataValue(metadata, "product_id") || resolveMetadataValue(metadata, "item_id"));
|
|
9490
|
+
const hasProductIdentity = Boolean(resolveMetadataValue(metadata, "product_name") || resolveMetadataValue(metadata, "item_name") || resolveMetadataValue(metadata, "brand") && resolveMetadataValue(metadata, "name"));
|
|
9491
|
+
const hasPrice = Boolean(resolveMetadataValue(metadata, "price") || resolveMetadataValue(metadata, "retail_price") || resolveMetadataValue(metadata, "unit_price") || resolveMetadataValue(metadata, "msrp"));
|
|
9492
|
+
if (hasSku || hasProductIdentity && hasPrice || hasProductIdentity) {
|
|
9493
|
+
return true;
|
|
9494
|
+
}
|
|
9495
|
+
const hasContentName = /(?:^|\n)\s*(?:product\s*name|item\s*name|product|item|title|name)\s*[:\-–]\s*[^\n,]+/i.test(item.content || "");
|
|
9496
|
+
const hasContentPrice = /(?:^|\n)\s*(?:price|retail\s*price|unit\s*price|cost|msrp)\s*[:\-–]?\s*\$?\s*[\d,.]+/i.test(item.content || "");
|
|
9497
|
+
const hasContentCategory = /(?:^|\n)\s*(?:category|department|collection)\s*[:\-–]\s*[^\n,]+/i.test(item.content || "");
|
|
9498
|
+
const hasContentBrand = /(?:^|\n)\s*(?:brand|manufacturer|vendor)\s*[:\-–]\s*[^\n,]+/i.test(item.content || "");
|
|
9499
|
+
return hasContentName && (hasContentPrice || hasContentCategory || hasContentBrand);
|
|
9263
9500
|
}
|
|
9264
9501
|
static isTimeSeriesData(item) {
|
|
9265
9502
|
const metadata = item.metadata || {};
|
|
@@ -9749,64 +9986,46 @@ ${schemaProfileText}` : ""}`;
|
|
|
9749
9986
|
return resolveMetadataValue(meta, uiKey);
|
|
9750
9987
|
}
|
|
9751
9988
|
static extractProductInfo(item, config, trainedSchema) {
|
|
9752
|
-
var _a2, _b;
|
|
9989
|
+
var _a2, _b, _c;
|
|
9753
9990
|
if (!item) return null;
|
|
9754
9991
|
const meta = item.metadata || {};
|
|
9755
9992
|
const content = item.content || "";
|
|
9756
|
-
|
|
9757
|
-
|
|
9758
|
-
|
|
9993
|
+
let name = this.getDynamicVal(meta, "name", config, trainedSchema);
|
|
9994
|
+
let price = this.getDynamicVal(meta, "price", config, trainedSchema);
|
|
9995
|
+
let brand = this.getDynamicVal(meta, "brand", config, trainedSchema);
|
|
9759
9996
|
const description = this.cleanProductDescription(
|
|
9760
9997
|
(_a2 = this.extractProductDescriptionFromContent(content)) != null ? _a2 : this.getProductDescriptionValue(meta, config, trainedSchema)
|
|
9761
9998
|
);
|
|
9762
|
-
if (name
|
|
9763
|
-
|
|
9764
|
-
if (
|
|
9765
|
-
|
|
9766
|
-
finalName = nameMatch ? nameMatch[1].trim() : (_b = content.split("\n")[0]) == null ? void 0 : _b.substring(0, 60);
|
|
9767
|
-
}
|
|
9768
|
-
if (!finalName) {
|
|
9769
|
-
console.warn(
|
|
9770
|
-
`[UITransformer] Unable to read product name from metadata or content for item ${item.id}. Defaulting to 'Unnamed Product'.`,
|
|
9771
|
-
{ metadataKeys: Object.keys(meta), contentLength: content.length }
|
|
9772
|
-
);
|
|
9999
|
+
if (!name && content) {
|
|
10000
|
+
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);
|
|
10001
|
+
if (nameMatch && nameMatch[1]) {
|
|
10002
|
+
name = nameMatch[1].trim();
|
|
9773
10003
|
}
|
|
9774
|
-
|
|
9775
|
-
|
|
9776
|
-
|
|
9777
|
-
|
|
9778
|
-
|
|
9779
|
-
if (finalPrice === void 0) {
|
|
9780
|
-
console.warn(
|
|
9781
|
-
`[UITransformer] Unable to read product price from metadata or content for item ${item.id}.`,
|
|
9782
|
-
{ metadataKeys: Object.keys(meta), contentSnippet: content.substring(0, 100) }
|
|
9783
|
-
);
|
|
9784
|
-
}
|
|
9785
|
-
const imageValue = this.getDynamicVal(meta, "image", config, trainedSchema);
|
|
9786
|
-
if (!imageValue) {
|
|
9787
|
-
console.debug(
|
|
9788
|
-
`[UITransformer] Unable to read product image URL from metadata for item ${item.id}.`
|
|
9789
|
-
);
|
|
10004
|
+
}
|
|
10005
|
+
if (!brand && content) {
|
|
10006
|
+
const brandMatch = content.match(/(?:^|\n)\s*(?:brand|manufacturer|vendor)\s*[:\-–]\s*([^\n,]+)/i);
|
|
10007
|
+
if (brandMatch && brandMatch[1]) {
|
|
10008
|
+
brand = brandMatch[1].trim();
|
|
9790
10009
|
}
|
|
9791
|
-
|
|
9792
|
-
|
|
9793
|
-
|
|
9794
|
-
|
|
10010
|
+
}
|
|
10011
|
+
if (price === void 0 && content) {
|
|
10012
|
+
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,.]+)/);
|
|
10013
|
+
if (priceMatch && priceMatch[1]) {
|
|
10014
|
+
price = priceMatch[1].replace(/,/g, "");
|
|
9795
10015
|
}
|
|
10016
|
+
}
|
|
10017
|
+
if (name || this.isProductData(item)) {
|
|
10018
|
+
const finalName = name ? String(name) : ((_b = content.split("\n")[0]) == null ? void 0 : _b.substring(0, 60)) || "Unnamed Product";
|
|
10019
|
+
const imageValue = this.getDynamicVal(meta, "image", config, trainedSchema) || ((_c = content.match(/(?:^|\n)\s*(?:image|image\s*url|photo|thumbnail)\s*[:\-–]\s*(https?:\/\/[^\s\n,]+)/i)) == null ? void 0 : _c[1]);
|
|
9796
10020
|
return {
|
|
9797
10021
|
id: item.id,
|
|
9798
|
-
name: finalName
|
|
9799
|
-
price:
|
|
10022
|
+
name: finalName,
|
|
10023
|
+
price: typeof price === "number" || typeof price === "string" ? price : void 0,
|
|
9800
10024
|
image: typeof imageValue === "string" ? imageValue : void 0,
|
|
9801
10025
|
brand: brand ? String(brand) : void 0,
|
|
9802
10026
|
description,
|
|
9803
10027
|
inStock: this.determineStockStatus(item)
|
|
9804
10028
|
};
|
|
9805
|
-
} else {
|
|
9806
|
-
console.warn(
|
|
9807
|
-
`[UITransformer] Item ${item.id} skipped: unable to read name or identify as product data from metadata/content.`,
|
|
9808
|
-
{ metadataKeys: Object.keys(meta), contentSnippet: content.substring(0, 100) }
|
|
9809
|
-
);
|
|
9810
10029
|
}
|
|
9811
10030
|
return null;
|
|
9812
10031
|
}
|
|
@@ -10181,6 +10400,17 @@ var Pipeline = class {
|
|
|
10181
10400
|
- If answer cannot be found in the sources, say that you cannot answer the question and do not hallucinate.
|
|
10182
10401
|
- Do not use information from previous turns to answer the question.
|
|
10183
10402
|
- 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.
|
|
10403
|
+
- Be concise, direct, and focused. Deliver clean, high-quality answers within token limits without unnecessary filler.
|
|
10404
|
+
|
|
10405
|
+
### CATEGORY & DOMAIN SYNONYM MATCHING:
|
|
10406
|
+
- Intelligently understand common category synonyms and related terms from the context:
|
|
10407
|
+
- 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.
|
|
10408
|
+
- If the user asks about "clothes", "apparel", "wear", "shirts", "garments", or "fashion", map these to the "Clothing" category in the catalog.
|
|
10409
|
+
- If the user asks about "electronics", "gadgets", "tech", "devices", or "appliances", map these to the "Electronics" category.
|
|
10410
|
+
- If the user asks about "food", "snacks", "groceries", "beverages", or "drinks", map these to the "Grocery" category.
|
|
10411
|
+
- If the user asks about "office items", "stationery", "work supplies", or "desk equipment", map these to the "Office Supplies" category.
|
|
10412
|
+
- If the user asks about "toys", "games", "play items", or "hobbies", map these to the "Toys & Games" category.
|
|
10413
|
+
- Do NOT reject a query or say no items exist simply because the user used a natural synonym (e.g., "makeup") instead of the exact catalog column label (e.g., "Beauty"). Identify the relevant items from the context and present them.
|
|
10184
10414
|
|
|
10185
10415
|
### PRODUCT DISPLAY:
|
|
10186
10416
|
- Recommended Products should only be displayed when the user explicitly asks for products.
|
|
@@ -10350,13 +10580,23 @@ ${m.content}`).join("\n\n---\n\n");
|
|
|
10350
10580
|
const texts = chunks.map((c) => PIIMasker.mask(c.content));
|
|
10351
10581
|
if (typeof this.embeddingProvider.batchEmbed === "function") {
|
|
10352
10582
|
try {
|
|
10353
|
-
const batchSize =
|
|
10354
|
-
const
|
|
10583
|
+
const batchSize = 100;
|
|
10584
|
+
const slices = [];
|
|
10355
10585
|
for (let i = 0; i < texts.length; i += batchSize) {
|
|
10356
|
-
|
|
10357
|
-
const batchVectors = await this.embeddingProvider.batchEmbed(slice, { taskType: "document" });
|
|
10358
|
-
allVectors.push(...batchVectors);
|
|
10586
|
+
slices.push(texts.slice(i, i + batchSize));
|
|
10359
10587
|
}
|
|
10588
|
+
const batchEmbedFn = this.embeddingProvider.batchEmbed.bind(this.embeddingProvider);
|
|
10589
|
+
const concurrentBatches = await BatchProcessor.processConcurrent(
|
|
10590
|
+
slices,
|
|
10591
|
+
(slice) => batchEmbedFn(slice, { taskType: "document" }),
|
|
10592
|
+
5,
|
|
10593
|
+
{
|
|
10594
|
+
maxRetries: 3,
|
|
10595
|
+
initialDelayMs: 100,
|
|
10596
|
+
throwOnPartialFailure: false
|
|
10597
|
+
}
|
|
10598
|
+
);
|
|
10599
|
+
const allVectors = concurrentBatches.results.flat();
|
|
10360
10600
|
if (allVectors.length === chunks.length) {
|
|
10361
10601
|
return allVectors;
|
|
10362
10602
|
}
|
|
@@ -10369,7 +10609,7 @@ ${m.content}`).join("\n\n---\n\n");
|
|
|
10369
10609
|
const concurrentResult = await BatchProcessor.processConcurrent(
|
|
10370
10610
|
texts,
|
|
10371
10611
|
(text) => this.embeddingProvider.embed(text, { taskType: "document" }),
|
|
10372
|
-
|
|
10612
|
+
20,
|
|
10373
10613
|
{
|
|
10374
10614
|
maxRetries: 3,
|
|
10375
10615
|
initialDelayMs: 100,
|
|
@@ -10387,7 +10627,7 @@ ${m.content}`).join("\n\n---\n\n");
|
|
|
10387
10627
|
/** Step 3: Upsert chunks to vector database with retry logic. */
|
|
10388
10628
|
async processUpserts(upsertDocs, namespace) {
|
|
10389
10629
|
const upsertBatchOptions = {
|
|
10390
|
-
batchSize:
|
|
10630
|
+
batchSize: 250,
|
|
10391
10631
|
maxRetries: 3,
|
|
10392
10632
|
initialDelayMs: 100
|
|
10393
10633
|
};
|
|
@@ -10545,7 +10785,7 @@ ${m.content}`).join("\n\n---\n\n");
|
|
|
10545
10785
|
*/
|
|
10546
10786
|
askStreamInternal(_0) {
|
|
10547
10787
|
return __asyncGenerator(this, arguments, function* (question, history = [], namespace) {
|
|
10548
|
-
var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x
|
|
10788
|
+
var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x;
|
|
10549
10789
|
yield new __await(this.initialize());
|
|
10550
10790
|
const ns = formatNamespace(namespace != null ? namespace : this.config.projectId);
|
|
10551
10791
|
const topK = (_b = (_a2 = this.config.rag) == null ? void 0 : _a2.topK) != null ? _b : 5;
|
|
@@ -10616,7 +10856,17 @@ ${m.content}`).join("\n\n---\n\n");
|
|
|
10616
10856
|
}
|
|
10617
10857
|
const rerankMs = performance.now() - rerankStart;
|
|
10618
10858
|
const totalCount = fullSources.length;
|
|
10619
|
-
const
|
|
10859
|
+
const rawPromptSources = fullSources.slice(0, 4);
|
|
10860
|
+
let accumulatedChars = 0;
|
|
10861
|
+
const promptSources = [];
|
|
10862
|
+
for (const s of rawPromptSources) {
|
|
10863
|
+
const textLen = ((s == null ? void 0 : s.content) || "").length;
|
|
10864
|
+
if (accumulatedChars + textLen > 3500 && promptSources.length >= 2) {
|
|
10865
|
+
break;
|
|
10866
|
+
}
|
|
10867
|
+
promptSources.push(s);
|
|
10868
|
+
accumulatedChars += textLen;
|
|
10869
|
+
}
|
|
10620
10870
|
let context = promptSources.length ? `[SEARCH SUMMARY: Found ${totalCount} matching record(s) out of ${rawSources.length} total resources searched in database. Context includes top ${promptSources.length} relevant records]
|
|
10621
10871
|
|
|
10622
10872
|
` + promptSources.map((m, i) => {
|
|
@@ -10694,7 +10944,7 @@ ${context}`;
|
|
|
10694
10944
|
const isNativeThinking = isClaude37 && (((_q = this.config.llm.options) == null ? void 0 : _q.thinking) === true || ((_r = this.config.llm.options) == null ? void 0 : _r.thinking) === "enabled" || ((_s = this.config.llm.options) == null ? void 0 : _s.thinking) !== false);
|
|
10695
10945
|
const modelNameLower = this.config.llm.model.toLowerCase();
|
|
10696
10946
|
const isReasoningModel = modelNameLower.includes("-r1") || modelNameLower.includes("deepseek-r1") || modelNameLower.includes("reasoning") || modelNameLower.includes("thinking");
|
|
10697
|
-
const isSimulatedThinking = !isNativeThinking && (((_t = this.config.llm.options) == null ? void 0 : _t.thinking) === true || ((_u = this.config.llm.options) == null ? void 0 : _u.thinking) === "enabled"
|
|
10947
|
+
const isSimulatedThinking = !isNativeThinking && (((_t = this.config.llm.options) == null ? void 0 : _t.thinking) === true || ((_u = this.config.llm.options) == null ? void 0 : _u.thinking) === "enabled");
|
|
10698
10948
|
let finalRestrictionSuffix = restrictionSuffix;
|
|
10699
10949
|
if (isSimulatedThinking) {
|
|
10700
10950
|
finalRestrictionSuffix += "\n\n(IMPORTANT: You must think step-by-step before answering. Write your thinking process inside a `<think>...</think>` block. Write the `<think>` block first, then follow it with your final response. Keep the thinking process thorough and detailed. Example: `<think>Thinking details here...</think>Final answer text here.`)";
|
|
@@ -10705,7 +10955,7 @@ ${context}`;
|
|
|
10705
10955
|
}));
|
|
10706
10956
|
const userQuestion = { role: "user", content: effectiveQuestion + finalRestrictionSuffix };
|
|
10707
10957
|
const messages = [...hardenedHistory, userQuestion];
|
|
10708
|
-
const systemPrompt = (
|
|
10958
|
+
const systemPrompt = (_v = this.config.llm.systemPrompt) != null ? _v : "";
|
|
10709
10959
|
const userPrompt = messages.map((m) => `${m.role}: ${m.content}`).join("\n");
|
|
10710
10960
|
let fullReply = "";
|
|
10711
10961
|
let thinkingText = "";
|
|
@@ -10727,57 +10977,54 @@ ${context}`;
|
|
|
10727
10977
|
yield { type: "thinking", text: thinkingDelta };
|
|
10728
10978
|
continue;
|
|
10729
10979
|
}
|
|
10730
|
-
|
|
10731
|
-
|
|
10732
|
-
|
|
10733
|
-
|
|
10734
|
-
|
|
10735
|
-
|
|
10736
|
-
|
|
10737
|
-
|
|
10738
|
-
|
|
10739
|
-
yield before;
|
|
10740
|
-
}
|
|
10741
|
-
inThinking = true;
|
|
10742
|
-
thinkingStartMs = performance.now();
|
|
10743
|
-
textBuffer = textBuffer.slice(thinkIndex + "<think>".length);
|
|
10744
|
-
} else {
|
|
10745
|
-
const maxSafeLength = textBuffer.length - "<think>".length;
|
|
10746
|
-
if (maxSafeLength > 0) {
|
|
10747
|
-
const safeText = textBuffer.slice(0, maxSafeLength);
|
|
10748
|
-
fullReply += safeText;
|
|
10749
|
-
yield safeText;
|
|
10750
|
-
textBuffer = textBuffer.slice(maxSafeLength);
|
|
10751
|
-
}
|
|
10752
|
-
break;
|
|
10980
|
+
textBuffer += chunk;
|
|
10981
|
+
while (textBuffer.length > 0) {
|
|
10982
|
+
if (!inThinking) {
|
|
10983
|
+
const thinkIndex = textBuffer.indexOf("<think>");
|
|
10984
|
+
if (thinkIndex !== -1) {
|
|
10985
|
+
const before = textBuffer.slice(0, thinkIndex);
|
|
10986
|
+
if (before) {
|
|
10987
|
+
fullReply += before;
|
|
10988
|
+
yield before;
|
|
10753
10989
|
}
|
|
10990
|
+
inThinking = true;
|
|
10991
|
+
thinkingStartMs = performance.now();
|
|
10992
|
+
textBuffer = textBuffer.slice(thinkIndex + "<think>".length);
|
|
10754
10993
|
} else {
|
|
10755
|
-
const
|
|
10756
|
-
|
|
10757
|
-
|
|
10758
|
-
|
|
10759
|
-
|
|
10760
|
-
|
|
10761
|
-
|
|
10762
|
-
}
|
|
10763
|
-
inThinking = false;
|
|
10764
|
-
textBuffer = textBuffer.slice(endThinkIndex + "</think>".length);
|
|
10765
|
-
} else {
|
|
10766
|
-
const maxSafeLength = textBuffer.length - "</think>".length;
|
|
10767
|
-
if (maxSafeLength > 0) {
|
|
10768
|
-
const thinkingDelta = textBuffer.slice(0, maxSafeLength);
|
|
10769
|
-
thinkingText += thinkingDelta;
|
|
10770
|
-
thinkingDurationMs = performance.now() - thinkingStartMs;
|
|
10771
|
-
yield { type: "thinking", text: thinkingDelta };
|
|
10772
|
-
textBuffer = textBuffer.slice(maxSafeLength);
|
|
10773
|
-
}
|
|
10774
|
-
break;
|
|
10994
|
+
const partialMatch = textBuffer.match(/<t?(?:h(?:i(?:n(?:k)?)?)?)?$/);
|
|
10995
|
+
const safeLen = partialMatch ? textBuffer.length - partialMatch[0].length : textBuffer.length;
|
|
10996
|
+
if (safeLen > 0) {
|
|
10997
|
+
const safeText = textBuffer.slice(0, safeLen);
|
|
10998
|
+
fullReply += safeText;
|
|
10999
|
+
yield safeText;
|
|
11000
|
+
textBuffer = textBuffer.slice(safeLen);
|
|
10775
11001
|
}
|
|
11002
|
+
break;
|
|
11003
|
+
}
|
|
11004
|
+
} else {
|
|
11005
|
+
const endThinkIndex = textBuffer.indexOf("</think>");
|
|
11006
|
+
if (endThinkIndex !== -1) {
|
|
11007
|
+
const thinkingDelta = textBuffer.slice(0, endThinkIndex);
|
|
11008
|
+
if (thinkingDelta) {
|
|
11009
|
+
thinkingText += thinkingDelta;
|
|
11010
|
+
thinkingDurationMs = performance.now() - thinkingStartMs;
|
|
11011
|
+
yield { type: "thinking", text: thinkingDelta };
|
|
11012
|
+
}
|
|
11013
|
+
inThinking = false;
|
|
11014
|
+
textBuffer = textBuffer.slice(endThinkIndex + "</think>".length);
|
|
11015
|
+
} else {
|
|
11016
|
+
const partialMatch = textBuffer.match(/<\/?t?(?:h(?:i(?:n(?:k)?)?)?)?$/);
|
|
11017
|
+
const safeLen = partialMatch ? textBuffer.length - partialMatch[0].length : textBuffer.length;
|
|
11018
|
+
if (safeLen > 0) {
|
|
11019
|
+
const thinkingDelta = textBuffer.slice(0, safeLen);
|
|
11020
|
+
thinkingText += thinkingDelta;
|
|
11021
|
+
thinkingDurationMs = performance.now() - thinkingStartMs;
|
|
11022
|
+
yield { type: "thinking", text: thinkingDelta };
|
|
11023
|
+
textBuffer = textBuffer.slice(safeLen);
|
|
11024
|
+
}
|
|
11025
|
+
break;
|
|
10776
11026
|
}
|
|
10777
11027
|
}
|
|
10778
|
-
} else {
|
|
10779
|
-
fullReply += chunk;
|
|
10780
|
-
yield chunk;
|
|
10781
11028
|
}
|
|
10782
11029
|
}
|
|
10783
11030
|
} catch (temp) {
|
|
@@ -10790,7 +11037,7 @@ ${context}`;
|
|
|
10790
11037
|
throw error[0];
|
|
10791
11038
|
}
|
|
10792
11039
|
}
|
|
10793
|
-
if (
|
|
11040
|
+
if (textBuffer.length > 0) {
|
|
10794
11041
|
if (inThinking) {
|
|
10795
11042
|
thinkingText += textBuffer;
|
|
10796
11043
|
yield { type: "thinking", text: textBuffer };
|
|
@@ -10801,22 +11048,21 @@ ${context}`;
|
|
|
10801
11048
|
}
|
|
10802
11049
|
} else {
|
|
10803
11050
|
const reply = yield new __await(this.llmProvider.chat(messages, context));
|
|
10804
|
-
|
|
10805
|
-
|
|
10806
|
-
|
|
10807
|
-
|
|
10808
|
-
|
|
10809
|
-
|
|
10810
|
-
|
|
10811
|
-
|
|
10812
|
-
|
|
10813
|
-
}
|
|
11051
|
+
const thinkStart = reply.indexOf("<think>");
|
|
11052
|
+
const thinkEnd = reply.indexOf("</think>");
|
|
11053
|
+
if (thinkStart !== -1 && thinkEnd !== -1 && thinkEnd > thinkStart) {
|
|
11054
|
+
thinkingText = reply.slice(thinkStart + "<think>".length, thinkEnd);
|
|
11055
|
+
fullReply = reply.slice(0, thinkStart) + reply.slice(thinkEnd + "</think>".length);
|
|
11056
|
+
thinkingDurationMs = 100;
|
|
11057
|
+
} else if (thinkStart !== -1) {
|
|
11058
|
+
thinkingText = reply.slice(thinkStart + "<think>".length);
|
|
11059
|
+
fullReply = reply.slice(0, thinkStart);
|
|
10814
11060
|
} else {
|
|
10815
11061
|
fullReply = reply;
|
|
10816
11062
|
}
|
|
10817
11063
|
yield fullReply;
|
|
10818
11064
|
}
|
|
10819
|
-
const runHallucination = ((
|
|
11065
|
+
const runHallucination = ((_w = this.config.llm.options) == null ? void 0 : _w.hallucinationScoring) === true;
|
|
10820
11066
|
if (runHallucination) {
|
|
10821
11067
|
hallucinationScoringPromise = scoreHallucination(this.llmProvider, fullReply, context).catch(() => void 0);
|
|
10822
11068
|
}
|
|
@@ -10838,96 +11084,45 @@ ${context}`;
|
|
|
10838
11084
|
totalTokens: promptTokens + completionTokens,
|
|
10839
11085
|
estimatedCostUsd: estimateCostUsd(promptTokens, completionTokens, this.config.llm.model)
|
|
10840
11086
|
};
|
|
10841
|
-
const awaitHallucination = ((
|
|
11087
|
+
const awaitHallucination = ((_x = this.config.llm.options) == null ? void 0 : _x.awaitHallucination) === true;
|
|
10842
11088
|
const [uiTransformation, hallucinationResult] = yield new __await(Promise.all([
|
|
10843
11089
|
uiTransformationPromise,
|
|
10844
11090
|
awaitHallucination ? hallucinationScoringPromise : Promise.resolve(void 0)
|
|
10845
11091
|
]));
|
|
10846
|
-
|
|
10847
|
-
|
|
10848
|
-
|
|
10849
|
-
|
|
10850
|
-
|
|
10851
|
-
|
|
10852
|
-
|
|
10853
|
-
|
|
10854
|
-
|
|
10855
|
-
|
|
10856
|
-
|
|
10857
|
-
|
|
10858
|
-
|
|
10859
|
-
|
|
10860
|
-
|
|
10861
|
-
|
|
10862
|
-
|
|
10863
|
-
|
|
10864
|
-
|
|
10865
|
-
|
|
10866
|
-
|
|
10867
|
-
|
|
10868
|
-
|
|
10869
|
-
|
|
10870
|
-
|
|
10871
|
-
|
|
10872
|
-
|
|
10873
|
-
|
|
10874
|
-
|
|
10875
|
-
|
|
11092
|
+
RAGEvaluator.evaluate(effectiveQuestion, context, fullReply).catch(() => {
|
|
11093
|
+
});
|
|
11094
|
+
const buildTrace = (hScore) => {
|
|
11095
|
+
var _a3, _b2;
|
|
11096
|
+
return __spreadValues({
|
|
11097
|
+
requestId,
|
|
11098
|
+
query: question,
|
|
11099
|
+
rewrittenQuery: rewrittenQuery || ((promptOptResult == null ? void 0 : promptOptResult.wasModified) ? promptOptResult.optimizedPrompt : void 0),
|
|
11100
|
+
// systemPrompt / userPrompt intentionally excluded:
|
|
11101
|
+
// they may contain business logic or post-masking user PII and were
|
|
11102
|
+
// being emitted to the client browser via SSE (security risk).
|
|
11103
|
+
chunks: (sources || []).filter(Boolean).map((s) => {
|
|
11104
|
+
var _a4, _b3;
|
|
11105
|
+
return {
|
|
11106
|
+
id: s.id,
|
|
11107
|
+
score: s.score,
|
|
11108
|
+
content: (_a4 = s == null ? void 0 : s.content) != null ? _a4 : "",
|
|
11109
|
+
metadata: (_b3 = s == null ? void 0 : s.metadata) != null ? _b3 : {},
|
|
11110
|
+
namespace: ns
|
|
11111
|
+
};
|
|
11112
|
+
}),
|
|
11113
|
+
latency,
|
|
11114
|
+
tokens,
|
|
11115
|
+
// Always populate model/provider on the trace (fixes gap where telemetry
|
|
11116
|
+
// was reading finalTrace?.model with a fallback — now it's always set).
|
|
11117
|
+
model: (_a3 = this.config.llm) == null ? void 0 : _a3.model,
|
|
11118
|
+
provider: (_b2 = this.config.llm) == null ? void 0 : _b2.provider,
|
|
11119
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
11120
|
+
}, hScore ? {
|
|
11121
|
+
hallucinationScore: hScore.score,
|
|
11122
|
+
hallucinationReason: hScore.reason
|
|
11123
|
+
} : {});
|
|
11124
|
+
};
|
|
10876
11125
|
const trace = buildTrace(hallucinationResult);
|
|
10877
|
-
const isTelemetryActive = (_A = (_z = this.config.telemetry) == null ? void 0 : _z.enabled) != null ? _A : Boolean(this.config.licenseKey);
|
|
10878
|
-
if (isTelemetryActive) {
|
|
10879
|
-
const defaultUrl = "https://www.retrivora.com/api/telemetry";
|
|
10880
|
-
const telemetryUrl = ((_B = this.config.telemetry) == null ? void 0 : _B.url) || process.env.TELEMETRY_URL || process.env.NEXT_PUBLIC_TELEMETRY_URL || defaultUrl;
|
|
10881
|
-
const absoluteUrl = telemetryUrl.startsWith("http") ? telemetryUrl : "https://www.retrivora.com" + (telemetryUrl.startsWith("/") ? telemetryUrl : "/" + telemetryUrl);
|
|
10882
|
-
(async () => {
|
|
10883
|
-
var _a3, _b2, _c2, _d2, _e2;
|
|
10884
|
-
try {
|
|
10885
|
-
let finalTrace = trace;
|
|
10886
|
-
if (!awaitHallucination && runHallucination) {
|
|
10887
|
-
const backgroundScoreResult = await hallucinationScoringPromise;
|
|
10888
|
-
if (backgroundScoreResult) {
|
|
10889
|
-
finalTrace = buildTrace(backgroundScoreResult);
|
|
10890
|
-
}
|
|
10891
|
-
}
|
|
10892
|
-
const modelName = (finalTrace == null ? void 0 : finalTrace.model) || ((_a3 = this.config.llm) == null ? void 0 : _a3.model) || "llama-3.1-8b-instant";
|
|
10893
|
-
const providerName = (finalTrace == null ? void 0 : finalTrace.provider) || ((_b2 = this.config.llm) == null ? void 0 : _b2.provider) || "groq";
|
|
10894
|
-
const tokenCount = Number(((_c2 = finalTrace == null ? void 0 : finalTrace.tokens) == null ? void 0 : _c2.totalTokens) || 0);
|
|
10895
|
-
const costEst = Number(((_d2 = finalTrace == null ? void 0 : finalTrace.tokens) == null ? void 0 : _d2.estimatedCostUsd) || 0);
|
|
10896
|
-
const latencyDuration = Number(((_e2 = finalTrace == null ? void 0 : finalTrace.latency) == null ? void 0 : _e2.totalMs) || 0);
|
|
10897
|
-
if (typeof window === "undefined") {
|
|
10898
|
-
Logger.info(`[Retrivora Pipeline Telemetry] \u{1F4CA} Dispatching RAG telemetry -> Project: "${ns}", Tokens: ${tokenCount}, Latency: ${latencyDuration}ms`);
|
|
10899
|
-
}
|
|
10900
|
-
await fetch(absoluteUrl, {
|
|
10901
|
-
method: "POST",
|
|
10902
|
-
headers: {
|
|
10903
|
-
"Content-Type": "application/json"
|
|
10904
|
-
},
|
|
10905
|
-
body: JSON.stringify({
|
|
10906
|
-
trace: finalTrace,
|
|
10907
|
-
licenseKey: this.config.licenseKey,
|
|
10908
|
-
projectId: ns,
|
|
10909
|
-
organization: process.env.RETRIVORA_ORGANIZATION || "default-org",
|
|
10910
|
-
sdkVersion: SDK_VERSION,
|
|
10911
|
-
model: modelName,
|
|
10912
|
-
provider: providerName,
|
|
10913
|
-
tokens: tokenCount,
|
|
10914
|
-
cost: costEst,
|
|
10915
|
-
costUsd: costEst,
|
|
10916
|
-
latencyMs: latencyDuration,
|
|
10917
|
-
status: "success",
|
|
10918
|
-
action: "RAG_QUERY",
|
|
10919
|
-
feature: "RAG",
|
|
10920
|
-
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
10921
|
-
details: `Org: default-org | Model: ${providerName}/${modelName} | Tokens: ${tokenCount} | Latency: ${latencyDuration}ms | Cost: $${costEst} | SDK: v${SDK_VERSION} | Feature: RAG`
|
|
10922
|
-
})
|
|
10923
|
-
}).catch((err) => {
|
|
10924
|
-
Logger.warn(`[Pipeline Telemetry] Failed to send trace async to ${absoluteUrl}`, { error: err.message });
|
|
10925
|
-
});
|
|
10926
|
-
} catch (err) {
|
|
10927
|
-
Logger.warn(`[Pipeline Telemetry] Failed to send trace async to ${absoluteUrl}:`, { error: err.message });
|
|
10928
|
-
}
|
|
10929
|
-
})();
|
|
10930
|
-
}
|
|
10931
11126
|
yield {
|
|
10932
11127
|
reply: "",
|
|
10933
11128
|
sources,
|
|
@@ -12585,8 +12780,8 @@ init_RedisProvider();
|
|
|
12585
12780
|
init_WeaviateProvider();
|
|
12586
12781
|
init_UniversalVectorProvider();
|
|
12587
12782
|
|
|
12588
|
-
// src/handlers/
|
|
12589
|
-
var
|
|
12783
|
+
// src/handlers/chatHandler.ts
|
|
12784
|
+
var import_server3 = require("next/server");
|
|
12590
12785
|
|
|
12591
12786
|
// src/core/DatabaseStorage.ts
|
|
12592
12787
|
var fs = __toESM(require("fs"));
|
|
@@ -13070,9 +13265,8 @@ var _DatabaseStorage = class _DatabaseStorage {
|
|
|
13070
13265
|
_DatabaseStorage.SUBSCRIPTION_CACHE_TTL_MS = 5 * 60 * 1e3;
|
|
13071
13266
|
var DatabaseStorage = _DatabaseStorage;
|
|
13072
13267
|
|
|
13073
|
-
// src/handlers/
|
|
13074
|
-
|
|
13075
|
-
var import_crypto2 = require("crypto");
|
|
13268
|
+
// src/handlers/internal/auth.ts
|
|
13269
|
+
var import_server = require("next/server");
|
|
13076
13270
|
async function checkAuth(req, onAuthorize) {
|
|
13077
13271
|
if (!onAuthorize) return null;
|
|
13078
13272
|
try {
|
|
@@ -13089,6 +13283,8 @@ async function checkAuth(req, onAuthorize) {
|
|
|
13089
13283
|
return import_server.NextResponse.json({ error: msg }, { status: 401 });
|
|
13090
13284
|
}
|
|
13091
13285
|
}
|
|
13286
|
+
|
|
13287
|
+
// src/handlers/internal/rateLimit.ts
|
|
13092
13288
|
var RateLimiter = class {
|
|
13093
13289
|
constructor(windowMs = 6e4, max = 30) {
|
|
13094
13290
|
this.hits = /* @__PURE__ */ new Map();
|
|
@@ -13150,6 +13346,9 @@ function checkRateLimit(req) {
|
|
|
13150
13346
|
}
|
|
13151
13347
|
return null;
|
|
13152
13348
|
}
|
|
13349
|
+
|
|
13350
|
+
// src/handlers/internal/license.ts
|
|
13351
|
+
var import_server2 = require("next/server");
|
|
13153
13352
|
var FREE_TIER_NAMES = /* @__PURE__ */ new Set(["hobby", "free", "free_trial", "trial"]);
|
|
13154
13353
|
function isFreeTier(tier) {
|
|
13155
13354
|
if (!tier) return true;
|
|
@@ -13194,7 +13393,7 @@ async function enforceLicense(req, config, bodyLicenseKey) {
|
|
|
13194
13393
|
} catch (err) {
|
|
13195
13394
|
return {
|
|
13196
13395
|
ok: false,
|
|
13197
|
-
response:
|
|
13396
|
+
response: import_server2.NextResponse.json(
|
|
13198
13397
|
{ error: `License validation failed: ${err.message}` },
|
|
13199
13398
|
{ status: 401 }
|
|
13200
13399
|
)
|
|
@@ -13252,6 +13451,8 @@ function resolveLicensedNamespace(payload, requestedNamespace) {
|
|
|
13252
13451
|
const defaultNamespace = ((_a2 = payload.projectId) == null ? void 0 : _a2.startsWith("retrivora-")) ? payload.projectId : `retrivora-${payload.projectId || "default"}`;
|
|
13253
13452
|
return { ok: true, namespace: requested || defaultNamespace };
|
|
13254
13453
|
}
|
|
13454
|
+
|
|
13455
|
+
// src/handlers/internal/sanitize.ts
|
|
13255
13456
|
var MAX_MESSAGE_LENGTH = 8e3;
|
|
13256
13457
|
function sanitizeInput(raw) {
|
|
13257
13458
|
const stripped = raw.replace(/[\u0000-\u0008\u000B\u000C\u000E-\u001F\u007F-\u009F]/g, "");
|
|
@@ -13264,6 +13465,9 @@ function sanitizeInput(raw) {
|
|
|
13264
13465
|
}
|
|
13265
13466
|
return { ok: true, value: trimmed };
|
|
13266
13467
|
}
|
|
13468
|
+
|
|
13469
|
+
// src/handlers/internal/plugin.ts
|
|
13470
|
+
var import_crypto2 = require("crypto");
|
|
13267
13471
|
var _pluginCache = /* @__PURE__ */ new Map();
|
|
13268
13472
|
var PLUGIN_CACHE_TTL_MS = 10 * 60 * 1e3;
|
|
13269
13473
|
function configHash(config) {
|
|
@@ -13278,6 +13482,9 @@ function getOrCreatePlugin(configOrPlugin) {
|
|
|
13278
13482
|
_pluginCache.set(key, { plugin, createdAt: Date.now() });
|
|
13279
13483
|
return plugin;
|
|
13280
13484
|
}
|
|
13485
|
+
|
|
13486
|
+
// src/handlers/internal/telemetry.ts
|
|
13487
|
+
init_version();
|
|
13281
13488
|
var TELEMETRY_ALLOWED_HOSTS = /* @__PURE__ */ new Set([
|
|
13282
13489
|
"retrivora.com",
|
|
13283
13490
|
"www.retrivora.com"
|
|
@@ -13298,8 +13505,20 @@ function sanitizeTelemetryUrl(raw) {
|
|
|
13298
13505
|
return null;
|
|
13299
13506
|
}
|
|
13300
13507
|
}
|
|
13301
|
-
|
|
13302
|
-
|
|
13508
|
+
function classifyError(message) {
|
|
13509
|
+
const m = (message || "").toLowerCase();
|
|
13510
|
+
if (/license|revoked|terminated|suspended/.test(m)) return { errorCode: "LICENSE_ERROR", errorType: "LICENSE_ERROR" };
|
|
13511
|
+
if (/rate.?limit|429/.test(m)) return { errorCode: "RATE_LIMIT_ERROR", errorType: "RATE_LIMIT_ERROR" };
|
|
13512
|
+
if (/unauthorized|401|403/.test(m)) return { errorCode: "AUTH_ERROR", errorType: "AUTH_ERROR" };
|
|
13513
|
+
if (/embedding|embed.?fail/.test(m)) return { errorCode: "EMBEDDING_ERROR", errorType: "EMBEDDING_ERROR" };
|
|
13514
|
+
if (/vector|pinecone|qdrant|pgvector|chroma|milvus|mongodb/.test(m)) return { errorCode: "VECTOR_DB_ERROR", errorType: "VECTOR_DB_ERROR" };
|
|
13515
|
+
if (/llm|chat|generation|openai|groq|anthropic|gemini|mistral/.test(m)) return { errorCode: "LLM_ERROR", errorType: "LLM_ERROR" };
|
|
13516
|
+
if (/fetch|network|timeout|econnrefused|socket/.test(m)) return { errorCode: "NETWORK_ERROR", errorType: "NETWORK_ERROR" };
|
|
13517
|
+
if (/stream/.test(m)) return { errorCode: "STREAMING_ERROR", errorType: "STREAMING_ERROR" };
|
|
13518
|
+
return { errorCode: "INTERNAL_ERROR", errorType: "INTERNAL_ERROR" };
|
|
13519
|
+
}
|
|
13520
|
+
async function reportTelemetry(req, plugin, action, status, details, trace, extra) {
|
|
13521
|
+
var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k, _l, _m, _n, _o, _p;
|
|
13303
13522
|
try {
|
|
13304
13523
|
const config = plugin.getConfig();
|
|
13305
13524
|
const licenseKey = config.licenseKey || process.env.RETRIVORA_LICENSE_KEY || process.env.RAG_LICENSE_KEY;
|
|
@@ -13310,13 +13529,14 @@ async function reportTelemetry(req, plugin, action, status, details, trace) {
|
|
|
13310
13529
|
if (!telemetryUrl) return;
|
|
13311
13530
|
const userAgent = req.headers.get("user-agent") || `Retrivora-SDK/${SDK_VERSION}`;
|
|
13312
13531
|
const projectId = config.projectId || "default";
|
|
13313
|
-
const model = (trace == null ? void 0 : trace.model) || ((_a2 = config.llm) == null ? void 0 : _a2.model) ||
|
|
13314
|
-
const provider = (trace == null ? void 0 : trace.provider) || ((
|
|
13315
|
-
const tokens = Number(((
|
|
13316
|
-
const costUsd = Number(((
|
|
13317
|
-
const latencyMs = Number(((
|
|
13532
|
+
const model = (trace == null ? void 0 : trace.model) || ((_a2 = config.llm) == null ? void 0 : _a2.model) || "llama-3.1-8b-instant";
|
|
13533
|
+
const provider = (trace == null ? void 0 : trace.provider) || ((_b = config.llm) == null ? void 0 : _b.provider) || "groq";
|
|
13534
|
+
const tokens = Number(((_c = trace == null ? void 0 : trace.tokens) == null ? void 0 : _c.totalTokens) || (trace == null ? void 0 : trace.totalTokens) || 0);
|
|
13535
|
+
const costUsd = Number(((_d = trace == null ? void 0 : trace.tokens) == null ? void 0 : _d.estimatedCostUsd) || (trace == null ? void 0 : trace.costUsd) || 0);
|
|
13536
|
+
const latencyMs = Number(((_e = trace == null ? void 0 : trace.latency) == null ? void 0 : _e.totalMs) || (trace == null ? void 0 : trace.latencyMs) || 0);
|
|
13537
|
+
const sanitizedTrace = trace ? __spreadProps(__spreadValues({}, trace), { systemPrompt: void 0, userPrompt: void 0 }) : void 0;
|
|
13318
13538
|
const payload = {
|
|
13319
|
-
trace,
|
|
13539
|
+
trace: sanitizedTrace,
|
|
13320
13540
|
projectId,
|
|
13321
13541
|
organization: process.env.RETRIVORA_ORGANIZATION || "default-org",
|
|
13322
13542
|
sdkVersion: SDK_VERSION,
|
|
@@ -13331,7 +13551,17 @@ async function reportTelemetry(req, plugin, action, status, details, trace) {
|
|
|
13331
13551
|
feature: action,
|
|
13332
13552
|
userAgent,
|
|
13333
13553
|
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
13334
|
-
details
|
|
13554
|
+
details,
|
|
13555
|
+
// ── Structured observability fields ──────────────────────────────────
|
|
13556
|
+
requestId: (_f = trace == null ? void 0 : trace.requestId) != null ? _f : null,
|
|
13557
|
+
embedMs: (_h = (_g2 = trace == null ? void 0 : trace.latency) == null ? void 0 : _g2.embedMs) != null ? _h : null,
|
|
13558
|
+
retrieveMs: (_j = (_i = trace == null ? void 0 : trace.latency) == null ? void 0 : _i.retrieveMs) != null ? _j : null,
|
|
13559
|
+
generateMs: (_l = (_k = trace == null ? void 0 : trace.latency) == null ? void 0 : _k.generateMs) != null ? _l : null,
|
|
13560
|
+
ttftMs: (_m = extra == null ? void 0 : extra.ttftMs) != null ? _m : null,
|
|
13561
|
+
errorCode: (_n = extra == null ? void 0 : extra.errorCode) != null ? _n : null,
|
|
13562
|
+
errorType: (_o = extra == null ? void 0 : extra.errorType) != null ? _o : null,
|
|
13563
|
+
isStreaming: (_p = extra == null ? void 0 : extra.isStreaming) != null ? _p : false,
|
|
13564
|
+
environment: process.env.NODE_ENV || "development"
|
|
13335
13565
|
};
|
|
13336
13566
|
await fetch(telemetryUrl, {
|
|
13337
13567
|
method: "POST",
|
|
@@ -13347,43 +13577,8 @@ async function reportTelemetry(req, plugin, action, status, details, trace) {
|
|
|
13347
13577
|
} catch (e) {
|
|
13348
13578
|
}
|
|
13349
13579
|
}
|
|
13350
|
-
function sseFrame(payload) {
|
|
13351
|
-
return `data: ${JSON.stringify(payload)}
|
|
13352
13580
|
|
|
13353
|
-
|
|
13354
|
-
}
|
|
13355
|
-
function sseTextFrame(text) {
|
|
13356
|
-
return `data: ${JSON.stringify({ type: "text", text })}
|
|
13357
|
-
|
|
13358
|
-
`;
|
|
13359
|
-
}
|
|
13360
|
-
function sseMetaFrame(meta) {
|
|
13361
|
-
return `data: ${JSON.stringify(__spreadValues({ type: "metadata" }, meta != null ? meta : {}))}
|
|
13362
|
-
|
|
13363
|
-
`;
|
|
13364
|
-
}
|
|
13365
|
-
function sseUIFrame(uiTransformation) {
|
|
13366
|
-
return `data: ${JSON.stringify({ type: "ui_transformation", data: uiTransformation })}
|
|
13367
|
-
|
|
13368
|
-
`;
|
|
13369
|
-
}
|
|
13370
|
-
function sseObservabilityFrame(trace) {
|
|
13371
|
-
return `data: ${JSON.stringify({ type: "observability", data: trace })}
|
|
13372
|
-
|
|
13373
|
-
`;
|
|
13374
|
-
}
|
|
13375
|
-
function sseErrorFrame(message) {
|
|
13376
|
-
return `data: ${JSON.stringify({ type: "error", error: message })}
|
|
13377
|
-
|
|
13378
|
-
`;
|
|
13379
|
-
}
|
|
13380
|
-
var SSE_HEADERS = {
|
|
13381
|
-
"Content-Type": "text/event-stream; charset=utf-8",
|
|
13382
|
-
"Cache-Control": "no-cache, no-transform",
|
|
13383
|
-
Connection: "keep-alive",
|
|
13384
|
-
"X-Accel-Buffering": "no"
|
|
13385
|
-
// Disable Nginx buffering for streaming
|
|
13386
|
-
};
|
|
13581
|
+
// src/handlers/chatHandler.ts
|
|
13387
13582
|
function createChatHandler(configOrPlugin, options) {
|
|
13388
13583
|
const plugin = getOrCreatePlugin(configOrPlugin);
|
|
13389
13584
|
const storage = new DatabaseStorage(plugin.getConfig());
|
|
@@ -13435,7 +13630,7 @@ function createChatHandler(configOrPlugin, options) {
|
|
|
13435
13630
|
if (!namespaceCheck.ok) return namespaceCheck.response;
|
|
13436
13631
|
const sanitized = sanitizeInput(rawMessage != null ? rawMessage : "");
|
|
13437
13632
|
if (!sanitized.ok) {
|
|
13438
|
-
return
|
|
13633
|
+
return import_server3.NextResponse.json({ error: { code: "INVALID_INPUT", message: sanitized.error } }, { status: 400 });
|
|
13439
13634
|
}
|
|
13440
13635
|
const message = sanitized.value;
|
|
13441
13636
|
const userMsgId = userMessageId || `user_${Date.now()}_${Math.random().toString(36).slice(2, 6)}`;
|
|
@@ -13454,18 +13649,68 @@ function createChatHandler(configOrPlugin, options) {
|
|
|
13454
13649
|
uiTransformation: result.ui_transformation,
|
|
13455
13650
|
trace: result.trace
|
|
13456
13651
|
}).catch((err) => console.warn("[createChatHandler] Failed to save assistant message:", err));
|
|
13457
|
-
await reportTelemetry(
|
|
13458
|
-
|
|
13652
|
+
await reportTelemetry(
|
|
13653
|
+
req,
|
|
13654
|
+
plugin,
|
|
13655
|
+
"RAG_QUERY",
|
|
13656
|
+
"success",
|
|
13657
|
+
`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`,
|
|
13658
|
+
result.trace,
|
|
13659
|
+
{ isStreaming: false }
|
|
13660
|
+
);
|
|
13661
|
+
return import_server3.NextResponse.json(__spreadProps(__spreadValues({}, result), {
|
|
13459
13662
|
messageId: assistantMsgId,
|
|
13460
13663
|
userMessageId: userMsgId
|
|
13461
13664
|
}));
|
|
13462
13665
|
} catch (err) {
|
|
13463
13666
|
const message = err instanceof Error ? err.message : "Internal server error";
|
|
13464
|
-
|
|
13465
|
-
|
|
13667
|
+
const { errorCode, errorType } = classifyError(message);
|
|
13668
|
+
await reportTelemetry(req, plugin, "RAG_QUERY", "error", message, void 0, { isStreaming: false, errorCode, errorType });
|
|
13669
|
+
return import_server3.NextResponse.json({ error: message }, { status: 500 });
|
|
13466
13670
|
}
|
|
13467
13671
|
};
|
|
13468
13672
|
}
|
|
13673
|
+
|
|
13674
|
+
// src/handlers/internal/sse.ts
|
|
13675
|
+
function sseFrame(payload) {
|
|
13676
|
+
return `data: ${JSON.stringify(payload)}
|
|
13677
|
+
|
|
13678
|
+
`;
|
|
13679
|
+
}
|
|
13680
|
+
function sseTextFrame(text) {
|
|
13681
|
+
return `data: ${JSON.stringify({ type: "text", text })}
|
|
13682
|
+
|
|
13683
|
+
`;
|
|
13684
|
+
}
|
|
13685
|
+
function sseMetaFrame(meta) {
|
|
13686
|
+
return `data: ${JSON.stringify(__spreadValues({ type: "metadata" }, meta != null ? meta : {}))}
|
|
13687
|
+
|
|
13688
|
+
`;
|
|
13689
|
+
}
|
|
13690
|
+
function sseUIFrame(uiTransformation) {
|
|
13691
|
+
return `data: ${JSON.stringify({ type: "ui_transformation", data: uiTransformation })}
|
|
13692
|
+
|
|
13693
|
+
`;
|
|
13694
|
+
}
|
|
13695
|
+
function sseObservabilityFrame(trace) {
|
|
13696
|
+
return `data: ${JSON.stringify({ type: "observability", data: trace })}
|
|
13697
|
+
|
|
13698
|
+
`;
|
|
13699
|
+
}
|
|
13700
|
+
function sseErrorFrame(message) {
|
|
13701
|
+
return `data: ${JSON.stringify({ type: "error", error: message })}
|
|
13702
|
+
|
|
13703
|
+
`;
|
|
13704
|
+
}
|
|
13705
|
+
var SSE_HEADERS = {
|
|
13706
|
+
"Content-Type": "text/event-stream; charset=utf-8",
|
|
13707
|
+
"Cache-Control": "no-cache, no-transform",
|
|
13708
|
+
Connection: "keep-alive",
|
|
13709
|
+
"X-Accel-Buffering": "no"
|
|
13710
|
+
// Disable Nginx buffering for streaming
|
|
13711
|
+
};
|
|
13712
|
+
|
|
13713
|
+
// src/handlers/streamHandler.ts
|
|
13469
13714
|
function createStreamHandler(configOrPlugin, options) {
|
|
13470
13715
|
const plugin = getOrCreatePlugin(configOrPlugin);
|
|
13471
13716
|
const storage = new DatabaseStorage(plugin.getConfig());
|
|
@@ -13540,6 +13785,7 @@ function createStreamHandler(configOrPlugin, options) {
|
|
|
13540
13785
|
const assistantMsgId = messageId || `assistant_${Date.now()}_${Math.random().toString(36).slice(2, 6)}`;
|
|
13541
13786
|
const encoder = new TextEncoder();
|
|
13542
13787
|
let isActive = true;
|
|
13788
|
+
let streamTelemetrySent = false;
|
|
13543
13789
|
const stream = new ReadableStream({
|
|
13544
13790
|
async start(controller) {
|
|
13545
13791
|
var _a3, _b2, _c, _d, _e;
|
|
@@ -13554,11 +13800,16 @@ function createStreamHandler(configOrPlugin, options) {
|
|
|
13554
13800
|
try {
|
|
13555
13801
|
const pipelineStream = plugin.chatStream(message, history, namespaceCheck.namespace);
|
|
13556
13802
|
let fullReply = "";
|
|
13803
|
+
let ttftMs;
|
|
13804
|
+
const handlerRequestStartMs = Date.now();
|
|
13557
13805
|
try {
|
|
13558
13806
|
for (var iter = __forAwait(pipelineStream), more, temp, error; more = !(temp = await iter.next()).done; more = false) {
|
|
13559
13807
|
const chunk = temp.value;
|
|
13560
13808
|
if (!isActive) break;
|
|
13561
13809
|
if (typeof chunk === "string") {
|
|
13810
|
+
if (ttftMs === void 0) {
|
|
13811
|
+
ttftMs = Date.now() - handlerRequestStartMs;
|
|
13812
|
+
}
|
|
13562
13813
|
fullReply += chunk;
|
|
13563
13814
|
enqueue(sseTextFrame(chunk));
|
|
13564
13815
|
} else if (chunk && typeof chunk === "object" && "type" in chunk && chunk.type === "thinking") {
|
|
@@ -13599,7 +13850,16 @@ function createStreamHandler(configOrPlugin, options) {
|
|
|
13599
13850
|
uiTransformation,
|
|
13600
13851
|
trace: responseChunk == null ? void 0 : responseChunk.trace
|
|
13601
13852
|
}).catch((err) => console.warn("[createStreamHandler] Failed to save assistant message:", err));
|
|
13602
|
-
|
|
13853
|
+
streamTelemetrySent = true;
|
|
13854
|
+
await reportTelemetry(
|
|
13855
|
+
req,
|
|
13856
|
+
plugin,
|
|
13857
|
+
"RAG_QUERY",
|
|
13858
|
+
"success",
|
|
13859
|
+
`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`,
|
|
13860
|
+
responseChunk == null ? void 0 : responseChunk.trace,
|
|
13861
|
+
{ ttftMs, isStreaming: true }
|
|
13862
|
+
);
|
|
13603
13863
|
}
|
|
13604
13864
|
}
|
|
13605
13865
|
} catch (temp) {
|
|
@@ -13615,11 +13875,21 @@ function createStreamHandler(configOrPlugin, options) {
|
|
|
13615
13875
|
} catch (streamError) {
|
|
13616
13876
|
if (isActive) {
|
|
13617
13877
|
let errorMessage = streamError instanceof Error ? streamError.message : String(streamError);
|
|
13618
|
-
if (errorMessage.includes("
|
|
13878
|
+
if (errorMessage.includes("LICENSE_REVOKED") || errorMessage.toLowerCase().includes("key has been revoked") || errorMessage.toLowerCase().includes("key has been terminated") || errorMessage.toLowerCase().includes("key has been suspended")) {
|
|
13619
13879
|
errorMessage = "Your Retrivora license key has been terminated, suspended, or revoked. Access denied.";
|
|
13620
13880
|
}
|
|
13621
13881
|
console.error("[createStreamHandler] Stream error:", streamError);
|
|
13622
|
-
|
|
13882
|
+
const { errorCode, errorType } = classifyError(errorMessage);
|
|
13883
|
+
streamTelemetrySent = true;
|
|
13884
|
+
await reportTelemetry(
|
|
13885
|
+
req,
|
|
13886
|
+
plugin,
|
|
13887
|
+
"RAG_QUERY",
|
|
13888
|
+
"error",
|
|
13889
|
+
errorMessage,
|
|
13890
|
+
void 0,
|
|
13891
|
+
{ isStreaming: true, errorCode, errorType }
|
|
13892
|
+
);
|
|
13623
13893
|
try {
|
|
13624
13894
|
enqueue(sseErrorFrame(errorMessage));
|
|
13625
13895
|
} catch (e) {
|
|
@@ -13628,6 +13898,10 @@ function createStreamHandler(configOrPlugin, options) {
|
|
|
13628
13898
|
} finally {
|
|
13629
13899
|
if (isActive) {
|
|
13630
13900
|
isActive = false;
|
|
13901
|
+
try {
|
|
13902
|
+
enqueue(sseFrame({ type: "done" }));
|
|
13903
|
+
} catch (e) {
|
|
13904
|
+
}
|
|
13631
13905
|
try {
|
|
13632
13906
|
controller.close();
|
|
13633
13907
|
} catch (e) {
|
|
@@ -13637,11 +13911,26 @@ function createStreamHandler(configOrPlugin, options) {
|
|
|
13637
13911
|
},
|
|
13638
13912
|
cancel(reason) {
|
|
13639
13913
|
isActive = false;
|
|
13914
|
+
if (!streamTelemetrySent) {
|
|
13915
|
+
reportTelemetry(
|
|
13916
|
+
req,
|
|
13917
|
+
plugin,
|
|
13918
|
+
"RAG_QUERY_ABORTED",
|
|
13919
|
+
"error",
|
|
13920
|
+
"Client disconnected mid-stream",
|
|
13921
|
+
void 0,
|
|
13922
|
+
{ isStreaming: true, errorCode: "CLIENT_DISCONNECT", errorType: "STREAMING_ERROR" }
|
|
13923
|
+
).catch(() => {
|
|
13924
|
+
});
|
|
13925
|
+
}
|
|
13640
13926
|
}
|
|
13641
13927
|
});
|
|
13642
13928
|
return new Response(stream, { headers: __spreadProps(__spreadValues({}, SSE_HEADERS), { "x-message-id": assistantMsgId, "x-user-message-id": userMsgId }) });
|
|
13643
13929
|
};
|
|
13644
13930
|
}
|
|
13931
|
+
|
|
13932
|
+
// src/handlers/ingestHandler.ts
|
|
13933
|
+
var import_server4 = require("next/server");
|
|
13645
13934
|
function createIngestHandler(configOrPlugin, options) {
|
|
13646
13935
|
const plugin = getOrCreatePlugin(configOrPlugin);
|
|
13647
13936
|
const onAuthorize = options == null ? void 0 : options.onAuthorize;
|
|
@@ -13655,7 +13944,7 @@ function createIngestHandler(configOrPlugin, options) {
|
|
|
13655
13944
|
if (!licenseCheck.ok) return licenseCheck.response;
|
|
13656
13945
|
const { documents, namespace } = body;
|
|
13657
13946
|
if (!Array.isArray(documents) || documents.length === 0) {
|
|
13658
|
-
return
|
|
13947
|
+
return import_server4.NextResponse.json({ error: "documents array is required" }, { status: 400 });
|
|
13659
13948
|
}
|
|
13660
13949
|
const namespaceCheck = resolveLicensedNamespace(licenseCheck.payload, namespace);
|
|
13661
13950
|
if (!namespaceCheck.ok) return namespaceCheck.response;
|
|
@@ -13682,14 +13971,17 @@ function createIngestHandler(configOrPlugin, options) {
|
|
|
13682
13971
|
}
|
|
13683
13972
|
const results = await plugin.ingest(documents, namespaceCheck.namespace);
|
|
13684
13973
|
await reportTelemetry(req, plugin, "DOCUMENT_INGESTION", "success", `Ingested ${documents.length} document(s)`);
|
|
13685
|
-
return
|
|
13974
|
+
return import_server4.NextResponse.json({ results });
|
|
13686
13975
|
} catch (err) {
|
|
13687
13976
|
const message = err instanceof Error ? err.message : "Internal server error";
|
|
13688
13977
|
await reportTelemetry(req, plugin, "DOCUMENT_INGESTION", "error", message);
|
|
13689
|
-
return
|
|
13978
|
+
return import_server4.NextResponse.json({ error: message }, { status: 500 });
|
|
13690
13979
|
}
|
|
13691
13980
|
};
|
|
13692
13981
|
}
|
|
13982
|
+
|
|
13983
|
+
// src/handlers/healthHandler.ts
|
|
13984
|
+
var import_server5 = require("next/server");
|
|
13693
13985
|
function createHealthHandler(configOrPlugin, options) {
|
|
13694
13986
|
const plugin = getOrCreatePlugin(configOrPlugin);
|
|
13695
13987
|
const onAuthorize = options == null ? void 0 : options.onAuthorize;
|
|
@@ -13701,17 +13993,21 @@ function createHealthHandler(configOrPlugin, options) {
|
|
|
13701
13993
|
try {
|
|
13702
13994
|
const health = await plugin.checkHealth();
|
|
13703
13995
|
const status = health.allHealthy ? "ok" : "degraded";
|
|
13704
|
-
return
|
|
13996
|
+
return import_server5.NextResponse.json(__spreadProps(__spreadValues({
|
|
13705
13997
|
status
|
|
13706
13998
|
}, health), {
|
|
13707
13999
|
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
13708
14000
|
}));
|
|
13709
14001
|
} catch (err) {
|
|
13710
14002
|
const message = err instanceof Error ? err.message : "Unknown error";
|
|
13711
|
-
return
|
|
14003
|
+
return import_server5.NextResponse.json({ status: "error", error: message }, { status: 500 });
|
|
13712
14004
|
}
|
|
13713
14005
|
};
|
|
13714
14006
|
}
|
|
14007
|
+
|
|
14008
|
+
// src/handlers/licenseHandler.ts
|
|
14009
|
+
var import_server6 = require("next/server");
|
|
14010
|
+
init_version();
|
|
13715
14011
|
function createLicenseHandler(configOrPlugin, options) {
|
|
13716
14012
|
const plugin = getOrCreatePlugin(configOrPlugin);
|
|
13717
14013
|
const onAuthorize = options == null ? void 0 : options.onAuthorize;
|
|
@@ -13737,7 +14033,7 @@ function createLicenseHandler(configOrPlugin, options) {
|
|
|
13737
14033
|
const licenseKey = (rawKey || "").trim().replace(/^["']|["']$/g, "").trim();
|
|
13738
14034
|
const projectId = (body == null ? void 0 : body.projectId) || config.projectId || "";
|
|
13739
14035
|
const payload = await LicenseVerifier.verifyAsync(licenseKey, projectId);
|
|
13740
|
-
return
|
|
14036
|
+
return import_server6.NextResponse.json({
|
|
13741
14037
|
valid: true,
|
|
13742
14038
|
licenseStatus: "ACTIVE",
|
|
13743
14039
|
minimumSupportedVersion: "2.1.0",
|
|
@@ -13747,7 +14043,7 @@ function createLicenseHandler(configOrPlugin, options) {
|
|
|
13747
14043
|
message: "License key is active and valid."
|
|
13748
14044
|
});
|
|
13749
14045
|
} catch (err) {
|
|
13750
|
-
return
|
|
14046
|
+
return import_server6.NextResponse.json(
|
|
13751
14047
|
{
|
|
13752
14048
|
valid: false,
|
|
13753
14049
|
licenseStatus: "REVOKED",
|
|
@@ -13762,6 +14058,9 @@ function createLicenseHandler(configOrPlugin, options) {
|
|
|
13762
14058
|
}
|
|
13763
14059
|
};
|
|
13764
14060
|
}
|
|
14061
|
+
|
|
14062
|
+
// src/handlers/uploadHandler.ts
|
|
14063
|
+
var import_server7 = require("next/server");
|
|
13765
14064
|
function createUploadHandler(configOrPlugin, options) {
|
|
13766
14065
|
const plugin = getOrCreatePlugin(configOrPlugin);
|
|
13767
14066
|
const onAuthorize = options == null ? void 0 : options.onAuthorize;
|
|
@@ -13780,7 +14079,7 @@ function createUploadHandler(configOrPlugin, options) {
|
|
|
13780
14079
|
const namespaceCheck = resolveLicensedNamespace(licenseCheck.payload, namespace);
|
|
13781
14080
|
if (!namespaceCheck.ok) return namespaceCheck.response;
|
|
13782
14081
|
if (!files || files.length === 0) {
|
|
13783
|
-
return
|
|
14082
|
+
return import_server7.NextResponse.json({ error: "No files provided" }, { status: 400 });
|
|
13784
14083
|
}
|
|
13785
14084
|
if (isFreeTier(licenseCheck.payload.tier)) {
|
|
13786
14085
|
const totalUploadBytes = files.reduce((sum, f) => sum + (f.size || 0), 0);
|
|
@@ -13802,137 +14101,174 @@ function createUploadHandler(configOrPlugin, options) {
|
|
|
13802
14101
|
);
|
|
13803
14102
|
}
|
|
13804
14103
|
}
|
|
13805
|
-
const
|
|
13806
|
-
|
|
13807
|
-
|
|
13808
|
-
|
|
13809
|
-
|
|
13810
|
-
const
|
|
13811
|
-
|
|
13812
|
-
|
|
13813
|
-
|
|
13814
|
-
|
|
13815
|
-
|
|
13816
|
-
|
|
13817
|
-
|
|
13818
|
-
|
|
13819
|
-
const
|
|
13820
|
-
|
|
13821
|
-
|
|
13822
|
-
|
|
13823
|
-
|
|
13824
|
-
rowData[key]
|
|
14104
|
+
const fileDocsArrays = await Promise.all(
|
|
14105
|
+
files.map(async (file) => {
|
|
14106
|
+
const fileDocs = [];
|
|
14107
|
+
const buffer = Buffer.isBuffer(file) ? file : Buffer.from(await file.arrayBuffer());
|
|
14108
|
+
const isExcel = file.name.toLowerCase().endsWith(".xlsx") || file.name.toLowerCase().endsWith(".xls") || file.type.includes("spreadsheet") || file.type.includes("excel");
|
|
14109
|
+
const SPREADSHEET_CHUNK_SIZE = 30;
|
|
14110
|
+
if (file.name.toLowerCase().endsWith(".csv") || file.name.toLowerCase().endsWith(".tsv") || file.type === "text/csv" || file.type.includes("tab-separated")) {
|
|
14111
|
+
const text = buffer.toString("utf-8");
|
|
14112
|
+
const Papa = await import("papaparse");
|
|
14113
|
+
const parsed = Papa.default.parse(text, { header: true, skipEmptyLines: true });
|
|
14114
|
+
if (parsed.data && parsed.data.length > 0) {
|
|
14115
|
+
const csvCols = Object.keys(parsed.data[0]);
|
|
14116
|
+
const rawRows = [];
|
|
14117
|
+
let lastRowData = null;
|
|
14118
|
+
for (const row of parsed.data) {
|
|
14119
|
+
const rowData = __spreadValues({}, row);
|
|
14120
|
+
const groupingKey = Object.keys(rowData)[0];
|
|
14121
|
+
if (lastRowData && groupingKey && rowData[groupingKey] && rowData[groupingKey] === lastRowData[groupingKey]) {
|
|
14122
|
+
for (const key of Object.keys(rowData)) {
|
|
14123
|
+
if (!rowData[key] && lastRowData[key]) {
|
|
14124
|
+
rowData[key] = lastRowData[key];
|
|
14125
|
+
}
|
|
13825
14126
|
}
|
|
13826
14127
|
}
|
|
14128
|
+
lastRowData = __spreadValues({}, rowData);
|
|
14129
|
+
rawRows.push(rowData);
|
|
13827
14130
|
}
|
|
13828
|
-
|
|
13829
|
-
|
|
13830
|
-
|
|
13831
|
-
|
|
13832
|
-
|
|
14131
|
+
for (let i = 0; i < rawRows.length; i += SPREADSHEET_CHUNK_SIZE) {
|
|
14132
|
+
const slice = rawRows.slice(i, i + SPREADSHEET_CHUNK_SIZE);
|
|
14133
|
+
const startRow = i + 1;
|
|
14134
|
+
const endRow = i + slice.length;
|
|
14135
|
+
const chunkIndex = Math.floor(i / SPREADSHEET_CHUNK_SIZE) + 1;
|
|
14136
|
+
const lines = [
|
|
14137
|
+
`File: ${file.name} (Rows ${startRow}-${endRow} of ${rawRows.length})`,
|
|
14138
|
+
`| ${csvCols.join(" | ")} |`,
|
|
14139
|
+
`| ${csvCols.map(() => "---").join(" | ")} |`
|
|
14140
|
+
];
|
|
14141
|
+
for (const r of slice) {
|
|
14142
|
+
const rowValues = csvCols.map((col) => {
|
|
14143
|
+
const val = r[col];
|
|
14144
|
+
if (val === void 0 || val === null) return "";
|
|
14145
|
+
return String(val).replace(/\|/g, "\\|").replace(/\r?\n/g, " ").trim();
|
|
14146
|
+
});
|
|
14147
|
+
lines.push(`| ${rowValues.join(" | ")} |`);
|
|
13833
14148
|
}
|
|
14149
|
+
fileDocs.push({
|
|
14150
|
+
docId: `${file.name}-chunk-${chunkIndex}`,
|
|
14151
|
+
content: lines.join("\n"),
|
|
14152
|
+
metadata: __spreadValues({
|
|
14153
|
+
fileName: file.name,
|
|
14154
|
+
fileSize: file.size,
|
|
14155
|
+
fileType: file.type || "text/csv",
|
|
14156
|
+
uploadedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
14157
|
+
startRow,
|
|
14158
|
+
endRow,
|
|
14159
|
+
rowCount: slice.length,
|
|
14160
|
+
headers: csvCols
|
|
14161
|
+
}, dimension ? { dimension } : {})
|
|
14162
|
+
});
|
|
13834
14163
|
}
|
|
13835
|
-
documents.push({
|
|
13836
|
-
docId: `${file.name}-row-${i}`,
|
|
13837
|
-
content: contentParts.join(", "),
|
|
13838
|
-
metadata: __spreadValues(__spreadProps(__spreadValues({
|
|
13839
|
-
fileName: file.name,
|
|
13840
|
-
fileSize: file.size,
|
|
13841
|
-
fileType: file.type || "text/csv",
|
|
13842
|
-
uploadedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
13843
|
-
}, dimension ? { dimension } : {}), {
|
|
13844
|
-
csvHeaders: csvCols
|
|
13845
|
-
}), rowData)
|
|
13846
|
-
});
|
|
13847
14164
|
}
|
|
13848
|
-
}
|
|
13849
|
-
|
|
13850
|
-
|
|
13851
|
-
|
|
13852
|
-
|
|
13853
|
-
|
|
13854
|
-
|
|
13855
|
-
|
|
13856
|
-
|
|
13857
|
-
|
|
13858
|
-
|
|
13859
|
-
|
|
13860
|
-
|
|
13861
|
-
|
|
13862
|
-
|
|
13863
|
-
|
|
13864
|
-
|
|
13865
|
-
|
|
13866
|
-
|
|
13867
|
-
|
|
13868
|
-
|
|
13869
|
-
|
|
13870
|
-
|
|
13871
|
-
|
|
13872
|
-
|
|
13873
|
-
|
|
13874
|
-
|
|
13875
|
-
|
|
13876
|
-
|
|
13877
|
-
|
|
13878
|
-
|
|
13879
|
-
|
|
13880
|
-
|
|
13881
|
-
|
|
13882
|
-
|
|
13883
|
-
|
|
13884
|
-
|
|
13885
|
-
|
|
13886
|
-
uploadedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
13887
|
-
}, dimension ? { dimension } : {}), {
|
|
13888
|
-
headers
|
|
13889
|
-
}), rowData)
|
|
14165
|
+
} else if (isExcel) {
|
|
14166
|
+
let excelLoaded = false;
|
|
14167
|
+
try {
|
|
14168
|
+
const XLSX = await import("xlsx");
|
|
14169
|
+
const workbook = XLSX.read(buffer, { type: "buffer", cellDates: true });
|
|
14170
|
+
for (const sheetName of workbook.SheetNames) {
|
|
14171
|
+
const sheet = workbook.Sheets[sheetName];
|
|
14172
|
+
if (!sheet) continue;
|
|
14173
|
+
const rawGrid = XLSX.utils.sheet_to_json(sheet, { header: 1, defval: "", blankrows: false });
|
|
14174
|
+
if (!rawGrid || rawGrid.length === 0) continue;
|
|
14175
|
+
let headerRowIndex = 0;
|
|
14176
|
+
while (headerRowIndex < rawGrid.length && (!rawGrid[headerRowIndex] || !rawGrid[headerRowIndex].some((c) => String(c != null ? c : "").trim().length > 0))) {
|
|
14177
|
+
headerRowIndex++;
|
|
14178
|
+
}
|
|
14179
|
+
if (headerRowIndex >= rawGrid.length) continue;
|
|
14180
|
+
const rawHeaders = rawGrid[headerRowIndex].map((h, idx) => {
|
|
14181
|
+
const s = String(h != null ? h : "").trim();
|
|
14182
|
+
return s || `Column_${idx + 1}`;
|
|
14183
|
+
});
|
|
14184
|
+
const dataRows = rawGrid.slice(headerRowIndex + 1).filter(
|
|
14185
|
+
(row) => row && row.some((c) => String(c != null ? c : "").trim().length > 0)
|
|
14186
|
+
);
|
|
14187
|
+
if (dataRows.length === 0) continue;
|
|
14188
|
+
for (let i = 0; i < dataRows.length; i += SPREADSHEET_CHUNK_SIZE) {
|
|
14189
|
+
const slice = dataRows.slice(i, i + SPREADSHEET_CHUNK_SIZE);
|
|
14190
|
+
const startRow = headerRowIndex + 1 + i + 1;
|
|
14191
|
+
const endRow = startRow + slice.length - 1;
|
|
14192
|
+
const chunkIndex = Math.floor(i / SPREADSHEET_CHUNK_SIZE) + 1;
|
|
14193
|
+
const lines = [
|
|
14194
|
+
`File: ${file.name} | Sheet: ${sheetName} (Rows ${startRow}-${endRow} of ${dataRows.length})`,
|
|
14195
|
+
`| ${rawHeaders.join(" | ")} |`,
|
|
14196
|
+
`| ${rawHeaders.map(() => "---").join(" | ")} |`
|
|
14197
|
+
];
|
|
14198
|
+
for (const r of slice) {
|
|
14199
|
+
const rowValues = rawHeaders.map((_, colIdx) => {
|
|
14200
|
+
const val = r[colIdx];
|
|
14201
|
+
if (val === void 0 || val === null) return "";
|
|
14202
|
+
return String(val).replace(/\|/g, "\\|").replace(/\r?\n/g, " ").trim();
|
|
13890
14203
|
});
|
|
14204
|
+
lines.push(`| ${rowValues.join(" | ")} |`);
|
|
13891
14205
|
}
|
|
14206
|
+
fileDocs.push({
|
|
14207
|
+
docId: `${file.name}-${sheetName}-chunk-${chunkIndex}`,
|
|
14208
|
+
content: lines.join("\n"),
|
|
14209
|
+
metadata: __spreadValues({
|
|
14210
|
+
fileName: file.name,
|
|
14211
|
+
sheetName,
|
|
14212
|
+
fileSize: file.size,
|
|
14213
|
+
fileType: file.type || "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
14214
|
+
uploadedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
14215
|
+
startRow,
|
|
14216
|
+
endRow,
|
|
14217
|
+
rowCount: slice.length,
|
|
14218
|
+
headers: rawHeaders
|
|
14219
|
+
}, dimension ? { dimension } : {})
|
|
14220
|
+
});
|
|
14221
|
+
}
|
|
14222
|
+
}
|
|
14223
|
+
excelLoaded = fileDocs.length > 0;
|
|
14224
|
+
} catch (xlsxErr) {
|
|
14225
|
+
excelLoaded = false;
|
|
14226
|
+
}
|
|
14227
|
+
if (!excelLoaded) {
|
|
14228
|
+
const content = await DocumentParser.parse(buffer, file.name, file.type);
|
|
14229
|
+
if (content && content.trim()) {
|
|
14230
|
+
fileDocs.push({
|
|
14231
|
+
docId: file.name,
|
|
14232
|
+
content,
|
|
14233
|
+
metadata: __spreadValues({
|
|
14234
|
+
fileName: file.name,
|
|
14235
|
+
fileSize: file.size,
|
|
14236
|
+
fileType: file.type || "application/vnd.ms-excel",
|
|
14237
|
+
uploadedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
14238
|
+
}, dimension ? { dimension } : {})
|
|
13892
14239
|
});
|
|
13893
|
-
}
|
|
13894
|
-
excelLoaded = documents.length > 0;
|
|
14240
|
+
}
|
|
13895
14241
|
}
|
|
13896
|
-
}
|
|
13897
|
-
excelLoaded = false;
|
|
13898
|
-
}
|
|
13899
|
-
if (!excelLoaded) {
|
|
14242
|
+
} else {
|
|
13900
14243
|
const content = await DocumentParser.parse(buffer, file.name, file.type);
|
|
13901
|
-
|
|
14244
|
+
fileDocs.push({
|
|
13902
14245
|
docId: file.name,
|
|
13903
14246
|
content,
|
|
13904
14247
|
metadata: __spreadValues({
|
|
13905
14248
|
fileName: file.name,
|
|
13906
14249
|
fileSize: file.size,
|
|
13907
|
-
fileType: file.type
|
|
14250
|
+
fileType: file.type,
|
|
13908
14251
|
uploadedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
13909
14252
|
}, dimension ? { dimension } : {})
|
|
13910
14253
|
});
|
|
13911
14254
|
}
|
|
13912
|
-
|
|
13913
|
-
|
|
13914
|
-
|
|
13915
|
-
|
|
13916
|
-
content,
|
|
13917
|
-
metadata: __spreadValues({
|
|
13918
|
-
fileName: file.name,
|
|
13919
|
-
fileSize: file.size,
|
|
13920
|
-
fileType: file.type,
|
|
13921
|
-
uploadedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
13922
|
-
}, dimension ? { dimension } : {})
|
|
13923
|
-
});
|
|
13924
|
-
}
|
|
13925
|
-
}
|
|
14255
|
+
return fileDocs;
|
|
14256
|
+
})
|
|
14257
|
+
);
|
|
14258
|
+
const documents = fileDocsArrays.flat();
|
|
13926
14259
|
const results = await plugin.ingest(documents, namespaceCheck.namespace);
|
|
13927
14260
|
await reportTelemetry(req, plugin, "DOCUMENT_INGESTION", "success", `Uploaded & ingested ${files.length} file(s)`);
|
|
13928
|
-
return
|
|
14261
|
+
return import_server7.NextResponse.json({ message: "Upload successful", results });
|
|
13929
14262
|
} catch (err) {
|
|
13930
14263
|
const message = err instanceof Error ? err.message : "Upload failed";
|
|
13931
14264
|
await reportTelemetry(req, plugin, "DOCUMENT_INGESTION", "error", message);
|
|
13932
|
-
return
|
|
14265
|
+
return import_server7.NextResponse.json({ error: message }, { status: 500 });
|
|
13933
14266
|
}
|
|
13934
14267
|
};
|
|
13935
14268
|
}
|
|
14269
|
+
|
|
14270
|
+
// src/handlers/suggestionsHandler.ts
|
|
14271
|
+
var import_server8 = require("next/server");
|
|
13936
14272
|
function createSuggestionsHandler(configOrPlugin, options) {
|
|
13937
14273
|
const plugin = getOrCreatePlugin(configOrPlugin);
|
|
13938
14274
|
const onAuthorize = options == null ? void 0 : options.onAuthorize;
|
|
@@ -13963,20 +14299,23 @@ function createSuggestionsHandler(configOrPlugin, options) {
|
|
|
13963
14299
|
namespace = url.searchParams.get("namespace") || void 0;
|
|
13964
14300
|
}
|
|
13965
14301
|
if (!query || typeof query !== "string" || !query.trim()) {
|
|
13966
|
-
return
|
|
14302
|
+
return import_server8.NextResponse.json({ suggestions: [] });
|
|
13967
14303
|
}
|
|
13968
14304
|
const namespaceCheck = resolveLicensedNamespace(effectiveLicensePayload, namespace);
|
|
13969
14305
|
if (!namespaceCheck.ok) return namespaceCheck.response;
|
|
13970
14306
|
const suggestions = await plugin.getSuggestions(query, namespaceCheck.namespace);
|
|
13971
14307
|
await reportTelemetry(req, plugin, "AUTO_SUGGESTIONS", "success", `Fetched ${suggestions.length} suggestions for: ${query.slice(0, 30)}`);
|
|
13972
|
-
return
|
|
14308
|
+
return import_server8.NextResponse.json({ suggestions });
|
|
13973
14309
|
} catch (err) {
|
|
13974
14310
|
const message = err instanceof Error ? err.message : "Internal server error";
|
|
13975
14311
|
await reportTelemetry(req, plugin, "AUTO_SUGGESTIONS", "error", message);
|
|
13976
|
-
return
|
|
14312
|
+
return import_server8.NextResponse.json({ error: message }, { status: 500 });
|
|
13977
14313
|
}
|
|
13978
14314
|
};
|
|
13979
14315
|
}
|
|
14316
|
+
|
|
14317
|
+
// src/handlers/historyHandler.ts
|
|
14318
|
+
var import_server9 = require("next/server");
|
|
13980
14319
|
function createHistoryHandler(configOrPlugin, options) {
|
|
13981
14320
|
const plugin = getOrCreatePlugin(configOrPlugin);
|
|
13982
14321
|
const storage = new DatabaseStorage(plugin.getConfig());
|
|
@@ -13991,25 +14330,28 @@ function createHistoryHandler(configOrPlugin, options) {
|
|
|
13991
14330
|
if (req.method === "GET") {
|
|
13992
14331
|
try {
|
|
13993
14332
|
const history = await storage.getHistory(sessionId, scope);
|
|
13994
|
-
return
|
|
14333
|
+
return import_server9.NextResponse.json({ history });
|
|
13995
14334
|
} catch (err) {
|
|
13996
14335
|
const message = err instanceof Error ? err.message : "Failed to fetch history";
|
|
13997
|
-
return
|
|
14336
|
+
return import_server9.NextResponse.json({ error: message }, { status: 500 });
|
|
13998
14337
|
}
|
|
13999
14338
|
} else if (req.method === "POST") {
|
|
14000
14339
|
try {
|
|
14001
14340
|
const body = await req.json().catch(() => ({}));
|
|
14002
14341
|
const sid = body.sessionId || sessionId;
|
|
14003
14342
|
await storage.clearHistory(sid, scope);
|
|
14004
|
-
return
|
|
14343
|
+
return import_server9.NextResponse.json({ success: true });
|
|
14005
14344
|
} catch (err) {
|
|
14006
14345
|
const message = err instanceof Error ? err.message : "Failed to clear history";
|
|
14007
|
-
return
|
|
14346
|
+
return import_server9.NextResponse.json({ error: message }, { status: 500 });
|
|
14008
14347
|
}
|
|
14009
14348
|
}
|
|
14010
|
-
return
|
|
14349
|
+
return import_server9.NextResponse.json({ error: "Method Not Allowed" }, { status: 405 });
|
|
14011
14350
|
};
|
|
14012
14351
|
}
|
|
14352
|
+
|
|
14353
|
+
// src/handlers/feedbackHandler.ts
|
|
14354
|
+
var import_server10 = require("next/server");
|
|
14013
14355
|
function createFeedbackHandler(configOrPlugin, options) {
|
|
14014
14356
|
const plugin = getOrCreatePlugin(configOrPlugin);
|
|
14015
14357
|
const storage = new DatabaseStorage(plugin.getConfig());
|
|
@@ -14025,45 +14367,48 @@ function createFeedbackHandler(configOrPlugin, options) {
|
|
|
14025
14367
|
const messageId = url.searchParams.get("messageId");
|
|
14026
14368
|
if (!messageId) {
|
|
14027
14369
|
const feedbackList = await storage.listFeedback(scope);
|
|
14028
|
-
return
|
|
14370
|
+
return import_server10.NextResponse.json({ feedback: feedbackList });
|
|
14029
14371
|
}
|
|
14030
14372
|
const feedback = await storage.getFeedback(messageId, scope);
|
|
14031
|
-
return
|
|
14373
|
+
return import_server10.NextResponse.json({ feedback });
|
|
14032
14374
|
} catch (err) {
|
|
14033
14375
|
const message = err instanceof Error ? err.message : "Failed to fetch feedback";
|
|
14034
|
-
return
|
|
14376
|
+
return import_server10.NextResponse.json({ error: message }, { status: 500 });
|
|
14035
14377
|
}
|
|
14036
14378
|
} else if (req.method === "POST") {
|
|
14037
14379
|
try {
|
|
14038
14380
|
const body = await req.json();
|
|
14039
14381
|
const { messageId, sessionId = "default", rating, comment } = body;
|
|
14040
14382
|
if (!messageId) {
|
|
14041
|
-
return
|
|
14383
|
+
return import_server10.NextResponse.json({ error: "messageId is required" }, { status: 400 });
|
|
14042
14384
|
}
|
|
14043
14385
|
if (!rating) {
|
|
14044
|
-
return
|
|
14386
|
+
return import_server10.NextResponse.json({ error: "rating is required" }, { status: 400 });
|
|
14045
14387
|
}
|
|
14046
14388
|
if (scope && scope.length > 0) {
|
|
14047
14389
|
const matches = scope.some(
|
|
14048
14390
|
(pid) => pid && (sessionId === pid || sessionId.startsWith(pid) || sessionId.includes(pid))
|
|
14049
14391
|
);
|
|
14050
14392
|
if (!matches) {
|
|
14051
|
-
return
|
|
14393
|
+
return import_server10.NextResponse.json(
|
|
14052
14394
|
{ error: "sessionId is outside the authorized project scope" },
|
|
14053
14395
|
{ status: 403 }
|
|
14054
14396
|
);
|
|
14055
14397
|
}
|
|
14056
14398
|
}
|
|
14057
14399
|
await storage.saveFeedback({ messageId, sessionId, rating, comment });
|
|
14058
|
-
return
|
|
14400
|
+
return import_server10.NextResponse.json({ success: true });
|
|
14059
14401
|
} catch (err) {
|
|
14060
14402
|
const message = err instanceof Error ? err.message : "Failed to save feedback";
|
|
14061
|
-
return
|
|
14403
|
+
return import_server10.NextResponse.json({ error: message }, { status: 500 });
|
|
14062
14404
|
}
|
|
14063
14405
|
}
|
|
14064
|
-
return
|
|
14406
|
+
return import_server10.NextResponse.json({ error: "Method Not Allowed" }, { status: 405 });
|
|
14065
14407
|
};
|
|
14066
14408
|
}
|
|
14409
|
+
|
|
14410
|
+
// src/handlers/sessionsHandler.ts
|
|
14411
|
+
var import_server11 = require("next/server");
|
|
14067
14412
|
function createSessionsHandler(configOrPlugin, options) {
|
|
14068
14413
|
const plugin = getOrCreatePlugin(configOrPlugin);
|
|
14069
14414
|
const storage = new DatabaseStorage(plugin.getConfig());
|
|
@@ -14077,13 +14422,16 @@ function createSessionsHandler(configOrPlugin, options) {
|
|
|
14077
14422
|
try {
|
|
14078
14423
|
const scope = req && onResolveScope ? await onResolveScope(req) : void 0;
|
|
14079
14424
|
const sessions = await storage.listSessions(scope);
|
|
14080
|
-
return
|
|
14425
|
+
return import_server11.NextResponse.json({ sessions });
|
|
14081
14426
|
} catch (err) {
|
|
14082
14427
|
const message = err instanceof Error ? err.message : "Failed to list sessions";
|
|
14083
|
-
return
|
|
14428
|
+
return import_server11.NextResponse.json({ error: message }, { status: 500 });
|
|
14084
14429
|
}
|
|
14085
14430
|
};
|
|
14086
14431
|
}
|
|
14432
|
+
|
|
14433
|
+
// src/handlers/ragHandler.ts
|
|
14434
|
+
var import_server12 = require("next/server");
|
|
14087
14435
|
function createRagHandler(configOrPlugin, options) {
|
|
14088
14436
|
const plugin = getOrCreatePlugin(configOrPlugin);
|
|
14089
14437
|
const onAuthorize = options == null ? void 0 : options.onAuthorize;
|
|
@@ -14119,7 +14467,7 @@ function createRagHandler(configOrPlugin, options) {
|
|
|
14119
14467
|
case "v1/license/validate":
|
|
14120
14468
|
return licenseHandler(req);
|
|
14121
14469
|
default:
|
|
14122
|
-
return
|
|
14470
|
+
return import_server12.NextResponse.json({ error: `Not Found: POST segment "${segment}" not supported.` }, { status: 404 });
|
|
14123
14471
|
}
|
|
14124
14472
|
}
|
|
14125
14473
|
async function routeGetRequest(req, segment) {
|
|
@@ -14135,7 +14483,7 @@ function createRagHandler(configOrPlugin, options) {
|
|
|
14135
14483
|
case "sessions":
|
|
14136
14484
|
return sessionsHandler(req);
|
|
14137
14485
|
default:
|
|
14138
|
-
return
|
|
14486
|
+
return import_server12.NextResponse.json({ error: `Method Not Allowed: GET segment "${segment}" not supported.` }, { status: 405 });
|
|
14139
14487
|
}
|
|
14140
14488
|
}
|
|
14141
14489
|
async function getSegment(req, context) {
|
|
@@ -14170,7 +14518,7 @@ function createRagHandler(configOrPlugin, options) {
|
|
|
14170
14518
|
return await routeGetRequest(req, segment);
|
|
14171
14519
|
} catch (err) {
|
|
14172
14520
|
const msg = err instanceof Error ? err.message : "GET Routing failed";
|
|
14173
|
-
return
|
|
14521
|
+
return import_server12.NextResponse.json({ error: msg }, { status: 500 });
|
|
14174
14522
|
}
|
|
14175
14523
|
},
|
|
14176
14524
|
POST: async (req, context) => {
|
|
@@ -14179,7 +14527,7 @@ function createRagHandler(configOrPlugin, options) {
|
|
|
14179
14527
|
return await routePostRequest(req, segment);
|
|
14180
14528
|
} catch (err) {
|
|
14181
14529
|
const msg = err instanceof Error ? err.message : "POST Routing failed";
|
|
14182
|
-
return
|
|
14530
|
+
return import_server12.NextResponse.json({ error: msg }, { status: 500 });
|
|
14183
14531
|
}
|
|
14184
14532
|
}
|
|
14185
14533
|
};
|
|
@@ -14228,11 +14576,11 @@ var _LicenseValidator = class _LicenseValidator {
|
|
|
14228
14576
|
* Primary validation entry-point for UI / hook consumers.
|
|
14229
14577
|
*
|
|
14230
14578
|
* Resolves the license key, consults the local success cache, performs a
|
|
14231
|
-
* server POST for authoritative status, and falls back to
|
|
14232
|
-
*
|
|
14579
|
+
* server POST for authoritative status, and falls back to `LicenseVerifier.verifyAsync()`
|
|
14580
|
+
* if the server can't be reached.
|
|
14233
14581
|
*
|
|
14234
14582
|
* @throws LicenseValidationError - When no key is provided OR both the
|
|
14235
|
-
* server call AND the
|
|
14583
|
+
* server call AND the fallback report an invalid/expired key.
|
|
14236
14584
|
* @throws SDKVersionUnsupportedError - When server reports forceUpgrade=true.
|
|
14237
14585
|
* @returns A `LicenseValidationResponse` that the UI can use to enable or
|
|
14238
14586
|
* lock the chat widget.
|
|
@@ -14281,6 +14629,8 @@ var _LicenseValidator = class _LicenseValidator {
|
|
|
14281
14629
|
this.cache.delete(effectiveLicenseKey);
|
|
14282
14630
|
}
|
|
14283
14631
|
}
|
|
14632
|
+
const existing = this.inflight.get(effectiveLicenseKey);
|
|
14633
|
+
if (existing) return existing;
|
|
14284
14634
|
const inflightPromise = this._doNetworkValidate(
|
|
14285
14635
|
effectiveLicenseKey,
|
|
14286
14636
|
effectiveProjectId,
|
|
@@ -14549,8 +14899,8 @@ var LicenseConfigResolver = class {
|
|
|
14549
14899
|
};
|
|
14550
14900
|
}
|
|
14551
14901
|
};
|
|
14552
|
-
LicenseConfigResolver.RETRIVORA_CONFIG_URL = "https://
|
|
14553
|
-
LicenseConfigResolver.RETRIVORA_CREDENTIALS_URL = "https://
|
|
14902
|
+
LicenseConfigResolver.RETRIVORA_CONFIG_URL = "https://llm.retrivora.com/api/v1/config";
|
|
14903
|
+
LicenseConfigResolver.RETRIVORA_CREDENTIALS_URL = "https://llm.retrivora.com/api/v1/credentials";
|
|
14554
14904
|
LicenseConfigResolver.CACHE_TTL_MS = 36e5;
|
|
14555
14905
|
// 1 hour
|
|
14556
14906
|
LicenseConfigResolver.configCache = /* @__PURE__ */ new Map();
|
|
@@ -14714,6 +15064,9 @@ var _TelemetryService = class _TelemetryService {
|
|
|
14714
15064
|
console.warn("[TelemetryService] Auto-flush error:", err.message);
|
|
14715
15065
|
});
|
|
14716
15066
|
}, this.flushIntervalMs);
|
|
15067
|
+
if (this.flushTimer && typeof this.flushTimer.unref === "function") {
|
|
15068
|
+
this.flushTimer.unref();
|
|
15069
|
+
}
|
|
14717
15070
|
}
|
|
14718
15071
|
/**
|
|
14719
15072
|
* Stop telemetry service and flush remaining events
|
|
@@ -14987,6 +15340,7 @@ async function createRetrivora(options) {
|
|
|
14987
15340
|
IntentClassifier,
|
|
14988
15341
|
LLMFactory,
|
|
14989
15342
|
LLM_PROFILES,
|
|
15343
|
+
LangChainAgent,
|
|
14990
15344
|
LicenseConfigResolver,
|
|
14991
15345
|
LicenseValidationError,
|
|
14992
15346
|
LicenseValidator,
|
|
@@ -15046,10 +15400,12 @@ async function createRetrivora(options) {
|
|
|
15046
15400
|
createHealthHandler,
|
|
15047
15401
|
createHistoryHandler,
|
|
15048
15402
|
createIngestHandler,
|
|
15403
|
+
createLicenseHandler,
|
|
15049
15404
|
createRagHandler,
|
|
15050
15405
|
createRetrivora,
|
|
15051
15406
|
createSessionsHandler,
|
|
15052
15407
|
createStreamHandler,
|
|
15408
|
+
createSuggestionsHandler,
|
|
15053
15409
|
createUploadHandler,
|
|
15054
15410
|
decideVisualization,
|
|
15055
15411
|
getRagConfig,
|
|
@@ -15063,6 +15419,8 @@ async function createRetrivora(options) {
|
|
|
15063
15419
|
sseErrorFrame,
|
|
15064
15420
|
sseFrame,
|
|
15065
15421
|
sseMetaFrame,
|
|
15422
|
+
sseObservabilityFrame,
|
|
15066
15423
|
sseTextFrame,
|
|
15424
|
+
sseUIFrame,
|
|
15067
15425
|
wrapError
|
|
15068
15426
|
});
|