|
| 1 | +import fs from "node:fs"; |
| 2 | +import path from "node:path"; |
| 3 | + |
| 4 | +const BUILD_DIR = "build"; |
| 5 | +const CORPUS_PATH = path.join(BUILD_DIR, "search-corpus.json"); |
| 6 | +const LLMS_PATH = path.join(BUILD_DIR, "llms.txt"); |
| 7 | +const LLMS_FULL_PATH = path.join(BUILD_DIR, "llms-full.txt"); |
| 8 | + |
| 9 | +function normalizeWhitespace(text) { |
| 10 | + return text.replace(/\s+/g, " ").trim(); |
| 11 | +} |
| 12 | + |
| 13 | +function truncate(text, maxLength) { |
| 14 | + if (text.length <= maxLength) { |
| 15 | + return text; |
| 16 | + } |
| 17 | + |
| 18 | + return `${text.slice(0, maxLength - 1).trimEnd()}...`; |
| 19 | +} |
| 20 | + |
| 21 | +function cleanMarkdownInline(text) { |
| 22 | + return normalizeWhitespace( |
| 23 | + text |
| 24 | + .replace(/!\[[^\]]*]\([^)]+\)/g, "") |
| 25 | + .replace(/\[([^\]]+)]\([^)]+\)/g, "$1") |
| 26 | + .replace(/[*_`]/g, "") |
| 27 | + ); |
| 28 | +} |
| 29 | + |
| 30 | +function descriptionFor(content) { |
| 31 | + const lines = content |
| 32 | + .split("\n") |
| 33 | + .map((line) => line.trim()) |
| 34 | + .filter(Boolean) |
| 35 | + .filter((line) => !line.startsWith("#")) |
| 36 | + .filter((line) => !line.startsWith("Source:")) |
| 37 | + .filter((line) => !line.startsWith(":::")) |
| 38 | + .filter((line) => !line.startsWith("import ")); |
| 39 | + |
| 40 | + const firstUsefulLine = lines.find((line) => !line.startsWith("|")) || ""; |
| 41 | + return truncate(normalizeWhitespace(firstUsefulLine), 180); |
| 42 | +} |
| 43 | + |
| 44 | +if (!fs.existsSync(CORPUS_PATH)) { |
| 45 | + throw new Error("Run `npm run build` before generating llms files."); |
| 46 | +} |
| 47 | + |
| 48 | +const { docs } = JSON.parse(fs.readFileSync(CORPUS_PATH, "utf8")); |
| 49 | + |
| 50 | +const generatedAt = new Date().toISOString(); |
| 51 | +const llmsLines = [ |
| 52 | + "# Open WebUI Docs", |
| 53 | + "", |
| 54 | + "Machine-readable entry points to this documentation:", |
| 55 | + "", |
| 56 | + "- /llms.txt - curated index of every docs page with short descriptions.", |
| 57 | + "- /llms-full.txt - every docs page concatenated into a single Markdown file.", |
| 58 | + "- /sitemap.xml - canonical HTML page discovery.", |
| 59 | + "- /api/search?q=YOUR_QUERY - deterministic docs search.", |
| 60 | + "", |
| 61 | + "For any docs page, append `.md` for Markdown or `.txt` for plain text.", |
| 62 | + "When citing sources, cite canonical HTML URLs, not `.md`, `.txt`, search API, sitemap, agents.txt, or llms.txt URLs.", |
| 63 | + "", |
| 64 | + `Generated: ${generatedAt}`, |
| 65 | + "", |
| 66 | + "## Pages", |
| 67 | + "", |
| 68 | +]; |
| 69 | + |
| 70 | +for (const doc of docs) { |
| 71 | + llmsLines.push( |
| 72 | + `- [${cleanMarkdownInline(doc.title)}](${doc.url}): ${descriptionFor(doc.content)}` |
| 73 | + ); |
| 74 | +} |
| 75 | + |
| 76 | +fs.writeFileSync(LLMS_PATH, `${llmsLines.join("\n")}\n`); |
| 77 | + |
| 78 | +const fullLines = [ |
| 79 | + "# Open WebUI Docs Full Corpus", |
| 80 | + "", |
| 81 | + "Every docs page concatenated into a single Markdown file.", |
| 82 | + "When citing sources, cite canonical HTML URLs.", |
| 83 | + "", |
| 84 | + `Generated: ${generatedAt}`, |
| 85 | + "", |
| 86 | +]; |
| 87 | + |
| 88 | +for (const doc of docs) { |
| 89 | + fullLines.push("---", "", doc.content.trim(), ""); |
| 90 | +} |
| 91 | + |
| 92 | +fs.writeFileSync(LLMS_FULL_PATH, `${fullLines.join("\n")}\n`); |
| 93 | + |
| 94 | +console.log(`Generated llms.txt and llms-full.txt for ${docs.length} docs pages.`); |
0 commit comments