@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.
package/dist/server.js CHANGED
@@ -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}':`, {
@@ -3113,7 +3141,7 @@ function getRagConfig(baseConfig, env = process.env) {
3113
3141
  return getEnvConfig(env, baseConfig);
3114
3142
  }
3115
3143
  function getEnvConfig(env = process.env, base) {
3116
- 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;
3144
+ 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;
3117
3145
  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__";
3118
3146
  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;
3119
3147
  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);
@@ -3193,25 +3221,25 @@ function getEnvConfig(env = process.env, base) {
3193
3221
  return true;
3194
3222
  }
3195
3223
  })();
3196
- const DEFAULT_FREE_GATEWAY = "https://llm.retrivora.com/api/v1";
3224
+ const defaultGatewayUrl = (_ma = (_la = readString(env, "LITELLM_BASE_URL")) != null ? _la : readString(env, "LLM_BASE_URL")) != null ? _ma : "https://llm.retrivora.com/api/v1";
3197
3225
  const defaultLlmProvider = isFreeTier ? "universal_rest" : "universal_rest";
3198
- 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;
3199
- 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;
3200
- 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";
3201
- 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";
3202
- 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";
3226
+ 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;
3227
+ 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;
3228
+ 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";
3229
+ 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;
3230
+ 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";
3203
3231
  const defaultEmbeddingProvider = isFreeTier ? "universal_rest" : "universal_rest";
