@retrivora-ai/rag-engine 2.5.8 → 2.6.0
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/{ILLMProvider-B2N3lMBc.d.mts → ILLMProvider-DUCL1r75.d.mts} +10 -0
- package/dist/{ILLMProvider-B2N3lMBc.d.ts → ILLMProvider-DUCL1r75.d.ts} +10 -0
- package/dist/handlers/index.d.mts +2 -2
- package/dist/handlers/index.d.ts +2 -2
- package/dist/handlers/index.js +969 -104
- package/dist/handlers/index.mjs +969 -104
- package/dist/{index-CAGGPyI7.d.mts → index-BVSHblli.d.mts} +1 -1
- package/dist/{index-BxzDv2jd.d.ts → index-CsBcqng1.d.ts} +1 -1
- package/dist/{index-37V810a-.d.mts → index-D6Oq2zyc.d.mts} +1 -1
- package/dist/{index-DdQxoIpu.d.ts → index-DGW0NNmR.d.ts} +1 -1
- package/dist/index.d.mts +4 -4
- package/dist/index.d.ts +4 -4
- package/dist/index.js +4 -4
- package/dist/index.mjs +4 -4
- package/dist/server.d.mts +271 -9
- package/dist/server.d.ts +271 -9
- package/dist/server.js +983 -104
- package/dist/server.mjs +976 -104
- package/package.json +1 -1
package/dist/handlers/index.mjs
CHANGED
|
@@ -312,7 +312,7 @@ var init_package = __esm({
|
|
|
312
312
|
"package.json"() {
|
|
313
313
|
package_default = {
|
|
314
314
|
name: "@retrivora-ai/rag-engine",
|
|
315
|
-
version: "2.
|
|
315
|
+
version: "2.6.0",
|
|
316
316
|
description: "Retrivora AI is a plug-and-play AI engine for RAG chat experiences \u2014 generic vector DB + LLM provider, embeddable or standalone.",
|
|
317
317
|
author: "Abhinav Alkuchi",
|
|
318
318
|
license: "UNLICENSED",
|
|
@@ -2774,7 +2774,7 @@ var _LicenseVerifier = class _LicenseVerifier {
|
|
|
2774
2774
|
*/
|
|
2775
2775
|
static async generateMachineFingerprint() {
|
|
2776
2776
|
if (typeof window !== "undefined" && typeof navigator !== "undefined") {
|
|
2777
|
-
return
|
|
2777
|
+
return "browser-client";
|
|
2778
2778
|
}
|
|
2779
2779
|
try {
|
|
2780
2780
|
const os2 = await import("os");
|
|
@@ -2846,7 +2846,7 @@ var _LicenseVerifier = class _LicenseVerifier {
|
|
|
2846
2846
|
try {
|
|
2847
2847
|
response = await fetch(endpoint, {
|
|
2848
2848
|
method: "POST",
|
|
2849
|
-
signal: AbortSignal.timeout(
|
|
2849
|
+
signal: AbortSignal.timeout(6e3),
|
|
2850
2850
|
headers: {
|
|
2851
2851
|
"Content-Type": "application/json",
|
|
2852
2852
|
"x-license-key": licenseKey
|
|
@@ -5576,27 +5576,384 @@ Text to extract from:
|
|
|
5576
5576
|
}
|
|
5577
5577
|
};
|
|
5578
5578
|
|
|
5579
|
+
// src/rag/HybridSearch.ts
|
|
5580
|
+
var HybridSearch = class {
|
|
5581
|
+
/**
|
|
5582
|
+
* Tokenize text into normalized alphanumeric keywords, keeping exact codes, numbers, and identifiers.
|
|
5583
|
+
*/
|
|
5584
|
+
static tokenize(text) {
|
|
5585
|
+
if (!text) return [];
|
|
5586
|
+
return text.toLowerCase().replace(/[^\w\s\-_#./:]/g, " ").split(/\s+/).map((t) => t.trim()).filter((t) => t.length > 1 && !this.stopWords.has(t));
|
|
5587
|
+
}
|
|
5588
|
+
/**
|
|
5589
|
+
* Calculate Okapi BM25 sparse scores for an array of document chunks given a query.
|
|
5590
|
+
*/
|
|
5591
|
+
static scoreBM25(documents, query, k1 = 1.5, b = 0.75) {
|
|
5592
|
+
const scores = /* @__PURE__ */ new Map();
|
|
5593
|
+
const queryTokens = this.tokenize(query);
|
|
5594
|
+
if (queryTokens.length === 0 || documents.length === 0) {
|
|
5595
|
+
documents.forEach((d) => scores.set(d.id, 0));
|
|
5596
|
+
return scores;
|
|
5597
|
+
}
|
|
5598
|
+
const docTokens = documents.map((d) => ({
|
|
5599
|
+
id: d.id,
|
|
5600
|
+
tokens: this.tokenize(d.content)
|
|
5601
|
+
}));
|
|
5602
|
+
const N = documents.length;
|
|
5603
|
+
const avgdl = docTokens.reduce((acc, d) => acc + d.tokens.length, 0) / (N || 1);
|
|
5604
|
+
const dfMap = /* @__PURE__ */ new Map();
|
|
5605
|
+
for (const qt of queryTokens) {
|
|
5606
|
+
let count = 0;
|
|
5607
|
+
for (const dt of docTokens) {
|
|
5608
|
+
if (dt.tokens.includes(qt)) count++;
|
|
5609
|
+
}
|
|
5610
|
+
dfMap.set(qt, count);
|
|
5611
|
+
}
|
|
5612
|
+
for (const dt of docTokens) {
|
|
5613
|
+
let score = 0;
|
|
5614
|
+
const docLen = dt.tokens.length;
|
|
5615
|
+
const tfMap = /* @__PURE__ */ new Map();
|
|
5616
|
+
for (const t of dt.tokens) {
|
|
5617
|
+
tfMap.set(t, (tfMap.get(t) || 0) + 1);
|
|
5618
|
+
}
|
|
5619
|
+
for (const qt of queryTokens) {
|
|
5620
|
+
const tf = tfMap.get(qt) || 0;
|
|
5621
|
+
if (tf === 0) continue;
|
|
5622
|
+
const df = dfMap.get(qt) || 0;
|
|
5623
|
+
const idf = Math.log((N - df + 0.5) / (df + 0.5) + 1);
|
|
5624
|
+
const numerator = tf * (k1 + 1);
|
|
5625
|
+
const denominator = tf + k1 * (1 - b + b * (docLen / (avgdl || 1)));
|
|
5626
|
+
score += idf * (numerator / (denominator || 1));
|
|
5627
|
+
}
|
|
5628
|
+
scores.set(dt.id, score);
|
|
5629
|
+
}
|
|
5630
|
+
return scores;
|
|
5631
|
+
}
|
|
5632
|
+
/**
|
|
5633
|
+
* Fuse Dense Vector Matches with Sparse BM25 Scores using Reciprocal Rank Fusion (RRF).
|
|
5634
|
+
*/
|
|
5635
|
+
static fuse(denseMatches, query, options = {}) {
|
|
5636
|
+
var _a2, _b, _c;
|
|
5637
|
+
if (denseMatches.length === 0) return [];
|
|
5638
|
+
const denseWeight = (_a2 = options.denseWeight) != null ? _a2 : 0.6;
|
|
5639
|
+
const sparseWeight = (_b = options.sparseWeight) != null ? _b : 0.4;
|
|
5640
|
+
const k = (_c = options.rrfK) != null ? _c : 60;
|
|
5641
|
+
const sortedDense = [...denseMatches].sort((a, b) => b.score - a.score);
|
|
5642
|
+
const denseRankMap = /* @__PURE__ */ new Map();
|
|
5643
|
+
sortedDense.forEach((match, idx) => {
|
|
5644
|
+
denseRankMap.set(match.id, idx + 1);
|
|
5645
|
+
});
|
|
5646
|
+
const docItems = denseMatches.map((m) => ({ id: m.id, content: m.content }));
|
|
5647
|
+
const bm25ScoreMap = this.scoreBM25(docItems, query, options.bm25K1, options.bm25B);
|
|
5648
|
+
const sortedSparse = [...denseMatches].sort((a, b) => {
|
|
5649
|
+
const scoreA = bm25ScoreMap.get(a.id) || 0;
|
|
5650
|
+
const scoreB = bm25ScoreMap.get(b.id) || 0;
|
|
5651
|
+
return scoreB - scoreA;
|
|
5652
|
+
});
|
|
5653
|
+
const sparseRankMap = /* @__PURE__ */ new Map();
|
|
5654
|
+
sortedSparse.forEach((match, idx) => {
|
|
5655
|
+
sparseRankMap.set(match.id, idx + 1);
|
|
5656
|
+
});
|
|
5657
|
+
const fusedMatches = denseMatches.map((match) => {
|
|
5658
|
+
var _a3;
|
|
5659
|
+
const denseRank = denseRankMap.get(match.id) || denseMatches.length;
|
|
5660
|
+
const sparseRank = sparseRankMap.get(match.id) || denseMatches.length;
|
|
5661
|
+
const denseRrf = denseWeight / (k + denseRank);
|
|
5662
|
+
const sparseRrf = sparseWeight / (k + sparseRank);
|
|
5663
|
+
const combinedRrf = denseRrf + sparseRrf;
|
|
5664
|
+
const bm25Raw = bm25ScoreMap.get(match.id) || 0;
|
|
5665
|
+
return __spreadProps(__spreadValues({}, match), {
|
|
5666
|
+
rrfScore: combinedRrf,
|
|
5667
|
+
bm25Score: bm25Raw,
|
|
5668
|
+
rerankScore: ((_a3 = match.rerankScore) != null ? _a3 : match.score) * 0.5 + combinedRrf * 50
|
|
5669
|
+
});
|
|
5670
|
+
});
|
|
5671
|
+
return fusedMatches.sort((a, b) => {
|
|
5672
|
+
var _a3, _b2;
|
|
5673
|
+
return ((_a3 = b.rerankScore) != null ? _a3 : b.score) - ((_b2 = a.rerankScore) != null ? _b2 : a.score);
|
|
5674
|
+
});
|
|
5675
|
+
}
|
|
5676
|
+
};
|
|
5677
|
+
HybridSearch.stopWords = /* @__PURE__ */ new Set([
|
|
5678
|
+
"a",
|
|
5679
|
+
"about",
|
|
5680
|
+
"above",
|
|
5681
|
+
"after",
|
|
5682
|
+
"again",
|
|
5683
|
+
"against",
|
|
5684
|
+
"all",
|
|
5685
|
+
"am",
|
|
5686
|
+
"an",
|
|
5687
|
+
"and",
|
|
5688
|
+
"any",
|
|
5689
|
+
"are",
|
|
5690
|
+
"aren't",
|
|
5691
|
+
"as",
|
|
5692
|
+
"at",
|
|
5693
|
+
"be",
|
|
5694
|
+
"because",
|
|
5695
|
+
"been",
|
|
5696
|
+
"before",
|
|
5697
|
+
"being",
|
|
5698
|
+
"below",
|
|
5699
|
+
"between",
|
|
5700
|
+
"both",
|
|
5701
|
+
"but",
|
|
5702
|
+
"by",
|
|
5703
|
+
"can't",
|
|
5704
|
+
"cannot",
|
|
5705
|
+
"could",
|
|
5706
|
+
"couldn't",
|
|
5707
|
+
"did",
|
|
5708
|
+
"didn't",
|
|
5709
|
+
"do",
|
|
5710
|
+
"does",
|
|
5711
|
+
"doesn't",
|
|
5712
|
+
"doing",
|
|
5713
|
+
"don't",
|
|
5714
|
+
"down",
|
|
5715
|
+
"during",
|
|
5716
|
+
"each",
|
|
5717
|
+
"few",
|
|
5718
|
+
"for",
|
|
5719
|
+
"from",
|
|
5720
|
+
"further",
|
|
5721
|
+
"had",
|
|
5722
|
+
"hadn't",
|
|
5723
|
+
"has",
|
|
5724
|
+
"hasn't",
|
|
5725
|
+
"have",
|
|
5726
|
+
"haven't",
|
|
5727
|
+
"having",
|
|
5728
|
+
"he",
|
|
5729
|
+
"he'd",
|
|
5730
|
+
"he'll",
|
|
5731
|
+
"he's",
|
|
5732
|
+
"her",
|
|
5733
|
+
"here",
|
|
5734
|
+
"here's",
|
|
5735
|
+
"hers",
|
|
5736
|
+
"herself",
|
|
5737
|
+
"him",
|
|
5738
|
+
"himself",
|
|
5739
|
+
"his",
|
|
5740
|
+
"how",
|
|
5741
|
+
"how's",
|
|
5742
|
+
"i",
|
|
5743
|
+
"i'd",
|
|
5744
|
+
"i'll",
|
|
5745
|
+
"i'm",
|
|
5746
|
+
"i've",
|
|
5747
|
+
"if",
|
|
5748
|
+
"in",
|
|
5749
|
+
"into",
|
|
5750
|
+
"is",
|
|
5751
|
+
"isn't",
|
|
5752
|
+
"it",
|
|
5753
|
+
"it's",
|
|
5754
|
+
"its",
|
|
5755
|
+
"itself",
|
|
5756
|
+
"let's",
|
|
5757
|
+
"me",
|
|
5758
|
+
"more",
|
|
5759
|
+
"most",
|
|
5760
|
+
"mustn't",
|
|
5761
|
+
"my",
|
|
5762
|
+
"myself",
|
|
5763
|
+
"no",
|
|
5764
|
+
"nor",
|
|
5765
|
+
"not",
|
|
5766
|
+
"of",
|
|
5767
|
+
"off",
|
|
5768
|
+
"on",
|
|
5769
|
+
"once",
|
|
5770
|
+
"only",
|
|
5771
|
+
"or",
|
|
5772
|
+
"other",
|
|
5773
|
+
"ought",
|
|
5774
|
+
"our",
|
|
5775
|
+
"ours",
|
|
5776
|
+
"ourselves",
|
|
5777
|
+
"out",
|
|
5778
|
+
"over",
|
|
5779
|
+
"own",
|
|
5780
|
+
"same",
|
|
5781
|
+
"shan't",
|
|
5782
|
+
"she",
|
|
5783
|
+
"she'd",
|
|
5784
|
+
"she'll",
|
|
5785
|
+
"she's",
|
|
5786
|
+
"should",
|
|
5787
|
+
"shouldn't",
|
|
5788
|
+
"so",
|
|
5789
|
+
"some",
|
|
5790
|
+
"such",
|
|
5791
|
+
"than",
|
|
5792
|
+
"that",
|
|
5793
|
+
"that's",
|
|
5794
|
+
"the",
|
|
5795
|
+
"their",
|
|
5796
|
+
"theirs",
|
|
5797
|
+
"them",
|
|
5798
|
+
"themselves",
|
|
5799
|
+
"then",
|
|
5800
|
+
"there",
|
|
5801
|
+
"there's",
|
|
5802
|
+
"these",
|
|
5803
|
+
"they",
|
|
5804
|
+
"they'd",
|
|
5805
|
+
"they'll",
|
|
5806
|
+
"they're",
|
|
5807
|
+
"they've",
|
|
5808
|
+
"this",
|
|
5809
|
+
"those",
|
|
5810
|
+
"through",
|
|
5811
|
+
"to",
|
|
5812
|
+
"too",
|
|
5813
|
+
"under",
|
|
5814
|
+
"until",
|
|
5815
|
+
"up",
|
|
5816
|
+
"very",
|
|
5817
|
+
"was",
|
|
5818
|
+
"wasn't",
|
|
5819
|
+
"we",
|
|
5820
|
+
"we'd",
|
|
5821
|
+
"we'll",
|
|
5822
|
+
"we're",
|
|
5823
|
+
"we've",
|
|
5824
|
+
"were",
|
|
5825
|
+
"weren't",
|
|
5826
|
+
"what",
|
|
5827
|
+
"what's",
|
|
5828
|
+
"when",
|
|
5829
|
+
"when's",
|
|
5830
|
+
"where",
|
|
5831
|
+
"where's",
|
|
5832
|
+
"which",
|
|
5833
|
+
"while",
|
|
5834
|
+
"who",
|
|
5835
|
+
"who's",
|
|
5836
|
+
"whom",
|
|
5837
|
+
"why",
|
|
5838
|
+
"why's",
|
|
5839
|
+
"with",
|
|
5840
|
+
"won't",
|
|
5841
|
+
"would",
|
|
5842
|
+
"wouldn't",
|
|
5843
|
+
"you",
|
|
5844
|
+
"you'd",
|
|
5845
|
+
"you'll",
|
|
5846
|
+
"you're",
|
|
5847
|
+
"you've",
|
|
5848
|
+
"your",
|
|
5849
|
+
"yours",
|
|
5850
|
+
"yourself",
|
|
5851
|
+
"yourselves"
|
|
5852
|
+
]);
|
|
5853
|
+
|
|
5579
5854
|
// src/rag/Reranker.ts
|
|
5580
5855
|
var MAX_RERANK_CONTENT_CHARS = 64e3;
|
|
5581
5856
|
var Reranker = class {
|
|
5582
5857
|
/**
|
|
5583
|
-
* Re-ranks matches based on a secondary relevance score using
|
|
5584
|
-
*
|
|
5585
|
-
* Fix RAG-2: keyword re-ranking no longer mutates the original `score`
|
|
5586
|
-
* field (which consumers interpret as the raw vector similarity). A new
|
|
5587
|
-
* `rerankScore` field is added so both the original similarity and the
|
|
5588
|
-
* boosted rank signal are available to callers.
|
|
5858
|
+
* Re-ranks matches based on a secondary relevance score using Hybrid BM25, Cohere, Jina or LLM.
|
|
5589
5859
|
*/
|
|
5590
|
-
async rerank(matches, query, limit = 5, llm) {
|
|
5591
|
-
|
|
5592
|
-
|
|
5860
|
+
async rerank(matches, query, limit = 5, llm, options = {}) {
|
|
5861
|
+
var _a2, _b;
|
|
5862
|
+
if (!matches || matches.length <= 1) {
|
|
5863
|
+
return matches ? matches.slice(0, limit) : [];
|
|
5593
5864
|
}
|
|
5594
|
-
|
|
5595
|
-
|
|
5596
|
-
|
|
5597
|
-
|
|
5598
|
-
|
|
5865
|
+
const cohereKey = options.cohereApiKey || (typeof process !== "undefined" ? (_a2 = process.env) == null ? void 0 : _a2.COHERE_API_KEY : void 0);
|
|
5866
|
+
const jinaKey = options.jinaApiKey || (typeof process !== "undefined" ? (_b = process.env) == null ? void 0 : _b.JINA_API_KEY : void 0);
|
|
5867
|
+
if ((options.provider === "cohere" || cohereKey) && cohereKey) {
|
|
5868
|
+
try {
|
|
5869
|
+
const cohereResult = await this.cohereRerank(matches, query, limit, cohereKey, options.cohereModel);
|
|
5870
|
+
if (cohereResult.length > 0) return cohereResult;
|
|
5871
|
+
} catch (err) {
|
|
5872
|
+
console.warn("[Reranker] Cohere reranking failed, falling back to HybridSearch:", err);
|
|
5873
|
+
}
|
|
5874
|
+
}
|
|
5875
|
+
if ((options.provider === "jina" || jinaKey) && jinaKey) {
|
|
5876
|
+
try {
|
|
5877
|
+
const jinaResult = await this.jinaRerank(matches, query, limit, jinaKey, options.jinaModel);
|
|
5878
|
+
if (jinaResult.length > 0) return jinaResult;
|
|
5879
|
+
} catch (err) {
|
|
5880
|
+
console.warn("[Reranker] Jina reranking failed, falling back to HybridSearch:", err);
|
|
5881
|
+
}
|
|
5882
|
+
}
|
|
5883
|
+
if (!options.provider || options.provider === "hybrid") {
|
|
5884
|
+
const fused = HybridSearch.fuse(matches, query);
|
|
5885
|
+
if (fused.length > 0) {
|
|
5886
|
+
return fused.slice(0, limit);
|
|
5887
|
+
}
|
|
5888
|
+
}
|
|
5889
|
+
if (llm && (options.provider === "llm" || !options.provider)) {
|
|
5890
|
+
try {
|
|
5891
|
+
return await this.llmRerank(matches, query, limit, llm);
|
|
5892
|
+
} catch (error) {
|
|
5893
|
+
console.warn("[Reranker] LLM re-ranking failed, falling back to keyword sorting:", error);
|
|
5894
|
+
}
|
|
5895
|
+
}
|
|
5896
|
+
return this.keywordRerank(matches, query, limit);
|
|
5897
|
+
}
|
|
5898
|
+
/**
|
|
5899
|
+
* Cohere Rerank v3.5 API integration.
|
|
5900
|
+
*/
|
|
5901
|
+
async cohereRerank(matches, query, limit, apiKey, model = "rerank-v3.5") {
|
|
5902
|
+
const documents = matches.map((m) => m.content || "");
|
|
5903
|
+
const response = await fetch("https://api.cohere.com/v1/rerank", {
|
|
5904
|
+
method: "POST",
|
|
5905
|
+
headers: {
|
|
5906
|
+
"Authorization": `Bearer ${apiKey}`,
|
|
5907
|
+
"Content-Type": "application/json"
|
|
5908
|
+
},
|
|
5909
|
+
body: JSON.stringify({
|
|
5910
|
+
model,
|
|
5911
|
+
query,
|
|
5912
|
+
documents,
|
|
5913
|
+
top_n: limit
|
|
5914
|
+
})
|
|
5915
|
+
});
|
|
5916
|
+
if (!response.ok) {
|
|
5917
|
+
throw new Error(`Cohere API returned status ${response.status}`);
|
|
5918
|
+
}
|
|
5919
|
+
const data = await response.json();
|
|
5920
|
+
if (!data.results || !Array.isArray(data.results)) return [];
|
|
5921
|
+
return data.results.map((res) => {
|
|
5922
|
+
const match = matches[res.index];
|
|
5923
|
+
return __spreadProps(__spreadValues({}, match), {
|
|
5924
|
+
rerankScore: res.relevance_score
|
|
5925
|
+
});
|
|
5926
|
+
});
|
|
5927
|
+
}
|
|
5928
|
+
/**
|
|
5929
|
+
* Jina AI Reranker v2 API integration.
|
|
5930
|
+
*/
|
|
5931
|
+
async jinaRerank(matches, query, limit, apiKey, model = "jina-reranker-v2-base-multilingual") {
|
|
5932
|
+
const documents = matches.map((m) => m.content || "");
|
|
5933
|
+
const response = await fetch("https://api.jina.ai/v1/rerank", {
|
|
5934
|
+
method: "POST",
|
|
5935
|
+
headers: {
|
|
5936
|
+
"Authorization": `Bearer ${apiKey}`,
|
|
5937
|
+
"Content-Type": "application/json"
|
|
5938
|
+
},
|
|
5939
|
+
body: JSON.stringify({
|
|
5940
|
+
model,
|
|
5941
|
+
query,
|
|
5942
|
+
documents,
|
|
5943
|
+
top_n: limit
|
|
5944
|
+
})
|
|
5945
|
+
});
|
|
5946
|
+
if (!response.ok) {
|
|
5947
|
+
throw new Error(`Jina API returned status ${response.status}`);
|
|
5599
5948
|
}
|
|
5949
|
+
const data = await response.json();
|
|
5950
|
+
if (!data.results || !Array.isArray(data.results)) return [];
|
|
5951
|
+
return data.results.map((res) => {
|
|
5952
|
+
const match = matches[res.index];
|
|
5953
|
+
return __spreadProps(__spreadValues({}, match), {
|
|
5954
|
+
rerankScore: res.relevance_score
|
|
5955
|
+
});
|
|
5956
|
+
});
|
|
5600
5957
|
}
|
|
5601
5958
|
/**
|
|
5602
5959
|
* Keyword-based re-ranking fallback.
|
|
@@ -5682,6 +6039,176 @@ ${docList}` }],
|
|
|
5682
6039
|
}
|
|
5683
6040
|
};
|
|
5684
6041
|
|
|
6042
|
+
// src/rag/RAGEvaluator.ts
|
|
6043
|
+
var RAGEvaluator = class {
|
|
6044
|
+
/**
|
|
6045
|
+
* Evaluate a RAG interaction across the full RAG Triad.
|
|
6046
|
+
*/
|
|
6047
|
+
static async evaluate(query, context, answer, llm) {
|
|
6048
|
+
if (!llm || !context || !answer) {
|
|
6049
|
+
return this.deterministicHeuristic(query, context, answer);
|
|
6050
|
+
}
|
|
6051
|
+
try {
|
|
6052
|
+
const prompt = `You are an automated RAG Triad Evaluator. Evaluate the quality of the following RAG system interaction:
|
|
6053
|
+
|
|
6054
|
+
USER QUERY:
|
|
6055
|
+
${query}
|
|
6056
|
+
|
|
6057
|
+
RETRIEVED CONTEXT:
|
|
6058
|
+
${context.slice(0, 3e3)}
|
|
6059
|
+
|
|
6060
|
+
GENERATED ANSWER:
|
|
6061
|
+
${answer}
|
|
6062
|
+
|
|
6063
|
+
Evaluate these 3 metrics on a scale of 0.0 to 1.0:
|
|
6064
|
+
1. "contextRelevance": Are the retrieved documents relevant to the query? (1.0 = highly relevant, 0.0 = irrelevant)
|
|
6065
|
+
2. "faithfulness": Is every claim in the generated answer supported by the retrieved context? (1.0 = 100% grounded/no hallucination, 0.0 = completely fabricated)
|
|
6066
|
+
3. "answerRelevance": Does the answer directly address the user's question? (1.0 = direct answer, 0.0 = evasive/unrelated)
|
|
6067
|
+
|
|
6068
|
+
Return ONLY valid JSON with no markdown fences:
|
|
6069
|
+
{"contextRelevance": 0.95, "faithfulness": 0.90, "answerRelevance": 0.92, "reason": "Answer is fully supported by the document."}`;
|
|
6070
|
+
const raw = await llm.chat(
|
|
6071
|
+
[{ role: "user", content: prompt }],
|
|
6072
|
+
"",
|
|
6073
|
+
{
|
|
6074
|
+
systemPrompt: "You are a strict AI safety and hallucination evaluator. Output only valid JSON.",
|
|
6075
|
+
temperature: 0,
|
|
6076
|
+
maxTokens: 150
|
|
6077
|
+
}
|
|
6078
|
+
);
|
|
6079
|
+
const cleaned = raw.replace(/```json|```/g, "").trim();
|
|
6080
|
+
const parsed = JSON.parse(cleaned);
|
|
6081
|
+
const cr = typeof parsed.contextRelevance === "number" ? Math.max(0, Math.min(1, parsed.contextRelevance)) : 0.85;
|
|
6082
|
+
const f = typeof parsed.faithfulness === "number" ? Math.max(0, Math.min(1, parsed.faithfulness)) : 0.9;
|
|
6083
|
+
const ar = typeof parsed.answerRelevance === "number" ? Math.max(0, Math.min(1, parsed.answerRelevance)) : 0.88;
|
|
6084
|
+
const overall = Number((cr * 0.3 + f * 0.4 + ar * 0.3).toFixed(2));
|
|
6085
|
+
return {
|
|
6086
|
+
contextRelevance: cr,
|
|
6087
|
+
faithfulness: f,
|
|
6088
|
+
answerRelevance: ar,
|
|
6089
|
+
overallQuality: overall,
|
|
6090
|
+
isHallucinated: f < 0.6,
|
|
6091
|
+
reason: parsed.reason || "Evaluated successfully."
|
|
6092
|
+
};
|
|
6093
|
+
} catch (err) {
|
|
6094
|
+
return this.deterministicHeuristic(query, context, answer);
|
|
6095
|
+
}
|
|
6096
|
+
}
|
|
6097
|
+
/**
|
|
6098
|
+
* Deterministic fast keyword-overlap heuristic when LLM evaluation is not invoked.
|
|
6099
|
+
*/
|
|
6100
|
+
static deterministicHeuristic(query, context, answer) {
|
|
6101
|
+
if (!context || !answer) {
|
|
6102
|
+
return { contextRelevance: 0.5, faithfulness: 0.8, answerRelevance: 0.7, overallQuality: 0.67, isHallucinated: false };
|
|
6103
|
+
}
|
|
6104
|
+
const queryWords = query.toLowerCase().split(/\s+/).filter((w) => w.length > 2);
|
|
6105
|
+
const contextLower = context.toLowerCase();
|
|
6106
|
+
const answerLower = answer.toLowerCase();
|
|
6107
|
+
let queryContextHits = 0;
|
|
6108
|
+
queryWords.forEach((w) => {
|
|
6109
|
+
if (contextLower.includes(w)) queryContextHits++;
|
|
6110
|
+
});
|
|
6111
|
+
const contextRelevance = queryWords.length > 0 ? Math.min(1, queryContextHits / queryWords.length + 0.3) : 0.8;
|
|
6112
|
+
const numbersInAnswer = answer.match(/\b\d+(?:\.\d+)?%?\b/g) || [];
|
|
6113
|
+
let numberHits = 0;
|
|
6114
|
+
numbersInAnswer.forEach((num) => {
|
|
6115
|
+
if (context.includes(num)) numberHits++;
|
|
6116
|
+
});
|
|
6117
|
+
const faithfulness = numbersInAnswer.length > 0 ? Math.min(1, numberHits / numbersInAnswer.length + 0.2) : 0.9;
|
|
6118
|
+
const answerRelevance = 0.85;
|
|
6119
|
+
const overallQuality = Number((contextRelevance * 0.3 + faithfulness * 0.4 + answerRelevance * 0.3).toFixed(2));
|
|
6120
|
+
return {
|
|
6121
|
+
contextRelevance: Number(contextRelevance.toFixed(2)),
|
|
6122
|
+
faithfulness: Number(faithfulness.toFixed(2)),
|
|
6123
|
+
answerRelevance,
|
|
6124
|
+
overallQuality,
|
|
6125
|
+
isHallucinated: faithfulness < 0.5,
|
|
6126
|
+
reason: "Computed via deterministic grounding heuristic."
|
|
6127
|
+
};
|
|
6128
|
+
}
|
|
6129
|
+
};
|
|
6130
|
+
|
|
6131
|
+
// src/security/PIIMasker.ts
|
|
6132
|
+
var PIIMasker = class {
|
|
6133
|
+
/**
|
|
6134
|
+
* Redact sensitive information from text.
|
|
6135
|
+
*/
|
|
6136
|
+
static mask(text, options = {}) {
|
|
6137
|
+
if (!text || typeof text !== "string") return text;
|
|
6138
|
+
let sanitized = text;
|
|
6139
|
+
if (options.maskApiKeys !== false) {
|
|
6140
|
+
sanitized = sanitized.replace(this.API_KEY_REGEX, "[REDACTED_API_KEY]");
|
|
6141
|
+
}
|
|
6142
|
+
if (options.maskCreditCards !== false) {
|
|
6143
|
+
sanitized = sanitized.replace(this.CREDIT_CARD_REGEX, (match) => {
|
|
6144
|
+
const digits = match.replace(/[- ]/g, "");
|
|
6145
|
+
if (digits.length >= 13 && digits.length <= 19) {
|
|
6146
|
+
return "[REDACTED_CREDIT_CARD]";
|
|
6147
|
+
}
|
|
6148
|
+
return match;
|
|
6149
|
+
});
|
|
6150
|
+
}
|
|
6151
|
+
if (options.maskSSN !== false) {
|
|
6152
|
+
sanitized = sanitized.replace(this.SSN_REGEX, "[REDACTED_SSN]");
|
|
6153
|
+
}
|
|
6154
|
+
if (options.maskEmails !== false) {
|
|
6155
|
+
sanitized = sanitized.replace(this.EMAIL_REGEX, "[REDACTED_EMAIL]");
|
|
6156
|
+
}
|
|
6157
|
+
if (options.maskPhones !== false) {
|
|
6158
|
+
sanitized = sanitized.replace(this.PHONE_REGEX, "[REDACTED_PHONE]");
|
|
6159
|
+
}
|
|
6160
|
+
if (options.maskIPs === true) {
|
|
6161
|
+
sanitized = sanitized.replace(this.IP_REGEX, "[REDACTED_IP]");
|
|
6162
|
+
}
|
|
6163
|
+
return sanitized;
|
|
6164
|
+
}
|
|
6165
|
+
};
|
|
6166
|
+
PIIMasker.SSN_REGEX = /\b(?!000|666|9\d{2})\d{3}[- ]?(?!00)\d{2}[- ]?(?!0000)\d{4}\b/g;
|
|
6167
|
+
PIIMasker.CREDIT_CARD_REGEX = /\b(?:\d{4}[- ]?){3}\d{4}\b|\b\d{15,16}\b/g;
|
|
6168
|
+
PIIMasker.EMAIL_REGEX = /\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b/g;
|
|
6169
|
+
PIIMasker.PHONE_REGEX = /\b(?:\+?\d{1,3}[-.\s]?)?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}\b/g;
|
|
6170
|
+
PIIMasker.API_KEY_REGEX = /\b(?:sk-[a-zA-Z0-9]{20,}|AKIA[0-9A-Z]{16}|rtv_[a-zA-Z0-9_-]{20,}|ghp_[a-zA-Z0-9]{36}|bearer\s+[a-zA-Z0-9._-]{20,})\b/gi;
|
|
6171
|
+
PIIMasker.IP_REGEX = /\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/g;
|
|
6172
|
+
|
|
6173
|
+
// src/security/PromptGuard.ts
|
|
6174
|
+
var PromptGuard = class {
|
|
6175
|
+
/**
|
|
6176
|
+
* Scan and evaluate user query or document text for prompt injection vectors.
|
|
6177
|
+
*/
|
|
6178
|
+
static evaluate(text) {
|
|
6179
|
+
if (!text || typeof text !== "string") {
|
|
6180
|
+
return { isSafe: true, threatLevel: "none", detectedPatterns: [], sanitizedText: text };
|
|
6181
|
+
}
|
|
6182
|
+
const detected = [];
|
|
6183
|
+
let maxSeverity = "none";
|
|
6184
|
+
for (const item of this.INJECTION_PATTERNS) {
|
|
6185
|
+
if (item.pattern.test(text)) {
|
|
6186
|
+
detected.push(item.name);
|
|
6187
|
+
if (item.severity === "high") {
|
|
6188
|
+
maxSeverity = "high";
|
|
6189
|
+
} else if (item.severity === "medium" && maxSeverity !== "high") {
|
|
6190
|
+
maxSeverity = "medium";
|
|
6191
|
+
}
|
|
6192
|
+
}
|
|
6193
|
+
}
|
|
6194
|
+
const sanitized = text.replace(/<\/?(?:system|instruction|admin|prompt_override)>/gi, "");
|
|
6195
|
+
return {
|
|
6196
|
+
isSafe: maxSeverity !== "high",
|
|
6197
|
+
threatLevel: maxSeverity,
|
|
6198
|
+
detectedPatterns: detected,
|
|
6199
|
+
sanitizedText: sanitized
|
|
6200
|
+
};
|
|
6201
|
+
}
|
|
6202
|
+
};
|
|
6203
|
+
PromptGuard.INJECTION_PATTERNS = [
|
|
6204
|
+
{ pattern: /ignore\s+(?:all\s+)?(?:previous|prior|above)\s+(?:instructions|prompts|rules)/i, name: "override_instructions", severity: "high" },
|
|
6205
|
+
{ pattern: /(?:show|repeat|print|reveal|expose|output)\s+(?:the\s+)?(?:system\s+prompt|initial\s+instructions|system\s+message)/i, name: "system_prompt_leak", severity: "high" },
|
|
6206
|
+
{ pattern: /you\s+are\s+now\s+(?:in\s+developer\s+mode|unrestricted|DAN|jailbroken|godmode)/i, name: "jailbreak_persona", severity: "high" },
|
|
6207
|
+
{ pattern: /<\/?(?:system|instruction|admin|prompt_override)>/i, name: "tag_injection", severity: "medium" },
|
|
6208
|
+
{ pattern: /base64\s*decode\s*and\s*execute/i, name: "obfuscated_payload", severity: "high" },
|
|
6209
|
+
{ pattern: /do\s+anything\s+now/i, name: "dan_variant", severity: "high" }
|
|
6210
|
+
];
|
|
6211
|
+
|
|
5685
6212
|
// src/rag/LlamaIndexIngestor.ts
|
|
5686
6213
|
var LlamaIndexIngestor = class {
|
|
5687
6214
|
/**
|
|
@@ -9623,7 +10150,7 @@ ${m.content}`).join("\n\n---\n\n");
|
|
|
9623
10150
|
*/
|
|
9624
10151
|
async processEmbeddings(chunks) {
|
|
9625
10152
|
if (chunks.length === 0) return [];
|
|
9626
|
-
const texts = chunks.map((c) => c.content);
|
|
10153
|
+
const texts = chunks.map((c) => PIIMasker.mask(c.content));
|
|
9627
10154
|
if (typeof this.embeddingProvider.batchEmbed === "function") {
|
|
9628
10155
|
try {
|
|
9629
10156
|
const batchSize = 50;
|
|
@@ -9829,10 +10356,12 @@ ${m.content}`).join("\n\n---\n\n");
|
|
|
9829
10356
|
const requestId = `req_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
|
|
9830
10357
|
const requestStart = performance.now();
|
|
9831
10358
|
try {
|
|
9832
|
-
|
|
10359
|
+
const guardCheck = PromptGuard.evaluate(question);
|
|
10360
|
+
let effectiveQuestion = guardCheck.sanitizedText;
|
|
10361
|
+
effectiveQuestion = PIIMasker.mask(effectiveQuestion);
|
|
9833
10362
|
let promptOptResult;
|
|
9834
10363
|
if (((_e = this.config.rag) == null ? void 0 : _e.optimizePrompt) !== false) {
|
|
9835
|
-
promptOptResult = PromptOptimizer.optimize(
|
|
10364
|
+
promptOptResult = PromptOptimizer.optimize(effectiveQuestion, history);
|
|
9836
10365
|
effectiveQuestion = promptOptResult.optimizedPrompt;
|
|
9837
10366
|
}
|
|
9838
10367
|
let searchQuery = effectiveQuestion;
|
|
@@ -10117,6 +10646,7 @@ ${context}`;
|
|
|
10117
10646
|
uiTransformationPromise,
|
|
10118
10647
|
awaitHallucination ? hallucinationScoringPromise : Promise.resolve(void 0)
|
|
10119
10648
|
]));
|
|
10649
|
+
const triadScore = yield new __await(RAGEvaluator.evaluate(effectiveQuestion, context, fullReply));
|
|
10120
10650
|
const buildTrace = (hScore) => __spreadValues({
|
|
10121
10651
|
requestId,
|
|
10122
10652
|
query: question,
|
|
@@ -10135,7 +10665,13 @@ ${context}`;
|
|
|
10135
10665
|
}),
|
|
10136
10666
|
latency,
|
|
10137
10667
|
tokens,
|
|
10138
|
-
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
10668
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
10669
|
+
ragTriad: {
|
|
10670
|
+
contextRelevance: triadScore.contextRelevance,
|
|
10671
|
+
faithfulness: triadScore.faithfulness,
|
|
10672
|
+
answerRelevance: triadScore.answerRelevance,
|
|
10673
|
+
overallQuality: triadScore.overallQuality
|
|
10674
|
+
}
|
|
10139
10675
|
}, hScore ? {
|
|
10140
10676
|
hallucinationScore: hScore.score,
|
|
10141
10677
|
hallucinationReason: hScore.reason
|
|
@@ -10718,6 +11254,374 @@ var VectorPlugin = class {
|
|
|
10718
11254
|
}
|
|
10719
11255
|
};
|
|
10720
11256
|
|
|
11257
|
+
// src/utils/UniversalTableParser.ts
|
|
11258
|
+
var UniversalTableParser = class {
|
|
11259
|
+
/**
|
|
11260
|
+
* Parse a raw 2D grid of strings with complete matrix intelligence.
|
|
11261
|
+
*/
|
|
11262
|
+
static parseGrid(grid, sheetName = "Data") {
|
|
11263
|
+
if (!grid || grid.length === 0) return "";
|
|
11264
|
+
const cleanGrid = grid.map((row) => Array.isArray(row) ? row.map((c) => c !== null && c !== void 0 ? String(c).trim() : "") : []).filter((row) => row.some((cell) => cell.length > 0));
|
|
11265
|
+
if (cleanGrid.length === 0) return "";
|
|
11266
|
+
const { headerRows, dataRows } = this.splitHeaderAndDataRows(cleanGrid);
|
|
11267
|
+
const fusedHeaders = this.fuseHeaders(headerRows);
|
|
11268
|
+
const matrixInfo = this.analyzeMatrixStructure(fusedHeaders, dataRows);
|
|
11269
|
+
const sections = [`--- Sheet: ${sheetName} ---`];
|
|
11270
|
+
const markdownTable = this.generateMarkdownTable(fusedHeaders, dataRows);
|
|
11271
|
+
if (markdownTable) {
|
|
11272
|
+
sections.push(`
|
|
11273
|
+
## Table: ${sheetName}
|
|
11274
|
+
${markdownTable}`);
|
|
11275
|
+
}
|
|
11276
|
+
if (matrixInfo.isComparisonMatrix && matrixInfo.entityIndices.length > 0) {
|
|
11277
|
+
const entityProfiles = this.generateEntityProfiles(fusedHeaders, dataRows, matrixInfo);
|
|
11278
|
+
if (entityProfiles) {
|
|
11279
|
+
sections.push(`
|
|
11280
|
+
## Entity Comparison Profiles
|
|
11281
|
+
${entityProfiles}`);
|
|
11282
|
+
}
|
|
11283
|
+
const criteriaComparisons = this.generateCriteriaComparisons(fusedHeaders, dataRows, matrixInfo);
|
|
11284
|
+
if (criteriaComparisons) {
|
|
11285
|
+
sections.push(`
|
|
11286
|
+
## Criteria Breakdown & Comparisons
|
|
11287
|
+
${criteriaComparisons}`);
|
|
11288
|
+
}
|
|
11289
|
+
} else {
|
|
11290
|
+
const contextualRows = this.generateContextualRows(fusedHeaders, dataRows);
|
|
11291
|
+
if (contextualRows) {
|
|
11292
|
+
sections.push(`
|
|
11293
|
+
## Structured Records
|
|
11294
|
+
${contextualRows}`);
|
|
11295
|
+
}
|
|
11296
|
+
}
|
|
11297
|
+
return sections.join("\n\n");
|
|
11298
|
+
}
|
|
11299
|
+
/**
|
|
11300
|
+
* Parse a raw CSV / TSV text string into tri-perspective contextual format.
|
|
11301
|
+
*/
|
|
11302
|
+
static parseDelimitedText(text, title = "Table", delimiter) {
|
|
11303
|
+
if (!text || !text.trim()) return "";
|
|
11304
|
+
if (!delimiter) {
|
|
11305
|
+
const firstLine = text.split(/\r?\n/)[0] || "";
|
|
11306
|
+
const commaCount = (firstLine.match(/,/g) || []).length;
|
|
11307
|
+
const tabCount = (firstLine.match(/\t/g) || []).length;
|
|
11308
|
+
const semiCount = (firstLine.match(/;/g) || []).length;
|
|
11309
|
+
const pipeCount = (firstLine.match(/\|/g) || []).length;
|
|
11310
|
+
if (tabCount > commaCount && tabCount > semiCount) delimiter = " ";
|
|
11311
|
+
else if (semiCount > commaCount && semiCount > tabCount) delimiter = ";";
|
|
11312
|
+
else if (pipeCount > commaCount && pipeCount > semiCount) delimiter = "|";
|
|
11313
|
+
else delimiter = ",";
|
|
11314
|
+
}
|
|
11315
|
+
const lines = text.split(/\r?\n/);
|
|
11316
|
+
const grid = [];
|
|
11317
|
+
for (const line of lines) {
|
|
11318
|
+
if (!line.trim()) continue;
|
|
11319
|
+
const row = [];
|
|
11320
|
+
let inQuote = false;
|
|
11321
|
+
let current = "";
|
|
11322
|
+
for (let i = 0; i < line.length; i++) {
|
|
11323
|
+
const char = line[i];
|
|
11324
|
+
if (char === '"') {
|
|
11325
|
+
if (inQuote && line[i + 1] === '"') {
|
|
11326
|
+
current += '"';
|
|
11327
|
+
i++;
|
|
11328
|
+
} else {
|
|
11329
|
+
inQuote = !inQuote;
|
|
11330
|
+
}
|
|
11331
|
+
} else if (char === delimiter && !inQuote) {
|
|
11332
|
+
row.push(current.trim());
|
|
11333
|
+
current = "";
|
|
11334
|
+
} else {
|
|
11335
|
+
current += char;
|
|
11336
|
+
}
|
|
11337
|
+
}
|
|
11338
|
+
row.push(current.trim());
|
|
11339
|
+
grid.push(row);
|
|
11340
|
+
}
|
|
11341
|
+
return this.parseGrid(grid, title);
|
|
11342
|
+
}
|
|
11343
|
+
/**
|
|
11344
|
+
* Parse a SheetJS sheet object with complete merged cell and matrix intelligence.
|
|
11345
|
+
*/
|
|
11346
|
+
static parseSheet(sheet, sheetName, XLSX) {
|
|
11347
|
+
if (!sheet) return "";
|
|
11348
|
+
const grid = this.extractAndUnrollGrid(sheet, XLSX);
|
|
11349
|
+
if (!grid || grid.length === 0) {
|
|
11350
|
+
const csv = XLSX.utils.sheet_to_csv(sheet, { blankrows: false });
|
|
11351
|
+
return csv && csv.trim() ? `--- Sheet: ${sheetName} ---
|
|
11352
|
+
${csv.trim()}` : "";
|
|
11353
|
+
}
|
|
11354
|
+
return this.parseGrid(grid, sheetName);
|
|
11355
|
+
}
|
|
11356
|
+
/**
|
|
11357
|
+
* Unrolls SheetJS merged cell ranges (`!merges`) across the 2D grid
|
|
11358
|
+
* so no child cell is left blank.
|
|
11359
|
+
*/
|
|
11360
|
+
static extractAndUnrollGrid(sheet, XLSX) {
|
|
11361
|
+
var _a2;
|
|
11362
|
+
const rawGrid = XLSX.utils.sheet_to_json(sheet, { header: 1, blankrows: false, defval: "" });
|
|
11363
|
+
if (!rawGrid || rawGrid.length === 0) return [];
|
|
11364
|
+
const grid = rawGrid.map(
|
|
11365
|
+
(row) => (Array.isArray(row) ? row : []).map((cell) => cell !== null && cell !== void 0 ? String(cell).trim() : "")
|
|
11366
|
+
);
|
|
11367
|
+
const merges = sheet["!merges"] || [];
|
|
11368
|
+
for (const merge of merges) {
|
|
11369
|
+
const { s, e } = merge;
|
|
11370
|
+
const originVal = ((_a2 = grid[s.r]) == null ? void 0 : _a2[s.c]) || "";
|
|
11371
|
+
if (!originVal) continue;
|
|
11372
|
+
for (let r = s.r; r <= e.r; r++) {
|
|
11373
|
+
if (!grid[r]) grid[r] = [];
|
|
11374
|
+
for (let c = s.c; c <= e.c; c++) {
|
|
11375
|
+
if (!grid[r][c] || grid[r][c] === "") {
|
|
11376
|
+
grid[r][c] = originVal;
|
|
11377
|
+
}
|
|
11378
|
+
}
|
|
11379
|
+
}
|
|
11380
|
+
}
|
|
11381
|
+
return grid.filter((row) => row.some((cell) => cell.length > 0));
|
|
11382
|
+
}
|
|
11383
|
+
/**
|
|
11384
|
+
* Intelligently identify where header rows end and data rows begin.
|
|
11385
|
+
*/
|
|
11386
|
+
static splitHeaderAndDataRows(grid) {
|
|
11387
|
+
if (grid.length <= 1) {
|
|
11388
|
+
return { headerRows: grid, dataRows: [] };
|
|
11389
|
+
}
|
|
11390
|
+
let headerRowCount = 1;
|
|
11391
|
+
for (let r = 1; r < Math.min(4, grid.length); r++) {
|
|
11392
|
+
const row = grid[r];
|
|
11393
|
+
const nonEmptyCells = row.filter(Boolean);
|
|
11394
|
+
if (nonEmptyCells.length === 0) continue;
|
|
11395
|
+
const isDataRow = nonEmptyCells.some(
|
|
11396
|
+
(c) => /^-?\d+(?:\.\d+)?%?$/.test(c) || /^\$?[\d,.]+(?:\s*(?:mil|k|m|b|usd|eur))?$/i.test(c) || /^(yes|no|true|false|pass|fail|compliant|non-compliant)$/i.test(c)
|
|
11397
|
+
);
|
|
11398
|
+
const hasSubheaderLabels = nonEmptyCells.some(
|
|
11399
|
+
(c) => /\b(bidder|vendor|supplier|score|weight|criteria|item|specification|technical|commercial|rating|status|cost|price|qty|unit)\b/i.test(c)
|
|
11400
|
+
);
|
|
11401
|
+
if (!isDataRow && hasSubheaderLabels) {
|
|
11402
|
+
headerRowCount = r + 1;
|
|
11403
|
+
} else if (!isDataRow && r === 1 && nonEmptyCells.length >= 2) {
|
|
11404
|
+
headerRowCount = 2;
|
|
11405
|
+
} else {
|
|
11406
|
+
break;
|
|
11407
|
+
}
|
|
11408
|
+
}
|
|
11409
|
+
const headerRows = grid.slice(0, headerRowCount);
|
|
11410
|
+
const dataRows = grid.slice(headerRowCount);
|
|
11411
|
+
return { headerRows, dataRows };
|
|
11412
|
+
}
|
|
11413
|
+
/**
|
|
11414
|
+
* Fuses multi-tier header rows into composite paths (e.g. `Commercial > Payment Terms > Bidder A`).
|
|
11415
|
+
*/
|
|
11416
|
+
static fuseHeaders(headerRows) {
|
|
11417
|
+
var _a2, _b;
|
|
11418
|
+
if (headerRows.length === 0) return [];
|
|
11419
|
+
if (headerRows.length === 1) {
|
|
11420
|
+
return headerRows[0].map((h, i) => h || `Column_${i + 1}`);
|
|
11421
|
+
}
|
|
11422
|
+
const maxCols = Math.max(...headerRows.map((r) => r.length));
|
|
11423
|
+
const fused = [];
|
|
11424
|
+
for (let c = 0; c < maxCols; c++) {
|
|
11425
|
+
const components = [];
|
|
11426
|
+
for (let r = 0; r < headerRows.length; r++) {
|
|
11427
|
+
const val = (_b = (_a2 = headerRows[r]) == null ? void 0 : _a2[c]) == null ? void 0 : _b.trim();
|
|
11428
|
+
if (val && !components.includes(val)) {
|
|
11429
|
+
components.push(val);
|
|
11430
|
+
}
|
|
11431
|
+
}
|
|
11432
|
+
fused.push(components.join(" > ") || `Column_${c + 1}`);
|
|
11433
|
+
}
|
|
11434
|
+
return fused;
|
|
11435
|
+
}
|
|
11436
|
+
/**
|
|
11437
|
+
* Analyzes whether the table is a comparison matrix (columns = entities, rows = criteria).
|
|
11438
|
+
*/
|
|
11439
|
+
static analyzeMatrixStructure(headers, dataRows) {
|
|
11440
|
+
const entityPattern = /\b(bidder|vendor|supplier|candidate|option|company|contractor|partner|proposal|firm|provider|solution|tier|package)\b/i;
|
|
11441
|
+
const entityIndices = [];
|
|
11442
|
+
const entityNames = [];
|
|
11443
|
+
let criteriaColumnIndex = 0;
|
|
11444
|
+
headers.forEach((h, idx) => {
|
|
11445
|
+
if (entityPattern.test(h) || /\b(bidder\s*\d+|vendor\s*[a-z0-9]+|option\s*[a-z0-9]+)\b/i.test(h)) {
|
|
11446
|
+
entityIndices.push(idx);
|
|
11447
|
+
const parts = h.split(" > ");
|
|
11448
|
+
entityNames.push(parts[parts.length - 1]);
|
|
11449
|
+
} else if (/\b(criteria|criterion|description|specification|category|requirement|item|feature)\b/i.test(h)) {
|
|
11450
|
+
criteriaColumnIndex = idx;
|
|
11451
|
+
}
|
|
11452
|
+
});
|
|
11453
|
+
const isComparisonMatrix = entityIndices.length >= 2 || headers.length >= 3 && entityIndices.length >= 1;
|
|
11454
|
+
return {
|
|
11455
|
+
isComparisonMatrix,
|
|
11456
|
+
entityIndices: entityIndices.length > 0 ? entityIndices : headers.slice(1).map((_, i) => i + 1),
|
|
11457
|
+
entityNames: entityNames.length > 0 ? entityNames : headers.slice(1),
|
|
11458
|
+
criteriaColumnIndex
|
|
11459
|
+
};
|
|
11460
|
+
}
|
|
11461
|
+
/**
|
|
11462
|
+
* Perspective A: Clean Markdown Table
|
|
11463
|
+
*/
|
|
11464
|
+
static generateMarkdownTable(headers, dataRows) {
|
|
11465
|
+
if (headers.length === 0 || dataRows.length === 0) return "";
|
|
11466
|
+
const cleanHeaders = headers.map((h) => h.replace(/\|/g, "-"));
|
|
11467
|
+
const lines = [];
|
|
11468
|
+
lines.push(`| ${cleanHeaders.join(" | ")} |`);
|
|
11469
|
+
lines.push(`| ${cleanHeaders.map(() => "---").join(" | ")} |`);
|
|
11470
|
+
for (const row of dataRows) {
|
|
11471
|
+
const paddedRow = headers.map((_, i) => (row[i] || "").replace(/\|/g, "-"));
|
|
11472
|
+
if (paddedRow.some((cell) => cell.trim().length > 0)) {
|
|
11473
|
+
lines.push(`| ${paddedRow.join(" | ")} |`);
|
|
11474
|
+
}
|
|
11475
|
+
}
|
|
11476
|
+
return lines.join("\n");
|
|
11477
|
+
}
|
|
11478
|
+
/**
|
|
11479
|
+
* Perspective B: Entity-Centric Profiles (e.g. Bidder 1, Bidder 2 full summary)
|
|
11480
|
+
*/
|
|
11481
|
+
static generateEntityProfiles(headers, dataRows, matrixInfo) {
|
|
11482
|
+
const profiles = [];
|
|
11483
|
+
matrixInfo.entityIndices.forEach((colIdx, i) => {
|
|
11484
|
+
const entityName = matrixInfo.entityNames[i] || headers[colIdx] || `Entity ${i + 1}`;
|
|
11485
|
+
const lines = [`### Profile & Evaluation: ${entityName}`];
|
|
11486
|
+
for (const row of dataRows) {
|
|
11487
|
+
const criterion = row[matrixInfo.criteriaColumnIndex] || row[0] || "Specification";
|
|
11488
|
+
const val = row[colIdx];
|
|
11489
|
+
if (val !== void 0 && val !== null && String(val).trim() !== "") {
|
|
11490
|
+
lines.push(`- **${criterion}**: ${String(val).trim()}`);
|
|
11491
|
+
}
|
|
11492
|
+
}
|
|
11493
|
+
if (lines.length > 1) {
|
|
11494
|
+
profiles.push(lines.join("\n"));
|
|
11495
|
+
}
|
|
11496
|
+
});
|
|
11497
|
+
return profiles.join("\n\n");
|
|
11498
|
+
}
|
|
11499
|
+
/**
|
|
11500
|
+
* Perspective C: Criteria Cross-Comparisons (e.g. Commercial Bid comparison across all Bidders)
|
|
11501
|
+
*/
|
|
11502
|
+
static generateCriteriaComparisons(headers, dataRows, matrixInfo) {
|
|
11503
|
+
const comparisons = [];
|
|
11504
|
+
for (const row of dataRows) {
|
|
11505
|
+
const criterion = row[matrixInfo.criteriaColumnIndex] || row[0] || "Metric";
|
|
11506
|
+
const entityPairs = [];
|
|
11507
|
+
matrixInfo.entityIndices.forEach((colIdx, i) => {
|
|
11508
|
+
const entityName = matrixInfo.entityNames[i] || headers[colIdx] || `Entity ${i + 1}`;
|
|
11509
|
+
const val = row[colIdx];
|
|
11510
|
+
if (val !== void 0 && val !== null && String(val).trim() !== "") {
|
|
11511
|
+
entityPairs.push(`${entityName}: ${String(val).trim()}`);
|
|
11512
|
+
}
|
|
11513
|
+
});
|
|
11514
|
+
if (entityPairs.length > 0) {
|
|
11515
|
+
comparisons.push(`- **${criterion}** $\\rightarrow$ ${entityPairs.join(" | ")}`);
|
|
11516
|
+
}
|
|
11517
|
+
}
|
|
11518
|
+
return comparisons.join("\n");
|
|
11519
|
+
}
|
|
11520
|
+
/**
|
|
11521
|
+
* Standard contextual key-value rows for general tabular records.
|
|
11522
|
+
*/
|
|
11523
|
+
static generateContextualRows(headers, dataRows) {
|
|
11524
|
+
const lines = [];
|
|
11525
|
+
for (const row of dataRows) {
|
|
11526
|
+
const pairs = [];
|
|
11527
|
+
headers.forEach((h, i) => {
|
|
11528
|
+
const val = row[i];
|
|
11529
|
+
if (val !== void 0 && val !== null && String(val).trim() !== "") {
|
|
11530
|
+
pairs.push(`${h}: ${String(val).trim()}`);
|
|
11531
|
+
}
|
|
11532
|
+
});
|
|
11533
|
+
if (pairs.length > 0) {
|
|
11534
|
+
lines.push(pairs.join(" | "));
|
|
11535
|
+
}
|
|
11536
|
+
}
|
|
11537
|
+
return lines.join("\n");
|
|
11538
|
+
}
|
|
11539
|
+
};
|
|
11540
|
+
|
|
11541
|
+
// src/utils/VisionExtractor.ts
|
|
11542
|
+
var VisionExtractor = class {
|
|
11543
|
+
/**
|
|
11544
|
+
* Check if a buffer represents an image format.
|
|
11545
|
+
*/
|
|
11546
|
+
static isImageBuffer(buffer, fileName = "", mimeType = "") {
|
|
11547
|
+
var _a2;
|
|
11548
|
+
const ext = ((_a2 = fileName.split(".").pop()) == null ? void 0 : _a2.toLowerCase()) || "";
|
|
11549
|
+
if (["png", "jpg", "jpeg", "webp", "bmp", "tiff", "gif"].includes(ext)) {
|
|
11550
|
+
return true;
|
|
11551
|
+
}
|
|
11552
|
+
if (mimeType.startsWith("image/")) {
|
|
11553
|
+
return true;
|
|
11554
|
+
}
|
|
11555
|
+
if (buffer && buffer.length >= 4) {
|
|
11556
|
+
if (buffer[0] === 137 && buffer[1] === 80 && buffer[2] === 78 && buffer[3] === 71) return true;
|
|
11557
|
+
if (buffer[0] === 255 && buffer[1] === 216 && buffer[2] === 255) return true;
|
|
11558
|
+
if (buffer[0] === 82 && buffer[1] === 73 && buffer[2] === 70 && buffer[3] === 70) return true;
|
|
11559
|
+
}
|
|
11560
|
+
return false;
|
|
11561
|
+
}
|
|
11562
|
+
/**
|
|
11563
|
+
* Extract rich textual content, tables, and diagram descriptions from an image buffer using Vision LLM.
|
|
11564
|
+
*/
|
|
11565
|
+
static async extractFromImage(buffer, fileName = "document.png", options = {}) {
|
|
11566
|
+
var _a2, _b, _c, _d, _e, _f, _g2, _h, _i;
|
|
11567
|
+
const base64Image = buffer.toString("base64");
|
|
11568
|
+
const ext = ((_a2 = fileName.split(".").pop()) == null ? void 0 : _a2.toLowerCase()) || "png";
|
|
11569
|
+
const mime = ext === "jpg" || ext === "jpeg" ? "image/jpeg" : ext === "webp" ? "image/webp" : "image/png";
|
|
11570
|
+
const apiKey = options.apiKey || (typeof process !== "undefined" ? ((_b = process.env) == null ? void 0 : _b.OPENAI_API_KEY) || ((_c = process.env) == null ? void 0 : _c.GEMINI_API_KEY) || ((_d = process.env) == null ? void 0 : _d.ANTHROPIC_API_KEY) : void 0);
|
|
11571
|
+
if (!apiKey) {
|
|
11572
|
+
return `[Scanned Document / Image: ${fileName} - Size: ${(buffer.length / 1024).toFixed(1)} KB]`;
|
|
11573
|
+
}
|
|
11574
|
+
try {
|
|
11575
|
+
const response = await fetch("https://api.openai.com/v1/chat/completions", {
|
|
11576
|
+
method: "POST",
|
|
11577
|
+
headers: {
|
|
11578
|
+
"Authorization": `Bearer ${apiKey}`,
|
|
11579
|
+
"Content-Type": "application/json"
|
|
11580
|
+
},
|
|
11581
|
+
body: JSON.stringify({
|
|
11582
|
+
model: options.model || "gpt-4o-mini",
|
|
11583
|
+
messages: [
|
|
11584
|
+
{
|
|
11585
|
+
role: "system",
|
|
11586
|
+
content: "You are an expert Document OCR & Data Extraction engine. Transcribe all text, numbers, headings, tables, and diagram structures in this image into clean, structured GitHub Markdown. For tables, use standard markdown table syntax. Preserve exact numbers, dates, and column headers. Do not summarize \u2014 transcribe faithfully."
|
|
11587
|
+
},
|
|
11588
|
+
{
|
|
11589
|
+
role: "user",
|
|
11590
|
+
content: [
|
|
11591
|
+
{
|
|
11592
|
+
type: "text",
|
|
11593
|
+
text: `Please transcribe the content of this document/image (${fileName}):`
|
|
11594
|
+
},
|
|
11595
|
+
{
|
|
11596
|
+
type: "image_url",
|
|
11597
|
+
image_url: {
|
|
11598
|
+
url: `data:${mime};base64,${base64Image}`,
|
|
11599
|
+
detail: "high"
|
|
11600
|
+
}
|
|
11601
|
+
}
|
|
11602
|
+
]
|
|
11603
|
+
}
|
|
11604
|
+
],
|
|
11605
|
+
max_tokens: options.maxTokens || 2048,
|
|
11606
|
+
temperature: (_e = options.temperature) != null ? _e : 0.1
|
|
11607
|
+
})
|
|
11608
|
+
});
|
|
11609
|
+
if (response.ok) {
|
|
11610
|
+
const data = await response.json();
|
|
11611
|
+
const text = (_i = (_h = (_g2 = (_f = data.choices) == null ? void 0 : _f[0]) == null ? void 0 : _g2.message) == null ? void 0 : _h.content) == null ? void 0 : _i.trim();
|
|
11612
|
+
if (text) {
|
|
11613
|
+
return `--- Document / Image OCR: ${fileName} ---
|
|
11614
|
+
|
|
11615
|
+
${text}`;
|
|
11616
|
+
}
|
|
11617
|
+
}
|
|
11618
|
+
} catch (err) {
|
|
11619
|
+
console.warn("[VisionExtractor] OpenAI Vision OCR failed:", err);
|
|
11620
|
+
}
|
|
11621
|
+
return `[Scanned Image Document: ${fileName}]`;
|
|
11622
|
+
}
|
|
11623
|
+
};
|
|
11624
|
+
|
|
10721
11625
|
// src/utils/DocumentParser.ts
|
|
10722
11626
|
var DocumentParser = class {
|
|
10723
11627
|
/**
|
|
@@ -10741,52 +11645,11 @@ var DocumentParser = class {
|
|
|
10741
11645
|
return "text";
|
|
10742
11646
|
}
|
|
10743
11647
|
/**
|
|
10744
|
-
* Convert spreadsheet sheets into high-fidelity, context-preserved tabular text.
|
|
10745
|
-
*
|
|
11648
|
+
* Convert spreadsheet sheets into high-fidelity, context-preserved tabular text via UniversalTableParser.
|
|
11649
|
+
* Unrolls merged cells, fuses multi-level headers, and generates tri-perspective comparison profiles.
|
|
10746
11650
|
*/
|
|
10747
11651
|
static formatSheetToContextualText(sheet, sheetName, XLSX) {
|
|
10748
|
-
|
|
10749
|
-
if (!jsonRows || jsonRows.length === 0) {
|
|
10750
|
-
const csv = XLSX.utils.sheet_to_csv(sheet, { blankrows: false });
|
|
10751
|
-
return csv && csv.trim() ? `--- Sheet: ${sheetName} ---
|
|
10752
|
-
${csv.trim()}` : "";
|
|
10753
|
-
}
|
|
10754
|
-
const lines = [`--- Sheet: ${sheetName} ---`];
|
|
10755
|
-
let currentHeader = [];
|
|
10756
|
-
for (let r = 0; r < jsonRows.length; r++) {
|
|
10757
|
-
const row = jsonRows[r];
|
|
10758
|
-
if (!row || row.length === 0) continue;
|
|
10759
|
-
const cells = row.map((c) => c !== null && c !== void 0 ? String(c).trim() : "").filter(Boolean);
|
|
10760
|
-
if (cells.length === 0) continue;
|
|
10761
|
-
if (cells.length === 1 || cells.length <= 2 && cells[0].toUpperCase().includes("STEP")) {
|
|
10762
|
-
lines.push(`
|
|
10763
|
-
## ${cells.join(" ")}`);
|
|
10764
|
-
continue;
|
|
10765
|
-
}
|
|
10766
|
-
const isHeaderCandidate = cells.every((c) => isNaN(Number(c)) && !/^\$?[\d,.]+/.test(c));
|
|
10767
|
-
if (isHeaderCandidate && cells.length >= 2) {
|
|
10768
|
-
currentHeader = row.map((c) => c !== null && c !== void 0 ? String(c).trim() : "");
|
|
10769
|
-
lines.push(`| ${cells.join(" | ")} |`);
|
|
10770
|
-
lines.push(`| ${cells.map(() => "---").join(" | ")} |`);
|
|
10771
|
-
continue;
|
|
10772
|
-
}
|
|
10773
|
-
if (currentHeader.length > 0 && cells.length > 1) {
|
|
10774
|
-
const fieldPairs = [];
|
|
10775
|
-
for (let c = 0; c < row.length; c++) {
|
|
10776
|
-
const h = currentHeader[c];
|
|
10777
|
-
const val = row[c];
|
|
10778
|
-
if (h && val !== null && val !== void 0 && String(val).trim() !== "") {
|
|
10779
|
-
fieldPairs.push(`${h}: ${String(val).trim()}`);
|
|
10780
|
-
}
|
|
10781
|
-
}
|
|
10782
|
-
if (fieldPairs.length > 0) {
|
|
10783
|
-
lines.push(fieldPairs.join(" | "));
|
|
10784
|
-
continue;
|
|
10785
|
-
}
|
|
10786
|
-
}
|
|
10787
|
-
lines.push(cells.join(", "));
|
|
10788
|
-
}
|
|
10789
|
-
return lines.join("\n");
|
|
11652
|
+
return UniversalTableParser.parseSheet(sheet, sheetName, XLSX);
|
|
10790
11653
|
}
|
|
10791
11654
|
/**
|
|
10792
11655
|
* Extract text from a File or Buffer based on its type.
|
|
@@ -10810,7 +11673,9 @@ ${csv.trim()}` : "";
|
|
|
10810
11673
|
return this.readAsText(file);
|
|
10811
11674
|
}
|
|
10812
11675
|
if (extension === "csv" || extension === "tsv" || mimeType === "text/csv" || mimeType === "text/tab-separated-values") {
|
|
10813
|
-
|
|
11676
|
+
const text = await this.readAsText(file);
|
|
11677
|
+
const parsed = UniversalTableParser.parseDelimitedText(text, fileName);
|
|
11678
|
+
return parsed || text;
|
|
10814
11679
|
}
|
|
10815
11680
|
if (extension === "pptx" || extension === "ppt" || extension === "odp" || mimeType.includes("presentation") || mimeType.includes("powerpoint")) {
|
|
10816
11681
|
try {
|
|
@@ -10917,16 +11782,22 @@ ${csv.trim()}`);
|
|
|
10917
11782
|
return this.parseLegacyExcelOrFallback(buffer, fileName);
|
|
10918
11783
|
}
|
|
10919
11784
|
if (extension === "pdf" || mimeType === "application/pdf") {
|
|
11785
|
+
const buffer = Buffer.isBuffer(file) ? file : Buffer.from(await file.arrayBuffer());
|
|
10920
11786
|
try {
|
|
10921
11787
|
const pdfModule = await import("pdf-parse");
|
|
10922
|
-
const buffer = Buffer.isBuffer(file) ? file : Buffer.from(await file.arrayBuffer());
|
|
10923
11788
|
const pdf = pdfModule.default || pdfModule;
|
|
10924
11789
|
const data = await pdf(buffer);
|
|
10925
|
-
|
|
11790
|
+
if (data.text && data.text.trim().length > 30) {
|
|
11791
|
+
return data.text;
|
|
11792
|
+
}
|
|
10926
11793
|
} catch (err) {
|
|
10927
|
-
console.warn(
|
|
10928
|
-
return `[PDF Parsing Error for ${fileName}]`;
|
|
11794
|
+
console.warn("[DocumentParser] pdf-parse failed, attempting Vision OCR fallback:", err);
|
|
10929
11795
|
}
|
|
11796
|
+
return VisionExtractor.extractFromImage(buffer, fileName);
|
|
11797
|
+
}
|
|
11798
|
+
if (["png", "jpg", "jpeg", "webp", "bmp", "tiff", "gif"].includes(extension) || mimeType.startsWith("image/")) {
|
|
11799
|
+
const buffer = Buffer.isBuffer(file) ? file : Buffer.from(await file.arrayBuffer());
|
|
11800
|
+
return VisionExtractor.extractFromImage(buffer, fileName);
|
|
10930
11801
|
}
|
|
10931
11802
|
if (extension === "docx" || extension === "doc" || extension === "odt" || extension === "rtf" || mimeType === "application/vnd.openxmlformats-officedocument.wordprocessingml.document" || mimeType === "application/msword" || mimeType === "application/rtf") {
|
|
10932
11803
|
try {
|
|
@@ -11268,44 +12139,38 @@ var _DatabaseStorage = class _DatabaseStorage {
|
|
|
11268
12139
|
*
|
|
11269
12140
|
* In non-production environments this is a complete no-op so local
|
|
11270
12141
|
* development works without a license key.
|
|
12142
|
+
/**
|
|
12143
|
+
* Checks if the caller holds a valid paid-tier license in production.
|
|
12144
|
+
* Returns true if allowed, false if not supported on current tier.
|
|
11271
12145
|
*/
|
|
11272
|
-
async
|
|
11273
|
-
if (process.env.NODE_ENV !== "production") return;
|
|
11274
|
-
if (
|
|
11275
|
-
if (
|
|
11276
|
-
|
|
11277
|
-
"[Retrivora SDK] Chat History and Feedback features require a paid license key in production."
|
|
11278
|
-
);
|
|
12146
|
+
async isPaidSubscription() {
|
|
12147
|
+
if (process.env.NODE_ENV !== "production") return true;
|
|
12148
|
+
if (!this.config.licenseKey) return false;
|
|
12149
|
+
if (Date.now() - this.subscriptionCheckedAt < _DatabaseStorage.SUBSCRIPTION_CACHE_TTL_MS) {
|
|
12150
|
+
return true;
|
|
11279
12151
|
}
|
|
11280
12152
|
try {
|
|
11281
12153
|
const payload = await LicenseVerifier.verifyAsync(this.config.licenseKey, this.config.projectId);
|
|
11282
12154
|
const tier = (payload.tier || "").toLowerCase();
|
|
11283
|
-
const PAID_TIERS = /* @__PURE__ */ new Set(["premium", "enterprise", "growth", "pro", "developer_pro"]);
|
|
11284
|
-
|
|
11285
|
-
|
|
11286
|
-
|
|
11287
|
-
);
|
|
12155
|
+
const PAID_TIERS = /* @__PURE__ */ new Set(["premium", "enterprise", "growth", "pro", "developer_pro", "free_trial"]);
|
|
12156
|
+
const isAllowed = PAID_TIERS.has(tier) || tier.includes("pro") || tier.includes("enterprise");
|
|
12157
|
+
if (isAllowed) {
|
|
12158
|
+
this.subscriptionCheckedAt = Date.now();
|
|
11288
12159
|
}
|
|
11289
|
-
|
|
11290
|
-
} catch (
|
|
11291
|
-
|
|
11292
|
-
`[Retrivora SDK] Subscription check failed: ${err instanceof Error ? err.message : String(err)}`
|
|
11293
|
-
);
|
|
12160
|
+
return isAllowed;
|
|
12161
|
+
} catch (e) {
|
|
12162
|
+
return false;
|
|
11294
12163
|
}
|
|
11295
12164
|
}
|
|
11296
|
-
async
|
|
12165
|
+
async isHistoryAvailable() {
|
|
11297
12166
|
var _a2, _b;
|
|
11298
|
-
|
|
11299
|
-
|
|
11300
|
-
throw new Error("[Retrivora SDK] Chat History is disabled in RagConfig.");
|
|
11301
|
-
}
|
|
12167
|
+
if (((_b = (_a2 = this.config.rag) == null ? void 0 : _a2.history) == null ? void 0 : _b.enabled) === false) return false;
|
|
12168
|
+
return this.isPaidSubscription();
|
|
11302
12169
|
}
|
|
11303
|
-
async
|
|
12170
|
+
async isFeedbackAvailable() {
|
|
11304
12171
|
var _a2, _b;
|
|
11305
|
-
|
|
11306
|
-
|
|
11307
|
-
throw new Error("[Retrivora SDK] User Feedback is disabled in RagConfig.");
|
|
11308
|
-
}
|
|
12172
|
+
if (((_b = (_a2 = this.config.rag) == null ? void 0 : _a2.feedback) == null ? void 0 : _b.enabled) === false) return false;
|
|
12173
|
+
return this.isPaidSubscription();
|
|
11309
12174
|
}
|
|
11310
12175
|
/**
|
|
11311
12176
|
* Returns (or lazily creates) the PostgreSQL connection pool for this project.
|
|
@@ -11419,7 +12284,7 @@ var _DatabaseStorage = class _DatabaseStorage {
|
|
|
11419
12284
|
}
|
|
11420
12285
|
// ─── Message History Operations ──────────────────────────────────────────
|
|
11421
12286
|
async saveMessage(sessionId, message) {
|
|
11422
|
-
await this.
|
|
12287
|
+
if (!await this.isHistoryAvailable()) return;
|
|
11423
12288
|
const createdAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
11424
12289
|
const msgId = message.id || `msg_${Date.now()}_${Math.random().toString(36).slice(2, 6)}`;
|
|
11425
12290
|
if (this.provider === "postgresql" || this.provider === "pgvector") {
|
|
@@ -11482,7 +12347,7 @@ var _DatabaseStorage = class _DatabaseStorage {
|
|
|
11482
12347
|
}
|
|
11483
12348
|
}
|
|
11484
12349
|
async getHistory(sessionId, projectIds) {
|
|
11485
|
-
await this.
|
|
12350
|
+
if (!await this.isHistoryAvailable()) return [];
|
|
11486
12351
|
if (projectIds && projectIds.length > 0 && !this.sessionInScope(sessionId, projectIds)) {
|
|
11487
12352
|
return [];
|
|
11488
12353
|
}
|
|
@@ -11516,7 +12381,7 @@ var _DatabaseStorage = class _DatabaseStorage {
|
|
|
11516
12381
|
}
|
|
11517
12382
|
}
|
|
11518
12383
|
async clearHistory(sessionId, projectIds) {
|
|
11519
|
-
await this.
|
|
12384
|
+
if (!await this.isHistoryAvailable()) return;
|
|
11520
12385
|
if (projectIds && projectIds.length > 0 && !this.sessionInScope(sessionId, projectIds)) {
|
|
11521
12386
|
return;
|
|
11522
12387
|
}
|
|
@@ -11556,7 +12421,7 @@ var _DatabaseStorage = class _DatabaseStorage {
|
|
|
11556
12421
|
});
|
|
11557
12422
|
}
|
|
11558
12423
|
async listSessions(projectIds) {
|
|
11559
|
-
await this.
|
|
12424
|
+
if (!await this.isHistoryAvailable()) return [];
|
|
11560
12425
|
let raw = [];
|
|
11561
12426
|
if (this.provider === "postgresql" || this.provider === "pgvector") {
|
|
11562
12427
|
const pool = await this.getPGPool();
|
|
@@ -11575,7 +12440,7 @@ var _DatabaseStorage = class _DatabaseStorage {
|
|
|
11575
12440
|
}
|
|
11576
12441
|
// ─── Feedback Operations ──────────────────────────────────────────────────
|
|
11577
12442
|
async saveFeedback(feedback) {
|
|
11578
|
-
await this.
|
|
12443
|
+
if (!await this.isFeedbackAvailable()) return;
|
|
11579
12444
|
const id = `fb_${Date.now()}_${Math.random().toString(36).slice(2, 6)}`;
|
|
11580
12445
|
const createdAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
11581
12446
|
if (this.provider === "postgresql" || this.provider === "pgvector") {
|
|
@@ -11625,7 +12490,7 @@ var _DatabaseStorage = class _DatabaseStorage {
|
|
|
11625
12490
|
}
|
|
11626
12491
|
}
|
|
11627
12492
|
async getFeedback(messageId, projectIds) {
|
|
11628
|
-
await this.
|
|
12493
|
+
if (!await this.isFeedbackAvailable()) return null;
|
|
11629
12494
|
let item = null;
|
|
11630
12495
|
if (this.provider === "postgresql" || this.provider === "pgvector") {
|
|
11631
12496
|
const pool = await this.getPGPool();
|
|
@@ -11661,7 +12526,7 @@ var _DatabaseStorage = class _DatabaseStorage {
|
|
|
11661
12526
|
return item;
|
|
11662
12527
|
}
|
|
11663
12528
|
async listFeedback(projectIds) {
|
|
11664
|
-
await this.
|
|
12529
|
+
if (!await this.isFeedbackAvailable()) return [];
|
|
11665
12530
|
let raw = [];
|
|
11666
12531
|
if (this.provider === "postgresql" || this.provider === "pgvector") {
|
|
11667
12532
|
const pool = await this.getPGPool();
|