@retrivora-ai/rag-engine 2.6.0 → 2.6.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. package/README.md +8 -6
  2. package/dist/ChromaDBProvider-NDA2IBOH.mjs +8 -0
  3. package/dist/{ILLMProvider-DUCL1r75.d.mts → ILLMProvider-CzXcTA6d.d.mts} +21 -3
  4. package/dist/{ILLMProvider-DUCL1r75.d.ts → ILLMProvider-CzXcTA6d.d.ts} +21 -3
  5. package/dist/MilvusProvider-H6GGL5FQ.mjs +8 -0
  6. package/dist/MongoDBProvider-KC3GF3FH.mjs +8 -0
  7. package/dist/MultiTablePostgresProvider-6LJBFZL2.mjs +9 -0
  8. package/dist/PineconeProvider-22RF2QPF.mjs +10 -0
  9. package/dist/PineconeProvider-HBBIXLT3.mjs +10 -0
  10. package/dist/PostgreSQLProvider-VWFL4OIP.mjs +8 -0
  11. package/dist/QdrantProvider-UU7RGXBI.mjs +8 -0
  12. package/dist/RedisProvider-MIIPSAAE.mjs +8 -0
  13. package/dist/SimpleGraphProvider-HJIS4S7Y.mjs +70 -0
  14. package/dist/UniversalVectorProvider-NO3L255G.mjs +9 -0
  15. package/dist/WeaviateProvider-YRZDZFAC.mjs +8 -0
  16. package/dist/chunk-23PFNEBF.mjs +107 -0
  17. package/dist/chunk-3E4GZGDX.mjs +10851 -0
  18. package/dist/chunk-3SQ4DODU.mjs +238 -0
  19. package/dist/chunk-AHMMUZ4W.mjs +152 -0
  20. package/dist/chunk-ASBNNEVI.mjs +81 -0
  21. package/dist/chunk-AT4TFUQN.mjs +219 -0
  22. package/dist/chunk-BEPLYQBS.mjs +89 -0
  23. package/dist/chunk-G7MJRFCO.mjs +340 -0
  24. package/dist/chunk-IHWKVAUR.mjs +112 -0
  25. package/dist/chunk-IMP6FUCY.mjs +30 -0
  26. package/dist/chunk-LFNV6DEL.mjs +281 -0
  27. package/dist/chunk-MAXMVO7B.mjs +211 -0
  28. package/dist/chunk-MGJ6V2IT.mjs +203 -0
  29. package/dist/chunk-N3TI27LG.mjs +334 -0
  30. package/dist/chunk-NWYE4Q7B.mjs +334 -0
  31. package/dist/chunk-OJMAT4EG.mjs +219 -0
  32. package/dist/chunk-QKZM3RSJ.mjs +10851 -0
  33. package/dist/chunk-QUY3E6HJ.mjs +168 -0
  34. package/dist/chunk-SG6XYLQE.mjs +53 -0
  35. package/dist/chunk-UT6SZZEC.mjs +69 -0
  36. package/dist/chunk-VW3U4MMX.mjs +157 -0
  37. package/dist/chunk-WOJCOTXI.mjs +281 -0
  38. package/dist/chunk-XP6NJMX4.mjs +168 -0
  39. package/dist/chunk-XYBKDJDW.mjs +696 -0
  40. package/dist/handlers/index.d.mts +2 -2
  41. package/dist/handlers/index.d.ts +2 -2
  42. package/dist/handlers/index.js +9736 -9354
  43. package/dist/handlers/index.mjs +24 -13686
  44. package/dist/{index-CsBcqng1.d.ts → index-BBbt5A2y.d.mts} +61 -47
  45. package/dist/{index-BVSHblli.d.mts → index-BST9Gzsv.d.mts} +8 -7
  46. package/dist/{index-D6Oq2zyc.d.mts → index-BZ_rR6V8.d.ts} +61 -47
  47. package/dist/{index-DGW0NNmR.d.ts → index-qLvtMUr-.d.ts} +8 -7
  48. package/dist/index.css +23 -0
  49. package/dist/index.d.mts +4 -4
  50. package/dist/index.d.ts +4 -4
  51. package/dist/index.js +278 -190
  52. package/dist/index.min.css +1 -1
  53. package/dist/index.mjs +218 -1558
  54. package/dist/server.d.mts +58 -12
  55. package/dist/server.d.ts +58 -12
  56. package/dist/server.js +968 -577
  57. package/dist/server.mjs +395 -14244
  58. package/package.json +1 -1
