Skip to content

feat(docs): add llms.txt and raw Markdown endpoints - #10679

Open
mantoujun12 wants to merge 2 commits into
AstrBotDevs:masterfrom
mantoujun12:feat/docs-llm-support
Open

mantoujun12 wants to merge 2 commits into
AstrBotDevs:masterfrom
mantoujun12:feat/docs-llm-support

Conversation

@mantoujun12

@mantoujun12 mantoujun12 commented Oct 22, 2026 •

Copy link
Copy Markdown
Collaborator

Close #10676

本 PR 为 AstrBot 文档站增加面向 AI Agent 的内容发现与读取能力,解决 AI 工具只能抓取 HTML、需要自行遍历页面以及重复解析导航栏等问题。

主要效果:

  • 提供标准的 /llms.txt 文档索引。
  • 提供双语 /llms-full.txt 以及按语言拆分的完整文档。
  • 提供 /raw/zh/...md、/raw/en/...md 单篇 Markdown 原文。
  • 在规范文档 HTML 中加入 rel="alternate" type="text/markdown",方便 AI 工具发现原文。
  • 所有文件均在文档构建时自动生成,不修改人工维护的 Markdown 源文件。

Modifications / 改动点

新增公开入口

入口 内容 说明
/llms.txt AI Agent 文档索引 按语言和顶级主题分组,包含标题、摘要、HTML URL 和 Markdown URL
/llms-full.txt 中英文完整文档 保留正文、代码块、表格和提示内容,内部文档链接转为绝对 URL
/llms-zh.txt 中文完整文档 便于 Agent 按语言缩小上下文
/llms-en.txt 英文完整文档 便于 Agent 按语言缩小上下文
/raw/zh/<path>.md 中文单篇 Markdown 原文 原样复制源码,包括 frontmatter、代码块和原始链接
/raw/en/<path>.md 英文单篇 Markdown 原文 原样复制源码,包括 frontmatter、代码块和原始链接

生成结果示例:

项目 当前结果
Markdown 源文档 211 篇
进入索引和完整文档的规范页面 209 篇
raw Markdown 文件 211 个
llms.txt 60,551 bytes
llms-full.txt 1,162,770 bytes
llms-zh.txt 616,422 bytes
llms-en.txt 546,389 bytes

两个 layout: false / noindex 重定向占位页可以从 raw URL 直接访问,但不会进入 Agent 索引和完整文档,避免推荐过时入口。

核心改动

改动点 实现
路由生成 复用现有中英文规则:zh/index.md → /,zh/dev/x.md → /dev/x,en/dev/x.md → /en/dev/x
文档发现 只扫描 docs/zh/**/*.md 和 docs/en/**/*.md,按 Unicode 码点稳定排序
草稿过滤 draft: true 页面不生成任何公开产物
索引过滤 noindex、layout: false 页面不进入索引和完整文档
摘要提取 提取标题后的首个正文段落,跳过标题、警告框、代码块和 YAML 元数据
完整文档清理 移除 frontmatter、Vue <script setup> 和 VitePress 容器围栏,保留提示文本及代码内容
链接处理 完整文档中的相对文档链接转换为 AstrBot 文档站绝对 URL
确定性 不写入构建时间、随机值或机器相关信息,相同源码重复构建字节一致
体积保护 单语言完整文档上限 3 MiB,双语完整文档上限 5 MiB,超出时构建失败
旧产物清理 每次仅重建 public/raw 和四个生成文本,不触碰其他静态资源
静态资源处理 通过 srcExclude 禁止 VitePress 将 raw Markdown 当作 Vue 页面编译
HTML 发现 通过 transformPageData 读取原始 filePath,正确兼容中文 rewrite 路由
生成物管理 四个 llms 文件及 public/raw 加入 .gitignore,不提交生成结果

涉及文件

文件 改动
docs/scripts/generate-llms.mjs 新增无第三方依赖的生成器
docs/tests/generate-llms.test.mjs 新增路由、过滤、内容清理、链接、确定性和体积测试
docs/.vitepress/config.mjs 添加 raw 静态资源排除和 Markdown alternate 链接
docs/package.json 增加 docs:generate、docs:test,并在 dev/build 前生成文件
docs/README.md 补充 AI Agent 文档入口和维护命令
.github/workflows/docs_ci.yml 在文档相关 PR 和 master 更新时执行测试、构建及产物检查
.gitignore 忽略自动生成文件

