@retrivora-ai/rag-engine 2.1.1 → 2.1.3

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.
@@ -591,26 +591,54 @@ function cleanApiKeyOverride(apiKeyOverride) {
591
591
  }
592
592
  return key;
593
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
+ }
594
620
  async function dispatchChatCompletion(req, apiKeyOverride) {
595
- const effectiveKey = cleanApiKeyOverride(apiKeyOverride);
596
- const provider = resolveProvider(req.model);
597
- console.log(`[LLM Gateway Router] Dispatching chat request: model=${req.model}, provider=${provider}, hasGroqKey=${Boolean(process.env.GROQ_API_KEY)}, hasGeminiKey=${Boolean(process.env.GROQ_API_KEY || process.env.GEMINI_API_KEY)}, hasHFToken=${Boolean(process.env.HF_TOKEN)}`);
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)}`);
598
626
  try {
599
627
  switch (provider) {
600
628
  case "groq":
601
- return await handleGroqRequest(req, effectiveKey);
629
+ return await handleGroqRequest(__spreadProps(__spreadValues({}, req), { model: activeModel }), effectiveKey);
602
630
  case "openai":
603
- return await handleOpenAIRequest(req, effectiveKey);
631
+ return await handleOpenAIRequest(__spreadProps(__spreadValues({}, req), { model: activeModel }), effectiveKey);
604
632
  case "gemini":
605
- return await handleGeminiRequest(req, effectiveKey);
633
+ return await handleGeminiRequest(__spreadProps(__spreadValues({}, req), { model: activeModel }), effectiveKey);
606
634
  case "anthropic":
607
- return await handleAnthropicRequest(req, effectiveKey);
635
+ return await handleAnthropicRequest(__spreadProps(__spreadValues({}, req), { model: activeModel }), effectiveKey);
608
636
  case "ollama":
609
- return await handleOllamaRequest(req);
637
+ return await handleOllamaRequest(__spreadProps(__spreadValues({}, req), { model: activeModel }));
610
638
  case "huggingface":
611
- return await handleHuggingFaceChatRequest(req, effectiveKey);
639
+ return await handleHuggingFaceChatRequest(__spreadProps(__spreadValues({}, req), { model: activeModel }), effectiveKey);
612
640
  default:
613
- throw new Error(`Unsupported LLM provider for model: ${req.model}`);
641
+ throw new Error(`Unsupported LLM provider for model: ${activeModel}`);
614
642
  }
615
643
  } catch (error) {
616
644
  console.error(`[LLM Gateway Router] Provider '${provider}' failed for model '${req.model}':`, {
@@ -3052,7 +3080,7 @@ function readEnum(env, name, fallback, allowed) {
3052
3080
  throw new Error(`[getRagConfig] ${name} must be one of: ${allowed.join(", ")}`);
3053
3081
  }
3054
3082
  function getEnvConfig(env = process.env, base) {
3055
- var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _A, _B, _C, _D, _E, _F, _G, _H, _I, _J, _K, _L, _M, _N, _O, _P, _Q, _R, _S, _T, _U, _V, _W, _X, _Y, _Z, __, _$, _aa, _ba, _ca, _da, _ea, _fa, _ga, _ha, _ia, _ja, _ka, _la, _ma, _na, _oa, _pa, _qa, _ra, _sa, _ta, _ua, _va, _wa, _xa, _ya, _za, _Aa, _Ba, _Ca, _Da, _Ea, _Fa, _Ga, _Ha, _Ia, _Ja, _Ka, _La, _Ma, _Na, _Oa, _Pa, _Qa, _Ra, _Sa, _Ta, _Ua, _Va, _Wa, _Xa, _Ya, _Za, __a, _$a, _ab, _bb, _cb, _db, _eb, _fb, _gb, _hb, _ib, _jb, _kb, _lb, _mb, _nb, _ob, _pb, _qb, _rb, _sb, _tb, _ub, _vb, _wb;
3083
+ var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _A, _B, _C, _D, _E, _F, _G, _H, _I, _J, _K, _L, _M, _N, _O, _P, _Q, _R, _S, _T, _U, _V, _W, _X, _Y, _Z, __, _$, _aa, _ba, _ca, _da, _ea, _fa, _ga, _ha, _ia, _ja, _ka, _la, _ma, _na, _oa, _pa, _qa, _ra, _sa, _ta, _ua, _va, _wa, _xa, _ya, _za, _Aa, _Ba, _Ca, _Da, _Ea, _Fa, _Ga, _Ha, _Ia, _Ja, _Ka, _La, _Ma, _Na, _Oa, _Pa, _Qa, _Ra, _Sa, _Ta, _Ua, _Va, _Wa, _Xa, _Ya, _Za, __a, _$a, _ab, _bb, _cb, _db, _eb, _fb, _gb, _hb, _ib, _jb, _kb, _lb, _mb, _nb, _ob, _pb, _qb, _rb, _sb, _tb, _ub, _vb, _wb, _xb, _yb, _zb, _Ab, _Bb, _Cb, _Db, _Eb, _Fb;
3056
3084
  const projectId = (_c = (_b = (_a2 = readString(env, "RAG_PROJECT_ID")) != null ? _a2 : readString(env, "NEXT_PUBLIC_PROJECT_ID")) != null ? _b : base == null ? void 0 : base.projectId) != null ? _c : "__default__";
3057
3085
  const licenseKey = (_g2 = (_f = (_e = (_d = readString(env, "RAG_LICENSE_KEY")) != null ? _d : readString(env, "RETRIVORA_LICENSE_KEY")) != null ? _e : readString(env, "NEXT_PUBLIC_RETRIVORA_LICENSE_KEY")) != null ? _f : readString(env, "LICENSE_KEY")) != null ? _g2 : base == null ? void 0 : base.licenseKey;
3058
3086
  const telemetryEnabled = readString(env, "TELEMETRY_ENABLED") === "true" || readString(env, "NEXT_PUBLIC_TELEMETRY_ENABLED") === "true" || ((_h = base == null ? void 0 : base.telemetry) == null ? void 0 : _h.enabled) || Boolean(licenseKey);
@@ -3132,25 +3160,25 @@ function getEnvConfig(env = process.env, base) {
3132
3160
  return true;
3133
3161
  }
3134
3162
  })();
3135
- const DEFAULT_FREE_GATEWAY = "https://llm.retrivora.com/api/v1";
3163
+ const defaultGatewayUrl = (_ma = (_la = readString(env, "LITELLM_BASE_URL")) != null ? _la : readString(env, "LLM_BASE_URL")) != null ? _ma : "https://llm.retrivora.com/api/v1";
3136
3164
  const defaultLlmProvider = isFreeTier ? "universal_rest" : "universal_rest";
3137
- const llmProvider = (_na = (_ma = readEnum(env, "LLM_PROVIDER", defaultLlmProvider, LLM_PROVIDERS)) != null ? _ma : (_la = base == null ? void 0 : base.llm) == null ? void 0 : _la.provider) != null ? _na : defaultLlmProvider;
3138
- const llmBaseUrl = (_qa = (_pa = readString(env, "LLM_BASE_URL")) != null ? _pa : (_oa = base == null ? void 0 : base.llm) == null ? void 0 : _oa.baseUrl) != null ? _qa : DEFAULT_FREE_GATEWAY;
3139
- const llmModel = (_ua = (_sa = readString(env, "LLM_MODEL")) != null ? _sa : (_ra = base == null ? void 0 : base.llm) == null ? void 0 : _ra.model) != null ? _ua : isFreeTier ? "llama-3.1-8b-instant" : (_ta = DEFAULT_MODEL_BY_PROVIDER[llmProvider]) != null ? _ta : "gpt-4o";
3140
- const llmApiKey = (_ya = (_xa = (_va = llmApiKeyByProvider[llmProvider]) != null ? _va : readString(env, "LLM_API_KEY")) != null ? _xa : (_wa = base == null ? void 0 : base.llm) == null ? void 0 : _wa.apiKey) != null ? _ya : "sk-retrivora-cancer-280590";
3141
- const llmProfile = (_Ca = (_Ba = readString(env, "LLM_UNIVERSAL_PROFILE")) != null ? _Ba : (_Aa = (_za = base == null ? void 0 : base.llm) == null ? void 0 : _za.options) == null ? void 0 : _Aa.profile) != null ? _Ca : "litellm";
3165
+ const llmProvider = (_pa = (_oa = readEnum(env, "LLM_PROVIDER", defaultLlmProvider, LLM_PROVIDERS)) != null ? _oa : (_na = base == null ? void 0 : base.llm) == null ? void 0 : _na.provider) != null ? _pa : defaultLlmProvider;
3166
+ const llmBaseUrl = (_ta = (_sa = (_qa = readString(env, "LITELLM_BASE_URL")) != null ? _qa : readString(env, "LLM_BASE_URL")) != null ? _sa : (_ra = base == null ? void 0 : base.llm) == null ? void 0 : _ra.baseUrl) != null ? _ta : defaultGatewayUrl;
3167
+ const llmModel = (_xa = (_va = readString(env, "LLM_MODEL")) != null ? _va : (_ua = base == null ? void 0 : base.llm) == null ? void 0 : _ua.model) != null ? _xa : isFreeTier ? "llama-3.1-8b-instant" : (_wa = DEFAULT_MODEL_BY_PROVIDER[llmProvider]) != null ? _wa : "gpt-4o";
3168
+ const llmApiKey = (_Da = (_Ca = (_Aa = (_za = (_ya = llmApiKeyByProvider[llmProvider]) != null ? _ya : readString(env, "LLM_API_KEY")) != null ? _za : readString(env, "LITELLM_MASTER_KEY")) != null ? _Aa : readString(env, "LITELLM_API_KEY")) != null ? _Ca : (_Ba = base == null ? void 0 : base.llm) == null ? void 0 : _Ba.apiKey) != null ? _Da : licenseKey;
3169
+ const llmProfile = (_Ha = (_Ga = readString(env, "LLM_UNIVERSAL_PROFILE")) != null ? _Ga : (_Fa = (_Ea = base == null ? void 0 : base.llm) == null ? void 0 : _Ea.options) == null ? void 0 : _Fa.profile) != null ? _Ha : "litellm";
3142
3170
  const defaultEmbeddingProvider = isFreeTier ? "universal_rest" : "universal_rest";
3143
- const embeddingProvider = (_Fa = (_Ea = readEnum(env, "EMBEDDING_PROVIDER", defaultEmbeddingProvider, EMBEDDING_PROVIDERS)) != null ? _Ea : (_Da = base == null ? void 0 : base.embedding) == null ? void 0 : _Da.provider) != null ? _Fa : defaultEmbeddingProvider;
3144
- const embeddingBaseUrl = (_Ia = (_Ha = readString(env, "EMBEDDING_BASE_URL")) != null ? _Ha : (_Ga = base == null ? void 0 : base.embedding) == null ? void 0 : _Ga.baseUrl) != null ? _Ia : DEFAULT_FREE_GATEWAY;
3145
- const embeddingModel = (_La = (_Ka = readString(env, "EMBEDDING_MODEL")) != null ? _Ka : (_Ja = base == null ? void 0 : base.embedding) == null ? void 0 : _Ja.model) != null ? _La : "text-embedding-004";
3146
- const embeddingApiKey = (_Qa = (_Pa = (_Na = (_Ma = embeddingApiKeyByProvider[embeddingProvider]) != null ? _Ma : readString(env, "EMBEDDING_API_KEY")) != null ? _Na : readString(env, "LLM_API_KEY")) != null ? _Pa : (_Oa = base == null ? void 0 : base.embedding) == null ? void 0 : _Oa.apiKey) != null ? _Qa : "sk-retrivora-cancer-280590";
3147
- const embeddingProfile = (_Ua = (_Ta = readString(env, "EMBEDDING_UNIVERSAL_PROFILE")) != null ? _Ta : (_Sa = (_Ra = base == null ? void 0 : base.embedding) == null ? void 0 : _Ra.options) == null ? void 0 : _Sa.profile) != null ? _Ua : "litellm";
3171
+ const embeddingProvider = (_Ka = (_Ja = readEnum(env, "EMBEDDING_PROVIDER", defaultEmbeddingProvider, EMBEDDING_PROVIDERS)) != null ? _Ja : (_Ia = base == null ? void 0 : base.embedding) == null ? void 0 : _Ia.provider) != null ? _Ka : defaultEmbeddingProvider;
3172
+ const embeddingBaseUrl = (_Pa = (_Oa = (_Ma = (_La = readString(env, "LITELLM_BASE_URL")) != null ? _La : readString(env, "EMBEDDING_BASE_URL")) != null ? _Ma : readString(env, "LLM_BASE_URL")) != null ? _Oa : (_Na = base == null ? void 0 : base.embedding) == null ? void 0 : _Na.baseUrl) != null ? _Pa : defaultGatewayUrl;
3173
+ const embeddingModel = (_Sa = (_Ra = readString(env, "EMBEDDING_MODEL")) != null ? _Ra : (_Qa = base == null ? void 0 : base.embedding) == null ? void 0 : _Qa.model) != null ? _Sa : "text-embedding-004";
3174
+ const embeddingApiKey = (_Za = (_Ya = (_Wa = (_Va = (_Ua = (_Ta = embeddingApiKeyByProvider[embeddingProvider]) != null ? _Ta : readString(env, "EMBEDDING_API_KEY")) != null ? _Ua : readString(env, "LLM_API_KEY")) != null ? _Va : readString(env, "LITELLM_MASTER_KEY")) != null ? _Wa : readString(env, "LITELLM_API_KEY")) != null ? _Ya : (_Xa = base == null ? void 0 : base.embedding) == null ? void 0 : _Xa.apiKey) != null ? _Za : licenseKey;
3175
+ const embeddingProfile = (_bb = (_ab = readString(env, "EMBEDDING_UNIVERSAL_PROFILE")) != null ? _ab : (_$a = (__a = base == null ? void 0 : base.embedding) == null ? void 0 : __a.options) == null ? void 0 : _$a.profile) != null ? _bb : "litellm";
3148
3176
  return __spreadProps(__spreadValues({
3149
3177
  projectId,
3150
3178
  licenseKey,
3151
3179
  vectorDb: {
3152
3180
  provider: vectorProvider,
3153
- indexName: (_Wa = (_Va = readString(env, "VECTOR_DB_INDEX")) != null ? _Va : vectorDbOptions.indexName) != null ? _Wa : "rag-index",
3181
+ indexName: (_db = (_cb = readString(env, "VECTOR_DB_INDEX")) != null ? _cb : vectorDbOptions.indexName) != null ? _db : "rag-index",
3154
3182
  options: vectorDbOptions
3155
3183
  },
3156
3184
  llm: {
@@ -3180,30 +3208,30 @@ function getEnvConfig(env = process.env, base) {
3180
3208
  }
3181
3209
  },
3182
3210
  ui: {
3183
- title: (_Ya = (_Xa = readString(env, "NEXT_PUBLIC_UI_TITLE")) != null ? _Xa : readString(env, "UI_TITLE")) != null ? _Ya : "AI Assistant",
3184
- subtitle: (__a = (_Za = readString(env, "NEXT_PUBLIC_UI_SUBTITLE")) != null ? _Za : readString(env, "UI_SUBTITLE")) != null ? __a : "Powered by RAG",
3185
- primaryColor: (_ab = (_$a = readString(env, "NEXT_PUBLIC_PRIMARY_COLOR")) != null ? _$a : readString(env, "UI_PRIMARY_COLOR")) != null ? _ab : "#10b981",
3186
- accentColor: (_cb = (_bb = readString(env, "NEXT_PUBLIC_ACCENT_COLOR")) != null ? _bb : readString(env, "UI_ACCENT_COLOR")) != null ? _cb : "#3b82f6",
3187
- logoUrl: (_db = readString(env, "NEXT_PUBLIC_LOGO_URL")) != null ? _db : readString(env, "UI_LOGO_URL"),
3188
- placeholder: (_fb = (_eb = readString(env, "NEXT_PUBLIC_PLACEHOLDER")) != null ? _eb : readString(env, "UI_PLACEHOLDER")) != null ? _fb : "Ask me anything\u2026",
3189
- showSources: ((_hb = (_gb = readString(env, "NEXT_PUBLIC_SHOW_SOURCES")) != null ? _gb : readString(env, "UI_SHOW_SOURCES")) != null ? _hb : "true") !== "false",
3190
- welcomeMessage: (_jb = (_ib = readString(env, "NEXT_PUBLIC_WELCOME_MESSAGE")) != null ? _ib : readString(env, "UI_WELCOME_MESSAGE")) != null ? _jb : "Hello! I'm your AI assistant. Ask me anything about your documents.",
3191
- visualStyle: (_lb = (_kb = readString(env, "NEXT_PUBLIC_UI_VISUAL_STYLE")) != null ? _kb : readString(env, "UI_VISUAL_STYLE")) != null ? _lb : "glass",
3192
- borderRadius: (_nb = (_mb = readString(env, "NEXT_PUBLIC_UI_BORDER_RADIUS")) != null ? _mb : readString(env, "UI_BORDER_RADIUS")) != null ? _nb : "xl",
3193
- allowUpload: ((_pb = (_ob = readString(env, "NEXT_PUBLIC_ALLOW_UPLOAD")) != null ? _ob : readString(env, "UI_ALLOW_UPLOAD")) != null ? _pb : "false") === "true"
3211
+ title: (_fb = (_eb = readString(env, "NEXT_PUBLIC_UI_TITLE")) != null ? _eb : readString(env, "UI_TITLE")) != null ? _fb : "AI Assistant",
3212
+ subtitle: (_hb = (_gb = readString(env, "NEXT_PUBLIC_UI_SUBTITLE")) != null ? _gb : readString(env, "UI_SUBTITLE")) != null ? _hb : "Powered by RAG",
3213
+ primaryColor: (_jb = (_ib = readString(env, "NEXT_PUBLIC_PRIMARY_COLOR")) != null ? _ib : readString(env, "UI_PRIMARY_COLOR")) != null ? _jb : "#10b981",
3214
+ accentColor: (_lb = (_kb = readString(env, "NEXT_PUBLIC_ACCENT_COLOR")) != null ? _kb : readString(env, "UI_ACCENT_COLOR")) != null ? _lb : "#3b82f6",
3215
+ logoUrl: (_mb = readString(env, "NEXT_PUBLIC_LOGO_URL")) != null ? _mb : readString(env, "UI_LOGO_URL"),
3216
+ placeholder: (_ob = (_nb = readString(env, "NEXT_PUBLIC_PLACEHOLDER")) != null ? _nb : readString(env, "UI_PLACEHOLDER")) != null ? _ob : "Ask me anything\u2026",
3217
+ showSources: ((_qb = (_pb = readString(env, "NEXT_PUBLIC_SHOW_SOURCES")) != null ? _pb : readString(env, "UI_SHOW_SOURCES")) != null ? _qb : "true") !== "false",
3218
+ welcomeMessage: (_sb = (_rb = readString(env, "NEXT_PUBLIC_WELCOME_MESSAGE")) != null ? _rb : readString(env, "UI_WELCOME_MESSAGE")) != null ? _sb : "Hello! I'm your AI assistant. Ask me anything about your documents.",
3219
+ visualStyle: (_ub = (_tb = readString(env, "NEXT_PUBLIC_UI_VISUAL_STYLE")) != null ? _tb : readString(env, "UI_VISUAL_STYLE")) != null ? _ub : "glass",
3220
+ borderRadius: (_wb = (_vb = readString(env, "NEXT_PUBLIC_UI_BORDER_RADIUS")) != null ? _vb : readString(env, "UI_BORDER_RADIUS")) != null ? _wb : "xl",
3221
+ allowUpload: ((_yb = (_xb = readString(env, "NEXT_PUBLIC_ALLOW_UPLOAD")) != null ? _xb : readString(env, "UI_ALLOW_UPLOAD")) != null ? _yb : "false") === "true"
3194
3222
  },
3195
3223
  rag: {
3196
3224
  topK: readNumber(env, "RAG_TOP_K", 5),
3197
3225
  scoreThreshold: readNumber(env, "RAG_SCORE_THRESHOLD", 0),
3198
3226
  chunkSize: readNumber(env, "RAG_CHUNK_SIZE", 1e3),
3199
3227
  chunkOverlap: readNumber(env, "RAG_CHUNK_OVERLAP", 200),
3200
- filterableFields: (_qb = readString(env, "RAG_FILTERABLE_FIELDS")) == null ? void 0 : _qb.split(",").map((f) => f.trim()),
3228
+ filterableFields: (_zb = readString(env, "RAG_FILTERABLE_FIELDS")) == null ? void 0 : _zb.split(",").map((f) => f.trim()),
3201
3229
  // Query pipeline toggles — read from .env.local
3202
3230
  useQueryTransformation: readString(env, "RAG_USE_QUERY_TRANSFORMATION") === "true",
3203
3231
  useReranking: readString(env, "RAG_USE_RERANKING") === "true",
3204
3232
  useGraphRetrieval: readString(env, "RAG_USE_GRAPH_RETRIEVAL") === "true",
3205
- architecture: (_rb = readString(env, "RAG_ARCHITECTURE")) != null ? _rb : "simple",
3206
- chunkingStrategy: (_sb = readString(env, "RAG_CHUNKING_STRATEGY")) != null ? _sb : "recursive",
3233
+ architecture: (_Ab = readString(env, "RAG_ARCHITECTURE")) != null ? _Ab : "simple",
3234
+ chunkingStrategy: (_Bb = readString(env, "RAG_CHUNKING_STRATEGY")) != null ? _Bb : "recursive",
3207
3235
  uiMapping: (() => {
3208
3236
  const raw = readString(env, "RAG_UI_MAPPING");
3209
3237
  if (!raw) return void 0;
@@ -3216,7 +3244,7 @@ function getEnvConfig(env = process.env, base) {
3216
3244
  },
3217
3245
  telemetry: {
3218
3246
  enabled: telemetryEnabled,
3219
- url: (_wb = (_vb = (_tb = readString(env, "TELEMETRY_URL")) != null ? _tb : readString(env, "NEXT_PUBLIC_TELEMETRY_URL")) != null ? _vb : (_ub = base == null ? void 0 : base.telemetry) == null ? void 0 : _ub.url) != null ? _wb : process.env.NODE_ENV === "development" ? "http://localhost:3001/api/telemetry" : "https://retrivora.com/api/telemetry"
3247
+ url: (_Fb = (_Eb = (_Cb = readString(env, "TELEMETRY_URL")) != null ? _Cb : readString(env, "NEXT_PUBLIC_TELEMETRY_URL")) != null ? _Eb : (_Db = base == null ? void 0 : base.telemetry) == null ? void 0 : _Db.url) != null ? _Fb : process.env.NODE_ENV === "development" ? "http://localhost:3001/api/telemetry" : "https://retrivora.com/api/telemetry"
3220
3248
  }
3221
3249
  }, readString(env, "GRAPH_DB_PROVIDER") ? {
3222
3250
  graphDb: {
@@ -4535,7 +4563,7 @@ var UniversalLLMAdapter = class {
4535
4563
  });
4536
4564
  }
4537
4565
  async chat(messages, context) {
4538
- var _a2, _b, _c;
4566
+ var _a2, _b, _c, _d, _e;
4539
4567
  const path2 = (_a2 = this.opts.chatPath) != null ? _a2 : "/chat/completions";
4540
4568
  const safeMessages = (Array.isArray(messages) ? messages : []).filter((m) => Boolean(m && typeof m === "object")).map((m) => ({
4541
4569
  role: m.role || "user",
@@ -4564,8 +4592,15 @@ ${context != null ? context : "None"}` },
4564
4592
  temperature: this.temperature
