@retrivora-ai/rag-engine 2.4.3 → 2.4.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +22 -0
- package/dist/{BatchProcessor-zbQMaZHm.d.mts → BatchProcessor-BGslIR5l.d.mts} +65 -4
- package/dist/{BatchProcessor-D42E706a.d.ts → BatchProcessor-C3gY6kZ0.d.ts} +65 -4
- package/dist/handlers/index.js +668 -837
- package/dist/handlers/index.mjs +678 -837
- package/dist/index.css +5 -0
- package/dist/index.d.mts +4 -28
- package/dist/index.d.ts +4 -28
- package/dist/index.js +91 -43
- package/dist/index.min.css +1 -1
- package/dist/index.mjs +91 -43
- package/dist/server.d.mts +21 -7
- package/dist/server.d.ts +21 -7
- package/dist/server.js +731 -875
- package/dist/server.mjs +741 -875
- package/package.json +11 -4
- package/src/LicensedRetrivora.ts +0 -278
- package/src/app/api/chat/route.ts +0 -4
- package/src/app/api/health/route.ts +0 -5
- package/src/app/api/ingest/route.ts +0 -27
- package/src/app/api/suggestions/route.ts +0 -5
- package/src/app/api/upload/route.ts +0 -5
- package/src/app/constants.tsx +0 -270
- package/src/app/favicon.ico +0 -0
- package/src/app/globals.css +0 -449
- package/src/app/layout.tsx +0 -22
- package/src/app/page.tsx +0 -64
- package/src/app/types.ts +0 -30
- package/src/browser.tsx +0 -127
- package/src/components/ChatWidget.tsx +0 -393
- package/src/components/ChatWindow.tsx +0 -952
- package/src/components/CodeViewer.tsx +0 -56
- package/src/components/ConfigProvider.tsx +0 -141
- package/src/components/DocumentUpload.tsx +0 -354
- package/src/components/DynamicChart.tsx +0 -297
- package/src/components/HourglassLoader.tsx +0 -87
- package/src/components/MarkdownComponents.tsx +0 -186
- package/src/components/MessageBubble.tsx +0 -498
- package/src/components/ObservabilityPanel.tsx +0 -374
- package/src/components/ProductCard.tsx +0 -107
- package/src/components/ProductCarousel.tsx +0 -97
- package/src/components/SourceCard.tsx +0 -61
- package/src/components/ThemeProvider.tsx +0 -8
- package/src/components/ThemeToggle.tsx +0 -29
- package/src/components/ThinkingBlock.tsx +0 -75
- package/src/components/UIDispatcher.tsx +0 -342
- package/src/components/VisualizationRenderer.tsx +0 -621
- package/src/components/constants.tsx +0 -503
- package/src/config/ConfigBuilder.ts +0 -285
- package/src/config/EmbeddingStrategy.ts +0 -148
- package/src/config/LicenseConfigResolver.ts +0 -298
- package/src/config/RagConfig.ts +0 -328
- package/src/config/UniversalProfiles.ts +0 -82
- package/src/config/constants.ts +0 -64
- package/src/config/serverConfig.ts +0 -378
- package/src/config/uiConstants.ts +0 -47
- package/src/core/BatchProcessor.ts +0 -385
- package/src/core/CircuitBreaker.ts +0 -118
- package/src/core/ConfigFetcher.ts +0 -220
- package/src/core/ConfigResolver.ts +0 -165
- package/src/core/ConfigValidator.ts +0 -197
- package/src/core/DatabaseStorage.ts +0 -526
- package/src/core/FreeTierLimitsGuard.ts +0 -281
- package/src/core/LLMRouter.ts +0 -79
- package/src/core/LangChainAgent.ts +0 -132
- package/src/core/LicenseValidator.ts +0 -284
- package/src/core/LicenseVerifier.ts +0 -261
- package/src/core/MultiAgentCoordinator.ts +0 -239
- package/src/core/Pipeline.ts +0 -1279
- package/src/core/PluginManager.ts +0 -277
- package/src/core/ProjectSettings.ts +0 -247
- package/src/core/ProviderHealthCheck.ts +0 -133
- package/src/core/ProviderInterfaces.ts +0 -37
- package/src/core/ProviderRegistry.ts +0 -156
- package/src/core/QueryProcessor.ts +0 -399
- package/src/core/Retrivora.ts +0 -98
- package/src/core/VectorPlugin.ts +0 -193
- package/src/core/mcp.ts +0 -261
- package/src/exceptions/index.ts +0 -131
- package/src/handlers/index.ts +0 -1625
- package/src/hooks/useHydrated.ts +0 -13
- package/src/hooks/useProjectSettings.ts +0 -105
- package/src/hooks/useRagChat.ts +0 -436
- package/src/hooks/useStoredMessages.ts +0 -64
- package/src/index.css +0 -4269
- package/src/index.ts +0 -137
- package/src/lib/plugin.ts +0 -24
- package/src/llm/ILLMProvider.ts +0 -60
- package/src/llm/LLMFactory.ts +0 -141
- package/src/llm/providers/AnthropicProvider.ts +0 -215
- package/src/llm/providers/GeminiProvider.ts +0 -311
- package/src/llm/providers/GroqProvider.ts +0 -184
- package/src/llm/providers/OllamaProvider.ts +0 -232
- package/src/llm/providers/OpenAIProvider.ts +0 -188
- package/src/llm/providers/QwenProvider.ts +0 -199
- package/src/llm/providers/UniversalLLMAdapter.ts +0 -422
- package/src/llm/utils.ts +0 -46
- package/src/providers/graphdb/BaseGraphProvider.ts +0 -43
- package/src/providers/graphdb/SimpleGraphProvider.ts +0 -66
- package/src/providers/vectordb/BaseVectorProvider.ts +0 -98
- package/src/providers/vectordb/ChromaDBProvider.ts +0 -130
- package/src/providers/vectordb/MilvusProvider.ts +0 -134
- package/src/providers/vectordb/MongoDBProvider.ts +0 -250
- package/src/providers/vectordb/MultiTablePostgresProvider.ts +0 -461
- package/src/providers/vectordb/PineconeProvider.ts +0 -295
- package/src/providers/vectordb/PostgreSQLProvider.ts +0 -227
- package/src/providers/vectordb/QdrantProvider.ts +0 -294
- package/src/providers/vectordb/RedisProvider.ts +0 -105
- package/src/providers/vectordb/UniversalVectorProvider.ts +0 -221
- package/src/providers/vectordb/WeaviateProvider.ts +0 -172
- package/src/rag/DocumentChunker.ts +0 -144
- package/src/rag/EntityExtractor.ts +0 -90
- package/src/rag/LlamaIndexIngestor.ts +0 -60
- package/src/rag/Reranker.ts +0 -106
- package/src/rendering/IntentClassifier.ts +0 -167
- package/src/rendering/RendererRegistry.ts +0 -164
- package/src/rendering/RuleEngine.ts +0 -67
- package/src/rendering/VisualizationDecisionEngine.ts +0 -164
- package/src/rendering/__tests__/VisualizationDecisionEngine.test.ts +0 -237
- package/src/rendering/index.ts +0 -12
- package/src/rendering/rules/Rule1SpecificInfoRule.ts +0 -28
- package/src/rendering/rules/Rule2ComparisonRule.ts +0 -24
- package/src/rendering/rules/Rule3ProductDiscoveryRule.ts +0 -28
- package/src/rendering/rules/Rule4AnalyticalRule.ts +0 -38
- package/src/rendering/rules/Rule5MixedResponseRule.ts +0 -29
- package/src/rendering/rules/Rule6SmallResultSetRule.ts +0 -24
- package/src/rendering/rules/Rule7LargeTableRule.ts +0 -35
- package/src/rendering/types.ts +0 -96
- package/src/server.ts +0 -156
- package/src/tailwind.css +0 -194
- package/src/telemetry/TelemetryService.ts +0 -330
- package/src/types/chat.ts +0 -89
- package/src/types/index.ts +0 -278
- package/src/types/pdf-parse.d.ts +0 -13
- package/src/types/props.ts +0 -169
- package/src/utils/DocumentParser.ts +0 -111
- package/src/utils/ProductExtractor.ts +0 -350
- package/src/utils/SchemaMapper.ts +0 -176
- package/src/utils/UITransformer.ts +0 -1812
- package/src/utils/synonyms.ts +0 -80
- package/src/utils/templateUtils.ts +0 -78
- package/src/version.ts +0 -6
package/dist/handlers/index.js
CHANGED
|
@@ -50,6 +50,7 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
50
50
|
}
|
|
51
51
|
return to;
|
|
52
52
|
};
|
|
53
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
53
54
|
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
54
55
|
// If the importer is in node compatibility mode or this is not an ESM
|
|
55
56
|
// file that has been converted to a CommonJS file using a Babel-
|
|
@@ -317,7 +318,7 @@ var init_package = __esm({
|
|
|
317
318
|
"package.json"() {
|
|
318
319
|
package_default = {
|
|
319
320
|
name: "@retrivora-ai/rag-engine",
|
|
320
|
-
version: "2.4.
|
|
321
|
+
version: "2.4.6",
|
|
321
322
|
description: "Retrivora AI is a plug-and-play AI engine for RAG chat experiences \u2014 generic vector DB + LLM provider, embeddable or standalone.",
|
|
322
323
|
author: "Abhinav Alkuchi",
|
|
323
324
|
license: "UNLICENSED",
|
|
@@ -389,8 +390,6 @@ var init_package = __esm({
|
|
|
389
390
|
},
|
|
390
391
|
files: [
|
|
391
392
|
"dist",
|
|
392
|
-
"src",
|
|
393
|
-
".env.example",
|
|
394
393
|
"README.md",
|
|
395
394
|
"FREE_TIER_ARCHITECTURE.md"
|
|
396
395
|
],
|
|
@@ -400,14 +399,19 @@ var init_package = __esm({
|
|
|
400
399
|
"build:pkg": "npx @tailwindcss/cli -i src/tailwind.css -o src/index.css && tsup src/index.ts src/handlers/index.ts src/server.ts --format cjs,esm --dts --clean --no-splitting --tsconfig tsconfig.build.json --external react,react-dom,next,mongodb,pg,openai,@anthropic-ai/sdk,@pinecone-database/pinecone,axios,lucide-react,mammoth,pdf-parse,xlsx,react-markdown,remark-gfm,next-themes,langchain,@langchain/core,@langchain/openai,llamaindex --inject-style && npx @tailwindcss/cli -i src/tailwind.css -o dist/index.css && npx @tailwindcss/cli -i src/tailwind.css -o dist/index.min.css --minify",
|
|
401
400
|
lint: "eslint",
|
|
402
401
|
typecheck: "tsc --noEmit -p tsconfig.build.json",
|
|
402
|
+
test: "jest",
|
|
403
403
|
clean: "rm -rf dist"
|
|
404
404
|
},
|
|
405
405
|
peerDependencies: {
|
|
406
|
+
axios: ">=1.0.0",
|
|
406
407
|
next: ">=15.0.0",
|
|
407
408
|
react: ">=18.0.0",
|
|
408
409
|
"react-dom": ">=18.0.0"
|
|
409
410
|
},
|
|
410
411
|
peerDependenciesMeta: {
|
|
412
|
+
axios: {
|
|
413
|
+
optional: true
|
|
414
|
+
},
|
|
411
415
|
next: {
|
|
412
416
|
optional: true
|
|
413
417
|
}
|
|
@@ -416,9 +420,9 @@ var init_package = __esm({
|
|
|
416
420
|
"@anthropic-ai/sdk": "^0.95.1",
|
|
417
421
|
"@google/genai": "^0.8.0",
|
|
418
422
|
"@google/generative-ai": "^0.24.1",
|
|
423
|
+
"@retrivora-ai/billing-guards": "*",
|
|
419
424
|
"@pinecone-database/pinecone": "^7.2.0",
|
|
420
425
|
"@types/papaparse": "^5.5.2",
|
|
421
|
-
axios: "^1.15.0",
|
|
422
426
|
"lucide-react": "^1.8.0",
|
|
423
427
|
"next-themes": "^0.4.6",
|
|
424
428
|
openai: "^6.34.0",
|
|
@@ -443,16 +447,20 @@ var init_package = __esm({
|
|
|
443
447
|
"@tailwindcss/cli": "^4",
|
|
444
448
|
"@tailwindcss/postcss": "^4",
|
|
445
449
|
"@types/estree": "^1.0.9",
|
|
450
|
+
"@types/jest": "^30.0.0",
|
|
446
451
|
"@types/node": "^20",
|
|
447
452
|
"@types/pdf-parse": "^1.1.5",
|
|
448
453
|
"@types/pg": "^8.20.0",
|
|
449
454
|
"@types/react": "^19.2.14",
|
|
450
455
|
"@types/react-dom": "^19.2.3",
|
|
456
|
+
axios: "^1.15.0",
|
|
451
457
|
dotenv: "^17.4.2",
|
|
452
458
|
eslint: "^9",
|
|
453
459
|
"eslint-config-next": "16.2.4",
|
|
460
|
+
jest: "^30.4.2",
|
|
454
461
|
next: "16.2.10",
|
|
455
462
|
tailwindcss: "^4",
|
|
463
|
+
"ts-jest": "^29.4.12",
|
|
456
464
|
tsup: "^8.5.1",
|
|
457
465
|
typescript: "^5"
|
|
458
466
|
}
|
|
@@ -495,7 +503,7 @@ var init_ConfigFetcher = __esm({
|
|
|
495
503
|
* PINECONE_API_KEY if available, or vector operations are handled server-side.
|
|
496
504
|
*/
|
|
497
505
|
static async fetchRemoteConfig(projectId, licenseKey) {
|
|
498
|
-
var
|
|
506
|
+
var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k;
|
|
499
507
|
const cacheKey = `${projectId}:${licenseKey || ""}`;
|
|
500
508
|
const now = Date.now();
|
|
501
509
|
const cached = this.cache.get(cacheKey);
|
|
@@ -540,7 +548,7 @@ var init_ConfigFetcher = __esm({
|
|
|
540
548
|
indexName: data.vectorDb.indexName || "retrivora-free",
|
|
541
549
|
provider: data.vectorDb.provider || "pinecone",
|
|
542
550
|
projectId: fetchedProjectId,
|
|
543
|
-
namespace: ((
|
|
551
|
+
namespace: ((_a2 = data.workspace) == null ? void 0 : _a2.namespace) || data.vectorDb.namespace || fetchedProjectId
|
|
544
552
|
},
|
|
545
553
|
embedding: {
|
|
546
554
|
provider: ((_b = data.embedding) == null ? void 0 : _b.provider) || "universal_rest",
|
|
@@ -597,16 +605,20 @@ var init_ConfigFetcher = __esm({
|
|
|
597
605
|
return config;
|
|
598
606
|
}
|
|
599
607
|
}
|
|
600
|
-
} catch (
|
|
608
|
+
} catch (err) {
|
|
609
|
+
console.warn(
|
|
610
|
+
`[ConfigFetcher] Failed to fetch config from ${baseUrl}:`,
|
|
611
|
+
err instanceof Error ? err.message : String(err)
|
|
612
|
+
);
|
|
601
613
|
}
|
|
602
614
|
}
|
|
603
615
|
return null;
|
|
604
616
|
}
|
|
605
617
|
/** Convenience: fetch only vector DB config (backwards compat). */
|
|
606
618
|
static async fetchRemoteVectorConfig(projectId, licenseKey) {
|
|
607
|
-
var
|
|
619
|
+
var _a2;
|
|
608
620
|
const config = await this.fetchRemoteConfig(projectId, licenseKey);
|
|
609
|
-
return (
|
|
621
|
+
return (_a2 = config == null ? void 0 : config.vectorDb) != null ? _a2 : null;
|
|
610
622
|
}
|
|
611
623
|
};
|
|
612
624
|
ConfigFetcher.cache = /* @__PURE__ */ new Map();
|
|
@@ -646,7 +658,7 @@ var init_PineconeProvider = __esm({
|
|
|
646
658
|
const errors = [];
|
|
647
659
|
const opts = config.options || {};
|
|
648
660
|
const hasLicenseKey = Boolean(
|
|
649
|
-
config.licenseKey || process.env.RETRIVORA_LICENSE_KEY
|
|
661
|
+
config.licenseKey || process.env.RETRIVORA_LICENSE_KEY
|
|
650
662
|
);
|
|
651
663
|
if (!opts.apiKey && !hasLicenseKey) {
|
|
652
664
|
errors.push({
|
|
@@ -663,7 +675,7 @@ var init_PineconeProvider = __esm({
|
|
|
663
675
|
static getHealthChecker() {
|
|
664
676
|
return {
|
|
665
677
|
async check(config) {
|
|
666
|
-
var
|
|
678
|
+
var _a2, _b;
|
|
667
679
|
const opts = config.options || {};
|
|
668
680
|
const indexName = config.indexName;
|
|
669
681
|
const timestamp = Date.now();
|
|
@@ -671,7 +683,7 @@ var init_PineconeProvider = __esm({
|
|
|
671
683
|
const { Pinecone: Pinecone2 } = await import("@pinecone-database/pinecone");
|
|
672
684
|
const client = new Pinecone2({ apiKey: opts.apiKey });
|
|
673
685
|
const indexes = await client.listIndexes();
|
|
674
|
-
const indexNames = (_b = (
|
|
686
|
+
const indexNames = (_b = (_a2 = indexes.indexes) == null ? void 0 : _a2.map((i) => i.name)) != null ? _b : [];
|
|
675
687
|
if (!indexNames.includes(indexName)) {
|
|
676
688
|
return {
|
|
677
689
|
healthy: false,
|
|
@@ -698,25 +710,13 @@ var init_PineconeProvider = __esm({
|
|
|
698
710
|
};
|
|
699
711
|
}
|
|
700
712
|
async initialize() {
|
|
701
|
-
var
|
|
713
|
+
var _a2, _b;
|
|
702
714
|
if (this.client) return;
|
|
703
715
|
let key = this.apiKey || process.env.PINECONE_API_KEY || "";
|
|
704
716
|
if (!key) {
|
|
705
|
-
const licenseKey = process.env.RETRIVORA_LICENSE_KEY
|
|
717
|
+
const licenseKey = process.env.RETRIVORA_LICENSE_KEY;
|
|
706
718
|
this.licenseKey = licenseKey;
|
|
707
|
-
const
|
|
708
|
-
if (!licenseKey) return void 0;
|
|
709
|
-
try {
|
|
710
|
-
const parts = licenseKey.split(".");
|
|
711
|
-
if (parts.length >= 2) {
|
|
712
|
-
const payload = JSON.parse(Buffer.from(parts[1], "base64url").toString("utf8"));
|
|
713
|
-
return payload == null ? void 0 : payload.projectId;
|
|
714
|
-
}
|
|
715
|
-
} catch (e) {
|
|
716
|
-
}
|
|
717
|
-
return void 0;
|
|
718
|
-
})();
|
|
719
|
-
const projectId = process.env.RAG_PROJECT_ID || process.env.NEXT_PUBLIC_RAG_PROJECT_ID || jwtProjectId || this.projectId || "default";
|
|
719
|
+
const projectId = process.env.RAG_PROJECT_ID || process.env.NEXT_PUBLIC_RAG_PROJECT_ID || this.projectId || "default";
|
|
720
720
|
this.projectId = projectId;
|
|
721
721
|
const remoteConfig = await ConfigFetcher.fetchRemoteVectorConfig(projectId, licenseKey);
|
|
722
722
|
if (remoteConfig) {
|
|
@@ -744,7 +744,7 @@ var init_PineconeProvider = __esm({
|
|
|
744
744
|
this.client = new import_pinecone.Pinecone({ apiKey: key });
|
|
745
745
|
try {
|
|
746
746
|
const indexes = await this.client.listIndexes();
|
|
747
|
-
const names = (_b = (
|
|
747
|
+
const names = (_b = (_a2 = indexes.indexes) == null ? void 0 : _a2.map((i) => i.name)) != null ? _b : [];
|
|
748
748
|
if (!names.includes(this.indexName)) {
|
|
749
749
|
console.warn(`[PineconeProvider] Target index "${this.indexName}" not listed. Proceeding with configured index name.`);
|
|
750
750
|
}
|
|
@@ -763,7 +763,7 @@ var init_PineconeProvider = __esm({
|
|
|
763
763
|
return namespace ? idx.namespace(namespace) : idx.namespace("");
|
|
764
764
|
}
|
|
765
765
|
async upsert(doc, namespace) {
|
|
766
|
-
var
|
|
766
|
+
var _a2, _b;
|
|
767
767
|
if (!this.client) {
|
|
768
768
|
if (!this.licenseKey) throw new Error("[PineconeProvider] Cannot execute vector operation: Missing API key and License key.");
|
|
769
769
|
const res = await fetch(`${this.getControlPlaneUrl()}/api/v1/vectors/upsert`, {
|
|
@@ -773,7 +773,7 @@ var init_PineconeProvider = __esm({
|
|
|
773
773
|
projectId: this.projectId,
|
|
774
774
|
indexName: this.indexName,
|
|
775
775
|
namespace: namespace || this.projectId,
|
|
776
|
-
records: [{ id: String(doc.id), values: doc.vector, metadata: __spreadValues({ content: doc.content }, (
|
|
776
|
+
records: [{ id: String(doc.id), values: doc.vector, metadata: __spreadValues({ content: doc.content }, (_a2 = doc.metadata) != null ? _a2 : {}) }]
|
|
777
777
|
})
|
|
778
778
|
});
|
|
779
779
|
if (!res.ok) throw new Error(`[PineconeProvider] Proxy upsert failed: ${await res.text()}`);
|
|
@@ -794,11 +794,11 @@ var init_PineconeProvider = __esm({
|
|
|
794
794
|
if (!this.licenseKey) throw new Error("[PineconeProvider] Cannot execute vector operation: Missing API key and License key.");
|
|
795
795
|
for (let i = 0; i < docs.length; i += BATCH) {
|
|
796
796
|
const records = docs.slice(i, i + BATCH).map((d) => {
|
|
797
|
-
var
|
|
797
|
+
var _a2;
|
|
798
798
|
return {
|
|
799
799
|
id: String(d.id),
|
|
800
800
|
values: d.vector,
|
|
801
|
-
metadata: __spreadValues({ content: d.content }, (
|
|
801
|
+
metadata: __spreadValues({ content: d.content }, (_a2 = d.metadata) != null ? _a2 : {})
|
|
802
802
|
};
|
|
803
803
|
});
|
|
804
804
|
const res = await fetch(`${this.getControlPlaneUrl()}/api/v1/vectors/upsert`, {
|
|
@@ -818,18 +818,18 @@ var init_PineconeProvider = __esm({
|
|
|
818
818
|
const targetIdx = await this.getActiveIndex(namespace);
|
|
819
819
|
for (let i = 0; i < docs.length; i += BATCH) {
|
|
820
820
|
const records = docs.slice(i, i + BATCH).map((d) => {
|
|
821
|
-
var
|
|
821
|
+
var _a2;
|
|
822
822
|
return {
|
|
823
823
|
id: String(d.id),
|
|
824
824
|
values: d.vector,
|
|
825
|
-
metadata: __spreadValues({ content: d.content }, (
|
|
825
|
+
metadata: __spreadValues({ content: d.content }, (_a2 = d.metadata) != null ? _a2 : {})
|
|
826
826
|
};
|
|
827
827
|
});
|
|
828
828
|
await targetIdx.upsert({ records });
|
|
829
829
|
}
|
|
830
830
|
}
|
|
831
831
|
async query(vector, topK, namespace, filter) {
|
|
832
|
-
var
|
|
832
|
+
var _a2, _b;
|
|
833
833
|
const pineconeFilter = this.sanitizeFilter(filter);
|
|
834
834
|
if (!this.client) {
|
|
835
835
|
if (!this.licenseKey) throw new Error("[PineconeProvider] Cannot execute vector operation: Missing API key and License key.");
|
|
@@ -847,11 +847,11 @@ var init_PineconeProvider = __esm({
|
|
|
847
847
|
});
|
|
848
848
|
if (!res.ok) throw new Error(`[PineconeProvider] Proxy query failed: ${await res.text()}`);
|
|
849
849
|
const data = await res.json();
|
|
850
|
-
return ((
|
|
851
|
-
var
|
|
850
|
+
return ((_a2 = data.matches) != null ? _a2 : []).map((m) => {
|
|
851
|
+
var _a3, _b2;
|
|
852
852
|
return {
|
|
853
853
|
id: m.id,
|
|
854
|
-
score: (
|
|
854
|
+
score: (_a3 = m.score) != null ? _a3 : 0,
|
|
855
855
|
content: ((_b2 = m.metadata) == null ? void 0 : _b2.content) || "",
|
|
856
856
|
metadata: m.metadata
|
|
857
857
|
};
|
|
@@ -864,10 +864,10 @@ var init_PineconeProvider = __esm({
|
|
|
864
864
|
includeMetadata: true
|
|
865
865
|
}, Object.keys(pineconeFilter).length > 0 ? { filter: pineconeFilter } : {}));
|
|
866
866
|
return ((_b = result.matches) != null ? _b : []).map((m) => {
|
|
867
|
-
var
|
|
867
|
+
var _a3, _b2;
|
|
868
868
|
return {
|
|
869
869
|
id: m.id,
|
|
870
|
-
score: (
|
|
870
|
+
score: (_a3 = m.score) != null ? _a3 : 0,
|
|
871
871
|
content: ((_b2 = m.metadata) == null ? void 0 : _b2.content) || "",
|
|
872
872
|
metadata: m.metadata
|
|
873
873
|
};
|
|
@@ -910,13 +910,13 @@ var init_PostgreSQLProvider = __esm({
|
|
|
910
910
|
init_BaseVectorProvider();
|
|
911
911
|
PostgreSQLProvider = class extends BaseVectorProvider {
|
|
912
912
|
constructor(config) {
|
|
913
|
-
var
|
|
913
|
+
var _a2;
|
|
914
914
|
super(config);
|
|
915
915
|
this.tableName = this.indexName.replace(/[^a-z0-9_]/gi, "_");
|
|
916
916
|
const opts = config.options;
|
|
917
917
|
if (!opts.connectionString) throw new Error("[PostgreSQLProvider] options.connectionString is required");
|
|
918
918
|
this.connectionString = opts.connectionString;
|
|
919
|
-
this.dimensions = (
|
|
919
|
+
this.dimensions = (_a2 = opts.dimensions) != null ? _a2 : 1536;
|
|
920
920
|
}
|
|
921
921
|
static getValidator() {
|
|
922
922
|
return {
|
|
@@ -997,7 +997,7 @@ var init_PostgreSQLProvider = __esm({
|
|
|
997
997
|
}
|
|
998
998
|
}
|
|
999
999
|
async upsert(doc, namespace = "") {
|
|
1000
|
-
var
|
|
1000
|
+
var _a2;
|
|
1001
1001
|
const vectorLiteral = `[${doc.vector.join(",")}]`;
|
|
1002
1002
|
await this.pool.query(
|
|
1003
1003
|
`INSERT INTO ${this.tableName} (id, namespace, content, metadata, embedding)
|
|
@@ -1007,7 +1007,7 @@ var init_PostgreSQLProvider = __esm({
|
|
|
1007
1007
|
content = EXCLUDED.content,
|
|
1008
1008
|
metadata = EXCLUDED.metadata,
|
|
1009
1009
|
embedding = EXCLUDED.embedding`,
|
|
1010
|
-
[doc.id, namespace, doc.content, JSON.stringify((
|
|
1010
|
+
[doc.id, namespace, doc.content, JSON.stringify((_a2 = doc.metadata) != null ? _a2 : {}), vectorLiteral]
|
|
1011
1011
|
);
|
|
1012
1012
|
}
|
|
1013
1013
|
async batchUpsert(docs, namespace = "") {
|
|
@@ -1020,9 +1020,9 @@ var init_PostgreSQLProvider = __esm({
|
|
|
1020
1020
|
const batch = docs.slice(i, i + BATCH_SIZE);
|
|
1021
1021
|
const values = [];
|
|
1022
1022
|
const valuePlaceholders = batch.map((doc, idx) => {
|
|
1023
|
-
var
|
|
1023
|
+
var _a2;
|
|
1024
1024
|
const offset = idx * 5;
|
|
1025
|
-
values.push(doc.id, namespace, doc.content, JSON.stringify((
|
|
1025
|
+
values.push(doc.id, namespace, doc.content, JSON.stringify((_a2 = doc.metadata) != null ? _a2 : {}), `[${doc.vector.join(",")}]`);
|
|
1026
1026
|
return `($${offset + 1}, $${offset + 2}, $${offset + 3}, $${offset + 4}, $${offset + 5}::vector)`;
|
|
1027
1027
|
}).join(", ");
|
|
1028
1028
|
const query = `
|
|
@@ -1105,8 +1105,8 @@ var init_PostgreSQLProvider = __esm({
|
|
|
1105
1105
|
|
|
1106
1106
|
// src/utils/synonyms.ts
|
|
1107
1107
|
function resolveMetadataValue(meta, uiKey) {
|
|
1108
|
-
var
|
|
1109
|
-
const synonyms = (
|
|
1108
|
+
var _a2;
|
|
1109
|
+
const synonyms = (_a2 = FIELD_SYNONYMS[uiKey]) != null ? _a2 : [];
|
|
1110
1110
|
const keys = Object.keys(meta);
|
|
1111
1111
|
const systemKeys = ["namespace", "filename", "filesize", "filetype", "docid", "uploadedat", "dimension", "chunkindex"];
|
|
1112
1112
|
let match = keys.find((k) => k.toLowerCase() === uiKey.toLowerCase());
|
|
@@ -1193,14 +1193,14 @@ var init_MultiTablePostgresProvider = __esm({
|
|
|
1193
1193
|
init_synonyms();
|
|
1194
1194
|
MultiTablePostgresProvider = class extends BaseVectorProvider {
|
|
1195
1195
|
constructor(config) {
|
|
1196
|
-
var
|
|
1196
|
+
var _a2, _b, _c;
|
|
1197
1197
|
super(config);
|
|
1198
1198
|
const opts = config.options || {};
|
|
1199
1199
|
if (!opts.connectionString) {
|
|
1200
1200
|
throw new Error("[MultiTablePostgresProvider] options.connectionString is required");
|
|
1201
1201
|
}
|
|
1202
1202
|
this.connectionString = opts.connectionString;
|
|
1203
|
-
this.dimensions = (
|
|
1203
|
+
this.dimensions = (_a2 = opts.dimensions) != null ? _a2 : 768;
|
|
1204
1204
|
this.tables = [];
|
|
1205
1205
|
const rawSearchFields = (_c = (_b = opts.searchFields) != null ? _b : process.env.VECTOR_DB_SEARCH_FIELDS) != null ? _c : ["name", "product_name", "productname", "title"];
|
|
1206
1206
|
this.searchFields = typeof rawSearchFields === "string" ? rawSearchFields.split(",").map((f) => f.trim()).filter(Boolean) : rawSearchFields;
|
|
@@ -1239,7 +1239,6 @@ var init_MultiTablePostgresProvider = __esm({
|
|
|
1239
1239
|
this.tables = res.rows.map((r) => r.table_name);
|
|
1240
1240
|
if (this.tables.length === 0) {
|
|
1241
1241
|
console.warn('[MultiTablePostgresProvider] No tables with "embedding" columns found in the database.');
|
|
1242
|
-
} else {
|
|
1243
1242
|
}
|
|
1244
1243
|
} finally {
|
|
1245
1244
|
client.release();
|
|
@@ -1255,11 +1254,11 @@ var init_MultiTablePostgresProvider = __esm({
|
|
|
1255
1254
|
* Batch upsert documents by dynamically provisioning tables and columns based on CSV headers.
|
|
1256
1255
|
*/
|
|
1257
1256
|
async batchUpsert(docs, namespace = "") {
|
|
1258
|
-
var
|
|
1257
|
+
var _a2;
|
|
1259
1258
|
if (docs.length === 0) return;
|
|
1260
1259
|
const docsByFile = {};
|
|
1261
1260
|
for (const doc of docs) {
|
|
1262
|
-
const fileName = ((
|
|
1261
|
+
const fileName = ((_a2 = doc.metadata) == null ? void 0 : _a2.fileName) || this.uploadTable;
|
|
1263
1262
|
if (!docsByFile[fileName]) docsByFile[fileName] = [];
|
|
1264
1263
|
docsByFile[fileName].push(doc);
|
|
1265
1264
|
}
|
|
@@ -1312,11 +1311,11 @@ var init_MultiTablePostgresProvider = __esm({
|
|
|
1312
1311
|
const headerColumns = csvHeaders.map((h) => `"${h}"`).join(", ");
|
|
1313
1312
|
const allColumns = `id, ${headerColumns ? headerColumns + ", " : ""}namespace, content, metadata, embedding`;
|
|
1314
1313
|
const valuePlaceholders = batch.map((doc, idx) => {
|
|
1315
|
-
var
|
|
1314
|
+
var _a3, _b;
|
|
1316
1315
|
const offset = idx * (5 + csvHeaders.length);
|
|
1317
1316
|
values.push(doc.id);
|
|
1318
1317
|
for (const h of csvHeaders) {
|
|
1319
|
-
values.push(String(((
|
|
1318
|
+
values.push(String(((_a3 = doc.metadata) == null ? void 0 : _a3[h]) || ""));
|
|
1320
1319
|
}
|
|
1321
1320
|
values.push(namespace, doc.content, JSON.stringify((_b = doc.metadata) != null ? _b : {}), `[${doc.vector.join(",")}]`);
|
|
1322
1321
|
const docPlaceholders = [];
|
|
@@ -1355,7 +1354,7 @@ var init_MultiTablePostgresProvider = __esm({
|
|
|
1355
1354
|
* Query all configured tables and merge results, sorted by cosine similarity score.
|
|
1356
1355
|
*/
|
|
1357
1356
|
async query(vector, topK, _namespace, _filter) {
|
|
1358
|
-
var
|
|
1357
|
+
var _a2;
|
|
1359
1358
|
if (!this.pool) {
|
|
1360
1359
|
throw new Error("[MultiTablePostgresProvider] Provider not initialized. Call initialize() first.");
|
|
1361
1360
|
}
|
|
@@ -1383,11 +1382,11 @@ var init_MultiTablePostgresProvider = __esm({
|
|
|
1383
1382
|
const filterParams = [];
|
|
1384
1383
|
if (metadataFilters && Object.keys(metadataFilters).length > 0) {
|
|
1385
1384
|
const conditions = Object.entries(metadataFilters).map(([key, val]) => {
|
|
1386
|
-
var
|
|
1385
|
+
var _a4;
|
|
1387
1386
|
filterParams.push(String(val));
|
|
1388
1387
|
const baseOffset = queryText && dynamicKeywordQuery ? 2 : 1;
|
|
1389
1388
|
const paramIdx = baseOffset + filterParams.length;
|
|
1390
|
-
const synonyms = (
|
|
1389
|
+
const synonyms = (_a4 = FIELD_SYNONYMS[key]) != null ? _a4 : [];
|
|
1391
1390
|
const keysToCheck = [key, ...synonyms];
|
|
1392
1391
|
const coalesceExprs = keysToCheck.flatMap((k) => [
|
|
1393
1392
|
`metadata->>'${k}'`,
|
|
@@ -1454,7 +1453,7 @@ var init_MultiTablePostgresProvider = __esm({
|
|
|
1454
1453
|
}
|
|
1455
1454
|
const tableResults = [];
|
|
1456
1455
|
for (const row of result.rows) {
|
|
1457
|
-
const
|
|
1456
|
+
const _a3 = row, { hybrid_score, id } = _a3, rest = __objRest(_a3, ["hybrid_score", "id"]);
|
|
1458
1457
|
delete rest.embedding;
|
|
1459
1458
|
delete rest.vector_score;
|
|
1460
1459
|
delete rest.keyword_score;
|
|
@@ -1483,10 +1482,10 @@ var init_MultiTablePostgresProvider = __esm({
|
|
|
1483
1482
|
}
|
|
1484
1483
|
allResults.sort((a, b) => b.score - a.score);
|
|
1485
1484
|
if (allResults.length === 0) return [];
|
|
1486
|
-
const bestMatchTable = (
|
|
1485
|
+
const bestMatchTable = (_a2 = allResults[0].metadata) == null ? void 0 : _a2.source_table;
|
|
1487
1486
|
const finalSorted = allResults.filter((res) => {
|
|
1488
|
-
var
|
|
1489
|
-
return ((
|
|
1487
|
+
var _a3;
|
|
1488
|
+
return ((_a3 = res.metadata) == null ? void 0 : _a3.source_table) === bestMatchTable;
|
|
1490
1489
|
});
|
|
1491
1490
|
return finalSorted.slice(0, Math.max(topK, 15));
|
|
1492
1491
|
}
|
|
@@ -1884,14 +1883,14 @@ var init_QdrantProvider = __esm({
|
|
|
1884
1883
|
* Samples points from the collection to discover available payload fields.
|
|
1885
1884
|
*/
|
|
1886
1885
|
async discoverSchema() {
|
|
1887
|
-
var
|
|
1886
|
+
var _a2;
|
|
1888
1887
|
try {
|
|
1889
1888
|
const { data } = await this.http.post(`/collections/${this.indexName}/points/scroll`, {
|
|
1890
1889
|
limit: 20,
|
|
1891
1890
|
with_payload: true,
|
|
1892
1891
|
with_vector: false
|
|
1893
1892
|
});
|
|
1894
|
-
const points = ((
|
|
1893
|
+
const points = ((_a2 = data.result) == null ? void 0 : _a2.points) || [];
|
|
1895
1894
|
const keys = /* @__PURE__ */ new Set();
|
|
1896
1895
|
for (const point of points) {
|
|
1897
1896
|
const payload = point.payload || {};
|
|
@@ -1917,11 +1916,11 @@ var init_QdrantProvider = __esm({
|
|
|
1917
1916
|
* Ensures the collection exists. Creates it if missing.
|
|
1918
1917
|
*/
|
|
1919
1918
|
async ensureCollection() {
|
|
1920
|
-
var
|
|
1919
|
+
var _a2;
|
|
1921
1920
|
try {
|
|
1922
1921
|
await this.http.get(`/collections/${this.indexName}`);
|
|
1923
1922
|
} catch (err) {
|
|
1924
|
-
if (import_axios4.default.isAxiosError(err) && ((
|
|
1923
|
+
if (import_axios4.default.isAxiosError(err) && ((_a2 = err.response) == null ? void 0 : _a2.status) === 404) {
|
|
1925
1924
|
const opts = this.config.options;
|
|
1926
1925
|
const dimensionsForCreate = opts.dimensions || 1536;
|
|
1927
1926
|
await this.http.put(`/collections/${this.indexName}`, {
|
|
@@ -1939,7 +1938,7 @@ var init_QdrantProvider = __esm({
|
|
|
1939
1938
|
* Ensures that a payload field has an index.
|
|
1940
1939
|
*/
|
|
1941
1940
|
async ensureIndex(fieldName, schema = "keyword") {
|
|
1942
|
-
var
|
|
1941
|
+
var _a2;
|
|
1943
1942
|
let fullPath = fieldName;
|
|
1944
1943
|
if (!this.isFlatPayload && !fieldName.includes(".") && fieldName !== "namespace" && fieldName !== this.contentField) {
|
|
1945
1944
|
fullPath = `${this.metadataField}.${fieldName}`;
|
|
@@ -1951,7 +1950,7 @@ var init_QdrantProvider = __esm({
|
|
|
1951
1950
|
});
|
|
1952
1951
|
} catch (err) {
|
|
1953
1952
|
let status;
|
|
1954
|
-
if (import_axios4.default.isAxiosError(err)) status = (
|
|
1953
|
+
if (import_axios4.default.isAxiosError(err)) status = (_a2 = err.response) == null ? void 0 : _a2.status;
|
|
1955
1954
|
if (status === 409) return;
|
|
1956
1955
|
console.warn(`[QdrantProvider] \u26A0\uFE0F Could not ensure index for "${fullPath}":`, err instanceof Error ? err.message : String(err));
|
|
1957
1956
|
}
|
|
@@ -1980,7 +1979,7 @@ var init_QdrantProvider = __esm({
|
|
|
1980
1979
|
await this.http.put(`/collections/${this.indexName}/points`, payload);
|
|
1981
1980
|
}
|
|
1982
1981
|
async query(vector, topK, namespace, _filter) {
|
|
1983
|
-
var
|
|
1982
|
+
var _a2;
|
|
1984
1983
|
const must = [];
|
|
1985
1984
|
if (namespace) {
|
|
1986
1985
|
must.push({ key: "namespace", match: { value: namespace } });
|
|
@@ -2002,7 +2001,7 @@ var init_QdrantProvider = __esm({
|
|
|
2002
2001
|
limit: topK,
|
|
2003
2002
|
with_payload: true,
|
|
2004
2003
|
params: {
|
|
2005
|
-
hnsw_ef: ((
|
|
2004
|
+
hnsw_ef: ((_a2 = this.config.options) == null ? void 0 : _a2.efSearch) || Math.max(topK * 20, 128),
|
|
2006
2005
|
exact: false
|
|
2007
2006
|
},
|
|
2008
2007
|
filter: must.length > 0 ? { must } : void 0
|
|
@@ -2097,12 +2096,12 @@ var init_ChromaDBProvider = __esm({
|
|
|
2097
2096
|
* Get or create the ChromaDB collection.
|
|
2098
2097
|
*/
|
|
2099
2098
|
async initialize() {
|
|
2100
|
-
var
|
|
2099
|
+
var _a2;
|
|
2101
2100
|
try {
|
|
2102
2101
|
const { data } = await this.http.get(`/api/v1/collections/${this.indexName}`);
|
|
2103
2102
|
this.collectionId = data.id;
|
|
2104
2103
|
} catch (err) {
|
|
2105
|
-
if (import_axios5.default.isAxiosError(err) && ((
|
|
2104
|
+
if (import_axios5.default.isAxiosError(err) && ((_a2 = err.response) == null ? void 0 : _a2.status) === 404) {
|
|
2106
2105
|
const { data } = await this.http.post("/api/v1/collections", {
|
|
2107
2106
|
name: this.indexName
|
|
2108
2107
|
});
|
|
@@ -2320,7 +2319,7 @@ var init_WeaviateProvider = __esm({
|
|
|
2320
2319
|
await this.http.post("/v1/batch/objects", payload);
|
|
2321
2320
|
}
|
|
2322
2321
|
async query(vector, topK, namespace, _filter) {
|
|
2323
|
-
var
|
|
2322
|
+
var _a2, _b;
|
|
2324
2323
|
const queryText = _filter == null ? void 0 : _filter.queryText;
|
|
2325
2324
|
const sanitizedFilter = this.sanitizeFilter(_filter);
|
|
2326
2325
|
const searchParams = queryText ? `hybrid: { query: ${JSON.stringify(queryText)}, alpha: 0.5 }` : `nearVector: { vector: ${JSON.stringify(vector)} }`;
|
|
@@ -2346,7 +2345,7 @@ var init_WeaviateProvider = __esm({
|
|
|
2346
2345
|
`
|
|
2347
2346
|
};
|
|
2348
2347
|
const { data } = await this.http.post("/v1/graphql", graphqlQuery);
|
|
2349
|
-
const results = ((_b = (
|
|
2348
|
+
const results = ((_b = (_a2 = data.data) == null ? void 0 : _a2.Get) == null ? void 0 : _b[this.indexName]) || [];
|
|
2350
2349
|
return results.map((res) => ({
|
|
2351
2350
|
id: res["_additional"].id,
|
|
2352
2351
|
score: 1 - res["_additional"].distance,
|
|
@@ -2428,21 +2427,21 @@ var init_UniversalVectorProvider = __esm({
|
|
|
2428
2427
|
}
|
|
2429
2428
|
}
|
|
2430
2429
|
async initialize() {
|
|
2431
|
-
var
|
|
2430
|
+
var _a2;
|
|
2432
2431
|
this.http = import_axios8.default.create({
|
|
2433
2432
|
baseURL: this.opts.baseUrl,
|
|
2434
2433
|
headers: __spreadValues({
|
|
2435
2434
|
"Content-Type": "application/json"
|
|
2436
2435
|
}, this.opts.headers),
|
|
2437
|
-
timeout: (
|
|
2436
|
+
timeout: (_a2 = this.opts.timeout) != null ? _a2 : 3e4
|
|
2438
2437
|
});
|
|
2439
2438
|
if (!await this.ping()) {
|
|
2440
2439
|
throw new Error(`[UniversalVectorProvider] Failed to connect to ${this.opts.baseUrl}`);
|
|
2441
2440
|
}
|
|
2442
2441
|
}
|
|
2443
2442
|
async upsert(doc, namespace) {
|
|
2444
|
-
var
|
|
2445
|
-
const endpoint = (
|
|
2443
|
+
var _a2, _b, _c;
|
|
2444
|
+
const endpoint = (_a2 = this.opts.upsertEndpoint) != null ? _a2 : "/upsert";
|
|
2446
2445
|
const template = (_b = this.opts.upsertTemplate) != null ? _b : JSON.stringify({
|
|
2447
2446
|
id: "{{id}}",
|
|
2448
2447
|
vector: "{{vector}}",
|
|
@@ -2475,8 +2474,8 @@ var init_UniversalVectorProvider = __esm({
|
|
|
2475
2474
|
}
|
|
2476
2475
|
}
|
|
2477
2476
|
async query(vector, topK, namespace, filter) {
|
|
2478
|
-
var
|
|
2479
|
-
const endpoint = (
|
|
2477
|
+
var _a2, _b;
|
|
2478
|
+
const endpoint = (_a2 = this.opts.queryEndpoint) != null ? _a2 : "/query";
|
|
2480
2479
|
const template = (_b = this.opts.queryTemplate) != null ? _b : JSON.stringify({
|
|
2481
2480
|
vector: "{{vector}}",
|
|
2482
2481
|
limit: "{{topK}}",
|
|
@@ -2502,9 +2501,9 @@ var init_UniversalVectorProvider = __esm({
|
|
|
2502
2501
|
);
|
|
2503
2502
|
}
|
|
2504
2503
|
return results.map((item) => {
|
|
2505
|
-
var
|
|
2504
|
+
var _a3, _b2, _c, _d, _e, _f, _g2;
|
|
2506
2505
|
return {
|
|
2507
|
-
id: item[(
|
|
2506
|
+
id: item[(_a3 = this.opts.queryIdField) != null ? _a3 : "id"],
|
|
2508
2507
|
score: (_c = item[(_b2 = this.opts.queryScoreField) != null ? _b2 : "score"]) != null ? _c : 0,
|
|
2509
2508
|
content: (_e = item[(_d = this.opts.queryContentField) != null ? _d : "content"]) != null ? _e : "",
|
|
2510
2509
|
metadata: (_g2 = item[(_f = this.opts.queryMetadataField) != null ? _f : "metadata"]) != null ? _g2 : {}
|
|
@@ -2517,8 +2516,8 @@ var init_UniversalVectorProvider = __esm({
|
|
|
2517
2516
|
}
|
|
2518
2517
|
}
|
|
2519
2518
|
async delete(id, namespace) {
|
|
2520
|
-
var
|
|
2521
|
-
const endpoint = (
|
|
2519
|
+
var _a2, _b;
|
|
2520
|
+
const endpoint = (_a2 = this.opts.deleteEndpoint) != null ? _a2 : "/delete";
|
|
2522
2521
|
const template = (_b = this.opts.deleteTemplate) != null ? _b : JSON.stringify({
|
|
2523
2522
|
id: "{{id}}",
|
|
2524
2523
|
namespace: "{{namespace}}"
|
|
@@ -2536,8 +2535,8 @@ var init_UniversalVectorProvider = __esm({
|
|
|
2536
2535
|
}
|
|
2537
2536
|
}
|
|
2538
2537
|
async deleteNamespace(namespace) {
|
|
2539
|
-
var
|
|
2540
|
-
const endpoint = (
|
|
2538
|
+
var _a2;
|
|
2539
|
+
const endpoint = (_a2 = this.opts.deleteNamespaceEndpoint) != null ? _a2 : "/delete-namespace";
|
|
2541
2540
|
try {
|
|
2542
2541
|
await this.http.post(endpoint, { namespace });
|
|
2543
2542
|
} catch (error) {
|
|
@@ -2547,9 +2546,9 @@ var init_UniversalVectorProvider = __esm({
|
|
|
2547
2546
|
}
|
|
2548
2547
|
}
|
|
2549
2548
|
async ping() {
|
|
2550
|
-
var
|
|
2549
|
+
var _a2;
|
|
2551
2550
|
try {
|
|
2552
|
-
const endpoint = (
|
|
2551
|
+
const endpoint = (_a2 = this.opts.pingEndpoint) != null ? _a2 : "/health";
|
|
2553
2552
|
const response = await this.http.get(endpoint, { timeout: 5e3 });
|
|
2554
2553
|
return response.status >= 200 && response.status < 300;
|
|
2555
2554
|
} catch (e) {
|
|
@@ -2703,13 +2702,13 @@ var AuthenticationException = class extends RetrivoraError {
|
|
|
2703
2702
|
}
|
|
2704
2703
|
};
|
|
2705
2704
|
function wrapError(err, defaultCode, defaultMessage) {
|
|
2706
|
-
var
|
|
2705
|
+
var _a2;
|
|
2707
2706
|
if (err instanceof RetrivoraError) {
|
|
2708
2707
|
return err;
|
|
2709
2708
|
}
|
|
2710
2709
|
const error = err;
|
|
2711
2710
|
const message = (error == null ? void 0 : error.message) || defaultMessage || String(err);
|
|
2712
|
-
const status = (error == null ? void 0 : error.status) || (error == null ? void 0 : error.statusCode) || ((
|
|
2711
|
+
const status = (error == null ? void 0 : error.status) || (error == null ? void 0 : error.statusCode) || ((_a2 = error == null ? void 0 : error.response) == null ? void 0 : _a2.status);
|
|
2713
2712
|
const code = error == null ? void 0 : error.code;
|
|
2714
2713
|
if (status === 429 || /rate[- ]?limit/i.test(message) || code === "RATE_LIMIT_EXCEEDED") {
|
|
2715
2714
|
return new RateLimitException(message, err);
|
|
@@ -2990,8 +2989,8 @@ var UI_BORDER_RADIUS_OPTIONS = ["none", "sm", "md", "lg", "xl", "full"];
|
|
|
2990
2989
|
// src/config/serverConfig.ts
|
|
2991
2990
|
var _runtimeConfig = null;
|
|
2992
2991
|
function readString(env, name) {
|
|
2993
|
-
var
|
|
2994
|
-
const value = (
|
|
2992
|
+
var _a2;
|
|
2993
|
+
const value = (_a2 = env[name]) == null ? void 0 : _a2.trim();
|
|
2995
2994
|
return value ? value : void 0;
|
|
2996
2995
|
}
|
|
2997
2996
|
function readNumber(env, name, fallback) {
|
|
@@ -3004,66 +3003,60 @@ function readNumber(env, name, fallback) {
|
|
|
3004
3003
|
return parsed;
|
|
3005
3004
|
}
|
|
3006
3005
|
function readEnum(env, name, fallback, allowed) {
|
|
3007
|
-
var
|
|
3008
|
-
const value = (
|
|
3006
|
+
var _a2;
|
|
3007
|
+
const value = (_a2 = readString(env, name)) != null ? _a2 : fallback;
|
|
3009
3008
|
if (allowed.includes(value)) {
|
|
3010
3009
|
return value;
|
|
3011
3010
|
}
|
|
3012
3011
|
throw new Error(`[getRagConfig] ${name} must be one of: ${allowed.join(", ")}`);
|
|
3013
3012
|
}
|
|
3014
3013
|
function getEnvConfig(env = process.env, base) {
|
|
3015
|
-
var
|
|
3016
|
-
const licenseKey = (
|
|
3017
|
-
|
|
3018
|
-
|
|
3019
|
-
|
|
3020
|
-
|
|
3021
|
-
|
|
3022
|
-
|
|
3023
|
-
|
|
3024
|
-
|
|
3025
|
-
} catch (e) {
|
|
3026
|
-
}
|
|
3027
|
-
return void 0;
|
|
3028
|
-
})();
|
|
3029
|
-
const projectId = (_i = (_h = (_g2 = (_f = (_e = readString(env, "RAG_PROJECT_ID")) != null ? _e : readString(env, "NEXT_PUBLIC_RAG_PROJECT_ID")) != null ? _f : readString(env, "NEXT_PUBLIC_PROJECT_ID")) != null ? _g2 : base == null ? void 0 : base.projectId) != null ? _h : jwtProjectId) != null ? _i : "default";
|
|
3030
|
-
const telemetryEnabled = readString(env, "TELEMETRY_ENABLED") === "true" || readString(env, "NEXT_PUBLIC_TELEMETRY_ENABLED") === "true" || ((_j = base == null ? void 0 : base.telemetry) == null ? void 0 : _j.enabled) || Boolean(licenseKey);
|
|
3014
|
+
var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _A, _B, _C, _D, _E, _F, _G, _H, _I, _J, _K, _L, _M, _N, _O, _P, _Q, _R, _S, _T, _U, _V, _W, _X, _Y, _Z, __, _$, _aa, _ba, _ca, _da, _ea, _fa, _ga, _ha, _ia, _ja, _ka, _la, _ma, _na, _oa, _pa, _qa, _ra, _sa, _ta, _ua, _va, _wa, _xa, _ya, _za, _Aa, _Ba, _Ca, _Da, _Ea, _Fa, _Ga, _Ha, _Ia, _Ja, _Ka, _La, _Ma, _Na, _Oa, _Pa, _Qa, _Ra, _Sa, _Ta, _Ua, _Va, _Wa, _Xa, _Ya, _Za, __a, _$a, _ab, _bb, _cb, _db, _eb, _fb, _gb, _hb, _ib, _jb, _kb, _lb, _mb, _nb, _ob, _pb, _qb, _rb, _sb, _tb, _ub, _vb, _wb, _xb, _yb, _zb, _Ab, _Bb, _Cb, _Db, _Eb, _Fb, _Gb, _Hb, _Ib, _Jb;
|
|
3015
|
+
const licenseKey = (_c = (_b = (_a2 = readString(env, "RAG_LICENSE_KEY")) != null ? _a2 : readString(env, "RETRIVORA_LICENSE_KEY")) != null ? _b : (
|
|
3016
|
+
// Fix SEC-4: NEXT_PUBLIC_RETRIVORA_LICENSE_KEY is NOT included here.
|
|
3017
|
+
// That variable is inlined into the browser bundle by Next.js at build time
|
|
3018
|
+
// and must never be used as a server-side secret source. Developers who
|
|
3019
|
+
// previously relied on this should migrate to RETRIVORA_LICENSE_KEY.
|
|
3020
|
+
readString(env, "LICENSE_KEY")
|
|
3021
|
+
)) != null ? _c : base == null ? void 0 : base.licenseKey;
|
|
3022
|
+
const projectId = (_g2 = (_f = (_e = (_d = readString(env, "RAG_PROJECT_ID")) != null ? _d : readString(env, "NEXT_PUBLIC_RAG_PROJECT_ID")) != null ? _e : readString(env, "NEXT_PUBLIC_PROJECT_ID")) != null ? _f : base == null ? void 0 : base.projectId) != null ? _g2 : "default";
|
|
3023
|
+
const telemetryEnabled = readString(env, "TELEMETRY_ENABLED") === "true" || readString(env, "NEXT_PUBLIC_TELEMETRY_ENABLED") === "true" || ((_h = base == null ? void 0 : base.telemetry) == null ? void 0 : _h.enabled) || Boolean(licenseKey);
|
|
3031
3024
|
const vectorProvider = readEnum(env, "VECTOR_DB_PROVIDER", "pinecone", VECTOR_DB_PROVIDERS);
|
|
3032
3025
|
const embeddingDimensions = readNumber(env, "EMBEDDING_DIMENSIONS", 768);
|
|
3033
3026
|
const vectorDbOptions = {};
|
|
3034
3027
|
if (vectorProvider === "pinecone") {
|
|
3035
|
-
vectorDbOptions.apiKey = (
|
|
3036
|
-
vectorDbOptions.indexName = (
|
|
3028
|
+
vectorDbOptions.apiKey = (_l = (_k = readString(env, "PINECONE_API_KEY")) != null ? _k : (_j = (_i = base == null ? void 0 : base.vectorDb) == null ? void 0 : _i.options) == null ? void 0 : _j.apiKey) != null ? _l : "";
|
|
3029
|
+
vectorDbOptions.indexName = (_r = (_q = (_n = readString(env, "PINECONE_INDEX")) != null ? _n : (_m = base == null ? void 0 : base.vectorDb) == null ? void 0 : _m.indexName) != null ? _q : (_p = (_o = base == null ? void 0 : base.vectorDb) == null ? void 0 : _o.options) == null ? void 0 : _p.indexName) != null ? _r : "retrivora-free";
|
|
3037
3030
|
} else if (vectorProvider === "pgvector" || vectorProvider === "postgresql") {
|
|
3038
|
-
vectorDbOptions.connectionString = (
|
|
3039
|
-
vectorDbOptions.tables = (
|
|
3040
|
-
vectorDbOptions.searchFields = (
|
|
3031
|
+
vectorDbOptions.connectionString = (_w = (_v = (_s = readString(env, "PGVECTOR_CONNECTION_STRING")) != null ? _s : readString(env, "POSTGRES_URL")) != null ? _v : (_u = (_t = base == null ? void 0 : base.vectorDb) == null ? void 0 : _t.options) == null ? void 0 : _u.connectionString) != null ? _w : "";
|
|
3032
|
+
vectorDbOptions.tables = (_B = (_y = (_x = readString(env, "VECTOR_DB_TABLES")) != null ? _x : readString(env, "POSTGRES_TABLES")) == null ? void 0 : _y.split(",").map((t) => t.trim())) != null ? _B : (_A = (_z = base == null ? void 0 : base.vectorDb) == null ? void 0 : _z.options) == null ? void 0 : _A.tables;
|
|
3033
|
+
vectorDbOptions.searchFields = (_F = (_C = readString(env, "POSTGRES_SEARCH_FIELDS")) == null ? void 0 : _C.split(",").map((f) => f.trim())) != null ? _F : (_E = (_D = base == null ? void 0 : base.vectorDb) == null ? void 0 : _D.options) == null ? void 0 : _E.searchFields;
|
|
3041
3034
|
vectorDbOptions.dimensions = embeddingDimensions;
|
|
3042
3035
|
} else if (vectorProvider === "mongodb") {
|
|
3043
|
-
vectorDbOptions.uri = (
|
|
3044
|
-
vectorDbOptions.database = (
|
|
3045
|
-
vectorDbOptions.collection = (
|
|
3046
|
-
vectorDbOptions.indexName = (
|
|
3036
|
+
vectorDbOptions.uri = (_J = (_I = readString(env, "MONGODB_URI")) != null ? _I : (_H = (_G = base == null ? void 0 : base.vectorDb) == null ? void 0 : _G.options) == null ? void 0 : _H.uri) != null ? _J : "";
|
|
3037
|
+
vectorDbOptions.database = (_N = (_M = readString(env, "MONGODB_DB")) != null ? _M : (_L = (_K = base == null ? void 0 : base.vectorDb) == null ? void 0 : _K.options) == null ? void 0 : _L.database) != null ? _N : "";
|
|
3038
|
+
vectorDbOptions.collection = (_R = (_Q = readString(env, "MONGODB_COLLECTION")) != null ? _Q : (_P = (_O = base == null ? void 0 : base.vectorDb) == null ? void 0 : _O.options) == null ? void 0 : _P.collection) != null ? _R : "";
|
|
3039
|
+
vectorDbOptions.indexName = (_X = (_W = (_T = readString(env, "MONGODB_INDEX_NAME")) != null ? _T : (_S = base == null ? void 0 : base.vectorDb) == null ? void 0 : _S.indexName) != null ? _W : (_V = (_U = base == null ? void 0 : base.vectorDb) == null ? void 0 : _U.options) == null ? void 0 : _V.indexName) != null ? _X : "vector_index";
|
|
3047
3040
|
} else if (vectorProvider === "qdrant") {
|
|
3048
|
-
vectorDbOptions.baseUrl = (
|
|
3049
|
-
vectorDbOptions.apiKey = (
|
|
3041
|
+
vectorDbOptions.baseUrl = (_$ = (__ = readString(env, "QDRANT_URL")) != null ? __ : (_Z = (_Y = base == null ? void 0 : base.vectorDb) == null ? void 0 : _Y.options) == null ? void 0 : _Z.baseUrl) != null ? _$ : "http://localhost:6333";
|
|
3042
|
+
vectorDbOptions.apiKey = (_ca = readString(env, "QDRANT_API_KEY")) != null ? _ca : (_ba = (_aa = base == null ? void 0 : base.vectorDb) == null ? void 0 : _aa.options) == null ? void 0 : _ba.apiKey;
|
|
3050
3043
|
vectorDbOptions.dimensions = embeddingDimensions;
|
|
3051
3044
|
} else if (vectorProvider === "milvus") {
|
|
3052
|
-
vectorDbOptions.baseUrl = (
|
|
3045
|
+
vectorDbOptions.baseUrl = (_da = readString(env, "MILVUS_URL")) != null ? _da : "http://localhost:19530";
|
|
3053
3046
|
vectorDbOptions.apiKey = readString(env, "MILVUS_API_KEY");
|
|
3054
3047
|
} else if (vectorProvider === "chromadb") {
|
|
3055
|
-
vectorDbOptions.baseUrl = (
|
|
3048
|
+
vectorDbOptions.baseUrl = (_ea = readString(env, "CHROMADB_URL")) != null ? _ea : "http://localhost:8000";
|
|
3056
3049
|
} else if (vectorProvider === "weaviate") {
|
|
3057
|
-
vectorDbOptions.baseUrl = (
|
|
3050
|
+
vectorDbOptions.baseUrl = (_fa = readString(env, "WEAVIATE_URL")) != null ? _fa : "http://localhost:8080";
|
|
3058
3051
|
vectorDbOptions.apiKey = readString(env, "WEAVIATE_API_KEY");
|
|
3059
3052
|
} else if (vectorProvider === "redis") {
|
|
3060
|
-
vectorDbOptions.baseUrl = (
|
|
3053
|
+
vectorDbOptions.baseUrl = (_ga = readString(env, "REDIS_URL")) != null ? _ga : "";
|
|
3061
3054
|
vectorDbOptions.apiKey = readString(env, "REDIS_API_KEY");
|
|
3062
3055
|
} else if (vectorProvider === "rest") {
|
|
3063
|
-
vectorDbOptions.baseUrl = (
|
|
3056
|
+
vectorDbOptions.baseUrl = (_ha = readString(env, "VECTOR_DB_REST_URL")) != null ? _ha : "";
|
|
3064
3057
|
vectorDbOptions.headers = readString(env, "VECTOR_DB_REST_API_KEY") ? { "api-key": readString(env, "VECTOR_DB_REST_API_KEY") } : {};
|
|
3065
3058
|
} else if (vectorProvider === "universal_rest") {
|
|
3066
|
-
vectorDbOptions.baseUrl = (
|
|
3059
|
+
vectorDbOptions.baseUrl = (_ja = (_ia = readString(env, "VECTOR_BASE_URL")) != null ? _ia : readString(env, "VECTOR_DB_REST_URL")) != null ? _ja : "";
|
|
3067
3060
|
vectorDbOptions.profile = readString(env, "VECTOR_UNIVERSAL_PROFILE");
|
|
3068
3061
|
vectorDbOptions.headers = readString(env, "VECTOR_DB_REST_API_KEY") ? { Authorization: `Bearer ${readString(env, "VECTOR_DB_REST_API_KEY")}` } : {};
|
|
3069
3062
|
}
|
|
@@ -3082,8 +3075,8 @@ function getEnvConfig(env = process.env, base) {
|
|
|
3082
3075
|
// Anthropic needs a separate embedding provider; key kept for completeness
|
|
3083
3076
|
gemini: readString(env, "GEMINI_API_KEY"),
|
|
3084
3077
|
ollama: void 0,
|
|
3085
|
-
universal_rest: (
|
|
3086
|
-
custom: (
|
|
3078
|
+
universal_rest: (_ka = readString(env, "EMBEDDING_API_KEY")) != null ? _ka : readString(env, "OPENAI_API_KEY"),
|
|
3079
|
+
custom: (_la = readString(env, "EMBEDDING_API_KEY")) != null ? _la : readString(env, "OPENAI_API_KEY")
|
|
3087
3080
|
};
|
|
3088
3081
|
const DEFAULT_MODEL_BY_PROVIDER = {
|
|
3089
3082
|
openai: "gpt-4o",
|
|
@@ -3105,27 +3098,27 @@ function getEnvConfig(env = process.env, base) {
|
|
|
3105
3098
|
})();
|
|
3106
3099
|
const tierBasedLLMModel = isFreeTier2 ? "llama-3.1-8b-instant" : "llama-3.3-70b-versatile";
|
|
3107
3100
|
const tierBasedEmbeddingModel = isFreeTier2 ? "text-embedding-004" : "text-embedding-004";
|
|
3108
|
-
const controlPlaneUrl = (
|
|
3101
|
+
const controlPlaneUrl = (_ma = readString(env, "RETRIVORA_CONTROL_PLANE_URL")) != null ? _ma : readString(env, "NEXT_PUBLIC_RETRIVORA_CONTROL_PLANE_URL");
|
|
3109
3102
|
const defaultGatewayUrl = controlPlaneUrl ? `${controlPlaneUrl.replace(/\/$/, "")}/api/v1` : "https://llm.retrivora.com/api/v1";
|
|
3110
3103
|
const rt = _runtimeConfig;
|
|
3111
3104
|
const defaultLlmProvider = "universal_rest";
|
|
3112
|
-
const llmProvider = (
|
|
3113
|
-
const llmBaseUrl = (
|
|
3114
|
-
const llmModel = (
|
|
3115
|
-
const llmApiKey = (
|
|
3116
|
-
const llmProfile = (
|
|
3105
|
+
const llmProvider = (_pa = (_oa = readEnum(env, "LLM_PROVIDER", defaultLlmProvider, LLM_PROVIDERS)) != null ? _oa : (_na = base == null ? void 0 : base.llm) == null ? void 0 : _na.provider) != null ? _pa : defaultLlmProvider;
|
|
3106
|
+
const llmBaseUrl = (_ua = (_ta = (_sa = (_qa = readString(env, "LITELLM_BASE_URL")) != null ? _qa : readString(env, "LLM_BASE_URL")) != null ? _sa : (_ra = base == null ? void 0 : base.llm) == null ? void 0 : _ra.baseUrl) != null ? _ta : rt == null ? void 0 : rt.llmBaseUrl) != null ? _ua : defaultGatewayUrl;
|
|
3107
|
+
const llmModel = (_ya = (_xa = (_wa = readString(env, "LLM_MODEL")) != null ? _wa : (_va = base == null ? void 0 : base.llm) == null ? void 0 : _va.model) != null ? _xa : rt == null ? void 0 : rt.llmModel) != null ? _ya : tierBasedLLMModel;
|
|
3108
|
+
const llmApiKey = (_Ca = (_Ba = (_za = readString(env, "LITELLM_MASTER_KEY")) != null ? _za : rt == null ? void 0 : rt.litellmApiKey) != null ? _Ba : (_Aa = base == null ? void 0 : base.llm) == null ? void 0 : _Aa.apiKey) != null ? _Ca : licenseKey;
|
|
3109
|
+
const llmProfile = (_Ga = (_Fa = readString(env, "LLM_UNIVERSAL_PROFILE")) != null ? _Fa : (_Ea = (_Da = base == null ? void 0 : base.llm) == null ? void 0 : _Da.options) == null ? void 0 : _Ea.profile) != null ? _Ga : "litellm";
|
|
3117
3110
|
const defaultEmbeddingProvider = "universal_rest";
|
|
3118
|
-
const embeddingProvider = (
|
|
3119
|
-
const embeddingBaseUrl = (
|
|
3120
|
-
const embeddingModel = (
|
|
3121
|
-
const embeddingApiKey = (
|
|
3122
|
-
const embeddingProfile = (
|
|
3111
|
+
const embeddingProvider = (_Ja = (_Ia = readEnum(env, "EMBEDDING_PROVIDER", defaultEmbeddingProvider, EMBEDDING_PROVIDERS)) != null ? _Ia : (_Ha = base == null ? void 0 : base.embedding) == null ? void 0 : _Ha.provider) != null ? _Ja : defaultEmbeddingProvider;
|
|
3112
|
+
const embeddingBaseUrl = (_Qa = (_Pa = (_Oa = (_Na = (_La = (_Ka = readString(env, "LITELLM_BASE_URL")) != null ? _Ka : readString(env, "EMBEDDING_BASE_URL")) != null ? _La : readString(env, "LLM_BASE_URL")) != null ? _Na : (_Ma = base == null ? void 0 : base.embedding) == null ? void 0 : _Ma.baseUrl) != null ? _Oa : rt == null ? void 0 : rt.embeddingBaseUrl) != null ? _Pa : rt == null ? void 0 : rt.llmBaseUrl) != null ? _Qa : defaultGatewayUrl;
|
|
3113
|
+
const embeddingModel = (_Ua = (_Ta = (_Sa = readString(env, "EMBEDDING_MODEL")) != null ? _Sa : (_Ra = base == null ? void 0 : base.embedding) == null ? void 0 : _Ra.model) != null ? _Ta : rt == null ? void 0 : rt.embeddingModel) != null ? _Ua : tierBasedEmbeddingModel;
|
|
3114
|
+
const embeddingApiKey = (_Ya = (_Xa = (_Va = readString(env, "LITELLM_MASTER_KEY")) != null ? _Va : rt == null ? void 0 : rt.litellmApiKey) != null ? _Xa : (_Wa = base == null ? void 0 : base.embedding) == null ? void 0 : _Wa.apiKey) != null ? _Ya : licenseKey;
|
|
3115
|
+
const embeddingProfile = (_ab = (_$a = readString(env, "EMBEDDING_UNIVERSAL_PROFILE")) != null ? _$a : (__a = (_Za = base == null ? void 0 : base.embedding) == null ? void 0 : _Za.options) == null ? void 0 : __a.profile) != null ? _ab : "litellm";
|
|
3123
3116
|
return __spreadProps(__spreadValues({
|
|
3124
3117
|
projectId,
|
|
3125
3118
|
licenseKey,
|
|
3126
3119
|
vectorDb: {
|
|
3127
3120
|
provider: vectorProvider,
|
|
3128
|
-
indexName: (
|
|
3121
|
+
indexName: (_cb = (_bb = readString(env, "VECTOR_DB_INDEX")) != null ? _bb : vectorDbOptions.indexName) != null ? _cb : "rag-index",
|
|
3129
3122
|
options: vectorDbOptions
|
|
3130
3123
|
},
|
|
3131
3124
|
llm: {
|
|
@@ -3133,8 +3126,8 @@ function getEnvConfig(env = process.env, base) {
|
|
|
3133
3126
|
model: llmModel,
|
|
3134
3127
|
apiKey: llmApiKey,
|
|
3135
3128
|
baseUrl: llmBaseUrl,
|
|
3136
|
-
systemPrompt: (
|
|
3137
|
-
maxTokens: readNumber(env, "LLM_MAX_TOKENS", (
|
|
3129
|
+
systemPrompt: (_db = readString(env, "LLM_SYSTEM_PROMPT")) != null ? _db : rt == null ? void 0 : rt.systemPrompt,
|
|
3130
|
+
maxTokens: readNumber(env, "LLM_MAX_TOKENS", (_eb = rt == null ? void 0 : rt.maxTokens) != null ? _eb : 4096),
|
|
3138
3131
|
temperature: readNumber(env, "LLM_TEMPERATURE", 0.7),
|
|
3139
3132
|
options: {
|
|
3140
3133
|
profile: llmProfile,
|
|
@@ -3155,30 +3148,30 @@ function getEnvConfig(env = process.env, base) {
|
|
|
3155
3148
|
}
|
|
3156
3149
|
},
|
|
3157
3150
|
ui: {
|
|
3158
|
-
title: (
|
|
3159
|
-
subtitle: (
|
|
3160
|
-
primaryColor: (
|
|
3161
|
-
accentColor: (
|
|
3162
|
-
logoUrl: (
|
|
3163
|
-
placeholder: (
|
|
3164
|
-
showSources: ((
|
|
3165
|
-
welcomeMessage: (
|
|
3166
|
-
visualStyle: (
|
|
3167
|
-
borderRadius: (
|
|
3168
|
-
allowUpload: ((
|
|
3151
|
+
title: (_gb = (_fb = readString(env, "NEXT_PUBLIC_UI_TITLE")) != null ? _fb : readString(env, "UI_TITLE")) != null ? _gb : "AI Assistant",
|
|
3152
|
+
subtitle: (_ib = (_hb = readString(env, "NEXT_PUBLIC_UI_SUBTITLE")) != null ? _hb : readString(env, "UI_SUBTITLE")) != null ? _ib : "Powered by RAG",
|
|
3153
|
+
primaryColor: (_kb = (_jb = readString(env, "NEXT_PUBLIC_PRIMARY_COLOR")) != null ? _jb : readString(env, "UI_PRIMARY_COLOR")) != null ? _kb : "#10b981",
|
|
3154
|
+
accentColor: (_mb = (_lb = readString(env, "NEXT_PUBLIC_ACCENT_COLOR")) != null ? _lb : readString(env, "UI_ACCENT_COLOR")) != null ? _mb : "#3b82f6",
|
|
3155
|
+
logoUrl: (_nb = readString(env, "NEXT_PUBLIC_LOGO_URL")) != null ? _nb : readString(env, "UI_LOGO_URL"),
|
|
3156
|
+
placeholder: (_pb = (_ob = readString(env, "NEXT_PUBLIC_PLACEHOLDER")) != null ? _ob : readString(env, "UI_PLACEHOLDER")) != null ? _pb : "Ask me anything\u2026",
|
|
3157
|
+
showSources: ((_rb = (_qb = readString(env, "NEXT_PUBLIC_SHOW_SOURCES")) != null ? _qb : readString(env, "UI_SHOW_SOURCES")) != null ? _rb : "true") !== "false",
|
|
3158
|
+
welcomeMessage: (_tb = (_sb = readString(env, "NEXT_PUBLIC_WELCOME_MESSAGE")) != null ? _sb : readString(env, "UI_WELCOME_MESSAGE")) != null ? _tb : "Hello! I'm your AI assistant. Ask me anything about your documents.",
|
|
3159
|
+
visualStyle: (_vb = (_ub = readString(env, "NEXT_PUBLIC_UI_VISUAL_STYLE")) != null ? _ub : readString(env, "UI_VISUAL_STYLE")) != null ? _vb : "glass",
|
|
3160
|
+
borderRadius: (_xb = (_wb = readString(env, "NEXT_PUBLIC_UI_BORDER_RADIUS")) != null ? _wb : readString(env, "UI_BORDER_RADIUS")) != null ? _xb : "xl",
|
|
3161
|
+
allowUpload: ((_zb = (_yb = readString(env, "NEXT_PUBLIC_ALLOW_UPLOAD")) != null ? _yb : readString(env, "UI_ALLOW_UPLOAD")) != null ? _zb : "false") === "true"
|
|
3169
3162
|
},
|
|
3170
3163
|
rag: {
|
|
3171
|
-
topK: readNumber(env, "RAG_TOP_K", (
|
|
3164
|
+
topK: readNumber(env, "RAG_TOP_K", (_Ab = rt == null ? void 0 : rt.topK) != null ? _Ab : 5),
|
|
3172
3165
|
scoreThreshold: readNumber(env, "RAG_SCORE_THRESHOLD", 0),
|
|
3173
|
-
chunkSize: readNumber(env, "RAG_CHUNK_SIZE", (
|
|
3174
|
-
chunkOverlap: readNumber(env, "RAG_CHUNK_OVERLAP", (
|
|
3175
|
-
filterableFields: (
|
|
3166
|
+
chunkSize: readNumber(env, "RAG_CHUNK_SIZE", (_Bb = rt == null ? void 0 : rt.chunkSize) != null ? _Bb : 1e3),
|
|
3167
|
+
chunkOverlap: readNumber(env, "RAG_CHUNK_OVERLAP", (_Cb = rt == null ? void 0 : rt.chunkOverlap) != null ? _Cb : 200),
|
|
3168
|
+
filterableFields: (_Db = readString(env, "RAG_FILTERABLE_FIELDS")) == null ? void 0 : _Db.split(",").map((f) => f.trim()),
|
|
3176
3169
|
// Query pipeline toggles — read from .env.local
|
|
3177
3170
|
useQueryTransformation: readString(env, "RAG_USE_QUERY_TRANSFORMATION") === "true",
|
|
3178
3171
|
useReranking: readString(env, "RAG_USE_RERANKING") === "true",
|
|
3179
3172
|
useGraphRetrieval: readString(env, "RAG_USE_GRAPH_RETRIEVAL") === "true",
|
|
3180
|
-
architecture: (
|
|
3181
|
-
chunkingStrategy: (
|
|
3173
|
+
architecture: (_Eb = readString(env, "RAG_ARCHITECTURE")) != null ? _Eb : "simple",
|
|
3174
|
+
chunkingStrategy: (_Fb = readString(env, "RAG_CHUNKING_STRATEGY")) != null ? _Fb : "recursive",
|
|
3182
3175
|
uiMapping: (() => {
|
|
3183
3176
|
const raw = readString(env, "RAG_UI_MAPPING");
|
|
3184
3177
|
if (!raw) return void 0;
|
|
@@ -3191,7 +3184,7 @@ function getEnvConfig(env = process.env, base) {
|
|
|
3191
3184
|
},
|
|
3192
3185
|
telemetry: {
|
|
3193
3186
|
enabled: telemetryEnabled,
|
|
3194
|
-
url: (
|
|
3187
|
+
url: (_Jb = (_Ib = (_Gb = readString(env, "TELEMETRY_URL")) != null ? _Gb : readString(env, "NEXT_PUBLIC_TELEMETRY_URL")) != null ? _Ib : (_Hb = base == null ? void 0 : base.telemetry) == null ? void 0 : _Hb.url) != null ? _Jb : "https://www.retrivora.com/api/telemetry"
|
|
3195
3188
|
}
|
|
3196
3189
|
}, readString(env, "GRAPH_DB_PROVIDER") ? {
|
|
3197
3190
|
graphDb: {
|
|
@@ -3204,8 +3197,8 @@ function getEnvConfig(env = process.env, base) {
|
|
|
3204
3197
|
}
|
|
3205
3198
|
} : {}), {
|
|
3206
3199
|
mcpServers: (() => {
|
|
3207
|
-
var
|
|
3208
|
-
const raw = (
|
|
3200
|
+
var _a3;
|
|
3201
|
+
const raw = (_a3 = readString(env, "MCP_SERVERS")) != null ? _a3 : readString(env, "NEXT_PUBLIC_MCP_SERVERS");
|
|
3209
3202
|
if (!raw) return void 0;
|
|
3210
3203
|
try {
|
|
3211
3204
|
return JSON.parse(raw);
|
|
@@ -3251,10 +3244,10 @@ var ConfigResolver = class {
|
|
|
3251
3244
|
* fallback behavior.
|
|
3252
3245
|
*/
|
|
3253
3246
|
static resolveUniversal(hostConfig, env = process.env) {
|
|
3254
|
-
var
|
|
3247
|
+
var _a2;
|
|
3255
3248
|
if (!hostConfig) return this.resolve(void 0, env);
|
|
3256
3249
|
const normalized = __spreadProps(__spreadValues({}, hostConfig), {
|
|
3257
|
-
vectorDb: (
|
|
3250
|
+
vectorDb: (_a2 = hostConfig.vectorDb) != null ? _a2 : hostConfig.vectorDatabase,
|
|
3258
3251
|
rag: this.mergeRetrievalWorkflow(hostConfig.rag, hostConfig.retrieval, hostConfig.workflow)
|
|
3259
3252
|
});
|
|
3260
3253
|
return this.resolve(normalized, env);
|
|
@@ -3274,11 +3267,11 @@ var ConfigResolver = class {
|
|
|
3274
3267
|
}
|
|
3275
3268
|
}
|
|
3276
3269
|
static mergeRetrievalWorkflow(rag, retrieval, workflow) {
|
|
3277
|
-
var
|
|
3270
|
+
var _a2, _b, _c, _d, _e, _f;
|
|
3278
3271
|
if (!rag && !retrieval && !workflow) return void 0;
|
|
3279
3272
|
const normalized = __spreadValues({}, rag != null ? rag : {});
|
|
3280
3273
|
if (retrieval) {
|
|
3281
|
-
normalized.topK = (
|
|
3274
|
+
normalized.topK = (_a2 = retrieval.topK) != null ? _a2 : normalized.topK;
|
|
3282
3275
|
normalized.scoreThreshold = (_b = retrieval.scoreThreshold) != null ? _b : normalized.scoreThreshold;
|
|
3283
3276
|
normalized.useReranking = (_c = retrieval.useReranking) != null ? _c : normalized.useReranking;
|
|
3284
3277
|
if (retrieval.strategy === "hybrid") normalized.architecture = (_d = normalized.architecture) != null ? _d : "hybrid";
|
|
@@ -3406,8 +3399,8 @@ var OpenAIProvider = class {
|
|
|
3406
3399
|
};
|
|
3407
3400
|
}
|
|
3408
3401
|
async chat(messages, context, options) {
|
|
3409
|
-
var
|
|
3410
|
-
const basePrompt = (_b = (
|
|
3402
|
+
var _a2, _b, _c, _d, _e, _f, _g2, _h, _i;
|
|
3403
|
+
const basePrompt = (_b = (_a2 = options == null ? void 0 : options.systemPrompt) != null ? _a2 : this.llmConfig.systemPrompt) != null ? _b : "You are a helpful assistant. Answer questions based on the provided context.";
|
|
3411
3404
|
const systemMessage = {
|
|
3412
3405
|
role: "system",
|
|
3413
3406
|
content: buildSystemContent(basePrompt, context)
|
|
@@ -3431,8 +3424,8 @@ var OpenAIProvider = class {
|
|
|
3431
3424
|
}
|
|
3432
3425
|
chatStream(messages, context, options) {
|
|
3433
3426
|
return __asyncGenerator(this, null, function* () {
|
|
3434
|
-
var
|
|
3435
|
-
const basePrompt = (_b = (
|
|
3427
|
+
var _a2, _b, _c, _d, _e, _f, _g2, _h;
|
|
3428
|
+
const basePrompt = (_b = (_a2 = options == null ? void 0 : options.systemPrompt) != null ? _a2 : this.llmConfig.systemPrompt) != null ? _b : "You are a helpful assistant. Answer questions based on the provided context.";
|
|
3436
3429
|
const systemMessage = {
|
|
3437
3430
|
role: "system",
|
|
3438
3431
|
content: buildSystemContent(basePrompt, context)
|
|
@@ -3479,8 +3472,8 @@ var OpenAIProvider = class {
|
|
|
3479
3472
|
return results[0];
|
|
3480
3473
|
}
|
|
3481
3474
|
async batchEmbed(texts, options) {
|
|
3482
|
-
var
|
|
3483
|
-
const model = (_c = (_b = options == null ? void 0 : options.model) != null ? _b : (
|
|
3475
|
+
var _a2, _b, _c, _d, _e;
|
|
3476
|
+
const model = (_c = (_b = options == null ? void 0 : options.model) != null ? _b : (_a2 = this.embeddingConfig) == null ? void 0 : _a2.model) != null ? _c : "text-embedding-3-small";
|
|
3484
3477
|
const apiKey = (_e = (_d = this.embeddingConfig) == null ? void 0 : _d.apiKey) != null ? _e : this.llmConfig.apiKey;
|
|
3485
3478
|
const client = apiKey !== this.llmConfig.apiKey ? new import_openai.default({ apiKey }) : this.client;
|
|
3486
3479
|
const response = await client.embeddings.create({ model, input: texts });
|
|
@@ -3539,11 +3532,7 @@ var AnthropicProvider = class {
|
|
|
3539
3532
|
try {
|
|
3540
3533
|
const { default: Anthropic2 } = await import("@anthropic-ai/sdk");
|
|
3541
3534
|
const client = new Anthropic2({ apiKey });
|
|
3542
|
-
await client.
|
|
3543
|
-
model: modelName,
|
|
3544
|
-
max_tokens: 10,
|
|
3545
|
-
messages: [{ role: "user", content: "ping" }]
|
|
3546
|
-
});
|
|
3535
|
+
await client.models.list();
|
|
3547
3536
|
return { healthy: true, provider: "anthropic", capabilities: { model: modelName }, timestamp };
|
|
3548
3537
|
} catch (error) {
|
|
3549
3538
|
return {
|
|
@@ -3557,8 +3546,8 @@ var AnthropicProvider = class {
|
|
|
3557
3546
|
};
|
|
3558
3547
|
}
|
|
3559
3548
|
async chat(messages, context, options) {
|
|
3560
|
-
var
|
|
3561
|
-
const basePrompt = (_b = (
|
|
3549
|
+
var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k;
|
|
3550
|
+
const basePrompt = (_b = (_a2 = options == null ? void 0 : options.systemPrompt) != null ? _a2 : this.llmConfig.systemPrompt) != null ? _b : "You are a helpful assistant. Use the context below to answer the user's question accurately.";
|
|
3562
3551
|
const system = buildSystemContent(basePrompt, context);
|
|
3563
3552
|
const anthropicMessages = messages.map((m) => ({
|
|
3564
3553
|
role: m.role === "assistant" ? "assistant" : "user",
|
|
@@ -3596,8 +3585,8 @@ var AnthropicProvider = class {
|
|
|
3596
3585
|
}
|
|
3597
3586
|
chatStream(messages, context, options) {
|
|
3598
3587
|
return __asyncGenerator(this, null, function* () {
|
|
3599
|
-
var
|
|
3600
|
-
const basePrompt = (_b = (
|
|
3588
|
+
var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k;
|
|
3589
|
+
const basePrompt = (_b = (_a2 = options == null ? void 0 : options.systemPrompt) != null ? _a2 : this.llmConfig.systemPrompt) != null ? _b : "You are a helpful assistant. Use the context below to answer the user's question accurately.";
|
|
3601
3590
|
const system = buildSystemContent(basePrompt, context);
|
|
3602
3591
|
const anthropicMessages = messages.map((m) => ({
|
|
3603
3592
|
role: m.role === "assistant" ? "assistant" : "user",
|
|
@@ -3681,8 +3670,8 @@ var AnthropicProvider = class {
|
|
|
3681
3670
|
var import_axios = __toESM(require("axios"));
|
|
3682
3671
|
var OllamaProvider = class {
|
|
3683
3672
|
constructor(llmConfig, embeddingConfig) {
|
|
3684
|
-
var
|
|
3685
|
-
const rawBaseURL = ((
|
|
3673
|
+
var _a2, _b, _c;
|
|
3674
|
+
const rawBaseURL = ((_a2 = llmConfig.baseUrl) != null ? _a2 : "http://localhost:11434").replace(/\/+$/, "");
|
|
3686
3675
|
const baseURL = rawBaseURL.replace(/\/(?:api\/v1|v1|api)$/i, "");
|
|
3687
3676
|
const timeout = Number((_b = llmConfig.options) == null ? void 0 : _b.timeout) || 3e5;
|
|
3688
3677
|
const headers = ((_c = llmConfig.options) == null ? void 0 : _c.headers) || {};
|
|
@@ -3742,8 +3731,8 @@ var OllamaProvider = class {
|
|
|
3742
3731
|
};
|
|
3743
3732
|
}
|
|
3744
3733
|
async chat(messages, context, options) {
|
|
3745
|
-
var
|
|
3746
|
-
const basePrompt = (_b = (
|
|
3734
|
+
var _a2, _b, _c, _d, _e, _f;
|
|
3735
|
+
const basePrompt = (_b = (_a2 = options == null ? void 0 : options.systemPrompt) != null ? _a2 : this.llmConfig.systemPrompt) != null ? _b : "You are a helpful assistant. Use the provided context to answer the user's question.";
|
|
3747
3736
|
const system = buildSystemContent(basePrompt, context);
|
|
3748
3737
|
const { data } = await this.http.post("/api/chat", {
|
|
3749
3738
|
model: this.llmConfig.model,
|
|
@@ -3761,8 +3750,8 @@ var OllamaProvider = class {
|
|
|
3761
3750
|
}
|
|
3762
3751
|
chatStream(messages, context, options) {
|
|
3763
3752
|
return __asyncGenerator(this, null, function* () {
|
|
3764
|
-
var
|
|
3765
|
-
const basePrompt = (_b = (
|
|
3753
|
+
var _a2, _b, _c, _d, _e, _f, _g2, _h;
|
|
3754
|
+
const basePrompt = (_b = (_a2 = options == null ? void 0 : options.systemPrompt) != null ? _a2 : this.llmConfig.systemPrompt) != null ? _b : "You are a helpful assistant. Use the provided context to answer the user's question.";
|
|
3766
3755
|
const system = buildSystemContent(basePrompt, context);
|
|
3767
3756
|
const response = yield new __await(this.http.post("/api/chat", {
|
|
3768
3757
|
model: this.llmConfig.model,
|
|
@@ -3820,8 +3809,8 @@ var OllamaProvider = class {
|
|
|
3820
3809
|
});
|
|
3821
3810
|
}
|
|
3822
3811
|
async embed(text, options) {
|
|
3823
|
-
var
|
|
3824
|
-
const model = (_c = (_b = options == null ? void 0 : options.model) != null ? _b : (
|
|
3812
|
+
var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k;
|
|
3813
|
+
const model = (_c = (_b = options == null ? void 0 : options.model) != null ? _b : (_a2 = this.embeddingConfig) == null ? void 0 : _a2.model) != null ? _c : "nomic-embed-text";
|
|
3825
3814
|
const baseURL = (_f = (_e = (_d = this.embeddingConfig) == null ? void 0 : _d.baseUrl) != null ? _e : this.llmConfig.baseUrl) != null ? _f : "http://localhost:11434";
|
|
3826
3815
|
const client = baseURL !== ((_g2 = this.llmConfig.baseUrl) != null ? _g2 : "http://localhost:11434") ? import_axios.default.create({ baseURL, timeout: 6e4 }) : this.http;
|
|
3827
3816
|
let prompt = text;
|
|
@@ -3922,9 +3911,9 @@ var GeminiProvider = class {
|
|
|
3922
3911
|
static getHealthChecker() {
|
|
3923
3912
|
return {
|
|
3924
3913
|
async check(config) {
|
|
3925
|
-
var
|
|
3914
|
+
var _a2, _b;
|
|
3926
3915
|
const timestamp = Date.now();
|
|
3927
|
-
const apiKey = (_b = (
|
|
3916
|
+
const apiKey = (_b = (_a2 = config.apiKey) != null ? _a2 : process.env.GOOGLE_GENAI_API_KEY) != null ? _b : "";
|
|
3928
3917
|
const modelName = sanitizeModel(config.model);
|
|
3929
3918
|
try {
|
|
3930
3919
|
const { GoogleGenerativeAI: GoogleGenerativeAI2 } = await import("@google/generative-ai");
|
|
@@ -3954,16 +3943,16 @@ var GeminiProvider = class {
|
|
|
3954
3943
|
/** Resolve the embedding client — uses a separate client when the embedding
|
|
3955
3944
|
* API key differs from the LLM API key. */
|
|
3956
3945
|
get embeddingClient() {
|
|
3957
|
-
var
|
|
3958
|
-
if (((
|
|
3946
|
+
var _a2;
|
|
3947
|
+
if (((_a2 = this.embeddingConfig) == null ? void 0 : _a2.apiKey) && this.embeddingConfig.apiKey !== this.llmConfig.apiKey) {
|
|
3959
3948
|
return buildClient(this.embeddingConfig.apiKey);
|
|
3960
3949
|
}
|
|
3961
3950
|
return this.client;
|
|
3962
3951
|
}
|
|
3963
3952
|
/** Resolve the embedding model to use, in order of specificity. */
|
|
3964
3953
|
resolveEmbeddingModel(optionsModel) {
|
|
3965
|
-
var
|
|
3966
|
-
return sanitizeModel((_b = optionsModel != null ? optionsModel : (
|
|
3954
|
+
var _a2, _b;
|
|
3955
|
+
return sanitizeModel((_b = optionsModel != null ? optionsModel : (_a2 = this.embeddingConfig) == null ? void 0 : _a2.model) != null ? _b : DEFAULT_EMBEDDING_MODEL);
|
|
3967
3956
|
}
|
|
3968
3957
|
/**
|
|
3969
3958
|
* Convert ChatMessage[] to the Gemini contents format.
|
|
@@ -3983,8 +3972,8 @@ var GeminiProvider = class {
|
|
|
3983
3972
|
// ILLMProvider — chat
|
|
3984
3973
|
// -------------------------------------------------------------------------
|
|
3985
3974
|
async chat(messages, context, options) {
|
|
3986
|
-
var
|
|
3987
|
-
const basePrompt = (_b = (
|
|
3975
|
+
var _a2, _b, _c, _d, _e, _f;
|
|
3976
|
+
const basePrompt = (_b = (_a2 = options == null ? void 0 : options.systemPrompt) != null ? _a2 : this.llmConfig.systemPrompt) != null ? _b : "You are a helpful assistant. Answer questions based on the provided context.";
|
|
3988
3977
|
const model = this.client.getGenerativeModel({
|
|
3989
3978
|
model: this.llmConfig.model,
|
|
3990
3979
|
systemInstruction: buildSystemContent(basePrompt, context)
|
|
@@ -4001,8 +3990,8 @@ var GeminiProvider = class {
|
|
|
4001
3990
|
}
|
|
4002
3991
|
chatStream(messages, context, options) {
|
|
4003
3992
|
return __asyncGenerator(this, null, function* () {
|
|
4004
|
-
var
|
|
4005
|
-
const basePrompt = (_b = (
|
|
3993
|
+
var _a2, _b, _c, _d, _e, _f;
|
|
3994
|
+
const basePrompt = (_b = (_a2 = options == null ? void 0 : options.systemPrompt) != null ? _a2 : this.llmConfig.systemPrompt) != null ? _b : "You are a helpful assistant. Answer questions based on the provided context.";
|
|
4006
3995
|
const model = this.client.getGenerativeModel({
|
|
4007
3996
|
model: this.llmConfig.model,
|
|
4008
3997
|
systemInstruction: buildSystemContent(basePrompt, context)
|
|
@@ -4037,11 +4026,11 @@ var GeminiProvider = class {
|
|
|
4037
4026
|
// ILLMProvider — embeddings
|
|
4038
4027
|
// -------------------------------------------------------------------------
|
|
4039
4028
|
async embed(text, options) {
|
|
4040
|
-
var
|
|
4029
|
+
var _a2, _b;
|
|
4041
4030
|
const content = applyPrefix(
|
|
4042
4031
|
text,
|
|
4043
4032
|
options == null ? void 0 : options.taskType,
|
|
4044
|
-
(
|
|
4033
|
+
(_a2 = this.embeddingConfig) == null ? void 0 : _a2.queryPrefix,
|
|
4045
4034
|
(_b = this.embeddingConfig) == null ? void 0 : _b.documentPrefix
|
|
4046
4035
|
);
|
|
4047
4036
|
const modelName = this.resolveEmbeddingModel(options == null ? void 0 : options.model);
|
|
@@ -4058,7 +4047,7 @@ var GeminiProvider = class {
|
|
|
4058
4047
|
const modelName = this.resolveEmbeddingModel(options == null ? void 0 : options.model);
|
|
4059
4048
|
const model = this.embeddingClient.getGenerativeModel({ model: modelName });
|
|
4060
4049
|
const requests = texts.map((text) => {
|
|
4061
|
-
var
|
|
4050
|
+
var _a2, _b;
|
|
4062
4051
|
return {
|
|
4063
4052
|
content: {
|
|
4064
4053
|
role: "user",
|
|
@@ -4066,7 +4055,7 @@ var GeminiProvider = class {
|
|
|
4066
4055
|
text: applyPrefix(
|
|
4067
4056
|
text,
|
|
4068
4057
|
options == null ? void 0 : options.taskType,
|
|
4069
|
-
(
|
|
4058
|
+
(_a2 = this.embeddingConfig) == null ? void 0 : _a2.queryPrefix,
|
|
4070
4059
|
(_b = this.embeddingConfig) == null ? void 0 : _b.documentPrefix
|
|
4071
4060
|
)
|
|
4072
4061
|
}]
|
|
@@ -4083,8 +4072,8 @@ var GeminiProvider = class {
|
|
|
4083
4072
|
throw err;
|
|
4084
4073
|
});
|
|
4085
4074
|
return response.embeddings.map((e) => {
|
|
4086
|
-
var
|
|
4087
|
-
return (
|
|
4075
|
+
var _a2;
|
|
4076
|
+
return (_a2 = e.values) != null ? _a2 : [];
|
|
4088
4077
|
});
|
|
4089
4078
|
}
|
|
4090
4079
|
// -------------------------------------------------------------------------
|
|
@@ -4173,8 +4162,8 @@ var GroqProvider = class {
|
|
|
4173
4162
|
};
|
|
4174
4163
|
}
|
|
4175
4164
|
async chat(messages, context, options) {
|
|
4176
|
-
var
|
|
4177
|
-
const basePrompt = (_b = (
|
|
4165
|
+
var _a2, _b, _c, _d, _e, _f, _g2, _h, _i;
|
|
4166
|
+
const basePrompt = (_b = (_a2 = options == null ? void 0 : options.systemPrompt) != null ? _a2 : this.llmConfig.systemPrompt) != null ? _b : "You are a helpful assistant. Answer questions based on the provided context.";
|
|
4178
4167
|
const systemMessage = {
|
|
4179
4168
|
role: "system",
|
|
4180
4169
|
content: buildSystemContent(basePrompt, context)
|
|
@@ -4198,8 +4187,8 @@ var GroqProvider = class {
|
|
|
4198
4187
|
}
|
|
4199
4188
|
chatStream(messages, context, options) {
|
|
4200
4189
|
return __asyncGenerator(this, null, function* () {
|
|
4201
|
-
var
|
|
4202
|
-
const basePrompt = (_b = (
|
|
4190
|
+
var _a2, _b, _c, _d, _e, _f, _g2, _h;
|
|
4191
|
+
const basePrompt = (_b = (_a2 = options == null ? void 0 : options.systemPrompt) != null ? _a2 : this.llmConfig.systemPrompt) != null ? _b : "You are a helpful assistant. Answer questions based on the provided context.";
|
|
4203
4192
|
const systemMessage = {
|
|
4204
4193
|
role: "system",
|
|
4205
4194
|
content: buildSystemContent(basePrompt, context)
|
|
@@ -4333,8 +4322,8 @@ var QwenProvider = class {
|
|
|
4333
4322
|
};
|
|
4334
4323
|
}
|
|
4335
4324
|
async chat(messages, context, options) {
|
|
4336
|
-
var
|
|
4337
|
-
const basePrompt = (_b = (
|
|
4325
|
+
var _a2, _b, _c, _d, _e, _f, _g2, _h, _i;
|
|
4326
|
+
const basePrompt = (_b = (_a2 = options == null ? void 0 : options.systemPrompt) != null ? _a2 : this.llmConfig.systemPrompt) != null ? _b : "You are a helpful assistant. Answer questions based on the provided context.";
|
|
4338
4327
|
const systemMessage = {
|
|
4339
4328
|
role: "system",
|
|
4340
4329
|
content: buildSystemContent(basePrompt, context)
|
|
@@ -4358,8 +4347,8 @@ var QwenProvider = class {
|
|
|
4358
4347
|
}
|
|
4359
4348
|
chatStream(messages, context, options) {
|
|
4360
4349
|
return __asyncGenerator(this, null, function* () {
|
|
4361
|
-
var
|
|
4362
|
-
const basePrompt = (_b = (
|
|
4350
|
+
var _a2, _b, _c, _d, _e, _f, _g2, _h;
|
|
4351
|
+
const basePrompt = (_b = (_a2 = options == null ? void 0 : options.systemPrompt) != null ? _a2 : this.llmConfig.systemPrompt) != null ? _b : "You are a helpful assistant. Answer questions based on the provided context.";
|
|
4363
4352
|
const systemMessage = {
|
|
4364
4353
|
role: "system",
|
|
4365
4354
|
content: buildSystemContent(basePrompt, context)
|
|
@@ -4406,8 +4395,8 @@ var QwenProvider = class {
|
|
|
4406
4395
|
return results[0];
|
|
4407
4396
|
}
|
|
4408
4397
|
async batchEmbed(texts, options) {
|
|
4409
|
-
var
|
|
4410
|
-
const model = (_c = (_b = options == null ? void 0 : options.model) != null ? _b : (
|
|
4398
|
+
var _a2, _b, _c, _d, _e, _f;
|
|
4399
|
+
const model = (_c = (_b = options == null ? void 0 : options.model) != null ? _b : (_a2 = this.embeddingConfig) == null ? void 0 : _a2.model) != null ? _c : "text-embedding-v1";
|
|
4411
4400
|
const apiKey = (_e = (_d = this.embeddingConfig) == null ? void 0 : _d.apiKey) != null ? _e : this.llmConfig.apiKey;
|
|
4412
4401
|
const client = apiKey !== this.llmConfig.apiKey ? new import_openai3.default({
|
|
4413
4402
|
apiKey,
|
|
@@ -4468,9 +4457,9 @@ var LLM_PROFILES = {
|
|
|
4468
4457
|
|
|
4469
4458
|
// src/llm/providers/UniversalLLMAdapter.ts
|
|
4470
4459
|
function extractContent(obj) {
|
|
4471
|
-
var
|
|
4460
|
+
var _a2, _b, _c;
|
|
4472
4461
|
if (!obj || typeof obj !== "object") return void 0;
|
|
4473
|
-
const choice = (
|
|
4462
|
+
const choice = (_a2 = obj.choices) == null ? void 0 : _a2[0];
|
|
4474
4463
|
if (!choice) return void 0;
|
|
4475
4464
|
if (typeof ((_b = choice.message) == null ? void 0 : _b.content) === "string") return choice.message.content;
|
|
4476
4465
|
if (typeof ((_c = choice.delta) == null ? void 0 : _c.content) === "string") return choice.delta.content;
|
|
@@ -4480,10 +4469,10 @@ function extractContent(obj) {
|
|
|
4480
4469
|
}
|
|
4481
4470
|
var UniversalLLMAdapter = class {
|
|
4482
4471
|
constructor(config) {
|
|
4483
|
-
var
|
|
4472
|
+
var _a2, _b, _c, _d, _e, _f, _g2, _h;
|
|
4484
4473
|
this.model = config.model;
|
|
4485
4474
|
const llmConfig = config;
|
|
4486
|
-
const options = (
|
|
4475
|
+
const options = (_a2 = llmConfig.options) != null ? _a2 : {};
|
|
4487
4476
|
let profile = {};
|
|
4488
4477
|
if (typeof options.profile === "string") {
|
|
4489
4478
|
profile = LLM_PROFILES[options.profile] || {};
|
|
@@ -4509,8 +4498,8 @@ var UniversalLLMAdapter = class {
|
|
|
4509
4498
|
});
|
|
4510
4499
|
}
|
|
4511
4500
|
async chat(messages, context) {
|
|
4512
|
-
var
|
|
4513
|
-
const path2 = (
|
|
4501
|
+
var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j;
|
|
4502
|
+
const path2 = (_a2 = this.opts.chatPath) != null ? _a2 : "/chat/completions";
|
|
4514
4503
|
const safeMessages = (Array.isArray(messages) ? messages : []).filter((m) => Boolean(m && typeof m === "object")).map((m) => ({
|
|
4515
4504
|
role: m.role || "user",
|
|
4516
4505
|
content: typeof m.content === "string" ? m.content : m.content != null ? String(m.content) : ""
|
|
@@ -4587,8 +4576,8 @@ ${context != null ? context : "None"}` },
|
|
|
4587
4576
|
*/
|
|
4588
4577
|
chatStream(messages, context) {
|
|
4589
4578
|
return __asyncGenerator(this, null, function* () {
|
|
4590
|
-
var
|
|
4591
|
-
const path2 = (
|
|
4579
|
+
var _a2, _b, _c, _d, _e, _f;
|
|
4580
|
+
const path2 = (_a2 = this.opts.chatPath) != null ? _a2 : "/chat/completions";
|
|
4592
4581
|
const url = `${this.baseUrl.replace(/\/$/, "")}${path2}`;
|
|
4593
4582
|
const extractPath = ((_b = this.opts.responseExtractPath) != null ? _b : "choices[0].message.content").replace("message.content", "delta.content");
|
|
4594
4583
|
const safeMessages = (Array.isArray(messages) ? messages : []).filter((m) => Boolean(m && typeof m === "object")).map((m) => ({
|
|
@@ -4714,8 +4703,8 @@ ${context != null ? context : "None"}` },
|
|
|
4714
4703
|
});
|
|
4715
4704
|
}
|
|
4716
4705
|
async embed(text) {
|
|
4717
|
-
var
|
|
4718
|
-
const path2 = (
|
|
4706
|
+
var _a2, _b, _c, _d, _e, _f, _g2, _h;
|
|
4707
|
+
const path2 = (_a2 = this.opts.embedPath) != null ? _a2 : "/embeddings";
|
|
4719
4708
|
const payload = this.opts.embedPayloadTemplate ? buildPayload(this.opts.embedPayloadTemplate, { input: text, model: this.model }) : { input: text, model: this.model };
|
|
4720
4709
|
try {
|
|
4721
4710
|
const { data } = await this.http.post(path2, payload);
|
|
@@ -4839,7 +4828,7 @@ var LLMFactory = class _LLMFactory {
|
|
|
4839
4828
|
];
|
|
4840
4829
|
}
|
|
4841
4830
|
static create(llmConfig, embeddingConfig) {
|
|
4842
|
-
var
|
|
4831
|
+
var _a2, _b, _c;
|
|
4843
4832
|
switch (llmConfig.provider) {
|
|
4844
4833
|
case "openai":
|
|
4845
4834
|
return new OpenAIProvider(llmConfig, embeddingConfig);
|
|
@@ -4858,7 +4847,7 @@ var LLMFactory = class _LLMFactory {
|
|
|
4858
4847
|
case "custom":
|
|
4859
4848
|
return new UniversalLLMAdapter(llmConfig);
|
|
4860
4849
|
default: {
|
|
4861
|
-
const providerName = String((
|
|
4850
|
+
const providerName = String((_a2 = llmConfig.provider) != null ? _a2 : "").toLowerCase();
|
|
4862
4851
|
const customFactory = customProviders.get(providerName);
|
|
4863
4852
|
if (customFactory) {
|
|
4864
4853
|
return customFactory(llmConfig);
|
|
@@ -4960,7 +4949,7 @@ var ProviderRegistry = class {
|
|
|
4960
4949
|
return null;
|
|
4961
4950
|
}
|
|
4962
4951
|
static async loadVectorProviderClass(provider) {
|
|
4963
|
-
var
|
|
4952
|
+
var _a2;
|
|
4964
4953
|
if (this.vectorProviders[provider]) return this.vectorProviders[provider];
|
|
4965
4954
|
switch (provider) {
|
|
4966
4955
|
case "pinecone": {
|
|
@@ -4969,7 +4958,7 @@ var ProviderRegistry = class {
|
|
|
4969
4958
|
}
|
|
4970
4959
|
case "pgvector":
|
|
4971
4960
|
case "postgresql": {
|
|
4972
|
-
const postgresMode = ((
|
|
4961
|
+
const postgresMode = ((_a2 = process.env.POSTGRES_MODE) != null ? _a2 : "multi").toLowerCase();
|
|
4973
4962
|
if (postgresMode === "single") {
|
|
4974
4963
|
const { PostgreSQLProvider: PostgreSQLProvider2 } = await Promise.resolve().then(() => (init_PostgreSQLProvider(), PostgreSQLProvider_exports));
|
|
4975
4964
|
return PostgreSQLProvider2;
|
|
@@ -5261,13 +5250,15 @@ var DocumentChunker = class {
|
|
|
5261
5250
|
}
|
|
5262
5251
|
}
|
|
5263
5252
|
const parts = text.split(separator);
|
|
5264
|
-
for (
|
|
5265
|
-
|
|
5266
|
-
|
|
5253
|
+
for (let i = 0; i < parts.length; i++) {
|
|
5254
|
+
const part = parts[i];
|
|
5255
|
+
const splitVal = (i === 0 ? "" : separator) + part;
|
|
5256
|
+
if (splitVal.length <= chunkSize) {
|
|
5257
|
+
finalSplits.push(splitVal);
|
|
5267
5258
|
} else if (nextSeparators.length > 0) {
|
|
5268
|
-
finalSplits.push(...this.recursiveSplit(
|
|
5259
|
+
finalSplits.push(...this.recursiveSplit(splitVal, nextSeparators, chunkSize));
|
|
5269
5260
|
} else {
|
|
5270
|
-
finalSplits.push(
|
|
5261
|
+
finalSplits.push(splitVal);
|
|
5271
5262
|
}
|
|
5272
5263
|
}
|
|
5273
5264
|
return finalSplits;
|
|
@@ -5346,74 +5337,108 @@ Text to extract from:
|
|
|
5346
5337
|
};
|
|
5347
5338
|
|
|
5348
5339
|
// src/rag/Reranker.ts
|
|
5340
|
+
var MAX_RERANK_CONTENT_CHARS = 64e3;
|
|
5349
5341
|
var Reranker = class {
|
|
5350
5342
|
/**
|
|
5351
5343
|
* Re-ranks matches based on a secondary relevance score using an LLM.
|
|
5344
|
+
*
|
|
5345
|
+
* Fix RAG-2: keyword re-ranking no longer mutates the original `score`
|
|
5346
|
+
* field (which consumers interpret as the raw vector similarity). A new
|
|
5347
|
+
* `rerankScore` field is added so both the original similarity and the
|
|
5348
|
+
* boosted rank signal are available to callers.
|
|
5352
5349
|
*/
|
|
5353
5350
|
async rerank(matches, query, limit = 5, llm) {
|
|
5354
5351
|
if (!llm || matches.length <= 1) {
|
|
5355
|
-
|
|
5356
|
-
if (keywords.length === 0) {
|
|
5357
|
-
return matches.sort((a, b) => b.score - a.score).slice(0, limit);
|
|
5358
|
-
}
|
|
5359
|
-
const scoredMatches = matches.map((match) => {
|
|
5360
|
-
var _a3;
|
|
5361
|
-
const contentLower = ((_a3 = match == null ? void 0 : match.content) != null ? _a3 : "").toLowerCase();
|
|
5362
|
-
let keywordScore = 0;
|
|
5363
|
-
keywords.forEach((keyword) => {
|
|
5364
|
-
if (contentLower.includes(keyword)) {
|
|
5365
|
-
keywordScore += 1;
|
|
5366
|
-
}
|
|
5367
|
-
});
|
|
5368
|
-
const normalizedKeywordScore = keywordScore / keywords.length;
|
|
5369
|
-
const combinedScore = match.score + normalizedKeywordScore * 0.5;
|
|
5370
|
-
return __spreadProps(__spreadValues({}, match), {
|
|
5371
|
-
score: combinedScore
|
|
5372
|
-
});
|
|
5373
|
-
});
|
|
5374
|
-
return scoredMatches.sort((a, b) => b.score - a.score).slice(0, limit);
|
|
5352
|
+
return this.keywordRerank(matches, query, limit);
|
|
5375
5353
|
}
|
|
5376
5354
|
try {
|
|
5377
|
-
|
|
5378
|
-
|
|
5379
|
-
|
|
5355
|
+
return await this.llmRerank(matches, query, limit, llm);
|
|
5356
|
+
} catch (error) {
|
|
5357
|
+
console.warn("[Reranker] LLM re-ranking failed, falling back to keyword sorting:", error);
|
|
5358
|
+
return this.keywordRerank(matches, query, limit);
|
|
5359
|
+
}
|
|
5360
|
+
}
|
|
5361
|
+
/**
|
|
5362
|
+
* Keyword-based re-ranking fallback.
|
|
5363
|
+
*
|
|
5364
|
+
* Fix RAG-2: `rerankScore` is a separate field; `score` (the original
|
|
5365
|
+
* vector similarity) is never mutated. Callers that display `score` to
|
|
5366
|
+
* end-users receive the canonical cosine similarity value.
|
|
5367
|
+
*/
|
|
5368
|
+
keywordRerank(matches, query, limit) {
|
|
5369
|
+
const keywords = query.toLowerCase().replace(/[^\w\s]/g, "").split(/\s+/).filter((w) => w.length > 2);
|
|
5370
|
+
if (keywords.length === 0) {
|
|
5371
|
+
return matches.sort((a, b) => b.score - a.score).slice(0, limit);
|
|
5372
|
+
}
|
|
5373
|
+
const scoredMatches = matches.map((match) => {
|
|
5374
|
+
var _a2;
|
|
5375
|
+
const contentLower = ((_a2 = match == null ? void 0 : match.content) != null ? _a2 : "").toLowerCase();
|
|
5376
|
+
let keywordHits = 0;
|
|
5377
|
+
keywords.forEach((keyword) => {
|
|
5378
|
+
if (contentLower.includes(keyword)) {
|
|
5379
|
+
keywordHits++;
|
|
5380
|
+
}
|
|
5381
|
+
});
|
|
5382
|
+
const normalizedKeywordScore = keywordHits / keywords.length;
|
|
5383
|
+
return __spreadProps(__spreadValues({}, match), {
|
|
5384
|
+
rerankScore: match.score + normalizedKeywordScore * 0.5
|
|
5385
|
+
});
|
|
5386
|
+
});
|
|
5387
|
+
return scoredMatches.sort((a, b) => {
|
|
5388
|
+
var _a2, _b;
|
|
5389
|
+
return ((_a2 = b.rerankScore) != null ? _a2 : b.score) - ((_b = a.rerankScore) != null ? _b : a.score);
|
|
5390
|
+
}).slice(0, limit);
|
|
5391
|
+
}
|
|
5392
|
+
/**
|
|
5393
|
+
* LLM-based re-ranking.
|
|
5394
|
+
*
|
|
5395
|
+
* Fix RAG-1: total document content is now budgeted against
|
|
5396
|
+
* MAX_RERANK_CONTENT_CHARS to prevent silent truncation or failures
|
|
5397
|
+
* on models with small context windows.
|
|
5398
|
+
*/
|
|
5399
|
+
async llmRerank(matches, query, limit, llm) {
|
|
5400
|
+
const candidates = matches.slice(0, 10);
|
|
5401
|
+
let charsUsed = 0;
|
|
5402
|
+
const budgetedCandidates = candidates.filter((m) => {
|
|
5403
|
+
const len = (m.content || "").length;
|
|
5404
|
+
if (charsUsed + len > MAX_RERANK_CONTENT_CHARS) return false;
|
|
5405
|
+
charsUsed += len;
|
|
5406
|
+
return true;
|
|
5407
|
+
});
|
|
5408
|
+
const docList = budgetedCandidates.map((m, i) => {
|
|
5409
|
+
var _a2;
|
|
5410
|
+
return `[${i}] ${((_a2 = m == null ? void 0 : m.content) != null ? _a2 : "").replace(/\n/g, " ")}`;
|
|
5411
|
+
}).join("\n");
|
|
5412
|
+
const response = await llm.chat(
|
|
5413
|
+
[{ role: "user", content: `Query: "${query}"
|
|
5380
5414
|
|
|
5381
5415
|
Documents:
|
|
5382
|
-
${
|
|
5383
|
-
|
|
5384
|
-
|
|
5385
|
-
|
|
5386
|
-
|
|
5387
|
-
|
|
5388
|
-
systemPrompt: 'You are a relevance ranking expert. Given the user query and a list of retrieved document chunks, rank the chunks by relevance to the query. Return the result as a comma-separated list of document indices in order of relevance, from most relevant to least relevant. Use the indices provided in brackets like [0], [1], etc. Only return the indices (e.g., "2,0,1"), nothing else. Do not include brackets in the output.',
|
|
5389
|
-
temperature: 0,
|
|
5390
|
-
maxTokens: 50
|
|
5391
|
-
}
|
|
5392
|
-
);
|
|
5393
|
-
const cleanedResponse = response.trim().replace(/[\[\]\s]/g, "");
|
|
5394
|
-
const rankedIndices = cleanedResponse.split(",").map(Number).filter((n) => !isNaN(n));
|
|
5395
|
-
if (rankedIndices.length > 0) {
|
|
5396
|
-
const rankedMatches = [];
|
|
5397
|
-
const usedIndices = /* @__PURE__ */ new Set();
|
|
5398
|
-
for (const index of rankedIndices) {
|
|
5399
|
-
if (index >= 0 && index < topN.length && !usedIndices.has(index)) {
|
|
5400
|
-
rankedMatches.push(topN[index]);
|
|
5401
|
-
usedIndices.add(index);
|
|
5402
|
-
}
|
|
5403
|
-
}
|
|
5404
|
-
for (let i = 0; i < topN.length; i++) {
|
|
5405
|
-
if (!usedIndices.has(i)) {
|
|
5406
|
-
rankedMatches.push(topN[i]);
|
|
5407
|
-
}
|
|
5408
|
-
}
|
|
5409
|
-
const rest = matches.slice(10);
|
|
5410
|
-
rankedMatches.push(...rest);
|
|
5411
|
-
return rankedMatches.slice(0, limit);
|
|
5416
|
+
${docList}` }],
|
|
5417
|
+
"",
|
|
5418
|
+
{
|
|
5419
|
+
systemPrompt: 'You are a relevance ranking expert. Given the user query and a list of retrieved document chunks, rank the chunks by relevance to the query. Return the result as a comma-separated list of document indices in order of relevance, from most relevant to least relevant. Use the indices provided in brackets like [0], [1], etc. Only return the indices (e.g., "2,0,1"), nothing else. Do not include brackets in the output.',
|
|
5420
|
+
temperature: 0,
|
|
5421
|
+
maxTokens: 50
|
|
5412
5422
|
}
|
|
5413
|
-
|
|
5414
|
-
|
|
5423
|
+
);
|
|
5424
|
+
const cleanedResponse = response.trim().replace(/[\[\]\s]/g, "");
|
|
5425
|
+
const rankedIndices = cleanedResponse.split(",").map(Number).filter((n) => !isNaN(n));
|
|
5426
|
+
if (rankedIndices.length === 0) {
|
|
5427
|
+
return matches.sort((a, b) => b.score - a.score).slice(0, limit);
|
|
5428
|
+
}
|
|
5429
|
+
const rankedMatches = [];
|
|
5430
|
+
const usedIndices = /* @__PURE__ */ new Set();
|
|
5431
|
+
for (const index of rankedIndices) {
|
|
5432
|
+
if (index >= 0 && index < budgetedCandidates.length && !usedIndices.has(index)) {
|
|
5433
|
+
rankedMatches.push(budgetedCandidates[index]);
|
|
5434
|
+
usedIndices.add(index);
|
|
5435
|
+
}
|
|
5436
|
+
}
|
|
5437
|
+
for (let i = 0; i < budgetedCandidates.length; i++) {
|
|
5438
|
+
if (!usedIndices.has(i)) rankedMatches.push(budgetedCandidates[i]);
|
|
5415
5439
|
}
|
|
5416
|
-
|
|
5440
|
+
rankedMatches.push(...matches.slice(10));
|
|
5441
|
+
return rankedMatches.slice(0, limit);
|
|
5417
5442
|
}
|
|
5418
5443
|
};
|
|
5419
5444
|
|
|
@@ -5425,11 +5450,11 @@ var LlamaIndexIngestor = class {
|
|
|
5425
5450
|
* than standard character-count splitting.
|
|
5426
5451
|
*/
|
|
5427
5452
|
async chunk(text, options = {}) {
|
|
5428
|
-
var
|
|
5453
|
+
var _a2, _b;
|
|
5429
5454
|
try {
|
|
5430
5455
|
const { Document, SentenceSplitter, MetadataMode } = await import(`${"llamaindex"}`);
|
|
5431
5456
|
const splitter = new SentenceSplitter({
|
|
5432
|
-
chunkSize: (
|
|
5457
|
+
chunkSize: (_a2 = options.chunkSize) != null ? _a2 : 1e3,
|
|
5433
5458
|
chunkOverlap: (_b = options.chunkOverlap) != null ? _b : 200
|
|
5434
5459
|
});
|
|
5435
5460
|
const doc = new Document({ text, metadata: options.metadata || {} });
|
|
@@ -5537,7 +5562,7 @@ ${error instanceof Error ? error.message : String(error)}`
|
|
|
5537
5562
|
* The agent returns `{ messages: [...] }` — the last message is the final answer.
|
|
5538
5563
|
*/
|
|
5539
5564
|
async run(input, chatHistory = []) {
|
|
5540
|
-
var
|
|
5565
|
+
var _a2, _b, _c;
|
|
5541
5566
|
if (!this.agent) {
|
|
5542
5567
|
throw new Error("[LangChainAgent] Agent not initialized. Call initialize() first.");
|
|
5543
5568
|
}
|
|
@@ -5548,7 +5573,7 @@ ${error instanceof Error ? error.message : String(error)}`
|
|
|
5548
5573
|
const response = await this.agent.invoke({
|
|
5549
5574
|
messages: [...historyMessages, new HumanMessage(input)]
|
|
5550
5575
|
});
|
|
5551
|
-
const lastMessage = (
|
|
5576
|
+
const lastMessage = (_a2 = response == null ? void 0 : response.messages) == null ? void 0 : _a2.at(-1);
|
|
5552
5577
|
if (lastMessage && typeof lastMessage.content === "string") {
|
|
5553
5578
|
return lastMessage.content;
|
|
5554
5579
|
}
|
|
@@ -5599,7 +5624,7 @@ var MCPClient = class {
|
|
|
5599
5624
|
}
|
|
5600
5625
|
}
|
|
5601
5626
|
async connectStdio() {
|
|
5602
|
-
var
|
|
5627
|
+
var _a2;
|
|
5603
5628
|
const cmd = this.config.command;
|
|
5604
5629
|
if (!cmd) {
|
|
5605
5630
|
throw new Error(`[MCPClient] Command option is required for stdio transport on "${this.config.name}"`);
|
|
@@ -5614,7 +5639,7 @@ var MCPClient = class {
|
|
|
5614
5639
|
this.stdioBuffer += data.toString("utf-8");
|
|
5615
5640
|
this.processStdioLines();
|
|
5616
5641
|
});
|
|
5617
|
-
(
|
|
5642
|
+
(_a2 = this.childProcess.stderr) == null ? void 0 : _a2.on("data", (data) => {
|
|
5618
5643
|
console.warn(`[MCPClient] [stderr] [${this.config.name}]:`, data.toString("utf-8"));
|
|
5619
5644
|
});
|
|
5620
5645
|
this.childProcess.on("close", (code) => {
|
|
@@ -5688,14 +5713,14 @@ var MCPClient = class {
|
|
|
5688
5713
|
}
|
|
5689
5714
|
}
|
|
5690
5715
|
async sendNotification(method, params) {
|
|
5691
|
-
var
|
|
5716
|
+
var _a2;
|
|
5692
5717
|
const notification = {
|
|
5693
5718
|
jsonrpc: "2.0",
|
|
5694
5719
|
method,
|
|
5695
5720
|
params
|
|
5696
5721
|
};
|
|
5697
5722
|
if (this.config.transport === "stdio") {
|
|
5698
|
-
if ((
|
|
5723
|
+
if ((_a2 = this.childProcess) == null ? void 0 : _a2.stdin) {
|
|
5699
5724
|
this.childProcess.stdin.write(JSON.stringify(notification) + "\n", "utf-8");
|
|
5700
5725
|
}
|
|
5701
5726
|
} else if (this.sseUrl) {
|
|
@@ -5773,11 +5798,11 @@ var MCPRegistry = class {
|
|
|
5773
5798
|
// src/core/MultiAgentCoordinator.ts
|
|
5774
5799
|
var MultiAgentCoordinator = class {
|
|
5775
5800
|
constructor(options) {
|
|
5776
|
-
var
|
|
5801
|
+
var _a2;
|
|
5777
5802
|
this.llmProvider = options.llmProvider;
|
|
5778
5803
|
this.mcpRegistry = options.mcpRegistry;
|
|
5779
5804
|
this.documentSearch = options.documentSearch;
|
|
5780
|
-
this.maxIterations = (
|
|
5805
|
+
this.maxIterations = (_a2 = options.maxIterations) != null ? _a2 : 5;
|
|
5781
5806
|
}
|
|
5782
5807
|
/**
|
|
5783
5808
|
* Run the multi-agent coordination loop synchronously and return the final response.
|
|
@@ -5829,8 +5854,8 @@ Available Tools:
|
|
|
5829
5854
|
|
|
5830
5855
|
${mcpTools.length > 0 ? "MCP Server Tools:" : ""}
|
|
5831
5856
|
${mcpTools.map((mt) => {
|
|
5832
|
-
var
|
|
5833
|
-
return `- ${mt.tool.name}(${JSON.stringify(((
|
|
5857
|
+
var _a2;
|
|
5858
|
+
return `- ${mt.tool.name}(${JSON.stringify(((_a2 = mt.tool.inputSchema) == null ? void 0 : _a2.properties) || {})}): ${mt.tool.description || "No description provided."}`;
|
|
5834
5859
|
}).join("\n")}
|
|
5835
5860
|
|
|
5836
5861
|
Tool Calling Protocol:
|
|
@@ -5988,100 +6013,6 @@ ${toolResultText}`
|
|
|
5988
6013
|
}
|
|
5989
6014
|
};
|
|
5990
6015
|
|
|
5991
|
-
// src/core/CircuitBreaker.ts
|
|
5992
|
-
var CircuitBreaker = class {
|
|
5993
|
-
constructor(options = {}) {
|
|
5994
|
-
this.state = "closed";
|
|
5995
|
-
this.failureCount = 0;
|
|
5996
|
-
this.successCount = 0;
|
|
5997
|
-
this.lastFailureAt = 0;
|
|
5998
|
-
this.halfOpenCalls = 0;
|
|
5999
|
-
var _a3, _b, _c;
|
|
6000
|
-
this.failureThreshold = (_a3 = options.failureThreshold) != null ? _a3 : 5;
|
|
6001
|
-
this.resetTimeoutMs = (_b = options.resetTimeoutMs) != null ? _b : 3e4;
|
|
6002
|
-
this.halfOpenMaxCalls = (_c = options.halfOpenMaxCalls) != null ? _c : 1;
|
|
6003
|
-
}
|
|
6004
|
-
record(success) {
|
|
6005
|
-
const now = Date.now();
|
|
6006
|
-
if (success) {
|
|
6007
|
-
this.successCount++;
|
|
6008
|
-
if (this.state === "half-open") {
|
|
6009
|
-
this.state = "closed";
|
|
6010
|
-
this.failureCount = 0;
|
|
6011
|
-
this.halfOpenCalls = 0;
|
|
6012
|
-
this.successCount = 1;
|
|
6013
|
-
} else if (this.state === "closed") {
|
|
6014
|
-
if (this.successCount >= this.failureThreshold) {
|
|
6015
|
-
this.failureCount = Math.max(0, this.failureCount - 1);
|
|
6016
|
-
this.successCount = 0;
|
|
6017
|
-
}
|
|
6018
|
-
}
|
|
6019
|
-
} else {
|
|
6020
|
-
this.failureCount++;
|
|
6021
|
-
this.lastFailureAt = now;
|
|
6022
|
-
this.successCount = 0;
|
|
6023
|
-
if (this.state === "half-open") {
|
|
6024
|
-
this.state = "open";
|
|
6025
|
-
this.halfOpenCalls = 0;
|
|
6026
|
-
} else if (this.state === "closed" && this.failureCount >= this.failureThreshold) {
|
|
6027
|
-
this.state = "open";
|
|
6028
|
-
}
|
|
6029
|
-
}
|
|
6030
|
-
}
|
|
6031
|
-
canExecute() {
|
|
6032
|
-
const now = Date.now();
|
|
6033
|
-
if (this.state === "closed") {
|
|
6034
|
-
return true;
|
|
6035
|
-
}
|
|
6036
|
-
if (this.state === "open") {
|
|
6037
|
-
if (now - this.lastFailureAt >= this.resetTimeoutMs) {
|
|
6038
|
-
this.state = "half-open";
|
|
6039
|
-
this.halfOpenCalls = 0;
|
|
6040
|
-
return true;
|
|
6041
|
-
}
|
|
6042
|
-
return false;
|
|
6043
|
-
}
|
|
6044
|
-
if (this.state === "half-open") {
|
|
6045
|
-
return this.halfOpenCalls < this.halfOpenMaxCalls;
|
|
6046
|
-
}
|
|
6047
|
-
return true;
|
|
6048
|
-
}
|
|
6049
|
-
async wrap(fn) {
|
|
6050
|
-
if (!this.canExecute()) {
|
|
6051
|
-
const retryAfterSec = Math.ceil(
|
|
6052
|
-
Math.max(0, this.resetTimeoutMs - (Date.now() - this.lastFailureAt)) / 1e3
|
|
6053
|
-
);
|
|
6054
|
-
throw new Error(
|
|
6055
|
-
`Circuit breaker is open. Try again in ${retryAfterSec}s. Failed ${this.failureCount}/${this.failureThreshold} consecutive calls.`
|
|
6056
|
-
);
|
|
6057
|
-
}
|
|
6058
|
-
if (this.state === "half-open") {
|
|
6059
|
-
this.halfOpenCalls++;
|
|
6060
|
-
}
|
|
6061
|
-
try {
|
|
6062
|
-
const result = await fn();
|
|
6063
|
-
this.record(true);
|
|
6064
|
-
return result;
|
|
6065
|
-
} catch (err) {
|
|
6066
|
-
this.record(false);
|
|
6067
|
-
throw err;
|
|
6068
|
-
}
|
|
6069
|
-
}
|
|
6070
|
-
getState() {
|
|
6071
|
-
return this.state;
|
|
6072
|
-
}
|
|
6073
|
-
getFailureCount() {
|
|
6074
|
-
return this.failureCount;
|
|
6075
|
-
}
|
|
6076
|
-
reset() {
|
|
6077
|
-
this.state = "closed";
|
|
6078
|
-
this.failureCount = 0;
|
|
6079
|
-
this.successCount = 0;
|
|
6080
|
-
this.lastFailureAt = 0;
|
|
6081
|
-
this.halfOpenCalls = 0;
|
|
6082
|
-
}
|
|
6083
|
-
};
|
|
6084
|
-
|
|
6085
6016
|
// src/core/BatchProcessor.ts
|
|
6086
6017
|
function isTransientError(error) {
|
|
6087
6018
|
if (!(error instanceof Error)) return false;
|
|
@@ -6105,30 +6036,34 @@ function calculateBackoffDelay(attempt, initialDelayMs, maxDelayMs, multiplier)
|
|
|
6105
6036
|
function sleep(ms) {
|
|
6106
6037
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
6107
6038
|
}
|
|
6108
|
-
var
|
|
6039
|
+
var BatchProcessor = class _BatchProcessor {
|
|
6109
6040
|
/**
|
|
6110
|
-
* Execute a processor call through
|
|
6111
|
-
* Only transient failures count toward opening the circuit.
|
|
6041
|
+
* Execute a processor call, optionally through a caller-provided circuit
|
|
6042
|
+
* breaker. Only transient failures count toward opening the circuit.
|
|
6043
|
+
*
|
|
6044
|
+
* Fix ARCH-3: removed the static module-level `cb` field. Callers must
|
|
6045
|
+
* supply their own CircuitBreaker via BatchOptions.circuitBreaker.
|
|
6046
|
+
* Fix TYPE-1: uses the public retryAfterMs() and incrementHalfOpen()
|
|
6047
|
+
* API instead of accessing private fields via `as any`.
|
|
6112
6048
|
*/
|
|
6113
|
-
static async executeWithCircuitBreaker(processor) {
|
|
6114
|
-
if (!
|
|
6115
|
-
|
|
6116
|
-
|
|
6117
|
-
|
|
6049
|
+
static async executeWithCircuitBreaker(processor, cb) {
|
|
6050
|
+
if (!cb) {
|
|
6051
|
+
return processor();
|
|
6052
|
+
}
|
|
6053
|
+
if (!cb.canExecute()) {
|
|
6054
|
+
const retryAfterSec = Math.ceil(cb.retryAfterMs() / 1e3);
|
|
6118
6055
|
throw new Error(
|
|
6119
6056
|
`[BatchProcessor] Circuit breaker is open. Try again in ${retryAfterSec}s.`
|
|
6120
6057
|
);
|
|
6121
6058
|
}
|
|
6122
|
-
|
|
6123
|
-
this.cb.halfOpenCalls++;
|
|
6124
|
-
}
|
|
6059
|
+
cb.incrementHalfOpen();
|
|
6125
6060
|
try {
|
|
6126
6061
|
const result = await processor();
|
|
6127
|
-
|
|
6062
|
+
cb.record(true);
|
|
6128
6063
|
return result;
|
|
6129
6064
|
} catch (err) {
|
|
6130
6065
|
if (isTransientError(err)) {
|
|
6131
|
-
|
|
6066
|
+
cb.record(false);
|
|
6132
6067
|
}
|
|
6133
6068
|
throw err;
|
|
6134
6069
|
}
|
|
@@ -6173,7 +6108,10 @@ var _BatchProcessor = class _BatchProcessor {
|
|
|
6173
6108
|
let lastError;
|
|
6174
6109
|
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
6175
6110
|
try {
|
|
6176
|
-
const result = await _BatchProcessor.executeWithCircuitBreaker(
|
|
6111
|
+
const result = await _BatchProcessor.executeWithCircuitBreaker(
|
|
6112
|
+
() => processor(batch),
|
|
6113
|
+
options == null ? void 0 : options.circuitBreaker
|
|
6114
|
+
);
|
|
6177
6115
|
results.push(result);
|
|
6178
6116
|
success = true;
|
|
6179
6117
|
break;
|
|
@@ -6231,7 +6169,10 @@ ${errorMessages}`
|
|
|
6231
6169
|
let lastError;
|
|
6232
6170
|
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
6233
6171
|
try {
|
|
6234
|
-
const result = await _BatchProcessor.executeWithCircuitBreaker(
|
|
6172
|
+
const result = await _BatchProcessor.executeWithCircuitBreaker(
|
|
6173
|
+
() => processor(item),
|
|
6174
|
+
options == null ? void 0 : options.circuitBreaker
|
|
6175
|
+
);
|
|
6235
6176
|
results.push(result);
|
|
6236
6177
|
success = true;
|
|
6237
6178
|
break;
|
|
@@ -6304,7 +6245,10 @@ ${errorMessages}`
|
|
|
6304
6245
|
}));
|
|
6305
6246
|
const chunkPromises = chunk.map(async ({ item, originalIndex }) => {
|
|
6306
6247
|
try {
|
|
6307
|
-
const result = await _BatchProcessor.executeWithCircuitBreaker(
|
|
6248
|
+
const result = await _BatchProcessor.executeWithCircuitBreaker(
|
|
6249
|
+
() => processor(item),
|
|
6250
|
+
options == null ? void 0 : options.circuitBreaker
|
|
6251
|
+
);
|
|
6308
6252
|
return { success: true, result, index: originalIndex };
|
|
6309
6253
|
} catch (err) {
|
|
6310
6254
|
const error = err instanceof Error ? err : new Error(String(err));
|
|
@@ -6332,12 +6276,6 @@ ${errorMessages}`
|
|
|
6332
6276
|
return { results, errors, totalProcessed, totalFailed };
|
|
6333
6277
|
}
|
|
6334
6278
|
};
|
|
6335
|
-
_BatchProcessor.cb = new CircuitBreaker({
|
|
6336
|
-
failureThreshold: 5,
|
|
6337
|
-
resetTimeoutMs: 3e4,
|
|
6338
|
-
halfOpenMaxCalls: 1
|
|
6339
|
-
});
|
|
6340
|
-
var BatchProcessor = _BatchProcessor;
|
|
6341
6279
|
|
|
6342
6280
|
// src/config/EmbeddingStrategy.ts
|
|
6343
6281
|
var EmbeddingStrategyResolver = class {
|
|
@@ -6435,7 +6373,7 @@ var QueryProcessor = class {
|
|
|
6435
6373
|
* @param validFields Optional list of known filterable fields to look for
|
|
6436
6374
|
*/
|
|
6437
6375
|
static extractQueryFieldHints(question, validFields = []) {
|
|
6438
|
-
var
|
|
6376
|
+
var _a2, _b, _c, _d, _e, _f, _g2, _h;
|
|
6439
6377
|
if (!question.trim()) return [];
|
|
6440
6378
|
const hints = /* @__PURE__ */ new Map();
|
|
6441
6379
|
const addHint = (value, field) => {
|
|
@@ -6480,11 +6418,11 @@ var QueryProcessor = class {
|
|
|
6480
6418
|
if (name) addHint(name, "name");
|
|
6481
6419
|
}
|
|
6482
6420
|
}
|
|
6483
|
-
const genericPattern = /\b([a-z][a-z0-9_]{1,30})\s*[:=]\s*["'
|
|
6421
|
+
const genericPattern = /\b([a-z][a-z0-9_]{1,30})\s*[:=]\s*(?:"([^"\n?.!,]{1,60})"|'([^'\n?.!,]{1,60})'|([^\s\n?.!,:=]{1,60}))/gi;
|
|
6484
6422
|
for (const match of question.matchAll(genericPattern)) {
|
|
6485
6423
|
const field = match[1];
|
|
6486
|
-
const value = match[2];
|
|
6487
|
-
if (field && !this.isLikelyPromptPhrase(field)) {
|
|
6424
|
+
const value = (_b = (_a2 = match[2]) != null ? _a2 : match[3]) != null ? _b : match[4];
|
|
6425
|
+
if (field && value && !this.isLikelyPromptPhrase(field)) {
|
|
6488
6426
|
const resolvedField = resolveValidField(field);
|
|
6489
6427
|
if (resolvedField) {
|
|
6490
6428
|
addHint(value, resolvedField);
|
|
@@ -6515,7 +6453,7 @@ var QueryProcessor = class {
|
|
|
6515
6453
|
];
|
|
6516
6454
|
for (const p of universalPatterns) {
|
|
6517
6455
|
for (const match of question.matchAll(p.regex)) {
|
|
6518
|
-
const val = p.group ? (
|
|
6456
|
+
const val = p.group ? (_c = match[p.group]) != null ? _c : match[0] : match[0];
|
|
6519
6457
|
if (!val) continue;
|
|
6520
6458
|
if (p.field) addHint(val, p.field);
|
|
6521
6459
|
else addHint(val);
|
|
@@ -6523,22 +6461,23 @@ var QueryProcessor = class {
|
|
|
6523
6461
|
}
|
|
6524
6462
|
for (const pattern of naturalQuestionPatterns) {
|
|
6525
6463
|
for (const match of question.matchAll(pattern)) {
|
|
6526
|
-
const value = (
|
|
6464
|
+
const value = (_d = match[2]) != null ? _d : match[1];
|
|
6527
6465
|
if (value) addHint(value);
|
|
6528
6466
|
}
|
|
6529
6467
|
}
|
|
6530
6468
|
const fieldPattern = `([^\\n:=?.!,]{1,60}?)`;
|
|
6531
|
-
const valuePattern = `([
|
|
6469
|
+
const valuePattern = `(?:"([^"\\n?.!,]{1,120})"|'([^'\\n?.!,]{1,120})'|([^\\s\\n?.!,:=]{1,120}))`;
|
|
6532
6470
|
const fieldValuePatterns = [
|
|
6533
|
-
new RegExp(`\\b${fieldPattern}\\s*(?:=|:)\\s
|
|
6534
|
-
new RegExp(`\\b${fieldPattern}\\s+(?:is|are|was|were|equals?|equal to|named|called)\\s
|
|
6535
|
-
new RegExp(`\\bwith\\s+${fieldPattern}\\s
|
|
6536
|
-
new RegExp(`\\b(?:under|in|for|from|of)\\s+${fieldPattern}\\s
|
|
6471
|
+
new RegExp(`\\b${fieldPattern}\\s*(?:=|:)\\s*${valuePattern}`, "gi"),
|
|
6472
|
+
new RegExp(`\\b${fieldPattern}\\s+(?:is|are|was|were|equals?|equal to|named|called)\\s+${valuePattern}`, "gi"),
|
|
6473
|
+
new RegExp(`\\bwith\\s+${fieldPattern}\\s+${valuePattern}`, "gi"),
|
|
6474
|
+
new RegExp(`\\b(?:under|in|for|from|of)\\s+${fieldPattern}\\s+${valuePattern}`, "gi")
|
|
6537
6475
|
];
|
|
6538
6476
|
for (const pattern of fieldValuePatterns) {
|
|
6539
6477
|
for (const match of question.matchAll(pattern)) {
|
|
6540
|
-
const field = (
|
|
6541
|
-
const value = (
|
|
6478
|
+
const field = (_e = match[1]) != null ? _e : "";
|
|
6479
|
+
const value = (_h = (_g2 = (_f = match[2]) != null ? _f : match[3]) != null ? _g2 : match[4]) != null ? _h : "";
|
|
6480
|
+
if (!value) continue;
|
|
6542
6481
|
const resolvedField = resolveValidField(field);
|
|
6543
6482
|
if (resolvedField) {
|
|
6544
6483
|
addHint(value, resolvedField);
|
|
@@ -6608,10 +6547,10 @@ var QueryProcessor = class {
|
|
|
6608
6547
|
}
|
|
6609
6548
|
static normalizeNumericOperator(operator) {
|
|
6610
6549
|
const op = operator.toLowerCase().trim();
|
|
6611
|
-
if (op === ">" || /\b(greater than|more than|above|over)\b/.test(op)) return "gt";
|
|
6612
6550
|
if (op === ">=" || /\b(greater than or equal to|more than or equal to|at least)\b/.test(op)) return "gte";
|
|
6613
|
-
if (op === "<" || /\b(less than|below|under)\b/.test(op)) return "lt";
|
|
6614
6551
|
if (op === "<=" || /\b(less than or equal to|at most)\b/.test(op)) return "lte";
|
|
6552
|
+
if (op === ">" || /\b(greater than|more than|above|over)\b/.test(op)) return "gt";
|
|
6553
|
+
if (op === "<" || /\b(less than|below|under)\b/.test(op)) return "lt";
|
|
6615
6554
|
return "eq";
|
|
6616
6555
|
}
|
|
6617
6556
|
static normalizePredicateField(field, validFields) {
|
|
@@ -6739,11 +6678,11 @@ var LLMRouter = class {
|
|
|
6739
6678
|
* When provided it is used directly as the 'default' role without re-constructing.
|
|
6740
6679
|
*/
|
|
6741
6680
|
async initialize(prebuiltDefault) {
|
|
6742
|
-
var
|
|
6681
|
+
var _a2;
|
|
6743
6682
|
const defaultModel = prebuiltDefault != null ? prebuiltDefault : LLMFactory.create(this.config.llm, this.config.embedding);
|
|
6744
6683
|
this.models.set("default", defaultModel);
|
|
6745
6684
|
const envFastModel = process.env.FAST_LLM_MODEL;
|
|
6746
|
-
const providerFastDefault = (
|
|
6685
|
+
const providerFastDefault = (_a2 = FAST_MODEL_DEFAULTS[this.config.llm.provider]) != null ? _a2 : "";
|
|
6747
6686
|
const fastModelName = envFastModel || providerFastDefault;
|
|
6748
6687
|
if (fastModelName && fastModelName !== this.config.llm.model) {
|
|
6749
6688
|
const fastConfig = __spreadProps(__spreadValues({}, this.config.llm), {
|
|
@@ -6760,8 +6699,8 @@ var LLMRouter = class {
|
|
|
6760
6699
|
* Falls back to 'default' if the requested role is not registered.
|
|
6761
6700
|
*/
|
|
6762
6701
|
get(role) {
|
|
6763
|
-
var
|
|
6764
|
-
const provider = (
|
|
6702
|
+
var _a2;
|
|
6703
|
+
const provider = (_a2 = this.models.get(role)) != null ? _a2 : this.models.get("default");
|
|
6765
6704
|
if (!provider) {
|
|
6766
6705
|
throw new Error(`[LLMRouter] No provider registered for role: "${role}". Did you call initialize()?`);
|
|
6767
6706
|
}
|
|
@@ -6847,8 +6786,8 @@ var IntentClassifier = class {
|
|
|
6847
6786
|
numericFieldCount = numericKeys.size;
|
|
6848
6787
|
categoricalFieldCount = catKeys.size;
|
|
6849
6788
|
if (productKeyMatches >= 2 || docs.some((d) => {
|
|
6850
|
-
var
|
|
6851
|
-
return ((
|
|
6789
|
+
var _a2, _b;
|
|
6790
|
+
return ((_a2 = d == null ? void 0 : d.content) != null ? _a2 : "").toLowerCase().includes("price") || ((_b = d == null ? void 0 : d.content) != null ? _b : "").toLowerCase().includes("brand");
|
|
6852
6791
|
})) {
|
|
6853
6792
|
isProductLike = true;
|
|
6854
6793
|
}
|
|
@@ -7038,7 +6977,7 @@ var Rule6SmallResultSetRule = class {
|
|
|
7038
6977
|
var Rule7LargeTableRule = class {
|
|
7039
6978
|
constructor() {
|
|
7040
6979
|
this.name = "Rule7LargeTable";
|
|
7041
|
-
this.priority =
|
|
6980
|
+
this.priority = 82;
|
|
7042
6981
|
}
|
|
7043
6982
|
evaluate(context, intent) {
|
|
7044
6983
|
const docs = context.retrievedDocuments || [];
|
|
@@ -7081,8 +7020,8 @@ var RuleEngine = class {
|
|
|
7081
7020
|
this.registerRule(new Rule5MixedResponseRule());
|
|
7082
7021
|
this.registerRule(new Rule2ComparisonRule());
|
|
7083
7022
|
this.registerRule(new Rule3ProductDiscoveryRule());
|
|
7084
|
-
this.registerRule(new Rule4AnalyticalRule());
|
|
7085
7023
|
this.registerRule(new Rule7LargeTableRule());
|
|
7024
|
+
this.registerRule(new Rule4AnalyticalRule());
|
|
7086
7025
|
}
|
|
7087
7026
|
/**
|
|
7088
7027
|
* Add or replace a rule in the engine.
|
|
@@ -7130,8 +7069,8 @@ var TextRendererStrategy = class {
|
|
|
7130
7069
|
}
|
|
7131
7070
|
const docs = Array.isArray(data) ? data : [];
|
|
7132
7071
|
const text = docs.map((d) => {
|
|
7133
|
-
var
|
|
7134
|
-
return (
|
|
7072
|
+
var _a2;
|
|
7073
|
+
return (_a2 = d == null ? void 0 : d.content) != null ? _a2 : "";
|
|
7135
7074
|
}).join("\n\n");
|
|
7136
7075
|
return { type: "text", title, data: { content: text || "No detailed content available." } };
|
|
7137
7076
|
}
|
|
@@ -7274,9 +7213,9 @@ var LRUDecisionCache = class {
|
|
|
7274
7213
|
this.maxSize = maxSize;
|
|
7275
7214
|
}
|
|
7276
7215
|
generateKey(context) {
|
|
7277
|
-
var
|
|
7216
|
+
var _a2, _b;
|
|
7278
7217
|
const q = (context.userQuery || "").toLowerCase().trim();
|
|
7279
|
-
const docCount = (_b = (
|
|
7218
|
+
const docCount = (_b = (_a2 = context.retrievedDocuments) == null ? void 0 : _a2.length) != null ? _b : 0;
|
|
7280
7219
|
return `${q}::docs:${docCount}`;
|
|
7281
7220
|
}
|
|
7282
7221
|
get(context) {
|
|
@@ -7377,7 +7316,7 @@ var UITransformer = class _UITransformer {
|
|
|
7377
7316
|
* Prefer `analyzeAndDecide()` in production.
|
|
7378
7317
|
*/
|
|
7379
7318
|
static transform(userQuery, retrievedData, config, trainedSchema, intent) {
|
|
7380
|
-
var
|
|
7319
|
+
var _a2, _b, _c;
|
|
7381
7320
|
if (!retrievedData || retrievedData.length === 0) {
|
|
7382
7321
|
return this.createTextResponse("No data available", "No relevant data found for your query.");
|
|
7383
7322
|
}
|
|
@@ -7397,7 +7336,7 @@ var UITransformer = class _UITransformer {
|
|
|
7397
7336
|
return this.transformToBarChart(filteredData, profile, userQuery, resolvedIntent.visualizationHint === "ranking");
|
|
7398
7337
|
}
|
|
7399
7338
|
if (resolvedIntent.visualizationHint === "distribution") {
|
|
7400
|
-
return (
|
|
7339
|
+
return (_a2 = this.transformToHistogram(profile, userQuery)) != null ? _a2 : this.transformToBarChart(filteredData, profile, userQuery);
|
|
7401
7340
|
}
|
|
7402
7341
|
if (resolvedIntent.visualizationHint === "correlation") {
|
|
7403
7342
|
return (_b = this.transformToScatterPlot(profile, userQuery)) != null ? _b : this.transformToBarChart(filteredData, profile, userQuery);
|
|
@@ -7730,11 +7669,11 @@ ${schemaProfileText}` : ""}`;
|
|
|
7730
7669
|
};
|
|
7731
7670
|
}
|
|
7732
7671
|
static transformToPieChart(data, profile, query = "") {
|
|
7733
|
-
var
|
|
7672
|
+
var _a2;
|
|
7734
7673
|
const dimension = profile ? this.selectDimensionField(profile, query) : void 0;
|
|
7735
7674
|
const categories = dimension ? Array.from(new Set(profile.records.map((record) => {
|
|
7736
|
-
var
|
|
7737
|
-
return String((
|
|
7675
|
+
var _a3;
|
|
7676
|
+
return String((_a3 = record.fields[dimension.key]) != null ? _a3 : "");
|
|
7738
7677
|
}).filter(Boolean))) : this.detectCategories(data);
|
|
7739
7678
|
if (categories.length === 0) return null;
|
|
7740
7679
|
const categoryData = dimension && profile ? this.aggregateProfileByDimension(profile, dimension.key) : this.aggregateByCategory(data, categories);
|
|
@@ -7744,7 +7683,7 @@ ${schemaProfileText}` : ""}`;
|
|
|
7744
7683
|
});
|
|
7745
7684
|
return {
|
|
7746
7685
|
type: "pie_chart",
|
|
7747
|
-
title: `Distribution by ${(
|
|
7686
|
+
title: `Distribution by ${(_a2 = dimension == null ? void 0 : dimension.label) != null ? _a2 : "Category"}`,
|
|
7748
7687
|
description: `Showing breakdown across ${pieData.length} categories`,
|
|
7749
7688
|
data: pieData
|
|
7750
7689
|
};
|
|
@@ -7754,8 +7693,8 @@ ${schemaProfileText}` : ""}`;
|
|
|
7754
7693
|
const valueField = profile.numericFields[0];
|
|
7755
7694
|
const buckets = /* @__PURE__ */ new Map();
|
|
7756
7695
|
profile.records.forEach((record) => {
|
|
7757
|
-
var
|
|
7758
|
-
const timestamp = String((
|
|
7696
|
+
var _a2, _b, _c;
|
|
7697
|
+
const timestamp = String((_a2 = record.fields[dateField.key]) != null ? _a2 : "");
|
|
7759
7698
|
const value = (_b = this.toFiniteNumber(record.fields[valueField.key])) != null ? _b : 0;
|
|
7760
7699
|
buckets.set(timestamp, ((_c = buckets.get(timestamp)) != null ? _c : 0) + value);
|
|
7761
7700
|
});
|
|
@@ -7768,23 +7707,23 @@ ${schemaProfileText}` : ""}`;
|
|
|
7768
7707
|
};
|
|
7769
7708
|
}
|
|
7770
7709
|
static transformToBarChart(data, profile, query = "", horizontal = false) {
|
|
7771
|
-
var
|
|
7710
|
+
var _a2;
|
|
7772
7711
|
const dimension = profile ? this.selectDimensionField(profile, query) : void 0;
|
|
7773
7712
|
const measure = profile ? this.selectNumericField(profile, query) : void 0;
|
|
7774
7713
|
const aggregate = dimension && profile ? this.aggregateProfileByDimension(profile, dimension.key, measure == null ? void 0 : measure.key) : this.aggregateByCategory(data, this.detectCategories(data));
|
|
7775
7714
|
const barData = Object.entries(aggregate).map(([category, value]) => ({ category, value: Number(value) })).sort((a, b) => horizontal ? b.value - a.value : 0).slice(0, 12);
|
|
7776
7715
|
const fallbackData = barData.length > 0 ? barData : data.slice(0, 12).map((item, index) => {
|
|
7777
|
-
var
|
|
7716
|
+
var _a3, _b, _c, _d, _e;
|
|
7778
7717
|
const meta = item.metadata || {};
|
|
7779
7718
|
const label = String(
|
|
7780
|
-
(_c = (_b = (
|
|
7719
|
+
(_c = (_b = (_a3 = this.getDynamicVal(meta, "name")) != null ? _a3 : meta.label) != null ? _b : meta.title) != null ? _c : `Result ${index + 1}`
|
|
7781
7720
|
);
|
|
7782
7721
|
const value = (_e = (_d = this.extractNumericValue(meta)) != null ? _d : item.score) != null ? _e : 0;
|
|
7783
7722
|
return { category: label, value: Number(value) };
|
|
7784
7723
|
});
|
|
7785
7724
|
return {
|
|
7786
7725
|
type: horizontal ? "horizontal_bar" : "bar_chart",
|
|
7787
|
-
title: dimension ? `${(
|
|
7726
|
+
title: dimension ? `${(_a2 = measure == null ? void 0 : measure.label) != null ? _a2 : "Count"} by ${dimension.label}` : "Comparison",
|
|
7788
7727
|
description: `Showing ${fallbackData.length} comparable values`,
|
|
7789
7728
|
data: fallbackData
|
|
7790
7729
|
};
|
|
@@ -7819,11 +7758,11 @@ ${schemaProfileText}` : ""}`;
|
|
|
7819
7758
|
if (fields.length < 2) return null;
|
|
7820
7759
|
const [xField, yField] = fields;
|
|
7821
7760
|
const points = profile.records.map((record) => {
|
|
7822
|
-
var
|
|
7761
|
+
var _a2;
|
|
7823
7762
|
const x = this.toFiniteNumber(record.fields[xField.key]);
|
|
7824
7763
|
const y = this.toFiniteNumber(record.fields[yField.key]);
|
|
7825
7764
|
if (x === null || y === null) return null;
|
|
7826
|
-
return { x, y, label: String((
|
|
7765
|
+
return { x, y, label: String((_a2 = this.getRecordLabel(record)) != null ? _a2 : record.id) };
|
|
7827
7766
|
}).filter((point) => point !== null).slice(0, 100);
|
|
7828
7767
|
if (points.length === 0) return null;
|
|
7829
7768
|
return {
|
|
@@ -7853,9 +7792,9 @@ ${schemaProfileText}` : ""}`;
|
|
|
7853
7792
|
static transformToRadarChart(data) {
|
|
7854
7793
|
const attributeMap = {};
|
|
7855
7794
|
data.forEach((item) => {
|
|
7856
|
-
var
|
|
7795
|
+
var _a2, _b, _c;
|
|
7857
7796
|
const meta = item.metadata || {};
|
|
7858
|
-
const seriesName = String((_c = (_b = (
|
|
7797
|
+
const seriesName = String((_c = (_b = (_a2 = meta.name) != null ? _a2 : meta.product) != null ? _b : item.id) != null ? _c : "Item");
|
|
7859
7798
|
Object.entries(meta).forEach(([key, val]) => {
|
|
7860
7799
|
if (typeof val === "number" && !["price", "id"].includes(key.toLowerCase())) {
|
|
7861
7800
|
if (!attributeMap[key]) attributeMap[key] = {};
|
|
@@ -7871,8 +7810,8 @@ ${schemaProfileText}` : ""}`;
|
|
|
7871
7810
|
title: "Product Comparison",
|
|
7872
7811
|
description: `Comparing ${data.length} items across ${radarData.length} attributes`,
|
|
7873
7812
|
data: radarData.length > 0 ? radarData : data.map((d) => {
|
|
7874
|
-
var
|
|
7875
|
-
return { attribute: ((
|
|
7813
|
+
var _a2;
|
|
7814
|
+
return { attribute: ((_a2 = d == null ? void 0 : d.content) != null ? _a2 : "").substring(0, 40) };
|
|
7876
7815
|
})
|
|
7877
7816
|
};
|
|
7878
7817
|
}
|
|
@@ -7891,8 +7830,8 @@ ${schemaProfileText}` : ""}`;
|
|
|
7891
7830
|
return this.createTextResponse(
|
|
7892
7831
|
"Retrieved Context",
|
|
7893
7832
|
data.map((item) => {
|
|
7894
|
-
var
|
|
7895
|
-
return (
|
|
7833
|
+
var _a2;
|
|
7834
|
+
return (_a2 = item == null ? void 0 : item.content) != null ? _a2 : "";
|
|
7896
7835
|
}).join("\n\n"),
|
|
7897
7836
|
`Found ${data.length} relevant results`
|
|
7898
7837
|
);
|
|
@@ -7943,11 +7882,11 @@ ${schemaProfileText}` : ""}`;
|
|
|
7943
7882
|
return null;
|
|
7944
7883
|
}
|
|
7945
7884
|
static normalizeTransformation(payload) {
|
|
7946
|
-
var
|
|
7885
|
+
var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j;
|
|
7947
7886
|
if (!payload || typeof payload !== "object") return null;
|
|
7948
7887
|
const p = payload;
|
|
7949
7888
|
const type = this.normalizeVisualizationType(
|
|
7950
|
-
String((_c = (_b = (
|
|
7889
|
+
String((_c = (_b = (_a2 = p.type) != null ? _a2 : p.view) != null ? _b : p.chartType) != null ? _c : "")
|
|
7951
7890
|
);
|
|
7952
7891
|
if (!type) return null;
|
|
7953
7892
|
const title = String((_e = (_d = p.title) != null ? _d : p.heading) != null ? _e : "Visualization");
|
|
@@ -7958,7 +7897,7 @@ ${schemaProfileText}` : ""}`;
|
|
|
7958
7897
|
return this.validateTransformation(transformation) ? transformation : null;
|
|
7959
7898
|
}
|
|
7960
7899
|
static normalizeVisualizationType(type) {
|
|
7961
|
-
var
|
|
7900
|
+
var _a2;
|
|
7962
7901
|
const mapping = {
|
|
7963
7902
|
pie: "pie_chart",
|
|
7964
7903
|
pie_chart: "pie_chart",
|
|
@@ -7986,7 +7925,7 @@ ${schemaProfileText}` : ""}`;
|
|
|
7986
7925
|
product_carousel: "product_carousel",
|
|
7987
7926
|
carousel: "carousel"
|
|
7988
7927
|
};
|
|
7989
|
-
return (
|
|
7928
|
+
return (_a2 = mapping[type.toLowerCase()]) != null ? _a2 : null;
|
|
7990
7929
|
}
|
|
7991
7930
|
static validateTransformation(t) {
|
|
7992
7931
|
const { type, data } = t;
|
|
@@ -8102,7 +8041,7 @@ ${schemaProfileText}` : ""}`;
|
|
|
8102
8041
|
}
|
|
8103
8042
|
static profileData(data) {
|
|
8104
8043
|
const records = (data || []).filter((item) => Boolean(item && typeof item === "object")).map((item) => {
|
|
8105
|
-
var
|
|
8044
|
+
var _a2, _b;
|
|
8106
8045
|
const fields2 = {};
|
|
8107
8046
|
Object.entries(item.metadata || {}).forEach(([key, value]) => {
|
|
8108
8047
|
const primitive = this.toPrimitive(value);
|
|
@@ -8111,7 +8050,7 @@ ${schemaProfileText}` : ""}`;
|
|
|
8111
8050
|
if (!fields2.content && item.content) fields2.content = item.content.substring(0, 500);
|
|
8112
8051
|
return {
|
|
8113
8052
|
id: item.id,
|
|
8114
|
-
content: (
|
|
8053
|
+
content: (_a2 = item.content) != null ? _a2 : "",
|
|
8115
8054
|
score: (_b = item.score) != null ? _b : 0,
|
|
8116
8055
|
fields: fields2,
|
|
8117
8056
|
source: item
|
|
@@ -8192,16 +8131,16 @@ ${schemaProfileText}` : ""}`;
|
|
|
8192
8131
|
return null;
|
|
8193
8132
|
}
|
|
8194
8133
|
static selectDimensionField(profile, query) {
|
|
8195
|
-
var
|
|
8134
|
+
var _a2, _b;
|
|
8196
8135
|
const productCategory = profile.categoricalFields.find(
|
|
8197
8136
|
(field) => /category|department|collection|type|group|segment|region|country|state|city/i.test(field.key)
|
|
8198
8137
|
);
|
|
8199
8138
|
const ranked = this.rankFieldsByQuery(profile.categoricalFields, query);
|
|
8200
|
-
return (_b = (
|
|
8139
|
+
return (_b = (_a2 = ranked[0]) != null ? _a2 : productCategory) != null ? _b : profile.categoricalFields[0];
|
|
8201
8140
|
}
|
|
8202
8141
|
static selectNumericField(profile, query) {
|
|
8203
|
-
var
|
|
8204
|
-
return (
|
|
8142
|
+
var _a2;
|
|
8143
|
+
return (_a2 = this.rankFieldsByQuery(profile.numericFields, query)[0]) != null ? _a2 : profile.numericFields[0];
|
|
8205
8144
|
}
|
|
8206
8145
|
static rankFieldsByQuery(fields, query) {
|
|
8207
8146
|
const q = query.toLowerCase();
|
|
@@ -8230,8 +8169,8 @@ ${schemaProfileText}` : ""}`;
|
|
|
8230
8169
|
static aggregateProfileByDimension(profile, dimensionKey, measureKey) {
|
|
8231
8170
|
const result = {};
|
|
8232
8171
|
profile.records.forEach((record) => {
|
|
8233
|
-
var
|
|
8234
|
-
const category = String((
|
|
8172
|
+
var _a2, _b, _c;
|
|
8173
|
+
const category = String((_a2 = record.fields[dimensionKey]) != null ? _a2 : "Other").trim() || "Other";
|
|
8235
8174
|
const value = measureKey ? (_b = this.toFiniteNumber(record.fields[measureKey])) != null ? _b : 0 : 1;
|
|
8236
8175
|
result[category] = ((_c = result[category]) != null ? _c : 0) + value;
|
|
8237
8176
|
});
|
|
@@ -8311,8 +8250,8 @@ ${schemaProfileText}` : ""}`;
|
|
|
8311
8250
|
static aggregateByCategory(data, categories) {
|
|
8312
8251
|
const result = Object.fromEntries(categories.map((c) => [c, 0]));
|
|
8313
8252
|
data.forEach((item) => {
|
|
8314
|
-
var
|
|
8315
|
-
const cat = (
|
|
8253
|
+
var _a2;
|
|
8254
|
+
const cat = (_a2 = this.getProductCategory(item)) != null ? _a2 : "Other";
|
|
8316
8255
|
if (Object.prototype.hasOwnProperty.call(result, cat)) result[cat]++;
|
|
8317
8256
|
else result["Other"] = (result["Other"] || 0) + 1;
|
|
8318
8257
|
});
|
|
@@ -8320,17 +8259,17 @@ ${schemaProfileText}` : ""}`;
|
|
|
8320
8259
|
}
|
|
8321
8260
|
static extractTimeSeriesData(data) {
|
|
8322
8261
|
return data.map((item) => {
|
|
8323
|
-
var
|
|
8262
|
+
var _a2, _b, _c, _d, _e, _f;
|
|
8324
8263
|
const meta = item.metadata || {};
|
|
8325
8264
|
return {
|
|
8326
|
-
timestamp: (_b = (
|
|
8265
|
+
timestamp: (_b = (_a2 = meta.timestamp) != null ? _a2 : meta.date) != null ? _b : (/* @__PURE__ */ new Date()).toISOString(),
|
|
8327
8266
|
value: (_d = (_c = meta.value) != null ? _c : item.score) != null ? _d : 0,
|
|
8328
8267
|
label: (_f = meta.label) != null ? _f : ((_e = item == null ? void 0 : item.content) != null ? _e : "").substring(0, 50)
|
|
8329
8268
|
};
|
|
8330
8269
|
});
|
|
8331
8270
|
}
|
|
8332
8271
|
static extractNumericValue(meta) {
|
|
8333
|
-
var
|
|
8272
|
+
var _a2;
|
|
8334
8273
|
const preferredKeys = [
|
|
8335
8274
|
"value",
|
|
8336
8275
|
"count",
|
|
@@ -8345,7 +8284,7 @@ ${schemaProfileText}` : ""}`;
|
|
|
8345
8284
|
"price"
|
|
8346
8285
|
];
|
|
8347
8286
|
for (const key of preferredKeys) {
|
|
8348
|
-
const raw = (
|
|
8287
|
+
const raw = (_a2 = resolveMetadataValue(meta, key)) != null ? _a2 : meta[key];
|
|
8349
8288
|
const value = typeof raw === "number" ? raw : Number(String(raw != null ? raw : "").replace(/[$,% ,]/g, ""));
|
|
8350
8289
|
if (Number.isFinite(value)) return value;
|
|
8351
8290
|
}
|
|
@@ -8440,8 +8379,8 @@ ${schemaProfileText}` : ""}`;
|
|
|
8440
8379
|
}, query.includes(normalizedField) ? 3 : 0);
|
|
8441
8380
|
}
|
|
8442
8381
|
static resolveTableCellValue(item, column) {
|
|
8443
|
-
var
|
|
8444
|
-
if (column === "Content") return ((
|
|
8382
|
+
var _a2, _b, _c;
|
|
8383
|
+
if (column === "Content") return ((_a2 = item == null ? void 0 : item.content) != null ? _a2 : "").substring(0, 100);
|
|
8445
8384
|
const meta = item.metadata || {};
|
|
8446
8385
|
const normalizedColumn = this.normalizeComparableField(column);
|
|
8447
8386
|
const exactMetadata = Object.entries(meta).find(
|
|
@@ -8493,8 +8432,8 @@ ${schemaProfileText}` : ""}`;
|
|
|
8493
8432
|
let inStock = 0;
|
|
8494
8433
|
let outOfStock = 0;
|
|
8495
8434
|
data.forEach((d) => {
|
|
8496
|
-
var
|
|
8497
|
-
const cat = (
|
|
8435
|
+
var _a2, _b;
|
|
8436
|
+
const cat = (_a2 = this.getProductCategory(d)) != null ? _a2 : "Other";
|
|
8498
8437
|
if (cat === category) {
|
|
8499
8438
|
const quantity = (_b = this.extractStockQuantity(d)) != null ? _b : 1;
|
|
8500
8439
|
if (this.determineStockStatus(d)) inStock += quantity;
|
|
@@ -8540,9 +8479,9 @@ ${schemaProfileText}` : ""}`;
|
|
|
8540
8479
|
}
|
|
8541
8480
|
// ─── Product Extraction ───────────────────────────────────────────────────
|
|
8542
8481
|
static getDynamicVal(meta, uiKey, config, trainedSchema) {
|
|
8543
|
-
var
|
|
8482
|
+
var _a2;
|
|
8544
8483
|
if (!meta) return void 0;
|
|
8545
|
-
const mapping = (
|
|
8484
|
+
const mapping = (_a2 = config == null ? void 0 : config.rag) == null ? void 0 : _a2.uiMapping;
|
|
8546
8485
|
if ((mapping == null ? void 0 : mapping[uiKey]) && meta[mapping[uiKey]] !== void 0) return meta[mapping[uiKey]];
|
|
8547
8486
|
if (trainedSchema && typeof trainedSchema === "object") {
|
|
8548
8487
|
const trainedKey = trainedSchema[uiKey];
|
|
@@ -8551,7 +8490,7 @@ ${schemaProfileText}` : ""}`;
|
|
|
8551
8490
|
return resolveMetadataValue(meta, uiKey);
|
|
8552
8491
|
}
|
|
8553
8492
|
static extractProductInfo(item, config, trainedSchema) {
|
|
8554
|
-
var
|
|
8493
|
+
var _a2, _b;
|
|
8555
8494
|
if (!item) return null;
|
|
8556
8495
|
const meta = item.metadata || {};
|
|
8557
8496
|
const content = item.content || "";
|
|
@@ -8559,7 +8498,7 @@ ${schemaProfileText}` : ""}`;
|
|
|
8559
8498
|
const price = this.getDynamicVal(meta, "price", config, trainedSchema);
|
|
8560
8499
|
const brand = this.getDynamicVal(meta, "brand", config, trainedSchema);
|
|
8561
8500
|
const description = this.cleanProductDescription(
|
|
8562
|
-
(
|
|
8501
|
+
(_a2 = this.extractProductDescriptionFromContent(content)) != null ? _a2 : this.getProductDescriptionValue(meta, config, trainedSchema)
|
|
8563
8502
|
);
|
|
8564
8503
|
if (name || this.isProductData(item)) {
|
|
8565
8504
|
let finalName = name ? String(name) : void 0;
|
|
@@ -8694,10 +8633,10 @@ RULES:
|
|
|
8694
8633
|
}
|
|
8695
8634
|
static buildContextSummary(sources, maxChars = 6e3) {
|
|
8696
8635
|
const items = (sources || []).filter(Boolean).map((s, i) => {
|
|
8697
|
-
var
|
|
8636
|
+
var _a2, _b, _c, _d;
|
|
8698
8637
|
return {
|
|
8699
8638
|
index: i + 1,
|
|
8700
|
-
content: (_b = (
|
|
8639
|
+
content: (_b = (_a2 = s == null ? void 0 : s.content) == null ? void 0 : _a2.substring(0, 400)) != null ? _b : "",
|
|
8701
8640
|
metadata: (_c = s == null ? void 0 : s.metadata) != null ? _c : {},
|
|
8702
8641
|
score: (_d = s == null ? void 0 : s.score) != null ? _d : 0
|
|
8703
8642
|
};
|
|
@@ -8737,7 +8676,7 @@ var SchemaMapper = class {
|
|
|
8737
8676
|
return promise;
|
|
8738
8677
|
}
|
|
8739
8678
|
static async _doTrain(llm, cacheKey, keys) {
|
|
8740
|
-
var
|
|
8679
|
+
var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k;
|
|
8741
8680
|
const propertyList = Object.entries(this.TARGET_PROPERTIES).map(([prop, desc]) => `- ${prop} (${desc})`).join("\n");
|
|
8742
8681
|
const messages = [
|
|
8743
8682
|
{ role: "system", content: "You are a database schema expert. Respond ONLY with valid JSON." },
|
|
@@ -8752,7 +8691,7 @@ Return a JSON object like {"name":"Title","price":"Price",...}. Omit unmapped pr
|
|
|
8752
8691
|
}
|
|
8753
8692
|
];
|
|
8754
8693
|
try {
|
|
8755
|
-
const baseUrl = (
|
|
8694
|
+
const baseUrl = (_a2 = llm == null ? void 0 : llm.baseUrl) != null ? _a2 : "";
|
|
8756
8695
|
const apiKey = (_b = llm == null ? void 0 : llm.apiKey) != null ? _b : "";
|
|
8757
8696
|
const model = (_c = llm == null ? void 0 : llm.model) != null ? _c : "llama-3.1-8b-instant";
|
|
8758
8697
|
let responseText;
|
|
@@ -8927,9 +8866,9 @@ var Pipeline = class {
|
|
|
8927
8866
|
this.initialised = false;
|
|
8928
8867
|
/** Namespace-specific static cold context cache for CAG */
|
|
8929
8868
|
this.coldContexts = /* @__PURE__ */ new Map();
|
|
8930
|
-
var
|
|
8869
|
+
var _a2, _b, _c, _d, _e;
|
|
8931
8870
|
this.chunker = new DocumentChunker(
|
|
8932
|
-
(_b = (
|
|
8871
|
+
(_b = (_a2 = config.rag) == null ? void 0 : _a2.chunkSize) != null ? _b : 1e3,
|
|
8933
8872
|
(_d = (_c = config.rag) == null ? void 0 : _c.chunkOverlap) != null ? _d : 200
|
|
8934
8873
|
);
|
|
8935
8874
|
if (((_e = config.rag) == null ? void 0 : _e.chunkingStrategy) === "llamaindex") {
|
|
@@ -8945,7 +8884,7 @@ var Pipeline = class {
|
|
|
8945
8884
|
return this.initialised ? this.llmProvider : void 0;
|
|
8946
8885
|
}
|
|
8947
8886
|
async initialize() {
|
|
8948
|
-
var
|
|
8887
|
+
var _a2, _b, _c, _d;
|
|
8949
8888
|
if (this.initialised) return;
|
|
8950
8889
|
const chartInstruction = `You are a helpful assistant. Use the provided context to answer questions accurately.
|
|
8951
8890
|
|
|
@@ -8988,7 +8927,7 @@ var Pipeline = class {
|
|
|
8988
8927
|
this.entityExtractor = new EntityExtractor(this.llmProvider);
|
|
8989
8928
|
}
|
|
8990
8929
|
await this.vectorDB.initialize();
|
|
8991
|
-
if (((
|
|
8930
|
+
if (((_a2 = this.config.rag) == null ? void 0 : _a2.architecture) === "agentic" || ((_b = this.config.rag) == null ? void 0 : _b.architecture) === "multi-agent" || this.config.mcpServers && this.config.mcpServers.length > 0) {
|
|
8992
8931
|
this.mcpRegistry = new MCPRegistry(this.config.mcpServers || []);
|
|
8993
8932
|
this.multiAgentCoordinator = new MultiAgentCoordinator({
|
|
8994
8933
|
llmProvider: this.llmProvider,
|
|
@@ -9006,8 +8945,8 @@ var Pipeline = class {
|
|
|
9006
8945
|
this.initialised = true;
|
|
9007
8946
|
}
|
|
9008
8947
|
async loadColdContext(ns) {
|
|
9009
|
-
var
|
|
9010
|
-
const cagConfig = (
|
|
8948
|
+
var _a2, _b;
|
|
8949
|
+
const cagConfig = (_a2 = this.config.rag) == null ? void 0 : _a2.cag;
|
|
9011
8950
|
if (!cagConfig || !cagConfig.enabled) return;
|
|
9012
8951
|
try {
|
|
9013
8952
|
const coldNs = cagConfig.coldNamespace || ns;
|
|
@@ -9060,12 +8999,12 @@ ${m.content}`).join("\n\n---\n\n");
|
|
|
9060
8999
|
}
|
|
9061
9000
|
/** Step 1: Chunk the document content. */
|
|
9062
9001
|
async prepareChunks(doc) {
|
|
9063
|
-
var
|
|
9002
|
+
var _a2, _b;
|
|
9064
9003
|
if (this.llamaIngestor) {
|
|
9065
9004
|
return this.llamaIngestor.chunk(doc.content, {
|
|
9066
9005
|
docId: doc.docId,
|
|
9067
9006
|
metadata: doc.metadata,
|
|
9068
|
-
chunkSize: (
|
|
9007
|
+
chunkSize: (_a2 = this.config.rag) == null ? void 0 : _a2.chunkSize,
|
|
9069
9008
|
chunkOverlap: (_b = this.config.rag) == null ? void 0 : _b.chunkOverlap
|
|
9070
9009
|
});
|
|
9071
9010
|
}
|
|
@@ -9163,9 +9102,9 @@ ${m.content}`).join("\n\n---\n\n");
|
|
|
9163
9102
|
return { reply, sources };
|
|
9164
9103
|
}
|
|
9165
9104
|
async ask(question, history = [], namespace) {
|
|
9166
|
-
var
|
|
9105
|
+
var _a2, _b;
|
|
9167
9106
|
await this.initialize();
|
|
9168
|
-
if ((((
|
|
9107
|
+
if ((((_a2 = this.config.rag) == null ? void 0 : _a2.architecture) === "agentic" || ((_b = this.config.rag) == null ? void 0 : _b.architecture) === "multi-agent" || this.config.mcpServers && this.config.mcpServers.length > 0) && this.multiAgentCoordinator) {
|
|
9169
9108
|
return await this.multiAgentCoordinator.run(question, history);
|
|
9170
9109
|
}
|
|
9171
9110
|
const stream = this.askStream(question, history, namespace);
|
|
@@ -9200,9 +9139,9 @@ ${m.content}`).join("\n\n---\n\n");
|
|
|
9200
9139
|
}
|
|
9201
9140
|
askStream(_0) {
|
|
9202
9141
|
return __asyncGenerator(this, arguments, function* (question, history = [], namespace) {
|
|
9203
|
-
var
|
|
9142
|
+
var _a2, _b;
|
|
9204
9143
|
yield new __await(this.initialize());
|
|
9205
|
-
if ((((
|
|
9144
|
+
if ((((_a2 = this.config.rag) == null ? void 0 : _a2.architecture) === "agentic" || ((_b = this.config.rag) == null ? void 0 : _b.architecture) === "multi-agent" || this.config.mcpServers && this.config.mcpServers.length > 0) && this.multiAgentCoordinator) {
|
|
9206
9145
|
const stream2 = this.multiAgentCoordinator.runStream(question, history);
|
|
9207
9146
|
try {
|
|
9208
9147
|
for (var iter = __forAwait(stream2), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
|
|
@@ -9251,10 +9190,10 @@ ${m.content}`).join("\n\n---\n\n");
|
|
|
9251
9190
|
*/
|
|
9252
9191
|
askStreamInternal(_0) {
|
|
9253
9192
|
return __asyncGenerator(this, arguments, function* (question, history = [], namespace) {
|
|
9254
|
-
var
|
|
9193
|
+
var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _A, _B;
|
|
9255
9194
|
yield new __await(this.initialize());
|
|
9256
9195
|
const ns = formatNamespace(namespace != null ? namespace : this.config.projectId);
|
|
9257
|
-
const topK = (_b = (
|
|
9196
|
+
const topK = (_b = (_a2 = this.config.rag) == null ? void 0 : _a2.topK) != null ? _b : 5;
|
|
9258
9197
|
const scoreThreshold = (_d = (_c = this.config.rag) == null ? void 0 : _c.scoreThreshold) != null ? _d : 0.3;
|
|
9259
9198
|
const requestId = `req_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
|
|
9260
9199
|
const requestStart = performance.now();
|
|
@@ -9303,8 +9242,8 @@ ${m.content}`).join("\n\n---\n\n");
|
|
|
9303
9242
|
const rerankStart = performance.now();
|
|
9304
9243
|
const structuredSources = this.applyStructuredFilters(rawSources, filter).filter((s) => Boolean(s && typeof s === "object"));
|
|
9305
9244
|
let fullSources = hasNumericPredicates ? structuredSources : structuredSources.filter((m) => {
|
|
9306
|
-
var
|
|
9307
|
-
return ((
|
|
9245
|
+
var _a3;
|
|
9246
|
+
return ((_a3 = m == null ? void 0 : m.score) != null ? _a3 : 0) >= scoreThreshold;
|
|
9308
9247
|
});
|
|
9309
9248
|
const rerankLimit = Math.max(retrievalLimit, fullSources.length);
|
|
9310
9249
|
const useReranking = arch !== "naive" && (arch === "retrieve-and-rerank" || ((_m = this.config.rag) == null ? void 0 : _m.useReranking));
|
|
@@ -9317,13 +9256,13 @@ ${m.content}`).join("\n\n---\n\n");
|
|
|
9317
9256
|
let context = promptSources.length ? `[SEARCH SUMMARY: Found ${totalCount} matching record(s) out of ${rawSources.length} total resources searched in database. Context includes top ${promptSources.length} relevant records]
|
|
9318
9257
|
|
|
9319
9258
|
` + promptSources.map((m, i) => {
|
|
9320
|
-
var
|
|
9259
|
+
var _a3;
|
|
9321
9260
|
return `[Source ${i + 1}]
|
|
9322
|
-
${(
|
|
9261
|
+
${(_a3 = m == null ? void 0 : m.content) != null ? _a3 : ""}`;
|
|
9323
9262
|
}).join("\n\n---\n\n") : "No relevant context found.";
|
|
9324
9263
|
const sources = [...fullSources].filter((s) => Boolean(s && typeof s === "object")).sort((a, b) => {
|
|
9325
|
-
var
|
|
9326
|
-
return ((
|
|
9264
|
+
var _a3, _b2;
|
|
9265
|
+
return ((_a3 = b == null ? void 0 : b.score) != null ? _a3 : 0) - ((_b2 = a == null ? void 0 : a.score) != null ? _b2 : 0);
|
|
9327
9266
|
});
|
|
9328
9267
|
if (graphData && graphData.nodes.length > 0) {
|
|
9329
9268
|
const graphContext = graphData.nodes.map(
|
|
@@ -9351,10 +9290,10 @@ ${context}`;
|
|
|
9351
9290
|
yield {
|
|
9352
9291
|
reply: "",
|
|
9353
9292
|
sources: (sources || []).filter((s) => Boolean(s && typeof s === "object")).map((s) => {
|
|
9354
|
-
var
|
|
9293
|
+
var _a3, _b2, _c2;
|
|
9355
9294
|
return {
|
|
9356
9295
|
id: s.id,
|
|
9357
|
-
score: (
|
|
9296
|
+
score: (_a3 = s.score) != null ? _a3 : 0,
|
|
9358
9297
|
content: (_b2 = s == null ? void 0 : s.content) != null ? _b2 : "",
|
|
9359
9298
|
metadata: (_c2 = s == null ? void 0 : s.metadata) != null ? _c2 : {},
|
|
9360
9299
|
namespace: ns
|
|
@@ -9547,11 +9486,11 @@ ${context}`;
|
|
|
9547
9486
|
systemPrompt,
|
|
9548
9487
|
userPrompt: question + finalRestrictionSuffix,
|
|
9549
9488
|
chunks: (sources || []).filter(Boolean).map((s) => {
|
|
9550
|
-
var
|
|
9489
|
+
var _a3, _b2;
|
|
9551
9490
|
return {
|
|
9552
9491
|
id: s.id,
|
|
9553
9492
|
score: s.score,
|
|
9554
|
-
content: (
|
|
9493
|
+
content: (_a3 = s == null ? void 0 : s.content) != null ? _a3 : "",
|
|
9555
9494
|
metadata: (_b2 = s == null ? void 0 : s.metadata) != null ? _b2 : {},
|
|
9556
9495
|
namespace: ns
|
|
9557
9496
|
};
|
|
@@ -9570,7 +9509,7 @@ ${context}`;
|
|
|
9570
9509
|
const telemetryUrl = ((_B = this.config.telemetry) == null ? void 0 : _B.url) || process.env.TELEMETRY_URL || process.env.NEXT_PUBLIC_TELEMETRY_URL || defaultUrl;
|
|
9571
9510
|
const absoluteUrl = telemetryUrl.startsWith("http") ? telemetryUrl : "https://www.retrivora.com" + (telemetryUrl.startsWith("/") ? telemetryUrl : "/" + telemetryUrl);
|
|
9572
9511
|
(async () => {
|
|
9573
|
-
var
|
|
9512
|
+
var _a3, _b2, _c2, _d2, _e2;
|
|
9574
9513
|
try {
|
|
9575
9514
|
let finalTrace = trace;
|
|
9576
9515
|
if (!awaitHallucination && runHallucination) {
|
|
@@ -9579,7 +9518,7 @@ ${context}`;
|
|
|
9579
9518
|
finalTrace = buildTrace(backgroundScoreResult);
|
|
9580
9519
|
}
|
|
9581
9520
|
}
|
|
9582
|
-
const modelName = (finalTrace == null ? void 0 : finalTrace.model) || ((
|
|
9521
|
+
const modelName = (finalTrace == null ? void 0 : finalTrace.model) || ((_a3 = this.config.llm) == null ? void 0 : _a3.model) || "llama-3.1-8b-instant";
|
|
9583
9522
|
const providerName = (finalTrace == null ? void 0 : finalTrace.provider) || ((_b2 = this.config.llm) == null ? void 0 : _b2.provider) || "groq";
|
|
9584
9523
|
const tokenCount = Number(((_c2 = finalTrace == null ? void 0 : finalTrace.tokens) == null ? void 0 : _c2.totalTokens) || 0);
|
|
9585
9524
|
const costEst = Number(((_d2 = finalTrace == null ? void 0 : finalTrace.tokens) == null ? void 0 : _d2.estimatedCostUsd) || 0);
|
|
@@ -9633,7 +9572,7 @@ ${context}`;
|
|
|
9633
9572
|
* Uses an LRU-bounded embedding cache to avoid re-embedding the same query.
|
|
9634
9573
|
*/
|
|
9635
9574
|
async generateUiTransformation(question, sources, cachedSchema, forceDeterministic = false) {
|
|
9636
|
-
var
|
|
9575
|
+
var _a2;
|
|
9637
9576
|
if (!sources || sources.length === 0) {
|
|
9638
9577
|
return UITransformer.transform(question, sources, this.config, cachedSchema);
|
|
9639
9578
|
}
|
|
@@ -9646,7 +9585,7 @@ ${context}`;
|
|
|
9646
9585
|
{ visualizationHint: "product_browse", recommendedChart: "text", filterInStockOnly: false, wantsExplicitTable: false, isTemporal: false, isComparison: false, language: "en" }
|
|
9647
9586
|
);
|
|
9648
9587
|
}
|
|
9649
|
-
const enableLlmUiTransform = ((
|
|
9588
|
+
const enableLlmUiTransform = ((_a2 = this.config.llm.options) == null ? void 0 : _a2.enableLlmUiTransform) === true;
|
|
9650
9589
|
if (forceDeterministic || !enableLlmUiTransform) {
|
|
9651
9590
|
return UITransformer.transform(question, sources, this.config, cachedSchema);
|
|
9652
9591
|
}
|
|
@@ -9664,15 +9603,15 @@ ${context}`;
|
|
|
9664
9603
|
const value = this.resolveNumericPredicateValue(source, predicate);
|
|
9665
9604
|
return value !== null && this.matchesNumericPredicate(value, predicate);
|
|
9666
9605
|
})).sort((a, b) => {
|
|
9667
|
-
var
|
|
9606
|
+
var _a2, _b;
|
|
9668
9607
|
const primary = predicates[0];
|
|
9669
|
-
const aValue = (
|
|
9608
|
+
const aValue = (_a2 = this.resolveNumericPredicateValue(a, primary)) != null ? _a2 : 0;
|
|
9670
9609
|
const bValue = (_b = this.resolveNumericPredicateValue(b, primary)) != null ? _b : 0;
|
|
9671
9610
|
return primary.operator === "lt" || primary.operator === "lte" ? aValue - bValue : bValue - aValue;
|
|
9672
9611
|
});
|
|
9673
9612
|
}
|
|
9674
9613
|
resolveNumericPredicateValue(source, predicate) {
|
|
9675
|
-
var
|
|
9614
|
+
var _a2;
|
|
9676
9615
|
if (!source) return null;
|
|
9677
9616
|
const meta = source.metadata || {};
|
|
9678
9617
|
const field = predicate.field;
|
|
@@ -9685,7 +9624,7 @@ ${context}`;
|
|
|
9685
9624
|
const value = this.toFiniteNumber(Array.isArray(fuzzy) ? fuzzy[1] : fuzzy.value);
|
|
9686
9625
|
if (value !== null) return value;
|
|
9687
9626
|
}
|
|
9688
|
-
const contentValue = this.extractNumericValueFromContent((
|
|
9627
|
+
const contentValue = this.extractNumericValueFromContent((_a2 = source.content) != null ? _a2 : "", field);
|
|
9689
9628
|
if (contentValue !== null) return contentValue;
|
|
9690
9629
|
}
|
|
9691
9630
|
for (const [key, value] of entries) {
|
|
@@ -9741,8 +9680,8 @@ ${context}`;
|
|
|
9741
9680
|
return Number.isFinite(numeric) ? numeric : null;
|
|
9742
9681
|
}
|
|
9743
9682
|
async retrieve(query, options) {
|
|
9744
|
-
var
|
|
9745
|
-
const ns = formatNamespace((
|
|
9683
|
+
var _a2, _b, _c, _d, _e, _f, _g2;
|
|
9684
|
+
const ns = formatNamespace((_a2 = options.namespace) != null ? _a2 : this.config.projectId);
|
|
9746
9685
|
const topK = (_b = options.topK) != null ? _b : 5;
|
|
9747
9686
|
const cacheKey = `${ns}::${query}`;
|
|
9748
9687
|
let queryVector = this.embeddingCache.get(cacheKey);
|
|
@@ -9787,11 +9726,11 @@ ${context}`;
|
|
|
9787
9726
|
namespace: ns,
|
|
9788
9727
|
count: resolvedSources.length,
|
|
9789
9728
|
sample: resolvedSources.slice(0, 2).map((s) => {
|
|
9790
|
-
var
|
|
9729
|
+
var _a3;
|
|
9791
9730
|
return {
|
|
9792
9731
|
id: s == null ? void 0 : s.id,
|
|
9793
9732
|
score: s == null ? void 0 : s.score,
|
|
9794
|
-
contentSnippet: String((
|
|
9733
|
+
contentSnippet: String((_a3 = s == null ? void 0 : s.content) != null ? _a3 : "").substring(0, 150),
|
|
9795
9734
|
metadata: s == null ? void 0 : s.metadata
|
|
9796
9735
|
};
|
|
9797
9736
|
})
|
|
@@ -9805,8 +9744,8 @@ Focus on extracting the core intent and entities. Do not answer the question, ju
|
|
|
9805
9744
|
|
|
9806
9745
|
History:
|
|
9807
9746
|
${(history || []).map((m) => {
|
|
9808
|
-
var
|
|
9809
|
-
return `${(m == null ? void 0 : m.role) || "user"}: ${(
|
|
9747
|
+
var _a2;
|
|
9748
|
+
return `${(m == null ? void 0 : m.role) || "user"}: ${(_a2 = m == null ? void 0 : m.content) != null ? _a2 : ""}`;
|
|
9810
9749
|
}).join("\n")}
|
|
9811
9750
|
|
|
9812
9751
|
New Question: ${question}
|
|
@@ -9833,8 +9772,8 @@ Optimized Search Query:`;
|
|
|
9833
9772
|
const { sources } = await this.retrieve(query, { namespace: ns, topK: 5 });
|
|
9834
9773
|
if (!sources || sources.length === 0) return [];
|
|
9835
9774
|
const context = sources.map((s) => {
|
|
9836
|
-
var
|
|
9837
|
-
return (
|
|
9775
|
+
var _a2;
|
|
9776
|
+
return (_a2 = s == null ? void 0 : s.content) != null ? _a2 : "";
|
|
9838
9777
|
}).join("\n\n---\n\n");
|
|
9839
9778
|
const prompt = `Based on the following snippets from a document, what are at least 5 short, relevant questions a user might ask?
|
|
9840
9779
|
Focus on questions that can be answered by the context.
|
|
@@ -9973,186 +9912,8 @@ var ProviderHealthCheck = class {
|
|
|
9973
9912
|
};
|
|
9974
9913
|
|
|
9975
9914
|
// src/core/FreeTierLimitsGuard.ts
|
|
9976
|
-
var
|
|
9977
|
-
|
|
9978
|
-
MAX_FILE_SIZE_BYTES: 20 * 1024 * 1024,
|
|
9979
|
-
MAX_STORAGE_BYTES: 250 * 1024 * 1024,
|
|
9980
|
-
MAX_TRIAL_REQUEST_UNITS: 500,
|
|
9981
|
-
MAX_DAILY_REQUEST_UNITS: 50,
|
|
9982
|
-
TRIAL_DURATION_DAYS: 14,
|
|
9983
|
-
GRACE_PERIOD_DAYS: 2,
|
|
9984
|
-
MAX_RPM: 10,
|
|
9985
|
-
MAX_RPH: 100,
|
|
9986
|
-
MAX_RPD: 500,
|
|
9987
|
-
MAX_CONCURRENT_REQUESTS: 2,
|
|
9988
|
-
MAX_INPUT_TOKENS: 2e4,
|
|
9989
|
-
MAX_OUTPUT_TOKENS: 4e3,
|
|
9990
|
-
UPGRADE_REMINDER_DAYS: [7, 12, 14],
|
|
9991
|
-
MAX_USERS: 1,
|
|
9992
|
-
MAX_NAMESPACES: 1,
|
|
9993
|
-
MAX_PROJECTS: 1
|
|
9994
|
-
};
|
|
9995
|
-
var FreeTierLimitsGuard = class {
|
|
9996
|
-
static calculateTrialTimestamps(startedAt) {
|
|
9997
|
-
const startDate = startedAt ? new Date(startedAt) : /* @__PURE__ */ new Date();
|
|
9998
|
-
const trialStartedAtMs = startDate.getTime();
|
|
9999
|
-
const trialExpiresAtMs = trialStartedAtMs + FREE_TIER_QUOTAS.TRIAL_DURATION_DAYS * 24 * 60 * 60 * 1e3;
|
|
10000
|
-
const gracePeriodExpiresAtMs = trialStartedAtMs + (FREE_TIER_QUOTAS.TRIAL_DURATION_DAYS + FREE_TIER_QUOTAS.GRACE_PERIOD_DAYS) * 24 * 60 * 60 * 1e3;
|
|
10001
|
-
return {
|
|
10002
|
-
trial_started_at: new Date(trialStartedAtMs).toISOString(),
|
|
10003
|
-
trial_expires_at: new Date(trialExpiresAtMs).toISOString(),
|
|
10004
|
-
grace_period_expires_at: new Date(gracePeriodExpiresAtMs).toISOString()
|
|
10005
|
-
};
|
|
10006
|
-
}
|
|
10007
|
-
static calculateRequestUnits(operationType, options) {
|
|
10008
|
-
var _a3;
|
|
10009
|
-
const op = operationType.toLowerCase().trim();
|
|
10010
|
-
if (op.includes("suggestion") || op.includes("template") || op.includes("history") || op.includes("model") || op.includes("health") || op.includes("auth") || op.includes("license") || op.includes("usage") || op.includes("feedback") || op.includes("analytic")) {
|
|
10011
|
-
return 0;
|
|
10012
|
-
}
|
|
10013
|
-
if (op.includes("image")) {
|
|
10014
|
-
return 10;
|
|
10015
|
-
}
|
|
10016
|
-
if (op.includes("embedding")) {
|
|
10017
|
-
const chunks = (_a3 = options == null ? void 0 : options.chunkCount) != null ? _a3 : 1;
|
|
10018
|
-
return Math.max(1, Math.ceil(chunks / 1e3));
|
|
10019
|
-
}
|
|
10020
|
-
return 1;
|
|
10021
|
-
}
|
|
10022
|
-
static checkTrialStatus(startedAt, totalUnitsUsed = 0, nowServerTime = /* @__PURE__ */ new Date()) {
|
|
10023
|
-
const timestamps = this.calculateTrialTimestamps(startedAt);
|
|
10024
|
-
const startMs = new Date(timestamps.trial_started_at).getTime();
|
|
10025
|
-
const expireMs = new Date(timestamps.trial_expires_at).getTime();
|
|
10026
|
-
const graceExpireMs = new Date(timestamps.grace_period_expires_at).getTime();
|
|
10027
|
-
const nowMs = nowServerTime.getTime();
|
|
10028
|
-
const daysElapsed = Math.floor(Math.max(0, nowMs - startMs) / (1e3 * 60 * 60 * 24));
|
|
10029
|
-
const daysRemaining = Math.max(0, Math.ceil((expireMs - nowMs) / (1e3 * 60 * 60 * 24)));
|
|
10030
|
-
const isConsumedUnits = totalUnitsUsed >= FREE_TIER_QUOTAS.MAX_TRIAL_REQUEST_UNITS;
|
|
10031
|
-
const isExpiredTime = nowMs > expireMs;
|
|
10032
|
-
const isPastGraceTime = nowMs > graceExpireMs;
|
|
10033
|
-
let status = "active";
|
|
10034
|
-
let reason;
|
|
10035
|
-
if (isPastGraceTime || isConsumedUnits && isExpiredTime) {
|
|
10036
|
-
status = "expired";
|
|
10037
|
-
reason = "Trial period and grace period have expired. Upgrade your plan.";
|
|
10038
|
-
} else if (isExpiredTime || isConsumedUnits) {
|
|
10039
|
-
status = "grace_period";
|
|
10040
|
-
reason = isConsumedUnits ? "Total trial request quota (500 units) consumed. Grace period active (dashboard access only)." : "14-day trial period expired. 2-day grace period active (dashboard access only).";
|
|
10041
|
-
}
|
|
10042
|
-
let upgradeReminderDue;
|
|
10043
|
-
if (FREE_TIER_QUOTAS.UPGRADE_REMINDER_DAYS.includes(daysElapsed)) {
|
|
10044
|
-
upgradeReminderDue = daysElapsed;
|
|
10045
|
-
}
|
|
10046
|
-
let usageReminderDue;
|
|
10047
|
-
const usagePercent = totalUnitsUsed / FREE_TIER_QUOTAS.MAX_TRIAL_REQUEST_UNITS * 100;
|
|
10048
|
-
if (usagePercent >= 100) {
|
|
10049
|
-
usageReminderDue = "100%";
|
|
10050
|
-
} else if (usagePercent >= 90) {
|
|
10051
|
-
usageReminderDue = "90%";
|
|
10052
|
-
} else if (usagePercent >= 80) {
|
|
10053
|
-
usageReminderDue = "80%";
|
|
10054
|
-
}
|
|
10055
|
-
return {
|
|
10056
|
-
status,
|
|
10057
|
-
daysElapsed,
|
|
10058
|
-
daysRemaining,
|
|
10059
|
-
isGracePeriod: status === "grace_period",
|
|
10060
|
-
isExpired: status === "expired",
|
|
10061
|
-
upgradeReminderDue,
|
|
10062
|
-
usageReminderDue,
|
|
10063
|
-
reason
|
|
10064
|
-
};
|
|
10065
|
-
}
|
|
10066
|
-
static checkIngestionAllowed(stats, incomingSizeBytes = 0) {
|
|
10067
|
-
var _a3, _b;
|
|
10068
|
-
const docs = (_a3 = stats.documentCount) != null ? _a3 : 0;
|
|
10069
|
-
const storage = (_b = stats.totalStorageBytes) != null ? _b : 0;
|
|
10070
|
-
if (incomingSizeBytes > FREE_TIER_QUOTAS.MAX_FILE_SIZE_BYTES) {
|
|
10071
|
-
const mbSize = (incomingSizeBytes / (1024 * 1024)).toFixed(1);
|
|
10072
|
-
return {
|
|
10073
|
-
allowed: false,
|
|
10074
|
-
reason: `[FreeTierIngestor] File size (${mbSize}MB) exceeds maximum allowed size of 20MB under Free Tier rules.`
|
|
10075
|
-
};
|
|
10076
|
-
}
|
|
10077
|
-
if (docs >= FREE_TIER_QUOTAS.MAX_DOCUMENTS) {
|
|
10078
|
-
return {
|
|
10079
|
-
allowed: false,
|
|
10080
|
-
reason: `Document limit reached (${docs}/${FREE_TIER_QUOTAS.MAX_DOCUMENTS}). Upgrade to Pro to ingest more documents.`
|
|
10081
|
-
};
|
|
10082
|
-
}
|
|
10083
|
-
if (storage + incomingSizeBytes > FREE_TIER_QUOTAS.MAX_STORAGE_BYTES) {
|
|
10084
|
-
const mbUsed = (storage / (1024 * 1024)).toFixed(1);
|
|
10085
|
-
return {
|
|
10086
|
-
allowed: false,
|
|
10087
|
-
reason: `Storage quota exceeded (${mbUsed}MB / 250MB). Upgrade for unlimited storage.`
|
|
10088
|
-
};
|
|
10089
|
-
}
|
|
10090
|
-
return { allowed: true };
|
|
10091
|
-
}
|
|
10092
|
-
static checkRequestAllowed(params) {
|
|
10093
|
-
var _a3, _b, _c;
|
|
10094
|
-
const op = (_a3 = params.operationType) != null ? _a3 : "chat";
|
|
10095
|
-
const costUnits = this.calculateRequestUnits(op, { chunkCount: params.chunkCount });
|
|
10096
|
-
if (costUnits === 0) {
|
|
10097
|
-
return { allowed: true, costUnits: 0 };
|
|
10098
|
-
}
|
|
10099
|
-
if (params.inputTokens && params.inputTokens > FREE_TIER_QUOTAS.MAX_INPUT_TOKENS) {
|
|
10100
|
-
return {
|
|
10101
|
-
allowed: false,
|
|
10102
|
-
reason: "Token limit exceeded. Upgrade your plan.",
|
|
10103
|
-
costUnits
|
|
10104
|
-
};
|
|
10105
|
-
}
|
|
10106
|
-
if (params.outputTokens && params.outputTokens > FREE_TIER_QUOTAS.MAX_OUTPUT_TOKENS) {
|
|
10107
|
-
return {
|
|
10108
|
-
allowed: false,
|
|
10109
|
-
reason: "Token limit exceeded. Upgrade your plan.",
|
|
10110
|
-
costUnits
|
|
10111
|
-
};
|
|
10112
|
-
}
|
|
10113
|
-
if (params.concurrentRequests && params.concurrentRequests > FREE_TIER_QUOTAS.MAX_CONCURRENT_REQUESTS) {
|
|
10114
|
-
return {
|
|
10115
|
-
allowed: false,
|
|
10116
|
-
reason: `Concurrent limit exceeded (max ${FREE_TIER_QUOTAS.MAX_CONCURRENT_REQUESTS} active requests). Please wait for active streams to finish.`,
|
|
10117
|
-
costUnits
|
|
10118
|
-
};
|
|
10119
|
-
}
|
|
10120
|
-
const totalUnits = (_b = params.totalUnitsUsed) != null ? _b : 0;
|
|
10121
|
-
const trialStatus = this.checkTrialStatus(params.trialStartedAt, totalUnits, params.nowServerTime);
|
|
10122
|
-
if (trialStatus.status === "expired" || trialStatus.status === "grace_period") {
|
|
10123
|
-
return {
|
|
10124
|
-
allowed: false,
|
|
10125
|
-
reason: trialStatus.reason || "Trial limit reached. Upgrade your plan.",
|
|
10126
|
-
costUnits
|
|
10127
|
-
};
|
|
10128
|
-
}
|
|
10129
|
-
if (totalUnits + costUnits > FREE_TIER_QUOTAS.MAX_TRIAL_REQUEST_UNITS) {
|
|
10130
|
-
return {
|
|
10131
|
-
allowed: false,
|
|
10132
|
-
reason: `Total trial request unit limit reached (${totalUnits}/${FREE_TIER_QUOTAS.MAX_TRIAL_REQUEST_UNITS}). Upgrade your plan.`,
|
|
10133
|
-
costUnits
|
|
10134
|
-
};
|
|
10135
|
-
}
|
|
10136
|
-
const dailyUnits = (_c = params.dailyUnitsUsed) != null ? _c : 0;
|
|
10137
|
-
if (dailyUnits + costUnits > FREE_TIER_QUOTAS.MAX_DAILY_REQUEST_UNITS) {
|
|
10138
|
-
return {
|
|
10139
|
-
allowed: false,
|
|
10140
|
-
reason: `Daily request quota limit reached (${dailyUnits}/${FREE_TIER_QUOTAS.MAX_DAILY_REQUEST_UNITS} units/day). Quota resets tomorrow.`,
|
|
10141
|
-
costUnits
|
|
10142
|
-
};
|
|
10143
|
-
}
|
|
10144
|
-
return { allowed: true, costUnits };
|
|
10145
|
-
}
|
|
10146
|
-
static checkQueryAllowed(dailyQueriesCount = 0) {
|
|
10147
|
-
if (dailyQueriesCount >= FREE_TIER_QUOTAS.MAX_DAILY_REQUEST_UNITS) {
|
|
10148
|
-
return {
|
|
10149
|
-
allowed: false,
|
|
10150
|
-
reason: `Daily query limit reached (${FREE_TIER_QUOTAS.MAX_DAILY_REQUEST_UNITS}/day). Quota resets tomorrow.`
|
|
10151
|
-
};
|
|
10152
|
-
}
|
|
10153
|
-
return { allowed: true };
|
|
10154
|
-
}
|
|
10155
|
-
};
|
|
9915
|
+
var FreeTierLimitsGuard_exports = {};
|
|
9916
|
+
__reExport(FreeTierLimitsGuard_exports, require("@retrivora-ai/billing-guards"));
|
|
10156
9917
|
|
|
10157
9918
|
// src/core/VectorPlugin.ts
|
|
10158
9919
|
var VectorPlugin = class {
|
|
@@ -10160,12 +9921,12 @@ var VectorPlugin = class {
|
|
|
10160
9921
|
const resolvedConfig = ConfigResolver.resolve(hostConfig);
|
|
10161
9922
|
this.config = resolvedConfig;
|
|
10162
9923
|
this.validationPromise = (async () => {
|
|
10163
|
-
var
|
|
9924
|
+
var _a2;
|
|
10164
9925
|
await ConfigValidator.validateAndThrow(resolvedConfig);
|
|
10165
9926
|
await LicenseVerifier.verifyAsync(
|
|
10166
9927
|
resolvedConfig.licenseKey,
|
|
10167
9928
|
resolvedConfig.projectId,
|
|
10168
|
-
(
|
|
9929
|
+
(_a2 = resolvedConfig.vectorDb) == null ? void 0 : _a2.provider
|
|
10169
9930
|
);
|
|
10170
9931
|
})();
|
|
10171
9932
|
this.validationPromise.catch(() => {
|
|
@@ -10201,8 +9962,8 @@ var VectorPlugin = class {
|
|
|
10201
9962
|
}
|
|
10202
9963
|
estimateInputTokens(message, history = []) {
|
|
10203
9964
|
const allContent = message.length + history.reduce((acc, m) => {
|
|
10204
|
-
var
|
|
10205
|
-
return acc + (((
|
|
9965
|
+
var _a2, _b;
|
|
9966
|
+
return acc + (((_a2 = m == null ? void 0 : m.content) == null ? void 0 : _a2.length) || ((_b = m == null ? void 0 : m.text) == null ? void 0 : _b.length) || 0);
|
|
10206
9967
|
}, 0);
|
|
10207
9968
|
return Math.ceil(allContent / 4);
|
|
10208
9969
|
}
|
|
@@ -10210,7 +9971,7 @@ var VectorPlugin = class {
|
|
|
10210
9971
|
* Run a chat query.
|
|
10211
9972
|
*/
|
|
10212
9973
|
async chat(message, history = [], namespace) {
|
|
10213
|
-
var
|
|
9974
|
+
var _a2;
|
|
10214
9975
|
try {
|
|
10215
9976
|
await this.validationPromise;
|
|
10216
9977
|
} catch (err) {
|
|
@@ -10218,11 +9979,11 @@ var VectorPlugin = class {
|
|
|
10218
9979
|
}
|
|
10219
9980
|
try {
|
|
10220
9981
|
const inputTokens = this.estimateInputTokens(message, history);
|
|
10221
|
-
const tokenCheck = FreeTierLimitsGuard.checkRequestAllowed({
|
|
9982
|
+
const tokenCheck = FreeTierLimitsGuard_exports.FreeTierLimitsGuard.checkRequestAllowed({
|
|
10222
9983
|
operationType: "chat",
|
|
10223
9984
|
inputTokens
|
|
10224
9985
|
});
|
|
10225
|
-
if (!tokenCheck.allowed && ((
|
|
9986
|
+
if (!tokenCheck.allowed && ((_a2 = tokenCheck.reason) == null ? void 0 : _a2.toLowerCase().includes("token"))) {
|
|
10226
9987
|
throw wrapError(new Error(tokenCheck.reason), "RATE_LIMITED");
|
|
10227
9988
|
}
|
|
10228
9989
|
return await this.pipeline.ask(message, history, namespace);
|
|
@@ -10243,7 +10004,7 @@ var VectorPlugin = class {
|
|
|
10243
10004
|
*/
|
|
10244
10005
|
chatStream(_0) {
|
|
10245
10006
|
return __asyncGenerator(this, arguments, function* (message, history = [], namespace) {
|
|
10246
|
-
var
|
|
10007
|
+
var _a2;
|
|
10247
10008
|
try {
|
|
10248
10009
|
yield new __await(this.validationPromise);
|
|
10249
10010
|
} catch (err) {
|
|
@@ -10251,11 +10012,11 @@ var VectorPlugin = class {
|
|
|
10251
10012
|
}
|
|
10252
10013
|
try {
|
|
10253
10014
|
const inputTokens = this.estimateInputTokens(message, history);
|
|
10254
|
-
const tokenCheck = FreeTierLimitsGuard.checkRequestAllowed({
|
|
10015
|
+
const tokenCheck = FreeTierLimitsGuard_exports.FreeTierLimitsGuard.checkRequestAllowed({
|
|
10255
10016
|
operationType: "chat_stream",
|
|
10256
10017
|
inputTokens
|
|
10257
10018
|
});
|
|
10258
|
-
if (!tokenCheck.allowed && ((
|
|
10019
|
+
if (!tokenCheck.allowed && ((_a2 = tokenCheck.reason) == null ? void 0 : _a2.toLowerCase().includes("token"))) {
|
|
10259
10020
|
throw wrapError(new Error(tokenCheck.reason), "RATE_LIMITED");
|
|
10260
10021
|
}
|
|
10261
10022
|
const stream = this.pipeline.askStream(message, history, namespace);
|
|
@@ -10330,8 +10091,8 @@ var DocumentParser = class {
|
|
|
10330
10091
|
* Extract text from a File or Buffer based on its type.
|
|
10331
10092
|
*/
|
|
10332
10093
|
static async parse(file, fileName, mimeType) {
|
|
10333
|
-
var
|
|
10334
|
-
const extension = ((
|
|
10094
|
+
var _a2;
|
|
10095
|
+
const extension = ((_a2 = fileName.split(".").pop()) == null ? void 0 : _a2.toLowerCase()) || "";
|
|
10335
10096
|
if (extension === "txt" || extension === "md" || mimeType === "text/plain" || mimeType === "text/markdown") {
|
|
10336
10097
|
return this.readAsText(file);
|
|
10337
10098
|
}
|
|
@@ -10410,7 +10171,17 @@ ${csv.trim()}`);
|
|
|
10410
10171
|
var fs = __toESM(require("fs"));
|
|
10411
10172
|
var path = __toESM(require("path"));
|
|
10412
10173
|
var os = __toESM(require("os"));
|
|
10413
|
-
|
|
10174
|
+
function validateTableName(name) {
|
|
10175
|
+
if (!/^[a-zA-Z_][a-zA-Z0-9_]{0,62}$/.test(name)) {
|
|
10176
|
+
throw new Error(
|
|
10177
|
+
`[DatabaseStorage] Invalid table name "${name}". Table names must start with a letter or underscore and contain only alphanumeric characters and underscores (max 63 chars).`
|
|
10178
|
+
);
|
|
10179
|
+
}
|
|
10180
|
+
return name;
|
|
10181
|
+
}
|
|
10182
|
+
var _pgInitPromises = /* @__PURE__ */ new Map();
|
|
10183
|
+
var _DatabaseStorage = class _DatabaseStorage {
|
|
10184
|
+
// 5 minutes
|
|
10414
10185
|
constructor(config) {
|
|
10415
10186
|
// Dynamic PG Pool
|
|
10416
10187
|
this.pgPool = null;
|
|
@@ -10422,36 +10193,50 @@ var DatabaseStorage = class {
|
|
|
10422
10193
|
this.fallbackDir = this.isServerless ? path.join(os.tmpdir(), ".retrivora") : path.join(process.cwd(), ".retrivora");
|
|
10423
10194
|
this.historyFile = path.join(this.fallbackDir, "history.json");
|
|
10424
10195
|
this.feedbackFile = path.join(this.fallbackDir, "feedback.json");
|
|
10425
|
-
|
|
10196
|
+
/** Subscription check result cache (Fix PERF-1). */
|
|
10197
|
+
this.subscriptionCheckedAt = 0;
|
|
10198
|
+
var _a2, _b, _c, _d, _e;
|
|
10426
10199
|
this.config = config;
|
|
10427
|
-
this.provider = ((
|
|
10200
|
+
this.provider = ((_a2 = config.vectorDb) == null ? void 0 : _a2.provider) || "fs";
|
|
10428
10201
|
const rawHistoryTable = ((_c = (_b = config.rag) == null ? void 0 : _b.history) == null ? void 0 : _c.tableName) || "retrivora_history";
|
|
10429
10202
|
const rawFeedbackTable = ((_e = (_d = config.rag) == null ? void 0 : _d.feedback) == null ? void 0 : _e.tableName) || "retrivora_feedback";
|
|
10430
|
-
this.historyTableName = rawHistoryTable
|
|
10431
|
-
this.feedbackTableName = rawFeedbackTable
|
|
10203
|
+
this.historyTableName = validateTableName(rawHistoryTable);
|
|
10204
|
+
this.feedbackTableName = validateTableName(rawFeedbackTable);
|
|
10432
10205
|
}
|
|
10433
10206
|
/**
|
|
10434
|
-
*
|
|
10435
|
-
*
|
|
10436
|
-
*
|
|
10207
|
+
* Checks that the caller holds a valid paid-tier license in production.
|
|
10208
|
+
*
|
|
10209
|
+
* Fix PERF-1: result is cached for 5 minutes so we don't call
|
|
10210
|
+
* LicenseVerifier.verifyAsync() on every DB operation (saveMessage,
|
|
10211
|
+
* getHistory, etc.).
|
|
10212
|
+
*
|
|
10213
|
+
* Fix PERF-2: renamed from `checkPremiumSubscription` to clarify intent.
|
|
10214
|
+
* The previous allowlist admitted all free tiers, defeating the gate.
|
|
10215
|
+
* The new allowlist only passes paid tiers. Free-tier access to history
|
|
10216
|
+
* and feedback is a product decision — update the constant below if the
|
|
10217
|
+
* policy changes.
|
|
10218
|
+
*
|
|
10219
|
+
* In non-production environments this is a complete no-op so local
|
|
10220
|
+
* development works without a license key.
|
|
10437
10221
|
*/
|
|
10438
|
-
async
|
|
10439
|
-
if (process.env.NODE_ENV !== "production")
|
|
10440
|
-
|
|
10441
|
-
}
|
|
10222
|
+
async checkPaidSubscription() {
|
|
10223
|
+
if (process.env.NODE_ENV !== "production") return;
|
|
10224
|
+
if (Date.now() - this.subscriptionCheckedAt < _DatabaseStorage.SUBSCRIPTION_CACHE_TTL_MS) return;
|
|
10442
10225
|
if (!this.config.licenseKey) {
|
|
10443
10226
|
throw new Error(
|
|
10444
|
-
"[Retrivora SDK] Chat History and Feedback features require a
|
|
10227
|
+
"[Retrivora SDK] Chat History and Feedback features require a paid license key in production."
|
|
10445
10228
|
);
|
|
10446
10229
|
}
|
|
10447
10230
|
try {
|
|
10448
10231
|
const payload = await LicenseVerifier.verifyAsync(this.config.licenseKey, this.config.projectId);
|
|
10449
10232
|
const tier = (payload.tier || "").toLowerCase();
|
|
10450
|
-
|
|
10233
|
+
const PAID_TIERS = /* @__PURE__ */ new Set(["premium", "enterprise", "growth", "pro", "developer_pro"]);
|
|
10234
|
+
if (!PAID_TIERS.has(tier)) {
|
|
10451
10235
|
throw new Error(
|
|
10452
10236
|
`[Retrivora SDK] Chat History and Feedback features are not available on the "${tier}" tier. Please upgrade to a Developer Pro or Enterprise plan.`
|
|
10453
10237
|
);
|
|
10454
10238
|
}
|
|
10239
|
+
this.subscriptionCheckedAt = Date.now();
|
|
10455
10240
|
} catch (err) {
|
|
10456
10241
|
throw new Error(
|
|
10457
10242
|
`[Retrivora SDK] Subscription check failed: ${err instanceof Error ? err.message : String(err)}`
|
|
@@ -10459,34 +10244,45 @@ var DatabaseStorage = class {
|
|
|
10459
10244
|
}
|
|
10460
10245
|
}
|
|
10461
10246
|
async checkHistoryEnabled() {
|
|
10462
|
-
var
|
|
10463
|
-
await this.
|
|
10464
|
-
if (((_b = (
|
|
10247
|
+
var _a2, _b;
|
|
10248
|
+
await this.checkPaidSubscription();
|
|
10249
|
+
if (((_b = (_a2 = this.config.rag) == null ? void 0 : _a2.history) == null ? void 0 : _b.enabled) === false) {
|
|
10465
10250
|
throw new Error("[Retrivora SDK] Chat History is disabled in RagConfig.");
|
|
10466
10251
|
}
|
|
10467
10252
|
}
|
|
10468
10253
|
async checkFeedbackEnabled() {
|
|
10469
|
-
var
|
|
10470
|
-
await this.
|
|
10471
|
-
if (((_b = (
|
|
10254
|
+
var _a2, _b;
|
|
10255
|
+
await this.checkPaidSubscription();
|
|
10256
|
+
if (((_b = (_a2 = this.config.rag) == null ? void 0 : _a2.feedback) == null ? void 0 : _b.enabled) === false) {
|
|
10472
10257
|
throw new Error("[Retrivora SDK] User Feedback is disabled in RagConfig.");
|
|
10473
10258
|
}
|
|
10474
10259
|
}
|
|
10475
|
-
|
|
10476
|
-
|
|
10477
|
-
|
|
10478
|
-
|
|
10479
|
-
|
|
10480
|
-
|
|
10260
|
+
/**
|
|
10261
|
+
* Returns (or lazily creates) the PostgreSQL connection pool for this project.
|
|
10262
|
+
*
|
|
10263
|
+
* Fix ERR-2: the previous implementation had a double-initialization race
|
|
10264
|
+
* condition — two concurrent requests would both find _g[globalKey] missing,
|
|
10265
|
+
* both create a Pool, and the second Pool would be orphaned, leaking
|
|
10266
|
+
* database connections. This version stores a single Promise in the module-
|
|
10267
|
+
* level _pgInitPromises map so all concurrent callers await the same
|
|
10268
|
+
* initialization without creating duplicate pools.
|
|
10269
|
+
*/
|
|
10270
|
+
getPGPool() {
|
|
10271
|
+
const globalKey = `pg:${this.config.projectId}`;
|
|
10272
|
+
if (!_pgInitPromises.has(globalKey)) {
|
|
10273
|
+
_pgInitPromises.set(globalKey, this.createPGPool());
|
|
10481
10274
|
}
|
|
10275
|
+
return _pgInitPromises.get(globalKey);
|
|
10276
|
+
}
|
|
10277
|
+
async createPGPool() {
|
|
10482
10278
|
const opts = this.config.vectorDb.options;
|
|
10483
10279
|
if (!opts.connectionString) {
|
|
10484
10280
|
throw new Error("[DatabaseStorage] pg connectionString option is missing.");
|
|
10485
10281
|
}
|
|
10486
10282
|
const { Pool: Pool3 } = await import("pg");
|
|
10487
|
-
|
|
10488
|
-
|
|
10489
|
-
const client = await
|
|
10283
|
+
const pool = new Pool3({ connectionString: opts.connectionString });
|
|
10284
|
+
this.pgPool = pool;
|
|
10285
|
+
const client = await pool.connect();
|
|
10490
10286
|
try {
|
|
10491
10287
|
await client.query(`
|
|
10492
10288
|
CREATE TABLE IF NOT EXISTS ${this.historyTableName} (
|
|
@@ -10513,7 +10309,7 @@ var DatabaseStorage = class {
|
|
|
10513
10309
|
} finally {
|
|
10514
10310
|
client.release();
|
|
10515
10311
|
}
|
|
10516
|
-
return
|
|
10312
|
+
return pool;
|
|
10517
10313
|
}
|
|
10518
10314
|
async getMongoClient() {
|
|
10519
10315
|
const globalKey = `__retrivora_mongo_client_${this.config.projectId}`;
|
|
@@ -10693,17 +10489,21 @@ var DatabaseStorage = class {
|
|
|
10693
10489
|
}
|
|
10694
10490
|
}
|
|
10695
10491
|
/**
|
|
10696
|
-
*
|
|
10697
|
-
*
|
|
10698
|
-
*
|
|
10699
|
-
*
|
|
10700
|
-
*
|
|
10492
|
+
* Returns true when `sessionId` is in scope for the given `projectIds`.
|
|
10493
|
+
*
|
|
10494
|
+
* Fix RAG-5: the previous implementation used `sessionId.includes(pid)` which
|
|
10495
|
+
* allows cross-tenant data leakage. If a project ID "abc" is a substring of
|
|
10496
|
+
* another tenant's session ID (e.g., "xyz-abc-user-123"), the check would
|
|
10497
|
+
* incorrectly return true. We now only allow exact match or strict delimiter-
|
|
10498
|
+
* separated prefix match to prevent this attack.
|
|
10701
10499
|
*/
|
|
10702
10500
|
sessionInScope(sessionId, projectIds) {
|
|
10703
10501
|
if (!projectIds || projectIds.length === 0) return true;
|
|
10704
|
-
return projectIds.some(
|
|
10705
|
-
|
|
10706
|
-
|
|
10502
|
+
return projectIds.some((pid) => {
|
|
10503
|
+
if (!pid) return false;
|
|
10504
|
+
if (sessionId === pid) return true;
|
|
10505
|
+
return sessionId.startsWith(`${pid}-`) || sessionId.startsWith(`${pid}_`) || sessionId.startsWith(`${pid}/`);
|
|
10506
|
+
});
|
|
10707
10507
|
}
|
|
10708
10508
|
async listSessions(projectIds) {
|
|
10709
10509
|
await this.checkHistoryEnabled();
|
|
@@ -10852,9 +10652,12 @@ var DatabaseStorage = class {
|
|
|
10852
10652
|
}
|
|
10853
10653
|
}
|
|
10854
10654
|
};
|
|
10655
|
+
_DatabaseStorage.SUBSCRIPTION_CACHE_TTL_MS = 5 * 60 * 1e3;
|
|
10656
|
+
var DatabaseStorage = _DatabaseStorage;
|
|
10855
10657
|
|
|
10856
10658
|
// src/handlers/index.ts
|
|
10857
10659
|
init_version();
|
|
10660
|
+
var import_crypto2 = require("crypto");
|
|
10858
10661
|
async function checkAuth(req, onAuthorize) {
|
|
10859
10662
|
if (!onAuthorize) return null;
|
|
10860
10663
|
try {
|
|
@@ -10876,6 +10679,17 @@ var RateLimiter = class {
|
|
|
10876
10679
|
this.hits = /* @__PURE__ */ new Map();
|
|
10877
10680
|
this.windowMs = windowMs;
|
|
10878
10681
|
this.max = max;
|
|
10682
|
+
this.pruneTimer = setInterval(() => this.prune(), windowMs);
|
|
10683
|
+
if (typeof this.pruneTimer === "object" && "unref" in this.pruneTimer) {
|
|
10684
|
+
this.pruneTimer.unref();
|
|
10685
|
+
}
|
|
10686
|
+
}
|
|
10687
|
+
/** Prune entries whose timestamps are all outside the window. */
|
|
10688
|
+
prune() {
|
|
10689
|
+
const cutoff = Date.now() - this.windowMs;
|
|
10690
|
+
for (const [k, timestamps] of this.hits.entries()) {
|
|
10691
|
+
if (timestamps.every((t) => t <= cutoff)) this.hits.delete(k);
|
|
10692
|
+
}
|
|
10879
10693
|
}
|
|
10880
10694
|
/** Returns true if the request should be allowed, false if rate-limited. */
|
|
10881
10695
|
allow(key) {
|
|
@@ -10884,11 +10698,6 @@ var RateLimiter = class {
|
|
|
10884
10698
|
const existing = (this.hits.get(key) || []).filter((t) => t > cutoff);
|
|
10885
10699
|
existing.push(now);
|
|
10886
10700
|
this.hits.set(key, existing);
|
|
10887
|
-
if (this.hits.size > 5e3) {
|
|
10888
|
-
for (const [k, timestamps] of this.hits.entries()) {
|
|
10889
|
-
if (timestamps.every((t) => t <= cutoff)) this.hits.delete(k);
|
|
10890
|
-
}
|
|
10891
|
-
}
|
|
10892
10701
|
return existing.length <= this.max;
|
|
10893
10702
|
}
|
|
10894
10703
|
retryAfterSec(key) {
|
|
@@ -10903,8 +10712,8 @@ var _g = global;
|
|
|
10903
10712
|
var _a;
|
|
10904
10713
|
var rateLimiter = (_a = _g.__retrivoraRateLimiter) != null ? _a : _g.__retrivoraRateLimiter = new RateLimiter(6e4, 30);
|
|
10905
10714
|
function getRateLimitKey(req) {
|
|
10906
|
-
var
|
|
10907
|
-
const ip = ((_b = (
|
|
10715
|
+
var _a2, _b;
|
|
10716
|
+
const ip = ((_b = (_a2 = req.headers.get("x-forwarded-for")) == null ? void 0 : _a2.split(",")[0]) == null ? void 0 : _b.trim()) || req.headers.get("x-real-ip") || "127.0.0.1";
|
|
10908
10717
|
return `ip:${ip}`;
|
|
10909
10718
|
}
|
|
10910
10719
|
function checkRateLimit(req) {
|
|
@@ -10931,8 +10740,6 @@ function isFreeTier(tier) {
|
|
|
10931
10740
|
if (!tier) return true;
|
|
10932
10741
|
return FREE_TIER_NAMES.has(tier.toLowerCase().trim());
|
|
10933
10742
|
}
|
|
10934
|
-
var _a2;
|
|
10935
|
-
var _freeTierGuardInstance = (_a2 = _g.__retrivoraFreeTierGuard) != null ? _a2 : _g.__retrivoraFreeTierGuard = new FreeTierLimitsGuard();
|
|
10936
10743
|
function createPaymentRequiredResponse(reason, details) {
|
|
10937
10744
|
const body = __spreadProps(__spreadValues({
|
|
10938
10745
|
type: "https://retrivora.com/docs/errors/payment-required",
|
|
@@ -10943,10 +10750,10 @@ function createPaymentRequiredResponse(reason, details) {
|
|
|
10943
10750
|
instance: void 0
|
|
10944
10751
|
}, details != null ? details : {}), {
|
|
10945
10752
|
quota: {
|
|
10946
|
-
maxDailyRequestUnits: FREE_TIER_QUOTAS.MAX_DAILY_REQUEST_UNITS,
|
|
10947
|
-
maxTrialRequestUnits: FREE_TIER_QUOTAS.MAX_TRIAL_REQUEST_UNITS,
|
|
10948
|
-
maxDocuments: FREE_TIER_QUOTAS.MAX_DOCUMENTS,
|
|
10949
|
-
maxStorageBytes: FREE_TIER_QUOTAS.MAX_STORAGE_BYTES,
|
|
10753
|
+
maxDailyRequestUnits: FreeTierLimitsGuard_exports.FREE_TIER_QUOTAS.MAX_DAILY_REQUEST_UNITS,
|
|
10754
|
+
maxTrialRequestUnits: FreeTierLimitsGuard_exports.FREE_TIER_QUOTAS.MAX_TRIAL_REQUEST_UNITS,
|
|
10755
|
+
maxDocuments: FreeTierLimitsGuard_exports.FREE_TIER_QUOTAS.MAX_DOCUMENTS,
|
|
10756
|
+
maxStorageBytes: FreeTierLimitsGuard_exports.FREE_TIER_QUOTAS.MAX_STORAGE_BYTES,
|
|
10950
10757
|
upgradeUrl: "https://retrivora.com/pricing"
|
|
10951
10758
|
}
|
|
10952
10759
|
});
|
|
@@ -10958,17 +10765,17 @@ function createPaymentRequiredResponse(reason, details) {
|
|
|
10958
10765
|
});
|
|
10959
10766
|
}
|
|
10960
10767
|
function resolveLicenseKey(req, config, bodyLicenseKey) {
|
|
10961
|
-
|
|
10962
|
-
return req.headers.get("x-license-key") || bodyLicenseKey || ((_a3 = req.headers.get("authorization")) == null ? void 0 : _a3.replace(/^Bearer\s+/i, "")) || config.licenseKey || process.env.NEXT_PUBLIC_RETRIVORA_LICENSE_KEY || process.env.RETRIVORA_LICENSE_KEY || process.env.RAG_LICENSE_KEY || "";
|
|
10768
|
+
return req.headers.get("x-license-key") || bodyLicenseKey || config.licenseKey || process.env.RETRIVORA_LICENSE_KEY || process.env.RAG_LICENSE_KEY || "";
|
|
10963
10769
|
}
|
|
10964
10770
|
async function enforceLicense(req, config, bodyLicenseKey) {
|
|
10965
10771
|
const rawKey = resolveLicenseKey(req, config, bodyLicenseKey);
|
|
10966
10772
|
const licenseKey = (rawKey || "").trim().replace(/^["']|["']$/g, "").trim();
|
|
10967
|
-
const
|
|
10773
|
+
const url = new URL(req.url);
|
|
10774
|
+
const requestedProjectId = url.searchParams.get("projectId") || url.searchParams.get("project-id") || req.headers.get("x-project-id") || req.headers.get("X-Project-Id") || config.projectId || "";
|
|
10968
10775
|
try {
|
|
10969
10776
|
let payload;
|
|
10970
10777
|
try {
|
|
10971
|
-
payload = await LicenseVerifier.verifyAsync(licenseKey,
|
|
10778
|
+
payload = await LicenseVerifier.verifyAsync(licenseKey, requestedProjectId);
|
|
10972
10779
|
} catch (err) {
|
|
10973
10780
|
return {
|
|
10974
10781
|
ok: false,
|
|
@@ -10980,7 +10787,7 @@ async function enforceLicense(req, config, bodyLicenseKey) {
|
|
|
10980
10787
|
}
|
|
10981
10788
|
return { ok: true, payload };
|
|
10982
10789
|
} catch (err) {
|
|
10983
|
-
const message = err instanceof Error ? err.message : "Missing or invalid RETRIVORA_LICENSE_KEY. Set
|
|
10790
|
+
const message = err instanceof Error ? err.message : "Missing or invalid RETRIVORA_LICENSE_KEY. Set RETRIVORA_LICENSE_KEY in your environment or contact your administrator.";
|
|
10984
10791
|
const body = {
|
|
10985
10792
|
error: {
|
|
10986
10793
|
code: "LICENSE_REVOKED",
|
|
@@ -11038,38 +10845,59 @@ function sanitizeInput(raw) {
|
|
|
11038
10845
|
}
|
|
11039
10846
|
return { ok: true, value: trimmed };
|
|
11040
10847
|
}
|
|
10848
|
+
var _pluginCache = /* @__PURE__ */ new Map();
|
|
10849
|
+
var PLUGIN_CACHE_TTL_MS = 10 * 60 * 1e3;
|
|
10850
|
+
function configHash(config) {
|
|
10851
|
+
return (0, import_crypto2.createHash)("sha1").update(JSON.stringify(config != null ? config : {})).digest("hex").slice(0, 16);
|
|
10852
|
+
}
|
|
11041
10853
|
function getOrCreatePlugin(configOrPlugin) {
|
|
11042
10854
|
if (configOrPlugin instanceof VectorPlugin) return configOrPlugin;
|
|
11043
|
-
const
|
|
11044
|
-
|
|
11045
|
-
|
|
10855
|
+
const key = configHash(configOrPlugin);
|
|
10856
|
+
const cached = _pluginCache.get(key);
|
|
10857
|
+
if (cached && Date.now() - cached.createdAt < PLUGIN_CACHE_TTL_MS) return cached.plugin;
|
|
10858
|
+
const plugin = new VectorPlugin(configOrPlugin);
|
|
10859
|
+
_pluginCache.set(key, { plugin, createdAt: Date.now() });
|
|
10860
|
+
return plugin;
|
|
10861
|
+
}
|
|
10862
|
+
var TELEMETRY_ALLOWED_HOSTS = /* @__PURE__ */ new Set([
|
|
10863
|
+
"retrivora.com",
|
|
10864
|
+
"www.retrivora.com"
|
|
10865
|
+
]);
|
|
10866
|
+
function sanitizeTelemetryUrl(raw) {
|
|
10867
|
+
if (process.env.NODE_ENV !== "production") return raw;
|
|
10868
|
+
try {
|
|
10869
|
+
const parsed = new URL(raw);
|
|
10870
|
+
const hostname = parsed.hostname.toLowerCase().replace(/^www\./, "");
|
|
10871
|
+
if (!TELEMETRY_ALLOWED_HOSTS.has(`www.${hostname}`) && !TELEMETRY_ALLOWED_HOSTS.has(hostname)) {
|
|
10872
|
+
console.warn(
|
|
10873
|
+
`[Retrivora SDK Telemetry] Blocked non-allowlisted telemetry URL: ${parsed.hostname}. Only retrivora.com endpoints are permitted in production.`
|
|
10874
|
+
);
|
|
10875
|
+
return null;
|
|
10876
|
+
}
|
|
10877
|
+
return parsed.toString();
|
|
10878
|
+
} catch (e) {
|
|
10879
|
+
return null;
|
|
11046
10880
|
}
|
|
11047
|
-
return _g[cacheKey];
|
|
11048
10881
|
}
|
|
11049
10882
|
function reportTelemetry(req, plugin, action, status, details, trace) {
|
|
11050
|
-
var
|
|
10883
|
+
var _a2, _b, _c, _d, _e, _f, _g2;
|
|
11051
10884
|
try {
|
|
11052
10885
|
const config = plugin.getConfig();
|
|
11053
|
-
const licenseKey = config.licenseKey || process.env.
|
|
10886
|
+
const licenseKey = config.licenseKey || process.env.RETRIVORA_LICENSE_KEY || process.env.RAG_LICENSE_KEY;
|
|
11054
10887
|
const telemetryConfig = config.telemetry;
|
|
11055
|
-
const host = req.headers.get("host") || "localhost";
|
|
11056
10888
|
const defaultUrl = "https://www.retrivora.com/api/telemetry";
|
|
11057
|
-
const
|
|
10889
|
+
const rawTelemetryUrl = (telemetryConfig == null ? void 0 : telemetryConfig.url) || process.env.TELEMETRY_URL || process.env.NEXT_PUBLIC_TELEMETRY_URL || defaultUrl;
|
|
10890
|
+
const telemetryUrl = sanitizeTelemetryUrl(rawTelemetryUrl);
|
|
10891
|
+
if (!telemetryUrl) return;
|
|
11058
10892
|
const userAgent = req.headers.get("user-agent") || `Retrivora-SDK/${SDK_VERSION}`;
|
|
11059
|
-
let absoluteUrl = telemetryUrl;
|
|
11060
|
-
if (!telemetryUrl.startsWith("http")) {
|
|
11061
|
-
const proto = req.headers.get("x-forwarded-proto") || "http";
|
|
11062
|
-
absoluteUrl = `${proto}://${host}${telemetryUrl}`;
|
|
11063
|
-
}
|
|
11064
10893
|
const projectId = config.projectId || "default";
|
|
11065
|
-
const model = (trace == null ? void 0 : trace.model) || ((
|
|
10894
|
+
const model = (trace == null ? void 0 : trace.model) || ((_a2 = config.llm) == null ? void 0 : _a2.model) || ((_b = config.embedding) == null ? void 0 : _b.model) || "llama-3.1-8b-instant";
|
|
11066
10895
|
const provider = (trace == null ? void 0 : trace.provider) || ((_c = config.llm) == null ? void 0 : _c.provider) || ((_d = config.embedding) == null ? void 0 : _d.provider) || "groq";
|
|
11067
10896
|
const tokens = Number(((_e = trace == null ? void 0 : trace.tokens) == null ? void 0 : _e.totalTokens) || (trace == null ? void 0 : trace.totalTokens) || 0);
|
|
11068
10897
|
const costUsd = Number(((_f = trace == null ? void 0 : trace.tokens) == null ? void 0 : _f.estimatedCostUsd) || (trace == null ? void 0 : trace.costUsd) || 0);
|
|
11069
10898
|
const latencyMs = Number(((_g2 = trace == null ? void 0 : trace.latency) == null ? void 0 : _g2.totalMs) || (trace == null ? void 0 : trace.latencyMs) || 0);
|
|
11070
10899
|
const payload = {
|
|
11071
10900
|
trace,
|
|
11072
|
-
licenseKey,
|
|
11073
10901
|
projectId,
|
|
11074
10902
|
organization: process.env.RETRIVORA_ORGANIZATION || "default-org",
|
|
11075
10903
|
sdkVersion: SDK_VERSION,
|
|
@@ -11086,14 +10914,13 @@ function reportTelemetry(req, plugin, action, status, details, trace) {
|
|
|
11086
10914
|
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
11087
10915
|
details
|
|
11088
10916
|
};
|
|
11089
|
-
fetch(
|
|
10917
|
+
fetch(telemetryUrl, {
|
|
11090
10918
|
method: "POST",
|
|
11091
|
-
headers: {
|
|
11092
|
-
"Content-Type": "application/json"
|
|
11093
|
-
|
|
11094
|
-
"x-real-ip": req.headers.get("x-real-ip") || "",
|
|
10919
|
+
headers: __spreadProps(__spreadValues({
|
|
10920
|
+
"Content-Type": "application/json"
|
|
10921
|
+
}, licenseKey ? { "x-license-key": licenseKey } : {}), {
|
|
11095
10922
|
"user-agent": userAgent
|
|
11096
|
-
},
|
|
10923
|
+
}),
|
|
11097
10924
|
body: JSON.stringify(payload)
|
|
11098
10925
|
}).catch((err) => {
|
|
11099
10926
|
console.warn("[Retrivora SDK Telemetry] Async report warning:", err.message);
|
|
@@ -11143,7 +10970,7 @@ function createChatHandler(configOrPlugin, options) {
|
|
|
11143
10970
|
const storage = new DatabaseStorage(plugin.getConfig());
|
|
11144
10971
|
const onAuthorize = options == null ? void 0 : options.onAuthorize;
|
|
11145
10972
|
return async function POST(req, context) {
|
|
11146
|
-
var
|
|
10973
|
+
var _a2, _b, _c, _d, _e, _f;
|
|
11147
10974
|
const authResult = await checkAuth(req, onAuthorize);
|
|
11148
10975
|
if (authResult) return authResult;
|
|
11149
10976
|
const rateLimited = checkRateLimit(req);
|
|
@@ -11156,18 +10983,17 @@ function createChatHandler(configOrPlugin, options) {
|
|
|
11156
10983
|
if (!licenseCheck.ok) return licenseCheck.response;
|
|
11157
10984
|
if (isFreeTier(licenseCheck.payload.tier)) {
|
|
11158
10985
|
const estimatedInputTokens = Math.ceil(
|
|
11159
|
-
((((
|
|
11160
|
-
var
|
|
11161
|
-
return a + (((
|
|
10986
|
+
((((_a2 = bodyObj.message) == null ? void 0 : _a2.length) || 0) + (Array.isArray(bodyObj.messages) ? bodyObj.messages.reduce((a, m) => {
|
|
10987
|
+
var _a3, _b2;
|
|
10988
|
+
return a + (((_a3 = m == null ? void 0 : m.content) == null ? void 0 : _a3.length) || ((_b2 = m == null ? void 0 : m.text) == null ? void 0 : _b2.length) || 0);
|
|
11162
10989
|
}, 0) : 0) + (((_b = bodyObj.history) == null ? void 0 : _b.reduce((a, m) => {
|
|
11163
|
-
var
|
|
11164
|
-
return a + (((
|
|
10990
|
+
var _a3, _b2;
|
|
10991
|
+
return a + (((_a3 = m == null ? void 0 : m.content) == null ? void 0 : _a3.length) || ((_b2 = m == null ? void 0 : m.text) == null ? void 0 : _b2.length) || 0);
|
|
11165
10992
|
}, 0)) || 0)) / 4
|
|
11166
10993
|
);
|
|
11167
|
-
const chatCheck = FreeTierLimitsGuard.checkRequestAllowed({
|
|
10994
|
+
const chatCheck = FreeTierLimitsGuard_exports.FreeTierLimitsGuard.checkRequestAllowed({
|
|
11168
10995
|
operationType: "chat",
|
|
11169
|
-
inputTokens: estimatedInputTokens
|
|
11170
|
-
projectId: licenseCheck.payload.projectId
|
|
10996
|
+
inputTokens: estimatedInputTokens
|
|
11171
10997
|
});
|
|
11172
10998
|
if (!chatCheck.allowed) {
|
|
11173
10999
|
return createPaymentRequiredResponse(
|
|
@@ -11226,7 +11052,7 @@ function createStreamHandler(configOrPlugin, options) {
|
|
|
11226
11052
|
const storage = new DatabaseStorage(plugin.getConfig());
|
|
11227
11053
|
const onAuthorize = options == null ? void 0 : options.onAuthorize;
|
|
11228
11054
|
return async function POST(req, context) {
|
|
11229
|
-
var
|
|
11055
|
+
var _a2, _b;
|
|
11230
11056
|
const authResult = await checkAuth(req, onAuthorize);
|
|
11231
11057
|
if (authResult) return authResult;
|
|
11232
11058
|
const rateLimited = checkRateLimit(req);
|
|
@@ -11234,10 +11060,6 @@ function createStreamHandler(configOrPlugin, options) {
|
|
|
11234
11060
|
const config = plugin.getConfig();
|
|
11235
11061
|
let body;
|
|
11236
11062
|
try {
|
|
11237
|
-
if ((_a3 = req.headers.get("content-type")) == null ? void 0 : _a3.includes("multipart/form-data")) {
|
|
11238
|
-
const uploader = createUploadHandler(plugin, { onAuthorize });
|
|
11239
|
-
return uploader(req);
|
|
11240
|
-
}
|
|
11241
11063
|
body = await req.json();
|
|
11242
11064
|
} catch (e) {
|
|
11243
11065
|
return new Response(JSON.stringify({ error: { code: "INVALID_JSON", message: "Invalid JSON body" } }), {
|
|
@@ -11250,15 +11072,15 @@ function createStreamHandler(configOrPlugin, options) {
|
|
|
11250
11072
|
if (!licenseCheck.ok) return licenseCheck.response;
|
|
11251
11073
|
if (isFreeTier(licenseCheck.payload.tier)) {
|
|
11252
11074
|
const estimatedInputTokens = Math.ceil(
|
|
11253
|
-
((((
|
|
11254
|
-
var
|
|
11255
|
-
return a + (((
|
|
11256
|
-
}, 0) : 0) + (((
|
|
11257
|
-
var
|
|
11258
|
-
return a + (((
|
|
11075
|
+
((((_a2 = bodyObj.message) == null ? void 0 : _a2.length) || 0) + (Array.isArray(bodyObj.messages) ? bodyObj.messages.reduce((a, m) => {
|
|
11076
|
+
var _a3, _b2;
|
|
11077
|
+
return a + (((_a3 = m == null ? void 0 : m.content) == null ? void 0 : _a3.length) || ((_b2 = m == null ? void 0 : m.text) == null ? void 0 : _b2.length) || 0);
|
|
11078
|
+
}, 0) : 0) + (((_b = bodyObj.history) == null ? void 0 : _b.reduce((a, m) => {
|
|
11079
|
+
var _a3, _b2;
|
|
11080
|
+
return a + (((_a3 = m == null ? void 0 : m.content) == null ? void 0 : _a3.length) || ((_b2 = m == null ? void 0 : m.text) == null ? void 0 : _b2.length) || 0);
|
|
11259
11081
|
}, 0)) || 0)) / 4
|
|
11260
11082
|
);
|
|
11261
|
-
const chatCheck = FreeTierLimitsGuard.checkRequestAllowed({
|
|
11083
|
+
const chatCheck = FreeTierLimitsGuard_exports.FreeTierLimitsGuard.checkRequestAllowed({
|
|
11262
11084
|
operationType: "chat_stream",
|
|
11263
11085
|
inputTokens: estimatedInputTokens,
|
|
11264
11086
|
projectId: licenseCheck.payload.projectId
|
|
@@ -11301,7 +11123,7 @@ function createStreamHandler(configOrPlugin, options) {
|
|
|
11301
11123
|
let isActive = true;
|
|
11302
11124
|
const stream = new ReadableStream({
|
|
11303
11125
|
async start(controller) {
|
|
11304
|
-
var
|
|
11126
|
+
var _a3, _b2, _c, _d, _e;
|
|
11305
11127
|
const enqueue = (text) => {
|
|
11306
11128
|
if (!isActive) return;
|
|
11307
11129
|
try {
|
|
@@ -11334,7 +11156,7 @@ function createStreamHandler(configOrPlugin, options) {
|
|
|
11334
11156
|
let uiTransformation = responseChunk == null ? void 0 : responseChunk.ui_transformation;
|
|
11335
11157
|
if (sources.length > 0) {
|
|
11336
11158
|
try {
|
|
11337
|
-
uiTransformation = (
|
|
11159
|
+
uiTransformation = (_a3 = responseChunk == null ? void 0 : responseChunk.ui_transformation) != null ? _a3 : UITransformer.transform(message, sources, plugin.getConfig());
|
|
11338
11160
|
if (uiTransformation) {
|
|
11339
11161
|
enqueue(sseUIFrame(uiTransformation));
|
|
11340
11162
|
}
|
|
@@ -11358,7 +11180,7 @@ function createStreamHandler(configOrPlugin, options) {
|
|
|
11358
11180
|
uiTransformation,
|
|
11359
11181
|
trace: responseChunk == null ? void 0 : responseChunk.trace
|
|
11360
11182
|
}).catch((err) => console.warn("[createStreamHandler] Failed to save assistant message:", err));
|
|
11361
|
-
reportTelemetry(req, plugin, "QUERY_GENERATION", "success", `Tokens: ${((
|
|
11183
|
+
reportTelemetry(req, plugin, "QUERY_GENERATION", "success", `Tokens: ${((_c = (_b2 = responseChunk == null ? void 0 : responseChunk.trace) == null ? void 0 : _b2.tokens) == null ? void 0 : _c.totalTokens) || 0}, Latency: ${((_e = (_d = responseChunk == null ? void 0 : responseChunk.trace) == null ? void 0 : _d.latency) == null ? void 0 : _e.totalMs) || 0}ms`, responseChunk == null ? void 0 : responseChunk.trace);
|
|
11362
11184
|
}
|
|
11363
11185
|
}
|
|
11364
11186
|
} catch (temp) {
|
|
@@ -11423,7 +11245,7 @@ function createIngestHandler(configOrPlugin, options) {
|
|
|
11423
11245
|
(sum, d) => sum + Buffer.byteLength(d.content || "", "utf8"),
|
|
11424
11246
|
0
|
|
11425
11247
|
);
|
|
11426
|
-
const ingestCheck = FreeTierLimitsGuard.checkIngestionAllowed(
|
|
11248
|
+
const ingestCheck = FreeTierLimitsGuard_exports.FreeTierLimitsGuard.checkIngestionAllowed(
|
|
11427
11249
|
{ documentCount: documents.length },
|
|
11428
11250
|
totalIncomingBytes
|
|
11429
11251
|
);
|
|
@@ -11475,7 +11297,6 @@ function createLicenseHandler(configOrPlugin, options) {
|
|
|
11475
11297
|
const plugin = getOrCreatePlugin(configOrPlugin);
|
|
11476
11298
|
const onAuthorize = options == null ? void 0 : options.onAuthorize;
|
|
11477
11299
|
return async function POST(req) {
|
|
11478
|
-
var _a3;
|
|
11479
11300
|
if (req) {
|
|
11480
11301
|
const authResult = await checkAuth(req, onAuthorize);
|
|
11481
11302
|
if (authResult) return authResult;
|
|
@@ -11483,7 +11304,17 @@ function createLicenseHandler(configOrPlugin, options) {
|
|
|
11483
11304
|
try {
|
|
11484
11305
|
const body = await req.json().catch(() => ({}));
|
|
11485
11306
|
const config = plugin.getConfig();
|
|
11486
|
-
const
|
|
11307
|
+
const authHeader = req.headers.get("authorization");
|
|
11308
|
+
let authHeaderKey = "";
|
|
11309
|
+
if (authHeader && authHeader.toLowerCase().startsWith("bearer ")) {
|
|
11310
|
+
authHeaderKey = authHeader.replace(/^Bearer\s+/i, "");
|
|
11311
|
+
if (authHeaderKey) {
|
|
11312
|
+
console.warn(
|
|
11313
|
+
'[Retrivora SDK] DEPRECATION WARNING (SEC-7): Repurposing the standard "Authorization: Bearer" header to carry the license key is deprecated and will be removed in a future release. Please use the custom "x-license-key" header instead.'
|
|
11314
|
+
);
|
|
11315
|
+
}
|
|
11316
|
+
}
|
|
11317
|
+
const rawKey = req.headers.get("x-license-key") || (body == null ? void 0 : body.licenseKey) || authHeaderKey || config.licenseKey || process.env.RETRIVORA_LICENSE_KEY || "";
|
|
11487
11318
|
const licenseKey = (rawKey || "").trim().replace(/^["']|["']$/g, "").trim();
|
|
11488
11319
|
const projectId = (body == null ? void 0 : body.projectId) || config.projectId || "";
|
|
11489
11320
|
const payload = await LicenseVerifier.verifyAsync(licenseKey, projectId);
|
|
@@ -11534,7 +11365,7 @@ function createUploadHandler(configOrPlugin, options) {
|
|
|
11534
11365
|
}
|
|
11535
11366
|
if (isFreeTier(licenseCheck.payload.tier)) {
|
|
11536
11367
|
const totalUploadBytes = files.reduce((sum, f) => sum + (f.size || 0), 0);
|
|
11537
|
-
const uploadCheck = FreeTierLimitsGuard.checkIngestionAllowed(
|
|
11368
|
+
const uploadCheck = FreeTierLimitsGuard_exports.FreeTierLimitsGuard.checkIngestionAllowed(
|
|
11538
11369
|
{ documentCount: files.length },
|
|
11539
11370
|
totalUploadBytes
|
|
11540
11371
|
);
|
|
@@ -11546,8 +11377,8 @@ function createUploadHandler(configOrPlugin, options) {
|
|
|
11546
11377
|
projectId: licenseCheck.payload.projectId,
|
|
11547
11378
|
fileCount: files.length,
|
|
11548
11379
|
totalUploadBytes,
|
|
11549
|
-
maxFileSizeBytes: FREE_TIER_QUOTAS.MAX_FILE_SIZE_BYTES,
|
|
11550
|
-
maxStorageBytes: FREE_TIER_QUOTAS.MAX_STORAGE_BYTES
|
|
11380
|
+
maxFileSizeBytes: FreeTierLimitsGuard_exports.FREE_TIER_QUOTAS.MAX_FILE_SIZE_BYTES,
|
|
11381
|
+
maxStorageBytes: FreeTierLimitsGuard_exports.FREE_TIER_QUOTAS.MAX_STORAGE_BYTES
|
|
11551
11382
|
}
|
|
11552
11383
|
);
|
|
11553
11384
|
}
|
|
@@ -11861,7 +11692,7 @@ function createRagHandler(configOrPlugin, options) {
|
|
|
11861
11692
|
}
|
|
11862
11693
|
}
|
|
11863
11694
|
async function getSegment(req, context) {
|
|
11864
|
-
var
|
|
11695
|
+
var _a2, _b, _c;
|
|
11865
11696
|
const contentType = req.headers.get("content-type") || "";
|
|
11866
11697
|
if (contentType.includes("multipart/form-data")) {
|
|
11867
11698
|
return "upload";
|
|
@@ -11880,7 +11711,7 @@ function createRagHandler(configOrPlugin, options) {
|
|
|
11880
11711
|
if (pathname.endsWith("/sessions")) return "sessions";
|
|
11881
11712
|
} catch (e) {
|
|
11882
11713
|
}
|
|
11883
|
-
const resolvedParams = typeof ((
|
|
11714
|
+
const resolvedParams = typeof ((_a2 = context == null ? void 0 : context.params) == null ? void 0 : _a2.then) === "function" ? await context.params : context == null ? void 0 : context.params;
|
|
11884
11715
|
const segments = (resolvedParams == null ? void 0 : resolvedParams.retrivora) || ((_b = resolvedParams == null ? void 0 : resolvedParams.params) == null ? void 0 : _b.retrivora) || (resolvedParams == null ? void 0 : resolvedParams.rag) || ((_c = resolvedParams == null ? void 0 : resolvedParams.params) == null ? void 0 : _c.rag) || [];
|
|
11885
11716
|
const joined = segments.join("/");
|
|
11886
11717
|
return joined || "chat";
|