可供维护者裁剪的范围

以下功能可以独立调整,不需要整体回退本 PR。请在 Review 中直接标注需要保留或删除的编号。

编号 功能 当前实现 若不需要
A 核心 llms/raw 能力 /llms.txt、/llms-full.txt、raw Markdown 删除 A 会导致本 Issue 的主要能力不存在
B 按语言完整文档 /llms-zh.txt、/llms-en.txt 可以只保留双语 /llms-full.txt
C HTML alternate 标签 规范页面自动声明 Markdown 原文 可以删除 transformPageData,raw 入口仍可用
D 主题分组与自动摘要 llms.txt 按一级目录分组并提取首段 可以改成仅按语言平铺的标题链接
E noindex 页面策略 可在 raw 访问,但不进入索引 可以改为完全不生成对应 raw 文件
F 体积上限 3 MiB / 5 MiB 可以取消限制,或由维护者指定其他阈值
G 独立 Docs CI PR、master 文档变更自动验证 可以删除工作流,不影响本地生成和构建
H README 说明 docs/README.md 增加入口说明 可以删除该说明段落
  • This is NOT a breaking change. / 这不是一个破坏性变更。

Screenshots or Test Results / 运行截图或测试结果

本次没有人类阅读界面的视觉改动,因此不提供页面截图。

测试命令

命令 结果
pnpm run docs:test 4 项测试全部通过
pnpm run docs:build VitePress 完整生产构建通过
git diff --check 通过

产物检查

检查项 结果
llms.txt 中的 raw 链接 209 / 209 可解析
llms.txt 中的站内链接 421 / 421 可解析
Markdown alternate 标签 209 个规范页面全部存在
raw 文件数量 211
raw 目录中的意外 HTML 页面 0
sitemap 中的 raw URL 0
生成文件进入 GitHub 变更 0,已忽略

测试覆盖

  • 中文根路由、英文 /en 路由和嵌套 index.md 路由。
  • raw URL 特殊字符编码。
  • frontmatter、草稿、noindex 和 layout: false 过滤。
  • 原始 Markdown 字节级复制及 CRLF 保留。
  • 完整文档中的 frontmatter、Vue script 和 VitePress 容器清理。
  • 代码块内容不被误处理。
  • 相对文档链接转换为绝对 URL。
  • 两次构建输出字节一致。
  • 超出体积上限时明确失败。

部署后待验证

本次未执行线上发布。文档站部署后建议补做以下冒烟检查:

请求 预期
/llms.txt HTTP 200,text/plain
/llms-full.txt HTTP 200,text/plain
/raw/zh/dev/star/plugin-new.md HTTP 200,text/markdown
/raw/en/dev/star/plugin-new.md HTTP 200,text/markdown
原有 /dev/star/plugin-new.html HTTP 200,阅读页面保持正常

Checklist / 检查清单

  • 😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
    / 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。

  • 👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
    / 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”。

  • 📚 I checked the affected WebUI instructions and screenshots in docs/zh and docs/en against the changed navigation, page structure, and labels, and updated them in this PR (or explained why no documentation update is needed). For renamed, moved, or merged entry points, I included an old entry → new entry mapping in the documentation and changelog.
    / 我已对照变化后的 WebUI 入口、页面结构和术语,核对并在本 PR 中更新 docs/zh 和 docs/en 的相关操作说明与截图(或说明无需更新文档的原因)。入口改名、移动或合并时,已在文档和 changelog 中补充 旧入口 → 新入口 对照。

  • 🤓 I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in requirements.txt and pyproject.toml.
    / 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到 requirements.txt 和 pyproject.toml 文件相应位置。

  • 😮 My changes do not introduce malicious code.
    / 我的更改没有引入恶意代码。

Summary by Sourcery

Provide deterministic AI-agent documentation indexes and raw Markdown access alongside automated validation for the documentation site.