4565
4593
  };
4566
4594
  }
4567
- const isSelfHost = this.baseUrl.includes("retrivora.com") || this.baseUrl.includes("localhost");
4568
- if (isSelfHost || Boolean(process.env.VERCEL)) {
4595
+ try {
4596
+ const { data: data2 } = await this.http.post(path2, payload);
4597
+ const extractPath2 = (_b = this.opts.responseExtractPath) != null ? _b : "choices[0].message.content";
4598
+ const result2 = (_c = resolvePath(data2, extractPath2)) != null ? _c : extractContent(data2);
4599
+ if (result2 !== void 0) {
4600
+ return String(result2);
4601
+ }
4602
+ } catch (httpErr) {
4603
+ console.warn(`[UniversalLLMAdapter] Direct HTTP POST to ${this.baseUrl}${path2} failed (${httpErr.message}). Attempting in-process gateway dispatch fallback...`);
4569
4604
  const _g2 = globalThis;
4570
4605
  let dispatch = _g2.__retrivoraDispatchChat;
4571
4606
  if (typeof dispatch !== "function") {
@@ -4580,28 +4615,22 @@ ${context != null ? context : "None"}` },
4580
4615
  }
4581
4616
  }
4582
4617
  if (typeof dispatch === "function") {
4583
- try {
4584
- const res = await dispatch({
4585
- model: this.model,
4586
- messages: formattedMessages,
4587
- max_tokens: this.maxTokens,
4588
- temperature: this.temperature
4589
- }, this.apiKey);
4590
- const content = extractContent(res == null ? void 0 : res.response);
4591
- if (content !== void 0) {
4592
- return content;
4593
- }
4594
- throw new Error(`[UniversalLLMAdapter] In-process dispatch returned empty content for model: ${this.model}`);
4595
- } catch (dispatchErr) {
4596
- throw dispatchErr;
4618
+ const res = await dispatch({
4619
+ model: this.model,
4620
+ messages: formattedMessages,
4621
+ max_tokens: this.maxTokens,
4622
+ temperature: this.temperature
4623
+ }, this.apiKey);
4624
+ const content = extractContent(res == null ? void 0 : res.response);
4625
+ if (content !== void 0) {
4626
+ return content;
4597
4627
  }
4598
- } else {
4599
- 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.`);
4600
4628
  }
4629
+ throw httpErr;
4601
4630
  }
