Skip to content

Commit 51c97cc

Browse files
committed
Address llms external filtering review
1 parent 0a12262 commit 51c97cc

2 files changed

Lines changed: 67 additions & 65 deletions

File tree

src/commands/import.js

Lines changed: 15 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3336,14 +3336,14 @@ function narrowToDocsSubtreeIfNeeded(llms, sourceUrl, hits) {
33363336
* enter the stub tree (skeleton has zero visibility of OAS files). The caller
33373337
* downloads the returned URLs into the staged `oas/` dir.
33383338
*/
3339-
function extractOasJsonUrlsFromParsed(parsed) {
3340-
const captured = []
3339+
function dropParsedItems(parsed, shouldDrop) {
3340+
const dropped = []
33413341
const keptSections = []
33423342
for (const section of parsed.sections) {
33433343
const keptItems = []
33443344
for (const item of section.items) {
3345-
if (isOasJsonUrl(item.url)) {
3346-
captured.push({ url: item.url, text: item.text || null })
3345+
if (shouldDrop(item)) {
3346+
dropped.push(item)
33473347
continue
33483348
}
33493349
keptItems.push(item)
@@ -3354,7 +3354,14 @@ function extractOasJsonUrlsFromParsed(parsed) {
33543354
}
33553355
}
33563356
parsed.sections = keptSections
3357-
return captured
3357+
return dropped
3358+
}
3359+
3360+
function extractOasJsonUrlsFromParsed(parsed) {
3361+
return dropParsedItems(parsed, (item) => isOasJsonUrl(item.url)).map((item) => ({
3362+
url: item.url,
3363+
text: item.text || null,
3364+
}))
33583365
}
33593366

33603367
/**
@@ -3363,45 +3370,11 @@ function extractOasJsonUrlsFromParsed(parsed) {
33633370
* items dropped.
33643371
*/
33653372
function dropAssetItemsFromParsed(parsed) {
3366-
let dropped = 0
3367-
const keptSections = []
3368-
for (const section of parsed.sections) {
3369-
const keptItems = []
3370-
for (const item of section.items) {
3371-
if (isAssetOrMetaUrl(item.url)) {
3372-
dropped++
3373-
continue
3374-
}
3375-
keptItems.push(item)
3376-
}
3377-
if (keptItems.length > 0) {
3378-
section.items = keptItems
3379-
keptSections.push(section)
3380-
}
3381-
}
3382-
parsed.sections = keptSections
3383-
return dropped
3373+
return dropParsedItems(parsed, (item) => isAssetOrMetaUrl(item.url)).length
33843374
}
33853375

33863376
function dropExternalItemsFromParsed(parsed, allowedOrigins) {
3387-
let dropped = 0
3388-
const keptSections = []
3389-
for (const section of parsed.sections) {
3390-
const keptItems = []
3391-
for (const item of section.items) {
3392-
if (isExternalToAllowedOrigins(item.url, allowedOrigins)) {
3393-
dropped++
3394-
continue
3395-
}
3396-
keptItems.push(item)
3397-
}
3398-
if (keptItems.length > 0) {
3399-
section.items = keptItems
3400-
keptSections.push(section)
3401-
}
3402-
}
3403-
parsed.sections = keptSections
3404-
return dropped
3377+
return dropParsedItems(parsed, (item) => isExternalToAllowedOrigins(item.url, allowedOrigins)).length
34053378
}
34063379

34073380
/**
@@ -4511,7 +4484,7 @@ function makeIconPicker() {
45114484
}
45124485
}
45134486

4514-
export const __test__ = { discoverLlmsTxt, mergeValidHits, dropExternalItemsFromParsed }
4487+
export const __test__ = { discoverLlmsTxt, mergeValidHits, produceOrganizedForSource }
45154488

45164489
function formatDuration(ms) {
45174490
const safe = Math.max(0, ms)

src/commands/import.test.js

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,32 +26,61 @@ function mergedItems(merged) {
2626
return merged.parsed.sections.flatMap((section) => section.items.map((item) => ({ text: item.text, url: item.url })))
2727
}
2828

29-
test('llms drops external page links before organization', () => {
30-
const parsed = {
31-
sections: [
32-
{
33-
title: 'Guides',
34-
items: [
35-
{ text: 'Overview', url: 'https://docs.example.com/overview.md' },
36-
{ text: 'Canonical', url: 'https://canonical-docs.example.com/install.md' },
37-
{ text: 'Platform Status', url: 'https://status.example.com/' },
38-
{ text: 'GitHub', url: 'https://github.com/example/project' },
39-
],
40-
},
41-
{
42-
title: 'External Only',
43-
items: [{ text: 'Support', url: 'https://support.example.com/' }],
44-
},
45-
],
29+
function organizedPages(organized) {
30+
const pages = []
31+
const visit = (page) => {
32+
pages.push(page)
33+
for (const child of page.pages || []) visit(child)
4634
}
35+
for (const category of organized.categories || []) {
36+
for (const page of category.pages || []) visit(page)
37+
}
38+
return pages
39+
}
40+
41+
test('llms import drops external page links before organization', async () => {
42+
mockLlmsFetch({
43+
'https://docs.example.com/llms.txt': `# Docs
44+
45+
## Guides
46+
- [Overview](/overview.md)
47+
- [Install](/install.md)
48+
- [Configure](/configure.md)
49+
- [Deploy](/deploy.md)
50+
- [Operate](/operate.md)
51+
52+
## Reference
53+
- [CLI](/cli.md)
54+
- [SDK](/sdk.md)
55+
- [API](/api.md)
56+
- [Auth](/auth.md)
57+
- [Webhooks](/webhooks.md)
58+
59+
## Admin
60+
- [Users](/users.md)
61+
- [Teams](/teams.md)
62+
- [Billing](/billing.md)
63+
- [Audit Logs](/audit-logs.md)
64+
- [Settings](/settings.md)
65+
66+
## External
67+
- [Platform Status](https://status.example.com/)
68+
- [GitHub](https://github.com/example/project)
69+
`,
70+
})
4771

48-
const dropped = __test__.dropExternalItemsFromParsed(parsed, new Set(['https://docs.example.com', 'https://canonical-docs.example.com']))
72+
const organized = await __test__.produceOrganizedForSource(
73+
new URL('https://docs.example.com/'),
74+
{ model: 'test' },
75+
async (_label, fn) => fn(),
76+
)
77+
const pages = organizedPages(organized)
78+
const urls = pages.map((page) => page.url).filter(Boolean)
4979

50-
assert.equal(dropped, 3)
51-
assert.deepEqual(mergedItems({ parsed }), [
52-
{ text: 'Overview', url: 'https://docs.example.com/overview.md' },
53-
{ text: 'Canonical', url: 'https://canonical-docs.example.com/install.md' },
54-
])
80+
assert(urls.includes('https://docs.example.com/overview.md'))
81+
assert(urls.includes('https://docs.example.com/cli.md'))
82+
assert(!urls.some((url) => url.includes('status.example.com')))
83+
assert(!urls.some((url) => url.includes('github.com/example/project')))
5584
})
5685

5786
test('llms discovery and merge resolve nested files, dedupe canonical pages, and preserve distinct page states', async () => {

0 commit comments

Comments
 (0)