3204
- 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;
3205
- 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;
3206
- 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";
3207
- 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";
3208
- 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";
3232
+ 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;
3233
+ 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;
3234
+ 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";
3235
+ 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;
3236
+ 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";
3209
3237
  return __spreadProps(__spreadValues({
3210
3238
  projectId,
3211
3239
  licenseKey,
3212
3240
  vectorDb: {
3213
3241
  provider: vectorProvider,
3214
- indexName: (_Wa = (_Va = readString(env, "VECTOR_DB_INDEX")) != null ? _Va : vectorDbOptions.indexName) != null ? _Wa : "rag-index",
3242
+ indexName: (_db = (_cb = readString(env, "VECTOR_DB_INDEX")) != null ? _cb : vectorDbOptions.indexName) != null ? _db : "rag-index",
3215
3243
  options: vectorDbOptions
3216
3244
  },
3217
3245
  llm: {
@@ -3241,30 +3269,30 @@ function getEnvConfig(env = process.env, base) {
3241
3269
  }
3242
3270
  },
3243
3271
  ui: {
3244
- title: (_Ya = (_Xa = readString(env, "NEXT_PUBLIC_UI_TITLE")) != null ? _Xa : readString(env, "UI_TITLE")) != null ? _Ya : "AI Assistant",
3245
- subtitle: (__a = (_Za = readString(env, "NEXT_PUBLIC_UI_SUBTITLE")) != null ? _Za : readString(env, "UI_SUBTITLE")) != null ? __a : "Powered by RAG",
3246
- primaryColor: (_ab = (_$a = readString(env, "NEXT_PUBLIC_PRIMARY_COLOR")) != null ? _$a : readString(env, "UI_PRIMARY_COLOR")) != null ? _ab : "#10b981",
3247
- accentColor: (_cb = (_bb = readString(env, "NEXT_PUBLIC_ACCENT_COLOR")) != null ? _bb : readString(env, "UI_ACCENT_COLOR")) != null ? _cb : "#3b82f6",
3248
- logoUrl: (_db = readString(env, "NEXT_PUBLIC_LOGO_URL")) != null ? _db : readString(env, "UI_LOGO_URL"),
3249
- placeholder: (_fb = (_eb = readString(env, "NEXT_PUBLIC_PLACEHOLDER")) != null ? _eb : readString(env, "UI_PLACEHOLDER")) != null ? _fb : "Ask me anything\u2026",
3250
- showSources: ((_hb = (_gb = readString(env, "NEXT_PUBLIC_SHOW_SOURCES")) != null ? _gb : readString(env, "UI_SHOW_SOURCES")) != null ? _hb : "true") !== "false",
3251
- 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.",
3252
- visualStyle: (_lb = (_kb = readString(env, "NEXT_PUBLIC_UI_VISUAL_STYLE")) != null ? _kb : readString(env, "UI_VISUAL_STYLE")) != null ? _lb : "glass",
3253
- borderRadius: (_nb = (_mb = readString(env, "NEXT_PUBLIC_UI_BORDER_RADIUS")) != null ? _mb : readString(env, "UI_BORDER_RADIUS")) != null ? _nb : "xl",
3254
- allowUpload: ((_pb = (_ob = readString(env, "NEXT_PUBLIC_ALLOW_UPLOAD")) != null ? _ob : readString(env, "UI_ALLOW_UPLOAD")) != null ? _pb : "false") === "true"
3272
+ title: (_fb = (_eb = readString(env, "NEXT_PUBLIC_UI_TITLE")) != null ? _eb : readString(env, "UI_TITLE")) != null ? _fb : "AI Assistant",
3273
+ subtitle: (_hb = (_gb = readString(env, "NEXT_PUBLIC_UI_SUBTITLE")) != null ? _gb : readString(env, "UI_SUBTITLE")) != null ? _hb : "Powered by RAG",
3274
+ primaryColor: (_jb = (_ib = readString(env, "NEXT_PUBLIC_PRIMARY_COLOR")) != null ? _ib : readString(env, "UI_PRIMARY_COLOR")) != null ? _jb : "#10b981",
3275
+ accentColor: (_lb = (_kb = readString(env, "NEXT_PUBLIC_ACCENT_COLOR")) != null ? _kb : readString(env, "UI_ACCENT_COLOR")) != null ? _lb : "#3b82f6",
3276
+ logoUrl: (_mb = readString(env, "NEXT_PUBLIC_LOGO_URL")) != null ? _mb : readString(env, "UI_LOGO_URL"),
3277
+ placeholder: (_ob = (_nb = readString(env, "NEXT_PUBLIC_PLACEHOLDER")) != null ? _nb : readString(env, "UI_PLACEHOLDER")) != null ? _ob : "Ask me anything\u2026",
3278
+ showSources: ((_qb = (_pb = readString(env, "NEXT_PUBLIC_SHOW_SOURCES")) != null ? _pb : readString(env, "UI_SHOW_SOURCES")) != null ? _qb : "true") !== "false",
3279
+ 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.",
3280
+ visualStyle: (_ub = (_tb = readString(env, "NEXT_PUBLIC_UI_VISUAL_STYLE")) != null ? _tb : readString(env, "UI_VISUAL_STYLE")) != null ? _ub : "glass",
3281
+ borderRadius: (_wb = (_vb = readString(env, "NEXT_PUBLIC_UI_BORDER_RADIUS")) != null ? _vb : readString(env, "UI_BORDER_RADIUS")) != null ? _wb : "xl",
3282
+ allowUpload: ((_yb = (_xb = readString(env, "NEXT_PUBLIC_ALLOW_UPLOAD")) != null ? _xb : readString(env, "UI_ALLOW_UPLOAD")) != null ? _yb : "false") === "true"
3255
3283
  },
3256
3284
  rag: {
3257
3285
  topK: readNumber(env, "RAG_TOP_K", 5),
3258
3286
  scoreThreshold: readNumber(env, "RAG_SCORE_THRESHOLD", 0),
3259
3287
  chunkSize: readNumber(env, "RAG_CHUNK_SIZE", 1e3),
3260
3288
  chunkOverlap: readNumber(env, "RAG_CHUNK_OVERLAP", 200),
3261
- filterableFields: (_qb = readString(env, "RAG_FILTERABLE_FIELDS")) == null ? void 0 : _qb.split(",").map((f) => f.trim()),
3289
+ filterableFields: (_zb = readString(env, "RAG_FILTERABLE_FIELDS")) == null ? void 0 : _zb.split(",").map((f) => f.trim()),
3262
3290
  // Query pipeline toggles — read from .env.local
3263
3291
  useQueryTransformation: readString(env, "RAG_USE_QUERY_TRANSFORMATION") === "true",
3264
3292
  useReranking: readString(env, "RAG_USE_RERANKING") === "true",
3265
3293
  useGraphRetrieval: readString(env, "RAG_USE_GRAPH_RETRIEVAL") === "true",
3266
- architecture: (_rb = readString(env, "RAG_ARCHITECTURE")) != null ? _rb : "simple",
3267
- chunkingStrategy: (_sb = readString(env, "RAG_CHUNKING_STRATEGY")) != null ? _sb : "recursive",
3294
+ architecture: (_Ab = readString(env, "RAG_ARCHITECTURE")) != null ? _Ab : "simple",
3295
+ chunkingStrategy: (_Bb = readString(env, "RAG_CHUNKING_STRATEGY")) != null ? _Bb : "recursive",
3268
3296
  uiMapping: (() => {
3269
3297
  const raw = readString(env, "RAG_UI_MAPPING");
3270
3298
  if (!raw) return void 0;
@@ -3277,7 +3305,7 @@ function getEnvConfig(env = process.env, base) {
3277
3305
  },
3278
3306
  telemetry: {
3279
3307
  enabled: telemetryEnabled,
3280
- 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"
3308
+ 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"
3281
3309
  }
3282
3310
  }, readString(env, "GRAPH_DB_PROVIDER") ? {
3283
3311
  graphDb: {
@@ -4635,7 +4663,7 @@ var UniversalLLMAdapter = class {
4635
4663
  });
4636
4664
  }
4637
4665
  async chat(messages, context) {
4638
- var _a2, _b, _c;
4666
+ var _a2, _b, _c, _d, _e;
4639
4667
  const path2 = (_a2 = this.opts.chatPath) != null ? _a2 : "/chat/completions";
4640
4668
  const safeMessages = (Array.isArray(messages) ? messages : []).filter((m) => Boolean(m && typeof m === "object")).map((m) => ({
4641
4669
  role: m.role || "user",
@@ -4664,8 +4692,15 @@ ${context != null ? context : "None"}` },
4664
4692
  temperature: this.temperature
4665
4693
  };
4666
4694
  }
4667
- const isSelfHost = this.baseUrl.includes("retrivora.com") || this.baseUrl.includes("localhost");
4668
- if (isSelfHost || Boolean(process.env.VERCEL)) {
4695
+ try {
4696
+ const { data: data2 } = await this.http.post(path2, payload);
4697
+ const extractPath2 = (_b = this.opts.responseExtractPath) != null ? _b : "choices[0].message.content";
4698
+ const result2 = (_c = resolvePath(data2, extractPath2)) != null ? _c : extractContent(data2);
4699
+ if (result2 !== void 0) {
4700
+ return String(result2);
4701
+ }
4702
+ } catch (httpErr) {
4703
+ console.warn(`[UniversalLLMAdapter] Direct HTTP POST to ${this.baseUrl}${path2} failed (${httpErr.message}). Attempting in-process gateway dispatch fallback...`);
4669
4704
  const _g2 = globalThis;
4670
4705
  let dispatch = _g2.__retrivoraDispatchChat;
4671
4706
  if (typeof dispatch !== "function") {
@@ -4680,28 +4715,22 @@ ${context != null ? context : "None"}` },
4680
4715
  }
4681
4716
  }
4682
4717
  if (typeof dispatch === "function") {
4683
- try {
4684
- const res = await dispatch({
4685
- model: this.model,
4686
- messages: formattedMessages,
4687
- max_tokens: this.maxTokens,
4688
- temperature: this.temperature
4689
- }, this.apiKey);
4690
- const content = extractContent(res == null ? void 0 : res.response);
4691
- if (content !== void 0) {
4692
- return content;
4693
- }
4694
- throw new Error(`[UniversalLLMAdapter] In-process dispatch returned empty content for model: ${this.model}`);
4695
- } catch (dispatchErr) {
4696
- throw dispatchErr;
4718
+ const res = await dispatch({
4719
+ model: this.model,
4720
+ messages: formattedMessages,
4721
+ max_tokens: this.maxTokens,
4722
+ temperature: this.temperature
4723
+ }, this.apiKey);
4724
+ const content = extractContent(res == null ? void 0 : res.response);
4725
+ if (content !== void 0) {
4726
+ return content;
4697
4727
  }
4698
- } else {
4699
- 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.`);
4700
4728
  }
4729
+ throw httpErr;
4701
4730
  }
4702
4731
  const { data } = await this.http.post(path2, payload);
4703
- const extractPath = (_b = this.opts.responseExtractPath) != null ? _b : "choices[0].message.content";
4704
- const result = (_c = resolvePath(data, extractPath)) != null ? _c : extractContent(data);
4732
+ const extractPath = (_d = this.opts.responseExtractPath) != null ? _d : "choices[0].message.content";
4733
+ const result = (_e = resolvePath(data, extractPath)) != null ? _e : extractContent(data);
4705
4734
  if (result === void 0) {
4706
4735
  throw new Error(`[UniversalLLMAdapter] Could not extract text from path '${extractPath}' in response.`);
4707
4736
  }
@@ -4843,7 +4872,7 @@ ${context != null ? context : "None"}` },
4843
4872
  });
4844
4873
  }
4845
4874
  async embed(text) {
4846
- var _a2, _b, _c, _d;
4875
+ var _a2, _b, _c, _d, _e;
4847
4876
  const path2 = (_a2 = this.opts.embedPath) != null ? _a2 : "/embeddings";
4848
4877
  let payload;
4849
4878
  if (this.opts.embedPayloadTemplate) {
@@ -4857,8 +4886,15 @@ ${context != null ? context : "None"}` },
4857
4886
  input: text
4858
4887
  };
4859
4888
  }
4860
- const isSelfHost = this.baseUrl.includes("retrivora.com") || this.baseUrl.includes("localhost");
4861
- if (isSelfHost || Boolean(process.env.VERCEL)) {
4889
+ try {
4890
+ const { data: data2 } = await this.http.post(path2, payload);
4891
+ const extractPath2 = (_b = this.opts.embedExtractPath) != null ? _b : "data[0].embedding";
4892
+ const vector2 = resolvePath(data2, extractPath2);
4893
+ if (Array.isArray(vector2)) {
4894
+ return vector2;
4895
+ }
4896
+ } catch (httpErr) {
4897
+ console.warn(`[UniversalLLMAdapter] Direct HTTP embedding POST to ${this.baseUrl}${path2} failed (${httpErr.message}). Attempting in-process gateway dispatch fallback...`);
4862
4898
  const _g2 = globalThis;
4863
4899
  let dispatch = _g2.__retrivoraDispatchEmbedding;
4864
4900
  if (typeof dispatch !== "function") {
@@ -4873,24 +4909,18 @@ ${context != null ? context : "None"}` },
4873
4909
  }
4874
4910
  }
4875
4911
  if (typeof dispatch === "function") {
4876
- try {
4877
- const res = await dispatch({
4878
- model: this.model,
4879
- input: text
4880
- }, this.apiKey);
4881
- if ((_c = (_b = res == null ? void 0 : res.data) == null ? void 0 : _b[0]) == null ? void 0 : _c.embedding) {
4882
- return res.data[0].embedding;
4883
- }
4884
- throw new Error(`[UniversalLLMAdapter] In-process embedding dispatch returned invalid vector for model: ${this.model}`);
4885
- } catch (dispatchErr) {
4886
- throw dispatchErr;
4912
+ const res = await dispatch({
4913
+ model: this.model,
4914
+ input: text
4915
+ }, this.apiKey);
4916
+ if ((_d = (_c = res == null ? void 0 : res.data) == null ? void 0 : _c[0]) == null ? void 0 : _d.embedding) {
4917
+ return res.data[0].embedding;
4887
4918
  }
4888
- } else {
4889
- 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.`);
4890
4919
  }
4920
+ throw httpErr;
4891
4921
  }
4892
4922
  const { data } = await this.http.post(path2, payload);
4893
- const extractPath = (_d = this.opts.embedExtractPath) != null ? _d : "data[0].embedding";
4923
+ const extractPath = (_e = this.opts.embedExtractPath) != null ? _e : "data[0].embedding";
4894
4924
  const vector = resolvePath(data, extractPath);
4895
4925
  if (!Array.isArray(vector)) {
4896
4926
  throw new Error(`[UniversalLLMAdapter] Expected a number array at '${extractPath}' for embeddings.`);
package/dist/server.mjs CHANGED
@@ -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 getRagConfig(baseConfig, env = process.env) {
3017
3045
  return getEnvConfig(env, baseConfig);
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: {
@@ -4539,7 +4567,7 @@ var UniversalLLMAdapter = class {
4539
4567
  });
4540
4568
  }
4541
4569
  async chat(messages, context) {
4542
- var _a2, _b, _c;
4570
+ var _a2, _b, _c, _d, _e;
4543
4571
  const path2 = (_a2 = this.opts.chatPath) != null ? _a2 : "/chat/completions";
4544
4572
  const safeMessages = (Array.isArray(messages) ? messages : []).filter((m) => Boolean(m && typeof m === "object")).map((m) => ({
4545
4573
  role: m.role || "user",
@@ -4568,8 +4596,15 @@ ${context != null ? context : "None"}` },
4568
4596
  temperature: this.temperature
4569
4597
  };
4570
4598
  }
4571
- const isSelfHost = this.baseUrl.includes("retrivora.com") || this.baseUrl.includes("localhost");
4572
- if (isSelfHost || Boolean(process.env.VERCEL)) {
4599
+ try {
4600
+ const { data: data2 } = await this.http.post(path2, payload);
4601
+ const extractPath2 = (_b = this.opts.responseExtractPath) != null ? _b : "choices[0].message.content";
4602
+ const result2 = (_c = resolvePath(data2, extractPath2)) != null ? _c : extractContent(data2);
4603
+ if (result2 !== void 0) {
4604
+ return String(result2);
4605
+ }
4606
+ } catch (httpErr) {
4607
+ console.warn(`[UniversalLLMAdapter] Direct HTTP POST to ${this.baseUrl}${path2} failed (${httpErr.message}). Attempting in-process gateway dispatch fallback...`);
4573
4608
  const _g2 = globalThis;
4574
4609
  let dispatch = _g2.__retrivoraDispatchChat;
4575
4610
  if (typeof dispatch !== "function") {
@@ -4584,28 +4619,22 @@ ${context != null ? context : "None"}` },
4584
4619
  }
4585
4620
  }
4586
4621
  if (typeof dispatch === "function") {
4587
- try {
4588
- const res = await dispatch({
4589
- model: this.model,
4590
- messages: formattedMessages,
4591
- max_tokens: this.maxTokens,
4592
- temperature: this.temperature
4593
- }, this.apiKey);
4594
- const content = extractContent(res == null ? void 0 : res.response);
4595
- if (content !== void 0) {
4596
- return content;
4597
- }
4598
- throw new Error(`[UniversalLLMAdapter] In-process dispatch returned empty content for model: ${this.model}`);
4599
- } catch (dispatchErr) {
4600
- throw dispatchErr;
4622
+ const res = await dispatch({
4623
+ model: this.model,
4624
+ messages: formattedMessages,
4625
+ max_tokens: this.maxTokens,
4626
+ temperature: this.temperature
4627
+ }, this.apiKey);
4628
+ const content = extractContent(res == null ? void 0 : res.response);
4629
+ if (content !== void 0) {
4630
+ return content;
4601
4631
  }
4602
- } else {
4603
- 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.`);
4604
4632
  }
4633
+ throw httpErr;
4605
4634
  }
4606
4635
  const { data } = await this.http.post(path2, payload);
4607
- const extractPath = (_b = this.opts.responseExtractPath) != null ? _b : "choices[0].message.content";
4608
- const result = (_c = resolvePath(data, extractPath)) != null ? _c : extractContent(data);
4636
+ const extractPath = (_d = this.opts.responseExtractPath) != null ? _d : "choices[0].message.content";
4637
+ const result = (_e = resolvePath(data, extractPath)) != null ? _e : extractContent(data);
4609
4638
  if (result === void 0) {
4610
4639
  throw new Error(`[UniversalLLMAdapter] Could not extract text from path '${extractPath}' in response.`);
4611
4640
  }
@@ -4747,7 +4776,7 @@ ${context != null ? context : "None"}` },
4747
4776
  });
4748
4777
  }
4749
4778
  async embed(text) {
4750
- var _a2, _b, _c, _d;
4779
+ var _a2, _b, _c, _d, _e;
4751
4780
  const path2 = (_a2 = this.opts.embedPath) != null ? _a2 : "/embeddings";
4752
4781
  let payload;
4753
4782
  if (this.opts.embedPayloadTemplate) {
@@ -4761,8 +4790,15 @@ ${context != null ? context : "None"}` },
4761
4790
  input: text
4762
4791
  };
