-
Notifications
You must be signed in to change notification settings - Fork 175
feat(agents): serve markdown for versioned api-reference URLs #4100
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
revmag
wants to merge
1
commit into
feat/agent-openapi-discovery
Choose a base branch
from
agent-md/versioned-api-reference-markdown
base: feat/agent-openapi-discovery
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import { agentResponse, agentNotFoundResponse } from '@/utils/agentResponseHeaders' | ||
| import { buildApiReferenceMarkdown } from '@/utils/openapiMarkdown' | ||
| import { getOpenAPISpecForVersion } from '@/utils/openapiSpec' | ||
| import { resolveLatestVersion } from '@/utils/apiReference' | ||
|
|
||
| export const revalidate = 86400 // 24h β see API_SPEC_REVALIDATE_SECONDS | ||
|
|
||
| /** | ||
| * Markdown twin of /api-reference/<version>. Agents request these directly | ||
| * (31 requests over 30 days across 7 release tags, all previously 404) and | ||
| * `.md` is the convention llms.txt advertises for every other page. | ||
| */ | ||
| export async function GET(_: Request, props: { params: Promise<{ version: string }> }) { | ||
| const params = await props.params | ||
| const raw = params.version | ||
| const version = raw === 'latest' ? await resolveLatestVersion() : raw | ||
|
|
||
| if (!version) return agentNotFoundResponse(`/api-reference/${raw}.md`) | ||
|
|
||
| const spec = await getOpenAPISpecForVersion(version) | ||
| if (!spec) return agentNotFoundResponse(`/api-reference/${raw}.md`) | ||
|
|
||
| return agentResponse(buildApiReferenceMarkdown(spec), { varyAccept: true }) | ||
| } |
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,89 @@ | ||
| const test = require('node:test') | ||
| const assert = require('node:assert/strict') | ||
| const { loadTsModule } = require('./helpers/loadTsModule') | ||
|
|
||
| const { | ||
| parseApiReferenceVersionPath, | ||
| shouldRewriteApiReferenceVersionToMarkdown, | ||
| buildApiReferenceVersionMarkdownRewritePath, | ||
| shouldRewriteApiReferenceToOpenAPISpec, | ||
| shouldRewriteApiReferenceIndexToMarkdown, | ||
| } = loadTsModule('utils/apiReferenceMarkdownRouting.ts') | ||
|
|
||
| test('parseApiReferenceVersionPath accepts release tags, latest, and .md twins', () => { | ||
| assert.equal(parseApiReferenceVersionPath('/api-reference/v0.139.0'), 'v0.139.0') | ||
| assert.equal(parseApiReferenceVersionPath('/api-reference/v0.139.0/'), 'v0.139.0') | ||
| assert.equal(parseApiReferenceVersionPath('/api-reference/v0.139.0.md'), 'v0.139.0') | ||
| assert.equal(parseApiReferenceVersionPath('/api-reference/latest'), 'latest') | ||
| assert.equal(parseApiReferenceVersionPath('/api-reference/latest.md'), 'latest') | ||
| }) | ||
|
|
||
| test('parseApiReferenceVersionPath rejects the index and non-version segments', () => { | ||
| assert.equal(parseApiReferenceVersionPath('/api-reference'), null) | ||
| assert.equal(parseApiReferenceVersionPath('/api-reference/'), null) | ||
| assert.equal(parseApiReferenceVersionPath('/api-reference/not-a-version'), null) | ||
| assert.equal(parseApiReferenceVersionPath('/api-reference/v0.139.0/query-range'), null) | ||
| assert.equal(parseApiReferenceVersionPath('/docs/introduction'), null) | ||
| }) | ||
|
|
||
| // 31 requests over 30 days across 7 release tags, all previously 404. | ||
| test('versioned .md URLs rewrite to the markdown twin', () => { | ||
| assert.equal( | ||
| shouldRewriteApiReferenceVersionToMarkdown('/api-reference/v0.139.0.md', false), | ||
| true | ||
| ) | ||
| assert.equal(shouldRewriteApiReferenceVersionToMarkdown('/api-reference/latest.md', false), true) | ||
| assert.equal( | ||
| buildApiReferenceVersionMarkdownRewritePath('/api-reference/v0.139.0.md'), | ||
| '/api/api-reference-markdown/v0.139.0' | ||
| ) | ||
| assert.equal( | ||
| buildApiReferenceVersionMarkdownRewritePath('/api-reference/latest'), | ||
| '/api/api-reference-markdown/latest' | ||
| ) | ||
| }) | ||
|
|
||
| test('Accept: text/markdown on a version now gets markdown, not YAML', () => { | ||
| assert.equal(shouldRewriteApiReferenceVersionToMarkdown('/api-reference/v0.139.0', true), true) | ||
| assert.equal( | ||
| shouldRewriteApiReferenceToOpenAPISpec('/api-reference/v0.139.0', 'text/markdown'), | ||
| false | ||
| ) | ||
| }) | ||
|
|
||
| test('YAML is still served when the client actually asks for YAML', () => { | ||
| ;['text/yaml', 'application/yaml', 'application/x-yaml', 'application/vnd.oai.openapi'].forEach( | ||
| (accept) => { | ||
| assert.equal( | ||
| shouldRewriteApiReferenceToOpenAPISpec('/api-reference/v0.139.0', accept), | ||
| true, | ||
| `expected YAML for Accept: ${accept}` | ||
| ) | ||
| } | ||
| ) | ||
| assert.equal(shouldRewriteApiReferenceToOpenAPISpec('/api-reference/latest', 'text/yaml'), true) | ||
| }) | ||
|
|
||
| test('the YAML path no longer depends on user-agent sniffing', () => { | ||
| // Previously gated on isBot, so a browser UA sending the same header got HTML. | ||
| assert.equal(shouldRewriteApiReferenceToOpenAPISpec('/api-reference/v0.139.0', 'text/yaml'), true) | ||
| assert.equal( | ||
| shouldRewriteApiReferenceToOpenAPISpec('/api-reference/v0.139.0', 'text/html'), | ||
| false | ||
| ) | ||
| assert.equal(shouldRewriteApiReferenceToOpenAPISpec('/api-reference/v0.139.0', ''), false) | ||
| }) | ||
|
|
||
| test('the index keeps its own markdown twin and is never treated as a version', () => { | ||
| assert.equal(shouldRewriteApiReferenceIndexToMarkdown('/api-reference', true), true) | ||
| assert.equal(shouldRewriteApiReferenceVersionToMarkdown('/api-reference', true), false) | ||
| assert.equal(shouldRewriteApiReferenceToOpenAPISpec('/api-reference', 'text/yaml'), false) | ||
| }) | ||
|
|
||
| test('plain HTML requests to a version are untouched', () => { | ||
| assert.equal(shouldRewriteApiReferenceVersionToMarkdown('/api-reference/v0.139.0', false), false) | ||
| assert.equal( | ||
| shouldRewriteApiReferenceToOpenAPISpec('/api-reference/v0.139.0', 'text/html'), | ||
| false | ||
| ) | ||
| }) |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Versioned api-reference URLs now content-negotiate but their HTML variant gets no
Vary: Accept. (finding anchored here; the affected code is theVaryblock below at lines ~180β186, which is outside this PR's diff.)After this PR,
/api-reference/<tag>serves three representations off the same URL keyed onAccept:NextResponse.next)Accept: text/markdownβ this new rewrite to/api/api-reference-markdown/<tag>)Accept: *yamlβ/api/api-reference-openapi/<tag>)The two rewrite targets set
Vary: Acceptthemselves (agentResponse({ varyAccept: true })and the openapi route). But the plain HTML response for/api-reference/<tag>gets noVaryβ the block below only coversisDocsPathname,servesMarkdownAlternate, andisApiReferenceIndexPath, andservesMarkdownAlternateexplicitly excludes the/api-referenceprefix. That's exactly the poisoning case that block's own comment warns about: a shared cache can store the HTML for/api-reference/v0.139.0and later serve it to anAccept: text/markdown/yamlrequest (or the reverse).The index page got
Varyin #4067; this PR extends the same negotiation to versioned URLs without extending theVary. Suggest adding the version path to that condition:(add
parseApiReferenceVersionPathto the existing@/utils/apiReferenceMarkdownRoutingimport).