@retrivora-ai/rag-engine 2.2.3 → 2.2.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.
package/dist/server.js CHANGED
@@ -121,645 +121,6 @@ var init_templateUtils = __esm({
121
121
  }
122
122
  });
123
123
 
124
- // ../../services/llm-gateway/providers/groq.ts
125
- async function handleGroqRequest(req, apiKeyOverride) {
126
- const apiKey = apiKeyOverride || process.env.GROQ_API_KEY;
127
- if (!apiKey) {
128
- throw new Error("Groq API Key missing. Please set GROQ_API_KEY in environment variables.");
129
- }
130
- const modelName = req.model.replace(/^groq\//i, "");
131
- const payload = __spreadProps(__spreadValues({}, req), {
132
- model: modelName
133
- });
134
- const response = await fetch("https://api.groq.com/openai/v1/chat/completions", {
135
- method: "POST",
136
- headers: {
137
- "Content-Type": "application/json",
138
- "Authorization": `Bearer ${apiKey}`
139
- },
140
- body: JSON.stringify(payload)
141
- });
142
- if (!response.ok) {
143
- const errorText = await response.text();
144
- throw new Error(`Groq API Error (${response.status}): ${errorText}`);
145
- }
146
- if (req.stream && response.body) {
147
- return { stream: response.body };
148
- }
149
- const json = await response.json();
150
- return { response: json };
151
- }
152
- var init_groq = __esm({
153
- "../../services/llm-gateway/providers/groq.ts"() {
154
- "use strict";
155
- }
156
- });
157
-
158
- // ../../services/llm-gateway/providers/openai.ts
159
- async function handleOpenAIRequest(req, apiKeyOverride) {
160
- const apiKey = apiKeyOverride || process.env.OPENAI_API_KEY;
161
- if (!apiKey) {
162
- throw new Error("OpenAI API Key missing. Please set OPENAI_API_KEY in environment variables.");
163
- }
164
- const modelName = req.model.replace(/^(openai|gpt)\//i, "");
165
- const payload = __spreadProps(__spreadValues({}, req), {
166
- model: modelName
167
- });
168
- const response = await fetch("https://api.openai.com/v1/chat/completions", {
169
- method: "POST",
170
- headers: {
171
- "Content-Type": "application/json",
172
- "Authorization": `Bearer ${apiKey}`
173
- },
174
- body: JSON.stringify(payload)
175
- });
176
- if (!response.ok) {
177
- const errorText = await response.text();
178
- throw new Error(`OpenAI API Error (${response.status}): ${errorText}`);
179
- }
180
- if (req.stream && response.body) {
181
- return { stream: response.body };
182
- }
183
- const json = await response.json();
184
- return { response: json };
185
- }
186
- async function handleOpenAIEmbedding(req, apiKeyOverride) {
187
- const apiKey = apiKeyOverride || process.env.OPENAI_API_KEY;
188
- if (!apiKey) {
189
- throw new Error("OpenAI API Key missing. Please set OPENAI_API_KEY in environment variables.");
190
- }
191
- const modelName = req.model.replace(/^(openai)\//i, "");
192
- const response = await fetch("https://api.openai.com/v1/embeddings", {
193
- method: "POST",
194
- headers: {
195
- "Content-Type": "application/json",
196
- "Authorization": `Bearer ${apiKey}`
197
- },
198
- body: JSON.stringify({
199
- model: modelName,
200
- input: req.input
201
- })
202
- });
203
- if (!response.ok) {
204
- const errorText = await response.text();
205
- throw new Error(`OpenAI Embedding API Error (${response.status}): ${errorText}`);
206
- }
207
- return await response.json();
208
- }
209
- var init_openai = __esm({
210
- "../../services/llm-gateway/providers/openai.ts"() {
211
- "use strict";
212
- }
213
- });
214
-
215
- // ../../services/llm-gateway/providers/gemini.ts
216
- async function handleGeminiRequest(req, apiKeyOverride) {
217
- const apiKey = apiKeyOverride || process.env.GEMINI_API_KEY;
218
- if (!apiKey) {
219
- throw new Error("Gemini API Key missing. Please set GEMINI_API_KEY in environment variables.");
220
- }
221
- let modelName = req.model.replace(/^(google|gemini)\//i, "");
222
- if (!modelName.startsWith("gemini-")) {
223
- modelName = `gemini-${modelName}`;
224
- }
225
- const payload = __spreadProps(__spreadValues({}, req), {
226
- model: modelName
227
- });
228
- const response = await fetch("https://generativelanguage.googleapis.com/v1beta/openai/chat/completions", {
229
- method: "POST",
230
- headers: {
231
- "Content-Type": "application/json",
232
- "Authorization": `Bearer ${apiKey}`
233
- },
234
- body: JSON.stringify(payload)
235
- });
236
- if (!response.ok) {
237
- const errorText = await response.text();
238
- throw new Error(`Google Gemini API Error (${response.status}): ${errorText}`);
239
- }
240
- if (req.stream && response.body) {
241
- return { stream: response.body };
242
- }
243
- const json = await response.json();
244
- return { response: json };
245
- }
246
- async function handleGeminiEmbedding(req, apiKeyOverride) {
247
- const apiKey = apiKeyOverride || process.env.GEMINI_API_KEY;
248
- if (!apiKey) {
249
- throw new Error("Gemini API Key missing. Please set GEMINI_API_KEY in environment variables.");
250
- }
251
- let modelName = req.model.replace(/^(google|gemini)\//i, "");
252
- if (!modelName || modelName === "default" || modelName.includes("text-embedding-3") || modelName.includes("ada") || !modelName.startsWith("text-embedding-00") && !modelName.startsWith("embedding-00")) {
253
- modelName = "text-embedding-004";
254
- }
255
- const response = await fetch("https://generativelanguage.googleapis.com/v1beta/openai/embeddings", {
256
- method: "POST",
257
- headers: {
258
- "Content-Type": "application/json",
259
- "Authorization": `Bearer ${apiKey}`
260
- },
261
- body: JSON.stringify({
262
- model: modelName,
263
- input: req.input
264
- })
265
- });
266
- if (!response.ok) {
267
- const errorText = await response.text();
268
- throw new Error(`Google Gemini Embedding API Error (${response.status}): ${errorText}`);
269
- }
270
- return await response.json();
271
- }
272
- var init_gemini = __esm({
273
- "../../services/llm-gateway/providers/gemini.ts"() {
274
- "use strict";
275
- }
276
- });
277
-
278
- // ../../services/llm-gateway/providers/huggingface.ts
279
- async function handleHuggingFaceChatRequest(req, apiKeyOverride) {
280
- var _a2, _b;
281
- const apiKey = apiKeyOverride || process.env.HUGGINGFACE_API_KEY || process.env.HF_TOKEN;
282
- if (!apiKey) {
283
- throw new Error("HuggingFace API Key missing. Set HUGGINGFACE_API_KEY or HF_TOKEN in environment variables.");
284
- }
285
- let modelName = req.model.replace(/^(huggingface|hf)\//i, "");
286
- if (!modelName || modelName === "default" || modelName.toLowerCase() === "qwen" || modelName.toLowerCase() === "qwen-coder") {
287
- modelName = "Qwen/Qwen2.5-Coder-32B-Instruct";
288
- }
289
- const payload = __spreadProps(__spreadValues({}, req), {
290
- model: modelName
291
- });
292
- try {
293
- const response = await fetch("https://router.huggingface.co/v1/chat/completions", {
294
- method: "POST",
295
- headers: {
296
- "Content-Type": "application/json",
297
- "Authorization": `Bearer ${apiKey}`
298
- },
299
- body: JSON.stringify(payload)
300
- });
301
- if (response.ok) {
302
- if (req.stream && response.body) {
303
- return { stream: response.body };
304
- }
305
- const json = await response.json();
306
- return { response: json };
307
- }
308
- } catch (e) {
309
- }
310
- try {
311
- const response = await fetch("https://router.huggingface.co/hf-inference/v1/chat/completions", {
312
- method: "POST",
313
- headers: {
314
- "Content-Type": "application/json",
315
- "Authorization": `Bearer ${apiKey}`
316
- },
317
- body: JSON.stringify(payload)
318
- });
319
- if (response.ok) {
320
- if (req.stream && response.body) {
321
- return { stream: response.body };
322
- }
323
- const json = await response.json();
324
- return { response: json };
325
- }
326
- } catch (e) {
327
- }
328
- const lastUserMsg = ((_a2 = req.messages.filter((m) => m.role === "user").pop()) == null ? void 0 : _a2.content) || "Hello";
329
- const pipelineRes = await fetch(`https://router.huggingface.co/hf-inference/models/${modelName}`, {
330
- method: "POST",
331
- headers: {
332
- "Content-Type": "application/json",
333
- "Authorization": `Bearer ${apiKey}`
334
- },
335
- body: JSON.stringify({
336
- inputs: lastUserMsg,
337
- parameters: { max_new_tokens: req.max_tokens || 256, temperature: req.temperature || 0.7 },
338
- options: { wait_for_model: true }
339
- })
340
- });
341
- if (!pipelineRes.ok) {
342
- const errorText = await pipelineRes.text();
343
- throw new Error(`HuggingFace API Error (${pipelineRes.status}): ${errorText}`);
344
- }
345
- const raw = await pipelineRes.json();
346
- const textOutput = Array.isArray(raw) ? ((_b = raw[0]) == null ? void 0 : _b.generated_text) || JSON.stringify(raw) : JSON.stringify(raw);
347
- const formattedResponse = {
348
- id: `hf-${Date.now()}`,
349
- object: "chat.completion",
350
- created: Math.floor(Date.now() / 1e3),
351
- model: modelName,
352
- choices: [
353
- {
354
- index: 0,
355
- message: {
356
- role: "assistant",
357
- content: textOutput
358
- },
359
- finish_reason: "stop"
360
- }
361
- ],
362
- usage: {
363
- prompt_tokens: lastUserMsg.length,
364
- completion_tokens: textOutput.length,
365
- total_tokens: lastUserMsg.length + textOutput.length
366
- }
367
- };
368
- return { response: formattedResponse };
369
- }
370
- async function handleHuggingFaceEmbedding(req, apiKeyOverride) {
371
- const apiKey = apiKeyOverride || process.env.HUGGINGFACE_API_KEY || process.env.HF_TOKEN;
372
- if (!apiKey) {
373
- throw new Error("HuggingFace API Key missing. Set HUGGINGFACE_API_KEY or HF_TOKEN in environment variables.");
374
- }
375
- let modelName = req.model.replace(/^(huggingface|hf)\//i, "");
376
- if (!modelName || modelName === "default" || modelName.includes("text-embedding-3") || modelName.includes("ada") || !modelName.includes("/") && !modelName.includes("bge") && !modelName.includes("sentence-transformers")) {
377
- modelName = "BAAI/bge-base-en-v1.5";
378
- }
379
- const inputs = Array.isArray(req.input) ? req.input : [req.input];
380
- const fetchEmbeddingFromModel = async (targetModel) => {
381
- try {
382
- const routerRes = await fetch("https://router.huggingface.co/hf-inference/v1/embeddings", {
383
- method: "POST",
384
- headers: {
385
- "Content-Type": "application/json",
386
- "Authorization": `Bearer ${apiKey}`
387
- },
388
- body: JSON.stringify({
389
- model: targetModel,
390
- input: inputs
391
- })
392
- });
393
- if (routerRes.ok) {
394
- const json = await routerRes.json();
395
- if (json && json.data) return json;
396
- }
397
- } catch (e) {
398
- }
399
- const response = await fetch(`https://router.huggingface.co/hf-inference/models/${targetModel}`, {
400
- method: "POST",
401
- headers: {
402
- "Content-Type": "application/json",
403
- "Authorization": `Bearer ${apiKey}`
404
- },
405
- body: JSON.stringify({
406
- inputs,
407
- options: { wait_for_model: true }
408
- })
409
- });
410
- if (!response.ok) {
411
- const errorText = await response.text();
412
- throw new Error(`HuggingFace API Error (${response.status}): ${errorText}`);
413
- }
414
- const rawData = await response.json();
415
- const embeddings = Array.isArray(rawData[0]) ? typeof rawData[0][0] === "number" ? rawData : rawData.map((arr) => arr[0]) : [rawData];
416
- return {
417
- object: "list",
418
- data: embeddings.map((vec, idx) => ({
419
- object: "embedding",
420
- embedding: vec,
421
- index: idx
422
- })),
423
- model: targetModel,
424
- usage: {
425
- prompt_tokens: inputs.reduce((acc, str) => acc + str.length, 0),
426
- completion_tokens: 0,
427
- total_tokens: inputs.reduce((acc, str) => acc + str.length, 0)
428
- }
429
- };
430
- };
431
- try {
432
- return await fetchEmbeddingFromModel(modelName);
433
- } catch (err) {
434
- if (modelName !== "BAAI/bge-base-en-v1.5") {
435
- console.warn(`[LLM Gateway] HuggingFace model ${modelName} failed. Falling back to BAAI/bge-base-en-v1.5...`, err);
436
- return await fetchEmbeddingFromModel("BAAI/bge-base-en-v1.5");
437
- }
438
- throw err;
439
- }
440
- }
441
- var init_huggingface = __esm({
442
- "../../services/llm-gateway/providers/huggingface.ts"() {
443
- "use strict";
444
- }
445
- });
446
-
447
- // ../../services/llm-gateway/providers/anthropic.ts
448
- async function handleAnthropicRequest(req, apiKeyOverride) {
449
- var _a2, _b;
450
- const apiKey = apiKeyOverride || process.env.ANTHROPIC_API_KEY;
451
- if (!apiKey) {
452
- throw new Error("Anthropic API Key missing. Please set ANTHROPIC_API_KEY in environment variables.");
453
- }
454
- const modelName = req.model.replace(/^(anthropic|claude)\//i, "");
455
- let systemPrompt = void 0;
456
- const anthropicMessages = [];
457
- for (const msg of req.messages) {
458
- if (msg.role === "system") {
459
- systemPrompt = systemPrompt ? `${systemPrompt}
460
- ${msg.content}` : msg.content;
461
- } else {
462
- anthropicMessages.push({
463
- role: msg.role === "assistant" ? "assistant" : "user",
464
- content: msg.content
465
- });
466
- }
467
- }
468
- if (anthropicMessages.length === 0) {
469
- anthropicMessages.push({ role: "user", content: "Hello" });
470
- }
471
- const payload = {
472
- model: modelName,
473
- messages: anthropicMessages,
474
- max_tokens: req.max_tokens || req.max_completion_tokens || 1024,
475
- stream: req.stream || false
476
- };
477
- if (systemPrompt) {
478
- payload.system = systemPrompt;
479
- }
480
- if (req.temperature !== void 0) {
481
- payload.temperature = req.temperature;
482
- }
483
- const response = await fetch("https://api.anthropic.com/v1/messages", {
484
- method: "POST",
485
- headers: {
486
- "Content-Type": "application/json",
487
- "x-api-key": apiKey,
488
- "anthropic-version": "2023-06-01"
489
- },
490
- body: JSON.stringify(payload)
491
- });
492
- if (!response.ok) {
493
- const errorText = await response.text();
494
- throw new Error(`Anthropic API Error (${response.status}): ${errorText}`);
495
- }
496
- if (req.stream && response.body) {
497
- return { stream: response.body };
498
- }
499
- const json = await response.json();
500
- const textContent = ((_b = (_a2 = json.content) == null ? void 0 : _a2[0]) == null ? void 0 : _b.text) || "";
501
- const openAIResponse = {
502
- id: json.id || `chatcmpl-${Date.now()}`,
503
- object: "chat.completion",
504
- created: Math.floor(Date.now() / 1e3),
505
- model: json.model || modelName,
506
- choices: [
507
- {
508
- index: 0,
509
- message: {
510
- role: "assistant",
511
- content: textContent
512
- },
513
- finish_reason: json.stop_reason === "end_turn" ? "stop" : json.stop_reason || "stop"
514
- }
515
- ],
516
- usage: json.usage ? {
517
- prompt_tokens: json.usage.input_tokens,
518
- completion_tokens: json.usage.output_tokens,
519
- total_tokens: json.usage.input_tokens + json.usage.output_tokens
520
- } : void 0
521
- };
522
- return { response: openAIResponse };
523
- }
524
- var init_anthropic = __esm({
525
- "../../services/llm-gateway/providers/anthropic.ts"() {
526
- "use strict";
527
- }
528
- });
529
-
530
- // ../../services/llm-gateway/providers/ollama.ts
531
- async function handleOllamaRequest(req, baseUrlOverride) {
532
- const baseUrl = (baseUrlOverride || process.env.OLLAMA_BASE_URL || "http://localhost:11434/v1").replace(/\/+$/, "");
533
- const modelName = req.model.replace(/^ollama\//i, "");
534
- const payload = __spreadProps(__spreadValues({}, req), {
535
- model: modelName
536
- });
537
- const response = await fetch(`${baseUrl}/chat/completions`, {
538
- method: "POST",
539
- headers: {
540
- "Content-Type": "application/json"
541
- },
542
- body: JSON.stringify(payload)
543
- });
544
- if (!response.ok) {
545
- const errorText = await response.text();
546
- throw new Error(`Ollama API Error (${response.status}): ${errorText}`);
547
- }
548
- if (req.stream && response.body) {
549
- return { stream: response.body };
550
- }
551
- const json = await response.json();
552
- return { response: json };
553
- }
554
- var init_ollama = __esm({
555
- "../../services/llm-gateway/providers/ollama.ts"() {
556
- "use strict";
557
- }
558
- });
559
-
560
- // ../../services/llm-gateway/router.ts
561
- var router_exports = {};
562
- __export(router_exports, {
563
- SUPPORTED_MODELS: () => SUPPORTED_MODELS,
564
- dispatchChatCompletion: () => dispatchChatCompletion,
565
- dispatchEmbedding: () => dispatchEmbedding,
566
- resolveProvider: () => resolveProvider
567
- });
568
- function resolveProvider(model) {
569
- const lower = model.toLowerCase();
570
- if (lower.startsWith("huggingface/") || lower.startsWith("hf/")) {
571
- return "huggingface";
572
- }
573
- if (lower.startsWith("ollama/")) {
574
- return "ollama";
575
- }
576
- if (lower.startsWith("groq/") || lower.includes("llama") || lower.includes("qwen") || lower.includes("mixtral")) {
577
- return "groq";
578
- }
579
- if (process.env.GROQ_API_KEY) return "groq";
580
- if (process.env.OPENAI_API_KEY) return "openai";
581
- if (process.env.GEMINI_API_KEY) return "gemini";
582
- if (process.env.ANTHROPIC_API_KEY) return "anthropic";
583
- if (process.env.HF_TOKEN || process.env.HUGGINGFACE_API_KEY) return "huggingface";
584
- return "groq";
585
- }
586
- function cleanApiKeyOverride(apiKeyOverride) {
587
- if (!apiKeyOverride) return void 0;
588
- const key = apiKeyOverride.trim();
589
- if (key === process.env.LITELLM_API_KEY || key === process.env.LITELLM_MASTER_KEY || key.startsWith("sk-retrivora") || !key.startsWith("gsk_") && !key.startsWith("AIza") && !key.startsWith("sk-ant-") && !key.startsWith("sk-proj-")) {
590
- return void 0;
591
- }
592
- return key;
593
- }
594
- async function resolveUserGatewayConfig(apiKeyOverride) {
595
- var _a2;
596
- if (!apiKeyOverride || !process.env.NEXT_PUBLIC_SUPABASE_URL || !process.env.SUPABASE_SERVICE_ROLE_KEY) {
597
- return {};
598
- }
599
- try {
600
- const { createAdminClient } = await import("@/lib/supabase-server");
601
- const supabase = createAdminClient();
602
- const { data: licenseRecord } = await supabase.from("licenses").select("project_id, customer_name, tier").eq("license_key", apiKeyOverride.trim()).single();
603
- if (!licenseRecord) {
604
- return {};
605
- }
606
- const projectId = licenseRecord.project_id;
607
- const { data: configs } = await supabase.from("gateway_config").select("project_id, default_model, provider_keys, is_active").in("project_id", [projectId, "global"]).eq("is_active", true);
608
- if (!configs || configs.length === 0) {
609
- return {};
610
- }
611
- const matchedConfig = configs.find((c) => c.project_id === projectId) || configs.find((c) => c.project_id === "global");
612
- const customGroqKey = (_a2 = matchedConfig == null ? void 0 : matchedConfig.provider_keys) == null ? void 0 : _a2.groq;
613
- const targetModel = matchedConfig == null ? void 0 : matchedConfig.default_model;
614
- return { customGroqKey, targetModel };
615
- } catch (err) {
616
- console.warn("[LLM Gateway Router] Error resolving user gateway_config:", err.message);
617
- return {};
618
- }
619
- }
620
- async function dispatchChatCompletion(req, apiKeyOverride) {
621
- const { customGroqKey, targetModel } = await resolveUserGatewayConfig(apiKeyOverride);
622
- const effectiveKey = customGroqKey || cleanApiKeyOverride(apiKeyOverride);
623
- const activeModel = req.model || targetModel || "llama-3.1-8b-instant";
624
- const provider = resolveProvider(activeModel);
625
- console.log(`[LLM Gateway Router] Dispatching chat request: model=${activeModel}, provider=${provider}, hasCustomKey=${Boolean(customGroqKey)}, hasGlobalGroqKey=${Boolean(process.env.GROQ_API_KEY)}`);
626
- try {
627
- switch (provider) {
628
- case "groq":
629
- return await handleGroqRequest(__spreadProps(__spreadValues({}, req), { model: activeModel }), effectiveKey);
630
- case "openai":
631
- return await handleOpenAIRequest(__spreadProps(__spreadValues({}, req), { model: activeModel }), effectiveKey);
632
- case "gemini":
633
- return await handleGeminiRequest(__spreadProps(__spreadValues({}, req), { model: activeModel }), effectiveKey);
634
- case "anthropic":
635
- return await handleAnthropicRequest(__spreadProps(__spreadValues({}, req), { model: activeModel }), effectiveKey);
636
- case "ollama":
637
- return await handleOllamaRequest(__spreadProps(__spreadValues({}, req), { model: activeModel }));
638
- case "huggingface":
639
- return await handleHuggingFaceChatRequest(__spreadProps(__spreadValues({}, req), { model: activeModel }), effectiveKey);
640
- default:
641
- throw new Error(`Unsupported LLM provider for model: ${activeModel}`);
642
- }
643
- } catch (error) {
644
- console.error(`[LLM Gateway Router] Provider '${provider}' failed for model '${req.model}':`, {
645
- message: error.message,
646
- stack: error.stack
647
- });
648
- if (/rate[- ]?limit|429|exhausted/i.test(error.message)) {
649
- const isGroqKey = process.env.GROQ_API_KEY || effectiveKey && effectiveKey.startsWith("gsk_");
650
- const reqModelLower = req.model.toLowerCase();
651
- if (isGroqKey && !reqModelLower.includes("llama-3.3-70b")) {
652
- console.warn(`[LLM Gateway] Provider ${provider} rate limited (${error.message}). Falling back to secondary Groq model (llama-3.3-70b-versatile)...`);
653
- try {
654
- return await handleGroqRequest(__spreadProps(__spreadValues({}, req), { model: "llama-3.3-70b-versatile" }), effectiveKey);
655
- } catch (fErr) {
656
- console.error("[LLM Gateway] Fallback groq/llama-3.3-70b-versatile failed:", fErr.message);
657
- }
658
- }
659
- const geminiKey = process.env.GEMINI_API_KEY || effectiveKey;
660
- if (provider !== "gemini" && geminiKey && (geminiKey.startsWith("AIza") || !geminiKey.startsWith("AQ."))) {
661
- console.warn(`[LLM Gateway] Provider ${provider} rate limited (${error.message}). Falling back to Google Gemini (gemini-2.5-flash)...`);
662
- try {
663
- return await handleGeminiRequest(__spreadProps(__spreadValues({}, req), { model: "gemini-2.5-flash" }), effectiveKey);
664
- } catch (fErr) {
665
- console.error("[LLM Gateway] Fallback gemini-2.5-flash failed:", fErr.message);
666
- }
667
- }
668
- if (provider !== "huggingface" && (process.env.HF_TOKEN || process.env.HUGGINGFACE_API_KEY)) {
669
- console.warn(`[LLM Gateway] Provider ${provider} rate limited (${error.message}). Falling back to Hugging Face (Qwen/Qwen2.5-Coder-32B-Instruct)...`);
670
- try {
671
- return await handleHuggingFaceChatRequest(__spreadProps(__spreadValues({}, req), { model: "Qwen/Qwen2.5-Coder-32B-Instruct" }), effectiveKey);
672
- } catch (fErr) {
673
- console.error("[LLM Gateway] Fallback HuggingFace Qwen failed:", fErr.message);
674
- }
675
- }
676
- if (provider !== "openai" && process.env.OPENAI_API_KEY) {
677
- console.warn(`[LLM Gateway] Provider ${provider} rate limited (${error.message}). Falling back to OpenAI (gpt-4o-mini)...`);
678
- try {
679
- return await handleOpenAIRequest(__spreadProps(__spreadValues({}, req), { model: "gpt-4o-mini" }), effectiveKey);
680
- } catch (e) {
681
- }
682
- }
683
- console.warn(`[LLM Gateway] Provider ${provider} rate limited (${error.message}). Falling back to local Ollama (llama3.2)...`);
684
- return handleOllamaRequest(__spreadProps(__spreadValues({}, req), { model: "llama3.2" }));
685
- }
686
- throw error;
687
- }
688
- }
689
- async function dispatchEmbedding(req, apiKeyOverride) {
690
- const effectiveKey = cleanApiKeyOverride(apiKeyOverride);
691
- if (req.model.startsWith("hf/") || req.model.startsWith("huggingface/") || req.model.includes("bge-")) {
692
- try {
693
- return await handleHuggingFaceEmbedding(req, effectiveKey);
694
- } catch (err) {
695
- console.warn("[LLM Gateway] HuggingFace explicit model embedding failed, falling back to default gateway chain:", err);
696
- }
697
- }
698
- const geminiKey = process.env.GEMINI_API_KEY || effectiveKey;
699
- const isGeminiKeyValid = Boolean(geminiKey && !geminiKey.startsWith("eyJ"));
700
- if (isGeminiKeyValid) {
701
- try {
702
- return await handleGeminiEmbedding(req, effectiveKey);
703
- } catch (err) {
704
- console.warn("[LLM Gateway] Gemini embedding failed:", err);
705
- }
706
- }
707
- if (process.env.OPENAI_API_KEY) {
708
- try {
709
- return await handleOpenAIEmbedding(req, effectiveKey);
710
- } catch (err) {
711
- console.warn("[LLM Gateway] OpenAI embedding failed:", err);
712
- }
713
- }
714
- if (process.env.HUGGINGFACE_API_KEY || process.env.HF_TOKEN) {
715
- try {
716
- return await handleHuggingFaceEmbedding(req, effectiveKey);
717
- } catch (err) {
718
- console.warn("[LLM Gateway] HuggingFace fallback embedding failed:", err);
719
- }
720
- }
721
- return handleGeminiEmbedding(req, effectiveKey);
722
- }
723
- var SUPPORTED_MODELS;
724
- var init_router = __esm({
725
- "../../services/llm-gateway/router.ts"() {
726
- "use strict";
727
- init_groq();
728
- init_openai();
729
- init_gemini();
730
- init_huggingface();
731
- init_anthropic();
732
- init_ollama();
733
- SUPPORTED_MODELS = [
734
- { id: "groq/llama-3.1-8b-instant", object: "model", created: 17e8, owned_by: "groq" },
735
- { id: "llama-3.1-8b-instant", object: "model", created: 17e8, owned_by: "groq" },
736
- { id: "groq/llama-3.3-70b-versatile", object: "model", created: 17e8, owned_by: "groq" },
737
- { id: "llama-3.3-70b-versatile", object: "model", created: 17e8, owned_by: "groq" },
738
- { id: "gpt-4o-mini", object: "model", created: 17e8, owned_by: "openai" },
739
- { id: "openai/gpt-4o-mini", object: "model", created: 17e8, owned_by: "openai" },
740
- { id: "gpt-4o", object: "model", created: 17e8, owned_by: "openai" },
741
- { id: "gemini-2.5-flash", object: "model", created: 17e8, owned_by: "google" },
742
- { id: "google/gemini-2.5-flash", object: "model", created: 17e8, owned_by: "google" },
743
- { id: "claude-3-5-haiku-20241022", object: "model", created: 17e8, owned_by: "anthropic" },
744
- { id: "anthropic/claude-3-5-haiku-20241022", object: "model", created: 17e8, owned_by: "anthropic" },
745
- { id: "text-embedding-004", object: "model", created: 17e8, owned_by: "google" },
746
- { id: "google/text-embedding-004", object: "model", created: 17e8, owned_by: "google" },
747
- { id: "BAAI/bge-base-en-v1.5", object: "model", created: 17e8, owned_by: "huggingface" },
748
- { id: "huggingface/BAAI/bge-base-en-v1.5", object: "model", created: 17e8, owned_by: "huggingface" },
749
- { id: "groq/qwen3-32b", object: "model", created: 17e8, owned_by: "groq" },
750
- { id: "qwen/qwen3-32b", object: "model", created: 17e8, owned_by: "groq" },
751
- { id: "qwen/qwen3-embedding-8b", object: "model", created: 17e8, owned_by: "qwen" },
752
- { id: "qwen3-embedding-8b", object: "model", created: 17e8, owned_by: "qwen" },
753
- { id: "ollama/llama3.2", object: "model", created: 17e8, owned_by: "ollama" }
754
- ];
755
- if (typeof globalThis !== "undefined") {
756
- const _g2 = globalThis;
757
- _g2.__retrivoraDispatchChat = dispatchChatCompletion;
758
- _g2.__retrivoraDispatchEmbedding = dispatchEmbedding;
759
- }
760
- }
761
- });
762
-
763
124
  // src/providers/vectordb/BaseVectorProvider.ts
764
125
  var BaseVectorProvider;
765
126
  var init_BaseVectorProvider = __esm({
@@ -816,8 +177,8 @@ var init_ConfigFetcher = __esm({
816
177
  process.env.NEXT_PUBLIC_RETRIVORA_CONTROL_PLANE_URL,
817
178
  "https://www.retrivora.com",
818
179
  "https://retrivora.com",
819
- "http://localhost:3001",
820
- "http://localhost:3000"
180
+ "http://localhost:3000",
181
+ "http://localhost:3001"
821
182
  ].filter(Boolean);
822
183
  for (const baseUrl of controlPlaneUrls) {
823
184
  try {
@@ -3454,7 +2815,7 @@ function getEnvConfig(env = process.env, base) {
3454
2815
  },
3455
2816
  telemetry: {
3456
2817
  enabled: telemetryEnabled,
3457
- url: (_Kb = (_Jb = (_Hb = readString(env, "TELEMETRY_URL")) != null ? _Hb : readString(env, "NEXT_PUBLIC_TELEMETRY_URL")) != null ? _Jb : (_Ib = base == null ? void 0 : base.telemetry) == null ? void 0 : _Ib.url) != null ? _Kb : process.env.NODE_ENV === "development" ? "http://localhost:3001/api/telemetry" : "https://retrivora.com/api/telemetry"
2818
+ url: (_Kb = (_Jb = (_Hb = readString(env, "TELEMETRY_URL")) != null ? _Hb : readString(env, "NEXT_PUBLIC_TELEMETRY_URL")) != null ? _Jb : (_Ib = base == null ? void 0 : base.telemetry) == null ? void 0 : _Ib.url) != null ? _Kb : "https://www.retrivora.com/api/telemetry"
3458
2819
  }
3459
2820
  }, readString(env, "GRAPH_DB_PROVIDER") ? {
3460
2821
  graphDb: {
@@ -4703,9 +4064,7 @@ var OPENAI_BASE = {
4703
4064
  };
4704
4065
  var LLM_PROFILES = {
4705
4066
  "openai-compatible": OPENAI_BASE,
4706
- "litellm": __spreadProps(__spreadValues({}, OPENAI_BASE), {
4707
- chatPayloadTemplate: '{"model":"{{model}}","messages":{{messages}},"max_tokens":{{maxTokens}},"temperature":{{temperature}},"stream":false}'
4708
- }),
4067
+ "litellm": __spreadValues({}, OPENAI_BASE),
4709
4068
  "anthropic-claude": {
4710
4069
  chatPath: "/v1/messages",
4711
4070
  responseExtractPath: "content[0].text",
@@ -4849,20 +4208,8 @@ ${context != null ? context : "None"}` },
4849
4208
  return String(result2);
4850
4209
  }
4851
4210
  } catch (httpErr) {
4852
- console.warn(`[UniversalLLMAdapter] Direct HTTP POST to ${this.baseUrl}${path2} failed (${httpErr.message}). Attempting in-process gateway dispatch fallback...`);
4853
4211
  const _g2 = globalThis;
4854
- let dispatch = _g2.__retrivoraDispatchChat;
4855
- if (typeof dispatch !== "function") {
4856
- try {
4857
- const gateway = await Promise.resolve().then(() => (init_router(), router_exports));
4858
- if (gateway == null ? void 0 : gateway.dispatchChatCompletion) {
4859
- _g2.__retrivoraDispatchChat = gateway.dispatchChatCompletion;
4860
- _g2.__retrivoraDispatchEmbedding = gateway.dispatchEmbedding;
4861
- dispatch = gateway.dispatchChatCompletion;
4862
- }
4863
- } catch (e) {
4864
- }
4865
- }
4212
+ const dispatch = _g2.__retrivoraDispatchChat;
4866
4213
  if (typeof dispatch === "function") {
4867
4214
  const res = await dispatch({
4868
4215
  model: this.model,
@@ -4888,11 +4235,10 @@ ${context != null ? context : "None"}` },
4888
4235
  /**
4889
4236
  * Streaming chat using native fetch + ReadableStream.
4890
4237
  * Parses OpenAI-compatible SSE frames: `data: {...}\n\n`
4891
- * Works with vLLM, LMStudio, Together AI, Fireworks, and any OpenAI-compatible API.
4892
4238
  */
4893
4239
  chatStream(messages, context) {
4894
4240
  return __asyncGenerator(this, null, function* () {
4895
- var _a2, _b, _c, _d, _e;
4241
+ var _a2, _b, _c, _d, _e, _f;
4896
4242
  const path2 = (_a2 = this.opts.chatPath) != null ? _a2 : "/chat/completions";
4897
4243
  const url = `${this.baseUrl.replace(/\/$/, "")}${path2}`;
4898
4244
  const extractPath = ((_b = this.opts.responseExtractPath) != null ? _b : "choices[0].message.content").replace("message.content", "delta.content");
@@ -4927,22 +4273,35 @@ ${context != null ? context : "None"}` },
4927
4273
  stream: true
4928
4274
  };
4929
4275
  }
4930
- const isSelfHost = this.baseUrl.includes("retrivora.com") || this.baseUrl.includes("localhost");
4931
4276
  let streamBody = null;
4932
- if (isSelfHost || Boolean(process.env.VERCEL)) {
4933
- const _g2 = globalThis;
4934
- let dispatch = _g2.__retrivoraDispatchChat;
4935
- if (typeof dispatch !== "function") {
4936
- try {
4937
- const gateway = yield new __await(Promise.resolve().then(() => (init_router(), router_exports)));
4938
- if (gateway == null ? void 0 : gateway.dispatchChatCompletion) {
4939
- _g2.__retrivoraDispatchChat = gateway.dispatchChatCompletion;
4940
- _g2.__retrivoraDispatchEmbedding = gateway.dispatchEmbedding;
4941
- dispatch = gateway.dispatchChatCompletion;
4277
+ try {
4278
+ const response = yield new __await(fetch(url, {
4279
+ method: "POST",
4280
+ headers: this.resolvedHeaders,
4281
+ body: typeof payload === "string" ? payload : JSON.stringify(payload)
4282
+ }));
4283
+ if (response.ok) {
4284
+ const contentType = response.headers.get("content-type") || "";
4285
+ if (contentType.includes("application/json")) {
4286
+ const json = yield new __await(response.json());
4287
+ const text = (_c = resolvePath(json, extractPath)) != null ? _c : extractContent(json);
4288
+ if (text && typeof text === "string") {
4289
+ yield text;
4290
+ return;
4942
4291
  }
4943
- } catch (e) {
4292
+ } else if (response.body) {
4293
+ streamBody = response.body;
4944
4294
  }
4295
+ } else {
4296
+ const errorText = yield new __await(response.text().catch(() => response.statusText));
4297
+ console.warn(`[UniversalLLMAdapter] Remote HTTP stream returned ${response.status}: ${errorText}. Attempting in-process fallback...`);
4945
4298
  }
4299
+ } catch (fetchErr) {
4300
+ console.warn(`[UniversalLLMAdapter] Remote HTTP stream fetch warning: ${fetchErr == null ? void 0 : fetchErr.message}. Attempting in-process fallback...`);
4301
+ }
4302
+ if (!streamBody) {
4303
+ const _g2 = globalThis;
4304
+ const dispatch = _g2.__retrivoraDispatchChat;
4946
4305
  if (typeof dispatch === "function") {
4947
4306
  try {
4948
4307
  const res = yield new __await(dispatch({
@@ -4960,29 +4319,14 @@ ${context != null ? context : "None"}` },
4960
4319
  yield content;
4961
4320
  return;
4962
4321
  }
4963
- throw new Error(`[UniversalLLMAdapter] In-process dispatch stream returned empty response for model: ${this.model}`);
4964
4322
  }
4965
4323
  } catch (dispatchErr) {
4966
- throw dispatchErr;
4324
+ console.warn("[UniversalLLMAdapter] In-process dispatch error:", dispatchErr);
4967
4325
  }
4968
- } else {
4969
- throw new Error(`[UniversalLLMAdapter] In-process gateway dispatch not registered. Direct self-referential HTTP calls to ${this.baseUrl} are disabled on Vercel to prevent HTTP 508 Loop Detected.`);
4970
4326
  }
4971
4327
  }
4972
4328
  if (!streamBody) {
4973
- const response = yield new __await(fetch(url, {
4974
- method: "POST",
4975
- headers: this.resolvedHeaders,
4976
- body: JSON.stringify(payload)
4977
- }));
4978
- if (!response.ok) {
4979
- const errorText = yield new __await(response.text().catch(() => response.statusText));
4980
- throw new Error(`[UniversalLLMAdapter] Streaming request failed (${response.status}): ${errorText}`);
4981
- }
4982
- if (!response.body) {
4983
- throw new Error("[UniversalLLMAdapter] Response body is null \u2014 server did not send a streaming response.");
4984
- }
4985
- streamBody = response.body;
4329
+ throw new Error(`[UniversalLLMAdapter] Streaming request failed. Ensure RETRIVORA_LICENSE_KEY is valid or LLM service is available.`);
4986
4330
  }
4987
4331
  const reader = streamBody.getReader();
4988
4332
  const decoder = new TextDecoder("utf-8");
@@ -4993,14 +4337,14 @@ ${context != null ? context : "None"}` },
4993
4337
  if (done) break;
4994
4338
  buffer += decoder.decode(value, { stream: true });
4995
4339
  const lines = buffer.split("\n");
4996
- buffer = (_c = lines.pop()) != null ? _c : "";
4340
+ buffer = (_d = lines.pop()) != null ? _d : "";
4997
4341
  for (const line of lines) {
4998
4342
  const trimmed = line.trim();
4999
4343
  if (!trimmed || trimmed === "data: [DONE]") continue;
5000
4344
  if (!trimmed.startsWith("data:")) continue;
5001
4345
  try {
5002
4346
  const json = JSON.parse(trimmed.slice(5).trim());
5003
- const text = (_d = resolvePath(json, extractPath)) != null ? _d : extractContent(json);
4347
+ const text = (_e = resolvePath(json, extractPath)) != null ? _e : extractContent(json);
5004
4348
  if (text && typeof text === "string") yield text;
5005
4349
  } catch (e) {
5006
4350
  }
@@ -5010,7 +4354,7 @@ ${context != null ? context : "None"}` },
5010
4354
  const jsonStr = buffer.replace(/^data:\s*/, "").trim();
5011
4355
  try {
5012
4356
  const json = JSON.parse(jsonStr);
5013
- const text = (_e = resolvePath(json, extractPath)) != null ? _e : extractContent(json);
4357
+ const text = (_f = resolvePath(json, extractPath)) != null ? _f : extractContent(json);
5014
4358
  if (text && typeof text === "string") yield text;
5015
4359
  } catch (e) {
5016
4360
  }
@@ -5023,74 +4367,42 @@ ${context != null ? context : "None"}` },
5023
4367
  async embed(text) {
5024
4368
  var _a2, _b, _c, _d, _e;
5025
4369
  const path2 = (_a2 = this.opts.embedPath) != null ? _a2 : "/embeddings";
5026
- let payload;
5027
- if (this.opts.embedPayloadTemplate) {
5028
- payload = buildPayload(this.opts.embedPayloadTemplate, {
5029
- model: this.model,
5030
- input: text
5031
- });
5032
- } else {
5033
- payload = {
5034
- model: this.model,
5035
- input: text
5036
- };
5037
- }
4370
+ const payload = this.opts.embedPayloadTemplate ? buildPayload(this.opts.embedPayloadTemplate, { input: text, model: this.model }) : { input: text, model: this.model };
5038
4371
  try {
5039
4372
  const { data: data2 } = await this.http.post(path2, payload);
5040
4373
  const extractPath2 = (_b = this.opts.embedExtractPath) != null ? _b : "data[0].embedding";
5041
4374
  const vector2 = resolvePath(data2, extractPath2);
5042
- if (Array.isArray(vector2)) {
5043
- return vector2;
5044
- }
5045
- } catch (httpErr) {
5046
- console.warn(`[UniversalLLMAdapter] Direct HTTP embedding POST to ${this.baseUrl}${path2} failed (${httpErr.message}). Attempting in-process gateway dispatch fallback...`);
4375
+ if (Array.isArray(vector2)) return vector2;
4376
+ } catch (e) {
5047
4377
  const _g2 = globalThis;
5048
- let dispatch = _g2.__retrivoraDispatchEmbedding;
5049
- if (typeof dispatch !== "function") {
5050
- try {
5051
- const gateway = await Promise.resolve().then(() => (init_router(), router_exports));
5052
- if (gateway == null ? void 0 : gateway.dispatchEmbedding) {
5053
- _g2.__retrivoraDispatchChat = gateway.dispatchChatCompletion;
5054
- _g2.__retrivoraDispatchEmbedding = gateway.dispatchEmbedding;
5055
- dispatch = gateway.dispatchEmbedding;
5056
- }
5057
- } catch (e) {
5058
- }
5059
- }
5060
- if (typeof dispatch === "function") {
5061
- const res = await dispatch({
5062
- model: this.model,
5063
- input: text
5064
- }, this.apiKey);
4378
+ const dispatchEmbed = _g2.__retrivoraDispatchEmbedding;
4379
+ if (typeof dispatchEmbed === "function") {
4380
+ const res = await dispatchEmbed({ input: text, model: this.model }, this.apiKey);
5065
4381
  if ((_d = (_c = res == null ? void 0 : res.data) == null ? void 0 : _c[0]) == null ? void 0 : _d.embedding) {
5066
4382
  return res.data[0].embedding;
5067
4383
  }
5068
4384
  }
5069
- throw httpErr;
5070
4385
  }
5071
4386
  const { data } = await this.http.post(path2, payload);
5072
4387
  const extractPath = (_e = this.opts.embedExtractPath) != null ? _e : "data[0].embedding";
5073
4388
  const vector = resolvePath(data, extractPath);
5074
4389
  if (!Array.isArray(vector)) {
5075
- throw new Error(`[UniversalLLMAdapter] Expected a number array at '${extractPath}' for embeddings.`);
4390
+ throw new Error(`[UniversalLLMAdapter] Could not extract embedding vector from path '${extractPath}' in response.`);
5076
4391
  }
5077
4392
  return vector;
5078
4393
  }
5079
4394
  async batchEmbed(texts) {
5080
- const vectors = [];
4395
+ const results = [];
5081
4396
  for (const text of texts) {
5082
- vectors.push(await this.embed(text));
4397
+ results.push(await this.embed(text));
5083
4398
  }
5084
- return vectors;
4399
+ return results;
5085
4400
  }
5086
4401
  async ping() {
5087
4402
  try {
5088
- if (this.opts.pingPath) {
5089
- await this.http.get(this.opts.pingPath);
5090
- }
4403
+ await this.embed("ping");
5091
4404
  return true;
5092
- } catch (err) {
5093
- console.error("[UniversalLLMAdapter] Ping failed:", err);
4405
+ } catch (e) {
5094
4406
  return false;
5095
4407
  }
5096
4408
  }
@@ -5496,7 +4808,7 @@ var ConfigValidator = class {
5496
4808
  // package.json
5497
4809
  var package_default = {
5498
4810
  name: "@retrivora-ai/rag-engine",
5499
- version: "2.2.3",
4811
+ version: "2.2.5",
5500
4812
  description: "Retrivora AI is a plug-and-play AI engine for RAG chat experiences \u2014 generic vector DB + LLM provider, embeddable or standalone.",
5501
4813
  author: "Abhinav Alkuchi",
5502
4814
  license: "UNLICENSED",
@@ -5533,6 +4845,8 @@ var package_default = {
5533
4845
  import: "./dist/index.mjs"
5534
4846
  },
5535
4847
  "./style.css": "./dist/index.css",
4848
+ "./index.css": "./dist/index.css",
4849
+ "./styles.css": "./dist/index.css",
5536
4850
  "./handlers": {
5537
4851
  types: "./dist/handlers/index.d.ts",
5538
4852
  require: "./dist/handlers/index.js",
@@ -7715,19 +7029,6 @@ var UITransformer = class _UITransformer {
7715
7029
  if (!retrievedData || retrievedData.length === 0) {
7716
7030
  return this.createTextResponse("No data available", "No relevant data found for your query.");
7717
7031
  }
7718
- console.log("[UITransformer.transform] Processing retrievedData:", {
7719
- userQuery,
7720
- retrievedCount: retrievedData.length,
7721
- sample: retrievedData.slice(0, 2).map((item) => {
7722
- var _a3;
7723
- return {
7724
- id: item == null ? void 0 : item.id,
7725
- contentType: typeof (item == null ? void 0 : item.content),
7726
- contentSnippet: String((_a3 = item == null ? void 0 : item.content) != null ? _a3 : "").substring(0, 150),
7727
- metadataKeys: Object.keys((item == null ? void 0 : item.metadata) || {})
7728
- };
7729
- })
7730
- });
7731
7032
  const resolvedIntent = intent != null ? intent : this.detectIntentHeuristic(userQuery);
7732
7033
  const filteredData = resolvedIntent.filterInStockOnly ? retrievedData.filter((item) => this.determineStockStatus(item)) : retrievedData;
7733
7034
  const profile = this.profileData(filteredData);
@@ -9067,59 +8368,70 @@ RULES:
9067
8368
  var SchemaMapper = class {
9068
8369
  /**
9069
8370
  * Trains the plugin on a set of keys.
9070
- * This is done once per schema and cached.
8371
+ * Results are cached in-process; concurrent calls for the same key set are
8372
+ * deduplicated to a single LLM request.
9071
8373
  */
9072
8374
  static async train(llm, projectId, keys) {
9073
- const cacheKey = `${projectId}:${keys.sort().join(",")}`;
8375
+ const cacheKey = `${projectId}:${keys.slice().sort().join(",")}`;
9074
8376
  if (this.cache.has(cacheKey)) {
9075
8377
  return this.cache.get(cacheKey);
9076
8378
  }
9077
- console.log(`[SchemaMapper] \u{1F9E0} Training AI on new schema keys: ${keys.join(", ")}`);
8379
+ if (this.pending.has(cacheKey)) {
8380
+ return this.pending.get(cacheKey);
8381
+ }
8382
+ const promise = this._doTrain(llm, cacheKey, keys);
8383
+ this.pending.set(cacheKey, promise);
8384
+ promise.finally(() => this.pending.delete(cacheKey));
8385
+ return promise;
8386
+ }
8387
+ static async _doTrain(llm, cacheKey, keys) {
8388
+ var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k;
8389
+ console.log(`[SchemaMapper] \u{1F9E0} Training on new schema keys: ${keys.join(", ")}`);
9078
8390
  const propertyList = Object.entries(this.TARGET_PROPERTIES).map(([prop, desc]) => `- ${prop} (${desc})`).join("\n");
9079
- const prompt = `
9080
- Given these metadata keys from a database: [${keys.join(", ")}]
8391
+ const messages = [
8392
+ { role: "system", content: "You are a database schema expert. Respond ONLY with valid JSON." },
8393
+ {
8394
+ role: "user",
8395
+ content: `Keys: [${keys.join(", ")}]
9081
8396
 
9082
- Identify which keys best correspond to these standard UI properties:
8397
+ Map to:
9083
8398
  ${propertyList}
9084
8399
 
9085
- Return ONLY a valid JSON object where the keys are the UI properties and the values are the EXACT matching database keys from the list above.
9086
- If no good match is found for a property, omit it.
9087
-
9088
- Example:
9089
- {
9090
- "name": "Title",
9091
- "price": "Variant Price",
9092
- "brand": "Vendor",
9093
- "image": "Image Src",
9094
- "stock": "Variant Inventory Qty"
9095
- }
9096
- `;
8400
+ Return a JSON object like {"name":"Title","price":"Price",...}. Omit unmapped properties.`
8401
+ }
8402
+ ];
9097
8403
  try {
9098
- const response = await llm.chat(
9099
- [
9100
- { role: "system", content: "You are a database schema expert. You ONLY respond with valid JSON." },
9101
- { role: "user", content: prompt }
9102
- ],
9103
- ""
9104
- );
9105
- const startIdx = response.indexOf("{");
9106
- if (startIdx !== -1) {
9107
- let braceCount = 0;
9108
- let endIdx = -1;
9109
- for (let i = startIdx; i < response.length; i++) {
9110
- if (response[i] === "{") braceCount++;
9111
- else if (response[i] === "}") {
9112
- braceCount--;
9113
- if (braceCount === 0) {
9114
- endIdx = i;
9115
- break;
9116
- }
9117
- }
8404
+ const baseUrl = (_a2 = llm == null ? void 0 : llm.baseUrl) != null ? _a2 : "";
8405
+ const apiKey = (_b = llm == null ? void 0 : llm.apiKey) != null ? _b : "";
8406
+ const model = (_c = llm == null ? void 0 : llm.model) != null ? _c : "llama-3.1-8b-instant";
8407
+ let responseText;
8408
+ if (baseUrl) {
8409
+ const endpoint = `${baseUrl.replace(/\/$/, "")}/chat/completions`;
8410
+ const headers = { "Content-Type": "application/json" };
8411
+ if (apiKey) headers["Authorization"] = `Bearer ${apiKey}`;
8412
+ const res = await fetch(endpoint, {
8413
+ method: "POST",
8414
+ headers,
8415
+ body: JSON.stringify({
8416
+ model,
8417
+ messages,
8418
+ max_tokens: 256,
8419
+ // Only needs a small JSON object back
8420
+ temperature: 0,
8421
+ stream: false
8422
+ }),
8423
+ signal: AbortSignal.timeout(8e3)
8424
+ });
8425
+ if (res.ok) {
8426
+ const data = await res.json();
8427
+ responseText = (_k = (_j = (_f = (_e = (_d = data == null ? void 0 : data.choices) == null ? void 0 : _d[0]) == null ? void 0 : _e.message) == null ? void 0 : _f.content) != null ? _j : (_i = (_h = (_g2 = data == null ? void 0 : data.choices) == null ? void 0 : _g2[0]) == null ? void 0 : _h.delta) == null ? void 0 : _i.content) != null ? _k : void 0;
8428
+ } else {
8429
+ console.warn(`[SchemaMapper] LLM returned ${res.status}, falling back to heuristics.`);
9118
8430
  }
9119
- if (endIdx !== -1) {
9120
- const jsonContent = response.substring(startIdx, endIdx + 1);
9121
- const cleanJson = this.sanitizeJson(jsonContent);
9122
- const mapping = JSON.parse(cleanJson);
8431
+ }
8432
+ if (responseText) {
8433
+ const mapping = this._parseJson(responseText);
8434
+ if (mapping) {
9123
8435
  this.cache.set(cacheKey, mapping);
9124
8436
  return mapping;
9125
8437
  }
@@ -9127,23 +8439,42 @@ Example:
9127
8439
  } catch (error) {
9128
8440
  console.warn("[SchemaMapper] AI training failed, falling back to heuristics:", error);
9129
8441
  }
8442
+ this.cache.set(cacheKey, {});
9130
8443
  return {};
9131
8444
  }
9132
- /**
9133
- * Forgiving JSON parser that fixes common AI formatting mistakes.
9134
- */
9135
- static sanitizeJson(s) {
8445
+ static _parseJson(text) {
8446
+ const startIdx = text.indexOf("{");
8447
+ if (startIdx === -1) return null;
8448
+ let braceCount = 0;
8449
+ let endIdx = -1;
8450
+ for (let i = startIdx; i < text.length; i++) {
8451
+ if (text[i] === "{") braceCount++;
8452
+ else if (text[i] === "}") {
8453
+ braceCount--;
8454
+ if (braceCount === 0) {
8455
+ endIdx = i;
8456
+ break;
8457
+ }
8458
+ }
8459
+ }
8460
+ if (endIdx === -1) return null;
8461
+ try {
8462
+ return JSON.parse(this._sanitizeJson(text.substring(startIdx, endIdx + 1)));
8463
+ } catch (e) {
8464
+ return null;
8465
+ }
8466
+ }
8467
+ static _sanitizeJson(s) {
9136
8468
  return s.replace(/\/\*[\s\S]*?\*\/|([^:]|^)\/\/.*$/gm, "").replace(/[\u201C\u201D\u2018\u2019]/g, '"').replace(/'/g, '"').replace(/([{,]\s*)([a-zA-Z0-9_]+?)\s*:/g, '$1"$2":').replace(/,\s*([}\]])/g, "$1").trim();
9137
8469
  }
9138
8470
  static getCached(projectId, keys) {
9139
- const cacheKey = `${projectId}:${keys.sort().join(",")}`;
8471
+ const cacheKey = `${projectId}:${keys.slice().sort().join(",")}`;
9140
8472
  return this.cache.get(cacheKey);
9141
8473
  }
9142
8474
  };
9143
8475
  SchemaMapper.cache = /* @__PURE__ */ new Map();
9144
- /**
9145
- * Descriptions of standard UI properties to help the AI map fields accurately.
9146
- */
8476
+ /** In-flight deduplication — prevents concurrent identical LLM calls. */
8477
+ SchemaMapper.pending = /* @__PURE__ */ new Map();
9147
8478
  SchemaMapper.TARGET_PROPERTIES = {
9148
8479
  name: "The primary title, name, or label of the item",
9149
8480
  price: "The numeric cost, price, MSRP, or amount",
@@ -9615,19 +8946,6 @@ ${m.content}`).join("\n\n---\n\n");
9615
8946
  const wantsExhaustiveList = true;
9616
8947
  const retrievalLimit = Math.max(topK * 50, 1e3);
9617
8948
  const rawSources = ((strategyResult === "vector" || strategyResult === "both") && queryVector && queryVector.length > 0 ? yield new __await(this.vectorDB.query(queryVector, retrievalLimit, ns, filter)) : []).filter((s) => Boolean(s && typeof s === "object"));
9618
- console.log("[Pipeline] Raw vectorDB.query response:", {
9619
- rawCount: rawSources.length,
9620
- sample: rawSources.slice(0, 2).map((s) => {
9621
- var _a3;
9622
- return {
9623
- id: s == null ? void 0 : s.id,
9624
- score: s == null ? void 0 : s.score,
9625
- contentType: typeof (s == null ? void 0 : s.content),
9626
- contentSnippet: String((_a3 = s == null ? void 0 : s.content) != null ? _a3 : "").substring(0, 150),
9627
- metadataKeys: Object.keys((s == null ? void 0 : s.metadata) || {})
9628
- };
9629
- })
9630
- });
9631
8949
  const retrieveEnd = performance.now();
9632
8950
  const embedMs = retrieveEnd - embedStart;
9633
8951
  const retrieveMs = retrieveEnd - embedStart;
@@ -9656,21 +8974,6 @@ ${(_a3 = m == null ? void 0 : m.content) != null ? _a3 : ""}`;
9656
8974
  var _a3, _b2;
9657
8975
  return ((_a3 = b == null ? void 0 : b.score) != null ? _a3 : 0) - ((_b2 = a == null ? void 0 : a.score) != null ? _b2 : 0);
9658
8976
  });
9659
- console.log("[Pipeline] Final sources for prompt & UI:", {
9660
- count: sources.length,
9661
- totalRawCount: rawSources.length,
9662
- sources: sources.slice(0, 10).map((s, idx) => {
9663
- var _a3;
9664
- return {
9665
- rank: idx + 1,
9666
- id: s == null ? void 0 : s.id,
9667
- score: s == null ? void 0 : s.score,
9668
- contentType: typeof (s == null ? void 0 : s.content),
9669
- contentSnippet: String((_a3 = s == null ? void 0 : s.content) != null ? _a3 : "").substring(0, 150),
9670
- metadata: s == null ? void 0 : s.metadata
9671
- };
9672
- })
9673
- });
9674
8977
  if (graphData && graphData.nodes.length > 0) {
9675
8978
  const graphContext = graphData.nodes.map(
9676
8979
  (n) => `Entity: ${n.label} (${n.id})${n.properties ? " - " + JSON.stringify(n.properties) : ""}`
@@ -9912,9 +9215,9 @@ ${context}`;
9912
9215
  const trace = buildTrace(hallucinationResult);
9913
9216
  const isTelemetryActive = (_A = (_z = this.config.telemetry) == null ? void 0 : _z.enabled) != null ? _A : Boolean(this.config.licenseKey);
9914
9217
  if (isTelemetryActive) {
9915
- const defaultUrl = process.env.NODE_ENV === "development" && !process.env.TELEMETRY_URL && !process.env.NEXT_PUBLIC_TELEMETRY_URL ? "http://localhost:3001/api/telemetry" : "https://retrivora.com/api/telemetry";
9218
+ const defaultUrl = "https://www.retrivora.com/api/telemetry";
9916
9219
  const telemetryUrl = ((_B = this.config.telemetry) == null ? void 0 : _B.url) || process.env.TELEMETRY_URL || process.env.NEXT_PUBLIC_TELEMETRY_URL || defaultUrl;
9917
- const absoluteUrl = telemetryUrl.startsWith("http") ? telemetryUrl : (process.env.NEXT_PUBLIC_APP_URL || `http://localhost:${process.env.PORT || 3e3}`) + telemetryUrl;
9220
+ const absoluteUrl = telemetryUrl.startsWith("http") ? telemetryUrl : "https://www.retrivora.com" + (telemetryUrl.startsWith("/") ? telemetryUrl : "/" + telemetryUrl);
9918
9221
  (async () => {
9919
9222
  var _a3, _b2, _c2, _d2, _e2;
9920
9223
  try {
@@ -9930,7 +9233,7 @@ ${context}`;
9930
9233
  const tokenCount = Number(((_c2 = finalTrace == null ? void 0 : finalTrace.tokens) == null ? void 0 : _c2.totalTokens) || 0);
9931
9234
  const costEst = Number(((_d2 = finalTrace == null ? void 0 : finalTrace.tokens) == null ? void 0 : _d2.estimatedCostUsd) || 0);
9932
9235
  const latencyDuration = Number(((_e2 = finalTrace == null ? void 0 : finalTrace.latency) == null ? void 0 : _e2.totalMs) || 0);
9933
- console.log(`[Retrivora Pipeline Telemetry] \u{1F4CA} Dispatching RAG telemetry -> Project: "${ns}", Model: "${providerName}/${modelName}", Tokens: ${tokenCount}, Latency: ${latencyDuration}ms`);
9236
+ console.log(`[Retrivora Pipeline Telemetry] \u{1F4CA} Dispatching RAG telemetry -> Project: "${ns}", Tokens: ${tokenCount}, Latency: ${latencyDuration}ms`);
9934
9237
  await fetch(absoluteUrl, {
9935
9238
  method: "POST",
9936
9239
  headers: {
@@ -10534,8 +9837,8 @@ var VectorPlugin = class {
10534
9837
  }
10535
9838
  try {
10536
9839
  return await this.pipeline.getSuggestions(query, namespace);
10537
- } catch (err) {
10538
- throw wrapError(err, "RETRIEVAL_FAILED");
9840
+ } catch (e) {
9841
+ return [];
10539
9842
  }
10540
9843
  }
10541
9844
  };
@@ -11348,15 +10651,14 @@ function getOrCreatePlugin(configOrPlugin) {
11348
10651
  return _g[cacheKey];
11349
10652
  }
11350
10653
  function reportTelemetry(req, plugin, action, status, details, trace) {
11351
- var _a2, _b, _c, _d, _e, _f, _g2, _h;
10654
+ var _a2, _b, _c, _d, _e, _f, _g2;
11352
10655
  try {
11353
10656
  const config = plugin.getConfig();
11354
10657
  const licenseKey = config.licenseKey || process.env.RAG_LICENSE_KEY || process.env.RETRIVORA_LICENSE_KEY || process.env.NEXT_PUBLIC_RETRIVORA_LICENSE_KEY;
11355
10658
  const telemetryConfig = config.telemetry;
11356
- const enabled = (_a2 = telemetryConfig == null ? void 0 : telemetryConfig.enabled) != null ? _a2 : Boolean(licenseKey);
11357
- const defaultUrl = process.env.NODE_ENV === "development" && !process.env.TELEMETRY_URL && !process.env.NEXT_PUBLIC_TELEMETRY_URL ? "http://localhost:3001/api/telemetry" : "https://www.retrivora.com/api/telemetry";
11358
- const telemetryUrl = (telemetryConfig == null ? void 0 : telemetryConfig.url) || process.env.TELEMETRY_URL || process.env.NEXT_PUBLIC_TELEMETRY_URL || defaultUrl;
11359
10659
  const host = req.headers.get("host") || "localhost";
10660
+ const defaultUrl = "https://www.retrivora.com/api/telemetry";
10661
+ const telemetryUrl = (telemetryConfig == null ? void 0 : telemetryConfig.url) || process.env.TELEMETRY_URL || process.env.NEXT_PUBLIC_TELEMETRY_URL || defaultUrl;
11360
10662
  const userAgent = req.headers.get("user-agent") || `Retrivora-SDK/${SDK_VERSION}`;
11361
10663
  let absoluteUrl = telemetryUrl;
11362
10664
  if (!telemetryUrl.startsWith("http")) {
@@ -11364,11 +10666,11 @@ function reportTelemetry(req, plugin, action, status, details, trace) {
11364
10666
  absoluteUrl = `${proto}://${host}${telemetryUrl}`;
11365
10667
  }
11366
10668
  const projectId = config.projectId || "default";
11367
- const model = (trace == null ? void 0 : trace.model) || ((_b = config.llm) == null ? void 0 : _b.model) || ((_c = config.embedding) == null ? void 0 : _c.model) || "llama-3.1-8b-instant";
11368
- const provider = (trace == null ? void 0 : trace.provider) || ((_d = config.llm) == null ? void 0 : _d.provider) || ((_e = config.embedding) == null ? void 0 : _e.provider) || "groq";
11369
- const tokens = Number(((_f = trace == null ? void 0 : trace.tokens) == null ? void 0 : _f.totalTokens) || (trace == null ? void 0 : trace.totalTokens) || 0);
11370
- const costUsd = Number(((_g2 = trace == null ? void 0 : trace.tokens) == null ? void 0 : _g2.estimatedCostUsd) || (trace == null ? void 0 : trace.costUsd) || 0);
11371
- const latencyMs = Number(((_h = trace == null ? void 0 : trace.latency) == null ? void 0 : _h.totalMs) || (trace == null ? void 0 : trace.latencyMs) || 0);
10669
+ const model = (trace == null ? void 0 : trace.model) || ((_a2 = config.llm) == null ? void 0 : _a2.model) || ((_b = config.embedding) == null ? void 0 : _b.model) || "llama-3.1-8b-instant";
10670
+ const provider = (trace == null ? void 0 : trace.provider) || ((_c = config.llm) == null ? void 0 : _c.provider) || ((_d = config.embedding) == null ? void 0 : _d.provider) || "groq";
10671
+ const tokens = Number(((_e = trace == null ? void 0 : trace.tokens) == null ? void 0 : _e.totalTokens) || (trace == null ? void 0 : trace.totalTokens) || 0);
10672
+ const costUsd = Number(((_f = trace == null ? void 0 : trace.tokens) == null ? void 0 : _f.estimatedCostUsd) || (trace == null ? void 0 : trace.costUsd) || 0);
10673
+ const latencyMs = Number(((_g2 = trace == null ? void 0 : trace.latency) == null ? void 0 : _g2.totalMs) || (trace == null ? void 0 : trace.latencyMs) || 0);
11372
10674
  const payload = {
11373
10675
  trace,
11374
10676
  licenseKey,
@@ -11388,7 +10690,7 @@ function reportTelemetry(req, plugin, action, status, details, trace) {
11388
10690
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
11389
10691
  details
11390
10692
  };
11391
- console.log(`[Retrivora SDK Telemetry] \u{1F4CA} Dispatching telemetry -> Feature: "${action}", Status: "${status}", Project: "${projectId}", Model: "${provider}/${model}", Latency: ${latencyMs}ms`);
10693
+ console.log(`[Retrivora SDK Telemetry] Dispatching telemetry -> Feature: "${action}", Status: "${status}", Project: "${projectId}", Latency: ${latencyMs}ms`);
11392
10694
  fetch(absoluteUrl, {
11393
10695
  method: "POST",
11394
10696
  headers: {
@@ -11445,7 +10747,7 @@ function createChatHandler(configOrPlugin, options) {
11445
10747
  const plugin = getOrCreatePlugin(configOrPlugin);
11446
10748
  const storage = new DatabaseStorage(plugin.getConfig());
11447
10749
  const onAuthorize = options == null ? void 0 : options.onAuthorize;
11448
- return async function POST(req) {
10750
+ return async function POST(req, context) {
11449
10751
  var _a2, _b, _c, _d;
11450
10752
  const authResult = await checkAuth(req, onAuthorize);
11451
10753
  if (authResult) return authResult;
@@ -11501,7 +10803,7 @@ function createStreamHandler(configOrPlugin, options) {
11501
10803
  const plugin = getOrCreatePlugin(configOrPlugin);
11502
10804
  const storage = new DatabaseStorage(plugin.getConfig());
11503
10805
  const onAuthorize = options == null ? void 0 : options.onAuthorize;
11504
- return async function POST(req) {
10806
+ return async function POST(req, context) {
11505
10807
  var _a2;
11506
10808
  const authResult = await checkAuth(req, onAuthorize);
11507
10809
  if (authResult) return authResult;
@@ -11651,7 +10953,7 @@ function createStreamHandler(configOrPlugin, options) {
11651
10953
  function createIngestHandler(configOrPlugin, options) {
11652
10954
  const plugin = getOrCreatePlugin(configOrPlugin);
11653
10955
  const onAuthorize = options == null ? void 0 : options.onAuthorize;
11654
- return async function POST(req) {
10956
+ return async function POST(req, context) {
11655
10957
  const authResult = await checkAuth(req, onAuthorize);
11656
10958
  if (authResult) return authResult;
11657
10959
  try {
@@ -11695,7 +10997,7 @@ function createHealthHandler(configOrPlugin, options) {
11695
10997
  function createUploadHandler(configOrPlugin, options) {
11696
10998
  const plugin = getOrCreatePlugin(configOrPlugin);
11697
10999
  const onAuthorize = options == null ? void 0 : options.onAuthorize;
11698
- return async function POST(req) {
11000
+ return async function POST(req, context) {
11699
11001
  const authResult = await checkAuth(req, onAuthorize);
11700
11002
  if (authResult) return authResult;
11701
11003
  try {
@@ -11828,8 +11130,8 @@ function createSuggestionsHandler(configOrPlugin, options) {
11828
11130
  query = url.searchParams.get("query") || "";
11829
11131
  namespace = url.searchParams.get("namespace") || void 0;
11830
11132
  }
11831
- if (typeof query !== "string") {
11832
- return import_server.NextResponse.json({ error: "query is required" }, { status: 400 });
11133
+ if (!query || typeof query !== "string" || !query.trim()) {
11134
+ return import_server.NextResponse.json({ suggestions: [] });
11833
11135
  }
11834
11136
  const suggestions = await plugin.getSuggestions(query, namespace);
11835
11137
  reportTelemetry(req, plugin, "AUTO_SUGGESTIONS", "success", `Fetched ${suggestions.length} suggestions for: ${query.slice(0, 30)}`);