4763
4792
  }
4764
- const isSelfHost = this.baseUrl.includes("retrivora.com") || this.baseUrl.includes("localhost");
4765
- if (isSelfHost || Boolean(process.env.VERCEL)) {
4793
+ try {
4794
+ const { data: data2 } = await this.http.post(path2, payload);
4795
+ const extractPath2 = (_b = this.opts.embedExtractPath) != null ? _b : "data[0].embedding";
4796
+ const vector2 = resolvePath(data2, extractPath2);
4797
+ if (Array.isArray(vector2)) {
4798
+ return vector2;
4799
+ }
4800
+ } catch (httpErr) {
4801
+ console.warn(`[UniversalLLMAdapter] Direct HTTP embedding POST to ${this.baseUrl}${path2} failed (${httpErr.message}). Attempting in-process gateway dispatch fallback...`);
4766
4802
  const _g2 = globalThis;
4767
4803
  let dispatch = _g2.__retrivoraDispatchEmbedding;
4768
4804
  if (typeof dispatch !== "function") {
@@ -4777,24 +4813,18 @@ ${context != null ? context : "None"}` },
4777
4813
  }
4778
4814
  }
4779
4815
  if (typeof dispatch === "function") {
4780
- try {
4781
- const res = await dispatch({
4782
- model: this.model,
4783
- input: text
4784
- }, this.apiKey);
4785
- if ((_c = (_b = res == null ? void 0 : res.data) == null ? void 0 : _b[0]) == null ? void 0 : _c.embedding) {
4786
- return res.data[0].embedding;
4787
- }
4788
- throw new Error(`[UniversalLLMAdapter] In-process embedding dispatch returned invalid vector for model: ${this.model}`);
4789
- } catch (dispatchErr) {
4790
- throw dispatchErr;
4816
+ const res = await dispatch({
4817
+ model: this.model,
4818
+ input: text
4819
+ }, this.apiKey);
4820
+ if ((_d = (_c = res == null ? void 0 : res.data) == null ? void 0 : _c[0]) == null ? void 0 : _d.embedding) {
4821
+ return res.data[0].embedding;
4791
4822
  }
4792
- } else {
4793
- 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.`);
4794
4823
  }
4824
+ throw httpErr;
4795
4825
  }
4796
4826
  const { data } = await this.http.post(path2, payload);
4797
- const extractPath = (_d = this.opts.embedExtractPath) != null ? _d : "data[0].embedding";
4827
+ const extractPath = (_e = this.opts.embedExtractPath) != null ? _e : "data[0].embedding";
4798
4828
  const vector = resolvePath(data, extractPath);
4799
4829
  if (!Array.isArray(vector)) {
4800
4830
  throw new Error(`[UniversalLLMAdapter] Expected a number array at '${extractPath}' for embeddings.`);