New Features:

  • Add AI-agent-friendly documentation discovery through generated /llms.txt, language-specific and combined full-text indexes, and raw Markdown endpoints.
  • Expose Markdown source links from canonical documentation pages through alternate HTML metadata.

Enhancements:

  • Generate deterministic, filtered documentation outputs with normalized content, resolved internal links, stale-output cleanup, and configurable size safeguards.
  • Add maintainer guidance and commands for generating and testing AI-facing documentation artifacts.

CI:

  • Add dedicated documentation CI validation for generator tests, production builds, and required generated artifacts.

Documentation:

  • Document the generated AI-agent documentation endpoints and local maintenance commands.

Tests:

  • Add coverage for routing, filtering, Markdown preservation, content normalization, link conversion, deterministic output, size limits, and concurrent generation.

Chores:

  • Ignore generated documentation artifacts and raw Markdown outputs in version control.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 6 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path=".github/workflows/docs_ci.yml" line_range="20" />
<code_context>
+    runs-on: ubuntu-latest
+    steps:
+      - name: Checkout
+        uses: actions/checkout@v7
+
+      - name: Setup pnpm
</code_context>
<issue_to_address>
**Pull requests can steal CI token**

When an untrusted pull request changes the documentation scripts or other code executed by the workflow, `actions/checkout` persists the `GITHUB_TOKEN` in the local Git configuration, and the workflow later runs PR-controlled generator, test, and build code. A malicious PR can read and exfiltrate that token, then use the repository permissions it carries.

Grant the workflow only `contents: read` and set `persist-credentials: false` on checkout.
</issue_to_address>

