@cubicecho/agent-docs 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +52 -2
- package/_agents/docs/01-install.md +53 -0
- package/_agents/docs/checks.md +15 -0
- package/_agents/docs/ci.md +30 -0
- package/_agents/docs/cli.md +39 -0
- package/_agents/docs/common-mistakes.md +49 -0
- package/_agents/docs/config.md +25 -0
- package/dist/bin.d.ts +7 -0
- package/dist/bin.js +11 -0
- package/dist/check/check-agent-docs.d.ts +22 -0
- package/dist/check/check-agent-docs.js +254 -0
- package/dist/cli/run.d.ts +24 -0
- package/dist/cli/run.js +131 -0
- package/dist/consts.d.ts +16 -0
- package/dist/consts.js +16 -0
- package/dist/core/config.d.ts +31 -0
- package/dist/core/config.js +69 -0
- package/dist/core/errors.d.ts +17 -0
- package/dist/core/errors.js +21 -0
- package/dist/core/files.d.ts +28 -0
- package/dist/core/files.js +67 -0
- package/dist/core/json.d.ts +26 -0
- package/dist/core/json.js +44 -0
- package/dist/core/package-manifest.d.ts +36 -0
- package/dist/core/package-manifest.js +45 -0
- package/dist/defaults.d.ts +26 -0
- package/dist/defaults.js +14 -0
- package/dist/exports/entry-points.d.ts +30 -0
- package/dist/exports/entry-points.js +119 -0
- package/dist/exports/export-index.d.ts +44 -0
- package/dist/exports/export-index.js +233 -0
- package/dist/exports/module-source.d.ts +74 -0
- package/dist/exports/module-source.js +161 -0
- package/dist/exports/scan.d.ts +29 -0
- package/dist/exports/scan.js +156 -0
- package/dist/exports/summary.d.ts +36 -0
- package/dist/exports/summary.js +84 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +16 -0
- package/dist/llms/identity.d.ts +21 -0
- package/dist/llms/identity.js +67 -0
- package/dist/llms/plan.d.ts +53 -0
- package/dist/llms/plan.js +318 -0
- package/dist/llms/sync.d.ts +16 -0
- package/dist/llms/sync.js +36 -0
- package/dist/markdown/anchors.d.ts +35 -0
- package/dist/markdown/anchors.js +66 -0
- package/dist/markdown/page.d.ts +20 -0
- package/dist/markdown/page.js +57 -0
- package/llms.txt +42 -0
- package/package.json +65 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Benjamin Van Treese
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,53 @@
|
|
|
1
|
-
|
|
1
|
+
<!-- AI agents: read llms.txt and _agents/docs/ for the full, version-matched API. -->
|
|
2
|
+
# @cubicecho/agent-docs
|
|
2
3
|
|
|
3
|
-
|
|
4
|
+
Generates and checks the docs a package ships for AI coding agents. `agent-docs llms` writes `llms.txt` from the
|
|
5
|
+
pages in `_agents/docs/` and, for a TypeScript package, from the doc comments behind every `package.json`
|
|
6
|
+
`exports` entry. `agent-docs check` holds the README, AGENTS.md, links, page sizes and package contents to the
|
|
7
|
+
agent docs standard. Both exit 1 with one line per problem, so CI can run them as they are.
|
|
8
|
+
|
|
9
|
+
It exists because every repo had its own copy of the generator, and the copies had drifted: some indexed only
|
|
10
|
+
`src/index.ts` and missed subpath entries such as `./mdns`, and all of them cut a summary short at "e.g.". One
|
|
11
|
+
package, one set of fixes.
|
|
12
|
+
|
|
13
|
+
## Install
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
npm i -D @cubicecho/agent-docs
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Node 22 or later. No runtime dependencies.
|
|
20
|
+
|
|
21
|
+
## Quick start
|
|
22
|
+
|
|
23
|
+
```json
|
|
24
|
+
{
|
|
25
|
+
"scripts": {
|
|
26
|
+
"llms": "agent-docs llms",
|
|
27
|
+
"llms:check": "agent-docs llms --check",
|
|
28
|
+
"docs:check": "agent-docs check"
|
|
29
|
+
},
|
|
30
|
+
"files": ["dist", "llms.txt", "_agents/docs"]
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
```sh
|
|
35
|
+
npm run llms # write llms.txt, then commit it
|
|
36
|
+
npm run llms:check # in CI: fails when llms.txt is stale
|
|
37
|
+
npm run docs:check # in CI: fails when the docs fall short of the standard
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Documentation
|
|
41
|
+
|
|
42
|
+
- [llms.txt](https://github.com/cubicecho/agent-docs/blob/main/llms.txt), the index of everything below
|
|
43
|
+
- [Install and wire up](https://github.com/cubicecho/agent-docs/blob/main/_agents/docs/01-install.md)
|
|
44
|
+
- [The agent-docs command](https://github.com/cubicecho/agent-docs/blob/main/_agents/docs/cli.md)
|
|
45
|
+
- [`_agents/llms.json`](https://github.com/cubicecho/agent-docs/blob/main/_agents/docs/config.md)
|
|
46
|
+
- [What check enforces](https://github.com/cubicecho/agent-docs/blob/main/_agents/docs/checks.md)
|
|
47
|
+
- [CI](https://github.com/cubicecho/agent-docs/blob/main/_agents/docs/ci.md)
|
|
48
|
+
- [Common mistakes](https://github.com/cubicecho/agent-docs/blob/main/_agents/docs/common-mistakes.md)
|
|
49
|
+
- [Changelog](https://github.com/cubicecho/agent-docs/blob/main/CHANGELOG.md)
|
|
50
|
+
|
|
51
|
+
## License
|
|
52
|
+
|
|
53
|
+
[MIT](https://github.com/cubicecho/agent-docs/blob/main/LICENSE)
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# Install and wire up
|
|
2
|
+
|
|
3
|
+
Add `@cubicecho/agent-docs` as a dev dependency, then give the package three npm scripts: `llms` writes `llms.txt`, `llms:check` fails when it is stale, and `docs:check` checks the rest of the agent docs standard.
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
npm i -D @cubicecho/agent-docs
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
```json
|
|
10
|
+
{
|
|
11
|
+
"scripts": {
|
|
12
|
+
"llms": "agent-docs llms",
|
|
13
|
+
"llms:check": "agent-docs llms --check",
|
|
14
|
+
"docs:check": "agent-docs check"
|
|
15
|
+
},
|
|
16
|
+
"files": ["dist", "llms.txt", "_agents/docs"]
|
|
17
|
+
}
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Both commands work on the current folder, so run them from the package root. Node 22 or later; no other runtime dependencies.
|
|
21
|
+
|
|
22
|
+
## What it needs
|
|
23
|
+
|
|
24
|
+
- A manifest with a name and a description: `package.json`, `haxelib.json`, `pyproject.toml` (`[project]`) or Home Assistant's `manifest.json`, read in that order. `title` and `summary` in `_agents/llms.json` replace them.
|
|
25
|
+
- Pages in `_agents/docs/`, each a `# Title` line followed by a one-paragraph summary. See [the CLI page](cli.md#llms) for how they are listed.
|
|
26
|
+
- For TypeScript, nothing more: when `src/` holds `.ts` files, every `package.json` `exports` entry is indexed from its doc comments.
|
|
27
|
+
|
|
28
|
+
## First run
|
|
29
|
+
|
|
30
|
+
```sh
|
|
31
|
+
npx agent-docs llms
|
|
32
|
+
npx agent-docs check
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Commit `llms.txt` (and `_agents/docs/exports/` when it is generated). Then add the two checks to CI, as [the CI page](ci.md) shows.
|
|
36
|
+
|
|
37
|
+
## From code
|
|
38
|
+
|
|
39
|
+
The same work is exported as functions, for a build script or a test:
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
import { checkAgentDocs, findStaleFiles, planLlms, writeLlms } from '@cubicecho/agent-docs';
|
|
43
|
+
|
|
44
|
+
const root = process.cwd();
|
|
45
|
+
const plan = planLlms({ root });
|
|
46
|
+
const stale = findStaleFiles(root, plan);
|
|
47
|
+
if (stale.length > 0) {
|
|
48
|
+
writeLlms(root, plan);
|
|
49
|
+
}
|
|
50
|
+
const { problems } = checkAgentDocs(root);
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
`planLlms` throws an `AgentDocsError` whose `problems` holds one line per problem; `checkAgentDocs` returns them instead.
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# What check enforces
|
|
2
|
+
|
|
3
|
+
`agent-docs check` holds a package to nine rules and prints one line per broken one, saying what to change. A private workspace root (`private: true` with `workspaces`) is held to the repo-level rules only: links, CLAUDE.md and `.agents/`.
|
|
4
|
+
|
|
5
|
+
1. **Page size.** Each page in the docs folder is at most 25 KB. Skills servers and file tools truncate larger reads.
|
|
6
|
+
2. **Contents list.** A page over 100 lines lists every `##` heading as a `(#anchor)` link above its first `##`.
|
|
7
|
+
3. **Links.** Every relative link in the pages, `llms.txt` and `AGENTS.md` stays inside the repo, names a file that exists and, with `#anchor`, a heading in it. Anchors follow GitHub: lower case, punctuation dropped but `_` and `-` kept, spaces to hyphens, a repeated heading gets `-1`. `README.md` has no relative links except `#anchor`s, because npm and haxelib render it away from the repo. Links in code spans and fences are ignored.
|
|
8
|
+
4. **README.** `README.md` exists and contains `<!-- AI agents:`, the hidden pointer to the agent docs.
|
|
9
|
+
5. **CLAUDE.md.** If present, it is a symlink to `AGENTS.md` or contains `@AGENTS.md`. A file that only links to AGENTS.md hides it.
|
|
10
|
+
6. **No hidden `.agents/`.** Only `.agents/skills/` may exist, because search tools skip dot-folders.
|
|
11
|
+
7. **`files`.** A published `package.json` with a `files` list includes `llms.txt` and the docs folder, and not all of `_agents`.
|
|
12
|
+
8. **LICENSE.** A published package has a `LICENSE`, `LICENCE` or `COPYING` file, and a package that is not a workspace root has at least one page.
|
|
13
|
+
9. **haxelib.** With `haxelib.json` and a `.releaserc.json`, the `semantic-release-haxelib` plugin's `additionalFiles` lists `llms.txt` and every page, every listed file exists, and `LICENSE.md` exists. The plugin takes files, not folders, and skips a missing path silently.
|
|
14
|
+
|
|
15
|
+
`agent-docs llms` fails on its own problems: a page without a `# Title` or a summary paragraph, a manifest without a name or description, an `exports` entry whose `src/` file is missing, a re-export of a missing module, an `export` statement it can't read (such as `export =`), and an llms.txt over 25 KB.
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# CI
|
|
2
|
+
|
|
3
|
+
Run `llms --check` and `check` wherever the repo already runs its checks, and check what `npm pack` would ship after the build.
|
|
4
|
+
|
|
5
|
+
```yaml
|
|
6
|
+
- run: npm ci
|
|
7
|
+
- name: Agent docs
|
|
8
|
+
run: |
|
|
9
|
+
npx agent-docs llms --check
|
|
10
|
+
npx agent-docs check
|
|
11
|
+
- run: npm run build
|
|
12
|
+
- name: The package ships the agent docs
|
|
13
|
+
run: |
|
|
14
|
+
npm pack --dry-run --ignore-scripts 2>&1 | tee pack.log
|
|
15
|
+
grep -q ' llms.txt$' pack.log && grep -q '_agents/docs/' pack.log
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
`--ignore-scripts` keeps `prepack` from building a second time. In a matrix workflow, run the docs checks in one leg: they don't depend on the Node version.
|
|
19
|
+
|
|
20
|
+
## Moving off a copied scripts/llms-txt.mjs
|
|
21
|
+
|
|
22
|
+
1. Delete `scripts/llms-txt.mjs` (and `scripts/check-agent-docs.mjs`).
|
|
23
|
+
2. Point the scripts at the command: `"llms": "agent-docs llms"`, `"llms:check": "agent-docs llms --check"`, `"docs:check": "agent-docs check"`.
|
|
24
|
+
3. Take the generator out of `build`: `"build": "tsc -p tsconfig.build.json"`. A build that rewrites llms.txt makes a later `--check` pass every time.
|
|
25
|
+
4. Replace a `git diff --exit-code -- llms.txt` step after the build with `npm run llms:check` before it.
|
|
26
|
+
5. Add `"_agents/docs"` to `files`, run `npm run llms`, and commit the new llms.txt: it now has a section per `exports` entry.
|
|
27
|
+
|
|
28
|
+
## Monorepos
|
|
29
|
+
|
|
30
|
+
Run both commands in each published package's folder (`npm run docs:check --workspaces`), and `agent-docs check` once at the root for the repo-level rules.
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# The agent-docs command
|
|
2
|
+
|
|
3
|
+
`agent-docs llms` writes or checks `llms.txt`, and `agent-docs check` checks the package against the agent docs standard. Each prints one line per problem and exits 1 when there is any; otherwise it exits 0.
|
|
4
|
+
|
|
5
|
+
## llms
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
agent-docs llms # write llms.txt
|
|
9
|
+
agent-docs llms --check # CI: exit 1 if llms.txt or an export page is missing or stale
|
|
10
|
+
agent-docs llms --stdout # print what would be written, write nothing
|
|
11
|
+
agent-docs llms --base-url https://org.github.io/pkg/ # add a "Base URL:" line
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
What it writes, in order:
|
|
15
|
+
|
|
16
|
+
1. `# <name>`, `> <description>`, the optional `Base URL:` line, and one paragraph saying where the file comes from.
|
|
17
|
+
2. One link per page in `_agents/docs/` (or `docsDir`): the page's `# H1` is the link text, and its first plain paragraph is the note. HTML comments, code fences, lists, quotes and tables are skipped on the way to that paragraph, and links in it become their text. Top-level pages go under `## Docs`, each subfolder under its own `##`, named from the folder (`getting-started` → `Getting started`, a `01-` prefix dropped) or from `sections` in `_agents/llms.json`. Pages sort by file name, so prefix them to set the order.
|
|
18
|
+
3. When `src/` holds TypeScript: `Requires Node …` and the peer dependencies, then one `## Exports: <import path>` section per `package.json` `exports` entry. Each section opens with the first paragraph of the entry file's own doc comment and lists each export with the first sentence of its doc comment.
|
|
19
|
+
4. `extra` from `_agents/llms.json`, as written.
|
|
20
|
+
|
|
21
|
+
### How an export entry finds its source
|
|
22
|
+
|
|
23
|
+
An entry's `types` (or `import`, then `default`) target in `dist/` maps to the same path in `src/` with `.ts`: `./dist/spec/entry.js` → `src/spec/entry.ts`. Without `exports`, `src/index.ts` is the only entry. Wildcard subpaths and non-code targets (`./package.json`, CSS) are skipped. A name re-exported through `export { … } from`, `export *`, `export * as ns` or an import is followed to the declaration that documents it. Names re-exported from another package are listed on one line, since that package documents them.
|
|
24
|
+
|
|
25
|
+
### When the export index is too big
|
|
26
|
+
|
|
27
|
+
If llms.txt would pass 25 KB with the index inline, the index moves to generated pages, `_agents/docs/exports/01-index.md`, `02-mdns.md` and so on, one per entry, and llms.txt links them under `## Exports`. An entry whose page would pass 25 KB is split into `01-index-1.md`, `01-index-2.md`. Each generated page starts with a `<!-- Generated by @cubicecho/agent-docs … -->` line; a rerun replaces those and deletes the ones it no longer makes. If llms.txt is still over 25 KB after that, the command fails.
|
|
28
|
+
|
|
29
|
+
### Undocumented exports
|
|
30
|
+
|
|
31
|
+
An export with no doc comment is listed by name only, and `llms` prints `no doc comment on …` without failing.
|
|
32
|
+
|
|
33
|
+
## check
|
|
34
|
+
|
|
35
|
+
```sh
|
|
36
|
+
agent-docs check
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
It changes nothing. Every rule is on [the checks page](checks.md).
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# Common mistakes
|
|
2
|
+
|
|
3
|
+
The mistakes that make `llms` or `check` fail, or let a stale index through, each with its fix.
|
|
4
|
+
|
|
5
|
+
## Checking llms.txt after a build that regenerates it
|
|
6
|
+
|
|
7
|
+
`llms --check` then compares the file with itself and always passes. Run it before the build, and keep the generator out of `build` and `prepack`.
|
|
8
|
+
|
|
9
|
+
```json
|
|
10
|
+
"build": "tsc -p tsconfig.build.json",
|
|
11
|
+
"llms:check": "agent-docs llms --check"
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Editing llms.txt or a generated export page by hand
|
|
15
|
+
|
|
16
|
+
The next `llms` run overwrites it, and `llms --check` fails until then. Change the page in `_agents/docs/` or the doc comment in `src/`, then rerun `agent-docs llms`. A hand-written page placed in `_agents/docs/exports/` stops `llms` with "was written by hand"; move it to another folder.
|
|
17
|
+
|
|
18
|
+
## A blank line between a doc comment and its declaration
|
|
19
|
+
|
|
20
|
+
The comment is then read as the file's own comment, not the declaration's, and the export is listed with no summary. Keep the comment directly above the declaration; a `// biome-ignore` line between them is fine.
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
/** Fetches a URL and parses its body as JSON. */
|
|
24
|
+
export async function fetchJson(url: string): Promise<unknown> { ... }
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## An export form the index can't read
|
|
28
|
+
|
|
29
|
+
`export = x` and other forms outside declarations, `export { … }` lists and `export *` stop `llms` with the file and line. Write a declaration or an `export { … } from` list.
|
|
30
|
+
|
|
31
|
+
## An exports entry with no matching source
|
|
32
|
+
|
|
33
|
+
`exports["./x"]` pointing at `./dist/x.js` needs `src/x.ts`. For a package whose build doesn't mirror `src/`, set `"exports": false` in `_agents/llms.json`.
|
|
34
|
+
|
|
35
|
+
## Leaving the docs out of the package
|
|
36
|
+
|
|
37
|
+
`files: ["dist"]` ships neither llms.txt nor the pages, so an agent in `node_modules` gets nothing. List both, never all of `_agents`:
|
|
38
|
+
|
|
39
|
+
```json
|
|
40
|
+
"files": ["dist", "llms.txt", "_agents/docs"]
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## CLAUDE.md that links to AGENTS.md
|
|
44
|
+
|
|
45
|
+
Claude Code reads only CLAUDE.md when both exist and does not follow a link. Make it a symlink (`ln -s AGENTS.md CLAUDE.md`) or a file whose first line is `@AGENTS.md`.
|
|
46
|
+
|
|
47
|
+
## Relative links in the README
|
|
48
|
+
|
|
49
|
+
npm and haxelib render the README away from the repo, so `[docs](_agents/docs/cli.md)` breaks there. Use the absolute GitHub URL in the README; relative links belong in `_agents/docs/` and llms.txt.
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# _agents/llms.json
|
|
2
|
+
|
|
3
|
+
`_agents/llms.json` is the optional overrides file both commands read; without it, every field takes its default.
|
|
4
|
+
|
|
5
|
+
```json
|
|
6
|
+
{
|
|
7
|
+
"title": "dropecho.ds (haxelib) / @dropecho/ds (npm)",
|
|
8
|
+
"summary": "One line that replaces the manifest's description.",
|
|
9
|
+
"sections": { "guides": "How-to guides", "api": "API reference" },
|
|
10
|
+
"extra": "## Elsewhere\n\n- [Site](https://example.com/): the human docs.",
|
|
11
|
+
"docsDir": "skill",
|
|
12
|
+
"exports": false
|
|
13
|
+
}
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
| Field | Type | Default | Does |
|
|
17
|
+
|---|---|---|---|
|
|
18
|
+
| `title` | string | the manifest's `name` | The `# H1` of llms.txt |
|
|
19
|
+
| `summary` | string | the manifest's `description` | The `>` line; whitespace collapses to single spaces |
|
|
20
|
+
| `sections` | object of strings | none | Heading text per docs subfolder name |
|
|
21
|
+
| `extra` | string | none | Markdown appended after the generated sections |
|
|
22
|
+
| `docsDir` | string | `_agents/docs` | Where the pages are, relative to the package root. `check` reads it too, and then wants it in `package.json` `files` |
|
|
23
|
+
| `exports` | boolean | `true` | `false` turns the TypeScript export index off |
|
|
24
|
+
|
|
25
|
+
A field of the wrong type fails both commands with a line naming it. A dual haxelib and npm package sets `title` to name both, since the npm name would win otherwise.
|
package/dist/bin.d.ts
ADDED
package/dist/bin.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* The `agent-docs` command.
|
|
4
|
+
*
|
|
5
|
+
* Runs in the current folder, which is the package root.
|
|
6
|
+
*/
|
|
7
|
+
import { runCli } from './cli/run.js';
|
|
8
|
+
process.exitCode = runCli(process.argv.slice(2), process.cwd(), {
|
|
9
|
+
out: (line) => console.log(line),
|
|
10
|
+
err: (line) => console.error(line),
|
|
11
|
+
});
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { type LimitSettings } from '../defaults.ts';
|
|
2
|
+
/** What {@link checkAgentDocs} found. */
|
|
3
|
+
export interface CheckResult {
|
|
4
|
+
/** One line per problem, each saying what to change; empty when the package meets the standard. */
|
|
5
|
+
problems: string[];
|
|
6
|
+
/** How many pages the docs folder holds. */
|
|
7
|
+
pageCount: number;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Checks a package against the agent documentation standard, changing nothing.
|
|
11
|
+
*
|
|
12
|
+
* @param root - Package root.
|
|
13
|
+
* @param overrides - Limits to change, for a test or an unusual repo.
|
|
14
|
+
* @returns The problems found and the page count.
|
|
15
|
+
*
|
|
16
|
+
* @remarks
|
|
17
|
+
* The rules: pages within `pageLimitKb`, a contents list on a page over `contentsAfterLines`, relative links that
|
|
18
|
+
* resolve inside the repo (README has none but `#anchor`s), the README's hidden pointer, CLAUDE.md as a link to
|
|
19
|
+
* AGENTS.md, no hidden `.agents/`, a LICENSE and the right `files` for a published package, and the
|
|
20
|
+
* semantic-release-haxelib file list. A private workspace root is held to the repo-level rules only.
|
|
21
|
+
*/
|
|
22
|
+
export declare function checkAgentDocs(root: string, overrides?: Partial<LimitSettings>): CheckResult;
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
import { existsSync, lstatSync, readdirSync, readFileSync } from 'node:fs';
|
|
2
|
+
import { dirname, join, relative } from 'node:path';
|
|
3
|
+
import { BYTES_PER_KB, LLMS_FILE, POINTER_LINE, POINTER_TEXT } from '../consts.js';
|
|
4
|
+
import { loadLlmsConfig } from '../core/config.js';
|
|
5
|
+
import { AgentDocsError } from '../core/errors.js';
|
|
6
|
+
import { markdownFiles } from '../core/files.js';
|
|
7
|
+
import { isJsonObject, readJsonFile } from '../core/json.js';
|
|
8
|
+
import { readPackageManifest } from '../core/package-manifest.js';
|
|
9
|
+
import { LIMIT_DEFAULTS } from '../defaults.js';
|
|
10
|
+
import { headings, links } from '../markdown/anchors.js';
|
|
11
|
+
const URL_SCHEME = /^[a-z][a-z0-9+.-]*:/i;
|
|
12
|
+
const LICENSE_FILE = /^(LICEN[CS]E|COPYING)(\.|$)/i;
|
|
13
|
+
const HAXELIB_PLUGIN = 'semantic-release-haxelib';
|
|
14
|
+
/** Files whose relative links are checked, beside the pages. */
|
|
15
|
+
const LINKED_FILES = [LLMS_FILE, 'README.md', 'AGENTS.md'];
|
|
16
|
+
/**
|
|
17
|
+
* Checks a package against the agent documentation standard, changing nothing.
|
|
18
|
+
*
|
|
19
|
+
* @param root - Package root.
|
|
20
|
+
* @param overrides - Limits to change, for a test or an unusual repo.
|
|
21
|
+
* @returns The problems found and the page count.
|
|
22
|
+
*
|
|
23
|
+
* @remarks
|
|
24
|
+
* The rules: pages within `pageLimitKb`, a contents list on a page over `contentsAfterLines`, relative links that
|
|
25
|
+
* resolve inside the repo (README has none but `#anchor`s), the README's hidden pointer, CLAUDE.md as a link to
|
|
26
|
+
* AGENTS.md, no hidden `.agents/`, a LICENSE and the right `files` for a published package, and the
|
|
27
|
+
* semantic-release-haxelib file list. A private workspace root is held to the repo-level rules only.
|
|
28
|
+
*/
|
|
29
|
+
export function checkAgentDocs(root, overrides = {}) {
|
|
30
|
+
const limits = { ...LIMIT_DEFAULTS, ...overrides };
|
|
31
|
+
const problems = [];
|
|
32
|
+
const setup = readSetup(root, problems);
|
|
33
|
+
if (setup === undefined) {
|
|
34
|
+
return { problems, pageCount: 0 };
|
|
35
|
+
}
|
|
36
|
+
const { docsDir, isWorkspaceRoot, pages } = setup;
|
|
37
|
+
if (pages.length === 0 && isWorkspaceRoot === false) {
|
|
38
|
+
problems.push(`${docsDir}/ has no pages: write the agent docs there, starting with 01-install.md.`);
|
|
39
|
+
}
|
|
40
|
+
problems.push(...pageProblems(root, pages, limits), ...linkProblems(root, pages, isWorkspaceRoot), ...repoProblems(root, isWorkspaceRoot), ...packageProblems(root, docsDir), ...haxelibProblems(root, pages));
|
|
41
|
+
return { problems, pageCount: pages.length };
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Reads what every rule needs: the docs folder, its pages and whether this is a workspace root.
|
|
45
|
+
*
|
|
46
|
+
* @param root - Package root.
|
|
47
|
+
* @param problems - Receives a line when a config file can't be read; appended to in place.
|
|
48
|
+
* @returns The setup, or undefined when llms.json or package.json can't be read.
|
|
49
|
+
*/
|
|
50
|
+
function readSetup(root, problems) {
|
|
51
|
+
try {
|
|
52
|
+
const { docsDir } = loadLlmsConfig(root);
|
|
53
|
+
const isWorkspaceRoot = readPackageManifest(root)?.isWorkspaceRoot ?? false;
|
|
54
|
+
const pages = markdownFiles(root, docsDir).map((path) => join(root, path));
|
|
55
|
+
return { docsDir, isWorkspaceRoot, pages };
|
|
56
|
+
}
|
|
57
|
+
catch (error) {
|
|
58
|
+
if (error instanceof AgentDocsError) {
|
|
59
|
+
problems.push(...error.problems);
|
|
60
|
+
return undefined;
|
|
61
|
+
}
|
|
62
|
+
throw error;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* A path as messages show it.
|
|
67
|
+
*
|
|
68
|
+
* @param root - Package root.
|
|
69
|
+
* @param path - Absolute path.
|
|
70
|
+
* @returns The path relative to the root, or `.` for the root itself.
|
|
71
|
+
*/
|
|
72
|
+
function shown(root, path) {
|
|
73
|
+
return relative(root, path) || '.';
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Rules 1 and 2: page size, and a contents list on a long page.
|
|
77
|
+
*
|
|
78
|
+
* @param root - Package root.
|
|
79
|
+
* @param pages - Absolute page paths.
|
|
80
|
+
* @param limits - The size limits.
|
|
81
|
+
* @returns One line per page over a limit.
|
|
82
|
+
*/
|
|
83
|
+
function pageProblems(root, pages, limits) {
|
|
84
|
+
const { pageLimitKb, contentsAfterLines } = limits;
|
|
85
|
+
return pages.flatMap((page) => {
|
|
86
|
+
const text = readFileSync(page, 'utf8');
|
|
87
|
+
const found = [];
|
|
88
|
+
const size = Buffer.byteLength(text);
|
|
89
|
+
if (size > pageLimitKb * BYTES_PER_KB) {
|
|
90
|
+
found.push(`${shown(root, page)} is ${Math.round(size / BYTES_PER_KB)} KB, over ${pageLimitKb} KB: split it by task.`);
|
|
91
|
+
}
|
|
92
|
+
const lineCount = text.split('\n').length;
|
|
93
|
+
if (lineCount > contentsAfterLines) {
|
|
94
|
+
const top = text.slice(0, Math.max(0, text.search(/^## /m)));
|
|
95
|
+
const missing = headings(text).filter((h) => h.level === 2 && top.includes(`(#${h.anchor})`) === false);
|
|
96
|
+
if (missing.length > 0) {
|
|
97
|
+
const anchors = missing.map((h) => `#${h.anchor}`).join(', ');
|
|
98
|
+
found.push(`${shown(root, page)} is ${lineCount} lines; its contents list lacks ${anchors}: list each "##" heading above the first one.`);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return found;
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Rule 3: every relative link resolves inside the repo, and the README has none but anchors.
|
|
106
|
+
*
|
|
107
|
+
* @param root - Package root.
|
|
108
|
+
* @param pages - Absolute page paths.
|
|
109
|
+
* @param isWorkspaceRoot - A private monorepo root, whose README is read on GitHub only.
|
|
110
|
+
* @returns One line per bad link.
|
|
111
|
+
*/
|
|
112
|
+
function linkProblems(root, pages, isWorkspaceRoot) {
|
|
113
|
+
const readme = join(root, 'README.md');
|
|
114
|
+
const sources = [...pages, ...LINKED_FILES.map((file) => join(root, file)).filter((file) => existsSync(file))];
|
|
115
|
+
const found = [];
|
|
116
|
+
for (const file of sources) {
|
|
117
|
+
for (const href of links(readFileSync(file, 'utf8'))) {
|
|
118
|
+
const isAbsolute = URL_SCHEME.test(href) || href.startsWith('//');
|
|
119
|
+
if (isAbsolute) {
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
const [path, anchor] = href.split('#');
|
|
123
|
+
const isReadmeRelative = file === readme && path !== '' && isWorkspaceRoot === false;
|
|
124
|
+
if (isReadmeRelative) {
|
|
125
|
+
found.push(`README.md links to ${href} relatively: npm and haxelib render the README away from the repo, so use an absolute URL.`);
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
const target = path === '' ? file : join(dirname(file), decodeURI(path));
|
|
129
|
+
if (relative(root, target).startsWith('..')) {
|
|
130
|
+
found.push(`${shown(root, file)} links to ${href}, outside the repo: it breaks on GitHub and in the package. Use an absolute URL.`);
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
if (existsSync(target) === false) {
|
|
134
|
+
found.push(`${shown(root, file)} links to ${href}, which does not exist: fix the link or add the file.`);
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
if (anchor && target.endsWith('.md')) {
|
|
138
|
+
const known = new Set(headings(readFileSync(target, 'utf8')).map((h) => h.anchor));
|
|
139
|
+
if (known.has(anchor) === false) {
|
|
140
|
+
found.push(`${shown(root, file)} links to ${href}; ${shown(root, target)} has no such heading: fix the anchor or the heading.`);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return found;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Rules 4 to 6: the README and its pointer, CLAUDE.md, and the hidden `.agents/` folder.
|
|
149
|
+
*
|
|
150
|
+
* @param root - Package root.
|
|
151
|
+
* @param isWorkspaceRoot - A private monorepo root, which needs no pointer.
|
|
152
|
+
* @returns One line per problem.
|
|
153
|
+
*/
|
|
154
|
+
function repoProblems(root, isWorkspaceRoot) {
|
|
155
|
+
const found = [];
|
|
156
|
+
const readme = join(root, 'README.md');
|
|
157
|
+
if (existsSync(readme) === false) {
|
|
158
|
+
found.push('README.md is missing: write one for people, starting with the hidden pointer line.');
|
|
159
|
+
}
|
|
160
|
+
else if (isWorkspaceRoot === false && readFileSync(readme, 'utf8').includes(`<!-- ${POINTER_TEXT}`) === false) {
|
|
161
|
+
found.push(`README.md lacks the hidden pointer: add ${POINTER_LINE} as its first line.`);
|
|
162
|
+
}
|
|
163
|
+
const claude = join(root, 'CLAUDE.md');
|
|
164
|
+
if (existsSync(claude)) {
|
|
165
|
+
const isLink = lstatSync(claude).isSymbolicLink();
|
|
166
|
+
const importsAgents = isLink || readFileSync(claude, 'utf8').includes('@AGENTS.md');
|
|
167
|
+
if (importsAgents === false) {
|
|
168
|
+
found.push('CLAUDE.md is a separate file: make it a symlink to AGENTS.md, or import it with "@AGENTS.md".');
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
const hidden = join(root, '.agents');
|
|
172
|
+
if (existsSync(hidden)) {
|
|
173
|
+
const strays = readdirSync(hidden).filter((name) => name !== 'skills');
|
|
174
|
+
if (strays.length > 0) {
|
|
175
|
+
found.push(`.agents/ holds ${strays.join(', ')}: move it with "git mv .agents _agents".`);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
return found;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Rules 7 and 8: a published package ships a license, llms.txt and the pages, and never all of `_agents`.
|
|
182
|
+
*
|
|
183
|
+
* @param root - Package root.
|
|
184
|
+
* @param docsDir - The pages' folder, which `files` must list.
|
|
185
|
+
* @returns One line per problem; none for a private package.
|
|
186
|
+
*/
|
|
187
|
+
function packageProblems(root, docsDir) {
|
|
188
|
+
const manifest = readPackageManifest(root);
|
|
189
|
+
if (manifest === undefined || manifest.isPrivate) {
|
|
190
|
+
return [];
|
|
191
|
+
}
|
|
192
|
+
const found = [];
|
|
193
|
+
const hasLicense = readdirSync(root).some((name) => LICENSE_FILE.test(name));
|
|
194
|
+
if (hasLicense === false) {
|
|
195
|
+
found.push('No LICENSE file: the published package carries no license text. Add one.');
|
|
196
|
+
}
|
|
197
|
+
if (manifest.files === undefined) {
|
|
198
|
+
return found;
|
|
199
|
+
}
|
|
200
|
+
const files = manifest.files.map((f) => f.replace(/^\.\//, '').replace(/\/$/, ''));
|
|
201
|
+
for (const needed of [LLMS_FILE, docsDir]) {
|
|
202
|
+
if (files.includes(needed) === false) {
|
|
203
|
+
found.push(`package.json "files" lacks "${needed}", so it never ships: add it.`);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
if (files.includes('_agents')) {
|
|
207
|
+
found.push(`package.json "files" ships all of "_agents": list "${docsDir}" (and "_agents/skills") instead.`);
|
|
208
|
+
}
|
|
209
|
+
return found;
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Rule 9: semantic-release-haxelib lists llms.txt and every page, every listed file exists, and LICENSE.md does.
|
|
213
|
+
*
|
|
214
|
+
* @param root - Package root.
|
|
215
|
+
* @param pages - Absolute page paths.
|
|
216
|
+
* @returns One line per problem; none unless the package has both haxelib.json and a release config.
|
|
217
|
+
*
|
|
218
|
+
* @remarks
|
|
219
|
+
* The plugin zips the class path plus `additionalFiles`, one file per entry, and silently skips a path that doesn't
|
|
220
|
+
* exist, so a page missing from the list never ships.
|
|
221
|
+
*/
|
|
222
|
+
function haxelibProblems(root, pages) {
|
|
223
|
+
const releaseFile = ['.releaserc.json', '.releaserc'].find((file) => existsSync(join(root, file)));
|
|
224
|
+
const isHaxelib = existsSync(join(root, 'haxelib.json')) && releaseFile !== undefined;
|
|
225
|
+
if (isHaxelib === false || releaseFile === undefined) {
|
|
226
|
+
return [];
|
|
227
|
+
}
|
|
228
|
+
const found = [];
|
|
229
|
+
let release;
|
|
230
|
+
try {
|
|
231
|
+
release = readJsonFile(root, releaseFile);
|
|
232
|
+
}
|
|
233
|
+
catch {
|
|
234
|
+
found.push(`${releaseFile} is not JSON, so the haxelib file list can't be checked: write it as JSON.`);
|
|
235
|
+
}
|
|
236
|
+
const plugins = isJsonObject(release) && Array.isArray(release.plugins) ? release.plugins : [];
|
|
237
|
+
const haxelib = plugins.find((plugin) => Array.isArray(plugin) && plugin[0] === HAXELIB_PLUGIN);
|
|
238
|
+
if (Array.isArray(haxelib)) {
|
|
239
|
+
const options = isJsonObject(haxelib[1]) ? haxelib[1] : {};
|
|
240
|
+
const listedFiles = Array.isArray(options.additionalFiles) ? options.additionalFiles : [];
|
|
241
|
+
const listed = new Set(listedFiles.filter((f) => typeof f === 'string').map((f) => f.replace(/^\.\//, '')));
|
|
242
|
+
const wanted = [LLMS_FILE, ...pages.map((page) => relative(root, page).split('\\').join('/'))];
|
|
243
|
+
for (const file of wanted.filter((f) => listed.has(f) === false)) {
|
|
244
|
+
found.push(`${releaseFile} ${HAXELIB_PLUGIN} "additionalFiles" lacks "${file}", so the haxelib zip won't contain it: add it.`);
|
|
245
|
+
}
|
|
246
|
+
for (const file of [...listed].filter((f) => existsSync(join(root, f)) === false)) {
|
|
247
|
+
found.push(`${releaseFile} lists "${file}" in "additionalFiles", but it doesn't exist (the plugin skips it silently): fix or remove the entry.`);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
if (existsSync(join(root, 'LICENSE.md')) === false) {
|
|
251
|
+
found.push('LICENSE.md is missing: the haxelib zip only takes a license file by that exact name.');
|
|
252
|
+
}
|
|
253
|
+
return found;
|
|
254
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/** Where the CLI prints. */
|
|
2
|
+
export interface CliOutput {
|
|
3
|
+
/** Prints one line of normal output. */
|
|
4
|
+
out: (line: string) => void;
|
|
5
|
+
/** Prints one line of problems or usage. */
|
|
6
|
+
err: (line: string) => void;
|
|
7
|
+
}
|
|
8
|
+
/** The process exit codes the CLI returns. */
|
|
9
|
+
export declare const ExitCode: {
|
|
10
|
+
/** Everything passed or was written. */
|
|
11
|
+
readonly Ok: 0;
|
|
12
|
+
/** At least one problem, or a usage error. */
|
|
13
|
+
readonly Failed: 1;
|
|
14
|
+
};
|
|
15
|
+
export type ExitCode = (typeof ExitCode)[keyof typeof ExitCode];
|
|
16
|
+
/**
|
|
17
|
+
* Runs one CLI command against a package.
|
|
18
|
+
*
|
|
19
|
+
* @param argv - The arguments after the program name.
|
|
20
|
+
* @param cwd - The package root.
|
|
21
|
+
* @param output - Where to print.
|
|
22
|
+
* @returns The exit code: 1 with one line per problem, 0 otherwise.
|
|
23
|
+
*/
|
|
24
|
+
export declare function runCli(argv: readonly string[], cwd: string, output: CliOutput): ExitCode;
|