4602
4631
  const { data } = await this.http.post(path2, payload);
4603
- const extractPath = (_b = this.opts.responseExtractPath) != null ? _b : "choices[0].message.content";
4604
- const result = (_c = resolvePath(data, extractPath)) != null ? _c : extractContent(data);
4632
+ const extractPath = (_d = this.opts.responseExtractPath) != null ? _d : "choices[0].message.content";
4633
+ const result = (_e = resolvePath(data, extractPath)) != null ? _e : extractContent(data);
4605
4634
  if (result === void 0) {
4606
4635
  throw new Error(`[UniversalLLMAdapter] Could not extract text from path '${extractPath}' in response.`);
4607
4636
  }
@@ -4743,7 +4772,7 @@ ${context != null ? context : "None"}` },
4743
4772
  });
4744
4773
  }
4745
4774
  async embed(text) {
4746
- var _a2, _b, _c, _d;
4775
+ var _a2, _b, _c, _d, _e;
4747
4776
  const path2 = (_a2 = this.opts.embedPath) != null ? _a2 : "/embeddings";
4748
4777
  let payload;
4749
4778
  if (this.opts.embedPayloadTemplate) {
@@ -4757,8 +4786,15 @@ ${context != null ? context : "None"}` },
4757
4786
  input: text
4758
4787
  };
