Skip to content
Source
Sign inSign up
TanStack/schemas
Open History to see the last commit

nvidia-sitemap.ts

127 lines4.2 KB

1/** Native sitemap discovery supplements incomplete per-category reference tables. */
2import { cachedDocs, docsRun, mapConcurrent, tryDocs } from './model-facts.ts'
3import type { DocsRun } from './model-facts.ts'
4import {
5 fetchNvidiaText,
6 parseNvidiaInfer,
7 nvidiaStatedModelIds,
8} from './nvidia-openapi.ts'
9import type { NvidiaIndexRow } from './nvidia-openapi.ts'
10import type { DocsFailures } from './types.ts'
11​
12export const NVIDIA_SITEMAP_URL = 'https://docs.api.nvidia.com/sitemap.xml'
13export function parseNvidiaInferSitemap(xml: string): string[] {
14 if (!/<urlset(?:\s|>)/.test(xml) || !/<\/urlset>/.test(xml))
15 throw new Error('nvidia: sitemap has no urlset')
16 const urls = [...xml.matchAll(/<loc>\s*([^<]+?)\s*<\/loc>/g)].flatMap(
17 (match) => {
18 const url = match[1]?.trim()
19 return url &&
20 /^https:\/\/docs\.api\.nvidia\.com\/nim\/reference\/[a-zA-Z0-9._-]+-(?:infer|invoke)$/.test(
21 url,
22 )
23 ? [url]
24 : []
25 },
26 )
27 if (!urls.length)
28 throw new Error('nvidia: sitemap parsed no native infer URLs')
29 return [...new Set(urls)]
31const key = (text: string) => text.toLowerCase().replace(/[^a-z0-9]/g, '')
32/** Names select candidate pages only. Structured native schema IDs perform every join. */
33export function nvidiaSitemapCandidates(
34 rawId: string,
35 urls: string[],
36): string[] {
37 const model = rawId.split('/')[1]
38 if (!model) return []
39 return urls.filter((url) =>
40 key(
41 url
42 .split('/')
43 .at(-1)
44 ?.replace(/-(infer|invoke)$/, '') ?? '',
45 ).endsWith(key(model)),
46 )
48export async function discoverNvidiaSitemap(
49 rawIds: string[],
50 kv?: KVNamespace,
51 run: DocsRun = docsRun(),
52): Promise<{
53 rows: NvidiaIndexRow[]
54 failures: DocsFailures
55 unavailable: string[]
56}> {
57 const rows: NvidiaIndexRow[] = []
58 const unavailable = new Set<string>()
59 if (!rawIds.length)
60 return { rows, failures: run, unavailable: [...unavailable] }
61 const urls = await tryDocs(run, NVIDIA_SITEMAP_URL, (cached) =>
62 cached(kv, NVIDIA_SITEMAP_URL, async () =>
63 parseNvidiaInferSitemap(await fetchNvidiaText(NVIDIA_SITEMAP_URL)),
64 ),
65 )
66 if (urls === null) return { rows, failures: run, unavailable: rawIds }
67 const wanted = new Map<string, string[]>()
68 for (const rawId of rawIds)
69 for (const url of nvidiaSitemapCandidates(rawId, urls))
70 wanted.set(url, [...(wanted.get(url) ?? []), rawId])
71 const parsed = await mapConcurrent(
72 [...wanted],
73 3,
74 async ([inferUrl, candidates]) => {
75 const page = inferUrl + '.md'
76 const ids = await tryDocs(run, page, async (cached) => {
77 const stated = await cached(
78 kv,
79 'nvidia-sitemap-binding:' + page,
80 async () => {
81 const markdown = await cachedDocs(kv, page, () =>
82 fetchNvidiaText(page),
83 )
84 const infer = parseNvidiaInfer(markdown)
85 if (!infer)
86 throw new Error(
87 'nvidia: sitemap infer page has no OpenAPI document: ' + page,
88 )
89 const nativeIds = nvidiaStatedModelIds(infer.document)
90 if (nativeIds.length !== 1)
91 throw new Error(
92 'nvidia: sitemap infer page does not identify one native model: ' +
93 page,
94 )
95 // The binding cache stores only source-owned IDs, never URL-derived IDs.
96 return nativeIds
97 },
98 )
99 if (
100 !Array.isArray(stated) ||
101 stated.length !== 1 ||
102 typeof stated[0] !== 'string' ||
103 !candidates.includes(stated[0])
104 )
105 throw new Error(
106 'nvidia: sitemap infer page does not identify its candidate model: ' +
107 page +
108 '; expected ' +
109 candidates.join(', ') +
110 '; stated ' +
111 JSON.stringify(stated),
112 )
113 return stated
114 })
115 if (ids === null)
116 for (const candidate of candidates) unavailable.add(candidate)
117 return (ids ?? []).map((rawId) => ({ rawId, inferUrl }))
118 },
119 )
120 const seen = new Set<string>()
121 for (const row of parsed.flat())
122 if (!seen.has(row.rawId)) {
123 seen.add(row.rawId)
124 rows.push(row)
125 }
126 return { rows, failures: run, unavailable: [...unavailable] }