@@ -0,0 +1,238 @@
1
+ import {
2
+ BaseVectorProvider
3
+ } from "./chunk-IMP6FUCY.mjs";
4
+ import {
5
+ __spreadValues
6
+ } from "./chunk-UT6SZZEC.mjs";
7
+
8
+ // src/providers/vectordb/QdrantProvider.ts
9
+ import axios from "axios";
10
+ import crypto from "crypto";
11
+ var QdrantProvider = class extends BaseVectorProvider {
12
+ constructor(config) {
13
+ super(config);
14
+ this.schemaDiscovered = false;
15
+ const opts = config.options;
16
+ const baseUrl = opts.baseUrl;
17
+ if (!baseUrl) throw new Error("[QdrantProvider] baseUrl is required");
18
+ this.contentField = opts.contentField || "content";
19
+ this.metadataField = opts.metadataField || "metadata";
20
+ this.isFlatPayload = !!opts.flatPayload;
21
+ this.http = axios.create({
22
+ baseURL: baseUrl,
23
+ timeout: 15e3,
24
+ // 15s timeout for vector DB operations
25
+ headers: __spreadValues({
26
+ "Content-Type": "application/json"
27
+ }, opts.apiKey ? { "api-key": opts.apiKey } : {})
28
+ });
29
+ }
30
+ async initialize() {
31
+ if (this.schemaDiscovered) return;
32
+ await this.ping();
33
+ await this.ensureCollection();
34
+ const discoveredFields = await this.discoverSchema();
35
+ const opts = this.config.options;
36
+ const configFields = opts.filterableFields || [];
37
+ const allFields = [.../* @__PURE__ */ new Set([...discoveredFields, ...configFields])];
38
+ if (allFields.length > 0) {
39
+ await Promise.all(
40
+ allFields.map(async (fieldInfo) => {
41
+ const [fieldName, schemaType] = fieldInfo.split(":");
42
+ return this.ensureIndex(fieldName, schemaType || "keyword");
43
+ })
44
+ );
45
+ }
46
+ this.schemaDiscovered = true;
47
+ }
48
+ /**
49
+ * Samples points from the collection to discover available payload fields.
50
+ */
51
+ async discoverSchema() {
52
+ var _a;
53
+ try {
54
+ const { data } = await this.http.post(`/collections/${this.indexName}/points/scroll`, {
55
+ limit: 20,
56
+ with_payload: true,
57
+ with_vector: false
58
+ });
59
+ const points = ((_a = data.result) == null ? void 0 : _a.points) || [];
60
+ const keys = /* @__PURE__ */ new Set();
61
+ for (const point of points) {
62
+ const payload = point.payload || {};
63
+ Object.keys(payload).forEach((k) => {
64
+ if (k !== "namespace" && k !== this.contentField && k !== this.metadataField) {
65
+ keys.add(k);
66
+ }
67
+ });
68
+ if (!this.isFlatPayload && payload[this.metadataField]) {
69
+ const meta = payload[this.metadataField];
70
+ if (typeof meta === "object" && meta !== null) {
71
+ Object.keys(meta).forEach((k) => keys.add(k));
72
+ }
73
+ }
74
+ }
75
+ return Array.from(keys);
76
+ } catch (err) {
77
+ console.warn(`[QdrantProvider] \u26A0\uFE0F Schema discovery failed:`, err instanceof Error ? err.message : String(err));
78
+ return [];
79
+ }
80
+ }
81
+ /**
82
+ * Ensures the collection exists. Creates it if missing.
83
+ */
84
+ async ensureCollection() {
85
+ var _a;
86
+ try {
87
+ await this.http.get(`/collections/${this.indexName}`);
88
+ } catch (err) {
89
+ if (axios.isAxiosError(err) && ((_a = err.response) == null ? void 0 : _a.status) === 404) {
90
+ const opts = this.config.options;
91
+ const dimensionsForCreate = opts.dimensions || 1536;
92
+ await this.http.put(`/collections/${this.indexName}`, {
93
+ vectors: {
94
+ size: dimensionsForCreate,
95
+ distance: "Cosine"
96
+ }
97
+ });
98
+ } else {
99
+ throw err;
100
+ }
101
+ }
102
+ }
103
+ /**
104
+ * Ensures that a payload field has an index.
105
+ */
106
+ async ensureIndex(fieldName, schema = "keyword") {
107
+ var _a;
108
+ let fullPath = fieldName;
109
+ if (!this.isFlatPayload && !fieldName.includes(".") && fieldName !== "namespace" && fieldName !== this.contentField) {
110
+ fullPath = `${this.metadataField}.${fieldName}`;
111
+ }
112
+ try {
113
+ await this.http.put(`/collections/${this.indexName}/index`, {
114
+ field_name: fullPath,
115
+ field_schema: schema
116
+ });
117
+ } catch (err) {
118
+ let status;
119
+ if (axios.isAxiosError(err)) status = (_a = err.response) == null ? void 0 : _a.status;
120
+ if (status === 409) return;
121
+ console.warn(`[QdrantProvider] \u26A0\uFE0F Could not ensure index for "${fullPath}":`, err instanceof Error ? err.message : String(err));
122
+ }
123
+ }
124
+ async upsert(doc, namespace) {
125
+ await this.batchUpsert([doc], namespace);
126
+ }
127
+ async batchUpsert(docs, namespace) {
128
+ const payload = {
129
+ points: docs.map((doc) => {
130
+ const pointPayload = __spreadValues({
131
+ [this.contentField]: doc.content
132
+ }, namespace ? { namespace } : {});
133
+ if (this.isFlatPayload) {
134
+ Object.assign(pointPayload, doc.metadata || {});
135
+ } else {
136
+ pointPayload[this.metadataField] = doc.metadata || {};
137
+ }
138
+ return {
139
+ id: this.normalizeId(doc.id),
140
+ vector: doc.vector,
141
+ payload: pointPayload
142
+ };
143
+ })
144
+ };
145
+ await this.http.put(`/collections/${this.indexName}/points`, payload);
146
+ }
147
+ async query(vector, topK, namespace, _filter) {
148
+ var _a;
149
+ const must = [];
150
+ if (namespace) {
151
+ must.push({ key: "namespace", match: { value: namespace } });
152
+ }
153
+ const sanitizedFilter = this.sanitizeFilter(_filter);
154
+ for (const [key, value] of Object.entries(sanitizedFilter)) {
155
+ let filterKey = key;
156
+ if (!this.isFlatPayload && !key.includes(".") && key !== "namespace" && key !== this.contentField) {
157
+ filterKey = `${this.metadataField}.${key}`;
158
+ }
159
+ if (Array.isArray(value)) {
160
+ must.push({ key: filterKey, match: { any: value } });
161
+ } else {
162
+ must.push({ key: filterKey, match: { value } });
163
+ }
164
+ }
165
+ const payload = {
166
+ vector,
167
+ limit: topK,
168
+ with_payload: true,
169
+ params: {
170
+ hnsw_ef: ((_a = this.config.options) == null ? void 0 : _a.efSearch) || Math.max(topK * 20, 128),
171
+ exact: false
172
+ },
173
+ filter: must.length > 0 ? { must } : void 0
174
+ };
175
+ const { data } = await this.http.post(`/collections/${this.indexName}/points/search`, payload);
176
+ const results = (data.result || []).map((res) => {
177
+ const p = res.payload || {};
178
+ let content = p[this.contentField] || "";
179
+ if (!content) {
180
+ const stringFields = Object.entries(p).filter(([, v]) => typeof v === "string").map(([k, v]) => ({ key: k, val: v }));
181
+ if (stringFields.length > 0) {
182
+ const bestMatch = stringFields.sort((a, b) => b.val.length - a.val.length)[0];
183
+ content = bestMatch.val;
184
+ } else {
185
+ content = JSON.stringify(p);
186
+ }
187
+ }
188
+ let metadata = {};
189
+ if (this.isFlatPayload) {
190
+ metadata = __spreadValues({}, p);
191
+ delete metadata[this.contentField];
192
+ delete metadata["namespace"];
193
+ } else {
194
+ metadata = p[this.metadataField] || p;
195
+ }
196
+ return {
197
+ id: res.id,
198
+ score: res.score,
199
+ content,
200
+ metadata
201
+ };
202
+ });
203
+ return results;
204
+ }
205
+ async delete(id) {
206
+ await this.http.post(`/collections/${this.indexName}/points/delete`, {
207
+ points: [this.normalizeId(id)]
208
+ });
209
+ }
210
+ async deleteNamespace(_namespace) {
211
+ await this.http.post(`/collections/${this.indexName}/points/delete`, {
212
+ filter: {
213
+ must: [{ key: "namespace", match: { value: _namespace } }]
214
+ }
215
+ });
216
+ }
217
+ async ping() {
218
+ try {
219
+ await this.http.get("/healthz");
220
+ return true;
221
+ } catch (e) {
222
+ return false;
223
+ }
224
+ }
225
+ normalizeId(id) {
226
+ if (typeof id === "number") return id;
227
+ const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
228
+ if (uuidRegex.test(id)) return id.toLowerCase();
229
+ const hash = crypto.createHash("md5").update(id).digest("hex");
230
+ return `${hash.slice(0, 8)}-${hash.slice(8, 12)}-${hash.slice(12, 16)}-${hash.slice(16, 20)}-${hash.slice(20, 32)}`;
231
+ }
232
+ async disconnect() {
233
+ }
234
+ };
235
+
236
+ export {
237
+ QdrantProvider
238
+ };
@@ -0,0 +1,152 @@
1
+ import {
2
+ BaseVectorProvider
3
+ } from "./chunk-IMP6FUCY.mjs";
4
+ import {
5
+ __spreadProps,
6
+ __spreadValues
7
+ } from "./chunk-UT6SZZEC.mjs";
8
+
9
+ // src/providers/vectordb/WeaviateProvider.ts
10
+ import axios from "axios";
11
+ var WeaviateProvider = class extends BaseVectorProvider {
12
+ constructor(config) {
13
+ super(config);
14
+ const opts = config.options;
15
+ const baseUrl = opts.baseUrl || opts.url;
16
+ if (!baseUrl) throw new Error("[WeaviateProvider] options.baseUrl is required");
17
+ this.http = axios.create({
18
+ baseURL: baseUrl,
19
+ headers: __spreadValues(__spreadValues({
20
+ "Content-Type": "application/json"
21
+ }, opts.apiKey ? {
22
+ "Authorization": `Bearer ${opts.apiKey}`
23
+ } : {}), opts.openaiApiKey ? {
24
+ "X-OpenAI-Api-Key": opts.openaiApiKey
25
+ } : {})
26
+ });
27
+ }
28
+ async initialize() {
29
+ await this.ping();
30
+ }
31
+ async upsert(doc, namespace) {
32
+ const primitiveMetadata = this.extractPrimitiveMetadata(doc.metadata);
33
+ const payload = {
34
+ class: this.indexName,
35
+ id: doc.id,
36
+ vector: doc.vector,
37
+ properties: __spreadProps(__spreadValues({
38
+ content: doc.content,
39
+ metadata: JSON.stringify(doc.metadata || {})
40
+ }, primitiveMetadata), {
41
+ namespace: namespace || ""
42
+ })
43
+ };
44
+ await this.http.post("/v1/objects", payload);
45
+ }
46
+ async batchUpsert(docs, namespace) {
47
+ const payload = {
48
+ objects: docs.map((doc) => ({
49
+ class: this.indexName,
50
+ id: doc.id,
51
+ vector: doc.vector,
52
+ properties: __spreadProps(__spreadValues({
53
+ content: doc.content,
54
+ metadata: JSON.stringify(doc.metadata || {})
55
+ }, this.extractPrimitiveMetadata(doc.metadata)), {
56
+ namespace: namespace || ""
57
+ })
58
+ }))
59
+ };
60
+ await this.http.post("/v1/batch/objects", payload);
61
+ }
62
+ async query(vector, topK, namespace, _filter) {
63
+ var _a, _b;
64
+ const queryText = _filter == null ? void 0 : _filter.queryText;
65
+ const sanitizedFilter = this.sanitizeFilter(_filter);
66
+ const searchParams = queryText ? `hybrid: { query: ${JSON.stringify(queryText)}, alpha: 0.5 }` : `nearVector: { vector: ${JSON.stringify(vector)} }`;
67
+ const where = this.buildWhereFilter(namespace, sanitizedFilter);
68
+ const graphqlQuery = {
69
+ query: `
70
+ {
71
+ Get {
72
+ ${this.indexName}(
73
+ ${searchParams}
74
+ limit: ${topK}
75
+ ${where ? `where: ${where}` : ""}
76
+ ) {
77
+ content
78
+ metadata
79
+ _additional {
80
+ id
81
+ distance
82
+ }
83
+ }
84
+ }
85
+ }
86
+ `
87
+ };
88
+ const { data } = await this.http.post("/v1/graphql", graphqlQuery);
89
+ const results = ((_b = (_a = data.data) == null ? void 0 : _a.Get) == null ? void 0 : _b[this.indexName]) || [];
90
+ return results.map((res) => ({
91
+ id: res["_additional"].id,
92
+ score: 1 - res["_additional"].distance,
93
+ content: res["content"],
94
+ metadata: res["metadata"] ? JSON.parse(res["metadata"]) : {}
95
+ }));
96
+ }
97
+ async delete(id, _namespace) {
98
+ await this.http.delete(`/v1/objects/${this.indexName}/${id}`);
99
+ }
100
+ async deleteNamespace(namespace) {
101
+ await this.http.delete("/v1/batch/objects", {
102
+ data: {
103
+ match: {
104
+ class: this.indexName,
105
+ where: { path: ["namespace"], operator: "Equal", valueString: namespace }
106
+ }
107
+ }
108
+ });
109
+ }
110
+ async ping() {
111
+ try {
112
+ await this.http.get("/v1/.well-known/live");
113
+ return true;
114
+ } catch (e) {
115
+ return false;
116
+ }
117
+ }
118
+ async disconnect() {
119
+ }
120
+ extractPrimitiveMetadata(metadata) {
121
+ const result = {};
122
+ Object.entries(metadata || {}).forEach(([key, value]) => {
123
+ if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
124
+ result[key] = value;
125
+ }
126
+ });
127
+ return result;
128
+ }
129
+ buildWhereFilter(namespace, filter) {
130
+ const operands = [];
131
+ if (namespace) operands.push(this.weaviateOperand("namespace", namespace));
132
+ Object.entries(filter || {}).forEach(([key, value]) => {
133
+ if (key === "namespace") return;
134
+ if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
135
+ operands.push(this.weaviateOperand(key, value));
136
+ }
137
+ });
138
+ if (operands.length === 0) return void 0;
139
+ if (operands.length === 1) return operands[0];
140
+ return `{ operator: And, operands: [${operands.join(", ")}] }`;
141
+ }
142
+ weaviateOperand(key, value) {
143
+ const path = `path: [${JSON.stringify(key)}], operator: Equal`;
144
+ if (typeof value === "number") return `{ ${path}, valueNumber: ${value} }`;
145
+ if (typeof value === "boolean") return `{ ${path}, valueBoolean: ${value} }`;
146
+ return `{ ${path}, valueString: ${JSON.stringify(value)} }`;
147
+ }
148
+ };
149
+
150
+ export {
151
+ WeaviateProvider
152
+ };
@@ -0,0 +1,81 @@
1
+ import {
2
+ BaseVectorProvider
3
+ } from "./chunk-IMP6FUCY.mjs";
4
+ import {
5
+ __spreadValues
6
+ } from "./chunk-UT6SZZEC.mjs";
7
+
8
+ // src/providers/vectordb/RedisProvider.ts
9
+ import axios from "axios";
10
+ var RedisProvider = class extends BaseVectorProvider {
11
+ constructor(config) {
12
+ super(config);
13
+ const opts = config.options;
14
+ const baseUrl = opts.baseUrl || opts.url || (opts.host ? `http://${opts.host}:${opts.port || 6379}` : void 0);
15
+ if (!baseUrl) throw new Error("[RedisProvider] options.baseUrl is required");
16
+ this.http = axios.create({
17
+ baseURL: baseUrl,
18
+ headers: __spreadValues({
19
+ "Content-Type": "application/json"
20
+ }, opts.apiKey ? { Authorization: `Bearer ${opts.apiKey}` } : {})
21
+ });
22
+ }
23
+ async initialize() {
24
+ await this.ping();
25
+ }
26
+ async upsert(doc, namespace) {
27
+ await this.batchUpsert([doc], namespace);
28
+ }
29
+ async batchUpsert(docs, namespace) {
30
+ const payload = docs.map((doc) => __spreadValues({
31
+ id: String(doc.id),
32
+ vector: doc.vector,
33
+ data: doc.content,
34
+ metadata: doc.metadata || {}
35
+ }, namespace ? { namespace } : {}));
36
+ await this.http.post("/upsert", payload);
37
+ }
38
+ async query(vector, topK, namespace, _filter) {
39
+ const payload = {
40
+ index: this.indexName,
41
+ vector,
42
+ topK,
43
+ namespace
44
+ };
45
+ const { data } = await this.http.post("/search", payload);
46
+ return (data.results || []).map((res) => ({
47
+ id: res["id"],
48
+ score: res["score"],
49
+ content: res["content"],
50
+ metadata: res["metadata"] || {}
51
+ }));
52
+ }
53
+ async delete(id, namespace) {
54
+ await this.http.post("/delete", __spreadValues({
55
+ ids: [id]
56
+ }, namespace ? { namespace } : {}));
57
+ }
58
+ async deleteNamespace(namespace) {
59
+ if (!namespace) {
60
+ throw new Error("[RedisProvider] namespace is required for deleteNamespace");
61
+ }
62
+ await this.http.post("/reset", { namespace });
63
+ }
64
+ /**
65
+ * Check reachability of the Upstash Vector API.
66
+ */
67
+ async ping() {
68
+ try {
69
+ await this.http.get("/");
70
+ return true;
71
+ } catch (e) {
72
+ return false;
73
+ }
74
+ }
75
+ async disconnect() {
76
+ }
77
+ };
78
+
79
+ export {
80
+ RedisProvider
81
+ };
@@ -0,0 +1,219 @@
1
+ import {
2
+ LicenseValidationError,
3
+ LicenseVerifier,
4
+ SDKVersionUnsupportedError
5
+ } from "./chunk-XYBKDJDW.mjs";
6
+ import {
7
+ SDK_VERSION
8
+ } from "./chunk-XP6NJMX4.mjs";
9
+ import {
10
+ __spreadValues
11
+ } from "./chunk-UT6SZZEC.mjs";
12
+
13
+ // src/core/LicenseValidator.ts
14
+ function canUseLocalValidationFallback(baseUrl, status) {
15
+ const explicitFallback = typeof process !== "undefined" && process.env.RETRIVORA_ALLOW_LOCAL_LICENSE_FALLBACK === "true";
16
+ const isLocalSdkRoute = baseUrl.startsWith("/api/retrivora") || baseUrl.includes("/api/retrivora");
17
+ if (status !== void 0) {
18
+ return status === 404 && (explicitFallback || isLocalSdkRoute);
19
+ }
20
+ return explicitFallback || isLocalSdkRoute;
21
+ }
22
+ var _LicenseValidator = class _LicenseValidator {
23
+ // 5-minute TTL for successful validation only
24
+ constructor() {
25
+ this.cache = /* @__PURE__ */ new Map();
26
+ /** In-flight deduplication: concurrent calls for same key share one network request */
27
+ this.inflight = /* @__PURE__ */ new Map();
28
+ }
29
+ /** @returns Singleton instance of the validator (shared cache across all consumers). */
30
+ static getInstance() {
31
+ if (!_LicenseValidator.instance) {
32
+ _LicenseValidator.instance = new _LicenseValidator();
33
+ }
34
+ return _LicenseValidator.instance;
35
+ }
36
+ /**
37
+ * Purge entries from the local success cache.
38
+ *
39
+ * @param licenseKey - Optional. If provided, only that key's cache entry is
40
+ * removed. If omitted the entire cache is cleared.
41
+ * Callers use this after any 401/403 to ensure the next
42
+ * validate() call re-validates against the server.
43
+ */
44
+ purgeCache(licenseKey) {
45
+ if (licenseKey) {
46
+ this.cache.delete(licenseKey);
47
+ } else {
48
+ this.cache.clear();
49
+ }
50
+ }
51
+ /**
52
+ * Primary validation entry-point for UI / hook consumers.
53
+ *
54
+ * Resolves the license key, consults the local success cache, performs a
55
+ * server POST for authoritative status, and falls back to `LicenseVerifier.verifyAsync()`
56
+ * if the server can't be reached.
57
+ *
58
+ * @throws LicenseValidationError - When no key is provided OR both the
59
+ * server call AND the fallback report an invalid/expired key.
60
+ * @throws SDKVersionUnsupportedError - When server reports forceUpgrade=true.
61
+ * @returns A `LicenseValidationResponse` that the UI can use to enable or
62
+ * lock the chat widget.
63
+ */
64
+ async validate(request) {
65
+ const {
66
+ licenseKey,
67
+ projectId,
68
+ sdkVersion = SDK_VERSION,
69
+ sdk = "@retrivora-ai/rag-engine",
70
+ platform = typeof window !== "undefined" ? "browser" : "node",
71
+ retrivoraApiBase,
72
+ headers: customHeaders
73
+ } = request;
74
+ const effectiveProjectId = projectId || (typeof process !== "undefined" ? process.env.NEXT_PUBLIC_RETRIVORA_PROJECT_ID || process.env.RETRIVORA_PROJECT_ID : "") || "";
75
+ const getHeader = (name) => {
76
+ if (!customHeaders) return void 0;
77
+ const target = name.toLowerCase();
78
+ for (const [k, v] of Object.entries(customHeaders)) {
79
+ if (k.toLowerCase() === target) return v;
80
+ }
81
+ return void 0;
82
+ };
83
+ const authHeader = getHeader("authorization");
84
+ let authHeaderKey = "";
85
+ if (authHeader && authHeader.toLowerCase().startsWith("bearer ")) {
86
+ authHeaderKey = authHeader.replace(/^Bearer\s+/i, "");
87
+ if (authHeaderKey) {
88
+ console.warn(
89
+ '[Retrivora SDK] DEPRECATION WARNING (SEC-7): Repurposing the standard "Authorization: Bearer" header to carry the license key is deprecated and will be removed in a future release. Please use the custom "x-license-key" header instead.'
90
+ );
91
+ }
92
+ }
93
+ const rawKey = licenseKey || getHeader("x-license-key") || authHeaderKey || (typeof process !== "undefined" ? process.env.NEXT_PUBLIC_RETRIVORA_LICENSE_KEY || process.env.RETRIVORA_LICENSE_KEY : "") || "";
94
+ const effectiveLicenseKey = (rawKey || "").trim().replace(/^["']|["']$/g, "").trim();
95
+ if (!effectiveLicenseKey) {
96
+ this.purgeCache(effectiveLicenseKey);
97
+ throw new LicenseValidationError("Missing RETRIVORA_LICENSE_KEY in request. Set NEXT_PUBLIC_RETRIVORA_LICENSE_KEY in your environment or pass licenseKey as a prop.");
98
+ }
99
+ const cached = this.cache.get(effectiveLicenseKey);
100
+ if (cached) {
101
+ const isFresh = Date.now() - cached.timestamp < _LicenseValidator.SUCCESS_CACHE_TTL_MS;
102
+ if (isFresh && cached.response.valid && cached.response.licenseStatus === "ACTIVE" && !cached.response.forceUpgrade) {
103
+ return cached.response;
104
+ } else {
105
+ this.cache.delete(effectiveLicenseKey);
106
+ }
107
+ }
108
+ const existing = this.inflight.get(effectiveLicenseKey);
109
+ if (existing) return existing;
110
+ const inflightPromise = this._doNetworkValidate(
111
+ effectiveLicenseKey,
112
+ effectiveProjectId,
113
+ sdkVersion,
114
+ sdk,
115
+ platform,
116
+ retrivoraApiBase,
117
+ customHeaders
118
+ );
119
+ this.inflight.set(effectiveLicenseKey, inflightPromise);
120
+ try {
121
+ return await inflightPromise;
122
+ } finally {
123
+ this.inflight.delete(effectiveLicenseKey);
124
+ }
125
+ }
126
+ /** Performs the actual network request. Called once; result shared by in-flight dedup. */
127
+ async _doNetworkValidate(effectiveLicenseKey, effectiveProjectId, sdkVersion, sdk, platform, retrivoraApiBase, customHeaders) {
128
+ const base = retrivoraApiBase || (typeof process !== "undefined" ? process.env.NEXT_PUBLIC_RETRIVORA_API_BASE || process.env.RETRIVORA_API_BASE : "") || "https://www.retrivora.com/api";
129
+ const validateUrl = `${base.replace(/\/$/, "")}/v1/license/validate`;
130
+ let res = null;
131
+ try {
132
+ res = await fetch(validateUrl, {
133
+ method: "POST",
134
+ cache: "no-store",
135
+ headers: __spreadValues({
136
+ "Content-Type": "application/json",
137
+ "x-license-key": effectiveLicenseKey,
138
+ "x-sdk-version": sdkVersion
139
+ }, customHeaders || {}),
140
+ body: JSON.stringify({
141
+ licenseKey: effectiveLicenseKey,
142
+ projectId: effectiveProjectId,
143
+ sdkVersion,
144
+ sdk,
145
+ platform
146
+ })
147
+ });
148
+ } catch (err) {
149
+ try {
150
+ if (!canUseLocalValidationFallback(base)) {
151
+ throw err;
152
+ }
153
+ const payload = await LicenseVerifier.verifyAsync(effectiveLicenseKey, effectiveProjectId);
154
+ const fallbackResp = {
155
+ valid: true,
156
+ licenseStatus: "ACTIVE",
157
+ minimumSupportedVersion: "2.1.0",
158
+ latestVersion: SDK_VERSION,
159
+ forceUpgrade: false,
160
+ expiresAt: payload.expiresAt ? new Date(payload.expiresAt * 1e3).toISOString() : null,
161
+ message: "Local license verification active."
162
+ };
163
+ this.cache.set(effectiveLicenseKey, { response: fallbackResp, timestamp: Date.now() });
164
+ return fallbackResp;
165
+ } catch (e) {
166
+ this.purgeCache(effectiveLicenseKey);
167
+ throw new LicenseValidationError(`License validation request failed: ${(err == null ? void 0 : err.message) || "Network error"}`);
168
+ }
169
+ }
170
+ if (res && !res.ok && canUseLocalValidationFallback(base, res.status)) {
171
+ try {
172
+ const payload = await LicenseVerifier.verifyAsync(effectiveLicenseKey, effectiveProjectId);
173
+ const fallbackResp = {
174
+ valid: true,
175
+ licenseStatus: "ACTIVE",
176
+ minimumSupportedVersion: "2.1.0",
177
+ latestVersion: SDK_VERSION,
178
+ forceUpgrade: false,
179
+ expiresAt: payload.expiresAt ? new Date(payload.expiresAt * 1e3).toISOString() : null,
180
+ message: "Local license verification active."
181
+ };
182
+ this.cache.set(effectiveLicenseKey, { response: fallbackResp, timestamp: Date.now() });
183
+ return fallbackResp;
184
+ } catch (e) {
185
+ }
186
+ }
187
+ const data = await res.json().catch(() => ({
188
+ valid: false,
189
+ licenseStatus: "REVOKED",
190
+ minimumSupportedVersion: "2.1.0",
191
+ latestVersion: SDK_VERSION,
192
+ forceUpgrade: false,
193
+ expiresAt: null,
194
+ message: "Failed to parse license validation response."
195
+ }));
196
+ if (!res.ok || !data.valid || data.licenseStatus !== "ACTIVE" || data.forceUpgrade) {
197
+ this.purgeCache(effectiveLicenseKey);
198
+ if (data.forceUpgrade || res.status === 426) {
199
+ throw new SDKVersionUnsupportedError(
200
+ data.message || `This SDK version (${sdkVersion}) is no longer supported. Upgrade to version ${data.latestVersion || "2.2.6"} or later.`,
201
+ data
202
+ );
203
+ }
204
+ const errMsg = data.message || `License validation failed with status: ${data.licenseStatus}. Access denied.`;
205
+ throw new LicenseValidationError(errMsg, data);
206
+ }
207
+ this.cache.set(effectiveLicenseKey, {
208
+ response: data,
209
+ timestamp: Date.now()
210
+ });
211
+ return data;
212
+ }
213
+ };
214
+ _LicenseValidator.SUCCESS_CACHE_TTL_MS = 5 * 60 * 1e3;
215
+ var LicenseValidator = _LicenseValidator;
216
+
217
+ export {
218
+ LicenseValidator
219
+ };