@retrivora-ai/rag-engine 2.5.9 → 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 +967 -102
- package/dist/handlers/index.mjs +967 -102
- 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 +1 -1
- package/dist/index.mjs +1 -1
- package/dist/server.d.mts +271 -9
- package/dist/server.d.ts +271 -9
- package/dist/server.js +981 -102
- package/dist/server.mjs +974 -102
- package/package.json +1 -1
package/dist/handlers/index.js
CHANGED
|
@@ -318,7 +318,7 @@ var init_package = __esm({
|
|
|
318
318
|
"package.json"() {
|
|
319
319
|
package_default = {
|
|
320
320
|
name: "@retrivora-ai/rag-engine",
|
|
321
|
-
version: "2.
|
|
321
|
+
version: "2.6.0",
|
|
322
322
|
description: "Retrivora AI is a plug-and-play AI engine for RAG chat experiences \u2014 generic vector DB + LLM provider, embeddable or standalone.",
|
|
323
323
|
author: "Abhinav Alkuchi",
|
|
324
324
|
license: "UNLICENSED",
|
|
@@ -5603,27 +5603,384 @@ Text to extract from:
|
|
|
5603
5603
|
}
|
|
5604
5604
|
};
|
|
5605
5605
|
|
|
5606
|
+
// src/rag/HybridSearch.ts
|
|
5607
|
+
var HybridSearch = class {
|
|
5608
|
+
/**
|
|
5609
|
+
* Tokenize text into normalized alphanumeric keywords, keeping exact codes, numbers, and identifiers.
|
|
5610
|
+
*/
|
|
5611
|
+
static tokenize(text) {
|
|
5612
|
+
if (!text) return [];
|
|
5613
|
+
return text.toLowerCase().replace(/[^\w\s\-_#./:]/g, " ").split(/\s+/).map((t) => t.trim()).filter((t) => t.length > 1 && !this.stopWords.has(t));
|
|
5614
|
+
}
|
|
5615
|
+
/**
|
|
5616
|
+
* Calculate Okapi BM25 sparse scores for an array of document chunks given a query.
|
|
5617
|
+
*/
|
|
5618
|
+
static scoreBM25(documents, query, k1 = 1.5, b = 0.75) {
|
|
5619
|
+
const scores = /* @__PURE__ */ new Map();
|
|
5620
|
+
const queryTokens = this.tokenize(query);
|
|
5621
|
+
if (queryTokens.length === 0 || documents.length === 0) {
|
|
5622
|
+
documents.forEach((d) => scores.set(d.id, 0));
|
|
5623
|
+
return scores;
|
|
5624
|
+
}
|
|
5625
|
+
const docTokens = documents.map((d) => ({
|
|
5626
|
+
id: d.id,
|
|
5627
|
+
tokens: this.tokenize(d.content)
|
|
5628
|
+
}));
|
|
5629
|
+
const N = documents.length;
|
|
5630
|
+
const avgdl = docTokens.reduce((acc, d) => acc + d.tokens.length, 0) / (N || 1);
|
|
5631
|
+
const dfMap = /* @__PURE__ */ new Map();
|
|
5632
|
+
for (const qt of queryTokens) {
|
|
5633
|
+
let count = 0;
|
|
5634
|
+
for (const dt of docTokens) {
|
|
5635
|
+
if (dt.tokens.includes(qt)) count++;
|
|
5636
|
+
}
|
|
5637
|
+
dfMap.set(qt, count);
|
|
5638
|
+
}
|
|
5639
|
+
for (const dt of docTokens) {
|
|
5640
|
+
let score = 0;
|
|
5641
|
+
const docLen = dt.tokens.length;
|
|
5642
|
+
const tfMap = /* @__PURE__ */ new Map();
|
|
5643
|
+
for (const t of dt.tokens) {
|
|
5644
|
+
tfMap.set(t, (tfMap.get(t) || 0) + 1);
|
|
5645
|
+
}
|
|
5646
|
+
for (const qt of queryTokens) {
|
|
5647
|
+
const tf = tfMap.get(qt) || 0;
|
|
5648
|
+
if (tf === 0) continue;
|
|
5649
|
+
const df = dfMap.get(qt) || 0;
|
|
5650
|
+
const idf = Math.log((N - df + 0.5) / (df + 0.5) + 1);
|
|
5651
|
+
const numerator = tf * (k1 + 1);
|
|
5652
|
+
const denominator = tf + k1 * (1 - b + b * (docLen / (avgdl || 1)));
|
|
5653
|
+
score += idf * (numerator / (denominator || 1));
|
|
5654
|
+
}
|
|
5655
|
+
scores.set(dt.id, score);
|
|
5656
|
+
}
|
|
5657
|
+
return scores;
|
|
5658
|
+
}
|
|
5659
|
+
/**
|
|
5660
|
+
* Fuse Dense Vector Matches with Sparse BM25 Scores using Reciprocal Rank Fusion (RRF).
|
|
5661
|
+
*/
|
|
5662
|
+
static fuse(denseMatches, query, options = {}) {
|
|
5663
|
+
var _a2, _b, _c;
|
|
5664
|
+
if (denseMatches.length === 0) return [];
|
|
5665
|
+
const denseWeight = (_a2 = options.denseWeight) != null ? _a2 : 0.6;
|
|
5666
|
+
const sparseWeight = (_b = options.sparseWeight) != null ? _b : 0.4;
|
|
5667
|
+
const k = (_c = options.rrfK) != null ? _c : 60;
|
|
5668
|
+
const sortedDense = [...denseMatches].sort((a, b) => b.score - a.score);
|
|
5669
|
+
const denseRankMap = /* @__PURE__ */ new Map();
|
|
5670
|
+
sortedDense.forEach((match, idx) => {
|
|
5671
|
+
denseRankMap.set(match.id, idx + 1);
|
|
5672
|
+
});
|
|
5673
|
+
const docItems = denseMatches.map((m) => ({ id: m.id, content: m.content }));
|
|
5674
|
+
const bm25ScoreMap = this.scoreBM25(docItems, query, options.bm25K1, options.bm25B);
|
|
5675
|
+
const sortedSparse = [...denseMatches].sort((a, b) => {
|
|
5676
|
+
const scoreA = bm25ScoreMap.get(a.id) || 0;
|
|
5677
|
+
const scoreB = bm25ScoreMap.get(b.id) || 0;
|
|
5678
|
+
return scoreB - scoreA;
|
|
5679
|
+
});
|
|
5680
|
+
const sparseRankMap = /* @__PURE__ */ new Map();
|
|
5681
|
+
sortedSparse.forEach((match, idx) => {
|
|
5682
|
+
sparseRankMap.set(match.id, idx + 1);
|
|
5683
|
+
});
|
|
5684
|
+
const fusedMatches = denseMatches.map((match) => {
|
|
5685
|
+
var _a3;
|
|
5686
|
+
const denseRank = denseRankMap.get(match.id) || denseMatches.length;
|
|
5687
|
+
const sparseRank = sparseRankMap.get(match.id) || denseMatches.length;
|
|
5688
|
+
const denseRrf = denseWeight / (k + denseRank);
|
|
5689
|
+
const sparseRrf = sparseWeight / (k + sparseRank);
|
|
5690
|
+
const combinedRrf = denseRrf + sparseRrf;
|
|
5691
|
+
const bm25Raw = bm25ScoreMap.get(match.id) || 0;
|
|
5692
|
+
return __spreadProps(__spreadValues({}, match), {
|
|
5693
|
+
rrfScore: combinedRrf,
|
|
5694
|
+
bm25Score: bm25Raw,
|
|
5695
|
+
rerankScore: ((_a3 = match.rerankScore) != null ? _a3 : match.score) * 0.5 + combinedRrf * 50
|
|
5696
|
+
});
|
|
5697
|
+
});
|
|
5698
|
+
return fusedMatches.sort((a, b) => {
|
|
5699
|
+
var _a3, _b2;
|
|
5700
|
+
return ((_a3 = b.rerankScore) != null ? _a3 : b.score) - ((_b2 = a.rerankScore) != null ? _b2 : a.score);
|
|
5701
|
+
});
|
|
5702
|
+
}
|
|
5703
|
+
};
|
|
5704
|
+
HybridSearch.stopWords = /* @__PURE__ */ new Set([
|
|
5705
|
+
"a",
|
|
5706
|
+
"about",
|
|
5707
|
+
"above",
|
|
5708
|
+
"after",
|
|
5709
|
+
"again",
|
|
5710
|
+
"against",
|
|
5711
|
+
"all",
|
|
5712
|
+
"am",
|
|
5713
|
+
"an",
|
|
5714
|
+
"and",
|
|
5715
|
+
"any",
|
|
5716
|
+
"are",
|
|
5717
|
+
"aren't",
|
|
5718
|
+
"as",
|
|
5719
|
+
"at",
|
|
5720
|
+
"be",
|
|
5721
|
+
"because",
|
|
5722
|
+
"been",
|
|
5723
|
+
"before",
|
|
5724
|
+
"being",
|
|
5725
|
+
"below",
|
|
5726
|
+
"between",
|
|
5727
|
+
"both",
|
|
5728
|
+
"but",
|
|
5729
|
+
"by",
|
|
5730
|
+
"can't",
|
|
5731
|
+
"cannot",
|
|
5732
|
+
"could",
|
|
5733
|
+
"couldn't",
|
|
5734
|
+
"did",
|
|
5735
|
+
"didn't",
|
|
5736
|
+
"do",
|
|
5737
|
+
"does",
|
|
5738
|
+
"doesn't",
|
|
5739
|
+
"doing",
|
|
5740
|
+
"don't",
|
|
5741
|
+
"down",
|
|
5742
|
+
"during",
|
|
5743
|
+
"each",
|
|
5744
|
+
"few",
|
|
5745
|
+
"for",
|
|
5746
|
+
"from",
|
|
5747
|
+
"further",
|
|
5748
|
+
"had",
|
|
5749
|
+
"hadn't",
|
|
5750
|
+
"has",
|
|
5751
|
+
"hasn't",
|
|
5752
|
+
"have",
|
|
5753
|
+
"haven't",
|
|
5754
|
+
"having",
|
|
5755
|
+
"he",
|
|
5756
|
+
"he'd",
|
|
5757
|
+
"he'll",
|
|
5758
|
+
"he's",
|
|
5759
|
+
"her",
|
|
5760
|
+
"here",
|
|
5761
|
+
"here's",
|
|
5762
|
+
"hers",
|
|
5763
|
+
"herself",
|
|
5764
|
+
"him",
|
|
5765
|
+
"himself",
|
|
5766
|
+
"his",
|
|
5767
|
+
"how",
|
|
5768
|
+
"how's",
|
|
5769
|
+
"i",
|
|
5770
|
+
"i'd",
|
|
5771
|
+
"i'll",
|
|
5772
|
+
"i'm",
|
|
5773
|
+
"i've",
|
|
5774
|
+
"if",
|
|
5775
|
+
"in",
|
|
5776
|
+
"into",
|
|
5777
|
+
"is",
|
|
5778
|
+
"isn't",
|
|
5779
|
+
"it",
|
|
5780
|
+
"it's",
|
|
5781
|
+
"its",
|
|
5782
|
+
"itself",
|
|
5783
|
+
"let's",
|
|
5784
|
+
"me",
|
|
5785
|
+
"more",
|
|
5786
|
+
"most",
|
|
5787
|
+
"mustn't",
|
|
5788
|
+
"my",
|
|
5789
|
+
"myself",
|
|
5790
|
+
"no",
|
|
5791
|
+
"nor",
|
|
5792
|
+
"not",
|
|
5793
|
+
"of",
|
|
5794
|
+
"off",
|
|
5795
|
+
"on",
|
|
5796
|
+
"once",
|
|
5797
|
+
"only",
|
|
5798
|
+
"or",
|
|
5799
|
+
"other",
|
|
5800
|
+
"ought",
|
|
5801
|
+
"our",
|
|
5802
|
+
"ours",
|
|
5803
|
+
"ourselves",
|
|
5804
|
+
"out",
|
|
5805
|
+
"over",
|
|
5806
|
+
"own",
|
|
5807
|
+
"same",
|
|
5808
|
+
"shan't",
|
|
5809
|
+
"she",
|
|
5810
|
+
"she'd",
|
|
5811
|
+
"she'll",
|
|
5812
|
+
"she's",
|
|
5813
|
+
"should",
|
|
5814
|
+
"shouldn't",
|
|
5815
|
+
"so",
|
|
5816
|
+
"some",
|
|
5817
|
+
"such",
|
|
5818
|
+
"than",
|
|
5819
|
+
"that",
|
|
5820
|
+
"that's",
|
|
5821
|
+
"the",
|
|
5822
|
+
"their",
|
|
5823
|
+
"theirs",
|
|
5824
|
+
"them",
|
|
5825
|
+
"themselves",
|
|
5826
|
+
"then",
|
|
5827
|
+
"there",
|
|
5828
|
+
"there's",
|
|
5829
|
+
"these",
|
|
5830
|
+
"they",
|
|
5831
|
+
"they'd",
|
|
5832
|
+
"they'll",
|
|
5833
|
+
"they're",
|
|
5834
|
+
"they've",
|
|
5835
|
+
"this",
|
|
5836
|
+
"those",
|
|
5837
|
+
"through",
|
|
5838
|
+
"to",
|
|
5839
|
+
"too",
|
|
5840
|
+
"under",
|
|
5841
|
+
"until",
|
|
5842
|
+
"up",
|
|
5843
|
+
"very",
|
|
5844
|
+
"was",
|
|
5845
|
+
"wasn't",
|
|
5846
|
+
"we",
|
|
5847
|
+
"we'd",
|
|
5848
|
+
"we'll",
|
|
5849
|
+
"we're",
|
|
5850
|
+
"we've",
|
|
5851
|
+
"were",
|
|
5852
|
+
"weren't",
|
|
5853
|
+
"what",
|
|
5854
|
+
"what's",
|
|
5855
|
+
"when",
|
|
5856
|
+
"when's",
|
|
5857
|
+
"where",
|
|
5858
|
+
"where's",
|
|
5859
|
+
"which",
|
|
5860
|
+
"while",
|
|
5861
|
+
"who",
|
|
5862
|
+
"who's",
|
|
5863
|
+
"whom",
|
|
5864
|
+
"why",
|
|
5865
|
+
"why's",
|
|
5866
|
+
"with",
|
|
5867
|
+
"won't",
|
|
5868
|
+
"would",
|
|
5869
|
+
"wouldn't",
|
|
5870
|
+
"you",
|
|
5871
|
+
"you'd",
|
|
5872
|
+
"you'll",
|
|
5873
|
+
"you're",
|
|
5874
|
+
"you've",
|
|
5875
|
+
"your",
|
|
5876
|
+
"yours",
|
|
5877
|
+
"yourself",
|
|
5878
|
+
"yourselves"
|
|
5879
|
+
]);
|
|
5880
|
+
|
|
5606
5881
|
// src/rag/Reranker.ts
|
|
5607
5882
|
var MAX_RERANK_CONTENT_CHARS = 64e3;
|
|
5608
5883
|
var Reranker = class {
|
|
5609
5884
|
/**
|
|
5610
|
-
* Re-ranks matches based on a secondary relevance score using
|
|
5611
|
-
*
|
|
5612
|
-
* Fix RAG-2: keyword re-ranking no longer mutates the original `score`
|
|
5613
|
-
* field (which consumers interpret as the raw vector similarity). A new
|
|
5614
|
-
* `rerankScore` field is added so both the original similarity and the
|
|
5615
|
-
* boosted rank signal are available to callers.
|
|
5885
|
+
* Re-ranks matches based on a secondary relevance score using Hybrid BM25, Cohere, Jina or LLM.
|
|
5616
5886
|
*/
|
|
5617
|
-
async rerank(matches, query, limit = 5, llm) {
|
|
5618
|
-
|
|
5619
|
-
|
|
5887
|
+
async rerank(matches, query, limit = 5, llm, options = {}) {
|
|
5888
|
+
var _a2, _b;
|
|
5889
|
+
if (!matches || matches.length <= 1) {
|
|
5890
|
+
return matches ? matches.slice(0, limit) : [];
|
|
5620
5891
|
}
|
|
5621
|
-
|
|
5622
|
-
|
|
5623
|
-
|
|
5624
|
-
|
|
5625
|
-
|
|
5892
|
+
const cohereKey = options.cohereApiKey || (typeof process !== "undefined" ? (_a2 = process.env) == null ? void 0 : _a2.COHERE_API_KEY : void 0);
|
|
5893
|
+
const jinaKey = options.jinaApiKey || (typeof process !== "undefined" ? (_b = process.env) == null ? void 0 : _b.JINA_API_KEY : void 0);
|
|
5894
|
+
if ((options.provider === "cohere" || cohereKey) && cohereKey) {
|
|
5895
|
+
try {
|
|
5896
|
+
const cohereResult = await this.cohereRerank(matches, query, limit, cohereKey, options.cohereModel);
|
|
5897
|
+
if (cohereResult.length > 0) return cohereResult;
|
|
5898
|
+
} catch (err) {
|
|
5899
|
+
console.warn("[Reranker] Cohere reranking failed, falling back to HybridSearch:", err);
|
|
5900
|
+
}
|
|
5901
|
+
}
|
|
5902
|
+
if ((options.provider === "jina" || jinaKey) && jinaKey) {
|
|
5903
|
+
try {
|
|
5904
|
+
const jinaResult = await this.jinaRerank(matches, query, limit, jinaKey, options.jinaModel);
|
|
5905
|
+
if (jinaResult.length > 0) return jinaResult;
|
|
5906
|
+
} catch (err) {
|
|
5907
|
+
console.warn("[Reranker] Jina reranking failed, falling back to HybridSearch:", err);
|
|
5908
|
+
}
|
|
5909
|
+
}
|
|
5910
|
+
if (!options.provider || options.provider === "hybrid") {
|
|
5911
|
+
const fused = HybridSearch.fuse(matches, query);
|
|
5912
|
+
if (fused.length > 0) {
|
|
5913
|
+
return fused.slice(0, limit);
|
|
5914
|
+
}
|
|
5915
|
+
}
|
|
5916
|
+
if (llm && (options.provider === "llm" || !options.provider)) {
|
|
5917
|
+
try {
|
|
5918
|
+
return await this.llmRerank(matches, query, limit, llm);
|
|
5919
|
+
} catch (error) {
|
|
5920
|
+
console.warn("[Reranker] LLM re-ranking failed, falling back to keyword sorting:", error);
|
|
5921
|
+
}
|
|
5922
|
+
}
|
|
5923
|
+
return this.keywordRerank(matches, query, limit);
|
|
5924
|
+
}
|
|
5925
|
+
/**
|
|
5926
|
+
* Cohere Rerank v3.5 API integration.
|
|
5927
|
+
*/
|
|
5928
|
+
async cohereRerank(matches, query, limit, apiKey, model = "rerank-v3.5") {
|
|
5929
|
+
const documents = matches.map((m) => m.content || "");
|
|
5930
|
+
const response = await fetch("https://api.cohere.com/v1/rerank", {
|
|
5931
|
+
method: "POST",
|
|
5932
|
+
headers: {
|
|
5933
|
+
"Authorization": `Bearer ${apiKey}`,
|
|
5934
|
+
"Content-Type": "application/json"
|
|
5935
|
+
},
|
|
5936
|
+
body: JSON.stringify({
|
|
5937
|
+
model,
|
|
5938
|
+
query,
|
|
5939
|
+
documents,
|
|
5940
|
+
top_n: limit
|
|
5941
|
+
})
|
|
5942
|
+
});
|
|
5943
|
+
if (!response.ok) {
|
|
5944
|
+
throw new Error(`Cohere API returned status ${response.status}`);
|
|
5945
|
+
}
|
|
5946
|
+
const data = await response.json();
|
|
5947
|
+
if (!data.results || !Array.isArray(data.results)) return [];
|
|
5948
|
+
return data.results.map((res) => {
|
|
5949
|
+
const match = matches[res.index];
|
|
5950
|
+
return __spreadProps(__spreadValues({}, match), {
|
|
5951
|
+
rerankScore: res.relevance_score
|
|
5952
|
+
});
|
|
5953
|
+
});
|
|
5954
|
+
}
|
|
5955
|
+
/**
|
|
5956
|
+
* Jina AI Reranker v2 API integration.
|
|
5957
|
+
*/
|
|
5958
|
+
async jinaRerank(matches, query, limit, apiKey, model = "jina-reranker-v2-base-multilingual") {
|
|
5959
|
+
const documents = matches.map((m) => m.content || "");
|
|
5960
|
+
const response = await fetch("https://api.jina.ai/v1/rerank", {
|
|
5961
|
+
method: "POST",
|
|
5962
|
+
headers: {
|
|
5963
|
+
"Authorization": `Bearer ${apiKey}`,
|
|
5964
|
+
"Content-Type": "application/json"
|
|
5965
|
+
},
|
|
5966
|
+
body: JSON.stringify({
|
|
5967
|
+
model,
|
|
5968
|
+
query,
|
|
5969
|
+
documents,
|
|
5970
|
+
top_n: limit
|
|
5971
|
+
})
|
|
5972
|
+
});
|
|
5973
|
+
if (!response.ok) {
|
|
5974
|
+
throw new Error(`Jina API returned status ${response.status}`);
|
|
5626
5975
|
}
|
|
5976
|
+
const data = await response.json();
|
|
5977
|
+
if (!data.results || !Array.isArray(data.results)) return [];
|
|
5978
|
+
return data.results.map((res) => {
|
|
5979
|
+
const match = matches[res.index];
|
|
5980
|
+
return __spreadProps(__spreadValues({}, match), {
|
|
5981
|
+
rerankScore: res.relevance_score
|
|
5982
|
+
});
|
|
5983
|
+
});
|
|
5627
5984
|
}
|
|
5628
5985
|
/**
|
|
5629
5986
|
* Keyword-based re-ranking fallback.
|
|
@@ -5709,6 +6066,176 @@ ${docList}` }],
|
|
|
5709
6066
|
}
|
|
5710
6067
|
};
|
|
5711
6068
|
|
|
6069
|
+
// src/rag/RAGEvaluator.ts
|
|
6070
|
+
var RAGEvaluator = class {
|
|
6071
|
+
/**
|
|
6072
|
+
* Evaluate a RAG interaction across the full RAG Triad.
|
|
6073
|
+
*/
|
|
6074
|
+
static async evaluate(query, context, answer, llm) {
|
|
6075
|
+
if (!llm || !context || !answer) {
|
|
6076
|
+
return this.deterministicHeuristic(query, context, answer);
|
|
6077
|
+
}
|
|
6078
|
+
try {
|
|
6079
|
+
const prompt = `You are an automated RAG Triad Evaluator. Evaluate the quality of the following RAG system interaction:
|
|
6080
|
+
|
|
6081
|
+
USER QUERY:
|
|
6082
|
+
${query}
|
|
6083
|
+
|
|
6084
|
+
RETRIEVED CONTEXT:
|
|
6085
|
+
${context.slice(0, 3e3)}
|
|
6086
|
+
|
|
6087
|
+
GENERATED ANSWER:
|
|
6088
|
+
${answer}
|
|
6089
|
+
|
|
6090
|
+
Evaluate these 3 metrics on a scale of 0.0 to 1.0:
|
|
6091
|
+
1. "contextRelevance": Are the retrieved documents relevant to the query? (1.0 = highly relevant, 0.0 = irrelevant)
|
|
6092
|
+
2. "faithfulness": Is every claim in the generated answer supported by the retrieved context? (1.0 = 100% grounded/no hallucination, 0.0 = completely fabricated)
|
|
6093
|
+
3. "answerRelevance": Does the answer directly address the user's question? (1.0 = direct answer, 0.0 = evasive/unrelated)
|
|
6094
|
+
|
|
6095
|
+
Return ONLY valid JSON with no markdown fences:
|
|
6096
|
+
{"contextRelevance": 0.95, "faithfulness": 0.90, "answerRelevance": 0.92, "reason": "Answer is fully supported by the document."}`;
|
|
6097
|
+
const raw = await llm.chat(
|
|
6098
|
+
[{ role: "user", content: prompt }],
|
|
6099
|
+
"",
|
|
6100
|
+
{
|
|
6101
|
+
systemPrompt: "You are a strict AI safety and hallucination evaluator. Output only valid JSON.",
|
|
6102
|
+
temperature: 0,
|
|
6103
|
+
maxTokens: 150
|
|
6104
|
+
}
|
|
6105
|
+
);
|
|
6106
|
+
const cleaned = raw.replace(/```json|```/g, "").trim();
|
|
6107
|
+
const parsed = JSON.parse(cleaned);
|
|
6108
|
+
const cr = typeof parsed.contextRelevance === "number" ? Math.max(0, Math.min(1, parsed.contextRelevance)) : 0.85;
|
|
6109
|
+
const f = typeof parsed.faithfulness === "number" ? Math.max(0, Math.min(1, parsed.faithfulness)) : 0.9;
|
|
6110
|
+
const ar = typeof parsed.answerRelevance === "number" ? Math.max(0, Math.min(1, parsed.answerRelevance)) : 0.88;
|
|
6111
|
+
const overall = Number((cr * 0.3 + f * 0.4 + ar * 0.3).toFixed(2));
|
|
6112
|
+
return {
|
|
6113
|
+
contextRelevance: cr,
|
|
6114
|
+
faithfulness: f,
|
|
6115
|
+
answerRelevance: ar,
|
|
6116
|
+
overallQuality: overall,
|
|
6117
|
+
isHallucinated: f < 0.6,
|
|
6118
|
+
reason: parsed.reason || "Evaluated successfully."
|
|
6119
|
+
};
|
|
6120
|
+
} catch (err) {
|
|
6121
|
+
return this.deterministicHeuristic(query, context, answer);
|
|
6122
|
+
}
|
|
6123
|
+
}
|
|
6124
|
+
/**
|
|
6125
|
+
* Deterministic fast keyword-overlap heuristic when LLM evaluation is not invoked.
|
|
6126
|
+
*/
|
|
6127
|
+
static deterministicHeuristic(query, context, answer) {
|
|
6128
|
+
if (!context || !answer) {
|
|
6129
|
+
return { contextRelevance: 0.5, faithfulness: 0.8, answerRelevance: 0.7, overallQuality: 0.67, isHallucinated: false };
|
|
6130
|
+
}
|
|
6131
|
+
const queryWords = query.toLowerCase().split(/\s+/).filter((w) => w.length > 2);
|
|
6132
|
+
const contextLower = context.toLowerCase();
|
|
6133
|
+
const answerLower = answer.toLowerCase();
|
|
6134
|
+
let queryContextHits = 0;
|
|
6135
|
+
queryWords.forEach((w) => {
|
|
6136
|
+
if (contextLower.includes(w)) queryContextHits++;
|
|
6137
|
+
});
|
|
6138
|
+
const contextRelevance = queryWords.length > 0 ? Math.min(1, queryContextHits / queryWords.length + 0.3) : 0.8;
|
|
6139
|
+
const numbersInAnswer = answer.match(/\b\d+(?:\.\d+)?%?\b/g) || [];
|
|
6140
|
+
let numberHits = 0;
|
|
6141
|
+
numbersInAnswer.forEach((num) => {
|
|
6142
|
+
if (context.includes(num)) numberHits++;
|
|
6143
|
+
});
|
|
6144
|
+
const faithfulness = numbersInAnswer.length > 0 ? Math.min(1, numberHits / numbersInAnswer.length + 0.2) : 0.9;
|
|
6145
|
+
const answerRelevance = 0.85;
|
|
6146
|
+
const overallQuality = Number((contextRelevance * 0.3 + faithfulness * 0.4 + answerRelevance * 0.3).toFixed(2));
|
|
6147
|
+
return {
|
|
6148
|
+
contextRelevance: Number(contextRelevance.toFixed(2)),
|
|
6149
|
+
faithfulness: Number(faithfulness.toFixed(2)),
|
|
6150
|
+
answerRelevance,
|
|
6151
|
+
overallQuality,
|
|
6152
|
+
isHallucinated: faithfulness < 0.5,
|
|
6153
|
+
reason: "Computed via deterministic grounding heuristic."
|
|
6154
|
+
};
|
|
6155
|
+
}
|
|
6156
|
+
};
|
|
6157
|
+
|
|
6158
|
+
// src/security/PIIMasker.ts
|
|
6159
|
+
var PIIMasker = class {
|
|
6160
|
+
/**
|
|
6161
|
+
* Redact sensitive information from text.
|
|
6162
|
+
*/
|
|
6163
|
+
static mask(text, options = {}) {
|
|
6164
|
+
if (!text || typeof text !== "string") return text;
|
|
6165
|
+
let sanitized = text;
|
|
6166
|
+
if (options.maskApiKeys !== false) {
|
|
6167
|
+
sanitized = sanitized.replace(this.API_KEY_REGEX, "[REDACTED_API_KEY]");
|
|
6168
|
+
}
|
|
6169
|
+
if (options.maskCreditCards !== false) {
|
|
6170
|
+
sanitized = sanitized.replace(this.CREDIT_CARD_REGEX, (match) => {
|
|
6171
|
+
const digits = match.replace(/[- ]/g, "");
|
|
6172
|
+
if (digits.length >= 13 && digits.length <= 19) {
|
|
6173
|
+
return "[REDACTED_CREDIT_CARD]";
|
|
6174
|
+
}
|
|
6175
|
+
return match;
|
|
6176
|
+
});
|
|
6177
|
+
}
|
|
6178
|
+
if (options.maskSSN !== false) {
|
|
6179
|
+
sanitized = sanitized.replace(this.SSN_REGEX, "[REDACTED_SSN]");
|
|
6180
|
+
}
|
|
6181
|
+
if (options.maskEmails !== false) {
|
|
6182
|
+
sanitized = sanitized.replace(this.EMAIL_REGEX, "[REDACTED_EMAIL]");
|
|
6183
|
+
}
|
|
6184
|
+
if (options.maskPhones !== false) {
|
|
6185
|
+
sanitized = sanitized.replace(this.PHONE_REGEX, "[REDACTED_PHONE]");
|
|
6186
|
+
}
|
|
6187
|
+
if (options.maskIPs === true) {
|
|
6188
|
+
sanitized = sanitized.replace(this.IP_REGEX, "[REDACTED_IP]");
|
|
6189
|
+
}
|
|
6190
|
+
return sanitized;
|
|
6191
|
+
}
|
|
6192
|
+
};
|
|
6193
|
+
PIIMasker.SSN_REGEX = /\b(?!000|666|9\d{2})\d{3}[- ]?(?!00)\d{2}[- ]?(?!0000)\d{4}\b/g;
|
|
6194
|
+
PIIMasker.CREDIT_CARD_REGEX = /\b(?:\d{4}[- ]?){3}\d{4}\b|\b\d{15,16}\b/g;
|
|
6195
|
+
PIIMasker.EMAIL_REGEX = /\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b/g;
|
|
6196
|
+
PIIMasker.PHONE_REGEX = /\b(?:\+?\d{1,3}[-.\s]?)?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}\b/g;
|
|
6197
|
+
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;
|
|
6198
|
+
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;
|
|
6199
|
+
|
|
6200
|
+
// src/security/PromptGuard.ts
|
|
6201
|
+
var PromptGuard = class {
|
|
6202
|
+
/**
|
|
6203
|
+
* Scan and evaluate user query or document text for prompt injection vectors.
|
|
6204
|
+
*/
|
|
6205
|
+
static evaluate(text) {
|
|
6206
|
+
if (!text || typeof text !== "string") {
|
|
6207
|
+
return { isSafe: true, threatLevel: "none", detectedPatterns: [], sanitizedText: text };
|
|
6208
|
+
}
|
|
6209
|
+
const detected = [];
|
|
6210
|
+
let maxSeverity = "none";
|
|
6211
|
+
for (const item of this.INJECTION_PATTERNS) {
|
|
6212
|
+
if (item.pattern.test(text)) {
|
|
6213
|
+
detected.push(item.name);
|
|
6214
|
+
if (item.severity === "high") {
|
|
6215
|
+
maxSeverity = "high";
|
|
6216
|
+
} else if (item.severity === "medium" && maxSeverity !== "high") {
|
|
6217
|
+
maxSeverity = "medium";
|
|
6218
|
+
}
|
|
6219
|
+
}
|
|
6220
|
+
}
|
|
6221
|
+
const sanitized = text.replace(/<\/?(?:system|instruction|admin|prompt_override)>/gi, "");
|
|
6222
|
+
return {
|
|
6223
|
+
isSafe: maxSeverity !== "high",
|
|
6224
|
+
threatLevel: maxSeverity,
|
|
6225
|
+
detectedPatterns: detected,
|
|
6226
|
+
sanitizedText: sanitized
|
|
6227
|
+
};
|
|
6228
|
+
}
|
|
6229
|
+
};
|
|
6230
|
+
PromptGuard.INJECTION_PATTERNS = [
|
|
6231
|
+
{ pattern: /ignore\s+(?:all\s+)?(?:previous|prior|above)\s+(?:instructions|prompts|rules)/i, name: "override_instructions", severity: "high" },
|
|
6232
|
+
{ 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" },
|
|
6233
|
+
{ pattern: /you\s+are\s+now\s+(?:in\s+developer\s+mode|unrestricted|DAN|jailbroken|godmode)/i, name: "jailbreak_persona", severity: "high" },
|
|
6234
|
+
{ pattern: /<\/?(?:system|instruction|admin|prompt_override)>/i, name: "tag_injection", severity: "medium" },
|
|
6235
|
+
{ pattern: /base64\s*decode\s*and\s*execute/i, name: "obfuscated_payload", severity: "high" },
|
|
6236
|
+
{ pattern: /do\s+anything\s+now/i, name: "dan_variant", severity: "high" }
|
|
6237
|
+
];
|
|
6238
|
+
|
|
5712
6239
|
// src/rag/LlamaIndexIngestor.ts
|
|
5713
6240
|
var LlamaIndexIngestor = class {
|
|
5714
6241
|
/**
|
|
@@ -9650,7 +10177,7 @@ ${m.content}`).join("\n\n---\n\n");
|
|
|
9650
10177
|
*/
|
|
9651
10178
|
async processEmbeddings(chunks) {
|
|
9652
10179
|
if (chunks.length === 0) return [];
|
|
9653
|
-
const texts = chunks.map((c) => c.content);
|
|
10180
|
+
const texts = chunks.map((c) => PIIMasker.mask(c.content));
|
|
9654
10181
|
if (typeof this.embeddingProvider.batchEmbed === "function") {
|
|
9655
10182
|
try {
|
|
9656
10183
|
const batchSize = 50;
|
|
@@ -9856,10 +10383,12 @@ ${m.content}`).join("\n\n---\n\n");
|
|
|
9856
10383
|
const requestId = `req_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
|
|
9857
10384
|
const requestStart = performance.now();
|
|
9858
10385
|
try {
|
|
9859
|
-
|
|
10386
|
+
const guardCheck = PromptGuard.evaluate(question);
|
|
10387
|
+
let effectiveQuestion = guardCheck.sanitizedText;
|
|
10388
|
+
effectiveQuestion = PIIMasker.mask(effectiveQuestion);
|
|
9860
10389
|
let promptOptResult;
|
|
9861
10390
|
if (((_e = this.config.rag) == null ? void 0 : _e.optimizePrompt) !== false) {
|
|
9862
|
-
promptOptResult = PromptOptimizer.optimize(
|
|
10391
|
+
promptOptResult = PromptOptimizer.optimize(effectiveQuestion, history);
|
|
9863
10392
|
effectiveQuestion = promptOptResult.optimizedPrompt;
|
|
9864
10393
|
}
|
|
9865
10394
|
let searchQuery = effectiveQuestion;
|
|
@@ -10144,6 +10673,7 @@ ${context}`;
|
|
|
10144
10673
|
uiTransformationPromise,
|
|
10145
10674
|
awaitHallucination ? hallucinationScoringPromise : Promise.resolve(void 0)
|
|
10146
10675
|
]));
|
|
10676
|
+
const triadScore = yield new __await(RAGEvaluator.evaluate(effectiveQuestion, context, fullReply));
|
|
10147
10677
|
const buildTrace = (hScore) => __spreadValues({
|
|
10148
10678
|
requestId,
|
|
10149
10679
|
query: question,
|
|
@@ -10162,7 +10692,13 @@ ${context}`;
|
|
|
10162
10692
|
}),
|
|
10163
10693
|
latency,
|
|
10164
10694
|
tokens,
|
|
10165
|
-
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
10695
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
10696
|
+
ragTriad: {
|
|
10697
|
+
contextRelevance: triadScore.contextRelevance,
|
|
10698
|
+
faithfulness: triadScore.faithfulness,
|
|
10699
|
+
answerRelevance: triadScore.answerRelevance,
|
|
10700
|
+
overallQuality: triadScore.overallQuality
|
|
10701
|
+
}
|
|
10166
10702
|
}, hScore ? {
|
|
10167
10703
|
hallucinationScore: hScore.score,
|
|
10168
10704
|
hallucinationReason: hScore.reason
|
|
@@ -10744,6 +11280,374 @@ var VectorPlugin = class {
|
|
|
10744
11280
|
}
|
|
10745
11281
|
};
|
|
10746
11282
|
|
|
11283
|
+
// src/utils/UniversalTableParser.ts
|
|
11284
|
+
var UniversalTableParser = class {
|
|
11285
|
+
/**
|
|
11286
|
+
* Parse a raw 2D grid of strings with complete matrix intelligence.
|
|
11287
|
+
*/
|
|
11288
|
+
static parseGrid(grid, sheetName = "Data") {
|
|
11289
|
+
if (!grid || grid.length === 0) return "";
|
|
11290
|
+
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));
|
|
11291
|
+
if (cleanGrid.length === 0) return "";
|
|
11292
|
+
const { headerRows, dataRows } = this.splitHeaderAndDataRows(cleanGrid);
|
|
11293
|
+
const fusedHeaders = this.fuseHeaders(headerRows);
|
|
11294
|
+
const matrixInfo = this.analyzeMatrixStructure(fusedHeaders, dataRows);
|
|
11295
|
+
const sections = [`--- Sheet: ${sheetName} ---`];
|
|
11296
|
+
const markdownTable = this.generateMarkdownTable(fusedHeaders, dataRows);
|
|
11297
|
+
if (markdownTable) {
|
|
11298
|
+
sections.push(`
|
|
11299
|
+
## Table: ${sheetName}
|
|
11300
|
+
${markdownTable}`);
|
|
11301
|
+
}
|
|
11302
|
+
if (matrixInfo.isComparisonMatrix && matrixInfo.entityIndices.length > 0) {
|
|
11303
|
+
const entityProfiles = this.generateEntityProfiles(fusedHeaders, dataRows, matrixInfo);
|
|
11304
|
+
if (entityProfiles) {
|
|
11305
|
+
sections.push(`
|
|
11306
|
+
## Entity Comparison Profiles
|
|
11307
|
+
${entityProfiles}`);
|
|
11308
|
+
}
|
|
11309
|
+
const criteriaComparisons = this.generateCriteriaComparisons(fusedHeaders, dataRows, matrixInfo);
|
|
11310
|
+
if (criteriaComparisons) {
|
|
11311
|
+
sections.push(`
|
|
11312
|
+
## Criteria Breakdown & Comparisons
|
|
11313
|
+
${criteriaComparisons}`);
|
|
11314
|
+
}
|
|
11315
|
+
} else {
|
|
11316
|
+
const contextualRows = this.generateContextualRows(fusedHeaders, dataRows);
|
|
11317
|
+
if (contextualRows) {
|
|
11318
|
+
sections.push(`
|
|
11319
|
+
## Structured Records
|
|
11320
|
+
${contextualRows}`);
|
|
11321
|
+
}
|
|
11322
|
+
}
|
|
11323
|
+
return sections.join("\n\n");
|
|
11324
|
+
}
|
|
11325
|
+
/**
|
|
11326
|
+
* Parse a raw CSV / TSV text string into tri-perspective contextual format.
|
|
11327
|
+
*/
|
|
11328
|
+
static parseDelimitedText(text, title = "Table", delimiter) {
|
|
11329
|
+
if (!text || !text.trim()) return "";
|
|
11330
|
+
if (!delimiter) {
|
|
11331
|
+
const firstLine = text.split(/\r?\n/)[0] || "";
|
|
11332
|
+
const commaCount = (firstLine.match(/,/g) || []).length;
|
|
11333
|
+
const tabCount = (firstLine.match(/\t/g) || []).length;
|
|
11334
|
+
const semiCount = (firstLine.match(/;/g) || []).length;
|
|
11335
|
+
const pipeCount = (firstLine.match(/\|/g) || []).length;
|
|
11336
|
+
if (tabCount > commaCount && tabCount > semiCount) delimiter = " ";
|
|
11337
|
+
else if (semiCount > commaCount && semiCount > tabCount) delimiter = ";";
|
|
11338
|
+
else if (pipeCount > commaCount && pipeCount > semiCount) delimiter = "|";
|
|
11339
|
+
else delimiter = ",";
|
|
11340
|
+
}
|
|
11341
|
+
const lines = text.split(/\r?\n/);
|
|
11342
|
+
const grid = [];
|
|
11343
|
+
for (const line of lines) {
|
|
11344
|
+
if (!line.trim()) continue;
|
|
11345
|
+
const row = [];
|
|
11346
|
+
let inQuote = false;
|
|
11347
|
+
let current = "";
|
|
11348
|
+
for (let i = 0; i < line.length; i++) {
|
|
11349
|
+
const char = line[i];
|
|
11350
|
+
if (char === '"') {
|
|
11351
|
+
if (inQuote && line[i + 1] === '"') {
|
|
11352
|
+
current += '"';
|
|
11353
|
+
i++;
|
|
11354
|
+
} else {
|
|
11355
|
+
inQuote = !inQuote;
|
|
11356
|
+
}
|
|
11357
|
+
} else if (char === delimiter && !inQuote) {
|
|
11358
|
+
row.push(current.trim());
|
|
11359
|
+
current = "";
|
|
11360
|
+
} else {
|
|
11361
|
+
current += char;
|
|
11362
|
+
}
|
|
11363
|
+
}
|
|
11364
|
+
row.push(current.trim());
|
|
11365
|
+
grid.push(row);
|
|
11366
|
+
}
|
|
11367
|
+
return this.parseGrid(grid, title);
|
|
11368
|
+
}
|
|
11369
|
+
/**
|
|
11370
|
+
* Parse a SheetJS sheet object with complete merged cell and matrix intelligence.
|
|
11371
|
+
*/
|
|
11372
|
+
static parseSheet(sheet, sheetName, XLSX) {
|
|
11373
|
+
if (!sheet) return "";
|
|
11374
|
+
const grid = this.extractAndUnrollGrid(sheet, XLSX);
|
|
11375
|
+
if (!grid || grid.length === 0) {
|
|
11376
|
+
const csv = XLSX.utils.sheet_to_csv(sheet, { blankrows: false });
|
|
11377
|
+
return csv && csv.trim() ? `--- Sheet: ${sheetName} ---
|
|
11378
|
+
${csv.trim()}` : "";
|
|
11379
|
+
}
|
|
11380
|
+
return this.parseGrid(grid, sheetName);
|
|
11381
|
+
}
|
|
11382
|
+
/**
|
|
11383
|
+
* Unrolls SheetJS merged cell ranges (`!merges`) across the 2D grid
|
|
11384
|
+
* so no child cell is left blank.
|
|
11385
|
+
*/
|
|
11386
|
+
static extractAndUnrollGrid(sheet, XLSX) {
|
|
11387
|
+
var _a2;
|
|
11388
|
+
const rawGrid = XLSX.utils.sheet_to_json(sheet, { header: 1, blankrows: false, defval: "" });
|
|
11389
|
+
if (!rawGrid || rawGrid.length === 0) return [];
|
|
11390
|
+
const grid = rawGrid.map(
|
|
11391
|
+
(row) => (Array.isArray(row) ? row : []).map((cell) => cell !== null && cell !== void 0 ? String(cell).trim() : "")
|
|
11392
|
+
);
|
|
11393
|
+
const merges = sheet["!merges"] || [];
|
|
11394
|
+
for (const merge of merges) {
|
|
11395
|
+
const { s, e } = merge;
|
|
11396
|
+
const originVal = ((_a2 = grid[s.r]) == null ? void 0 : _a2[s.c]) || "";
|
|
11397
|
+
if (!originVal) continue;
|
|
11398
|
+
for (let r = s.r; r <= e.r; r++) {
|
|
11399
|
+
if (!grid[r]) grid[r] = [];
|
|
11400
|
+
for (let c = s.c; c <= e.c; c++) {
|
|
11401
|
+
if (!grid[r][c] || grid[r][c] === "") {
|
|
11402
|
+
grid[r][c] = originVal;
|
|
11403
|
+
}
|
|
11404
|
+
}
|
|
11405
|
+
}
|
|
11406
|
+
}
|
|
11407
|
+
return grid.filter((row) => row.some((cell) => cell.length > 0));
|
|
11408
|
+
}
|
|
11409
|
+
/**
|
|
11410
|
+
* Intelligently identify where header rows end and data rows begin.
|
|
11411
|
+
*/
|
|
11412
|
+
static splitHeaderAndDataRows(grid) {
|
|
11413
|
+
if (grid.length <= 1) {
|
|
11414
|
+
return { headerRows: grid, dataRows: [] };
|
|
11415
|
+
}
|
|
11416
|
+
let headerRowCount = 1;
|
|
11417
|
+
for (let r = 1; r < Math.min(4, grid.length); r++) {
|
|
11418
|
+
const row = grid[r];
|
|
11419
|
+
const nonEmptyCells = row.filter(Boolean);
|
|
11420
|
+
if (nonEmptyCells.length === 0) continue;
|
|
11421
|
+
const isDataRow = nonEmptyCells.some(
|
|
11422
|
+
(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)
|
|
11423
|
+
);
|
|
11424
|
+
const hasSubheaderLabels = nonEmptyCells.some(
|
|
11425
|
+
(c) => /\b(bidder|vendor|supplier|score|weight|criteria|item|specification|technical|commercial|rating|status|cost|price|qty|unit)\b/i.test(c)
|
|
11426
|
+
);
|
|
11427
|
+
if (!isDataRow && hasSubheaderLabels) {
|
|
11428
|
+
headerRowCount = r + 1;
|
|
11429
|
+
} else if (!isDataRow && r === 1 && nonEmptyCells.length >= 2) {
|
|
11430
|
+
headerRowCount = 2;
|
|
11431
|
+
} else {
|
|
11432
|
+
break;
|
|
11433
|
+
}
|
|
11434
|
+
}
|
|
11435
|
+
const headerRows = grid.slice(0, headerRowCount);
|
|
11436
|
+
const dataRows = grid.slice(headerRowCount);
|
|
11437
|
+
return { headerRows, dataRows };
|
|
11438
|
+
}
|
|
11439
|
+
/**
|
|
11440
|
+
* Fuses multi-tier header rows into composite paths (e.g. `Commercial > Payment Terms > Bidder A`).
|
|
11441
|
+
*/
|
|
11442
|
+
static fuseHeaders(headerRows) {
|
|
11443
|
+
var _a2, _b;
|
|
11444
|
+
if (headerRows.length === 0) return [];
|
|
11445
|
+
if (headerRows.length === 1) {
|
|
11446
|
+
return headerRows[0].map((h, i) => h || `Column_${i + 1}`);
|
|
11447
|
+
}
|
|
11448
|
+
const maxCols = Math.max(...headerRows.map((r) => r.length));
|
|
11449
|
+
const fused = [];
|
|
11450
|
+
for (let c = 0; c < maxCols; c++) {
|
|
11451
|
+
const components = [];
|
|
11452
|
+
for (let r = 0; r < headerRows.length; r++) {
|
|
11453
|
+
const val = (_b = (_a2 = headerRows[r]) == null ? void 0 : _a2[c]) == null ? void 0 : _b.trim();
|
|
11454
|
+
if (val && !components.includes(val)) {
|
|
11455
|
+
components.push(val);
|
|
11456
|
+
}
|
|
11457
|
+
}
|
|
11458
|
+
fused.push(components.join(" > ") || `Column_${c + 1}`);
|
|
11459
|
+
}
|
|
11460
|
+
return fused;
|
|
11461
|
+
}
|
|
11462
|
+
/**
|
|
11463
|
+
* Analyzes whether the table is a comparison matrix (columns = entities, rows = criteria).
|
|
11464
|
+
*/
|
|
11465
|
+
static analyzeMatrixStructure(headers, dataRows) {
|
|
11466
|
+
const entityPattern = /\b(bidder|vendor|supplier|candidate|option|company|contractor|partner|proposal|firm|provider|solution|tier|package)\b/i;
|
|
11467
|
+
const entityIndices = [];
|
|
11468
|
+
const entityNames = [];
|
|
11469
|
+
let criteriaColumnIndex = 0;
|
|
11470
|
+
headers.forEach((h, idx) => {
|
|
11471
|
+
if (entityPattern.test(h) || /\b(bidder\s*\d+|vendor\s*[a-z0-9]+|option\s*[a-z0-9]+)\b/i.test(h)) {
|
|
11472
|
+
entityIndices.push(idx);
|
|
11473
|
+
const parts = h.split(" > ");
|
|
11474
|
+
entityNames.push(parts[parts.length - 1]);
|
|
11475
|
+
} else if (/\b(criteria|criterion|description|specification|category|requirement|item|feature)\b/i.test(h)) {
|
|
11476
|
+
criteriaColumnIndex = idx;
|
|
11477
|
+
}
|
|
11478
|
+
});
|
|
11479
|
+
const isComparisonMatrix = entityIndices.length >= 2 || headers.length >= 3 && entityIndices.length >= 1;
|
|
11480
|
+
return {
|
|
11481
|
+
isComparisonMatrix,
|
|
11482
|
+
entityIndices: entityIndices.length > 0 ? entityIndices : headers.slice(1).map((_, i) => i + 1),
|
|
11483
|
+
entityNames: entityNames.length > 0 ? entityNames : headers.slice(1),
|
|
11484
|
+
criteriaColumnIndex
|
|
11485
|
+
};
|
|
11486
|
+
}
|
|
11487
|
+
/**
|
|
11488
|
+
* Perspective A: Clean Markdown Table
|
|
11489
|
+
*/
|
|
11490
|
+
static generateMarkdownTable(headers, dataRows) {
|
|
11491
|
+
if (headers.length === 0 || dataRows.length === 0) return "";
|
|
11492
|
+
const cleanHeaders = headers.map((h) => h.replace(/\|/g, "-"));
|
|
11493
|
+
const lines = [];
|
|
11494
|
+
lines.push(`| ${cleanHeaders.join(" | ")} |`);
|
|
11495
|
+
lines.push(`| ${cleanHeaders.map(() => "---").join(" | ")} |`);
|
|
11496
|
+
for (const row of dataRows) {
|
|
11497
|
+
const paddedRow = headers.map((_, i) => (row[i] || "").replace(/\|/g, "-"));
|
|
11498
|
+
if (paddedRow.some((cell) => cell.trim().length > 0)) {
|
|
11499
|
+
lines.push(`| ${paddedRow.join(" | ")} |`);
|
|
11500
|
+
}
|
|
11501
|
+
}
|
|
11502
|
+
return lines.join("\n");
|
|
11503
|
+
}
|
|
11504
|
+
/**
|
|
11505
|
+
* Perspective B: Entity-Centric Profiles (e.g. Bidder 1, Bidder 2 full summary)
|
|
11506
|
+
*/
|
|
11507
|
+
static generateEntityProfiles(headers, dataRows, matrixInfo) {
|
|
11508
|
+
const profiles = [];
|
|
11509
|
+
matrixInfo.entityIndices.forEach((colIdx, i) => {
|
|
11510
|
+
const entityName = matrixInfo.entityNames[i] || headers[colIdx] || `Entity ${i + 1}`;
|
|
11511
|
+
const lines = [`### Profile & Evaluation: ${entityName}`];
|
|
11512
|
+
for (const row of dataRows) {
|
|
11513
|
+
const criterion = row[matrixInfo.criteriaColumnIndex] || row[0] || "Specification";
|
|
11514
|
+
const val = row[colIdx];
|
|
11515
|
+
if (val !== void 0 && val !== null && String(val).trim() !== "") {
|
|
11516
|
+
lines.push(`- **${criterion}**: ${String(val).trim()}`);
|
|
11517
|
+
}
|
|
11518
|
+
}
|
|
11519
|
+
if (lines.length > 1) {
|
|
11520
|
+
profiles.push(lines.join("\n"));
|
|
11521
|
+
}
|
|
11522
|
+
});
|
|
11523
|
+
return profiles.join("\n\n");
|
|
11524
|
+
}
|
|
11525
|
+
/**
|
|
11526
|
+
* Perspective C: Criteria Cross-Comparisons (e.g. Commercial Bid comparison across all Bidders)
|
|
11527
|
+
*/
|
|
11528
|
+
static generateCriteriaComparisons(headers, dataRows, matrixInfo) {
|
|
11529
|
+
const comparisons = [];
|
|
11530
|
+
for (const row of dataRows) {
|
|
11531
|
+
const criterion = row[matrixInfo.criteriaColumnIndex] || row[0] || "Metric";
|
|
11532
|
+
const entityPairs = [];
|
|
11533
|
+
matrixInfo.entityIndices.forEach((colIdx, i) => {
|
|
11534
|
+
const entityName = matrixInfo.entityNames[i] || headers[colIdx] || `Entity ${i + 1}`;
|
|
11535
|
+
const val = row[colIdx];
|
|
11536
|
+
if (val !== void 0 && val !== null && String(val).trim() !== "") {
|
|
11537
|
+
entityPairs.push(`${entityName}: ${String(val).trim()}`);
|
|
11538
|
+
}
|
|
11539
|
+
});
|
|
11540
|
+
if (entityPairs.length > 0) {
|
|
11541
|
+
comparisons.push(`- **${criterion}** $\\rightarrow$ ${entityPairs.join(" | ")}`);
|
|
11542
|
+
}
|
|
11543
|
+
}
|
|
11544
|
+
return comparisons.join("\n");
|
|
11545
|
+
}
|
|
11546
|
+
/**
|
|
11547
|
+
* Standard contextual key-value rows for general tabular records.
|
|
11548
|
+
*/
|
|
11549
|
+
static generateContextualRows(headers, dataRows) {
|
|
11550
|
+
const lines = [];
|
|
11551
|
+
for (const row of dataRows) {
|
|
11552
|
+
const pairs = [];
|
|
11553
|
+
headers.forEach((h, i) => {
|
|
11554
|
+
const val = row[i];
|
|
11555
|
+
if (val !== void 0 && val !== null && String(val).trim() !== "") {
|
|
11556
|
+
pairs.push(`${h}: ${String(val).trim()}`);
|
|
11557
|
+
}
|
|
11558
|
+
});
|
|
11559
|
+
if (pairs.length > 0) {
|
|
11560
|
+
lines.push(pairs.join(" | "));
|
|
11561
|
+
}
|
|
11562
|
+
}
|
|
11563
|
+
return lines.join("\n");
|
|
11564
|
+
}
|
|
11565
|
+
};
|
|
11566
|
+
|
|
11567
|
+
// src/utils/VisionExtractor.ts
|
|
11568
|
+
var VisionExtractor = class {
|
|
11569
|
+
/**
|
|
11570
|
+
* Check if a buffer represents an image format.
|
|
11571
|
+
*/
|
|
11572
|
+
static isImageBuffer(buffer, fileName = "", mimeType = "") {
|
|
11573
|
+
var _a2;
|
|
11574
|
+
const ext = ((_a2 = fileName.split(".").pop()) == null ? void 0 : _a2.toLowerCase()) || "";
|
|
11575
|
+
if (["png", "jpg", "jpeg", "webp", "bmp", "tiff", "gif"].includes(ext)) {
|
|
11576
|
+
return true;
|
|
11577
|
+
}
|
|
11578
|
+
if (mimeType.startsWith("image/")) {
|
|
11579
|
+
return true;
|
|
11580
|
+
}
|
|
11581
|
+
if (buffer && buffer.length >= 4) {
|
|
11582
|
+
if (buffer[0] === 137 && buffer[1] === 80 && buffer[2] === 78 && buffer[3] === 71) return true;
|
|
11583
|
+
if (buffer[0] === 255 && buffer[1] === 216 && buffer[2] === 255) return true;
|
|
11584
|
+
if (buffer[0] === 82 && buffer[1] === 73 && buffer[2] === 70 && buffer[3] === 70) return true;
|
|
11585
|
+
}
|
|
11586
|
+
return false;
|
|
11587
|
+
}
|
|
11588
|
+
/**
|
|
11589
|
+
* Extract rich textual content, tables, and diagram descriptions from an image buffer using Vision LLM.
|
|
11590
|
+
*/
|
|
11591
|
+
static async extractFromImage(buffer, fileName = "document.png", options = {}) {
|
|
11592
|
+
var _a2, _b, _c, _d, _e, _f, _g2, _h, _i;
|
|
11593
|
+
const base64Image = buffer.toString("base64");
|
|
11594
|
+
const ext = ((_a2 = fileName.split(".").pop()) == null ? void 0 : _a2.toLowerCase()) || "png";
|
|
11595
|
+
const mime = ext === "jpg" || ext === "jpeg" ? "image/jpeg" : ext === "webp" ? "image/webp" : "image/png";
|
|
11596
|
+
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);
|
|
11597
|
+
if (!apiKey) {
|
|
11598
|
+
return `[Scanned Document / Image: ${fileName} - Size: ${(buffer.length / 1024).toFixed(1)} KB]`;
|
|
11599
|
+
}
|
|
11600
|
+
try {
|
|
11601
|
+
const response = await fetch("https://api.openai.com/v1/chat/completions", {
|
|
11602
|
+
method: "POST",
|
|
11603
|
+
headers: {
|
|
11604
|
+
"Authorization": `Bearer ${apiKey}`,
|
|
11605
|
+
"Content-Type": "application/json"
|
|
11606
|
+
},
|
|
11607
|
+
body: JSON.stringify({
|
|
11608
|
+
model: options.model || "gpt-4o-mini",
|
|
11609
|
+
messages: [
|
|
11610
|
+
{
|
|
11611
|
+
role: "system",
|
|
11612
|
+
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."
|
|
11613
|
+
},
|
|
11614
|
+
{
|
|
11615
|
+
role: "user",
|
|
11616
|
+
content: [
|
|
11617
|
+
{
|
|
11618
|
+
type: "text",
|
|
11619
|
+
text: `Please transcribe the content of this document/image (${fileName}):`
|
|
11620
|
+
},
|
|
11621
|
+
{
|
|
11622
|
+
type: "image_url",
|
|
11623
|
+
image_url: {
|
|
11624
|
+
url: `data:${mime};base64,${base64Image}`,
|
|
11625
|
+
detail: "high"
|
|
11626
|
+
}
|
|
11627
|
+
}
|
|
11628
|
+
]
|
|
11629
|
+
}
|
|
11630
|
+
],
|
|
11631
|
+
max_tokens: options.maxTokens || 2048,
|
|
11632
|
+
temperature: (_e = options.temperature) != null ? _e : 0.1
|
|
11633
|
+
})
|
|
11634
|
+
});
|
|
11635
|
+
if (response.ok) {
|
|
11636
|
+
const data = await response.json();
|
|
11637
|
+
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();
|
|
11638
|
+
if (text) {
|
|
11639
|
+
return `--- Document / Image OCR: ${fileName} ---
|
|
11640
|
+
|
|
11641
|
+
${text}`;
|
|
11642
|
+
}
|
|
11643
|
+
}
|
|
11644
|
+
} catch (err) {
|
|
11645
|
+
console.warn("[VisionExtractor] OpenAI Vision OCR failed:", err);
|
|
11646
|
+
}
|
|
11647
|
+
return `[Scanned Image Document: ${fileName}]`;
|
|
11648
|
+
}
|
|
11649
|
+
};
|
|
11650
|
+
|
|
10747
11651
|
// src/utils/DocumentParser.ts
|
|
10748
11652
|
var DocumentParser = class {
|
|
10749
11653
|
/**
|
|
@@ -10767,52 +11671,11 @@ var DocumentParser = class {
|
|
|
10767
11671
|
return "text";
|
|
10768
11672
|
}
|
|
10769
11673
|
/**
|
|
10770
|
-
* Convert spreadsheet sheets into high-fidelity, context-preserved tabular text.
|
|
10771
|
-
*
|
|
11674
|
+
* Convert spreadsheet sheets into high-fidelity, context-preserved tabular text via UniversalTableParser.
|
|
11675
|
+
* Unrolls merged cells, fuses multi-level headers, and generates tri-perspective comparison profiles.
|
|
10772
11676
|
*/
|
|
10773
11677
|
static formatSheetToContextualText(sheet, sheetName, XLSX) {
|
|
10774
|
-
|
|
10775
|
-
if (!jsonRows || jsonRows.length === 0) {
|
|
10776
|
-
const csv = XLSX.utils.sheet_to_csv(sheet, { blankrows: false });
|
|
10777
|
-
return csv && csv.trim() ? `--- Sheet: ${sheetName} ---
|
|
10778
|
-
${csv.trim()}` : "";
|
|
10779
|
-
}
|
|
10780
|
-
const lines = [`--- Sheet: ${sheetName} ---`];
|
|
10781
|
-
let currentHeader = [];
|
|
10782
|
-
for (let r = 0; r < jsonRows.length; r++) {
|
|
10783
|
-
const row = jsonRows[r];
|
|
10784
|
-
if (!row || row.length === 0) continue;
|
|
10785
|
-
const cells = row.map((c) => c !== null && c !== void 0 ? String(c).trim() : "").filter(Boolean);
|
|
10786
|
-
if (cells.length === 0) continue;
|
|
10787
|
-
if (cells.length === 1 || cells.length <= 2 && cells[0].toUpperCase().includes("STEP")) {
|
|
10788
|
-
lines.push(`
|
|
10789
|
-
## ${cells.join(" ")}`);
|
|
10790
|
-
continue;
|
|
10791
|
-
}
|
|
10792
|
-
const isHeaderCandidate = cells.every((c) => isNaN(Number(c)) && !/^\$?[\d,.]+/.test(c));
|
|
10793
|
-
if (isHeaderCandidate && cells.length >= 2) {
|
|
10794
|
-
currentHeader = row.map((c) => c !== null && c !== void 0 ? String(c).trim() : "");
|
|
10795
|
-
lines.push(`| ${cells.join(" | ")} |`);
|
|
10796
|
-
lines.push(`| ${cells.map(() => "---").join(" | ")} |`);
|
|
10797
|
-
continue;
|
|
10798
|
-
}
|
|
10799
|
-
if (currentHeader.length > 0 && cells.length > 1) {
|
|
10800
|
-
const fieldPairs = [];
|
|
10801
|
-
for (let c = 0; c < row.length; c++) {
|
|
10802
|
-
const h = currentHeader[c];
|
|
10803
|
-
const val = row[c];
|
|
10804
|
-
if (h && val !== null && val !== void 0 && String(val).trim() !== "") {
|
|
10805
|
-
fieldPairs.push(`${h}: ${String(val).trim()}`);
|
|
10806
|
-
}
|
|
10807
|
-
}
|
|
10808
|
-
if (fieldPairs.length > 0) {
|
|
10809
|
-
lines.push(fieldPairs.join(" | "));
|
|
10810
|
-
continue;
|
|
10811
|
-
}
|
|
10812
|
-
}
|
|
10813
|
-
lines.push(cells.join(", "));
|
|
10814
|
-
}
|
|
10815
|
-
return lines.join("\n");
|
|
11678
|
+
return UniversalTableParser.parseSheet(sheet, sheetName, XLSX);
|
|
10816
11679
|
}
|
|
10817
11680
|
/**
|
|
10818
11681
|
* Extract text from a File or Buffer based on its type.
|
|
@@ -10836,7 +11699,9 @@ ${csv.trim()}` : "";
|
|
|
10836
11699
|
return this.readAsText(file);
|
|
10837
11700
|
}
|
|
10838
11701
|
if (extension === "csv" || extension === "tsv" || mimeType === "text/csv" || mimeType === "text/tab-separated-values") {
|
|
10839
|
-
|
|
11702
|
+
const text = await this.readAsText(file);
|
|
11703
|
+
const parsed = UniversalTableParser.parseDelimitedText(text, fileName);
|
|
11704
|
+
return parsed || text;
|
|
10840
11705
|
}
|
|
10841
11706
|
if (extension === "pptx" || extension === "ppt" || extension === "odp" || mimeType.includes("presentation") || mimeType.includes("powerpoint")) {
|
|
10842
11707
|
try {
|
|
@@ -10943,16 +11808,22 @@ ${csv.trim()}`);
|
|
|
10943
11808
|
return this.parseLegacyExcelOrFallback(buffer, fileName);
|
|
10944
11809
|
}
|
|
10945
11810
|
if (extension === "pdf" || mimeType === "application/pdf") {
|
|
11811
|
+
const buffer = Buffer.isBuffer(file) ? file : Buffer.from(await file.arrayBuffer());
|
|
10946
11812
|
try {
|
|
10947
11813
|
const pdfModule = await import("pdf-parse");
|
|
10948
|
-
const buffer = Buffer.isBuffer(file) ? file : Buffer.from(await file.arrayBuffer());
|
|
10949
11814
|
const pdf = pdfModule.default || pdfModule;
|
|
10950
11815
|
const data = await pdf(buffer);
|
|
10951
|
-
|
|
11816
|
+
if (data.text && data.text.trim().length > 30) {
|
|
11817
|
+
return data.text;
|
|
11818
|
+
}
|
|
10952
11819
|
} catch (err) {
|
|
10953
|
-
console.warn(
|
|
10954
|
-
return `[PDF Parsing Error for ${fileName}]`;
|
|
11820
|
+
console.warn("[DocumentParser] pdf-parse failed, attempting Vision OCR fallback:", err);
|
|
10955
11821
|
}
|
|
11822
|
+
return VisionExtractor.extractFromImage(buffer, fileName);
|
|
11823
|
+
}
|
|
11824
|
+
if (["png", "jpg", "jpeg", "webp", "bmp", "tiff", "gif"].includes(extension) || mimeType.startsWith("image/")) {
|
|
11825
|
+
const buffer = Buffer.isBuffer(file) ? file : Buffer.from(await file.arrayBuffer());
|
|
11826
|
+
return VisionExtractor.extractFromImage(buffer, fileName);
|
|
10956
11827
|
}
|
|
10957
11828
|
if (extension === "docx" || extension === "doc" || extension === "odt" || extension === "rtf" || mimeType === "application/vnd.openxmlformats-officedocument.wordprocessingml.document" || mimeType === "application/msword" || mimeType === "application/rtf") {
|
|
10958
11829
|
try {
|
|
@@ -11294,44 +12165,38 @@ var _DatabaseStorage = class _DatabaseStorage {
|
|
|
11294
12165
|
*
|
|
11295
12166
|
* In non-production environments this is a complete no-op so local
|
|
11296
12167
|
* development works without a license key.
|
|
12168
|
+
/**
|
|
12169
|
+
* Checks if the caller holds a valid paid-tier license in production.
|
|
12170
|
+
* Returns true if allowed, false if not supported on current tier.
|
|
11297
12171
|
*/
|
|
11298
|
-
async
|
|
11299
|
-
if (process.env.NODE_ENV !== "production") return;
|
|
11300
|
-
if (
|
|
11301
|
-
if (
|
|
11302
|
-
|
|
11303
|
-
"[Retrivora SDK] Chat History and Feedback features require a paid license key in production."
|
|
11304
|
-
);
|
|
12172
|
+
async isPaidSubscription() {
|
|
12173
|
+
if (process.env.NODE_ENV !== "production") return true;
|
|
12174
|
+
if (!this.config.licenseKey) return false;
|
|
12175
|
+
if (Date.now() - this.subscriptionCheckedAt < _DatabaseStorage.SUBSCRIPTION_CACHE_TTL_MS) {
|
|
12176
|
+
return true;
|
|
11305
12177
|
}
|
|
11306
12178
|
try {
|
|
11307
12179
|
const payload = await LicenseVerifier.verifyAsync(this.config.licenseKey, this.config.projectId);
|
|
11308
12180
|
const tier = (payload.tier || "").toLowerCase();
|
|
11309
|
-
const PAID_TIERS = /* @__PURE__ */ new Set(["premium", "enterprise", "growth", "pro", "developer_pro"]);
|
|
11310
|
-
|
|
11311
|
-
|
|
11312
|
-
|
|
11313
|
-
);
|
|
12181
|
+
const PAID_TIERS = /* @__PURE__ */ new Set(["premium", "enterprise", "growth", "pro", "developer_pro", "free_trial"]);
|
|
12182
|
+
const isAllowed = PAID_TIERS.has(tier) || tier.includes("pro") || tier.includes("enterprise");
|
|
12183
|
+
if (isAllowed) {
|
|
12184
|
+
this.subscriptionCheckedAt = Date.now();
|
|
11314
12185
|
}
|
|
11315
|
-
|
|
11316
|
-
} catch (
|
|
11317
|
-
|
|
11318
|
-
`[Retrivora SDK] Subscription check failed: ${err instanceof Error ? err.message : String(err)}`
|
|
11319
|
-
);
|
|
12186
|
+
return isAllowed;
|
|
12187
|
+
} catch (e) {
|
|
12188
|
+
return false;
|
|
11320
12189
|
}
|
|
11321
12190
|
}
|
|
11322
|
-
async
|
|
12191
|
+
async isHistoryAvailable() {
|
|
11323
12192
|
var _a2, _b;
|
|
11324
|
-
|
|
11325
|
-
|
|
11326
|
-
throw new Error("[Retrivora SDK] Chat History is disabled in RagConfig.");
|
|
11327
|
-
}
|
|
12193
|
+
if (((_b = (_a2 = this.config.rag) == null ? void 0 : _a2.history) == null ? void 0 : _b.enabled) === false) return false;
|
|
12194
|
+
return this.isPaidSubscription();
|
|
11328
12195
|
}
|
|
11329
|
-
async
|
|
12196
|
+
async isFeedbackAvailable() {
|
|
11330
12197
|
var _a2, _b;
|
|
11331
|
-
|
|
11332
|
-
|
|
11333
|
-
throw new Error("[Retrivora SDK] User Feedback is disabled in RagConfig.");
|
|
11334
|
-
}
|
|
12198
|
+
if (((_b = (_a2 = this.config.rag) == null ? void 0 : _a2.feedback) == null ? void 0 : _b.enabled) === false) return false;
|
|
12199
|
+
return this.isPaidSubscription();
|
|
11335
12200
|
}
|
|
11336
12201
|
/**
|
|
11337
12202
|
* Returns (or lazily creates) the PostgreSQL connection pool for this project.
|
|
@@ -11445,7 +12310,7 @@ var _DatabaseStorage = class _DatabaseStorage {
|
|
|
11445
12310
|
}
|
|
11446
12311
|
// ─── Message History Operations ──────────────────────────────────────────
|
|
11447
12312
|
async saveMessage(sessionId, message) {
|
|
11448
|
-
await this.
|
|
12313
|
+
if (!await this.isHistoryAvailable()) return;
|
|
11449
12314
|
const createdAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
11450
12315
|
const msgId = message.id || `msg_${Date.now()}_${Math.random().toString(36).slice(2, 6)}`;
|
|
11451
12316
|
if (this.provider === "postgresql" || this.provider === "pgvector") {
|
|
@@ -11508,7 +12373,7 @@ var _DatabaseStorage = class _DatabaseStorage {
|
|
|
11508
12373
|
}
|
|
11509
12374
|
}
|
|
11510
12375
|
async getHistory(sessionId, projectIds) {
|
|
11511
|
-
await this.
|
|
12376
|
+
if (!await this.isHistoryAvailable()) return [];
|
|
11512
12377
|
if (projectIds && projectIds.length > 0 && !this.sessionInScope(sessionId, projectIds)) {
|
|
11513
12378
|
return [];
|
|
11514
12379
|
}
|
|
@@ -11542,7 +12407,7 @@ var _DatabaseStorage = class _DatabaseStorage {
|
|
|
11542
12407
|
}
|
|
11543
12408
|
}
|
|
11544
12409
|
async clearHistory(sessionId, projectIds) {
|
|
11545
|
-
await this.
|
|
12410
|
+
if (!await this.isHistoryAvailable()) return;
|
|
11546
12411
|
if (projectIds && projectIds.length > 0 && !this.sessionInScope(sessionId, projectIds)) {
|
|
11547
12412
|
return;
|
|
11548
12413
|
}
|
|
@@ -11582,7 +12447,7 @@ var _DatabaseStorage = class _DatabaseStorage {
|
|
|
11582
12447
|
});
|
|
11583
12448
|
}
|
|
11584
12449
|
async listSessions(projectIds) {
|
|
11585
|
-
await this.
|
|
12450
|
+
if (!await this.isHistoryAvailable()) return [];
|
|
11586
12451
|
let raw = [];
|
|
11587
12452
|
if (this.provider === "postgresql" || this.provider === "pgvector") {
|
|
11588
12453
|
const pool = await this.getPGPool();
|
|
@@ -11601,7 +12466,7 @@ var _DatabaseStorage = class _DatabaseStorage {
|
|
|
11601
12466
|
}
|
|
11602
12467
|
// ─── Feedback Operations ──────────────────────────────────────────────────
|
|
11603
12468
|
async saveFeedback(feedback) {
|
|
11604
|
-
await this.
|
|
12469
|
+
if (!await this.isFeedbackAvailable()) return;
|
|
11605
12470
|
const id = `fb_${Date.now()}_${Math.random().toString(36).slice(2, 6)}`;
|
|
11606
12471
|
const createdAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
11607
12472
|
if (this.provider === "postgresql" || this.provider === "pgvector") {
|
|
@@ -11651,7 +12516,7 @@ var _DatabaseStorage = class _DatabaseStorage {
|
|
|
11651
12516
|
}
|
|
11652
12517
|
}
|
|
11653
12518
|
async getFeedback(messageId, projectIds) {
|
|
11654
|
-
await this.
|
|
12519
|
+
if (!await this.isFeedbackAvailable()) return null;
|
|
11655
12520
|
let item = null;
|
|
11656
12521
|
if (this.provider === "postgresql" || this.provider === "pgvector") {
|
|
11657
12522
|
const pool = await this.getPGPool();
|
|
@@ -11687,7 +12552,7 @@ var _DatabaseStorage = class _DatabaseStorage {
|
|
|
11687
12552
|
return item;
|
|
11688
12553
|
}
|
|
11689
12554
|
async listFeedback(projectIds) {
|
|
11690
|
-
await this.
|
|
12555
|
+
if (!await this.isFeedbackAvailable()) return [];
|
|
11691
12556
|
let raw = [];
|
|
11692
12557
|
if (this.provider === "postgresql" || this.provider === "pgvector") {
|
|
11693
12558
|
const pool = await this.getPGPool();
|