-
-
Notifications
You must be signed in to change notification settings - Fork 339
feat(intent): public read-only registry API at /api/v1/intent #885
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
tannerlinsley
wants to merge
3
commits into
main
Choose a base branch
from
taren/clever-banzai-a19355
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.
+678
−47
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Large diffs are not rendered by default.
Oops, something went wrong.
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,39 @@ | ||
| import { createFileRoute } from '@tanstack/react-router' | ||
|
|
||
| export const Route = createFileRoute('/api/v1/intent/packages/$name')({ | ||
| server: { | ||
| handlers: { | ||
| GET: async ({ | ||
| request, | ||
| params, | ||
| }: { | ||
| request: Request | ||
| params: { name: string } | ||
| }) => { | ||
| const [{ applyIntentRateLimit, intentJsonResponse, intentErrorResponse }, fns] = | ||
| await Promise.all([ | ||
| import('~/utils/intent-api.server'), | ||
| import('~/utils/intent.functions'), | ||
| ]) | ||
|
|
||
| const decision = await applyIntentRateLimit(request) | ||
| if (decision.limited) return decision.response | ||
|
|
||
| const detail = await fns.getIntentPackageDetail({ | ||
| data: { name: params.name }, | ||
| }) | ||
|
|
||
| if (!detail) { | ||
| return intentErrorResponse( | ||
| `Package not found: ${params.name}`, | ||
| 'NOT_FOUND', | ||
| 404, | ||
| decision.rl, | ||
| ) | ||
| } | ||
|
|
||
| return intentJsonResponse(detail, decision.rl) | ||
| }, | ||
| }, | ||
| }, | ||
| }) |
102 changes: 102 additions & 0 deletions
102
src/routes/api/v1/intent/packages.$name.versions.$version.skills.$skill.ts
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,102 @@ | ||
| import { createFileRoute } from '@tanstack/react-router' | ||
|
|
||
| export const Route = createFileRoute( | ||
| '/api/v1/intent/packages/$name/versions/$version/skills/$skill', | ||
| )({ | ||
| server: { | ||
| handlers: { | ||
| GET: async ({ | ||
| request, | ||
| params, | ||
| }: { | ||
| request: Request | ||
| params: { name: string; version: string; skill: string } | ||
| }) => { | ||
| const [ | ||
| { | ||
| applyIntentRateLimit, | ||
| intentJsonResponse, | ||
| intentErrorResponse, | ||
| buildSkillContentUrls, | ||
| }, | ||
| fns, | ||
| dbModule, | ||
| ] = await Promise.all([ | ||
| import('~/utils/intent-api.server'), | ||
| import('~/utils/intent.functions'), | ||
| import('~/utils/intent-db.server'), | ||
| ]) | ||
|
|
||
| const decision = await applyIntentRateLimit(request) | ||
| if (decision.limited) return decision.response | ||
|
|
||
| const versions = await dbModule.getPackageVersions(params.name) | ||
| const versionRecord = versions.find((v) => v.version === params.version) | ||
| if (!versionRecord) { | ||
| return intentErrorResponse( | ||
| `Version not found: ${params.name}@${params.version}`, | ||
| 'NOT_FOUND', | ||
| 404, | ||
| decision.rl, | ||
| ) | ||
| } | ||
|
|
||
| const skills = await dbModule.getSkillsForVersion(versionRecord.id) | ||
| const skill = skills.find((s) => s.name === params.skill) | ||
|
|
||
| if (!skill) { | ||
| return intentErrorResponse( | ||
| `Skill not found: ${params.skill} in ${params.name}@${params.version}`, | ||
| 'NOT_FOUND', | ||
| 404, | ||
| decision.rl, | ||
| ) | ||
| } | ||
|
|
||
| // Markdown body is kept as an undocumented escape hatch only fetched | ||
| // when the caller explicitly opts in (?include=markdown). Default | ||
| // responses point at the CDN to keep our egress near zero. | ||
| const url = new URL(request.url) | ||
| const includeMarkdown = url.searchParams | ||
| .get('include') | ||
| ?.split(',') | ||
| .includes('markdown') | ||
|
|
||
| const markdown = includeMarkdown | ||
| ? await fns.getIntentSkillMarkdown({ | ||
| data: { | ||
| packageName: params.name, | ||
| version: params.version, | ||
| skillName: params.skill, | ||
| }, | ||
| }) | ||
| : undefined | ||
|
|
||
| return intentJsonResponse( | ||
| { | ||
| packageName: params.name, | ||
| version: params.version, | ||
| skill: { | ||
| id: skill.id, | ||
| name: skill.name, | ||
| description: skill.description, | ||
| type: skill.type, | ||
| framework: skill.framework, | ||
| requires: skill.requires, | ||
| skillPath: skill.skillPath, | ||
| contentHash: skill.contentHash, | ||
| lineCount: skill.lineCount, | ||
| }, | ||
| content: buildSkillContentUrls( | ||
| params.name, | ||
| params.version, | ||
| skill.skillPath, | ||
| ), | ||
| ...(includeMarkdown ? { markdown } : {}), | ||
| }, | ||
| decision.rl, | ||
| ) | ||
| }, | ||
| }, | ||
| }, | ||
| }) |
61 changes: 61 additions & 0 deletions
61
src/routes/api/v1/intent/packages.$name.versions.$version.skills.ts
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,61 @@ | ||
| import { createFileRoute } from '@tanstack/react-router' | ||
|
|
||
| export const Route = createFileRoute( | ||
| '/api/v1/intent/packages/$name/versions/$version/skills', | ||
| )({ | ||
| server: { | ||
| handlers: { | ||
| GET: async ({ | ||
| request, | ||
| params, | ||
| }: { | ||
| request: Request | ||
| params: { name: string; version: string } | ||
| }) => { | ||
| const [ | ||
| { | ||
| applyIntentRateLimit, | ||
| intentJsonResponse, | ||
| intentErrorResponse, | ||
| buildSkillContentUrls, | ||
| }, | ||
| fns, | ||
| ] = await Promise.all([ | ||
| import('~/utils/intent-api.server'), | ||
| import('~/utils/intent.functions'), | ||
| ]) | ||
|
|
||
| const decision = await applyIntentRateLimit(request) | ||
| if (decision.limited) return decision.response | ||
|
|
||
| const result = await fns.getIntentVersionSkills({ | ||
| data: { packageName: params.name, version: params.version }, | ||
| }) | ||
|
|
||
| if (!result) { | ||
| return intentErrorResponse( | ||
| `Version not found: ${params.name}@${params.version}`, | ||
| 'NOT_FOUND', | ||
| 404, | ||
| decision.rl, | ||
| ) | ||
| } | ||
|
|
||
| return intentJsonResponse( | ||
| { | ||
| ...result, | ||
| skills: result.skills.map((skill) => ({ | ||
| ...skill, | ||
| content: buildSkillContentUrls( | ||
| params.name, | ||
| params.version, | ||
| skill.skillPath, | ||
| ), | ||
| })), | ||
| }, | ||
| decision.rl, | ||
| ) | ||
| }, | ||
| }, | ||
| }, | ||
| }) |
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,41 @@ | ||
| import { createFileRoute } from '@tanstack/react-router' | ||
|
|
||
| export const Route = createFileRoute('/api/v1/intent/packages')({ | ||
| server: { | ||
| handlers: { | ||
| GET: async ({ request }: { request: Request }) => { | ||
| const [{ applyIntentRateLimit, intentJsonResponse }, fns] = | ||
| await Promise.all([ | ||
| import('~/utils/intent-api.server'), | ||
| import('~/utils/intent.functions'), | ||
| ]) | ||
|
|
||
| const decision = await applyIntentRateLimit(request) | ||
| if (decision.limited) return decision.response | ||
|
|
||
| const url = new URL(request.url) | ||
| const search = url.searchParams.get('q') ?? undefined | ||
| const framework = url.searchParams.get('framework') ?? undefined | ||
| const sortParam = url.searchParams.get('sort') | ||
| const sort: 'downloads' | 'name' | 'skills' | 'newest' | undefined = | ||
| sortParam === 'downloads' || | ||
| sortParam === 'name' || | ||
| sortParam === 'skills' || | ||
| sortParam === 'newest' | ||
| ? sortParam | ||
| : undefined | ||
| const page = parseInt(url.searchParams.get('page') ?? '0', 10) || 0 | ||
| const pageSize = Math.min( | ||
| Math.max(parseInt(url.searchParams.get('pageSize') ?? '24', 10) || 24, 1), | ||
| 100, | ||
| ) | ||
|
|
||
| const result = await fns.getIntentDirectory({ | ||
| data: { search, framework, sort, page, pageSize }, | ||
| }) | ||
|
|
||
| return intentJsonResponse(result, decision.rl) | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
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,53 @@ | ||
| import { createFileRoute } from '@tanstack/react-router' | ||
|
|
||
| export const Route = createFileRoute('/api/v1/intent/search')({ | ||
| server: { | ||
| handlers: { | ||
| GET: async ({ request }: { request: Request }) => { | ||
| const [ | ||
| { | ||
| applyIntentRateLimit, | ||
| intentJsonResponse, | ||
| intentErrorResponse, | ||
| buildSkillContentUrls, | ||
| }, | ||
| dbModule, | ||
| ] = await Promise.all([ | ||
| import('~/utils/intent-api.server'), | ||
| import('~/utils/intent-db.server'), | ||
| ]) | ||
|
|
||
| const decision = await applyIntentRateLimit(request) | ||
| if (decision.limited) return decision.response | ||
|
|
||
| const url = new URL(request.url) | ||
| const q = url.searchParams.get('q')?.trim() ?? '' | ||
| const limitParam = url.searchParams.get('limit') | ||
| const limit = Math.min( | ||
| Math.max(parseInt(limitParam ?? '20', 10) || 20, 1), | ||
| 100, | ||
| ) | ||
|
|
||
| if (!q) { | ||
| return intentErrorResponse( | ||
| 'Missing required query parameter: q', | ||
| 'INVALID_REQUEST', | ||
| 400, | ||
| decision.rl, | ||
| ) | ||
| } | ||
|
|
||
| const rows = await dbModule.searchSkills(q, limit) | ||
| const results = rows.map((row) => ({ | ||
| ...row, | ||
| content: buildSkillContentUrls( | ||
| row.packageName, | ||
| row.version, | ||
| row.skillPath, | ||
| ), | ||
| })) | ||
| return intentJsonResponse({ query: q, limit, results }, decision.rl) | ||
| }, | ||
| }, | ||
| }, | ||
| }) |
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.
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.
Normalize negative
pagevalues to0.page=-1currently survives parsing and is forwarded downstream; clamp to avoid invalid pagination input.Suggested patch
📝 Committable suggestion
🤖 Prompt for AI Agents