@retrivora-ai/rag-engine 2.4.3 → 2.4.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (139) hide show
  1. package/dist/{BatchProcessor-zbQMaZHm.d.mts → BatchProcessor-Csy_K1qQ.d.mts} +2 -0
  2. package/dist/{BatchProcessor-D42E706a.d.ts → BatchProcessor-Dg18zZGk.d.ts} +2 -0
  3. package/dist/handlers/index.js +27 -4
  4. package/dist/handlers/index.mjs +27 -4
  5. package/dist/index.d.mts +3 -3
  6. package/dist/index.d.ts +3 -3
  7. package/dist/index.js +12 -10
  8. package/dist/index.mjs +12 -10
  9. package/dist/server.d.mts +2 -2
  10. package/dist/server.d.ts +2 -2
  11. package/dist/server.js +28 -4
  12. package/dist/server.mjs +28 -4
  13. package/package.json +1 -3
  14. package/src/LicensedRetrivora.ts +0 -278
  15. package/src/app/api/chat/route.ts +0 -4
  16. package/src/app/api/health/route.ts +0 -5
  17. package/src/app/api/ingest/route.ts +0 -27
  18. package/src/app/api/suggestions/route.ts +0 -5
  19. package/src/app/api/upload/route.ts +0 -5
  20. package/src/app/constants.tsx +0 -270
  21. package/src/app/favicon.ico +0 -0
  22. package/src/app/globals.css +0 -449
  23. package/src/app/layout.tsx +0 -22
  24. package/src/app/page.tsx +0 -64
  25. package/src/app/types.ts +0 -30
  26. package/src/browser.tsx +0 -127
  27. package/src/components/ChatWidget.tsx +0 -393
  28. package/src/components/ChatWindow.tsx +0 -952
  29. package/src/components/CodeViewer.tsx +0 -56
  30. package/src/components/ConfigProvider.tsx +0 -141
  31. package/src/components/DocumentUpload.tsx +0 -354
  32. package/src/components/DynamicChart.tsx +0 -297
  33. package/src/components/HourglassLoader.tsx +0 -87
  34. package/src/components/MarkdownComponents.tsx +0 -186
  35. package/src/components/MessageBubble.tsx +0 -498
  36. package/src/components/ObservabilityPanel.tsx +0 -374
  37. package/src/components/ProductCard.tsx +0 -107
  38. package/src/components/ProductCarousel.tsx +0 -97
  39. package/src/components/SourceCard.tsx +0 -61
  40. package/src/components/ThemeProvider.tsx +0 -8
  41. package/src/components/ThemeToggle.tsx +0 -29
  42. package/src/components/ThinkingBlock.tsx +0 -75
  43. package/src/components/UIDispatcher.tsx +0 -342
  44. package/src/components/VisualizationRenderer.tsx +0 -621
  45. package/src/components/constants.tsx +0 -503
  46. package/src/config/ConfigBuilder.ts +0 -285
  47. package/src/config/EmbeddingStrategy.ts +0 -148
  48. package/src/config/LicenseConfigResolver.ts +0 -298
  49. package/src/config/RagConfig.ts +0 -328
  50. package/src/config/UniversalProfiles.ts +0 -82
  51. package/src/config/constants.ts +0 -64
  52. package/src/config/serverConfig.ts +0 -378
  53. package/src/config/uiConstants.ts +0 -47
  54. package/src/core/BatchProcessor.ts +0 -385
  55. package/src/core/CircuitBreaker.ts +0 -118
  56. package/src/core/ConfigFetcher.ts +0 -220
  57. package/src/core/ConfigResolver.ts +0 -165
  58. package/src/core/ConfigValidator.ts +0 -197
  59. package/src/core/DatabaseStorage.ts +0 -526
  60. package/src/core/FreeTierLimitsGuard.ts +0 -281
  61. package/src/core/LLMRouter.ts +0 -79
  62. package/src/core/LangChainAgent.ts +0 -132
  63. package/src/core/LicenseValidator.ts +0 -284
  64. package/src/core/LicenseVerifier.ts +0 -261
  65. package/src/core/MultiAgentCoordinator.ts +0 -239
  66. package/src/core/Pipeline.ts +0 -1279
  67. package/src/core/PluginManager.ts +0 -277
  68. package/src/core/ProjectSettings.ts +0 -247
  69. package/src/core/ProviderHealthCheck.ts +0 -133
  70. package/src/core/ProviderInterfaces.ts +0 -37
  71. package/src/core/ProviderRegistry.ts +0 -156
  72. package/src/core/QueryProcessor.ts +0 -399
  73. package/src/core/Retrivora.ts +0 -98
  74. package/src/core/VectorPlugin.ts +0 -193
  75. package/src/core/mcp.ts +0 -261
  76. package/src/exceptions/index.ts +0 -131
  77. package/src/handlers/index.ts +0 -1625
  78. package/src/hooks/useHydrated.ts +0 -13
  79. package/src/hooks/useProjectSettings.ts +0 -105
  80. package/src/hooks/useRagChat.ts +0 -436
  81. package/src/hooks/useStoredMessages.ts +0 -64
  82. package/src/index.css +0 -4269
  83. package/src/index.ts +0 -137
  84. package/src/lib/plugin.ts +0 -24
  85. package/src/llm/ILLMProvider.ts +0 -60
  86. package/src/llm/LLMFactory.ts +0 -141
  87. package/src/llm/providers/AnthropicProvider.ts +0 -215
  88. package/src/llm/providers/GeminiProvider.ts +0 -311
  89. package/src/llm/providers/GroqProvider.ts +0 -184
  90. package/src/llm/providers/OllamaProvider.ts +0 -232
  91. package/src/llm/providers/OpenAIProvider.ts +0 -188
  92. package/src/llm/providers/QwenProvider.ts +0 -199
  93. package/src/llm/providers/UniversalLLMAdapter.ts +0 -422
  94. package/src/llm/utils.ts +0 -46
  95. package/src/providers/graphdb/BaseGraphProvider.ts +0 -43
  96. package/src/providers/graphdb/SimpleGraphProvider.ts +0 -66
  97. package/src/providers/vectordb/BaseVectorProvider.ts +0 -98
  98. package/src/providers/vectordb/ChromaDBProvider.ts +0 -130
  99. package/src/providers/vectordb/MilvusProvider.ts +0 -134
  100. package/src/providers/vectordb/MongoDBProvider.ts +0 -250
  101. package/src/providers/vectordb/MultiTablePostgresProvider.ts +0 -461
  102. package/src/providers/vectordb/PineconeProvider.ts +0 -295
  103. package/src/providers/vectordb/PostgreSQLProvider.ts +0 -227
  104. package/src/providers/vectordb/QdrantProvider.ts +0 -294
  105. package/src/providers/vectordb/RedisProvider.ts +0 -105
  106. package/src/providers/vectordb/UniversalVectorProvider.ts +0 -221
  107. package/src/providers/vectordb/WeaviateProvider.ts +0 -172
  108. package/src/rag/DocumentChunker.ts +0 -144
  109. package/src/rag/EntityExtractor.ts +0 -90
  110. package/src/rag/LlamaIndexIngestor.ts +0 -60
  111. package/src/rag/Reranker.ts +0 -106
  112. package/src/rendering/IntentClassifier.ts +0 -167
  113. package/src/rendering/RendererRegistry.ts +0 -164
  114. package/src/rendering/RuleEngine.ts +0 -67
  115. package/src/rendering/VisualizationDecisionEngine.ts +0 -164
  116. package/src/rendering/__tests__/VisualizationDecisionEngine.test.ts +0 -237
  117. package/src/rendering/index.ts +0 -12
  118. package/src/rendering/rules/Rule1SpecificInfoRule.ts +0 -28
  119. package/src/rendering/rules/Rule2ComparisonRule.ts +0 -24
  120. package/src/rendering/rules/Rule3ProductDiscoveryRule.ts +0 -28
  121. package/src/rendering/rules/Rule4AnalyticalRule.ts +0 -38
  122. package/src/rendering/rules/Rule5MixedResponseRule.ts +0 -29
  123. package/src/rendering/rules/Rule6SmallResultSetRule.ts +0 -24
  124. package/src/rendering/rules/Rule7LargeTableRule.ts +0 -35
  125. package/src/rendering/types.ts +0 -96
  126. package/src/server.ts +0 -156
  127. package/src/tailwind.css +0 -194
  128. package/src/telemetry/TelemetryService.ts +0 -330
  129. package/src/types/chat.ts +0 -89
  130. package/src/types/index.ts +0 -278
  131. package/src/types/pdf-parse.d.ts +0 -13
  132. package/src/types/props.ts +0 -169
  133. package/src/utils/DocumentParser.ts +0 -111
  134. package/src/utils/ProductExtractor.ts +0 -350
  135. package/src/utils/SchemaMapper.ts +0 -176
  136. package/src/utils/UITransformer.ts +0 -1812
  137. package/src/utils/synonyms.ts +0 -80
  138. package/src/utils/templateUtils.ts +0 -78
  139. package/src/version.ts +0 -6