### Comment 2
<location path="docs/scripts/generate-llms.mjs" line_range="833" />
<code_context>
+
+  const rawRoot = join(publicDir, "raw");
+  await mkdir(publicDir, { recursive: true });
+  await rm(rawRoot, { recursive: true, force: true });
+  await Promise.all(
+    ["llms.txt", "llms-full.txt", "llms-zh.txt", "llms-en.txt"].map(
</code_context>
<issue_to_address>
**Generated docs become inconsistent**

When generator runs overlap, or a run stops after removing existing outputs, `generateLlms` removes `public/raw` and the index files before writing replacements, so concurrent runs can delete each other’s files and a failed run leaves partial outputs. Callers see failed builds or inconsistent generated docs.

Write outputs to a staging location and publish them atomically, or serialize generator runs.
</issue_to_address>

### Comment 3
<location path="docs/scripts/generate-llms.mjs" line_range="510" />
<code_context>
+function resolveInternalLink(target, document) {
+  if (
+    !target ||
+    target.startsWith("#") ||
+    target.startsWith("//") ||
+    /^[A-Za-z][A-Za-z0-9+.-]*:/.test(target)
</code_context>
<issue_to_address>
**Fragment links reach wrong sections**

When a full-text output contains a fragment-only link and duplicate heading IDs, `resolveInternalLink` leaves fragment-only links unchanged, so concatenated full-text pages resolve them against the aggregate document; readers land on another page’s section or the wrong section.

Rewrite fragment-only links to target the page they came from, rather than leaving them relative to the aggregate document.
</issue_to_address>

### Comment 4
<location path="docs/scripts/generate-llms.mjs" line_range="526" />
<code_context>
+
+  let sitePath;
+  if (pathname.startsWith("/")) {
+    sitePath = cleanSitePath(pathname);
+  } else {
+    const languagePrefix = document.language === "en" ? "/en" : "";
</code_context>
<issue_to_address>
**English links target Chinese pages**

When an English Markdown page links to an English documentation page using a site-root path without an `/en/` prefix, `resolveInternalLink` leaves the English locale prefix off the generated URL, so readers following these links from the English full-text output land on the Chinese pages.

Resolve site-root documentation links with the source page’s locale while leaving root-level asset URLs unchanged.
</issue_to_address>

### Comment 5
<location path="docs/scripts/generate-llms.mjs" line_range="117-121" />
<code_context>
+ * @param {string} pathname Absolute site path.
+ * @returns {string} URL-safe site path.
+ */
+function encodeSitePath(pathname) {
+  return encodeURI(pathname)
+    .replaceAll("(", "%28")
+    .replaceAll(")", "%29");
+}
+
+/**
</code_context>
<issue_to_address>
**Encoded links break on fetch**

When a Markdown document links to a page using a percent-encoded path, such as `guide%20page.md`, `resolveInternalLink` passes percent-encoded paths such as `guide%20page.md` to `publicUrl`, and `encodeSitePath` encodes the `%` again, producing `%2520`. The generated full-text link then points to a nonexistent URL.

Normalize or decode URL escapes in the path before encoding it for the generated URL, while preserving literal percent characters safely.
</issue_to_address>

### Comment 6
<location path="docs/scripts/generate-llms.mjs" line_range="118-121" />
<code_context>
+ * @returns {string} URL-safe site path.
+ */
+function encodeSitePath(pathname) {
+  return encodeURI(pathname)
+    .replaceAll("(", "%28")
+    .replaceAll(")", "%29");
+}
+
+/**
</code_context>
<issue_to_address>
**Reserved filename characters break links**

When a Markdown filename contains `#` or `?`, `encodeSitePath` uses `encodeURI`, which leaves `#` and `?` unescaped in path segments. A generated raw or canonical link for a Markdown filename containing either character is parsed as a fragment or query instead of addressing that file, so callers cannot fetch it through the advertised URL.

Encode each path segment so reserved filename characters are percent-encoded while `/` remains a separator.
</issue_to_address>

Sourcery assessment

Approval pending. 6 findings to address first.

Blocking findings: .github/workflows/docs_ci.yml:20, docs/scripts/generate-llms.mjs:833, docs/scripts/generate-llms.mjs:510, docs/scripts/generate-llms.mjs:526, docs/scripts/generate-llms.mjs:121, and 1 more


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread .github/workflows/docs_ci.yml Resolved
Comment thread docs/scripts/generate-llms.mjs Resolved
Comment thread docs/scripts/generate-llms.mjs Resolved
Comment thread docs/scripts/generate-llms.mjs Resolved
Comment thread docs/scripts/generate-llms.mjs Resolved
Comment thread docs/scripts/generate-llms.mjs Resolved
@mantoujun12

Copy link
Copy Markdown
Collaborator Author

已按本轮代码评审意见完成修复,提交:dc786e781。

修复内容

  1. CI token 权限:为 docs_ci.yml 增加 permissions: contents: read,并在 �ctions/checkout 中设置 persist-credentials: false,PR 代码不再能读取持久化的 GITHUB_TOKEN。
  2. 生成一致性:生成器现在先在同一文件系统内完整写入 staging,再通过跨进程锁串行发布;发布前保留旧文件备份,失败时回滚。目录级重命名已改为逐文件替换,以兼容 Windows。并发运行测试确认不会留下 staging、lock 或 backup 文件。
  3. 片段链接:仅含锚点的链接现在会改写为来源页面的绝对 URL 加锚点,避免在聚合完整文档中跳到其他页面。
  4. 英文站点根链接:英文页面中的站内文档链接会自动补上 /en/,同时保留 /logo.png、/assets/、/api/、/raw/、/llms* 等根级静态资源路径。
  5. 百分号编码:路径在编码前按段解码一次,guide%20page.md 不再生成 guide%2520page,同时无法解码的字面 % 仍会安全编码。
  6. 保留字符:URL 编码改为逐路径段处理,文件名中的 #、?、括号等会分别编码为 %23、%3F、%28/%29,不会误解析为 fragment 或 query。

验证结果

  • pnpm run docs:test:5 项全部通过,新增了并发生成、片段链接、英文根链接、百分号编码和保留字符回归测试。
  • pnpm run docs:build:VitePress 生产构建通过。
  • 实际产物检查:211 个 raw 文件、209 个规范页面;llms-full.txt 中无 fragment-only 链接,也无 %2520 双重编码。
  • 生成目录无残留 .llms-* staging、lock 或 backup 文件。

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sourcery assessment

Approved.

@Soulter Soulter moved this to In Progress in AstrBot Roadmap Nov 2, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

[Feature] 增强文档站对 AI Agent 的友好性(llms.txt / Markdown Export)

1 participant