@retrivora-ai/rag-engine 2.1.4 → 2.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5247,7 +5247,7 @@ var ConfigValidator = class {
5247
5247
  // package.json
5248
5248
  var package_default = {
5249
5249
  name: "@retrivora-ai/rag-engine",
5250
- version: "2.1.4",
5250
+ version: "2.1.5",
5251
5251
  description: "Retrivora AI is a plug-and-play AI engine for RAG chat experiences \u2014 generic vector DB + LLM provider, embeddable or standalone.",
5252
5252
  author: "Abhinav Alkuchi",
5253
5253
  license: "UNLICENSED",
@@ -10941,12 +10941,19 @@ function createStreamHandler(configOrPlugin, options) {
10941
10941
  const storage = new DatabaseStorage(plugin.getConfig());
10942
10942
  const onAuthorize = options == null ? void 0 : options.onAuthorize;
10943
10943
  return async function POST(req) {
10944
+ var _a2;
10944
10945
  const authResult = await checkAuth(req, onAuthorize);
10945
10946
  if (authResult) return authResult;
10946
10947
  const rateLimited = checkRateLimit(req);
10947
10948
  if (rateLimited) return rateLimited;
10948
10949
  let body;
10949
10950
  try {
10951
+ if ((_a2 = req.headers.get("content-type")) == null ? void 0 : _a2.includes("multipart/form-data")) {
10952
+ return new Response(JSON.stringify({ error: { code: "INVALID_ENDPOINT", message: "Multipart file upload requests must be sent to the /upload endpoint." } }), {
10953
+ status: 400,
10954
+ headers: { "Content-Type": "application/json" }
10955
+ });
10956
+ }
10950
10957
  body = await req.json();
10951
10958
  } catch (e) {
10952
10959
  return new Response(JSON.stringify({ error: { code: "INVALID_JSON", message: "Invalid JSON body" } }), {
@@ -10984,7 +10991,7 @@ function createStreamHandler(configOrPlugin, options) {
10984
10991
  let isActive = true;
10985
10992
  const stream = new ReadableStream({
10986
10993
  async start(controller) {
10987
- var _a2, _b, _c, _d, _e;
10994
+ var _a3, _b, _c, _d, _e;
10988
10995
  const enqueue = (text) => {
10989
10996
  if (!isActive) return;
10990
10997
  try {
@@ -11017,7 +11024,7 @@ function createStreamHandler(configOrPlugin, options) {
11017
11024
  let uiTransformation = responseChunk == null ? void 0 : responseChunk.ui_transformation;
11018
11025
  if (sources.length > 0) {
11019
11026
  try {
11020
- uiTransformation = (_a2 = responseChunk == null ? void 0 : responseChunk.ui_transformation) != null ? _a2 : UITransformer.transform(message, sources, plugin.getConfig());
11027
+ uiTransformation = (_a3 = responseChunk == null ? void 0 : responseChunk.ui_transformation) != null ? _a3 : UITransformer.transform(message, sources, plugin.getConfig());
11021
11028
  if (uiTransformation) {
11022
11029
  enqueue(sseUIFrame(uiTransformation));
11023
11030
  }
@@ -11414,16 +11421,32 @@ function createRagHandler(configOrPlugin, options) {
11414
11421
  return import_server.NextResponse.json({ error: `Method Not Allowed: GET segment "${segment}" not supported.` }, { status: 405 });
11415
11422
  }
11416
11423
  }
11417
- async function getSegment(context) {
11418
- var _a2;
11424
+ async function getSegment(req, context) {
11425
+ var _a2, _b;
11426
+ const contentType = req.headers.get("content-type") || "";
11427
+ if (contentType.includes("multipart/form-data")) {
11428
+ return "upload";
11429
+ }
11430
+ try {
11431
+ const url = new URL(req.url);
11432
+ const pathname = url.pathname;
11433
+ if (pathname.endsWith("/upload")) return "upload";
11434
+ if (pathname.endsWith("/chat") || pathname.endsWith("/chat-sync")) return "chat";
11435
+ if (pathname.endsWith("/health")) return "health";
11436
+ if (pathname.endsWith("/suggestions")) return "suggestions";
11437
+ if (pathname.endsWith("/feedback")) return "feedback";
11438
+ if (pathname.endsWith("/history")) return "history";
11439
+ } catch (e) {
11440
+ }
11419
11441
  const resolvedParams = typeof ((_a2 = context == null ? void 0 : context.params) == null ? void 0 : _a2.then) === "function" ? await context.params : context == null ? void 0 : context.params;
11420
- const segments = (resolvedParams == null ? void 0 : resolvedParams.retrivora) || [];
11421
- return segments.join("/") || "chat";
11442
+ const segments = (resolvedParams == null ? void 0 : resolvedParams.retrivora) || ((_b = resolvedParams == null ? void 0 : resolvedParams.params) == null ? void 0 : _b.retrivora) || [];
11443
+ const joined = segments.join("/");
11444
+ return joined || "chat";
11422
11445
  }
11423
11446
  return {
11424
11447
  GET: async (req, context) => {
11425
11448
  try {
11426
- const segment = await getSegment(context);
11449
+ const segment = await getSegment(req, context);
11427
11450
  return await routeGetRequest(req, segment);
11428
11451
  } catch (err) {
11429
11452
  const msg = err instanceof Error ? err.message : "GET Routing failed";
@@ -11432,7 +11455,7 @@ function createRagHandler(configOrPlugin, options) {
11432
11455
  },
11433
11456
  POST: async (req, context) => {
11434
11457
  try {
11435
- const segment = await getSegment(context);
11458
+ const segment = await getSegment(req, context);
11436
11459
  return await routePostRequest(req, segment);
11437
11460
  } catch (err) {
11438
11461
  const msg = err instanceof Error ? err.message : "POST Routing failed";
@@ -5212,7 +5212,7 @@ var ConfigValidator = class {
5212
5212
  // package.json
5213
5213
  var package_default = {
5214
5214
  name: "@retrivora-ai/rag-engine",
5215
- version: "2.1.4",
5215
+ version: "2.1.5",
5216
5216
  description: "Retrivora AI is a plug-and-play AI engine for RAG chat experiences \u2014 generic vector DB + LLM provider, embeddable or standalone.",
5217
5217
  author: "Abhinav Alkuchi",
5218
5218
  license: "UNLICENSED",
@@ -10906,12 +10906,19 @@ function createStreamHandler(configOrPlugin, options) {
10906
10906
  const storage = new DatabaseStorage(plugin.getConfig());
10907
10907
  const onAuthorize = options == null ? void 0 : options.onAuthorize;
10908
10908
  return async function POST(req) {
10909
+ var _a2;
10909
10910
  const authResult = await checkAuth(req, onAuthorize);
10910
10911
  if (authResult) return authResult;
10911
10912
  const rateLimited = checkRateLimit(req);
10912
10913
  if (rateLimited) return rateLimited;
10913
10914
  let body;
10914
10915
  try {
10916
+ if ((_a2 = req.headers.get("content-type")) == null ? void 0 : _a2.includes("multipart/form-data")) {
10917
+ return new Response(JSON.stringify({ error: { code: "INVALID_ENDPOINT", message: "Multipart file upload requests must be sent to the /upload endpoint." } }), {
10918
+ status: 400,
10919
+ headers: { "Content-Type": "application/json" }
10920
+ });
10921
+ }
10915
10922
  body = await req.json();
10916
10923
  } catch (e) {
10917
10924
  return new Response(JSON.stringify({ error: { code: "INVALID_JSON", message: "Invalid JSON body" } }), {
@@ -10949,7 +10956,7 @@ function createStreamHandler(configOrPlugin, options) {
10949
10956
  let isActive = true;
10950
10957
  const stream = new ReadableStream({
10951
10958
  async start(controller) {
10952
- var _a2, _b, _c, _d, _e;
10959
+ var _a3, _b, _c, _d, _e;
10953
10960
  const enqueue = (text) => {
10954
10961
  if (!isActive) return;
10955
10962
  try {
@@ -10982,7 +10989,7 @@ function createStreamHandler(configOrPlugin, options) {
10982
10989
  let uiTransformation = responseChunk == null ? void 0 : responseChunk.ui_transformation;
10983
10990
  if (sources.length > 0) {
10984
10991
  try {
10985
- uiTransformation = (_a2 = responseChunk == null ? void 0 : responseChunk.ui_transformation) != null ? _a2 : UITransformer.transform(message, sources, plugin.getConfig());
10992
+ uiTransformation = (_a3 = responseChunk == null ? void 0 : responseChunk.ui_transformation) != null ? _a3 : UITransformer.transform(message, sources, plugin.getConfig());
10986
10993
  if (uiTransformation) {
10987
10994
  enqueue(sseUIFrame(uiTransformation));
10988
10995
  }
@@ -11379,16 +11386,32 @@ function createRagHandler(configOrPlugin, options) {
11379
11386
  return NextResponse.json({ error: `Method Not Allowed: GET segment "${segment}" not supported.` }, { status: 405 });
11380
11387
  }
11381
11388
  }
11382
- async function getSegment(context) {
11383
- var _a2;
11389
+ async function getSegment(req, context) {
11390
+ var _a2, _b;
11391
+ const contentType = req.headers.get("content-type") || "";
11392
+ if (contentType.includes("multipart/form-data")) {
11393
+ return "upload";
11394
+ }
11395
+ try {
11396
+ const url = new URL(req.url);
11397
+ const pathname = url.pathname;
11398
+ if (pathname.endsWith("/upload")) return "upload";
11399
+ if (pathname.endsWith("/chat") || pathname.endsWith("/chat-sync")) return "chat";
11400
+ if (pathname.endsWith("/health")) return "health";
11401
+ if (pathname.endsWith("/suggestions")) return "suggestions";
11402
+ if (pathname.endsWith("/feedback")) return "feedback";
11403
+ if (pathname.endsWith("/history")) return "history";
11404
+ } catch (e) {
11405
+ }
11384
11406
  const resolvedParams = typeof ((_a2 = context == null ? void 0 : context.params) == null ? void 0 : _a2.then) === "function" ? await context.params : context == null ? void 0 : context.params;
11385
- const segments = (resolvedParams == null ? void 0 : resolvedParams.retrivora) || [];
11386
- return segments.join("/") || "chat";
11407
+ const segments = (resolvedParams == null ? void 0 : resolvedParams.retrivora) || ((_b = resolvedParams == null ? void 0 : resolvedParams.params) == null ? void 0 : _b.retrivora) || [];
11408
+ const joined = segments.join("/");
11409
+ return joined || "chat";
11387
11410
  }
11388
11411
  return {
11389
11412
  GET: async (req, context) => {
11390
11413
  try {
11391
- const segment = await getSegment(context);
11414
+ const segment = await getSegment(req, context);
11392
11415
  return await routeGetRequest(req, segment);
11393
11416
  } catch (err) {
11394
11417
  const msg = err instanceof Error ? err.message : "GET Routing failed";
@@ -11397,7 +11420,7 @@ function createRagHandler(configOrPlugin, options) {
11397
11420
  },
11398
11421
  POST: async (req, context) => {
11399
11422
  try {
11400
- const segment = await getSegment(context);
11423
+ const segment = await getSegment(req, context);
11401
11424
  return await routePostRequest(req, segment);
11402
11425
  } catch (err) {
11403
11426
  const msg = err instanceof Error ? err.message : "POST Routing failed";
package/dist/index.js CHANGED
@@ -3570,7 +3570,7 @@ function ChatWidget({ position = "bottom-right", onAddToCart, apiUrl, retrivoraA
3570
3570
  // package.json
3571
3571
  var package_default = {
3572
3572
  name: "@retrivora-ai/rag-engine",
3573
- version: "2.1.4",
3573
+ version: "2.1.5",
3574
3574
  description: "Retrivora AI is a plug-and-play AI engine for RAG chat experiences \u2014 generic vector DB + LLM provider, embeddable or standalone.",
3575
3575
  author: "Abhinav Alkuchi",
3576
3576
  license: "UNLICENSED",
package/dist/index.mjs CHANGED
@@ -3571,7 +3571,7 @@ function ChatWidget({ position = "bottom-right", onAddToCart, apiUrl, retrivoraA
3571
3571
  // package.json
3572
3572
  var package_default = {
3573
3573
  name: "@retrivora-ai/rag-engine",
3574
- version: "2.1.4",
3574
+ version: "2.1.5",
3575
3575
  description: "Retrivora AI is a plug-and-play AI engine for RAG chat experiences \u2014 generic vector DB + LLM provider, embeddable or standalone.",
3576
3576
  author: "Abhinav Alkuchi",
3577
3577
  license: "UNLICENSED",
package/dist/server.js CHANGED
@@ -5348,7 +5348,7 @@ var ConfigValidator = class {
5348
5348
  // package.json
5349
5349
  var package_default = {
5350
5350
  name: "@retrivora-ai/rag-engine",
5351
- version: "2.1.4",
5351
+ version: "2.1.5",
5352
5352
  description: "Retrivora AI is a plug-and-play AI engine for RAG chat experiences \u2014 generic vector DB + LLM provider, embeddable or standalone.",
5353
5353
  author: "Abhinav Alkuchi",
5354
5354
  license: "UNLICENSED",
@@ -11355,12 +11355,19 @@ function createStreamHandler(configOrPlugin, options) {
11355
11355
  const storage = new DatabaseStorage(plugin.getConfig());
11356
11356
  const onAuthorize = options == null ? void 0 : options.onAuthorize;
11357
11357
  return async function POST(req) {
11358
+ var _a2;
11358
11359
  const authResult = await checkAuth(req, onAuthorize);
11359
11360
  if (authResult) return authResult;
11360
11361
  const rateLimited = checkRateLimit(req);
11361
11362
  if (rateLimited) return rateLimited;
11362
11363
  let body;
11363
11364
  try {
11365
+ if ((_a2 = req.headers.get("content-type")) == null ? void 0 : _a2.includes("multipart/form-data")) {
11366
+ return new Response(JSON.stringify({ error: { code: "INVALID_ENDPOINT", message: "Multipart file upload requests must be sent to the /upload endpoint." } }), {
11367
+ status: 400,
11368
+ headers: { "Content-Type": "application/json" }
11369
+ });
11370
+ }
11364
11371
  body = await req.json();
11365
11372
  } catch (e) {
11366
11373
  return new Response(JSON.stringify({ error: { code: "INVALID_JSON", message: "Invalid JSON body" } }), {
@@ -11398,7 +11405,7 @@ function createStreamHandler(configOrPlugin, options) {
11398
11405
  let isActive = true;
11399
11406
  const stream = new ReadableStream({
11400
11407
  async start(controller) {
11401
- var _a2, _b, _c, _d, _e;
11408
+ var _a3, _b, _c, _d, _e;
11402
11409
  const enqueue = (text) => {
11403
11410
  if (!isActive) return;
11404
11411
  try {
@@ -11431,7 +11438,7 @@ function createStreamHandler(configOrPlugin, options) {
11431
11438
  let uiTransformation = responseChunk == null ? void 0 : responseChunk.ui_transformation;
11432
11439
  if (sources.length > 0) {
11433
11440
  try {
11434
- uiTransformation = (_a2 = responseChunk == null ? void 0 : responseChunk.ui_transformation) != null ? _a2 : UITransformer.transform(message, sources, plugin.getConfig());
11441
+ uiTransformation = (_a3 = responseChunk == null ? void 0 : responseChunk.ui_transformation) != null ? _a3 : UITransformer.transform(message, sources, plugin.getConfig());
11435
11442
  if (uiTransformation) {
11436
11443
  enqueue(sseUIFrame(uiTransformation));
11437
11444
  }
@@ -11828,16 +11835,32 @@ function createRagHandler(configOrPlugin, options) {
11828
11835
  return import_server.NextResponse.json({ error: `Method Not Allowed: GET segment "${segment}" not supported.` }, { status: 405 });
11829
11836
  }
11830
11837
  }
11831
- async function getSegment(context) {
11832
- var _a2;
11838
+ async function getSegment(req, context) {
11839
+ var _a2, _b;
11840
+ const contentType = req.headers.get("content-type") || "";
11841
+ if (contentType.includes("multipart/form-data")) {
11842
+ return "upload";
11843
+ }
11844
+ try {
11845
+ const url = new URL(req.url);
11846
+ const pathname = url.pathname;
11847
+ if (pathname.endsWith("/upload")) return "upload";
11848
+ if (pathname.endsWith("/chat") || pathname.endsWith("/chat-sync")) return "chat";
11849
+ if (pathname.endsWith("/health")) return "health";
11850
+ if (pathname.endsWith("/suggestions")) return "suggestions";
11851
+ if (pathname.endsWith("/feedback")) return "feedback";
11852
+ if (pathname.endsWith("/history")) return "history";
11853
+ } catch (e) {
11854
+ }
11833
11855
  const resolvedParams = typeof ((_a2 = context == null ? void 0 : context.params) == null ? void 0 : _a2.then) === "function" ? await context.params : context == null ? void 0 : context.params;
11834
- const segments = (resolvedParams == null ? void 0 : resolvedParams.retrivora) || [];
11835
- return segments.join("/") || "chat";
11856
+ const segments = (resolvedParams == null ? void 0 : resolvedParams.retrivora) || ((_b = resolvedParams == null ? void 0 : resolvedParams.params) == null ? void 0 : _b.retrivora) || [];
11857
+ const joined = segments.join("/");
11858
+ return joined || "chat";
11836
11859
  }
11837
11860
  return {
11838
11861
  GET: async (req, context) => {
11839
11862
  try {
11840
- const segment = await getSegment(context);
11863
+ const segment = await getSegment(req, context);
11841
11864
  return await routeGetRequest(req, segment);
11842
11865
  } catch (err) {
11843
11866
  const msg = err instanceof Error ? err.message : "GET Routing failed";
@@ -11846,7 +11869,7 @@ function createRagHandler(configOrPlugin, options) {
11846
11869
  },
11847
11870
  POST: async (req, context) => {
11848
11871
  try {
11849
- const segment = await getSegment(context);
11872
+ const segment = await getSegment(req, context);
11850
11873
  return await routePostRequest(req, segment);
11851
11874
  } catch (err) {
11852
11875
  const msg = err instanceof Error ? err.message : "POST Routing failed";
package/dist/server.mjs CHANGED
@@ -5251,7 +5251,7 @@ var ConfigValidator = class {
5251
5251
  // package.json
5252
5252
  var package_default = {
5253
5253
  name: "@retrivora-ai/rag-engine",
5254
- version: "2.1.4",
5254
+ version: "2.1.5",
5255
5255
  description: "Retrivora AI is a plug-and-play AI engine for RAG chat experiences \u2014 generic vector DB + LLM provider, embeddable or standalone.",
5256
5256
  author: "Abhinav Alkuchi",
5257
5257
  license: "UNLICENSED",
@@ -11258,12 +11258,19 @@ function createStreamHandler(configOrPlugin, options) {
11258
11258
  const storage = new DatabaseStorage(plugin.getConfig());
11259
11259
  const onAuthorize = options == null ? void 0 : options.onAuthorize;
11260
11260
  return async function POST(req) {
11261
+ var _a2;
11261
11262
  const authResult = await checkAuth(req, onAuthorize);
11262
11263
  if (authResult) return authResult;
11263
11264
  const rateLimited = checkRateLimit(req);
11264
11265
  if (rateLimited) return rateLimited;
11265
11266
  let body;
11266
11267
  try {
11268
+ if ((_a2 = req.headers.get("content-type")) == null ? void 0 : _a2.includes("multipart/form-data")) {
11269
+ return new Response(JSON.stringify({ error: { code: "INVALID_ENDPOINT", message: "Multipart file upload requests must be sent to the /upload endpoint." } }), {
11270
+ status: 400,
11271
+ headers: { "Content-Type": "application/json" }
11272
+ });
11273
+ }
11267
11274
  body = await req.json();
11268
11275
  } catch (e) {
11269
11276
  return new Response(JSON.stringify({ error: { code: "INVALID_JSON", message: "Invalid JSON body" } }), {
@@ -11301,7 +11308,7 @@ function createStreamHandler(configOrPlugin, options) {
11301
11308
  let isActive = true;
11302
11309
  const stream = new ReadableStream({
11303
11310
  async start(controller) {
11304
- var _a2, _b, _c, _d, _e;
11311
+ var _a3, _b, _c, _d, _e;
11305
11312
  const enqueue = (text) => {
11306
11313
  if (!isActive) return;
11307
11314
  try {
@@ -11334,7 +11341,7 @@ function createStreamHandler(configOrPlugin, options) {
11334
11341
  let uiTransformation = responseChunk == null ? void 0 : responseChunk.ui_transformation;
11335
11342
  if (sources.length > 0) {
11336
11343
  try {
11337
- uiTransformation = (_a2 = responseChunk == null ? void 0 : responseChunk.ui_transformation) != null ? _a2 : UITransformer.transform(message, sources, plugin.getConfig());
11344
+ uiTransformation = (_a3 = responseChunk == null ? void 0 : responseChunk.ui_transformation) != null ? _a3 : UITransformer.transform(message, sources, plugin.getConfig());
11338
11345
  if (uiTransformation) {
11339
11346
  enqueue(sseUIFrame(uiTransformation));
11340
11347
  }
@@ -11731,16 +11738,32 @@ function createRagHandler(configOrPlugin, options) {
11731
11738
  return NextResponse.json({ error: `Method Not Allowed: GET segment "${segment}" not supported.` }, { status: 405 });
11732
11739
  }
11733
11740
  }
11734
- async function getSegment(context) {
11735
- var _a2;
11741
+ async function getSegment(req, context) {
11742
+ var _a2, _b;
11743
+ const contentType = req.headers.get("content-type") || "";
11744
+ if (contentType.includes("multipart/form-data")) {
11745
+ return "upload";
11746
+ }
11747
+ try {
11748
+ const url = new URL(req.url);
11749
+ const pathname = url.pathname;
11750
+ if (pathname.endsWith("/upload")) return "upload";
11751
+ if (pathname.endsWith("/chat") || pathname.endsWith("/chat-sync")) return "chat";
11752
+ if (pathname.endsWith("/health")) return "health";
11753
+ if (pathname.endsWith("/suggestions")) return "suggestions";
11754
+ if (pathname.endsWith("/feedback")) return "feedback";
11755
+ if (pathname.endsWith("/history")) return "history";
11756
+ } catch (e) {
11757
+ }
11736
11758
  const resolvedParams = typeof ((_a2 = context == null ? void 0 : context.params) == null ? void 0 : _a2.then) === "function" ? await context.params : context == null ? void 0 : context.params;
11737
- const segments = (resolvedParams == null ? void 0 : resolvedParams.retrivora) || [];
11738
- return segments.join("/") || "chat";
11759
+ const segments = (resolvedParams == null ? void 0 : resolvedParams.retrivora) || ((_b = resolvedParams == null ? void 0 : resolvedParams.params) == null ? void 0 : _b.retrivora) || [];
11760
+ const joined = segments.join("/");
11761
+ return joined || "chat";
11739
11762
  }
11740
11763
  return {
11741
11764
  GET: async (req, context) => {
11742
11765
  try {
11743
- const segment = await getSegment(context);
11766
+ const segment = await getSegment(req, context);
11744
11767
  return await routeGetRequest(req, segment);
11745
11768
  } catch (err) {
11746
11769
  const msg = err instanceof Error ? err.message : "GET Routing failed";
@@ -11749,7 +11772,7 @@ function createRagHandler(configOrPlugin, options) {
11749
11772
  },
11750
11773
  POST: async (req, context) => {
11751
11774
  try {
11752
- const segment = await getSegment(context);
11775
+ const segment = await getSegment(req, context);
11753
11776
  return await routePostRequest(req, segment);
11754
11777
  } catch (err) {
11755
11778
  const msg = err instanceof Error ? err.message : "POST Routing failed";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@retrivora-ai/rag-engine",
3
- "version": "2.1.4",
3
+ "version": "2.1.5",
4
4
  "description": "Retrivora AI is a plug-and-play AI engine for RAG chat experiences — generic vector DB + LLM provider, embeddable or standalone.",
5
5
  "author": "Abhinav Alkuchi",
6
6
  "license": "UNLICENSED",
@@ -378,6 +378,12 @@ export function createStreamHandler(
378
378
  userMessageId?: string;
379
379
  };
380
380
  try {
381
+ if (req.headers.get('content-type')?.includes('multipart/form-data')) {
382
+ return new Response(JSON.stringify({ error: { code: 'INVALID_ENDPOINT', message: 'Multipart file upload requests must be sent to the /upload endpoint.' } }), {
383
+ status: 400,
384
+ headers: { 'Content-Type': 'application/json' },
385
+ });
386
+ }
381
387
  body = await req.json();
382
388
  } catch {
383
389
  return new Response(JSON.stringify({ error: { code: 'INVALID_JSON', message: 'Invalid JSON body' } }), {
@@ -961,19 +967,39 @@ export function createRagHandler(
961
967
  }
962
968
  }
963
969
 
964
- async function getSegment(context: any): Promise<string> {
970
+ async function getSegment(req: NextRequest, context: any): Promise<string> {
971
+ // 1. If content-type is multipart/form-data, it MUST be an upload request!
972
+ const contentType = req.headers.get('content-type') || '';
973
+ if (contentType.includes('multipart/form-data')) {
974
+ return 'upload';
975
+ }
976
+
977
+ // 2. Check path directly from request URL
978
+ try {
979
+ const url = new URL(req.url);
980
+ const pathname = url.pathname;
981
+ if (pathname.endsWith('/upload')) return 'upload';
982
+ if (pathname.endsWith('/chat') || pathname.endsWith('/chat-sync')) return 'chat';
983
+ if (pathname.endsWith('/health')) return 'health';
984
+ if (pathname.endsWith('/suggestions')) return 'suggestions';
985
+ if (pathname.endsWith('/feedback')) return 'feedback';
986
+ if (pathname.endsWith('/history')) return 'history';
987
+ } catch { /* silent */ }
988
+
989
+ // 3. Fall back to resolved context params
965
990
  const resolvedParams = typeof context?.params?.then === 'function'
966
991
  ? await context.params
967
992
  : context?.params;
968
993
 
969
- const segments: string[] = resolvedParams?.retrivora || [];
970
- return segments.join('/') || 'chat';
994
+ const segments: string[] = resolvedParams?.retrivora || resolvedParams?.params?.retrivora || [];
995
+ const joined = segments.join('/');
996
+ return joined || 'chat';
971
997
  }
972
998
 
973
999
  return {
974
1000
  GET: async (req: NextRequest, context: any) => {
975
1001
  try {
976
- const segment = await getSegment(context);
1002
+ const segment = await getSegment(req, context);
977
1003
  return await routeGetRequest(req, segment);
978
1004
  } catch (err) {
979
1005
  const msg = err instanceof Error ? err.message : 'GET Routing failed';
@@ -982,7 +1008,7 @@ export function createRagHandler(
982
1008
  },
983
1009
  POST: async (req: NextRequest, context: any) => {
984
1010
  try {
985
- const segment = await getSegment(context);
1011
+ const segment = await getSegment(req, context);
986
1012
  return await routePostRequest(req, segment);
987
1013
  } catch (err) {
988
1014
  const msg = err instanceof Error ? err.message : 'POST Routing failed';