@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.
Files changed (142) hide show
  1. package/README.md +22 -0
  2. package/dist/{BatchProcessor-zbQMaZHm.d.mts → BatchProcessor-BGslIR5l.d.mts} +65 -4
  3. package/dist/{BatchProcessor-D42E706a.d.ts → BatchProcessor-C3gY6kZ0.d.ts} +65 -4
  4. package/dist/handlers/index.js +668 -837
  5. package/dist/handlers/index.mjs +678 -837
  6. package/dist/index.css +5 -0
  7. package/dist/index.d.mts +4 -28
  8. package/dist/index.d.ts +4 -28
  9. package/dist/index.js +91 -43
  10. package/dist/index.min.css +1 -1
  11. package/dist/index.mjs +91 -43
  12. package/dist/server.d.mts +21 -7
  13. package/dist/server.d.ts +21 -7
  14. package/dist/server.js +731 -875
  15. package/dist/server.mjs +741 -875
  16. package/package.json +11 -4
  17. package/src/LicensedRetrivora.ts +0 -278
  18. package/src/app/api/chat/route.ts +0 -4
  19. package/src/app/api/health/route.ts +0 -5
  20. package/src/app/api/ingest/route.ts +0 -27
  21. package/src/app/api/suggestions/route.ts +0 -5
  22. package/src/app/api/upload/route.ts +0 -5
  23. package/src/app/constants.tsx +0 -270
  24. package/src/app/favicon.ico +0 -0
  25. package/src/app/globals.css +0 -449
  26. package/src/app/layout.tsx +0 -22
  27. package/src/app/page.tsx +0 -64
  28. package/src/app/types.ts +0 -30
  29. package/src/browser.tsx +0 -127
  30. package/src/components/ChatWidget.tsx +0 -393
  31. package/src/components/ChatWindow.tsx +0 -952
  32. package/src/components/CodeViewer.tsx +0 -56
  33. package/src/components/ConfigProvider.tsx +0 -141
  34. package/src/components/DocumentUpload.tsx +0 -354
  35. package/src/components/DynamicChart.tsx +0 -297
  36. package/src/components/HourglassLoader.tsx +0 -87
  37. package/src/components/MarkdownComponents.tsx +0 -186
  38. package/src/components/MessageBubble.tsx +0 -498
  39. package/src/components/ObservabilityPanel.tsx +0 -374
  40. package/src/components/ProductCard.tsx +0 -107
  41. package/src/components/ProductCarousel.tsx +0 -97
  42. package/src/components/SourceCard.tsx +0 -61
  43. package/src/components/ThemeProvider.tsx +0 -8
  44. package/src/components/ThemeToggle.tsx +0 -29
  45. package/src/components/ThinkingBlock.tsx +0 -75
  46. package/src/components/UIDispatcher.tsx +0 -342
  47. package/src/components/VisualizationRenderer.tsx +0 -621
  48. package/src/components/constants.tsx +0 -503
  49. package/src/config/ConfigBuilder.ts +0 -285
  50. package/src/config/EmbeddingStrategy.ts +0 -148
  51. package/src/config/LicenseConfigResolver.ts +0 -298
  52. package/src/config/RagConfig.ts +0 -328
  53. package/src/config/UniversalProfiles.ts +0 -82
  54. package/src/config/constants.ts +0 -64
  55. package/src/config/serverConfig.ts +0 -378
  56. package/src/config/uiConstants.ts +0 -47
  57. package/src/core/BatchProcessor.ts +0 -385
  58. package/src/core/CircuitBreaker.ts +0 -118
  59. package/src/core/ConfigFetcher.ts +0 -220
  60. package/src/core/ConfigResolver.ts +0 -165
  61. package/src/core/ConfigValidator.ts +0 -197
  62. package/src/core/DatabaseStorage.ts +0 -526
  63. package/src/core/FreeTierLimitsGuard.ts +0 -281
  64. package/src/core/LLMRouter.ts +0 -79
  65. package/src/core/LangChainAgent.ts +0 -132
  66. package/src/core/LicenseValidator.ts +0 -284
  67. package/src/core/LicenseVerifier.ts +0 -261
  68. package/src/core/MultiAgentCoordinator.ts +0 -239
  69. package/src/core/Pipeline.ts +0 -1279
  70. package/src/core/PluginManager.ts +0 -277
  71. package/src/core/ProjectSettings.ts +0 -247
  72. package/src/core/ProviderHealthCheck.ts +0 -133
  73. package/src/core/ProviderInterfaces.ts +0 -37
  74. package/src/core/ProviderRegistry.ts +0 -156
  75. package/src/core/QueryProcessor.ts +0 -399
  76. package/src/core/Retrivora.ts +0 -98
  77. package/src/core/VectorPlugin.ts +0 -193
  78. package/src/core/mcp.ts +0 -261
  79. package/src/exceptions/index.ts +0 -131
  80. package/src/handlers/index.ts +0 -1625
  81. package/src/hooks/useHydrated.ts +0 -13
  82. package/src/hooks/useProjectSettings.ts +0 -105
  83. package/src/hooks/useRagChat.ts +0 -436
  84. package/src/hooks/useStoredMessages.ts +0 -64
  85. package/src/index.css +0 -4269
  86. package/src/index.ts +0 -137
  87. package/src/lib/plugin.ts +0 -24
  88. package/src/llm/ILLMProvider.ts +0 -60
  89. package/src/llm/LLMFactory.ts +0 -141
  90. package/src/llm/providers/AnthropicProvider.ts +0 -215
  91. package/src/llm/providers/GeminiProvider.ts +0 -311
  92. package/src/llm/providers/GroqProvider.ts +0 -184
  93. package/src/llm/providers/OllamaProvider.ts +0 -232
  94. package/src/llm/providers/OpenAIProvider.ts +0 -188
  95. package/src/llm/providers/QwenProvider.ts +0 -199
  96. package/src/llm/providers/UniversalLLMAdapter.ts +0 -422
  97. package/src/llm/utils.ts +0 -46
  98. package/src/providers/graphdb/BaseGraphProvider.ts +0 -43
  99. package/src/providers/graphdb/SimpleGraphProvider.ts +0 -66
  100. package/src/providers/vectordb/BaseVectorProvider.ts +0 -98
  101. package/src/providers/vectordb/ChromaDBProvider.ts +0 -130
  102. package/src/providers/vectordb/MilvusProvider.ts +0 -134
  103. package/src/providers/vectordb/MongoDBProvider.ts +0 -250
  104. package/src/providers/vectordb/MultiTablePostgresProvider.ts +0 -461
  105. package/src/providers/vectordb/PineconeProvider.ts +0 -295
  106. package/src/providers/vectordb/PostgreSQLProvider.ts +0 -227
  107. package/src/providers/vectordb/QdrantProvider.ts +0 -294
  108. package/src/providers/vectordb/RedisProvider.ts +0 -105
  109. package/src/providers/vectordb/UniversalVectorProvider.ts +0 -221
  110. package/src/providers/vectordb/WeaviateProvider.ts +0 -172
  111. package/src/rag/DocumentChunker.ts +0 -144
  112. package/src/rag/EntityExtractor.ts +0 -90
  113. package/src/rag/LlamaIndexIngestor.ts +0 -60
  114. package/src/rag/Reranker.ts +0 -106
  115. package/src/rendering/IntentClassifier.ts +0 -167
  116. package/src/rendering/RendererRegistry.ts +0 -164
  117. package/src/rendering/RuleEngine.ts +0 -67
  118. package/src/rendering/VisualizationDecisionEngine.ts +0 -164
  119. package/src/rendering/__tests__/VisualizationDecisionEngine.test.ts +0 -237
  120. package/src/rendering/index.ts +0 -12
  121. package/src/rendering/rules/Rule1SpecificInfoRule.ts +0 -28
  122. package/src/rendering/rules/Rule2ComparisonRule.ts +0 -24
  123. package/src/rendering/rules/Rule3ProductDiscoveryRule.ts +0 -28
  124. package/src/rendering/rules/Rule4AnalyticalRule.ts +0 -38
  125. package/src/rendering/rules/Rule5MixedResponseRule.ts +0 -29
  126. package/src/rendering/rules/Rule6SmallResultSetRule.ts +0 -24
  127. package/src/rendering/rules/Rule7LargeTableRule.ts +0 -35
  128. package/src/rendering/types.ts +0 -96
  129. package/src/server.ts +0 -156
  130. package/src/tailwind.css +0 -194
  131. package/src/telemetry/TelemetryService.ts +0 -330
  132. package/src/types/chat.ts +0 -89
  133. package/src/types/index.ts +0 -278
  134. package/src/types/pdf-parse.d.ts +0 -13
  135. package/src/types/props.ts +0 -169
  136. package/src/utils/DocumentParser.ts +0 -111
  137. package/src/utils/ProductExtractor.ts +0 -350
  138. package/src/utils/SchemaMapper.ts +0 -176
  139. package/src/utils/UITransformer.ts +0 -1812
  140. package/src/utils/synonyms.ts +0 -80
  141. package/src/utils/templateUtils.ts +0 -78
  142. package/src/version.ts +0 -6