4759
4788
  }
4760
- const isSelfHost = this.baseUrl.includes("retrivora.com") || this.baseUrl.includes("localhost");
4761
- if (isSelfHost || Boolean(process.env.VERCEL)) {
4789
+ try {
4790
+ const { data: data2 } = await this.http.post(path2, payload);
4791
+ const extractPath2 = (_b = this.opts.embedExtractPath) != null ? _b : "data[0].embedding";
4792
+ const vector2 = resolvePath(data2, extractPath2);
4793
+ if (Array.isArray(vector2)) {
4794
+ return vector2;
4795
+ }
4796
+ } catch (httpErr) {
4797
+ console.warn(`[UniversalLLMAdapter] Direct HTTP embedding POST to ${this.baseUrl}${path2} failed (${httpErr.message}). Attempting in-process gateway dispatch fallback...`);
4762
4798
  const _g2 = globalThis;
4763
4799
  let dispatch = _g2.__retrivoraDispatchEmbedding;
4764
4800
  if (typeof dispatch !== "function") {
@@ -4773,24 +4809,18 @@ ${context != null ? context : "None"}` },
4773
4809
  }
4774
4810
  }
4775
4811
  if (typeof dispatch === "function") {
4776
- try {
4777
- const res = await dispatch({
4778
- model: this.model,
4779
- input: text
4780
- }, this.apiKey);
4781
- if ((_c = (_b = res == null ? void 0 : res.data) == null ? void 0 : _b[0]) == null ? void 0 : _c.embedding) {
4782
- return res.data[0].embedding;
4783
- }
4784
- throw new Error(`[UniversalLLMAdapter] In-process embedding dispatch returned invalid vector for model: ${this.model}`);
4785
- } catch (dispatchErr) {
4786
- throw dispatchErr;
4812
+ const res = await dispatch({
4813
+ model: this.model,
4814
+ input: text
4815
+ }, this.apiKey);
4816
+ if ((_d = (_c = res == null ? void 0 : res.data) == null ? void 0 : _c[0]) == null ? void 0 : _d.embedding) {
4817
+ return res.data[0].embedding;
4787
4818
  }
4788
- } else {
4789
- 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.`);
4790
4819
  }
4820
+ throw httpErr;
4791
4821
  }
4792
4822
  const { data } = await this.http.post(path2, payload);
4793
- const extractPath = (_d = this.opts.embedExtractPath) != null ? _d : "data[0].embedding";
4823
+ const extractPath = (_e = this.opts.embedExtractPath) != null ? _e : "data[0].embedding";
4794
4824
  const vector = resolvePath(data, extractPath);
4795
4825
  if (!Array.isArray(vector)) {
4796
4826
  throw new Error(`[UniversalLLMAdapter] Expected a number array at '${extractPath}' for embeddings.`);
@@ -576,26 +576,54 @@ function cleanApiKeyOverride(apiKeyOverride) {
576
576
  }
577
577
  return key;
578
578
  }
