-
-
Notifications
You must be signed in to change notification settings - Fork 371
feat(ui): add llms.txt generation for npm packages #1382
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lukeocodes
wants to merge
22
commits into
npmx-dev:main
Choose a base branch
from
lukeocodes:feat/llms-txt
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
b92b7f1
feat(types): add llms-txt type definitions
lukeocodes 31aeabd
feat(llms-txt): add core utility functions
lukeocodes 557bccd
feat(llms-txt): add API route for llms.txt generation
lukeocodes 634148c
test(llms-txt): add unit tests for utility functions
lukeocodes 165a605
feat(llms-txt): replace API route with file-based server routes
lukeocodes 80487a7
refactor(llms-txt): add handler factory and split llms.txt/llms_full.…
lukeocodes 7a57b2c
feat(llms-txt): add middleware for versioned, org, and root routes
lukeocodes 00566e0
feat(llms-txt): support shorthand URL redirects for llms.txt paths
lukeocodes b5a17c9
chore(llms-txt): add ISR cache rules and vitest server alias
lukeocodes e83f60f
test(llms-txt): add generateRootLlmsTxt unit tests
lukeocodes 2374448
fix(llms-txt): move all handling to middleware for Vercel compatibility
lukeocodes eccca21
fix(llms-txt): resolve type errors in middleware and utils
lukeocodes b455ff1
refactor(llm-docs): rename llms-txt files to llm-docs
lukeocodes fd27bc7
test(llm-docs): add failing tests for .md routes in generateRootLlmsTxt
lukeocodes 2567ac3
feat(llm-docs): add handlePackageMd for raw README .md routes
lukeocodes 48dc0da
feat(llm-docs): handle .md routes in middleware
lukeocodes 1adcc2f
feat(llm-docs): add .md shorthand redirects
lukeocodes 8d6efc1
fix: remove no-op replace
lukeocodes 456017b
fix(llm-docs): remove versioned .md routes due to Vercel ISR conflict
lukeocodes 448eac0
fix(llm-docs): rename llms_full.txt to llms-full.txt
lukeocodes 16c42ce
feat(llm-docs): add alternate link tags for llms.txt and .md routes
lukeocodes b4e40e3
Merge branch 'main' into feat/llms-txt
lukeocodes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| #!/usr/bin/env bash | ||
| # | ||
| # Smoke test all llm-docs routes (llms.txt, llms-full.txt, .md) | ||
| # Usage: ./scripts/smoke-test-llm-docs.sh http://localhost:3333 | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| BASE="${1:?Usage: $0 <base-url>}" | ||
| BASE="${BASE%/}" # strip trailing slash | ||
|
|
||
| PASS=0 | ||
| FAIL=0 | ||
|
|
||
| check() { | ||
| local label="$1" | ||
| local url="$2" | ||
| local expect_status="${3:-200}" | ||
|
|
||
| status=$(curl -s -o /dev/null -w "%{http_code}" -L "$url") | ||
|
|
||
| if [ "$status" = "$expect_status" ]; then | ||
| echo " PASS GET $url $status $label" | ||
| PASS=$((PASS + 1)) | ||
| else | ||
| echo " FAIL GET $url $status $label (expected $expect_status)" | ||
| FAIL=$((FAIL + 1)) | ||
| fi | ||
| } | ||
|
|
||
| echo "=== Root ===" | ||
| check "Root llms.txt" "$BASE/llms.txt" | ||
|
|
||
| echo "" | ||
| echo "=== Unscoped package (latest) ===" | ||
| check "llms.txt" "$BASE/package/nuxt/llms.txt" | ||
| check "llms-full.txt" "$BASE/package/nuxt/llms-full.txt" | ||
| check ".md" "$BASE/package/nuxt.md" | ||
|
|
||
| echo "" | ||
| echo "=== Unscoped package (versioned) ===" | ||
| check "llms.txt" "$BASE/package/nuxt/v/3.16.2/llms.txt" | ||
| check "llms-full.txt" "$BASE/package/nuxt/v/3.16.2/llms-full.txt" | ||
|
|
||
| echo "" | ||
| echo "=== Scoped package (latest) ===" | ||
| check "llms.txt" "$BASE/package/@nuxt/kit/llms.txt" | ||
| check "llms-full.txt" "$BASE/package/@nuxt/kit/llms-full.txt" | ||
| check ".md" "$BASE/package/@nuxt/kit.md" | ||
|
|
||
| echo "" | ||
| echo "=== Scoped package (versioned) ===" | ||
| check "llms.txt" "$BASE/package/@nuxt/kit/v/4.3.1/llms.txt" | ||
| check "llms-full.txt" "$BASE/package/@nuxt/kit/v/4.3.1/llms-full.txt" | ||
|
|
||
| echo "" | ||
| echo "=== Org-level ===" | ||
| check "Org llms.txt" "$BASE/package/@nuxt/llms.txt" | ||
|
|
||
| echo "" | ||
| echo "=== Shorthand redirects (follow → 200) ===" | ||
| check "Unscoped .md redirect" "$BASE/nuxt.md" | ||
| check "Scoped .md redirect" "$BASE/@nuxt/kit.md" | ||
| check "Unscoped llms.txt redirect" "$BASE/nuxt/llms.txt" | ||
| check "Scoped llms.txt redirect" "$BASE/@nuxt/kit/llms.txt" | ||
|
|
||
| echo "" | ||
| echo "=== Results ===" | ||
| echo " $PASS passed, $FAIL failed" | ||
| exit $FAIL | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| import * as v from 'valibot' | ||
| import { PackageRouteParamsSchema } from '#shared/schemas/package' | ||
| import { handleApiError } from '#server/utils/error-handler' | ||
| import { | ||
| handleLlmsTxt, | ||
| handleOrgLlmsTxt, | ||
| generateRootLlmsTxt, | ||
| handlePackageMd, | ||
| } from '#server/utils/llm-docs' | ||
|
|
||
| const CACHE_HEADER = 's-maxage=3600, stale-while-revalidate=86400' | ||
|
|
||
| /** | ||
| * Middleware to handle ALL llms.txt / llms-full.txt / .md routes. | ||
| * | ||
| * All llms.txt handling lives here rather than in file-based routes because | ||
| * Vercel's ISR route rules with glob patterns (e.g. `/package/ ** /llms.txt`) | ||
| * create catch-all serverless functions that interfere with Nitro's file-based | ||
| * route resolution — scoped packages and versioned paths fail to match. | ||
| * | ||
| * Handles: | ||
| * - /llms.txt (root discovery page) | ||
| * - /package/:name.md (unscoped, latest, raw README) | ||
| * - /package/@:org/:name.md (scoped, latest, raw README) | ||
| * - /package/@:org/llms.txt (org package listing) | ||
| * - /package/:name/llms.txt (unscoped, latest) | ||
| * - /package/:name/llms-full.txt (unscoped, latest, full) | ||
| * - /package/@:org/:name/llms.txt (scoped, latest) | ||
| * - /package/@:org/:name/llms-full.txt (scoped, latest, full) | ||
| * - /package/:name/v/:version/llms.txt (unscoped, versioned) | ||
| * - /package/:name/v/:version/llms-full.txt (unscoped, versioned, full) | ||
| * - /package/@:org/:name/v/:version/llms.txt (scoped, versioned) | ||
| * - /package/@:org/:name/v/:version/llms-full.txt (scoped, versioned, full) | ||
| */ | ||
| export default defineEventHandler(async event => { | ||
| const path = event.path.split('?')[0] ?? '/' | ||
|
|
||
| // Handle .md routes — raw README markdown (latest version only) | ||
| if (path.startsWith('/package/') && path.endsWith('.md') && !path.includes('/v/')) { | ||
| const rawPackageName = path.slice('/package/'.length, -'.md'.length) | ||
|
|
||
| if (!rawPackageName) return | ||
|
|
||
| try { | ||
| const { packageName } = v.parse(PackageRouteParamsSchema, { | ||
| packageName: rawPackageName, | ||
| }) | ||
|
|
||
| const content = await handlePackageMd(packageName) | ||
| setHeader(event, 'Content-Type', 'text/markdown; charset=utf-8') | ||
| setHeader(event, 'Cache-Control', CACHE_HEADER) | ||
| return content | ||
| } catch (error: unknown) { | ||
| handleApiError(error, { | ||
| statusCode: 502, | ||
| message: 'Failed to generate package markdown.', | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| if (!path.endsWith('/llms.txt') && !path.endsWith('/llms-full.txt')) return | ||
|
|
||
| const full = path.endsWith('/llms-full.txt') | ||
| const suffix = full ? '/llms-full.txt' : '/llms.txt' | ||
|
|
||
| // Root /llms.txt | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a thing that I personally dislike is the amount of comments that explain very simple statements. If a comment is needed, the code should be made more readable. |
||
| if (path === '/llms.txt') { | ||
| const url = getRequestURL(event) | ||
| const baseUrl = `${url.protocol}//${url.host}` | ||
| setHeader(event, 'Content-Type', 'text/markdown; charset=utf-8') | ||
| setHeader(event, 'Cache-Control', CACHE_HEADER) | ||
| return generateRootLlmsTxt(baseUrl) | ||
| } | ||
|
|
||
| if (!path.startsWith('/package/')) return | ||
|
|
||
| // Strip /package/ prefix and /llms[_full].txt suffix | ||
| const inner = path.slice('/package/'.length, -suffix.length) | ||
|
|
||
| // Org-level: /package/@org/llms.txt (inner = "@org") | ||
| if (!full && inner.startsWith('@') && !inner.includes('/')) { | ||
| const orgName = inner.slice(1) | ||
| try { | ||
| const url = getRequestURL(event) | ||
| const baseUrl = `${url.protocol}//${url.host}` | ||
| const content = await handleOrgLlmsTxt(orgName, baseUrl) | ||
| setHeader(event, 'Content-Type', 'text/markdown; charset=utf-8') | ||
| setHeader(event, 'Cache-Control', CACHE_HEADER) | ||
| return content | ||
| } catch (error: unknown) { | ||
| handleApiError(error, { statusCode: 502, message: 'Failed to generate org llms.txt.' }) | ||
| } | ||
| } | ||
|
|
||
| // Parse package name and optional version from inner path | ||
| let rawPackageName: string | ||
| let rawVersion: string | undefined | ||
|
|
||
| if (inner.includes('/v/')) { | ||
| // Versioned path | ||
| if (inner.startsWith('@')) { | ||
| const match = inner.match(/^(@[^/]+\/[^/]+)\/v\/(.+)$/) | ||
| if (!match?.[1] || !match[2]) return | ||
| rawPackageName = match[1] | ||
| rawVersion = match[2] | ||
| } else { | ||
| const match = inner.match(/^([^/]+)\/v\/(.+)$/) | ||
| if (!match?.[1] || !match[2]) return | ||
| rawPackageName = match[1] | ||
| rawVersion = match[2] | ||
| } | ||
| } else { | ||
| // Latest version — inner is just the package name | ||
| rawPackageName = inner | ||
| } | ||
|
|
||
| if (!rawPackageName) return | ||
|
|
||
| try { | ||
| const { packageName, version } = v.parse(PackageRouteParamsSchema, { | ||
| packageName: rawPackageName, | ||
| version: rawVersion, | ||
| }) | ||
|
|
||
| const content = await handleLlmsTxt(packageName, version, { includeAgentFiles: full }) | ||
| setHeader(event, 'Content-Type', 'text/markdown; charset=utf-8') | ||
| setHeader(event, 'Cache-Control', CACHE_HEADER) | ||
| return content | ||
| } catch (error: unknown) { | ||
| handleApiError(error, { | ||
| statusCode: 502, | ||
| message: `Failed to generate ${full ? 'llms-full.txt' : 'llms.txt'}.`, | ||
| }) | ||
| } | ||
| }) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.