@@ -1,1625 +0,0 @@
1
- import { NextRequest, NextResponse } from 'next/server';
2
- import { VectorPlugin } from '../core/VectorPlugin';
3
- import { RagConfig } from '../config/RagConfig';
4
- import { ChatMessage, ChatResponse } from '../types';
5
- import { DocumentParser } from '../utils/DocumentParser';
6
- import { UITransformer } from '../utils/UITransformer';
7
- import { DatabaseStorage, HistoryMessage } from '../core/DatabaseStorage';
8
- import { LicenseVerifier, LicensePayload } from '../core/LicenseVerifier';
9
- import { SDK_VERSION } from '../version';
10
- import { FreeTierLimitsGuard, FREE_TIER_QUOTAS } from '../core/FreeTierLimitsGuard';
11
-
12
- /**
13
- * Shared options accepted by every handler factory.
14
- *
15
- * Pass `onAuthorize` to plug in per-request auth rules; the callback is
16
- * called before rate-limiting, license enforcement, or any business logic
17
- * runs. Return `true` or the resolved promise `true` to allow, `false`
18
- * to get a 401, or return a `Response` to fully control the denial (for
19
- * example redirecting an OAuth-style flow to a login page).
20
- */
21
- export interface HandlerOptions {
22
- /** Optional synchronous or asynchronous authorization gate. */
23
- onAuthorize?: (req: NextRequest) => boolean | Response | Promise<boolean | Response>;
24
- /**
25
- * Optional scope resolver for multi-tenant deployments.
26
- *
27
- * Returns an allow-list of project ids that the caller is authorized to
28
- * access on the `sessions`, `history`, and `feedback` endpoints. When
29
- * the callback returns a non-empty array, server-side filtering is
30
- * applied to every `listSessions`, `getHistory`, `clearHistory`,
31
- * `listFeedback`, and `getFeedback` call so the caller can never leak
32
- * another tenant's data even when client-side filters are tampered with.
33
- */
34
- onResolveScope?: (req: NextRequest) => string[] | undefined | Promise<string[] | undefined>;
35
- }
36
-
37
- async function checkAuth(
38
- req: NextRequest,
39
- onAuthorize?: (req: NextRequest) => boolean | Response | Promise<boolean | Response>
40
- ): Promise<Response | null> {
41
- if (!onAuthorize) return null;
42
- try {
43
- const res = await onAuthorize(req);
44
- if (res === false) {
45
- return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
46
- }
47
- if (res instanceof Response) {
48
- return res;
49
- }
50
- return null;
51
- } catch (err) {
52
- const msg = err instanceof Error ? err.message : 'Authorization check failed';
53
- return NextResponse.json({ error: msg }, { status: 401 });
54
- }
55
- }
56
-
57
-
58
- // ─── Rate Limiter ─────────────────────────────────────────────────────────────
59
-
60
- /**
61
- * In-memory sliding-window rate limiter.
62
- * Keyed by IP + licenseKey. Rejects requests that exceed the limit.
63
- * Automatically prunes expired entries to avoid memory growth.
64
- */
65
- class RateLimiter {
66
- private readonly hits = new Map<string, number[]>();
67
- private readonly windowMs: number;
68
- private readonly max: number;
69
-
70
- constructor(windowMs = 60_000, max = 30) {
71
- this.windowMs = windowMs;
72
- this.max = max;
73
- }
74
-
75
- /** Returns true if the request should be allowed, false if rate-limited. */
76
- allow(key: string): boolean {
77
- const now = Date.now();
78
- const cutoff = now - this.windowMs;
79
- const existing = (this.hits.get(key) || []).filter(t => t > cutoff);
80
- existing.push(now);
81
- this.hits.set(key, existing);
82
- // Periodically prune stale entries to avoid memory leak
83
- if (this.hits.size > 5000) {
84
- for (const [k, timestamps] of this.hits.entries()) {
85
- if (timestamps.every(t => t <= cutoff)) this.hits.delete(k);
86
- }
87
- }
88
- return existing.length <= this.max;
89
- }
90
-
91
- retryAfterSec(key: string): number {
92
- const now = Date.now();
93
- const cutoff = now - this.windowMs;
94
- const existing = (this.hits.get(key) || []).filter(t => t > cutoff);
95
- if (existing.length === 0) return 0;
96
- return Math.ceil((existing[0] + this.windowMs - now) / 1000);
97
- }
98
- }
99
-
100
- // Global singleton rate limiter: 30 requests per 60s per IP
101
- const _g = global as any;
102
- const rateLimiter: RateLimiter = _g.__retrivoraRateLimiter ??
103
- (_g.__retrivoraRateLimiter = new RateLimiter(60_000, 30));
104
-
105
- /** Derive a rate-limit key from request headers. */
106
- function getRateLimitKey(req: NextRequest): string {
107
- const ip = req.headers.get('x-forwarded-for')?.split(',')[0]?.trim()
108
- || req.headers.get('x-real-ip')
109
- || '127.0.0.1';
110
- return `ip:${ip}`;
111
- }
112
-
113
- /** Check rate limit and return a 429 response if exceeded, else null. */
114
- function checkRateLimit(req: NextRequest): Response | null {
115
- const key = getRateLimitKey(req);
116
- if (!rateLimiter.allow(key)) {
117
- const retryAfter = rateLimiter.retryAfterSec(key);
118
- return new Response(
119
- JSON.stringify({ error: { code: 'RATE_LIMITED', message: 'Too many requests. Please slow down.' } }),
120
- {
121
- status: 429,
122
- headers: {
123
- 'Content-Type': 'application/json',
124
- 'Retry-After': String(retryAfter),
125
- 'X-RateLimit-Limit': '30',
126
- 'X-RateLimit-Window': '60',
127
- },
128
- }
129
- );
130
- }
131
- return null;
132
- }
133
-
134
- // ─── Free Tier Limits Guard ───────────────────────────────────────────────────
135
-
136
- const FREE_TIER_NAMES = new Set(['hobby', 'free', 'free_trial', 'trial']);
137
-
138
- function isFreeTier(tier?: string | null): boolean {
139
- if (!tier) return true;
140
- return FREE_TIER_NAMES.has(tier.toLowerCase().trim());
141
- }
142
-
143
- const _freeTierGuardInstance: any = _g.__retrivoraFreeTierGuard ??
144
- (_g.__retrivoraFreeTierGuard = new (FreeTierLimitsGuard as any)());
145
-
146
- function createPaymentRequiredResponse(reason: string, details?: Record<string, unknown>): Response {
147
- const body = {
148
- type: 'https://retrivora.com/docs/errors/payment-required',
149
- title: 'Payment Required',
150
- status: 402,
151
- detail: reason,
152
- code: 'FREE_TIER_LIMIT_EXCEEDED',
153
- instance: undefined as string | undefined,
154
- ...(details ?? {}),
155
- quota: {
156
- maxDailyRequestUnits: FREE_TIER_QUOTAS.MAX_DAILY_REQUEST_UNITS,
157
- maxTrialRequestUnits: FREE_TIER_QUOTAS.MAX_TRIAL_REQUEST_UNITS,
158
- maxDocuments: FREE_TIER_QUOTAS.MAX_DOCUMENTS,
159
- maxStorageBytes: FREE_TIER_QUOTAS.MAX_STORAGE_BYTES,
160
- upgradeUrl: 'https://retrivora.com/pricing',
161
- },
162
- };
163
- return new Response(JSON.stringify(body), {
164
- status: 402,
165
- headers: {
166
- 'Content-Type': 'application/problem+json',
167
- },
168
- });
169
- }
170
-
171
- // ─── License Enforcement ──────────────────────────────────────────────────────
172
-
173
- /**
174
- * Resolve the license key from a request, trying (in priority order):
175
- * 1. `x-license-key` header
176
- * 2. Body property `licenseKey` (already-parsed body, optional)
177
- * 3. `Authorization: Bearer <key>` header
178
- * 4. SDK/plugin config and environment variables
179
- */
180
- function resolveLicenseKey(
181
- req: NextRequest,
182
- config: RagConfig,
183
- bodyLicenseKey?: string
184
- ): string {
185
- return (
186
- req.headers.get('x-license-key') ||
187
- bodyLicenseKey ||
188
- req.headers.get('authorization')?.replace(/^Bearer\s+/i, '') ||
189
- config.licenseKey ||
190
- process.env.NEXT_PUBLIC_RETRIVORA_LICENSE_KEY ||
191
- process.env.RETRIVORA_LICENSE_KEY ||
192
- process.env.RAG_LICENSE_KEY ||
193
- ''
194
- );
195
- }
196
-
197
- /**
198
- * Enforce a valid Retrivora license key.
199
- *
200
- * - Extracts license key from headers / body / env / config
201
- * - Performs cryptographic + expiration validation via `LicenseVerifier.verify`
202
- * - Returns `null` when validation passes (contains the decoded payload for future tier checks)
203
- * - Returns a 403 Response ready to be sent when validation fails
204
- *
205
- * In development (`NODE_ENV !== 'production'`) a missing key still produces a
206
- * console warning but passes (fail-open local-dev behavior); in production any
207
- * invalid / expired / missing key produces a hard 403.
208
- */
209
- async function enforceLicense(
210
- req: NextRequest,
211
- config: RagConfig,
212
- bodyLicenseKey?: string
213
- ): Promise<{ ok: true; payload: LicensePayload } | { ok: false; response: Response }> {
214
- const rawKey = resolveLicenseKey(req, config, bodyLicenseKey);
215
- const licenseKey = (rawKey || '').trim().replace(/^["']|["']$/g, '').trim();
216
- const projectId = config.projectId || '';
217
-
218
- try {
219
- // Always use the async verifier which hits the Retrivora backend API.
220
- let payload;
221
- try {
222
- payload = await LicenseVerifier.verifyAsync(licenseKey, projectId);
223
- } catch (err: any) {
224
- return {
225
- ok: false,
226
- response: NextResponse.json(
227
- { error: `License validation failed: ${err.message}` },
228
- { status: 401 }
229
- )
230
- };
231
- }
232
- return { ok: true, payload };
233
- } catch (err: any) {
234
- const message =
235
- err instanceof Error
236
- ? err.message
237
- : 'Missing or invalid RETRIVORA_LICENSE_KEY. Set NEXT_PUBLIC_RETRIVORA_LICENSE_KEY in your environment or contact your administrator.';
238
- const body = {
239
- error: {
240
- code: 'LICENSE_REVOKED',
241
- message: 'Your Retrivora license key has been terminated, suspended, or revoked. Access denied.',
242
- },
243
- details: message,
244
- };
245
- return {
246
- ok: false,
247
- response: new Response(JSON.stringify(body), {
248
- status: 403,
249
- headers: { 'Content-Type': 'application/json' },
250
- }),
251
- };
252
- }
253
- }
254
-
255
- function getAllowedNamespaces(projectId: string): Set<string> {
256
- const trimmed = projectId.trim();
257
- const withoutPrefix = trimmed.startsWith('retrivora-')
258
- ? trimmed.replace(/^retrivora-/, '')
259
- : trimmed;
260
-
261
- return new Set([
262
- trimmed,
263
- withoutPrefix,
264
- `retrivora-${withoutPrefix}`,
265
- ].filter(Boolean));
266
- }
267
-
268
- function resolveLicensedNamespace(
269
- payload: LicensePayload,
270
- requestedNamespace?: string
271
- ): { ok: true; namespace: string } | { ok: false; response: Response } {
272
- const allowed = getAllowedNamespaces(payload.projectId);
273
- const requested = requestedNamespace?.trim();
274
-
275
- if (requested && !allowed.has(requested)) {
276
- return {
277
- ok: false,
278
- response: new Response(JSON.stringify({
279
- error: {
280
- code: 'NAMESPACE_FORBIDDEN',
281
- message: 'Requested namespace is not authorized for this license.',
282
- },
283
- }), {
284
- status: 403,
285
- headers: { 'Content-Type': 'application/json' },
286
- }),
287
- };
288
- }
289
-
290
- const defaultNamespace = payload.projectId.startsWith('retrivora-')
291
- ? payload.projectId
292
- : `retrivora-${payload.projectId}`;
293
-
294
- return { ok: true, namespace: requested || defaultNamespace };
295
- }
296
-
297
- // ─── Input Sanitization ───────────────────────────────────────────────────────
298
-
299
- const MAX_MESSAGE_LENGTH = 8000;
300
-
301
- /**
302
- * Sanitize a chat message:
303
- * - Strip Unicode control characters (except standard whitespace)
304
- * - Enforce max character length
305
- */
306
- function sanitizeInput(raw: string): { ok: true; value: string } | { ok: false; error: string } {
307
- // Strip control chars (U+0000–U+001F except tab/newline/CR, and U+007F–U+009F)
308
- const stripped = raw.replace(/[\u0000-\u0008\u000B\u000C\u000E-\u001F\u007F-\u009F]/g, '');
309
- const trimmed = stripped.trim();
310
- if (!trimmed) {
311
- return { ok: false, error: 'message must not be empty' };
312
- }
313
- if (trimmed.length > MAX_MESSAGE_LENGTH) {
314
- return { ok: false, error: `message must be ≤ ${MAX_MESSAGE_LENGTH} characters` };
315
- }
316
- return { ok: true, value: trimmed };
317
- }
318
-
319
- // ─── Plugin Singleton ─────────────────────────────────────────────────────────
320
-
321
- /**
322
- * Get or create a module-level VectorPlugin singleton keyed by a stable hash of the config.
323
- * Prevents unnecessary DB connection pool recreation on hot reloads and serverless cold starts.
324
- */
325
- function getOrCreatePlugin(configOrPlugin: Partial<RagConfig> | VectorPlugin | undefined): VectorPlugin {
326
- if (configOrPlugin instanceof VectorPlugin) return configOrPlugin;
327
- // Use a global to survive Next.js hot reloads in dev
328
- const cacheKey = '__retrivoraPlugin_' + JSON.stringify(configOrPlugin ?? {}).slice(0, 64);
329
- if (!_g[cacheKey]) {
330
- _g[cacheKey] = new VectorPlugin(configOrPlugin);
331
- }
332
- return _g[cacheKey];
333
- }
334
-
335
- // ─── Telemetry Reporter Helper ───────────────────────────────────────────────
336
-
337
- /**
338
- * Asynchronously sends telemetry payload to telemetryUrl when telemetry is enabled
339
- * or when a licenseKey is set. Non-blocking.
340
- */
341
- function reportTelemetry(
342
- req: NextRequest,
343
- plugin: VectorPlugin,
344
- action: string,
345
- status: 'success' | 'error',
346
- details: string,
347
- trace?: any
348
- ) {
349
- try {
350
- const config = plugin.getConfig();
351
- const licenseKey = config.licenseKey || process.env.RAG_LICENSE_KEY || process.env.RETRIVORA_LICENSE_KEY || process.env.NEXT_PUBLIC_RETRIVORA_LICENSE_KEY;
352
- const telemetryConfig = config.telemetry;
353
-
354
- const host = req.headers.get('host') || 'localhost';
355
- const defaultUrl = 'https://www.retrivora.com/api/telemetry';
356
-
357
- const telemetryUrl = telemetryConfig?.url || process.env.TELEMETRY_URL || process.env.NEXT_PUBLIC_TELEMETRY_URL || defaultUrl;
358
- const userAgent = req.headers.get('user-agent') || `Retrivora-SDK/${SDK_VERSION}`;
359
-
360
- let absoluteUrl = telemetryUrl;
361
- if (!telemetryUrl.startsWith('http')) {
362
- const proto = req.headers.get('x-forwarded-proto') || 'http';
363
- absoluteUrl = `${proto}://${host}${telemetryUrl}`;
364
- }
365
-
366
- const projectId = config.projectId || 'default';
367
- const model = trace?.model || config.llm?.model || config.embedding?.model || 'llama-3.1-8b-instant';
368
- const provider = trace?.provider || config.llm?.provider || config.embedding?.provider || 'groq';
369
- const tokens = Number(trace?.tokens?.totalTokens || trace?.totalTokens || 0);
370
- const costUsd = Number(trace?.tokens?.estimatedCostUsd || trace?.costUsd || 0);
371
- const latencyMs = Number(trace?.latency?.totalMs || trace?.latencyMs || 0);
372
-
373
- const payload = {
374
- trace,
375
- licenseKey,
376
- projectId,
377
- organization: process.env.RETRIVORA_ORGANIZATION || 'default-org',
378
- sdkVersion: SDK_VERSION,
379
- model,
380
- provider,
381
- tokens,
382
- cost: costUsd,
383
- costUsd,
384
- latencyMs,
385
- status,
386
- action,
387
- feature: action,
388
- userAgent,
389
- timestamp: new Date().toISOString(),
390
- details,
391
- };
392
-
393
- fetch(absoluteUrl, {
394
- method: 'POST',
395
- headers: {
396
- 'Content-Type': 'application/json',
397
- 'x-forwarded-for': req.headers.get('x-forwarded-for') || '',
398
- 'x-real-ip': req.headers.get('x-real-ip') || '',
399
- 'user-agent': userAgent,
400
- },
401
- body: JSON.stringify(payload),
402
- }).catch(err => {
403
- console.warn('[Retrivora SDK Telemetry] Async report warning:', err.message);
404
- });
405
- } catch {
406
- /* non-blocking */
407
- }
408
- }
409
-
410
- // ─── SSE helpers ──────────────────────────────────────────────────────────────
411
-
412
- /**
413
- * Encode a payload as a Server-Sent Events frame.
414
- * data: <json>\n\n
415
- */
416
- function sseFrame(payload: unknown): string {
417
- return `data: ${JSON.stringify(payload)}\n\n`;
418
- }
419
-
420
- /** Encode a plain text chunk as an SSE frame. */
421
- function sseTextFrame(text: string): string {
422
- return `data: ${JSON.stringify({ type: 'text', text })}\n\n`;
423
- }
424
-
425
- /** Encode the retrieval metadata with UI transformation as an SSE frame. */
426
- function sseMetaFrame(meta: unknown): string {
427
- return `data: ${JSON.stringify({ type: 'metadata', ...((meta as object) ?? {}) })}\n\n`;
428
- }
429
-
430
- /** Encode the UI transformation result as an SSE frame. */
431
- function sseUIFrame(uiTransformation: unknown): string {
432
- return `data: ${JSON.stringify({ type: 'ui_transformation', data: uiTransformation })}\n\n`;
433
- }
434
-
435
- /** Encode the observability trace as an SSE frame. */
436
- function sseObservabilityFrame(trace: unknown): string {
437
- return `data: ${JSON.stringify({ type: 'observability', data: trace })}\n\n`;
438
- }
439
-
440
- /** Encode a stream error as an SSE frame. */
441
- function sseErrorFrame(message: string): string {
442
- return `data: ${JSON.stringify({ type: 'error', error: message })}\n\n`;
443
- }
444
-
445
- /** SSE response headers. */
446
- const SSE_HEADERS: Record<string, string> = {
447
- 'Content-Type': 'text/event-stream; charset=utf-8',
448
- 'Cache-Control': 'no-cache, no-transform',
449
- Connection: 'keep-alive',
450
- 'X-Accel-Buffering': 'no', // Disable Nginx buffering for streaming
451
- };
452
-
453
-
454
-
455
- // ─── Handler Factories ────────────────────────────────────────────────────────
456
-
457
- /**
458
- * Build a Next.js App Router POST handler for **synchronous** RAG chat.
459
- *
460
- * Pipeline (in execution order):
461
- * 1. `options.onAuthorize` — custom authorization gate (optional)
462
- * 2. In-memory IP rate limiter (30 req / 60 s sliding window)
463
- * 3. JSON body parse → `enforceLicense()` RSA signature validation
464
- * 4. Input sanitization + length / toxicity / PII / prompt-injection heuristics
465
- * 5. User-message persistence via `DatabaseStorage`
466
- * 6. LLM-driven RAG retrieval via `plugin.chat()`
467
- * 7. Observability UI transformation + response persistence
468
- * 8. JSON response (`ChatResponse` shape)
469
- *
470
- * Use this handler when you don't need streaming (e.g. server-side SDKs).
471
- * For streaming use `createStreamHandler` — the same pipeline but with
472
- * Server-Sent Events for incremental tokens.
473
- *
474
- * @param configOrPlugin - Either a partial `RagConfig` (a plugin will be
475
- * created or reused for you) or an already-constructed
476
- * `VectorPlugin` instance.
477
- * @param options - See `HandlerOptions`. `onAuthorize` is recommended
478
- * for multi-tenant deployments to gate access.
479
- * @returns An async `POST(req, ctx)` function suitable for being the default
480
- * export of a Next.js route file.
481
- */
482
- export function createChatHandler(
483
- configOrPlugin?: Partial<RagConfig> | VectorPlugin,
484
- options?: HandlerOptions
485
- ) {
486
- const plugin = getOrCreatePlugin(configOrPlugin);
487
- const storage = new DatabaseStorage(plugin.getConfig());
488
- const onAuthorize = options?.onAuthorize;
489
-
490
- return async function POST(req: any, context?: any) {
491
- const authResult = await checkAuth(req, onAuthorize);
492
- if (authResult) return authResult as any;
493
-
494
- // 1. Rate limit check
495
- const rateLimited = checkRateLimit(req);
496
- if (rateLimited) return rateLimited;
497
-
498
- const config = plugin.getConfig();
499
-
500
- try {
501
- const body = await req.json();
502
- const bodyObj = (body ?? {}) as any;
503
-
504
- // 2. License enforcement (after body parse so we can read body.licenseKey)
505
- const licenseCheck = await enforceLicense(req as NextRequest, config, bodyObj?.licenseKey);
506
- if (!licenseCheck.ok) return licenseCheck.response as any;
507
-
508
- // 2b. Free Tier limits check (hobby / free / free_trial tiers)
509
- if (isFreeTier(licenseCheck.payload.tier)) {
510
- const estimatedInputTokens = Math.ceil(
511
- ((bodyObj.message?.length || 0) +
512
- (Array.isArray(bodyObj.messages)
513
- ? bodyObj.messages.reduce((a: number, m: any) => a + (m?.content?.length || m?.text?.length || 0), 0)
514
- : 0) +
515
- (bodyObj.history?.reduce((a: number, m: any) => a + (m?.content?.length || m?.text?.length || 0), 0) || 0)) / 4
516
- );
517
- const chatCheck = (FreeTierLimitsGuard as any).checkRequestAllowed({
518
- operationType: 'chat',
519
- inputTokens: estimatedInputTokens,
520
- projectId: licenseCheck.payload.projectId,
521
- });
522
- if (!chatCheck.allowed) {
523
- return createPaymentRequiredResponse(
524
- chatCheck.reason || 'Free tier quota exceeded.',
525
- { tier: licenseCheck.payload.tier, projectId: licenseCheck.payload.projectId }
526
- );
527
- }
528
- }
529
-
530
- let rawMessage = bodyObj.message;
531
- if (!rawMessage && Array.isArray(bodyObj.messages) && bodyObj.messages.length > 0) {
532
- const lastMsg = bodyObj.messages[bodyObj.messages.length - 1];
533
- rawMessage = typeof lastMsg === 'string' ? lastMsg : (lastMsg?.content || lastMsg?.text);
534
- }
535
- if (!rawMessage && typeof bodyObj.prompt === 'string') {
536
- rawMessage = bodyObj.prompt;
537
- }
538
- const history = bodyObj.history || (Array.isArray(bodyObj.messages) ? bodyObj.messages.slice(0, -1) : []);
539
- const { namespace, sessionId = 'default', messageId, userMessageId } = bodyObj;
540
- const namespaceCheck = resolveLicensedNamespace(licenseCheck.payload, namespace);
541
- if (!namespaceCheck.ok) return namespaceCheck.response as any;
542
-
543
- // 3. Input sanitization
544
- const sanitized = sanitizeInput(rawMessage ?? '');
545
- if (!sanitized.ok) {
546
- return NextResponse.json({ error: { code: 'INVALID_INPUT', message: sanitized.error } }, { status: 400 });
547
- }
548
- const message = sanitized.value;
549
-
550
- // Save user message to history
551
- const userMsgId = userMessageId || `user_${Date.now()}_${Math.random().toString(36).slice(2, 6)}`;
552
- await storage.saveMessage(sessionId, {
553
- id: userMsgId,
554
- role: 'user',
555
- content: message,
556
- }).catch(err => console.warn('[createChatHandler] Failed to save user message:', err));
557
-
558
- const result = await plugin.chat(message, history, namespaceCheck.namespace);
559
-
560
- // Save assistant message to history
561
- const assistantMsgId = messageId || `assistant_${Date.now()}_${Math.random().toString(36).slice(2, 6)}`;
562
- await storage.saveMessage(sessionId, {
563
- id: assistantMsgId,
564
- role: 'assistant',
565
- content: result.reply,
566
- sources: result.sources,
567
- uiTransformation: result.ui_transformation,
568
- trace: result.trace,
569
- }).catch(err => console.warn('[createChatHandler] Failed to save assistant message:', err));
570
-
571
- reportTelemetry(req, plugin, 'QUERY_GENERATION', 'success', `Tokens: ${result.trace?.tokens?.totalTokens || 0}, Latency: ${result.trace?.latency?.totalMs || 0}ms`, result.trace);
572
-
573
- return NextResponse.json({
574
- ...result,
575
- messageId: assistantMsgId,
576
- userMessageId: userMsgId,
577
- });
578
- } catch (err) {
579
- const message = err instanceof Error ? err.message : 'Internal server error';
580
- reportTelemetry(req, plugin, 'QUERY_GENERATION', 'error', message);
581
- return NextResponse.json({ error: message }, { status: 500 });
582
- }
583
- };
584
- }
585
-
586
- /**
587
- * Build a Next.js App Router POST handler for **streaming** RAG chat via SSE.
588
- *
589
- * Same pipeline as `createChatHandler`, but the `plugin.chat()` call is run
590
- * in streaming mode and every token is immediately emitted as a Server-Sent
591
- * Event frame. The stream contains frames for `text`, `thinking`,
592
- * `metadata`, `ui_transformation`, `observability`, and a terminal `done`.
593
- *
594
- * Multipart upload short-circuit: When the request `Content-Type` is
595
- * `multipart/form-data` the request is transparently delegated to
596
- * `createUploadHandler` (which also runs its own `enforceLicense()` gate).
597
- *
598
- * Pipeline (in execution order):
599
- * 1. `options.onAuthorize` — custom authorization gate (optional)
600
- * 2. In-memory IP rate limiter (30 req / 60 s sliding window)
601
- * 3. JSON body parse (or form-data upload delegation)
602
- * 4. `enforceLicense()` RSA signature validation — 403 if missing / terminated
603
- * 5. Input sanitization + length / toxicity / PII / prompt-injection heuristics
604
- * 6. User-message persistence via `DatabaseStorage`
605
- * 7. Streaming RAG retrieval via `plugin.chat({ stream: true })`
606
- * 8. Persist assistant message + emit SSE frames
607
- *
608
- * @param configOrPlugin - Partial `RagConfig` or an existing `VectorPlugin`.
609
- * @param options - See `HandlerOptions`.
610
- * @returns Async `POST(req, ctx)` function for `app/.../route.ts`.
611
- */
612
- export function createStreamHandler(
613
- configOrPlugin?: Partial<RagConfig> | VectorPlugin,
614
- options?: HandlerOptions
615
- ) {
616
- const plugin = getOrCreatePlugin(configOrPlugin);
617
- const storage = new DatabaseStorage(plugin.getConfig());
618
- const onAuthorize = options?.onAuthorize;
619
-
620
- return async function POST(req: any, context?: any) {
621
- const authResult = await checkAuth(req, onAuthorize);
622
- if (authResult) return authResult as any;
623
-
624
- // 1. Rate limit check
625
- const rateLimited = checkRateLimit(req);
626
- if (rateLimited) return rateLimited;
627
-
628
- const config = plugin.getConfig();
629
-
630
- let body: {
631
- message?: string;
632
- history?: ChatMessage[];
633
- namespace?: string;
634
- sessionId?: string;
635
- messageId?: string;
636
- userMessageId?: string;
637
- licenseKey?: string;
638
- };
639
- try {
640
- if (req.headers.get('content-type')?.includes('multipart/form-data')) {
641
- // Uploads bypass the JSON body parse — createUploadHandler enforces its own license check
642
- const uploader = createUploadHandler(plugin, { onAuthorize });
643
- return uploader(req);
644
- }
645
- body = await req.json();
646
- } catch {
647
- return new Response(JSON.stringify({ error: { code: 'INVALID_JSON', message: 'Invalid JSON body' } }), {
648
- status: 400,
649
- headers: { 'Content-Type': 'application/json' },
650
- });
651
- }
652
-
653
- const bodyObj = (body ?? {}) as any;
654
-
655
- // 2b. License enforcement (for JSON-body requests)
656
- const licenseCheck = await enforceLicense(req as NextRequest, config, bodyObj?.licenseKey);
657
- if (!licenseCheck.ok) return licenseCheck.response as any;
658
-
659
- // 2c. Free Tier limits check (hobby / free / free_trial tiers)
660
- if (isFreeTier(licenseCheck.payload.tier)) {
661
- const estimatedInputTokens = Math.ceil(
662
- ((bodyObj.message?.length || 0) +
663
- (Array.isArray(bodyObj.messages)
664
- ? bodyObj.messages.reduce((a: number, m: any) => a + (m?.content?.length || m?.text?.length || 0), 0)
665
- : 0) +
666
- (bodyObj.history?.reduce((a: number, m: any) => a + (m?.content?.length || m?.text?.length || 0), 0) || 0)) / 4
667
- );
668
- const chatCheck = (FreeTierLimitsGuard as any).checkRequestAllowed({
669
- operationType: 'chat_stream',
670
- inputTokens: estimatedInputTokens,
671
- projectId: licenseCheck.payload.projectId,
672
- });
673
- if (!chatCheck.allowed) {
674
- return createPaymentRequiredResponse(
675
- chatCheck.reason || 'Free tier quota exceeded.',
676
- { tier: licenseCheck.payload.tier, projectId: licenseCheck.payload.projectId }
677
- );
678
- }
679
- }
680
-
681
- let rawMessage = bodyObj.message;
682
- if (!rawMessage && Array.isArray(bodyObj.messages) && bodyObj.messages.length > 0) {
683
- const lastMsg = bodyObj.messages[bodyObj.messages.length - 1];
684
- rawMessage = typeof lastMsg === 'string' ? lastMsg : (lastMsg?.content || lastMsg?.text);
685
- }
686
- if (!rawMessage && typeof bodyObj.prompt === 'string') {
687
- rawMessage = bodyObj.prompt;
688
- }
689
- const history = bodyObj.history || (Array.isArray(bodyObj.messages) ? bodyObj.messages.slice(0, -1) : []);
690
- const { namespace, sessionId = 'default', messageId, userMessageId } = bodyObj;
691
- const namespaceCheck = resolveLicensedNamespace(licenseCheck.payload, namespace);
692
- if (!namespaceCheck.ok) return namespaceCheck.response as any;
693
-
694
- // 3. Input sanitization
695
- const sanitized = sanitizeInput(rawMessage ?? '');
696
- if (!sanitized.ok) {
697
- return new Response(
698
- JSON.stringify({ error: { code: 'INVALID_INPUT', message: sanitized.error } }),
699
- { status: 400, headers: { 'Content-Type': 'application/json' } }
700
- );
701
- }
702
- const message = sanitized.value;
703
-
704
- // Save user message to history
705
- const userMsgId = userMessageId || `user_${Date.now()}_${Math.random().toString(36).slice(2, 6)}`;
706
- await storage.saveMessage(sessionId, {
707
- id: userMsgId,
708
- role: 'user',
709
- content: message,
710
- }).catch(err => console.warn('[createStreamHandler] Failed to save user message:', err));
711
-
712
- const assistantMsgId = messageId || `assistant_${Date.now()}_${Math.random().toString(36).slice(2, 6)}`;
713
-
714
- const encoder = new TextEncoder();
715
- let isActive = true;
716
-
717
- const stream = new ReadableStream({
718
- async start(controller) {
719
- const enqueue = (text: string) => {
720
- if (!isActive) return;
721
- try {
722
- controller.enqueue(encoder.encode(text));
723
- } catch (err) {
724
- console.warn('[createStreamHandler] Failed to enqueue (stream already closed):', err);
725
- }
726
- };
727
-
728
- try {
729
- const pipelineStream = plugin.chatStream(message, history, namespaceCheck.namespace);
730
- let fullReply = '';
731
-
732
- for await (const chunk of pipelineStream) {
733
- if (!isActive) break;
734
- if (typeof chunk === 'string') {
735
- fullReply += chunk;
736
- enqueue(sseTextFrame(chunk));
737
- } else if (chunk && typeof chunk === 'object' && 'type' in chunk && (chunk as any).type === 'thinking') {
738
- enqueue(`data: ${JSON.stringify(chunk)}\n\n`);
739
- } else {
740
- // Retrieval metadata object — always the final frame
741
- enqueue(sseMetaFrame(chunk));
742
-
743
- const responseChunk = chunk as ChatResponse;
744
- const sources = responseChunk?.sources || [];
745
-
746
- if (responseChunk?.trace) {
747
- enqueue(sseObservabilityFrame(responseChunk.trace));
748
- }
749
-
750
- let uiTransformation = responseChunk?.ui_transformation;
751
- if (sources.length > 0) {
752
- try {
753
- uiTransformation =
754
- responseChunk?.ui_transformation ??
755
- UITransformer.transform(message, sources, plugin.getConfig());
756
-
757
- if (uiTransformation) {
758
- enqueue(sseUIFrame(uiTransformation));
759
- }
760
- } catch (transformError) {
761
- console.warn('[createStreamHandler] UI transformation warning:', transformError);
762
- try {
763
- const fallback = UITransformer.transform(message, sources, plugin.getConfig());
764
- if (fallback) {
765
- uiTransformation = fallback;
766
- enqueue(sseUIFrame(fallback));
767
- }
768
- } catch { /* silent */ }
769
- }
770
- }
771
-
772
- // Save final completed assistant response to history
773
- await storage.saveMessage(sessionId, {
774
- id: assistantMsgId,
775
- role: 'assistant',
776
- content: fullReply,
777
- sources,
778
- uiTransformation,
779
- trace: responseChunk?.trace,
780
- }).catch(err => console.warn('[createStreamHandler] Failed to save assistant message:', err));
781
-
782
- reportTelemetry(req, plugin, 'QUERY_GENERATION', 'success', `Tokens: ${responseChunk?.trace?.tokens?.totalTokens || 0}, Latency: ${responseChunk?.trace?.latency?.totalMs || 0}ms`, responseChunk?.trace);
783
- }
784
- }
785
- } catch (streamError) {
786
- if (isActive) {
787
- let errorMessage =
788
- streamError instanceof Error ? streamError.message : String(streamError);
789
-
790
- if (
791
- errorMessage.includes('403') ||
792
- errorMessage.toLowerCase().includes('license') ||
793
- errorMessage.toLowerCase().includes('revoked') ||
794
- errorMessage.toLowerCase().includes('terminated')
795
- ) {
796
- errorMessage = 'Your Retrivora license key has been terminated, suspended, or revoked. Access denied.';
797
- }
798
-
799
- console.error('[createStreamHandler] Stream error:', streamError);
800
- reportTelemetry(req, plugin, 'QUERY_GENERATION', 'error', errorMessage);
801
- try {
802
- enqueue(sseErrorFrame(errorMessage));
803
- } catch { /* silent */ }
804
- }
805
- } finally {
806
- if (isActive) {
807
- isActive = false;
808
- try {
809
- controller.close();
810
- } catch { /* silent */ }
811
- }
812
- }
813
- },
814
- cancel(reason) {
815
- isActive = false;
816
- //console.log('[createStreamHandler] Stream connection closed by client:', reason);
817
- }
818
- });
819
-
820
- return new Response(stream, { headers: { ...SSE_HEADERS, 'x-message-id': assistantMsgId, 'x-user-message-id': userMsgId } });
821
- };
822
- }
823
-
824
- /**
825
- * Build a POST handler that ingests raw pre-parsed documents into the vector DB.
826
- *
827
- * Unlike `createUploadHandler` (which takes files and does parsing/splitting),
828
- * this endpoint expects the caller to supply `documents[]` already split and
829
- * prepared. This is useful for bulk backfill jobs, migrations, or programmatic
830
- * sync pipelines.
831
- *
832
- * Security: Requires a valid license (RSA signature check via `enforceLicense`)
833
- * AFTER JSON body parse so the optional `body.licenseKey` override is read.
834
- *
835
- * @param configOrPlugin - Partial `RagConfig` or existing `VectorPlugin`.
836
- * @param options - Optional `onAuthorize` gate.
837
- * @returns Async POST handler.
838
- */
839
- export function createIngestHandler(
840
- configOrPlugin?: Partial<RagConfig> | VectorPlugin,
841
- options?: HandlerOptions
842
- ) {
843
- const plugin = getOrCreatePlugin(configOrPlugin);
844
- const onAuthorize = options?.onAuthorize;
845
-
846
- return async function POST(req: any, context?: any) {
847
- const authResult = await checkAuth(req, onAuthorize);
848
- if (authResult) return authResult as any;
849
-
850
- const config = plugin.getConfig();
851
-
852
- try {
853
- const body = await req.json();
854
-
855
- // License enforcement before ingestion
856
- const licenseCheck = await enforceLicense(req as NextRequest, config, body?.licenseKey);
857
- if (!licenseCheck.ok) return licenseCheck.response as any;
858
-
859
- const { documents, namespace } = body as {
860
- documents: Array<{ docId: string; content: string; metadata?: Record<string, unknown> }>;
861
- namespace?: string;
862
- };
863
-
864
- if (!Array.isArray(documents) || documents.length === 0) {
865
- return NextResponse.json({ error: 'documents array is required' }, { status: 400 });
866
- }
867
-
868
- const namespaceCheck = resolveLicensedNamespace(licenseCheck.payload, namespace);
869
- if (!namespaceCheck.ok) return namespaceCheck.response as any;
870
-
871
- // Free Tier ingestion limits check
872
- if (isFreeTier(licenseCheck.payload.tier)) {
873
- const totalIncomingBytes = documents.reduce(
874
- (sum, d) => sum + Buffer.byteLength(d.content || '', 'utf8'),
875
- 0
876
- );
877
- const ingestCheck = (FreeTierLimitsGuard as any).checkIngestionAllowed(
878
- { documentCount: documents.length },
879
- totalIncomingBytes
880
- );
881
- if (!ingestCheck.allowed) {
882
- return createPaymentRequiredResponse(
883
- ingestCheck.reason || 'Free tier ingestion limit reached.',
884
- {
885
- tier: licenseCheck.payload.tier,
886
- projectId: licenseCheck.payload.projectId,
887
- documents: documents.length,
888
- incomingBytes: totalIncomingBytes,
889
- }
890
- );
891
- }
892
- }
893
-
894
- const results = await plugin.ingest(documents, namespaceCheck.namespace);
895
- reportTelemetry(req, plugin, 'DOCUMENT_INGESTION', 'success', `Ingested ${documents.length} document(s)`);
896
- return NextResponse.json({ results });
897
- } catch (err) {
898
- const message = err instanceof Error ? err.message : 'Internal server error';
899
- reportTelemetry(req, plugin, 'DOCUMENT_INGESTION', 'error', message);
900
- return NextResponse.json({ error: message }, { status: 500 });
901
- }
902
- };
903
- }
904
-
905
- /**
906
- * Build a GET handler that returns health / readiness status.
907
- *
908
- * Response shape (200 OK):
909
- * ```json
910
- * {
911
- * "ok": true,
912
- * "status": "healthy",
913
- * "sdkVersion": "x.y.z",
914
- * "timestamp": "ISO string",
915
- * "embedding": { "provider": "...", "ok": true },
916
- * "vectorStore": { "provider": "...", "ok": true }
917
- * }
918
- * ```
919
- *
920
- * License check: This endpoint is intentionally **NOT** license-gated so
921
- * orchestrators (Kubernetes, uptime probes, Vercel cron pings) can use it
922
- * to assert the process is alive even when no license has been configured
923
- * yet during deploy.
924
- *
925
- * @param configOrPlugin - Partial `RagConfig` or existing `VectorPlugin`.
926
- * @param options - Optional `onAuthorize` gate (rarely used here).
927
- * @returns Async GET handler.
928
- */
929
- export function createHealthHandler(
930
- configOrPlugin?: Partial<RagConfig> | VectorPlugin,
931
- options?: HandlerOptions
932
- ) {
933
- const plugin = getOrCreatePlugin(configOrPlugin);
934
- const onAuthorize = options?.onAuthorize;
935
-
936
- return async function GET(req?: NextRequest) {
937
- if (req) {
938
- const authResult = await checkAuth(req, onAuthorize);
939
- if (authResult) return authResult as any;
940
- }
941
-
942
- try {
943
- const health = await plugin.checkHealth();
944
- const status = health.allHealthy ? 'ok' : 'degraded';
945
- return NextResponse.json({
946
- status,
947
- ...health,
948
- timestamp: new Date().toISOString(),
949
- });
950
- } catch (err) {
951
- const message = err instanceof Error ? err.message : 'Unknown error';
952
- return NextResponse.json({ status: 'error', error: message }, { status: 500 });
953
- }
954
- };
955
- }
956
-
957
- /**
958
- * Build a POST handler for the SDK-facing `/v1/license/validate` endpoint.
959
- *
960
- * Client flow: `LicenseValidator.validate()` calls this URL, receives
961
- * authoritative DB-backed status (ACTIVE / TERMINATED / SUSPENDED / EXPIRED /
962
- // REVOKED), and falls back to pure-local `LicenseVerifier.verifyAsync()` if the
963
- * server is unreachable or returns an error.
964
- *
965
- * This handler does NOT enforce license — it is the verifier itself. It does,
966
- * however, still apply rate limiting to mitigate abuse.
967
- *
968
- * @param configOrPlugin - Partial `RagConfig` or existing `VectorPlugin`.
969
- * @param options - Optional `onAuthorize` gate.
970
- * @returns Async POST handler returning `LicenseValidationResponse` JSON.
971
- */
972
- export function createLicenseHandler(
973
- configOrPlugin?: Partial<RagConfig> | VectorPlugin,
974
- options?: HandlerOptions
975
- ) {
976
- const plugin = getOrCreatePlugin(configOrPlugin);
977
- const onAuthorize = options?.onAuthorize;
978
-
979
- return async function POST(req: NextRequest) {
980
- if (req) {
981
- const authResult = await checkAuth(req, onAuthorize);
982
- if (authResult) return authResult as any;
983
- }
984
-
985
- try {
986
- const body = await req.json().catch(() => ({}));
987
- const config = plugin.getConfig();
988
- const rawKey =
989
- req.headers.get('x-license-key') ||
990
- body?.licenseKey ||
991
- req.headers.get('authorization')?.replace(/^Bearer\s+/i, '') ||
992
- config.licenseKey ||
993
- process.env.NEXT_PUBLIC_RETRIVORA_LICENSE_KEY ||
994
- process.env.RETRIVORA_LICENSE_KEY ||
995
- '';
996
-
997
- const licenseKey = (rawKey || '').trim().replace(/^["']|["']$/g, '').trim();
998
-
999
- const projectId = body?.projectId || config.projectId || '';
1000
-
1001
- //console.log(`[DEBUG] createLicenseHandler: projectId="${projectId}", body.projectId="${body?.projectId}", config.projectId="${config.projectId}"`);
1002
-
1003
- const payload = await LicenseVerifier.verifyAsync(licenseKey, projectId);
1004
-
1005
- return NextResponse.json({
1006
- valid: true,
1007
- licenseStatus: 'ACTIVE',
1008
- minimumSupportedVersion: '2.1.0',
1009
- latestVersion: SDK_VERSION,
1010
- forceUpgrade: false,
1011
- expiresAt: payload.expiresAt ? new Date(payload.expiresAt * 1000).toISOString() : null,
1012
- message: 'License key is active and valid.',
1013
- });
1014
- } catch (err: any) {
1015
- return NextResponse.json(
1016
- {
1017
- valid: false,
1018
- licenseStatus: 'REVOKED',
1019
- minimumSupportedVersion: '2.1.0',
1020
- latestVersion: SDK_VERSION,
1021
- forceUpgrade: false,
1022
- expiresAt: null,
1023
- message: err?.message || 'License validation failed.',
1024
- },
1025
- { status: 400 }
1026
- );
1027
- }
1028
- };
1029
- }
1030
-
1031
- /**
1032
- * Build a POST handler that accepts `multipart/form-data` file uploads.
1033
- *
1034
- * Workflow:
1035
- * 1. `onAuthorize` → license enforcement via `enforceLicense` (no JSON body,
1036
- * so the key must come from headers or the environment).
1037
- * 2. Read FormData files[] list → max 25 files per request, 100 MB each.
1038
- * 3. Per-file: use `DocumentParser` to extract raw text by MIME type (PDF,
1039
- * DOCX, TXT, MD, HTML, JSON, CSV, images via OCR fallback, etc.).
1040
- * 4. Chunk by the configured chunker (RecursiveCharacter, semantic, etc.).
1041
- * 5. Call `plugin.ingest()` to embed and insert into the vector store.
1042
- * 6. Return JSON summary per file (status, chunks ingested, docId).
1043
- *
1044
- * @param configOrPlugin - Partial `RagConfig` or existing `VectorPlugin`.
1045
- * @param options - Optional `onAuthorize` gate.
1046
- * @returns Async POST handler.
1047
- */
1048
- export function createUploadHandler(
1049
- configOrPlugin?: Partial<RagConfig> | VectorPlugin,
1050
- options?: HandlerOptions
1051
- ) {
1052
- const plugin = getOrCreatePlugin(configOrPlugin);
1053
- const onAuthorize = options?.onAuthorize;
1054
-
1055
- return async function POST(req: any, context?: any) {
1056
- //console.log('Upload handler invoked');
1057
- const authResult = await checkAuth(req, onAuthorize);
1058
- if (authResult) return authResult as any;
1059
-
1060
- // License enforcement (uploads use headers/env for license key, no JSON body)
1061
- const config = plugin.getConfig();
1062
- const licenseCheck = await enforceLicense(req as NextRequest, config);
1063
- if (!licenseCheck.ok) return licenseCheck.response as any;
1064
-
1065
- try {
1066
- const formData = await req.formData();
1067
- const files = formData.getAll('files') as File[];
1068
- //console.log('Files received for upload:', files.map(f => f.name));
1069
- const namespace = (formData.get('namespace') as string) || undefined;
1070
- const dimensionRaw = formData.get('dimension');
1071
- const dimension = dimensionRaw ? parseInt(dimensionRaw as string, 10) : undefined;
1072
- const namespaceCheck = resolveLicensedNamespace(licenseCheck.payload, namespace);
1073
- if (!namespaceCheck.ok) return namespaceCheck.response as any;
1074
-
1075
- if (!files || files.length === 0) {
1076
- return NextResponse.json({ error: 'No files provided' }, { status: 400 });
1077
- }
1078
-
1079
- // Free Tier upload limits check (before parsing to fail fast on large uploads)
1080
- if (isFreeTier(licenseCheck.payload.tier)) {
1081
- const totalUploadBytes = files.reduce((sum, f) => sum + (f.size || 0), 0);
1082
- const uploadCheck = (FreeTierLimitsGuard as any).checkIngestionAllowed(
1083
- { documentCount: files.length },
1084
- totalUploadBytes
1085
- );
1086
- if (!uploadCheck.allowed) {
1087
- return createPaymentRequiredResponse(
1088
- uploadCheck.reason || 'Free tier upload limit reached.',
1089
- {
1090
- tier: licenseCheck.payload.tier,
1091
- projectId: licenseCheck.payload.projectId,
1092
- fileCount: files.length,
1093
- totalUploadBytes,
1094
- maxFileSizeBytes: FREE_TIER_QUOTAS.MAX_FILE_SIZE_BYTES,
1095
- maxStorageBytes: FREE_TIER_QUOTAS.MAX_STORAGE_BYTES,
1096
- }
1097
- );
1098
- }
1099
- }
1100
-
1101
- const documents: { docId: string; content: string; metadata?: Record<string, unknown> }[] = [];
1102
-
1103
- for (const file of files) {
1104
- const isExcel =
1105
- file.name.toLowerCase().endsWith('.xlsx') ||
1106
- file.name.toLowerCase().endsWith('.xls') ||
1107
- file.type.includes('spreadsheet') ||
1108
- file.type.includes('excel');
1109
-
1110
- if (file.name.toLowerCase().endsWith('.csv') || file.type === 'text/csv') {
1111
- const text = await file.text();
1112
- const Papa = await import('papaparse');
1113
- const parsed = Papa.default.parse(text, { header: true, skipEmptyLines: true });
1114
-
1115
- if (parsed.data && parsed.data.length > 0) {
1116
- let i = 0;
1117
- let lastRowData: Record<string, string> | null = null;
1118
- const csvCols = Object.keys(parsed.data[0] as Record<string, string>);
1119
-
1120
- for (const row of parsed.data) {
1121
- i++;
1122
- const rowData = row as Record<string, string>;
1123
- const groupingKey = Object.keys(rowData)[0];
1124
-
1125
- if (lastRowData && groupingKey && rowData[groupingKey] && rowData[groupingKey] === lastRowData[groupingKey]) {
1126
- for (const key of Object.keys(rowData)) {
1127
- if (!rowData[key] && lastRowData[key]) {
1128
- rowData[key] = lastRowData[key];
1129
- }
1130
- }
1131
- }
1132
- lastRowData = { ...rowData };
1133
-
1134
- const contentParts: string[] = [];
1135
- for (const [key, val] of Object.entries(rowData)) {
1136
- if (key && val) {
1137
- contentParts.push(`${key}: ${val}`);
1138
- }
1139
- }
1140
-
1141
- documents.push({
1142
- docId: `${file.name}-row-${i}`,
1143
- content: contentParts.join(', '),
1144
- metadata: {
1145
- fileName: file.name,
1146
- fileSize: file.size,
1147
- fileType: file.type || 'text/csv',
1148
- uploadedAt: new Date().toISOString(),
1149
- ...(dimension ? { dimension } : {}),
1150
- csvHeaders: csvCols,
1151
- ...rowData
1152
- }
1153
- });
1154
- }
1155
- }
1156
- } else if (isExcel) {
1157
- const xlsx = await import('xlsx');
1158
- const buffer = Buffer.isBuffer(file) ? file : Buffer.from(await file.arrayBuffer());
1159
- const workbook = xlsx.read(buffer, { type: 'buffer' });
1160
-
1161
- for (const sheetName of workbook.SheetNames) {
1162
- const sheet = workbook.Sheets[sheetName];
1163
- const jsonRows = xlsx.utils.sheet_to_json<Record<string, any>>(sheet, { defval: '' });
1164
-
1165
- if (jsonRows && jsonRows.length > 0) {
1166
- let i = 0;
1167
- const headers = Object.keys(jsonRows[0] || {});
1168
-
1169
- for (const rowData of jsonRows) {
1170
- i++;
1171
- const contentParts: string[] = [];
1172
- for (const [key, val] of Object.entries(rowData)) {
1173
- if (key && val !== undefined && val !== null && String(val).trim()) {
1174
- contentParts.push(`${key}: ${val}`);
1175
- }
1176
- }
1177
-
1178
- if (contentParts.length > 0) {
1179
- documents.push({
1180
- docId: `${file.name}-${sheetName}-row-${i}`,
1181
- content: contentParts.join(', '),
1182
- metadata: {
1183
- fileName: file.name,
1184
- sheetName,
1185
- fileSize: file.size,
1186
- fileType: file.type || 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
1187
- uploadedAt: new Date().toISOString(),
1188
- ...(dimension ? { dimension } : {}),
1189
- headers,
1190
- ...rowData
1191
- }
1192
- });
1193
- }
1194
- }
1195
- }
1196
- }
1197
- } else {
1198
- const content = await DocumentParser.parse(file, file.name, file.type);
1199
- documents.push({
1200
- docId: file.name,
1201
- content,
1202
- metadata: {
1203
- fileName: file.name,
1204
- fileSize: file.size,
1205
- fileType: file.type,
1206
- uploadedAt: new Date().toISOString(),
1207
- ...(dimension ? { dimension } : {}),
1208
- },
1209
- });
1210
- }
1211
- }
1212
-
1213
- const results = await plugin.ingest(documents, namespaceCheck.namespace);
1214
- reportTelemetry(req, plugin, 'DOCUMENT_INGESTION', 'success', `Uploaded & ingested ${files.length} file(s)`);
1215
- return NextResponse.json({ message: 'Upload successful', results });
1216
- } catch (err) {
1217
- const message = err instanceof Error ? err.message : 'Upload failed';
1218
- reportTelemetry(req, plugin, 'DOCUMENT_INGESTION', 'error', message);
1219
- return NextResponse.json({ error: message }, { status: 500 });
1220
- }
1221
- };
1222
- }
1223
-
1224
- /**
1225
- * Build a GET/POST handler for auto-suggestions (used by ChatWidget prompt chips).
1226
- *
1227
- * Requests:
1228
- * - GET `?query=...&namespace=...`
1229
- * - POST `{ query: string, namespace?: string, licenseKey?: string }`
1230
- *
1231
- * Response (200 OK): `{ suggestions: string[] }`
1232
- *
1233
- * License enforcement: Runs BEFORE body parse on headers/env, and if the POST
1234
- * body contains a `licenseKey` override the check is repeated with the body
1235
- * key included so the per-request override is honored.
1236
- *
1237
- * @param configOrPlugin - Partial `RagConfig` or existing `VectorPlugin`.
1238
- * @param options - Optional `onAuthorize` gate.
1239
- * @returns Async handler accepting both GET and POST methods.
1240
- */
1241
- export function createSuggestionsHandler(
1242
- configOrPlugin?: Partial<RagConfig> | VectorPlugin,
1243
- options?: HandlerOptions
1244
- ) {
1245
- const plugin = getOrCreatePlugin(configOrPlugin);
1246
- const onAuthorize = options?.onAuthorize;
1247
-
1248
- return async function handler(req: NextRequest) {
1249
- const authResult = await checkAuth(req, onAuthorize);
1250
- if (authResult) return authResult as any;
1251
-
1252
- // License enforcement for auto-suggestions endpoint
1253
- const config = plugin.getConfig();
1254
- const licenseCheck = await enforceLicense(req, config);
1255
- if (!licenseCheck.ok) return licenseCheck.response as any;
1256
- let effectiveLicensePayload = licenseCheck.payload;
1257
-
1258
- try {
1259
- let query = '';
1260
- let namespace: string | undefined = undefined;
1261
- let bodyLicenseKey: string | undefined = undefined;
1262
-
1263
- if (req.method === 'POST') {
1264
- const body = await req.json().catch(() => ({}));
1265
- query = body.query || '';
1266
- namespace = body.namespace;
1267
- bodyLicenseKey = body.licenseKey;
1268
- // Re-check license if body contained a key override
1269
- if (bodyLicenseKey) {
1270
- const recheck = await enforceLicense(req, config, bodyLicenseKey);
1271
- if (!recheck.ok) return recheck.response as any;
1272
- effectiveLicensePayload = recheck.payload;
1273
- }
1274
- } else {
1275
- const url = new URL(req.url);
1276
- query = url.searchParams.get('query') || '';
1277
- namespace = url.searchParams.get('namespace') || undefined;
1278
- }
1279
-
1280
- if (!query || typeof query !== 'string' || !query.trim()) {
1281
- return NextResponse.json({ suggestions: [] });
1282
- }
1283
-
1284
- const namespaceCheck = resolveLicensedNamespace(effectiveLicensePayload, namespace);
1285
- if (!namespaceCheck.ok) return namespaceCheck.response as any;
1286
-
1287
- const suggestions = await plugin.getSuggestions(query, namespaceCheck.namespace);
1288
- reportTelemetry(req, plugin, 'AUTO_SUGGESTIONS', 'success', `Fetched ${suggestions.length} suggestions for: ${query.slice(0, 30)}`);
1289
- return NextResponse.json({ suggestions });
1290
- } catch (err) {
1291
- const message = err instanceof Error ? err.message : 'Internal server error';
1292
- reportTelemetry(req, plugin, 'AUTO_SUGGESTIONS', 'error', message);
1293
- return NextResponse.json({ error: message }, { status: 500 });
1294
- }
1295
- };
1296
- }
1297
-
1298
- /**
1299
- * Build a handler for chat-history listing and clearing.
1300
- *
1301
- * Routes dispatched by `createRagHandler`:
1302
- * - GET `/history?sessionId=...&limit=...` → returns `{ messages: HistoryMessage[] }`
1303
- * - POST `/history/clear` with `{ sessionId? }` JSON body → returns `{ ok: true }`
1304
- *
1305
- * Storage: Uses `DatabaseStorage` (default SQLite or custom configured driver).
1306
- * License enforcement is handled by the upstream `createRagHandler` router when
1307
- * this handler is composed through it; standalone usage is rare.
1308
- *
1309
- * @param configOrPlugin - Partial `RagConfig` or existing `VectorPlugin`.
1310
- * @param options - Optional `onAuthorize` gate.
1311
- * @returns Async handler accepting GET (list) or POST (clear) methods.
1312
- */
1313
- export function createHistoryHandler(
1314
- configOrPlugin?: Partial<RagConfig> | VectorPlugin,
1315
- options?: HandlerOptions
1316
- ) {
1317
- const plugin = getOrCreatePlugin(configOrPlugin);
1318
- const storage = new DatabaseStorage(plugin.getConfig());
1319
- const onAuthorize = options?.onAuthorize;
1320
- const onResolveScope = options?.onResolveScope;
1321
-
1322
- return async function handler(req: NextRequest) {
1323
- const authResult = await checkAuth(req, onAuthorize);
1324
- if (authResult) return authResult as any;
1325
-
1326
- const scope = onResolveScope ? await onResolveScope(req) : undefined;
1327
-
1328
- const url = new URL(req.url);
1329
- const sessionId = url.searchParams.get('sessionId') || 'default';
1330
-
1331
- if (req.method === 'GET') {
1332
- try {
1333
- const history = await storage.getHistory(sessionId, scope);
1334
- return NextResponse.json({ history });
1335
- } catch (err) {
1336
- const message = err instanceof Error ? err.message : 'Failed to fetch history';
1337
- return NextResponse.json({ error: message }, { status: 500 });
1338
- }
1339
- } else if (req.method === 'POST') {
1340
- try {
1341
- const body = await req.json().catch(() => ({}));
1342
- const sid = body.sessionId || sessionId;
1343
- await storage.clearHistory(sid, scope);
1344
- return NextResponse.json({ success: true });
1345
- } catch (err) {
1346
- const message = err instanceof Error ? err.message : 'Failed to clear history';
1347
- return NextResponse.json({ error: message }, { status: 500 });
1348
- }
1349
- }
1350
- return NextResponse.json({ error: 'Method Not Allowed' }, { status: 405 });
1351
- };
1352
- }
1353
-
1354
- /**
1355
- * Build a POST handler for collecting thumbs-up / thumbs-down user feedback.
1356
- *
1357
- * Request: `{ messageId, sessionId?, rating: 1 | -1, comment?, reason? }`
1358
- * Response: `{ ok: true, id: string }` (201 Created)
1359
- *
1360
- * Feedback is written to `DatabaseStorage.feedback` for RAG pipeline
1361
- * evaluation, offline fine-tuning dataset curation, and support triage.
1362
- *
1363
- * @param configOrPlugin - Partial `RagConfig` or existing `VectorPlugin`.
1364
- * @param options - Optional `onAuthorize` gate.
1365
- * @returns Async POST handler.
1366
- */
1367
- export function createFeedbackHandler(
1368
- configOrPlugin?: Partial<RagConfig> | VectorPlugin,
1369
- options?: HandlerOptions
1370
- ) {
1371
- const plugin = getOrCreatePlugin(configOrPlugin);
1372
- const storage = new DatabaseStorage(plugin.getConfig());
1373
- const onAuthorize = options?.onAuthorize;
1374
- const onResolveScope = options?.onResolveScope;
1375
-
1376
- return async function handler(req: NextRequest) {
1377
- const authResult = await checkAuth(req, onAuthorize);
1378
- if (authResult) return authResult as any;
1379
-
1380
- const scope = onResolveScope ? await onResolveScope(req) : undefined;
1381
-
1382
- if (req.method === 'GET') {
1383
- try {
1384
- const url = new URL(req.url);
1385
- const messageId = url.searchParams.get('messageId');
1386
- if (!messageId) {
1387
- const feedbackList = await storage.listFeedback(scope);
1388
- return NextResponse.json({ feedback: feedbackList });
1389
- }
1390
- const feedback = await storage.getFeedback(messageId, scope);
1391
- return NextResponse.json({ feedback });
1392
- } catch (err) {
1393
- const message = err instanceof Error ? err.message : 'Failed to fetch feedback';
1394
- return NextResponse.json({ error: message }, { status: 500 });
1395
- }
1396
- } else if (req.method === 'POST') {
1397
- try {
1398
- const body = await req.json();
1399
- const { messageId, sessionId = 'default', rating, comment } = body as {
1400
- messageId: string;
1401
- sessionId?: string;
1402
- rating: 'thumbs_up' | 'thumbs_down';
1403
- comment?: string;
1404
- };
1405
-
1406
- if (!messageId) {
1407
- return NextResponse.json({ error: 'messageId is required' }, { status: 400 });
1408
- }
1409
- if (!rating) {
1410
- return NextResponse.json({ error: 'rating is required' }, { status: 400 });
1411
- }
1412
- if (scope && scope.length > 0) {
1413
- const matches = scope.some(
1414
- (pid) => pid && (sessionId === pid || sessionId.startsWith(pid) || sessionId.includes(pid))
1415
- );
1416
- if (!matches) {
1417
- return NextResponse.json(
1418
- { error: 'sessionId is outside the authorized project scope' },
1419
- { status: 403 }
1420
- );
1421
- }
1422
- }
1423
-
1424
- await storage.saveFeedback({ messageId, sessionId, rating, comment });
1425
- return NextResponse.json({ success: true });
1426
- } catch (err) {
1427
- const message = err instanceof Error ? err.message : 'Failed to save feedback';
1428
- return NextResponse.json({ error: message }, { status: 500 });
1429
- }
1430
- }
1431
- return NextResponse.json({ error: 'Method Not Allowed' }, { status: 405 });
1432
- };
1433
- }
1434
-
1435
- /**
1436
- * Build a GET handler that lists the set of chat sessions.
1437
- *
1438
- * Response: `{ sessions: { id, title, lastMessageAt, preview }[] }`
1439
- *
1440
- * Sessions are read from `DatabaseStorage`. Combined with `createHistoryHandler`
1441
- * this enables a sidebar of prior conversations for custom UIs built on top of
1442
- * the `useRagChat` headless hook.
1443
- *
1444
- * @param configOrPlugin - Partial `RagConfig` or existing `VectorPlugin`.
1445
- * @param options - Optional `onAuthorize` gate.
1446
- * @returns Async GET handler returning a session listing.
1447
- */
1448
- export function createSessionsHandler(
1449
- configOrPlugin?: Partial<RagConfig> | VectorPlugin,
1450
- options?: HandlerOptions
1451
- ) {
1452
- const plugin = getOrCreatePlugin(configOrPlugin);
1453
- const storage = new DatabaseStorage(plugin.getConfig());
1454
- const onAuthorize = options?.onAuthorize;
1455
- const onResolveScope = options?.onResolveScope;
1456
-
1457
- return async function GET(req?: NextRequest) {
1458
- if (req) {
1459
- const authResult = await checkAuth(req, onAuthorize);
1460
- if (authResult) return authResult as any;
1461
- }
1462
- try {
1463
- const scope = req && onResolveScope ? await onResolveScope(req) : undefined;
1464
- const sessions = await storage.listSessions(scope);
1465
- return NextResponse.json({ sessions });
1466
- } catch (err) {
1467
- const message = err instanceof Error ? err.message : 'Failed to list sessions';
1468
- return NextResponse.json({ error: message }, { status: 500 });
1469
- }
1470
- };
1471
- }
1472
-
1473
- /**
1474
- * Build a Next.js catch-all route that dispatches to every sub-handler by path.
1475
- *
1476
- * Intended drop-in export:
1477
- * ```ts
1478
- * // app/api/retrivora/[[...retrivora]]/route.ts
1479
- * import { createRagHandler } from '@retrivora-ai/rag-engine';
1480
- * const { GET, POST } = createRagHandler({ /* RagConfig *\/ });
1481
- * export { GET, POST };
1482
- * ```
1483
- *
1484
- * Segments dispatched (POST unless marked GET):
1485
- * - `chat` → createStreamHandler (SSE streaming chat)
1486
- * - `upload` → createUploadHandler (file ingestion)
1487
- * - `suggestions` → createSuggestionsHandler (GET/POST)
1488
- * - `v1/license/validate` → createLicenseHandler
1489
- * - `health` → createHealthHandler (GET)
1490
- * - `feedback` → createFeedbackHandler
1491
- * - `history` → createHistoryHandler (GET/POST)
1492
- * - `sessions` → createSessionsHandler (GET)
1493
- * - Any other segment → 404 with a JSON error body
1494
- *
1495
- * This is the **recommended** way to mount the SDK in a Next.js app. Using
1496
- * any single handler individually without its license enforcement gate is a
1497
- * security anti-pattern — prefer `createRagHandler` unless you have a very
1498
- * specific reason to split routes up.
1499
- *
1500
- * @param configOrPlugin - Partial `RagConfig` or existing `VectorPlugin`.
1501
- * @param options - Optional `onAuthorize` gate (propagated to every
1502
- * sub-handler so auth rules apply uniformly).
1503
- * @returns An object `{ GET, POST }` to re-export as your route file defaults.
1504
- */
1505
- export function createRagHandler(
1506
- configOrPlugin?: Partial<RagConfig> | VectorPlugin,
1507
- options?: HandlerOptions
1508
- ) {
1509
- const plugin = getOrCreatePlugin(configOrPlugin);
1510
- const onAuthorize = options?.onAuthorize;
1511
- const onResolveScope = options?.onResolveScope;
1512
- const scopeableOptions: HandlerOptions = { onAuthorize, onResolveScope };
1513
-
1514
- const chatHandler = createChatHandler(plugin, scopeableOptions);
1515
- const streamHandler = createStreamHandler(plugin, scopeableOptions);
1516
- const uploadHandler = createUploadHandler(plugin, scopeableOptions);
1517
- const healthHandler = createHealthHandler(plugin, scopeableOptions);
1518
- const suggestionsHandler = createSuggestionsHandler(plugin, scopeableOptions);
1519
- const historyHandler = createHistoryHandler(plugin, scopeableOptions);
1520
- const feedbackHandler = createFeedbackHandler(plugin, scopeableOptions);
1521
- const sessionsHandler = createSessionsHandler(plugin, scopeableOptions);
1522
- const licenseHandler = createLicenseHandler(plugin, scopeableOptions);
1523
-
1524
- async function routePostRequest(req: any, segment: string) {
1525
- switch (segment) {
1526
- case 'chat':
1527
- return streamHandler(req);
1528
- case 'chat-sync':
1529
- return chatHandler(req);
1530
- case 'upload':
1531
- return uploadHandler(req);
1532
- case 'suggestions':
1533
- return suggestionsHandler(req);
1534
- case 'health':
1535
- return healthHandler(req);
1536
- case 'history':
1537
- case 'history/clear':
1538
- return historyHandler(req);
1539
- case 'feedback':
1540
- return feedbackHandler(req);
1541
- case 'license/validate':
1542
- case 'v1/license/validate':
1543
- return licenseHandler(req);
1544
- default:
1545
- return NextResponse.json({ error: `Not Found: POST segment "${segment}" not supported.` }, { status: 404 });
1546
- }
1547
- }
1548
-
1549
- async function routeGetRequest(req: any, segment: string) {
1550
- switch (segment) {
1551
- case 'health':
1552
- return healthHandler(req);
1553
- case 'suggestions':
1554
- return suggestionsHandler(req);
1555
- case 'history':
1556
- return historyHandler(req);
1557
- case 'feedback':
1558
- return feedbackHandler(req);
1559
- case 'sessions':
1560
- return sessionsHandler(req);
1561
- default:
1562
- return NextResponse.json({ error: `Method Not Allowed: GET segment "${segment}" not supported.` }, { status: 405 });
1563
- }
1564
- }
1565
-
1566
- async function getSegment(req: any, context: any): Promise<string> {
1567
- // 1. If content-type is multipart/form-data, it MUST be an upload request!
1568
- const contentType = req.headers.get('content-type') || '';
1569
- if (contentType.includes('multipart/form-data')) {
1570
- return 'upload';
1571
- }
1572
-
1573
- // 2. Check path directly from request URL
1574
- try {
1575
- const url = new URL(req.url);
1576
- const pathname = url.pathname;
1577
- if (pathname.includes('/license/validate')) return 'v1/license/validate';
1578
- if (pathname.endsWith('/upload')) return 'upload';
1579
- if (pathname.endsWith('/chat') || pathname.endsWith('/chat-sync')) return 'chat';
1580
- if (pathname.endsWith('/health')) return 'health';
1581
- if (pathname.endsWith('/suggestions')) return 'suggestions';
1582
- if (pathname.endsWith('/feedback')) return 'feedback';
1583
- if (pathname.endsWith('/history')) return 'history';
1584
- if (pathname.endsWith('/history/clear')) return 'history/clear';
1585
- if (pathname.endsWith('/sessions')) return 'sessions';
1586
- } catch { /* silent */ }
1587
-
1588
- // 3. Fall back to resolved context params
1589
- const resolvedParams = typeof context?.params?.then === 'function'
1590
- ? await context.params
1591
- : context?.params;
1592
-
1593
- const segments: string[] = resolvedParams?.retrivora ||
1594
- resolvedParams?.params?.retrivora ||
1595
- resolvedParams?.rag ||
1596
- resolvedParams?.params?.rag ||
1597
- [];
1598
- const joined = segments.join('/');
1599
- return joined || 'chat';
1600
- }
1601
-
1602
- return {
1603
- GET: async (req: any, context?: any) => {
1604
- try {
1605
- const segment = await getSegment(req, context);
1606
- return await routeGetRequest(req, segment);
1607
- } catch (err) {
1608
- const msg = err instanceof Error ? err.message : 'GET Routing failed';
1609
- return NextResponse.json({ error: msg }, { status: 500 });
1610
- }
1611
- },
1612
- POST: async (req: any, context?: any) => {
1613
- try {
1614
- const segment = await getSegment(req, context);
1615
- return await routePostRequest(req, segment);
1616
- } catch (err) {
1617
- const msg = err instanceof Error ? err.message : 'POST Routing failed';
1618
- return NextResponse.json({ error: msg }, { status: 500 });
1619
- }
1620
- }
1621
- };
1622
- }
1623
-
1624
- // Re-export SSE helpers so host apps can use them in custom handlers
1625
- export { sseFrame, sseTextFrame, sseMetaFrame, sseErrorFrame, sseUIFrame, sseObservabilityFrame };