579
+ async function resolveUserGatewayConfig(apiKeyOverride) {
580
+ var _a2;
581
+ if (!apiKeyOverride || !process.env.NEXT_PUBLIC_SUPABASE_URL || !process.env.SUPABASE_SERVICE_ROLE_KEY) {
582
+ return {};
583
+ }
584
+ try {
585
+ const { createAdminClient } = await import("@/lib/supabase-server");
586
+ const supabase = createAdminClient();
587
+ const { data: licenseRecord } = await supabase.from("licenses").select("project_id, customer_name, tier").eq("license_key", apiKeyOverride.trim()).single();
588
+ if (!licenseRecord) {
589
+ return {};
590
+ }
591
+ const projectId = licenseRecord.project_id;
592
+ 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);
593
+ if (!configs || configs.length === 0) {
594
+ return {};
595
+ }
596
+ const matchedConfig = configs.find((c) => c.project_id === projectId) || configs.find((c) => c.project_id === "global");
597
+ const customGroqKey = (_a2 = matchedConfig == null ? void 0 : matchedConfig.provider_keys) == null ? void 0 : _a2.groq;
598
+ const targetModel = matchedConfig == null ? void 0 : matchedConfig.default_model;
599
+ return { customGroqKey, targetModel };
600
+ } catch (err) {
601
+ console.warn("[LLM Gateway Router] Error resolving user gateway_config:", err.message);
602
+ return {};
603
+ }
604
+ }
579
605
  async function dispatchChatCompletion(req, apiKeyOverride) {
580
- const effectiveKey = cleanApiKeyOverride(apiKeyOverride);
581
- const provider = resolveProvider(req.model);
582
- console.log(`[LLM Gateway Router] Dispatching chat request: model=${req.model}, provider=${provider}, hasGroqKey=${Boolean(process.env.GROQ_API_KEY)}, hasGeminiKey=${Boolean(process.env.GROQ_API_KEY || process.env.GEMINI_API_KEY)}, hasHFToken=${Boolean(process.env.HF_TOKEN)}`);
606
+ const { customGroqKey, targetModel } = await resolveUserGatewayConfig(apiKeyOverride);
607
+ const effectiveKey = customGroqKey || cleanApiKeyOverride(apiKeyOverride);
608
+ const activeModel = req.model || targetModel || "llama-3.1-8b-instant";
609
+ const provider = resolveProvider(activeModel);
610
+ console.log(`[LLM Gateway Router] Dispatching chat request: model=${activeModel}, provider=${provider}, hasCustomKey=${Boolean(customGroqKey)}, hasGlobalGroqKey=${Boolean(process.env.GROQ_API_KEY)}`);
583
611
  try {
584
612
  switch (provider) {
585
613
  case "groq":
586
- return await handleGroqRequest(req, effectiveKey);
614
+ return await handleGroqRequest(__spreadProps(__spreadValues({}, req), { model: activeModel }), effectiveKey);
587
615
  case "openai":
588
- return await handleOpenAIRequest(req, effectiveKey);
616
+ return await handleOpenAIRequest(__spreadProps(__spreadValues({}, req), { model: activeModel }), effectiveKey);
589
617
  case "gemini":
590
- return await handleGeminiRequest(req, effectiveKey);
618
+ return await handleGeminiRequest(__spreadProps(__spreadValues({}, req), { model: activeModel }), effectiveKey);
591
619
  case "anthropic":
592
- return await handleAnthropicRequest(req, effectiveKey);
620
+ return await handleAnthropicRequest(__spreadProps(__spreadValues({}, req), { model: activeModel }), effectiveKey);
593
621
  case "ollama":
594
- return await handleOllamaRequest(req);
622
+ return await handleOllamaRequest(__spreadProps(__spreadValues({}, req), { model: activeModel }));
595
623
  case "huggingface":
596
- return await handleHuggingFaceChatRequest(req, effectiveKey);
624
+ return await handleHuggingFaceChatRequest(__spreadProps(__spreadValues({}, req), { model: activeModel }), effectiveKey);
597
625
  default:
598
- throw new Error(`Unsupported LLM provider for model: ${req.model}`);
626
+ throw new Error(`Unsupported LLM provider for model: ${activeModel}`);
599
627
  }
600
628
  } catch (error) {
601
629
  console.error(`[LLM Gateway Router] Provider '${provider}' failed for model '${req.model}':`, {
@@ -3017,7 +3045,7 @@ function readEnum(env, name, fallback, allowed) {
3017
3045
  throw new Error(`[getRagConfig] ${name} must be one of: ${allowed.join(", ")}`);
3018
3046
  }
3019
3047
  function getEnvConfig(env = process.env, base) {
3020
- var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _A, _B, _C, _D, _E, _F, _G, _H, _I, _J, _K, _L, _M, _N, _O, _P, _Q, _R, _S, _T, _U, _V, _W, _X, _Y, _Z, __, _$, _aa, _ba, _ca, _da, _ea, _fa, _ga, _ha, _ia, _ja, _ka, _la, _ma, _na, _oa, _pa, _qa, _ra, _sa, _ta, _ua, _va, _wa, _xa, _ya, _za, _Aa, _Ba, _Ca, _Da, _Ea, _Fa, _Ga, _Ha, _Ia, _Ja, _Ka, _La, _Ma, _Na, _Oa, _Pa, _Qa, _Ra, _Sa, _Ta, _Ua, _Va, _Wa, _Xa, _Ya, _Za, __a, _$a, _ab, _bb, _cb, _db, _eb, _fb, _gb, _hb, _ib, _jb, _kb, _lb, _mb, _nb, _ob, _pb, _qb, _rb, _sb, _tb, _ub, _vb, _wb;
3048
+ var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _A, _B, _C, _D, _E, _F, _G, _H, _I, _J, _K, _L, _M, _N, _O, _P, _Q, _R, _S, _T, _U, _V, _W, _X, _Y, _Z, __, _$, _aa, _ba, _ca, _da, _ea, _fa, _ga, _ha, _ia, _ja, _ka, _la, _ma, _na, _oa, _pa, _qa, _ra, _sa, _ta, _ua, _va, _wa, _xa, _ya, _za, _Aa, _Ba, _Ca, _Da, _Ea, _Fa, _Ga, _Ha, _Ia, _Ja, _Ka, _La, _Ma, _Na, _Oa, _Pa, _Qa, _Ra, _Sa, _Ta, _Ua, _Va, _Wa, _Xa, _Ya, _Za, __a, _$a, _ab, _bb, _cb, _db, _eb, _fb, _gb, _hb, _ib, _jb, _kb, _lb, _mb, _nb, _ob, _pb, _qb, _rb, _sb, _tb, _ub, _vb, _wb, _xb, _yb, _zb, _Ab, _Bb, _Cb, _Db, _Eb, _Fb;
3021
3049
  const projectId = (_c = (_b = (_a2 = readString(env, "RAG_PROJECT_ID")) != null ? _a2 : readString(env, "NEXT_PUBLIC_PROJECT_ID")) != null ? _b : base == null ? void 0 : base.projectId) != null ? _c : "__default__";
3022
3050
  const licenseKey = (_g2 = (_f = (_e = (_d = readString(env, "RAG_LICENSE_KEY")) != null ? _d : readString(env, "RETRIVORA_LICENSE_KEY")) != null ? _e : readString(env, "NEXT_PUBLIC_RETRIVORA_LICENSE_KEY")) != null ? _f : readString(env, "LICENSE_KEY")) != null ? _g2 : base == null ? void 0 : base.licenseKey;
3023
3051
  const telemetryEnabled = readString(env, "TELEMETRY_ENABLED") === "true" || readString(env, "NEXT_PUBLIC_TELEMETRY_ENABLED") === "true" || ((_h = base == null ? void 0 : base.telemetry) == null ? void 0 : _h.enabled) || Boolean(licenseKey);
@@ -3097,25 +3125,25 @@ function getEnvConfig(env = process.env, base) {
3097
3125
  return true;
3098
3126
  }
3099
3127
  })();
3100
- const DEFAULT_FREE_GATEWAY = "https://llm.retrivora.com/api/v1";
3128
+ const defaultGatewayUrl = (_ma = (_la = readString(env, "LITELLM_BASE_URL")) != null ? _la : readString(env, "LLM_BASE_URL")) != null ? _ma : "https://llm.retrivora.com/api/v1";
3101
3129
  const defaultLlmProvider = isFreeTier ? "universal_rest" : "universal_rest";
3102
- const llmProvider = (_na = (_ma = readEnum(env, "LLM_PROVIDER", defaultLlmProvider, LLM_PROVIDERS)) != null ? _ma : (_la = base == null ? void 0 : base.llm) == null ? void 0 : _la.provider) != null ? _na : defaultLlmProvider;
3103
- const llmBaseUrl = (_qa = (_pa = readString(env, "LLM_BASE_URL")) != null ? _pa : (_oa = base == null ? void 0 : base.llm) == null ? void 0 : _oa.baseUrl) != null ? _qa : DEFAULT_FREE_GATEWAY;
3104
- const llmModel = (_ua = (_sa = readString(env, "LLM_MODEL")) != null ? _sa : (_ra = base == null ? void 0 : base.llm) == null ? void 0 : _ra.model) != null ? _ua : isFreeTier ? "llama-3.1-8b-instant" : (_ta = DEFAULT_MODEL_BY_PROVIDER[llmProvider]) != null ? _ta : "gpt-4o";
3105
- const llmApiKey = (_ya = (_xa = (_va = llmApiKeyByProvider[llmProvider]) != null ? _va : readString(env, "LLM_API_KEY")) != null ? _xa : (_wa = base == null ? void 0 : base.llm) == null ? void 0 : _wa.apiKey) != null ? _ya : "sk-retrivora-cancer-280590";
3106
- const llmProfile = (_Ca = (_Ba = readString(env, "LLM_UNIVERSAL_PROFILE")) != null ? _Ba : (_Aa = (_za = base == null ? void 0 : base.llm) == null ? void 0 : _za.options) == null ? void 0 : _Aa.profile) != null ? _Ca : "litellm";
3130
+ const llmProvider = (_pa = (_oa = readEnum(env, "LLM_PROVIDER", defaultLlmProvider, LLM_PROVIDERS)) != null ? _oa : (_na = base == null ? void 0 : base.llm) == null ? void 0 : _na.provider) != null ? _pa : defaultLlmProvider;
3131
+ const llmBaseUrl = (_ta = (_sa = (_qa = readString(env, "LITELLM_BASE_URL")) != null ? _qa : readString(env, "LLM_BASE_URL")) != null ? _sa : (_ra = base == null ? void 0 : base.llm) == null ? void 0 : _ra.baseUrl) != null ? _ta : defaultGatewayUrl;
3132
+ const llmModel = (_xa = (_va = readString(env, "LLM_MODEL")) != null ? _va : (_ua = base == null ? void 0 : base.llm) == null ? void 0 : _ua.model) != null ? _xa : isFreeTier ? "llama-3.1-8b-instant" : (_wa = DEFAULT_MODEL_BY_PROVIDER[llmProvider]) != null ? _wa : "gpt-4o";
3133
+ const llmApiKey = (_Da = (_Ca = (_Aa = (_za = (_ya = llmApiKeyByProvider[llmProvider]) != null ? _ya : readString(env, "LLM_API_KEY")) != null ? _za : readString(env, "LITELLM_MASTER_KEY")) != null ? _Aa : readString(env, "LITELLM_API_KEY")) != null ? _Ca : (_Ba = base == null ? void 0 : base.llm) == null ? void 0 : _Ba.apiKey) != null ? _Da : licenseKey;
3134
+ const llmProfile = (_Ha = (_Ga = readString(env, "LLM_UNIVERSAL_PROFILE")) != null ? _Ga : (_Fa = (_Ea = base == null ? void 0 : base.llm) == null ? void 0 : _Ea.options) == null ? void 0 : _Fa.profile) != null ? _Ha : "litellm";
3107
3135
  const defaultEmbeddingProvider = isFreeTier ? "universal_rest" : "universal_rest";
3108
- const embeddingProvider = (_Fa = (_Ea = readEnum(env, "EMBEDDING_PROVIDER", defaultEmbeddingProvider, EMBEDDING_PROVIDERS)) != null ? _Ea : (_Da = base == null ? void 0 : base.embedding) == null ? void 0 : _Da.provider) != null ? _Fa : defaultEmbeddingProvider;
3109
- const embeddingBaseUrl = (_Ia = (_Ha = readString(env, "EMBEDDING_BASE_URL")) != null ? _Ha : (_Ga = base == null ? void 0 : base.embedding) == null ? void 0 : _Ga.baseUrl) != null ? _Ia : DEFAULT_FREE_GATEWAY;
3110
- const embeddingModel = (_La = (_Ka = readString(env, "EMBEDDING_MODEL")) != null ? _Ka : (_Ja = base == null ? void 0 : base.embedding) == null ? void 0 : _Ja.model) != null ? _La : "text-embedding-004";
3111
- const embeddingApiKey = (_Qa = (_Pa = (_Na = (_Ma = embeddingApiKeyByProvider[embeddingProvider]) != null ? _Ma : readString(env, "EMBEDDING_API_KEY")) != null ? _Na : readString(env, "LLM_API_KEY")) != null ? _Pa : (_Oa = base == null ? void 0 : base.embedding) == null ? void 0 : _Oa.apiKey) != null ? _Qa : "sk-retrivora-cancer-280590";
3112
- const embeddingProfile = (_Ua = (_Ta = readString(env, "EMBEDDING_UNIVERSAL_PROFILE")) != null ? _Ta : (_Sa = (_Ra = base == null ? void 0 : base.embedding) == null ? void 0 : _Ra.options) == null ? void 0 : _Sa.profile) != null ? _Ua : "litellm";
3136
+ const embeddingProvider = (_Ka = (_Ja = readEnum(env, "EMBEDDING_PROVIDER", defaultEmbeddingProvider, EMBEDDING_PROVIDERS)) != null ? _Ja : (_Ia = base == null ? void 0 : base.embedding) == null ? void 0 : _Ia.provider) != null ? _Ka : defaultEmbeddingProvider;
3137
+ const embeddingBaseUrl = (_Pa = (_Oa = (_Ma = (_La = readString(env, "LITELLM_BASE_URL")) != null ? _La : readString(env, "EMBEDDING_BASE_URL")) != null ? _Ma : readString(env, "LLM_BASE_URL")) != null ? _Oa : (_Na = base == null ? void 0 : base.embedding) == null ? void 0 : _Na.baseUrl) != null ? _Pa : defaultGatewayUrl;
3138
+ const embeddingModel = (_Sa = (_Ra = readString(env, "EMBEDDING_MODEL")) != null ? _Ra : (_Qa = base == null ? void 0 : base.embedding) == null ? void 0 : _Qa.model) != null ? _Sa : "text-embedding-004";
3139
+ const embeddingApiKey = (_Za = (_Ya = (_Wa = (_Va = (_Ua = (_Ta = embeddingApiKeyByProvider[embeddingProvider]) != null ? _Ta : readString(env, "EMBEDDING_API_KEY")) != null ? _Ua : readString(env, "LLM_API_KEY")) != null ? _Va : readString(env, "LITELLM_MASTER_KEY")) != null ? _Wa : readString(env, "LITELLM_API_KEY")) != null ? _Ya : (_Xa = base == null ? void 0 : base.embedding) == null ? void 0 : _Xa.apiKey) != null ? _Za : licenseKey;
3140
+ const embeddingProfile = (_bb = (_ab = readString(env, "EMBEDDING_UNIVERSAL_PROFILE")) != null ? _ab : (_$a = (__a = base == null ? void 0 : base.embedding) == null ? void 0 : __a.options) == null ? void 0 : _$a.profile) != null ? _bb : "litellm";
3113
3141
  return __spreadProps(__spreadValues({
3114
3142
  projectId,
3115
3143
  licenseKey,
3116
3144
  vectorDb: {
3117
3145
  provider: vectorProvider,
3118
- indexName: (_Wa = (_Va = readString(env, "VECTOR_DB_INDEX")) != null ? _Va : vectorDbOptions.indexName) != null ? _Wa : "rag-index",
3146
+ indexName: (_db = (_cb = readString(env, "VECTOR_DB_INDEX")) != null ? _cb : vectorDbOptions.indexName) != null ? _db : "rag-index",
3119
3147
  options: vectorDbOptions
3120
3148
  },
3121
3149
  llm: {
@@ -3145,30 +3173,30 @@ function getEnvConfig(env = process.env, base) {
3145
3173
  }
3146
3174
  },
3147
3175
  ui: {
3148
- title: (_Ya = (_Xa = readString(env, "NEXT_PUBLIC_UI_TITLE")) != null ? _Xa : readString(env, "UI_TITLE")) != null ? _Ya : "AI Assistant",
3149
- subtitle: (__a = (_Za = readString(env, "NEXT_PUBLIC_UI_SUBTITLE")) != null ? _Za : readString(env, "UI_SUBTITLE")) != null ? __a : "Powered by RAG",
3150
- primaryColor: (_ab = (_$a = readString(env, "NEXT_PUBLIC_PRIMARY_COLOR")) != null ? _$a : readString(env, "UI_PRIMARY_COLOR")) != null ? _ab : "#10b981",
3151
- accentColor: (_cb = (_bb = readString(env, "NEXT_PUBLIC_ACCENT_COLOR")) != null ? _bb : readString(env, "UI_ACCENT_COLOR")) != null ? _cb : "#3b82f6",
3152
- logoUrl: (_db = readString(env, "NEXT_PUBLIC_LOGO_URL")) != null ? _db : readString(env, "UI_LOGO_URL"),
3153
- placeholder: (_fb = (_eb = readString(env, "NEXT_PUBLIC_PLACEHOLDER")) != null ? _eb : readString(env, "UI_PLACEHOLDER")) != null ? _fb : "Ask me anything\u2026",
3154
- showSources: ((_hb = (_gb = readString(env, "NEXT_PUBLIC_SHOW_SOURCES")) != null ? _gb : readString(env, "UI_SHOW_SOURCES")) != null ? _hb : "true") !== "false",
3155
- welcomeMessage: (_jb = (_ib = readString(env, "NEXT_PUBLIC_WELCOME_MESSAGE")) != null ? _ib : readString(env, "UI_WELCOME_MESSAGE")) != null ? _jb : "Hello! I'm your AI assistant. Ask me anything about your documents.",
3156
- visualStyle: (_lb = (_kb = readString(env, "NEXT_PUBLIC_UI_VISUAL_STYLE")) != null ? _kb : readString(env, "UI_VISUAL_STYLE")) != null ? _lb : "glass",
3157
- borderRadius: (_nb = (_mb = readString(env, "NEXT_PUBLIC_UI_BORDER_RADIUS")) != null ? _mb : readString(env, "UI_BORDER_RADIUS")) != null ? _nb : "xl",
3158
- allowUpload: ((_pb = (_ob = readString(env, "NEXT_PUBLIC_ALLOW_UPLOAD")) != null ? _ob : readString(env, "UI_ALLOW_UPLOAD")) != null ? _pb : "false") === "true"
3176
+ title: (_fb = (_eb = readString(env, "NEXT_PUBLIC_UI_TITLE")) != null ? _eb : readString(env, "UI_TITLE")) != null ? _fb : "AI Assistant",
3177
+ subtitle: (_hb = (_gb = readString(env, "NEXT_PUBLIC_UI_SUBTITLE")) != null ? _gb : readString(env, "UI_SUBTITLE")) != null ? _hb : "Powered by RAG",
3178
+ primaryColor: (_jb = (_ib = readString(env, "NEXT_PUBLIC_PRIMARY_COLOR")) != null ? _ib : readString(env, "UI_PRIMARY_COLOR")) != null ? _jb : "#10b981",
3179
+ accentColor: (_lb = (_kb = readString(env, "NEXT_PUBLIC_ACCENT_COLOR")) != null ? _kb : readString(env, "UI_ACCENT_COLOR")) != null ? _lb : "#3b82f6",
3180
+ logoUrl: (_mb = readString(env, "NEXT_PUBLIC_LOGO_URL")) != null ? _mb : readString(env, "UI_LOGO_URL"),
3181
+ placeholder: (_ob = (_nb = readString(env, "NEXT_PUBLIC_PLACEHOLDER")) != null ? _nb : readString(env, "UI_PLACEHOLDER")) != null ? _ob : "Ask me anything\u2026",
3182
+ showSources: ((_qb = (_pb = readString(env, "NEXT_PUBLIC_SHOW_SOURCES")) != null ? _pb : readString(env, "UI_SHOW_SOURCES")) != null ? _qb : "true") !== "false",
3183
+ welcomeMessage: (_sb = (_rb = readString(env, "NEXT_PUBLIC_WELCOME_MESSAGE")) != null ? _rb : readString(env, "UI_WELCOME_MESSAGE")) != null ? _sb : "Hello! I'm your AI assistant. Ask me anything about your documents.",
3184
+ visualStyle: (_ub = (_tb = readString(env, "NEXT_PUBLIC_UI_VISUAL_STYLE")) != null ? _tb : readString(env, "UI_VISUAL_STYLE")) != null ? _ub : "glass",
3185
+ borderRadius: (_wb = (_vb = readString(env, "NEXT_PUBLIC_UI_BORDER_RADIUS")) != null ? _vb : readString(env, "UI_BORDER_RADIUS")) != null ? _wb : "xl",
3186
+ allowUpload: ((_yb = (_xb = readString(env, "NEXT_PUBLIC_ALLOW_UPLOAD")) != null ? _xb : readString(env, "UI_ALLOW_UPLOAD")) != null ? _yb : "false") === "true"
3159
3187
  },
3160
3188
  rag: {
3161
3189
  topK: readNumber(env, "RAG_TOP_K", 5),
3162
3190
  scoreThreshold: readNumber(env, "RAG_SCORE_THRESHOLD", 0),
3163
3191
  chunkSize: readNumber(env, "RAG_CHUNK_SIZE", 1e3),
3164
3192
  chunkOverlap: readNumber(env, "RAG_CHUNK_OVERLAP", 200),
3165
- filterableFields: (_qb = readString(env, "RAG_FILTERABLE_FIELDS")) == null ? void 0 : _qb.split(",").map((f) => f.trim()),
3193
+ filterableFields: (_zb = readString(env, "RAG_FILTERABLE_FIELDS")) == null ? void 0 : _zb.split(",").map((f) => f.trim()),
3166
3194
  // Query pipeline toggles — read from .env.local
3167
3195
  useQueryTransformation: readString(env, "RAG_USE_QUERY_TRANSFORMATION") === "true",
3168
3196
  useReranking: readString(env, "RAG_USE_RERANKING") === "true",
3169
3197
  useGraphRetrieval: readString(env, "RAG_USE_GRAPH_RETRIEVAL") === "true",
3170
- architecture: (_rb = readString(env, "RAG_ARCHITECTURE")) != null ? _rb : "simple",
3171
- chunkingStrategy: (_sb = readString(env, "RAG_CHUNKING_STRATEGY")) != null ? _sb : "recursive",
3198
+ architecture: (_Ab = readString(env, "RAG_ARCHITECTURE")) != null ? _Ab : "simple",
3199
+ chunkingStrategy: (_Bb = readString(env, "RAG_CHUNKING_STRATEGY")) != null ? _Bb : "recursive",
3172
3200
  uiMapping: (() => {
3173
3201
  const raw = readString(env, "RAG_UI_MAPPING");
3174
3202
  if (!raw) return void 0;
@@ -3181,7 +3209,7 @@ function getEnvConfig(env = process.env, base) {
3181
3209
  },
3182
3210
  telemetry: {
3183
3211
  enabled: telemetryEnabled,
3184
- url: (_wb = (_vb = (_tb = readString(env, "TELEMETRY_URL")) != null ? _tb : readString(env, "NEXT_PUBLIC_TELEMETRY_URL")) != null ? _vb : (_ub = base == null ? void 0 : base.telemetry) == null ? void 0 : _ub.url) != null ? _wb : process.env.NODE_ENV === "development" ? "http://localhost:3001/api/telemetry" : "https://retrivora.com/api/telemetry"
3212
+ url: (_Fb = (_Eb = (_Cb = readString(env, "TELEMETRY_URL")) != null ? _Cb : readString(env, "NEXT_PUBLIC_TELEMETRY_URL")) != null ? _Eb : (_Db = base == null ? void 0 : base.telemetry) == null ? void 0 : _Db.url) != null ? _Fb : process.env.NODE_ENV === "development" ? "http://localhost:3001/api/telemetry" : "https://retrivora.com/api/telemetry"
3185
3213
  }
3186
3214
  }, readString(env, "GRAPH_DB_PROVIDER") ? {
3187
3215
  graphDb: {
@@ -4500,7 +4528,7 @@ var UniversalLLMAdapter = class {
4500
4528
  });
4501
4529
  }
4502
4530
  async chat(messages, context) {
4503
- var _a2, _b, _c;
4531
+ var _a2, _b, _c, _d, _e;
4504
4532
  const path2 = (_a2 = this.opts.chatPath) != null ? _a2 : "/chat/completions";
4505
4533
  const safeMessages = (Array.isArray(messages) ? messages : []).filter((m) => Boolean(m && typeof m === "object")).map((m) => ({
4506
4534
  role: m.role || "user",
@@ -4529,8 +4557,15 @@ ${context != null ? context : "None"}` },
4529
4557
  temperature: this.temperature
4530
4558
  };
4531
4559
  }
4532
- const isSelfHost = this.baseUrl.includes("retrivora.com") || this.baseUrl.includes("localhost");
4533
- if (isSelfHost || Boolean(process.env.VERCEL)) {
4560
+ try {
4561
+ const { data: data2 } = await this.http.post(path2, payload);
4562
+ const extractPath2 = (_b = this.opts.responseExtractPath) != null ? _b : "choices[0].message.content";
4563
+ const result2 = (_c = resolvePath(data2, extractPath2)) != null ? _c : extractContent(data2);
4564
+ if (result2 !== void 0) {
4565
+ return String(result2);
4566
+ }
4567
+ } catch (httpErr) {
4568
+ console.warn(`[UniversalLLMAdapter] Direct HTTP POST to ${this.baseUrl}${path2} failed (${httpErr.message}). Attempting in-process gateway dispatch fallback...`);
4534
4569
  const _g2 = globalThis;
4535
4570
  let dispatch = _g2.__retrivoraDispatchChat;
4536
4571
  if (typeof dispatch !== "function") {
@@ -4545,28 +4580,22 @@ ${context != null ? context : "None"}` },
4545
4580
  }
4546
4581
  }
4547
4582
  if (typeof dispatch === "function") {
4548
- try {
4549
- const res = await dispatch({
4550
- model: this.model,
4551
- messages: formattedMessages,
4552
- max_tokens: this.maxTokens,
4553
- temperature: this.temperature
4554
- }, this.apiKey);
4555
- const content = extractContent(res == null ? void 0 : res.response);
4556
- if (content !== void 0) {
4557
- return content;
4558
- }
4559
- throw new Error(`[UniversalLLMAdapter] In-process dispatch returned empty content for model: ${this.model}`);
4560
- } catch (dispatchErr) {
4561
- throw dispatchErr;
4583
+ const res = await dispatch({
4584
+ model: this.model,
4585
+ messages: formattedMessages,
4586
+ max_tokens: this.maxTokens,
4587
+ temperature: this.temperature
4588
+ }, this.apiKey);
4589
+ const content = extractContent(res == null ? void 0 : res.response);
4590
+ if (content !== void 0) {
4591
+ return content;
4562
4592
  }
4563
- } else {
4564
- 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.`);
4565
4593
  }
4594
+ throw httpErr;
4566
4595
  }
4567
4596
  const { data } = await this.http.post(path2, payload);
4568
- const extractPath = (_b = this.opts.responseExtractPath) != null ? _b : "choices[0].message.content";
4569
- const result = (_c = resolvePath(data, extractPath)) != null ? _c : extractContent(data);
4597
+ const extractPath = (_d = this.opts.responseExtractPath) != null ? _d : "choices[0].message.content";
4598
+ const result = (_e = resolvePath(data, extractPath)) != null ? _e : extractContent(data);
4570
4599
  if (result === void 0) {
4571
4600
  throw new Error(`[UniversalLLMAdapter] Could not extract text from path '${extractPath}' in response.`);
4572
4601
  }
@@ -4708,7 +4737,7 @@ ${context != null ? context : "None"}` },
4708
4737
  });
4709
4738
  }
4710
4739
  async embed(text) {
4711
- var _a2, _b, _c, _d;
4740
+ var _a2, _b, _c, _d, _e;
4712
4741
  const path2 = (_a2 = this.opts.embedPath) != null ? _a2 : "/embeddings";
4713
4742
  let payload;
4714
4743
  if (this.opts.embedPayloadTemplate) {
@@ -4722,8 +4751,15 @@ ${context != null ? context : "None"}` },
4722
4751
  input: text
4723
4752
  };
4724
4753
  }
4725
- const isSelfHost = this.baseUrl.includes("retrivora.com") || this.baseUrl.includes("localhost");
4726
- if (isSelfHost || Boolean(process.env.VERCEL)) {
4754
+ try {
4755
+ const { data: data2 } = await this.http.post(path2, payload);
4756
+ const extractPath2 = (_b = this.opts.embedExtractPath) != null ? _b : "data[0].embedding";
4757
+ const vector2 = resolvePath(data2, extractPath2);
4758
+ if (Array.isArray(vector2)) {
4759
+ return vector2;
4760
+ }
4761
+ } catch (httpErr) {
4762
+ console.warn(`[UniversalLLMAdapter] Direct HTTP embedding POST to ${this.baseUrl}${path2} failed (${httpErr.message}). Attempting in-process gateway dispatch fallback...`);
4727
4763
  const _g2 = globalThis;
4728
4764
  let dispatch = _g2.__retrivoraDispatchEmbedding;
4729
4765
  if (typeof dispatch !== "function") {
@@ -4738,24 +4774,18 @@ ${context != null ? context : "None"}` },
4738
4774
  }
4739
4775
  }
4740
4776
  if (typeof dispatch === "function") {
4741
- try {
4742
- const res = await dispatch({
4743
- model: this.model,
4744
- input: text
4745
- }, this.apiKey);
4746
- if ((_c = (_b = res == null ? void 0 : res.data) == null ? void 0 : _b[0]) == null ? void 0 : _c.embedding) {
4747
- return res.data[0].embedding;
4748
- }
4749
- throw new Error(`[UniversalLLMAdapter] In-process embedding dispatch returned invalid vector for model: ${this.model}`);
4750
- } catch (dispatchErr) {
4751
- throw dispatchErr;
4777
+ const res = await dispatch({
4778
+ model: this.model,
4779
+ input: text
4780
+ }, this.apiKey);
4781
+ if ((_d = (_c = res == null ? void 0 : res.data) == null ? void 0 : _c[0]) == null ? void 0 : _d.embedding) {
4782
+ return res.data[0].embedding;
4752
4783
  }
4753
- } else {
4754
- 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.`);
4755
4784
  }
4785
+ throw httpErr;
4756
4786
  }
4757
4787
  const { data } = await this.http.post(path2, payload);
4758
- const extractPath = (_d = this.opts.embedExtractPath) != null ? _d : "data[0].embedding";
4788
+ const extractPath = (_e = this.opts.embedExtractPath) != null ? _e : "data[0].embedding";
4759
4789
  const vector = resolvePath(data, extractPath);
4760
4790
  if (!Array.isArray(vector)) {
4761
4791
  throw new Error(`[UniversalLLMAdapter] Expected a number array at '${extractPath}' for embeddings.`);