@@ -1,281 +0,0 @@
1
- /**
2
- * FreeTierLimitsGuard — Enforces strict operational quotas for Free Tier workspaces.
3
- * Retrivora Free Tier (Hobby) policy enforcement & Request Units accounting.
4
- *
5
- * Inline copy for the published SDK package (@retrivora-ai/rag-engine). The
6
- * canonical source lives in `services/billing/FreeTierLimitsGuard.ts` and is
7
- * kept in sync for the monorepo via copy on changes; bundling it here ensures
8
- * published dist builds don't depend on `@services/*` path aliases that only
9
- * resolve inside the Turborepo workspace.
10
- */
11
-
12
- export interface WorkspaceUsageStats {
13
- documentCount: number;
14
- totalStorageBytes: number;
15
- totalEmbeddingsCount: number;
16
- dailyQueriesCount: number;
17
- totalRequestUnitsUsed?: number;
18
- dailyRequestUnitsUsed?: number;
19
- trialStartedAt?: string;
20
- concurrentRequests?: number;
21
- }
22
-
23
- export const FREE_TIER_QUOTAS = {
24
- MAX_DOCUMENTS: 25,
25
- MAX_FILE_SIZE_BYTES: 20 * 1024 * 1024,
26
- MAX_STORAGE_BYTES: 250 * 1024 * 1024,
27
- MAX_TRIAL_REQUEST_UNITS: 500,
28
- MAX_DAILY_REQUEST_UNITS: 50,
29
- TRIAL_DURATION_DAYS: 14,
30
- GRACE_PERIOD_DAYS: 2,
31
- MAX_RPM: 10,
32
- MAX_RPH: 100,
33
- MAX_RPD: 500,
34
- MAX_CONCURRENT_REQUESTS: 2,
35
- MAX_INPUT_TOKENS: 20000,
36
- MAX_OUTPUT_TOKENS: 4000,
37
- UPGRADE_REMINDER_DAYS: [7, 12, 14] as const,
38
- MAX_USERS: 1,
39
- MAX_NAMESPACES: 1,
40
- MAX_PROJECTS: 1,
41
- } as const;
42
-
43
- export interface TrialTimestamps {
44
- trial_started_at: string;
45
- trial_expires_at: string;
46
- grace_period_expires_at: string;
47
- }
48
-
49
- export interface TrialStatusResult {
50
- status: 'active' | 'grace_period' | 'expired';
51
- daysElapsed: number;
52
- daysRemaining: number;
53
- isGracePeriod: boolean;
54
- isExpired: boolean;
55
- upgradeReminderDue?: number;
56
- usageReminderDue?: '80%' | '90%' | '100%';
57
- reason?: string;
58
- }
59
-
60
- export interface RequestCheckParams {
61
- operationType?: string;
62
- inputTokens?: number;
63
- outputTokens?: number;
64
- dailyUnitsUsed?: number;
65
- totalUnitsUsed?: number;
66
- trialStartedAt?: string | Date;
67
- concurrentRequests?: number;
68
- chunkCount?: number;
69
- nowServerTime?: Date;
70
- }
71
-
72
- export class FreeTierLimitsGuard {
73
- static calculateTrialTimestamps(startedAt?: Date | string): TrialTimestamps {
74
- const startDate = startedAt ? new Date(startedAt) : new Date();
75
- const trialStartedAtMs = startDate.getTime();
76
-
77
- const trialExpiresAtMs = trialStartedAtMs + FREE_TIER_QUOTAS.TRIAL_DURATION_DAYS * 24 * 60 * 60 * 1000;
78
- const gracePeriodExpiresAtMs =
79
- trialStartedAtMs + (FREE_TIER_QUOTAS.TRIAL_DURATION_DAYS + FREE_TIER_QUOTAS.GRACE_PERIOD_DAYS) * 24 * 60 * 60 * 1000;
80
-
81
- return {
82
- trial_started_at: new Date(trialStartedAtMs).toISOString(),
83
- trial_expires_at: new Date(trialExpiresAtMs).toISOString(),
84
- grace_period_expires_at: new Date(gracePeriodExpiresAtMs).toISOString(),
85
- };
86
- }
87
-
88
- static calculateRequestUnits(operationType: string, options?: { chunkCount?: number }): number {
89
- const op = operationType.toLowerCase().trim();
90
-
91
- if (
92
- op.includes('suggestion') ||
93
- op.includes('template') ||
94
- op.includes('history') ||
95
- op.includes('model') ||
96
- op.includes('health') ||
97
- op.includes('auth') ||
98
- op.includes('license') ||
99
- op.includes('usage') ||
100
- op.includes('feedback') ||
101
- op.includes('analytic')
102
- ) {
103
- return 0;
104
- }
105
-
106
- if (op.includes('image')) {
107
- return 10;
108
- }
109
-
110
- if (op.includes('embedding')) {
111
- const chunks = options?.chunkCount ?? 1;
112
- return Math.max(1, Math.ceil(chunks / 1000));
113
- }
114
-
115
- return 1;
116
- }
117
-
118
- static checkTrialStatus(
119
- startedAt?: Date | string,
120
- totalUnitsUsed = 0,
121
- nowServerTime = new Date()
122
- ): TrialStatusResult {
123
- const timestamps = this.calculateTrialTimestamps(startedAt);
124
- const startMs = new Date(timestamps.trial_started_at).getTime();
125
- const expireMs = new Date(timestamps.trial_expires_at).getTime();
126
- const graceExpireMs = new Date(timestamps.grace_period_expires_at).getTime();
127
- const nowMs = nowServerTime.getTime();
128
-
129
- const daysElapsed = Math.floor(Math.max(0, nowMs - startMs) / (1000 * 60 * 60 * 24));
130
- const daysRemaining = Math.max(0, Math.ceil((expireMs - nowMs) / (1000 * 60 * 60 * 24)));
131
-
132
- const isConsumedUnits = totalUnitsUsed >= FREE_TIER_QUOTAS.MAX_TRIAL_REQUEST_UNITS;
133
- const isExpiredTime = nowMs > expireMs;
134
- const isPastGraceTime = nowMs > graceExpireMs;
135
-
136
- let status: 'active' | 'grace_period' | 'expired' = 'active';
137
- let reason: string | undefined;
138
-
139
- if (isPastGraceTime || (isConsumedUnits && isExpiredTime)) {
140
- status = 'expired';
141
- reason = 'Trial period and grace period have expired. Upgrade your plan.';
142
- } else if (isExpiredTime || isConsumedUnits) {
143
- status = 'grace_period';
144
- reason = isConsumedUnits
145
- ? 'Total trial request quota (500 units) consumed. Grace period active (dashboard access only).'
146
- : '14-day trial period expired. 2-day grace period active (dashboard access only).';
147
- }
148
-
149
- let upgradeReminderDue: number | undefined;
150
- if (FREE_TIER_QUOTAS.UPGRADE_REMINDER_DAYS.includes(daysElapsed as any)) {
151
- upgradeReminderDue = daysElapsed;
152
- }
153
-
154
- let usageReminderDue: '80%' | '90%' | '100%' | undefined;
155
- const usagePercent = (totalUnitsUsed / FREE_TIER_QUOTAS.MAX_TRIAL_REQUEST_UNITS) * 100;
156
- if (usagePercent >= 100) {
157
- usageReminderDue = '100%';
158
- } else if (usagePercent >= 90) {
159
- usageReminderDue = '90%';
160
- } else if (usagePercent >= 80) {
161
- usageReminderDue = '80%';
162
- }
163
-
164
- return {
165
- status,
166
- daysElapsed,
167
- daysRemaining,
168
- isGracePeriod: status === 'grace_period',
169
- isExpired: status === 'expired',
170
- upgradeReminderDue,
171
- usageReminderDue,
172
- reason,
173
- };
174
- }
175
-
176
- static checkIngestionAllowed(
177
- stats: Partial<WorkspaceUsageStats>,
178
- incomingSizeBytes = 0
179
- ): { allowed: boolean; reason?: string } {
180
- const docs = stats.documentCount ?? 0;
181
- const storage = stats.totalStorageBytes ?? 0;
182
-
183
- if (incomingSizeBytes > FREE_TIER_QUOTAS.MAX_FILE_SIZE_BYTES) {
184
- const mbSize = (incomingSizeBytes / (1024 * 1024)).toFixed(1);
185
- return {
186
- allowed: false,
187
- reason: `[FreeTierIngestor] File size (${mbSize}MB) exceeds maximum allowed size of 20MB under Free Tier rules.`,
188
- };
189
- }
190
-
191
- if (docs >= FREE_TIER_QUOTAS.MAX_DOCUMENTS) {
192
- return {
193
- allowed: false,
194
- reason: `Document limit reached (${docs}/${FREE_TIER_QUOTAS.MAX_DOCUMENTS}). Upgrade to Pro to ingest more documents.`,
195
- };
196
- }
197
-
198
- if (storage + incomingSizeBytes > FREE_TIER_QUOTAS.MAX_STORAGE_BYTES) {
199
- const mbUsed = (storage / (1024 * 1024)).toFixed(1);
200
- return {
201
- allowed: false,
202
- reason: `Storage quota exceeded (${mbUsed}MB / 250MB). Upgrade for unlimited storage.`,
203
- };
204
- }
205
-
206
- return { allowed: true };
207
- }
208
-
209
- static checkRequestAllowed(params: RequestCheckParams): { allowed: boolean; reason?: string; costUnits?: number } {
210
- const op = params.operationType ?? 'chat';
211
- const costUnits = this.calculateRequestUnits(op, { chunkCount: params.chunkCount });
212
-
213
- if (costUnits === 0) {
214
- return { allowed: true, costUnits: 0 };
215
- }
216
-
217
- if (params.inputTokens && params.inputTokens > FREE_TIER_QUOTAS.MAX_INPUT_TOKENS) {
218
- return {
219
- allowed: false,
220
- reason: 'Token limit exceeded. Upgrade your plan.',
221
- costUnits,
222
- };
223
- }
224
- if (params.outputTokens && params.outputTokens > FREE_TIER_QUOTAS.MAX_OUTPUT_TOKENS) {
225
- return {
226
- allowed: false,
227
- reason: 'Token limit exceeded. Upgrade your plan.',
228
- costUnits,
229
- };
230
- }
231
-
232
- if (params.concurrentRequests && params.concurrentRequests > FREE_TIER_QUOTAS.MAX_CONCURRENT_REQUESTS) {
233
- return {
234
- allowed: false,
235
- reason: `Concurrent limit exceeded (max ${FREE_TIER_QUOTAS.MAX_CONCURRENT_REQUESTS} active requests). Please wait for active streams to finish.`,
236
- costUnits,
237
- };
238
- }
239
-
240
- const totalUnits = params.totalUnitsUsed ?? 0;
241
- const trialStatus = this.checkTrialStatus(params.trialStartedAt, totalUnits, params.nowServerTime);
242
-
243
- if (trialStatus.status === 'expired' || trialStatus.status === 'grace_period') {
244
- return {
245
- allowed: false,
246
- reason: trialStatus.reason || 'Trial limit reached. Upgrade your plan.',
247
- costUnits,
248
- };
249
- }
250
-
251
- if (totalUnits + costUnits > FREE_TIER_QUOTAS.MAX_TRIAL_REQUEST_UNITS) {
252
- return {
253
- allowed: false,
254
- reason: `Total trial request unit limit reached (${totalUnits}/${FREE_TIER_QUOTAS.MAX_TRIAL_REQUEST_UNITS}). Upgrade your plan.`,
255
- costUnits,
256
- };
257
- }
258
-
259
- const dailyUnits = params.dailyUnitsUsed ?? 0;
260
- if (dailyUnits + costUnits > FREE_TIER_QUOTAS.MAX_DAILY_REQUEST_UNITS) {
261
- return {
262
- allowed: false,
263
- reason: `Daily request quota limit reached (${dailyUnits}/${FREE_TIER_QUOTAS.MAX_DAILY_REQUEST_UNITS} units/day). Quota resets tomorrow.`,
264
- costUnits,
265
- };
266
- }
267
-
268
- return { allowed: true, costUnits };
269
- }
270
-
271
- static checkQueryAllowed(dailyQueriesCount = 0): { allowed: boolean; reason?: string } {
272
- if (dailyQueriesCount >= FREE_TIER_QUOTAS.MAX_DAILY_REQUEST_UNITS) {
273
- return {
274
- allowed: false,
275
- reason: `Daily query limit reached (${FREE_TIER_QUOTAS.MAX_DAILY_REQUEST_UNITS}/day). Quota resets tomorrow.`,
276
- };
277
- }
278
-
279
- return { allowed: true };
280
- }
281
- }
@@ -1,79 +0,0 @@
1
- import { ILLMProvider } from '../llm/ILLMProvider';
2
- import { LLMFactory } from '../llm/LLMFactory';
3
- import { RagConfig, LLMConfig } from '../config/RagConfig';
4
-
5
- /**
6
- * Provider-aware fast model defaults.
7
- * Maps an LLM provider name to a sensible lightweight model for routing tasks.
8
- */
9
- const FAST_MODEL_DEFAULTS: Record<string, string> = {
10
- openai: 'gpt-4o-mini',
11
- gemini: 'gemini-2.0-flash',
12
- anthropic: 'claude-3-haiku-20240307',
13
- ollama: '', // Ollama has no universal lightweight default — reuse main model
14
- rest: '',
15
- universal_rest: '',
16
- custom: '',
17
- };
18
-
19
- /**
20
- * LLMRouter — manages multiple LLM instances for different task roles.
21
- *
22
- * Roles:
23
- * - 'default' : the main configured LLM (used for chat generation)
24
- * - 'fast' : a lightweight model for classification/routing tasks (UI decisions, strategy)
25
- * - 'powerful': reserved for future use (defaults to main model)
26
- *
27
- * Provider-agnostic: will never mix provider SDKs (e.g., won't set a Gemini model name on an
28
- * OpenAI client). If FAST_LLM_MODEL is set in the environment it is applied to the *same* provider.
29
- */
30
- export class LLMRouter {
31
- private models = new Map<string, ILLMProvider>();
32
-
33
- constructor(private config: RagConfig) { }
34
-
35
- /**
36
- * Initialize all LLM roles.
37
- *
38
- * @param prebuiltDefault - optional pre-built provider (from EmbeddingStrategyResolver).
39
- * When provided it is used directly as the 'default' role without re-constructing.
40
- */
41
- async initialize(prebuiltDefault?: ILLMProvider): Promise<void> {
42
- // 1. Seed 'default' — prefer the pre-built provider so we don't double-construct it.
43
- const defaultModel = prebuiltDefault ?? LLMFactory.create(this.config.llm, this.config.embedding);
44
- this.models.set('default', defaultModel);
45
-
46
- // 2. Resolve 'fast' model — always stays on the SAME provider to avoid SDK mismatches.
47
- const envFastModel = process.env.FAST_LLM_MODEL;
48
- const providerFastDefault = FAST_MODEL_DEFAULTS[this.config.llm.provider] ?? '';
49
-
50
- const fastModelName = envFastModel || providerFastDefault;
51
-
52
- if (fastModelName && fastModelName !== this.config.llm.model) {
53
- //console.log(`[LLMRouter] Fast role → provider="${this.config.llm.provider}" model="${fastModelName}"`);
54
- const fastConfig: LLMConfig = {
55
- ...this.config.llm,
56
- model: fastModelName,
57
- };
58
- this.models.set('fast', LLMFactory.create(fastConfig, this.config.embedding));
59
- } else {
60
- //console.log(`[LLMRouter] Fast role → reusing default model (no lightweight alternative configured).`);
61
- this.models.set('fast', defaultModel);
62
- }
63
-
64
- // 3. 'powerful' defaults to main model (reserved for future use)
65
- this.models.set('powerful', defaultModel);
66
- }
67
-
68
- /**
69
- * Retrieve a model provider by its task role.
70
- * Falls back to 'default' if the requested role is not registered.
71
- */
72
- get(role: 'fast' | 'powerful' | 'default'): ILLMProvider {
73
- const provider = this.models.get(role) ?? this.models.get('default');
74
- if (!provider) {
75
- throw new Error(`[LLMRouter] No provider registered for role: "${role}". Did you call initialize()?`);
76
- }
77
- return provider;
78
- }
79
- }
@@ -1,132 +0,0 @@
1
- import { Pipeline } from "./Pipeline";
2
- import { RagConfig } from "../config/RagConfig";
3
-
4
- /**
5
- * LangChainAgent — Orchestration layer that turns the RAG pipeline into an Agent.
6
- *
7
- * Uses dynamic imports to keep LangChain an optional peer dependency.
8
- * Requires `langchain` (v1.x) and `@langchain/core` to be installed.
9
- *
10
- * LangChain v1.x migration notes:
11
- * - `AgentExecutor` and `createOpenAIFunctionsAgent` are removed.
12
- * - The replacement is `createAgent` from the `langchain` root package,
13
- * which returns a `ReactAgent` (LangGraph-backed ReAct agent).
14
- * - Invocation uses `{ messages: [...] }` instead of `{ input, chat_history }`.
15
- */
16
- export class LangChainAgent {
17
- private agent?: any;
18
-
19
- constructor(private pipeline: Pipeline, private config: RagConfig) {}
20
-
21
- /**
22
- * Initializes the LangChain ReAct agent with the RAG pipeline as a tool.
23
- * Dynamically imports langchain so the package doesn't crash if it isn't installed.
24
- */
25
- async initialize(chatModel: unknown) {
26
- try {
27
- const [{ DynamicTool }, , { createAgent }] = await Promise.all([
28
- import('@langchain/core/tools'),
29
- import('@langchain/core/messages'),
30
- import('langchain'),
31
- ]);
32
-
33
- // 1. Define the RAG Search Tool
34
- const searchTool = new DynamicTool({
35
- name: 'document_search',
36
- description:
37
- 'Use this tool to search through the knowledge base and document repository. Input should be a specific search query.',
38
- func: async (query: string) => {
39
- const response = await this.pipeline.ask(query);
40
- return `Search Results:\n${response.reply}\n\nSources Used: ${JSON.stringify(
41
- response.sources.map((s) => s.id)
42
- )}`;
43
- },
44
- });
45
-
46
- // 2. Create the ReAct agent (langchain v1.x API)
47
- // - `model` replaces the old `llm` parameter
48
- // - `createAgent` replaces `createOpenAIFunctionsAgent` + `AgentExecutor`
49
- const defaultAgentPrompt = 'You are a helpful AI assistant with access to a document search tool.';
50
- let finalSystemPrompt = this.config.llm.systemPrompt || defaultAgentPrompt;
51
-
52
- const chartInstruction = `\n\nWhen presenting structured data, statistics, or comparisons, decide if it is best presented as a table or a chart.
53
- - For tables: use standard Markdown tables.
54
- - For charts (bar, line, pie): You MUST output a fenced code block with the language 'chart'.
55
- - CRITICAL RULES:
56
- 1. Use ONLY valid JSON (double quotes, no trailing commas).
57
- 2. NO math expressions (e.g., use 100, NOT 50+50).
58
- 3. dataKeys MUST exactly match the property names in the data objects.
59
- 4. data objects MUST be FLAT (no nested objects or arrays).
60
- 5. DO NOT output naked JSON or markdown tables for visualizations.
61
- 6. ALWAYS wrap your visualization data in a \`\`\`chart ... \`\`\` code block.
62
- \`\`\`chart
63
- {
64
- "type": "bar" | "line" | "pie",
65
- "xAxisKey": "name",
66
- "name": "Distribution Chart",
67
- "dataKeys": ["value"],
68
- "data": [{"name": "A", "value": 10}]
69
- }
70
- \`\`\``;
71
-
72
- if (!finalSystemPrompt.includes('chart')) {
73
- finalSystemPrompt += chartInstruction;
74
- }
75
-
76
- this.agent = createAgent({
77
- model: chatModel as any,
78
- tools: [searchTool],
79
- systemPrompt: finalSystemPrompt,
80
- });
81
- } catch (error) {
82
- const isMissing =
83
- error instanceof Error && error.message.includes('Cannot find module');
84
- const hint = isMissing
85
- ? " Make sure 'langchain' and '@langchain/core' are installed:\n npm install langchain @langchain/core"
86
- : '';
87
- throw new Error(
88
- `[LangChainAgent] Failed to initialize.${hint}\n${
89
- error instanceof Error ? error.message : String(error)
90
- }`
91
- );
92
- }
93
- }
94
-
95
- /**
96
- * Run the agentic flow.
97
- *
98
- * LangChain v1.x: invoke takes `{ messages: [...] }` instead of `{ input, chat_history }`.
99
- * The agent returns `{ messages: [...] }` — the last message is the final answer.
100
- */
101
- async run(input: string, chatHistory: unknown[] = []) {
102
- if (!this.agent) {
103
- throw new Error('[LangChainAgent] Agent not initialized. Call initialize() first.');
104
- }
105
-
106
- // Dynamically import HumanMessage to construct the message list
107
- const { HumanMessage, AIMessage } = await import('@langchain/core/messages');
108
-
109
- const historyMessages = (chatHistory as Array<{ role: string; content: string }>).map((m) =>
110
- m.role === 'user' ? new HumanMessage(m.content) : new AIMessage(m.content)
111
- );
112
-
113
- const response = await this.agent.invoke({
114
- messages: [...historyMessages, new HumanMessage(input)],
115
- });
116
-
117
- // Extract the text from the last message in the response
118
- const lastMessage = response?.messages?.at(-1);
119
- if (lastMessage && typeof lastMessage.content === 'string') {
120
- return lastMessage.content;
121
- }
122
- // Fallback for structured content arrays
123
- if (Array.isArray(lastMessage?.content)) {
124
- return lastMessage.content
125
- .filter((c: { type: string }) => c.type === 'text')
126
- .map((c: { text: string }) => c.text)
127
- .join('');
128
- }
129
-
130
- return String(response?.output ?? response ?? '');
131
- }
132
- }