426
_req: Request,
427
{ params }: { params: Promise<{ slug?: string[] }> }
428
) {
429
- const { slug = [] } = await params;
430
- const [prefix, ...rest] = slug;
431
-
432
- // Find the matching source
433
- const match = sources.find((s) => s.prefix === prefix);
434
- if (!match) notFound();
435
-
436
- // Get the page from that source (MDX pages)
437
- const page = match.source.getPage(rest.length > 0 ? rest : undefined);
438
-
439
- if (page) {
440
- // Check if this is an OpenAPI page
441
- if ('getAPIPageProps' in page.data) {
442
- const markdown = await openapiPageToMarkdown(
443
- page as { url: string; data: OpenAPIPageData }
444
- );
445
- return new Response(markdown, {
446
- headers: {
447
- 'Content-Type': 'text/markdown; charset=utf-8',
448
- },
449
- });
450
- }
429
+ try {
430
+ const { slug = [] } = await params;
431
+ const [prefix, ...rest] = slug;
432
+
433
+ // Find the matching source
434
+ const match = sources.find((s) => s.prefix === prefix);
435
+ if (!match) notFound();
436
452
- // Regular MDX page
437
+ // Get the page from that source (MDX pages)
438
+ let page;
439
try {
454
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
455
- return new Response(await getLLMText(page as any), {
456
- headers: {
457
- 'Content-Type': 'text/markdown; charset=utf-8',
458
- },
459
- });
460
- } catch {
461
- return new Response(
462
- `# ${page.data.title} (${page.url})\n\n${page.data.description || ''}`,
463
- {
440
+ page = match.source.getPage(rest.length > 0 ? rest : undefined);
441
+ } catch (e) {
442
+ console.error('Error in getPage:', e);
443
+ page = null;
444
+ }
445
+
446
+ if (page) {
447
+ // Check if this is an OpenAPI page
448
+ if ('getAPIPageProps' in page.data) {
449
+ try {
450
+ const markdown = await openapiPageToMarkdown(
451
+ page as { url: string; data: OpenAPIPageData }
452
+ );
453
+ return new Response(markdown, {
454
+ headers: {
455
+ 'Content-Type': 'text/markdown; charset=utf-8',
456
+ },
457
+ });
458
+ } catch (e) {
459
+ console.error('Error generating OpenAPI markdown:', e);
460
+ const title = page.data?.title || 'API Reference';
461
+ const description = page.data?.description || '';
462
+ return new Response(
463
+ `# ${title}\n\n${description}`,
464
+ {
465
+ headers: {
466
+ 'Content-Type': 'text/markdown; charset=utf-8',
467
+ },
468
+ }
469
+ );
470
+ }
471
+ }
472
+
473
+ // Regular MDX page
474
+ try {
475
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
476
+ return new Response(await getLLMText(page as any), {
477
headers: {
478
'Content-Type': 'text/markdown; charset=utf-8',
479
},
467
- }
468
- );
480
+ });
481
+ } catch (e) {
482
+ console.error('Error generating LLM text:', e);
483
+ const title = page.data?.title || 'Documentation';
484
+ const description = page.data?.description || '';
485
+ return new Response(
486
+ `# ${title} (${page.url || ''})\n\n${description}`,
487
+ {
488
+ headers: {
489
+ 'Content-Type': 'text/markdown; charset=utf-8',
490
+ },
491
+ }
492
+ );
493
+ }
494
+ }
495
+
496
+ // Special handling for JSON toolkit pages
497
+ if (prefix === 'toolkits' && rest.length === 1) {
498
+ const toolkit = await getToolkit(rest[0]);
499
+ if (toolkit) {
500
+ return new Response(toolkitToMarkdown(toolkit), {
501
+ headers: {
502
+ 'Content-Type': 'text/markdown; charset=utf-8',
503
+ },
504
+ });
505
+ }
506
}
470
- }
507
472
- // Special handling for JSON toolkit pages
473
- if (prefix === 'toolkits' && rest.length === 1) {
474
- const toolkit = await getToolkit(rest[0]);
475
- if (toolkit) {
476
- return new Response(toolkitToMarkdown(toolkit), {
508
+ notFound();
509
+ } catch (e) {
510
+ // Don't catch notFound - let it propagate
511
+ if (e && typeof e === 'object' && 'digest' in e) {
512
+ throw e;
513
+ }
514
+ console.error('Unexpected error in llms.mdx route:', e);
515
+ return new Response(
516
+ '# Error\n\nAn error occurred while generating the markdown content.',
517
+ {
518
+ status: 500,
519
headers: {
520
'Content-Type': 'text/markdown; charset=utf-8',
521
},
480
- });
481
- }
522
+ }
523
+ );
524
}
483
-
484
- notFound();
525
}