@retrivora-ai/rag-engine 2.3.0 → 2.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{LicenseValidator-CENvo9o2.d.mts → BatchProcessor-7yV-UCHW.d.mts} +142 -3
- package/dist/{LicenseValidator-CsjJp2PP.d.ts → BatchProcessor-BfzuU4cK.d.ts} +142 -3
- package/dist/handlers/index.d.mts +1 -1
- package/dist/handlers/index.d.ts +1 -1
- package/dist/handlers/index.js +881 -331
- package/dist/handlers/index.mjs +881 -331
- package/dist/index-DR_O_B-W.d.ts +394 -0
- package/dist/index-fnpaCuma.d.mts +394 -0
- package/dist/index.css +58 -0
- package/dist/index.d.mts +63 -3
- package/dist/index.d.ts +63 -3
- package/dist/index.js +454 -36
- package/dist/index.mjs +453 -38
- package/dist/server.d.mts +35 -73
- package/dist/server.d.ts +35 -73
- package/dist/server.js +911 -345
- package/dist/server.mjs +911 -345
- package/package.json +3 -2
- package/src/components/ChatWidget.tsx +149 -48
- package/src/components/ChatWindow.tsx +52 -9
- package/src/components/CodeViewer.tsx +1 -1
- package/src/core/BatchProcessor.ts +42 -4
- package/src/core/CircuitBreaker.ts +118 -0
- package/src/core/DatabaseStorage.ts +55 -24
- package/src/core/FreeTierLimitsGuard.ts +281 -0
- package/src/core/LangChainAgent.ts +0 -2
- package/src/core/LicenseValidator.ts +66 -2
- package/src/core/LicenseVerifier.ts +31 -3
- package/src/core/VectorPlugin.ts +30 -0
- package/src/handlers/index.ts +491 -36
- package/src/index.css +58 -0
- package/src/index.ts +4 -0
- package/src/providers/vectordb/MilvusProvider.ts +0 -1
- package/src/providers/vectordb/MongoDBProvider.ts +0 -1
- package/src/providers/vectordb/MultiTablePostgresProvider.ts +2 -2
- package/src/providers/vectordb/RedisProvider.ts +0 -1
- package/src/providers/vectordb/WeaviateProvider.ts +0 -1
- package/src/rag/LlamaIndexIngestor.ts +0 -1
- package/src/server.ts +1 -0
- package/dist/index-BPJ3KDYI.d.ts +0 -195
- package/dist/index-Dmq5lH0j.d.mts +0 -195
package/dist/handlers/index.mjs
CHANGED
|
@@ -150,7 +150,7 @@ var init_ConfigFetcher = __esm({
|
|
|
150
150
|
* Fetch full project configuration (vectorDb + embedding + llm) from Retrivora Control Plane.
|
|
151
151
|
*/
|
|
152
152
|
static async fetchRemoteConfig(projectId, licenseKey) {
|
|
153
|
-
var
|
|
153
|
+
var _a3, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k;
|
|
154
154
|
const cacheKey = `${projectId}:${licenseKey || ""}`;
|
|
155
155
|
const now = Date.now();
|
|
156
156
|
const cached = this.cache.get(cacheKey);
|
|
@@ -176,7 +176,7 @@ var init_ConfigFetcher = __esm({
|
|
|
176
176
|
});
|
|
177
177
|
if (res.ok) {
|
|
178
178
|
const data = await res.json();
|
|
179
|
-
if ((data == null ? void 0 : data.success) && ((
|
|
179
|
+
if ((data == null ? void 0 : data.success) && ((_a3 = data == null ? void 0 : data.vectorDb) == null ? void 0 : _a3.apiKey)) {
|
|
180
180
|
const fetchedProjectId = data.projectId || projectId;
|
|
181
181
|
const config = {
|
|
182
182
|
projectId: fetchedProjectId,
|
|
@@ -212,9 +212,9 @@ var init_ConfigFetcher = __esm({
|
|
|
212
212
|
}
|
|
213
213
|
/** Convenience: fetch only vector DB config (backwards compat). */
|
|
214
214
|
static async fetchRemoteVectorConfig(projectId, licenseKey) {
|
|
215
|
-
var
|
|
215
|
+
var _a3;
|
|
216
216
|
const config = await this.fetchRemoteConfig(projectId, licenseKey);
|
|
217
|
-
return (
|
|
217
|
+
return (_a3 = config == null ? void 0 : config.vectorDb) != null ? _a3 : null;
|
|
218
218
|
}
|
|
219
219
|
};
|
|
220
220
|
ConfigFetcher.cache = /* @__PURE__ */ new Map();
|
|
@@ -263,7 +263,7 @@ var init_PineconeProvider = __esm({
|
|
|
263
263
|
static getHealthChecker() {
|
|
264
264
|
return {
|
|
265
265
|
async check(config) {
|
|
266
|
-
var
|
|
266
|
+
var _a3, _b;
|
|
267
267
|
const opts = config.options || {};
|
|
268
268
|
const indexName = config.indexName;
|
|
269
269
|
const timestamp = Date.now();
|
|
@@ -271,7 +271,7 @@ var init_PineconeProvider = __esm({
|
|
|
271
271
|
const { Pinecone: Pinecone2 } = await import("@pinecone-database/pinecone");
|
|
272
272
|
const client = new Pinecone2({ apiKey: opts.apiKey });
|
|
273
273
|
const indexes = await client.listIndexes();
|
|
274
|
-
const indexNames = (_b = (
|
|
274
|
+
const indexNames = (_b = (_a3 = indexes.indexes) == null ? void 0 : _a3.map((i) => i.name)) != null ? _b : [];
|
|
275
275
|
if (!indexNames.includes(indexName)) {
|
|
276
276
|
return {
|
|
277
277
|
healthy: false,
|
|
@@ -298,7 +298,7 @@ var init_PineconeProvider = __esm({
|
|
|
298
298
|
};
|
|
299
299
|
}
|
|
300
300
|
async initialize() {
|
|
301
|
-
var
|
|
301
|
+
var _a3, _b;
|
|
302
302
|
if (this.client) return;
|
|
303
303
|
let key = this.apiKey || process.env.PINECONE_API_KEY || "";
|
|
304
304
|
if (!key) {
|
|
@@ -335,7 +335,7 @@ var init_PineconeProvider = __esm({
|
|
|
335
335
|
this.client = new Pinecone({ apiKey: key });
|
|
336
336
|
try {
|
|
337
337
|
const indexes = await this.client.listIndexes();
|
|
338
|
-
const names = (_b = (
|
|
338
|
+
const names = (_b = (_a3 = indexes.indexes) == null ? void 0 : _a3.map((i) => i.name)) != null ? _b : [];
|
|
339
339
|
if (!names.includes(this.indexName)) {
|
|
340
340
|
console.warn(`[PineconeProvider] Target index "${this.indexName}" not listed. Proceeding with configured index name.`);
|
|
341
341
|
}
|
|
@@ -354,13 +354,13 @@ var init_PineconeProvider = __esm({
|
|
|
354
354
|
return namespace ? idx.namespace(namespace) : idx.namespace("");
|
|
355
355
|
}
|
|
356
356
|
async upsert(doc, namespace) {
|
|
357
|
-
var
|
|
357
|
+
var _a3;
|
|
358
358
|
const targetIdx = await this.getActiveIndex(namespace);
|
|
359
359
|
await targetIdx.upsert({
|
|
360
360
|
records: [{
|
|
361
361
|
id: String(doc.id),
|
|
362
362
|
values: doc.vector,
|
|
363
|
-
metadata: __spreadValues({ content: doc.content }, (
|
|
363
|
+
metadata: __spreadValues({ content: doc.content }, (_a3 = doc.metadata) != null ? _a3 : {})
|
|
364
364
|
}]
|
|
365
365
|
});
|
|
366
366
|
}
|
|
@@ -369,18 +369,18 @@ var init_PineconeProvider = __esm({
|
|
|
369
369
|
const BATCH = 100;
|
|
370
370
|
for (let i = 0; i < docs.length; i += BATCH) {
|
|
371
371
|
const records = docs.slice(i, i + BATCH).map((d) => {
|
|
372
|
-
var
|
|
372
|
+
var _a3;
|
|
373
373
|
return {
|
|
374
374
|
id: String(d.id),
|
|
375
375
|
values: d.vector,
|
|
376
|
-
metadata: __spreadValues({ content: d.content }, (
|
|
376
|
+
metadata: __spreadValues({ content: d.content }, (_a3 = d.metadata) != null ? _a3 : {})
|
|
377
377
|
};
|
|
378
378
|
});
|
|
379
379
|
await targetIdx.upsert({ records });
|
|
380
380
|
}
|
|
381
381
|
}
|
|
382
382
|
async query(vector, topK, namespace, filter) {
|
|
383
|
-
var
|
|
383
|
+
var _a3;
|
|
384
384
|
const targetIdx = await this.getActiveIndex(namespace);
|
|
385
385
|
const pineconeFilter = this.sanitizeFilter(filter);
|
|
386
386
|
const result = await targetIdx.query(__spreadValues({
|
|
@@ -388,11 +388,11 @@ var init_PineconeProvider = __esm({
|
|
|
388
388
|
topK,
|
|
389
389
|
includeMetadata: true
|
|
390
390
|
}, Object.keys(pineconeFilter).length > 0 ? { filter: pineconeFilter } : {}));
|
|
391
|
-
return ((
|
|
392
|
-
var
|
|
391
|
+
return ((_a3 = result.matches) != null ? _a3 : []).map((m) => {
|
|
392
|
+
var _a4, _b;
|
|
393
393
|
return {
|
|
394
394
|
id: m.id,
|
|
395
|
-
score: (
|
|
395
|
+
score: (_a4 = m.score) != null ? _a4 : 0,
|
|
396
396
|
content: ((_b = m.metadata) == null ? void 0 : _b.content) || "",
|
|
397
397
|
metadata: m.metadata
|
|
398
398
|
};
|
|
@@ -435,13 +435,13 @@ var init_PostgreSQLProvider = __esm({
|
|
|
435
435
|
init_BaseVectorProvider();
|
|
436
436
|
PostgreSQLProvider = class extends BaseVectorProvider {
|
|
437
437
|
constructor(config) {
|
|
438
|
-
var
|
|
438
|
+
var _a3;
|
|
439
439
|
super(config);
|
|
440
440
|
this.tableName = this.indexName.replace(/[^a-z0-9_]/gi, "_");
|
|
441
441
|
const opts = config.options;
|
|
442
442
|
if (!opts.connectionString) throw new Error("[PostgreSQLProvider] options.connectionString is required");
|
|
443
443
|
this.connectionString = opts.connectionString;
|
|
444
|
-
this.dimensions = (
|
|
444
|
+
this.dimensions = (_a3 = opts.dimensions) != null ? _a3 : 1536;
|
|
445
445
|
}
|
|
446
446
|
static getValidator() {
|
|
447
447
|
return {
|
|
@@ -522,7 +522,7 @@ var init_PostgreSQLProvider = __esm({
|
|
|
522
522
|
}
|
|
523
523
|
}
|
|
524
524
|
async upsert(doc, namespace = "") {
|
|
525
|
-
var
|
|
525
|
+
var _a3;
|
|
526
526
|
const vectorLiteral = `[${doc.vector.join(",")}]`;
|
|
527
527
|
await this.pool.query(
|
|
528
528
|
`INSERT INTO ${this.tableName} (id, namespace, content, metadata, embedding)
|
|
@@ -532,7 +532,7 @@ var init_PostgreSQLProvider = __esm({
|
|
|
532
532
|
content = EXCLUDED.content,
|
|
533
533
|
metadata = EXCLUDED.metadata,
|
|
534
534
|
embedding = EXCLUDED.embedding`,
|
|
535
|
-
[doc.id, namespace, doc.content, JSON.stringify((
|
|
535
|
+
[doc.id, namespace, doc.content, JSON.stringify((_a3 = doc.metadata) != null ? _a3 : {}), vectorLiteral]
|
|
536
536
|
);
|
|
537
537
|
}
|
|
538
538
|
async batchUpsert(docs, namespace = "") {
|
|
@@ -545,9 +545,9 @@ var init_PostgreSQLProvider = __esm({
|
|
|
545
545
|
const batch = docs.slice(i, i + BATCH_SIZE);
|
|
546
546
|
const values = [];
|
|
547
547
|
const valuePlaceholders = batch.map((doc, idx) => {
|
|
548
|
-
var
|
|
548
|
+
var _a3;
|
|
549
549
|
const offset = idx * 5;
|
|
550
|
-
values.push(doc.id, namespace, doc.content, JSON.stringify((
|
|
550
|
+
values.push(doc.id, namespace, doc.content, JSON.stringify((_a3 = doc.metadata) != null ? _a3 : {}), `[${doc.vector.join(",")}]`);
|
|
551
551
|
return `($${offset + 1}, $${offset + 2}, $${offset + 3}, $${offset + 4}, $${offset + 5}::vector)`;
|
|
552
552
|
}).join(", ");
|
|
553
553
|
const query = `
|
|
@@ -630,8 +630,8 @@ var init_PostgreSQLProvider = __esm({
|
|
|
630
630
|
|
|
631
631
|
// src/utils/synonyms.ts
|
|
632
632
|
function resolveMetadataValue(meta, uiKey) {
|
|
633
|
-
var
|
|
634
|
-
const synonyms = (
|
|
633
|
+
var _a3;
|
|
634
|
+
const synonyms = (_a3 = FIELD_SYNONYMS[uiKey]) != null ? _a3 : [];
|
|
635
635
|
const keys = Object.keys(meta);
|
|
636
636
|
const systemKeys = ["namespace", "filename", "filesize", "filetype", "docid", "uploadedat", "dimension", "chunkindex"];
|
|
637
637
|
let match = keys.find((k) => k.toLowerCase() === uiKey.toLowerCase());
|
|
@@ -718,14 +718,14 @@ var init_MultiTablePostgresProvider = __esm({
|
|
|
718
718
|
init_synonyms();
|
|
719
719
|
MultiTablePostgresProvider = class extends BaseVectorProvider {
|
|
720
720
|
constructor(config) {
|
|
721
|
-
var
|
|
721
|
+
var _a3, _b, _c;
|
|
722
722
|
super(config);
|
|
723
723
|
const opts = config.options || {};
|
|
724
724
|
if (!opts.connectionString) {
|
|
725
725
|
throw new Error("[MultiTablePostgresProvider] options.connectionString is required");
|
|
726
726
|
}
|
|
727
727
|
this.connectionString = opts.connectionString;
|
|
728
|
-
this.dimensions = (
|
|
728
|
+
this.dimensions = (_a3 = opts.dimensions) != null ? _a3 : 768;
|
|
729
729
|
this.tables = [];
|
|
730
730
|
const rawSearchFields = (_c = (_b = opts.searchFields) != null ? _b : process.env.VECTOR_DB_SEARCH_FIELDS) != null ? _c : ["name", "product_name", "productname", "title"];
|
|
731
731
|
this.searchFields = typeof rawSearchFields === "string" ? rawSearchFields.split(",").map((f) => f.trim()).filter(Boolean) : rawSearchFields;
|
|
@@ -783,11 +783,11 @@ var init_MultiTablePostgresProvider = __esm({
|
|
|
783
783
|
* Batch upsert documents by dynamically provisioning tables and columns based on CSV headers.
|
|
784
784
|
*/
|
|
785
785
|
async batchUpsert(docs, namespace = "") {
|
|
786
|
-
var
|
|
786
|
+
var _a3;
|
|
787
787
|
if (docs.length === 0) return;
|
|
788
788
|
const docsByFile = {};
|
|
789
789
|
for (const doc of docs) {
|
|
790
|
-
const fileName = ((
|
|
790
|
+
const fileName = ((_a3 = doc.metadata) == null ? void 0 : _a3.fileName) || this.uploadTable;
|
|
791
791
|
if (!docsByFile[fileName]) docsByFile[fileName] = [];
|
|
792
792
|
docsByFile[fileName].push(doc);
|
|
793
793
|
}
|
|
@@ -840,11 +840,11 @@ var init_MultiTablePostgresProvider = __esm({
|
|
|
840
840
|
const headerColumns = csvHeaders.map((h) => `"${h}"`).join(", ");
|
|
841
841
|
const allColumns = `id, ${headerColumns ? headerColumns + ", " : ""}namespace, content, metadata, embedding`;
|
|
842
842
|
const valuePlaceholders = batch.map((doc, idx) => {
|
|
843
|
-
var
|
|
843
|
+
var _a4, _b;
|
|
844
844
|
const offset = idx * (5 + csvHeaders.length);
|
|
845
845
|
values.push(doc.id);
|
|
846
846
|
for (const h of csvHeaders) {
|
|
847
|
-
values.push(String(((
|
|
847
|
+
values.push(String(((_a4 = doc.metadata) == null ? void 0 : _a4[h]) || ""));
|
|
848
848
|
}
|
|
849
849
|
values.push(namespace, doc.content, JSON.stringify((_b = doc.metadata) != null ? _b : {}), `[${doc.vector.join(",")}]`);
|
|
850
850
|
const docPlaceholders = [];
|
|
@@ -883,7 +883,7 @@ var init_MultiTablePostgresProvider = __esm({
|
|
|
883
883
|
* Query all configured tables and merge results, sorted by cosine similarity score.
|
|
884
884
|
*/
|
|
885
885
|
async query(vector, topK, _namespace, _filter) {
|
|
886
|
-
var
|
|
886
|
+
var _a3;
|
|
887
887
|
if (!this.pool) {
|
|
888
888
|
throw new Error("[MultiTablePostgresProvider] Provider not initialized. Call initialize() first.");
|
|
889
889
|
}
|
|
@@ -914,11 +914,11 @@ var init_MultiTablePostgresProvider = __esm({
|
|
|
914
914
|
const filterParams = [];
|
|
915
915
|
if (metadataFilters && Object.keys(metadataFilters).length > 0) {
|
|
916
916
|
const conditions = Object.entries(metadataFilters).map(([key, val]) => {
|
|
917
|
-
var
|
|
917
|
+
var _a5;
|
|
918
918
|
filterParams.push(String(val));
|
|
919
919
|
const baseOffset = queryText && dynamicKeywordQuery ? 2 : 1;
|
|
920
920
|
const paramIdx = baseOffset + filterParams.length;
|
|
921
|
-
const synonyms = (
|
|
921
|
+
const synonyms = (_a5 = FIELD_SYNONYMS[key]) != null ? _a5 : [];
|
|
922
922
|
const keysToCheck = [key, ...synonyms];
|
|
923
923
|
const coalesceExprs = keysToCheck.flatMap((k) => [
|
|
924
924
|
`metadata->>'${k}'`,
|
|
@@ -987,7 +987,7 @@ var init_MultiTablePostgresProvider = __esm({
|
|
|
987
987
|
}
|
|
988
988
|
const tableResults = [];
|
|
989
989
|
for (const row of result.rows) {
|
|
990
|
-
const
|
|
990
|
+
const _a4 = row, { hybrid_score, id } = _a4, rest = __objRest(_a4, ["hybrid_score", "id"]);
|
|
991
991
|
delete rest.embedding;
|
|
992
992
|
delete rest.vector_score;
|
|
993
993
|
delete rest.keyword_score;
|
|
@@ -1016,10 +1016,10 @@ var init_MultiTablePostgresProvider = __esm({
|
|
|
1016
1016
|
}
|
|
1017
1017
|
allResults.sort((a, b) => b.score - a.score);
|
|
1018
1018
|
if (allResults.length === 0) return [];
|
|
1019
|
-
const bestMatchTable = (
|
|
1019
|
+
const bestMatchTable = (_a3 = allResults[0].metadata) == null ? void 0 : _a3.source_table;
|
|
1020
1020
|
const finalSorted = allResults.filter((res) => {
|
|
1021
|
-
var
|
|
1022
|
-
return ((
|
|
1021
|
+
var _a4;
|
|
1022
|
+
return ((_a4 = res.metadata) == null ? void 0 : _a4.source_table) === bestMatchTable;
|
|
1023
1023
|
});
|
|
1024
1024
|
console.log(`[MultiTablePostgresProvider] --- Search Complete ---`);
|
|
1025
1025
|
console.log(`[MultiTablePostgresProvider] Final top match from "${bestMatchTable}" with score ${finalSorted[0].score.toFixed(4)}`);
|
|
@@ -1340,7 +1340,6 @@ var init_MilvusProvider = __esm({
|
|
|
1340
1340
|
metadata: res["metadata"] || {}
|
|
1341
1341
|
}));
|
|
1342
1342
|
}
|
|
1343
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
1344
1343
|
async delete(id, _namespace) {
|
|
1345
1344
|
await this.http.post("/v1/vector/delete", {
|
|
1346
1345
|
collectionName: this.indexName,
|
|
@@ -1424,14 +1423,14 @@ var init_QdrantProvider = __esm({
|
|
|
1424
1423
|
* Samples points from the collection to discover available payload fields.
|
|
1425
1424
|
*/
|
|
1426
1425
|
async discoverSchema() {
|
|
1427
|
-
var
|
|
1426
|
+
var _a3;
|
|
1428
1427
|
try {
|
|
1429
1428
|
const { data } = await this.http.post(`/collections/${this.indexName}/points/scroll`, {
|
|
1430
1429
|
limit: 20,
|
|
1431
1430
|
with_payload: true,
|
|
1432
1431
|
with_vector: false
|
|
1433
1432
|
});
|
|
1434
|
-
const points = ((
|
|
1433
|
+
const points = ((_a3 = data.result) == null ? void 0 : _a3.points) || [];
|
|
1435
1434
|
const keys = /* @__PURE__ */ new Set();
|
|
1436
1435
|
for (const point of points) {
|
|
1437
1436
|
const payload = point.payload || {};
|
|
@@ -1457,12 +1456,12 @@ var init_QdrantProvider = __esm({
|
|
|
1457
1456
|
* Ensures the collection exists. Creates it if missing.
|
|
1458
1457
|
*/
|
|
1459
1458
|
async ensureCollection() {
|
|
1460
|
-
var
|
|
1459
|
+
var _a3;
|
|
1461
1460
|
try {
|
|
1462
1461
|
await this.http.get(`/collections/${this.indexName}`);
|
|
1463
1462
|
console.log(`[QdrantProvider] \u2705 Collection "${this.indexName}" already exists.`);
|
|
1464
1463
|
} catch (err) {
|
|
1465
|
-
if (axios4.isAxiosError(err) && ((
|
|
1464
|
+
if (axios4.isAxiosError(err) && ((_a3 = err.response) == null ? void 0 : _a3.status) === 404) {
|
|
1466
1465
|
const opts = this.config.options;
|
|
1467
1466
|
const dimensionsForCreate = opts.dimensions || 1536;
|
|
1468
1467
|
console.log(`[QdrantProvider] \u23F3 Creating collection "${this.indexName}" (dimensions: ${dimensionsForCreate})...`);
|
|
@@ -1482,7 +1481,7 @@ var init_QdrantProvider = __esm({
|
|
|
1482
1481
|
* Ensures that a payload field has an index.
|
|
1483
1482
|
*/
|
|
1484
1483
|
async ensureIndex(fieldName, schema = "keyword") {
|
|
1485
|
-
var
|
|
1484
|
+
var _a3;
|
|
1486
1485
|
let fullPath = fieldName;
|
|
1487
1486
|
if (!this.isFlatPayload && !fieldName.includes(".") && fieldName !== "namespace" && fieldName !== this.contentField) {
|
|
1488
1487
|
fullPath = `${this.metadataField}.${fieldName}`;
|
|
@@ -1495,7 +1494,7 @@ var init_QdrantProvider = __esm({
|
|
|
1495
1494
|
console.log(`[QdrantProvider] \u2705 Ensured ${schema} index for "${fullPath}"`);
|
|
1496
1495
|
} catch (err) {
|
|
1497
1496
|
let status;
|
|
1498
|
-
if (axios4.isAxiosError(err)) status = (
|
|
1497
|
+
if (axios4.isAxiosError(err)) status = (_a3 = err.response) == null ? void 0 : _a3.status;
|
|
1499
1498
|
if (status === 409) return;
|
|
1500
1499
|
console.warn(`[QdrantProvider] \u26A0\uFE0F Could not ensure index for "${fullPath}":`, err instanceof Error ? err.message : String(err));
|
|
1501
1500
|
}
|
|
@@ -1524,7 +1523,7 @@ var init_QdrantProvider = __esm({
|
|
|
1524
1523
|
await this.http.put(`/collections/${this.indexName}/points`, payload);
|
|
1525
1524
|
}
|
|
1526
1525
|
async query(vector, topK, namespace, _filter) {
|
|
1527
|
-
var
|
|
1526
|
+
var _a3;
|
|
1528
1527
|
const must = [];
|
|
1529
1528
|
if (namespace) {
|
|
1530
1529
|
must.push({ key: "namespace", match: { value: namespace } });
|
|
@@ -1546,7 +1545,7 @@ var init_QdrantProvider = __esm({
|
|
|
1546
1545
|
limit: topK,
|
|
1547
1546
|
with_payload: true,
|
|
1548
1547
|
params: {
|
|
1549
|
-
hnsw_ef: ((
|
|
1548
|
+
hnsw_ef: ((_a3 = this.config.options) == null ? void 0 : _a3.efSearch) || Math.max(topK * 20, 128),
|
|
1550
1549
|
exact: false
|
|
1551
1550
|
},
|
|
1552
1551
|
filter: must.length > 0 ? { must } : void 0
|
|
@@ -1641,13 +1640,13 @@ var init_ChromaDBProvider = __esm({
|
|
|
1641
1640
|
* Get or create the ChromaDB collection.
|
|
1642
1641
|
*/
|
|
1643
1642
|
async initialize() {
|
|
1644
|
-
var
|
|
1643
|
+
var _a3;
|
|
1645
1644
|
try {
|
|
1646
1645
|
const { data } = await this.http.get(`/api/v1/collections/${this.indexName}`);
|
|
1647
1646
|
this.collectionId = data.id;
|
|
1648
1647
|
console.log(`[ChromaDBProvider] \u2705 Collection "${this.indexName}" found (id: ${this.collectionId})`);
|
|
1649
1648
|
} catch (err) {
|
|
1650
|
-
if (axios5.isAxiosError(err) && ((
|
|
1649
|
+
if (axios5.isAxiosError(err) && ((_a3 = err.response) == null ? void 0 : _a3.status) === 404) {
|
|
1651
1650
|
console.log(`[ChromaDBProvider] \u23F3 Collection "${this.indexName}" not found. Creating...`);
|
|
1652
1651
|
const { data } = await this.http.post("/api/v1/collections", {
|
|
1653
1652
|
name: this.indexName
|
|
@@ -1765,7 +1764,6 @@ var init_RedisProvider = __esm({
|
|
|
1765
1764
|
await this.upsert(doc, namespace);
|
|
1766
1765
|
}
|
|
1767
1766
|
}
|
|
1768
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
1769
1767
|
async query(vector, topK, namespace, _filter) {
|
|
1770
1768
|
const payload = {
|
|
1771
1769
|
index: this.indexName,
|
|
@@ -1868,7 +1866,7 @@ var init_WeaviateProvider = __esm({
|
|
|
1868
1866
|
await this.http.post("/v1/batch/objects", payload);
|
|
1869
1867
|
}
|
|
1870
1868
|
async query(vector, topK, namespace, _filter) {
|
|
1871
|
-
var
|
|
1869
|
+
var _a3, _b;
|
|
1872
1870
|
const queryText = _filter == null ? void 0 : _filter.queryText;
|
|
1873
1871
|
const sanitizedFilter = this.sanitizeFilter(_filter);
|
|
1874
1872
|
const searchParams = queryText ? `hybrid: { query: ${JSON.stringify(queryText)}, alpha: 0.5 }` : `nearVector: { vector: ${JSON.stringify(vector)} }`;
|
|
@@ -1894,7 +1892,7 @@ var init_WeaviateProvider = __esm({
|
|
|
1894
1892
|
`
|
|
1895
1893
|
};
|
|
1896
1894
|
const { data } = await this.http.post("/v1/graphql", graphqlQuery);
|
|
1897
|
-
const results = ((_b = (
|
|
1895
|
+
const results = ((_b = (_a3 = data.data) == null ? void 0 : _a3.Get) == null ? void 0 : _b[this.indexName]) || [];
|
|
1898
1896
|
return results.map((res) => ({
|
|
1899
1897
|
id: res["_additional"].id,
|
|
1900
1898
|
score: 1 - res["_additional"].distance,
|
|
@@ -1902,7 +1900,6 @@ var init_WeaviateProvider = __esm({
|
|
|
1902
1900
|
metadata: res["metadata"] ? JSON.parse(res["metadata"]) : {}
|
|
1903
1901
|
}));
|
|
1904
1902
|
}
|
|
1905
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
1906
1903
|
async delete(id, _namespace) {
|
|
1907
1904
|
await this.http.delete(`/v1/objects/${this.indexName}/${id}`);
|
|
1908
1905
|
}
|
|
@@ -1977,21 +1974,21 @@ var init_UniversalVectorProvider = __esm({
|
|
|
1977
1974
|
}
|
|
1978
1975
|
}
|
|
1979
1976
|
async initialize() {
|
|
1980
|
-
var
|
|
1977
|
+
var _a3;
|
|
1981
1978
|
this.http = axios8.create({
|
|
1982
1979
|
baseURL: this.opts.baseUrl,
|
|
1983
1980
|
headers: __spreadValues({
|
|
1984
1981
|
"Content-Type": "application/json"
|
|
1985
1982
|
}, this.opts.headers),
|
|
1986
|
-
timeout: (
|
|
1983
|
+
timeout: (_a3 = this.opts.timeout) != null ? _a3 : 3e4
|
|
1987
1984
|
});
|
|
1988
1985
|
if (!await this.ping()) {
|
|
1989
1986
|
throw new Error(`[UniversalVectorProvider] Failed to connect to ${this.opts.baseUrl}`);
|
|
1990
1987
|
}
|
|
1991
1988
|
}
|
|
1992
1989
|
async upsert(doc, namespace) {
|
|
1993
|
-
var
|
|
1994
|
-
const endpoint = (
|
|
1990
|
+
var _a3, _b, _c;
|
|
1991
|
+
const endpoint = (_a3 = this.opts.upsertEndpoint) != null ? _a3 : "/upsert";
|
|
1995
1992
|
const template = (_b = this.opts.upsertTemplate) != null ? _b : JSON.stringify({
|
|
1996
1993
|
id: "{{id}}",
|
|
1997
1994
|
vector: "{{vector}}",
|
|
@@ -2024,8 +2021,8 @@ var init_UniversalVectorProvider = __esm({
|
|
|
2024
2021
|
}
|
|
2025
2022
|
}
|
|
2026
2023
|
async query(vector, topK, namespace, filter) {
|
|
2027
|
-
var
|
|
2028
|
-
const endpoint = (
|
|
2024
|
+
var _a3, _b;
|
|
2025
|
+
const endpoint = (_a3 = this.opts.queryEndpoint) != null ? _a3 : "/query";
|
|
2029
2026
|
const template = (_b = this.opts.queryTemplate) != null ? _b : JSON.stringify({
|
|
2030
2027
|
vector: "{{vector}}",
|
|
2031
2028
|
limit: "{{topK}}",
|
|
@@ -2051,9 +2048,9 @@ var init_UniversalVectorProvider = __esm({
|
|
|
2051
2048
|
);
|
|
2052
2049
|
}
|
|
2053
2050
|
return results.map((item) => {
|
|
2054
|
-
var
|
|
2051
|
+
var _a4, _b2, _c, _d, _e, _f, _g2;
|
|
2055
2052
|
return {
|
|
2056
|
-
id: item[(
|
|
2053
|
+
id: item[(_a4 = this.opts.queryIdField) != null ? _a4 : "id"],
|
|
2057
2054
|
score: (_c = item[(_b2 = this.opts.queryScoreField) != null ? _b2 : "score"]) != null ? _c : 0,
|
|
2058
2055
|
content: (_e = item[(_d = this.opts.queryContentField) != null ? _d : "content"]) != null ? _e : "",
|
|
2059
2056
|
metadata: (_g2 = item[(_f = this.opts.queryMetadataField) != null ? _f : "metadata"]) != null ? _g2 : {}
|
|
@@ -2066,8 +2063,8 @@ var init_UniversalVectorProvider = __esm({
|
|
|
2066
2063
|
}
|
|
2067
2064
|
}
|
|
2068
2065
|
async delete(id, namespace) {
|
|
2069
|
-
var
|
|
2070
|
-
const endpoint = (
|
|
2066
|
+
var _a3, _b;
|
|
2067
|
+
const endpoint = (_a3 = this.opts.deleteEndpoint) != null ? _a3 : "/delete";
|
|
2071
2068
|
const template = (_b = this.opts.deleteTemplate) != null ? _b : JSON.stringify({
|
|
2072
2069
|
id: "{{id}}",
|
|
2073
2070
|
namespace: "{{namespace}}"
|
|
@@ -2085,8 +2082,8 @@ var init_UniversalVectorProvider = __esm({
|
|
|
2085
2082
|
}
|
|
2086
2083
|
}
|
|
2087
2084
|
async deleteNamespace(namespace) {
|
|
2088
|
-
var
|
|
2089
|
-
const endpoint = (
|
|
2085
|
+
var _a3;
|
|
2086
|
+
const endpoint = (_a3 = this.opts.deleteNamespaceEndpoint) != null ? _a3 : "/delete-namespace";
|
|
2090
2087
|
try {
|
|
2091
2088
|
await this.http.post(endpoint, { namespace });
|
|
2092
2089
|
} catch (error) {
|
|
@@ -2096,9 +2093,9 @@ var init_UniversalVectorProvider = __esm({
|
|
|
2096
2093
|
}
|
|
2097
2094
|
}
|
|
2098
2095
|
async ping() {
|
|
2099
|
-
var
|
|
2096
|
+
var _a3;
|
|
2100
2097
|
try {
|
|
2101
|
-
const endpoint = (
|
|
2098
|
+
const endpoint = (_a3 = this.opts.pingEndpoint) != null ? _a3 : "/health";
|
|
2102
2099
|
const response = await this.http.get(endpoint, { timeout: 5e3 });
|
|
2103
2100
|
return response.status >= 200 && response.status < 300;
|
|
2104
2101
|
} catch (e) {
|
|
@@ -2235,13 +2232,13 @@ var AuthenticationException = class extends RetrivoraError {
|
|
|
2235
2232
|
}
|
|
2236
2233
|
};
|
|
2237
2234
|
function wrapError(err, defaultCode, defaultMessage) {
|
|
2238
|
-
var
|
|
2235
|
+
var _a3;
|
|
2239
2236
|
if (err instanceof RetrivoraError) {
|
|
2240
2237
|
return err;
|
|
2241
2238
|
}
|
|
2242
2239
|
const error = err;
|
|
2243
2240
|
const message = (error == null ? void 0 : error.message) || defaultMessage || String(err);
|
|
2244
|
-
const status = (error == null ? void 0 : error.status) || (error == null ? void 0 : error.statusCode) || ((
|
|
2241
|
+
const status = (error == null ? void 0 : error.status) || (error == null ? void 0 : error.statusCode) || ((_a3 = error == null ? void 0 : error.response) == null ? void 0 : _a3.status);
|
|
2245
2242
|
const code = error == null ? void 0 : error.code;
|
|
2246
2243
|
if (status === 429 || /rate[- ]?limit/i.test(message) || code === "RATE_LIMIT_EXCEEDED") {
|
|
2247
2244
|
return new RateLimitException(message, err);
|
|
@@ -2523,8 +2520,8 @@ var UI_BORDER_RADIUS_OPTIONS = ["none", "sm", "md", "lg", "xl", "full"];
|
|
|
2523
2520
|
|
|
2524
2521
|
// src/config/serverConfig.ts
|
|
2525
2522
|
function readString(env, name) {
|
|
2526
|
-
var
|
|
2527
|
-
const value = (
|
|
2523
|
+
var _a3;
|
|
2524
|
+
const value = (_a3 = env[name]) == null ? void 0 : _a3.trim();
|
|
2528
2525
|
return value ? value : void 0;
|
|
2529
2526
|
}
|
|
2530
2527
|
function readNumber(env, name, fallback) {
|
|
@@ -2537,16 +2534,16 @@ function readNumber(env, name, fallback) {
|
|
|
2537
2534
|
return parsed;
|
|
2538
2535
|
}
|
|
2539
2536
|
function readEnum(env, name, fallback, allowed) {
|
|
2540
|
-
var
|
|
2541
|
-
const value = (
|
|
2537
|
+
var _a3;
|
|
2538
|
+
const value = (_a3 = readString(env, name)) != null ? _a3 : fallback;
|
|
2542
2539
|
if (allowed.includes(value)) {
|
|
2543
2540
|
return value;
|
|
2544
2541
|
}
|
|
2545
2542
|
throw new Error(`[getRagConfig] ${name} must be one of: ${allowed.join(", ")}`);
|
|
2546
2543
|
}
|
|
2547
2544
|
function getEnvConfig(env = process.env, base) {
|
|
2548
|
-
var
|
|
2549
|
-
const licenseKey = (_d = (_c = (_b = (
|
|
2545
|
+
var _a3, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _A, _B, _C, _D, _E, _F, _G, _H, _I, _J, _K, _L, _M, _N, _O, _P, _Q, _R, _S, _T, _U, _V, _W, _X, _Y, _Z, __, _$, _aa, _ba, _ca, _da, _ea, _fa, _ga, _ha, _ia, _ja, _ka, _la, _ma, _na, _oa, _pa, _qa, _ra, _sa, _ta, _ua, _va, _wa, _xa, _ya, _za, _Aa, _Ba, _Ca, _Da, _Ea, _Fa, _Ga, _Ha, _Ia, _Ja, _Ka, _La, _Ma, _Na, _Oa, _Pa, _Qa, _Ra, _Sa, _Ta, _Ua, _Va, _Wa, _Xa, _Ya, _Za, __a, _$a, _ab, _bb, _cb, _db, _eb, _fb, _gb, _hb, _ib, _jb, _kb, _lb, _mb, _nb, _ob, _pb, _qb, _rb, _sb, _tb, _ub, _vb, _wb, _xb, _yb, _zb, _Ab, _Bb, _Cb, _Db, _Eb, _Fb, _Gb, _Hb, _Ib, _Jb, _Kb;
|
|
2546
|
+
const licenseKey = (_d = (_c = (_b = (_a3 = readString(env, "RAG_LICENSE_KEY")) != null ? _a3 : readString(env, "RETRIVORA_LICENSE_KEY")) != null ? _b : readString(env, "NEXT_PUBLIC_RETRIVORA_LICENSE_KEY")) != null ? _c : readString(env, "LICENSE_KEY")) != null ? _d : base == null ? void 0 : base.licenseKey;
|
|
2550
2547
|
const jwtProjectId = (() => {
|
|
2551
2548
|
if (!licenseKey) return void 0;
|
|
2552
2549
|
try {
|
|
@@ -2627,7 +2624,7 @@ function getEnvConfig(env = process.env, base) {
|
|
|
2627
2624
|
rest: "default",
|
|
2628
2625
|
custom: "default"
|
|
2629
2626
|
};
|
|
2630
|
-
const
|
|
2627
|
+
const isFreeTier2 = (() => {
|
|
2631
2628
|
if (!licenseKey) return true;
|
|
2632
2629
|
try {
|
|
2633
2630
|
const payload = LicenseVerifier.verify(licenseKey, projectId);
|
|
@@ -2638,13 +2635,13 @@ function getEnvConfig(env = process.env, base) {
|
|
|
2638
2635
|
}
|
|
2639
2636
|
})();
|
|
2640
2637
|
const defaultGatewayUrl = (_ra = (_qa = readString(env, "LITELLM_BASE_URL")) != null ? _qa : readString(env, "LLM_BASE_URL")) != null ? _ra : "https://www.retrivora.com/api/v1";
|
|
2641
|
-
const defaultLlmProvider =
|
|
2638
|
+
const defaultLlmProvider = isFreeTier2 ? "universal_rest" : "universal_rest";
|
|
2642
2639
|
const llmProvider = (_ua = (_ta = readEnum(env, "LLM_PROVIDER", defaultLlmProvider, LLM_PROVIDERS)) != null ? _ta : (_sa = base == null ? void 0 : base.llm) == null ? void 0 : _sa.provider) != null ? _ua : defaultLlmProvider;
|
|
2643
2640
|
const llmBaseUrl = (_ya = (_xa = (_va = readString(env, "LITELLM_BASE_URL")) != null ? _va : readString(env, "LLM_BASE_URL")) != null ? _xa : (_wa = base == null ? void 0 : base.llm) == null ? void 0 : _wa.baseUrl) != null ? _ya : defaultGatewayUrl;
|
|
2644
|
-
const llmModel = (_Ca = (_Aa = readString(env, "LLM_MODEL")) != null ? _Aa : (_za = base == null ? void 0 : base.llm) == null ? void 0 : _za.model) != null ? _Ca :
|
|
2641
|
+
const llmModel = (_Ca = (_Aa = readString(env, "LLM_MODEL")) != null ? _Aa : (_za = base == null ? void 0 : base.llm) == null ? void 0 : _za.model) != null ? _Ca : isFreeTier2 ? "llama-3.1-8b-instant" : (_Ba = DEFAULT_MODEL_BY_PROVIDER[llmProvider]) != null ? _Ba : "gpt-4o";
|
|
2645
2642
|
const llmApiKey = (_Ia = (_Ha = (_Fa = (_Ea = (_Da = llmApiKeyByProvider[llmProvider]) != null ? _Da : readString(env, "LLM_API_KEY")) != null ? _Ea : readString(env, "LITELLM_MASTER_KEY")) != null ? _Fa : readString(env, "LITELLM_API_KEY")) != null ? _Ha : (_Ga = base == null ? void 0 : base.llm) == null ? void 0 : _Ga.apiKey) != null ? _Ia : licenseKey;
|
|
2646
2643
|
const llmProfile = (_Ma = (_La = readString(env, "LLM_UNIVERSAL_PROFILE")) != null ? _La : (_Ka = (_Ja = base == null ? void 0 : base.llm) == null ? void 0 : _Ja.options) == null ? void 0 : _Ka.profile) != null ? _Ma : "litellm";
|
|
2647
|
-
const defaultEmbeddingProvider =
|
|
2644
|
+
const defaultEmbeddingProvider = isFreeTier2 ? "universal_rest" : "universal_rest";
|
|
2648
2645
|
const embeddingProvider = (_Pa = (_Oa = readEnum(env, "EMBEDDING_PROVIDER", defaultEmbeddingProvider, EMBEDDING_PROVIDERS)) != null ? _Oa : (_Na = base == null ? void 0 : base.embedding) == null ? void 0 : _Na.provider) != null ? _Pa : defaultEmbeddingProvider;
|
|
2649
2646
|
const embeddingBaseUrl = (_Ua = (_Ta = (_Ra = (_Qa = readString(env, "LITELLM_BASE_URL")) != null ? _Qa : readString(env, "EMBEDDING_BASE_URL")) != null ? _Ra : readString(env, "LLM_BASE_URL")) != null ? _Ta : (_Sa = base == null ? void 0 : base.embedding) == null ? void 0 : _Sa.baseUrl) != null ? _Ua : defaultGatewayUrl;
|
|
2650
2647
|
const embeddingModel = (_Xa = (_Wa = readString(env, "EMBEDDING_MODEL")) != null ? _Wa : (_Va = base == null ? void 0 : base.embedding) == null ? void 0 : _Va.model) != null ? _Xa : "text-embedding-004";
|
|
@@ -2734,8 +2731,8 @@ function getEnvConfig(env = process.env, base) {
|
|
|
2734
2731
|
}
|
|
2735
2732
|
} : {}), {
|
|
2736
2733
|
mcpServers: (() => {
|
|
2737
|
-
var
|
|
2738
|
-
const raw = (
|
|
2734
|
+
var _a4;
|
|
2735
|
+
const raw = (_a4 = readString(env, "MCP_SERVERS")) != null ? _a4 : readString(env, "NEXT_PUBLIC_MCP_SERVERS");
|
|
2739
2736
|
if (!raw) return void 0;
|
|
2740
2737
|
try {
|
|
2741
2738
|
return JSON.parse(raw);
|
|
@@ -2780,10 +2777,10 @@ var ConfigResolver = class {
|
|
|
2780
2777
|
* fallback behavior.
|
|
2781
2778
|
*/
|
|
2782
2779
|
static resolveUniversal(hostConfig, env = process.env) {
|
|
2783
|
-
var
|
|
2780
|
+
var _a3;
|
|
2784
2781
|
if (!hostConfig) return this.resolve(void 0, env);
|
|
2785
2782
|
const normalized = __spreadProps(__spreadValues({}, hostConfig), {
|
|
2786
|
-
vectorDb: (
|
|
2783
|
+
vectorDb: (_a3 = hostConfig.vectorDb) != null ? _a3 : hostConfig.vectorDatabase,
|
|
2787
2784
|
rag: this.mergeRetrievalWorkflow(hostConfig.rag, hostConfig.retrieval, hostConfig.workflow)
|
|
2788
2785
|
});
|
|
2789
2786
|
return this.resolve(normalized, env);
|
|
@@ -2803,11 +2800,11 @@ var ConfigResolver = class {
|
|
|
2803
2800
|
}
|
|
2804
2801
|
}
|
|
2805
2802
|
static mergeRetrievalWorkflow(rag, retrieval, workflow) {
|
|
2806
|
-
var
|
|
2803
|
+
var _a3, _b, _c, _d, _e, _f;
|
|
2807
2804
|
if (!rag && !retrieval && !workflow) return void 0;
|
|
2808
2805
|
const normalized = __spreadValues({}, rag != null ? rag : {});
|
|
2809
2806
|
if (retrieval) {
|
|
2810
|
-
normalized.topK = (
|
|
2807
|
+
normalized.topK = (_a3 = retrieval.topK) != null ? _a3 : normalized.topK;
|
|
2811
2808
|
normalized.scoreThreshold = (_b = retrieval.scoreThreshold) != null ? _b : normalized.scoreThreshold;
|
|
2812
2809
|
normalized.useReranking = (_c = retrieval.useReranking) != null ? _c : normalized.useReranking;
|
|
2813
2810
|
if (retrieval.strategy === "hybrid") normalized.architecture = (_d = normalized.architecture) != null ? _d : "hybrid";
|
|
@@ -2935,8 +2932,8 @@ var OpenAIProvider = class {
|
|
|
2935
2932
|
};
|
|
2936
2933
|
}
|
|
2937
2934
|
async chat(messages, context, options) {
|
|
2938
|
-
var
|
|
2939
|
-
const basePrompt = (_b = (
|
|
2935
|
+
var _a3, _b, _c, _d, _e, _f, _g2, _h, _i;
|
|
2936
|
+
const basePrompt = (_b = (_a3 = options == null ? void 0 : options.systemPrompt) != null ? _a3 : this.llmConfig.systemPrompt) != null ? _b : "You are a helpful assistant. Answer questions based on the provided context.";
|
|
2940
2937
|
const systemMessage = {
|
|
2941
2938
|
role: "system",
|
|
2942
2939
|
content: buildSystemContent(basePrompt, context)
|
|
@@ -2960,8 +2957,8 @@ var OpenAIProvider = class {
|
|
|
2960
2957
|
}
|
|
2961
2958
|
chatStream(messages, context, options) {
|
|
2962
2959
|
return __asyncGenerator(this, null, function* () {
|
|
2963
|
-
var
|
|
2964
|
-
const basePrompt = (_b = (
|
|
2960
|
+
var _a3, _b, _c, _d, _e, _f, _g2, _h;
|
|
2961
|
+
const basePrompt = (_b = (_a3 = options == null ? void 0 : options.systemPrompt) != null ? _a3 : this.llmConfig.systemPrompt) != null ? _b : "You are a helpful assistant. Answer questions based on the provided context.";
|
|
2965
2962
|
const systemMessage = {
|
|
2966
2963
|
role: "system",
|
|
2967
2964
|
content: buildSystemContent(basePrompt, context)
|
|
@@ -3008,8 +3005,8 @@ var OpenAIProvider = class {
|
|
|
3008
3005
|
return results[0];
|
|
3009
3006
|
}
|
|
3010
3007
|
async batchEmbed(texts, options) {
|
|
3011
|
-
var
|
|
3012
|
-
const model = (_c = (_b = options == null ? void 0 : options.model) != null ? _b : (
|
|
3008
|
+
var _a3, _b, _c, _d, _e;
|
|
3009
|
+
const model = (_c = (_b = options == null ? void 0 : options.model) != null ? _b : (_a3 = this.embeddingConfig) == null ? void 0 : _a3.model) != null ? _c : "text-embedding-3-small";
|
|
3013
3010
|
const apiKey = (_e = (_d = this.embeddingConfig) == null ? void 0 : _d.apiKey) != null ? _e : this.llmConfig.apiKey;
|
|
3014
3011
|
const client = apiKey !== this.llmConfig.apiKey ? new OpenAI({ apiKey }) : this.client;
|
|
3015
3012
|
const response = await client.embeddings.create({ model, input: texts });
|
|
@@ -3086,8 +3083,8 @@ var AnthropicProvider = class {
|
|
|
3086
3083
|
};
|
|
3087
3084
|
}
|
|
3088
3085
|
async chat(messages, context, options) {
|
|
3089
|
-
var
|
|
3090
|
-
const basePrompt = (_b = (
|
|
3086
|
+
var _a3, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k;
|
|
3087
|
+
const basePrompt = (_b = (_a3 = options == null ? void 0 : options.systemPrompt) != null ? _a3 : this.llmConfig.systemPrompt) != null ? _b : "You are a helpful assistant. Use the context below to answer the user's question accurately.";
|
|
3091
3088
|
const system = buildSystemContent(basePrompt, context);
|
|
3092
3089
|
const anthropicMessages = messages.map((m) => ({
|
|
3093
3090
|
role: m.role === "assistant" ? "assistant" : "user",
|
|
@@ -3125,8 +3122,8 @@ var AnthropicProvider = class {
|
|
|
3125
3122
|
}
|
|
3126
3123
|
chatStream(messages, context, options) {
|
|
3127
3124
|
return __asyncGenerator(this, null, function* () {
|
|
3128
|
-
var
|
|
3129
|
-
const basePrompt = (_b = (
|
|
3125
|
+
var _a3, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k;
|
|
3126
|
+
const basePrompt = (_b = (_a3 = options == null ? void 0 : options.systemPrompt) != null ? _a3 : this.llmConfig.systemPrompt) != null ? _b : "You are a helpful assistant. Use the context below to answer the user's question accurately.";
|
|
3130
3127
|
const system = buildSystemContent(basePrompt, context);
|
|
3131
3128
|
const anthropicMessages = messages.map((m) => ({
|
|
3132
3129
|
role: m.role === "assistant" ? "assistant" : "user",
|
|
@@ -3210,8 +3207,8 @@ var AnthropicProvider = class {
|
|
|
3210
3207
|
import axios from "axios";
|
|
3211
3208
|
var OllamaProvider = class {
|
|
3212
3209
|
constructor(llmConfig, embeddingConfig) {
|
|
3213
|
-
var
|
|
3214
|
-
const rawBaseURL = ((
|
|
3210
|
+
var _a3, _b, _c;
|
|
3211
|
+
const rawBaseURL = ((_a3 = llmConfig.baseUrl) != null ? _a3 : "http://localhost:11434").replace(/\/+$/, "");
|
|
3215
3212
|
const baseURL = rawBaseURL.replace(/\/(?:api\/v1|v1|api)$/i, "");
|
|
3216
3213
|
const timeout = Number((_b = llmConfig.options) == null ? void 0 : _b.timeout) || 3e5;
|
|
3217
3214
|
const headers = ((_c = llmConfig.options) == null ? void 0 : _c.headers) || {};
|
|
@@ -3271,8 +3268,8 @@ var OllamaProvider = class {
|
|
|
3271
3268
|
};
|
|
3272
3269
|
}
|
|
3273
3270
|
async chat(messages, context, options) {
|
|
3274
|
-
var
|
|
3275
|
-
const basePrompt = (_b = (
|
|
3271
|
+
var _a3, _b, _c, _d, _e, _f;
|
|
3272
|
+
const basePrompt = (_b = (_a3 = options == null ? void 0 : options.systemPrompt) != null ? _a3 : this.llmConfig.systemPrompt) != null ? _b : "You are a helpful assistant. Use the provided context to answer the user's question.";
|
|
3276
3273
|
const system = buildSystemContent(basePrompt, context);
|
|
3277
3274
|
const { data } = await this.http.post("/api/chat", {
|
|
3278
3275
|
model: this.llmConfig.model,
|
|
@@ -3290,8 +3287,8 @@ var OllamaProvider = class {
|
|
|
3290
3287
|
}
|
|
3291
3288
|
chatStream(messages, context, options) {
|
|
3292
3289
|
return __asyncGenerator(this, null, function* () {
|
|
3293
|
-
var
|
|
3294
|
-
const basePrompt = (_b = (
|
|
3290
|
+
var _a3, _b, _c, _d, _e, _f, _g2, _h;
|
|
3291
|
+
const basePrompt = (_b = (_a3 = options == null ? void 0 : options.systemPrompt) != null ? _a3 : this.llmConfig.systemPrompt) != null ? _b : "You are a helpful assistant. Use the provided context to answer the user's question.";
|
|
3295
3292
|
const system = buildSystemContent(basePrompt, context);
|
|
3296
3293
|
const response = yield new __await(this.http.post("/api/chat", {
|
|
3297
3294
|
model: this.llmConfig.model,
|
|
@@ -3349,8 +3346,8 @@ var OllamaProvider = class {
|
|
|
3349
3346
|
});
|
|
3350
3347
|
}
|
|
3351
3348
|
async embed(text, options) {
|
|
3352
|
-
var
|
|
3353
|
-
const model = (_c = (_b = options == null ? void 0 : options.model) != null ? _b : (
|
|
3349
|
+
var _a3, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k;
|
|
3350
|
+
const model = (_c = (_b = options == null ? void 0 : options.model) != null ? _b : (_a3 = this.embeddingConfig) == null ? void 0 : _a3.model) != null ? _c : "nomic-embed-text";
|
|
3354
3351
|
const baseURL = (_f = (_e = (_d = this.embeddingConfig) == null ? void 0 : _d.baseUrl) != null ? _e : this.llmConfig.baseUrl) != null ? _f : "http://localhost:11434";
|
|
3355
3352
|
const client = baseURL !== ((_g2 = this.llmConfig.baseUrl) != null ? _g2 : "http://localhost:11434") ? axios.create({ baseURL, timeout: 6e4 }) : this.http;
|
|
3356
3353
|
let prompt = text;
|
|
@@ -3451,9 +3448,9 @@ var GeminiProvider = class {
|
|
|
3451
3448
|
static getHealthChecker() {
|
|
3452
3449
|
return {
|
|
3453
3450
|
async check(config) {
|
|
3454
|
-
var
|
|
3451
|
+
var _a3, _b;
|
|
3455
3452
|
const timestamp = Date.now();
|
|
3456
|
-
const apiKey = (_b = (
|
|
3453
|
+
const apiKey = (_b = (_a3 = config.apiKey) != null ? _a3 : process.env.GOOGLE_GENAI_API_KEY) != null ? _b : "";
|
|
3457
3454
|
const modelName = sanitizeModel(config.model);
|
|
3458
3455
|
try {
|
|
3459
3456
|
const { GoogleGenerativeAI: GoogleGenerativeAI2 } = await import("@google/generative-ai");
|
|
@@ -3483,16 +3480,16 @@ var GeminiProvider = class {
|
|
|
3483
3480
|
/** Resolve the embedding client — uses a separate client when the embedding
|
|
3484
3481
|
* API key differs from the LLM API key. */
|
|
3485
3482
|
get embeddingClient() {
|
|
3486
|
-
var
|
|
3487
|
-
if (((
|
|
3483
|
+
var _a3;
|
|
3484
|
+
if (((_a3 = this.embeddingConfig) == null ? void 0 : _a3.apiKey) && this.embeddingConfig.apiKey !== this.llmConfig.apiKey) {
|
|
3488
3485
|
return buildClient(this.embeddingConfig.apiKey);
|
|
3489
3486
|
}
|
|
3490
3487
|
return this.client;
|
|
3491
3488
|
}
|
|
3492
3489
|
/** Resolve the embedding model to use, in order of specificity. */
|
|
3493
3490
|
resolveEmbeddingModel(optionsModel) {
|
|
3494
|
-
var
|
|
3495
|
-
return sanitizeModel((_b = optionsModel != null ? optionsModel : (
|
|
3491
|
+
var _a3, _b;
|
|
3492
|
+
return sanitizeModel((_b = optionsModel != null ? optionsModel : (_a3 = this.embeddingConfig) == null ? void 0 : _a3.model) != null ? _b : DEFAULT_EMBEDDING_MODEL);
|
|
3496
3493
|
}
|
|
3497
3494
|
/**
|
|
3498
3495
|
* Convert ChatMessage[] to the Gemini contents format.
|
|
@@ -3512,8 +3509,8 @@ var GeminiProvider = class {
|
|
|
3512
3509
|
// ILLMProvider — chat
|
|
3513
3510
|
// -------------------------------------------------------------------------
|
|
3514
3511
|
async chat(messages, context, options) {
|
|
3515
|
-
var
|
|
3516
|
-
const basePrompt = (_b = (
|
|
3512
|
+
var _a3, _b, _c, _d, _e, _f;
|
|
3513
|
+
const basePrompt = (_b = (_a3 = options == null ? void 0 : options.systemPrompt) != null ? _a3 : this.llmConfig.systemPrompt) != null ? _b : "You are a helpful assistant. Answer questions based on the provided context.";
|
|
3517
3514
|
const model = this.client.getGenerativeModel({
|
|
3518
3515
|
model: this.llmConfig.model,
|
|
3519
3516
|
systemInstruction: buildSystemContent(basePrompt, context)
|
|
@@ -3530,8 +3527,8 @@ var GeminiProvider = class {
|
|
|
3530
3527
|
}
|
|
3531
3528
|
chatStream(messages, context, options) {
|
|
3532
3529
|
return __asyncGenerator(this, null, function* () {
|
|
3533
|
-
var
|
|
3534
|
-
const basePrompt = (_b = (
|
|
3530
|
+
var _a3, _b, _c, _d, _e, _f;
|
|
3531
|
+
const basePrompt = (_b = (_a3 = options == null ? void 0 : options.systemPrompt) != null ? _a3 : this.llmConfig.systemPrompt) != null ? _b : "You are a helpful assistant. Answer questions based on the provided context.";
|
|
3535
3532
|
const model = this.client.getGenerativeModel({
|
|
3536
3533
|
model: this.llmConfig.model,
|
|
3537
3534
|
systemInstruction: buildSystemContent(basePrompt, context)
|
|
@@ -3566,11 +3563,11 @@ var GeminiProvider = class {
|
|
|
3566
3563
|
// ILLMProvider — embeddings
|
|
3567
3564
|
// -------------------------------------------------------------------------
|
|
3568
3565
|
async embed(text, options) {
|
|
3569
|
-
var
|
|
3566
|
+
var _a3, _b;
|
|
3570
3567
|
const content = applyPrefix(
|
|
3571
3568
|
text,
|
|
3572
3569
|
options == null ? void 0 : options.taskType,
|
|
3573
|
-
(
|
|
3570
|
+
(_a3 = this.embeddingConfig) == null ? void 0 : _a3.queryPrefix,
|
|
3574
3571
|
(_b = this.embeddingConfig) == null ? void 0 : _b.documentPrefix
|
|
3575
3572
|
);
|
|
3576
3573
|
const modelName = this.resolveEmbeddingModel(options == null ? void 0 : options.model);
|
|
@@ -3587,7 +3584,7 @@ var GeminiProvider = class {
|
|
|
3587
3584
|
const modelName = this.resolveEmbeddingModel(options == null ? void 0 : options.model);
|
|
3588
3585
|
const model = this.embeddingClient.getGenerativeModel({ model: modelName });
|
|
3589
3586
|
const requests = texts.map((text) => {
|
|
3590
|
-
var
|
|
3587
|
+
var _a3, _b;
|
|
3591
3588
|
return {
|
|
3592
3589
|
content: {
|
|
3593
3590
|
role: "user",
|
|
@@ -3595,7 +3592,7 @@ var GeminiProvider = class {
|
|
|
3595
3592
|
text: applyPrefix(
|
|
3596
3593
|
text,
|
|
3597
3594
|
options == null ? void 0 : options.taskType,
|
|
3598
|
-
(
|
|
3595
|
+
(_a3 = this.embeddingConfig) == null ? void 0 : _a3.queryPrefix,
|
|
3599
3596
|
(_b = this.embeddingConfig) == null ? void 0 : _b.documentPrefix
|
|
3600
3597
|
)
|
|
3601
3598
|
}]
|
|
@@ -3612,8 +3609,8 @@ var GeminiProvider = class {
|
|
|
3612
3609
|
throw err;
|
|
3613
3610
|
});
|
|
3614
3611
|
return response.embeddings.map((e) => {
|
|
3615
|
-
var
|
|
3616
|
-
return (
|
|
3612
|
+
var _a3;
|
|
3613
|
+
return (_a3 = e.values) != null ? _a3 : [];
|
|
3617
3614
|
});
|
|
3618
3615
|
}
|
|
3619
3616
|
// -------------------------------------------------------------------------
|
|
@@ -3702,8 +3699,8 @@ var GroqProvider = class {
|
|
|
3702
3699
|
};
|
|
3703
3700
|
}
|
|
3704
3701
|
async chat(messages, context, options) {
|
|
3705
|
-
var
|
|
3706
|
-
const basePrompt = (_b = (
|
|
3702
|
+
var _a3, _b, _c, _d, _e, _f, _g2, _h, _i;
|
|
3703
|
+
const basePrompt = (_b = (_a3 = options == null ? void 0 : options.systemPrompt) != null ? _a3 : this.llmConfig.systemPrompt) != null ? _b : "You are a helpful assistant. Answer questions based on the provided context.";
|
|
3707
3704
|
const systemMessage = {
|
|
3708
3705
|
role: "system",
|
|
3709
3706
|
content: buildSystemContent(basePrompt, context)
|
|
@@ -3727,8 +3724,8 @@ var GroqProvider = class {
|
|
|
3727
3724
|
}
|
|
3728
3725
|
chatStream(messages, context, options) {
|
|
3729
3726
|
return __asyncGenerator(this, null, function* () {
|
|
3730
|
-
var
|
|
3731
|
-
const basePrompt = (_b = (
|
|
3727
|
+
var _a3, _b, _c, _d, _e, _f, _g2, _h;
|
|
3728
|
+
const basePrompt = (_b = (_a3 = options == null ? void 0 : options.systemPrompt) != null ? _a3 : this.llmConfig.systemPrompt) != null ? _b : "You are a helpful assistant. Answer questions based on the provided context.";
|
|
3732
3729
|
const systemMessage = {
|
|
3733
3730
|
role: "system",
|
|
3734
3731
|
content: buildSystemContent(basePrompt, context)
|
|
@@ -3862,8 +3859,8 @@ var QwenProvider = class {
|
|
|
3862
3859
|
};
|
|
3863
3860
|
}
|
|
3864
3861
|
async chat(messages, context, options) {
|
|
3865
|
-
var
|
|
3866
|
-
const basePrompt = (_b = (
|
|
3862
|
+
var _a3, _b, _c, _d, _e, _f, _g2, _h, _i;
|
|
3863
|
+
const basePrompt = (_b = (_a3 = options == null ? void 0 : options.systemPrompt) != null ? _a3 : this.llmConfig.systemPrompt) != null ? _b : "You are a helpful assistant. Answer questions based on the provided context.";
|
|
3867
3864
|
const systemMessage = {
|
|
3868
3865
|
role: "system",
|
|
3869
3866
|
content: buildSystemContent(basePrompt, context)
|
|
@@ -3887,8 +3884,8 @@ var QwenProvider = class {
|
|
|
3887
3884
|
}
|
|
3888
3885
|
chatStream(messages, context, options) {
|
|
3889
3886
|
return __asyncGenerator(this, null, function* () {
|
|
3890
|
-
var
|
|
3891
|
-
const basePrompt = (_b = (
|
|
3887
|
+
var _a3, _b, _c, _d, _e, _f, _g2, _h;
|
|
3888
|
+
const basePrompt = (_b = (_a3 = options == null ? void 0 : options.systemPrompt) != null ? _a3 : this.llmConfig.systemPrompt) != null ? _b : "You are a helpful assistant. Answer questions based on the provided context.";
|
|
3892
3889
|
const systemMessage = {
|
|
3893
3890
|
role: "system",
|
|
3894
3891
|
content: buildSystemContent(basePrompt, context)
|
|
@@ -3935,8 +3932,8 @@ var QwenProvider = class {
|
|
|
3935
3932
|
return results[0];
|
|
3936
3933
|
}
|
|
3937
3934
|
async batchEmbed(texts, options) {
|
|
3938
|
-
var
|
|
3939
|
-
const model = (_c = (_b = options == null ? void 0 : options.model) != null ? _b : (
|
|
3935
|
+
var _a3, _b, _c, _d, _e, _f;
|
|
3936
|
+
const model = (_c = (_b = options == null ? void 0 : options.model) != null ? _b : (_a3 = this.embeddingConfig) == null ? void 0 : _a3.model) != null ? _c : "text-embedding-v1";
|
|
3940
3937
|
const apiKey = (_e = (_d = this.embeddingConfig) == null ? void 0 : _d.apiKey) != null ? _e : this.llmConfig.apiKey;
|
|
3941
3938
|
const client = apiKey !== this.llmConfig.apiKey ? new OpenAI3({
|
|
3942
3939
|
apiKey,
|
|
@@ -3997,9 +3994,9 @@ var LLM_PROFILES = {
|
|
|
3997
3994
|
|
|
3998
3995
|
// src/llm/providers/UniversalLLMAdapter.ts
|
|
3999
3996
|
function extractContent(obj) {
|
|
4000
|
-
var
|
|
3997
|
+
var _a3, _b, _c;
|
|
4001
3998
|
if (!obj || typeof obj !== "object") return void 0;
|
|
4002
|
-
const choice = (
|
|
3999
|
+
const choice = (_a3 = obj.choices) == null ? void 0 : _a3[0];
|
|
4003
4000
|
if (!choice) return void 0;
|
|
4004
4001
|
if (typeof ((_b = choice.message) == null ? void 0 : _b.content) === "string") return choice.message.content;
|
|
4005
4002
|
if (typeof ((_c = choice.delta) == null ? void 0 : _c.content) === "string") return choice.delta.content;
|
|
@@ -4009,10 +4006,10 @@ function extractContent(obj) {
|
|
|
4009
4006
|
}
|
|
4010
4007
|
var UniversalLLMAdapter = class {
|
|
4011
4008
|
constructor(config) {
|
|
4012
|
-
var
|
|
4009
|
+
var _a3, _b, _c, _d, _e, _f, _g2, _h;
|
|
4013
4010
|
this.model = config.model;
|
|
4014
4011
|
const llmConfig = config;
|
|
4015
|
-
const options = (
|
|
4012
|
+
const options = (_a3 = llmConfig.options) != null ? _a3 : {};
|
|
4016
4013
|
let profile = {};
|
|
4017
4014
|
if (typeof options.profile === "string") {
|
|
4018
4015
|
profile = LLM_PROFILES[options.profile] || {};
|
|
@@ -4038,8 +4035,8 @@ var UniversalLLMAdapter = class {
|
|
|
4038
4035
|
});
|
|
4039
4036
|
}
|
|
4040
4037
|
async chat(messages, context) {
|
|
4041
|
-
var
|
|
4042
|
-
const path2 = (
|
|
4038
|
+
var _a3, _b, _c, _d, _e;
|
|
4039
|
+
const path2 = (_a3 = this.opts.chatPath) != null ? _a3 : "/chat/completions";
|
|
4043
4040
|
const safeMessages = (Array.isArray(messages) ? messages : []).filter((m) => Boolean(m && typeof m === "object")).map((m) => ({
|
|
4044
4041
|
role: m.role || "user",
|
|
4045
4042
|
content: typeof m.content === "string" ? m.content : m.content != null ? String(m.content) : ""
|
|
@@ -4105,8 +4102,8 @@ ${context != null ? context : "None"}` },
|
|
|
4105
4102
|
*/
|
|
4106
4103
|
chatStream(messages, context) {
|
|
4107
4104
|
return __asyncGenerator(this, null, function* () {
|
|
4108
|
-
var
|
|
4109
|
-
const path2 = (
|
|
4105
|
+
var _a3, _b, _c, _d, _e, _f;
|
|
4106
|
+
const path2 = (_a3 = this.opts.chatPath) != null ? _a3 : "/chat/completions";
|
|
4110
4107
|
const url = `${this.baseUrl.replace(/\/$/, "")}${path2}`;
|
|
4111
4108
|
const extractPath = ((_b = this.opts.responseExtractPath) != null ? _b : "choices[0].message.content").replace("message.content", "delta.content");
|
|
4112
4109
|
const safeMessages = (Array.isArray(messages) ? messages : []).filter((m) => Boolean(m && typeof m === "object")).map((m) => ({
|
|
@@ -4232,8 +4229,8 @@ ${context != null ? context : "None"}` },
|
|
|
4232
4229
|
});
|
|
4233
4230
|
}
|
|
4234
4231
|
async embed(text) {
|
|
4235
|
-
var
|
|
4236
|
-
const path2 = (
|
|
4232
|
+
var _a3, _b, _c, _d, _e;
|
|
4233
|
+
const path2 = (_a3 = this.opts.embedPath) != null ? _a3 : "/embeddings";
|
|
4237
4234
|
const payload = this.opts.embedPayloadTemplate ? buildPayload(this.opts.embedPayloadTemplate, { input: text, model: this.model }) : { input: text, model: this.model };
|
|
4238
4235
|
try {
|
|
4239
4236
|
const { data: data2 } = await this.http.post(path2, payload);
|
|
@@ -4322,7 +4319,7 @@ var LLMFactory = class _LLMFactory {
|
|
|
4322
4319
|
];
|
|
4323
4320
|
}
|
|
4324
4321
|
static create(llmConfig, embeddingConfig) {
|
|
4325
|
-
var
|
|
4322
|
+
var _a3, _b, _c;
|
|
4326
4323
|
switch (llmConfig.provider) {
|
|
4327
4324
|
case "openai":
|
|
4328
4325
|
return new OpenAIProvider(llmConfig, embeddingConfig);
|
|
@@ -4341,7 +4338,7 @@ var LLMFactory = class _LLMFactory {
|
|
|
4341
4338
|
case "custom":
|
|
4342
4339
|
return new UniversalLLMAdapter(llmConfig);
|
|
4343
4340
|
default: {
|
|
4344
|
-
const providerName = String((
|
|
4341
|
+
const providerName = String((_a3 = llmConfig.provider) != null ? _a3 : "").toLowerCase();
|
|
4345
4342
|
const customFactory = customProviders.get(providerName);
|
|
4346
4343
|
if (customFactory) {
|
|
4347
4344
|
return customFactory(llmConfig);
|
|
@@ -4443,7 +4440,7 @@ var ProviderRegistry = class {
|
|
|
4443
4440
|
return null;
|
|
4444
4441
|
}
|
|
4445
4442
|
static async loadVectorProviderClass(provider) {
|
|
4446
|
-
var
|
|
4443
|
+
var _a3;
|
|
4447
4444
|
if (this.vectorProviders[provider]) return this.vectorProviders[provider];
|
|
4448
4445
|
switch (provider) {
|
|
4449
4446
|
case "pinecone": {
|
|
@@ -4452,7 +4449,7 @@ var ProviderRegistry = class {
|
|
|
4452
4449
|
}
|
|
4453
4450
|
case "pgvector":
|
|
4454
4451
|
case "postgresql": {
|
|
4455
|
-
const postgresMode = ((
|
|
4452
|
+
const postgresMode = ((_a3 = process.env.POSTGRES_MODE) != null ? _a3 : "multi").toLowerCase();
|
|
4456
4453
|
if (postgresMode === "single") {
|
|
4457
4454
|
const { PostgreSQLProvider: PostgreSQLProvider2 } = await Promise.resolve().then(() => (init_PostgreSQLProvider(), PostgreSQLProvider_exports));
|
|
4458
4455
|
return PostgreSQLProvider2;
|
|
@@ -4675,7 +4672,7 @@ var ConfigValidator = class {
|
|
|
4675
4672
|
// package.json
|
|
4676
4673
|
var package_default = {
|
|
4677
4674
|
name: "@retrivora-ai/rag-engine",
|
|
4678
|
-
version: "2.3.
|
|
4675
|
+
version: "2.3.2",
|
|
4679
4676
|
description: "Retrivora AI is a plug-and-play AI engine for RAG chat experiences \u2014 generic vector DB + LLM provider, embeddable or standalone.",
|
|
4680
4677
|
author: "Abhinav Alkuchi",
|
|
4681
4678
|
license: "UNLICENSED",
|
|
@@ -4754,6 +4751,7 @@ var package_default = {
|
|
|
4754
4751
|
build: "npm run build:pkg",
|
|
4755
4752
|
"build:pkg": "npx @tailwindcss/cli -i src/tailwind.css -o src/index.css && tsup src/index.ts src/handlers/index.ts src/server.ts --format cjs,esm --dts --clean --no-splitting --tsconfig tsconfig.build.json --external react,react-dom,next,mongodb,pg,openai,@anthropic-ai/sdk,@pinecone-database/pinecone,axios,lucide-react,mammoth,pdf-parse,xlsx,react-markdown,remark-gfm,next-themes,langchain,@langchain/core,@langchain/openai,llamaindex --inject-style && npx @tailwindcss/cli -i src/tailwind.css -o dist/index.css",
|
|
4756
4753
|
lint: "eslint",
|
|
4754
|
+
typecheck: "tsc --noEmit -p tsconfig.build.json",
|
|
4757
4755
|
clean: "rm -rf dist"
|
|
4758
4756
|
},
|
|
4759
4757
|
peerDependencies: {
|
|
@@ -4980,8 +4978,8 @@ var Reranker = class {
|
|
|
4980
4978
|
return matches.sort((a, b) => b.score - a.score).slice(0, limit);
|
|
4981
4979
|
}
|
|
4982
4980
|
const scoredMatches = matches.map((match) => {
|
|
4983
|
-
var
|
|
4984
|
-
const contentLower = ((
|
|
4981
|
+
var _a3;
|
|
4982
|
+
const contentLower = ((_a3 = match == null ? void 0 : match.content) != null ? _a3 : "").toLowerCase();
|
|
4985
4983
|
let keywordScore = 0;
|
|
4986
4984
|
keywords.forEach((keyword) => {
|
|
4987
4985
|
if (contentLower.includes(keyword)) {
|
|
@@ -5003,8 +5001,8 @@ var Reranker = class {
|
|
|
5003
5001
|
|
|
5004
5002
|
Documents:
|
|
5005
5003
|
${topN.map((m, i) => {
|
|
5006
|
-
var
|
|
5007
|
-
return `[${i}] ${((
|
|
5004
|
+
var _a3;
|
|
5005
|
+
return `[${i}] ${((_a3 = m == null ? void 0 : m.content) != null ? _a3 : "").replace(/\n/g, " ")}`;
|
|
5008
5006
|
}).join("\n")}` }],
|
|
5009
5007
|
"",
|
|
5010
5008
|
{
|
|
@@ -5048,11 +5046,11 @@ var LlamaIndexIngestor = class {
|
|
|
5048
5046
|
* than standard character-count splitting.
|
|
5049
5047
|
*/
|
|
5050
5048
|
async chunk(text, options = {}) {
|
|
5051
|
-
var
|
|
5049
|
+
var _a3, _b;
|
|
5052
5050
|
try {
|
|
5053
5051
|
const { Document, SentenceSplitter, MetadataMode } = await import(`${"llamaindex"}`);
|
|
5054
5052
|
const splitter = new SentenceSplitter({
|
|
5055
|
-
chunkSize: (
|
|
5053
|
+
chunkSize: (_a3 = options.chunkSize) != null ? _a3 : 1e3,
|
|
5056
5054
|
chunkOverlap: (_b = options.chunkOverlap) != null ? _b : 200
|
|
5057
5055
|
});
|
|
5058
5056
|
const doc = new Document({ text, metadata: options.metadata || {} });
|
|
@@ -5140,7 +5138,6 @@ When presenting structured data, statistics, or comparisons, decide if it is bes
|
|
|
5140
5138
|
finalSystemPrompt += chartInstruction;
|
|
5141
5139
|
}
|
|
5142
5140
|
this.agent = createAgent({
|
|
5143
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
5144
5141
|
model: chatModel,
|
|
5145
5142
|
tools: [searchTool],
|
|
5146
5143
|
systemPrompt: finalSystemPrompt
|
|
@@ -5161,7 +5158,7 @@ ${error instanceof Error ? error.message : String(error)}`
|
|
|
5161
5158
|
* The agent returns `{ messages: [...] }` — the last message is the final answer.
|
|
5162
5159
|
*/
|
|
5163
5160
|
async run(input, chatHistory = []) {
|
|
5164
|
-
var
|
|
5161
|
+
var _a3, _b, _c;
|
|
5165
5162
|
if (!this.agent) {
|
|
5166
5163
|
throw new Error("[LangChainAgent] Agent not initialized. Call initialize() first.");
|
|
5167
5164
|
}
|
|
@@ -5172,7 +5169,7 @@ ${error instanceof Error ? error.message : String(error)}`
|
|
|
5172
5169
|
const response = await this.agent.invoke({
|
|
5173
5170
|
messages: [...historyMessages, new HumanMessage(input)]
|
|
5174
5171
|
});
|
|
5175
|
-
const lastMessage = (
|
|
5172
|
+
const lastMessage = (_a3 = response == null ? void 0 : response.messages) == null ? void 0 : _a3.at(-1);
|
|
5176
5173
|
if (lastMessage && typeof lastMessage.content === "string") {
|
|
5177
5174
|
return lastMessage.content;
|
|
5178
5175
|
}
|
|
@@ -5224,7 +5221,7 @@ var MCPClient = class {
|
|
|
5224
5221
|
}
|
|
5225
5222
|
}
|
|
5226
5223
|
async connectStdio() {
|
|
5227
|
-
var
|
|
5224
|
+
var _a3;
|
|
5228
5225
|
const cmd = this.config.command;
|
|
5229
5226
|
if (!cmd) {
|
|
5230
5227
|
throw new Error(`[MCPClient] Command option is required for stdio transport on "${this.config.name}"`);
|
|
@@ -5239,7 +5236,7 @@ var MCPClient = class {
|
|
|
5239
5236
|
this.stdioBuffer += data.toString("utf-8");
|
|
5240
5237
|
this.processStdioLines();
|
|
5241
5238
|
});
|
|
5242
|
-
(
|
|
5239
|
+
(_a3 = this.childProcess.stderr) == null ? void 0 : _a3.on("data", (data) => {
|
|
5243
5240
|
console.warn(`[MCPClient] [stderr] [${this.config.name}]:`, data.toString("utf-8"));
|
|
5244
5241
|
});
|
|
5245
5242
|
this.childProcess.on("close", (code) => {
|
|
@@ -5314,14 +5311,14 @@ var MCPClient = class {
|
|
|
5314
5311
|
}
|
|
5315
5312
|
}
|
|
5316
5313
|
async sendNotification(method, params) {
|
|
5317
|
-
var
|
|
5314
|
+
var _a3;
|
|
5318
5315
|
const notification = {
|
|
5319
5316
|
jsonrpc: "2.0",
|
|
5320
5317
|
method,
|
|
5321
5318
|
params
|
|
5322
5319
|
};
|
|
5323
5320
|
if (this.config.transport === "stdio") {
|
|
5324
|
-
if ((
|
|
5321
|
+
if ((_a3 = this.childProcess) == null ? void 0 : _a3.stdin) {
|
|
5325
5322
|
this.childProcess.stdin.write(JSON.stringify(notification) + "\n", "utf-8");
|
|
5326
5323
|
}
|
|
5327
5324
|
} else if (this.sseUrl) {
|
|
@@ -5399,11 +5396,11 @@ var MCPRegistry = class {
|
|
|
5399
5396
|
// src/core/MultiAgentCoordinator.ts
|
|
5400
5397
|
var MultiAgentCoordinator = class {
|
|
5401
5398
|
constructor(options) {
|
|
5402
|
-
var
|
|
5399
|
+
var _a3;
|
|
5403
5400
|
this.llmProvider = options.llmProvider;
|
|
5404
5401
|
this.mcpRegistry = options.mcpRegistry;
|
|
5405
5402
|
this.documentSearch = options.documentSearch;
|
|
5406
|
-
this.maxIterations = (
|
|
5403
|
+
this.maxIterations = (_a3 = options.maxIterations) != null ? _a3 : 5;
|
|
5407
5404
|
}
|
|
5408
5405
|
/**
|
|
5409
5406
|
* Run the multi-agent coordination loop synchronously and return the final response.
|
|
@@ -5455,8 +5452,8 @@ Available Tools:
|
|
|
5455
5452
|
|
|
5456
5453
|
${mcpTools.length > 0 ? "MCP Server Tools:" : ""}
|
|
5457
5454
|
${mcpTools.map((mt) => {
|
|
5458
|
-
var
|
|
5459
|
-
return `- ${mt.tool.name}(${JSON.stringify(((
|
|
5455
|
+
var _a3;
|
|
5456
|
+
return `- ${mt.tool.name}(${JSON.stringify(((_a3 = mt.tool.inputSchema) == null ? void 0 : _a3.properties) || {})}): ${mt.tool.description || "No description provided."}`;
|
|
5460
5457
|
}).join("\n")}
|
|
5461
5458
|
|
|
5462
5459
|
Tool Calling Protocol:
|
|
@@ -5614,6 +5611,100 @@ ${toolResultText}`
|
|
|
5614
5611
|
}
|
|
5615
5612
|
};
|
|
5616
5613
|
|
|
5614
|
+
// src/core/CircuitBreaker.ts
|
|
5615
|
+
var CircuitBreaker = class {
|
|
5616
|
+
constructor(options = {}) {
|
|
5617
|
+
this.state = "closed";
|
|
5618
|
+
this.failureCount = 0;
|
|
5619
|
+
this.successCount = 0;
|
|
5620
|
+
this.lastFailureAt = 0;
|
|
5621
|
+
this.halfOpenCalls = 0;
|
|
5622
|
+
var _a3, _b, _c;
|
|
5623
|
+
this.failureThreshold = (_a3 = options.failureThreshold) != null ? _a3 : 5;
|
|
5624
|
+
this.resetTimeoutMs = (_b = options.resetTimeoutMs) != null ? _b : 3e4;
|
|
5625
|
+
this.halfOpenMaxCalls = (_c = options.halfOpenMaxCalls) != null ? _c : 1;
|
|
5626
|
+
}
|
|
5627
|
+
record(success) {
|
|
5628
|
+
const now = Date.now();
|
|
5629
|
+
if (success) {
|
|
5630
|
+
this.successCount++;
|
|
5631
|
+
if (this.state === "half-open") {
|
|
5632
|
+
this.state = "closed";
|
|
5633
|
+
this.failureCount = 0;
|
|
5634
|
+
this.halfOpenCalls = 0;
|
|
5635
|
+
this.successCount = 1;
|
|
5636
|
+
} else if (this.state === "closed") {
|
|
5637
|
+
if (this.successCount >= this.failureThreshold) {
|
|
5638
|
+
this.failureCount = Math.max(0, this.failureCount - 1);
|
|
5639
|
+
this.successCount = 0;
|
|
5640
|
+
}
|
|
5641
|
+
}
|
|
5642
|
+
} else {
|
|
5643
|
+
this.failureCount++;
|
|
5644
|
+
this.lastFailureAt = now;
|
|
5645
|
+
this.successCount = 0;
|
|
5646
|
+
if (this.state === "half-open") {
|
|
5647
|
+
this.state = "open";
|
|
5648
|
+
this.halfOpenCalls = 0;
|
|
5649
|
+
} else if (this.state === "closed" && this.failureCount >= this.failureThreshold) {
|
|
5650
|
+
this.state = "open";
|
|
5651
|
+
}
|
|
5652
|
+
}
|
|
5653
|
+
}
|
|
5654
|
+
canExecute() {
|
|
5655
|
+
const now = Date.now();
|
|
5656
|
+
if (this.state === "closed") {
|
|
5657
|
+
return true;
|
|
5658
|
+
}
|
|
5659
|
+
if (this.state === "open") {
|
|
5660
|
+
if (now - this.lastFailureAt >= this.resetTimeoutMs) {
|
|
5661
|
+
this.state = "half-open";
|
|
5662
|
+
this.halfOpenCalls = 0;
|
|
5663
|
+
return true;
|
|
5664
|
+
}
|
|
5665
|
+
return false;
|
|
5666
|
+
}
|
|
5667
|
+
if (this.state === "half-open") {
|
|
5668
|
+
return this.halfOpenCalls < this.halfOpenMaxCalls;
|
|
5669
|
+
}
|
|
5670
|
+
return true;
|
|
5671
|
+
}
|
|
5672
|
+
async wrap(fn) {
|
|
5673
|
+
if (!this.canExecute()) {
|
|
5674
|
+
const retryAfterSec = Math.ceil(
|
|
5675
|
+
Math.max(0, this.resetTimeoutMs - (Date.now() - this.lastFailureAt)) / 1e3
|
|
5676
|
+
);
|
|
5677
|
+
throw new Error(
|
|
5678
|
+
`Circuit breaker is open. Try again in ${retryAfterSec}s. Failed ${this.failureCount}/${this.failureThreshold} consecutive calls.`
|
|
5679
|
+
);
|
|
5680
|
+
}
|
|
5681
|
+
if (this.state === "half-open") {
|
|
5682
|
+
this.halfOpenCalls++;
|
|
5683
|
+
}
|
|
5684
|
+
try {
|
|
5685
|
+
const result = await fn();
|
|
5686
|
+
this.record(true);
|
|
5687
|
+
return result;
|
|
5688
|
+
} catch (err) {
|
|
5689
|
+
this.record(false);
|
|
5690
|
+
throw err;
|
|
5691
|
+
}
|
|
5692
|
+
}
|
|
5693
|
+
getState() {
|
|
5694
|
+
return this.state;
|
|
5695
|
+
}
|
|
5696
|
+
getFailureCount() {
|
|
5697
|
+
return this.failureCount;
|
|
5698
|
+
}
|
|
5699
|
+
reset() {
|
|
5700
|
+
this.state = "closed";
|
|
5701
|
+
this.failureCount = 0;
|
|
5702
|
+
this.successCount = 0;
|
|
5703
|
+
this.lastFailureAt = 0;
|
|
5704
|
+
this.halfOpenCalls = 0;
|
|
5705
|
+
}
|
|
5706
|
+
};
|
|
5707
|
+
|
|
5617
5708
|
// src/core/BatchProcessor.ts
|
|
5618
5709
|
function isTransientError(error) {
|
|
5619
5710
|
if (!(error instanceof Error)) return false;
|
|
@@ -5637,7 +5728,34 @@ function calculateBackoffDelay(attempt, initialDelayMs, maxDelayMs, multiplier)
|
|
|
5637
5728
|
function sleep(ms) {
|
|
5638
5729
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
5639
5730
|
}
|
|
5640
|
-
var
|
|
5731
|
+
var _BatchProcessor = class _BatchProcessor {
|
|
5732
|
+
/**
|
|
5733
|
+
* Execute a processor call through the circuit breaker.
|
|
5734
|
+
* Only transient failures count toward opening the circuit.
|
|
5735
|
+
*/
|
|
5736
|
+
static async executeWithCircuitBreaker(processor) {
|
|
5737
|
+
if (!this.cb.canExecute()) {
|
|
5738
|
+
const retryAfterSec = Math.ceil(
|
|
5739
|
+
Math.max(0, 3e4 - (Date.now() - this.cb.lastFailureAt || 0)) / 1e3
|
|
5740
|
+
);
|
|
5741
|
+
throw new Error(
|
|
5742
|
+
`[BatchProcessor] Circuit breaker is open. Try again in ${retryAfterSec}s.`
|
|
5743
|
+
);
|
|
5744
|
+
}
|
|
5745
|
+
if (this.cb.state === "half-open") {
|
|
5746
|
+
this.cb.halfOpenCalls++;
|
|
5747
|
+
}
|
|
5748
|
+
try {
|
|
5749
|
+
const result = await processor();
|
|
5750
|
+
this.cb.record(true);
|
|
5751
|
+
return result;
|
|
5752
|
+
} catch (err) {
|
|
5753
|
+
if (isTransientError(err)) {
|
|
5754
|
+
this.cb.record(false);
|
|
5755
|
+
}
|
|
5756
|
+
throw err;
|
|
5757
|
+
}
|
|
5758
|
+
}
|
|
5641
5759
|
/**
|
|
5642
5760
|
* Processes an array of items in configurable batches with retry logic.
|
|
5643
5761
|
*
|
|
@@ -5678,7 +5796,7 @@ var BatchProcessor = class {
|
|
|
5678
5796
|
let lastError;
|
|
5679
5797
|
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
5680
5798
|
try {
|
|
5681
|
-
const result = await processor(batch);
|
|
5799
|
+
const result = await _BatchProcessor.executeWithCircuitBreaker(() => processor(batch));
|
|
5682
5800
|
results.push(result);
|
|
5683
5801
|
success = true;
|
|
5684
5802
|
break;
|
|
@@ -5736,7 +5854,7 @@ ${errorMessages}`
|
|
|
5736
5854
|
let lastError;
|
|
5737
5855
|
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
5738
5856
|
try {
|
|
5739
|
-
const result = await processor(item);
|
|
5857
|
+
const result = await _BatchProcessor.executeWithCircuitBreaker(() => processor(item));
|
|
5740
5858
|
results.push(result);
|
|
5741
5859
|
success = true;
|
|
5742
5860
|
break;
|
|
@@ -5809,7 +5927,7 @@ ${errorMessages}`
|
|
|
5809
5927
|
}));
|
|
5810
5928
|
const chunkPromises = chunk.map(async ({ item, originalIndex }) => {
|
|
5811
5929
|
try {
|
|
5812
|
-
const result = await processor(item);
|
|
5930
|
+
const result = await _BatchProcessor.executeWithCircuitBreaker(() => processor(item));
|
|
5813
5931
|
return { success: true, result, index: originalIndex };
|
|
5814
5932
|
} catch (err) {
|
|
5815
5933
|
const error = err instanceof Error ? err : new Error(String(err));
|
|
@@ -5837,6 +5955,12 @@ ${errorMessages}`
|
|
|
5837
5955
|
return { results, errors, totalProcessed, totalFailed };
|
|
5838
5956
|
}
|
|
5839
5957
|
};
|
|
5958
|
+
_BatchProcessor.cb = new CircuitBreaker({
|
|
5959
|
+
failureThreshold: 5,
|
|
5960
|
+
resetTimeoutMs: 3e4,
|
|
5961
|
+
halfOpenMaxCalls: 1
|
|
5962
|
+
});
|
|
5963
|
+
var BatchProcessor = _BatchProcessor;
|
|
5840
5964
|
|
|
5841
5965
|
// src/config/EmbeddingStrategy.ts
|
|
5842
5966
|
var EmbeddingStrategyResolver = class {
|
|
@@ -5934,7 +6058,7 @@ var QueryProcessor = class {
|
|
|
5934
6058
|
* @param validFields Optional list of known filterable fields to look for
|
|
5935
6059
|
*/
|
|
5936
6060
|
static extractQueryFieldHints(question, validFields = []) {
|
|
5937
|
-
var
|
|
6061
|
+
var _a3, _b, _c, _d;
|
|
5938
6062
|
if (!question.trim()) return [];
|
|
5939
6063
|
const hints = /* @__PURE__ */ new Map();
|
|
5940
6064
|
const addHint = (value, field) => {
|
|
@@ -6014,7 +6138,7 @@ var QueryProcessor = class {
|
|
|
6014
6138
|
];
|
|
6015
6139
|
for (const p of universalPatterns) {
|
|
6016
6140
|
for (const match of question.matchAll(p.regex)) {
|
|
6017
|
-
const val = p.group ? (
|
|
6141
|
+
const val = p.group ? (_a3 = match[p.group]) != null ? _a3 : match[0] : match[0];
|
|
6018
6142
|
if (!val) continue;
|
|
6019
6143
|
if (p.field) addHint(val, p.field);
|
|
6020
6144
|
else addHint(val);
|
|
@@ -6238,11 +6362,11 @@ var LLMRouter = class {
|
|
|
6238
6362
|
* When provided it is used directly as the 'default' role without re-constructing.
|
|
6239
6363
|
*/
|
|
6240
6364
|
async initialize(prebuiltDefault) {
|
|
6241
|
-
var
|
|
6365
|
+
var _a3;
|
|
6242
6366
|
const defaultModel = prebuiltDefault != null ? prebuiltDefault : LLMFactory.create(this.config.llm, this.config.embedding);
|
|
6243
6367
|
this.models.set("default", defaultModel);
|
|
6244
6368
|
const envFastModel = process.env.FAST_LLM_MODEL;
|
|
6245
|
-
const providerFastDefault = (
|
|
6369
|
+
const providerFastDefault = (_a3 = FAST_MODEL_DEFAULTS[this.config.llm.provider]) != null ? _a3 : "";
|
|
6246
6370
|
const fastModelName = envFastModel || providerFastDefault;
|
|
6247
6371
|
if (fastModelName && fastModelName !== this.config.llm.model) {
|
|
6248
6372
|
console.log(`[LLMRouter] Fast role \u2192 provider="${this.config.llm.provider}" model="${fastModelName}"`);
|
|
@@ -6261,8 +6385,8 @@ var LLMRouter = class {
|
|
|
6261
6385
|
* Falls back to 'default' if the requested role is not registered.
|
|
6262
6386
|
*/
|
|
6263
6387
|
get(role) {
|
|
6264
|
-
var
|
|
6265
|
-
const provider = (
|
|
6388
|
+
var _a3;
|
|
6389
|
+
const provider = (_a3 = this.models.get(role)) != null ? _a3 : this.models.get("default");
|
|
6266
6390
|
if (!provider) {
|
|
6267
6391
|
throw new Error(`[LLMRouter] No provider registered for role: "${role}". Did you call initialize()?`);
|
|
6268
6392
|
}
|
|
@@ -6348,8 +6472,8 @@ var IntentClassifier = class {
|
|
|
6348
6472
|
numericFieldCount = numericKeys.size;
|
|
6349
6473
|
categoricalFieldCount = catKeys.size;
|
|
6350
6474
|
if (productKeyMatches >= 2 || docs.some((d) => {
|
|
6351
|
-
var
|
|
6352
|
-
return ((
|
|
6475
|
+
var _a3, _b;
|
|
6476
|
+
return ((_a3 = d == null ? void 0 : d.content) != null ? _a3 : "").toLowerCase().includes("price") || ((_b = d == null ? void 0 : d.content) != null ? _b : "").toLowerCase().includes("brand");
|
|
6353
6477
|
})) {
|
|
6354
6478
|
isProductLike = true;
|
|
6355
6479
|
}
|
|
@@ -6631,8 +6755,8 @@ var TextRendererStrategy = class {
|
|
|
6631
6755
|
}
|
|
6632
6756
|
const docs = Array.isArray(data) ? data : [];
|
|
6633
6757
|
const text = docs.map((d) => {
|
|
6634
|
-
var
|
|
6635
|
-
return (
|
|
6758
|
+
var _a3;
|
|
6759
|
+
return (_a3 = d == null ? void 0 : d.content) != null ? _a3 : "";
|
|
6636
6760
|
}).join("\n\n");
|
|
6637
6761
|
return { type: "text", title, data: { content: text || "No detailed content available." } };
|
|
6638
6762
|
}
|
|
@@ -6775,9 +6899,9 @@ var LRUDecisionCache = class {
|
|
|
6775
6899
|
this.maxSize = maxSize;
|
|
6776
6900
|
}
|
|
6777
6901
|
generateKey(context) {
|
|
6778
|
-
var
|
|
6902
|
+
var _a3, _b;
|
|
6779
6903
|
const q = (context.userQuery || "").toLowerCase().trim();
|
|
6780
|
-
const docCount = (_b = (
|
|
6904
|
+
const docCount = (_b = (_a3 = context.retrievedDocuments) == null ? void 0 : _a3.length) != null ? _b : 0;
|
|
6781
6905
|
return `${q}::docs:${docCount}`;
|
|
6782
6906
|
}
|
|
6783
6907
|
get(context) {
|
|
@@ -6878,7 +7002,7 @@ var UITransformer = class _UITransformer {
|
|
|
6878
7002
|
* Prefer `analyzeAndDecide()` in production.
|
|
6879
7003
|
*/
|
|
6880
7004
|
static transform(userQuery, retrievedData, config, trainedSchema, intent) {
|
|
6881
|
-
var
|
|
7005
|
+
var _a3, _b, _c;
|
|
6882
7006
|
if (!retrievedData || retrievedData.length === 0) {
|
|
6883
7007
|
return this.createTextResponse("No data available", "No relevant data found for your query.");
|
|
6884
7008
|
}
|
|
@@ -6898,7 +7022,7 @@ var UITransformer = class _UITransformer {
|
|
|
6898
7022
|
return this.transformToBarChart(filteredData, profile, userQuery, resolvedIntent.visualizationHint === "ranking");
|
|
6899
7023
|
}
|
|
6900
7024
|
if (resolvedIntent.visualizationHint === "distribution") {
|
|
6901
|
-
return (
|
|
7025
|
+
return (_a3 = this.transformToHistogram(profile, userQuery)) != null ? _a3 : this.transformToBarChart(filteredData, profile, userQuery);
|
|
6902
7026
|
}
|
|
6903
7027
|
if (resolvedIntent.visualizationHint === "correlation") {
|
|
6904
7028
|
return (_b = this.transformToScatterPlot(profile, userQuery)) != null ? _b : this.transformToBarChart(filteredData, profile, userQuery);
|
|
@@ -7231,11 +7355,11 @@ ${schemaProfileText}` : ""}`;
|
|
|
7231
7355
|
};
|
|
7232
7356
|
}
|
|
7233
7357
|
static transformToPieChart(data, profile, query = "") {
|
|
7234
|
-
var
|
|
7358
|
+
var _a3;
|
|
7235
7359
|
const dimension = profile ? this.selectDimensionField(profile, query) : void 0;
|
|
7236
7360
|
const categories = dimension ? Array.from(new Set(profile.records.map((record) => {
|
|
7237
|
-
var
|
|
7238
|
-
return String((
|
|
7361
|
+
var _a4;
|
|
7362
|
+
return String((_a4 = record.fields[dimension.key]) != null ? _a4 : "");
|
|
7239
7363
|
}).filter(Boolean))) : this.detectCategories(data);
|
|
7240
7364
|
if (categories.length === 0) return null;
|
|
7241
7365
|
const categoryData = dimension && profile ? this.aggregateProfileByDimension(profile, dimension.key) : this.aggregateByCategory(data, categories);
|
|
@@ -7245,7 +7369,7 @@ ${schemaProfileText}` : ""}`;
|
|
|
7245
7369
|
});
|
|
7246
7370
|
return {
|
|
7247
7371
|
type: "pie_chart",
|
|
7248
|
-
title: `Distribution by ${(
|
|
7372
|
+
title: `Distribution by ${(_a3 = dimension == null ? void 0 : dimension.label) != null ? _a3 : "Category"}`,
|
|
7249
7373
|
description: `Showing breakdown across ${pieData.length} categories`,
|
|
7250
7374
|
data: pieData
|
|
7251
7375
|
};
|
|
@@ -7255,8 +7379,8 @@ ${schemaProfileText}` : ""}`;
|
|
|
7255
7379
|
const valueField = profile.numericFields[0];
|
|
7256
7380
|
const buckets = /* @__PURE__ */ new Map();
|
|
7257
7381
|
profile.records.forEach((record) => {
|
|
7258
|
-
var
|
|
7259
|
-
const timestamp = String((
|
|
7382
|
+
var _a3, _b, _c;
|
|
7383
|
+
const timestamp = String((_a3 = record.fields[dateField.key]) != null ? _a3 : "");
|
|
7260
7384
|
const value = (_b = this.toFiniteNumber(record.fields[valueField.key])) != null ? _b : 0;
|
|
7261
7385
|
buckets.set(timestamp, ((_c = buckets.get(timestamp)) != null ? _c : 0) + value);
|
|
7262
7386
|
});
|
|
@@ -7269,23 +7393,23 @@ ${schemaProfileText}` : ""}`;
|
|
|
7269
7393
|
};
|
|
7270
7394
|
}
|
|
7271
7395
|
static transformToBarChart(data, profile, query = "", horizontal = false) {
|
|
7272
|
-
var
|
|
7396
|
+
var _a3;
|
|
7273
7397
|
const dimension = profile ? this.selectDimensionField(profile, query) : void 0;
|
|
7274
7398
|
const measure = profile ? this.selectNumericField(profile, query) : void 0;
|
|
7275
7399
|
const aggregate = dimension && profile ? this.aggregateProfileByDimension(profile, dimension.key, measure == null ? void 0 : measure.key) : this.aggregateByCategory(data, this.detectCategories(data));
|
|
7276
7400
|
const barData = Object.entries(aggregate).map(([category, value]) => ({ category, value: Number(value) })).sort((a, b) => horizontal ? b.value - a.value : 0).slice(0, 12);
|
|
7277
7401
|
const fallbackData = barData.length > 0 ? barData : data.slice(0, 12).map((item, index) => {
|
|
7278
|
-
var
|
|
7402
|
+
var _a4, _b, _c, _d, _e;
|
|
7279
7403
|
const meta = item.metadata || {};
|
|
7280
7404
|
const label = String(
|
|
7281
|
-
(_c = (_b = (
|
|
7405
|
+
(_c = (_b = (_a4 = this.getDynamicVal(meta, "name")) != null ? _a4 : meta.label) != null ? _b : meta.title) != null ? _c : `Result ${index + 1}`
|
|
7282
7406
|
);
|
|
7283
7407
|
const value = (_e = (_d = this.extractNumericValue(meta)) != null ? _d : item.score) != null ? _e : 0;
|
|
7284
7408
|
return { category: label, value: Number(value) };
|
|
7285
7409
|
});
|
|
7286
7410
|
return {
|
|
7287
7411
|
type: horizontal ? "horizontal_bar" : "bar_chart",
|
|
7288
|
-
title: dimension ? `${(
|
|
7412
|
+
title: dimension ? `${(_a3 = measure == null ? void 0 : measure.label) != null ? _a3 : "Count"} by ${dimension.label}` : "Comparison",
|
|
7289
7413
|
description: `Showing ${fallbackData.length} comparable values`,
|
|
7290
7414
|
data: fallbackData
|
|
7291
7415
|
};
|
|
@@ -7320,11 +7444,11 @@ ${schemaProfileText}` : ""}`;
|
|
|
7320
7444
|
if (fields.length < 2) return null;
|
|
7321
7445
|
const [xField, yField] = fields;
|
|
7322
7446
|
const points = profile.records.map((record) => {
|
|
7323
|
-
var
|
|
7447
|
+
var _a3;
|
|
7324
7448
|
const x = this.toFiniteNumber(record.fields[xField.key]);
|
|
7325
7449
|
const y = this.toFiniteNumber(record.fields[yField.key]);
|
|
7326
7450
|
if (x === null || y === null) return null;
|
|
7327
|
-
return { x, y, label: String((
|
|
7451
|
+
return { x, y, label: String((_a3 = this.getRecordLabel(record)) != null ? _a3 : record.id) };
|
|
7328
7452
|
}).filter((point) => point !== null).slice(0, 100);
|
|
7329
7453
|
if (points.length === 0) return null;
|
|
7330
7454
|
return {
|
|
@@ -7354,9 +7478,9 @@ ${schemaProfileText}` : ""}`;
|
|
|
7354
7478
|
static transformToRadarChart(data) {
|
|
7355
7479
|
const attributeMap = {};
|
|
7356
7480
|
data.forEach((item) => {
|
|
7357
|
-
var
|
|
7481
|
+
var _a3, _b, _c;
|
|
7358
7482
|
const meta = item.metadata || {};
|
|
7359
|
-
const seriesName = String((_c = (_b = (
|
|
7483
|
+
const seriesName = String((_c = (_b = (_a3 = meta.name) != null ? _a3 : meta.product) != null ? _b : item.id) != null ? _c : "Item");
|
|
7360
7484
|
Object.entries(meta).forEach(([key, val]) => {
|
|
7361
7485
|
if (typeof val === "number" && !["price", "id"].includes(key.toLowerCase())) {
|
|
7362
7486
|
if (!attributeMap[key]) attributeMap[key] = {};
|
|
@@ -7372,8 +7496,8 @@ ${schemaProfileText}` : ""}`;
|
|
|
7372
7496
|
title: "Product Comparison",
|
|
7373
7497
|
description: `Comparing ${data.length} items across ${radarData.length} attributes`,
|
|
7374
7498
|
data: radarData.length > 0 ? radarData : data.map((d) => {
|
|
7375
|
-
var
|
|
7376
|
-
return { attribute: ((
|
|
7499
|
+
var _a3;
|
|
7500
|
+
return { attribute: ((_a3 = d == null ? void 0 : d.content) != null ? _a3 : "").substring(0, 40) };
|
|
7377
7501
|
})
|
|
7378
7502
|
};
|
|
7379
7503
|
}
|
|
@@ -7392,8 +7516,8 @@ ${schemaProfileText}` : ""}`;
|
|
|
7392
7516
|
return this.createTextResponse(
|
|
7393
7517
|
"Retrieved Context",
|
|
7394
7518
|
data.map((item) => {
|
|
7395
|
-
var
|
|
7396
|
-
return (
|
|
7519
|
+
var _a3;
|
|
7520
|
+
return (_a3 = item == null ? void 0 : item.content) != null ? _a3 : "";
|
|
7397
7521
|
}).join("\n\n"),
|
|
7398
7522
|
`Found ${data.length} relevant results`
|
|
7399
7523
|
);
|
|
@@ -7444,11 +7568,11 @@ ${schemaProfileText}` : ""}`;
|
|
|
7444
7568
|
return null;
|
|
7445
7569
|
}
|
|
7446
7570
|
static normalizeTransformation(payload) {
|
|
7447
|
-
var
|
|
7571
|
+
var _a3, _b, _c, _d, _e, _f, _g2, _h, _i, _j;
|
|
7448
7572
|
if (!payload || typeof payload !== "object") return null;
|
|
7449
7573
|
const p = payload;
|
|
7450
7574
|
const type = this.normalizeVisualizationType(
|
|
7451
|
-
String((_c = (_b = (
|
|
7575
|
+
String((_c = (_b = (_a3 = p.type) != null ? _a3 : p.view) != null ? _b : p.chartType) != null ? _c : "")
|
|
7452
7576
|
);
|
|
7453
7577
|
if (!type) return null;
|
|
7454
7578
|
const title = String((_e = (_d = p.title) != null ? _d : p.heading) != null ? _e : "Visualization");
|
|
@@ -7459,7 +7583,7 @@ ${schemaProfileText}` : ""}`;
|
|
|
7459
7583
|
return this.validateTransformation(transformation) ? transformation : null;
|
|
7460
7584
|
}
|
|
7461
7585
|
static normalizeVisualizationType(type) {
|
|
7462
|
-
var
|
|
7586
|
+
var _a3;
|
|
7463
7587
|
const mapping = {
|
|
7464
7588
|
pie: "pie_chart",
|
|
7465
7589
|
pie_chart: "pie_chart",
|
|
@@ -7487,7 +7611,7 @@ ${schemaProfileText}` : ""}`;
|
|
|
7487
7611
|
product_carousel: "product_carousel",
|
|
7488
7612
|
carousel: "carousel"
|
|
7489
7613
|
};
|
|
7490
|
-
return (
|
|
7614
|
+
return (_a3 = mapping[type.toLowerCase()]) != null ? _a3 : null;
|
|
7491
7615
|
}
|
|
7492
7616
|
static validateTransformation(t) {
|
|
7493
7617
|
const { type, data } = t;
|
|
@@ -7603,7 +7727,7 @@ ${schemaProfileText}` : ""}`;
|
|
|
7603
7727
|
}
|
|
7604
7728
|
static profileData(data) {
|
|
7605
7729
|
const records = (data || []).filter((item) => Boolean(item && typeof item === "object")).map((item) => {
|
|
7606
|
-
var
|
|
7730
|
+
var _a3, _b;
|
|
7607
7731
|
const fields2 = {};
|
|
7608
7732
|
Object.entries(item.metadata || {}).forEach(([key, value]) => {
|
|
7609
7733
|
const primitive = this.toPrimitive(value);
|
|
@@ -7612,7 +7736,7 @@ ${schemaProfileText}` : ""}`;
|
|
|
7612
7736
|
if (!fields2.content && item.content) fields2.content = item.content.substring(0, 500);
|
|
7613
7737
|
return {
|
|
7614
7738
|
id: item.id,
|
|
7615
|
-
content: (
|
|
7739
|
+
content: (_a3 = item.content) != null ? _a3 : "",
|
|
7616
7740
|
score: (_b = item.score) != null ? _b : 0,
|
|
7617
7741
|
fields: fields2,
|
|
7618
7742
|
source: item
|
|
@@ -7693,16 +7817,16 @@ ${schemaProfileText}` : ""}`;
|
|
|
7693
7817
|
return null;
|
|
7694
7818
|
}
|
|
7695
7819
|
static selectDimensionField(profile, query) {
|
|
7696
|
-
var
|
|
7820
|
+
var _a3, _b;
|
|
7697
7821
|
const productCategory = profile.categoricalFields.find(
|
|
7698
7822
|
(field) => /category|department|collection|type|group|segment|region|country|state|city/i.test(field.key)
|
|
7699
7823
|
);
|
|
7700
7824
|
const ranked = this.rankFieldsByQuery(profile.categoricalFields, query);
|
|
7701
|
-
return (_b = (
|
|
7825
|
+
return (_b = (_a3 = ranked[0]) != null ? _a3 : productCategory) != null ? _b : profile.categoricalFields[0];
|
|
7702
7826
|
}
|
|
7703
7827
|
static selectNumericField(profile, query) {
|
|
7704
|
-
var
|
|
7705
|
-
return (
|
|
7828
|
+
var _a3;
|
|
7829
|
+
return (_a3 = this.rankFieldsByQuery(profile.numericFields, query)[0]) != null ? _a3 : profile.numericFields[0];
|
|
7706
7830
|
}
|
|
7707
7831
|
static rankFieldsByQuery(fields, query) {
|
|
7708
7832
|
const q = query.toLowerCase();
|
|
@@ -7731,8 +7855,8 @@ ${schemaProfileText}` : ""}`;
|
|
|
7731
7855
|
static aggregateProfileByDimension(profile, dimensionKey, measureKey) {
|
|
7732
7856
|
const result = {};
|
|
7733
7857
|
profile.records.forEach((record) => {
|
|
7734
|
-
var
|
|
7735
|
-
const category = String((
|
|
7858
|
+
var _a3, _b, _c;
|
|
7859
|
+
const category = String((_a3 = record.fields[dimensionKey]) != null ? _a3 : "Other").trim() || "Other";
|
|
7736
7860
|
const value = measureKey ? (_b = this.toFiniteNumber(record.fields[measureKey])) != null ? _b : 0 : 1;
|
|
7737
7861
|
result[category] = ((_c = result[category]) != null ? _c : 0) + value;
|
|
7738
7862
|
});
|
|
@@ -7812,8 +7936,8 @@ ${schemaProfileText}` : ""}`;
|
|
|
7812
7936
|
static aggregateByCategory(data, categories) {
|
|
7813
7937
|
const result = Object.fromEntries(categories.map((c) => [c, 0]));
|
|
7814
7938
|
data.forEach((item) => {
|
|
7815
|
-
var
|
|
7816
|
-
const cat = (
|
|
7939
|
+
var _a3;
|
|
7940
|
+
const cat = (_a3 = this.getProductCategory(item)) != null ? _a3 : "Other";
|
|
7817
7941
|
if (Object.prototype.hasOwnProperty.call(result, cat)) result[cat]++;
|
|
7818
7942
|
else result["Other"] = (result["Other"] || 0) + 1;
|
|
7819
7943
|
});
|
|
@@ -7821,17 +7945,17 @@ ${schemaProfileText}` : ""}`;
|
|
|
7821
7945
|
}
|
|
7822
7946
|
static extractTimeSeriesData(data) {
|
|
7823
7947
|
return data.map((item) => {
|
|
7824
|
-
var
|
|
7948
|
+
var _a3, _b, _c, _d, _e, _f;
|
|
7825
7949
|
const meta = item.metadata || {};
|
|
7826
7950
|
return {
|
|
7827
|
-
timestamp: (_b = (
|
|
7951
|
+
timestamp: (_b = (_a3 = meta.timestamp) != null ? _a3 : meta.date) != null ? _b : (/* @__PURE__ */ new Date()).toISOString(),
|
|
7828
7952
|
value: (_d = (_c = meta.value) != null ? _c : item.score) != null ? _d : 0,
|
|
7829
7953
|
label: (_f = meta.label) != null ? _f : ((_e = item == null ? void 0 : item.content) != null ? _e : "").substring(0, 50)
|
|
7830
7954
|
};
|
|
7831
7955
|
});
|
|
7832
7956
|
}
|
|
7833
7957
|
static extractNumericValue(meta) {
|
|
7834
|
-
var
|
|
7958
|
+
var _a3;
|
|
7835
7959
|
const preferredKeys = [
|
|
7836
7960
|
"value",
|
|
7837
7961
|
"count",
|
|
@@ -7846,7 +7970,7 @@ ${schemaProfileText}` : ""}`;
|
|
|
7846
7970
|
"price"
|
|
7847
7971
|
];
|
|
7848
7972
|
for (const key of preferredKeys) {
|
|
7849
|
-
const raw = (
|
|
7973
|
+
const raw = (_a3 = resolveMetadataValue(meta, key)) != null ? _a3 : meta[key];
|
|
7850
7974
|
const value = typeof raw === "number" ? raw : Number(String(raw != null ? raw : "").replace(/[$,% ,]/g, ""));
|
|
7851
7975
|
if (Number.isFinite(value)) return value;
|
|
7852
7976
|
}
|
|
@@ -7941,8 +8065,8 @@ ${schemaProfileText}` : ""}`;
|
|
|
7941
8065
|
}, query.includes(normalizedField) ? 3 : 0);
|
|
7942
8066
|
}
|
|
7943
8067
|
static resolveTableCellValue(item, column) {
|
|
7944
|
-
var
|
|
7945
|
-
if (column === "Content") return ((
|
|
8068
|
+
var _a3, _b, _c;
|
|
8069
|
+
if (column === "Content") return ((_a3 = item == null ? void 0 : item.content) != null ? _a3 : "").substring(0, 100);
|
|
7946
8070
|
const meta = item.metadata || {};
|
|
7947
8071
|
const normalizedColumn = this.normalizeComparableField(column);
|
|
7948
8072
|
const exactMetadata = Object.entries(meta).find(
|
|
@@ -7994,8 +8118,8 @@ ${schemaProfileText}` : ""}`;
|
|
|
7994
8118
|
let inStock = 0;
|
|
7995
8119
|
let outOfStock = 0;
|
|
7996
8120
|
data.forEach((d) => {
|
|
7997
|
-
var
|
|
7998
|
-
const cat = (
|
|
8121
|
+
var _a3, _b;
|
|
8122
|
+
const cat = (_a3 = this.getProductCategory(d)) != null ? _a3 : "Other";
|
|
7999
8123
|
if (cat === category) {
|
|
8000
8124
|
const quantity = (_b = this.extractStockQuantity(d)) != null ? _b : 1;
|
|
8001
8125
|
if (this.determineStockStatus(d)) inStock += quantity;
|
|
@@ -8041,9 +8165,9 @@ ${schemaProfileText}` : ""}`;
|
|
|
8041
8165
|
}
|
|
8042
8166
|
// ─── Product Extraction ───────────────────────────────────────────────────
|
|
8043
8167
|
static getDynamicVal(meta, uiKey, config, trainedSchema) {
|
|
8044
|
-
var
|
|
8168
|
+
var _a3;
|
|
8045
8169
|
if (!meta) return void 0;
|
|
8046
|
-
const mapping = (
|
|
8170
|
+
const mapping = (_a3 = config == null ? void 0 : config.rag) == null ? void 0 : _a3.uiMapping;
|
|
8047
8171
|
if ((mapping == null ? void 0 : mapping[uiKey]) && meta[mapping[uiKey]] !== void 0) return meta[mapping[uiKey]];
|
|
8048
8172
|
if (trainedSchema && typeof trainedSchema === "object") {
|
|
8049
8173
|
const trainedKey = trainedSchema[uiKey];
|
|
@@ -8052,7 +8176,7 @@ ${schemaProfileText}` : ""}`;
|
|
|
8052
8176
|
return resolveMetadataValue(meta, uiKey);
|
|
8053
8177
|
}
|
|
8054
8178
|
static extractProductInfo(item, config, trainedSchema) {
|
|
8055
|
-
var
|
|
8179
|
+
var _a3, _b;
|
|
8056
8180
|
if (!item) return null;
|
|
8057
8181
|
const meta = item.metadata || {};
|
|
8058
8182
|
const content = item.content || "";
|
|
@@ -8060,7 +8184,7 @@ ${schemaProfileText}` : ""}`;
|
|
|
8060
8184
|
const price = this.getDynamicVal(meta, "price", config, trainedSchema);
|
|
8061
8185
|
const brand = this.getDynamicVal(meta, "brand", config, trainedSchema);
|
|
8062
8186
|
const description = this.cleanProductDescription(
|
|
8063
|
-
(
|
|
8187
|
+
(_a3 = this.extractProductDescriptionFromContent(content)) != null ? _a3 : this.getProductDescriptionValue(meta, config, trainedSchema)
|
|
8064
8188
|
);
|
|
8065
8189
|
if (name || this.isProductData(item)) {
|
|
8066
8190
|
let finalName = name ? String(name) : void 0;
|
|
@@ -8195,10 +8319,10 @@ RULES:
|
|
|
8195
8319
|
}
|
|
8196
8320
|
static buildContextSummary(sources, maxChars = 6e3) {
|
|
8197
8321
|
const items = (sources || []).filter(Boolean).map((s, i) => {
|
|
8198
|
-
var
|
|
8322
|
+
var _a3, _b, _c, _d;
|
|
8199
8323
|
return {
|
|
8200
8324
|
index: i + 1,
|
|
8201
|
-
content: (_b = (
|
|
8325
|
+
content: (_b = (_a3 = s == null ? void 0 : s.content) == null ? void 0 : _a3.substring(0, 400)) != null ? _b : "",
|
|
8202
8326
|
metadata: (_c = s == null ? void 0 : s.metadata) != null ? _c : {},
|
|
8203
8327
|
score: (_d = s == null ? void 0 : s.score) != null ? _d : 0
|
|
8204
8328
|
};
|
|
@@ -8238,7 +8362,7 @@ var SchemaMapper = class {
|
|
|
8238
8362
|
return promise;
|
|
8239
8363
|
}
|
|
8240
8364
|
static async _doTrain(llm, cacheKey, keys) {
|
|
8241
|
-
var
|
|
8365
|
+
var _a3, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k;
|
|
8242
8366
|
console.log(`[SchemaMapper] \u{1F9E0} Training on new schema keys: ${keys.join(", ")}`);
|
|
8243
8367
|
const propertyList = Object.entries(this.TARGET_PROPERTIES).map(([prop, desc]) => `- ${prop} (${desc})`).join("\n");
|
|
8244
8368
|
const messages = [
|
|
@@ -8254,7 +8378,7 @@ Return a JSON object like {"name":"Title","price":"Price",...}. Omit unmapped pr
|
|
|
8254
8378
|
}
|
|
8255
8379
|
];
|
|
8256
8380
|
try {
|
|
8257
|
-
const baseUrl = (
|
|
8381
|
+
const baseUrl = (_a3 = llm == null ? void 0 : llm.baseUrl) != null ? _a3 : "";
|
|
8258
8382
|
const apiKey = (_b = llm == null ? void 0 : llm.apiKey) != null ? _b : "";
|
|
8259
8383
|
const model = (_c = llm == null ? void 0 : llm.model) != null ? _c : "llama-3.1-8b-instant";
|
|
8260
8384
|
let responseText;
|
|
@@ -8429,9 +8553,9 @@ var Pipeline = class {
|
|
|
8429
8553
|
this.initialised = false;
|
|
8430
8554
|
/** Namespace-specific static cold context cache for CAG */
|
|
8431
8555
|
this.coldContexts = /* @__PURE__ */ new Map();
|
|
8432
|
-
var
|
|
8556
|
+
var _a3, _b, _c, _d, _e;
|
|
8433
8557
|
this.chunker = new DocumentChunker(
|
|
8434
|
-
(_b = (
|
|
8558
|
+
(_b = (_a3 = config.rag) == null ? void 0 : _a3.chunkSize) != null ? _b : 1e3,
|
|
8435
8559
|
(_d = (_c = config.rag) == null ? void 0 : _c.chunkOverlap) != null ? _d : 200
|
|
8436
8560
|
);
|
|
8437
8561
|
if (((_e = config.rag) == null ? void 0 : _e.chunkingStrategy) === "llamaindex") {
|
|
@@ -8447,7 +8571,7 @@ var Pipeline = class {
|
|
|
8447
8571
|
return this.initialised ? this.llmProvider : void 0;
|
|
8448
8572
|
}
|
|
8449
8573
|
async initialize() {
|
|
8450
|
-
var
|
|
8574
|
+
var _a3, _b, _c, _d;
|
|
8451
8575
|
if (this.initialised) return;
|
|
8452
8576
|
const chartInstruction = `You are a helpful assistant. Use the provided context to answer questions accurately.
|
|
8453
8577
|
|
|
@@ -8490,7 +8614,7 @@ var Pipeline = class {
|
|
|
8490
8614
|
this.entityExtractor = new EntityExtractor(this.llmProvider);
|
|
8491
8615
|
}
|
|
8492
8616
|
await this.vectorDB.initialize();
|
|
8493
|
-
if (((
|
|
8617
|
+
if (((_a3 = this.config.rag) == null ? void 0 : _a3.architecture) === "agentic" || ((_b = this.config.rag) == null ? void 0 : _b.architecture) === "multi-agent" || this.config.mcpServers && this.config.mcpServers.length > 0) {
|
|
8494
8618
|
this.mcpRegistry = new MCPRegistry(this.config.mcpServers || []);
|
|
8495
8619
|
this.multiAgentCoordinator = new MultiAgentCoordinator({
|
|
8496
8620
|
llmProvider: this.llmProvider,
|
|
@@ -8508,8 +8632,8 @@ var Pipeline = class {
|
|
|
8508
8632
|
this.initialised = true;
|
|
8509
8633
|
}
|
|
8510
8634
|
async loadColdContext(ns) {
|
|
8511
|
-
var
|
|
8512
|
-
const cagConfig = (
|
|
8635
|
+
var _a3, _b;
|
|
8636
|
+
const cagConfig = (_a3 = this.config.rag) == null ? void 0 : _a3.cag;
|
|
8513
8637
|
if (!cagConfig || !cagConfig.enabled) return;
|
|
8514
8638
|
try {
|
|
8515
8639
|
const coldNs = cagConfig.coldNamespace || ns;
|
|
@@ -8562,12 +8686,12 @@ ${m.content}`).join("\n\n---\n\n");
|
|
|
8562
8686
|
}
|
|
8563
8687
|
/** Step 1: Chunk the document content. */
|
|
8564
8688
|
async prepareChunks(doc) {
|
|
8565
|
-
var
|
|
8689
|
+
var _a3, _b;
|
|
8566
8690
|
if (this.llamaIngestor) {
|
|
8567
8691
|
return this.llamaIngestor.chunk(doc.content, {
|
|
8568
8692
|
docId: doc.docId,
|
|
8569
8693
|
metadata: doc.metadata,
|
|
8570
|
-
chunkSize: (
|
|
8694
|
+
chunkSize: (_a3 = this.config.rag) == null ? void 0 : _a3.chunkSize,
|
|
8571
8695
|
chunkOverlap: (_b = this.config.rag) == null ? void 0 : _b.chunkOverlap
|
|
8572
8696
|
});
|
|
8573
8697
|
}
|
|
@@ -8665,9 +8789,9 @@ ${m.content}`).join("\n\n---\n\n");
|
|
|
8665
8789
|
return { reply, sources };
|
|
8666
8790
|
}
|
|
8667
8791
|
async ask(question, history = [], namespace) {
|
|
8668
|
-
var
|
|
8792
|
+
var _a3, _b;
|
|
8669
8793
|
await this.initialize();
|
|
8670
|
-
if ((((
|
|
8794
|
+
if ((((_a3 = this.config.rag) == null ? void 0 : _a3.architecture) === "agentic" || ((_b = this.config.rag) == null ? void 0 : _b.architecture) === "multi-agent" || this.config.mcpServers && this.config.mcpServers.length > 0) && this.multiAgentCoordinator) {
|
|
8671
8795
|
return await this.multiAgentCoordinator.run(question, history);
|
|
8672
8796
|
}
|
|
8673
8797
|
const stream = this.askStream(question, history, namespace);
|
|
@@ -8702,9 +8826,9 @@ ${m.content}`).join("\n\n---\n\n");
|
|
|
8702
8826
|
}
|
|
8703
8827
|
askStream(_0) {
|
|
8704
8828
|
return __asyncGenerator(this, arguments, function* (question, history = [], namespace) {
|
|
8705
|
-
var
|
|
8829
|
+
var _a3, _b;
|
|
8706
8830
|
yield new __await(this.initialize());
|
|
8707
|
-
if ((((
|
|
8831
|
+
if ((((_a3 = this.config.rag) == null ? void 0 : _a3.architecture) === "agentic" || ((_b = this.config.rag) == null ? void 0 : _b.architecture) === "multi-agent" || this.config.mcpServers && this.config.mcpServers.length > 0) && this.multiAgentCoordinator) {
|
|
8708
8832
|
const stream2 = this.multiAgentCoordinator.runStream(question, history);
|
|
8709
8833
|
try {
|
|
8710
8834
|
for (var iter = __forAwait(stream2), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
|
|
@@ -8753,10 +8877,10 @@ ${m.content}`).join("\n\n---\n\n");
|
|
|
8753
8877
|
*/
|
|
8754
8878
|
askStreamInternal(_0) {
|
|
8755
8879
|
return __asyncGenerator(this, arguments, function* (question, history = [], namespace) {
|
|
8756
|
-
var
|
|
8880
|
+
var _a3, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _A, _B;
|
|
8757
8881
|
yield new __await(this.initialize());
|
|
8758
8882
|
const ns = formatNamespace(namespace != null ? namespace : this.config.projectId);
|
|
8759
|
-
const topK = (_b = (
|
|
8883
|
+
const topK = (_b = (_a3 = this.config.rag) == null ? void 0 : _a3.topK) != null ? _b : 5;
|
|
8760
8884
|
const scoreThreshold = (_d = (_c = this.config.rag) == null ? void 0 : _c.scoreThreshold) != null ? _d : 0.3;
|
|
8761
8885
|
const requestId = `req_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
|
|
8762
8886
|
const requestStart = performance.now();
|
|
@@ -8805,8 +8929,8 @@ ${m.content}`).join("\n\n---\n\n");
|
|
|
8805
8929
|
const rerankStart = performance.now();
|
|
8806
8930
|
const structuredSources = this.applyStructuredFilters(rawSources, filter).filter((s) => Boolean(s && typeof s === "object"));
|
|
8807
8931
|
let fullSources = hasNumericPredicates ? structuredSources : structuredSources.filter((m) => {
|
|
8808
|
-
var
|
|
8809
|
-
return ((
|
|
8932
|
+
var _a4;
|
|
8933
|
+
return ((_a4 = m == null ? void 0 : m.score) != null ? _a4 : 0) >= scoreThreshold;
|
|
8810
8934
|
});
|
|
8811
8935
|
const rerankLimit = Math.max(retrievalLimit, fullSources.length);
|
|
8812
8936
|
const useReranking = arch !== "naive" && (arch === "retrieve-and-rerank" || ((_m = this.config.rag) == null ? void 0 : _m.useReranking));
|
|
@@ -8819,13 +8943,13 @@ ${m.content}`).join("\n\n---\n\n");
|
|
|
8819
8943
|
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]
|
|
8820
8944
|
|
|
8821
8945
|
` + promptSources.map((m, i) => {
|
|
8822
|
-
var
|
|
8946
|
+
var _a4;
|
|
8823
8947
|
return `[Source ${i + 1}]
|
|
8824
|
-
${(
|
|
8948
|
+
${(_a4 = m == null ? void 0 : m.content) != null ? _a4 : ""}`;
|
|
8825
8949
|
}).join("\n\n---\n\n") : "No relevant context found.";
|
|
8826
8950
|
const sources = [...fullSources].filter((s) => Boolean(s && typeof s === "object")).sort((a, b) => {
|
|
8827
|
-
var
|
|
8828
|
-
return ((
|
|
8951
|
+
var _a4, _b2;
|
|
8952
|
+
return ((_a4 = b == null ? void 0 : b.score) != null ? _a4 : 0) - ((_b2 = a == null ? void 0 : a.score) != null ? _b2 : 0);
|
|
8829
8953
|
});
|
|
8830
8954
|
if (graphData && graphData.nodes.length > 0) {
|
|
8831
8955
|
const graphContext = graphData.nodes.map(
|
|
@@ -8853,10 +8977,10 @@ ${context}`;
|
|
|
8853
8977
|
yield {
|
|
8854
8978
|
reply: "",
|
|
8855
8979
|
sources: (sources || []).filter((s) => Boolean(s && typeof s === "object")).map((s) => {
|
|
8856
|
-
var
|
|
8980
|
+
var _a4, _b2, _c2;
|
|
8857
8981
|
return {
|
|
8858
8982
|
id: s.id,
|
|
8859
|
-
score: (
|
|
8983
|
+
score: (_a4 = s.score) != null ? _a4 : 0,
|
|
8860
8984
|
content: (_b2 = s == null ? void 0 : s.content) != null ? _b2 : "",
|
|
8861
8985
|
metadata: (_c2 = s == null ? void 0 : s.metadata) != null ? _c2 : {},
|
|
8862
8986
|
namespace: ns
|
|
@@ -9049,11 +9173,11 @@ ${context}`;
|
|
|
9049
9173
|
systemPrompt,
|
|
9050
9174
|
userPrompt: question + finalRestrictionSuffix,
|
|
9051
9175
|
chunks: (sources || []).filter(Boolean).map((s) => {
|
|
9052
|
-
var
|
|
9176
|
+
var _a4, _b2;
|
|
9053
9177
|
return {
|
|
9054
9178
|
id: s.id,
|
|
9055
9179
|
score: s.score,
|
|
9056
|
-
content: (
|
|
9180
|
+
content: (_a4 = s == null ? void 0 : s.content) != null ? _a4 : "",
|
|
9057
9181
|
metadata: (_b2 = s == null ? void 0 : s.metadata) != null ? _b2 : {},
|
|
9058
9182
|
namespace: ns
|
|
9059
9183
|
};
|
|
@@ -9072,7 +9196,7 @@ ${context}`;
|
|
|
9072
9196
|
const telemetryUrl = ((_B = this.config.telemetry) == null ? void 0 : _B.url) || process.env.TELEMETRY_URL || process.env.NEXT_PUBLIC_TELEMETRY_URL || defaultUrl;
|
|
9073
9197
|
const absoluteUrl = telemetryUrl.startsWith("http") ? telemetryUrl : "https://www.retrivora.com" + (telemetryUrl.startsWith("/") ? telemetryUrl : "/" + telemetryUrl);
|
|
9074
9198
|
(async () => {
|
|
9075
|
-
var
|
|
9199
|
+
var _a4, _b2, _c2, _d2, _e2;
|
|
9076
9200
|
try {
|
|
9077
9201
|
let finalTrace = trace;
|
|
9078
9202
|
if (!awaitHallucination && runHallucination) {
|
|
@@ -9081,7 +9205,7 @@ ${context}`;
|
|
|
9081
9205
|
finalTrace = buildTrace(backgroundScoreResult);
|
|
9082
9206
|
}
|
|
9083
9207
|
}
|
|
9084
|
-
const modelName = (finalTrace == null ? void 0 : finalTrace.model) || ((
|
|
9208
|
+
const modelName = (finalTrace == null ? void 0 : finalTrace.model) || ((_a4 = this.config.llm) == null ? void 0 : _a4.model) || "llama-3.1-8b-instant";
|
|
9085
9209
|
const providerName = (finalTrace == null ? void 0 : finalTrace.provider) || ((_b2 = this.config.llm) == null ? void 0 : _b2.provider) || "groq";
|
|
9086
9210
|
const tokenCount = Number(((_c2 = finalTrace == null ? void 0 : finalTrace.tokens) == null ? void 0 : _c2.totalTokens) || 0);
|
|
9087
9211
|
const costEst = Number(((_d2 = finalTrace == null ? void 0 : finalTrace.tokens) == null ? void 0 : _d2.estimatedCostUsd) || 0);
|
|
@@ -9135,7 +9259,7 @@ ${context}`;
|
|
|
9135
9259
|
* Uses an LRU-bounded embedding cache to avoid re-embedding the same query.
|
|
9136
9260
|
*/
|
|
9137
9261
|
async generateUiTransformation(question, sources, cachedSchema, forceDeterministic = false) {
|
|
9138
|
-
var
|
|
9262
|
+
var _a3;
|
|
9139
9263
|
if (!sources || sources.length === 0) {
|
|
9140
9264
|
return UITransformer.transform(question, sources, this.config, cachedSchema);
|
|
9141
9265
|
}
|
|
@@ -9148,7 +9272,7 @@ ${context}`;
|
|
|
9148
9272
|
{ visualizationHint: "product_browse", recommendedChart: "text", filterInStockOnly: false, wantsExplicitTable: false, isTemporal: false, isComparison: false, language: "en" }
|
|
9149
9273
|
);
|
|
9150
9274
|
}
|
|
9151
|
-
const enableLlmUiTransform = ((
|
|
9275
|
+
const enableLlmUiTransform = ((_a3 = this.config.llm.options) == null ? void 0 : _a3.enableLlmUiTransform) === true;
|
|
9152
9276
|
if (forceDeterministic || !enableLlmUiTransform) {
|
|
9153
9277
|
return UITransformer.transform(question, sources, this.config, cachedSchema);
|
|
9154
9278
|
}
|
|
@@ -9166,15 +9290,15 @@ ${context}`;
|
|
|
9166
9290
|
const value = this.resolveNumericPredicateValue(source, predicate);
|
|
9167
9291
|
return value !== null && this.matchesNumericPredicate(value, predicate);
|
|
9168
9292
|
})).sort((a, b) => {
|
|
9169
|
-
var
|
|
9293
|
+
var _a3, _b;
|
|
9170
9294
|
const primary = predicates[0];
|
|
9171
|
-
const aValue = (
|
|
9295
|
+
const aValue = (_a3 = this.resolveNumericPredicateValue(a, primary)) != null ? _a3 : 0;
|
|
9172
9296
|
const bValue = (_b = this.resolveNumericPredicateValue(b, primary)) != null ? _b : 0;
|
|
9173
9297
|
return primary.operator === "lt" || primary.operator === "lte" ? aValue - bValue : bValue - aValue;
|
|
9174
9298
|
});
|
|
9175
9299
|
}
|
|
9176
9300
|
resolveNumericPredicateValue(source, predicate) {
|
|
9177
|
-
var
|
|
9301
|
+
var _a3;
|
|
9178
9302
|
if (!source) return null;
|
|
9179
9303
|
const meta = source.metadata || {};
|
|
9180
9304
|
const field = predicate.field;
|
|
@@ -9187,7 +9311,7 @@ ${context}`;
|
|
|
9187
9311
|
const value = this.toFiniteNumber(Array.isArray(fuzzy) ? fuzzy[1] : fuzzy.value);
|
|
9188
9312
|
if (value !== null) return value;
|
|
9189
9313
|
}
|
|
9190
|
-
const contentValue = this.extractNumericValueFromContent((
|
|
9314
|
+
const contentValue = this.extractNumericValueFromContent((_a3 = source.content) != null ? _a3 : "", field);
|
|
9191
9315
|
if (contentValue !== null) return contentValue;
|
|
9192
9316
|
}
|
|
9193
9317
|
for (const [key, value] of entries) {
|
|
@@ -9243,8 +9367,8 @@ ${context}`;
|
|
|
9243
9367
|
return Number.isFinite(numeric) ? numeric : null;
|
|
9244
9368
|
}
|
|
9245
9369
|
async retrieve(query, options) {
|
|
9246
|
-
var
|
|
9247
|
-
const ns = formatNamespace((
|
|
9370
|
+
var _a3, _b, _c, _d, _e, _f, _g2;
|
|
9371
|
+
const ns = formatNamespace((_a3 = options.namespace) != null ? _a3 : this.config.projectId);
|
|
9248
9372
|
const topK = (_b = options.topK) != null ? _b : 5;
|
|
9249
9373
|
const cacheKey = `${ns}::${query}`;
|
|
9250
9374
|
let queryVector = this.embeddingCache.get(cacheKey);
|
|
@@ -9289,11 +9413,11 @@ ${context}`;
|
|
|
9289
9413
|
namespace: ns,
|
|
9290
9414
|
count: resolvedSources.length,
|
|
9291
9415
|
sample: resolvedSources.slice(0, 2).map((s) => {
|
|
9292
|
-
var
|
|
9416
|
+
var _a4;
|
|
9293
9417
|
return {
|
|
9294
9418
|
id: s == null ? void 0 : s.id,
|
|
9295
9419
|
score: s == null ? void 0 : s.score,
|
|
9296
|
-
contentSnippet: String((
|
|
9420
|
+
contentSnippet: String((_a4 = s == null ? void 0 : s.content) != null ? _a4 : "").substring(0, 150),
|
|
9297
9421
|
metadata: s == null ? void 0 : s.metadata
|
|
9298
9422
|
};
|
|
9299
9423
|
})
|
|
@@ -9307,8 +9431,8 @@ Focus on extracting the core intent and entities. Do not answer the question, ju
|
|
|
9307
9431
|
|
|
9308
9432
|
History:
|
|
9309
9433
|
${(history || []).map((m) => {
|
|
9310
|
-
var
|
|
9311
|
-
return `${(m == null ? void 0 : m.role) || "user"}: ${(
|
|
9434
|
+
var _a3;
|
|
9435
|
+
return `${(m == null ? void 0 : m.role) || "user"}: ${(_a3 = m == null ? void 0 : m.content) != null ? _a3 : ""}`;
|
|
9312
9436
|
}).join("\n")}
|
|
9313
9437
|
|
|
9314
9438
|
New Question: ${question}
|
|
@@ -9335,8 +9459,8 @@ Optimized Search Query:`;
|
|
|
9335
9459
|
const { sources } = await this.retrieve(query, { namespace: ns, topK: 5 });
|
|
9336
9460
|
if (!sources || sources.length === 0) return [];
|
|
9337
9461
|
const context = sources.map((s) => {
|
|
9338
|
-
var
|
|
9339
|
-
return (
|
|
9462
|
+
var _a3;
|
|
9463
|
+
return (_a3 = s == null ? void 0 : s.content) != null ? _a3 : "";
|
|
9340
9464
|
}).join("\n\n---\n\n");
|
|
9341
9465
|
const prompt = `Based on the following snippets from a document, what are at least 5 short, relevant questions a user might ask?
|
|
9342
9466
|
Focus on questions that can be answered by the context.
|
|
@@ -9474,18 +9598,200 @@ var ProviderHealthCheck = class {
|
|
|
9474
9598
|
}
|
|
9475
9599
|
};
|
|
9476
9600
|
|
|
9601
|
+
// src/core/FreeTierLimitsGuard.ts
|
|
9602
|
+
var FREE_TIER_QUOTAS = {
|
|
9603
|
+
MAX_DOCUMENTS: 25,
|
|
9604
|
+
MAX_FILE_SIZE_BYTES: 20 * 1024 * 1024,
|
|
9605
|
+
MAX_STORAGE_BYTES: 250 * 1024 * 1024,
|
|
9606
|
+
MAX_TRIAL_REQUEST_UNITS: 500,
|
|
9607
|
+
MAX_DAILY_REQUEST_UNITS: 50,
|
|
9608
|
+
TRIAL_DURATION_DAYS: 14,
|
|
9609
|
+
GRACE_PERIOD_DAYS: 2,
|
|
9610
|
+
MAX_RPM: 10,
|
|
9611
|
+
MAX_RPH: 100,
|
|
9612
|
+
MAX_RPD: 500,
|
|
9613
|
+
MAX_CONCURRENT_REQUESTS: 2,
|
|
9614
|
+
MAX_INPUT_TOKENS: 2e4,
|
|
9615
|
+
MAX_OUTPUT_TOKENS: 4e3,
|
|
9616
|
+
UPGRADE_REMINDER_DAYS: [7, 12, 14],
|
|
9617
|
+
MAX_USERS: 1,
|
|
9618
|
+
MAX_NAMESPACES: 1,
|
|
9619
|
+
MAX_PROJECTS: 1
|
|
9620
|
+
};
|
|
9621
|
+
var FreeTierLimitsGuard = class {
|
|
9622
|
+
static calculateTrialTimestamps(startedAt) {
|
|
9623
|
+
const startDate = startedAt ? new Date(startedAt) : /* @__PURE__ */ new Date();
|
|
9624
|
+
const trialStartedAtMs = startDate.getTime();
|
|
9625
|
+
const trialExpiresAtMs = trialStartedAtMs + FREE_TIER_QUOTAS.TRIAL_DURATION_DAYS * 24 * 60 * 60 * 1e3;
|
|
9626
|
+
const gracePeriodExpiresAtMs = trialStartedAtMs + (FREE_TIER_QUOTAS.TRIAL_DURATION_DAYS + FREE_TIER_QUOTAS.GRACE_PERIOD_DAYS) * 24 * 60 * 60 * 1e3;
|
|
9627
|
+
return {
|
|
9628
|
+
trial_started_at: new Date(trialStartedAtMs).toISOString(),
|
|
9629
|
+
trial_expires_at: new Date(trialExpiresAtMs).toISOString(),
|
|
9630
|
+
grace_period_expires_at: new Date(gracePeriodExpiresAtMs).toISOString()
|
|
9631
|
+
};
|
|
9632
|
+
}
|
|
9633
|
+
static calculateRequestUnits(operationType, options) {
|
|
9634
|
+
var _a3;
|
|
9635
|
+
const op = operationType.toLowerCase().trim();
|
|
9636
|
+
if (op.includes("suggestion") || op.includes("template") || op.includes("history") || op.includes("model") || op.includes("health") || op.includes("auth") || op.includes("license") || op.includes("usage") || op.includes("feedback") || op.includes("analytic")) {
|
|
9637
|
+
return 0;
|
|
9638
|
+
}
|
|
9639
|
+
if (op.includes("image")) {
|
|
9640
|
+
return 10;
|
|
9641
|
+
}
|
|
9642
|
+
if (op.includes("embedding")) {
|
|
9643
|
+
const chunks = (_a3 = options == null ? void 0 : options.chunkCount) != null ? _a3 : 1;
|
|
9644
|
+
return Math.max(1, Math.ceil(chunks / 1e3));
|
|
9645
|
+
}
|
|
9646
|
+
return 1;
|
|
9647
|
+
}
|
|
9648
|
+
static checkTrialStatus(startedAt, totalUnitsUsed = 0, nowServerTime = /* @__PURE__ */ new Date()) {
|
|
9649
|
+
const timestamps = this.calculateTrialTimestamps(startedAt);
|
|
9650
|
+
const startMs = new Date(timestamps.trial_started_at).getTime();
|
|
9651
|
+
const expireMs = new Date(timestamps.trial_expires_at).getTime();
|
|
9652
|
+
const graceExpireMs = new Date(timestamps.grace_period_expires_at).getTime();
|
|
9653
|
+
const nowMs = nowServerTime.getTime();
|
|
9654
|
+
const daysElapsed = Math.floor(Math.max(0, nowMs - startMs) / (1e3 * 60 * 60 * 24));
|
|
9655
|
+
const daysRemaining = Math.max(0, Math.ceil((expireMs - nowMs) / (1e3 * 60 * 60 * 24)));
|
|
9656
|
+
const isConsumedUnits = totalUnitsUsed >= FREE_TIER_QUOTAS.MAX_TRIAL_REQUEST_UNITS;
|
|
9657
|
+
const isExpiredTime = nowMs > expireMs;
|
|
9658
|
+
const isPastGraceTime = nowMs > graceExpireMs;
|
|
9659
|
+
let status = "active";
|
|
9660
|
+
let reason;
|
|
9661
|
+
if (isPastGraceTime || isConsumedUnits && isExpiredTime) {
|
|
9662
|
+
status = "expired";
|
|
9663
|
+
reason = "Trial period and grace period have expired. Upgrade your plan.";
|
|
9664
|
+
} else if (isExpiredTime || isConsumedUnits) {
|
|
9665
|
+
status = "grace_period";
|
|
9666
|
+
reason = isConsumedUnits ? "Total trial request quota (500 units) consumed. Grace period active (dashboard access only)." : "14-day trial period expired. 2-day grace period active (dashboard access only).";
|
|
9667
|
+
}
|
|
9668
|
+
let upgradeReminderDue;
|
|
9669
|
+
if (FREE_TIER_QUOTAS.UPGRADE_REMINDER_DAYS.includes(daysElapsed)) {
|
|
9670
|
+
upgradeReminderDue = daysElapsed;
|
|
9671
|
+
}
|
|
9672
|
+
let usageReminderDue;
|
|
9673
|
+
const usagePercent = totalUnitsUsed / FREE_TIER_QUOTAS.MAX_TRIAL_REQUEST_UNITS * 100;
|
|
9674
|
+
if (usagePercent >= 100) {
|
|
9675
|
+
usageReminderDue = "100%";
|
|
9676
|
+
} else if (usagePercent >= 90) {
|
|
9677
|
+
usageReminderDue = "90%";
|
|
9678
|
+
} else if (usagePercent >= 80) {
|
|
9679
|
+
usageReminderDue = "80%";
|
|
9680
|
+
}
|
|
9681
|
+
return {
|
|
9682
|
+
status,
|
|
9683
|
+
daysElapsed,
|
|
9684
|
+
daysRemaining,
|
|
9685
|
+
isGracePeriod: status === "grace_period",
|
|
9686
|
+
isExpired: status === "expired",
|
|
9687
|
+
upgradeReminderDue,
|
|
9688
|
+
usageReminderDue,
|
|
9689
|
+
reason
|
|
9690
|
+
};
|
|
9691
|
+
}
|
|
9692
|
+
static checkIngestionAllowed(stats, incomingSizeBytes = 0) {
|
|
9693
|
+
var _a3, _b;
|
|
9694
|
+
const docs = (_a3 = stats.documentCount) != null ? _a3 : 0;
|
|
9695
|
+
const storage = (_b = stats.totalStorageBytes) != null ? _b : 0;
|
|
9696
|
+
if (incomingSizeBytes > FREE_TIER_QUOTAS.MAX_FILE_SIZE_BYTES) {
|
|
9697
|
+
const mbSize = (incomingSizeBytes / (1024 * 1024)).toFixed(1);
|
|
9698
|
+
return {
|
|
9699
|
+
allowed: false,
|
|
9700
|
+
reason: `[FreeTierIngestor] File size (${mbSize}MB) exceeds maximum allowed size of 20MB under Free Tier rules.`
|
|
9701
|
+
};
|
|
9702
|
+
}
|
|
9703
|
+
if (docs >= FREE_TIER_QUOTAS.MAX_DOCUMENTS) {
|
|
9704
|
+
return {
|
|
9705
|
+
allowed: false,
|
|
9706
|
+
reason: `Document limit reached (${docs}/${FREE_TIER_QUOTAS.MAX_DOCUMENTS}). Upgrade to Pro to ingest more documents.`
|
|
9707
|
+
};
|
|
9708
|
+
}
|
|
9709
|
+
if (storage + incomingSizeBytes > FREE_TIER_QUOTAS.MAX_STORAGE_BYTES) {
|
|
9710
|
+
const mbUsed = (storage / (1024 * 1024)).toFixed(1);
|
|
9711
|
+
return {
|
|
9712
|
+
allowed: false,
|
|
9713
|
+
reason: `Storage quota exceeded (${mbUsed}MB / 250MB). Upgrade for unlimited storage.`
|
|
9714
|
+
};
|
|
9715
|
+
}
|
|
9716
|
+
return { allowed: true };
|
|
9717
|
+
}
|
|
9718
|
+
static checkRequestAllowed(params) {
|
|
9719
|
+
var _a3, _b, _c;
|
|
9720
|
+
const op = (_a3 = params.operationType) != null ? _a3 : "chat";
|
|
9721
|
+
const costUnits = this.calculateRequestUnits(op, { chunkCount: params.chunkCount });
|
|
9722
|
+
if (costUnits === 0) {
|
|
9723
|
+
return { allowed: true, costUnits: 0 };
|
|
9724
|
+
}
|
|
9725
|
+
if (params.inputTokens && params.inputTokens > FREE_TIER_QUOTAS.MAX_INPUT_TOKENS) {
|
|
9726
|
+
return {
|
|
9727
|
+
allowed: false,
|
|
9728
|
+
reason: "Token limit exceeded. Upgrade your plan.",
|
|
9729
|
+
costUnits
|
|
9730
|
+
};
|
|
9731
|
+
}
|
|
9732
|
+
if (params.outputTokens && params.outputTokens > FREE_TIER_QUOTAS.MAX_OUTPUT_TOKENS) {
|
|
9733
|
+
return {
|
|
9734
|
+
allowed: false,
|
|
9735
|
+
reason: "Token limit exceeded. Upgrade your plan.",
|
|
9736
|
+
costUnits
|
|
9737
|
+
};
|
|
9738
|
+
}
|
|
9739
|
+
if (params.concurrentRequests && params.concurrentRequests > FREE_TIER_QUOTAS.MAX_CONCURRENT_REQUESTS) {
|
|
9740
|
+
return {
|
|
9741
|
+
allowed: false,
|
|
9742
|
+
reason: `Concurrent limit exceeded (max ${FREE_TIER_QUOTAS.MAX_CONCURRENT_REQUESTS} active requests). Please wait for active streams to finish.`,
|
|
9743
|
+
costUnits
|
|
9744
|
+
};
|
|
9745
|
+
}
|
|
9746
|
+
const totalUnits = (_b = params.totalUnitsUsed) != null ? _b : 0;
|
|
9747
|
+
const trialStatus = this.checkTrialStatus(params.trialStartedAt, totalUnits, params.nowServerTime);
|
|
9748
|
+
if (trialStatus.status === "expired" || trialStatus.status === "grace_period") {
|
|
9749
|
+
return {
|
|
9750
|
+
allowed: false,
|
|
9751
|
+
reason: trialStatus.reason || "Trial limit reached. Upgrade your plan.",
|
|
9752
|
+
costUnits
|
|
9753
|
+
};
|
|
9754
|
+
}
|
|
9755
|
+
if (totalUnits + costUnits > FREE_TIER_QUOTAS.MAX_TRIAL_REQUEST_UNITS) {
|
|
9756
|
+
return {
|
|
9757
|
+
allowed: false,
|
|
9758
|
+
reason: `Total trial request unit limit reached (${totalUnits}/${FREE_TIER_QUOTAS.MAX_TRIAL_REQUEST_UNITS}). Upgrade your plan.`,
|
|
9759
|
+
costUnits
|
|
9760
|
+
};
|
|
9761
|
+
}
|
|
9762
|
+
const dailyUnits = (_c = params.dailyUnitsUsed) != null ? _c : 0;
|
|
9763
|
+
if (dailyUnits + costUnits > FREE_TIER_QUOTAS.MAX_DAILY_REQUEST_UNITS) {
|
|
9764
|
+
return {
|
|
9765
|
+
allowed: false,
|
|
9766
|
+
reason: `Daily request quota limit reached (${dailyUnits}/${FREE_TIER_QUOTAS.MAX_DAILY_REQUEST_UNITS} units/day). Quota resets tomorrow.`,
|
|
9767
|
+
costUnits
|
|
9768
|
+
};
|
|
9769
|
+
}
|
|
9770
|
+
return { allowed: true, costUnits };
|
|
9771
|
+
}
|
|
9772
|
+
static checkQueryAllowed(dailyQueriesCount = 0) {
|
|
9773
|
+
if (dailyQueriesCount >= FREE_TIER_QUOTAS.MAX_DAILY_REQUEST_UNITS) {
|
|
9774
|
+
return {
|
|
9775
|
+
allowed: false,
|
|
9776
|
+
reason: `Daily query limit reached (${FREE_TIER_QUOTAS.MAX_DAILY_REQUEST_UNITS}/day). Quota resets tomorrow.`
|
|
9777
|
+
};
|
|
9778
|
+
}
|
|
9779
|
+
return { allowed: true };
|
|
9780
|
+
}
|
|
9781
|
+
};
|
|
9782
|
+
|
|
9477
9783
|
// src/core/VectorPlugin.ts
|
|
9478
9784
|
var VectorPlugin = class {
|
|
9479
9785
|
constructor(hostConfig) {
|
|
9480
9786
|
const resolvedConfig = ConfigResolver.resolve(hostConfig);
|
|
9481
9787
|
this.config = resolvedConfig;
|
|
9482
9788
|
this.validationPromise = (async () => {
|
|
9483
|
-
var
|
|
9789
|
+
var _a3;
|
|
9484
9790
|
await ConfigValidator.validateAndThrow(resolvedConfig);
|
|
9485
9791
|
LicenseVerifier.verify(
|
|
9486
9792
|
resolvedConfig.licenseKey,
|
|
9487
9793
|
resolvedConfig.projectId,
|
|
9488
|
-
(
|
|
9794
|
+
(_a3 = resolvedConfig.vectorDb) == null ? void 0 : _a3.provider
|
|
9489
9795
|
);
|
|
9490
9796
|
})();
|
|
9491
9797
|
this.validationPromise.catch(() => {
|
|
@@ -9519,16 +9825,32 @@ var VectorPlugin = class {
|
|
|
9519
9825
|
this.config.embedding
|
|
9520
9826
|
);
|
|
9521
9827
|
}
|
|
9828
|
+
estimateInputTokens(message, history = []) {
|
|
9829
|
+
const allContent = message.length + history.reduce((acc, m) => {
|
|
9830
|
+
var _a3, _b;
|
|
9831
|
+
return acc + (((_a3 = m == null ? void 0 : m.content) == null ? void 0 : _a3.length) || ((_b = m == null ? void 0 : m.text) == null ? void 0 : _b.length) || 0);
|
|
9832
|
+
}, 0);
|
|
9833
|
+
return Math.ceil(allContent / 4);
|
|
9834
|
+
}
|
|
9522
9835
|
/**
|
|
9523
9836
|
* Run a chat query.
|
|
9524
9837
|
*/
|
|
9525
9838
|
async chat(message, history = [], namespace) {
|
|
9839
|
+
var _a3;
|
|
9526
9840
|
try {
|
|
9527
9841
|
await this.validationPromise;
|
|
9528
9842
|
} catch (err) {
|
|
9529
9843
|
throw wrapError(err, "CONFIGURATION_ERROR");
|
|
9530
9844
|
}
|
|
9531
9845
|
try {
|
|
9846
|
+
const inputTokens = this.estimateInputTokens(message, history);
|
|
9847
|
+
const tokenCheck = FreeTierLimitsGuard.checkRequestAllowed({
|
|
9848
|
+
operationType: "chat",
|
|
9849
|
+
inputTokens
|
|
9850
|
+
});
|
|
9851
|
+
if (!tokenCheck.allowed && ((_a3 = tokenCheck.reason) == null ? void 0 : _a3.toLowerCase().includes("token"))) {
|
|
9852
|
+
throw wrapError(new Error(tokenCheck.reason), "RATE_LIMITED");
|
|
9853
|
+
}
|
|
9532
9854
|
return await this.pipeline.ask(message, history, namespace);
|
|
9533
9855
|
} catch (err) {
|
|
9534
9856
|
const msg = String(err);
|
|
@@ -9536,6 +9858,9 @@ var VectorPlugin = class {
|
|
|
9536
9858
|
if (msg.includes("Embed") || msg.includes("embed")) {
|
|
9537
9859
|
defaultCode = "EMBEDDING_FAILED";
|
|
9538
9860
|
}
|
|
9861
|
+
if (msg.toLowerCase().includes("token limit")) {
|
|
9862
|
+
defaultCode = "RATE_LIMITED";
|
|
9863
|
+
}
|
|
9539
9864
|
throw wrapError(err, defaultCode);
|
|
9540
9865
|
}
|
|
9541
9866
|
}
|
|
@@ -9544,12 +9869,21 @@ var VectorPlugin = class {
|
|
|
9544
9869
|
*/
|
|
9545
9870
|
chatStream(_0) {
|
|
9546
9871
|
return __asyncGenerator(this, arguments, function* (message, history = [], namespace) {
|
|
9872
|
+
var _a3;
|
|
9547
9873
|
try {
|
|
9548
9874
|
yield new __await(this.validationPromise);
|
|
9549
9875
|
} catch (err) {
|
|
9550
9876
|
throw wrapError(err, "CONFIGURATION_ERROR");
|
|
9551
9877
|
}
|
|
9552
9878
|
try {
|
|
9879
|
+
const inputTokens = this.estimateInputTokens(message, history);
|
|
9880
|
+
const tokenCheck = FreeTierLimitsGuard.checkRequestAllowed({
|
|
9881
|
+
operationType: "chat_stream",
|
|
9882
|
+
inputTokens
|
|
9883
|
+
});
|
|
9884
|
+
if (!tokenCheck.allowed && ((_a3 = tokenCheck.reason) == null ? void 0 : _a3.toLowerCase().includes("token"))) {
|
|
9885
|
+
throw wrapError(new Error(tokenCheck.reason), "RATE_LIMITED");
|
|
9886
|
+
}
|
|
9553
9887
|
const stream = this.pipeline.askStream(message, history, namespace);
|
|
9554
9888
|
try {
|
|
9555
9889
|
for (var iter = __forAwait(stream), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
|
|
@@ -9572,6 +9906,9 @@ var VectorPlugin = class {
|
|
|
9572
9906
|
if (msg.includes("Embed") || msg.includes("embed")) {
|
|
9573
9907
|
defaultCode = "EMBEDDING_FAILED";
|
|
9574
9908
|
}
|
|
9909
|
+
if (msg.toLowerCase().includes("token limit")) {
|
|
9910
|
+
defaultCode = "RATE_LIMITED";
|
|
9911
|
+
}
|
|
9575
9912
|
throw wrapError(err, defaultCode);
|
|
9576
9913
|
}
|
|
9577
9914
|
});
|
|
@@ -9619,8 +9956,8 @@ var DocumentParser = class {
|
|
|
9619
9956
|
* Extract text from a File or Buffer based on its type.
|
|
9620
9957
|
*/
|
|
9621
9958
|
static async parse(file, fileName, mimeType) {
|
|
9622
|
-
var
|
|
9623
|
-
const extension = ((
|
|
9959
|
+
var _a3;
|
|
9960
|
+
const extension = ((_a3 = fileName.split(".").pop()) == null ? void 0 : _a3.toLowerCase()) || "";
|
|
9624
9961
|
if (extension === "txt" || extension === "md" || mimeType === "text/plain" || mimeType === "text/markdown") {
|
|
9625
9962
|
return this.readAsText(file);
|
|
9626
9963
|
}
|
|
@@ -9711,9 +10048,9 @@ var DatabaseStorage = class {
|
|
|
9711
10048
|
this.fallbackDir = this.isServerless ? path.join(os.tmpdir(), ".retrivora") : path.join(process.cwd(), ".retrivora");
|
|
9712
10049
|
this.historyFile = path.join(this.fallbackDir, "history.json");
|
|
9713
10050
|
this.feedbackFile = path.join(this.fallbackDir, "feedback.json");
|
|
9714
|
-
var
|
|
10051
|
+
var _a3, _b, _c, _d, _e;
|
|
9715
10052
|
this.config = config;
|
|
9716
|
-
this.provider = ((
|
|
10053
|
+
this.provider = ((_a3 = config.vectorDb) == null ? void 0 : _a3.provider) || "fs";
|
|
9717
10054
|
const rawHistoryTable = ((_c = (_b = config.rag) == null ? void 0 : _b.history) == null ? void 0 : _c.tableName) || "retrivora_history";
|
|
9718
10055
|
const rawFeedbackTable = ((_e = (_d = config.rag) == null ? void 0 : _d.feedback) == null ? void 0 : _e.tableName) || "retrivora_feedback";
|
|
9719
10056
|
this.historyTableName = rawHistoryTable.replace(/[^a-zA-Z0-9_]/g, "_");
|
|
@@ -9748,16 +10085,16 @@ var DatabaseStorage = class {
|
|
|
9748
10085
|
}
|
|
9749
10086
|
}
|
|
9750
10087
|
checkHistoryEnabled() {
|
|
9751
|
-
var
|
|
10088
|
+
var _a3, _b;
|
|
9752
10089
|
this.checkPremiumSubscription();
|
|
9753
|
-
if (((_b = (
|
|
10090
|
+
if (((_b = (_a3 = this.config.rag) == null ? void 0 : _a3.history) == null ? void 0 : _b.enabled) === false) {
|
|
9754
10091
|
throw new Error("[Retrivora SDK] Chat History is disabled in RagConfig.");
|
|
9755
10092
|
}
|
|
9756
10093
|
}
|
|
9757
10094
|
checkFeedbackEnabled() {
|
|
9758
|
-
var
|
|
10095
|
+
var _a3, _b;
|
|
9759
10096
|
this.checkPremiumSubscription();
|
|
9760
|
-
if (((_b = (
|
|
10097
|
+
if (((_b = (_a3 = this.config.rag) == null ? void 0 : _a3.feedback) == null ? void 0 : _b.enabled) === false) {
|
|
9761
10098
|
throw new Error("[Retrivora SDK] User Feedback is disabled in RagConfig.");
|
|
9762
10099
|
}
|
|
9763
10100
|
}
|
|
@@ -9924,8 +10261,11 @@ var DatabaseStorage = class {
|
|
|
9924
10261
|
this.writeLocalFile(this.historyFile, history);
|
|
9925
10262
|
}
|
|
9926
10263
|
}
|
|
9927
|
-
async getHistory(sessionId) {
|
|
10264
|
+
async getHistory(sessionId, projectIds) {
|
|
9928
10265
|
this.checkHistoryEnabled();
|
|
10266
|
+
if (projectIds && projectIds.length > 0 && !this.sessionInScope(sessionId, projectIds)) {
|
|
10267
|
+
return [];
|
|
10268
|
+
}
|
|
9929
10269
|
if (this.provider === "postgresql" || this.provider === "pgvector") {
|
|
9930
10270
|
const pool = await this.getPGPool();
|
|
9931
10271
|
const res = await pool.query(
|
|
@@ -9955,8 +10295,11 @@ var DatabaseStorage = class {
|
|
|
9955
10295
|
return history.filter((h) => h.session_id === sessionId).sort((a, b) => new Date(a.created_at).getTime() - new Date(b.created_at).getTime());
|
|
9956
10296
|
}
|
|
9957
10297
|
}
|
|
9958
|
-
async clearHistory(sessionId) {
|
|
10298
|
+
async clearHistory(sessionId, projectIds) {
|
|
9959
10299
|
this.checkHistoryEnabled();
|
|
10300
|
+
if (projectIds && projectIds.length > 0 && !this.sessionInScope(sessionId, projectIds)) {
|
|
10301
|
+
return;
|
|
10302
|
+
}
|
|
9960
10303
|
if (this.provider === "postgresql" || this.provider === "pgvector") {
|
|
9961
10304
|
const pool = await this.getPGPool();
|
|
9962
10305
|
await pool.query(`DELETE FROM ${this.historyTableName} WHERE session_id = $1`, [sessionId]);
|
|
@@ -9975,21 +10318,36 @@ var DatabaseStorage = class {
|
|
|
9975
10318
|
this.writeLocalFile(this.feedbackFile, filteredFeedback);
|
|
9976
10319
|
}
|
|
9977
10320
|
}
|
|
9978
|
-
|
|
10321
|
+
/**
|
|
10322
|
+
* Return true when `sessionId` falls into the allowed `projectIds` scope.
|
|
10323
|
+
* When `projectIds` is empty / undefined we allow the call (legacy behavior
|
|
10324
|
+
* for non-multi-tenant consumers). Session ids typically begin with the
|
|
10325
|
+
* project id they were created for, so we prefix-match as well as contains-match
|
|
10326
|
+
* to catch sharded / suffix-style ids used by some integrations.
|
|
10327
|
+
*/
|
|
10328
|
+
sessionInScope(sessionId, projectIds) {
|
|
10329
|
+
if (!projectIds || projectIds.length === 0) return true;
|
|
10330
|
+
return projectIds.some(
|
|
10331
|
+
(pid) => pid && (sessionId === pid || sessionId.startsWith(pid) || sessionId.includes(pid))
|
|
10332
|
+
);
|
|
10333
|
+
}
|
|
10334
|
+
async listSessions(projectIds) {
|
|
9979
10335
|
this.checkHistoryEnabled();
|
|
10336
|
+
let raw = [];
|
|
9980
10337
|
if (this.provider === "postgresql" || this.provider === "pgvector") {
|
|
9981
10338
|
const pool = await this.getPGPool();
|
|
9982
10339
|
const res = await pool.query(`SELECT DISTINCT session_id FROM ${this.historyTableName}`);
|
|
9983
|
-
|
|
10340
|
+
raw = res.rows.map((r) => r.session_id);
|
|
9984
10341
|
} else if (this.provider === "mongodb") {
|
|
9985
10342
|
await this.getMongoClient();
|
|
9986
10343
|
const db = this.getMongoDb();
|
|
9987
|
-
|
|
10344
|
+
raw = await db.collection(this.historyTableName).distinct("session_id");
|
|
9988
10345
|
} else {
|
|
9989
10346
|
const history = this.readLocalFile(this.historyFile);
|
|
9990
|
-
|
|
9991
|
-
return Array.from(sessions);
|
|
10347
|
+
raw = Array.from(new Set(history.map((h) => h.session_id)));
|
|
9992
10348
|
}
|
|
10349
|
+
if (!projectIds || projectIds.length === 0) return raw;
|
|
10350
|
+
return raw.filter((sid) => this.sessionInScope(sid, projectIds));
|
|
9993
10351
|
}
|
|
9994
10352
|
// ─── Feedback Operations ──────────────────────────────────────────────────
|
|
9995
10353
|
async saveFeedback(feedback) {
|
|
@@ -10042,8 +10400,9 @@ var DatabaseStorage = class {
|
|
|
10042
10400
|
this.writeLocalFile(this.feedbackFile, list);
|
|
10043
10401
|
}
|
|
10044
10402
|
}
|
|
10045
|
-
async getFeedback(messageId) {
|
|
10403
|
+
async getFeedback(messageId, projectIds) {
|
|
10046
10404
|
this.checkFeedbackEnabled();
|
|
10405
|
+
let item = null;
|
|
10047
10406
|
if (this.provider === "postgresql" || this.provider === "pgvector") {
|
|
10048
10407
|
const pool = await this.getPGPool();
|
|
10049
10408
|
const res = await pool.query(
|
|
@@ -10052,28 +10411,34 @@ var DatabaseStorage = class {
|
|
|
10052
10411
|
WHERE message_id = $1`,
|
|
10053
10412
|
[messageId]
|
|
10054
10413
|
);
|
|
10055
|
-
|
|
10414
|
+
item = res.rows[0] || null;
|
|
10056
10415
|
} else if (this.provider === "mongodb") {
|
|
10057
10416
|
await this.getMongoClient();
|
|
10058
10417
|
const db = this.getMongoDb();
|
|
10059
10418
|
const col = db.collection(this.feedbackTableName);
|
|
10060
|
-
const
|
|
10061
|
-
if (
|
|
10062
|
-
|
|
10063
|
-
|
|
10064
|
-
|
|
10065
|
-
|
|
10066
|
-
|
|
10067
|
-
|
|
10068
|
-
|
|
10069
|
-
|
|
10419
|
+
const raw = await col.findOne({ message_id: messageId });
|
|
10420
|
+
if (raw) {
|
|
10421
|
+
item = {
|
|
10422
|
+
id: raw.id,
|
|
10423
|
+
messageId: raw.message_id,
|
|
10424
|
+
sessionId: raw.session_id,
|
|
10425
|
+
rating: raw.rating,
|
|
10426
|
+
comment: raw.comment,
|
|
10427
|
+
createdAt: raw.created_at
|
|
10428
|
+
};
|
|
10429
|
+
}
|
|
10070
10430
|
} else {
|
|
10071
10431
|
const list = this.readLocalFile(this.feedbackFile);
|
|
10072
|
-
|
|
10432
|
+
item = list.find((f) => f.message_id === messageId) || null;
|
|
10433
|
+
}
|
|
10434
|
+
if (item && projectIds && projectIds.length > 0) {
|
|
10435
|
+
return this.sessionInScope(item.sessionId, projectIds) ? item : null;
|
|
10073
10436
|
}
|
|
10437
|
+
return item;
|
|
10074
10438
|
}
|
|
10075
|
-
async listFeedback() {
|
|
10439
|
+
async listFeedback(projectIds) {
|
|
10076
10440
|
this.checkFeedbackEnabled();
|
|
10441
|
+
let raw = [];
|
|
10077
10442
|
if (this.provider === "postgresql" || this.provider === "pgvector") {
|
|
10078
10443
|
const pool = await this.getPGPool();
|
|
10079
10444
|
const res = await pool.query(
|
|
@@ -10081,13 +10446,13 @@ var DatabaseStorage = class {
|
|
|
10081
10446
|
FROM ${this.feedbackTableName}
|
|
10082
10447
|
ORDER BY created_at DESC`
|
|
10083
10448
|
);
|
|
10084
|
-
|
|
10449
|
+
raw = res.rows;
|
|
10085
10450
|
} else if (this.provider === "mongodb") {
|
|
10086
10451
|
await this.getMongoClient();
|
|
10087
10452
|
const db = this.getMongoDb();
|
|
10088
10453
|
const col = db.collection(this.feedbackTableName);
|
|
10089
10454
|
const items = await col.find({}).sort({ created_at: -1 }).toArray();
|
|
10090
|
-
|
|
10455
|
+
raw = items.map((item) => ({
|
|
10091
10456
|
id: item.id,
|
|
10092
10457
|
messageId: item.message_id,
|
|
10093
10458
|
sessionId: item.session_id,
|
|
@@ -10097,8 +10462,10 @@ var DatabaseStorage = class {
|
|
|
10097
10462
|
}));
|
|
10098
10463
|
} else {
|
|
10099
10464
|
const list = this.readLocalFile(this.feedbackFile);
|
|
10100
|
-
|
|
10465
|
+
raw = [...list].sort((a, b) => new Date(b.created_at).getTime() - new Date(a.created_at).getTime());
|
|
10101
10466
|
}
|
|
10467
|
+
if (!projectIds || projectIds.length === 0) return raw;
|
|
10468
|
+
return raw.filter((f) => this.sessionInScope(f.sessionId, projectIds));
|
|
10102
10469
|
}
|
|
10103
10470
|
async disconnect() {
|
|
10104
10471
|
if (this.pgPool) {
|
|
@@ -10161,8 +10528,8 @@ var _g = global;
|
|
|
10161
10528
|
var _a;
|
|
10162
10529
|
var rateLimiter = (_a = _g.__retrivoraRateLimiter) != null ? _a : _g.__retrivoraRateLimiter = new RateLimiter(6e4, 30);
|
|
10163
10530
|
function getRateLimitKey(req) {
|
|
10164
|
-
var
|
|
10165
|
-
const ip = ((_b = (
|
|
10531
|
+
var _a3, _b;
|
|
10532
|
+
const ip = ((_b = (_a3 = req.headers.get("x-forwarded-for")) == null ? void 0 : _a3.split(",")[0]) == null ? void 0 : _b.trim()) || req.headers.get("x-real-ip") || "127.0.0.1";
|
|
10166
10533
|
return `ip:${ip}`;
|
|
10167
10534
|
}
|
|
10168
10535
|
function checkRateLimit(req) {
|
|
@@ -10184,6 +10551,66 @@ function checkRateLimit(req) {
|
|
|
10184
10551
|
}
|
|
10185
10552
|
return null;
|
|
10186
10553
|
}
|
|
10554
|
+
var FREE_TIER_NAMES = /* @__PURE__ */ new Set(["hobby", "free", "free_trial", "trial"]);
|
|
10555
|
+
function isFreeTier(tier) {
|
|
10556
|
+
if (!tier) return true;
|
|
10557
|
+
return FREE_TIER_NAMES.has(tier.toLowerCase().trim());
|
|
10558
|
+
}
|
|
10559
|
+
var _a2;
|
|
10560
|
+
var _freeTierGuardInstance = (_a2 = _g.__retrivoraFreeTierGuard) != null ? _a2 : _g.__retrivoraFreeTierGuard = new FreeTierLimitsGuard();
|
|
10561
|
+
function createPaymentRequiredResponse(reason, details) {
|
|
10562
|
+
const body = __spreadProps(__spreadValues({
|
|
10563
|
+
type: "https://retrivora.com/docs/errors/payment-required",
|
|
10564
|
+
title: "Payment Required",
|
|
10565
|
+
status: 402,
|
|
10566
|
+
detail: reason,
|
|
10567
|
+
code: "FREE_TIER_LIMIT_EXCEEDED",
|
|
10568
|
+
instance: void 0
|
|
10569
|
+
}, details != null ? details : {}), {
|
|
10570
|
+
quota: {
|
|
10571
|
+
maxDailyRequestUnits: FREE_TIER_QUOTAS.MAX_DAILY_REQUEST_UNITS,
|
|
10572
|
+
maxTrialRequestUnits: FREE_TIER_QUOTAS.MAX_TRIAL_REQUEST_UNITS,
|
|
10573
|
+
maxDocuments: FREE_TIER_QUOTAS.MAX_DOCUMENTS,
|
|
10574
|
+
maxStorageBytes: FREE_TIER_QUOTAS.MAX_STORAGE_BYTES,
|
|
10575
|
+
upgradeUrl: "https://retrivora.com/pricing"
|
|
10576
|
+
}
|
|
10577
|
+
});
|
|
10578
|
+
return new Response(JSON.stringify(body), {
|
|
10579
|
+
status: 402,
|
|
10580
|
+
headers: {
|
|
10581
|
+
"Content-Type": "application/problem+json"
|
|
10582
|
+
}
|
|
10583
|
+
});
|
|
10584
|
+
}
|
|
10585
|
+
function resolveLicenseKey(req, config, bodyLicenseKey) {
|
|
10586
|
+
var _a3;
|
|
10587
|
+
return req.headers.get("x-license-key") || bodyLicenseKey || ((_a3 = req.headers.get("authorization")) == null ? void 0 : _a3.replace(/^Bearer\s+/i, "")) || config.licenseKey || process.env.NEXT_PUBLIC_RETRIVORA_LICENSE_KEY || process.env.RETRIVORA_LICENSE_KEY || process.env.RAG_LICENSE_KEY || "";
|
|
10588
|
+
}
|
|
10589
|
+
function enforceLicense(req, config, bodyLicenseKey) {
|
|
10590
|
+
const rawKey = resolveLicenseKey(req, config, bodyLicenseKey);
|
|
10591
|
+
const licenseKey = (rawKey || "").trim().replace(/^["']|["']$/g, "").trim();
|
|
10592
|
+
const projectId = config.projectId || "my-rag-app";
|
|
10593
|
+
try {
|
|
10594
|
+
const payload = LicenseVerifier.verify(licenseKey, projectId);
|
|
10595
|
+
return { ok: true, payload };
|
|
10596
|
+
} catch (err) {
|
|
10597
|
+
const message = err instanceof Error ? err.message : "Missing or invalid RETRIVORA_LICENSE_KEY. Set NEXT_PUBLIC_RETRIVORA_LICENSE_KEY in your environment or contact your administrator.";
|
|
10598
|
+
const body = {
|
|
10599
|
+
error: {
|
|
10600
|
+
code: "LICENSE_REVOKED",
|
|
10601
|
+
message: "Your Retrivora license key has been terminated, suspended, or revoked. Access denied."
|
|
10602
|
+
},
|
|
10603
|
+
details: message
|
|
10604
|
+
};
|
|
10605
|
+
return {
|
|
10606
|
+
ok: false,
|
|
10607
|
+
response: new Response(JSON.stringify(body), {
|
|
10608
|
+
status: 403,
|
|
10609
|
+
headers: { "Content-Type": "application/json" }
|
|
10610
|
+
})
|
|
10611
|
+
};
|
|
10612
|
+
}
|
|
10613
|
+
}
|
|
10187
10614
|
var MAX_MESSAGE_LENGTH = 8e3;
|
|
10188
10615
|
function sanitizeInput(raw) {
|
|
10189
10616
|
const stripped = raw.replace(/[\u0000-\u0008\u000B\u000C\u000E-\u001F\u007F-\u009F]/g, "");
|
|
@@ -10205,7 +10632,7 @@ function getOrCreatePlugin(configOrPlugin) {
|
|
|
10205
10632
|
return _g[cacheKey];
|
|
10206
10633
|
}
|
|
10207
10634
|
function reportTelemetry(req, plugin, action, status, details, trace) {
|
|
10208
|
-
var
|
|
10635
|
+
var _a3, _b, _c, _d, _e, _f, _g2;
|
|
10209
10636
|
try {
|
|
10210
10637
|
const config = plugin.getConfig();
|
|
10211
10638
|
const licenseKey = config.licenseKey || process.env.RAG_LICENSE_KEY || process.env.RETRIVORA_LICENSE_KEY || process.env.NEXT_PUBLIC_RETRIVORA_LICENSE_KEY;
|
|
@@ -10220,7 +10647,7 @@ function reportTelemetry(req, plugin, action, status, details, trace) {
|
|
|
10220
10647
|
absoluteUrl = `${proto}://${host}${telemetryUrl}`;
|
|
10221
10648
|
}
|
|
10222
10649
|
const projectId = config.projectId || "default";
|
|
10223
|
-
const model = (trace == null ? void 0 : trace.model) || ((
|
|
10650
|
+
const model = (trace == null ? void 0 : trace.model) || ((_a3 = config.llm) == null ? void 0 : _a3.model) || ((_b = config.embedding) == null ? void 0 : _b.model) || "llama-3.1-8b-instant";
|
|
10224
10651
|
const provider = (trace == null ? void 0 : trace.provider) || ((_c = config.llm) == null ? void 0 : _c.provider) || ((_d = config.embedding) == null ? void 0 : _d.provider) || "groq";
|
|
10225
10652
|
const tokens = Number(((_e = trace == null ? void 0 : trace.tokens) == null ? void 0 : _e.totalTokens) || (trace == null ? void 0 : trace.totalTokens) || 0);
|
|
10226
10653
|
const costUsd = Number(((_f = trace == null ? void 0 : trace.tokens) == null ? void 0 : _f.estimatedCostUsd) || (trace == null ? void 0 : trace.costUsd) || 0);
|
|
@@ -10244,7 +10671,6 @@ function reportTelemetry(req, plugin, action, status, details, trace) {
|
|
|
10244
10671
|
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
10245
10672
|
details
|
|
10246
10673
|
};
|
|
10247
|
-
console.log(`[Retrivora SDK Telemetry] Dispatching telemetry -> Feature: "${action}", Status: "${status}", Project: "${projectId}", Latency: ${latencyMs}ms`);
|
|
10248
10674
|
fetch(absoluteUrl, {
|
|
10249
10675
|
method: "POST",
|
|
10250
10676
|
headers: {
|
|
@@ -10302,14 +10728,39 @@ function createChatHandler(configOrPlugin, options) {
|
|
|
10302
10728
|
const storage = new DatabaseStorage(plugin.getConfig());
|
|
10303
10729
|
const onAuthorize = options == null ? void 0 : options.onAuthorize;
|
|
10304
10730
|
return async function POST(req, context) {
|
|
10305
|
-
var
|
|
10731
|
+
var _a3, _b, _c, _d, _e, _f;
|
|
10306
10732
|
const authResult = await checkAuth(req, onAuthorize);
|
|
10307
10733
|
if (authResult) return authResult;
|
|
10308
10734
|
const rateLimited = checkRateLimit(req);
|
|
10309
10735
|
if (rateLimited) return rateLimited;
|
|
10736
|
+
const config = plugin.getConfig();
|
|
10310
10737
|
try {
|
|
10311
10738
|
const body = await req.json();
|
|
10312
10739
|
const bodyObj = body != null ? body : {};
|
|
10740
|
+
const licenseCheck = enforceLicense(req, config, bodyObj == null ? void 0 : bodyObj.licenseKey);
|
|
10741
|
+
if (!licenseCheck.ok) return licenseCheck.response;
|
|
10742
|
+
if (isFreeTier(licenseCheck.payload.tier)) {
|
|
10743
|
+
const estimatedInputTokens = Math.ceil(
|
|
10744
|
+
((((_a3 = bodyObj.message) == null ? void 0 : _a3.length) || 0) + (Array.isArray(bodyObj.messages) ? bodyObj.messages.reduce((a, m) => {
|
|
10745
|
+
var _a4, _b2;
|
|
10746
|
+
return a + (((_a4 = m == null ? void 0 : m.content) == null ? void 0 : _a4.length) || ((_b2 = m == null ? void 0 : m.text) == null ? void 0 : _b2.length) || 0);
|
|
10747
|
+
}, 0) : 0) + (((_b = bodyObj.history) == null ? void 0 : _b.reduce((a, m) => {
|
|
10748
|
+
var _a4, _b2;
|
|
10749
|
+
return a + (((_a4 = m == null ? void 0 : m.content) == null ? void 0 : _a4.length) || ((_b2 = m == null ? void 0 : m.text) == null ? void 0 : _b2.length) || 0);
|
|
10750
|
+
}, 0)) || 0)) / 4
|
|
10751
|
+
);
|
|
10752
|
+
const chatCheck = FreeTierLimitsGuard.checkRequestAllowed({
|
|
10753
|
+
operationType: "chat",
|
|
10754
|
+
inputTokens: estimatedInputTokens,
|
|
10755
|
+
projectId: licenseCheck.payload.projectId
|
|
10756
|
+
});
|
|
10757
|
+
if (!chatCheck.allowed) {
|
|
10758
|
+
return createPaymentRequiredResponse(
|
|
10759
|
+
chatCheck.reason || "Free tier quota exceeded.",
|
|
10760
|
+
{ tier: licenseCheck.payload.tier, projectId: licenseCheck.payload.projectId }
|
|
10761
|
+
);
|
|
10762
|
+
}
|
|
10763
|
+
}
|
|
10313
10764
|
let rawMessage = bodyObj.message;
|
|
10314
10765
|
if (!rawMessage && Array.isArray(bodyObj.messages) && bodyObj.messages.length > 0) {
|
|
10315
10766
|
const lastMsg = bodyObj.messages[bodyObj.messages.length - 1];
|
|
@@ -10341,7 +10792,7 @@ function createChatHandler(configOrPlugin, options) {
|
|
|
10341
10792
|
uiTransformation: result.ui_transformation,
|
|
10342
10793
|
trace: result.trace
|
|
10343
10794
|
}).catch((err) => console.warn("[createChatHandler] Failed to save assistant message:", err));
|
|
10344
|
-
reportTelemetry(req, plugin, "QUERY_GENERATION", "success", `Tokens: ${((
|
|
10795
|
+
reportTelemetry(req, plugin, "QUERY_GENERATION", "success", `Tokens: ${((_d = (_c = result.trace) == null ? void 0 : _c.tokens) == null ? void 0 : _d.totalTokens) || 0}, Latency: ${((_f = (_e = result.trace) == null ? void 0 : _e.latency) == null ? void 0 : _f.totalMs) || 0}ms`, result.trace);
|
|
10345
10796
|
return NextResponse.json(__spreadProps(__spreadValues({}, result), {
|
|
10346
10797
|
messageId: assistantMsgId,
|
|
10347
10798
|
userMessageId: userMsgId
|
|
@@ -10358,14 +10809,15 @@ function createStreamHandler(configOrPlugin, options) {
|
|
|
10358
10809
|
const storage = new DatabaseStorage(plugin.getConfig());
|
|
10359
10810
|
const onAuthorize = options == null ? void 0 : options.onAuthorize;
|
|
10360
10811
|
return async function POST(req, context) {
|
|
10361
|
-
var
|
|
10812
|
+
var _a3, _b, _c;
|
|
10362
10813
|
const authResult = await checkAuth(req, onAuthorize);
|
|
10363
10814
|
if (authResult) return authResult;
|
|
10364
10815
|
const rateLimited = checkRateLimit(req);
|
|
10365
10816
|
if (rateLimited) return rateLimited;
|
|
10817
|
+
const config = plugin.getConfig();
|
|
10366
10818
|
let body;
|
|
10367
10819
|
try {
|
|
10368
|
-
if ((
|
|
10820
|
+
if ((_a3 = req.headers.get("content-type")) == null ? void 0 : _a3.includes("multipart/form-data")) {
|
|
10369
10821
|
const uploader = createUploadHandler(plugin, { onAuthorize });
|
|
10370
10822
|
return uploader(req);
|
|
10371
10823
|
}
|
|
@@ -10377,6 +10829,30 @@ function createStreamHandler(configOrPlugin, options) {
|
|
|
10377
10829
|
});
|
|
10378
10830
|
}
|
|
10379
10831
|
const bodyObj = body != null ? body : {};
|
|
10832
|
+
const licenseCheck = enforceLicense(req, config, bodyObj == null ? void 0 : bodyObj.licenseKey);
|
|
10833
|
+
if (!licenseCheck.ok) return licenseCheck.response;
|
|
10834
|
+
if (isFreeTier(licenseCheck.payload.tier)) {
|
|
10835
|
+
const estimatedInputTokens = Math.ceil(
|
|
10836
|
+
((((_b = bodyObj.message) == null ? void 0 : _b.length) || 0) + (Array.isArray(bodyObj.messages) ? bodyObj.messages.reduce((a, m) => {
|
|
10837
|
+
var _a4, _b2;
|
|
10838
|
+
return a + (((_a4 = m == null ? void 0 : m.content) == null ? void 0 : _a4.length) || ((_b2 = m == null ? void 0 : m.text) == null ? void 0 : _b2.length) || 0);
|
|
10839
|
+
}, 0) : 0) + (((_c = bodyObj.history) == null ? void 0 : _c.reduce((a, m) => {
|
|
10840
|
+
var _a4, _b2;
|
|
10841
|
+
return a + (((_a4 = m == null ? void 0 : m.content) == null ? void 0 : _a4.length) || ((_b2 = m == null ? void 0 : m.text) == null ? void 0 : _b2.length) || 0);
|
|
10842
|
+
}, 0)) || 0)) / 4
|
|
10843
|
+
);
|
|
10844
|
+
const chatCheck = FreeTierLimitsGuard.checkRequestAllowed({
|
|
10845
|
+
operationType: "chat_stream",
|
|
10846
|
+
inputTokens: estimatedInputTokens,
|
|
10847
|
+
projectId: licenseCheck.payload.projectId
|
|
10848
|
+
});
|
|
10849
|
+
if (!chatCheck.allowed) {
|
|
10850
|
+
return createPaymentRequiredResponse(
|
|
10851
|
+
chatCheck.reason || "Free tier quota exceeded.",
|
|
10852
|
+
{ tier: licenseCheck.payload.tier, projectId: licenseCheck.payload.projectId }
|
|
10853
|
+
);
|
|
10854
|
+
}
|
|
10855
|
+
}
|
|
10380
10856
|
let rawMessage = bodyObj.message;
|
|
10381
10857
|
if (!rawMessage && Array.isArray(bodyObj.messages) && bodyObj.messages.length > 0) {
|
|
10382
10858
|
const lastMsg = bodyObj.messages[bodyObj.messages.length - 1];
|
|
@@ -10406,7 +10882,7 @@ function createStreamHandler(configOrPlugin, options) {
|
|
|
10406
10882
|
let isActive = true;
|
|
10407
10883
|
const stream = new ReadableStream({
|
|
10408
10884
|
async start(controller) {
|
|
10409
|
-
var
|
|
10885
|
+
var _a4, _b2, _c2, _d, _e;
|
|
10410
10886
|
const enqueue = (text) => {
|
|
10411
10887
|
if (!isActive) return;
|
|
10412
10888
|
try {
|
|
@@ -10439,7 +10915,7 @@ function createStreamHandler(configOrPlugin, options) {
|
|
|
10439
10915
|
let uiTransformation = responseChunk == null ? void 0 : responseChunk.ui_transformation;
|
|
10440
10916
|
if (sources.length > 0) {
|
|
10441
10917
|
try {
|
|
10442
|
-
uiTransformation = (
|
|
10918
|
+
uiTransformation = (_a4 = responseChunk == null ? void 0 : responseChunk.ui_transformation) != null ? _a4 : UITransformer.transform(message, sources, plugin.getConfig());
|
|
10443
10919
|
if (uiTransformation) {
|
|
10444
10920
|
enqueue(sseUIFrame(uiTransformation));
|
|
10445
10921
|
}
|
|
@@ -10463,7 +10939,7 @@ function createStreamHandler(configOrPlugin, options) {
|
|
|
10463
10939
|
uiTransformation,
|
|
10464
10940
|
trace: responseChunk == null ? void 0 : responseChunk.trace
|
|
10465
10941
|
}).catch((err) => console.warn("[createStreamHandler] Failed to save assistant message:", err));
|
|
10466
|
-
reportTelemetry(req, plugin, "QUERY_GENERATION", "success", `Tokens: ${((
|
|
10942
|
+
reportTelemetry(req, plugin, "QUERY_GENERATION", "success", `Tokens: ${((_c2 = (_b2 = responseChunk == null ? void 0 : responseChunk.trace) == null ? void 0 : _b2.tokens) == null ? void 0 : _c2.totalTokens) || 0}, Latency: ${((_e = (_d = responseChunk == null ? void 0 : responseChunk.trace) == null ? void 0 : _d.latency) == null ? void 0 : _e.totalMs) || 0}ms`, responseChunk == null ? void 0 : responseChunk.trace);
|
|
10467
10943
|
}
|
|
10468
10944
|
}
|
|
10469
10945
|
} catch (temp) {
|
|
@@ -10513,12 +10989,36 @@ function createIngestHandler(configOrPlugin, options) {
|
|
|
10513
10989
|
return async function POST(req, context) {
|
|
10514
10990
|
const authResult = await checkAuth(req, onAuthorize);
|
|
10515
10991
|
if (authResult) return authResult;
|
|
10992
|
+
const config = plugin.getConfig();
|
|
10516
10993
|
try {
|
|
10517
10994
|
const body = await req.json();
|
|
10995
|
+
const licenseCheck = enforceLicense(req, config, body == null ? void 0 : body.licenseKey);
|
|
10996
|
+
if (!licenseCheck.ok) return licenseCheck.response;
|
|
10518
10997
|
const { documents, namespace } = body;
|
|
10519
10998
|
if (!Array.isArray(documents) || documents.length === 0) {
|
|
10520
10999
|
return NextResponse.json({ error: "documents array is required" }, { status: 400 });
|
|
10521
11000
|
}
|
|
11001
|
+
if (isFreeTier(licenseCheck.payload.tier)) {
|
|
11002
|
+
const totalIncomingBytes = documents.reduce(
|
|
11003
|
+
(sum, d) => sum + Buffer.byteLength(d.content || "", "utf8"),
|
|
11004
|
+
0
|
|
11005
|
+
);
|
|
11006
|
+
const ingestCheck = FreeTierLimitsGuard.checkIngestionAllowed(
|
|
11007
|
+
{ documentCount: documents.length },
|
|
11008
|
+
totalIncomingBytes
|
|
11009
|
+
);
|
|
11010
|
+
if (!ingestCheck.allowed) {
|
|
11011
|
+
return createPaymentRequiredResponse(
|
|
11012
|
+
ingestCheck.reason || "Free tier ingestion limit reached.",
|
|
11013
|
+
{
|
|
11014
|
+
tier: licenseCheck.payload.tier,
|
|
11015
|
+
projectId: licenseCheck.payload.projectId,
|
|
11016
|
+
documents: documents.length,
|
|
11017
|
+
incomingBytes: totalIncomingBytes
|
|
11018
|
+
}
|
|
11019
|
+
);
|
|
11020
|
+
}
|
|
11021
|
+
}
|
|
10522
11022
|
const results = await plugin.ingest(documents, namespace);
|
|
10523
11023
|
reportTelemetry(req, plugin, "DOCUMENT_INGESTION", "success", `Ingested ${documents.length} document(s)`);
|
|
10524
11024
|
return NextResponse.json({ results });
|
|
@@ -10555,7 +11055,7 @@ function createLicenseHandler(configOrPlugin, options) {
|
|
|
10555
11055
|
const plugin = getOrCreatePlugin(configOrPlugin);
|
|
10556
11056
|
const onAuthorize = options == null ? void 0 : options.onAuthorize;
|
|
10557
11057
|
return async function POST(req) {
|
|
10558
|
-
var
|
|
11058
|
+
var _a3;
|
|
10559
11059
|
if (req) {
|
|
10560
11060
|
const authResult = await checkAuth(req, onAuthorize);
|
|
10561
11061
|
if (authResult) return authResult;
|
|
@@ -10563,7 +11063,7 @@ function createLicenseHandler(configOrPlugin, options) {
|
|
|
10563
11063
|
try {
|
|
10564
11064
|
const body = await req.json().catch(() => ({}));
|
|
10565
11065
|
const config = plugin.getConfig();
|
|
10566
|
-
const rawKey = req.headers.get("x-license-key") || (body == null ? void 0 : body.licenseKey) || ((
|
|
11066
|
+
const rawKey = req.headers.get("x-license-key") || (body == null ? void 0 : body.licenseKey) || ((_a3 = req.headers.get("authorization")) == null ? void 0 : _a3.replace(/^Bearer\s+/i, "")) || config.licenseKey || process.env.NEXT_PUBLIC_RETRIVORA_LICENSE_KEY || process.env.RETRIVORA_LICENSE_KEY || "";
|
|
10567
11067
|
const licenseKey = (rawKey || "").trim().replace(/^["']|["']$/g, "").trim();
|
|
10568
11068
|
const projectId = (body == null ? void 0 : body.projectId) || config.projectId || "my-rag-app";
|
|
10569
11069
|
const payload = LicenseVerifier.verify(licenseKey, projectId);
|
|
@@ -10598,6 +11098,9 @@ function createUploadHandler(configOrPlugin, options) {
|
|
|
10598
11098
|
return async function POST(req, context) {
|
|
10599
11099
|
const authResult = await checkAuth(req, onAuthorize);
|
|
10600
11100
|
if (authResult) return authResult;
|
|
11101
|
+
const config = plugin.getConfig();
|
|
11102
|
+
const licenseCheck = enforceLicense(req, config);
|
|
11103
|
+
if (!licenseCheck.ok) return licenseCheck.response;
|
|
10601
11104
|
try {
|
|
10602
11105
|
const formData = await req.formData();
|
|
10603
11106
|
const files = formData.getAll("files");
|
|
@@ -10607,6 +11110,26 @@ function createUploadHandler(configOrPlugin, options) {
|
|
|
10607
11110
|
if (!files || files.length === 0) {
|
|
10608
11111
|
return NextResponse.json({ error: "No files provided" }, { status: 400 });
|
|
10609
11112
|
}
|
|
11113
|
+
if (isFreeTier(licenseCheck.payload.tier)) {
|
|
11114
|
+
const totalUploadBytes = files.reduce((sum, f) => sum + (f.size || 0), 0);
|
|
11115
|
+
const uploadCheck = FreeTierLimitsGuard.checkIngestionAllowed(
|
|
11116
|
+
{ documentCount: files.length },
|
|
11117
|
+
totalUploadBytes
|
|
11118
|
+
);
|
|
11119
|
+
if (!uploadCheck.allowed) {
|
|
11120
|
+
return createPaymentRequiredResponse(
|
|
11121
|
+
uploadCheck.reason || "Free tier upload limit reached.",
|
|
11122
|
+
{
|
|
11123
|
+
tier: licenseCheck.payload.tier,
|
|
11124
|
+
projectId: licenseCheck.payload.projectId,
|
|
11125
|
+
fileCount: files.length,
|
|
11126
|
+
totalUploadBytes,
|
|
11127
|
+
maxFileSizeBytes: FREE_TIER_QUOTAS.MAX_FILE_SIZE_BYTES,
|
|
11128
|
+
maxStorageBytes: FREE_TIER_QUOTAS.MAX_STORAGE_BYTES
|
|
11129
|
+
}
|
|
11130
|
+
);
|
|
11131
|
+
}
|
|
11132
|
+
}
|
|
10610
11133
|
const documents = [];
|
|
10611
11134
|
for (const file of files) {
|
|
10612
11135
|
const isExcel = file.name.toLowerCase().endsWith(".xlsx") || file.name.toLowerCase().endsWith(".xls") || file.type.includes("spreadsheet") || file.type.includes("excel");
|
|
@@ -10716,13 +11239,22 @@ function createSuggestionsHandler(configOrPlugin, options) {
|
|
|
10716
11239
|
return async function handler(req) {
|
|
10717
11240
|
const authResult = await checkAuth(req, onAuthorize);
|
|
10718
11241
|
if (authResult) return authResult;
|
|
11242
|
+
const config = plugin.getConfig();
|
|
11243
|
+
const licenseCheck = enforceLicense(req, config);
|
|
11244
|
+
if (!licenseCheck.ok) return licenseCheck.response;
|
|
10719
11245
|
try {
|
|
10720
11246
|
let query = "";
|
|
10721
11247
|
let namespace = void 0;
|
|
11248
|
+
let bodyLicenseKey = void 0;
|
|
10722
11249
|
if (req.method === "POST") {
|
|
10723
11250
|
const body = await req.json().catch(() => ({}));
|
|
10724
11251
|
query = body.query || "";
|
|
10725
11252
|
namespace = body.namespace;
|
|
11253
|
+
bodyLicenseKey = body.licenseKey;
|
|
11254
|
+
if (bodyLicenseKey) {
|
|
11255
|
+
const recheck = enforceLicense(req, config, bodyLicenseKey);
|
|
11256
|
+
if (!recheck.ok) return recheck.response;
|
|
11257
|
+
}
|
|
10726
11258
|
} else {
|
|
10727
11259
|
const url = new URL(req.url);
|
|
10728
11260
|
query = url.searchParams.get("query") || "";
|
|
@@ -10745,14 +11277,16 @@ function createHistoryHandler(configOrPlugin, options) {
|
|
|
10745
11277
|
const plugin = getOrCreatePlugin(configOrPlugin);
|
|
10746
11278
|
const storage = new DatabaseStorage(plugin.getConfig());
|
|
10747
11279
|
const onAuthorize = options == null ? void 0 : options.onAuthorize;
|
|
11280
|
+
const onResolveScope = options == null ? void 0 : options.onResolveScope;
|
|
10748
11281
|
return async function handler(req) {
|
|
10749
11282
|
const authResult = await checkAuth(req, onAuthorize);
|
|
10750
11283
|
if (authResult) return authResult;
|
|
11284
|
+
const scope = onResolveScope ? await onResolveScope(req) : void 0;
|
|
10751
11285
|
const url = new URL(req.url);
|
|
10752
11286
|
const sessionId = url.searchParams.get("sessionId") || "default";
|
|
10753
11287
|
if (req.method === "GET") {
|
|
10754
11288
|
try {
|
|
10755
|
-
const history = await storage.getHistory(sessionId);
|
|
11289
|
+
const history = await storage.getHistory(sessionId, scope);
|
|
10756
11290
|
return NextResponse.json({ history });
|
|
10757
11291
|
} catch (err) {
|
|
10758
11292
|
const message = err instanceof Error ? err.message : "Failed to fetch history";
|
|
@@ -10762,7 +11296,7 @@ function createHistoryHandler(configOrPlugin, options) {
|
|
|
10762
11296
|
try {
|
|
10763
11297
|
const body = await req.json().catch(() => ({}));
|
|
10764
11298
|
const sid = body.sessionId || sessionId;
|
|
10765
|
-
await storage.clearHistory(sid);
|
|
11299
|
+
await storage.clearHistory(sid, scope);
|
|
10766
11300
|
return NextResponse.json({ success: true });
|
|
10767
11301
|
} catch (err) {
|
|
10768
11302
|
const message = err instanceof Error ? err.message : "Failed to clear history";
|
|
@@ -10776,18 +11310,20 @@ function createFeedbackHandler(configOrPlugin, options) {
|
|
|
10776
11310
|
const plugin = getOrCreatePlugin(configOrPlugin);
|
|
10777
11311
|
const storage = new DatabaseStorage(plugin.getConfig());
|
|
10778
11312
|
const onAuthorize = options == null ? void 0 : options.onAuthorize;
|
|
11313
|
+
const onResolveScope = options == null ? void 0 : options.onResolveScope;
|
|
10779
11314
|
return async function handler(req) {
|
|
10780
11315
|
const authResult = await checkAuth(req, onAuthorize);
|
|
10781
11316
|
if (authResult) return authResult;
|
|
11317
|
+
const scope = onResolveScope ? await onResolveScope(req) : void 0;
|
|
10782
11318
|
if (req.method === "GET") {
|
|
10783
11319
|
try {
|
|
10784
11320
|
const url = new URL(req.url);
|
|
10785
11321
|
const messageId = url.searchParams.get("messageId");
|
|
10786
11322
|
if (!messageId) {
|
|
10787
|
-
const feedbackList = await storage.listFeedback();
|
|
11323
|
+
const feedbackList = await storage.listFeedback(scope);
|
|
10788
11324
|
return NextResponse.json({ feedback: feedbackList });
|
|
10789
11325
|
}
|
|
10790
|
-
const feedback = await storage.getFeedback(messageId);
|
|
11326
|
+
const feedback = await storage.getFeedback(messageId, scope);
|
|
10791
11327
|
return NextResponse.json({ feedback });
|
|
10792
11328
|
} catch (err) {
|
|
10793
11329
|
const message = err instanceof Error ? err.message : "Failed to fetch feedback";
|
|
@@ -10803,6 +11339,17 @@ function createFeedbackHandler(configOrPlugin, options) {
|
|
|
10803
11339
|
if (!rating) {
|
|
10804
11340
|
return NextResponse.json({ error: "rating is required" }, { status: 400 });
|
|
10805
11341
|
}
|
|
11342
|
+
if (scope && scope.length > 0) {
|
|
11343
|
+
const matches = scope.some(
|
|
11344
|
+
(pid) => pid && (sessionId === pid || sessionId.startsWith(pid) || sessionId.includes(pid))
|
|
11345
|
+
);
|
|
11346
|
+
if (!matches) {
|
|
11347
|
+
return NextResponse.json(
|
|
11348
|
+
{ error: "sessionId is outside the authorized project scope" },
|
|
11349
|
+
{ status: 403 }
|
|
11350
|
+
);
|
|
11351
|
+
}
|
|
11352
|
+
}
|
|
10806
11353
|
await storage.saveFeedback({ messageId, sessionId, rating, comment });
|
|
10807
11354
|
return NextResponse.json({ success: true });
|
|
10808
11355
|
} catch (err) {
|
|
@@ -10817,14 +11364,15 @@ function createSessionsHandler(configOrPlugin, options) {
|
|
|
10817
11364
|
const plugin = getOrCreatePlugin(configOrPlugin);
|
|
10818
11365
|
const storage = new DatabaseStorage(plugin.getConfig());
|
|
10819
11366
|
const onAuthorize = options == null ? void 0 : options.onAuthorize;
|
|
11367
|
+
const onResolveScope = options == null ? void 0 : options.onResolveScope;
|
|
10820
11368
|
return async function GET(req) {
|
|
10821
11369
|
if (req) {
|
|
10822
11370
|
const authResult = await checkAuth(req, onAuthorize);
|
|
10823
11371
|
if (authResult) return authResult;
|
|
10824
11372
|
}
|
|
10825
|
-
void req;
|
|
10826
11373
|
try {
|
|
10827
|
-
const
|
|
11374
|
+
const scope = req && onResolveScope ? await onResolveScope(req) : void 0;
|
|
11375
|
+
const sessions = await storage.listSessions(scope);
|
|
10828
11376
|
return NextResponse.json({ sessions });
|
|
10829
11377
|
} catch (err) {
|
|
10830
11378
|
const message = err instanceof Error ? err.message : "Failed to list sessions";
|
|
@@ -10835,15 +11383,17 @@ function createSessionsHandler(configOrPlugin, options) {
|
|
|
10835
11383
|
function createRagHandler(configOrPlugin, options) {
|
|
10836
11384
|
const plugin = getOrCreatePlugin(configOrPlugin);
|
|
10837
11385
|
const onAuthorize = options == null ? void 0 : options.onAuthorize;
|
|
10838
|
-
const
|
|
10839
|
-
const
|
|
10840
|
-
const
|
|
10841
|
-
const
|
|
10842
|
-
const
|
|
10843
|
-
const
|
|
10844
|
-
const
|
|
10845
|
-
const
|
|
10846
|
-
const
|
|
11386
|
+
const onResolveScope = options == null ? void 0 : options.onResolveScope;
|
|
11387
|
+
const scopeableOptions = { onAuthorize, onResolveScope };
|
|
11388
|
+
const chatHandler = createChatHandler(plugin, scopeableOptions);
|
|
11389
|
+
const streamHandler = createStreamHandler(plugin, scopeableOptions);
|
|
11390
|
+
const uploadHandler = createUploadHandler(plugin, scopeableOptions);
|
|
11391
|
+
const healthHandler = createHealthHandler(plugin, scopeableOptions);
|
|
11392
|
+
const suggestionsHandler = createSuggestionsHandler(plugin, scopeableOptions);
|
|
11393
|
+
const historyHandler = createHistoryHandler(plugin, scopeableOptions);
|
|
11394
|
+
const feedbackHandler = createFeedbackHandler(plugin, scopeableOptions);
|
|
11395
|
+
const sessionsHandler = createSessionsHandler(plugin, scopeableOptions);
|
|
11396
|
+
const licenseHandler = createLicenseHandler(plugin, scopeableOptions);
|
|
10847
11397
|
async function routePostRequest(req, segment) {
|
|
10848
11398
|
switch (segment) {
|
|
10849
11399
|
case "chat":
|
|
@@ -10885,7 +11435,7 @@ function createRagHandler(configOrPlugin, options) {
|
|
|
10885
11435
|
}
|
|
10886
11436
|
}
|
|
10887
11437
|
async function getSegment(req, context) {
|
|
10888
|
-
var
|
|
11438
|
+
var _a3, _b;
|
|
10889
11439
|
const contentType = req.headers.get("content-type") || "";
|
|
10890
11440
|
if (contentType.includes("multipart/form-data")) {
|
|
10891
11441
|
return "upload";
|
|
@@ -10902,7 +11452,7 @@ function createRagHandler(configOrPlugin, options) {
|
|
|
10902
11452
|
if (pathname.endsWith("/history")) return "history";
|
|
10903
11453
|
} catch (e) {
|
|
10904
11454
|
}
|
|
10905
|
-
const resolvedParams = typeof ((
|
|
11455
|
+
const resolvedParams = typeof ((_a3 = context == null ? void 0 : context.params) == null ? void 0 : _a3.then) === "function" ? await context.params : context == null ? void 0 : context.params;
|
|
10906
11456
|
const segments = (resolvedParams == null ? void 0 : resolvedParams.retrivora) || ((_b = resolvedParams == null ? void 0 : resolvedParams.params) == null ? void 0 : _b.retrivora) || [];
|
|
10907
11457
|
const joined = segments.join("/");
|
|
10908
11458
|
return joined || "chat";
|