Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions packages/dev/mcp/shared/src/page-manager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {DEFAULT_CDN_BASE, fetchText} from './utils.js';
import {extractNameAndDescription, parseSectionsFromMarkdown} from './parser.js';
import {fetchText, getLibraryBaseUrl} from './utils.js';
import type {Library, PageInfo} from './types.js';
import path from 'path';

Expand All @@ -9,10 +9,6 @@ const pageCache = new Map<string, PageInfo>();
// Whether we've loaded the page index for a library yet.
const pageIndexLoaded = new Set<Library>();

function libBaseUrl(library: Library) {
return `${DEFAULT_CDN_BASE}/${library}`;
}

// Build an index of pages for the given library from the CDN's llms.txt.
export async function buildPageIndex(library: Library): Promise<PageInfo[]> {
if (pageIndexLoaded.has(library)) {
Expand All @@ -22,7 +18,8 @@ export async function buildPageIndex(library: Library): Promise<PageInfo[]> {
const pages: PageInfo[] = [];

// Read llms.txt to enumerate available pages without downloading them all.
const llmsUrl = `${libBaseUrl(library)}/llms.txt`;
const baseUrl = getLibraryBaseUrl(library);
const llmsUrl = `${baseUrl}/llms.txt`;
const txt = await fetchText(llmsUrl);
const re = /^\s*-\s*\[([^\]]+)\]\(([^)]+)\)(?:\s*:\s*(.*))?\s*$/;
for (const line of txt.split(/\r?\n/)) {
Expand All @@ -34,7 +31,7 @@ export async function buildPageIndex(library: Library): Promise<PageInfo[]> {
if (!href || !/\.md$/i.test(href)) {continue;}
const key = href.replace(/\.md$/i, '').replace(/\\/g, '/');
const name = display || path.basename(key);
const filePath = `${DEFAULT_CDN_BASE}/${key}.md`;
const filePath = `${baseUrl}/${key}.md`;
const info: PageInfo = {key, name, description, filePath, sections: []};
pages.push(info);
pageCache.set(info.key, info);
Expand Down Expand Up @@ -65,6 +62,8 @@ export async function resolvePageRef(library: Library, pageName: string): Promis
return pageCache.get(pageName)!;
}

const baseUrl = getLibraryBaseUrl(library);

if (pageName.includes('/')) {
const normalized = pageName.replace(/\\/g, '/');
const prefix = normalized.split('/', 1)[0];
Expand All @@ -73,7 +72,7 @@ export async function resolvePageRef(library: Library, pageName: string): Promis
}
const maybe = pageCache.get(normalized);
if (maybe) {return maybe;}
const filePath = `${DEFAULT_CDN_BASE}/${normalized}.md`;
const filePath = `${baseUrl}/${normalized}.md`;
const stub: PageInfo = {key: normalized, name: path.basename(normalized), description: undefined, filePath, sections: []};
pageCache.set(stub.key, stub);
return stub;
Expand All @@ -82,7 +81,7 @@ export async function resolvePageRef(library: Library, pageName: string): Promis
const key = `${library}/${pageName}`;
const maybe = pageCache.get(key);
if (maybe) {return maybe;}
const filePath = `${DEFAULT_CDN_BASE}/${key}.md`;
const filePath = `${baseUrl}/${key}.md`;
const stub: PageInfo = {key, name: pageName, description: undefined, filePath, sections: []};
pageCache.set(stub.key, stub);
return stub;
Expand Down
12 changes: 10 additions & 2 deletions packages/dev/mcp/shared/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,16 @@ export function errorToString(err: unknown): string {
}
}

// CDN base for docs. Can be overridden via env variable.
export const DEFAULT_CDN_BASE = process.env.DOCS_CDN_BASE ?? 'https://react-spectrum.adobe.com/beta';
// Default base URLs for each library
const DEFAULT_S2_BASE = 'https://react-spectrum.adobe.com';
const DEFAULT_REACT_ARIA_BASE = 'https://react-aria.adobe.com';

export function getLibraryBaseUrl(library: 's2' | 'react-aria'): string {
if (process.env.DOCS_CDN_BASE) {
return process.env.DOCS_CDN_BASE;
}
return library === 's2' ? DEFAULT_S2_BASE : DEFAULT_REACT_ARIA_BASE;
}

export async function fetchText(url: string, timeoutMs = 15000): Promise<string> {
const ctrl = new AbortController();
Expand Down